From 8aed602855bbc9a5b614515833796830a3437a98 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 14 Apr 2010 02:16:09 +0200 Subject: win32: mingw32/socket.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy --- win32/socket.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 win32/socket.c (limited to 'win32') diff --git a/win32/socket.c b/win32/socket.c new file mode 100644 index 000000000..4ebe2ef92 --- /dev/null +++ b/win32/socket.c @@ -0,0 +1,57 @@ +#include "libbb.h" +#include "win32.h" +#include "strbuf.h" +#include "git.h" + +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); + return error("unable to make a socket file descriptor: %s", + strerror(errno)); + } + 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); +} + +void winsock_init() +{ + static int init = 0; + if (!init) { + WSADATA wsa; + if (WSAStartup(MAKEWORD(2,2), &wsa)) + die("unable to initialize winsock subsystem, error %d", + WSAGetLastError()); + atexit((void(*)(void)) WSACleanup); + init = 1; + } +} + +/* this is the first function to call into WS_32; initialize it */ +#undef gethostbyname +struct hostent *mingw_gethostbyname(const char *host) +{ + winsock_init(); + return gethostbyname(host); +} -- cgit v1.2.3-55-g6feb