-
Notifications
You must be signed in to change notification settings - Fork 6
/
address.c
345 lines (299 loc) · 8.28 KB
/
address.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006-2009 Collabora Ltd.
* Contact: Youness Alaoui
* (C) 2006-2009 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
* Youness Alaoui, Collabora Ltd.
* Kai Vehmanen, Nokia
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
/*
* Porting to C library using libevent.
* Jackie Dinh - 2016
*/
#include "cice/address.h"
address_t*
address_new(void) {
address_t *addr;
addr = ICE_MALLOC(address_t);
if (addr == NULL)
return NULL;
addr->s.addr.sa_family = AF_UNSPEC;
memset (&addr->s, 0, sizeof(addr->s));
return addr;
}
void
address_free(address_t *addr) {
if ( addr == NULL )
return;
free(addr);
return;
}
void
address_to_string(const address_t *addr, char *dst) {
switch (addr->s.addr.sa_family) {
case AF_INET:
inet_ntop(AF_INET, &addr->s.ip4.sin_addr, dst, INET_ADDRSTRLEN);
break;
case AF_INET6:
inet_ntop(AF_INET6, &addr->s.ip6.sin6_addr, dst, INET6_ADDRSTRLEN);
break;
default:
break;
}
return;
}
void
address_set_port(address_t *addr, uint16_t port) {
if (addr == NULL)
return;
switch (addr->s.addr.sa_family) {
case AF_INET:
addr->s.ip4.sin_port = htons (port);
break;
case AF_INET6:
addr->s.ip6.sin6_port = htons (port);
break;
default:
break;
}
return;
}
int
address_equal_no_port(const address_t *a, const address_t *b) {
if (a == NULL || b == NULL)
return 0;
if (a->s.addr.sa_family != b->s.addr.sa_family)
return 0;
switch (a->s.addr.sa_family) {
case AF_INET:
return (a->s.ip4.sin_addr.s_addr == b->s.ip4.sin_addr.s_addr);
case AF_INET6:
return IN6_ARE_ADDR_EQUAL(&a->s.ip6.sin6_addr, &b->s.ip6.sin6_addr)
&& (a->s.ip6.sin6_scope_id == b->s.ip6.sin6_scope_id);
default:
break;
}
return 0;
}
int
address_equal(const address_t *a, const address_t *b) {
if (a == NULL || b == NULL)
return 0;
if (a->s.addr.sa_family != b->s.addr.sa_family)
return 0;
switch (a->s.addr.sa_family) {
case AF_INET:
return (a->s.ip4.sin_addr.s_addr == b->s.ip4.sin_addr.s_addr)
&& (a->s.ip4.sin_port == b->s.ip4.sin_port);
case AF_INET6:
return IN6_ARE_ADDR_EQUAL(&a->s.ip6.sin6_addr, &b->s.ip6.sin6_addr)
&& (a->s.ip6.sin6_port == b->s.ip6.sin6_port)
&& (a->s.ip6.sin6_scope_id == b->s.ip6.sin6_scope_id);
default:
return 0;
}
return 0;
}
void
print_address(const address_t *addr) {
char local_ip[ICE_ADDRESS_STRING_LEN] = {0};
address_to_string(addr, local_ip);
ICE_DEBUG("address info, addr=%s,port=%u",
local_ip,address_get_port(addr));
return;
}
uint32_t
address_get_port(const address_t *addr) {
if (!addr)
return 0;
switch (addr->s.addr.sa_family) {
case AF_INET:
return ntohs (addr->s.ip4.sin_port);
case AF_INET6:
return ntohs (addr->s.ip6.sin6_port);
default:
return 0;
}
return 0;
}
void
address_set_from_sockaddr (address_t *addr, const struct sockaddr *sa) {
switch (sa->sa_family) {
case AF_INET:
memcpy(&addr->s.ip4, sa, sizeof (addr->s.ip4));
break;
case AF_INET6:
memcpy(&addr->s.ip6, sa, sizeof (addr->s.ip6));
break;
default:
break;
}
return;
}
#ifdef USE_LIBEVENT2
int
address_set_from_string(address_t *addr, const char *str) {
struct addrinfo hints;
struct addrinfo *res;
memset(&hints, 0, sizeof (hints));
/* AI_NUMERICHOST prevents getaddrinfo() from doing DNS resolution. */
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_NUMERICHOST;
if (getaddrinfo (str, NULL, &hints, &res) != 0) {
ICE_ERROR("failed to get addrinfo, str=%s",str);
return ICE_ERR; /* invalid address */
}
address_set_from_sockaddr(addr, res->ai_addr);
freeaddrinfo(res);
return ICE_OK;
}
#endif //USE_LIBEVENT2
#ifdef USE_ESP32
int
address_set_from_string(address_t *addr, const char *str) {
struct sockaddr_in sa;
//FIXME: hardcode ip4 here.
memset(&sa,0,sizeof(sa));
sa.sin_len = 8;
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(str);
address_set_from_sockaddr(addr, (struct sockaddr*)&sa);
return ICE_OK;
}
#endif //USE_ESP32
int
address_is_valid(const address_t *a) {
switch (a->s.addr.sa_family) {
case AF_INET:
case AF_INET6:
return ICE_TRUE;
default:
return ICE_FALSE;
}
return ICE_FALSE;
}
void
address_copy_to_sockaddr(const address_t *addr, struct sockaddr *_sa) {
union {
struct sockaddr *addr;
struct sockaddr_in *in;
struct sockaddr_in6 *in6;
} sa;
sa.addr = _sa;
if (_sa == NULL )
return;
switch (addr->s.addr.sa_family) {
case AF_INET:
memcpy (sa.in, &addr->s.ip4, sizeof (*sa.in));
break;
case AF_INET6:
memcpy (sa.in6, &addr->s.ip6, sizeof (*sa.in6));
break;
default:
return;
}
return;
}
int
get_address_length (const address_t *addr) {
int len = 0;
switch (addr->s.addr.sa_family) {
case AF_INET:
//inet_ntop (AF_INET, &addr->s.ip4.sin_addr, dst, INET_ADDRSTRLEN);
len = sizeof(addr->s.ip4);
break;
case AF_INET6:
//inet_ntop (AF_INET6, &addr->s.ip6.sin6_addr, dst, INET6_ADDRSTRLEN);
len = sizeof(addr->s.ip6);
break;
default:
break;
}
return len;
}
address_t *
address_dup(const address_t *a) {
address_t *dup = ICE_MALLOC(address_t);
*dup = *a;
//INIT_LIST_HEAD(&dup->list);
return dup;
}
void
address_init(address_t *addr) {
if (addr == NULL)
return;
addr->s.addr.sa_family = AF_UNSPEC;
memset(&addr->s, 0, sizeof(addr->s));
//INIT_LIST_HEAD(&addr->list);
return;
}
static int
ipv4_address_is_private(uint32_t addr) {
addr = ntohl (addr);
/* http://tools.ietf.org/html/rfc3330 */
return (
/* 10.0.0.0/8 */
((addr & 0xff000000) == 0x0a000000) ||
/* 172.16.0.0/12 */
((addr & 0xfff00000) == 0xac100000) ||
/* 192.168.0.0/16 */
((addr & 0xffff0000) == 0xc0a80000) ||
/* 127.0.0.0/8 */
((addr & 0xff000000) == 0x7f000000));
}
static int
ipv6_address_is_private(const unsigned char *addr) {
return (
/* fe80::/10 */
((addr[0] == 0xfe) && ((addr[1] & 0xc0) == 0x80)) ||
/* fc00::/7 */
((addr[0] & 0xfe) == 0xfc) ||
/* ::1 loopback */
((memcmp (addr, "\x00\x00\x00\x00"
"\x00\x00\x00\x00"
"\x00\x00\x00\x00"
"\x00\x00\x00\x01", 16) == 0)));
}
int
address_is_private(const address_t *a)
{
switch (a->s.addr.sa_family) {
case AF_INET:
return ipv4_address_is_private (a->s.ip4.sin_addr.s_addr);
case AF_INET6:
return ipv6_address_is_private (a->s.ip6.sin6_addr.s6_addr);
default:
return 0;
}
return 0;
}