Browse code

core: added fields to message structure and branch structure to hold user-agent string retrieved from usrloc

Peter Dunkley authored on 17/05/2013 23:13:11
Showing 7 changed files
... ...
@@ -509,7 +509,7 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
509 509
 			ret=append_branch(msg, &a->val[0].u.str, &msg->dst_uri,
510 510
 					  &msg->path_vec, a->val[1].u.number,
511 511
 					  (flag_t)flags, msg->force_send_socket,
512
-					  0, 0, 0);
512
+					  0, 0, 0, 0);
513 513
 			/* if the uri is the ruri and q was also not changed, mark
514 514
 			   ruri as consumed, to avoid having an identical branch */
515 515
 			if ((a->val[0].u.str.s == 0 || a->val[0].u.str.len == 0) &&
... ...
@@ -77,6 +77,8 @@
77 77
 
78 78
 #define MAX_RUID_SIZE 65		/*!< Maximum length of ruid for location records */
79 79
 
80
+#define MAX_UA_SIZE 255			/*!< Maximum length of user-agent for location records */
81
+
80 82
 #define MY_VIA "Via: SIP/2.0/UDP "
81 83
 #define MY_VIA_LEN (sizeof(MY_VIA) - 1)
82 84
 
... ...
@@ -216,7 +216,7 @@ void set_branch_iterator(int n)
216 216
 char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
217 217
 		 str* path, unsigned int *flags,
218 218
 		 struct socket_info** force_socket,
219
-		 str *ruid, str *instance)
219
+		 str *ruid, str *instance, str *location_ua)
220 220
 {
221 221
 	if (i < nr_branches) {
222 222
 		*len = branches[i].len;
... ...
@@ -241,6 +241,11 @@ char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
241 241
 			instance->len = branches[i].instance_len;
242 242
 			instance->s = (instance->len)?branches[i].instance:0;
243 243
 		}
244
+		if (location_ua) {
245
+			location_ua->len = branches[i].location_ua_len;
246
+			location_ua->s
247
+				= (location_ua->len)?branches[i].location_ua:0;
248
+		}
244 249
 		return branches[i].uri;
245 250
 	} else {
246 251
 		*len = 0;
... ...
@@ -265,6 +270,10 @@ char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
265 265
 			instance->s = 0;
266 266
 			instance->len = 0;
267 267
 		}
268
+		if (location_ua) {
269
+			location_ua->s = 0;
270
+			location_ua->len = 0;
271
+		}
268 272
 		return 0;
269 273
 	}
270 274
 }
... ...
@@ -276,12 +285,12 @@ char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
276 276
  */
277 277
 char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
278 278
 		  unsigned int* flags, struct socket_info** force_socket,
279
-		  str* ruid, str *instance)
279
+		  str* ruid, str *instance, str *location_ua)
280 280
 {
281 281
 	char* ret;
282 282
 	
283 283
 	ret=get_branch(branch_iterator, len, q, dst_uri, path, flags,
284
-		       force_socket, ruid, instance);
284
+		       force_socket, ruid, instance, location_ua);
285 285
 	if (likely(ret))
286 286
 		branch_iterator++;
287 287
 	return ret;
... ...
@@ -316,7 +325,7 @@ int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
316 316
 		  qvalue_t q, unsigned int flags,
317 317
 		  struct socket_info* force_socket,
318 318
 		  str* instance, unsigned int reg_id,
319
-		  str* ruid)
319
+		  str* ruid, str* location_ua)
320 320
 {
321 321
 	str luri;
322 322
 
... ...
@@ -416,6 +425,21 @@ int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
416 416
 		branches[nr_branches].ruid_len = 0;
417 417
 	}
418 418
 
