[asterisk-commits] bbryant: branch bbryant/sip-tcptls r72934 - in /team/bbryant/sip-tcptls: chan...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jul 2 15:18:34 CDT 2007


Author: bbryant
Date: Mon Jul  2 15:18:34 2007
New Revision: 72934

URL: http://svn.digium.com/view/asterisk?view=rev&rev=72934
Log:
Added TCP/TLS support for sip registrations, peers, and srv lookups.
Also made sure the packets contained the correct information about the socket it's being sent from.

Modified:
    team/bbryant/sip-tcptls/channels/chan_sip.c
    team/bbryant/sip-tcptls/main/server.c

Modified: team/bbryant/sip-tcptls/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/bbryant/sip-tcptls/channels/chan_sip.c?view=diff&rev=72934&r1=72933&r2=72934
==============================================================================
--- team/bbryant/sip-tcptls/channels/chan_sip.c (original)
+++ team/bbryant/sip-tcptls/channels/chan_sip.c Mon Jul  2 15:18:34 2007
@@ -633,6 +633,13 @@
 #define DEC_CALL_RINGING 2
 #define INC_CALL_RINGING 3
 
+struct sip_socket {
+	int type;
+	int fd;
+	short port;
+	struct server_instance *ser;
+};
+
 /*! \brief sip_request: The data grabbed from the UDP socket */
 struct sip_request {
 	char *rlPart1; 	        /*!< SIP Method Name or "SIP/2.0" protocol version */
@@ -647,6 +654,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 sip_socket socket;
 };
 
 /*
@@ -868,9 +876,11 @@
 #define sipdebug_text		ast_test_flag(&global_flags[1], SIP_PAGE2_DEBUG_TEXT)
 
 /*!< Define some SIP transports */
-#define SIP_TRANSPORT_UDP (1 << 0)
-#define SIP_TRANSPORT_TCP (1 << 1)
-#define SIP_TRANSPORT_TLS (1 << 2)
+enum {
+	SIP_TRANSPORT_UDP = 1,
+	SIP_TRANSPORT_TCP = 1 << 1,
+	SIP_TRANSPORT_TLS = 1 << 2
+};
 
 /*! \brief T38 States for a call */
 enum t38state {
@@ -985,9 +995,7 @@
 		AST_STRING_FIELD(rpid_from);	/*!< Our RPID From header */
 		AST_STRING_FIELD(url);		/*!< URL to be sent with next message to peer */
 	);
-	int transport;
-	int fd;
-	struct server_instance *ser;
+	struct sip_socket socket;
 	unsigned int ocseq;			/*!< Current outgoing seqno */
 	unsigned int icseq;			/*!< Current incoming seqno */
 	ast_group_t callgroup;			/*!< Call group */
@@ -1104,6 +1112,7 @@
 struct sip_user {
 	/* Users who can access various contexts */
 	ASTOBJ_COMPONENTS(struct sip_user);
+	int transport;
 	char secret[80];		/*!< Password */
 	char md5secret[80];		/*!< Password in md5 */
 	char context[AST_MAX_CONTEXT];	/*!< Default context for incoming calls */
@@ -1137,6 +1146,7 @@
 struct sip_peer {
 	ASTOBJ_COMPONENTS(struct sip_peer);	/*!< name, refcount, objflags,  object pointers */
 					/*!< peer->name is the unique name of this object */
+	int transport;
 	char secret[80];		/*!< Password */
 	char md5secret[80];		/*!< Password in MD5 */
 	struct sip_auth *auth;		/*!< Realm authentication list */
@@ -1271,6 +1281,8 @@
 static struct ast_ha *localaddr;		/*!< List of local networks, on the same side of NAT as this Asterisk */
 static struct in_addr __ourip;
 static int ourport;
+static int ourport_tcp;
+static int ourport_tls;
 static struct sockaddr_in debugaddr;
 
 static struct ast_config *notify_types;		/*!< The list of manual NOTIFY types we know how to send */
@@ -1295,8 +1307,10 @@
 static int sip_senddigit_begin(struct ast_channel *ast, char digit);
 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
 
-#define get_transport(p) ( ( p->transport & SIP_TRANSPORT_UDP ) ? "UDP" : ( p->transport & SIP_TRANSPORT_TCP ) ? "TCP" : "TLS" )
-static int handle_request_do(struct sip_request *req, struct sockaddr_in *sin, int transport, int fd);
+#define get_transport(t) ( ( t & SIP_TRANSPORT_UDP ) ? "UDP" : ( t & SIP_TRANSPORT_TCP ) ? "TCP" : "TLS" )
+static int handle_request_do(struct sip_request *req, struct sockaddr_in *sin);
+static int sip_standard_port(struct sip_socket s);
+static void sip_prepare_socket(struct sip_pvt *p);
 
 /*--- Transmitting responses and requests */
 static int sipsock_read(int *id, int fd, short events, void *ignore);
@@ -1750,7 +1764,19 @@
 			}
 		}
 
