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 + 1 file changed, 1 insertion(+) (limited to 'include') 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 -- 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 'include') 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 'include') 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 'include') 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(-) (limited to 'include') 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