Merged in default (pull request #697)

Tue, 19 May 2020 10:29:23 +0000

author
Gary Kramlich <grim@reaperworld.com>
date
Tue, 19 May 2020 10:29:23 +0000
changeset 40417
48c621e76141
parent 40413
3f7b41f7fae8 (current diff)
parent 40416
54c050e07348 (diff)
child 40421
0718877994fa

Merged in default (pull request #697)

simple: Fix search for message headers

Approved-by: Gary Kramlich

libpurple/protocols/simple/simple.c file | annotate | diff | comparison | revisions
--- a/libpurple/protocols/simple/simple.c	Tue May 19 10:14:43 2020 +0000
+++ b/libpurple/protocols/simple/simple.c	Tue May 19 10:29:23 2020 +0000
@@ -605,52 +605,33 @@
 	return len;
 }
 
-static void sendout_sipmsg(struct simple_account_data *sip, struct sipmsg *msg) {
-	GSList *tmp = msg->headers;
-	gchar *name;
-	gchar *value;
-	GString *outstr = g_string_new("");
-	g_string_append_printf(outstr, "%s %s SIP/2.0\r\n", msg->method, msg->target);
-	while(tmp) {
-		name = ((struct siphdrelement*) (tmp->data))->name;
-		value = ((struct siphdrelement*) (tmp->data))->value;
-		g_string_append_printf(outstr, "%s: %s\r\n", name, value);
-		tmp = g_slist_next(tmp);
-	}
-	g_string_append_printf(outstr, "\r\n%s", msg->body ? msg->body : "");
-	sendout_pkt(sip->gc, outstr->str);
-	g_string_free(outstr, TRUE);
-}
-
-static void send_sip_response(PurpleConnection *gc, struct sipmsg *msg, int code,
-		const char *text, const char *body) {
-	GSList *tmp = msg->headers;
-	gchar *name;
-	gchar *value;
-	GString *outstr = g_string_new("");
+static void
+send_sip_response(PurpleConnection *gc, struct sipmsg *msg, gint code,
+                  const gchar *text, const gchar *body)
+{
+	gchar *outstr;
 
 	/* When sending the acknowlegements and errors, the content length from the original
 	   message is still here, but there is no body; we need to make sure we're sending the
 	   correct content length */
 	sipmsg_remove_header(msg, "Content-Length");
+	g_clear_pointer(&msg->body, g_free);
 	if(body) {
 		gchar len[12];
-		sprintf(len, "%" G_GSIZE_FORMAT , strlen(body));
+		msg->bodylen = strlen(body);
+		sprintf(len, "%d", msg->bodylen);
 		sipmsg_add_header(msg, "Content-Length", len);
-	}
-	else
+		msg->body = g_strdup(body);
+	} else {
 		sipmsg_add_header(msg, "Content-Length", "0");
-	g_string_append_printf(outstr, "SIP/2.0 %d %s\r\n", code, text);
-	while(tmp) {
-		name = ((struct siphdrelement*) (tmp->data))->name;
-		value = ((struct siphdrelement*) (tmp->data))->value;
+		msg->bodylen = 0;
+	}
+	msg->response = code;
 
-		g_string_append_printf(outstr, "%s: %s\r\n", name, value);
-		tmp = g_slist_next(tmp);
-	}
-	g_string_append_printf(outstr, "\r\n%s", body ? body : "");
-	sendout_pkt(gc, outstr->str);
-	g_string_free(outstr, TRUE);
+	outstr = sipmsg_to_string(msg, text);
+	sendout_pkt(gc, outstr);
+
+	g_free(outstr);
 }
 
 static void
@@ -1027,8 +1008,11 @@
 			/* TODO 408 */
 		} else {
 			if((currtime - trans->time > 2) && trans->retries == 0) {
+				gchar *outstr;
 				trans->retries++;
-				sendout_sipmsg(sip, trans->msg);
+				outstr = sipmsg_to_string(trans->msg, NULL);
+				sendout_pkt(sip->gc, outstr);
+				g_free(outstr);
 			}
 		}
 	}
