Merge commit 'origin/svn-head' into dhewg
[libogc.git] / lwip / netio.c
blob5fda935b02b603dbf51b8f85e22483bf7201df36
1 #include <stdlib.h>
2 #include <string.h>
3 #include <reent.h>
4 #include <errno.h>
5 #undef errno
6 extern int errno;
8 #include <sys/iosupport.h>
9 #include "lwip/ip_addr.h"
10 #include "network.h"
11 int netio_open(struct _reent *r, void *fileStruct, const char *path,int flags,int mode);
12 int netio_close(struct _reent *r,int fd);
13 int netio_write(struct _reent *r,int fd,const char *ptr,size_t len);
14 int netio_read(struct _reent *r,int fd,char *ptr,size_t len);
16 //---------------------------------------------------------------------------------
17 const devoptab_t dotab_stdnet = {
18 //---------------------------------------------------------------------------------
19 "stdnet", // device name
20 0, // size of file structure
21 netio_open, // device open
22 netio_close, // device close
23 netio_write, // device write
24 netio_read, // device read
25 NULL, // device seek
26 NULL, // device fstat
27 NULL, // device stat
28 NULL, // device link
29 NULL, // device unlink
30 NULL, // device chdir
31 NULL, // device rename
32 NULL, // device mkdir
33 0, // dirStateSize
34 NULL, // device diropen_r
35 NULL, // device dirreset_r
36 NULL, // device dirnext_r
37 NULL, // device dirclose_r
38 NULL // device statvfs_r
41 int netio_open(struct _reent *r, void *fileStruct, const char *path,int flags,int mode)
43 char *cport = NULL;
44 int optval = 1,nport = -1,udp_sock = INVALID_SOCKET;
45 struct sockaddr_in name;
46 socklen_t namelen = sizeof(struct sockaddr);
48 if(net_init()==SOCKET_ERROR) return -1;
50 udp_sock = net_socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
51 if(udp_sock==INVALID_SOCKET) return -1;
53 cport = strchr(path,':');
54 if(cport) {
55 *cport++ = '\0';
56 nport = atoi(cport);
58 if(nport==-1) nport = 7; //try to connect to the well known port 7
60 name.sin_addr.s_addr = inet_addr(path);
61 name.sin_port = htons(nport);
62 name.sin_family = AF_INET;
63 if(net_connect(udp_sock,(struct sockaddr*)&name,namelen)==-1) {
64 net_close(udp_sock);
65 return -1;
67 net_setsockopt(udp_sock,IPPROTO_TCP,TCP_NODELAY,&optval,sizeof(optval));
69 return udp_sock;
72 int netio_close(struct _reent *r,int fd)
74 if(fd<0) return -1;
76 net_close(fd);
78 return 0;
81 int netio_write(struct _reent *r,int fd,const char *ptr,size_t len)
83 int ret;
85 if(fd<0) return -1;
87 ret = net_write(fd,(void*)ptr,len);
89 return ret;
92 int netio_read(struct _reent *r,int fd,char *ptr,size_t len)
94 int ret;
96 if(fd<0) return -1;
98 ret = net_read(fd,ptr,len);
100 return ret;