From 7dc41624d037b85b8498c3a679415a74e00b90cf Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 15 Sep 2010 18:17:43 +1000 Subject: win32: implement inet_aton() --- win32/Kbuild | 1 + win32/net.c | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 win32/net.c (limited to 'win32') diff --git a/win32/Kbuild b/win32/Kbuild index a4a7f32d4..bc005c23a 100644 --- a/win32/Kbuild +++ b/win32/Kbuild @@ -9,5 +9,6 @@ lib-$(CONFIG_PLATFORM_MINGW32) += fnmatch.o lib-$(CONFIG_PLATFORM_MINGW32) += mingw.o lib-$(CONFIG_PLATFORM_MINGW32) += process.o lib-$(CONFIG_PLATFORM_MINGW32) += regex.o +lib-$(CONFIG_WIN32_NET) += net.o lib-$(CONFIG_PLATFORM_MINGW32) += termios.o lib-$(CONFIG_PLATFORM_MINGW32) += winansi.o diff --git a/win32/net.c b/win32/net.c new file mode 100644 index 000000000..93e19ae5c --- /dev/null +++ b/win32/net.c @@ -0,0 +1,11 @@ +#include "libbb.h" + +int inet_aton(const char *cp, struct in_addr *inp) +{ + unsigned long val = inet_addr(cp); + + if (val == INADDR_NONE) + return 0; + inp->S_un.S_addr = val; + return 1; +} -- cgit v1.2.3-55-g6feb From b34e416fa9d712bb4138911a2536ee709490d96b Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 15 Sep 2010 18:20:16 +1000 Subject: win32: add init_winsock() All network applets must call this function before using any winsock functions. --- include/mingw.h | 1 + win32/net.c | 9 +++++++++ 2 files changed, 10 insertions(+) (limited to 'win32') diff --git a/include/mingw.h b/include/mingw.h index d61d87bf3..d9a2fabd1 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -346,3 +346,4 @@ void free_environ(char **env); char **env_setenv(char **env, const char *name); const char *get_busybox_exec_path(void); +void init_winsock(void); diff --git a/win32/net.c b/win32/net.c index 93e19ae5c..82c29e57c 100644 --- a/win32/net.c +++ b/win32/net.c @@ -9,3 +9,12 @@ int inet_aton(const char *cp, struct in_addr *inp) inp->S_un.S_addr = val; return 1; } + +void init_winsock(void) +{ + WSADATA wsa; + if (WSAStartup(MAKEWORD(2,2), &wsa)) + bb_error_msg_and_die("unable to initialize winsock subsystem, error %d", + WSAGetLastError()); + atexit((void(*)(void)) WSACleanup); /* may conflict with other atexit? */ +} -- cgit v1.2.3-55-g6feb From 52748f68338010d28979c653ad17ca1f14d3df1a Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 15 Sep 2010 18:22:43 +1000 Subject: win32: reimplement socket() --- include/mingw.h | 6 +++++- win32/net.c | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) (limited to 'win32') diff --git a/include/mingw.h b/include/mingw.h index d9a2fabd1..b2d73ce1b 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -174,7 +174,11 @@ NOIMPL(ioctl,int fd UNUSED_PARAM, int code UNUSED_PARAM,...); */ #define hstrerror strerror -NOIMPL(mingw_socket,int domain UNUSED_PARAM, int type UNUSED_PARAM, int protocol UNUSED_PARAM); +#ifdef CONFIG_WIN32_NET +int mingw_socket(int domain, int type, int protocol); + +# define socket mingw_socket +#endif NOIMPL(mingw_sendto,SOCKET s UNUSED_PARAM, const char *buf UNUSED_PARAM, int len UNUSED_PARAM, int flags UNUSED_PARAM, const struct sockaddr *sa UNUSED_PARAM, int salen UNUSED_PARAM); NOIMPL(mingw_listen,SOCKET s UNUSED_PARAM,int backlog UNUSED_PARAM); NOIMPL(mingw_bind,SOCKET s UNUSED_PARAM,const struct sockaddr* sa UNUSED_PARAM,int salen UNUSED_PARAM); diff --git a/win32/net.c b/win32/net.c index 82c29e57c..98204b99c 100644 --- a/win32/net.c +++ b/win32/net.c @@ -18,3 +18,30 @@ void init_winsock(void) WSAGetLastError()); atexit((void(*)(void)) WSACleanup); /* may conflict with other atexit? */ } + +int mingw_socket(int domain, int type, int protocol) +{ + int sockfd; + SOCKET s = WSASocket(domain, type, protocol, NULL, 0, 0); + if (s == INVALID_SOCKET) { + /* + * WSAGetLastError() values are regular BSD error codes + * biased by WSABASEERR. + * However, strerror() does not know about networking + * specific errors, which are values beginning at 38 or so. + * Therefore, we choose to leave the biased error code + * in errno so that _if_ someone looks up the code somewhere, + * then it is at least the number that are usually listed. + */ + errno = WSAGetLastError(); + return -1; + } + /* convert into a file descriptor */ + if ((sockfd = _open_osfhandle(s, O_RDWR|O_BINARY)) < 0) { + closesocket(s); + bb_error_msg("unable to make a socket file descriptor: %s", + strerror(errno)); + return -1; + } + return sockfd; +} -- cgit v1.2.3-55-g6feb From 7483649514ed2049deaf59077dbb77c915ba22a0 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 15 Sep 2010 18:25:18 +1000 Subject: win32: reimplement connect() connect() now returns a socket handle, i.e. a HANDLE. This kind of handle works with read()/write(). But on the other hand, FILE* functions are dead because they are not crt file handles?? --- include/mingw.h | 2 ++ win32/net.c | 7 +++++++ 2 files changed, 9 insertions(+) (limited to 'win32') diff --git a/include/mingw.h b/include/mingw.h index b2d73ce1b..1caf65464 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -176,8 +176,10 @@ NOIMPL(ioctl,int fd UNUSED_PARAM, int code UNUSED_PARAM,...); #ifdef CONFIG_WIN32_NET int mingw_socket(int domain, int type, int protocol); +int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz); # define socket mingw_socket +# define connect mingw_connect #endif NOIMPL(mingw_sendto,SOCKET s UNUSED_PARAM, const char *buf UNUSED_PARAM, int len UNUSED_PARAM, int flags UNUSED_PARAM, const struct sockaddr *sa UNUSED_PARAM, int salen UNUSED_PARAM); NOIMPL(mingw_listen,SOCKET s UNUSED_PARAM,int backlog UNUSED_PARAM); diff --git a/win32/net.c b/win32/net.c index 98204b99c..eadda6b69 100644 --- a/win32/net.c +++ b/win32/net.c @@ -45,3 +45,10 @@ int mingw_socket(int domain, int type, int protocol) } return sockfd; } + +#undef connect +int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz) +{ + SOCKET s = (SOCKET)_get_osfhandle(sockfd); + return connect(s, sa, sz); +} -- cgit v1.2.3-55-g6feb