Browse code

pua_dialoginfo: use helper functions for operations with str inside build_dialoginfo()

Daniel-Constantin Mierla authored on 31/03/2021 07:33:35
Showing 1 changed files
... ...
@@ -53,6 +53,37 @@ void print_publ(publ_info_t* p)
53 53
 	LM_DBG("expires= %d\n", p->expires);
54 54
 }
55 55
 
56
+static xmlNodePtr puadi_xmlNewChildStr(xmlNodePtr node, xmlNsPtr ns, char *name,
57
+		str *sval)
58
+{
59
+	char buf[MAX_URI_SIZE+1];
60
+
61
+	if (sval->len > MAX_URI_SIZE) {
62
+		LM_ERR("value '%.*s' too long for '%s' - maximum=%d\n", sval->len,
63
+				sval->s, name, MAX_URI_SIZE);
64
+		return NULL;
65
+	}
66
+	memcpy(buf, sval->s, sval->len);
67
+	buf[sval->len]= '\0';
68
+
69
+	return xmlNewChild(node, ns, BAD_CAST name, BAD_CAST buf) ;
70
+}
71
+
72
+static xmlAttrPtr puadi_xmlNewPropStr(xmlNodePtr node, char *name, str *sval)
73
+{
74
+	char buf[MAX_URI_SIZE+1];
75
+
76
+	if (sval->len > MAX_URI_SIZE) {
77
+		LM_ERR("value '%.*s' too long for '%s' - maximum=%d\n", sval->len,
78
+				sval->s, name, MAX_URI_SIZE);
79
+		return NULL;
80
+	}
81
+	memcpy(buf, sval->s, sval->len);
82
+	buf[sval->len]= '\0';
83
+
84
+	return xmlNewProp(node, BAD_CAST name, BAD_CAST buf);
85
+}
86
+
56 87
 str* build_dialoginfo(char *state, str *entity, str *peer, str *callid,
57 88
 		unsigned int initiator, str *localtag, str *remotetag,
58 89
 		str *localtarget, str *remotetarget)
