xref: /unit/src/nxt_sockaddr.h (revision 65:10688b89aa16)
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 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_EXPORT nxt_sockaddr_t *nxt_sockaddr_alloc(nxt_mp_t *mp, socklen_t socklen,
73     size_t address_length)
74     NXT_MALLOC_LIKE;
75 NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_create(nxt_mp_t *mp,
76     struct sockaddr *sockaddr, socklen_t socklen, size_t address_length)
77     NXT_MALLOC_LIKE;
78 NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_copy(nxt_mp_t *mp, nxt_sockaddr_t *src)
79     NXT_MALLOC_LIKE;
80 NXT_EXPORT nxt_sockaddr_t *nxt_getsockname(nxt_task_t *task, nxt_mp_t *mp,
81     nxt_socket_t s)
82     NXT_MALLOC_LIKE;
83 NXT_EXPORT void nxt_sockaddr_text(nxt_sockaddr_t *sa);
84 
85 
86 NXT_EXPORT uint32_t nxt_sockaddr_port_number(nxt_sockaddr_t *sa);
87 NXT_EXPORT nxt_bool_t nxt_sockaddr_cmp(nxt_sockaddr_t *sa1,
88     nxt_sockaddr_t *sa2);
89 NXT_EXPORT size_t nxt_sockaddr_ntop(nxt_sockaddr_t *sa, u_char *buf,
90     u_char *end, nxt_bool_t port);
91 NXT_EXPORT void nxt_job_sockaddr_parse(nxt_job_sockaddr_parse_t *jbs);
92 NXT_EXPORT in_addr_t nxt_inet_addr(u_char *buf, size_t len);
93 #if (NXT_INET6)
94 NXT_EXPORT nxt_int_t nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf,
95     size_t len);
96 #endif
97 
98 
99 #define NXT_INET_ADDR_STR_LEN     (sizeof("255.255.255.255:65535") - 1)
100 
101 #define NXT_INET6_ADDR_STR_LEN                                                \
102     (sizeof("[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535") - 1)
103 
104 
105 #define nxt_sockaddr_start(sa)    ((u_char *) (sa) + (sa)->start)
106 #define nxt_sockaddr_address(sa)  ((u_char *) (sa) + (sa)->address_start)
107 #define nxt_sockaddr_port(sa)     ((u_char *) (sa) + (sa)->port_start)
108 #define nxt_sockaddr_length(sa)   ((sa)->length - (sa)->port_start)
109 
110 
111 #endif /* _NXT_SOCKADDR_H_INCLUDED_ */
112