xref: /unit/src/nxt_socket.h (revision 0:a63ceefd6ab0)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_UNIX_SOCKET_H_INCLUDED_
8 #define _NXT_UNIX_SOCKET_H_INCLUDED_
9 
10 
11 typedef int  nxt_socket_t;
12 
13 #define NXT_NONBLOCK  1
14 
15 
16 /*
17  * struct sockaddr and struct sockaddr_in are 16 bytes.
18  *
19  * struct sockaddr_in6 is:
20  *     28 bytes on Linux, FreeBSD, MacOSX, NetBSD, OpenBSD, AIX, HP-UX;
21  *     32 bytes on Solaris.
22  *
23  *
24  * struct sockaddr_un is:
25  *     94 bytes on HP-UX;
26  *    106 bytes on FreeBSD, MacOSX, NetBSD, OpenBSD;
27  *    110 bytes on Linux, Solaris;
28  *   1025 bytes on AIX.
29  *
30  * The real maximum sockaddr_un length however different from defined length:
31  *     OpenBSD can accept and return 105 bytes if address is not
32  *         zero-terminated;
33  *     Linux can accept 110 bytes and return 111 bytes;
34  *     MacOSX and NetBSD can accept and return 255 bytes;
35  *     Solaris can accept 257 bytes and return 258 bytes;
36  * FreeBSD maximum sockaddr_un length is equal to defined length.
37  *
38  * POSIX.1g renamed AF_UNIX to AF_LOCAL, however, Solaris up to 10
39  * version lacks AF_LOCAL.  AF_UNIX is defined even on Windows although
40  * struct sockaddr_un is not.
41  *
42  * Unix domain socket address without a trailing zero is accepted at least by:
43  *     Linux, FreeBSD, Solaris, MacOSX, NetBSD, and OpenBSD.
44  * Linux and Solaris add the trailing zero and return sockaddr_un length
45  * increased by one.  Others return sockaddr_un without the trailing zero.
46  *
47  * For unspecified Unix domain socket address
48  *     NetBSD returns sockaddr_un length equal to 106 and fills sun_path[]
49  *         with zeros;
50  *     FreeBSD, Solaris, MacOSX, and OpenBSD return sockaddr_un length
51  *         equal to 16 and fill sun_path[] with zeros;
52  *     Linux returns sockaddr_un length equal to 2 without sun_path[];
53  *
54  *     4.4BSD getsockname() and getpeername() returned zero length.
55  *     This behaviour has been inherited by BSD flavours and has been
56  *     eventually changed in NetBSD 1.2, FreeBSD 3.0, and OpenBSD 5.3.
57  *
58  *
59  * struct sockaddr_storage is:
60  *    128 bytes on Linux, FreeBSD, MacOSX, NetBSD;
61  *    256 bytes on Solaris, OpenBSD, and HP-UX;
62  *   1288 bytes on AIX.
63  *
64  * struct sockaddr_storage is too large on some platforms
65  * or less than real maximum struct sockaddr_un length.
66  */
67 
68 #if (NXT_HAVE_UNIX_DOMAIN)
69 #define NXT_SOCKADDR_LEN     sizeof(struct sockaddr_un)
70 
71 #elif (NXT_HAVE_SOCKADDR_IN6)
72 #define NXT_SOCKADDR_LEN     sizeof(struct sockaddr_in6)
73 
74 #else
75 #define NXT_SOCKADDR_LEN     sizeof(struct sockaddr_in)
76 #endif
77 
78 
79 typedef union {
80     struct sockaddr          buf;
81     uint64_t                 alignment;
82     char                     space[NXT_SOCKADDR_LEN];
83 } nxt_sockaddr_buf_t;
84 
85 
86 /*
87  * MAXHOSTNAMELEN is:
88  *    64 on Linux;
89  *   256 on FreeBSD, Solaris, MacOSX, NetBSD, OpenBSD.
90  */
91 #define NXT_MAXHOSTNAMELEN  MAXHOSTNAMELEN
92 
93 
94 typedef enum {
95     NXT_SOCKET_ERROR_IGNORE       = 0x1,
96     NXT_SOCKET_ECONNRESET_IGNORE  = 0x2,
97     NXT_SOCKET_EINVAL_IGNORE      = 0x4,
98 } nxt_socket_error_level_t;
99 
100 
101 NXT_EXPORT nxt_socket_t nxt_socket_create(nxt_uint_t family, nxt_uint_t type,
102     nxt_uint_t protocol, nxt_uint_t flags);
103 NXT_EXPORT void nxt_socket_close(nxt_socket_t s);
104 NXT_EXPORT nxt_int_t nxt_socket_getsockopt(nxt_socket_t s, nxt_uint_t level,
105     nxt_uint_t sockopt);
106 NXT_EXPORT nxt_int_t nxt_socket_setsockopt(nxt_socket_t s, nxt_uint_t level,
107     nxt_uint_t sockopt, int val);
108 NXT_EXPORT nxt_int_t nxt_socket_bind(nxt_socket_t s, nxt_sockaddr_t *sa,
109    nxt_bool_t test);
110 NXT_EXPORT nxt_int_t nxt_socket_connect(nxt_socket_t s, nxt_sockaddr_t *sa);
111 NXT_EXPORT void nxt_socket_shutdown(nxt_socket_t s, nxt_uint_t how);
112 nxt_uint_t nxt_socket_error_level(nxt_err_t err,
113     nxt_socket_error_level_t level);
114 
115 NXT_EXPORT nxt_int_t nxt_socketpair_create(nxt_socket_t *pair);
116 NXT_EXPORT void nxt_socketpair_close(nxt_socket_t *pair);
117 NXT_EXPORT ssize_t nxt_socketpair_send(nxt_event_fd_t *ev, nxt_fd_t fd,
118    nxt_iobuf_t *iob, nxt_uint_t niob);
119 NXT_EXPORT ssize_t nxt_socketpair_recv(nxt_event_fd_t *ev, nxt_fd_t *fd,
120    nxt_iobuf_t *iob, nxt_uint_t niob);
121 
122 
123 #define                                                                       \
124 nxt_socket_nonblocking(fd)                                                    \
125     nxt_fd_nonblocking(fd)
126 
127 #define                                                                       \
128 nxt_socket_blocking(fd)                                                       \
129     nxt_fd_blocking(fd)
130 
131 
132 #endif /* _NXT_UNIX_SOCKET_H_INCLUDED_ */
133