-		handle_request_do(&req, &ser->requestor, ((ser->parent->tls_cfg) ? SIP_TRANSPORT_TLS : SIP_TRANSPORT_TCP), ser->fd);
+		req.socket.fd = ser->fd;
+
+		if (ser->parent->tls_cfg) {
+			req.socket.type = SIP_TRANSPORT_TLS;
+			req.socket.port = ourport_tls;
+		} else {
+			req.socket.type = SIP_TRANSPORT_TCP;
+			req.socket.port = ourport_tcp;
+		}
+
+		req.socket.ser = ser;
+
+		handle_request_do(&req, &ser->requestor);
 	}
 
 	return NULL;
@@ -2008,12 +2034,14 @@
 static int __sip_xmit(struct sip_pvt *p, char *data, int len)
 {
 	int res;
-//	int fd;
 	const struct sockaddr_in *dst = sip_real_dst(p);
 
-//	fd = (p->transport & SIP_TRANSPORT_UDP) ? sipsock : (p->ser) ? p->ser->fd : p->fd;
-
-	res = sendto(p->fd, data, len, 0, (const struct sockaddr *)dst, sizeof(struct sockaddr_in));
+	sip_prepare_socket(p);
+
+	if (p->socket.fd == -1)
+		return XMIT_ERROR;
+
+	res = sendto(p->socket.fd, data, len, 0, (const struct sockaddr *)dst, sizeof(struct sockaddr_in));
 
 	if (res == -1) {
 		switch (errno) {
@@ -2026,6 +2054,7 @@
 	}
 	if (res != len)
 		ast_log(LOG_WARNING, "sip_xmit of %p (len %d) to %s:%d returned %d: %s\n", data, len, ast_inet_ntoa(dst->sin_addr), ntohs(dst->sin_port), res, strerror(errno));
+
 	return res;
 }
 
@@ -2038,7 +2067,7 @@
 
 	/* z9hG4bK is a magic cookie.  See RFC 3261 section 8.1.1.7 */
 	ast_string_field_build(p, via, "SIP/2.0/%s %s:%d;branch=z9hG4bK%08x%s",
-			get_transport(p), ast_inet_ntoa(p->ourip), ourport, p->branch, rport);
+			get_transport(p->socket.type), ast_inet_ntoa(p->ourip), p->socket.port, p->branch, rport);
 }
 
 /*! \brief NAT fix - decide which IP address to use for ASterisk server?
@@ -2119,7 +2148,7 @@
 	int xmitres = 0;
 
 	/* Don't retransmit TCP packets */
-	if (!(pkt->owner->transport & SIP_TRANSPORT_UDP))
+	if (!(pkt->owner->socket.type & SIP_TRANSPORT_UDP))
 		return 0;
 
 	/* Lock channel PVT */
