aboutsummaryrefslogtreecommitdiff
path: root/compat/mingw.c
diff options
context:
space:
mode:
authorJohannes Sixt <johannes.sixt@telecom.at>2007-12-26 13:51:18 +0100
committerJohannes Sixt <johannes.sixt@telecom.at>2008-06-26 08:45:09 +0200
commit746fb8574459798d127e3f9cf782ccb8e31c9e45 (patch)
treee1c59cd794cedc573f4a2ee81ae9291277d9bdce /compat/mingw.c
parent87bddba9924045e39e988d9704714db90abd5619 (diff)
downloadgit-746fb8574459798d127e3f9cf782ccb8e31c9e45.tar.gz
git-746fb8574459798d127e3f9cf782ccb8e31c9e45.tar.xz
Windows: Implement wrappers for gethostbyname(), socket(), and connect().
gethostbyname() is the first function that calls into the Winsock library, and it is wrapped only to initialize the library. socket() is wrapped for two reasons: - Windows's socket() creates things that are like low-level file handles, and they must be converted into file descriptors first. - And these handles cannot be used with plain ReadFile()/WriteFile() because they are opened for "overlapped IO". We have to use WSASocket() to create non-overlapped IO sockets. connect() must be wrapped because Windows's connect() expects the low-level sockets, not file descriptors, and we must first unwrap the file descriptor before we can pass it on to Windows's connect(). Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Diffstat (limited to 'compat/mingw.c')
-rw-r--r--compat/mingw.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/compat/mingw.c b/compat/mingw.c
index 2677e7862..d184c582e 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -432,6 +432,52 @@ char **env_setenv(char **env, const char *name)
return env;
}
+/* this is the first function to call into WS_32; initialize it */
+#undef gethostbyname
+struct hostent *mingw_gethostbyname(const char *host)
+{
+ WSADATA wsa;
+
+ if (WSAStartup(MAKEWORD(2,2), &wsa))
+ die("unable to initialize winsock subsystem, error %d",
+ WSAGetLastError());
+ atexit((void(*)(void)) WSACleanup);
+ return gethostbyname(host);
+}
+
+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);
+}
+
#undef rename
int mingw_rename(const char *pold, const char *pnew)
{