--- channels/chan_sip.c.jnx 2004-12-18 14:50:22.946600373 +0100 +++ channels/chan_sip.c 2004-12-18 15:25:59.659854934 +0100 @@ -190,7 +190,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 int ourport; @@ -8433,6 +8432,53 @@ return peer; } +static int find_ourip(void) +{ + char ourhost[256]; + struct ast_hostent ahp; + struct hostent *hp; + struct sip_pvt *p; + + /* 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 */ + ast_mutex_lock(®l.lock); + if (regl.registrations) { + p = sip_alloc(regl.registrations->callid, NULL, 0); + if (!p) { + ast_log(LOG_WARNING, "Unable to allocate SIP\n"); + } else { + if (create_addr(p, regl.registrations->hostname)) { + ast_log(LOG_WARNING, "Unable to create address for %s\n", regl.registrations->hostname); + } else { + if (ast_ouraddrfor(&p->sa.sin_addr, &__ourip)) { + ast_log(LOG_WARNING, "Unable to find our address from peer address\n"); + } else { + sip_destroy(p); + ast_mutex_unlock(®l.lock); + return 0; + } + } + sip_destroy(p); + } + } + ast_mutex_unlock(®l.lock); + return -1; +} + /*--- reload_config: Re-read SIP.conf config file ---*/ static int reload_config(void) { @@ -8451,10 +8497,6 @@ global_dtmfmode = SIP_DTMF_RFC2833; global_promiscredir = 0; - 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 */ @@ -8674,19 +8716,9 @@ } cat = ast_category_browse(cfg, cat); } - - 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);