@@ -3060,6 +3089,7 @@
  */
 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
 {
+	dialog->socket.type = peer->transport;
 	if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
 	    (!peer->maxms || ((peer->lastms >= 0)  && (peer->lastms <= peer->maxms)))) {
 		dialog->sa = (peer->addr.sin_addr.s_addr) ? peer->addr : peer->defaddr;
@@ -3217,7 +3247,7 @@
 		int tportno;
 		int ret;
 
-		snprintf(service, sizeof(service), "_sip._udp.%s", peername);
+		snprintf(service, sizeof(service), "_sip._%s.%s", get_transport(dialog->socket.type), peername);
 		ret = ast_get_srv(NULL, host, sizeof(host), &tportno, service);
 		if (ret > 0) {
 			hostn = host;
@@ -3334,7 +3364,7 @@
 		p->t38.jointcapability = p->t38.capability;
 		ast_debug(2,"Our T38 capability (%d), joint T38 capability (%d)\n", p->t38.capability, p->t38.jointcapability);
 
-		if (!(p->transport & SIP_TRANSPORT_UDP)) {
+		if (!(p->socket.type & SIP_TRANSPORT_UDP)) {
 			char name[256];
 			struct server_args ca;
 
@@ -3342,19 +3372,19 @@
 
 			ca.name = name;
 			ca.accept_fd = -1;
-			ca.tls_cfg = (p->ser) ? p->ser->parent->tls_cfg : NULL;
+			ca.tls_cfg = (p->socket.ser) ? p->socket.ser->parent->tls_cfg : NULL;
 			ca.sin = p->sa;
-			p->ser = (!p->ser) ? client_start(&ca) : p->ser;
-
-			if (!p->ser)
+			p->socket.ser = (!p->socket.ser) ? client_start(&ca) : p->socket.ser;
+
+			if (!p->socket.ser)
 				return -1;
 
-			p->fd = ca.accept_fd;
-
-			if (ast_pthread_create_background(&ca.master, NULL, sip_tcp_helper_thread, p->ser)) {
+			p->socket.fd = ca.accept_fd;
+
+			if (ast_pthread_create_background(&ca.master, NULL, sip_tcp_helper_thread, p->socket.ser)) {
 				ast_debug(1, "Unable to launch '%s'.", ca.name);
 				close(ca.accept_fd);
-				p->fd = ca.accept_fd = -1;
+				p->socket.fd = ca.accept_fd = -1;
 				return -1;
 			}
 		}
@@ -3408,6 +3438,9 @@
 		update_call_counter(p, DEC_CALL_LIMIT);
 		ast_debug(2, "This call did not properly clean up call limits. Call ID %s\n", p->callid);
 	}
+
+	if (p->socket.ser)
+		ast_free(p->socket.ser);
 
 	/* Remove link from peer to subscription of MWI */
 	if (p->relatedpeer && p->relatedpeer->mwipvt) 
@@ -4753,7 +4786,7 @@
 
 	ast_mutex_init(&p->pvt_lock);
 
-	p->ser = NULL;
+	p->socket.fd = -1;
 	p->method = intended_method;
 	p->initid = -1;
 	p->autokillid = -1;
@@ -4950,7 +4983,6 @@
 			/* Found the call */
 			sip_pvt_lock(p);
 			dialoglist_unlock();
-			p->transport = SIP_TRANSPORT_UDP;
 			return p;
 		}
 	}
@@ -4983,7 +5015,6 @@
 				ast_debug(4, "Failed allocating SIP dialog, sending 500 Server internal error and giving up\n");
 			}
 		}
-		p->transport = SIP_TRANSPORT_UDP;
 		return p;
 	} else if( sip_methods[intended_method].can_create == CAN_CREATE_DIALOG_UNSUPPORTED_METHOD) {
 		/* A method we do not support, let's take it on the volley */
@@ -4999,7 +5030,6 @@
 	if (intended_method == SIP_RESPONSE)
 		ast_debug(2, "That's odd...  Got a response on a call we dont know about. Callid %s\n", callid ? callid : "<unknown>");
 
-	p->transport = SIP_TRANSPORT_UDP;
 	return p;
 }
 