419
+	if (unlikely(location_ua && location_ua->len && location_ua->s)) {
420
+		if (unlikely(location_ua->len > MAX_UA_SIZE)) {
421
+			LOG(L_ERR, "too long location_ua: %.*s\n",
422
+			    location_ua->len, location_ua->s);
423
+			return -1;
424
+		}
425
+		memcpy(branches[nr_branches].location_ua, location_ua->s,
426
+		       location_ua->len);
427
+		branches[nr_branches].location_ua[location_ua->len] = 0;
428
+		branches[nr_branches].location_ua_len = location_ua->len;
429
+	} else {
430
+		branches[nr_branches].location_ua[0] = '\0';
431
+		branches[nr_branches].location_ua_len = 0;
432
+	}
433
+	
419 434
 	nr_branches++;
420 435
 	return 1;
421 436
 }
... ...
@@ -450,7 +474,7 @@ char* print_dset(struct sip_msg* msg, int* len)
450 450
 	crt_branch = get_branch_iterator();
451 451
 
452 452
 	init_branch_iterator();
453
-	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0))) {
453
+	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0, 0))) {
454 454
 		cnt++;
455 455
 		*len += uri.len;
456 456
 		if (q != Q_UNSPECIFIED) {
... ...
@@ -491,7 +515,7 @@ char* print_dset(struct sip_msg* msg, int* len)
491 491
 	}
492 492
 
493 493
 	init_branch_iterator();
494
-	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0))) {
494
+	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0, 0))) {
495 495
 		if (i) {
496 496
 			memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
497 497
 			p += CONTACT_DELIM_LEN;
... ...
@@ -70,6 +70,9 @@ struct branch
70 70
     char ruid[MAX_RUID_SIZE];
71 71
     unsigned int ruid_len;
72 72
 
73
+    char location_ua[MAX_UA_SIZE + 1];
74
+    unsigned int location_ua_len;
75
+
73 76
     /* Branch flags */
74 77
     flag_t flags;
75 78
 };
... ...
@@ -93,11 +96,11 @@ int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
93 93
 		  qvalue_t q, unsigned int flags,
94 94
 		  struct socket_info* force_socket,
95 95
 		  str* instance, unsigned int reg_id,
96
-		  str* ruid);
96
+		  str* ruid, str* location_ua);
97 97
 
98 98
 /*! \brief kamailio compatible version */
99 99
 #define km_append_branch(msg, uri, dst_uri, path, q, flags, force_socket) \
100
-    append_branch(msg, uri, dst_uri, path, q, flags, force_socket, 0, 0, 0)
100
+    append_branch(msg, uri, dst_uri, path, q, flags, force_socket, 0, 0, 0, 0)
101 101
 
102 102
 /*! \brief ser compatible append_branch version.
103 103
  *  append_branch version compatible with ser: no path or branch flags support
... ...
@@ -114,7 +117,7 @@ static inline int ser_append_branch(struct sip_msg* msg,
114 114
     s_uri.len=uri_len;
115 115
     s_dst_uri.s=dst_uri;
116 116
     s_dst_uri.len=dst_uri_len;
117
-    return append_branch(msg, &s_uri, &s_dst_uri, 0, q, 0, force_socket, 0, 0, 0);
117
+    return append_branch(msg, &s_uri, &s_dst_uri, 0, q, 0, force_socket, 0, 0, 0, 0);
118 118
 }
119 119
 
120 120
 
... ...
@@ -140,12 +143,12 @@ void set_branch_iterator(int n);
140 140
  */
141 141
 char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
142 142
 		  unsigned int* flags, struct socket_info** force_socket,
143
-		  str *ruid, str *instance);
143
+		  str *ruid, str *instance, str *location_ua);
144 144
 
145 145
 char* get_branch( unsigned int i, int* len, qvalue_t* q, str* dst_uri,
146 146
 		  str* path, unsigned int *flags,
147 147
 		  struct socket_info** force_socket,
148
-		  str* ruid, str *instance);
148
+		  str* ruid, str *instance, str *location_ua);
149 149
 
