2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2012, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Various sorts of access control
23 * \author Mark Spencer <markster@digium.com>
27 <support_level>core</support_level>
32 #include "asterisk/network.h"
34 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__)
36 #include <net/route.h>
40 #include <sys/sockio.h>
42 #elif defined(HAVE_GETIFADDRS)
46 #include "asterisk/acl.h"
47 #include "asterisk/channel.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/lock.h"
50 #include "asterisk/srv.h"
52 #if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS))
53 static int get_local_address(struct ast_sockaddr *ourip)
58 static void score_address(const struct sockaddr_in *sin, struct in_addr *best_addr, int *best_score)
63 address = ast_inet_ntoa(sin->sin_addr);
65 /* RFC 1700 alias for the local network */
66 if (address[0] == '0') {
68 /* RFC 1700 localnet */
69 } else if (strncmp(address, "127", 3) == 0) {
71 /* RFC 1918 non-public address space */
72 } else if (strncmp(address, "10.", 3) == 0) {
74 /* RFC 1918 non-public address space */
75 } else if (strncmp(address, "172", 3) == 0) {
76 /* 172.16.0.0 - 172.19.255.255, but not 172.160.0.0 - 172.169.255.255 */
77 if (address[4] == '1' && address[5] >= '6' && address[6] == '.') {
79 /* 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 */
80 } else if (address[4] == '2' && address[6] == '.') {
82 /* 172.30.0.0 - 172.31.255.255, but not 172.3.0.0 - 172.3.255.255 */
83 } else if (address[4] == '3' && (address[5] == '0' || address[5] == '1')) {
85 /* All other 172 addresses are public */
89 /* RFC 2544 Benchmark test range (198.18.0.0 - 198.19.255.255, but not 198.180.0.0 - 198.199.255.255) */
90 } else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.') {
92 /* RFC 1918 non-public address space */
93 } else if (strncmp(address, "192.168", 7) == 0) {
95 /* RFC 3330 Zeroconf network */
96 } else if (strncmp(address, "169.254", 7) == 0) {
97 /*!\note Better score than a test network, but not quite as good as RFC 1918
98 * address space. The reason is that some Linux distributions automatically
99 * configure a Zeroconf address before trying DHCP, so we want to prefer a
100 * DHCP lease to a Zeroconf address.
103 /* RFC 3330 Test network */
104 } else if (strncmp(address, "192.0.2.", 8) == 0) {
106 /* Every other address should be publically routable */
111 if (score > *best_score) {
113 memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr));
117 static int get_local_address(struct ast_sockaddr *ourip)
121 struct lifreq *ifr = NULL;
124 struct sockaddr_in *sa;
128 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__)
129 struct ifaddrs *ifap, *ifaphead;
131 const struct sockaddr_in *sin;
132 #endif /* BSD_OR_LINUX */
133 struct in_addr best_addr;
134 int best_score = -100;
135 memset(&best_addr, 0, sizeof(best_addr));
137 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__)
138 rtnerr = getifaddrs(&ifaphead);
143 #endif /* BSD_OR_LINUX */
145 s = socket(AF_INET, SOCK_STREAM, 0);
148 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__)
149 for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {
151 if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_INET) {
152 sin = (const struct sockaddr_in *) ifap->ifa_addr;
153 score_address(sin, &best_addr, &best_score);
156 if (best_score == 0) {
161 #endif /* BSD_OR_LINUX */
163 /* There is no reason whatsoever that this shouldn't work on Linux or BSD also. */
165 /* Get a count of interfaces on the machine */
166 ifn.lifn_family = AF_INET;
169 if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) {
174 bufsz = ifn.lifn_count * sizeof(struct lifreq);
175 if (!(buf = ast_malloc(bufsz))) {
179 memset(buf, 0, bufsz);
181 /* Get a list of interfaces on the machine */
182 ifc.lifc_len = bufsz;
184 ifc.lifc_family = AF_INET;
186 if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) {
192 for (ifr = ifc.lifc_req, x = 0; x < ifn.lifn_count; ifr++, x++) {
193 sa = (struct sockaddr_in *)&(ifr->lifr_addr);
194 score_address(sa, &best_addr, &best_score);
197 if (best_score == 0) {
207 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
208 freeifaddrs(ifaphead);
209 #endif /* BSD_OR_LINUX */
211 if (res == 0 && ourip) {
212 ast_sockaddr_setnull(ourip);
213 ourip->ss.ss_family = AF_INET;
214 ((struct sockaddr_in *)&ourip->ss)->sin_addr = best_addr;
218 #endif /* HAVE_GETIFADDRS */
220 /* Free HA structure */
221 void ast_free_ha(struct ast_ha *ha)
231 /* Free ACL list structure */
232 struct ast_acl_list *ast_free_acl_list(struct ast_acl_list *acl_list)
234 struct ast_acl *current;
240 AST_LIST_LOCK(acl_list);
241 while ((current = AST_LIST_REMOVE_HEAD(acl_list, list))) {
242 ast_free_ha(current->acl);
245 AST_LIST_UNLOCK(acl_list);
247 AST_LIST_HEAD_DESTROY(acl_list);
253 /* Copy HA structure */
254 void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to)
256 ast_sockaddr_copy(&to->addr, &from->addr);
257 ast_sockaddr_copy(&to->netmask, &from->netmask);
258 to->sense = from->sense;
261 /* Create duplicate of ha structure */
262 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
264 struct ast_ha *new_ha;
266 if ((new_ha = ast_calloc(1, sizeof(*new_ha)))) {
267 /* Copy from original to new object */
268 ast_copy_ha(original, new_ha);
274 /* Create duplicate HA link list */
275 /* Used in chan_sip2 templates */
276 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
278 struct ast_ha *start = original;
279 struct ast_ha *ret = NULL;
280 struct ast_ha *current, *prev = NULL;
283 current = ast_duplicate_ha(start); /* Create copy of this object */
285 prev->next = current; /* Link previous to this object */
289 ret = current; /* Save starting point */
292 start = start->next; /* Go to next object */
293 prev = current; /* Save pointer to this object */
295 return ret; /* Return start of list */
298 static int acl_new(struct ast_acl **pointer, const char *name) {
300 if (!(acl = ast_calloc(1, sizeof(*acl)))) {
305 ast_copy_string(acl->name, name, ACL_NAME_LENGTH);
309 struct ast_acl_list *ast_duplicate_acl_list(struct ast_acl_list *original)
311 struct ast_acl_list *clone;
312 struct ast_acl *current_cursor;
313 struct ast_acl *current_clone;
315 /* Early return if we receive a duplication request for a NULL original. */
320 if (!(clone = ast_calloc(1, sizeof(*clone)))) {
321 ast_log(LOG_WARNING, "Failed to allocate ast_acl_list struct while cloning an ACL\n");
324 AST_LIST_HEAD_INIT(clone);
326 AST_LIST_LOCK(original);
328 AST_LIST_TRAVERSE(original, current_cursor, list) {
329 if ((acl_new(¤t_clone, current_cursor->name))) {
330 ast_log(LOG_WARNING, "Failed to allocate ast_acl struct while cloning an ACL.");
334 /* Copy data from original ACL to clone ACL */
335 current_clone->acl = ast_duplicate_ha_list(current_cursor->acl);
337 current_clone->is_invalid = current_cursor->is_invalid;
338 current_clone->is_realtime = current_cursor->is_realtime;
340 AST_LIST_INSERT_TAIL(clone, current_clone, list);
343 AST_LIST_UNLOCK(original);
350 * Parse a netmask in CIDR notation
353 * For a mask of an IPv4 address, this should be a number between 0 and 32. For
354 * a mask of an IPv6 address, this should be a number between 0 and 128. This
355 * function creates an IPv6 ast_sockaddr from the given netmask. For masks of
356 * IPv4 addresses, this is accomplished by adding 96 to the original netmask.
358 * \param[out] addr The ast_sockaddr produced from the CIDR netmask
359 * \param is_v4 Tells if the address we are masking is IPv4.
360 * \param mask_str The CIDR mask to convert
364 static int parse_cidr_mask(struct ast_sockaddr *addr, int is_v4, const char *mask_str)
368 if (sscanf(mask_str, "%30d", &mask) != 1) {
373 struct sockaddr_in sin;
374 if (mask < 0 || mask > 32) {
377 memset(&sin, 0, sizeof(sin));
378 sin.sin_family = AF_INET;
379 /* If mask is 0, then we already have the
380 * appropriate all 0s address in sin from
384 sin.sin_addr.s_addr = htonl(0xFFFFFFFF << (32 - mask));
386 ast_sockaddr_from_sin(addr, &sin);
388 struct sockaddr_in6 sin6;
390 if (mask < 0 || mask > 128) {
393 memset(&sin6, 0, sizeof(sin6));
394 sin6.sin6_family = AF_INET6;
395 for (i = 0; i < 4; ++i) {
396 /* Once mask reaches 0, we don't have
397 * to explicitly set anything anymore
398 * since sin6 was zeroed out already
401 V6_WORD(&sin6, i) = htonl(0xFFFFFFFF << (mask < 32 ? (32 - mask) : 0));
402 mask -= mask < 32 ? mask : 32;
405 memcpy(&addr->ss, &sin6, sizeof(sin6));
406 addr->len = sizeof(sin6);
412 void ast_append_acl(const char *sense, const char *stuff, struct ast_acl_list **path, int *error, int *named_acl_flag)
414 struct ast_acl *acl = NULL;
415 struct ast_acl *current;
416 struct ast_acl_list *working_list;
420 /* If the ACL list is currently uninitialized, it must be initialized. */
422 struct ast_acl_list *list;
423 list = ast_calloc(1, sizeof(*list));
425 /* Allocation Error */
432 AST_LIST_HEAD_INIT(list);
436 working_list = *path;
438 AST_LIST_LOCK(working_list);
440 /* First we need to determine if we will need to add a new ACL node or if we can use an existing one. */
441 if (strncasecmp(sense, "a", 1)) {
442 /* The first element in the path should be the unnamed, base ACL. If that's the case, we use it. If not,
443 * we have to make one and link it up appropriately. */
444 current = AST_LIST_FIRST(working_list);
446 if (!current || !ast_strlen_zero(current->name)) {
447 if (acl_new(&acl, "")) {
452 // Need to INSERT the ACL at the head here.
453 AST_LIST_INSERT_HEAD(working_list, acl, list);
455 /* If the first element was already the unnamed base ACL, we just use that one. */
459 /* With the proper ACL set for modification, we can just pass this off to the ast_ha append function. */
460 acl->acl = ast_append_ha(sense, stuff, acl->acl, error);
462 AST_LIST_UNLOCK(working_list);
466 /* We are in ACL append mode, so we know we'll be adding one or more named ACLs. */
467 list = ast_strdupa(stuff);
469 while ((tmp = strsep(&list, ","))) {
470 struct ast_ha *named_ha;
471 int already_included = 0;
473 /* Remove leading whitespace from the string in case the user put spaces between items */
474 tmp = ast_skip_blanks(tmp);
476 /* The first step is to check for a duplicate */
477 AST_LIST_TRAVERSE(working_list, current, list) {
478 if (!strcasecmp(current->name, tmp)) { /* ACL= */
479 /* Inclusion of the same ACL multiple times isn't a catastrophic error, but it will raise the error flag and skip the entry. */
480 ast_log(LOG_ERROR, "Named ACL '%s' occurs multiple times in ACL definition. Please update your ACL configuration.", tmp);
484 already_included = 1;
489 if (already_included) {
493 if (acl_new(&acl, tmp)) {
494 /* This is a catastrophic allocation error and we'll return immediately if this happens. */
498 AST_LIST_UNLOCK(working_list);
502 /* Attempt to grab the Named ACL we are looking for. */
503 named_ha = ast_named_acl_find(tmp, &acl->is_realtime, &acl->is_invalid);
505 /* Set the ACL's ast_ha to the duplicated named ACL retrieved above. */
508 /* Raise the named_acl_flag since we are adding a named ACL to the ACL container. */
509 if (named_acl_flag) {
513 /* Now insert the new ACL at the end of the list. */
514 AST_LIST_INSERT_TAIL(working_list, acl, list);
517 AST_LIST_UNLOCK(working_list);
520 int ast_acl_list_is_empty(struct ast_acl_list *acl_list)
522 struct ast_acl *head;
528 AST_LIST_LOCK(acl_list);
529 head = AST_LIST_FIRST(acl_list);
530 AST_LIST_UNLOCK(acl_list);
539 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
542 struct ast_ha *prev = NULL;
544 char *tmp, *list = ast_strdupa(stuff);
545 char *address = NULL, *mask = NULL;
547 int allowing = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
548 const char *parsed_addr, *parsed_mask;
556 while ((tmp = strsep(&list, ","))) {
557 if (!(ha = ast_calloc(1, sizeof(*ha)))) {
564 address = strsep(&tmp, "/");
571 if (*address == '!') {
572 ha->sense = (allowing == AST_SENSE_DENY) ? AST_SENSE_ALLOW : AST_SENSE_DENY;
575 ha->sense = allowing;
578 if (!ast_sockaddr_parse(&ha->addr, address, PARSE_PORT_FORBID)) {
579 ast_log(LOG_WARNING, "Invalid IP address: %s\n", address);
587 /* If someone specifies an IPv4-mapped IPv6 address,
588 * we just convert this to an IPv4 ACL
590 if (ast_sockaddr_ipv4_mapped(&ha->addr, &ha->addr)) {
591 ast_log(LOG_NOTICE, "IPv4-mapped ACL network address specified. "
592 "Converting to an IPv4 ACL network address.\n");
595 addr_is_v4 = ast_sockaddr_is_ipv4(&ha->addr);
598 parse_cidr_mask(&ha->netmask, addr_is_v4, addr_is_v4 ? "32" : "128");
599 } else if (strchr(mask, ':') || strchr(mask, '.')) {
601 /* Mask is of x.x.x.x or x:x:x:x:x:x:x:x variety */
602 if (!ast_sockaddr_parse(&ha->netmask, mask, PARSE_PORT_FORBID)) {
603 ast_log(LOG_WARNING, "Invalid netmask: %s\n", mask);
610 /* If someone specifies an IPv4-mapped IPv6 netmask,
611 * we just convert this to an IPv4 ACL
613 if (ast_sockaddr_ipv4_mapped(&ha->netmask, &ha->netmask)) {
614 ast_log(LOG_NOTICE, "IPv4-mapped ACL netmask specified. "
615 "Converting to an IPv4 ACL netmask.\n");
617 mask_is_v4 = ast_sockaddr_is_ipv4(&ha->netmask);
618 if (addr_is_v4 ^ mask_is_v4) {
619 ast_log(LOG_WARNING, "Address and mask are not using same address scheme.\n");
626 } else if (parse_cidr_mask(&ha->netmask, addr_is_v4, mask)) {
627 ast_log(LOG_WARNING, "Invalid CIDR netmask: %s\n", mask);
635 if (ast_sockaddr_apply_netmask(&ha->addr, &ha->netmask, &ha->addr)) {
636 /* This shouldn't happen because ast_sockaddr_parse would
637 * have failed much earlier on an unsupported address scheme
639 char *failmask = ast_strdupa(ast_sockaddr_stringify(&ha->netmask));
640 char *failaddr = ast_strdupa(ast_sockaddr_stringify(&ha->addr));
641 ast_log(LOG_WARNING, "Unable to apply netmask %s to address %s\n", failmask, failaddr);
656 parsed_addr = ast_strdupa(ast_sockaddr_stringify(&ha->addr));
657 parsed_mask = ast_strdupa(ast_sockaddr_stringify(&ha->netmask));
659 ast_debug(3, "%s/%s sense %u appended to ACL\n", parsed_addr, parsed_mask, ha->sense);
665 void ast_ha_join(const struct ast_ha *ha, struct ast_str **buf)
667 for (; ha; ha = ha->next) {
668 const char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr));
669 ast_str_append(buf, 0, "%s%s/%s",
670 ha->sense == AST_SENSE_ALLOW ? "!" : "",
671 addr, ast_sockaddr_stringify_addr(&ha->netmask));
673 ast_str_append(buf, 0, ",");
678 void ast_ha_join_cidr(const struct ast_ha *ha, struct ast_str **buf)
680 for (; ha; ha = ha->next) {
681 const char *addr = ast_sockaddr_stringify_addr(&ha->addr);
682 ast_str_append(buf, 0, "%s%s/%d",
683 ha->sense == AST_SENSE_ALLOW ? "!" : "",
684 addr, ast_sockaddr_cidr_bits(&ha->netmask));
686 ast_str_append(buf, 0, ",");
691 enum ast_acl_sense ast_apply_acl(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr, const char *purpose)
695 /* If the list is NULL, there are no rules, so we'll allow automatically. */
697 return AST_SENSE_ALLOW;
700 AST_LIST_LOCK(acl_list);
702 AST_LIST_TRAVERSE(acl_list, acl, list) {
703 if (acl->is_invalid) {
704 /* In this case, the baseline ACL shouldn't ever trigger this, but if that somehow happens, it'll still be shown. */
705 ast_log(LOG_WARNING, "%sRejecting '%s' due to use of an invalid ACL '%s'.\n", purpose ? purpose : "", ast_sockaddr_stringify_addr(addr),
706 ast_strlen_zero(acl->name) ? "(BASELINE)" : acl->name);
707 AST_LIST_UNLOCK(acl_list);
708 return AST_SENSE_DENY;
712 if (ast_apply_ha(acl->acl, addr) == AST_SENSE_DENY) {
713 ast_log(LOG_NOTICE, "%sRejecting '%s' due to a failure to pass ACL '%s'\n", purpose ? purpose : "", ast_sockaddr_stringify_addr(addr),
714 ast_strlen_zero(acl->name) ? "(BASELINE)" : acl->name);
715 AST_LIST_UNLOCK(acl_list);
716 return AST_SENSE_DENY;
721 AST_LIST_UNLOCK(acl_list);
723 return AST_SENSE_ALLOW;
726 enum ast_acl_sense ast_apply_ha(const struct ast_ha *ha, const struct ast_sockaddr *addr)
728 /* Start optimistic */
729 enum ast_acl_sense res = AST_SENSE_ALLOW;
730 const struct ast_ha *current_ha;
732 for (current_ha = ha; current_ha; current_ha = current_ha->next) {
733 struct ast_sockaddr result;
734 struct ast_sockaddr mapped_addr;
735 const struct ast_sockaddr *addr_to_use;
736 #if 0 /* debugging code */
737 char iabuf[INET_ADDRSTRLEN];
738 char iabuf2[INET_ADDRSTRLEN];
740 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
741 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
742 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
744 if (ast_sockaddr_is_ipv4(¤t_ha->addr)) {
745 if (ast_sockaddr_is_ipv6(addr)) {
746 if (ast_sockaddr_is_ipv4_mapped(addr)) {
747 /* IPv4 ACLs apply to IPv4-mapped addresses */
748 if (!ast_sockaddr_ipv4_mapped(addr, &mapped_addr)) {
749 ast_log(LOG_ERROR, "%s provided to ast_sockaddr_ipv4_mapped could not be converted. That shouldn't be possible.\n",
750 ast_sockaddr_stringify(addr));
753 addr_to_use = &mapped_addr;
755 /* An IPv4 ACL does not apply to an IPv6 address */
759 /* Address is IPv4 and ACL is IPv4. No biggie */
763 if (ast_sockaddr_is_ipv6(addr) && !ast_sockaddr_is_ipv4_mapped(addr)) {
766 /* Address is IPv4 or IPv4 mapped but ACL is IPv6. Skip */
771 /* For each rule, if this address and the netmask = the net address
772 apply the current rule */
773 if (ast_sockaddr_apply_netmask(addr_to_use, ¤t_ha->netmask, &result)) {
774 /* Unlikely to happen since we know the address to be IPv4 or IPv6 */
777 if (!ast_sockaddr_cmp_addr(&result, ¤t_ha->addr)) {
778 res = current_ha->sense;
784 static int resolve_first(struct ast_sockaddr *addr, const char *name, int flag,
787 struct ast_sockaddr *addrs;
790 addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
793 ast_debug(1, "Multiple addresses. Using the first only\n");
795 ast_sockaddr_copy(addr, &addrs[0]);
798 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", name);
805 int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *hostname, const char *service)
813 snprintf(srv, sizeof(srv), "%s.%s", service, hostname);
814 if ((srv_ret = ast_get_srv(NULL, host, sizeof(host), &tportno, srv)) > 0) {
819 if (resolve_first(addr, hostname, PARSE_PORT_FORBID, addr->ss.ss_family) != 0) {
824 ast_sockaddr_set_port(addr, tportno);
830 struct dscp_codepoint {
835 /* IANA registered DSCP codepoints */
837 static const struct dscp_codepoint dscp_pool1[] = {
861 int ast_str2cos(const char *value, unsigned int *cos)
865 if (sscanf(value, "%30d", &fval) == 1) {
875 int ast_str2tos(const char *value, unsigned int *tos)
880 if (sscanf(value, "%30i", &fval) == 1) {
885 for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
886 if (!strcasecmp(value, dscp_pool1[x].name)) {
887 *tos = dscp_pool1[x].space << 2;
895 const char *ast_tos2str(unsigned int tos)
899 for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
900 if (dscp_pool1[x].space == (tos >> 2)) {
901 return dscp_pool1[x].name;
908 int ast_get_ip(struct ast_sockaddr *addr, const char *hostname)
910 return ast_get_ip_or_srv(addr, hostname, NULL);
913 int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us)
918 port = ast_sockaddr_port(us);
920 if ((s = socket(ast_sockaddr_is_ipv6(them) ? AF_INET6 : AF_INET,
921 SOCK_DGRAM, 0)) < 0) {
922 ast_log(LOG_ERROR, "Cannot create socket\n");
926 if (ast_connect(s, them)) {
927 ast_log(LOG_WARNING, "Cannot connect\n");
931 if (ast_getsockname(s, us)) {
933 ast_log(LOG_WARNING, "Cannot get socket name\n");
940 const char *them_addr = ast_strdupa(ast_sockaddr_stringify_addr(them));
941 const char *us_addr = ast_strdupa(ast_sockaddr_stringify_addr(us));
943 ast_debug(3, "For destination '%s', our source address is '%s'.\n",
947 ast_sockaddr_set_port(us, port);
952 int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr, int family)
954 char ourhost[MAXHOSTNAMELEN] = "";
955 struct ast_sockaddr root;
956 int res, port = ast_sockaddr_port(ourip);
958 /* just use the bind address if it is nonzero */
959 if (!ast_sockaddr_is_any(bindaddr)) {
960 ast_sockaddr_copy(ourip, bindaddr);
961 ast_debug(3, "Attached to given IP address\n");
964 /* try to use our hostname */
965 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
966 ast_log(LOG_WARNING, "Unable to get hostname\n");
968 if (resolve_first(ourip, ourhost, PARSE_PORT_FORBID, family) == 0) {
969 /* reset port since resolve_first wipes this out */
970 ast_sockaddr_set_port(ourip, port);
974 ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
975 /* A.ROOT-SERVERS.NET. */
976 if (!resolve_first(&root, "A.ROOT-SERVERS.NET", PARSE_PORT_FORBID, 0) &&
977 !ast_ouraddrfor(&root, ourip)) {
978 /* reset port since resolve_first wipes this out */
979 ast_sockaddr_set_port(ourip, port);
982 res = get_local_address(ourip);
983 ast_sockaddr_set_port(ourip, port);