@@ -1595,7 +1579,7 @@
 				sipmsg_remove_header(trans->msg, "Proxy-Authorization");
 				sipmsg_add_header(trans->msg, "Proxy-Authorization", auth);
 				g_free(auth);
-				resend = sipmsg_to_string(trans->msg);
+				resend = sipmsg_to_string(trans->msg, NULL);
 				/* resend request */
 				sendout_pkt(sip->gc, resend);
 				g_free(resend);
@@ -1639,7 +1623,7 @@
 							sipmsg_remove_header(trans->msg, "Authorization");
 							sipmsg_add_header(trans->msg, "Authorization", auth);
 							g_free(auth);
-							resend = sipmsg_to_string(trans->msg);
+							resend = sipmsg_to_string(trans->msg, NULL);
 							/* resend request */
 							sendout_pkt(sip->gc, resend);
 							g_free(resend);
--- a/libpurple/protocols/simple/sipmsg.c	Tue May 19 10:14:43 2020 +0000
+++ b/libpurple/protocols/simple/sipmsg.c	Tue May 19 10:29:23 2020 +0000
@@ -134,35 +134,42 @@
 
 void sipmsg_print(const struct sipmsg *msg) {
 	GSList *cur;
-	struct siphdrelement *elem;
-	purple_debug(PURPLE_DEBUG_MISC, "simple", "SIP MSG\n");
-	purple_debug(PURPLE_DEBUG_MISC, "simple", "response: %d\nmethod: %s\nbodylen: %d\n",msg->response,msg->method,msg->bodylen);
-	if(msg->target) purple_debug(PURPLE_DEBUG_MISC, "simple", "target: %s\n",msg->target);
+	PurpleKeyValuePair *elem;
+	purple_debug_misc("simple", "SIP MSG");
+	purple_debug_misc("simple", "response: %d\nmethod: %s\nbodylen: %d",
+	                  msg->response, msg->method, msg->bodylen);
+	if (msg->target) {
+		purple_debug_misc("simple", "target: %s", msg->target);
+	}
 	cur = msg->headers;
 	while(cur) {
 		elem = cur->data;
-		purple_debug(PURPLE_DEBUG_MISC, "simple", "name: %s value: %s\n",elem->name, elem->value);
+		purple_debug_misc("simple", "name: %s value: %s", elem->key,
+		                  (gchar *)elem->value);
 		cur = g_slist_next(cur);
 	}
 }
 
