rename project to visftp
[oftp] / watt32 / tcp.h
1 /*!\file inc/tcp.h
2  * Watt-32 public API.
3  */
4
5 /*
6  * Waterloo TCP
7  *
8  * Copyright (c) 1990-1993 Erick Engelke
9  *
10  * Portions copyright others, see copyright.h for details.
11  *
12  * This library is free software; you can use it or redistribute under
13  * the terms of the license included in LICENSE.H.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * file LICENSE.H for more details.
19  *
20  */
21
22 #ifndef __WATT_TCP_H
23 #define __WATT_TCP_H
24
25 /*
26  * Version (major.minor.dev-rel), 8-bit each.
27  */
28 #define WATTCP_MAJOR_VER  2
29 #define WATTCP_MINOR_VER  2
30 #define WATTCP_DEVEL_REL  10
31
32 #define WATTCP_VER  ((WATTCP_MAJOR_VER << 16) + \
33                      (WATTCP_MINOR_VER << 8) +  \
34                      WATTCP_DEVEL_REL)
35
36 #define WATTCP_VER_STRING  "2.2.10"
37
38 #if !defined(RC_INVOKED)
39
40 #include <stdio.h>
41 #include <sys/wtypes.h>  /* fd_set, iovec */
42 #include <sys/wtime.h>   /* struct timeval, cdecl */
43 #include <sys/swap.h>    /* intel(), intel16() */
44 #include <sys/w32api.h>  /* W32_FUNC, W32_DATA etc. */
45
46 #ifdef __WATCOMC__
47   #pragma read_only_directory;
48 #endif
49
50 #ifdef __cplusplus
51   extern "C" {
52   #if 0  /* keep Emacs's auto-indent happy */
53   }
54   #endif
55 #endif
56
57 W32_DATA const char *wattcpCopyright;  /* "See COPYRIGHT.H for details" */
58
59 W32_FUNC const char *wattcpVersion (void);      /* WatTCP target version/date */
60 W32_FUNC const char *wattcpCapabilities (void); /* what's been compiled in */
61
62 #if !defined(WATT32_BUILD)
63
64 /*
65  * Typedefs and constants
66  */
67 #ifndef BYTE
68 #define BYTE   unsigned char
69 #endif
70
71 #ifndef WORD
72 #define WORD   unsigned short
73 #endif
74
75 #ifndef DWORD
76 #define DWORD  unsigned long
77 #endif
78
79 #ifndef BOOL
80 #define BOOL   int
81 #endif
82
83 #ifndef sock_type
84 #define sock_type  void
85 #endif
86
87 /*
88  * Old compatibility
89  */
90 #ifndef byte
91 #define byte  unsigned char
92 #endif
93
94 #ifndef word
95 #define word  unsigned short
96 #endif
97
98 #ifndef dword
99 #define dword unsigned long
100 #endif
101
102 #ifndef longword
103 #define longword unsigned long
104 #endif
105
106 /*
107  * Basic typedefs
108  */
109 typedef BYTE  eth_address[6];
110 typedef BYTE  ip6_address[16];
111
112 typedef int (*ProtoHandler) (void *sock, const BYTE *data, unsigned len,
113                              const void *tcp_phdr, const void *udp_hdr);
114
115 typedef int (*UserHandler)  (void *sock);
116
117 #endif /* WATT32_BUILD */
118
119
120 typedef struct {
121         BYTE   undoc [4470];
122       } tcp_Socket;
123
124 typedef struct {
125         BYTE   undoc [1740];
126       } udp_Socket;
127
128
129 /* Silly C++ compilers needs this to supress warning at max warning level
130  */
131 typedef void (*VoidProc)(void);
132
133 #define MAX_COOKIES      10
134 #define MAX_NAMESERVERS  10
135 #define MAX_HOSTLEN      80
136
137 /* Modes for sock_mode()
138  */
139 #define TCP_MODE_BINARY  0x01   /* deprecated */
140 #define TCP_MODE_ASCII   0x02   /* deprecated */
141 #define SOCK_MODE_BINARY 0x01   /* new name */
142 #define SOCK_MODE_ASCII  0x02   /* new name */
143 #define UDP_MODE_CHK     0x04   /* defaults to checksum */
144 #define UDP_MODE_NOCHK   0x08
145 #define TCP_MODE_NAGLE   0x10   /* Nagle's algorithm */
146 #define TCP_MODE_NONAGLE 0x20
147
148 /* wait-states for sock_sselect()
149  */
150 #define SOCKESTABLISHED  1
151 #define SOCKDATAREADY    2
152 #define SOCKCLOSED       4
153
154 /*
155  * Hide "private" symbols by prefixing with "_w32_"
156  */
157 #undef  W32_NAMESPACE
158 #define W32_NAMESPACE(x)   _w32_ ## x
159
160 #define init_misc          W32_NAMESPACE (init_misc)
161 #define Random             W32_NAMESPACE (Random)
162 #define set_timeout        W32_NAMESPACE (set_timeout)
163 #define chk_timeout        W32_NAMESPACE (chk_timeout)
164 #define cmp_timeout        W32_NAMESPACE (cmp_timeout)
165 #define hires_timer        W32_NAMESPACE (hires_timer)
166 #define set_timediff       W32_NAMESPACE (set_timediff)
167 #define get_timediff       W32_NAMESPACE (get_timediff)
168 #define timeval_diff       W32_NAMESPACE (timeval_diff)
169
170 #define my_ip_addr         W32_NAMESPACE (my_ip_addr)
171 #define sin_mask           W32_NAMESPACE (sin_mask)
172 #define sock_delay         W32_NAMESPACE (sock_delay)
173 #define sock_inactive      W32_NAMESPACE (sock_inactive)
174 #define sock_data_timeout  W32_NAMESPACE (sock_data_timeout)
175 #define multihomes         W32_NAMESPACE (multihomes)
176 #define block_tcp          W32_NAMESPACE (block_tcp)
177 #define block_udp          W32_NAMESPACE (block_udp)
178 #define block_ip           W32_NAMESPACE (block_ip)
179 #define block_icmp         W32_NAMESPACE (block_icmp)
180 #define last_cookie        W32_NAMESPACE (last_cookie)
181 #define cookies            W32_NAMESPACE (cookies)
182 #define survive_eth        W32_NAMESPACE (survive_eth)
183 #define survive_bootp      W32_NAMESPACE (survive_bootp)
184 #define survive_dhcp       W32_NAMESPACE (survive_dhcp)
185 #define survive_rarp       W32_NAMESPACE (survive_rarp)
186 #define loopback_handler   W32_NAMESPACE (loopback_handler)
187 #define usr_init           W32_NAMESPACE (usr_init)
188 #define usr_post_init      W32_NAMESPACE (usr_post_init)
189 #define in_checksum        W32_NAMESPACE (in_checksum)
190 #define aton               W32_NAMESPACE (aton)
191 #define isaddr             W32_NAMESPACE (isaddr)
192 #define _printf            W32_NAMESPACE (_printf)
193 #define _outch             W32_NAMESPACE (_outch)
194 #define outs               W32_NAMESPACE (outs)
195 #define outsnl             W32_NAMESPACE (outsnl)
196 #define outsn              W32_NAMESPACE (outsn)
197 #define outhexes           W32_NAMESPACE (outhexes)
198 #define outhex             W32_NAMESPACE (outhex)
199 #define rip                W32_NAMESPACE (rip)
200 #define parse_config_table W32_NAMESPACE (parse_config_table)
201 #define init_timer_isr     W32_NAMESPACE (init_timer_isr)
202 #define exit_timer_isr     W32_NAMESPACE (exit_timer_isr)
203
204 #undef  sock_init
205 #define sock_init() watt_sock_init (sizeof(tcp_Socket), sizeof(udp_Socket))
206
207 W32_FUNC int         watt_sock_init (size_t, size_t);
208 W32_FUNC const char *sock_init_err  (int rc);
209
210 W32_FUNC void sock_exit (void);
211 W32_FUNC void dbug_init (void);  /* effective if compiled with `USE_DEBUG' */
212 W32_FUNC void init_misc (void);  /* may be called before sock_init() */
213 W32_FUNC void sock_sig_exit (const char *msg, int sigint);
214
215 #if !defined(WATT32_BUILD)
216 /*
217  * `s' is the pointer to a udp or tcp socket
218  */
219 W32_FUNC int    sock_read       (void *s, char *dp, int len);
220 W32_FUNC int    sock_preread    (void *s, char *dp, int len);
221 W32_FUNC int    sock_fastread   (void *s, char *dp, int len);
222 W32_FUNC int    sock_write      (void *s, const char *dp, int len);
223 W32_FUNC int    sock_enqueue    (void *s, const char *dp, int len);
224 W32_FUNC int    sock_fastwrite  (void *s, const char *dp, int len);
225 W32_FUNC size_t sock_setbuf     (void *s, char *buf, size_t len);
226 W32_FUNC void   sock_flush      (void *s);
227 W32_FUNC void   sock_noflush    (void *s);
228 W32_FUNC void   sock_flushnext  (void *s);
229 W32_FUNC int    sock_puts       (void *s, const char *dp);
230 W32_FUNC WORD   sock_gets       (void *s, char *dp, int n);
231 W32_FUNC BYTE   sock_putc       (void *s, char c);
232 W32_FUNC int    sock_getc       (void *s);
233 W32_FUNC WORD   sock_dataready  (void *s);
234 W32_FUNC int    sock_established(void *s);
235 W32_FUNC int    sock_close      (void *s);
236 W32_FUNC int    sock_abort      (void *s);
237 W32_FUNC void (*sock_yield      (void *s, VoidProc fn)) (void);
238 W32_FUNC int    sock_mode       (void *s, WORD mode);
239 W32_FUNC int    sock_sselect    (void *s, int waitstate);
240 W32_FUNC int    sock_timeout    (void *s, int seconds);
241 W32_FUNC int    sock_recv       (void *s, char *buf, unsigned len);
242 W32_FUNC int    sock_recv_init  (void *s, char *buf, unsigned len);
243 W32_FUNC int    sock_recv_from  (void *s, DWORD *ip, WORD *port, char *buf, unsigned len, int peek);
244 W32_FUNC int    sock_recv_used  (void *s);
245 W32_FUNC int    sock_keepalive  (void *s);
246
247 W32_FUNC size_t sock_rbsize     (const void *s);
248 W32_FUNC size_t sock_rbused     (const void *s);
249 W32_FUNC size_t sock_rbleft     (const void *s);
250 W32_FUNC size_t sock_tbsize     (const void *s);
251 W32_FUNC size_t sock_tbused     (const void *s);
252 W32_FUNC size_t sock_tbleft     (const void *s);
253
254 W32_FUNC int MS_CDECL sock_printf (void *s, const char *fmt, ...)
255   #if defined(__GNUC__)
256   __attribute__((format(printf,2,3)))
257   #endif
258   ;
259
260 W32_FUNC int MS_CDECL sock_scanf (void *s, const char *fmt, ...)
261   #if defined(__GNUC__)
262   __attribute__((format(scanf,2,3)))
263   #endif
264   ;
265
266 /*
267  * TCP or UDP specific stuff, must be used for open's and listens, but
268  * sock stuff above is used for everything else
269  */
270 W32_FUNC int udp_open   (void *s, WORD lport, DWORD ina, WORD port, ProtoHandler handler);
271 W32_FUNC int tcp_open   (void *s, WORD lport, DWORD ina, WORD port, ProtoHandler handler);
272 W32_FUNC int udp_listen (void *s, WORD lport, DWORD ina, WORD port, ProtoHandler handler);
273 W32_FUNC int tcp_listen (void *s, WORD lport, DWORD ina, WORD port, ProtoHandler handler, WORD timeout);
274 W32_FUNC int tcp_established (const void *s);
275
276 #endif  /* WATT32_BUILD */
277
278
279 W32_FUNC DWORD resolve    (const char *name);
280 W32_FUNC int   resolve_ip (DWORD ip, char *name, int len);
281 W32_FUNC DWORD lookup_host(const char *host, char *ip_str);
282 W32_FUNC const char *dom_strerror (int err);
283
284 /*
285  * less general functions
286  */
287 W32_FUNC int   tcp_cbreak (int mode);
288
289 W32_FUNC DWORD aton       (const char *name);
290 W32_FUNC int   isaddr     (const char *name);
291 W32_FUNC char *rip        (char *s);
292 W32_FUNC int   watt_kbhit (void);
293
294 #define tcp_cbrk(mode)  tcp_cbreak(mode) /* old name */
295
296 /*
297  * Set MD5 secret for TCP option 19 (RFC-2385).
298  * Only if built with USE_TCP_MD5.
299  */
300 W32_FUNC const char *tcp_md5_secret (void *s, const char *secret);
301
302 /*
303  * timers
304  */
305 W32_FUNC DWORD  set_timeout  (DWORD msec);
306 W32_FUNC int    chk_timeout  (DWORD value);
307 W32_FUNC int    cmp_timers   (DWORD t1, DWORD t2);
308 W32_FUNC int    set_timediff (long msec);
309 W32_FUNC long   get_timediff (DWORD now, DWORD t);
310 W32_FUNC int    hires_timer  (int on);
311 W32_FUNC double timeval_diff (const struct timeval *a, const struct timeval *b);
312
313 W32_FUNC void   init_timer_isr (void);
314 W32_FUNC void   exit_timer_isr (void);
315
316 W32_FUNC void   init_userSuppliedTimerTick (void);
317 W32_FUNC void   userTimerTick (DWORD);
318
319 W32_FUNC void   ip_timer_init (sock_type *s , unsigned delayseconds);
320 W32_FUNC int    ip_timer_expired (const sock_type *s);
321
322 /*
323  * TCP/IP system variables
324  */
325 W32_DATA DWORD my_ip_addr;
326 W32_DATA DWORD sin_mask;       /* eg.  0xFFFFFE00L */
327 W32_DATA int   sock_delay;
328 W32_DATA int   sock_inactive;
329 W32_DATA int   sock_data_timeout;
330 W32_DATA WORD  multihomes;
331
332 W32_DATA int   block_tcp;
333 W32_DATA int   block_udp;
334 W32_DATA int   block_icmp;
335 W32_DATA int   block_ip;
336 W32_DATA WORD  last_cookie;
337 W32_DATA DWORD cookies [MAX_COOKIES];
338
339 W32_DATA BOOL  survive_eth;
340 W32_DATA BOOL  survive_bootp;
341 W32_DATA BOOL  survive_dhcp;
342 W32_DATA BOOL  survive_rarp;
343
344 W32_DATA void (*loopback_handler) (void *ip);
345
346 /*
347  * things you probably won't need to know about
348  */
349
350 /*
351  * sock_debugdump
352  *      - dump some socket control block parameters
353  * used for testing the kernal, not recommended
354  */
355 W32_FUNC void sock_debugdump (const sock_type *s);
356
357 /*
358  * tcp_config - read a configuration file
359  *            - if special path desired, call after sock_init()
360  *            - NULL reads path WATTCP.CFG env-var or from program's path
361  * see sock_init();
362  */
363 W32_FUNC long tcp_config (const char *path);
364
365 /*
366  * tcp_tick - must be called periodically by user application.
367  *          - returns NULL when our socket closes
368  */
369 W32_FUNC WORD tcp_tick (sock_type *s);
370
371 /*
372  * tcp_set_debug_state - set to 1,2 or reset 0
373  */
374 W32_FUNC void tcp_set_debug_state (WORD x);
375
376
377 /*
378  * name domain constants, etc.
379  */
380 #define def_domain       W32_NAMESPACE (def_domain)
381 #define def_nameservers  W32_NAMESPACE (def_nameservers)
382 #define dns_timeout      W32_NAMESPACE (dns_timeout)
383 #define dom_errno        W32_NAMESPACE (dom_errno)
384 #define last_nameserver  W32_NAMESPACE (last_nameserver)
385 #define _mtu             W32_NAMESPACE (_mtu)
386 #define _mss             W32_NAMESPACE (_mss)
387 #define ctrace_on        W32_NAMESPACE (ctrace_on)
388 #define has_rdtsc        W32_NAMESPACE (has_rdtsc)
389 #define clocks_per_usec  W32_NAMESPACE (clocks_per_usec)
390 #define psocket          W32_NAMESPACE (psocket)
391 #define _inet_ntoa       W32_NAMESPACE (_inet_ntoa)
392 #define _inet_addr       W32_NAMESPACE (_inet_addr)
393
394 W32_DATA char         *def_domain;
395 W32_DATA int           dom_errno;
396 W32_DATA DWORD         def_nameservers [MAX_NAMESERVERS];
397 W32_DATA WORD          dns_timeout;
398 W32_DATA WORD          last_nameserver;
399 W32_DATA WORD         _watt_handle_cbreak;      /* ^C/^Break handle mode */
400 W32_DATA volatile int _watt_cbroke;             /* ^C/^Break occured */
401 W32_DATA unsigned     _mtu, _mss;
402 W32_DATA int           ctrace_on;
403 W32_DATA int           has_rdtsc;
404 W32_DATA DWORD         clocks_per_usec;
405
406 /* Old compatibility
407  */
408 #define wathndlcbrk  _watt_handle_cbreak
409 #define watcbroke    _watt_cbroke
410
411 /*
412  * sock_wait_ .. macros
413  */
414
415 /*
416  * sock_wait_established()
417  *      - Waits until connected or aborts if timeout etc.
418  *
419  * sock_wait_input()
420  *      - Waits for received input on socket 's'.
421  *      - May not be valid input for sock_gets()..  check returned length.
422  *
423  * sock_tick()
424  *      - Do tick and jump on abort.
425  *
426  * sock_wait_closed()
427  *      - Close socket and wait until fully closed.
428  *        Discards all received data.
429  *
430  * All these macros jump to label sock_err with contents of *statusptr
431  * set to
432  *       1 on closed normally.
433  *      -1 on error, call sockerr(s) for cause.
434  *
435  */
436 #if !defined(WATT32_BUILD)
437
438 W32_FUNC int _ip_delay0 (void *s, int sec, UserHandler fn, void *statusptr);
439 W32_FUNC int _ip_delay1 (void *s, int sec, UserHandler fn, void *statusptr);
440 W32_FUNC int _ip_delay2 (void *s, int sec, UserHandler fn, void *statusptr);
441
442
443 #define sock_wait_established(s,seconds,fn,statusptr) \
444         do {                                          \
445            if (_ip_delay0 (s,seconds,fn,statusptr))   \
446               goto sock_err;                          \
447         } while (0)
448
449 #define sock_wait_input(s,seconds,fn,statusptr)       \
450         do {                                          \
451            if (_ip_delay1 (s,seconds,fn,statusptr))   \
452               goto sock_err;                          \
453         } while (0)
454
455 #define sock_tick(s, statusptr)                       \
456         do {                                          \
457            if (!tcp_tick(s)) {                        \
458               if (statusptr) *statusptr = -1;         \
459               goto sock_err;                          \
460            }                                          \
461         } while (0)
462
463 #define sock_wait_closed(s,seconds,fn,statusptr)      \
464         do {                                          \
465            if (_ip_delay2(s,seconds,fn,statusptr))    \
466               goto sock_err;                          \
467         } while (0)
468
469 #endif  /* WATT32_BUILD */
470
471 /*
472  * User hook for WATTCP.CFG initialisation file.
473  */
474 W32_DATA void (*usr_init) (const char *keyword, const char *value);
475 W32_DATA void (*usr_post_init) (void);
476
477 enum config_tab_types {
478      ARG_ATOI,        /* convert to int */
479      ARG_ATOB,        /* convert to 8-bit byte */
480      ARG_ATOW,        /* convert to 16-bit word */
481      ARG_ATOIP,       /* convert to ip-address on host order */
482      ARG_ATOX_B,      /* convert to hex-byte */
483      ARG_ATOX_W,      /* convert to hex-word */
484      ARG_ATOX_D,      /* convert to hex-word */
485      ARG_STRDUP,      /* duplicate string value */
486      ARG_STRCPY,      /* copy string value */
487      ARG_RESOLVE,     /* resolve host to ip-address */
488      ARG_FUNC         /* call convertion function */
489    };
490
491 struct config_table {
492        const char            *keyword;
493        enum config_tab_types  type;
494        void                  *arg_func;
495      };
496
497
498 W32_FUNC int parse_config_table (const struct config_table *tab,
499                                  const char *section,
500                                  const char *name,
501                                  const char *value);
502
503 /*
504  * Run with no config file (embedded/diskless)
505  */
506 W32_DATA int _watt_no_config;
507
508 W32_FUNC void tcp_inject_config (
509               const struct config_table *cfg,
510               const char                *key,
511               const char                *value);
512
513 typedef long (*WattUserConfigFunc) (int pass, const struct config_table *cfg);
514
515 W32_FUNC WattUserConfigFunc _watt_user_config (WattUserConfigFunc fn);
516
517
518 /*
519  * Bypass standard handling of DHCP transient configuration
520  */
521 #include <sys/packon.h>
522
523 struct DHCP_config {
524        DWORD  my_ip;
525        DWORD  netmask;
526        DWORD  gateway;
527        DWORD  nameserver;
528        DWORD  server;
529        DWORD  iplease;
530        DWORD  renewal;
531        DWORD  rebind;
532        DWORD  tcp_keep_intvl;
533        BYTE   default_ttl;
534        char   hostname [MAX_HOSTLEN+1];
535        char   domain [MAX_HOSTLEN+1];
536        char   loghost [MAX_HOSTLEN+1]; /* Only used if USE_BSD_FUNC defined */
537      };
538
539 #include <sys/packoff.h>
540
541 enum DHCP_config_op {
542      DHCP_OP_READ  = 0,
543      DHCP_OP_WRITE = 1,
544      DHCP_OP_ERASE = 2
545    };
546
547 typedef int (*WattDHCPConfigFunc) (enum DHCP_config_op op,
548                                    struct DHCP_config *cfg);
549
550 W32_FUNC WattDHCPConfigFunc dhcp_set_config_func (WattDHCPConfigFunc fn);
551
552
553 /*
554  * Various function-pointer hooks etc.
555  */
556 W32_DATA int (MS_CDECL *_printf) (const char *, ...);
557 W32_DATA void (*_outch) (char c);
558 W32_DATA int  (*_resolve_hook) (void);
559 W32_DATA void (*wintr_chain) (void);
560
561 W32_DATA int (*tftp_writer) (const void *buf, unsigned length);
562 W32_DATA int (*tftp_terminator) (void);
563
564 W32_FUNC void  outs    (const char *s);
565 W32_FUNC void  outsnl  (const char *s);
566 W32_FUNC void  outsn   (const char *s, int n);
567 W32_FUNC void  outhexes(const char *s, int n);
568 W32_FUNC void  outhex  (char ch);
569
570 W32_FUNC int   wintr_enable (void);
571 W32_FUNC void  wintr_disable (void);
572 W32_FUNC void  wintr_shutdown (void);
573 W32_FUNC void  wintr_init (void);
574
575 W32_FUNC int   _ping     (DWORD host, DWORD num, const BYTE *pattern, long len);
576 W32_FUNC DWORD _chk_ping (DWORD host, DWORD *ping_num);
577
578 W32_FUNC int   _eth_init         (void);
579 W32_FUNC void  _eth_release      (void);
580 W32_FUNC void *_eth_formatpacket (const void *eth_dest, WORD eth_type);
581 W32_FUNC void  _eth_free         (const void *buf);
582 W32_FUNC void *_eth_arrived      (WORD *type, BOOL *brdcast);
583 W32_FUNC int   _eth_send         (WORD len, const void *sock, const char *file, unsigned line);
584 W32_FUNC int   _eth_set_addr     (const void *addr);
585 W32_FUNC BYTE  _eth_get_hwtype   (BYTE *hwtype, BYTE *hwlen);
586
587 W32_DATA void *(*_eth_recv_hook) (WORD *type);
588 W32_DATA int   (*_eth_recv_peek) (void *mac_buf);
589 W32_DATA int   (*_eth_xmit_hook) (const void *buf, unsigned len);
590 W32_FUNC WORD    in_checksum     (const void *buf, unsigned len);
591
592 #define inchksum(buf,len)  in_checksum(buf, len)
593
594
595 /*
596  * BSD-socket similarities.
597  * Refer <sys/socket.h> for the real thing.
598  */
599 #if !defined(WATT32_BUILD)
600
601 struct watt_sockaddr {     /* for _getpeername, _getsockname */
602        WORD   s_type;
603        WORD   s_port;
604        DWORD  s_ip;
605        BYTE   s_spares[6]; /* unused */
606      };
607
608 W32_FUNC DWORD _gethostid   (void);
609 W32_FUNC DWORD _sethostid   (DWORD ip);
610 W32_FUNC int   _getsockname (const void *s, void *dest, int *len);
611 W32_FUNC int   _getpeername (const void *s, void *dest, int *len);
612
613 W32_FUNC int   _chk_socket  (const void *s);
614 W32_FUNC void  psocket      (const void *s);
615
616 #endif
617
618 W32_FUNC char *_inet_ntoa   (char *s, DWORD x);
619 W32_FUNC DWORD _inet_addr   (const char *name);
620
621 W32_FUNC BOOL _arp_register (DWORD use, DWORD instead_of);
622 W32_FUNC BOOL _arp_resolve  (DWORD ina, eth_address *res);
623
624 W32_FUNC int   addwattcpd (VoidProc p);
625 W32_FUNC int   delwattcpd (VoidProc p);
626
627 W32_FUNC void _sock_debug_on  (void);
628 W32_FUNC void _sock_debug_off (void);
629
630 #if !defined(WATT32_BUILD)
631
632 W32_FUNC const char *sockerr  (const void *s);   /* UDP / TCP */
633 W32_FUNC void  sockerr_clear  (void *s);         /* UDP / TCP */
634
635 W32_FUNC const char *sockstate (const void *s);  /* UDP / TCP / Raw */
636
637 /*
638  * Reduce internal states to "user-easy" states, GvB 2002-09
639  */
640 enum TCP_SIMPLE_STATE {
641      TCP_CLOSED,
642      TCP_LISTENING,
643      TCP_OPENING,
644      TCP_OPEN,
645      TCP_CLOSING
646    };
647
648 W32_FUNC enum TCP_SIMPLE_STATE tcp_simple_state (const tcp_Socket *s);
649
650 #endif  /* WATT32_BUILD */
651
652
653 /*
654  * BSD functions for read/write/select
655  */
656 W32_FUNC int close_s  (int s);
657 W32_FUNC int write_s  (int s, const char *buf, int nbyte);
658 W32_FUNC int read_s   (int s,       char *buf, int nbyte);
659 W32_FUNC int writev_s (int s, const struct iovec *vector, size_t count);
660 W32_FUNC int readv_s  (int s, const struct iovec *vector, size_t count);
661
662 W32_FUNC const sock_type *__get_sock_from_s (int s, int proto);
663                             /* Use 'IPPROTO_x' for proto */
664
665 W32_FUNC int select_s (int num_sockets,
666                        fd_set *read_events,
667                        fd_set *write_events,
668                        fd_set *except_events,
669                        struct timeval *timeout);
670
671 /* The only BSD/Winsock replacement in this file.
672  * Normally belongs in djgpp's <unistd.h>.
673  */
674 #ifndef __DJGPP__
675 W32_FUNC int W32_CALL select (int num_sockets,
676                               fd_set *read_events,
677                               fd_set *write_events,
678                               fd_set *except_events,
679                               struct timeval *timeout);
680 #endif
681
682 /* Duplicated from <sys/socket.h>
683  */
684 W32_FUNC int W32_CALL gethostname (char *name, int len);
685
686 /*
687  * Multicast stuff (if built with `USE_MULTICAST')
688  */
689 W32_DATA int _multicast_on;
690
691 W32_FUNC int join_mcast_group  (DWORD ip);
692 W32_FUNC int leave_mcast_group (DWORD ip);
693 W32_FUNC int _ip4_is_multicast (DWORD ip);
694 W32_FUNC int multi_to_eth      (DWORD ip, eth_address *eth);
695 W32_FUNC int udp_SetTTL        (udp_Socket *s, BYTE ttl);
696
697
698 /*
699  * Commandline parsing
700  */
701 #if defined(__DJGPP__)
702   #include <unistd.h>
703
704 #elif defined(__MINGW32__)
705   #include <getopt.h>
706
707 #elif !defined(_GETOPT_H)  /* not using a local getopt.c */
708   #define optarg    W32_NAMESPACE (optarg)
709   #define optind    W32_NAMESPACE (optind)
710   #define opterr    W32_NAMESPACE (opterr)
711   #define optopt    W32_NAMESPACE (optopt)
712   #define optswchar W32_NAMESPACE (optswchar)
713   #define optmode   W32_NAMESPACE (optmode)
714   #define getopt    W32_NAMESPACE (getopt)
715
716   W32_DATA char *optarg;       /* argument of current option                    */
717   W32_DATA int   optind;       /* index of next argument; default=0: initialize */
718   W32_DATA int   opterr;       /* 0=disable error messages; default=1: enable   */
719   W32_DATA int   optopt;       /* option char returned from getopt()            */
720   W32_DATA char *optswchar;    /* characters introducing options; default="-"   */
721
722   W32_DATA enum _optmode {
723            GETOPT_UNIX,        /* options at start of argument list (default)   */
724            GETOPT_ANY,         /* move non-options to the end                   */
725            GETOPT_KEEP,        /* return options in order                       */
726          } optmode;
727
728   W32_FUNC int getopt (int argc, char *const *argv, const char *opt_str);
729 #endif
730
731
732 /*
733  * Statistics printing
734  */
735 W32_FUNC void print_mac_stats  (void);
736 W32_FUNC void print_pkt_stats  (void);
737 W32_FUNC void print_vjc_stats  (void);
738 W32_FUNC void print_arp_stats  (void);
739 W32_FUNC void print_pppoe_stats(void);
740 W32_FUNC void print_ip4_stats  (void);
741 W32_FUNC void print_ip6_stats  (void);
742 W32_FUNC void print_icmp_stats (void);
743 W32_FUNC void print_igmp_stats (void);
744 W32_FUNC void print_udp_stats  (void);
745 W32_FUNC void print_tcp_stats  (void);
746 W32_FUNC void print_all_stats  (void);
747 W32_FUNC void reset_stats      (void);
748
749 #if !defined(WATT32_BUILD)
750 W32_FUNC int sock_stats (void *s, DWORD *days, WORD *inactive,
751                          WORD *cwindow, DWORD *avg,  DWORD *sd);
752 #endif
753
754
755 /*
756  * Controlling timer interrupt handler for background processing.
757  * Not recommended, little tested
758  */
759 W32_FUNC void backgroundon  (void);
760 W32_FUNC void backgroundoff (void);
761 W32_FUNC void backgroundfn  (VoidProc func);
762
763
764 /*
765  * Misc functions
766  */
767 #if !defined(__DJGPP__) && !(defined(__WATCOMC__) && (__WATCOMC__ >= 1240))
768   #define ffs W32_NAMESPACE (ffs)
769   W32_FUNC int ffs (int mask);
770 #endif
771
772 #if defined (__HIGHC__)
773   W32_FUNC int system (const char *cmd);
774   pragma Alias (system, "_mw_watt_system");
775 #endif
776
777 W32_FUNC unsigned Random (unsigned a, unsigned b);
778
779
780 /*
781  * Tracing to RS-232 serial port, by Gundolf von Bachhaus <GBachhaus@gmx.net>
782  * Watt-32 library must be compiled with `USE_RS232_DBG' (see .\src\config.h)
783  */
784 W32_FUNC int            trace2com_init (WORD portAddress, DWORD baudRate);
785 W32_FUNC int MS_CDECL __trace2com      (const char *fmt, ...)
786   #if defined(__GNUC__)
787   __attribute__((format(printf,1,2)))
788   #endif
789   ;
790
791 #ifdef __cplusplus
792 }
793 #endif
794
795 #endif  /* RC_INVOKED */
796 #endif  /* __WATT_TCP_H */