2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
5 * (C) 1993,1994 John Brezak, Erik Bos.
11 #include <sys/types.h>
13 #include <sys/ioctl.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
26 static WORD wsa_errno;
27 static int wsa_initted;
28 static key_t wine_key = 0;
29 static FARPROC BlockFunction;
30 static fd_set fd_in_use;
42 #define IPC_PACKET_SIZE (sizeof(struct ipc_packet) - sizeof(long))
43 #define MTYPE 0xb0b0eb05
44 #define WINE_PACKED __attribute__ ((packed))
47 char *h_name WINE_PACKED; /* official name of host */
48 char **h_aliases WINE_PACKED; /* alias list */
49 int h_addrtype WINE_PACKED; /* host address type */
50 int h_length WINE_PACKED; /* length of address */
51 char **h_addr_list WINE_PACKED; /* list of addresses from name server */
57 char *p_name WINE_PACKED; /* official protocol name */
58 char **p_aliases WINE_PACKED; /* alias list */
59 int p_proto WINE_PACKED; /* protocol # */
63 char *s_name WINE_PACKED; /* official service name */
64 char **s_aliases WINE_PACKED; /* alias list */
65 int s_port WINE_PACKED; /* port # */
66 char *s_proto WINE_PACKED; /* protocol to use */
72 struct WIN_hostent hostent_addr;
73 struct WIN_hostent hostent_name;
74 struct WIN_protoent protoent_name;
75 struct WIN_protoent protoent_number;
76 struct WIN_servent servent_name;
77 struct WIN_servent servent_port;
79 struct WIN_hostent WSAhostent_addr;
80 struct WIN_hostent WSAhostent_name;
81 struct WIN_protoent WSAprotoent_name;
82 struct WIN_protoent WSAprotoent_number;
83 struct WIN_servent WSAservent_name;
84 struct WIN_servent WSAservent_port;
86 static struct WinSockHeap *heap;
90 #define dump_sockaddr(a) \
91 fprintf(stderr, "sockaddr_in: family %d, address %s, port %d\n", \
92 ((struct sockaddr_in *)a)->sin_family, \
93 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
94 ntohs(((struct sockaddr_in *)a)->sin_port))
96 static WORD wsaerrno(void)
100 #if defined(__FreeBSD__)
101 fprintf(stderr, "winsock: errno %d, (%s).\n",
102 errno, sys_errlist[errno]);
104 fprintf(stderr, "winsock: errno %d, (%s).\n",
105 errno, strerror(errno));
108 fprintf(stderr, "winsock: errno %d\n", errno);
114 case EINTR: return WSAEINTR;
115 case EACCES: return WSAEACCES;
116 case EFAULT: return WSAEFAULT;
117 case EINVAL: return WSAEINVAL;
118 case EMFILE: return WSAEMFILE;
119 case EWOULDBLOCK: return WSAEWOULDBLOCK;
120 case EINPROGRESS: return WSAEINPROGRESS;
121 case EALREADY: return WSAEALREADY;
123 case ENOTSOCK: return WSAENOTSOCK;
124 case EDESTADDRREQ: return WSAEDESTADDRREQ;
125 case EMSGSIZE: return WSAEMSGSIZE;
126 case EPROTOTYPE: return WSAEPROTOTYPE;
127 case ENOPROTOOPT: return WSAENOPROTOOPT;
128 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
129 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
130 case EOPNOTSUPP: return WSAEOPNOTSUPP;
131 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
132 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
133 case EADDRINUSE: return WSAEADDRINUSE;
134 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
135 case ENETDOWN: return WSAENETDOWN;
136 case ENETUNREACH: return WSAENETUNREACH;
137 case ENETRESET: return WSAENETRESET;
138 case ECONNABORTED: return WSAECONNABORTED;
139 case ECONNRESET: return WSAECONNRESET;
140 case ENOBUFS: return WSAENOBUFS;
141 case EISCONN: return WSAEISCONN;
142 case ENOTCONN: return WSAENOTCONN;
143 case ESHUTDOWN: return WSAESHUTDOWN;
144 case ETOOMANYREFS: return WSAETOOMANYREFS;
145 case ETIMEDOUT: return WSAETIMEDOUT;
146 case ECONNREFUSED: return WSAECONNREFUSED;
147 case ELOOP: return WSAELOOP;
148 case ENAMETOOLONG: return WSAENAMETOOLONG;
149 case EHOSTDOWN: return WSAEHOSTDOWN;
150 case EHOSTUNREACH: return WSAEHOSTUNREACH;
151 case ENOTEMPTY: return WSAENOTEMPTY;
153 case EPROCLIM: return WSAEPROCLIM;
155 case EUSERS: return WSAEUSERS;
156 case EDQUOT: return WSAEDQUOT;
157 case ESTALE: return WSAESTALE;
158 case EREMOTE: return WSAEREMOTE;
161 fprintf(stderr, "winsock: unknown errorno %d!\n", errno);
162 return WSAEOPNOTSUPP;
166 static void errno_to_wsaerrno(void)
168 wsa_errno = wsaerrno();
171 static void convert_sockopt(INT *level, INT *optname)
173 /* $%#%!@! why couldn't they use the same values for both winsock and unix ? */
179 case 0x01: *optname = SO_DEBUG;
181 case 0x04: *optname = SO_REUSEADDR;
183 case 0x08: *optname = SO_KEEPALIVE;
185 case 0x10: *optname = SO_DONTROUTE;
187 case 0x20: *optname = SO_BROADCAST;
189 case 0x80: *optname = SO_LINGER;
191 case 0x100: *optname = SO_OOBINLINE;
193 case 0x1001: *optname = SO_SNDBUF;
195 case 0x1002: *optname = SO_RCVBUF;
197 case 0x1007: *optname = SO_ERROR;
199 case 0x1008: *optname = SO_TYPE;
202 fprintf(stderr, "convert_sockopt() unknown optname %d\n", *optname);
206 case 6: *optname = IPPROTO_TCP;
211 static void CONVERT_HOSTENT(struct WIN_hostent *heap, struct hostent *host)
216 static void CONVERT_PROTOENT(struct WIN_protoent *heap, struct protoent *proto)
221 static void CONVERT_SERVENT(struct WIN_servent *heap, struct servent *serv)
226 #define CONVERT_HOSTENT(a,b) memcpy(a, &b, sizeof(a))
227 #define CONVERT_PROTOENT(a,b) memcpy(a, &b, sizeof(a))
228 #define CONVERT_SERVENT(a,b) memcpy(a, &b, sizeof(a))
231 SOCKET WINSOCK_accept(SOCKET s, struct sockaddr *addr, INT *addrlen)
235 dprintf_winsock(stddeb, "WSA_accept: socket %d, ptr %8x, length %d\n", s, (int) addr, *addrlen);
237 if ((sock = accept(s, addr, (int *) addrlen)) < 0) {
239 return INVALID_SOCKET;
244 INT WINSOCK_bind(SOCKET s, struct sockaddr *name, INT namelen)
246 dprintf_winsock(stddeb, "WSA_bind: socket %d, ptr %8x, length %d\n", s, (int) name, namelen);
249 if (bind(s, name, namelen) < 0) {
256 INT WINSOCK_closesocket(SOCKET s)
258 dprintf_winsock(stddeb, "WSA_closesocket: socket %d\n", s);
260 FD_CLR(s, &fd_in_use);
269 INT WINSOCK_connect(SOCKET s, struct sockaddr *name, INT namelen)
271 dprintf_winsock(stddeb, "WSA_connect: socket %d, ptr %8x, length %d\n", s, (int) name, namelen);
274 if (connect(s, name, namelen) < 0) {
281 INT WINSOCK_getpeername(SOCKET s, struct sockaddr *name, INT *namelen)
283 dprintf_winsock(stddeb, "WSA_getpeername: socket: %d, ptr %8x, ptr %8x\n", s, (int) name, *namelen);
286 if (getpeername(s, name, (int *) namelen) < 0) {
293 INT WINSOCK_getsockname(SOCKET s, struct sockaddr *name, INT *namelen)
295 dprintf_winsock(stddeb, "WSA_getsockname: socket: %d, ptr %8x, ptr %8x\n", s, (int) name, (int) *namelen);
296 if (getsockname(s, name, (int *) namelen) < 0) {
304 WINSOCK_getsockopt(SOCKET s, INT level, INT optname, char *optval, INT *optlen)
306 dprintf_winsock(stddeb, "WSA_getsockopt: socket: %d, opt %d, ptr %8x, ptr %8x\n", s, level, (int) optval, (int) *optlen);
307 convert_sockopt(&level, &optname);
309 if (getsockopt(s, (int) level, optname, optval, (int *) optlen) < 0) {
316 u_long WINSOCK_htonl(u_long hostlong)
318 return( htonl(hostlong) );
321 u_short WINSOCK_htons(u_short hostshort)
323 return( htons(hostshort) );
326 u_long WINSOCK_inet_addr(char *cp)
328 return( inet_addr(cp) );
331 char *WINSOCK_inet_ntoa(struct in_addr in)
335 /* dprintf_winsock(stddeb, "WSA_inet_ntoa: %8lx\n", (int) in);*/
337 if ((s = inet_ntoa(in)) == NULL) {
342 strncpy(heap->ntoa_buffer, s, sizeof(heap->ntoa_buffer) );
344 return (char *) &heap->ntoa_buffer;
347 INT WINSOCK_ioctlsocket(SOCKET s, long cmd, u_long *argp)
349 dprintf_winsock(stddeb, "WSA_ioctl: socket %d, cmd %ld, ptr %8x\n", s, cmd, (int) argp);
351 if (ioctl(s, cmd, argp) < 0) {
358 INT WINSOCK_listen(SOCKET s, INT backlog)
360 dprintf_winsock(stddeb, "WSA_listen: socket %d, backlog %d\n", s, backlog);
362 if (listen(s, backlog) < 0) {
369 u_long WINSOCK_ntohl(u_long netlong)
371 return( ntohl(netlong) );
374 u_short WINSOCK_ntohs(u_short netshort)
376 return( ntohs(netshort) );
379 INT WINSOCK_recv(SOCKET s, char *buf, INT len, INT flags)
383 dprintf_winsock(stddeb, "WSA_recv: socket %d, ptr %8x, length %d, flags %d\n", s, (int) buf, len, flags);
385 if ((length = recv(s, buf, len, flags)) < 0) {
392 INT WINSOCK_recvfrom(SOCKET s, char *buf, INT len, INT flags,
393 struct sockaddr *from, int *fromlen)
397 dprintf_winsock(stddeb, "WSA_recvfrom: socket %d, ptr %8lx, length %d, flags %d\n", s, (unsigned long)buf, len, flags);
399 if ((length = recvfrom(s, buf, len, flags, from, fromlen)) < 0) {
406 INT WINSOCK_select(INT nfds, fd_set *readfds, fd_set *writefds,
407 fd_set *exceptfds, struct timeval *timeout)
409 dprintf_winsock(stddeb, "WSA_select: fd # %d, ptr %8lx, ptr %8lx, ptr %8lX\n", nfds, (unsigned long) readfds, (unsigned long) writefds, (unsigned long) exceptfds);
411 return(select(nfds, readfds, writefds, exceptfds, timeout));
414 INT WINSOCK_send(SOCKET s, char *buf, INT len, INT flags)
418 dprintf_winsock(stddeb, "WSA_send: socket %d, ptr %8lx, length %d, flags %d\n", s, (unsigned long) buf, len, flags);
420 if ((length = send(s, buf, len, flags)) < 0) {
427 INT WINSOCK_sendto(SOCKET s, char *buf, INT len, INT flags,
428 struct sockaddr *to, INT tolen)
432 dprintf_winsock(stddeb, "WSA_sendto: socket %d, ptr %8lx, length %d, flags %d\n", s, (unsigned long) buf, len, flags);
434 if ((length = sendto(s, buf, len, flags, to, tolen)) < 0) {
441 INT WINSOCK_setsockopt(SOCKET s, INT level, INT optname, const char *optval,
444 dprintf_winsock(stddeb, "WSA_setsockopt: socket %d, level %d, opt %d, ptr %8x, len %d\n", s, level, optname, (int) optval, optlen);
445 convert_sockopt(&level, &optname);
447 if (setsockopt(s, level, optname, optval, optlen) < 0) {
454 INT WINSOCK_shutdown(SOCKET s, INT how)
456 dprintf_winsock(stddeb, "WSA_shutdown: socket s %d, how %d\n", s, how);
458 if (shutdown(s, how) < 0) {
465 SOCKET WINSOCK_socket(INT af, INT type, INT protocol)
469 dprintf_winsock(stddeb, "WSA_socket: af=%d type=%d protocol=%d\n", af, type, protocol);
471 if ((sock = socket(af, type, protocol)) < 0) {
473 dprintf_winsock(stddeb, "WSA_socket: failed !\n");
474 return INVALID_SOCKET;
478 wsa_errno = WSAEMFILE;
479 return INVALID_SOCKET;
482 FD_SET(sock, &fd_in_use);
484 dprintf_winsock(stddeb, "WSA_socket: fd %d\n", sock);
488 struct WIN_hostent *WINSOCK_gethostbyaddr(const char *addr, INT len, INT type)
490 struct hostent *host;
492 dprintf_winsock(stddeb, "WSA_gethostbyaddr: ptr %8x, len %d, type %d\n", (int) addr, len, type);
494 if ((host = gethostbyaddr(addr, len, type)) == NULL) {
498 CONVERT_HOSTENT(&heap->hostent_addr, host);
500 return &heap->hostent_addr;
503 struct WIN_hostent *WINSOCK_gethostbyname(const char *name)
505 struct hostent *host;
507 dprintf_winsock(stddeb, "WSA_gethostbyname: %s\n", name);
509 if ((host = gethostbyname(name)) == NULL) {
513 CONVERT_HOSTENT(&heap->hostent_name, host);
515 return &heap->hostent_name;
518 INT WINSOCK_gethostname(char *name, INT namelen)
520 dprintf_winsock(stddeb, "WSA_gethostname: name %s, len %d\n", name, namelen);
522 if (gethostname(name, namelen) < 0) {
529 struct WIN_protoent *WINSOCK_getprotobyname(char *name)
531 struct protoent *proto;
533 dprintf_winsock(stddeb, "WSA_getprotobyname: name %s\n", name);
535 if ((proto = getprotobyname(name)) == NULL) {
539 CONVERT_PROTOENT(&heap->protoent_name, proto);
541 return &heap->protoent_name;
544 struct WIN_protoent *WINSOCK_getprotobynumber(INT number)
546 struct protoent *proto;
548 dprintf_winsock(stddeb, "WSA_getprotobynumber: num %d\n", number);
550 if ((proto = getprotobynumber(number)) == NULL) {
554 CONVERT_PROTOENT(&heap->protoent_number, proto);
556 return &heap->protoent_number;
559 struct WIN_servent *WINSOCK_getservbyname(const char *name, const char *proto)
561 struct servent *service;
566 dprintf_winsock(stddeb, "WSA_getservbyname: name %s, proto %s\n", name, proto);
568 if ((service = getservbyname(name, proto)) == NULL) {
572 CONVERT_SERVENT(&heap->servent_name, service);
574 return &heap->servent_name;
577 struct WIN_servent *WINSOCK_getservbyport(INT port, const char *proto)
579 struct servent *service;
581 dprintf_winsock(stddeb, "WSA_getservbyport: port %d, name %s\n", port, proto);
583 if ((service = getservbyport(port, proto)) == NULL) {
587 CONVERT_SERVENT(&heap->servent_port, service);
589 return &heap->servent_port;
592 /******************** winsock specific functions ************************
595 static HANDLE new_handle = 0;
597 static HANDLE AllocWSAHandle(void)
602 static void recv_message(int sig)
604 struct ipc_packet message;
606 if (msgrcv(wine_key, &message, IPC_PACKET_SIZE, MTYPE, IPC_NOWAIT) == -1)
607 perror("wine: msgrcv");
610 "WSA: PostMessage (hwnd %d, wMsg %d, wParam %d, lParam %ld)\n",
616 PostMessage(message.hWnd, message.wMsg, message.handle, message.lParam);
618 signal(SIGUSR1, recv_message);
622 static void send_message(HANDLE handle, HWND hWnd, u_int wMsg, long lParam)
624 struct ipc_packet message;
626 message.mtype = MTYPE;
627 message.handle = handle;
630 message.lParam = lParam;
633 "WSA: send (hwnd %d, wMsg %d, handle %d, lParam %ld)\n",
634 hWnd, wMsg, handle, lParam);
636 if (msgsnd(wine_key, &message, IPC_PACKET_SIZE, IPC_NOWAIT) == -1)
637 perror("wine: msgsnd");
639 kill(getppid(), SIGUSR1);
643 HANDLE WSAAsyncGetHostByAddr(HWND hWnd, u_int wMsg, const char *addr,
644 INT len, INT type, char *buf, INT buflen)
647 struct hostent *host;
649 handle = AllocWSAHandle();
654 if ((host = gethostbyaddr(addr, len, type)) == NULL) {
655 send_message(hWnd, wMsg, handle, wsaerrno() << 16);
658 memcpy(buf, host, buflen);
659 send_message(hWnd, wMsg, handle, 0);
665 HANDLE WSAAsyncGetHostByName(HWND hWnd, u_int wMsg, const char *name,
666 char *buf, INT buflen)
669 struct hostent *host;
671 handle = AllocWSAHandle();
676 if ((host = gethostbyname(name)) == NULL) {
677 send_message(hWnd, wMsg, handle, wsaerrno() << 16);
680 memcpy(buf, host, buflen);
681 send_message(hWnd, wMsg, handle, 0);
687 HANDLE WSAAsyncGetProtoByName(HWND hWnd, u_int wMsg, const char *name,
688 char *buf, INT buflen)
691 struct protoent *proto;
693 handle = AllocWSAHandle();
698 if ((proto = getprotobyname(name)) == NULL) {
699 send_message(hWnd, wMsg, handle, wsaerrno() << 16);
702 memcpy(buf, proto, buflen);
703 send_message(hWnd, wMsg, handle, 0);
709 HANDLE WSAAsyncGetProtoByNumber(HWND hWnd, u_int wMsg, INT number,
710 char *buf, INT buflen)
713 struct protoent *proto;
715 handle = AllocWSAHandle();
720 if ((proto = getprotobynumber(number)) == NULL) {
721 send_message(hWnd, wMsg, handle, wsaerrno() << 16);
724 memcpy(buf, proto, buflen);
725 send_message(hWnd, wMsg, handle, 0);
731 HANDLE WSAAsyncGetServByName(HWND hWnd, u_int wMsg, const char *name,
732 const char *proto, char *buf, INT buflen)
735 struct servent *service;
737 handle = AllocWSAHandle();
742 if ((service = getservbyname(name, proto)) == NULL) {
743 send_message(hWnd, wMsg, handle, wsaerrno() << 16);
746 memcpy(buf, service, buflen);
747 send_message(hWnd, wMsg, handle, 0);
753 HANDLE WSAAsyncGetServByPort(HWND hWnd, u_int wMsg, INT port, const char
754 *proto, char *buf, INT buflen)
757 struct servent *service;
759 handle = AllocWSAHandle();
764 if ((service = getservbyport(port, proto)) == NULL) {
765 send_message(hWnd, wMsg, handle, wsaerrno() << 16);
768 memcpy(buf, service, buflen);
769 send_message(hWnd, wMsg, handle, 0);
774 INT WSAAsyncSelect(SOCKET s, HWND hWnd, u_int wMsg, long lEvent)
777 fd_set read_fds, write_fds, except_fds;
779 dprintf_winsock(stddeb, "WSA_AsyncSelect: socket %d, HWND %d, wMsg %d, event %ld\n", s, hWnd, wMsg, lEvent);
781 /* remove outstanding asyncselect() processes */
784 if (wMsg == 0 && lEvent == 0)
793 FD_ZERO(&except_fds);
795 if (lEvent & FD_READ)
796 FD_SET(s, &read_fds);
797 if (lEvent & FD_WRITE)
798 FD_SET(s, &write_fds);
800 fcntl(s, F_SETFL, O_NONBLOCK);
801 select(s + 1, &read_fds, &write_fds, &except_fds, NULL);
804 if (FD_ISSET(s, &read_fds))
806 if (FD_ISSET(s, &write_fds))
809 send_message(hWnd, wMsg, s, (wsaerrno() << 16) | event);
814 INT WSAFDIsSet(INT fd, fd_set *set)
816 return( FD_ISSET(fd, set) );
819 INT WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
821 dprintf_winsock(stddeb, "WSA_AsyncRequest: handle %d\n", hAsyncTaskHandle);
826 INT WSACancelBlockingCall(void)
828 dprintf_winsock(stddeb, "WSA_CancelBlockCall\n");
832 INT WSAGetLastError(void)
834 dprintf_winsock(stddeb, "WSA_GetLastError\n");
839 void WSASetLastError(INT iError)
841 dprintf_winsock(stddeb, "WSA_SetLastErorr %d\n", iError);
846 BOOL WSAIsBlocking(void)
848 dprintf_winsock(stddeb, "WSA_IsBlocking\n");
853 FARPROC WSASetBlockingHook(FARPROC lpBlockFunc)
855 dprintf_winsock(stddeb, "WSA_SetBlockHook %8lx, STUB!\n", (unsigned long) lpBlockFunc);
856 BlockFunction = lpBlockFunc;
858 return (FARPROC) lpBlockFunc;
861 INT WSAUnhookBlockingHook(void)
863 dprintf_winsock(stddeb, "WSA_UnhookBlockingHook\n");
864 BlockFunction = NULL;
869 WSADATA WINSOCK_data = {
887 INT WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
891 dprintf_winsock(stddeb, "WSAStartup: verReq=%x\n", wVersionRequested);
893 if (LOBYTE(wVersionRequested) < 1 ||
894 (LOBYTE(wVersionRequested) == 1 &&
895 HIBYTE(wVersionRequested) < 1))
896 return WSAVERNOTSUPPORTED;
901 /* alloc winsock heap */
903 if ((HeapHandle = GlobalAlloc(GMEM_FIXED,sizeof(struct WinSockHeap))) == 0)
904 return WSASYSNOTREADY;
906 heap = (struct WinSockHeap *) GlobalLock(HeapHandle);
907 bcopy(&WINSOCK_data, lpWSAData, sizeof(WINSOCK_data));
911 if ((wine_key = msgget(IPC_PRIVATE, 0600)) == -1)
912 perror("wine: msgget");
914 signal(SIGUSR1, recv_message);
929 if (msgctl(wine_key, IPC_RMID, NULL) == -1)
930 perror("wine: shmctl");
932 for (fd = 0; fd != FD_SETSIZE; fd++)
933 if (FD_ISSET(fd, &fd_in_use))