@@ -7304,8 +7334,8 @@
 static void build_contact(struct sip_pvt *p)
 {
 	/* Construct Contact: header */
-	if (ourport != STANDARD_SIP_PORT)
-		ast_string_field_build(p, our_contact, "<sip:%s%s%s:%d>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip), ourport);
+	if (!sip_standard_port(p->socket))
+		ast_string_field_build(p, our_contact, "<sip:%s%s%s:%d>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip), p->socket.port);
 	else
 		ast_string_field_build(p, our_contact, "<sip:%s%s%s>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip));
 }
@@ -7457,8 +7487,8 @@
 		l = tmp2;
 	}
 
-	if (ourport != STANDARD_SIP_PORT && ast_strlen_zero(p->fromdomain))
-		snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s:%d>;tag=%s", n, l, S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)), ourport, p->tag);
+	if (!sip_standard_port(p->socket) && ast_strlen_zero(p->fromdomain))
+		snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s:%d>;tag=%s", n, l, S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)), p->socket.port, p->tag);
 	else
 		snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%s", n, l, S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)), p->tag);
 
@@ -8005,34 +8035,6 @@
 			return 0;
 		}
 
-		p->transport = r->transport;
-
-		if(!(p->transport & SIP_TRANSPORT_UDP)) {
-			char name[256];
-			struct server_args ca;
-
-			snprintf(name, sizeof(name), "SIP Register to %s", p->peername);
-
-			ca.name = name;
-			ca.accept_fd = -1;
-			ca.tls_cfg = (p->ser) ? p->ser->parent->tls_cfg : NULL;
-			ca.sin = p->sa;
-			p->ser = (!p->ser) ? client_start(&ca) : p->ser;
-
-			if (!p->ser)
-				return -1;
-
-			p->fd = ca.accept_fd;
-
-			if (ast_pthread_create_background(&ca.master, NULL, sip_tcp_helper_thread, p->ser)) {
-				ast_debug(1, "Unable to launch '%s'.", ca.name);
-				close(ca.accept_fd);
-				p->fd = ca.accept_fd = -1;
-				return -1;
-			}
-		}
-
-
 		if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
 			append_history(p, "RegistryInit", "Account: %s@%s", r->username, r->hostname);
 
@@ -8093,6 +8095,8 @@
 		build_contact(p);
 	}
 
+	p->socket.type = r->transport;
+
 	/* set up a timeout */
 	if (auth == NULL)  {
 		if (r->timeout > -1) {
@@ -8189,6 +8193,7 @@
 	r->regattempts++;	/* Another attempt */
 	if (option_debug > 3)
 		ast_verbose("REGISTER attempt %d to %s@%s\n", r->regattempts, r->username, r->hostname);
+
 	return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
 }
 
@@ -15946,12 +15951,17 @@
 		req.data[res] = '\0';
 	req.len = res;
 
-	handle_request_do(&req, &sin, SIP_TRANSPORT_UDP, sipsock);
+	req.socket.fd 	= sipsock;
+	req.socket.type = SIP_TRANSPORT_UDP;
+	req.socket.ser	= NULL;
+	req.socket.port = ourport;
+
+	handle_request_do(&req, &sin);
 
 	return 1;
 }
 
-static int handle_request_do(struct sip_request *req, struct sockaddr_in *sin, int transport, int fd) {
+static int handle_request_do(struct sip_request *req, struct sockaddr_in *sin) {
 	struct sip_pvt *p;
 	int recount = 0;
 	int nounlock = 0;
@@ -15963,7 +15973,7 @@
 		req->len = lws2sws(req->data, req->len);	/* Fix multiline headers */
 	if (ast_test_flag(req, SIP_PKT_DEBUG))
 		ast_verbose("\n<--- SIP read from %s://%s:%d --->\n%s\n<------------->\n", 
-				   (transport & SIP_TRANSPORT_UDP) ? "udp" : (transport & SIP_TRANSPORT_TCP) ? "tcp" : "tls", ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), req->data);
+				    get_transport(req->socket.type), ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), req->data);
 
 	parse_request(req);
 	req->method = find_sip_method(req->rlPart1);
@@ -15986,8 +15996,7 @@
 			return 1;
 		}
 
-		p->transport = transport;
-		p->fd = fd;
+		p->socket = req->socket;
 
 		/* Go ahead and lock the owner if it has one -- we may need it */
 		/* becaues this is deadlock-prone, we need to try and unlock if failed */
@@ -16029,6 +16038,45 @@
 	ast_mutex_unlock(&netlock);
 
 	return 1;
