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