[svn-commits] oej: branch oej/obproxy r53289 - in /team/oej/obproxy: channels/ configs/

svn-commits at lists.digium.com svn-commits at lists.digium.com
Tue Feb 6 11:00:57 MST 2007


Author: oej
Date: Tue Feb  6 12:00:56 2007
New Revision: 53289

URL: http://svn.digium.com/view/asterisk?view=rev&rev=53289
Log:
You can now configure outboundproxy properly and use it.
In 1.4, outboundproxy= in the general section actually does *nothing*. Zilch. Nada.

Interesting feature.

Modified:
    team/oej/obproxy/channels/chan_sip.c
    team/oej/obproxy/configs/sip.conf.sample

Modified: team/oej/obproxy/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/oej/obproxy/channels/chan_sip.c?view=diff&rev=53289&r1=53288&r2=53289
==============================================================================
--- team/oej/obproxy/channels/chan_sip.c (original)
+++ team/oej/obproxy/channels/chan_sip.c Tue Feb  6 12:00:56 2007
@@ -353,6 +353,21 @@
 	REG_STATE_FAILED,	/*!< Registration failed after several tries */
 };
 
+/*! \brief definition of a sip proxy server 
+ *
+ * For outbound proxies, this is allocated in the SIP peer dynamically or 
+ * statically as the global_outboundproxy. The pointer in a SIP message is just
+ * a pointer and should *not* be de-allocated.
+ */
+struct sip_proxy {
+	char name[MAXHOSTNAMELEN];	/*!< DNS name of domain/host or IP */
+	struct sockaddr_in ip;		/*!< Currently used IP address and port */
+	time_t last_dnsupdate;		/*!< When this was resolved */
+	int force;			/*!< If it's an outbound proxy, Force use of this outbound proxy for all outbound requests */
+	/* Room for a SRV record chain based on the name */
+};
+
+
 #define CAN_NOT_CREATE_DIALOG	0
 #define CAN_CREATE_DIALOG	1
 #define CAN_CREATE_DIALOG_UNSUPPORTED_METHOD	2
@@ -556,6 +571,7 @@
 static int global_t1min;		/*!< T1 roundtrip time minimum */
 static int global_autoframing;          /*!< Turn autoframing on or off. */
 static enum transfermodes global_allowtransfer;	/*!< SIP Refer restriction scheme */
+static struct sip_proxy global_outboundproxy;	/*!< Outbound proxy */
 
 /*! \brief Codecs that we support by default: */
 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
@@ -946,6 +962,7 @@
 	int jointnoncodeccapability;            /*!< Joint Non codec capability */
 	int redircodecs;			/*!< Redirect codecs */
 	int maxcallbitrate;			/*!< Maximum Call Bitrate for Video Calls */	
+	struct sip_proxy *outboundproxy;	/*!< Outbound proxy for this dialog */
 	struct t38properties t38;		/*!< T38 settings */
 	struct sockaddr_in udptlredirip;	/*!< Where our T.38 UDPTL should be going if not to us */
 	struct ast_udptl *udptl;		/*!< T.38 UDPTL session */
@@ -1094,6 +1111,7 @@
 	int rtpkeepalive;		/*!<  Send RTP packets for keepalive */
 	ast_group_t callgroup;		/*!<  Call group */
 	ast_group_t pickupgroup;	/*!<  Pickup group */
+	struct sip_proxy *outboundproxy; /*!<  Outbound proxy for this peer */
 	struct sockaddr_in addr;	/*!<  IP address of peer */
 	int maxcallbitrate;		/*!< Maximum Bitrate for a video call */
 	
@@ -1183,7 +1201,6 @@
 static int externrefresh = 10;
 static struct ast_ha *localaddr;		/*!< List of local networks, on the same side of NAT as this Asterisk */
 static struct in_addr __ourip;
-static struct sockaddr_in outboundproxyip;
 static int ourport;
 static struct sockaddr_in debugaddr;
 
