xref: /unit/src/nxt_sockaddr.h (revision 337:854a1a440616)
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      * Size of the whole structure: struct sockaddr union and maximal textual
46      * representation, used to place sockaddr into appropriate free list.
47      */
48     uint8_t                       sockaddr_size;
49 
50     union {
51         struct sockaddr           sockaddr;
52         struct sockaddr_in        sockaddr_in;
53 #if (NXT_INET6)
54         struct sockaddr_in6       sockaddr_in6;
55 #endif
56 #if (NXT_HAVE_UNIX_DOMAIN)
57         struct sockaddr_un        sockaddr_un;
58 #endif
59     } u;
60 };
61 
62 
63 typedef struct {
64     nxt_job_resolve_t             resolve;
65     nxt_str_t                     addr;
66 
67     uint8_t                       wildcard;   /* 1 bit */
68     uint8_t                       no_port;    /* 1 bit */
69 } nxt_job_sockaddr_parse_t;
70 
71 
72 nxt_sockaddr_t *nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine,
73     nxt_listen_event_t *lev);
74 void nxt_sockaddr_cache_free(nxt_event_engine_t *engine, nxt_conn_t *c);
75 
76 NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_alloc(nxt_mp_t *mp, socklen_t socklen,
77     size_t address_length)
78     NXT_MALLOC_LIKE;
79 NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_create(nxt_mp_t *mp,
80     struct sockaddr *sockaddr, socklen_t socklen, size_t address_length)
81     NXT_MALLOC_LIKE;
82 NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_copy(nxt_mp_t *mp, nxt_sockaddr_t *src)
83     NXT_MALLOC_LIKE;
84 NXT_EXPORT nxt_sockaddr_t *nxt_getsockname(nxt_task_t *task, nxt_mp_t *mp,
85     nxt_socket_t s)
86     NXT_MALLOC_LIKE;
87 NXT_EXPORT void nxt_sockaddr_text(nxt_sockaddr_t *sa);
88 
89 
90 NXT_EXPORT uint32_t nxt_sockaddr_port_number(nxt_sockaddr_t *sa);
91 NXT_EXPORT nxt_bool_t nxt_sockaddr_cmp(nxt_sockaddr_t *sa1,
92     nxt_sockaddr_t *sa2);
93 NXT_EXPORT size_t nxt_sockaddr_ntop(nxt_sockaddr_t *sa, u_char *buf,
94     u_char *end, nxt_bool_t port);
95 NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_parse(nxt_mp_t *mp, nxt_str_t *addr);
96 NXT_EXPORT void nxt_job_sockaddr_parse(nxt_job_sockaddr_parse_t *jbs);
97 NXT_EXPORT in_addr_t nxt_inet_addr(u_char *buf, size_t len);
98 #if (NXT_INET6)
99 NXT_EXPORT nxt_int_t nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf,
100     size_t len);
101 #endif
102 
103 
104 #define NXT_INET_ADDR_STR_LEN     (sizeof("255.255.255.255:65535") - 1)
105 
106 #define NXT_INET6_ADDR_STR_LEN                                                \
107     (sizeof("[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535") - 1)
108 
109 
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