1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Component                                                        */
16 /**                                                                       */
17 /** BSD 4.3 Socket API Compatible Interface to NetX Duo                   */
18 /**                                                                       */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 /**************************************************************************/
24 /*                                                                        */
25 /*  BSD DEFINITIONS                                        RELEASE        */
26 /*                                                                        */
27 /*    nxd_bsd.h                                           PORTABLE C      */
28 /*                                                           6.1.9        */
29 /*  AUTHOR                                                                */
30 /*                                                                        */
31 /*    Yuxin Zhou, Microsoft Corporation                                   */
32 /*                                                                        */
33 /*  DESCRIPTION                                                           */
34 /*                                                                        */
35 /*    This file defines the constants, structures, etc... needed to       */
36 /*    implement the BSD 4.3 Socket API Compatible Interface to NetX Duo.  */
37 /*                                                                        */
38 /*                                                                        */
39 /*  RELEASE HISTORY                                                       */
40 /*                                                                        */
41 /*    DATE              NAME                      DESCRIPTION             */
42 /*                                                                        */
43 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
44 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
45 /*                                            resulting in version 6.1    */
46 /*  03-02-2021     Yuxin Zhou               Modified comment(s), and      */
47 /*                                            fixed compiler warnings,    */
48 /*                                            resulting in version 6.1.5  */
49 /*  10-15-2021     Yuxin Zhou               Modified comment(s), and      */
50 /*                                            defined IP protocols for    */
51 /*                                            ICMP, IGMP and ICMPv6,      */
52 /*                                            resulting in version 6.1.9  */
53 /*                                                                        */
54 /**************************************************************************/
55 
56 #ifndef NXD_BSD_H
57 #define NXD_BSD_H
58 
59 /* Determine if a C++ compiler is being used.  If so, ensure that standard
60    C is used to process the API information.  */
61 
62 #ifdef   __cplusplus
63 
64 /* Yes, C++ compiler is present.  Use standard C.  */
65 extern   "C" {
66 
67 #endif
68 
69 #ifndef __CCRX__
70 #include "time.h"
71 #endif /* __CCRX__ */
72 
73 /* Bring in the necessary NetX include file.  */
74 #include "nx_api.h"
75 
76 
77 /* Define the print error macro for reporting source line number.
78 
79 #define NX_BSD_PRINT_ERRORS
80 */
81 
82 /* Defined, raw pppoe feature is enabled. */
83 /*
84 #define NX_BSD_RAW_PPPOE_SUPPORT
85 */
86 
87 /* Defined, raw feature is enabled. */
88 /*
89 #define NX_BSD_RAW_SUPPORT
90 */
91 
92 /* Defined, raw packet is not duplicated. Then NetX will not handle packets such as ARP. */
93 /*
94 #define NX_DISABLE_BSD_RAW_PACKET_DUPLICATE
95 */
96 
97 #if defined(NX_BSD_RAW_SUPPORT) && defined(NX_BSD_RAW_PPPOE_SUPPORT)
98 #error "Raw feature conflicts with raw PPPoE feature"
99 #endif /* defined(NX_BSD_RAW_SUPPORT) && defined(NX_BSD_RAW_PPPOE_SUPPORT) */
100 
101 /* Defined, getaddrinfo is able to get address by name/cname. getnameinfo is able to get name by address. */
102 /*
103 #define NX_BSD_ENABLE_DNS
104 */
105 
106 
107 /*
108    Define the BSD socket timeout process to execute in the timer context.
109    If this option is not defined, application needs to create stack space for the BSD timeout process
110    thread, and passes the stack to the BSD layer through the bsd_initialize() call.  In this configuration,
111    the BSD timeout process is all done in the BSD timeout process thread context.
112 
113    User may choose to define NX_BSD_TIMEOUT_PROCESS_IN_TIMER, so the BSD timeout process is executed in the
114    ThreadX timer object context.  This configuration could eliminate an extra thread (and associated stack
115    space.)  The following parameters passed into bsd_initialize are ignored:
116    * bsd_thread_stack_area
117    * bsd_thread_stack_size
118    * bsd_thread_priority
119    However system designer needs to be aware of the following if the BSD timeout process is
120    executed in ThreadX timer context:
121    * The amount of BSD socket processing may slow down the ThreadX timer;
122    * If the timer is executed in ISR context (ThreadX library is built with TX_TIMER_PROCESS_IN_ISR),
123      the BSD timeout process is now executing in the ISR context;
124 
125      By default NX_BSD_TIMEOUT_PROCESS_IN_TIMER is NOT defined.
126 */
127 /* #define NX_BSD_TIMEOUT_PROCESS_IN_TIMER */
128 
129 /* Define configuration constants for the BSD compatibility layer.  Note that these can be overridden via -D or a #define somewhere else.  */
130 
131 #ifndef NX_BSD_TCP_WINDOW
132 #define NX_BSD_TCP_WINDOW                   65535                   /* 64k is typical window size for 100Mb ethernet.                       */
133 #endif
134 
135 #ifndef NX_BSD_SOCKFD_START
136 #define NX_BSD_SOCKFD_START                 32                      /* Logical FD starting value.                                           */
137 #endif
138 
139 #ifndef NX_BSD_MAX_SOCKETS
140 #define NX_BSD_MAX_SOCKETS                  32                      /* Maximum number of total sockets available in the BSD layer. Note     */
141 #endif                                                              /*   NOTE:  Must be multiple of 32!                                     */
142 
143 #ifndef NX_BSD_MAX_LISTEN_BACKLOG
144 #define NX_BSD_MAX_LISTEN_BACKLOG           5                       /* Maximum listen backlog.                                              */
145 #endif
146 
147 #ifndef NX_MICROSECOND_PER_CPU_TICK
148 #define NX_MICROSECOND_PER_CPU_TICK         (1000000/NX_IP_PERIODIC_RATE) /* Number of microseconds per timer interrupt, default 10ms.            */
149 #endif
150 #ifndef NX_BSD_TIMEOUT
151 #define NX_BSD_TIMEOUT                      (20*NX_IP_PERIODIC_RATE)
152                                                                     /* By default all internal NetX calls wait and block for 20 seconds.    */
153 #endif
154 
155 
156 #ifndef NX_BSD_TCP_SOCKET_DISCONNECT_TIMEOUT
157 #define NX_BSD_TCP_SOCKET_DISCONNECT_TIMEOUT 1                      /* Timeout in timer ticks for internal NetX to disconnect the socket.
158                                                                        The default value of 1 tick is so NetX BSD emulates native BSD
159                                                                        socket and performs an immediate socket close without sending an RST
160                                                                        packet.                                                              */
161 #endif
162 
163 /* Define configurable options for BSD extended options. */
164 
165 
166 #ifndef NX_BSD_TIMER_RATE
167 #define NX_BSD_TIMER_RATE                   (1 * NX_IP_PERIODIC_RATE)        /* Rate at which BSD timer runs.                                   */
168 #endif
169 
170 
171 /* Define BSD events */
172 
173 #define NX_BSD_RECEIVE_EVENT                ((ULONG) 0x00000001)    /* Event flag to signal a receive packet event                          */
174 #define NX_BSD_SELECT_EVENT                 ((ULONG) 0x00008000)    /* Event flag to signal a thread is waiting in select                   */
175 #define NX_BSD_ALL_EVENTS                   ((ULONG) 0xFFFFFFFF)    /* All event flag                                                       */
176 #define NX_BSD_CONNECT_EVENT                ((ULONG) 0x00000002)
177 #define NX_BSD_LINGER_EVENT                 ((ULONG) 0x00000004)    /* Event flag to signal a timed linger state has expired on a socket    */
178 #define NX_BSD_TIMED_WAIT_EVENT             ((ULONG) 0x00000008)    /* Event flag to signal a timed wait state has expired on a socket      */
179 #define NX_BSD_TIMER_EVENT                  ((ULONG) 0x00000010)    /* Event flag to singal a BSD 1 sec timer */
180 
181 /* For compatibility undefine the fd_set.  Then define the FD set size.  */
182 
183 #ifdef fd_set
184 #undef fd_set
185 #endif
186 
187 #ifdef   FD_SETSIZE
188 #undef   FD_SETSIZE                                                                     /* May be different in other header files e.g 64 in GNU types.h file    */
189 #define  FD_SETSIZE                         (NX_BSD_MAX_SOCKETS + NX_BSD_SOCKFD_START)  /* Number of sockets to select on - same is max sockets!                */
190 #else
191 #define  FD_SETSIZE                         (NX_BSD_MAX_SOCKETS + NX_BSD_SOCKFD_START)  /* Number of sockets to select on - same is max sockets!                */
192 #endif
193 
194 
195 /* Define some BSD protocol constants.  */
196 
197 #define SOCK_STREAM                         1                       /* TCP Socket                                                          */
198 #define SOCK_DGRAM                          2                       /* UDP Socket                                                          */
199 #define SOCK_RAW                            3                       /* Raw socket                                                          */
200 #define IPPROTO_ICMP                        1                       /* ICMP used with socket type SOCK_RAW                                 */
201 #define IPPROTO_IGMP                        2                       /* IGMP used with socket type SOCK_RAW                                 */
202 #define IPPROTO_TCP                         6                       /* TCP Socket                                                          */
203 #define IPPROTO_UDP                         17                      /* TCP Socket                                                          */
204 #define IPPROTO_ICMPV6                      58                      /* ICMPv6 used with socket type SOCK_RAW                               */
205 #define IPPROTO_RAW                         255                     /* Raw Socket                                                          */
206 
207 /* Define supported flags for 'send' and 'recv'. */
208 #define MSG_PEEK                            0x02                    /* Peek incoming message */
209 #define MSG_DONTWAIT                        0x40                    /* Nonblocking IO        */
210 
211 /* Address families.  */
212 
213 #define     AF_UNSPEC                       0                       /* Unspecified.                                                         */
214 #define     AF_NS                           1                       /* Local to host (pipes, portals).                                      */
215 #define     AF_INET                         2                       /* IPv4 socket (UDP, TCP, etc)                                          */
216 #define     AF_INET6                        3                       /* IPv6 socket (UDP, TCP, etc)                                          */
217 #define     AF_PACKET                       4                       /* Raw Packet type (Link Layer packets)                                 */
218 
219 /* Protocol families, same as address families.  */
220 #define     PF_INET                         AF_INET
221 #define     PF_INET6                        AF_INET6
222 #define     PF_PACKET                       AF_PACKET
223 #define     ETH_P_ALL                       3
224 
225 #define     INADDR_ANY                      0
226 #define     NX_BSD_LOCAL_IF_INADDR_ANY      0xFFFFFFFF
227 /* Define API error codes.  */
228 
229 #define NX_SOC_ERROR                       -1                       /* Failure.                                                             */
230 #ifndef ERROR
231 #define ERROR                               NX_SOC_ERROR
232 #endif
233 #define NX_SOC_OK                           0                       /* Success.                                                             */
234 #ifndef OK
235 #define OK                                  NX_SOC_OK
236 #endif
237 #define NX_BSD_BLOCK_POOL_ERROR             1
238 #define NX_BSD_MUTEX_ERROR                  2
239 #define NX_BSD_THREAD_ERROR                 4
240 #define NX_BSD_EVENT_ERROR                  7
241 #define NX_BSD_ENVIRONMENT_ERROR            8
242 
243 #ifndef NX_PACKET_OFFSET_ERROR
244 #define NX_PACKET_OFFSET_ERROR              0x53
245 #endif
246 
247 /* The Netx API does not require Host to Network conversion or vice versa. The following macro's are provided for source compatibility reasons only.  */
248 
249 #ifndef htons
250 #define htons(a)                            a
251 #endif
252 #ifndef htonl
253 #define htonl(a)                            a
254 #endif
255 #ifndef ntohs
256 #define ntohs(a)                            a
257 #endif
258 #ifndef ntohl
259 #define ntohl(a)                            a
260 #endif
261 
262 /* Define error handling macro.  */
263 
264 #ifdef NX_BSD_PRINT_ERRORS
265 #define NX_BSD_ERROR(status, line) printf(" NX BSD debug error message:, NX status: %x source line: %i \n", status, line)
266 #else
267 #define NX_BSD_ERROR(status, line)
268 #endif
269 
270 
271 /* Define file descriptor operation flags. */
272 
273 /* Note: FIONREAD is hardware dependant. The default is for i386 processor. */
274 #ifndef FIONREAD
275 #define FIONREAD                            0x541B                  /* Read bytes available for the ioctl() command                         */
276 #endif
277 
278 #define F_GETFL                             3                       /* Get file descriptors                                                 */
279 #define F_SETFL                             4                       /* Set a subset of file descriptors (e.g. O_NONBlOCK                    */
280 #define O_NONBLOCK                          0x4000                  /* Option to enable non blocking on a file (e.g. socket)                */
281 
282 #ifndef FIONBIO
283 #define FIONBIO                             0x5421                  /* Enables socket non blocking option for the ioctl() command            */
284 #endif
285 
286 
287 /* Define the minimal TCP socket listen backlog value. */
288 #ifndef NX_BSD_TCP_LISTEN_MIN_BACKLOG
289 #define NX_BSD_TCP_LISTEN_MIN_BACKLOG   1
290 #endif
291 
292 /* Define the maximum number of packets that can be queued on a UDP socket or RAW socket. */
293 #ifndef NX_BSD_SOCKET_QUEUE_MAX
294 #define NX_BSD_SOCKET_QUEUE_MAX 5
295 #endif
296 
297 /* Define the constants that determine how big the hash table is for raw socket Protocol. The
298    value must be a power of two, so subtracting one gives us the mask. */
299 
300 #ifndef NX_BSD_SOCKET_RAW_PROTOCOL_TABLE_SIZE
301 #define NX_BSD_SOCKET_RAW_PROTOCOL_TABLE_SIZE   32
302 #endif /* NX_BSD_SOCKET_RAW_PROTOCOL_TABLE_SIZE */
303 
304 #define NX_BSD_SOCKET_RAW_PROTOCOL_TABLE_MASK   (NX_BSD_SOCKET_RAW_PROTOCOL_TABLE_SIZE-1)
305 
306 /* Define additional BSD socket errors. */
307 
308 /* From errno-base.h in /usr/include/asm-generic;  */
309 #define EPERM        1  /* Operation not permitted */
310 #define E_MIN        1  /* Minimum Socket/IO error */
311 #define ENOENT       2  /* No such file or directory */
312 #define ESRCH        3  /* No such process */
313 #define EINTR        4  /* Interrupted system call */
314 #define EIO          5  /* I/O error */
315 #define ENXIO        6  /* No such device or address */
316 #define E2BIG        7  /* Argument list too long */
317 #define ENOEXEC      8  /* Exec format error */
318 #define EBADF        9  /* Bad file number */
319 #define ECHILD      10  /* No child processes */
320 #define EAGAIN      11  /* Try again */
321 #define ENOMEM      12  /* Out of memory */
322 #define EACCES      13  /* Permission denied */
323 #define EFAULT      14  /* Bad address */
324 #define ENOTBLK     15  /* Block device required */
325 #define EBUSY       16  /* Device or resource busy */
326 #define EEXIST      17  /* File exists */
327 #define EXDEV       18  /* Cross-device link */
328 #define ENODEV      19  /* No such device */
329 #define ENOTDIR     20  /* Not a directory */
330 #define EISDIR      21  /* Is a directory */
331 #define EINVAL      22  /* Invalid argument */
332 #define ENFILE      23  /* File table overflow */
333 #define EMFILE      24  /* Too many open files */
334 #define ENOTTY      25  /* Not a typewriter */
335 #define ETXTBSY     26  /* Text file busy */
336 #define EFBIG       27  /* File too large */
337 #define ENOSPC      28  /* No space left on device */
338 #define ESPIPE      29  /* Illegal seek */
339 #define EROFS       30  /* Read-only file system */
340 #define EMLINK      31  /* Too many links */
341 #define EPIPE       32  /* Broken pipe */
342 #define EDOM        33  /* Math argument out of domain of func */
343 #define ERANGE      34  /* Math result not representable */
344 
345 /* From errno.h in /usr/include/asm-generic;   */
346 
347 #define EDEADLK         35  /* Resource deadlock would occur */
348 #define ENAMETOOLONG    36  /* File name too long */
349 #define ENOLCK          37  /* No record locks available */
350 #define ENOSYS          38  /* Function not implemented */
351 #define ENOTEMPTY       39  /* Directory not empty */
352 #define ELOOP           40  /* Too many symbolic links encountered */
353 #define EWOULDBLOCK     EAGAIN  /* Operation would block */
354 #define ENOMSG          42  /* No message of desired type */
355 #define EIDRM           43  /* Identifier removed */
356 #define ECHRNG          44  /* Channel number out of range */
357 #define EL2NSYNC        45  /* Level 2 not synchronized */
358 #define EL3HLT          46  /* Level 3 halted */
359 #define EL3RST          47  /* Level 3 reset */
360 #define ELNRNG          48  /* Link number out of range */
361 #define EUNATCH         49  /* Protocol driver not attached */
362 #define ENOCSI          50  /* No CSI structure available */
363 #define EL2HLT          51  /* Level 2 halted */
364 #define EBADE           52  /* Invalid exchange */
365 #define EBADR           53  /* Invalid request descriptor */
366 #define EXFULL          54  /* Exchange full */
367 #define ENOANO          55  /* No anode */
368 #define EBADRQC         56  /* Invalid request code */
369 #define EBADSLT         57  /* Invalid slot */
370 #define EDEADLOCK       EDEADLK
371 #define EBFONT          59  /* Bad font file format */
372 #define ENOSTR          60  /* Device not a stream */
373 #define ENODATA         61  /* No data available */
374 #define ETIME           62  /* Timer expired */
375 #define ENOSR           63  /* Out of streams resources */
376 #define ENONET          64  /* Machine is not on the network */
377 #define ENOPKG          65  /* Package not installed */
378 #define EREMOTE         66  /* Object is remote */
379 #define ENOLINK         67  /* Link has been severed */
380 #define EADV            68  /* Advertise error */
381 #define ESRMNT          69  /* Srmount error */
382 #define ECOMM           70  /* Communication error on send */
383 #define EPROTO          71  /* Protocol error */
384 #define EMULTIHOP       72  /* Multihop attempted */
385 #define EDOTDOT         73  /* RFS specific error */
386 #define EBADMSG         74  /* Not a data message */
387 #define EOVERFLOW       75  /* Value too large for defined data type */
388 #define ENOTUNIQ        76  /* Name not unique on network */
389 #define EBADFD          77  /* File descriptor in bad state */
390 #define EREMCHG         78  /* Remote address changed */
391 #define ELIBACC         79  /* Can not access a needed shared library */
392 #define ELIBBAD         80  /* Accessing a corrupted shared library */
393 #define ELIBSCN         81  /* .lib section in a.out corrupted */
394 #define ELIBMAX         82  /* Attempting to link in too many shared libraries */
395 #define ELIBEXEC        83  /* Cannot exec a shared library directly */
396 #define EILSEQ          84  /* Illegal byte sequence */
397 #define ERESTART        85  /* Interrupted system call should be restarted */
398 #define ESTRPIPE        86  /* Streams pipe error */
399 #define EUSERS          87  /* Too many users */
400 #define ENOTSOCK        88  /* Socket operation on non-socket */
401 #define EDESTADDRREQ    89  /* Destination address required */
402 #define EMSGSIZE        90  /* Message too long */
403 #define EPROTOTYPE      91  /* Protocol wrong type for socket */
404 #define ENOPROTOOPT     92  /* Protocol not available */
405 #define EPROTONOSUPPORT 93  /* Protocol not supported */
406 #define ESOCKTNOSUPPORT 94  /* Socket type not supported */
407 #define EOPNOTSUPP      95  /* Operation not supported on transport endpoint */
408 #define EPFNOSUPPORT    96  /* Protocol family not supported */
409 #define EAFNOSUPPORT    97  /* Address family not supported by protocol */
410 #define EADDRINUSE      98  /* Address already in use */
411 #define EADDRNOTAVAIL   99  /* Cannot assign requested address */
412 #define ENETDOWN        100 /* Network is down */
413 #define ENETUNREACH     101 /* Network is unreachable */
414 #define ENETRESET       102 /* Network dropped connection because of reset */
415 #define ECONNABORTED    103 /* Software caused connection abort */
416 #define ECONNRESET      104 /* Connection reset by peer */
417 #define ENOBUFS         105 /* No buffer space available */
418 #define EISCONN         106 /* Transport endpoint is already connected */
419 #define ENOTCONN        107 /* Transport endpoint is not connected */
420 #define ESHUTDOWN       108 /* Cannot send after transport endpoint shutdown */
421 #define ETOOMANYREFS    109 /* Too many references: cannot splice */
422 #define ETIMEDOUT       110 /* Connection timed out */
423 #define ECONNREFUSED    111 /* Connection refused */
424 #define EHOSTDOWN       112 /* Host is down */
425 #define EHOSTUNREACH    113 /* No route to host */
426 #define EALREADY        114 /* Operation already in progress */
427 #define EINPROGRESS     115 /* Operation now in progress */
428 #define ESTALE          116 /* Stale NFS file handle */
429 #define EUCLEAN         117 /* Structure needs cleaning */
430 #define ENOTNAM         118 /* Not a XENIX named type file */
431 #define ENAVAIL         119 /* No XENIX semaphores available */
432 #define EISNAM          120 /* Is a named type file */
433 #define EREMOTEIO       121 /* Remote I/O error */
434 #define EDQUOT          122 /* Quota exceeded */
435 #define ENOMEDIUM       123 /* No medium found */
436 #define EMEDIUMTYPE     124 /* Wrong medium type */
437 #define ECANCELED       125 /* Operation Canceled */
438 #define ENOKEY          126 /* Required key not available */
439 #define EKEYEXPIRED     127 /* Key has expired */
440 #define EKEYREVOKED     128 /* Key has been revoked */
441 #define EKEYREJECTED    129 /* Key was rejected by service */
442 #define EOWNERDEAD      130 /* Owner died - for robust mutexes*/
443 #define ENOTRECOVERABLE 131 /* State not recoverable */
444 #define ERFKILL         132 /* Operation not possible due to RF-kill */
445 
446 
447 /* List of BSD sock options from socket.h in /usr/include/asm/socket.h and asm-generic/socket.h.
448    Note: not all of these are implemented in NetX Duo Extended socket options.
449 
450    The first set of socket options take the socket level (category) SOL_SOCKET. */
451 
452 #define SOL_SOCKET      1   /* Define the socket option category. */
453 #define IPPROTO_IP      2   /* Define the IP option category.     */
454 #define SO_MIN          1   /* Minimum Socket option ID */
455 #define SO_DEBUG        1   /* Debugging information is being recorded.*/
456 #define SO_REUSEADDR    2   /* Enable reuse of local addresses in the time wait state */
457 #define SO_TYPE         3   /* Socket type */
458 #define SO_ERROR        4   /* Socket error status */
459 #define SO_DONTROUTE    5   /* Bypass normal routing */
460 #define SO_BROADCAST    6   /* Transmission of broadcast messages is supported.*/
461 #define SO_SNDBUF       7   /* Enable setting trasnmit buffer size */
462 #define SO_RCVBUF       8   /* Enable setting receive buffer size */
463 #define SO_KEEPALIVE    9   /* Connections are kept alive with periodic messages */
464 #define SO_OOBINLINE    10  /* Out-of-band data is transmitted in line */
465 #define SO_NO_CHECK     11  /* Disable UDP checksum */
466 #define SO_PRIORITY     12  /* Set the protocol-defined priority for all packets to be sent on this socket */
467 #define SO_LINGER       13  /* Socket lingers on close pending remaining send/receive packets. */
468 #define SO_BSDCOMPAT    14  /* Enable BSD bug-to-bug compatibility */
469 #define SO_REUSEPORT    15  /* Rebind a port already in use */
470 
471 #ifndef SO_PASSCRED          /* Used for passing credentials. Not currently in use. */
472 #define SO_PASSCRED     16   /* Enable passing local user credentials  */
473 #define SO_PEERCRED     17   /* Obtain the process, user and group ids of the other end of the socket connection */
474 #define SO_RCVLOWAT     18   /* Enable receive "low water mark" */
475 #define SO_SNDLOWAT     19   /* Enable send "low water mark" */
476 #define SO_RCVTIMEO     20   /* Enable receive timeout */
477 #define SO_SNDTIMEO     21   /* Enable send timeout */
478 #endif  /* SO_PASSCRED */
479 #define SO_SNDBUFFORCE  22  /* Enable setting trasnmit buffer size overriding user limit (admin privelege) */
480 #define SO_RCVBUFFORCE  23  /* Enable setting trasnmit buffer size overriding user limit (admin privelege) */
481 #define SO_MAX          SO_RCVBUFFORCE /* Maximum Socket option ID      */
482 
483 /*  This second set of socket options take the socket level (category) IPPROTO_IP. */
484 
485 #define IP_MULTICAST_IF     27 /* Specify outgoing multicast interface */
486 #define IP_MULTICAST_TTL    28 /* Specify the TTL value to use for outgoing multicast packet. */
487 #define IP_MULTICAST_LOOP   29 /* Whether or not receive the outgoing multicast packet, loopbacloopbackk mode. */
488 #define IP_BLOCK_SOURCE     30 /* Block multicast from certain source. */
489 #define IP_UNBLOCK_SOURCE   31 /* Unblock multicast from certain source. */
490 #define IP_ADD_MEMBERSHIP   32 /* Join IPv4 multicast membership */
491 #define IP_DROP_MEMBERSHIP  33 /* Leave IPv4 multicast membership */
492 #define IP_HDRINCL          34 /* Raw socket IPv4 header included. */
493 #define IP_RAW_RX_NO_HEADER 35 /* NetX proprietary socket option that does not include
494                                   IPv4/IPv6 header (and extension headers) on received raw sockets.*/
495 #define IP_RAW_IPV6_HDRINCL 36 /* Transmitted buffer over IPv6 socket contains IPv6 header. */
496 #define IP_OPTION_MAX       IP_RAW_IPV6_HDRINCL
497 
498 
499 
500 /*
501  * User-settable options (used with setsockopt).
502  */
503 #define TCP_NODELAY 0x01    /* don't delay send to coalesce packets     */
504 #define TCP_MAXSEG  0x02    /* set maximum segment size                 */
505 #define TCP_NOPUSH  0x04    /* don't push last block of write           */
506 #define TCP_NOOPT   0x08    /* don't use TCP options                    */
507 
508 
509 /* Define data types used in structure timeval.  */
510 
511 #ifdef __CCRX__
512 typedef LONG        time_t;
513 #endif /* __CCRX__ */
514 typedef LONG        suseconds_t;
515 
516 #ifndef __SES_ARM
517 struct timeval
518 {
519     time_t          tv_sec;             /* Seconds      */
520     suseconds_t     tv_usec;            /* Microseconds */
521 };
522 #endif /* __SES_ARM */
523 
524 struct sockaddr_storage
525 {
526     USHORT ss_len;
527     USHORT ss_family;
528 };
529 
530 struct sockaddr
531 {
532     USHORT          sa_family;              /* Address family (e.g. , AF_INET).                 */
533     UCHAR           sa_data[14];            /* Protocol- specific address information.          */
534 };
535 
536 struct in6_addr
537 {
538     union
539     {
540         UCHAR _S6_u8[16];
541         ULONG _S6_u32[4];
542     } _S6_un;
543 };
544 
545 #define s6_addr     _S6_un._S6_u8
546 #define s6_addr32   _S6_un._S6_u32
547 
548 struct sockaddr_in6
549 {
550     USHORT          sin6_family;                 /* AF_INET6 */
551     USHORT          sin6_port;                   /* Transport layer port.  */
552     ULONG           sin6_flowinfo;               /* IPv6 flow information. */
553     struct in6_addr sin6_addr;                   /* IPv6 address. */
554     ULONG           sin6_scope_id;               /* set of interafces for a scope. */
555 
556 };
557 
558 /* Internet address (a structure for historical reasons).  */
559 
560 struct in_addr
561 {
562     ULONG           s_addr;             /* Internet address (32 bits).                         */
563 };
564 
565 typedef ULONG       in_addr_t;
566 typedef ULONG       socklen_t;
567 
568 
569 
570 /* Socket address, Internet style. */
571 
572 struct sockaddr_in
573 {
574     USHORT              sin_family;         /* Internet Protocol (AF_INET).                    */
575     USHORT              sin_port;           /* Address port (16 bits).                         */
576     struct in_addr      sin_addr;           /* Internet address (32 bits).                     */
577     CHAR                sin_zero[8];        /* Not used.                                       */
578 };
579 
580 
581 
582 typedef struct FD_SET_STRUCT                /* The select socket array manager.                                                             */
583 {
584    INT                  fd_count;           /* How many are SET?                                                                            */
585    ULONG                fd_array[(NX_BSD_MAX_SOCKETS + 31)/32]; /* Bit map of SOCKET Descriptors.                                                   */
586 } fd_set;
587 
588 
589 
590 typedef struct NX_BSD_SOCKET_SUSPEND_STRUCT
591 {
592     ULONG               nx_bsd_socket_suspend_actual_flags;
593     fd_set              nx_bsd_socket_suspend_read_fd_set;
594     fd_set              nx_bsd_socket_suspend_write_fd_set;
595     fd_set              nx_bsd_socket_suspend_exception_fd_set;
596 
597 } NX_BSD_SOCKET_SUSPEND;
598 
599 
600 struct ip_mreq
601 {
602     struct in_addr imr_multiaddr;     /* The IPv4 multicast address to join. */
603     struct in_addr imr_interface;     /* The interface to use for this group. */
604 };
605 
606 
607 /* Define additional BSD data structures for supporting socket options.  */
608 
609 struct sock_errno
610 {
611     INT error;                              /* default = 0; */
612 };
613 
614 struct linger
615 {
616     INT l_onoff;                            /* 0 = disabled; 1 = enabled; default = 0;*/
617     INT l_linger;                           /* linger time in seconds; default = 0;*/
618 };
619 
620 struct sock_keepalive
621 {
622     INT keepalive_enabled;                  /* 0 = disabled; 1 = enabled; default = 0;*/
623 };
624 
625 struct sock_reuseaddr
626 {
627     INT reuseaddr_enabled;                  /* 0 = disabled; 1 = enabled; default = 1; */
628 };
629 
630 struct sock_winsize
631 {
632     INT winsize;                            /* receive window size for TCP sockets   ; */
633 };
634 
635 /* Define an union struct for master ID and secondary ID used in NX_BSD_SOCKET_STRUCT. */
636 union UNION_ID
637 {
638     INT nx_bsd_socket_master_socket_id;
639     INT nx_bsd_socket_secondary_socket_id;
640 };
641 
642 
643 struct addrinfo
644 {
645     INT             ai_flags;
646     INT             ai_family;
647     INT             ai_socktype;
648     INT             ai_protocol;
649     socklen_t       ai_addrlen;
650     struct sockaddr *ai_addr;
651     CHAR            *ai_canonname;
652     struct addrinfo  *ai_next;
653 };
654 
655 struct NX_BSD_SERVICE_LIST
656 {
657     CHAR    *service_name;
658     USHORT  service_port;
659     INT     service_socktype;
660     INT     service_protocol;
661 };
662 
663 /* Define the Errors return by getaddrinfo. */
664 
665 /* The specified host doesn't have addresses in the specified address family. */
666 #define EAI_ADDRFAMILY  40
667 /* Name server temporary failure. */
668 #define EAI_AGAIN       41
669 /* hints.si_flags contains invalid flag. */
670 #define EAI_BADFLAGS    42
671 /* DNS fail. */
672 #define EAI_FAIL        43
673 /* Invalid address family. */
674 #define EAI_FAMILY      44
675 /* memory failure. */
676 #define EAI_MEMORY      45
677 /* host exsits, but doesn't have address in specified family. */
678 #define EAI_NODATA      46
679 #define EAI_NONAME      47
680 /* service not available for the specified socket type. */
681 #define EAI_SERVICE     48
682 #define EAI_OVERFLOW    49
683 /* invalid socktype. */
684 #define EAI_SOCKTYPE    50
685 #define EAI_SYSTEM      51
686 
687 
688 /* Define ai_flags value. */
689 #define AI_PASSIVE      0x0001
690 /* request CNAME. */
691 #define AI_CANONNAME    0x0002
692 /* host must be a address string. */
693 #define AI_NUMERICHOST  0x0004
694 /* service must be a port string. */
695 #define AI_NUMERICSERV  0x0008
696 #define AI_V4MAPPED     0x0010
697 #define AI_ALL          0x0020
698 #define AI_ADDRCONFIG   0x0040
699 
700 /* Return numeric string for hostname. */
701 #define NI_NUMERICHOST  0x0001
702 /* Return numeric string for service name. */
703 #define NI_NUMERICSERV  0x0002
704 /* Return only hostname portion of FQDN. */
705 #define NI_NOFQDN       0x0004
706 /* Return error if name can't be resolved from address. */
707 #define NI_NAMEREQD     0x0008
708 /* Datagram service. */
709 #define NI_DGRAM        0x0010
710 
711 
712 /* Defines maximum IPv4 addresses for getaddrinfo. */
713 #ifndef NX_BSD_IPV4_ADDR_MAX_NUM
714 #define NX_BSD_IPV4_ADDR_MAX_NUM 5
715 #endif /* NX_BSD_IPV4_ADDR_MAX_NUM  */
716 
717 /* Defines maximum IPv6 addresses for getaddrinfo. */
718 #ifndef NX_BSD_IPV6_ADDR_MAX_NUM
719 #define NX_BSD_IPV6_ADDR_MAX_NUM 5
720 #endif /* NX_BSD_IPV6_ADDR_MAX_NUM  */
721 
722 /* Defines maximum IPv4 addresses stored from DNS. */
723 #ifndef NX_BSD_IPV4_ADDR_PER_HOST
724 #define NX_BSD_IPV4_ADDR_PER_HOST 5
725 #endif /* NX_BSD_IPV4_ADDR_PER_HOST */
726 
727 /* Defines maximum IPv6 addresses stored from DNS. */
728 #ifndef NX_BSD_IPV6_ADDR_PER_HOST
729 #define NX_BSD_IPV6_ADDR_PER_HOST 2
730 #endif /* NX_BSD_IPV6_ADDR_PER_HOST */
731 
732 /* Define the BSD socket status bits. */
733 #define NX_BSD_SOCKET_CONNECTION_INPROGRESS      1
734 #define NX_BSD_SOCKET_ERROR                      (1 << 1)
735 #define NX_BSD_SOCKET_CONNECTED                  (1 << 2)
736 /* Disconnected from the stack. */
737 #define NX_BSD_SOCKET_DISCONNECT_FROM_STACK      (1 << 3)
738 #define NX_BSD_SOCKET_SERVER_MASTER_SOCKET       (1 << 4)
739 #define NX_BSD_SOCKET_SERVER_SECONDARY_SOCKET    (1 << 5)
740 #define NX_BSD_SOCKET_TX_HDR_INCLUDE             (1 << 6)
741 #define NX_BSD_SOCKET_RX_NO_HDR                  (1 << 7)
742 #define NX_BSD_SOCKET_IN_USE                     (1 << 8)
743 #define NX_BSD_SOCKET_CLIENT                     (1 << 9)
744 #define NX_BSD_SOCKET_ENABLE_LISTEN              (1 << 10)
745 #define NX_BSD_SOCKET_BOUND                      (1 << 11)
746 #define NX_BSD_SOCKET_ACCEPTING                  (1 << 12)
747 #define NX_BSD_SOCKET_CONNECTION_REQUEST         (1 << 13)
748 #define NX_BSD_SOCKET_DISCONNECTION_REQUEST      (1 << 14)
749 
750 /* Define the BSD socket options bits. */
751 #define NX_BSD_SOCKET_ENABLE_RAW_SOCKET             1
752 #define NX_BSD_SOCKET_ENABLE_OPTION_LINGER          (1 << 1)
753 #define NX_BSD_SOCKET_ENABLE_OPTION_REUSEADDR       (1 << 2)
754 #define NX_BSD_SOCKET_ENABLE_OPTION_NON_BLOCKING    (1 << 3)
755 
756 /* Define the internal management structure for the BSD layer.  */
757 
758 typedef struct NX_BSD_SOCKET_STRUCT
759 {
760     NX_TCP_SOCKET       *nx_bsd_socket_tcp_socket;
761     NX_UDP_SOCKET       *nx_bsd_socket_udp_socket;
762     ULONG               nx_bsd_socket_family;
763     /* Store the protocol number.  For example TCP is 6, UDP is 17. For raw socket
764        it is the protocol it wishes to receive. */
765     USHORT              nx_bsd_socket_protocol;
766     TX_THREAD           *nx_bsd_socket_busy;
767     union UNION_ID      nx_bsd_socket_union_id;
768     NX_PACKET*          nx_bsd_socket_received_packet;
769     NX_PACKET*          nx_bsd_socket_received_packet_tail;
770     UINT                nx_bsd_socket_received_byte_count;
771     UINT                nx_bsd_socket_received_byte_count_max;
772     UINT                nx_bsd_socket_received_packet_count;
773     UINT                nx_bsd_socket_received_packet_count_max;
774     ULONG               nx_bsd_socket_received_packet_offset;
775     INT                 nx_bsd_socket_source_port;
776     ULONG               nx_bsd_socket_local_bind_interface;
777     UINT                nx_bsd_socket_local_bind_interface_index;
778     NXD_ADDRESS         nx_bsd_socket_source_ip_address;
779     NXD_ADDRESS         nx_bsd_socket_peer_ip;
780 
781     /* For TCP/UDP, the local port is the port number this socket receives on.
782        For raw socket this field is not used. */
783     USHORT              nx_bsd_socket_local_port;
784     USHORT              nx_bsd_socket_peer_port;
785     INT                 nx_bsd_option_linger_time;
786     UINT                nx_bsd_option_linger_time_closed;
787     UINT                nx_bsd_option_linger_start_close;
788     UINT                nx_bsd_socket_time_wait_remaining;
789     ULONG               nx_bsd_option_receive_timeout;
790     ULONG               nx_bsd_option_send_timeout;
791     INT                 nx_bsd_file_descriptor_flags;
792     ULONG               nx_bsd_socket_status_flags;
793     ULONG               nx_bsd_socket_option_flags;
794     int                 nx_bsd_socket_error_code;
795 
796     struct NX_BSD_SOCKET_STRUCT
797                         *nx_bsd_socket_next;
798     struct NX_BSD_SOCKET_STRUCT
799                         *nx_bsd_socket_previous;
800 
801     INT                 nx_bsd_socket_id;
802 #ifdef NX_BSD_RAW_PPPOE_SUPPORT
803     UINT                nx_bsd_socket_create_id;
804 #endif /* NX_BSD_RAW_PPPOE_SUPPORT */
805 #if defined(NX_BSD_RAW_SUPPORT) || defined(NX_BSD_RAW_PPPOE_SUPPORT)
806     UCHAR               nx_bsd_socket_sll_addr[6];
807     USHORT              nx_bsd_socket_sll_protocol;
808     INT                 nx_bsd_socket_sll_ifindex;
809 #endif /* defined(NX_BSD_RAW_SUPPORT) || defined(NX_BSD_RAW_PPPOE_SUPPORT) */
810 
811 } NX_BSD_SOCKET;
812 
813 
814 /* Define the BSD function prototypes for use by the application.  */
815 
816 INT  accept(INT sockID, struct sockaddr *ClientAddress, INT *addressLength);
817 INT  bsd_initialize(NX_IP *default_ip, NX_PACKET_POOL *default_pool, CHAR *bsd_thread_stack_area, ULONG bsd_thread_stack_size, UINT bsd_thread_priority);
818 INT  bind(INT sockID, struct sockaddr *localAddress, INT addressLength);
819 INT  connect(INT sockID, struct sockaddr *remoteAddress, INT addressLength);
820 INT  getpeername(INT sockID, struct sockaddr *remoteAddress, INT *addressLength);
821 INT  getsockname(INT sockID, struct sockaddr *localAddress, INT *addressLength);
822 INT  ioctl(INT sockID, INT command, INT *result);
823 in_addr_t inet_addr(const CHAR *buffer);
824 CHAR *inet_ntoa(struct in_addr address_to_convert);
825 INT  inet_aton(const CHAR *cp_arg, struct in_addr *addr);
826 INT  inet_pton(INT af, const CHAR *src, VOID *dst);
827 const CHAR *inet_ntop(INT af, const VOID *src, CHAR *dst, socklen_t size);
828 INT  listen(INT sockID, INT backlog);
829 #ifdef NX_ENABLE_IP_RAW_PACKET_FILTER
830 UINT nx_bsd_raw_packet_receive(NX_BSD_SOCKET *bsd_socket_ptr, NX_PACKET **packet_ptr);
831 UINT nx_bsd_raw_packet_info_extract(NX_PACKET *packet_ptr, NXD_ADDRESS *nxd_address, UINT *interface_index);
832 VOID nx_bsd_raw_receive_notify(NX_IP *ip_ptr, UINT bsd_socket_index);
833 #endif
834 UINT nx_bsd_socket_set_inherited_settings(UINT master_sock_id, UINT secondary_sock_id);
835 INT  recvfrom(INT sockID, CHAR *buffer, INT buffersize, INT flags,struct sockaddr *fromAddr, INT *fromAddrLen);
836 INT  recv(INT sockID, VOID *rcvBuffer, INT bufferLength, INT flags);
837 INT  sendto(INT sockID, CHAR *msg, INT msgLength, INT flags, struct sockaddr *destAddr, INT destAddrLen);
838 INT  send(INT sockID, const CHAR *msg, INT msgLength, INT flags);
839 INT  select(INT nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
840 INT  soc_close( INT sockID);
841 INT  socket(INT protocolFamily, INT type, INT protocol);
842 INT  fcntl(INT sock_ID, UINT flag_type, UINT f_options);
843 INT  getsockopt(INT sockID, INT option_level, INT option_name, VOID *option_value, INT *option_length);
844 INT  setsockopt(INT sockID, INT option_level, INT option_name, const VOID *option_value, INT option_length);
845 INT  getaddrinfo(const CHAR *node, const CHAR *service, const struct addrinfo *hints, struct addrinfo **res);
846 VOID freeaddrinfo(struct addrinfo *res);
847 INT  getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags);
848 VOID nx_bsd_set_service_list(struct NX_BSD_SERVICE_LIST *serv_list_ptr, ULONG serv_list_len);
849 
850 #undef FD_SET
851 #undef FD_CLR
852 #undef FD_ISSET
853 #undef FD_ZERO
854 
855 VOID FD_SET(INT fd, fd_set *fdset);
856 VOID FD_CLR(INT fd, fd_set *fdset);
857 INT  FD_ISSET(INT fd, fd_set *fdset);
858 VOID FD_ZERO(fd_set *fdset);
859 VOID set_errno(INT tx_errno);
860 int  _nxd_get_errno(VOID);
861 #define errno (_nxd_get_errno())
862 
863 #ifdef NX_BSD_RAW_PPPOE_SUPPORT
864 
865 #define ETHERTYPE_PPPOE_DISC            0x8863  /* PPPoE Discovery protocol type */
866 #define ETHERTYPE_PPPOE_SESS            0x8864  /* PPPoE Session Protocol type */
867 
868 #define NX_LINK_PACKET_PPPOE_SESS_SEND   ETHERTYPE_PPPOE_SESS
869 #define NX_LINK_PACKET_PPPOE_DISC_SEND   ETHERTYPE_PPPOE_DISC
870 
871 
872 UINT  _nx_bsd_pppoe_packet_received(NX_PACKET *packet_ptr, UINT frame_type, UINT interface_id);
873 #endif /* NX_BSD_RAW_PPPOE_SUPPORT */
874 
875 #if defined(NX_BSD_RAW_SUPPORT) || defined(NX_BSD_RAW_PPPOE_SUPPORT)
876 /* Link Layer Socket Addressing */
877 struct sockaddr_ll
878 {
879     USHORT sll_family;   /* Address Family.  Must be AF_PACKET */
880     USHORT sll_protocol; /* LL frame type */
881     INT    sll_ifindex;  /* Interface Index. */
882     USHORT sll_hatype;   /* Header type */
883     UCHAR  sll_pkttype;  /* Packet type */
884     UCHAR  sll_halen;    /* Length of address */
885     UCHAR  sll_addr[8];  /* Physical layer address */
886 };
887 #endif /* defined(NX_BSD_RAW_SUPPORT) || defined(NX_BSD_RAW_PPPOE_SUPPORT) */
888 
889 #ifdef NX_BSD_RAW_SUPPORT
890 extern UINT _nx_driver_hardware_packet_send(NX_PACKET *packet_ptr);                                 /* Send hardware packet. */
891 extern VOID (*_nx_driver_hardware_packet_received_callback)(NX_PACKET *packet_ptr, UCHAR *consumed);/* Callback function pointer when packet is received. */
892 #endif /* NX_BSD_RAW_SUPPORT */
893 
894 
895 /* Determine if a C++ compiler is being used.  If so, complete the standard
896    C conditional started above.  */
897 #ifdef   __cplusplus
898         }
899 #endif
900 
901 #endif  /* NXD_BSD_H */
902 
903