xref: /unit/src/nxt_socket.h (revision 2084:7d479274f334)
10Sigor@sysoev.ru 
20Sigor@sysoev.ru /*
30Sigor@sysoev.ru  * Copyright (C) Igor Sysoev
40Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
50Sigor@sysoev.ru  */
60Sigor@sysoev.ru 
713Sigor@sysoev.ru #ifndef _NXT_SOCKET_H_INCLUDED_
813Sigor@sysoev.ru #define _NXT_SOCKET_H_INCLUDED_
90Sigor@sysoev.ru 
100Sigor@sysoev.ru 
110Sigor@sysoev.ru typedef int  nxt_socket_t;
120Sigor@sysoev.ru 
130Sigor@sysoev.ru #define NXT_NONBLOCK  1
140Sigor@sysoev.ru 
150Sigor@sysoev.ru 
160Sigor@sysoev.ru /*
170Sigor@sysoev.ru  * struct sockaddr and struct sockaddr_in are 16 bytes.
180Sigor@sysoev.ru  *
190Sigor@sysoev.ru  * struct sockaddr_in6 is:
200Sigor@sysoev.ru  *     28 bytes on Linux, FreeBSD, MacOSX, NetBSD, OpenBSD, AIX, HP-UX;
210Sigor@sysoev.ru  *     32 bytes on Solaris.
220Sigor@sysoev.ru  *
230Sigor@sysoev.ru  *
240Sigor@sysoev.ru  * struct sockaddr_un is:
250Sigor@sysoev.ru  *     94 bytes on HP-UX;
260Sigor@sysoev.ru  *    106 bytes on FreeBSD, MacOSX, NetBSD, OpenBSD;
270Sigor@sysoev.ru  *    110 bytes on Linux, Solaris;
280Sigor@sysoev.ru  *   1025 bytes on AIX.
290Sigor@sysoev.ru  *
300Sigor@sysoev.ru  * The real maximum sockaddr_un length however different from defined length:
310Sigor@sysoev.ru  *     OpenBSD can accept and return 105 bytes if address is not
320Sigor@sysoev.ru  *         zero-terminated;
330Sigor@sysoev.ru  *     Linux can accept 110 bytes and return 111 bytes;
340Sigor@sysoev.ru  *     MacOSX and NetBSD can accept and return 255 bytes;
350Sigor@sysoev.ru  *     Solaris can accept 257 bytes and return 258 bytes;
360Sigor@sysoev.ru  * FreeBSD maximum sockaddr_un length is equal to defined length.
370Sigor@sysoev.ru  *
380Sigor@sysoev.ru  * POSIX.1g renamed AF_UNIX to AF_LOCAL, however, Solaris up to 10
390Sigor@sysoev.ru  * version lacks AF_LOCAL.  AF_UNIX is defined even on Windows although
400Sigor@sysoev.ru  * struct sockaddr_un is not.
410Sigor@sysoev.ru  *
420Sigor@sysoev.ru  * Unix domain socket address without a trailing zero is accepted at least by:
430Sigor@sysoev.ru  *     Linux, FreeBSD, Solaris, MacOSX, NetBSD, and OpenBSD.
440Sigor@sysoev.ru  * Linux and Solaris add the trailing zero and return sockaddr_un length
450Sigor@sysoev.ru  * increased by one.  Others return sockaddr_un without the trailing zero.
460Sigor@sysoev.ru  *
470Sigor@sysoev.ru  * For unspecified Unix domain socket address
480Sigor@sysoev.ru  *     NetBSD returns sockaddr_un length equal to 106 and fills sun_path[]
490Sigor@sysoev.ru  *         with zeros;
500Sigor@sysoev.ru  *     FreeBSD, Solaris, MacOSX, and OpenBSD return sockaddr_un length
510Sigor@sysoev.ru  *         equal to 16 and fill sun_path[] with zeros;
520Sigor@sysoev.ru  *     Linux returns sockaddr_un length equal to 2 without sun_path[];
530Sigor@sysoev.ru  *
540Sigor@sysoev.ru  *     4.4BSD getsockname() and getpeername() returned zero length.
550Sigor@sysoev.ru  *     This behaviour has been inherited by BSD flavours and has been
560Sigor@sysoev.ru  *     eventually changed in NetBSD 1.2, FreeBSD 3.0, and OpenBSD 5.3.
570Sigor@sysoev.ru  *
580Sigor@sysoev.ru  *
590Sigor@sysoev.ru  * struct sockaddr_storage is:
600Sigor@sysoev.ru  *    128 bytes on Linux, FreeBSD, MacOSX, NetBSD;
610Sigor@sysoev.ru  *    256 bytes on Solaris, OpenBSD, and HP-UX;
620Sigor@sysoev.ru  *   1288 bytes on AIX.
630Sigor@sysoev.ru  *
640Sigor@sysoev.ru  * struct sockaddr_storage is too large on some platforms
650Sigor@sysoev.ru  * or less than real maximum struct sockaddr_un length.
660Sigor@sysoev.ru  */
670Sigor@sysoev.ru 
680Sigor@sysoev.ru #if (NXT_HAVE_UNIX_DOMAIN)
690Sigor@sysoev.ru #define NXT_SOCKADDR_LEN     sizeof(struct sockaddr_un)
700Sigor@sysoev.ru 
710Sigor@sysoev.ru #elif (NXT_HAVE_SOCKADDR_IN6)
720Sigor@sysoev.ru #define NXT_SOCKADDR_LEN     sizeof(struct sockaddr_in6)
730Sigor@sysoev.ru 
740Sigor@sysoev.ru #else
750Sigor@sysoev.ru #define NXT_SOCKADDR_LEN     sizeof(struct sockaddr_in)
760Sigor@sysoev.ru #endif
770Sigor@sysoev.ru 
780Sigor@sysoev.ru 
790Sigor@sysoev.ru typedef union {
800Sigor@sysoev.ru     struct sockaddr          buf;
810Sigor@sysoev.ru     uint64_t                 alignment;
820Sigor@sysoev.ru     char                     space[NXT_SOCKADDR_LEN];
830Sigor@sysoev.ru } nxt_sockaddr_buf_t;
840Sigor@sysoev.ru 
850Sigor@sysoev.ru 
860Sigor@sysoev.ru /*
870Sigor@sysoev.ru  * MAXHOSTNAMELEN is:
880Sigor@sysoev.ru  *    64 on Linux;
890Sigor@sysoev.ru  *   256 on FreeBSD, Solaris, MacOSX, NetBSD, OpenBSD.
900Sigor@sysoev.ru  */
910Sigor@sysoev.ru #define NXT_MAXHOSTNAMELEN  MAXHOSTNAMELEN
920Sigor@sysoev.ru 
930Sigor@sysoev.ru 
9413Sigor@sysoev.ru NXT_EXPORT nxt_socket_t nxt_socket_create(nxt_task_t *task, nxt_uint_t family,
9513Sigor@sysoev.ru     nxt_uint_t type, nxt_uint_t protocol, nxt_uint_t flags);
96229Sigor@sysoev.ru NXT_EXPORT void nxt_socket_defer_accept(nxt_task_t *task, nxt_socket_t s,
97229Sigor@sysoev.ru     nxt_sockaddr_t *sa);
9813Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_socket_getsockopt(nxt_task_t *task, nxt_socket_t s,
9913Sigor@sysoev.ru     nxt_uint_t level, nxt_uint_t sockopt);
10013Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_socket_setsockopt(nxt_task_t *task, nxt_socket_t s,
10113Sigor@sysoev.ru     nxt_uint_t level, nxt_uint_t sockopt, int val);
10213Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_socket_bind(nxt_task_t *task, nxt_socket_t s,
1031449Svbart@nginx.com     nxt_sockaddr_t *sa);
10413Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_socket_connect(nxt_task_t *task, nxt_socket_t s,
10513Sigor@sysoev.ru     nxt_sockaddr_t *sa);
10613Sigor@sysoev.ru NXT_EXPORT void nxt_socket_shutdown(nxt_task_t *task, nxt_socket_t s,
10713Sigor@sysoev.ru     nxt_uint_t how);
1081504Sigor@sysoev.ru NXT_EXPORT void nxt_socket_close(nxt_task_t *task, nxt_socket_t s);
1091263Sigor@sysoev.ru nxt_err_t nxt_socket_error(nxt_socket_t s);
11013Sigor@sysoev.ru nxt_uint_t nxt_socket_error_level(nxt_err_t err);
1110Sigor@sysoev.ru 
11213Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_socketpair_create(nxt_task_t *task,
11313Sigor@sysoev.ru     nxt_socket_t *pair);
11413Sigor@sysoev.ru NXT_EXPORT void nxt_socketpair_close(nxt_task_t *task, nxt_socket_t *pair);
1151553Smax.romanov@nginx.com NXT_EXPORT ssize_t nxt_socketpair_send(nxt_fd_event_t *ev, nxt_fd_t *fd,
1161008Szelenkov@nginx.com     nxt_iobuf_t *iob, nxt_uint_t niob);
1171996St.nateldemoura@f5.com NXT_EXPORT ssize_t nxt_socketpair_recv(nxt_fd_event_t *ev,
1181996St.nateldemoura@f5.com     nxt_iobuf_t *iob, nxt_uint_t niob, void *oob);
1190Sigor@sysoev.ru 
1200Sigor@sysoev.ru 
121*2084Salx.manpages@gmail.com #define nxt_socket_nonblocking(task, fd)                                      \
12213Sigor@sysoev.ru     nxt_fd_nonblocking(task, fd)
1230Sigor@sysoev.ru 
124*2084Salx.manpages@gmail.com #define nxt_socket_blocking(task, fd)                                         \
12513Sigor@sysoev.ru     nxt_fd_blocking(task, fd)
1260Sigor@sysoev.ru 
1270Sigor@sysoev.ru 
12813Sigor@sysoev.ru #endif /* _NXT_SOCKET_H_INCLUDED_ */
129