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() --- include/mingw.h | 1 + win32/Kbuild | 1 + win32/net.c | 11 +++++++++++ 3 files changed, 13 insertions(+) create mode 100644 win32/net.c diff --git a/include/mingw.h b/include/mingw.h index 53a0a0d67..d61d87bf3 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -15,6 +15,7 @@ typedef int pid_t; */ static inline unsigned int git_ntohl(unsigned int x) { return (unsigned int)ntohl(x); } #define ntohl git_ntohl +int inet_aton(const char *cp, struct in_addr *inp); /* * fcntl.h 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(+) 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(-) 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(+) 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 From d57f58814a25264aeab875a501d3eeeca12fd3f8 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 15 Sep 2010 17:51:22 +1000 Subject: win32: re-enable xconnect.c --- include/libbb.h | 2 -- include/mingw.h | 13 +++++++++++++ libbb/Kbuild.src | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index 31e733d77..01aee206e 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -484,7 +484,6 @@ void xlisten(int s, int backlog) FAST_FUNC; void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen) FAST_FUNC; ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to, socklen_t tolen) FAST_FUNC; -#if !ENABLE_PLATFORM_MINGW32 /* SO_REUSEADDR allows a server to rebind to an address that is already * "in use" by old connections to e.g. previous server instance which is * killed or crashed. Without it bind will fail until all such connections @@ -597,7 +596,6 @@ ssize_t recv_from_to(int fd, void *buf, size_t len, int flags, struct sockaddr *from, struct sockaddr *to, socklen_t sa_size) FAST_FUNC; -#endif char *xstrdup(const char *s) FAST_FUNC RETURNS_MALLOC; diff --git a/include/mingw.h b/include/mingw.h index 1caf65464..a7352bdfb 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -49,6 +49,19 @@ static inline void endgrent(void) {} #define NAME_MAX 255 #define MAXSYMLINKS 20 +/* + * netdb.h + */ + +typedef int sa_family_t; + +/* + * linux/un.h + */ +struct sockaddr_un { + sa_family_t sun_family; + char sun_path[1]; /* to make compiler happy, don't bother */ +}; /* * poll.h diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src index f30fa4818..9ea3d735d 100644 --- a/libbb/Kbuild.src +++ b/libbb/Kbuild.src @@ -100,6 +100,7 @@ lib-y += wfopen.o lib-y += wfopen_input.o lib-y += write.o lib-y += xatonum.o +lib-y += xconnect.o lib-y += xfunc_die.o lib-y += xfuncs.o lib-y += xfuncs_printf.o @@ -118,7 +119,6 @@ lib-$(CONFIG_PLATFORM_POSIX) += read_key.o lib-$(CONFIG_PLATFORM_POSIX) += safe_gethostname.o lib-$(CONFIG_PLATFORM_POSIX) += signals.o lib-$(CONFIG_PLATFORM_POSIX) += udp_io.o -lib-$(CONFIG_PLATFORM_POSIX) += xconnect.o lib-$(CONFIG_PLATFORM_POSIX) += xgethostbyname.o lib-$(CONFIG_FEATURE_UTMP) += utmp.o -- cgit v1.2.3-55-g6feb From 1a92e511e178a2be3d2c66479bd0b6fa113f26da Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Sat, 18 Sep 2010 08:55:52 +1000 Subject: wget: flush HTTP request before receving reply --- networking/wget.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/networking/wget.c b/networking/wget.c index 88bb09eb2..4521abfcf 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -737,6 +737,8 @@ int wget_main(int argc UNUSED_PARAM, char **argv) fprintf(sfp, /* "Connection: close\r\n" */ "\r\n"); } + fflush(sfp); + /* * Retrieve HTTP response line and check for "200" status code. */ -- cgit v1.2.3-55-g6feb From 7b7adf5fd030387571782bd6222a62a623556961 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Tue, 13 Apr 2010 22:05:41 +0200 Subject: win32: enable wget --- networking/wget.c | 1 + scripts/defconfig.mingw32 | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/networking/wget.c b/networking/wget.c index 4521abfcf..5b0907e11 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -579,6 +579,7 @@ int wget_main(int argc UNUSED_PARAM, char **argv) #endif INIT_G(); + IF_WIN32_NET(init_winsock();) #if ENABLE_FEATURE_WGET_LONG_OPTIONS applet_long_options = wget_longopts; diff --git a/scripts/defconfig.mingw32 b/scripts/defconfig.mingw32 index 943c3f4ab..89e08ced5 100644 --- a/scripts/defconfig.mingw32 +++ b/scripts/defconfig.mingw32 @@ -811,10 +811,10 @@ CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS=0 CONFIG_IFUPDOWN_UDHCPC_CMD_OPTIONS="" # CONFIG_UDPSVD is not set # CONFIG_VCONFIG is not set -# CONFIG_WGET is not set +CONFIG_WGET=y # CONFIG_FEATURE_WGET_STATUSBAR is not set # CONFIG_FEATURE_WGET_AUTHENTICATION is not set -# CONFIG_FEATURE_WGET_LONG_OPTIONS is not set +CONFIG_FEATURE_WGET_LONG_OPTIONS=y # CONFIG_FEATURE_WGET_TIMEOUT is not set # CONFIG_ZCIP is not set -- cgit v1.2.3-55-g6feb