[svn-commits] kmoore: trunk r370636 - in /trunk/channels: ./ sip/ sip/include/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Jul 31 14:10:50 CDT 2012


Author: kmoore
Date: Tue Jul 31 14:10:41 2012
New Revision: 370636

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=370636
Log:
Clean up chan_sip

This clean up was broken out from
https://reviewboard.asterisk.org/r/1976/ and addresses the following:
 - struct sip_refer converted to use the stringfields API.
 - sip_{refer|notify}_allocate -> sip_{notify|refer}_alloc to match
   other *alloc functions.
 - Replace get_msg_text, get_msg_text2 and get_pidf_body -> No, not
   get_pidf_msg_text_body3 but get_content, to match add_content.
 - get_body doesn't get the request body, renamed to get_content_line.
 - get_body_by_line doesn't get the body line, and is just a simple if
   test. Moved code inline and removed function.
 - Remove camelCase in struct sip_peer peer state variables,
   onHold -> onhold, inUse -> inuse, inRinging -> ringing.
 - Remove camelCase in struct sip_request rlPart1 -> rlpart1,
   rlPart2 -> rlpart2.
 - Rename instances of pvt->randdata to pvt->nonce because that is what
   it is, no need to update struct sip_pvt because _it already has a
   nonce field_.
 - Removed struct sip_pvt randdata stringfield.
 - Remove useless (and inconsistent) 'header' suffix on variables in
   handle_request_subscribe.
 - Use ast_strdupa on Event header in handle_request_subscribe to avoid
   overly complicated strncmp calls to find the event package.
 - Move get_destination check in handle_request_subscribe to avoid
   duplicate checking for packages that don't need it.
 - Move extension state callback management in handle_request_subscribe
   to avoid duplicate checking for packages that don't need it.
 - Remove duplicate append_date prototype.
 - Rename append_date -> add_date to match other add_xxx functions.
 - Added add_expires helper function, removed code that manually added
   expires header.
 - Remove _header suffix on add_diversion_header (no other header adding
   functions have this).
 - Don't pass req->debug to request handle_request_XXXXX handlers if req
   is also being passed.
 - Don't pass req->ignore to check_auth as req is already being passed.
 - Don't create a subscription in handle_request_subscribe if
   p->expiry == 0.
 - Don't walk of the back of referred_by_name when splitting string in
   get_refer_info
 - Remove duplicate check for no dialog in handle_incoming when
   sipmethod == SIP_REFER, handle_request_refer checks for that.

Review: https://reviewboard.asterisk.org/r/1993/
Patch-by: gareth

Modified:
    trunk/channels/chan_sip.c
    trunk/channels/sip/include/sip.h
    trunk/channels/sip/security_events.c

Modified: trunk/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/chan_sip.c?view=diff&rev=370636&r1=370635&r2=370636
==============================================================================
--- trunk/channels/chan_sip.c (original)
+++ trunk/channels/chan_sip.c Tue Jul 31 14:10:41 2012
@@ -1397,7 +1397,7 @@
 static int build_reply_digest(struct sip_pvt *p, int method, char *digest, int digest_len);
 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
 					 const char *secret, const char *md5secret, int sipmethod,
-					 const char *uri, enum xmittype reliable, int ignore);
+					 const char *uri, enum xmittype reliable);
 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
 					      int sipmethod, const char *uri, enum xmittype reliable,
 					      struct ast_sockaddr *addr, struct sip_peer **authpeer);
@@ -1415,7 +1415,7 @@
 /*--- Misc functions */
 static int check_rtp_timeout(struct sip_pvt *dialog, time_t t);
 static int reload_config(enum channelreloadreason reason);
-static void add_diversion_header(struct sip_request *req, struct sip_pvt *pvt);
+static void add_diversion(struct sip_request *req, struct sip_pvt *pvt);
 static int expire_register(const void *data);
 static void *do_monitor(void *data);
 static int restart_monitor(void);
@@ -1424,8 +1424,8 @@
 static int dialog_find_multiple(void *obj, void *arg, int flags);
 static struct ast_channel *sip_pvt_lock_full(struct sip_pvt *pvt);
 /* static int sip_addrcmp(char *name, struct sockaddr_in *sin);	Support for peer matching */
-static int sip_refer_allocate(struct sip_pvt *p);
-static int sip_notify_allocate(struct sip_pvt *p);
+static int sip_refer_alloc(struct sip_pvt *p);
+static int sip_notify_alloc(struct sip_pvt *p);
 static void ast_quiet_chan(struct ast_channel *chan);
 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target);
 static int do_magic_pickup(struct ast_channel *channel, const char *extension, const char *context);
@@ -1475,7 +1475,7 @@
 static char *complete_sip_show_history(const char *line, const char *word, int pos, int state);
 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state);
 static char *complete_sip_unregister(const char *line, const char *word, int pos, int state);
-static char *complete_sipnotify(const char *line, const char *word, int pos, int state);
+static char *complete_sip_notify(const char *line, const char *word, int pos, int state);
 static char *sip_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 static char *sip_show_channelstats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 static char *sip_show_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
@@ -1542,7 +1542,6 @@
 static int sip_reinvite_retry(const void *data);
 
 /*--- Parsing SIP requests and responses */
-static void append_date(struct sip_request *req);	/* Append date to SIP packet */
 static int determine_firstline_parts(struct sip_request *req);
 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize);
@@ -1567,12 +1566,12 @@
 static int get_rpid(struct sip_pvt *p, struct sip_request *oreq);
 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq, char **name, char **number, int *reason);
 static enum sip_get_dest_result get_destination(struct sip_pvt *p, struct sip_request *oreq, int *cc_recall_core_id);
