Fix calls to ast_get_ip() not initializing the address family.
[asterisk/asterisk.git] / main / acl.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Various sorts of access control
22  *
23  * \author Mark Spencer <markster@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "asterisk/network.h"
31
32 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__)
33 #include <fcntl.h>
34 #include <net/route.h>
35 #endif
36
37 #if defined(SOLARIS)
38 #include <sys/sockio.h>
39 #include <net/if.h>
40 #elif defined(HAVE_GETIFADDRS)
41 #include <ifaddrs.h>
42 #endif
43
44 #include "asterisk/acl.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/lock.h"
48 #include "asterisk/srv.h"
49
50 #if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS))
51 static int get_local_address(struct ast_sockaddr *ourip)
52 {
53         return -1;
54 }
55 #else
56 static void score_address(const struct sockaddr_in *sin, struct in_addr *best_addr, int *best_score)
57 {
58         const char *address;
59         int score;
60
61         address = ast_inet_ntoa(sin->sin_addr);
62
63         /* RFC 1700 alias for the local network */
64         if (address[0] == '0') {
65                 score = -25;
66         /* RFC 1700 localnet */
67         } else if (strncmp(address, "127", 3) == 0) {
68                 score = -20;
69         /* RFC 1918 non-public address space */
70         } else if (strncmp(address, "10.", 3) == 0) {
71                 score = -5;
72         /* RFC 1918 non-public address space */
73         } else if (strncmp(address, "172", 3) == 0) {
74                 /* 172.16.0.0 - 172.19.255.255, but not 172.160.0.0 - 172.169.255.255 */
75                 if (address[4] == '1' && address[5] >= '6' && address[6] == '.') {
76                         score = -5;
77                 /* 172.20.0.0 - 172.29.255.255, but not 172.200.0.0 - 172.255.255.255 nor 172.2.0.0 - 172.2.255.255 */
78                 } else if (address[4] == '2' && address[6] == '.') {
79                         score = -5;
80                 /* 172.30.0.0 - 172.31.255.255, but not 172.3.0.0 - 172.3.255.255 */
81                 } else if (address[4] == '3' && (address[5] == '0' || address[5] == '1')) {
82                         score = -5;
83                 /* All other 172 addresses are public */
84                 } else {
85                         score = 0;
86                 }
87         /* RFC 2544 Benchmark test range (198.18.0.0 - 198.19.255.255, but not 198.180.0.0 - 198.199.255.255) */
88         } else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.') {
89                 score = -10;
90         /* RFC 1918 non-public address space */
91         } else if (strncmp(address, "192.168", 7) == 0) {
92                 score = -5;
93         /* RFC 3330 Zeroconf network */
94         } else if (strncmp(address, "169.254", 7) == 0) {
95                 /*!\note Better score than a test network, but not quite as good as RFC 1918
96                  * address space.  The reason is that some Linux distributions automatically
97                  * configure a Zeroconf address before trying DHCP, so we want to prefer a
98                  * DHCP lease to a Zeroconf address.
99                  */
100                 score = -10;
101         /* RFC 3330 Test network */
102         } else if (strncmp(address, "192.0.2.", 8) == 0) {
103                 score = -15;
104         /* Every other address should be publically routable */
105         } else {
106                 score = 0;
107         }
108
109         if (score > *best_score) {
110                 *best_score = score;
111                 memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr));
112         }
113 }
114
115 static int get_local_address(struct ast_sockaddr *ourip)
116 {
117         int s, res = -1;
118 #ifdef SOLARIS
119         struct lifreq *ifr = NULL;
120         struct lifnum ifn;
121         struct lifconf ifc;
122         struct sockaddr_in *sa;
123         char *buf = NULL;
124         int bufsz, x;
125 #endif /* SOLARIS */
126 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__)
127         struct ifaddrs *ifap, *ifaphead;
128         int rtnerr;
129         const struct sockaddr_in *sin;
130 #endif /* BSD_OR_LINUX */
131         struct in_addr best_addr;
132         int best_score = -100;
133         memset(&best_addr, 0, sizeof(best_addr));
134
135 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__)
136         rtnerr = getifaddrs(&ifaphead);
137         if (rtnerr) {
138                 perror(NULL);
139                 return -1;
140         }
141 #endif /* BSD_OR_LINUX */
142
143         s = socket(AF_INET, SOCK_STREAM, 0);
144
145         if (s > 0) {
146 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__)
147                 for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {
148
149                         if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_INET) {
150                                 sin = (const struct sockaddr_in *) ifap->ifa_addr;
151                                 score_address(sin, &best_addr, &best_score);
152                                 res = 0;
153
154                                 if (best_score == 0) {
155                                         break;
156                                 }
157                         }
158                 }
159 #endif /* BSD_OR_LINUX */
160
161                 /* There is no reason whatsoever that this shouldn't work on Linux or BSD also. */
162 #ifdef SOLARIS
163                 /* Get a count of interfaces on the machine */
164                 ifn.lifn_family = AF_INET;
165                 ifn.lifn_flags = 0;
166                 ifn.lifn_count = 0;
167                 if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) {
168                         close(s);
169                         return -1;
170                 }
171
172                 bufsz = ifn.lifn_count * sizeof(struct lifreq);
173                 if (!(buf = malloc(bufsz))) {
174                         close(s);
175                         return -1;
176                 }
177                 memset(buf, 0, bufsz);
178
179                 /* Get a list of interfaces on the machine */
180                 ifc.lifc_len = bufsz;
181                 ifc.lifc_buf = buf;
182                 ifc.lifc_family = AF_INET;
183                 ifc.lifc_flags = 0;
184                 if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) {
185                         close(s);
186                         free(buf);
187                         return -1;
188                 }
189
190                 for (ifr = ifc.lifc_req, x = 0; x < ifn.lifn_count; ifr++, x++) {
191                         sa = (struct sockaddr_in *)&(ifr->lifr_addr);
192                         score_address(sa, &best_addr, &best_score);
193                         res = 0;
194
195                         if (best_score == 0) {
196                                 break;
197                         }
198                 }
199
200                 free(buf);
201 #endif /* SOLARIS */
202
203                 close(s);
204         }
205 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
206         freeifaddrs(ifaphead);
207 #endif /* BSD_OR_LINUX */
208
209         if (res == 0 && ourip) {
210                 ast_sockaddr_setnull(ourip);
211                 ourip->ss.ss_family = AF_INET;
212                 ((struct sockaddr_in *)&ourip->ss)->sin_addr = best_addr;
213         }
214         return res;
215 }
216 #endif /* HAVE_GETIFADDRS */
217
218 /* Free HA structure */
219 void ast_free_ha(struct ast_ha *ha)
220 {
221         struct ast_ha *hal;
222         while (ha) {
223                 hal = ha;
224                 ha = ha->next;
225                 ast_free(hal);
226         }
227 }
228
229 /* Copy HA structure */
230 void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to)
231 {
232         ast_sockaddr_copy(&to->addr, &from->addr);
233         ast_sockaddr_copy(&to->netmask, &from->netmask);
234         to->sense = from->sense;
235 }
236
237 /* Create duplicate of ha structure */
238 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
239 {
240         struct ast_ha *new_ha;
241
242         if ((new_ha = ast_calloc(1, sizeof(*new_ha)))) {
243                 /* Copy from original to new object */
244                 ast_copy_ha(original, new_ha);
245         }
246
247         return new_ha;
248 }
249
250 /* Create duplicate HA link list */
251 /*  Used in chan_sip2 templates */
252 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
253 {
254         struct ast_ha *start = original;
255         struct ast_ha *ret = NULL;
256         struct ast_ha *current, *prev = NULL;
257
258         while (start) {
259                 current = ast_duplicate_ha(start);  /* Create copy of this object */
260                 if (prev) {
261                         prev->next = current;           /* Link previous to this object */
262                 }
263
264                 if (!ret) {
265                         ret = current;                  /* Save starting point */
266                 }
267
268                 start = start->next;                /* Go to next object */
269                 prev = current;                     /* Save pointer to this object */
270         }
271         return ret;                             /* Return start of list */
272 }
273
274 /*!
275  * \brief
276  * Isolate a 32-bit section of an IPv6 address
277  *
278  * An IPv6 address can be divided into 4 32-bit chunks. This gives
279  * easy access to one of these chunks.
280  *
281  * \param sin6 A pointer to a struct sockaddr_in6
282  * \param index Which 32-bit chunk to operate on. Must be in the range 0-3.
283  */
284 #define V6_WORD(sin6, index) ((uint32_t *)&((sin6)->sin6_addr))[(index)]
285
286 /*!
287  * \brief
288  * Apply a netmask to an address and store the result in a separate structure.
289  *
290  * When dealing with IPv6 addresses, one cannot apply a netmask with a simple
291  * logical and operation. Furthermore, the incoming address may be an IPv4 address
292  * and need to be mapped properly before attempting to apply a rule.
293  *
294  * \param addr The IP address to apply the mask to.
295  * \param netmask The netmask configured in the host access rule.
296  * \param result The resultant address after applying the netmask to the given address
297  * \retval 0 Successfully applied netmask
298  * \reval -1 Failed to apply netmask
299  */
300 static int apply_netmask(const struct ast_sockaddr *addr, const struct ast_sockaddr *netmask,
301                 struct ast_sockaddr *result)
302 {
303         int res = 0;
304
305         if (ast_sockaddr_is_ipv4(addr)) {
306                 struct sockaddr_in result4 = { 0, };
307                 struct sockaddr_in *addr4 = (struct sockaddr_in *) &addr->ss;
308                 struct sockaddr_in *mask4 = (struct sockaddr_in *) &netmask->ss;
309                 result4.sin_family = AF_INET;
310                 result4.sin_addr.s_addr = addr4->sin_addr.s_addr & mask4->sin_addr.s_addr;
311                 ast_sockaddr_from_sin(result, &result4);
312         } else if (ast_sockaddr_is_ipv6(addr)) {
313                 struct sockaddr_in6 result6 = { 0, };
314                 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &addr->ss;
315                 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *) &netmask->ss;
316                 int i;
317                 result6.sin6_family = AF_INET6;
318                 for (i = 0; i < 4; ++i) {
319                         V6_WORD(&result6, i) = V6_WORD(addr6, i) & V6_WORD(mask6, i);
320                 }
321                 memcpy(&result->ss, &result6, sizeof(result6));
322                 result->len = sizeof(result6);
323         } else {
324                 /* Unsupported address scheme */
325                 res = -1;
326         }
327
328         return res;
329 }
330
331 /*!
332  * \brief
333  * Parse a netmask in CIDR notation
334  *
335  * \details
336  * For a mask of an IPv4 address, this should be a number between 0 and 32. For
337  * a mask of an IPv6 address, this should be a number between 0 and 128. This
338  * function creates an IPv6 ast_sockaddr from the given netmask. For masks of
339  * IPv4 addresses, this is accomplished by adding 96 to the original netmask.
340  *
341  * \param[out] addr The ast_sockaddr produced from the CIDR netmask
342  * \param is_v4 Tells if the address we are masking is IPv4.
343  * \param mask_str The CIDR mask to convert
344  * \retval -1 Failure
345  * \retval 0 Success
346  */
347 static int parse_cidr_mask(struct ast_sockaddr *addr, int is_v4, const char *mask_str)
348 {
349         int mask;
350
351         if (sscanf(mask_str, "%30d", &mask) != 1) {
352                 return -1;
353         }
354
355         if (is_v4) {
356                 struct sockaddr_in sin;
357                 if (mask < 0 || mask > 32) {
358                         return -1;
359                 }
360                 memset(&sin, 0, sizeof(sin));
361                 sin.sin_family = AF_INET;
362                 /* If mask is 0, then we already have the
363                  * appropriate all 0s address in sin from
364                  * the above memset.
365                  */
366                 if (mask != 0) {
367                         sin.sin_addr.s_addr = htonl(0xFFFFFFFF << (32 - mask));
368                 }
369                 ast_sockaddr_from_sin(addr, &sin);
370         } else {
371                 struct sockaddr_in6 sin6;
372                 int i;
373                 if (mask < 0 || mask > 128) {
374                         return -1;
375                 }
376                 memset(&sin6, 0, sizeof(sin6));
377                 sin6.sin6_family = AF_INET6;
378                 for (i = 0; i < 4; ++i) {
379                         /* Once mask reaches 0, we don't have
380                          * to explicitly set anything anymore
381                          * since sin6 was zeroed out already
382                          */
383                         if (mask > 0) {
384                                 V6_WORD(&sin6, i) = htonl(0xFFFFFFFF << (mask < 32 ? (32 - mask) : 0));
385                                 mask -= mask < 32 ? mask : 32;
386                         }
387                 }
388                 memcpy(&addr->ss, &sin6, sizeof(sin6));
389                 addr->len = sizeof(sin6);
390         }
391
392         return 0;
393 }
394
395 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
396 {
397         struct ast_ha *ha;
398         struct ast_ha *prev = NULL;
399         struct ast_ha *ret;
400         char *tmp = ast_strdupa(stuff);
401         char *address = NULL, *mask = NULL;
402         int addr_is_v4;
403
404         ret = path;
405         while (path) {
406                 prev = path;
407                 path = path->next;
408         }
409
410         if (!(ha = ast_calloc(1, sizeof(*ha)))) {
411                 if (error) {
412                         *error = 1;
413                 }
414                 return ret;
415         }
416
417         address = strsep(&tmp, "/");
418         if (!address) {
419                 address = tmp;
420         } else {
421                 mask = tmp;
422         }
423
424         if (!ast_sockaddr_parse(&ha->addr, address, PARSE_PORT_FORBID)) {
425                 ast_log(LOG_WARNING, "Invalid IP address: %s\n", address);
426                 ast_free_ha(ha);
427                 if (error) {
428                         *error = 1;
429                 }
430                 return ret;
431         }
432
433         /* If someone specifies an IPv4-mapped IPv6 address,
434          * we just convert this to an IPv4 ACL
435          */
436         if (ast_sockaddr_ipv4_mapped(&ha->addr, &ha->addr)) {
437                 ast_log(LOG_NOTICE, "IPv4-mapped ACL network address specified. "
438                                 "Converting to an IPv4 ACL network address.\n");
439         }
440
441         addr_is_v4 = ast_sockaddr_is_ipv4(&ha->addr);
442
443         if (!mask) {
444                 parse_cidr_mask(&ha->netmask, addr_is_v4, addr_is_v4 ? "32" : "128");
445         } else if (strchr(mask, ':') || strchr(mask, '.')) {
446                 int mask_is_v4;
447                 /* Mask is of x.x.x.x or x:x:x:x:x:x:x:x variety */
448                 if (!ast_sockaddr_parse(&ha->netmask, mask, PARSE_PORT_FORBID)) {
449                         ast_log(LOG_WARNING, "Invalid netmask: %s\n", mask);
450                         ast_free_ha(ha);
451                         if (error) {
452                                 *error = 1;
453                         }
454                         return ret;
455                 }
456                 /* If someone specifies an IPv4-mapped IPv6 netmask,
457                  * we just convert this to an IPv4 ACL
458                  */
459                 if (ast_sockaddr_ipv4_mapped(&ha->netmask, &ha->netmask)) {
460                         ast_log(LOG_NOTICE, "IPv4-mapped ACL netmask specified. "
461                                         "Converting to an IPv4 ACL netmask.\n");
462                 }
463                 mask_is_v4 = ast_sockaddr_is_ipv4(&ha->netmask);
464                 if (addr_is_v4 ^ mask_is_v4) {
465                         ast_log(LOG_WARNING, "Address and mask are not using same address scheme.\n");
466                         ast_free_ha(ha);
467                         if (error) {
468                                 *error = 1;
469                         }
470                         return ret;
471                 }
472         } else if (parse_cidr_mask(&ha->netmask, addr_is_v4, mask)) {
473                 ast_log(LOG_WARNING, "Invalid CIDR netmask: %s\n", mask);
474                 ast_free_ha(ha);
475                 if (error) {
476                         *error = 1;
477                 }
478                 return ret;
479         }
480
481         if (apply_netmask(&ha->addr, &ha->netmask, &ha->addr)) {
482                 /* This shouldn't happen because ast_sockaddr_parse would
483                  * have failed much earlier on an unsupported address scheme
484                  */
485                 char *failmask = ast_strdupa(ast_sockaddr_stringify(&ha->netmask));
486                 char *failaddr = ast_strdupa(ast_sockaddr_stringify(&ha->addr));
487                 ast_log(LOG_WARNING, "Unable to apply netmask %s to address %s\n", failmask, failaddr);
488                 ast_free_ha(ha);
489                 if (error) {
490                         *error = 1;
491                 }
492                 return ret;
493         }
494
495         ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
496
497         ha->next = NULL;
498         if (prev) {
499                 prev->next = ha;
500         } else {
501                 ret = ha;
502         }
503
504         {
505                 const char *addr = ast_strdupa(ast_sockaddr_stringify(&ha->addr));
506                 const char *mask = ast_strdupa(ast_sockaddr_stringify(&ha->netmask));
507
508                 ast_debug(1, "%s/%s sense %d appended to acl for peer\n", addr, mask, ha->sense);
509         }
510
511         return ret;
512 }
513
514 int ast_apply_ha(const struct ast_ha *ha, const struct ast_sockaddr *addr)
515 {
516         /* Start optimistic */
517         int res = AST_SENSE_ALLOW;
518         const struct ast_ha *current_ha;
519
520         for (current_ha = ha; current_ha; current_ha = current_ha->next) {
521                 struct ast_sockaddr result;
522                 struct ast_sockaddr mapped_addr;
523                 const struct ast_sockaddr *addr_to_use;
524 #if 0   /* debugging code */
525                 char iabuf[INET_ADDRSTRLEN];
526                 char iabuf2[INET_ADDRSTRLEN];
527                 /* DEBUG */
528                 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
529                 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
530                 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
531 #endif
532                 if (ast_sockaddr_is_ipv4(&ha->addr)) {
533                         if (ast_sockaddr_is_ipv6(addr)) {
534                                 if (ast_sockaddr_is_ipv4_mapped(addr)) {
535                                         /* IPv4 ACLs apply to IPv4-mapped addresses */
536                                         ast_sockaddr_ipv4_mapped(addr, &mapped_addr);
537                                         addr_to_use = &mapped_addr;
538                                 } else {
539                                         /* An IPv4 ACL does not apply to an IPv6 address */
540                                         continue;
541                                 }
542                         } else {
543                                 /* Address is IPv4 and ACL is IPv4. No biggie */
544                                 addr_to_use = addr;
545                         }
546                 } else {
547                         if (ast_sockaddr_is_ipv6(addr) && !ast_sockaddr_is_ipv4_mapped(addr)) {
548                                 addr_to_use = addr;
549                         } else {
550                                 /* Address is IPv4 or IPv4 mapped but ACL is IPv6. Skip */
551                                 continue;
552                         }
553                 }
554
555                 /* For each rule, if this address and the netmask = the net address
556                    apply the current rule */
557                 if (apply_netmask(addr_to_use, &current_ha->netmask, &result)) {
558                         /* Unlikely to happen since we know the address to be IPv4 or IPv6 */
559                         continue;
560                 }
561                 if (!ast_sockaddr_cmp_addr(&result, &current_ha->addr)) {
562                         res = current_ha->sense;
563                 }
564         }
565         return res;
566 }
567
568 static int resolve_first(struct ast_sockaddr *addr, const char *name, int flag,
569                          int family)
570 {
571         struct ast_sockaddr *addrs;
572         int addrs_cnt;
573
574         addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
575         if (addrs_cnt > 0) {
576                 if (addrs_cnt > 1) {
577                         ast_debug(1, "Multiple addresses. Using the first only\n");
578                 }
579                 ast_sockaddr_copy(addr, &addrs[0]);
580                 ast_free(addrs);
581         } else {
582                 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", name);
583                 return -1;
584         }
585
586         return 0;
587 }
588
589 int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *hostname, const char *service)
590 {
591         char srv[256];
592         char host[256];
593         int srv_ret = 0;
594         int tportno;
595
596         if (service) {
597                 snprintf(srv, sizeof(srv), "%s.%s", service, hostname);
598                 if ((srv_ret = ast_get_srv(NULL, host, sizeof(host), &tportno, srv)) > 0) {
599                         hostname = host;
600                 }
601         }
602
603         if (resolve_first(addr, hostname, PARSE_PORT_FORBID, addr->ss.ss_family) != 0) {
604                 return -1;
605         }
606
607         if (srv_ret > 0) {
608                 ast_sockaddr_set_port(addr, tportno);
609         }
610
611         return 0;
612 }
613
614 struct dscp_codepoint {
615         char *name;
616         unsigned int space;
617 };
618
619 /* IANA registered DSCP codepoints */
620
621 static const struct dscp_codepoint dscp_pool1[] = {
622         { "CS0", 0x00 },
623         { "CS1", 0x08 },
624         { "CS2", 0x10 },
625         { "CS3", 0x18 },
626         { "CS4", 0x20 },
627         { "CS5", 0x28 },
628         { "CS6", 0x30 },
629         { "CS7", 0x38 },
630         { "AF11", 0x0A },
631         { "AF12", 0x0C },
632         { "AF13", 0x0E },
633         { "AF21", 0x12 },
634         { "AF22", 0x14 },
635         { "AF23", 0x16 },
636         { "AF31", 0x1A },
637         { "AF32", 0x1C },
638         { "AF33", 0x1E },
639         { "AF41", 0x22 },
640         { "AF42", 0x24 },
641         { "AF43", 0x26 },
642         { "EF", 0x2E },
643 };
644
645 int ast_str2cos(const char *value, unsigned int *cos)
646 {
647         int fval;
648
649         if (sscanf(value, "%30d", &fval) == 1) {
650                 if (fval < 8) {
651                     *cos = fval;
652                     return 0;
653                 }
654         }
655
656         return -1;
657 }
658
659 int ast_str2tos(const char *value, unsigned int *tos)
660 {
661         int fval;
662         unsigned int x;
663
664         if (sscanf(value, "%30i", &fval) == 1) {
665                 *tos = fval & 0xFF;
666                 return 0;
667         }
668
669         for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
670                 if (!strcasecmp(value, dscp_pool1[x].name)) {
671                         *tos = dscp_pool1[x].space << 2;
672                         return 0;
673                 }
674         }
675
676         return -1;
677 }
678
679 const char *ast_tos2str(unsigned int tos)
680 {
681         unsigned int x;
682
683         for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
684                 if (dscp_pool1[x].space == (tos >> 2)) {
685                         return dscp_pool1[x].name;
686                 }
687         }
688
689         return "unknown";
690 }
691
692 int ast_get_ip(struct ast_sockaddr *addr, const char *hostname)
693 {
694         return ast_get_ip_or_srv(addr, hostname, NULL);
695 }
696
697 int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us)
698 {
699         int port;
700         int s;
701
702         port = ast_sockaddr_port(us);
703
704         if ((s = socket(ast_sockaddr_is_ipv6(them) ? AF_INET6 : AF_INET,
705                         SOCK_DGRAM, 0)) < 0) {
706                 ast_log(LOG_ERROR, "Cannot create socket\n");
707                 return -1;
708         }
709
710         if (ast_connect(s, them)) {
711                 ast_log(LOG_WARNING, "Cannot connect\n");
712                 close(s);
713                 return -1;
714         }
715         if (ast_getsockname(s, us)) {
716
717                 ast_log(LOG_WARNING, "Cannot get socket name\n");
718                 close(s);
719                 return -1;
720         }
721         close(s);
722
723         {
724                 const char *them_addr = ast_strdupa(ast_sockaddr_stringify_addr(them));
725                 const char *us_addr = ast_strdupa(ast_sockaddr_stringify_addr(us));
726
727                 ast_debug(3, "For destination '%s', our source address is '%s'.\n",
728                                 them_addr, us_addr);
729         }
730
731         ast_sockaddr_set_port(us, port);
732
733         return 0;
734 }
735
736 int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr, int family)
737 {
738         char ourhost[MAXHOSTNAMELEN] = "";
739         struct ast_sockaddr root;
740
741         /* just use the bind address if it is nonzero */
742         if (!ast_sockaddr_is_any(bindaddr)) {
743                 ast_sockaddr_copy(ourip, bindaddr);
744                 ast_debug(3, "Attached to given IP address\n");
745                 return 0;
746         }
747         /* try to use our hostname */
748         if (gethostname(ourhost, sizeof(ourhost) - 1)) {
749                 ast_log(LOG_WARNING, "Unable to get hostname\n");
750         } else {
751                 if (resolve_first(ourip, ourhost, PARSE_PORT_FORBID, family) == 0) {
752                         return 0;
753                 }
754         }
755         ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
756         /* A.ROOT-SERVERS.NET. */
757         if (!resolve_first(&root, "A.ROOT-SERVERS.NET", PARSE_PORT_FORBID, 0) &&
758             !ast_ouraddrfor(&root, ourip)) {
759                 return 0;
760         }
761         return get_local_address(ourip);
762 }
763