nxt_sockaddr.c (20:4dc92b438f58) nxt_sockaddr.c (65:10688b89aa16)
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) NGINX, Inc.
5 */
6
7#include <nxt_main.h>
8
9
10#if (NXT_INET6)
11static u_char *nxt_inet6_ntop(u_char *addr, u_char *buf, u_char *end);
12#endif
13
14static nxt_int_t nxt_job_sockaddr_unix_parse(nxt_job_sockaddr_parse_t *jbs);
15static nxt_int_t nxt_job_sockaddr_inet6_parse(nxt_job_sockaddr_parse_t *jbs);
16static nxt_int_t nxt_job_sockaddr_inet_parse(nxt_job_sockaddr_parse_t *jbs);
17
18
19nxt_sockaddr_t *
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) NGINX, Inc.
5 */
6
7#include <nxt_main.h>
8
9
10#if (NXT_INET6)
11static u_char *nxt_inet6_ntop(u_char *addr, u_char *buf, u_char *end);
12#endif
13
14static nxt_int_t nxt_job_sockaddr_unix_parse(nxt_job_sockaddr_parse_t *jbs);
15static nxt_int_t nxt_job_sockaddr_inet6_parse(nxt_job_sockaddr_parse_t *jbs);
16static nxt_int_t nxt_job_sockaddr_inet_parse(nxt_job_sockaddr_parse_t *jbs);
17
18
19nxt_sockaddr_t *
20nxt_sockaddr_alloc(nxt_mem_pool_t *mp, socklen_t socklen, size_t address_length)
20nxt_sockaddr_alloc(nxt_mp_t *mp, socklen_t socklen, size_t address_length)
21{
22 size_t size;
23 nxt_sockaddr_t *sa;
24
25 size = offsetof(nxt_sockaddr_t, u) + socklen + address_length;
26
27 /*
28 * The current struct sockaddr's define 32-bit fields at maximum
29 * and may define 64-bit AF_INET6 fields in the future. Alignment
21{
22 size_t size;
23 nxt_sockaddr_t *sa;
24
25 size = offsetof(nxt_sockaddr_t, u) + socklen + address_length;
26
27 /*
28 * The current struct sockaddr's define 32-bit fields at maximum
29 * and may define 64-bit AF_INET6 fields in the future. Alignment
30 * of memory allocated by nxt_mem_zalloc() is enough for these fields.
30 * of memory allocated by nxt_mp_zalloc() is enough for these fields.
31 * If 128-bit alignment will be required then nxt_mem_malloc() and
32 * nxt_memzero() should be used instead.
33 */
34
31 * If 128-bit alignment will be required then nxt_mem_malloc() and
32 * nxt_memzero() should be used instead.
33 */
34
35 sa = nxt_mem_zalloc(mp, size);
35 sa = nxt_mp_zalloc(mp, size);
36
37 if (nxt_fast_path(sa != NULL)) {
38 sa->socklen = socklen;
39 sa->length = address_length;
40 sa->sockaddr_size = size;
41 }
42
43 return sa;
44}
45
46
47nxt_sockaddr_t *
36
37 if (nxt_fast_path(sa != NULL)) {
38 sa->socklen = socklen;
39 sa->length = address_length;
40 sa->sockaddr_size = size;
41 }
42
43 return sa;
44}
45
46
47nxt_sockaddr_t *
48nxt_sockaddr_create(nxt_mem_pool_t *mp, struct sockaddr *sockaddr,
49 socklen_t length, size_t address_length)
48nxt_sockaddr_create(nxt_mp_t *mp, struct sockaddr *sockaddr, socklen_t length,
49 size_t address_length)
50{
51 size_t size, copy;
52 nxt_sockaddr_t *sa;
53
54 size = length;
55 copy = length;
56
57#if (NXT_HAVE_UNIX_DOMAIN)

--- 57 unchanged lines hidden (view full) ---

115#endif
116 }
117
118 return sa;
119}
120
121
122nxt_sockaddr_t *
50{
51 size_t size, copy;
52 nxt_sockaddr_t *sa;
53
54 size = length;
55 copy = length;
56
57#if (NXT_HAVE_UNIX_DOMAIN)

--- 57 unchanged lines hidden (view full) ---

115#endif
116 }
117
118 return sa;
119}
120
121
122nxt_sockaddr_t *
123nxt_sockaddr_copy(nxt_mem_pool_t *mp, nxt_sockaddr_t *src)
123nxt_sockaddr_copy(nxt_mp_t *mp, nxt_sockaddr_t *src)
124{
125 size_t length;
126 nxt_sockaddr_t *dst;
127
128 length = offsetof(nxt_sockaddr_t, u) + src->socklen;
129
124{
125 size_t length;
126 nxt_sockaddr_t *dst;
127
128 length = offsetof(nxt_sockaddr_t, u) + src->socklen;
129
130 dst = nxt_mem_alloc(mp, length);
130 dst = nxt_mp_alloc(mp, length);
131
132 if (nxt_fast_path(dst != NULL)) {
133 nxt_memcpy(dst, src, length);
134 }
135
136 return dst;
137}
138
139
140nxt_sockaddr_t *
131
132 if (nxt_fast_path(dst != NULL)) {
133 nxt_memcpy(dst, src, length);
134 }
135
136 return dst;
137}
138
139
140nxt_sockaddr_t *
141nxt_getsockname(nxt_task_t *task, nxt_mem_pool_t *mp, nxt_socket_t s)
141nxt_getsockname(nxt_task_t *task, nxt_mp_t *mp, nxt_socket_t s)
142{
143 int ret;
144 size_t length;
145 socklen_t socklen;
146 nxt_sockaddr_buf_t sockaddr;
147
148 socklen = NXT_SOCKADDR_LEN;
149

--- 453 unchanged lines hidden (view full) ---

603
604
605static nxt_int_t
606nxt_job_sockaddr_unix_parse(nxt_job_sockaddr_parse_t *jbs)
607{
608#if (NXT_HAVE_UNIX_DOMAIN)
609 size_t length, socklen;
610 u_char *path;
142{
143 int ret;
144 size_t length;
145 socklen_t socklen;
146 nxt_sockaddr_buf_t sockaddr;
147
148 socklen = NXT_SOCKADDR_LEN;
149

--- 453 unchanged lines hidden (view full) ---

603
604
605static nxt_int_t
606nxt_job_sockaddr_unix_parse(nxt_job_sockaddr_parse_t *jbs)
607{
608#if (NXT_HAVE_UNIX_DOMAIN)
609 size_t length, socklen;
610 u_char *path;
611 nxt_mem_pool_t *mp;
611 nxt_mp_t *mp;
612 nxt_sockaddr_t *sa;
613
614 /*
615 * Actual sockaddr_un length can be lesser or even larger than defined
616 * struct sockaddr_un length (see comment in unix/nxt_socket.h). So
617 * limit maximum Unix domain socket address length by defined sun_path[]
618 * length because some OSes accept addresses twice larger than defined
619 * struct sockaddr_un. Also reserve space for a trailing zero to avoid

--- 30 unchanged lines hidden (view full) ---

650 if (path[0] == '\0') {
651 socklen--;
652 }
653
654#endif
655
656 mp = jbs->resolve.job.mem_pool;
657
612 nxt_sockaddr_t *sa;
613
614 /*
615 * Actual sockaddr_un length can be lesser or even larger than defined
616 * struct sockaddr_un length (see comment in unix/nxt_socket.h). So
617 * limit maximum Unix domain socket address length by defined sun_path[]
618 * length because some OSes accept addresses twice larger than defined
619 * struct sockaddr_un. Also reserve space for a trailing zero to avoid

--- 30 unchanged lines hidden (view full) ---

650 if (path[0] == '\0') {
651 socklen--;
652 }
653
654#endif
655
656 mp = jbs->resolve.job.mem_pool;
657
658 jbs->resolve.sockaddrs = nxt_mem_alloc(mp, sizeof(void *));
658 jbs->resolve.sockaddrs = nxt_mp_alloc(mp, sizeof(void *));
659
660 if (nxt_fast_path(jbs->resolve.sockaddrs != NULL)) {
661 sa = nxt_sockaddr_alloc(mp, socklen, jbs->addr.length);
662
663 if (nxt_fast_path(sa != NULL)) {
664 jbs->resolve.count = 1;
665 jbs->resolve.sockaddrs[0] = sa;
666

--- 18 unchanged lines hidden (view full) ---

685
686
687static nxt_int_t
688nxt_job_sockaddr_inet6_parse(nxt_job_sockaddr_parse_t *jbs)
689{
690#if (NXT_INET6)
691 u_char *p, *addr, *addr_end;
692 size_t length;
659
660 if (nxt_fast_path(jbs->resolve.sockaddrs != NULL)) {
661 sa = nxt_sockaddr_alloc(mp, socklen, jbs->addr.length);
662
663 if (nxt_fast_path(sa != NULL)) {
664 jbs->resolve.count = 1;
665 jbs->resolve.sockaddrs[0] = sa;
666

--- 18 unchanged lines hidden (view full) ---

685
686
687static nxt_int_t
688nxt_job_sockaddr_inet6_parse(nxt_job_sockaddr_parse_t *jbs)
689{
690#if (NXT_INET6)
691 u_char *p, *addr, *addr_end;
692 size_t length;
693 nxt_mp_t *mp;
693 nxt_int_t port;
694 nxt_int_t port;
694 nxt_mem_pool_t *mp;
695 nxt_sockaddr_t *sa;
696 struct in6_addr *in6_addr;
697
698 length = jbs->addr.length - 1;
699 addr = jbs->addr.start + 1;
700
701 addr_end = nxt_memchr(addr, ']', length);
702
703 if (addr_end == NULL) {
704 goto invalid_address;
705 }
706
707 mp = jbs->resolve.job.mem_pool;
708
695 nxt_sockaddr_t *sa;
696 struct in6_addr *in6_addr;
697
698 length = jbs->addr.length - 1;
699 addr = jbs->addr.start + 1;
700
701 addr_end = nxt_memchr(addr, ']', length);
702
703 if (addr_end == NULL) {
704 goto invalid_address;
705 }
706
707 mp = jbs->resolve.job.mem_pool;
708
709 jbs->resolve.sockaddrs = nxt_mem_alloc(mp, sizeof(void *));
709 jbs->resolve.sockaddrs = nxt_mp_alloc(mp, sizeof(void *));
710
711 if (nxt_slow_path(jbs->resolve.sockaddrs == NULL)) {
712 return NXT_ERROR;
713 }
714
715 sa = nxt_sockaddr_alloc(mp, sizeof(struct sockaddr_in6));
716
717 if (nxt_slow_path(sa == NULL)) {

--- 59 unchanged lines hidden (view full) ---

777}
778
779
780static nxt_int_t
781nxt_job_sockaddr_inet_parse(nxt_job_sockaddr_parse_t *jbs)
782{
783 u_char *p, *host;
784 size_t length;
710
711 if (nxt_slow_path(jbs->resolve.sockaddrs == NULL)) {
712 return NXT_ERROR;
713 }
714
715 sa = nxt_sockaddr_alloc(mp, sizeof(struct sockaddr_in6));
716
717 if (nxt_slow_path(sa == NULL)) {

--- 59 unchanged lines hidden (view full) ---

777}
778
779
780static nxt_int_t
781nxt_job_sockaddr_inet_parse(nxt_job_sockaddr_parse_t *jbs)
782{
783 u_char *p, *host;
784 size_t length;
785 in_addr_t addr;
785 nxt_mp_t *mp;
786 nxt_int_t port;
786 nxt_int_t port;
787 nxt_mem_pool_t *mp;
787 in_addr_t addr;
788 nxt_sockaddr_t *sa;
789
790 addr = INADDR_ANY;
791
792 length = jbs->addr.length;
793 host = jbs->addr.start;
794
795 p = nxt_memchr(host, ':', length);

--- 59 unchanged lines hidden (view full) ---

855 }
856
857 /* "x.x.x.x:XX" */
858 }
859 }
860
861 mp = jbs->resolve.job.mem_pool;
862
788 nxt_sockaddr_t *sa;
789
790 addr = INADDR_ANY;
791
792 length = jbs->addr.length;
793 host = jbs->addr.start;
794
795 p = nxt_memchr(host, ':', length);

--- 59 unchanged lines hidden (view full) ---

855 }
856
857 /* "x.x.x.x:XX" */
858 }
859 }
860
861 mp = jbs->resolve.job.mem_pool;
862
863 jbs->resolve.sockaddrs = nxt_mem_alloc(mp, sizeof(void *));
863 jbs->resolve.sockaddrs = nxt_mp_alloc(mp, sizeof(void *));
864 if (nxt_slow_path(jbs->resolve.sockaddrs == NULL)) {
865 return NXT_ERROR;
866 }
867
868 sa = nxt_sockaddr_alloc(mp, sizeof(struct sockaddr_in),
869 NXT_INET_ADDR_STR_LEN);
870
871 if (nxt_fast_path(sa != NULL)) {

--- 206 unchanged lines hidden ---
864 if (nxt_slow_path(jbs->resolve.sockaddrs == NULL)) {
865 return NXT_ERROR;
866 }
867
868 sa = nxt_sockaddr_alloc(mp, sizeof(struct sockaddr_in),
869 NXT_INET_ADDR_STR_LEN);
870
871 if (nxt_fast_path(sa != NULL)) {

--- 206 unchanged lines hidden ---