@@ -1596,6 +1613,14 @@
 	set_udptl_peer: sip_set_udptl_peer,
 };
 
+/*! \brief Append to SIP dialog history 
+	\return Always returns 0 */
+#define append_history(p, event, fmt , args... )	append_history_full(p, "%-15s " fmt, event, ## args)
+
+static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
+	__attribute__ ((format (printf, 2, 3)));
+
+
 /*! \brief Convert transfer status to string */
 static char *referstatus2str(enum referstatus rstatus)
 {
@@ -1631,6 +1656,57 @@
 	ast_set_flag(&dialog->flags[0], SIP_ALREADYGONE);
 }
 
+/*! Resolve DNS srv name or host name in a sip_proxy structure */
+static int proxy_update(struct sip_proxy *proxy)
+{
+	/* if it's actually an IP address and not a name,
+           there's no need for a managed lookup */
+        if (!inet_aton(proxy->name, &proxy->ip.sin_addr)) {
+		/* Ok, not an IP address, then let's check if it's a domain or host */
+		/* XXX Todo - if we have proxy port, don't do SRV */
+		if (ast_get_ip_or_srv(&proxy->ip, proxy->name, srvlookup ? "_sip._udp" : NULL) < 0) {
+			ast_log(LOG_WARNING, "Unable to locate host '%s'\n", proxy->name);
+			return FALSE;
+		}
+	}
+	proxy->last_dnsupdate = time(NULL);
+	return TRUE;
+}
+
+/*! \brief Allocate and initialize sip proxy */
+static struct sip_proxy *proxy_allocate(char *name, char *port, int force)
+{
+	struct sip_proxy *proxy;
+	proxy = ast_calloc(1, sizeof(struct sip_proxy));
+	if (!proxy)
+		return NULL;
+	proxy->force = force;
+	ast_copy_string(proxy->name, name, sizeof(proxy->name));
+	if (!ast_strlen_zero(port))
+		proxy->ip.sin_port = htons(atoi(port));
+	proxy_update(proxy);
+	return proxy;
+}
+
+/*! \brief Get default outbound proxy or global proxy */
+static struct sip_proxy *obproxy_get(struct sip_pvt *dialog, struct sip_peer *peer)
+{
+	if (peer && peer->outboundproxy) {
+		if (option_debug && sipdebug)
+			ast_log(LOG_DEBUG, "OBPROXY: Applying peer OBproxy to this call\n");
+		append_history(dialog, "OBproxy", "Using peer obproxy %s", peer->outboundproxy->name);
+		return peer->outboundproxy;
+	}
+	if (global_outboundproxy.name[0]) {
+		if (option_debug && sipdebug)
+			ast_log(LOG_DEBUG, "OBPROXY: Applying global OBproxy to this call\n");
+		append_history(dialog, "OBproxy", "Using global obproxy %s", global_outboundproxy.name);
+		return &global_outboundproxy;
+	}
+	if (option_debug && sipdebug)
+		ast_log(LOG_DEBUG, "OBPROXY: Not applying OBproxy to this call\n");
+	return NULL;
+}
 
 /*! \brief returns true if 'name' (with optional trailing whitespace)
  * matches the sip method 'id'.
@@ -1722,6 +1798,9 @@
 /*! \brief The real destination address for a write */
 static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p)
 {
+	if (p->outboundproxy)
+		return &p->outboundproxy->ip;
+
 	return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? &p->recv : &p->sa;
 }
 
@@ -1800,13 +1879,6 @@
 		*us = bindaddr.sin_addr;
 	return AST_SUCCESS;
 }
-
-/*! \brief Append to SIP dialog history 
-	\return Always returns 0 */
-#define append_history(p, event, fmt , args... )	append_history_full(p, "%-15s " fmt, event, ## args)
-
-static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
-	__attribute__ ((format (printf, 2, 3)));
 
 /*! \brief Append to SIP dialog history with arg list  */
 static void append_history_va(struct sip_pvt *p, const char *fmt, va_list ap)
