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 270
 			instance->s = 0;
266 271
 			instance->len = 0;
267 272
 		}
273
+		if (location_ua) {
274
+			location_ua->s = 0;
275
+			location_ua->len = 0;
276
+		}
268 277
 		return 0;
269 278
 	}
270 279
 }
... ...
@@ -276,12 +285,12 @@ char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
276 285
  */
277 286
 char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
278 287
 		  unsigned int* flags, struct socket_info** force_socket,
279
-		  str* ruid, str *instance)
288
+		  str* ruid, str *instance, str *location_ua)
280 289
 {
281 290
 	char* ret;
282 291
 	
283 292
 	ret=get_branch(branch_iterator, len, q, dst_uri, path, flags,
284
-		       force_socket, ruid, instance);
293
+		       force_socket, ruid, instance, location_ua);
285 294
 	if (likely(ret))
286 295
 		branch_iterator++;
287 296
 	return ret;
... ...
@@ -316,7 +325,7 @@ int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
316 325
 		  qvalue_t q, unsigned int flags,
317 326
 		  struct socket_info* force_socket,
318 327
 		  str* instance, unsigned int reg_id,
319
-		  str* ruid)
328
+		  str* ruid, str* location_ua)
320 329
 {
321 330
 	str luri;
322 331
 
... ...
@@ -416,6 +425,21 @@ int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
416 425
 		branches[nr_branches].ruid_len = 0;
417 426
 	}
418 427
 
428
+	if (unlikely(location_ua && location_ua->len && location_ua->s)) {
429
+		if (unlikely(location_ua->len > MAX_UA_SIZE)) {
430
+			LOG(L_ERR, "too long location_ua: %.*s\n",
431
+			    location_ua->len, location_ua->s);
432
+			return -1;
433
+		}
434
+		memcpy(branches[nr_branches].location_ua, location_ua->s,
435
+		       location_ua->len);
436
+		branches[nr_branches].location_ua[location_ua->len] = 0;
437
+		branches[nr_branches].location_ua_len = location_ua->len;
438
+	} else {
439
+		branches[nr_branches].location_ua[0] = '\0';
440
+		branches[nr_branches].location_ua_len = 0;
441
+	}
442
+	
419 443
 	nr_branches++;
420 444
 	return 1;
421 445
 }
... ...
@@ -450,7 +474,7 @@ char* print_dset(struct sip_msg* msg, int* len)
450 474
 	crt_branch = get_branch_iterator();
451 475
 
452 476
 	init_branch_iterator();
453
-	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0))) {
477
+	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0, 0))) {
454 478
 		cnt++;
455 479
 		*len += uri.len;
456 480
 		if (q != Q_UNSPECIFIED) {
... ...
@@ -491,7 +515,7 @@ char* print_dset(struct sip_msg* msg, int* len)
491 515
 	}
492 516
 
493 517
 	init_branch_iterator();
494
-	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0))) {
518
+	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0, 0))) {
495 519
 		if (i) {
496 520
 			memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
497 521
 			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 96
 		  qvalue_t q, unsigned int flags,
94 97
 		  struct socket_info* force_socket,
95 98
 		  str* instance, unsigned int reg_id,
96
-		  str* ruid);
99
+		  str* ruid, str* location_ua);
97 100
 
98 101
 /*! \brief kamailio compatible version */
99 102
 #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)
103
+    append_branch(msg, uri, dst_uri, path, q, flags, force_socket, 0, 0, 0, 0)
101 104
 
102 105
 /*! \brief ser compatible append_branch version.
103 106
  *  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 117
     s_uri.len=uri_len;
115 118
     s_dst_uri.s=dst_uri;
116 119
     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);
120
+    return append_branch(msg, &s_uri, &s_dst_uri, 0, q, 0, force_socket, 0, 0, 0, 0);
118 121
 }
119 122
 
120 123
 
... ...
@@ -140,12 +143,12 @@ void set_branch_iterator(int n);
140 143
  */
141 144
 char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
142 145
 		  unsigned int* flags, struct socket_info** force_socket,
143
-		  str *ruid, str *instance);
146
+		  str *ruid, str *instance, str *location_ua);
144 147
 
145 148
 char* get_branch( unsigned int i, int* len, qvalue_t* q, str* dst_uri,
146 149
 		  str* path, unsigned int *flags,
147 150
 		  struct socket_info** force_socket,
148
-		  str* ruid, str *instance);
151
+		  str* ruid, str *instance, str *location_ua);
149 152
 
150 153
 /*! \brief
151 154
  * 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 464
 
464 465
 void reset_ruid(struct sip_msg* const msg);
465 466
 
467
+int set_ua(struct sip_msg* msg, str *location_ua);
468
+
469
+void reset_ua(struct sip_msg* const msg);
470
+
466 471
 /** force a specific send socket for forwarding a request.
467 472
  * @param msg - sip msg.
468 473
  * @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) {