Index: channels/chan_sip.c =================================================================== --- channels/chan_sip.c (revision 63181) +++ channels/chan_sip.c (working copy) @@ -613,6 +613,7 @@ char data[SIP_MAX_PACKET]; unsigned int sdp_start; /*!< the line number where the SDP begins */ unsigned int sdp_end; /*!< the line number where the SDP ends */ + struct sockaddr_in sin; /*!< the address on which the packet was received */ }; /* @@ -5578,6 +5579,7 @@ const char *ot; init_resp(resp, msg); + memcpy(&resp.sin, &req.sin, sizeof(resp.sin)); copy_via_headers(p, resp, req, "Via"); if (msg[0] == '2') copy_all_header(resp, req, "Record-Route"); @@ -14839,7 +14841,7 @@ */ static int sipsock_read(int *id, int fd, short events, void *ignore) { - struct sip_request req; + struct sip_request req = { 0, }; struct sockaddr_in sin = { 0, }; struct sip_pvt *p; int res; @@ -14847,8 +14849,15 @@ int nounlock; int recount = 0; int lockretry; + struct sinto { + struct cmsghdr cm; + struct sockaddr_in from; + struct sockaddr_in to; + } sinto; + struct msghdr msg = { .msg_control = &sinto, .msg_controllen = sizeof(sinto) }; - memset(&req, 0, sizeof(req)); + recvmsg(sipsock, &msg, MSG_PEEK); + memcpy(&req.sin, &sinto.to, sizeof(req.sin)); res = recvfrom(sipsock, req.data, sizeof(req.data) - 1, 0, (struct sockaddr *)&sin, &len); if (res < 0) { #if !defined(__FreeBSD__) @@ -16661,10 +16670,12 @@ } else { /* Allow SIP clients on the same host to access us: */ const int reuseFlag = 1; + const long true = 1L; setsockopt(sipsock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseFlag, sizeof reuseFlag); + setsockopt(sipsock, IPPROTO_IP, IP_PKTINFO, &true, sizeof(1L)); ast_enable_packet_fragmentation(sipsock);