Release 970101
[wine] / include / winsock.h
1 /* WINSOCK.H--definitions to be used with the WINSOCK.DLL
2  *
3  * This header file corresponds to version 1.1 of the Windows Sockets
4  * specification.
5  */
6
7 #ifndef _WINSOCKAPI_
8 #define _WINSOCKAPI_
9
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
12 #include <sys/types.h>
13 #include <sys/time.h>
14 #include <fcntl.h>
15 #include <netdb.h>
16 #include <sys/socket.h>
17 #include "windows.h"
18
19 #pragma pack(1)
20
21 /* Win16 socket-related types */
22
23 typedef UINT16          SOCKET16;
24
25 typedef struct ws_hostent
26 {
27         SEGPTR  h_name;         /* official name of host */
28         SEGPTR  h_aliases;      /* alias list */
29         INT16   h_addrtype;     /* host address type */
30         INT16   h_length;       /* length of address */
31         SEGPTR  h_addr_list;    /* list of addresses from name server */
32 } _ws_hostent;
33
34 typedef struct ws_protoent
35 {
36         SEGPTR  p_name;         /* official protocol name */
37         SEGPTR  p_aliases;      /* alias list */
38         INT16   p_proto;        /* protocol # */
39 } _ws_protoent;
40
41 typedef struct ws_servent 
42 {
43         SEGPTR  s_name;         /* official service name */
44         SEGPTR  s_aliases;      /* alias list */
45         INT16   s_port;         /* port # */
46         SEGPTR  s_proto;        /* protocol to use */
47 } _ws_servent;
48
49 typedef struct ws_netent
50 {
51         SEGPTR  n_name;         /* official name of net */
52         SEGPTR  n_aliases;      /* alias list */
53         INT16   n_addrtype;     /* net address type */
54         INT32   n_net;          /* network # */
55 } _ws_netent;
56
57 typedef struct sockaddr         ws_sockaddr;
58
59 typedef struct
60 {
61         UINT16    fd_count;               /* how many are SET? */
62         SOCKET16  fd_array[FD_SETSIZE];   /* an array of SOCKETs */
63 } ws_fd_set;
64
65 /* ws_fd_set operations */
66
67 INT16   __WSAFDIsSet( SOCKET16, ws_fd_set * );
68
69 #define WS_FD_CLR(fd, set) do { \
70     UINT16 __i; \
71     for (__i = 0; __i < ((ws_fd_set*)(set))->fd_count ; __i++) \
72     { \
73         if (((ws_fd_set*)(set))->fd_array[__i] == fd) \
74         { \
75             while (__i < ((ws_fd_set*)(set))->fd_count-1) \
76             { \
77                 ((ws_fd_set*)(set))->fd_array[__i] = \
78                     ((ws_fd_set*)(set))->fd_array[__i+1]; \
79                 __i++; \
80             } \
81             ((ws_fd_set*)(set))->fd_count--; \
82             break; \
83         } \
84     } \
85 } while(0)
86
87 #define WS_FD_SET(fd, set) do { \
88     if (((ws_fd_set*)(set))->fd_count < FD_SETSIZE) \
89         ((ws_fd_set*)(set))->fd_array[((ws_fd_set*)(set))->fd_count++]=(fd);\
90 } while(0)
91
92 #define WS_FD_ZERO(set) (((ws_fd_set*)(set))->fd_count=0)
93
94 #define WS_FD_ISSET(fd, set) __WSAFDIsSet((SOCKET16)(fd), (ws_fd_set*)(set))
95
96 /* 
97  * Internet address (old style... should be updated) 
98  */
99
100 typedef struct ws_addr_in
101 {
102         union {
103                 struct { BYTE   s_b1,s_b2,s_b3,s_b4; } S_un_b;
104                 struct { UINT16 s_w1,s_w2; } S_un_w;
105                 UINT32 S_addr;
106         } S_un;
107 #define ws_addr  S_un.S_addr            /* can be used for most tcp & ip code */
108 #define ws_host  S_un.S_un_b.s_b2       /* host on imp */
109 #define ws_net   S_un.S_un_b.s_b1       /* network */
110 #define ws_imp   S_un.S_un_w.s_w2       /* imp */
111 #define ws_impno S_un.S_un_b.s_b4       /* imp # */
112 #define ws_lh    S_un.S_un_b.s_b3       /* logical host */
113 } _ws_in_addr;
114
115 typedef struct ws_sockaddr_in
116 {
117         INT16           sin_family;
118         UINT16          sin_port;
119         _ws_in_addr     sin_addr;
120         char            sin_zero[8];
121 } _ws_sockaddr_in;
122
123 #define WSADESCRIPTION_LEN      256
124 #define WSASYS_STATUS_LEN       128
125
126 typedef struct WSAData {
127         WORD                    wVersion;
128         WORD                    wHighVersion;
129         char                    szDescription[WSADESCRIPTION_LEN+1];
130         char                    szSystemStatus[WSASYS_STATUS_LEN+1];
131         UINT16                  iMaxSockets;
132         UINT16                  iMaxUdpDg;
133         SEGPTR                  lpVendorInfo;
134 } WSADATA, *LPWSADATA;
135
136 #pragma pack(4)
137
138 /* ----------------------------------- no Win16 structure defs beyond this line! */
139
140 /*
141  * This is used instead of -1, since the
142  * SOCKET type is unsigned.
143  */
144 #define INVALID_SOCKET  (SOCKET16)(~0)
145 #define SOCKET_ERROR              (-1)
146
147 /*
148  * Types
149  */
150 #define WS_SOCK_STREAM     1               /* stream socket */
151 #define WS_SOCK_DGRAM      2               /* datagram socket */
152 #define WS_SOCK_RAW        3               /* raw-protocol interface */
153 #define WS_SOCK_RDM        4               /* reliably-delivered message */
154 #define WS_SOCK_SEQPACKET  5               /* sequenced packet stream */
155
156 #define WS_SOL_SOCKET           (-1)
157 #define WS_IPPROTO_TCP          6
158
159 /*
160  * Option flags per-socket.
161  */
162 #define WS_SO_DEBUG        0x0001          /* turn on debugging info recording */
163 #define WS_SO_ACCEPTCONN   0x0002          /* socket has had listen() */
164 #define WS_SO_REUSEADDR    0x0004          /* allow local address reuse */
165 #define WS_SO_KEEPALIVE    0x0008          /* keep connections alive */
166 #define WS_SO_DONTROUTE    0x0010          /* just use interface addresses */
167 #define WS_SO_BROADCAST    0x0020          /* permit sending of broadcast msgs */
168 #define WS_SO_USELOOPBACK  0x0040          /* bypass hardware when possible */
169 #define WS_SO_LINGER       0x0080          /* linger on close if data present */
170 #define WS_SO_OOBINLINE    0x0100          /* leave received OOB data in line */
171
172 #define WS_SO_DONTLINGER   (UINT16)(~WS_SO_LINGER)
173
174 /*
175  * Additional options.
176  */
177 #define WS_SO_SNDBUF       0x1001          /* send buffer size */
178 #define WS_SO_RCVBUF       0x1002          /* receive buffer size */
179 #define WS_SO_SNDLOWAT     0x1003          /* send low-water mark */
180 #define WS_SO_RCVLOWAT     0x1004          /* receive low-water mark */
181 #define WS_SO_SNDTIMEO     0x1005          /* send timeout */
182 #define WS_SO_RCVTIMEO     0x1006          /* receive timeout */
183 #define WS_SO_ERROR        0x1007          /* get error status and clear */
184 #define WS_SO_TYPE         0x1008          /* get socket type */
185
186 #define WS_IOCPARM_MASK    0x7f            /* parameters must be < 128 bytes */
187 #define WS_IOC_VOID        0x20000000      /* no parameters */
188 #define WS_IOC_OUT         0x40000000      /* copy out parameters */
189 #define WS_IOC_IN          0x80000000      /* copy in parameters */
190 #define WS_IOC_INOUT       (WS_IOC_IN|WS_IOC_OUT)
191 #define WS_IOR(x,y,t)      (WS_IOC_OUT|(((UINT32)sizeof(t)&WS_IOCPARM_MASK)<<16)|((x)<<8)|(y))
192 #define WS_IOW(x,y,t)      (WS_IOC_IN|(((UINT32)sizeof(t)&WS_IOCPARM_MASK)<<16)|((x)<<8)|(y))
193
194 /*
195  * Socket I/O flags (supported by spec 1.1)
196  */
197
198 #define WS_FIONREAD    WS_IOR('f', 127, u_long)
199 #define WS_FIONBIO     WS_IOW('f', 126, u_long)
200
201 #define WS_SIOCATMARK  WS_IOR('s',  7, u_long)
202
203 /*
204  * Maximum queue length specifiable by listen.
205  */
206 #ifdef SOMAXCONN
207 #undef SOMAXCONN
208 #endif
209 #define SOMAXCONN       5
210
211 #ifndef MSG_DONTROUTE
212 #define MSG_DONTROUTE   0x4             /* send without using routing tables */
213 #endif
214 #define MSG_MAXIOVLEN   16
215
216 /*
217  * Define constant based on rfc883, used by gethostbyxxxx() calls.
218  */
219 #define MAXGETHOSTSTRUCT        1024
220
221 /*
222  * Define flags to be used with the WSAAsyncSelect() call.
223  */
224 #define WS_FD_READ         0x01
225 #define WS_FD_WRITE        0x02
226 #define WS_FD_OOB          0x04
227 #define WS_FD_ACCEPT       0x08
228 #define WS_FD_CONNECT      0x10
229 #define WS_FD_CLOSE        0x20
230
231 #define WS_FD_NONBLOCK     0x10000000   /* internal per-socket flags */
232 #define WS_FD_INACTIVE     0x20000000
233 #define WS_FD_CONNECTED    0x40000000
234 #define WS_FD_INTERNAL     0xFFFF0000
235
236 /*
237  * All Windows Sockets error constants are biased by WSABASEERR from
238  * the "normal"
239  */
240 #define WSABASEERR              10000
241 /*
242  * Windows Sockets definitions of regular Microsoft C error constants
243  */
244 #define WSAEINTR                (WSABASEERR+4)
245 #define WSAEBADF                (WSABASEERR+9)
246 #define WSAEACCES               (WSABASEERR+13)
247 #define WSAEFAULT               (WSABASEERR+14)
248 #define WSAEINVAL               (WSABASEERR+22)
249 #define WSAEMFILE               (WSABASEERR+24)
250
251 /*
252  * Windows Sockets definitions of regular Berkeley error constants
253  */
254 #define WSAEWOULDBLOCK          (WSABASEERR+35)
255 #define WSAEINPROGRESS          (WSABASEERR+36)
256 #define WSAEALREADY             (WSABASEERR+37)
257 #define WSAENOTSOCK             (WSABASEERR+38)
258 #define WSAEDESTADDRREQ         (WSABASEERR+39)
259 #define WSAEMSGSIZE             (WSABASEERR+40)
260 #define WSAEPROTOTYPE           (WSABASEERR+41)
261 #define WSAENOPROTOOPT          (WSABASEERR+42)
262 #define WSAEPROTONOSUPPORT      (WSABASEERR+43)
263 #define WSAESOCKTNOSUPPORT      (WSABASEERR+44)
264 #define WSAEOPNOTSUPP           (WSABASEERR+45)
265 #define WSAEPFNOSUPPORT         (WSABASEERR+46)
266 #define WSAEAFNOSUPPORT         (WSABASEERR+47)
267 #define WSAEADDRINUSE           (WSABASEERR+48)
268 #define WSAEADDRNOTAVAIL        (WSABASEERR+49)
269 #define WSAENETDOWN             (WSABASEERR+50)
270 #define WSAENETUNREACH          (WSABASEERR+51)
271 #define WSAENETRESET            (WSABASEERR+52)
272 #define WSAECONNABORTED         (WSABASEERR+53)
273 #define WSAECONNRESET           (WSABASEERR+54)
274 #define WSAENOBUFS              (WSABASEERR+55)
275 #define WSAEISCONN              (WSABASEERR+56)
276 #define WSAENOTCONN             (WSABASEERR+57)
277 #define WSAESHUTDOWN            (WSABASEERR+58)
278 #define WSAETOOMANYREFS         (WSABASEERR+59)
279 #define WSAETIMEDOUT            (WSABASEERR+60)
280 #define WSAECONNREFUSED         (WSABASEERR+61)
281 #define WSAELOOP                (WSABASEERR+62)
282 #define WSAENAMETOOLONG         (WSABASEERR+63)
283 #define WSAEHOSTDOWN            (WSABASEERR+64)
284 #define WSAEHOSTUNREACH         (WSABASEERR+65)
285 #define WSAENOTEMPTY            (WSABASEERR+66)
286 #define WSAEPROCLIM             (WSABASEERR+67)
287 #define WSAEUSERS               (WSABASEERR+68)
288 #define WSAEDQUOT               (WSABASEERR+69)
289 #define WSAESTALE               (WSABASEERR+70)
290 #define WSAEREMOTE              (WSABASEERR+71)
291
292 /*
293  * Extended Windows Sockets error constant definitions
294  */
295 #define WSASYSNOTREADY          (WSABASEERR+91)
296 #define WSAVERNOTSUPPORTED      (WSABASEERR+92)
297 #define WSANOTINITIALISED       (WSABASEERR+93)
298
299 /*
300  * Error return codes from gethostbyname() and gethostbyaddr()
301  * (when using the resolver). Note that these errors are
302  * retrieved via WSAGetLastError() and must therefore follow
303  * the rules for avoiding clashes with error numbers from
304  * specific implementations or language run-time systems.
305  * For this reason the codes are based at WSABASEERR+1001.
306  * Note also that [WSA]NO_ADDRESS is defined only for
307  * compatibility purposes.
308  */
309
310 /* #define h_errno         WSAGetLastError() */
311
312 /* Authoritative Answer: Host not found */
313 #define WSAHOST_NOT_FOUND       (WSABASEERR+1001)
314
315 /* Non-Authoritative: Host not found, or SERVERFAIL */
316 #define WSATRY_AGAIN            (WSABASEERR+1002)
317
318 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
319 #define WSANO_RECOVERY          (WSABASEERR+1003)
320
321 /* Valid name, no data record of requested type */
322 #define WSANO_DATA              (WSABASEERR+1004)
323
324 /* no address, look for MX record */
325 #define WSANO_ADDRESS           WSANO_DATA
326
327 /* Socket function prototypes */
328
329 #ifdef __cplusplus
330 extern "C" {
331 #endif
332
333 /* Microsoft Windows Extension function prototypes */
334
335 INT16     WSAStartup(UINT16 wVersionRequired, LPWSADATA lpWSAData);
336 INT16     WSACleanup(void);
337 void      WSASetLastError(INT16 iError);
338 INT16     WSAGetLastError(void);
339 BOOL16    WSAIsBlocking(void);
340 INT16     WSAUnhookBlockingHook(void);
341 FARPROC16 WSASetBlockingHook(FARPROC16 lpBlockFunc);
342 INT16     WSACancelBlockingCall(void);
343 HANDLE16  WSAAsyncGetServByName(HWND16 hWnd, UINT16 wMsg,
344                                 LPCSTR name, LPCSTR proto,
345                                 SEGPTR buf, INT16 buflen);
346
347 HANDLE16  WSAAsyncGetServByPort(HWND16 hWnd, UINT16 wMsg, INT16 port,
348                                 LPCSTR proto, SEGPTR buf, INT16 buflen);
349
350 HANDLE16  WSAAsyncGetProtoByName(HWND16 hWnd, UINT16 wMsg,
351                                  LPCSTR name, SEGPTR buf, INT16 buflen);
352
353 HANDLE16  WSAAsyncGetProtoByNumber(HWND16 hWnd, UINT16 wMsg,
354                                    INT16 number, SEGPTR buf, INT16 buflen);
355
356 HANDLE16  WSAAsyncGetHostByName(HWND16 hWnd, UINT16 wMsg,
357                                 LPCSTR name, SEGPTR buf, INT16 buflen);
358
359 HANDLE16  WSAAsyncGetHostByAddr(HWND16 hWnd, UINT16 wMsg, LPCSTR addr, INT16 len,
360                                 INT16 type, SEGPTR buf, INT16 buflen);
361
362 INT16     WSACancelAsyncRequest(HANDLE16 hAsyncTaskHandle);
363 INT16     WSAAsyncSelect(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, UINT32 lEvent);
364
365 #ifdef __cplusplus
366 }
367 #endif
368
369 /* Microsoft Windows Extended data types */
370 typedef struct sockaddr SOCKADDR, *PSOCKADDR, *LPSOCKADDR;
371 typedef struct sockaddr_in SOCKADDR_IN, *PSOCKADDR_IN, *LPSOCKADDR_IN;
372 typedef struct linger LINGER, *PLINGER, *LPLINGER;
373 typedef struct in_addr IN_ADDR, *PIN_ADDR, *LPIN_ADDR;
374 typedef struct fd_set FD_SET, *PFD_SET, *LPFD_SET;
375 typedef struct hostent HOSTENT, *PHOSTENT, *LPHOSTENT;
376 typedef struct servent SERVENT, *PSERVENT, *LPSERVENT;
377 typedef struct protoent PROTOENT, *PPROTOENT, *LPPROTOENT;
378 typedef struct timeval TIMEVAL, *PTIMEVAL, *LPTIMEVAL;
379
380 /*
381  * Windows message parameter composition and decomposition
382  * macros.
383  *
384  * WSAMAKEASYNCREPLY is intended for use by the Windows Sockets implementation
385  * when constructing the response to a WSAAsyncGetXByY() routine.
386  */
387 #define WSAMAKEASYNCREPLY(buflen,error)     MAKELONG(buflen,error)
388 /*
389  * WSAMAKESELECTREPLY is intended for use by the Windows Sockets implementation
390  * when constructing the response to WSAAsyncSelect().
391  */
392 #define WSAMAKESELECTREPLY(event,error)     MAKELONG(event,error)
393 /*
394  * WSAGETASYNCBUFLEN is intended for use by the Windows Sockets application
395  * to extract the buffer length from the lParam in the response
396  * to a WSAGetXByY().
397  */
398 #define WSAGETASYNCBUFLEN(lParam)           LOWORD(lParam)
399 /*
400  * WSAGETASYNCERROR is intended for use by the Windows Sockets application
401  * to extract the error code from the lParam in the response
402  * to a WSAGetXByY().
403  */
404 #define WSAGETASYNCERROR(lParam)            HIWORD(lParam)
405 /*
406  * WSAGETSELECTEVENT is intended for use by the Windows Sockets application
407  * to extract the event code from the lParam in the response
408  * to a WSAAsyncSelect().
409  */
410 #define WSAGETSELECTEVENT(lParam)           LOWORD(lParam)
411 /*
412  * WSAGETSELECTERROR is intended for use by the Windows Sockets application
413  * to extract the error code from the lParam in the response
414  * to a WSAAsyncSelect().
415  */
416 #define WSAGETSELECTERROR(lParam)           HIWORD(lParam)
417
418 /* ----------------------------------- internal structures */
419
420 #define WS_DUP_NATIVE           0x0
421 #define WS_DUP_OFFSET           0x2
422 #define WS_DUP_SEGPTR           0x4
423
424 #define AOP_IO                  0x0000001       /* aop_control paramaters */
425
426 #define AOP_CONTROL_REMOVE      0x0000000       /* aop_control return values */
427 #define AOP_CONTROL_KEEP        0x0000001
428
429 typedef struct  __aop
430 {
431   /* AOp header */
432
433   struct __aop *next, *prev;
434   int           fd[2];                          /* pipe */
435   int   (*aop_control)(struct __aop*, int);     /* SIGIO handler */
436   pid_t         pid;                            /* child process pid */
437
438   /* custom data */
439
440   HWND16        hWnd;
441   UINT16        uMsg;
442
443   unsigned      flags;
444   SEGPTR        buffer_base;
445   int           buflen;
446 } ws_async_op;
447
448 #define WSMSG_ASYNC_SELECT      0x0000001
449 #define WSMSG_ASYNC_HOSTBYNAME  0x0000010
450 #define WSMSG_ASYNC_HOSTBYADDR  0x0000020
451 #define WSMSG_ASYNC_PROTOBYNAME 0x0000100
452 #define WSMSG_ASYNC_PROTOBYNUM  0x0000200
453 #define WSMSG_ASYNC_SERVBYNAME  0x0001000
454 #define WSMSG_ASYNC_SERVBYPORT  0x0002000
455
456 #define MTYPE_PARENT            0x50505357
457 #define MTYPE_CLIENT            0x50435357
458
459 #pragma pack(1)
460 typedef struct
461 {
462   long          mtype;          /* WSMSG_... */
463
464   UINT32        lParam;         /* WS_FD_... event */
465   UINT16        wParam;         /* socket handle - used only for MTYPE_CLIENT messages */
466 } ipc_packet;
467
468 #define MTYPE_PARENT_SIZE \
469         (sizeof(UINT32))
470 #define MTYPE_CLIENT_SIZE \
471         (sizeof(ipc_packet) - sizeof(long))
472 #pragma pack(4)
473
474 typedef struct
475 {
476   int                   fd;
477   unsigned              flags;
478   ws_async_op*          p_aop;  /* AsyncSelect() handler */
479 } ws_socket;
480
481 typedef struct
482 {
483   ws_async_op*  ws_aop;         /* init'ed for getXbyY */
484   ws_socket*    ws_sock;        /* init'ed for AsyncSelect */
485   int           lEvent;
486   int           lLength;
487   char*         buffer;
488   char*         init;
489   ipc_packet    ip;
490 } ws_async_ctl;
491
492 #define WS_MAX_SOCKETS_PER_THREAD       16
493 #define WS_MAX_UDP_DATAGRAM             1024
494
495 #define WSI_BLOCKINGCALL        0x00000001      /* per-thread info flags */
496
497 typedef struct __WSINFO
498 {
499   struct __WSINFO*      prev,*next;
500
501   unsigned              flags;
502   int                   errno;
503   INT16                 num_startup;
504   INT16                 num_async_rq;
505   INT16                 last_free;
506   UINT16                buflen;
507   char*                 buffer;                 /* allocated from SEGPTR heap */
508   char*                 dbuffer;                /* buffer for dummies (32 bytes) */
509
510   ws_socket             sock[WS_MAX_SOCKETS_PER_THREAD];
511   FARPROC16             blocking_hook;
512   HTASK16               tid;                    /* owning thread id - better switch
513                                                  * to TLS when it gets fixed */
514 } WSINFO, *LPWSINFO;
515
516 int WS_dup_he(LPWSINFO pwsi, struct hostent* p_he, int flag);
517 int WS_dup_pe(LPWSINFO pwsi, struct protoent* p_pe, int flag);
518 int WS_dup_se(LPWSINFO pwsi, struct servent* p_se, int flag);
519
520 void WS_do_async_gethost(LPWSINFO, unsigned);
521 void WS_do_async_getproto(LPWSINFO, unsigned);
522 void WS_do_async_getserv(LPWSINFO, unsigned);
523
524 int WINSOCK_async_io(int fd, int async);
525 int WINSOCK_unblock_io(int fd, int noblock);
526
527 int  WINSOCK_check_async_op(ws_async_op* p_aop);
528 void WINSOCK_link_async_op(ws_async_op* p_aop);
529 void WINSOCK_unlink_async_op(ws_async_op* p_aop);
530 void WINSOCK_cancel_async_op(HTASK16 tid);
531 void WINSOCK_do_async_select(void);
532 void WINSOCK_Shutdown(void);
533
534 UINT16 wsaErrno(void);
535 UINT16 wsaHerrno(void);
536
537 #endif  /* _WINSOCKAPI_ */
538