1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_SOCKET_H_INCLUDED_
8 #define _NXT_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 NXT_EXPORT nxt_socket_t nxt_socket_create(nxt_task_t *task, nxt_uint_t family,
95     nxt_uint_t type, nxt_uint_t protocol, nxt_uint_t flags);
96 NXT_EXPORT void nxt_socket_defer_accept(nxt_task_t *task, nxt_socket_t s,
97     nxt_sockaddr_t *sa);
98 NXT_EXPORT nxt_int_t nxt_socket_getsockopt(nxt_task_t *task, nxt_socket_t s,
99     nxt_uint_t level, nxt_uint_t sockopt);
100 NXT_EXPORT nxt_int_t nxt_socket_setsockopt(nxt_task_t *task, nxt_socket_t s,
101     nxt_uint_t level, nxt_uint_t sockopt, int val);
102 NXT_EXPORT nxt_int_t nxt_socket_bind(nxt_task_t *task, nxt_socket_t s,
103     nxt_sockaddr_t *sa);
104 NXT_EXPORT nxt_int_t nxt_socket_connect(nxt_task_t *task, nxt_socket_t s,
105     nxt_sockaddr_t *sa);
106 NXT_EXPORT void nxt_socket_shutdown(nxt_task_t *task, nxt_socket_t s,
107     nxt_uint_t how);
108 NXT_EXPORT void nxt_socket_close(nxt_task_t *task, nxt_socket_t s);
109 nxt_err_t nxt_socket_error(nxt_socket_t s);
110 nxt_uint_t nxt_socket_error_level(nxt_err_t err);
111 
112 NXT_EXPORT nxt_int_t nxt_socketpair_create(nxt_task_t *task,
113     nxt_socket_t *pair);
114 NXT_EXPORT void nxt_socketpair_close(nxt_task_t *task, nxt_socket_t *pair);
115 NXT_EXPORT ssize_t nxt_socketpair_send(nxt_fd_event_t *ev, nxt_fd_t *fd,
116     nxt_iobuf_t *iob, nxt_uint_t niob);
117 NXT_EXPORT ssize_t nxt_socketpair_recv(nxt_fd_event_t *ev,
118     nxt_iobuf_t *iob, nxt_uint_t niob, void *oob);
119 
120 
121 #define                                                                       \
122 nxt_socket_nonblocking(task, fd)                                              \
123     nxt_fd_nonblocking(task, fd)
124 
125 #define                                                                       \
126 nxt_socket_blocking(task, fd)                                                 \
127     nxt_fd_blocking(task, fd)
128 
129 
130 #endif /* _NXT_SOCKET_H_INCLUDED_ */
131