core: take sipmsg_find_to_header() into use
[siplcs.git] / src / core / sipe-sign.c
blobbdd8c48c375beeec1bbfa19709764b53525fdd8c
1 /*
2 * @file sipe-sign.c
4 * pidgin-sipe
6 * Copyright (C) 2011-2019 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2008 Novell, Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
24 #include <string.h>
26 #include <glib.h>
28 #include "sipmsg.h"
29 #include "sipe-backend.h"
30 #include "sipe-sign.h"
31 #include "sipe-utils.h"
33 static gchar * const empty_string = "";
35 void sipmsg_breakdown_parse(struct sipmsg_breakdown * msg, gchar * realm, gchar * target,
36 const gchar *protocol)
38 const gchar * hdr;
39 if (msg == NULL || msg->msg == NULL) {
40 SIPE_DEBUG_INFO_NOFORMAT("sipmsg_breakdown_parse msg or msg->msg is NULL");
41 return;
44 msg->rand = msg->num = msg->realm = msg->target_name =
45 msg->cseq = msg->from_url = msg->from_tag = msg->to_url = msg->to_tag =
46 msg->p_assertet_identity_sip_uri = msg->p_assertet_identity_tel_uri = empty_string;
47 msg->call_id = msg->expires = empty_string;
49 if ((hdr = sipmsg_find_header(msg->msg, "Proxy-Authorization")) ||
50 (hdr = sipmsg_find_header(msg->msg, "Proxy-Authentication-Info")) ||
51 (hdr = sipmsg_find_header(msg->msg, "Authentication-Info")) ) {
52 msg->protocol = sipmsg_find_part_of_header(hdr, NULL, " ", empty_string);
53 msg->rand = sipmsg_find_part_of_header(hdr, "rand=\"", "\"", empty_string);
54 msg->num = sipmsg_find_part_of_header(hdr, "num=\"", "\"", empty_string);
55 msg->realm = sipmsg_find_part_of_header(hdr, "realm=\"", "\"", empty_string);
56 msg->target_name = sipmsg_find_part_of_header(hdr, "targetname=\"", "\"", empty_string);
57 } else {
58 msg->protocol = g_strdup(protocol);
59 msg->realm = g_strdup(realm);
60 msg->target_name = g_strdup(target);
63 msg->call_id = sipmsg_find_call_id_header(msg->msg);
65 hdr = sipmsg_find_cseq_header(msg->msg);
66 if (NULL != hdr) {
67 msg->cseq = sipmsg_find_part_of_header(hdr, NULL, " ", empty_string);
70 hdr = sipmsg_find_from_header(msg->msg);
71 if (NULL != hdr) {
72 gchar *address = parse_from(hdr);
73 if (address)
74 msg->from_url = address;
75 msg->from_tag = sipmsg_find_part_of_header(hdr, ";tag=", ";", empty_string);
78 hdr = sipmsg_find_to_header(msg->msg);
79 if (NULL != hdr) {
80 gchar *address = parse_from(hdr);
81 if (address)
82 msg->to_url = address;
83 msg->to_tag = sipmsg_find_part_of_header(hdr, ";tag=", ";", empty_string);
86 hdr = sipmsg_find_header(msg->msg, "P-Asserted-Identity");
87 if (NULL == hdr) {
88 hdr = sipmsg_find_header(msg->msg, "P-Preferred-Identity");
90 if (NULL != hdr) {
91 gchar *sip_uri = NULL;
92 gchar *tel_uri = NULL;
94 sipmsg_parse_p_asserted_identity(hdr, &sip_uri, &tel_uri);
95 if (sip_uri)
96 msg->p_assertet_identity_sip_uri = sip_uri;
97 if (tel_uri)
98 msg->p_assertet_identity_tel_uri = tel_uri;
101 msg->expires = sipmsg_find_expires_header(msg->msg);
104 void
105 sipmsg_breakdown_free(struct sipmsg_breakdown * msg)
107 if (msg->protocol != empty_string)
108 g_free(msg->protocol);
109 if (msg->rand != empty_string)
110 g_free(msg->rand);
111 if (msg->num != empty_string)
112 g_free(msg->num);
113 if (msg->realm != empty_string)
114 g_free(msg->realm);
115 if (msg->target_name != empty_string)
116 g_free(msg->target_name);
118 // straight from header
119 //g_free(msg->call_id);
121 if (msg->cseq != empty_string)
122 g_free(msg->cseq);
123 if (msg->from_url != empty_string)
124 g_free(msg->from_url);
125 if (msg->from_tag != empty_string)
126 g_free(msg->from_tag);
127 if (msg->to_url != empty_string)
128 g_free(msg->to_url);
129 if (msg->to_tag != empty_string)
130 g_free(msg->to_tag);
132 if (msg->p_assertet_identity_sip_uri != empty_string)
133 g_free(msg->p_assertet_identity_sip_uri);
134 if (msg->p_assertet_identity_tel_uri != empty_string)
135 g_free(msg->p_assertet_identity_tel_uri);
137 // straight from header
138 //g_free (msg->expires);
141 gchar *
142 sipmsg_breakdown_get_string(int version,
143 struct sipmsg_breakdown * msgbd)
145 gchar *response_str;
146 gchar *msg;
147 if (msgbd->realm == empty_string || msgbd->realm == NULL) {
148 SIPE_DEBUG_INFO_NOFORMAT("realm NULL, so returning NULL signature string");
149 return NULL;
152 response_str = msgbd->msg->response != 0 ? g_strdup_printf("<%d>", msgbd->msg->response) : empty_string;
153 if (version < 3) {
154 msg = g_strdup_printf(
155 "<%s><%s><%s><%s><%s><%s><%s><%s><%s><%s><%s>" // 1 - 11
156 "<%s>%s", // 12 - 13
157 msgbd->protocol, msgbd->rand, msgbd->num, msgbd->realm, msgbd->target_name, msgbd->call_id, msgbd->cseq,
158 msgbd->msg->method, msgbd->from_url, msgbd->from_tag, msgbd->to_tag,
159 msgbd->expires ? msgbd->expires : empty_string, response_str
161 } else {
162 msg = g_strdup_printf(
163 "<%s><%s><%s><%s><%s><%s><%s><%s><%s><%s><%s><%s><%s><%s>" // 1 - 14
164 "<%s>%s", // 15 - 16
165 msgbd->protocol, msgbd->rand, msgbd->num, msgbd->realm, msgbd->target_name, msgbd->call_id, msgbd->cseq,
166 msgbd->msg->method, msgbd->from_url, msgbd->from_tag, msgbd->to_url, msgbd->to_tag,
167 msgbd->p_assertet_identity_sip_uri, msgbd->p_assertet_identity_tel_uri,
168 msgbd->expires ? msgbd->expires : empty_string, response_str
172 if (response_str != empty_string) {
173 g_free(response_str);
176 return msg;
180 Local Variables:
181 mode: c
182 c-file-style: "bsd"
183 indent-tabs-mode: t
184 tab-width: 8
185 End: