Index: channels/chan_sip.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_sip.c,v
retrieving revision 1.797
diff -r1.797 chan_sip.c
488c488
< 
---
> #define SIP_PAGE2_FROMREALTIME		(1 << 4)
517a518
> 	struct sockaddr_in defaddr;			/* Our peer */
822a824
> 
937c939,942
< 	if (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE)
---
> 	if (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE) {
> 		if (!ntohs(p->recv.sin_port)) {
> 			p->recv.sin_port = p->defaddr.sin_port;
> 		}
939c944,947
< 	else
---
> 	} else {
> 		if (!ntohs(p->sa.sin_port)) {
> 			p->sa.sin_port = p->defaddr.sin_port;
> 		}
940a949,950
> 	}
> 
942c952,953
< 		ast_log(LOG_WARNING, "sip_xmit of %p (len %d) to %s returned %d: %s\n", data, len, ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr), res, strerror(errno));
---
> 		
> 		ast_log(LOG_WARNING, "sip_xmit of %p (len %d) to %s:%d returned %d: %s\n", data, len, ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr), res, ntohs(p->sa.sin_port), strerror(errno));
1491a1503
> 	char *fullcontact = NULL;
1515a1528,1531
> 		if (!strcasecmp(tmp->name, "fullcontact")) {
> 			fullcontact = ast_strdupa(tmp->value);
> 			url_decode(fullcontact);
> 		}
1524d1539
< 		
1526,1528c1541,1546
< 	peer = build_peer(newpeername, var, !ast_test_flag((&global_flags_page2), SIP_PAGE2_RTCACHEFRIENDS));
< 
< 	if (!peer) {
---
> 	
> 	if ((peer = build_peer(newpeername, var, !ast_test_flag((&global_flags_page2), SIP_PAGE2_RTCACHEFRIENDS)))) {
> 		if (fullcontact) {
> 			ast_copy_string(peer->fullcontact, fullcontact, sizeof(peer->fullcontact));
> 		}
> 	} else {
1531a1550,1552
> 
> 	ast_set_flag((&peer->flags_page2), SIP_PAGE2_FROMREALTIME);
> 
1534a1556
> 
1658a1681,1684
> 
> 	r->defaddr.sin_addr = peer->defaddr.sin_addr;
> 	r->defaddr.sin_port = peer->defaddr.sin_port;
> 
4345c4371,4379
< 		ast_copy_string(invite, p->fullcontact, sizeof(invite));
---
> 		char *scan, *addr, *port_str, *expiry_str, *username, *contact;
> 		/* try to split this fullcontact into the invite contact */
> 	 	scan=p->fullcontact; /*ast_copy_string(scan,p->fullcontact,sizeof(scan));*/
>         	addr = strsep(&scan, ":");
> 	        port_str = strsep(&scan, ":");
> 	        expiry_str = strsep(&scan, ":");
> 	        username = strsep(&scan, ":");
>         	contact = scan; /* Contact include sip: and has to be the last part of the database entry as long as we use : as a separator */
> 		ast_copy_string(invite,contact,sizeof(invite));
5032c5066,5069
< 	ast_db_del("SIP/Registry", peer->name);
---
> 
> 	if (!ast_test_flag(&(peer->flags_page2), SIP_PAGE2_FROMREALTIME)) {
> 		ast_db_del("SIP/Registry", peer->name);
> 	}
5065c5102,5104
< 	if (ast_db_get("SIP/Registry", peer->name, data, sizeof(data)))
---
> 	if (!ast_test_flag(&(peer->flags_page2), SIP_PAGE2_FROMREALTIME)) {
> 		ast_copy_string(data, peer->fullcontact, sizeof(data));
> 	} else if (ast_db_get("SIP/Registry", peer->name, data, sizeof(data))) {
5066a5106
> 	}
5074c5114
< 
---
> 	ast_log(LOG_DEBUG,"Ouer Contact from db is:%s",contact);
5219c5259,5261
< 		ast_db_del("SIP/Registry", p->name);
---
> 		if (!ast_test_flag(&(p->flags_page2), SIP_PAGE2_FROMREALTIME)) {
> 			ast_db_del("SIP/Registry", p->name);
> 		}
5231a5274,5278
> 	if (!ast_test_flag((&global_flags_page2), SIP_PAGE2_RTNOUPDATE)) {
> 		char buf[128];
> 		url_encode(c, buf, sizeof(buf));
> 		ast_update_realtime("sippeers", "name", p->name, "fullcontact", buf, NULL);
> 	}
5292c5339
< 	if (!(ast_test_flag(p, SIP_REALTIME) && ast_test_flag((&p->flags_page2), SIP_PAGE2_RTCACHEFRIENDS)))
---
> 	if (!ast_test_flag((&p->flags_page2), SIP_PAGE2_FROMREALTIME)) {
5293a5341,5345
> 	} else {
> 		char buf[128];
> 		url_encode(data, buf, sizeof(buf));
> 		ast_update_realtime("sippeers", "name", p->name, "fullcontact", buf, NULL);
> 	}
10631c10683
< 		if (realtime && !strcasecmp(v->name, "regseconds")) {
---
> 		if (!strcasecmp(v->name, "regseconds")) {
10634c10686
< 		} else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
---
> 		} else if (!strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
10636c10688,10692
< 		} else if (realtime && !strcasecmp(v->name, "name"))
---
> 			if (ast_test_flag((&global_flags_page2), SIP_PAGE2_RTIGNOREREGEXPIRE)) {
> 				inet_aton(v->value, &(peer->defaddr.sin_addr));
> 				found++;
> 			}
> 		} else if (!strcasecmp(v->name, "name"))
10786c10842,10843
< 	if (realtime && !ast_test_flag((&global_flags_page2), SIP_PAGE2_RTIGNOREREGEXPIRE) && ast_test_flag(peer, SIP_DYNAMIC)) {
---
> 
> 	if (!ast_test_flag((&global_flags_page2), SIP_PAGE2_RTIGNOREREGEXPIRE) && ast_test_flag(peer, SIP_DYNAMIC)) {
10791a10849
> 			ast_update_realtime("sippeers", "name", peer->name, "fullcontact", "", NULL);
10797c10855
< 	if (!found && ast_test_flag(peer, SIP_DYNAMIC) && !ast_test_flag(peer, SIP_REALTIME))
---
> 	if (!found && ast_test_flag(peer, SIP_DYNAMIC) && !ast_test_flag(peer, SIP_REALTIME)) 
10881a10940
> 