-static int get_msg_text(char *buf, int len, struct sip_request *req);
 static int transmit_state_notify(struct sip_pvt *p, struct state_notify_data *data, int full, int timeout);
 static void update_connectedline(struct sip_pvt *p, const void *data, size_t datalen);
 static void update_redirecting(struct sip_pvt *p, const void *data, size_t datalen);
 static int get_domain(const char *str, char *domain, int len);
 static void get_realm(struct sip_pvt *p, const struct sip_request *req);
+static char *get_content(struct sip_request *req);
 
 /*-- TCP connection handling ---*/
 static void *_sip_tcp_helper_thread(struct ast_tcptls_session_instance *tcptls_session);
@@ -1597,7 +1596,7 @@
 static void build_callid_registry(struct sip_registry *reg, const struct ast_sockaddr *ourip, const char *fromdomain);
 static void make_our_tag(struct sip_pvt *pvt);
 static int add_header(struct sip_request *req, const char *var, const char *value);
-static int add_header_max_forwards(struct sip_pvt *dialog, struct sip_request *req);
+static int add_max_forwards(struct sip_pvt *dialog, struct sip_request *req);
 static int add_content(struct sip_request *req, const char *line);
 static int finalize_content(struct sip_request *req);
 static void destroy_msg_headers(struct sip_pvt *pvt);
@@ -1610,14 +1609,15 @@
 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field);
 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field);
 static void set_destination(struct sip_pvt *p, char *uri);
-static void append_date(struct sip_request *req);
+static void add_date(struct sip_request *req);
+static void add_expires(struct sip_request *req, int expires);
 static void build_contact(struct sip_pvt *p);
 
 /*------Request handling functions */
 static int handle_incoming(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int *recount, int *nounlock);
 static int handle_request_update(struct sip_pvt *p, struct sip_request *req);
-static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, uint32_t seqno, struct ast_sockaddr *addr, int *recount, const char *e, int *nounlock);
-static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, uint32_t seqno, int *nounlock);
+static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, uint32_t seqno, int *recount, const char *e, int *nounlock);
+static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, uint32_t seqno, int *nounlock);
 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req);
 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *sin, const char *e);
 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req);
@@ -1625,7 +1625,7 @@
 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, uint32_t seqno, const char *e);
 static void handle_request_info(struct sip_pvt *p, struct sip_request *req);
 static int handle_request_options(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, const char *e);
-static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, uint32_t seqno, struct ast_sockaddr *addr, int *nounlock);
+static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, uint32_t seqno, int *nounlock);
 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, uint32_t seqno, const char *e);
 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, uint32_t seqno, int *nounlock);
 
@@ -4404,10 +4404,10 @@
 			req->data->str);
 	}
 	if (p->do_history) {
-		struct sip_request tmp = { .rlPart1 = 0, };
+		struct sip_request tmp = { .rlpart1 = 0, };
 		parse_copy(&tmp, req);
 		append_history(p, reliable ? "TxRespRel" : "TxResp", "%s / %s - %s", tmp.data->str, sip_get_header(&tmp, "CSeq"),
-			(tmp.method == SIP_RESPONSE || tmp.method == SIP_UNKNOWN) ? REQ_OFFSET_TO_STR(&tmp, rlPart2) : sip_methods[tmp.method].text);
+			(tmp.method == SIP_RESPONSE || tmp.method == SIP_UNKNOWN) ? REQ_OFFSET_TO_STR(&tmp, rlpart2) : sip_methods[tmp.method].text);
 		deinit_req(&tmp);
 	}
 