+}
+
+static int sip_standard_port(struct sip_socket s) {
+	if (s.type & SIP_TRANSPORT_TLS)
+		return s.port == STANDARD_TLS_PORT;
+	else
+		return s.port == STANDARD_SIP_PORT;
+}
+
+static void sip_prepare_socket(struct sip_pvt *p) {
+	struct sip_socket *s = &p->socket;
+
+	if(s->fd != -1)
+		return;
+
+	if(!(s->type & SIP_TRANSPORT_UDP)) {
+		char name[] = "SIP socket";
+		struct server_args ca;
+
+		ca.name = name;
+		ca.accept_fd = -1;
+		ca.tls_cfg = (s->ser) ? s->ser->parent->tls_cfg : NULL;
+		ca.sin = *(sip_real_dst(p));
+		s->ser = (!s->ser) ? client_start(&ca) : s->ser;
+
+		if (!s->ser)
+			return;
+
+		s->fd = ca.accept_fd;
+
+		if (ast_pthread_create_background(&ca.master, NULL, sip_tcp_helper_thread, s->ser)) {
+			ast_log(LOG_DEBUG, "Unable to launch '%s'.", ca.name);
+			close(ca.accept_fd);
+			s->fd = ca.accept_fd = -1;
+			return;
+		}
+	} else {
+		s->fd = sipsock;
+	}
 }
 
 /*! \brief Send message waiting indication to alert peer that they've got voicemail */
@@ -16866,6 +16914,7 @@
 	user->maxcallbitrate = default_maxcallbitrate;
 	user->autoframing = global_autoframing;
 	user->prefs = default_prefs;
+	user->transport = SIP_TRANSPORT_UDP;
 	/* set default context */
 	strcpy(user->context, default_context);
 	strcpy(user->language, default_language);
@@ -16874,7 +16923,6 @@
 	for (; v; v = v->next) {
 		if (handle_common_options(&userflags[0], &mask[0], v))
 			continue;
-
 		if (!strcasecmp(v->name, "context")) {
 			ast_copy_string(user->context, v->value, sizeof(user->context));
 		} else if (!strcasecmp(v->name, "subscribecontext")) {
@@ -17029,7 +17077,7 @@
 	time_t regseconds = 0;
 	struct ast_flags peerflags[2] = {{(0)}};
 	struct ast_flags mask[2] = {{(0)}};
-	char callback[256] = "";
+	char callback[256] = "", *srvlookup = NULL;
 
 	if (!realtime)
 		/* Note we do NOT use find_peer here, to avoid realtime recursion */
@@ -17071,10 +17119,17 @@
 		peer->chanvars = NULL;
 		/* XXX should unregister ? */
 	}
+
 	for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
 		if (handle_common_options(&peerflags[0], &mask[0], v))
 			continue;
-		if (realtime && !strcasecmp(v->name, "regseconds")) {
+		if (!strcasecmp(v->name, "transport")) {
+			if (!strcasecmp(v->value, "tcp")) {
+				peer->transport = SIP_TRANSPORT_TCP;
+			} else if (!strcasecmp(v->value, "tls")) {
+				peer->transport = SIP_TRANSPORT_TLS;
+			}
+		} else if (realtime && !strcasecmp(v->name, "regseconds")) {
 			ast_get_time_t(v->value, &regseconds, 0, NULL);
 		} else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
 			inet_aton(v->value, &(peer->addr.sin_addr));
@@ -17140,14 +17195,7 @@
 					ast_sched_del(sched, peer->expire);
 				peer->expire = -1;
 				ast_clear_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
-				if (ast_get_ip_or_srv(&peer->addr, v->value, global_srvlookup ? "_sip._udp" : NULL)) {
-					unref_peer(peer);
-					return NULL;
-				}
-
-				ast_copy_string(peer->tohost, v->value, sizeof(peer->tohost));
-				if (!peer->addr.sin_port)
-					peer->addr.sin_port = htons(STANDARD_SIP_PORT);
+				srvlookup = v->value;
 			}
 		} else if (!strcasecmp(v->name, "defaultip")) {
 			if (ast_get_ip(&peer->defaddr, v->value)) {
@@ -17252,6 +17300,23 @@
 				peer->maxcallbitrate = default_maxcallbitrate;
 		}
 	}
