Add win32 directory to EXTRA_DIST
[sipe-libnice.git] / agent / address.c
blob5efd55a182e9f3b95a0eb266d13db1854fac3e8e
1 /*
2 * This file is part of the Nice GLib ICE library.
4 * (C) 2006-2009 Collabora Ltd.
5 * Contact: Youness Alaoui
6 * (C) 2006-2009 Nokia Corporation. All rights reserved.
7 * Contact: Kai Vehmanen
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
17 * License.
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
24 * Contributors:
25 * Dafydd Harries, Collabora Ltd.
26 * Youness Alaoui, Collabora Ltd.
27 * Kai Vehmanen, Nokia
29 * Alternatively, the contents of this file may be used under the terms of the
30 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
31 * case the provisions of LGPL are applicable instead of those above. If you
32 * wish to allow use of your version of this file only under the terms of the
33 * LGPL and not to allow others to use your version of this file under the
34 * MPL, indicate your decision by deleting the provisions above and replace
35 * them with the notice and other provisions required by the LGPL. If you do
36 * not delete the provisions above, a recipient may use your version of this
37 * file under either the MPL or the LGPL.
39 #ifdef HAVE_CONFIG_H
40 # include "config.h"
41 #else
42 #define NICEAPI_EXPORT
43 #endif
45 #include <string.h>
47 #include "address.h"
49 #ifdef G_OS_WIN32
50 #define inet_pton inet_pton_win32
51 #define inet_ntop inet_ntop_win32
54 static const char *
55 inet_ntop_win32 (int af, const void *src, char *dst, socklen_t cnt)
57 if (af == AF_INET) {
58 struct sockaddr_in in;
59 memset(&in, 0, sizeof(in));
60 in.sin_family = AF_INET;
61 memcpy(&in.sin_addr, src, sizeof(struct in_addr));
62 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in),
63 dst, cnt, NULL, 0, NI_NUMERICHOST);
64 return dst;
65 } else if (af == AF_INET6) {
66 struct sockaddr_in6 in;
67 memset(&in, 0, sizeof(in));
68 in.sin6_family = AF_INET6;
69 memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
70 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6),
71 dst, cnt, NULL, 0, NI_NUMERICHOST);
72 return dst;
74 return NULL;
77 static int
78 inet_pton_win32(int af, const char *src, void *dst)
80 struct addrinfo hints, *res, *ressave;
82 memset(&hints, 0, sizeof(struct addrinfo));
83 hints.ai_family = af;
85 if (getaddrinfo(src, NULL, &hints, &res) != 0) {
86 return 0;
89 ressave = res;
91 while (res) {
92 if( res->ai_addr->sa_family == AF_INET) {
93 memcpy(dst, &((struct sockaddr_in *) res->ai_addr)->sin_addr,
94 sizeof(struct in_addr));
95 res = res->ai_next;
96 } else if(res->ai_addr->sa_family == AF_INET6) {
97 memcpy(dst, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr,
98 sizeof(struct in_addr6));
99 res = res->ai_next;
103 freeaddrinfo(ressave);
104 return 1;
107 #endif
111 NICEAPI_EXPORT void
112 nice_address_init (NiceAddress *addr)
114 addr->s.addr.sa_family = AF_UNSPEC;
115 memset (&addr->s, 0, sizeof(addr->s));
118 NICEAPI_EXPORT NiceAddress *
119 nice_address_new (void)
121 NiceAddress *addr = g_slice_new0 (NiceAddress);
122 nice_address_init (addr);
123 return addr;
127 NICEAPI_EXPORT void
128 nice_address_set_ipv4 (NiceAddress *addr, guint32 addr_ipv4)
130 addr->s.ip4.sin_family = AF_INET;
131 #ifdef HAVE_SA_LEN
132 addr->s.ip4.sin_len = sizeof (addr->sa.ip4);
133 #endif
134 addr->s.ip4.sin_addr.s_addr = addr_ipv4 ? htonl (addr_ipv4) : INADDR_ANY;
135 addr->s.ip4.sin_port = 0;
139 NICEAPI_EXPORT void
140 nice_address_set_ipv6 (NiceAddress *addr, const guchar *addr_ipv6)
142 addr->s.ip6.sin6_family = AF_INET6;
143 #ifdef HAVE_SA_LEN
144 addr->s.ip6.sin6_len = sizeof (addr->sa.ip6);
145 #endif
146 memcpy (addr->s.ip6.sin6_addr.s6_addr, addr_ipv6, 16);
147 addr->s.ip6.sin6_port = 0;
148 addr->s.ip6.sin6_scope_id = 0;
152 NICEAPI_EXPORT void
153 nice_address_set_port (NiceAddress *addr, guint port)
155 g_assert (addr);
157 switch (addr->s.addr.sa_family)
159 case AF_INET:
160 addr->s.ip4.sin_port = htons (port);
161 break;
162 case AF_INET6:
163 addr->s.ip6.sin6_port = htons (port);
164 break;
165 default:
166 g_return_if_reached ();
171 guint
172 nice_address_get_port (const NiceAddress *addr)
174 if (!addr)
175 return 0;
177 switch (addr->s.addr.sa_family)
179 case AF_INET:
180 return ntohs (addr->s.ip4.sin_port);
181 case AF_INET6:
182 return ntohs (addr->s.ip6.sin6_port);
185 g_return_val_if_reached (0);
189 NICEAPI_EXPORT gboolean
190 nice_address_set_from_string (NiceAddress *addr, const gchar *str)
192 union
194 struct in_addr ipv4;
195 struct in6_addr ipv6;
196 } a;
198 if (inet_pton (AF_INET, str, &a.ipv4) > 0)
199 nice_address_set_ipv4 (addr, ntohl (a.ipv4.s_addr));
200 else if (inet_pton (AF_INET6, str, &a.ipv6) > 0)
201 nice_address_set_ipv6 (addr, a.ipv6.s6_addr);
202 else
203 return FALSE; /* Invalid address */
205 return TRUE;
209 NICEAPI_EXPORT void
210 nice_address_set_from_sockaddr (NiceAddress *addr,
211 const struct sockaddr *sa)
213 switch (sa->sa_family)
215 case AF_INET:
216 memcpy(&addr->s.ip4, sa, sizeof (addr->s.ip4));
217 break;
218 case AF_INET6:
219 memcpy(&addr->s.ip6, sa, sizeof (addr->s.ip6));
220 break;
221 default:
222 g_return_if_reached ();
227 NICEAPI_EXPORT void
228 nice_address_copy_to_sockaddr (const NiceAddress *addr,
229 struct sockaddr *sa)
231 struct sockaddr_in *sin4 = (struct sockaddr_in *)sa;
232 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
234 g_assert (sa);
236 switch (addr->s.addr.sa_family)
238 case AF_INET:
239 memcpy (sin4, &addr->s.ip4, sizeof (*sin4));
240 break;
241 case AF_INET6:
242 memcpy (sin6, &addr->s.ip6, sizeof (*sin6));
243 break;
244 default:
245 g_return_if_reached ();
249 NICEAPI_EXPORT void
250 nice_address_to_string (const NiceAddress *addr, gchar *dst)
252 switch (addr->s.addr.sa_family) {
253 case AF_INET:
254 inet_ntop (AF_INET, &addr->s.ip4.sin_addr, dst, INET_ADDRSTRLEN);
255 break;
256 case AF_INET6:
257 inet_ntop (AF_INET6, &addr->s.ip6.sin6_addr, dst, INET6_ADDRSTRLEN);
258 break;
259 default:
260 g_return_if_reached ();
265 NICEAPI_EXPORT gboolean
266 nice_address_equal (const NiceAddress *a, const NiceAddress *b)
268 if (a->s.addr.sa_family != b->s.addr.sa_family)
269 return FALSE;
271 switch (a->s.addr.sa_family)
273 case AF_INET:
274 return (a->s.ip4.sin_addr.s_addr == b->s.ip4.sin_addr.s_addr)
275 && (a->s.ip4.sin_port == b->s.ip4.sin_port);
277 case AF_INET6:
278 return IN6_ARE_ADDR_EQUAL (&a->s.ip6.sin6_addr, &b->s.ip6.sin6_addr)
279 && (a->s.ip6.sin6_port == b->s.ip6.sin6_port)
280 && (a->s.ip6.sin6_scope_id == b->s.ip6.sin6_scope_id);
283 g_return_val_if_reached (FALSE);
287 NICEAPI_EXPORT NiceAddress *
288 nice_address_dup (const NiceAddress *a)
290 NiceAddress *dup = g_slice_new0 (NiceAddress);
292 *dup = *a;
293 return dup;
297 NICEAPI_EXPORT void
298 nice_address_free (NiceAddress *addr)
300 g_slice_free (NiceAddress, addr);
304 /* "private" in the sense of "not routable on the Internet" */
305 static gboolean
306 ipv4_address_is_private (guint32 addr)
308 addr = ntohl (addr);
310 /* http://tools.ietf.org/html/rfc3330 */
311 return (
312 /* 10.0.0.0/8 */
313 ((addr & 0xff000000) == 0x0a000000) ||
314 /* 172.16.0.0/12 */
315 ((addr & 0xfff00000) == 0xac100000) ||
316 /* 192.168.0.0/16 */
317 ((addr & 0xffff0000) == 0xc0a80000) ||
318 /* 127.0.0.0/8 */
319 ((addr & 0xff000000) == 0x7f000000));
323 static gboolean
324 ipv6_address_is_private (const guchar *addr)
326 return (
327 /* fe80::/10 */
328 ((addr[0] == 0xfe) && ((addr[1] & 0xc) == 0x80)) ||
329 /* fc00::/7 */
330 ((addr[0] & 0xfe) == 0xfc) ||
331 /* ::1 loopback */
332 ((memcmp (addr, "\x00\x00\x00\x00"
333 "\x00\x00\x00\x00"
334 "\x00\x00\x00\x00"
335 "\x00\x00\x00\x01", 16) == 0)));
339 NICEAPI_EXPORT gboolean
340 nice_address_is_private (const NiceAddress *a)
342 switch (a->s.addr.sa_family)
344 case AF_INET:
345 return ipv4_address_is_private (a->s.ip4.sin_addr.s_addr);
346 case AF_INET6:
347 return ipv6_address_is_private (a->s.ip6.sin6_addr.s6_addr);
350 g_return_val_if_reached (FALSE);
354 NICEAPI_EXPORT gboolean
355 nice_address_is_valid (const NiceAddress *a)
357 switch (a->s.addr.sa_family)
359 case AF_INET:
360 case AF_INET6:
361 return TRUE;
362 default:
363 return FALSE;