@@ -4452,7 +4452,7 @@
 		}
 	}
 	if (p->do_history) {
-		struct sip_request tmp = { .rlPart1 = 0, };
+		struct sip_request tmp = { .rlpart1 = 0, };
 		parse_copy(&tmp, req);
 		append_history(p, reliable ? "TxReqRel" : "TxReq", "%s / %s - %s", tmp.data->str, sip_get_header(&tmp, "CSeq"), sip_methods[tmp.method].text);
 		deinit_req(&tmp);
@@ -6136,6 +6136,7 @@
 		if (p->refer->refer_call) {
 			p->refer->refer_call = dialog_unref(p->refer->refer_call, "unref dialog p->refer->refer_call");
 		}
+		ast_string_field_free_memory(p->refer);
 		ast_free(p->refer);
 	}
 	if (p->route) {
@@ -6238,7 +6239,7 @@
 static int update_call_counter(struct sip_pvt *fup, int event)
 {
 	char name[256];
-	int *inuse = NULL, *call_limit = NULL, *inringing = NULL;
+	int *inuse = NULL, *call_limit = NULL, *ringing = NULL;
 	int outgoing = fup->outgoing_call;
 	struct sip_peer *p = NULL;
 
@@ -6255,9 +6256,9 @@
 	/* Check the list of devices */
 	if (fup->relatedpeer) {
 		p = sip_ref_peer(fup->relatedpeer, "ref related peer for update_call_counter");
-		inuse = &p->inUse;
+		inuse = &p->inuse;
 		call_limit = &p->call_limit;
-		inringing = &p->inRinging;
+		ringing = &p->ringing;
 		ast_copy_string(name, fup->peername, sizeof(name));
 	}
 	if (!p) {
@@ -6266,7 +6267,7 @@
 	}
 
 	switch(event) {
-	/* incoming and outgoing affects the inUse counter */
+	/* incoming and outgoing affects the inuse counter */
 	case DEC_CALL_LIMIT:
 		/* Decrement inuse count if applicable */
 		if (inuse) {
@@ -6285,16 +6286,16 @@
 		}
 
 		/* Decrement ringing count if applicable */
-		if (inringing) {
+		if (ringing) {
 			sip_pvt_lock(fup);
 			ao2_lock(p);
-			if (*inringing > 0) {
+			if (*ringing > 0) {
 				if (ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
-					(*inringing)--;
+					(*ringing)--;
 					ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
 				}
 			} else {
-			   *inringing = 0;
+			   *ringing = 0;
 			}
 			ao2_unlock(p);
 			sip_pvt_unlock(fup);
@@ -6326,11 +6327,11 @@
 				return -1;
 			}
 		}
-		if (inringing && (event == INC_CALL_RINGING)) {
+		if (ringing && (event == INC_CALL_RINGING)) {
 			sip_pvt_lock(fup);
 			ao2_lock(p);
 			if (!ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
-				(*inringing)++;
+				(*ringing)++;
 				ast_set_flag(&fup->flags[0], SIP_INC_RINGING);
 			}
 			ao2_unlock(p);
@@ -6352,12 +6353,12 @@
 		break;
 
 	case DEC_CALL_RINGING:
-		if (inringing) {
+		if (ringing) {
 			sip_pvt_lock(fup);
 			ao2_lock(p);
 			if (ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
-				if (*inringing > 0) {
-					(*inringing)--;
+				if (*ringing > 0) {
+					(*ringing)--;
 				}
 				ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
 			}
@@ -7647,16 +7648,6 @@
 	return tmp;
 }
 
-/*! \brief Reads one line of SIP message body */
-static char *get_body_by_line(const char *line, const char *name, int nameLen, char delimiter)
-{
-	if (!strncasecmp(line, name, nameLen) && line[nameLen] == delimiter) {
-		return ast_skip_blanks(line + nameLen + 1);
-	}
-
-	return "";
-}
-
 /*! \brief Lookup 'name' in the SDP starting
  * at the 'start' line. Returns the matching line, and 'start'
  * is updated with the next line number.
@@ -7664,11 +7655,13 @@
 static const char *get_sdp_iterate(int *start, struct sip_request *req, const char *name)
 {
 	int len = strlen(name);
+	const char *line;
 
 	while (*start < (req->sdp_start + req->sdp_count)) {
-		const char *r = get_body_by_line(REQ_OFFSET_TO_STR(req, line[(*start)++]), name, len, '=');
-		if (r[0] != '\0')
-			return r;
+		line = REQ_OFFSET_TO_STR(req, line[(*start)++]);
+		if (!strncasecmp(line, name, len) && line[len] == '=') {
+			return ast_skip_blanks(line + len + 1);
+		}
 	}
 
 	/* if the line was not found, ensure that *start points past the SDP */
@@ -7703,17 +7696,17 @@
 	return type;
 }
 
-/*! \brief Get a specific line from the message body */
-static char *get_body(struct sip_request *req, char *name, char delimiter)
-{
-	int x;
+/*! \brief Get a specific line from the message content */
+static char *get_content_line(struct sip_request *req, char *name, char delimiter)
+{
+	int i;
 	int len = strlen(name);
-	char *r;
-
-	for (x = 0; x < req->lines; x++) {
-		r = get_body_by_line(REQ_OFFSET_TO_STR(req, line[x]), name, len, delimiter);
-		if (r[0] != '\0') {
-			return r;
+	const char *line;
+
+	for (i = 0; i < req->lines; i++) {
+		line = REQ_OFFSET_TO_STR(req, line[i]);
+		if (!strncasecmp(line, name, len) && line[len] == delimiter) {
+			return ast_skip_blanks(line + len + 1);
 		}
 	}
 
@@ -7821,6 +7814,30 @@
 {
 	int start = 0;
 	return __get_header(req, name, &start);
+}
+
+
+AST_THREADSTORAGE(sip_content_buf);
+
+/*! \brief Get message body content */
+static char *get_content(struct sip_request *req)
+{
+	struct ast_str *str;
+	int i;
+
+	if (!(str = ast_str_thread_get(&sip_content_buf, 128))) {
+		return NULL;
+	}
+
+	ast_str_reset(str);
+
+	for (i = 0; i < req->lines; i++) {
+		if (ast_str_append(&str, 0, "%s\n", REQ_OFFSET_TO_STR(req, line[i])) < 0) {
+			return NULL;
+		}
+	}
+
+	return ast_str_buffer(str);
 }
 
 /*! \brief Read RTP from network */
@@ -8353,7 +8370,7 @@
 {
 	const char *init_ruri = NULL;
 	if (sip_pvt_ptr->initreq.headers) {
-		init_ruri = REQ_OFFSET_TO_STR(&sip_pvt_ptr->initreq, rlPart2);
+		init_ruri = REQ_OFFSET_TO_STR(&sip_pvt_ptr->initreq, rlpart2);
 	}
 
 	/*
@@ -8687,7 +8704,7 @@
 		args.fromtag = fromtag;
 		args.seqno = seqno;
 		/* get via header information. */
-		args.ruri = REQ_OFFSET_TO_STR(req, rlPart2);
+		args.ruri = REQ_OFFSET_TO_STR(req, rlpart2);
 		via = parse_via(sip_get_header(req, "Via"));
 		if (via) {
 			args.viasentby = via->sent_by;
@@ -8700,7 +8717,7 @@
 		}
 		/* if it is a response, get the response code */
 		if (req->method == SIP_RESPONSE) {
-			const char* e = ast_skip_blanks(REQ_OFFSET_TO_STR(req, rlPart2));
+			const char* e = ast_skip_blanks(REQ_OFFSET_TO_STR(req, rlpart2));
 			int respid;
 			if (!ast_strlen_zero(e) && (sscanf(e, "%30d", &respid) == 1)) {
 				args.respid = respid;
@@ -8764,12 +8781,8 @@
 			logger_callid = ast_create_callid();
 		}
 
-		if (intended_method == SIP_REFER) {
-			/* We do support REFER, but not outside of a dialog yet */
-			transmit_response_using_temp(callid, addr, 1, intended_method, req, "603 Declined (no dialog)");
-
 		/* Ok, time to create a new SIP dialog object, a pvt */
-		} else if (!(p = sip_alloc(callid, addr, 1, intended_method, req, logger_callid)))  {
+		if (!(p = sip_alloc(callid, addr, 1, intended_method, req, logger_callid)))  {
 			/* We have a memory or file/socket error (can't allocate RTP sockets or something) so we're not
 				getting a dialog from sip_alloc.
 
@@ -10708,7 +10721,7 @@
 /*! \brief Add "Supported" header to sip message.  Since some options may
  *  be disabled in the config, the sip_pvt must be inspected to determine what
  *  is supported for this dialog. */
-static int add_supported_header(struct sip_pvt *pvt, struct sip_request *req)
+static int add_supported(struct sip_pvt *pvt, struct sip_request *req)
 {
 	int res;
 	if (st_get_mode(pvt, 0) != SESSION_TIMER_MODE_REFUSE) {
@@ -10748,7 +10761,7 @@
  * \pre dialog is assumed to be locked while calling this function
  * \brief Add 'Max-Forwards' header to SIP message 
  */
-static int add_header_max_forwards(struct sip_pvt *dialog, struct sip_request *req)
+static int add_max_forwards(struct sip_pvt *dialog, struct sip_request *req)
 {
 	char clen[10];
 
@@ -11154,7 +11167,7 @@
 	if (!ast_strlen_zero(global_useragent))
 		add_header(resp, "Server", global_useragent);
 	add_header(resp, "Allow", ALLOWED_METHODS);
-	add_supported_header(p, resp);
+	add_supported(p, resp);
 
 	/* If this is an invite, add Session-Timers related headers if the feature is active for this session */
 	if (p->method == SIP_INVITE && p->stimer && p->stimer->st_active == TRUE && p->stimer->st_active_peer_ua == TRUE) {
@@ -11167,10 +11180,7 @@
 	if (msg[0] == '2' && (p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER || p->method == SIP_PUBLISH)) {
 		/* For registration responses, we also need expiry and
 		   contact info */
-		char tmp[256];
-
-		snprintf(tmp, sizeof(tmp), "%d", p->expiry);
-		add_header(resp, "Expires", tmp);
+		add_expires(resp, p->expiry);
 		if (p->expiry) {	/* Only add contact if we have an expiry time */
 			char contact[SIPBUFSIZE];
 			const char *contact_uri = p->method == SIP_SUBSCRIBE ? p->our_contact : p->fullcontact;
@@ -11244,14 +11254,14 @@
 	}
 
 	if (sipmethod == SIP_CANCEL)
-		c = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);	/* Use original URI */
+		c = REQ_OFFSET_TO_STR(&p->initreq, rlpart2);	/* Use original URI */
 	else if (sipmethod == SIP_ACK) {
 		/* Use URI from Contact: in 200 OK (if INVITE)
 		(we only have the contacturi on INVITEs) */
 		if (!ast_strlen_zero(p->okcontacturi))
 			c = is_strict ? p->route->hop : p->okcontacturi;
 		else
-			c = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);
+			c = REQ_OFFSET_TO_STR(&p->initreq, rlpart2);
 	} else if (!ast_strlen_zero(p->okcontacturi))
 		c = is_strict ? p->route->hop : p->okcontacturi; /* Use for BYE or REINVITE */
 	else if (!ast_strlen_zero(p->uri))
@@ -11280,7 +11290,7 @@
 		set_destination(p, p->route->hop);
 		add_route(req, is_strict ? p->route->next : p->route);
 	}
-	add_header_max_forwards(p, req);
+	add_max_forwards(p, req);
 
 	ot = sip_get_header(orig, "To");
 	of = sip_get_header(orig, "From");
@@ -11365,7 +11375,7 @@
 
 	/* If we are sending a 302 Redirect we can add a diversion header if the redirect information is set */
 	if (!strncmp(msg, "302", 3)) {
-		add_diversion_header(&resp, p);
+		add_diversion(&resp, p);
 	}
 
 	/* If we are cancelling an incoming invite for some reason, add information
@@ -11495,7 +11505,7 @@
 {
 	struct sip_request resp;
 	respprep(&resp, p, msg, req);
-	append_date(&resp);
+	add_date(&resp);
 	add_header(&resp, "Unsupported", unsupported);
 	return send_response(p, &resp, XMIT_UNRELIABLE, 0);
 }
@@ -11507,7 +11517,7 @@
 	char minse_str[20];
 
 	respprep(&resp, p, msg, req);
-	append_date(&resp);
+	add_date(&resp);
 
 	snprintf(minse_str, sizeof(minse_str), "%d", minse_int);
 	add_header(&resp, "Min-SE", minse_str);
@@ -11523,16 +11533,25 @@
 	return __transmit_response(p, msg, req, req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL);
 }
 
-/*! \brief Append date to SIP message */
-static void append_date(struct sip_request *req)
-{
-	char tmpdat[256];
+/*! \brief Add date header to SIP message */
+static void add_date(struct sip_request *req)
+{
+	char tmp[256];
 	struct tm tm;
 	time_t t = time(NULL);
 
 	gmtime_r(&t, &tm);
-	strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T GMT", &tm);
-	add_header(req, "Date", tmpdat);
+	strftime(tmp, sizeof(tmp), "%a, %d %b %Y %T GMT", &tm);
+	add_header(req, "Date", tmp);
+}
+
+/*! \brief Add Expires header to SIP message */
+static void add_expires(struct sip_request *req, int expires)
+{
+	char tmp[32];
+
+	snprintf(tmp, sizeof(tmp), "%d", expires);
+	add_header(req, "Expires", tmp);
 }
 
 /*! \brief Append Retry-After header field when transmitting response */
@@ -11544,12 +11563,12 @@
 	return send_response(p, &resp, XMIT_UNRELIABLE, 0);
 }
 
-/*! \brief Append date and content length before transmitting response */
+/*! \brief Add date before transmitting response */
 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req)
 {
 	struct sip_request resp;
 	respprep(&resp, p, msg, req);
-	append_date(&resp);
+	add_date(&resp);
 	return send_response(p, &resp, XMIT_UNRELIABLE, 0);
 }
 
@@ -11575,7 +11594,7 @@
 }
 
 /*! \brief Respond with authorization request */
-static int transmit_response_with_auth(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *randdata, enum xmittype reliable, const char *header, int stale)
+static int transmit_response_with_auth(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *nonce, enum xmittype reliable, const char *header, int stale)
 {
 	struct sip_request resp;
 	char tmp[512];
@@ -11590,7 +11609,7 @@
 
 	/* Stale means that they sent us correct authentication, but
 	   based it on an old challenge (nonce) */
-	snprintf(tmp, sizeof(tmp), "Digest algorithm=MD5, realm=\"%s\", nonce=\"%s\"%s", p->realm, randdata, stale ? ", stale=true" : "");
+	snprintf(tmp, sizeof(tmp), "Digest algorithm=MD5, realm=\"%s\", nonce=\"%s\"%s", p->realm, nonce, stale ? ", stale=true" : "");
 	respprep(&resp, p, msg, req);
 	add_header(&resp, header, tmp);
 	append_history(p, "AuthChal", "Auth challenge sent for %s - nc %d", p->username, p->noncecount);
@@ -12878,12 +12897,12 @@
 static int determine_firstline_parts(struct sip_request *req)
 {
 	char *e = ast_skip_blanks(req->data->str);	/* there shouldn't be any */
-	char *local_rlPart1;
+	char *local_rlpart1;
 
 	if (!*e)
 		return -1;
-	req->rlPart1 = e - req->data->str;	/* method or protocol */
-	local_rlPart1 = e;
+	req->rlpart1 = e - req->data->str;	/* method or protocol */
+	local_rlpart1 = e;
 	e = ast_skip_nonblanks(e);
 	if (*e)
 		*e++ = '\0';
@@ -12893,10 +12912,10 @@
 		return -1;
 	ast_trim_blanks(e);
 
-	if (!strcasecmp(local_rlPart1, "SIP/2.0") ) { /* We have a response */
+	if (!strcasecmp(local_rlpart1, "SIP/2.0") ) { /* We have a response */
 		if (strlen(e) < 3)	/* status code is 3 digits */
 			return -1;
-		req->rlPart2 = e - req->data->str;
+		req->rlpart2 = e - req->data->str;
 	} else { /* We have a request */
 		if ( *e == '<' ) { /* XXX the spec says it must not be in <> ! */
 			ast_debug(3, "Oops. Bogus uri in <> %s\n", e);
@@ -12904,7 +12923,7 @@
 			if (!*e)
 				return -1;
 		}
-		req->rlPart2 = e - req->data->str;	/* URI */
+		req->rlpart2 = e - req->data->str;	/* URI */
 		e = ast_skip_nonblanks(e);
 		if (*e)
 			*e++ = '\0';
@@ -12937,7 +12956,7 @@
 	reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ?  SIP_UPDATE : SIP_INVITE, 0, 1);
 
 	add_header(&req, "Allow", ALLOWED_METHODS);
-	add_supported_header(p, &req);
+	add_supported(p, &req);
 	if (sipdebug) {
 		if (oldsdp == TRUE)
 			add_header(&req, "X-asterisk-Info", "SIP re-invite (Session-Timers)");
@@ -13182,7 +13201,7 @@
 	snprintf(tmp_n, sizeof(tmp_n), "%u %s", ++p->ocseq, sip_methods[sipmethod].text);
 
 	add_header(req, "Via", p->via);
-	add_header_max_forwards(p, req);
+	add_max_forwards(p, req);
 	/* This will be a no-op most of the time. However, under certain circumstances,
 	 * NOTIFY messages will use this function for preparing the request and should
 	 * have Route headers present.
@@ -13209,7 +13228,7 @@
  * \param req The request/response to which we will add the header
  * \param pvt The sip_pvt which represents the call-leg
  */
-static void add_diversion_header(struct sip_request *req, struct sip_pvt *pvt)
+static void add_diversion(struct sip_request *req, struct sip_pvt *pvt)
 {
 	const char *diverting_number;
 	const char *diverting_name;
@@ -13313,20 +13332,21 @@
 	if (p->options && p->options->auth) {
 		add_header(&req, p->options->authheader, p->options->auth);
 	}
-	append_date(&req);
-	if (sipmethod == SIP_REFER) {	/* Call transfer */
-		if (p->refer) {
-			char buf[SIPBUFSIZE];
-			if (!ast_strlen_zero(p->refer->refer_to)) {
-				add_header(&req, "Refer-To", p->refer->refer_to);
-			}
-			if (!ast_strlen_zero(p->refer->referred_by)) {
-				snprintf(buf, sizeof(buf), "%s <%s>", p->refer->referred_by_name, p->refer->referred_by);
-				add_header(&req, "Referred-By", buf);
-			}
+	add_date(&req);
+	if (sipmethod == SIP_REFER && p->refer) {	/* Call transfer */
+		char buf[SIPBUFSIZE];
+
+		if (!ast_strlen_zero(p->refer->refer_to)) {
+			add_header(&req, "Refer-To", p->refer->refer_to);
+		}
+		if (!ast_strlen_zero(p->refer->referred_by)) {
+			snprintf(buf, sizeof(buf), "%s%s%s",
+				p->refer->referred_by_name,
+				!ast_strlen_zero(p->refer->referred_by_name) ? " " : "",
+				p->refer->referred_by);
+			add_header(&req, "Referred-By", buf);
 		}
 	} else if (sipmethod == SIP_SUBSCRIBE) {
-		char buf[SIPBUFSIZE];
 		if (p->subscribed == MWI_NOTIFICATION) {
 			add_header(&req, "Event", "message-summary");
 			add_header(&req, "Accept", "application/simple-message-summary");
@@ -13334,8 +13354,7 @@
 			add_header(&req, "Event", "call-completion");
 			add_header(&req, "Accept", "application/call-completion");
 		}
-		snprintf(buf, sizeof(buf), "%d", p->expiry);
-		add_header(&req, "Expires", buf);
+		add_expires(&req, p->expiry);
 	}
 
 	/* This new INVITE is part of an attended transfer. Make sure that the
@@ -13362,7 +13381,7 @@
 	}
 
 	add_header(&req, "Allow", ALLOWED_METHODS);
-	add_supported_header(p, &req);
+	add_supported(p, &req);
 
 	if (p->options && p->options->addsipheaders && p->owner) {
 		struct ast_channel *chan = p->owner; /* The owner channel */
@@ -13410,7 +13429,7 @@
 	if ((sipmethod == SIP_INVITE || sipmethod == SIP_UPDATE) && ast_test_flag(&p->flags[0], SIP_SENDRPID))
 		add_rpid(&req, p);
 	if (sipmethod == SIP_INVITE) {
-		add_diversion_header(&req, p);
+		add_diversion(&req, p);
 	}
 	if (sdp) {
 		offered_media_list_destroy(p);
@@ -13421,7 +13440,7 @@
 			try_suggested_sip_codec(p);
 			add_sdp(&req, p, FALSE, TRUE, FALSE);
 		}
-	} else if (p->notify) {
+	} else if (sipmethod == SIP_NOTIFY && p->notify) {
 		for (var = p->notify->headers; var; var = var->next) {
 			add_header(&req, var->name, var->value);
 		}
@@ -13429,13 +13448,10 @@
 			add_content(&req, ast_str_buffer(p->notify->content));
 		}
 	} else if (sipmethod == SIP_PUBLISH) {
-		char expires[SIPBUFSIZE];
-
 		switch (p->epa_entry->static_data->event) {
 		case CALL_COMPLETION:
-			snprintf(expires, sizeof(expires), "%d", p->expiry);
 			add_header(&req, "Event", "call-completion");
-			add_header(&req, "Expires", expires);
+			add_expires(&req, p->expiry);
 			if (p->epa_entry->publish_type != SIP_PUBLISH_INITIAL) {
 				add_header(&req, "SIP-If-Match", p->epa_entry->entity_tag);
 			}
@@ -13656,7 +13672,7 @@
 	enum state { NOTIFY_OPEN, NOTIFY_INUSE, NOTIFY_CLOSED } local_state = NOTIFY_OPEN;
 	const char *statestring = "terminated";
 	const char *pidfstate = "--";
-	const char *pidfnote= "Ready";
+	const char *pidfnote ="Ready";
 	char hint[AST_MAX_EXTENSION];
 
 	switch (data->state) {
@@ -14057,7 +14073,7 @@
 	add_header(&req, "Subscription-state", terminate ? "terminated;reason=noresource" : "active");
 	add_header(&req, "Content-Type", "message/sipfrag;version=2.0");
 	add_header(&req, "Allow", ALLOWED_METHODS);
-	add_supported_header(p, &req);
+	add_supported(p, &req);
 
 	snprintf(tmp, sizeof(tmp), "SIP/2.0 %s\r\n", message);
 	add_content(&req, tmp);
@@ -14101,7 +14117,7 @@
 
 	/* Notify is outgoing call */
 	ast_set_flag(&p->flags[0], SIP_OUTGOING);
-	sip_notify_allocate(p);
+	sip_notify_alloc(p);
 
 	p->notify->headers = header = ast_variable_new("Subscription-State", "terminated", "");
 
@@ -14138,7 +14154,7 @@
 	}
 
 	respprep(&resp, p, "181 Call is being forwarded", &p->initreq);
-	add_diversion_header(&resp, p);
+	add_diversion(&resp, p);
 	send_response(p, &resp, XMIT_UNRELIABLE, 0);
 }
 
@@ -14166,7 +14182,7 @@
 			reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
 
 			add_header(&req, "Allow", ALLOWED_METHODS);
-			add_supported_header(p, &req);
+			add_supported(p, &req);
 			add_rpid(&req, p);
 			add_sdp(&req, p, FALSE, TRUE, FALSE);
 
@@ -14549,7 +14565,7 @@
 
 	build_via(p);
 	add_header(&req, "Via", p->via);
-	add_header_max_forwards(p, &req);
+	add_max_forwards(p, &req);
 	add_header(&req, "From", from);
 	add_header(&req, "To", to);
 	add_header(&req, "Call-ID", p->callid);
@@ -14585,8 +14601,7 @@
 		}
 	}
 
-	snprintf(tmp, sizeof(tmp), "%d", r->expiry);
-	add_header(&req, "Expires", tmp);
+	add_expires(&req, r->expiry);
 	add_header(&req, "Contact", p->our_contact);
 
 	initialize_initreq(p, &req);
@@ -14624,14 +14639,14 @@
 }
 
 /*! \brief Allocate SIP refer structure */
-static int sip_refer_allocate(struct sip_pvt *p)
-{
-	p->refer = ast_calloc(1, sizeof(struct sip_refer));
+static int sip_refer_alloc(struct sip_pvt *p)
+{
+	p->refer = ast_calloc_with_stringfields(1, struct sip_refer, 512);
 	return p->refer ? 1 : 0;
 }
 
 /*! \brief Allocate SIP refer structure */
-static int sip_notify_allocate(struct sip_pvt *p)
+static int sip_notify_alloc(struct sip_pvt *p)
 {
 	p->notify = ast_calloc(1, sizeof(struct sip_notify));
 	if (p->notify) {
@@ -14691,16 +14706,16 @@
 	}
 
 	/* save in case we get 407 challenge */
-	sip_refer_allocate(p);
-	ast_copy_string(p->refer->refer_to, referto, sizeof(p->refer->refer_to));
-	ast_copy_string(p->refer->referred_by, p->our_contact, sizeof(p->refer->referred_by));
+	sip_refer_alloc(p);
+	ast_string_field_set(p->refer, refer_to, referto);
+	ast_string_field_set(p->refer, referred_by, p->our_contact);
 	p->refer->status = REFER_SENT;   /* Set refer status */
 
 	reqprep(&req, p, SIP_REFER, 0, 1);
 
 	add_header(&req, "Refer-To", referto);
 	add_header(&req, "Allow", ALLOWED_METHODS);
-	add_supported_header(p, &req);
+	add_supported(p, &req);
 	if (!ast_strlen_zero(p->our_contact)) {
 		add_header(&req, "Referred-By", p->our_contact);
 	}
@@ -15490,16 +15505,16 @@
 	}
 }
 
-/*! \brief builds the sip_pvt's randdata field which is used for the nonce
+/*! \brief builds the sip_pvt's nonce field which is used for the authentication 
  *  challenge.  When forceupdate is not set, the nonce is only updated if
  *  the current one is stale.  In this case, a stalenonce is one which
  *  has already received a response, if a nonce has not received a response
  *  it is not always necessary or beneficial to create a new one. */
 
-static void set_nonce_randdata(struct sip_pvt *p, int forceupdate)
-{
-	if (p->stalenonce || forceupdate || ast_strlen_zero(p->randdata)) {
-		ast_string_field_build(p, randdata, "%08lx", ast_random());	/* Create nonce for challenge */
+static void build_nonce(struct sip_pvt *p, int forceupdate)
+{
+	if (p->stalenonce || forceupdate || ast_strlen_zero(p->nonce)) {
+		ast_string_field_build(p, nonce, "%08lx", ast_random());	/* Create nonce for challenge */
 		p->stalenonce = 0;
 	}
 }
@@ -15539,7 +15554,7 @@
 */
 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
 					 const char *secret, const char *md5secret, int sipmethod,
-					 const char *uri, enum xmittype reliable, int ignore)
+					 const char *uri, enum xmittype reliable)
 {
 	const char *response;
 	char *reqheader, *respheader;
@@ -15549,7 +15564,7 @@
 	char *c;
 	int  wrongnonce = FALSE;
 	int  good_response;
-	const char *usednonce = p->randdata;
+	const char *usednonce = p->nonce;
 	struct ast_str *buf;
 	int res;
 
@@ -15577,22 +15592,22 @@
 	 */
 	sip_auth_headers(WWW_AUTH, &respheader, &reqheader);
 
-	authtoken =  sip_get_header(req, reqheader);	
-	if (ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
+	authtoken = sip_get_header(req, reqheader);
+	if (req->ignore && !ast_strlen_zero(p->nonce) && ast_strlen_zero(authtoken)) {
 		/* This is a retransmitted invite/register/etc, don't reconstruct authentication
 		   information */
 		if (!reliable) {
 			/* Resend message if this was NOT a reliable delivery.   Otherwise the
 			   retransmission should get it */
-			transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
+			transmit_response_with_auth(p, response, req, p->nonce, reliable, respheader, 0);
 			/* Schedule auto destroy in 32 seconds (according to RFC 3261) */
 			sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
 		}
 		return AUTH_CHALLENGE_SENT;
-	} else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
+	} else if (ast_strlen_zero(p->nonce) || ast_strlen_zero(authtoken)) {
 		/* We have no auth, so issue challenge and request authentication */
-		set_nonce_randdata(p, 1); /* Create nonce for challenge */
-		transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
+		build_nonce(p, 1); /* Create nonce for challenge */
+		transmit_response_with_auth(p, response, req, p->nonce, reliable, respheader, 0);
 		/* Schedule auto destroy in 32 seconds */
 		sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
 		return AUTH_CHALLENGE_SENT;
@@ -15628,7 +15643,7 @@
 
 	/* Verify nonce from request matches our nonce, and the nonce has not already been responded to.
 	 * If this check fails, send 401 with new nonce */
-	if (strcasecmp(p->randdata, keys[K_NONCE].s) || p->stalenonce) { /* XXX it was 'n'casecmp ? */
+	if (strcasecmp(p->nonce, keys[K_NONCE].s) || p->stalenonce) { /* XXX it was 'n'casecmp ? */
 		wrongnonce = TRUE;
 		usednonce = keys[K_NONCE].s;
 	} else {
@@ -15664,21 +15679,21 @@
 			if (sipdebug)
 				ast_log(LOG_NOTICE, "Correct auth, but based on stale nonce received from '%s'\n", sip_get_header(req, "From"));
 			/* We got working auth token, based on stale nonce . */
-			set_nonce_randdata(p, 0);
-			transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, TRUE);
+			build_nonce(p, 0);
+			transmit_response_with_auth(p, response, req, p->nonce, reliable, respheader, TRUE);
 		} else {
 			/* Everything was wrong, so give the device one more try with a new challenge */
 			if (!req->ignore) {
 				if (sipdebug) {
 					ast_log(LOG_NOTICE, "Bad authentication received from '%s'\n", sip_get_header(req, "To"));
 				}
-				set_nonce_randdata(p, 1);
+				build_nonce(p, 1);
 			} else {
 				if (sipdebug) {
 					ast_log(LOG_NOTICE, "Duplicate authentication received from '%s'\n", sip_get_header(req, "To"));
 				}
 			}
-			transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
+			transmit_response_with_auth(p, response, req, p->nonce, reliable, respheader, FALSE);
 		}
 
 		/* Schedule auto destroy in 32 seconds */
@@ -15706,7 +15721,7 @@
 	}
 
 	/* If they put someone on hold, increment the value... otherwise decrement it */
-	ast_atomic_fetchadd_int(&p->relatedpeer->onHold, (hold ? +1 : -1));
+	ast_atomic_fetchadd_int(&p->relatedpeer->onhold, (hold ? +1 : -1));
 
 	/* Request device state update */
 	ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", p->relatedpeer->name);
@@ -15866,17 +15881,17 @@
 		respheader = "WWW-Authenticate";
 	}
 	authtoken = sip_get_header(req, reqheader);
-	if (req->ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
+	if (req->ignore && !ast_strlen_zero(p->nonce) && ast_strlen_zero(authtoken)) {
 		/* This is a retransmitted invite/register/etc, don't reconstruct authentication
 		 * information */
-		transmit_response_with_auth(p, response, req, p->randdata, 0, respheader, 0);
+		transmit_response_with_auth(p, response, req, p->nonce, 0, respheader, 0);
 		/* Schedule auto destroy in 32 seconds (according to RFC 3261) */
 		sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
 		return;
-	} else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
+	} else if (ast_strlen_zero(p->nonce) || ast_strlen_zero(authtoken)) {
 		/* We have no auth, so issue challenge and request authentication */
-		set_nonce_randdata(p, 1);
-		transmit_response_with_auth(p, response, req, p->randdata, 0, respheader, 0);
+		build_nonce(p, 1);
+		transmit_response_with_auth(p, response, req, p->nonce, 0, respheader, 0);
 		/* Schedule auto destroy in 32 seconds */
 		sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
 		return;
@@ -15918,11 +15933,11 @@
 	}
 
 	/* Verify nonce from request matches our nonce.  If not, send 401 with new nonce */
-	if (strcasecmp(p->randdata, keys[K_NONCE].s)) {
+	if (strcasecmp(p->nonce, keys[K_NONCE].s)) {
 		if (!req->ignore) {
-			set_nonce_randdata(p, 1);
-		}
-		transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
+			build_nonce(p, 1);
+		}
+		transmit_response_with_auth(p, response, req, p->nonce, reliable, respheader, FALSE);
 
 		/* Schedule auto destroy in 32 seconds */
 		sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
@@ -16097,7 +16112,7 @@
 			}
 
 			ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_NAT_FORCE_RPORT);
-			if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri2, XMIT_UNRELIABLE, req->ignore))) {
+			if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri2, XMIT_UNRELIABLE))) {
 				if (sip_cancel_destroy(p))
 					ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
 
@@ -16600,9 +16615,10 @@
 	}
 
 	/* Find the request URI */
-	if (req->rlPart2)
-		ast_copy_string(tmp, REQ_OFFSET_TO_STR(req, rlPart2), sizeof(tmp));
-	
+	if (req->rlpart2) {
+		ast_copy_string(tmp, REQ_OFFSET_TO_STR(req, rlpart2), sizeof(tmp));
+	}
+
 	uri = ast_strdupa(get_in_brackets(tmp));
 
 	if (parse_uri_legacy_check(uri, "sip:,sips:", &uri, &unused_password, &domain, NULL)) {
@@ -16832,7 +16848,6 @@
  */
 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req)
 {
-
 	const char *p_referred_by = NULL;
 	char *h_refer_to = NULL;
 	char *h_referred_by = NULL;
@@ -16842,11 +16857,10 @@
 	char *ptr;
 	struct sip_request *req = NULL;
 	const char *transfer_context = NULL;
-	struct sip_refer *referdata;
-
+	struct sip_refer *refer;
 
 	req = outgoing_req;
-	referdata = transferer->refer;
+	refer = transferer->refer;
 
 	if (!req) {

[... 1165 lines stripped ...]



More information about the svn-commits mailing list