Klaus Demo nginx / fd064d3
Introduced the ngx_sockaddr_t type. It's properly aligned and can hold any supported sockaddr. Ruslan Ermilov 6 years ago
12 changed file(s) with 63 addition(s) and 91 deletion(s). Raw diff Collapse all Expand all
150150 ls = cycle->listening.elts;
151151 for (i = 0; i < cycle->listening.nelts; i++) {
152152
153 ls[i].sockaddr = ngx_palloc(cycle->pool, NGX_SOCKADDRLEN);
153 ls[i].sockaddr = ngx_palloc(cycle->pool, sizeof(ngx_sockaddr_t));
154154 if (ls[i].sockaddr == NULL) {
155155 return NGX_ERROR;
156156 }
157157
158 ls[i].socklen = NGX_SOCKADDRLEN;
158 ls[i].socklen = sizeof(ngx_sockaddr_t);
159159 if (getsockname(ls[i].fd, ls[i].sockaddr, &ls[i].socklen) == -1) {
160160 ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno,
161161 "getsockname() of the inherited "
12771277 {
12781278 socklen_t len;
12791279 ngx_uint_t addr;
1280 u_char sa[NGX_SOCKADDRLEN];
1280 ngx_sockaddr_t sa;
12811281 struct sockaddr_in *sin;
12821282 #if (NGX_HAVE_INET6)
12831283 ngx_uint_t i;
13151315
13161316 if (addr == 0) {
13171317
1318 len = NGX_SOCKADDRLEN;
1319
1320 if (getsockname(c->fd, (struct sockaddr *) &sa, &len) == -1) {
1318 len = sizeof(ngx_sockaddr_t);
1319
1320 if (getsockname(c->fd, &sa.sockaddr, &len) == -1) {
13211321 ngx_connection_error(c, ngx_socket_errno, "getsockname() failed");
13221322 return NGX_ERROR;
13231323 }
762762 return NGX_ERROR;
763763 }
764764
765 ngx_memcpy(sin, u->sockaddr, sizeof(struct sockaddr_in));
765 ngx_memcpy(sin, &u->sockaddr, sizeof(struct sockaddr_in));
766766
767767 u->addrs[0].sockaddr = (struct sockaddr *) sin;
768768 u->addrs[0].socklen = sizeof(struct sockaddr_in);
789789
790790 u->family = u->addrs[0].sockaddr->sa_family;
791791 u->socklen = u->addrs[0].socklen;
792 ngx_memcpy(u->sockaddr, u->addrs[0].sockaddr, u->addrs[0].socklen);
792 ngx_memcpy(&u->sockaddr, u->addrs[0].sockaddr, u->addrs[0].socklen);
793793
794794 switch (u->family) {
795795
919919 return NGX_ERROR;
920920 }
921921
922 ngx_memcpy(sin6, u->sockaddr, sizeof(struct sockaddr_in6));
922 ngx_memcpy(sin6, &u->sockaddr, sizeof(struct sockaddr_in6));
923923
924924 u->addrs[0].sockaddr = (struct sockaddr *) sin6;
925925 u->addrs[0].socklen = sizeof(struct sockaddr_in6);
1212 #include <ngx_core.h>
1313
1414
15 /*
16 * TODO: autoconfigure NGX_SOCKADDRLEN and NGX_SOCKADDR_STRLEN as
17 * sizeof(struct sockaddr_storage)
18 * sizeof(struct sockaddr_un)
19 * sizeof(struct sockaddr_in6)
20 * sizeof(struct sockaddr_in)
21 */
22
2315 #define NGX_INET_ADDRSTRLEN (sizeof("255.255.255.255") - 1)
2416 #define NGX_INET6_ADDRSTRLEN \
2517 (sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") - 1)
2820
2921 #if (NGX_HAVE_UNIX_DOMAIN)
3022 #define NGX_SOCKADDR_STRLEN (sizeof("unix:") - 1 + NGX_UNIX_ADDRSTRLEN)
23 #elif (NGX_HAVE_INET6)
24 #define NGX_SOCKADDR_STRLEN (NGX_INET6_ADDRSTRLEN + sizeof("[]:65535") - 1)
3125 #else
32 #define NGX_SOCKADDR_STRLEN (NGX_INET6_ADDRSTRLEN + sizeof("[]:65535") - 1)
26 #define NGX_SOCKADDR_STRLEN (NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1)
3327 #endif
3428
29 /* compatibility */
30 #define NGX_SOCKADDRLEN sizeof(ngx_sockaddr_t)
31
32
33 typedef union {
34 struct sockaddr sockaddr;
35 struct sockaddr_in sockaddr_in;
36 #if (NGX_HAVE_INET6)
37 struct sockaddr_in6 sockaddr_in6;
38 #endif
3539 #if (NGX_HAVE_UNIX_DOMAIN)
36 #define NGX_SOCKADDRLEN sizeof(struct sockaddr_un)
37 #else
38 #define NGX_SOCKADDRLEN 512
40 struct sockaddr_un sockaddr_un;
3941 #endif
42 } ngx_sockaddr_t;
4043
4144
4245 typedef struct {
9194 unsigned wildcard:1;
9295
9396 socklen_t socklen;
94 u_char sockaddr[NGX_SOCKADDRLEN];
97 ngx_sockaddr_t sockaddr;
9598
9699 ngx_addr_t *addrs;
97100 ngx_uint_t naddrs;
29912991 static void
29922992 ngx_resolver_srv_names_handler(ngx_resolver_ctx_t *cctx)
29932993 {
2994 ngx_uint_t i;
2995 u_char (*sockaddr)[NGX_SOCKADDRLEN];
2996 ngx_addr_t *addrs;
2997 ngx_resolver_t *r;
2998 struct sockaddr_in *sin;
2999 ngx_resolver_ctx_t *ctx;
3000 ngx_resolver_srv_name_t *srv;
3001 #if (NGX_HAVE_INET6)
3002 struct sockaddr_in6 *sin6;
2994 ngx_uint_t i;
2995 ngx_addr_t *addrs;
2996 ngx_resolver_t *r;
2997 ngx_sockaddr_t *sockaddr;
2998 struct sockaddr_in *sin;
2999 ngx_resolver_ctx_t *ctx;
3000 ngx_resolver_srv_name_t *srv;
3001 #if (NGX_HAVE_INET6)
3002 struct sockaddr_in6 *sin6;
30033003 #endif
30043004
30053005 r = cctx->resolver;
30253025 return;
30263026 }
30273027
3028 sockaddr = ngx_resolver_alloc(r, cctx->naddrs * NGX_SOCKADDRLEN);
3028 sockaddr = ngx_resolver_alloc(r, cctx->naddrs * sizeof(ngx_sockaddr_t));
30293029 if (sockaddr == NULL) {
30303030 ngx_resolver_free(r, addrs);
30313031 ngx_resolve_name_done(cctx);
30383038 }
30393039
30403040 for (i = 0; i < cctx->naddrs; i++) {
3041 addrs[i].sockaddr = (struct sockaddr *) sockaddr[i];
3041 addrs[i].sockaddr = &sockaddr[i].sockaddr;
30423042 addrs[i].socklen = cctx->addrs[i].socklen;
30433043
3044 ngx_memcpy(sockaddr[i], cctx->addrs[i].sockaddr,
3044 ngx_memcpy(&sockaddr[i], cctx->addrs[i].sockaddr,
30453045 addrs[i].socklen);
30463046
30473047 switch (addrs[i].sockaddr->sa_family) {
41604160 ngx_resolver_export(ngx_resolver_t *r, ngx_resolver_node_t *rn,
41614161 ngx_uint_t rotate)
41624162 {
4163 ngx_uint_t d, i, j, n;
4164 u_char (*sockaddr)[NGX_SOCKADDRLEN];
4165 in_addr_t *addr;
4166 struct sockaddr_in *sin;
4167 ngx_resolver_addr_t *dst;
4168 #if (NGX_HAVE_INET6)
4169 struct in6_addr *addr6;
4170 struct sockaddr_in6 *sin6;
4163 ngx_uint_t d, i, j, n;
4164 in_addr_t *addr;
4165 ngx_sockaddr_t *sockaddr;
4166 struct sockaddr_in *sin;
4167 ngx_resolver_addr_t *dst;
4168 #if (NGX_HAVE_INET6)
4169 struct in6_addr *addr6;
4170 struct sockaddr_in6 *sin6;
41714171 #endif
41724172
41734173 n = rn->naddrs;
41804180 return NULL;
41814181 }
41824182
4183 sockaddr = ngx_resolver_calloc(r, n * NGX_SOCKADDRLEN);
4183 sockaddr = ngx_resolver_calloc(r, n * sizeof(ngx_sockaddr_t));
41844184 if (sockaddr == NULL) {
41854185 ngx_resolver_free(r, dst);
41864186 return NULL;
41954195 addr = (rn->naddrs == 1) ? &rn->u.addr : rn->u.addrs;
41964196
41974197 do {
4198 sin = (struct sockaddr_in *) sockaddr[d];
4198 sin = &sockaddr[d].sockaddr_in;
41994199 sin->sin_family = AF_INET;
42004200 sin->sin_addr.s_addr = addr[j++];
42014201 dst[d].sockaddr = (struct sockaddr *) sin;
42184218 addr6 = (rn->naddrs6 == 1) ? &rn->u6.addr6 : rn->u6.addrs6;
42194219
42204220 do {
4221 sin6 = (struct sockaddr_in6 *) sockaddr[d];
4221 sin6 = &sockaddr[d].sockaddr_in6;
42224222 sin6->sin6_family = AF_INET6;
42234223 ngx_memcpy(sin6->sin6_addr.s6_addr, addr6[j++].s6_addr, 16);
42244224 dst[d].sockaddr = (struct sockaddr *) sin6;
2727 ngx_uint_t level;
2828 ngx_socket_t s;
2929 ngx_event_t *rev, *wev;
30 ngx_sockaddr_t sa;
3031 ngx_listening_t *ls;
3132 ngx_connection_t *c, *lc;
3233 ngx_event_conf_t *ecf;
33 u_char sa[NGX_SOCKADDRLEN];
3434 #if (NGX_HAVE_ACCEPT4)
3535 static ngx_uint_t use_accept4 = 1;
3636 #endif
5757 "accept on %V, ready: %d", &ls->addr_text, ev->available);
5858
5959 do {
60 socklen = NGX_SOCKADDRLEN;
60 socklen = sizeof(ngx_sockaddr_t);
6161
6262 #if (NGX_HAVE_ACCEPT4)
6363 if (use_accept4) {
64 s = accept4(lc->fd, (struct sockaddr *) sa, &socklen,
65 SOCK_NONBLOCK);
64 s = accept4(lc->fd, &sa.sockaddr, &socklen, SOCK_NONBLOCK);
6665 } else {
67 s = accept(lc->fd, (struct sockaddr *) sa, &socklen);
66 s = accept(lc->fd, &sa.sockaddr, &socklen);
6867 }
6968 #else
70 s = accept(lc->fd, (struct sockaddr *) sa, &socklen);
69 s = accept(lc->fd, &sa.sockaddr, &socklen);
7170 #endif
7271
7372 if (s == (ngx_socket_t) -1) {
170169 return;
171170 }
172171
173 ngx_memcpy(c->sockaddr, sa, socklen);
172 ngx_memcpy(c->sockaddr, &sa, socklen);
174173
175174 log = ngx_palloc(c->pool, sizeof(ngx_log_t));
176175 if (log == NULL) {
327326 ngx_event_t *rev, *wev;
328327 struct iovec iov[1];
329328 struct msghdr msg;
329 ngx_sockaddr_t sa;
330330 ngx_listening_t *ls;
331331 ngx_event_conf_t *ecf;
332332 ngx_connection_t *c, *lc;
333 u_char sa[NGX_SOCKADDRLEN];
334333 static u_char buffer[65535];
335334
336335 #if (NGX_HAVE_MSGHDR_MSG_CONTROL)
375374 iov[0].iov_len = sizeof(buffer);
376375
377376 msg.msg_name = &sa;
378 msg.msg_namelen = sizeof(sa);
377 msg.msg_namelen = sizeof(ngx_sockaddr_t);
379378 msg.msg_iov = iov;
380379 msg.msg_iovlen = 1;
381380
2828 ngx_connection_t *connection;
2929
3030 socklen_t socklen;
31 u_char sockaddr[NGX_SOCKADDRLEN];
31 ngx_sockaddr_t sockaddr;
3232
3333 } ngx_http_upstream_keepalive_cache_t;
3434
39973997
39983998 ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t));
39993999
4000 ngx_memcpy(&lsopt.u.sockaddr, u.sockaddr, u.socklen);
4000 ngx_memcpy(&lsopt.u.sockaddr, &u.sockaddr, u.socklen);
40014001
40024002 lsopt.socklen = u.socklen;
40034003 lsopt.backlog = NGX_LISTEN_BACKLOG;
5757
5858
5959 typedef struct {
60 union {
61 struct sockaddr sockaddr;
62 struct sockaddr_in sockaddr_in;
63 #if (NGX_HAVE_INET6)
64 struct sockaddr_in6 sockaddr_in6;
65 #endif
66 #if (NGX_HAVE_UNIX_DOMAIN)
67 struct sockaddr_un sockaddr_un;
68 #endif
69 } u;
70
60 ngx_sockaddr_t u;
7161 socklen_t socklen;
7262
7363 unsigned set:1;
2626
2727
2828 typedef struct {
29 union {
30 struct sockaddr sockaddr;
31 struct sockaddr_in sockaddr_in;
32 #if (NGX_HAVE_INET6)
33 struct sockaddr_in6 sockaddr_in6;
34 #endif
35 #if (NGX_HAVE_UNIX_DOMAIN)
36 struct sockaddr_un sockaddr_un;
37 #endif
38 } u;
39
29 ngx_sockaddr_t u;
4030 socklen_t socklen;
4131
4232 /* server ctx */
336336
337337 ngx_memzero(ls, sizeof(ngx_mail_listen_t));
338338
339 ngx_memcpy(&ls->u.sockaddr, u.sockaddr, u.socklen);
339 ngx_memcpy(&ls->u.sockaddr, &u.sockaddr, u.socklen);
340340
341341 ls->socklen = u.socklen;
342342 ls->backlog = NGX_LISTEN_BACKLOG;
3030
3131
3232 typedef struct {
33 union {
34 struct sockaddr sockaddr;
35 struct sockaddr_in sockaddr_in;
36 #if (NGX_HAVE_INET6)
37 struct sockaddr_in6 sockaddr_in6;
38 #endif
39 #if (NGX_HAVE_UNIX_DOMAIN)
40 struct sockaddr_un sockaddr_un;
41 #endif
42 } u;
43
33 ngx_sockaddr_t u;
4434 socklen_t socklen;
4535
4636 /* server ctx */
279279
280280 ngx_memzero(ls, sizeof(ngx_stream_listen_t));
281281
282 ngx_memcpy(&ls->u.sockaddr, u.sockaddr, u.socklen);
282 ngx_memcpy(&ls->u.sockaddr, &u.sockaddr, u.socklen);
283283
284284 ls->socklen = u.socklen;
285285 ls->backlog = NGX_LISTEN_BACKLOG;