summaryrefslogtreecommitdiff
path: root/agent/address.c
blob: 3c20220db89920e262f26163d32821391c5e52e9 (plain)
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * 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.
 */
#ifdef HAVE_CONFIG_H
# include "config.h"
#else
#define NICEAPI_EXPORT
#endif

#include <string.h>

#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif

#include "address.h"

#ifdef G_OS_WIN32
#define inet_ntop inet_ntop_win32

/* Defined in recent versions of mingw:
 * https://github.com/mirror/mingw-w64/commit/0f4899473c4ba2e34fa447b1931a04e38c1f105e
 */
#ifndef IN6_ARE_ADDR_EQUAL
#define IN6_ARE_ADDR_EQUAL(a, b) \
  (memcmp ((const void *) (a), (const void *) (b), sizeof (struct in6_addr)) == 0)
#endif


static const char *
inet_ntop_win32 (int af, const void *src, char *dst, socklen_t cnt)
{
  if (af == AF_INET) {
    struct sockaddr_in in;
    memset(&in, 0, sizeof(in));
    in.sin_family = AF_INET;
    memcpy(&in.sin_addr, src, sizeof(struct in_addr));
    getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in),
        dst, cnt, NULL, 0, NI_NUMERICHOST);
    return dst;
  } else if (af == AF_INET6) {
    struct sockaddr_in6 in;
    memset(&in, 0, sizeof(in));
    in.sin6_family = AF_INET6;
    memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
    getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6),
        dst, cnt, NULL, 0, NI_NUMERICHOST);
    return dst;
  }
  return NULL;
}

#endif



NICEAPI_EXPORT void
nice_address_init (NiceAddress *addr)
{
  addr->s.addr.sa_family = AF_UNSPEC;
  memset (&addr->s, 0, sizeof(addr->s));
}

NICEAPI_EXPORT NiceAddress *
nice_address_new (void)
{
  NiceAddress *addr = g_slice_new0 (NiceAddress);
  nice_address_init (addr);
  return addr;
}


NICEAPI_EXPORT void
nice_address_set_ipv4 (NiceAddress *addr, guint32 addr_ipv4)
{
  addr->s.ip4.sin_family = AF_INET;
#ifdef HAVE_SA_LEN
  addr->s.ip4.sin_len = sizeof (addr->sa.ip4);
#endif
  addr->s.ip4.sin_addr.s_addr = addr_ipv4 ? htonl (addr_ipv4) : INADDR_ANY;
  addr->s.ip4.sin_port = 0;
}


NICEAPI_EXPORT void
nice_address_set_ipv6 (NiceAddress *addr, const guchar *addr_ipv6)
{
  addr->s.ip6.sin6_family = AF_INET6;
#ifdef HAVE_SA_LEN
  addr->s.ip6.sin6_len = sizeof (addr->sa.ip6);
#endif
  memcpy (addr->s.ip6.sin6_addr.s6_addr, addr_ipv6, 16);
  addr->s.ip6.sin6_port = 0;
  addr->s.ip6.sin6_scope_id = 0;
}


NICEAPI_EXPORT void
nice_address_set_port (NiceAddress *addr, guint port)
{
  g_assert (addr);

  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:
      g_return_if_reached ();
    }
}


guint
nice_address_get_port (const NiceAddress *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:
      g_return_val_if_reached (0);
    }
}


NICEAPI_EXPORT gboolean
nice_address_set_from_string (NiceAddress *addr, const gchar *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)
    return FALSE;  /* invalid address */

  nice_address_set_from_sockaddr (addr, res->ai_addr);

  freeaddrinfo (res);

  return TRUE;
}


NICEAPI_EXPORT void
nice_address_set_from_sockaddr (NiceAddress *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:
      g_return_if_reached ();
    }
}


NICEAPI_EXPORT void
nice_address_copy_to_sockaddr (const NiceAddress *addr,
                               struct sockaddr *_sa)
{
  union {
    struct sockaddr *addr;
    struct sockaddr_in *in;
    struct sockaddr_in6 *in6;
  } sa;

  sa.addr = _sa;

  g_assert (_sa);

  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:
      g_return_if_reached ();
    }
}

NICEAPI_EXPORT void
nice_address_to_string (const NiceAddress *addr, gchar *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:
      g_return_if_reached ();
  }
}


NICEAPI_EXPORT gboolean
nice_address_equal (const NiceAddress *a, const NiceAddress *b)
{
  if (a->s.addr.sa_family != b->s.addr.sa_family)
    return FALSE;

  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 == 0 || b->s.ip6.sin6_scope_id == 0 ||
              (a->s.ip6.sin6_scope_id == b->s.ip6.sin6_scope_id));

    default:
      g_return_val_if_reached (FALSE);
    }
}


NICEAPI_EXPORT NiceAddress *
nice_address_dup (const NiceAddress *a)
{
  NiceAddress *dup = g_slice_new0 (NiceAddress);

  *dup = *a;
  return dup;
}


NICEAPI_EXPORT void
nice_address_free (NiceAddress *addr)
{
  g_slice_free (NiceAddress, addr);
}


/* "private" in the sense of "not routable on the Internet" */
static gboolean
ipv4_address_is_private (guint32 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 gboolean
ipv6_address_is_private (const guchar *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))); 
}


NICEAPI_EXPORT gboolean
nice_address_is_private (const NiceAddress *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:
      g_return_val_if_reached (FALSE);
    }
}


NICEAPI_EXPORT gboolean
nice_address_is_valid (const NiceAddress *a)
{
 switch (a->s.addr.sa_family)
    {
    case AF_INET:
    case AF_INET6:
      return TRUE;
    default:
      return FALSE;
    }
}

NICEAPI_EXPORT int
nice_address_ip_version (const NiceAddress *addr)
{
 switch (addr->s.addr.sa_family)
    {
    case AF_INET:
      return 4;
    case AF_INET6:
      return 6;
    default:
      return 0;
    }
}

NICEAPI_EXPORT gboolean
nice_address_equal_no_port (const NiceAddress *a, const NiceAddress *b)
{
  if (a->s.addr.sa_family != b->s.addr.sa_family)
    return FALSE;

  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 == 0 || b->s.ip6.sin6_scope_id == 0 ||
              (a->s.ip6.sin6_scope_id == b->s.ip6.sin6_scope_id));

    default:
      g_return_val_if_reached (FALSE);
    }
}