]> git.pond.sub.org Git - empserver/commitdiff
Use src/lib/w32/w32sockets.c for client
authorMarkus Armbruster <armbru@pond.sub.org>
Sat, 18 Apr 2009 21:01:23 +0000 (23:01 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Sat, 5 Dec 2009 14:19:36 +0000 (15:19 +0100)
Move client's w32_connect() to w32sockets.c.

Replace w32_recv() and w32_send() by read() and write().

Replace w32_close() by w32_close_function.

Replace call of WSAStartup() in w32_sysdep_init() by
w32_socket_init().

Remove the identical copies of fd_is_socket(),
w32_set_winsock_errno(), w32_socket().

12 files changed:
Make.mk
src/client/Makefile.in
src/client/configure.ac
src/client/expect.c
src/client/host.c
src/client/login.c
src/client/main.c
src/client/play.c
src/client/sysdep_w32.c
src/client/sysdep_w32.h
src/lib/w32/sys/socket.h
src/lib/w32/w32sockets.c

diff --git a/Make.mk b/Make.mk
index afa731c189239236c3a54539cde2c21e6b37c5dc..adaad9a9665dc06c459db3f3caa85a1cf69082ff 100644 (file)
--- a/Make.mk
+++ b/Make.mk
@@ -74,7 +74,8 @@ gamedir := $(localstatedir)/empire
 builtindir := $(datadir)/empire/builtin
 einfodir := $(datadir)/empire/info.nr
 ehtmldir := $(datadir)/empire/info.html
-client/w32 := sys/uio.h w32io.c w32types.h
+client/w32 := arpa/inet.h netdb.h netinet/in.h sys/time.h sys/socket.h \
+sys/uio.h unistd.h w32io.c w32sockets.c w32types.h
 
 # How to substitute Autoconf output variables
 # Recursively expanded so that $@ and $< work.
index 004913511850cc600a1ab9084a05c39a69af6614..54805f509e65b7bebb1a905991041d89ecc26291 100644 (file)
@@ -95,7 +95,12 @@ termlib.$O: misc.h
 version.$O: version.h
 $(obj): config.h
 
-expect.$O: w32/w32types.h
-play.$O: w32/w32types.h
+expect.$O: w32/sys/socket.h w32/unistd.h w32/w32types.h
+host.$O: w32/sys/socket.h w32/netinet/in.h w32/arpa/inet.h w32/netdb.h w32/unistd.h w32/w32types.h
+login.$O: w32/unistd.h w32/w32types.h
+main.$O: w32/unistd.h w32/w32types.h
+play.$O: w32/sys/socket.h w32/unistd.h w32/w32types.h
 ringbuf.$O: w32/sys/uio.h w32/w32types.h
+sysdep_w32.$O: w32/sys/socket.h
 w32/w32io.$O: misc.h w32/sys/uio.h w32/w32types.h
+w32/w32sockets.$O: w32/sys/socket.h w32/unistd.h w32/w32types.h
index 9625e435f213792518c77b886e53cfbe3a0a95ff..0b92095df37ffc6a95364acd0209215a917319a2 100644 (file)
@@ -56,6 +56,7 @@ LIBS="$LIBS_SOCKETS $LIBS"
 LIB_SOCKET_NSL
 if test "$Windows_API" = yes; then
        AC_LIBOBJ([w32/w32io])
+       AC_LIBOBJ([w32/w32sockets])
 fi
 
 
index 63735ffa0cc7b130e317f67ea0b184678b942dad..50aa789161f7278fb1b85f62bfe7334c95951d1e 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#ifdef _WIN32
-#include "w32types.h"
-#else
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <unistd.h>
-#endif
 #include "misc.h"
 #include "proto.h"
 
-#ifdef _WIN32
-#define read(sock, buffer, buf_size) \
-       w32_recv((sock), (buffer), (buf_size), 0)
-#define write(sock, buffer, buf_size) \
-       w32_send((sock), (buffer), (buf_size), 0)
-#endif
-
 int
 recvline(int s, char *buf)
 {
index d7ea626c5442b30ab3ce139b8f4c21cb069f6d27..dde057cd08672e0c0e73950bc0d333dd7a1ad2f6 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#ifndef _WIN32
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <unistd.h>
-#endif
 #include "misc.h"
 
 /* Portability cruft, should become unnecessary eventually */
index 77d1bd83dacbdfc24545b81bd1cfe7b9082bd167..48d4321a5c49ee035366702b5bcce427fe6e758f 100644 (file)
@@ -38,8 +38,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#if !defined(_WIN32)
 #include <unistd.h>
+#ifdef _WIN32
+#include <windows.h>
 #endif
 #include "misc.h"
 #include "proto.h"
index 3df9d3ba4c7a6c532cf5eab95931cff0026a2ce1..dea0a01d7795b95e63343ece92a020b949920d81 100644 (file)
@@ -39,8 +39,8 @@
 #include <stdlib.h>
 #ifndef _WIN32
 #include <pwd.h>
-#include <unistd.h>
 #endif
+#include <unistd.h>
 #include "misc.h"
 #include "version.h"
 
index 313dbeb52ffdeb91a6d945ab27c7f9fb331e69ba..d20e8f4551f1250bdf0ead2603227aa9a0d46655 100644 (file)
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
-#ifndef _WIN32
-#include <sys/select.h>
-#include <unistd.h>
-#else
+#ifdef _WIN32
 #include <process.h>
-#include <io.h>
-#include "w32types.h"
+#include <sys/socket.h>
+#else
+#include <sys/select.h>
 #endif
+#include <unistd.h>
 #include "linebuf.h"
 #include "misc.h"
 #include "proto.h"
@@ -204,7 +203,7 @@ w32_select(int nfds, fd_set *rdfd, fd_set *wrfd, fd_set *errfd, struct timeval*
     default:
        assert(0);
     }
-    sock = W32_FD_TO_SOCKET(sockfd);
+    sock = w32_fd2socket(sockfd);
 
     assert(wrfd->fd_count == 0
           || (wrfd->fd_count == 1 && wrfd->fd_array[0] == (SOCKET)sockfd));
@@ -295,8 +294,6 @@ w32_ring_from_file_or_bounce_buf(struct ring *r, int fd)
     return res;
 }
 #define ring_from_file w32_ring_from_file_or_bounce_buf
-#define read(sock, buffer, buf_size) \
-       w32_recv((sock), (buffer), (buf_size), 0)
 #define select(nfds, rd, wr, error, time) \
        w32_select((nfds), (rd), (wr), (error), (time))
 #define sigemptyset(mask) ((void)0)
index 9fc08f2a6aea08b5cf13d538f0e0f875f8a4ee41..46a213344ae77b4a5220ed7fcd159169f23738b2 100644 (file)
  */
 
 #ifdef _WIN32
-#include <errno.h>
-#include <fcntl.h>
-#include "misc.h"
-
-static int
-fd_is_socket(int fd, SOCKET *sockp)
-{
-    SOCKET sock;
-    WSANETWORKEVENTS ev;
+#include <config.h>
 
-    sock = W32_FD_TO_SOCKET(fd);
-    if (sockp)
-       *sockp = sock;
-    return WSAEnumNetworkEvents(sock, NULL, &ev) == 0;
-}
-
-void
-w32_set_winsock_errno(void)
-{
-  int err = WSAGetLastError();
-  WSASetLastError(0);
-
-  /* Map some WSAE* errors to the runtime library's error codes.  */
-  switch (err)
-    {
-    case WSA_INVALID_HANDLE:
-      errno = EBADF;
-      break;
-    case WSA_NOT_ENOUGH_MEMORY:
-      errno = ENOMEM;
-      break;
-    case WSA_INVALID_PARAMETER:
-      errno = EINVAL;
-      break;
-    case WSAEWOULDBLOCK:
-      errno = EAGAIN;
-      break;
-    case WSAENAMETOOLONG:
-      errno = ENAMETOOLONG;
-      break;
-    case WSAENOTEMPTY:
-      errno = ENOTEMPTY;
-      break;
-    default:
-      errno = (err > 10000 && err < 10025) ? err - 10000 : err;
-      break;
-    }
-}
+#include <stdlib.h>
+#include <windows.h>
+#include "misc.h"
+#include "sys/socket.h"
 
 /*
  * Get user name in the WIN32 environment
@@ -102,15 +60,11 @@ w32_getpw(void)
     return &pwd;
 }
 
-/*
- * Initialize the WIN32 socket library and
- * set up stdout to work around bugs
- */
 void
 w32_sysdep_init(void)
 {
     int err;
-    WSADATA WsaData;
+
     /*
      * stdout is unbuffered under Windows if connected to a character
      * device, and putchar() screws up when printing multibyte strings
@@ -120,107 +74,12 @@ w32_sysdep_init(void)
      * after each prompt is required.
      */
     setvbuf(stdout, NULL, _IOLBF, 4096);
-    err = WSAStartup(MAKEWORD(2, 0), &WsaData);
+
+    err = w32_socket_init();
     if (err != 0) {
        printf("WSAStartup Failed, error code %d\n", err);
        exit(1);
     }
 }
 
-/*
- * POSIX compatible socket() replacement
- */
-#undef socket
-int
-w32_socket(int domain, int type, int protocol)
-{
-    SOCKET sock;
-
-    /*
-     * We have to use WSASocket() to create non-overlapped IO sockets.
-     * Overlapped IO sockets cannot be used with read/write.
-     */
-    sock = WSASocket(domain, type, protocol, NULL, 0, 0);
-    if (sock == INVALID_SOCKET) {
-       w32_set_winsock_errno();
-       return -1;
-    }
-    return W32_SOCKET_TO_FD(sock);
-}
-
-/*
- * POSIX compatible connect() replacement
- */
-#undef connect
-int
-w32_connect(int sockfd, const struct sockaddr *addr, int addrlen)
-{
-    SOCKET sock = W32_FD_TO_SOCKET(sockfd);
-    int result;
-
-    result = connect(sock, addr, addrlen);
-    if (result == SOCKET_ERROR) {
-       /* FIXME map WSAEWOULDBLOCK to EINPROGRESS */
-       w32_set_winsock_errno();
-       return -1;
-    }
-    return result;
-}
-
-/*
- * POSIX compatible recv() replacement
- */
-#undef recv
-int
-w32_recv(int sockfd, void *buffer, size_t buf_size, int flags)
-{
-    SOCKET socket = W32_FD_TO_SOCKET(sockfd);
-    int result;
-
-    result = recv(socket, buffer, buf_size, flags);
-    if (result == SOCKET_ERROR) {
-       w32_set_winsock_errno();
-       return -1;
-    }
-    return result;
-}
-
-/*
- * POSIX compatible send() replacement
- */
-int
-w32_send(int sockfd, const void *buffer, size_t buf_size, int flags)
-{
-    SOCKET socket = W32_FD_TO_SOCKET(sockfd);
-    int result;
-
-    result = send(socket, buffer, buf_size, flags);
-    if (result == SOCKET_ERROR)
-       w32_set_winsock_errno();
-    return result;
-}
-
-/*
- * POSIX compatible close() replacement
- */
-int
-w32_close(int fd)
-{
-    SOCKET sock;
-
-    if (fd_is_socket(fd, &sock)) {
-       if (closesocket(sock)) {
-           w32_set_winsock_errno();
-           return -1;
-       }
-       /*
-        * This always fails because the underlying handle is already
-        * gone, but it closes the fd just fine.
-        */
-       _close(fd);
-       return 0;
-    }
-    return _close(fd);
-}
-
 #endif /* _WIN32 */
index 0ab4c303d465c6d46aeb19ca4a18600ebc6e2e28..890690262f2495038fcf3a962257577efcf1177a 100644 (file)
  */
 
 #ifndef _SYSDEF_W32_H
-#include <getopt.h>
 #include <stdio.h>
-#include <winsock2.h>
-#include <ws2tcpip.h>
-#include <windows.h>
 
 struct passwd {
     char *pw_name;
 };
 
-#define W32_FD_TO_SOCKET(fd) ((SOCKET)_get_osfhandle((fd)))
-#define W32_SOCKET_TO_FD(fh) (_open_osfhandle((long)(fh), O_RDWR | O_BINARY))
-
-extern void w32_set_winsock_errno(void);
-
-extern int w32_recv(int sockfd, void *, size_t, int flags);
-extern int w32_send(int sockfd, const void *, size_t, int flags);
-extern int w32_close(int fd);
-extern int w32_socket(int domain, int type, int protocol);
-extern int w32_connect(int sockfd, const struct sockaddr *, int addrlen);
-
 extern struct passwd *w32_getpw(void);
 extern void w32_sysdep_init(void);
 
-#define recv(sockfd, buffer, buf_size, flags) \
-    w32_recv((sockfd), (buffer), (buf_size), (flags))
-#define close(fd) \
-    w32_close((fd))
-#define socket(domain, type, protocol) \
-    w32_socket((domain), (type), (protocol))
-#define connect(sockfd, addr, addrlen) \
-    w32_connect((sockfd), (addr), (addrlen))
-
 #ifdef _MSC_VER
 #define pclose _pclose
 #define popen _popen
index ac5927acd4461f99555dbb89c619a6004c9444f5..c11daadba5114df085c119b459a3032c9d2da7fc 100644 (file)
@@ -43,6 +43,8 @@ typedef int socklen_t;
     w32_accept((fd), (addr), (addrlen))
 #define bind(fd, name, namelen) \
     w32_bind((fd), (name), (namelen))
+#define connect(fd, addr, addrlen) \
+    w32_connect((fd), (addr), (addrlen))
 #define listen(fd, backlog) \
     w32_listen((fd), (backlog))
 #define setsockopt(fd, level, optname, optval, optlen) \
@@ -54,6 +56,7 @@ typedef int socklen_t;
 
 extern int w32_accept(int fd, struct sockaddr *addr, socklen_t *addrlen);
 extern int w32_bind(int fd, const struct sockaddr *name, socklen_t namelen);
+extern int w32_connect(int fd, const struct sockaddr *addr, socklen_t addrlen);
 extern int w32_listen(int fd, int backlog);
 extern int w32_setsockopt(int fd, int level, int optname,
                          const void *optval, socklen_t optlen);
index 318949d4bae01d737cd23c56267acea58488f8b7..6e90468f964f8d0d7b4d76f7ec2312dde552270b 100644 (file)
@@ -147,6 +147,25 @@ w32_bind(int fd, const struct sockaddr *name, socklen_t namelen)
     SOCKET_FUNCTION(bind(sock, name, namelen));
 }
 
+/*
+ * POSIX compatible connect() replacement
+ */
+#undef connect
+int
+w32_connect(int sockfd, const struct sockaddr *addr, int addrlen)
+{
+    SOCKET sock = W32_FD_TO_SOCKET(sockfd);
+    int result;
+
+    result = connect(sock, addr, addrlen);
+    if (result == SOCKET_ERROR) {
+       /* FIXME map WSAEWOULDBLOCK to EINPROGRESS */
+       w32_set_winsock_errno();
+       return -1;
+    }
+    return result;
+}
+
 /*
  * POSIX equivalent for listen().
  */