Index: channels/chan_sip.c =================================================================== --- channels/chan_sip.c (revision 41805) +++ channels/chan_sip.c (working copy) @@ -9745,10 +9745,31 @@ { int expires, expires_ms; struct sip_registry *r; + char via[1024] = ""; r=p->registry; switch (resp) { case 401: /* Unauthorized */ + /* automatically set IP address if behind NAT router */ + + + memset(via, 0, sizeof(via)); + strncpy(via, get_header(req, "Via"), sizeof(via) - 1); + char *c = strstr(via, "received="); + if (c) { + char *received = c + 9; + struct hostent *hp; + struct ast_hostent ahp; + c = strchr(received, ';'); + if (c) + *c = '\0'; + ast_log(LOG_NOTICE, "REGISTER indicated my host name is %s\n", received); + if (!(hp = ast_gethostbyname(received, &ahp))) + ast_log(LOG_WARNING, "Invalid address for in received= in Via: %s\n",received); + else + memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr)); + } + if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "WWW-Authenticate", "Authorization")) { ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s@%s' (Tries %d)\n", p->registry->username, p->registry->hostname, p->authtries); ast_set_flag(p, SIP_NEEDDESTROY);