Index: channels/chan_sip.c =================================================================== --- channels/chan_sip.c (revision 188546) +++ channels/chan_sip.c (working copy) @@ -2123,7 +2123,7 @@ static char *sip_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a); /*--- Internal UA client handling (outbound registrations) */ -static void ast_sip_ouraddrfor(struct in_addr *them, struct sockaddr_in *us); +static void ast_sip_ouraddrfor(struct in_addr *them, struct sockaddr_in *us, struct sip_pvt *p); static void sip_registry_destroy(struct sip_registry *reg); static int sip_register(const char *value, int lineno); static const char *regstate2str(enum sipregistrystate regstate) attribute_const; @@ -2923,7 +2923,7 @@ * externip or can get away with our internal bindaddr * 'us' is always overwritten. */ -static void ast_sip_ouraddrfor(struct in_addr *them, struct sockaddr_in *us) +static void ast_sip_ouraddrfor(struct in_addr *them, struct sockaddr_in *us, struct sip_pvt *p) { struct sockaddr_in theirs; /* Set want_remap to non-zero if we want to remap 'us' to an externally @@ -2967,9 +2967,24 @@ ast_log(LOG_WARNING, "stun failed\n"); ast_debug(1, "Target address %s is not local, substituting externip\n", ast_inet_ntoa(*(struct in_addr *)&them->s_addr)); - } else if (bindaddr.sin_addr.s_addr) { + } else { /* no remapping, but we bind to a specific address, so use it. */ - *us = bindaddr; + switch (p->socket.type) { + case (SIP_TRANSPORT_TCP): + if (sip_tcp_desc.local_address.sin_addr.s_addr) { + *us = sip_tcp_desc.local_address; + } + case (SIP_TRANSPORT_TLS): + if (sip_tls_desc.local_address.sin_addr.s_addr) { + *us = sip_tls_desc.local_address; + } + case (SIP_TRANSPORT_UDP): + /* fall through on purpose */ + default: + if (bindaddr.sin_addr.s_addr) { + *us = bindaddr; + } + } } } @@ -6246,7 +6261,7 @@ p->ourip = internip; else { p->sa = *sin; - ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip); + ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p); } /* Copy global flags to this PVT at setup. */ @@ -8220,7 +8235,7 @@ p->ourip = internip; else { p->sa = *sin; - ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip); + ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p); } p->branch = ast_random(); @@ -9141,13 +9156,10 @@ int ourport = ntohs(p->ourip.sin_port); - if (p->socket.type & SIP_TRANSPORT_UDP) { - if (!sip_standard_port(p->socket.type, ourport)) - ast_string_field_build(p, our_contact, "", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip.sin_addr), ourport); - else - ast_string_field_build(p, our_contact, "", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip.sin_addr)); - } else - ast_string_field_build(p, our_contact, "", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip.sin_addr), ourport, get_transport(p->socket.type)); + if (!sip_standard_port(p->socket.type, ourport)) + ast_string_field_build(p, our_contact, "", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip.sin_addr), ourport); + else + ast_string_field_build(p, our_contact, "", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip.sin_addr)); } /*! \brief Build the Remote Party-ID & From using callingpres options */ @@ -9815,7 +9827,7 @@ ast_set_flag(&p->flags[0], SIP_OUTGOING); /* Recalculate our side, and recalculate Call ID */ - ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip); + ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p); build_via(p); ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name"); build_callid_pvt(p); @@ -10077,7 +10089,7 @@ based on whether the remote host is on the external or internal network so we can register through nat */ - ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip); + ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p); build_contact(p); } @@ -15160,7 +15172,7 @@ ast_set_flag(&p->flags[0], SIP_OUTGOING); /* Recalculate our side, and recalculate Call ID */ - ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip); + ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p); build_via(p); ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name"); build_callid_pvt(p); @@ -20256,7 +20268,7 @@ return 0; } /* Recalculate our side, and recalculate Call ID */ - ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip); + ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p); build_via(p); ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name"); build_callid_pvt(p); @@ -20841,7 +20853,7 @@ ast_string_field_set(p, tohost, ast_inet_ntoa(peer->addr.sin_addr)); /* Recalculate our side, and recalculate Call ID */ - ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip); + ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p); build_via(p); ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name"); build_callid_pvt(p); @@ -21103,7 +21115,7 @@ if (ast_strlen_zero(p->peername) && ext) ast_string_field_set(p, peername, ext); /* Recalculate our side, and recalculate Call ID */ - ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip); + ast_sip_ouraddrfor(&p->sa.sin_addr, &p->ourip, p); build_via(p); ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name"); build_callid_pvt(p);