ngx_inet.h revision e18a033b
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#ifndef _NGX_INET_H_INCLUDED_
9#define _NGX_INET_H_INCLUDED_
10
11
12#include <ngx_config.h>
13#include <ngx_core.h>
14
15
16#define NGX_INET_ADDRSTRLEN   (sizeof("255.255.255.255") - 1)
17#define NGX_INET6_ADDRSTRLEN                                                 \
18    (sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") - 1)
19#define NGX_UNIX_ADDRSTRLEN                                                  \
20    (sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path))
21
22#if (NGX_HAVE_UNIX_DOMAIN)
23#define NGX_SOCKADDR_STRLEN   (sizeof("unix:") - 1 + NGX_UNIX_ADDRSTRLEN)
24#elif (NGX_HAVE_INET6)
25#define NGX_SOCKADDR_STRLEN   (NGX_INET6_ADDRSTRLEN + sizeof("[]:65535") - 1)
26#else
27#define NGX_SOCKADDR_STRLEN   (NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1)
28#endif
29
30/* compatibility */
31#define NGX_SOCKADDRLEN       sizeof(ngx_sockaddr_t)
32
33
34typedef union {
35    struct sockaddr           sockaddr;
36    struct sockaddr_in        sockaddr_in;
37#if (NGX_HAVE_INET6)
38    struct sockaddr_in6       sockaddr_in6;
39#endif
40#if (NGX_HAVE_UNIX_DOMAIN)
41    struct sockaddr_un        sockaddr_un;
42#endif
43} ngx_sockaddr_t;
44
45
46typedef struct {
47    in_addr_t                 addr;
48    in_addr_t                 mask;
49} ngx_in_cidr_t;
50
51
52#if (NGX_HAVE_INET6)
53
54typedef struct {
55    struct in6_addr           addr;
56    struct in6_addr           mask;
57} ngx_in6_cidr_t;
58
59#endif
60
61
62typedef struct {
63    ngx_uint_t                family;
64    union {
65        ngx_in_cidr_t         in;
66#if (NGX_HAVE_INET6)
67        ngx_in6_cidr_t        in6;
68#endif
69    } u;
70} ngx_cidr_t;
71
72
73typedef struct {
74    struct sockaddr          *sockaddr;
75    socklen_t                 socklen;
76    ngx_str_t                 name;
77} ngx_addr_t;
78
79
80typedef struct {
81    ngx_str_t                 url;
82    ngx_str_t                 host;
83    ngx_str_t                 port_text;
84    ngx_str_t                 uri;
85
86    in_port_t                 port;
87    in_port_t                 default_port;
88    int                       family;
89
90    unsigned                  listen:1;
91    unsigned                  uri_part:1;
92    unsigned                  no_resolve:1;
93
94    unsigned                  no_port:1;
95    unsigned                  wildcard:1;
96
97    socklen_t                 socklen;
98    ngx_sockaddr_t            sockaddr;
99
100    ngx_addr_t               *addrs;
101    ngx_uint_t                naddrs;
102
103    char                     *err;
104} ngx_url_t;
105
106
107in_addr_t ngx_inet_addr(u_char *text, size_t len);
108#if (NGX_HAVE_INET6)
109ngx_int_t ngx_inet6_addr(u_char *p, size_t len, u_char *addr);
110size_t ngx_inet6_ntop(u_char *p, u_char *text, size_t len);
111#endif
112size_t ngx_sock_ntop(struct sockaddr *sa, socklen_t socklen, u_char *text,
113    size_t len, ngx_uint_t port);
114size_t ngx_inet_ntop(int family, void *addr, u_char *text, size_t len);
115ngx_int_t ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr);
116ngx_int_t ngx_cidr_match(struct sockaddr *sa, ngx_array_t *cidrs);
117ngx_int_t ngx_parse_addr(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,
118    size_t len);
119ngx_int_t ngx_parse_addr_port(ngx_pool_t *pool, ngx_addr_t *addr,
120    u_char *text, size_t len);
121ngx_int_t ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u);
122ngx_int_t ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u);
123ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, socklen_t slen1,
124    struct sockaddr *sa2, socklen_t slen2, ngx_uint_t cmp_port);
125in_port_t ngx_inet_get_port(struct sockaddr *sa);
126void ngx_inet_set_port(struct sockaddr *sa, in_port_t port);
127
128
129#endif /* _NGX_INET_H_INCLUDED_ */
130