--- channels/chan_sip.c.findouraddr 2005-01-10 19:52:34.000000000 +0100 +++ channels/chan_sip.c 2005-01-10 20:29:12.789344239 +0100 @@ -192,7 +192,6 @@ static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263; static int noncodeccapability = AST_RTP_DTMF; -static char ourhost[256]; static struct in_addr __ourip; static struct sockaddr_in outboundproxyip; static int ourport; @@ -8954,6 +8953,53 @@ return peer; } +static int find_ourip(void) +{ + char ourhost[256]; + struct ast_hostent ahp; + struct hostent *hp; + struct sip_pvt *p; + struct in_addr saddr; + + /* just use the bind address if it is nonzero */ + if (ntohl(bindaddr.sin_addr.s_addr)) { + memcpy(&__ourip, &bindaddr.sin_addr, sizeof(__ourip)); + return 0; + } + /* try to use our hostname */ + if (gethostname(ourhost, sizeof(ourhost))) { + ast_log(LOG_WARNING, "Unable to get hostname\n"); + } else { + hp = ast_gethostbyname(ourhost, &ahp); + if (hp) { + memcpy(&__ourip, hp->h_addr, sizeof(__ourip)); + return 0; + } + } + /* try to use the IP address the kernel thinks it will use to talk to the first peer */ + ASTOBJ_CONTAINER_TRAVERSE(®l, !__ourip.s_addr, do { + ASTOBJ_RDLOCK(iterator); + p = sip_alloc(iterator->callid, NULL, 0); + if (!p) { + ast_log(LOG_WARNING, "Unable to allocate SIP\n"); + } else { + if (create_addr(p, iterator->hostname)) { + ast_log(LOG_WARNING, "Unable to create address for %s\n", iterator->hostname); + } else if (ast_ouraddrfor(&p->sa.sin_addr, &__ourip)) { + ast_log(LOG_WARNING, "Unable to find our address from peer address\n"); + } + sip_destroy(p); + } + ASTOBJ_UNLOCK(iterator); + } while(0)); + if (__ourip.s_addr) + return 0; + /* A.ROOT-SERVERS.NET. */ + if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, &__ourip)) + return 0; + return -1; +} + /*--- reload_config: Re-read SIP.conf config file ---*/ /* This function reloads all config data, except for active peers (with registrations). They will only @@ -8975,10 +9021,6 @@ char iabuf[INET_ADDRSTRLEN]; struct ast_flags dummy; - if (gethostname(ourhost, sizeof(ourhost))) { - ast_log(LOG_WARNING, "Unable to get hostname, SIP disabled\n"); - return 0; - } cfg = ast_load(config); /* We *must* have a config file otherwise stop immediately */ @@ -9210,20 +9252,9 @@ } cat = ast_category_browse(cfg, cat); } - - /* Find our IP address */ - if (ntohl(bindaddr.sin_addr.s_addr)) { - memcpy(&__ourip, &bindaddr.sin_addr, sizeof(__ourip)); - } else { - hp = ast_gethostbyname(ourhost, &ahp); - if (!hp) { - ast_log(LOG_WARNING, "Unable to get IP address for %s, SIP disabled\n", ourhost); - if (!__ourip.s_addr) { - ast_destroy(cfg); - return 0; - } - } else - memcpy(&__ourip, hp->h_addr, sizeof(__ourip)); + if (find_ourip()) { + ast_log(LOG_WARNING, "Unable to get own IP address, SIP disabled\n"); + return 0; } if (!ntohs(bindaddr.sin_port)) bindaddr.sin_port = ntohs(DEFAULT_SIP_PORT);