@@ -2059,6 +2131,14 @@
 	msg = sip_methods[sipmethod].text;
 
 	ast_mutex_lock(&p->lock);
+	
+	/* If we have an outbound proxy for this dialog, then delete it now since
+	   the rest of the requests in this dialog needs to follow the routing.
+	   If obforcing is set, we will keep the outbound proxy during the whole
+	   dialog, regardless of what the SIP rfc says 
+ 	 */
+	if (p->outboundproxy && !p->outboundproxy->force)
+		p->outboundproxy = NULL;
 	for (cur = p->packets; cur; prev = cur, cur = cur->next) {
 		if ((cur->seqno == seqno) && ((ast_test_flag(cur, FLAG_RESPONSE)) == resp) &&
 			((ast_test_flag(cur, FLAG_RESPONSE)) || 
@@ -2182,6 +2262,13 @@
 {
 	int res;
 
+	/* If we have an outbound proxy, reset peer address 
+		Only do this once.
+	*/
+	if (p->outboundproxy) {
+		p->sa = p->outboundproxy->ip;
+	}
+
 	add_blank(req);
 	if (sip_debug_test_pvt(p)) {
 		if (ast_test_flag(&p->flags[0], SIP_NAT_ROUTE))
@@ -2354,6 +2441,9 @@
 {
 	if (option_debug > 2)
 		ast_log(LOG_DEBUG, "Destroying SIP peer %s\n", peer->name);
+
+	if (peer->outboundproxy)
+		free(peer->outboundproxy);
 
 	/* Delete it, it needs to disappear */
 	if (peer->call)
@@ -2661,6 +2751,7 @@
 			ast_string_field_build(dialog, callid, "%s@%s", tmpcall, peer->fromdomain);
 		}
 	}
+	dialog->outboundproxy = obproxy_get(dialog, peer);
 	if (ast_strlen_zero(dialog->tohost))
 		ast_string_field_set(dialog, tohost, ast_inet_ntoa(dialog->sa.sin_addr));
 	if (!ast_strlen_zero(peer->fromdomain))
@@ -2715,6 +2806,18 @@
 		ASTOBJ_UNREF(p, sip_destroy_peer);
 		return res;
 	}
+	ast_string_field_set(dialog, tohost, peer);
+
+	/* Get the outbound proxy information */
+	dialog->outboundproxy = obproxy_get(dialog, NULL);
+
+	/* If we have an outbound proxy, don't bother with DNS resolution at all */
+	if (dialog->outboundproxy) 
+		return 0;
+
+	/* Let's see if we can find the host in DNS. First try DNS SRV records,
+	   then hostname lookup 
+	 */
 	hostn = peer;
 	portno = port ? atoi(port) : STANDARD_SIP_PORT;
 	if (srvlookup) {
@@ -2734,7 +2837,6 @@
 		ast_log(LOG_WARNING, "No such host: %s\n", peer);
 		return -1;
 	}
-	ast_string_field_set(dialog, tohost, peer);
 	memcpy(&dialog->sa.sin_addr, hp->h_addr, sizeof(dialog->sa.sin_addr));
 	dialog->sa.sin_port = htons(portno);
 	dialog->recv = dialog->sa;
@@ -7191,21 +7293,26 @@
 		}
 		if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
 			append_history(p, "RegistryInit", "Account: %s@%s", r->username, r->hostname);
+
+		p->outboundproxy = obproxy_get(p, NULL);
+
 		/* Find address to hostname */
 		if (create_addr(p, r->hostname)) {
 			/* we have what we hope is a temporary network error,
 			 * probably DNS.  We need to reschedule a registration try */
-			sip_destroy(p);
-			if (r->timeout > -1) {
-				ast_sched_del(sched, r->timeout);
-				r->timeout = ast_sched_add(sched, global_reg_timeout*1000, sip_reg_timeout, r);
-				ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
-			} else {
-				r->timeout = ast_sched_add(sched, global_reg_timeout*1000, sip_reg_timeout, r);
-				ast_log(LOG_WARNING, "Probably a DNS error for registration to %s@%s, trying REGISTER again (after %d seconds)\n", r->username, r->hostname, global_reg_timeout);
+			if (!p->outboundproxy) {
+				sip_destroy(p);
+				if (r->timeout > -1) {
+					ast_sched_del(sched, r->timeout);
+					r->timeout = ast_sched_add(sched, global_reg_timeout*1000, sip_reg_timeout, r);
+					ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
+				} else {
+					r->timeout = ast_sched_add(sched, global_reg_timeout*1000, sip_reg_timeout, r);
+					ast_log(LOG_WARNING, "Probably a DNS error for registration to %s@%s, trying REGISTER again (after %d seconds)\n", r->username, r->hostname, global_reg_timeout);
+				}
+				r->regattempts++;
+				return 0;
 			}
-			r->regattempts++;
-			return 0;
 		}
 		/* Copy back Call-ID in case create_addr changed it */
 		ast_string_field_set(r, callid, p->callid);
@@ -7324,8 +7431,9 @@
 	add_header_contentLength(&req, 0);
 
 	initialize_initreq(p, &req);
-	if (sip_debug_test_pvt(p))
+	if (sip_debug_test_pvt(p)) {
 		ast_verbose("REGISTER %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
+	}
 	r->regstate = auth ? REG_STATE_AUTHSENT : REG_STATE_REGSENT;
 	r->regattempts++;	/* Another attempt */
 	if (option_debug > 3)
@@ -10002,6 +10110,9 @@
 		ast_cli(fd, "  Send RPID    : %s\n", ast_test_flag(&peer->flags[0], SIP_SENDRPID) ? "Yes" : "No");
 		ast_cli(fd, "  Subscriptions: %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE) ? "Yes" : "No");
 		ast_cli(fd, "  Overlap dial : %s\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWOVERLAP) ? "Yes" : "No");
+		if (peer->outboundproxy)
+			ast_cli(fd, "  Outb. proxy  : %s %s\n", ast_strlen_zero(peer->outboundproxy->name) ? "<not set>" : peer->outboundproxy->name,
+							peer->outboundproxy->force ? "(forced)" : "");
 
 		/* - is enumerated */
 		ast_cli(fd, "  DTMFmode     : %s\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
@@ -10290,8 +10401,11 @@
 	ast_cli(fd, "  Notify ringing state:   %s\n", global_notifyringing ? "Yes" : "No");
 	ast_cli(fd, "  Notify hold state:      %s\n", global_notifyhold ? "Yes" : "No");
 	ast_cli(fd, "  SIP Transfer mode:      %s\n", transfermode2str(global_allowtransfer));
-	ast_cli(fd, "  Max Call Bitrate:       %d kbps\r\n", default_maxcallbitrate);
-	ast_cli(fd, "  Auto-Framing:           %s \r\n", global_autoframing ? "Yes" : "No");
+	ast_cli(fd, "  Max Call Bitrate:       %d kbps\n", default_maxcallbitrate);
+	ast_cli(fd, "  Auto-Framing:           %s\n", global_autoframing ? "Yes" : "No");
+	ast_cli(fd, "  Outb. proxy:            %s %s\n", ast_strlen_zero(global_outboundproxy.name) ? "<not set>" : global_outboundproxy.name,
+							global_outboundproxy.force ? "(forced)" : "");
+
 	ast_cli(fd, "\nDefault Settings:\n");
 	ast_cli(fd, "-----------------\n");
 	ast_cli(fd, "  Context:                %s\n", default_context);
@@ -15782,7 +15896,6 @@
 {
 	struct sip_peer *peer = NULL;
 	struct ast_ha *oldha = NULL;
-	int obproxyfound=0;
 	int found=0;
 	int firstpass=1;
 	int format=0;		/* Ama flags */
@@ -15866,22 +15979,32 @@
 			ast_set2_flag(&peer->flags[0], ast_true(v->value), SIP_USEREQPHONE);
 		} else if (!strcasecmp(v->name, "fromuser")) {
 			ast_copy_string(peer->fromuser, v->value, sizeof(peer->fromuser));
-		} else if (!strcasecmp(v->name, "host") || !strcasecmp(v->name, "outboundproxy")) {
+		} else if (!strcasecmp(v->name, "outboundproxy")) {
+			char *force, *port;
+			/* Set peer channel variable */
+			varname = ast_strdupa(v->value);
+			force = varname;
+			if ((port = strchr(varname, ':'))) {
+				*port++ = '\0';
+				force = port;
+			}
+			if ((varval = strchr(force, ','))) {
+				*varval++ = '\0';
+			}
+			/* Allocate proxy object */
+			peer->outboundproxy = proxy_allocate(varname, port, !strcmp(varval, "force"));
+		} else if (!strcasecmp(v->name, "host")) {
 			if (!strcasecmp(v->value, "dynamic")) {
-				if (!strcasecmp(v->name, "outboundproxy") || obproxyfound) {
-					ast_log(LOG_WARNING, "You can't have a dynamic outbound proxy, you big silly head at line %d.\n", v->lineno);
-				} else {
-					/* They'll register with us */
-					ast_set_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
-					if (!found) {
-						/* Initialize stuff iff we're not found, otherwise
-						   we keep going with what we had */
-						memset(&peer->addr.sin_addr, 0, 4);
-						if (peer->addr.sin_port) {
-							/* If we've already got a port, make it the default rather than absolute */
-							peer->defaddr.sin_port = peer->addr.sin_port;
-							peer->addr.sin_port = 0;
-						}
+				/* They'll register with us */
+				ast_set_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
+				if (!found) {
+					/* Initialize stuff iff we're not found, otherwise
+					   we keep going with what we had */
+					memset(&peer->addr.sin_addr, 0, 4);
+					if (peer->addr.sin_port) {
+						/* If we've already got a port, make it the default rather than absolute */
+						peer->defaddr.sin_port = peer->addr.sin_port;
+						peer->addr.sin_port = 0;
 					}
 				}
 			} else {
@@ -15890,19 +16013,13 @@
 					ast_sched_del(sched, peer->expire);
 				peer->expire = -1;
 				ast_clear_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
-				if (!obproxyfound || !strcasecmp(v->name, "outboundproxy")) {
-					if (ast_get_ip_or_srv(&peer->addr, v->value, srvlookup ? "_sip._udp" : NULL)) {
-						ASTOBJ_UNREF(peer, sip_destroy_peer);
-						return NULL;
-					}
+				if (ast_get_ip_or_srv(&peer->addr, v->value, srvlookup ? "_sip._udp" : NULL)) {
+					ASTOBJ_UNREF(peer, sip_destroy_peer);
+					return NULL;
 				}
-				if (!strcasecmp(v->name, "outboundproxy"))
-					obproxyfound=1;
-				else {
-					ast_copy_string(peer->tohost, v->value, sizeof(peer->tohost));
-					if (!peer->addr.sin_port)
-						peer->addr.sin_port = htons(STANDARD_SIP_PORT);
-				}
+				ast_copy_string(peer->tohost, v->value, sizeof(peer->tohost));
+				if (!peer->addr.sin_port)
+					peer->addr.sin_port = htons(STANDARD_SIP_PORT);
 			}
 		} else if (!strcasecmp(v->name, "defaultip")) {
 			if (ast_get_ip(&peer->defaddr, v->value)) {
@@ -16071,8 +16188,9 @@
 	memset(&localaddr, 0, sizeof(localaddr));
 	memset(&externip, 0, sizeof(externip));
 	memset(&default_prefs, 0 , sizeof(default_prefs));
-	outboundproxyip.sin_port = htons(STANDARD_SIP_PORT);
-	outboundproxyip.sin_family = AF_INET;	/* Type of address: IPv4 */
+	memset(&global_outboundproxy, 0, sizeof(struct sip_proxy));
+	global_outboundproxy.ip.sin_port = htons(STANDARD_SIP_PORT);
+	global_outboundproxy.ip.sin_family = AF_INET;	/* Type of address: IPv4 */
 	ourport = STANDARD_SIP_PORT;
 	srvlookup = DEFAULT_SRVLOOKUP;
 	global_tos_sip = DEFAULT_TOS_SIP;
@@ -16081,7 +16199,6 @@
 	externhost[0] = '\0';			/* External host name (for behind NAT DynDNS support) */
 	externexpire = 0;			/* Expiration for DNS re-issuing */
 	externrefresh = 10;
-	memset(&outboundproxyip, 0, sizeof(outboundproxyip));
 
 	/* Reset channel settings to default before re-configuring */
 	allow_external_domains = DEFAULT_ALLOW_EXT_DOM;				/* Allow external invites */
@@ -16247,12 +16364,20 @@
 		} else if (!strcasecmp(v->name, "fromdomain")) {
 			ast_copy_string(default_fromdomain, v->value, sizeof(default_fromdomain));
 		} else if (!strcasecmp(v->name, "outboundproxy")) {
-			if (ast_get_ip_or_srv(&outboundproxyip, v->value, srvlookup ? "_sip._udp" : NULL) < 0)
-				ast_log(LOG_WARNING, "Unable to locate host '%s'\n", v->value);
-		} else if (!strcasecmp(v->name, "outboundproxyport")) {
-			/* Port needs to be after IP */
-			sscanf(v->value, "%d", &format);
-			outboundproxyip.sin_port = htons(format);
+			char *name, *port = NULL, *force;
+
+			name = ast_strdupa(v->value);
+			if ((port = strchr(name, ':'))) {
+				*port++ = '\0';
+				global_outboundproxy.ip.sin_port = htons(atoi(port));
+			}
+	
+			if ((force = strchr(port ? port : name, ','))) {
+				*force++ = '\0';
+				global_outboundproxy.force = (!strcasecmp(force, "force"));
+			}
+			ast_copy_string(global_outboundproxy.name, name, sizeof(global_outboundproxy.name));
+			proxy_update(&global_outboundproxy);
 		} else if (!strcasecmp(v->name, "autocreatepeer")) {
 			autocreatepeer = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "srvlookup")) {

Modified: team/oej/obproxy/configs/sip.conf.sample
URL: http://svn.digium.com/view/asterisk/team/oej/obproxy/configs/sip.conf.sample?view=diff&rev=53289&r1=53288&r2=53289
==============================================================================
--- team/oej/obproxy/configs/sip.conf.sample (original)
+++ team/oej/obproxy/configs/sip.conf.sample Tue Feb  6 12:00:56 2007
@@ -135,6 +135,10 @@
 				; for Sipura and Grandstream ATAs, among others). This is
 				; contrary to the RFC3551 specification, the peer _should_
 				; be negotiating AAL2-G726-32 instead :-(
+
+;outboundproxy=proxy.provider.domain	   	; send outbound signaling to this proxy, not directly to the devices
+;outboundproxy=proxy.provider.domain:8080       ; send outbound signaling to this proxy, not directly to the devices
+;outboundproxy=proxy.provider.domain,force      ; Send ALL outbound signalling to proxy, ignoring route: headers
 
 ;
 ; If regcontext is specified, Asterisk will dynamically create and destroy a
@@ -509,6 +513,7 @@
 ;usereqphone=yes			; This provider requires ";user=phone" on URI
 ;call-limit=5				; permit only 5 simultaneous outgoing calls to this peer
 ;outboundproxy=proxy.provider.domain	; send outbound signaling to this proxy, not directly to the peer
+;outboundproxy=proxy.provider.domain,force ; Send ALL outbound signalling to proxy, ignoring route: headers
 					; Call-limits will not be enforced on real-time peers,
 					; since they are not stored in-memory
 ;port=80				; The port number we want to connect to on the remote side



More information about the svn-commits mailing list