1 /*
2 * ngtcp2
3 *
4 * Copyright (c) 2019 ngtcp2 contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "ngtcp2_addr.h"
26
27 #include <string.h>
28 #include <assert.h>
29
ngtcp2_addr_init(ngtcp2_addr * dest,const ngtcp2_sockaddr * addr,ngtcp2_socklen addrlen)30 ngtcp2_addr *ngtcp2_addr_init(ngtcp2_addr *dest, const ngtcp2_sockaddr *addr,
31 ngtcp2_socklen addrlen) {
32 dest->addrlen = addrlen;
33 dest->addr = (ngtcp2_sockaddr *)addr;
34 return dest;
35 }
36
ngtcp2_addr_copy(ngtcp2_addr * dest,const ngtcp2_addr * src)37 void ngtcp2_addr_copy(ngtcp2_addr *dest, const ngtcp2_addr *src) {
38 dest->addrlen = src->addrlen;
39 if (src->addrlen) {
40 memcpy(dest->addr, src->addr, (size_t)src->addrlen);
41 }
42 }
43
ngtcp2_addr_copy_byte(ngtcp2_addr * dest,const ngtcp2_sockaddr * addr,ngtcp2_socklen addrlen)44 void ngtcp2_addr_copy_byte(ngtcp2_addr *dest, const ngtcp2_sockaddr *addr,
45 ngtcp2_socklen addrlen) {
46 dest->addrlen = addrlen;
47 if (addrlen) {
48 memcpy(dest->addr, addr, (size_t)addrlen);
49 }
50 }
51
sockaddr_eq(const ngtcp2_sockaddr * a,const ngtcp2_sockaddr * b)52 static int sockaddr_eq(const ngtcp2_sockaddr *a, const ngtcp2_sockaddr *b) {
53 assert(a->sa_family == b->sa_family);
54
55 switch (a->sa_family) {
56 case NGTCP2_AF_INET: {
57 const ngtcp2_sockaddr_in *ai = (const ngtcp2_sockaddr_in *)(void *)a,
58 *bi = (const ngtcp2_sockaddr_in *)(void *)b;
59 return ai->sin_port == bi->sin_port &&
60 memcmp(&ai->sin_addr, &bi->sin_addr, sizeof(ai->sin_addr)) == 0;
61 }
62 case NGTCP2_AF_INET6: {
63 const ngtcp2_sockaddr_in6 *ai = (const ngtcp2_sockaddr_in6 *)(void *)a,
64 *bi = (const ngtcp2_sockaddr_in6 *)(void *)b;
65 return ai->sin6_port == bi->sin6_port &&
66 memcmp(&ai->sin6_addr, &bi->sin6_addr, sizeof(ai->sin6_addr)) == 0;
67 }
68 default:
69 assert(0);
70 abort();
71 }
72 }
73
ngtcp2_addr_eq(const ngtcp2_addr * a,const ngtcp2_addr * b)74 int ngtcp2_addr_eq(const ngtcp2_addr *a, const ngtcp2_addr *b) {
75 return a->addr->sa_family == b->addr->sa_family &&
76 sockaddr_eq(a->addr, b->addr);
77 }
78
ngtcp2_addr_compare(const ngtcp2_addr * aa,const ngtcp2_addr * bb)79 uint32_t ngtcp2_addr_compare(const ngtcp2_addr *aa, const ngtcp2_addr *bb) {
80 uint32_t flags = NGTCP2_ADDR_COMPARE_FLAG_NONE;
81 const ngtcp2_sockaddr *a = aa->addr;
82 const ngtcp2_sockaddr *b = bb->addr;
83
84 if (a->sa_family != b->sa_family) {
85 return NGTCP2_ADDR_COMPARE_FLAG_FAMILY;
86 }
87
88 switch (a->sa_family) {
89 case NGTCP2_AF_INET: {
90 const ngtcp2_sockaddr_in *ai = (const ngtcp2_sockaddr_in *)(void *)a,
91 *bi = (const ngtcp2_sockaddr_in *)(void *)b;
92 if (memcmp(&ai->sin_addr, &bi->sin_addr, sizeof(ai->sin_addr))) {
93 flags |= NGTCP2_ADDR_COMPARE_FLAG_ADDR;
94 }
95 if (ai->sin_port != bi->sin_port) {
96 flags |= NGTCP2_ADDR_COMPARE_FLAG_PORT;
97 }
98 return flags;
99 }
100 case NGTCP2_AF_INET6: {
101 const ngtcp2_sockaddr_in6 *ai = (const ngtcp2_sockaddr_in6 *)(void *)a,
102 *bi = (const ngtcp2_sockaddr_in6 *)(void *)b;
103 if (memcmp(&ai->sin6_addr, &bi->sin6_addr, sizeof(ai->sin6_addr))) {
104 flags |= NGTCP2_ADDR_COMPARE_FLAG_ADDR;
105 }
106 if (ai->sin6_port != bi->sin6_port) {
107 flags |= NGTCP2_ADDR_COMPARE_FLAG_PORT;
108 }
109 return flags;
110 }
111 default:
112 assert(0);
113 abort();
114 }
115 }
116
ngtcp2_addr_empty(const ngtcp2_addr * addr)117 int ngtcp2_addr_empty(const ngtcp2_addr *addr) { return addr->addrlen == 0; }
118