150 150
 /*! \brief
151 151
  * Empty the array of branches
... ...
@@ -898,6 +898,45 @@ void reset_ruid(struct sip_msg* const msg)
898 898
 }
899 899
 
900 900
 
901
+int set_ua(struct sip_msg* msg, str* location_ua)
902
+{
903
+	char* ptr;
904
+
905
+	if (unlikely(!msg || !location_ua)) {
906
+		LM_ERR("invalid location_ua parameter value\n");
907
+		return -1;
908
+	}
909
+
910
+	if (unlikely(location_ua->len == 0)) {
911
+		reset_ua(msg);
912
+	} else if (msg->location_ua.s && (msg->location_ua.len >= location_ua->len)) {
913
+		memcpy(msg->location_ua.s, location_ua->s, location_ua->len);
914
+		msg->location_ua.len = location_ua->len;
915
+	} else {
916
+		ptr = (char*)pkg_malloc(location_ua->len);
917
+		if (!ptr) {
918
+			LM_ERR("not enough pkg memory for location_ua\n");
919
+			return -1;
920
+		}
921
+		memcpy(ptr, location_ua->s, location_ua->len);
922
+		if (msg->location_ua.s) pkg_free(msg->location_ua.s);
923
+		msg->location_ua.s = ptr;
924
+		msg->location_ua.len = location_ua->len;
925
+	}
926
+	return 0;
927
+}
928
+
929
+
930
+void reset_ua(struct sip_msg* const msg)
931
+{
932
+	if(msg->location_ua.s != 0) {
933
+		pkg_free(msg->location_ua.s);
934
+	}
935
+	msg->location_ua.s = 0;
936
+	msg->location_ua.len = 0;
937
+}
938
+
939
+
901 940
 hdr_field_t* get_hdr(const sip_msg_t* const msg, const enum _hdr_types_t ht)
902 941
 {
903 942
 	hdr_field_t *hdr;
... ...
@@ -366,6 +366,7 @@ typedef struct sip_msg {
366 366
         str instance;
367 367
         unsigned int reg_id;
368 368
 	str ruid;
369
+	str location_ua;
369 370
 
370 371
 	struct {
371 372
 		int decoded;
... ...
@@ -463,6 +464,10 @@ int set_ruid(struct sip_msg* msg, str* ruid);
463 463
 
464 464
 void reset_ruid(struct sip_msg* const msg);
465 465
 
466
+int set_ua(struct sip_msg* msg, str *location_ua);
467
+
468
+void reset_ua(struct sip_msg* const msg);
469
+
466 470
 /** force a specific send socket for forwarding a request.
467 471
  * @param msg - sip msg.
468 472
  * @param fsocket - forced socket, pointer to struct socket_info, can be 0 (in
... ...
@@ -1621,7 +1621,7 @@ int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
1621 1621
 		char *c;
1622 1622
 		init_branch_iterator();
1623 1623
 		len = 0;
1624
-		while ((c = next_branch(&l, &q, &dst_uri, 0, 0, 0, 0, 0))) {
1624
+		while ((c = next_branch(&l, &q, &dst_uri, 0, 0, 0, 0, 0, 0))) {
1625 1625
 
1626 1626
 			if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1627 1627
 				l = dst_uri.len;
... ...
@@ -1645,7 +1645,7 @@ int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
1645 1645
 		init_branch_iterator();
1646 1646
 		res->len = 0;
1647 1647
 		n = 0;
1648
-		while ((c = next_branch(&l, &q, &dst_uri, 0, 0, 0, 0, 0))) {
1648
+		while ((c = next_branch(&l, &q, &dst_uri, 0, 0, 0, 0, 0, 0))) {
1649 1649
 			if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1650 1650
 				l = dst_uri.len;
1651 1651
 				c = dst_uri.s;
... ...
@@ -1687,7 +1687,7 @@ int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
1687 1687
 		if (n < 0 || n >= nr_branches) 
1688 1688
 			return -1;
1689 1689
 		init_branch_iterator();
1690
-		for (; (c = next_branch(&l, &q, &dst_uri, 0, 0, 0, 0, 0)) && n; n--);
1690
+		for (; (c = next_branch(&l, &q, &dst_uri, 0, 0, 0, 0, 0, 0)) && n; n--);
1691 1691
 		if (!c) return 1;
1692 1692
 		
1693 1693
 		if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {