xref: /unit/src/nxt_sockaddr.h (revision 358:40bbd4c2349d)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_SOCKADDR_H_INCLUDED_
8 #define _NXT_SOCKADDR_H_INCLUDED_
9 
10 
11 /*
12  * The nxt_sockaddr_t should be allocated using nxt_sockaddr_alloc()
13  * with actual "struct sockaddr_..." size:
14  *    nxt_sockaddr_alloc(pool, sizeof(struct sockaddr_in))
15  */
16 
17 /*
18  * A textual sockaddr representation is stored after struct sockaddr union
19  * and allocated as a whole.
20  */
21 
22 struct nxt_sockaddr_s {
23     /* Socket type: SOCKS_STREAM, SOCK_DGRAM, etc. */
24     uint8_t                       type;
25     /* Size of struct sockaddr. */
26     uint8_t                       socklen;
27     /*
28      * Textual sockaddr representation, e.g.: "127.0.0.1:8000",
29      * "[::1]:8000", and "unix:/path/to/socket".
30      */
31     uint8_t                       start;
32     uint8_t                       length;
33     /*
34      * Textual address representation, e.g: "127.0.0.1", "::1",
35      * and "unix:/path/to/socket".
36      */
37     uint8_t                       address_start;
38     uint8_t                       address_length;
39     /*
40      * Textual port representation, e.g. "8000".
41      * Port length is (start + length) - port_start.
42      */
43     uint8_t                       port_start;
44 
45     /* A cache hist used to place sockaddr into appropriate free list. */
46     uint8_t                       cache_hint;
47 
48     union {
49         struct sockaddr           sockaddr;
50         struct sockaddr_in        sockaddr_in;
51 #if (NXT_INET6)
52         struct sockaddr_in6       sockaddr_in6;
53 #endif
54 #if (NXT_HAVE_UNIX_DOMAIN)
55         struct sockaddr_un        sockaddr_un;
56 #endif
57     } u;
58 };
59 
60 
61 typedef struct {
62     nxt_job_resolve_t             resolve;
63     nxt_str_t                     addr;
64 
65     uint8_t                       wildcard;   /* 1 bit */
66     uint8_t                       no_port;    /* 1 bit */
67 } nxt_job_sockaddr_parse_t;
68 
69 
70 nxt_sockaddr_t *nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine,
71     nxt_listen_socket_t *ls);
72 void nxt_sockaddr_cache_free(nxt_event_engine_t *engine, nxt_conn_t *c);
73 
74 NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_alloc(nxt_mp_t *mp, socklen_t socklen,
75     size_t address_length)
76     NXT_MALLOC_LIKE;
77 NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_create(nxt_mp_t *mp,
78     struct sockaddr *sockaddr, socklen_t socklen, size_t address_length)
79     NXT_MALLOC_LIKE;
80 NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_copy(nxt_mp_t *mp, nxt_sockaddr_t *src)
81     NXT_MALLOC_LIKE;
82 NXT_EXPORT nxt_sockaddr_t *nxt_getsockname(nxt_task_t *task, nxt_mp_t *mp,
83     nxt_socket_t s)
84     NXT_MALLOC_LIKE;
85 NXT_EXPORT void nxt_sockaddr_text(nxt_sockaddr_t *sa);
86 
87 
88 NXT_EXPORT uint32_t nxt_sockaddr_port_number(nxt_sockaddr_t *sa);
89 NXT_EXPORT nxt_bool_t nxt_sockaddr_cmp(nxt_sockaddr_t *sa1,
90     nxt_sockaddr_t *sa2);
91 NXT_EXPORT size_t nxt_sockaddr_ntop(nxt_sockaddr_t *sa, u_char *buf,
92     u_char *end, nxt_bool_t port);
93 NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_parse(nxt_mp_t *mp, nxt_str_t *addr);
94 NXT_EXPORT void nxt_job_sockaddr_parse(nxt_job_sockaddr_parse_t *jbs);
95 NXT_EXPORT in_addr_t nxt_inet_addr(u_char *buf, size_t len);
96 #if (NXT_INET6)
97 NXT_EXPORT nxt_int_t nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf,
98     size_t len);
99 #endif
100 
101 
102 #define NXT_INET_ADDR_STR_LEN     (sizeof("255.255.255.255:65535") - 1)
103 
104 #define NXT_INET6_ADDR_STR_LEN                                                \
105     (sizeof("[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535") - 1)
106 
107 
108 #define nxt_sockaddr_size(sa)                                                 \
109     (offsetof(nxt_sockaddr_t, u) + sa->socklen + sa->length)
110 #define nxt_sockaddr_start(sa)    nxt_pointer_to(sa, (sa)->start)
111 #define nxt_sockaddr_address(sa)  nxt_pointer_to(sa, (sa)->address_start)
112 #define nxt_sockaddr_port(sa)     nxt_pointer_to(sa, (sa)->port_start)
113 #define nxt_sockaddr_port_length(sa)                                          \
114     (((sa)->start + (sa)->length) - (sa)->port_start)
115 
116 
117 #endif /* _NXT_SOCKADDR_H_INCLUDED_ */
118