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().
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.
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
LIB_SOCKET_NSL
if test "$Windows_API" = yes; then
AC_LIBOBJ([w32/w32io])
+ AC_LIBOBJ([w32/w32sockets])
fi
#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)
{
#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 */
#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"
#include <stdlib.h>
#ifndef _WIN32
#include <pwd.h>
-#include <unistd.h>
#endif
+#include <unistd.h>
#include "misc.h"
#include "version.h"
#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"
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));
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)
*/
#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
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
* 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 */
*/
#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
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) \
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);
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().
*/