-char *sipmsg_to_string(const struct sipmsg *msg) {
+gchar *
+sipmsg_to_string(const struct sipmsg *msg, const gchar *status_text)
+{
 	GSList *cur;
 	GString *outstr = g_string_new("");
-	struct siphdrelement *elem;
+	PurpleKeyValuePair *elem;
 
-	if(msg->response)
-		g_string_append_printf(outstr, "SIP/2.0 %d Unknown\r\n",
-			msg->response);
-	else
+	if (msg->response) {
+		g_string_append_printf(outstr, "SIP/2.0 %d %s\r\n", msg->response,
+		                       status_text ? status_text : "Unknown");
+	} else {
 		g_string_append_printf(outstr, "%s %s SIP/2.0\r\n",
 			msg->method, msg->target);
+	}
 
 	cur = msg->headers;
 	while(cur) {
 		elem = cur->data;
-		g_string_append_printf(outstr, "%s: %s\r\n", elem->name,
-			elem->value);
+		g_string_append_printf(outstr, "%s: %s\r\n", elem->key,
+		                       (gchar *)elem->value);
 		cur = g_slist_next(cur);
 	}
 
@@ -170,44 +177,49 @@
 
 	return g_string_free(outstr, FALSE);
 }
-void sipmsg_add_header(struct sipmsg *msg, const gchar *name, const gchar *value) {
-	struct siphdrelement *element = g_new(struct siphdrelement,1);
-	element->name = g_strdup(name);
-	element->value = g_strdup(value);
+
+void
+sipmsg_add_header(struct sipmsg *msg, const gchar *name, const gchar *value)
+{
+	PurpleKeyValuePair *element =
+	        purple_key_value_pair_new_full(name, g_strdup(value), g_free);
 	msg->headers = g_slist_append(msg->headers, element);
 }
 
-static void
-sipmsg_free_header(struct siphdrelement *elem)
-{
-	g_free(elem->name);
-	g_free(elem->value);
-	g_free(elem);
-}
-
 void sipmsg_free(struct sipmsg *msg) {
-	g_slist_free_full(msg->headers, (GDestroyNotify)sipmsg_free_header);
+	g_slist_free_full(msg->headers, (GDestroyNotify)purple_key_value_pair_free);
 	g_free(msg->method);
 	g_free(msg->target);
 	g_free(msg->body);
 	g_free(msg);
 }
 
-void sipmsg_remove_header(struct sipmsg *msg, const gchar *name) {
-	GSList *tmp = g_slist_find_custom(msg->headers, name, (GCompareFunc)g_ascii_strcasecmp);
-	if(tmp) {
-		struct siphdrelement *elem = tmp->data;
+static gint
+compare_header_names(gconstpointer a, gconstpointer b)
+{
+	const PurpleKeyValuePair *kvpa = a;
+	const gchar *name = b;
+	return g_ascii_strcasecmp(kvpa->key, name);
+}
+
+void
+sipmsg_remove_header(struct sipmsg *msg, const gchar *name)
+{
+	GSList *tmp = g_slist_find_custom(msg->headers, name, compare_header_names);
+	if (tmp) {
+		PurpleKeyValuePair *elem = tmp->data;
 		msg->headers = g_slist_delete_link(msg->headers, tmp);
-		sipmsg_free_header(elem);
+		purple_key_value_pair_free(elem);
 	}
 }
 
-const gchar *sipmsg_find_header(struct sipmsg *msg, const gchar *name) {
-	GSList *tmp = g_slist_find_custom(msg->headers, name, (GCompareFunc)g_ascii_strcasecmp);
-	if(tmp) {
-		struct siphdrelement *elem = tmp->data;
+const gchar *
+sipmsg_find_header(struct sipmsg *msg, const gchar *name)
+{
+	GSList *tmp = g_slist_find_custom(msg->headers, name, compare_header_names);
+	if (tmp) {
+		PurpleKeyValuePair *elem = tmp->data;
 		return elem->value;
 	}
 	return NULL;
 }
-
--- a/libpurple/protocols/simple/sipmsg.h	Tue May 19 10:14:43 2020 +0000
+++ b/libpurple/protocols/simple/sipmsg.h	Tue May 19 10:29:23 2020 +0000
@@ -32,11 +32,6 @@
 	gchar *body;
 };
 
-struct siphdrelement {
-	gchar *name;
-	gchar *value;
-};
-
 struct sipmsg *sipmsg_parse_msg(const gchar *msg);
 struct sipmsg *sipmsg_parse_header(const gchar *header);
 void sipmsg_add_header(struct sipmsg *msg, const gchar *name, const gchar *value);
@@ -44,6 +39,6 @@
 const gchar *sipmsg_find_header(struct sipmsg *msg, const gchar *name);
 void sipmsg_remove_header(struct sipmsg *msg, const gchar *name);
 void sipmsg_print(const struct sipmsg *msg);
-char *sipmsg_to_string(const struct sipmsg *msg);
+gchar *sipmsg_to_string(const struct sipmsg *msg, const gchar *status_text);
 
 #endif /* PURPLE_SIMPLE_SIPMSG_H */

mercurial