-
Notifications
You must be signed in to change notification settings - Fork 1
/
netio.c
101 lines (82 loc) · 2.37 KB
/
netio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdlib.h>
#include <string.h>
#include <reent.h>
#include <errno.h>
#undef errno
extern int errno;
#include <sys/iosupport.h>
#include "lwip/ip_addr.h"
#include "network.h"
int netio_open(struct _reent *r, void *fileStruct, const char *path,int flags,int mode);
int netio_close(struct _reent *r,int fd);
int netio_write(struct _reent *r,int fd,const char *ptr,size_t len);
int netio_read(struct _reent *r,int fd,char *ptr,size_t len);
//---------------------------------------------------------------------------------
const devoptab_t dotab_stdnet = {
//---------------------------------------------------------------------------------
"stdnet", // device name
0, // size of file structure
netio_open, // device open
netio_close, // device close
netio_write, // device write
netio_read, // device read
NULL, // device seek
NULL, // device fstat
NULL, // device stat
NULL, // device link
NULL, // device unlink
NULL, // device chdir
NULL, // device rename
NULL, // device mkdir
0, // dirStateSize
NULL, // device diropen_r
NULL, // device dirreset_r
NULL, // device dirnext_r
NULL, // device dirclose_r
NULL // device statvfs_r
};
int netio_open(struct _reent *r, void *fileStruct, const char *path,int flags,int mode)
{
char *cport = NULL;
int optval = 1,nport = -1,udp_sock = INVALID_SOCKET;
struct sockaddr_in name;
socklen_t namelen = sizeof(struct sockaddr);
if(net_init()==SOCKET_ERROR) return -1;
udp_sock = net_socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(udp_sock==INVALID_SOCKET) return -1;
cport = strchr(path,':');
if(cport) {
*cport++ = '\0';
nport = atoi(cport);
}
if(nport==-1) nport = 7; //try to connect to the well known port 7
name.sin_addr.s_addr = inet_addr(path);
name.sin_port = htons(nport);
name.sin_family = AF_INET;
if(net_connect(udp_sock,(struct sockaddr*)&name,namelen)==-1) {
net_close(udp_sock);
return -1;
}
net_setsockopt(udp_sock,IPPROTO_TCP,TCP_NODELAY,&optval,sizeof(optval));
return udp_sock;
}
int netio_close(struct _reent *r,int fd)
{
if(fd<0) return -1;
net_close(fd);
return 0;
}
int netio_write(struct _reent *r,int fd,const char *ptr,size_t len)
{
int ret;
if(fd<0) return -1;
ret = net_write(fd,(void*)ptr,len);
return ret;
}
int netio_read(struct _reent *r,int fd,char *ptr,size_t len)
{
int ret;
if(fd<0) return -1;
ret = net_read(fd,ptr,len);
return ret;
}