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.
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
25 WORD
errno_to_wsaerrno(int errno
)
31 return WSAEAFNOSUPPORT
;
37 return EPROTONOSUPPORT
;
46 #if defined(__FreeBSD__)
47 fprintf(stderr
, "winsock: errno_to_wsaerrno translation failure.\n\t: %s (%d)\n",
48 sys_errlist
[errno
], errno
);
50 fprintf(stderr
, "winsock: errno_to_wsaerrno translation failure.\n\t: %s (%d)\n",
51 strerror
[errno
], errno
);
54 fprintf (stderr
, "winsock: errno_to_wsaerrno translation failure.\n");
60 SOCKET
Winsock_accept(SOCKET s
, struct sockaddr FAR
*addr
, int FAR
*addrlen
)
64 if ((sock
= accept(s
, addr
, addrlen
)) < 0) {
65 wsa_errno
= errno_to_wsaerrno(errno
);
66 return INVALID_SOCKET
;
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
);
80 int Winsock_closesocket(SOCKET s
)
83 wsa_errno
= errno_to_wsaerrno(errno
);
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
);
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
);
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
);
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
);
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
)
144 if ((s
= inet_ntoa(in
)) == NULL
) {
145 wsa_errno
= errno_to_wsaerrno(errno
);
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
);
160 int Winsock_listen(SOCKET s
, int backlog
)
162 if (listen(s
, backlog
) < 0) {
163 wsa_errno
= errno_to_wsaerrno(errno
);
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
)
183 if ((length
= recv(s
, buf
, len
, flags
)) < 0) {
184 wsa_errno
= errno_to_wsaerrno(errno
);
190 int Winsock_recvfrom(SOCKET s
, char FAR
*buf
, int len
, int flags
,
191 struct sockaddr FAR
*from
, int FAR
*fromlen
)
195 if ((length
= recvfrom(s
, buf
, len
, flags
, from
, fromlen
)) < 0) {
196 wsa_errno
= errno_to_wsaerrno(errno
);
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
)
212 if ((length
= send(s
, buf
, len
, flags
)) < 0) {
213 wsa_errno
= errno_to_wsaerrno(errno
);
219 int Winsock_sendto(SOCKET s
, char FAR
*buf
, int len
, int flags
,
220 struct sockaddr FAR
*to
, int tolen
)
224 if ((length
= sendto(s
, buf
, len
, flags
, to
, tolen
)) < 0) {
225 wsa_errno
= errno_to_wsaerrno(errno
);
231 int Winsock_setsockopt(SOCKET s
, int level
, int optname
, const char FAR
*optval
,
234 if (setsockopt(s
, level
, optname
, optval
, optlen
) < 0) {
235 wsa_errno
= errno_to_wsaerrno(errno
);
241 int Winsock_shutdown(SOCKET s
, int how
)
243 if (shutdown(s
, how
) < 0) {
244 wsa_errno
= errno_to_wsaerrno(errno
);
250 SOCKET
Winsock_socket(WORD af
, WORD type
, WORD protocol
)
255 printf("Winsock_socket: af=%d type=%d protocol=%d\n", af
, type
, protocol
);
258 /* let the kernel do the dirty work..
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
;
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
);
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
);
294 int Winsock_gethostname(char FAR
*name
, int namelen
)
296 if (gethostname(name
, namelen
) < 0) {
297 wsa_errno
= errno_to_wsaerrno(errno
);
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
);
314 struct protoent
*Winsock_getprotobynumber(int number
)
316 struct protoent
*proto
;
318 if ((proto
= getprotobynumber(number
)) == NULL
) {
319 wsa_errno
= errno_to_wsaerrno(errno
);
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
);
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
);
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)
408 void WSASetLastError(int iError
)
413 BOOL
WSAIsBlocking (void)
418 FARPROC
WSASetBlockingHook(FARPROC lpBlockFunc
)
423 int WSAUnhookBlockingHook(void)
428 WSADATA Winsock_data
= {
446 int WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
449 fprintf(stderr
, "WSAStartup: verReq=%x\n", wVersionRequested
);
452 if (LOBYTE(wVersionRequested
) < 1 ||
453 (LOBYTE(wVersionRequested
) == 1 &&
454 HIBYTE(wVersionRequested
) < 1))
455 return WSAVERNOTSUPPORTED
;
460 bcopy(&Winsock_data
, lpWSAData
, sizeof(Winsock_data
));