... ...
@@ -65,24 +96,17 @@ str* build_dialoginfo(char *state, str *entity, str *peer, str *callid,
65 96
 	xmlNodePtr local_node = NULL;
66 97
 	xmlNodePtr tag_node = NULL;
67 98
 	str *body= NULL;
68
-	char buf[MAX_URI_SIZE+1];
69
-
70
-	if (entity->len > MAX_URI_SIZE) {
71
-		LM_ERR("entity URI '%.*s' too long, maximum=%d\n",entity->len,
72
-				entity->s, MAX_URI_SIZE);
73
-		return NULL;
74
-	}
75
-	memcpy(buf, entity->s, entity->len);
76
-	buf[entity->len]= '\0';
77 99
 
78 100
 	/* create the Publish body */
79 101
 	doc = xmlNewDoc(BAD_CAST "1.0");
80
-	if(doc==0)
102
+	if(doc==0) {
81 103
 		return NULL;
104
+	}
82 105
 
83 106
 	root_node = xmlNewNode(NULL, BAD_CAST "dialog-info");
84
-	if(root_node==0)
107
+	if(root_node==0) {
85 108
 		goto error;
109
+	}
86 110
 
87 111
 	xmlDocSetRootElement(doc, root_node);
88 112
 
... ...
@@ -94,8 +118,9 @@ str* build_dialoginfo(char *state, str *entity, str *peer, str *callid,
94 118
 			BAD_CAST "0");
95 119
 	xmlNewProp(root_node, BAD_CAST  "state",
96 120
 			BAD_CAST "full" );
97
-	xmlNewProp(root_node, BAD_CAST "entity",
98
-			BAD_CAST buf);
121
+	if(puadi_xmlNewPropStr(root_node, "entity", entity)==NULL) {
122
+		goto error;
123
+	}
99 124
 
100 125
 	/* RFC 3245 differs between id and call-id. For example if a call
101 126
 	 * is forked and 2 early dialogs are established, we should send 2
... ...
@@ -108,44 +133,30 @@ str* build_dialoginfo(char *state, str *entity, str *peer, str *callid,
108 133
 
109 134
 	/* dialog tag */
110 135
 	dialog_node =xmlNewChild(root_node, NULL, BAD_CAST "dialog", NULL) ;
111
-	if( dialog_node ==NULL)
112
-	{
136
+	if(dialog_node == NULL) {
113 137
 		LM_ERR("while adding child\n");
114 138
 		goto error;
115 139
 	}
116 140
 
117
-	if (callid->len > MAX_URI_SIZE) {
118
-		LM_ERR("call-id '%.*s' too long, maximum=%d\n", callid->len,
119
-				callid->s, MAX_URI_SIZE);
141
+	if(puadi_xmlNewPropStr(dialog_node, "id", callid)==NULL) {
120 142
 		goto error;
121 143
 	}
122
-	memcpy(buf, callid->s, callid->len);
123
-	buf[callid->len]= '\0';
124 144
 
125
-	xmlNewProp(dialog_node, BAD_CAST "id", BAD_CAST buf);
126 145
 	if (include_callid) {
127
-		xmlNewProp(dialog_node, BAD_CAST "call-id", BAD_CAST buf);
146
+		if(puadi_xmlNewPropStr(dialog_node, "call-id", callid)==NULL) {
147
+			goto error;
148
+		}
128 149
 	}
129 150
 	if (include_tags) {
130 151
 		if (localtag && localtag->s) {
131
-			if (localtag->len > MAX_URI_SIZE) {
132
-				LM_ERR("localtag '%.*s' too long, maximum=%d\n",
133
-						localtag->len, localtag->s, MAX_URI_SIZE);
152
+			if(puadi_xmlNewPropStr(dialog_node, "local-tag", localtag)==NULL) {
134 153
 				goto error;
135 154
 			}
136
-			memcpy(buf, localtag->s, localtag->len);
137
-			buf[localtag->len]= '\0';
138
-			xmlNewProp(dialog_node, BAD_CAST "local-tag", BAD_CAST buf);
139 155
 		}
140 156
 		if (remotetag && remotetag->s) {
141
-			if (remotetag->len > MAX_URI_SIZE) {
142
-				LM_ERR("remotetag '%.*s' too long, maximum=%d\n",
143
-						remotetag->len, remotetag->s, MAX_URI_SIZE);
157
+			if(puadi_xmlNewPropStr(dialog_node, "remote-tag", remotetag)==NULL) {
144 158
 				goto error;
145 159
 			}
146
-			memcpy(buf, remotetag->s, remotetag->len);
147
-			buf[remotetag->len]= '\0';
148
-			xmlNewProp(dialog_node, BAD_CAST "remote-tag", BAD_CAST buf);
149 160
 		}
150 161
 	}
151 162
 
... ...
@@ -158,8 +169,7 @@ str* build_dialoginfo(char *state, str *entity, str *peer, str *callid,
158 169
 	/* state tag */
159 170
 	state_node = xmlNewChild(dialog_node, NULL, BAD_CAST "state",
160 171
 			BAD_CAST state) ;
161
-	if( state_node ==NULL)
162
-	{
172
+	if(state_node == NULL) {
163 173
 		LM_ERR("while adding child\n");
164 174
 		goto error;
165 175
 	}
... ...
@@ -167,73 +177,49 @@ str* build_dialoginfo(char *state, str *entity, str *peer, str *callid,
167 177
 	if (include_localremote) {
168 178
 		/* remote tag*/
169 179
 		remote_node = xmlNewChild(dialog_node, NULL, BAD_CAST "remote", NULL) ;
170
-		if( remote_node ==NULL)
171
-		{
180
+		if( remote_node == NULL) {
172 181
 			LM_ERR("while adding child\n");
173 182
 			goto error;
174 183
 		}
175 184
 
176
-		if (peer->len > MAX_URI_SIZE) {
177
-			LM_ERR("peer '%.*s' too long, maximum=%d\n", peer->len, peer->s,
178
-					MAX_URI_SIZE);
179
-			goto error;
180
-		}
181
-		memcpy(buf, peer->s, peer->len);
182
-		buf[peer->len]= '\0';
183
-
184
-		tag_node = xmlNewChild(remote_node, NULL, BAD_CAST "identity",
185
-				BAD_CAST buf) ;
186
-		if( tag_node ==NULL)
187
-		{
185
+		tag_node = puadi_xmlNewChildStr(remote_node, NULL, "identity", peer) ;
186
+		if(tag_node ==NULL) {
188 187
 			LM_ERR("while adding child\n");
189 188
 			goto error;
190 189
 		}
191 190
 		tag_node = xmlNewChild(remote_node, NULL, BAD_CAST "target", NULL);
192
-		if( tag_node ==NULL)
193
-		{
191
+		if(tag_node == NULL) {
194 192
 			LM_ERR("while adding child\n");
195 193
 			goto error;
196 194
 		}
197 195
 		if (remotetarget && remotetarget->s) {
198
-			memcpy(buf, remotetarget->s, remotetarget->len);
199
-			buf[remotetarget->len]= '\0';
196
+			puadi_xmlNewPropStr(tag_node, "uri", remotetarget);
197
+		} else {
198
+			puadi_xmlNewPropStr(tag_node, "uri", peer);
200 199
 		}
201
-		xmlNewProp(tag_node, BAD_CAST "uri", BAD_CAST buf);
202 200
 
203 201
 		/* local tag*/
204 202
 		local_node = xmlNewChild(dialog_node, NULL, BAD_CAST "local", NULL);
205
-		if( local_node ==NULL)
206
-		{
203
+		if(local_node == NULL) {
207 204
 			LM_ERR("while adding child\n");
208 205
 			goto error;
209 206
 		}
210 207
 
211
-		if (entity->len > MAX_URI_SIZE) {
212
-			LM_ERR("entity '%.*s' too long, maximum=%d\n",
213
-					entity->len, entity->s, MAX_URI_SIZE);
214
-			goto error;
215
-		}
216
-		memcpy(buf, entity->s, entity->len);
217
-		buf[entity->len]= '\0';
218
-
219
-		tag_node = xmlNewChild(local_node, NULL, BAD_CAST "identity",
220
-				BAD_CAST buf) ;
221
-		if( tag_node ==NULL)
222
-		{
208
+		tag_node = puadi_xmlNewChildStr(local_node, NULL, "identity", entity) ;
209
+		if(tag_node == NULL) {
223 210
 			LM_ERR("while adding child\n");
224 211
 			goto error;
225 212
 		}
226 213
 		tag_node = xmlNewChild(local_node, NULL, BAD_CAST "target", NULL);
227
-		if( tag_node ==NULL)
228
-		{
214
+		if(tag_node ==NULL) {
229 215
 			LM_ERR("while adding child\n");
230 216
 			goto error;
231 217
 		}
232 218
 		if (localtarget && localtarget->s) {
233
-			memcpy(buf, localtarget->s, localtarget->len);
234
-			buf[localtarget->len]= '\0';
219
+			puadi_xmlNewPropStr(tag_node, "uri", localtarget);
220
+		} else {
221
+			puadi_xmlNewPropStr(tag_node, "uri", entity);
235 222
 		}
236
-		xmlNewProp(tag_node, BAD_CAST "uri", BAD_CAST buf);
237 223
 	}
238 224
 
239 225
 	/* create the body */
... ...
@@ -261,10 +247,10 @@ str* build_dialoginfo(char *state, str *entity, str *peer, str *callid,
261 247
 	return body;
262 248
 
263 249
 error:
264
-	if(body)
265
-	{
266
-		if(body->s)
250
+	if(body) {
251
+		if(body->s) {
267 252
 			xmlFree(body->s);
253
+		}
268 254
 		pkg_free(body);
269 255
 	}
270 256
 	if(doc) {