Release 940405
[wine/gsoc-2012-control.git] / misc / winsocket.c
blob0d520db709ea5bb3d2dfccf0fc9dd7e649cce7c3
1 /*
2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
4 *
5 * (C) 1993,1994 John Brezak, Erik Bos.
6 */
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14 #include <errno.h>
15 #include <netdb.h>
16 #include <unistd.h>
17 #include "winsock.h"
19 #define DEBUG_WINSOCK
21 /* XXX per task */
22 WORD wsa_errno;
23 int wsa_initted;
25 WORD errno_to_wsaerrno(int errno)
27 switch(errno) {
28 case ENETDOWN:
29 return WSAENETDOWN;
30 case EAFNOSUPPORT:
31 return WSAEAFNOSUPPORT;
32 case EMFILE:
33 return WSAEMFILE;
34 case ENOBUFS:
35 return WSAENOBUFS;
36 case EPROTONOSUPPORT:
37 return EPROTONOSUPPORT;
38 case EPROTOTYPE:
39 return WSAEPROTOTYPE;
40 case EBADF:
41 case ENOTSOCK:
42 return WSAENOTSOCK;
44 default:
45 #ifndef sun
46 #if defined(__FreeBSD__)
47 fprintf(stderr, "winsock: errno_to_wsaerrno translation failure.\n\t: %s (%d)\n",
48 sys_errlist[errno], errno);
49 #else
50 fprintf(stderr, "winsock: errno_to_wsaerrno translation failure.\n\t: %s (%d)\n",
51 strerror[errno], errno);
52 #endif
53 #else
54 fprintf (stderr, "winsock: errno_to_wsaerrno translation failure.\n");
55 #endif
56 return WSAENETDOWN;
60 SOCKET Winsock_accept(SOCKET s, struct sockaddr FAR *addr, int FAR *addrlen)
62 int sock;
64 if ((sock = accept(s, addr, addrlen)) < 0) {
65 wsa_errno = errno_to_wsaerrno(errno);
66 return INVALID_SOCKET;
68 return sock;
71 int Winsock_bind(SOCKET s, struct sockaddr FAR *name, int namelen)
73 if (bind(s, name, namelen) < 0) {
74 wsa_errno = errno_to_wsaerrno(errno);
75 return SOCKET_ERROR;
77 return 0;
80 int Winsock_closesocket(SOCKET s)
82 if (close(s) < 0) {
83 wsa_errno = errno_to_wsaerrno(errno);
84 return SOCKET_ERROR;
86 return 0;
89 int Winsock_connect(SOCKET s, struct sockaddr FAR *name, int namelen)
91 if (connect(s, name, namelen) < 0) {
92 wsa_errno = errno_to_wsaerrno(errno);
93 return SOCKET_ERROR;
95 return 0;
98 int Winsock_getpeername(SOCKET s, struct sockaddr FAR *name, int FAR *namelen)
100 if (getpeername(s, name, namelen) < 0) {
101 wsa_errno = errno_to_wsaerrno(errno);
102 return SOCKET_ERROR;
104 return 0;
107 int Winsock_getsockname(SOCKET s, struct sockaddr FAR *name, int FAR *namelen)
109 if (getsockname(s, name, namelen) < 0) {
110 wsa_errno = errno_to_wsaerrno(errno);
111 return SOCKET_ERROR;
113 return 0;
116 int Winsock_getsockopt(SOCKET s, int loptname, char FAR *optval, int FAR *optlen)
118 if (getsockopt(s, 0, loptname, optval, optlen) < 0) {
119 wsa_errno = errno_to_wsaerrno(errno);
120 return SOCKET_ERROR;
122 return 0;
125 u_long Winsock_htonl(u_long hostlong)
127 return( htonl(hostlong) );
130 u_short Winsock_htons(u_short hostshort)
132 return( htons(hostshort) );
135 u_long Winsock_inet_addr(char FAR *cp)
137 return( inet_addr(cp) );
140 char *Winsock_inet_ntoa(struct in_addr in)
142 char *s;
144 if ((s = inet_ntoa(in)) == NULL) {
145 wsa_errno = errno_to_wsaerrno(errno);
146 return NULL;
148 return s;
151 int Winsock_ioctlsocket(SOCKET s, long cmd, u_long FAR *argp)
153 if (ioctl(s, cmd, argp) < 0) {
154 wsa_errno = errno_to_wsaerrno(errno);
155 return SOCKET_ERROR;
157 return 0;
160 int Winsock_listen(SOCKET s, int backlog)
162 if (listen(s, backlog) < 0) {
163 wsa_errno = errno_to_wsaerrno(errno);
164 return SOCKET_ERROR;
166 return 0;
169 u_long Winsock_ntohl(u_long netlong)
171 return( ntohl(netlong) );
174 u_short Winsock_ntohs(u_short netshort)
176 return( ntohs(netshort) );
179 int Winsock_recv(SOCKET s, char FAR *buf, int len, int flags)
181 int length;
183 if ((length = recv(s, buf, len, flags)) < 0) {
184 wsa_errno = errno_to_wsaerrno(errno);
185 return SOCKET_ERROR;
187 return length;
190 int Winsock_recvfrom(SOCKET s, char FAR *buf, int len, int flags,
191 struct sockaddr FAR *from, int FAR *fromlen)
193 int length;
195 if ((length = recvfrom(s, buf, len, flags, from, fromlen)) < 0) {
196 wsa_errno = errno_to_wsaerrno(errno);
197 return SOCKET_ERROR;
199 return length;
202 int Winsock_select(int nfds, fd_set FAR *readfds, fd_set FAR *writefds,
203 fd_set FAR *exceptfds, struct timeval FAR *timeout)
205 return(select(nfds, readfds, writefds, exceptfds, timeout));
208 int Winsock_send(SOCKET s, char FAR *buf, int len, int flags)
210 int length;
212 if ((length = send(s, buf, len, flags)) < 0) {
213 wsa_errno = errno_to_wsaerrno(errno);
214 return SOCKET_ERROR;
216 return length;
219 int Winsock_sendto(SOCKET s, char FAR *buf, int len, int flags,
220 struct sockaddr FAR *to, int tolen)
222 int length;
224 if ((length = sendto(s, buf, len, flags, to, tolen)) < 0) {
225 wsa_errno = errno_to_wsaerrno(errno);
226 return SOCKET_ERROR;
228 return length;
231 int Winsock_setsockopt(SOCKET s, int level, int optname, const char FAR *optval,
232 int optlen)
234 if (setsockopt(s, level, optname, optval, optlen) < 0) {
235 wsa_errno = errno_to_wsaerrno(errno);
236 return SOCKET_ERROR;
238 return 0;
241 int Winsock_shutdown(SOCKET s, int how)
243 if (shutdown(s, how) < 0) {
244 wsa_errno = errno_to_wsaerrno(errno);
245 return SOCKET_ERROR;
247 return 0;
250 SOCKET Winsock_socket(WORD af, WORD type, WORD protocol)
252 int sock;
254 #ifdef DEBUG_WINSOCK
255 printf("Winsock_socket: af=%d type=%d protocol=%d\n", af, type, protocol);
256 #endif
258 /* let the kernel do the dirty work..
260 if (!wsa_initted) {
261 wsa_errno = WSANOTINITIALISED;
262 return INVALID_SOCKET;
265 if ((sock = socket(af, type, protocol)) < 0) {
266 wsa_errno = errno_to_wsaerrno(errno);
267 return INVALID_SOCKET;
269 return sock;
272 struct hostent *Winsock_gethostbyaddr(const char FAR *addr, int len, int type)
274 struct hostent *host;
276 if ((host = gethostbyaddr(addr, len, type)) == NULL) {
277 wsa_errno = errno_to_wsaerrno(errno);
278 return NULL;
280 return host;
283 struct hostent *Winsock_gethostbyname(const char FAR *name)
285 struct hostent *host;
287 if ((host = gethostbyname(name)) == NULL) {
288 wsa_errno = errno_to_wsaerrno(errno);
289 return NULL;
291 return host;
294 int Winsock_gethostname(char FAR *name, int namelen)
296 if (gethostname(name, namelen) < 0) {
297 wsa_errno = errno_to_wsaerrno(errno);
298 return SOCKET_ERROR;
300 return 0;
303 struct protoent *Winsock_getprotobyname(char FAR *name)
305 struct protoent *proto;
307 if ((proto = getprotobyname(name)) == NULL) {
308 wsa_errno = errno_to_wsaerrno(errno);
309 return NULL;
311 return proto;
314 struct protoent *Winsock_getprotobynumber(int number)
316 struct protoent *proto;
318 if ((proto = getprotobynumber(number)) == NULL) {
319 wsa_errno = errno_to_wsaerrno(errno);
320 return NULL;
322 return proto;
325 struct servent *Winsock_getservbyname(const char FAR *name, const char FAR *proto)
327 struct servent *service;
329 if ((service = getservbyname(name, proto)) == NULL) {
330 wsa_errno = errno_to_wsaerrno(errno);
331 return NULL;
333 return service;
336 struct servent *Winsock_getservbyport(int port, const char FAR *proto)
338 struct servent *service;
340 if ((service = getservbyport(port, proto)) == NULL) {
341 wsa_errno = errno_to_wsaerrno(errno);
342 return NULL;
344 return service;
347 HANDLE WSAAsyncGetHostByAddr(HWND hWnd, u_int wMsg, const char FAR *addr,
348 int len, int type, const char FAR *buf, int buflen)
353 HANDLE WSAAsyncGetHostByName(HWND hWnd, u_int wMsg, const char FAR *name,
354 char FAR *buf, int buflen)
359 HANDLE WSAAsyncGetProtoByName(HWND hWnd, u_int wMsg, const char FAR *name,
360 char FAR *buf, int buflen)
365 HANDLE WSAAsyncGetProtoByNumber(HWND hWnd, u_int wMsg, int number,
366 char FAR *buf, int buflen)
371 HANDLE WSAAsyncGetServByName(HWND hWnd, u_int wMsg, const char FAR *name,
372 const char FAR *proto, char FAR *buf, int buflen)
377 HANDLE WSAAsyncGetServByPort(HWND hWnd, u_int wMsg, int port, const char FAR
378 *proto, char FAR *buf, int buflen)
383 int WSAAsyncSelect(SOCKET s, HWND hWnd, u_int wMsg, long lEvent)
388 int WSAFDIsSet(int fd, fd_set *set)
390 return( FD_ISSET(fd, set) );
393 WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
398 WSACancelBlockingCall ( void )
403 int WSAGetLastError(void)
405 return wsa_errno;
408 void WSASetLastError(int iError)
410 wsa_errno = iError;
413 BOOL WSAIsBlocking (void)
418 FARPROC WSASetBlockingHook(FARPROC lpBlockFunc)
423 int WSAUnhookBlockingHook(void)
428 WSADATA Winsock_data = {
429 0x0101,
430 0x0101,
431 "WINE Sockets",
432 #ifdef linux
433 "LINUX/i386",
434 #endif
435 #ifdef __NetBSD__
436 "NetBSD/i386",
437 #endif
438 #ifdef sunos
439 "SunOS",
440 #endif
441 128,
442 1024,
443 NULL
446 int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
448 #ifdef DEBUG_WINSOCK
449 fprintf(stderr, "WSAStartup: verReq=%x\n", wVersionRequested);
450 #endif
452 if (LOBYTE(wVersionRequested) < 1 ||
453 (LOBYTE(wVersionRequested) == 1 &&
454 HIBYTE(wVersionRequested) < 1))
455 return WSAVERNOTSUPPORTED;
457 if (!lpWSAData)
458 return WSAEINVAL;
460 bcopy(&Winsock_data, lpWSAData, sizeof(Winsock_data));
462 wsa_initted = 1;
464 return(0);
467 int WSACleanup(void)
469 wsa_initted = 0;
470 return 0;