+
+	if (srvlookup) {
+		if (ast_get_ip_or_srv(&peer->addr, srvlookup, 
+								global_srvlookup ?  
+									((peer->transport & SIP_TRANSPORT_UDP) ? "_sip._udp" :
+									 (peer->transport & SIP_TRANSPORT_TCP) ? "_sip._tcp" :
+									 "_sip._tls")
+									: NULL)) {
+			unref_peer(peer);
+			return NULL;
+		}
+
+		ast_copy_string(peer->tohost, srvlookup, sizeof(peer->tohost));
+		if (!peer->addr.sin_port)
+			peer->addr.sin_port = htons(STANDARD_SIP_PORT);
+	}
+
 	if (!ast_test_flag(&global_flags[1], SIP_PAGE2_IGNOREREGEXPIRE) && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && realtime) {
 		time_t nowtime = time(NULL);
 
@@ -17362,7 +17427,9 @@
 	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;
+	ourport = 
+	ourport_tcp = STANDARD_SIP_PORT;
+	ourport_tls = STANDARD_TLS_PORT;
 	global_srvlookup = DEFAULT_SRVLOOKUP;
 	global_tos_sip = DEFAULT_TOS_SIP;
 	global_tos_audio = DEFAULT_TOS_AUDIO;
@@ -17483,8 +17550,8 @@
 				ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of %s\n", v->name, v->value, v->lineno, config);
 			}
 		} else if (!strcasecmp(v->name, "tcpbindport")) {
-			if (sscanf(v->value, "%d", &ourport) == 1) {
-				sip_tcp_desc.sin.sin_port = htons(ourport);
+			if (sscanf(v->value, "%d", &ourport_tcp) == 1) {
+				sip_tcp_desc.sin.sin_port = htons(ourport_tcp);
 			} else {
 				ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
 			}
@@ -17504,8 +17571,8 @@
 				ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of %s\n", v->name, v->value, v->lineno, config);
 			}
 		} else if (!strcasecmp(v->name,"tlsbindport")) {
-			if (sscanf(v->value, "%d", &ourport) == 1) {
-				sip_tls_desc.sin.sin_port = htons(ourport);
+			if (sscanf(v->value, "%d", &ourport_tls) == 1) {
+				sip_tls_desc.sin.sin_port = htons(ourport_tls);
 			} else {
 				ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
 			}

Modified: team/bbryant/sip-tcptls/main/server.c
URL: http://svn.digium.com/view/asterisk/team/bbryant/sip-tcptls/main/server.c?view=diff&rev=72934&r1=72933&r2=72934
==============================================================================
--- team/bbryant/sip-tcptls/main/server.c (original)
+++ team/bbryant/sip-tcptls/main/server.c Mon Jul  2 15:18:34 2007
@@ -156,8 +156,9 @@
 /*! A generic client routine for a TCP client
  *  and starts a thread for handling accept()
  */
-struct server_instance *client_start(struct server_args *desc) {
-	int x = 1, flags;
+struct server_instance *client_start(struct server_args *desc)
+{
+	int flags;
 	struct server_instance *ser;
 
 	/* Do nothing if nothing has changed */
@@ -172,14 +173,13 @@
 	if (desc->accept_fd != -1)
 		close(desc->accept_fd);
 
-	desc->accept_fd = socket(AF_INET, SOCK_STREAM, 0);
+	desc->accept_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 	if (desc->accept_fd < 0) {
 		ast_log(LOG_WARNING, "Unable to allocate socket for %s: %s\n",
 			desc->name, strerror(errno));
 		return NULL;
 	}
 
-	setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
 	if (connect(desc->accept_fd, (const struct sockaddr *)&desc->sin, sizeof(desc->sin))) {
 		ast_log(LOG_NOTICE, "Unable to connect %s to %s:%d: %s\n",
 			desc->name,
@@ -187,28 +187,19 @@
 			strerror(errno));
 		goto error;
 	}
-	if (desc->accept_fd < 0) {
-		if ((errno != EAGAIN) && (errno != EINTR))
-			ast_log(LOG_WARNING, "Connect failed: %s\n", strerror(errno));
-		goto error;
-	}
-	ser = ast_calloc(1, sizeof(*ser));
-	if (!ser) {
-		ast_log(LOG_WARNING, "No memory for new session: %s\n", strerror(errno));
-		goto error;
-	}
+
+	if (!(ser = ast_calloc(1, sizeof(*ser))))
+		goto error;
 
 	flags = fcntl(desc->accept_fd, F_GETFL);
 	fcntl(desc->accept_fd, F_SETFL, flags & ~O_NONBLOCK);
+
 	ser->fd = desc->accept_fd;
 	ser->parent = desc;
+	ser->parent->worker_fn = NULL;
 	memcpy(&ser->requestor, &desc->sin, sizeof(ser->requestor));
 
-	ser->parent->worker_fn = NULL;
-
 	ast_make_file_from_fd(ser);
-
-	
 
 	return ser;
 




More information about the asterisk-commits mailing list