Browse code

parser: relocated helper functions

Daniel-Constantin Mierla authored on 10/01/2012 22:33:00
Showing 25 changed files
1 1
deleted file mode 100644
... ...
@@ -1,211 +0,0 @@
1
-#include "parser_helpers.h"
2
-#include "errinfo.h"
3
-#include "../../parser/parse_to.h"
4
-#include "../../parser/parse_from.h"
5
-#include "../../parser/parse_rr.h"
6
-#include "../../dprint.h"
7
-#include "../../mem/mem.h"
8
-
9
-#include <string.h>
10
-
11
-struct sip_uri *parse_to_uri(struct sip_msg *msg)
12
-{
13
-	struct to_body *tb = NULL;
14
-	
15
-	if(msg==NULL)
16
-		return NULL;
17
-
18
-	if(parse_to_header(msg)<0)
19
-	{
20
-		LM_ERR("cannot parse TO header\n");
21
-		return NULL;
22
-	}
23
-
24
-	if(msg->to==NULL || get_to(msg)==NULL)
25
-		return NULL;
26
-
27
-	tb = get_to(msg);
28
-	
29
-	if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
30
-		return &tb->parsed_uri;
31
-
32
-	if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
33
-	{
34
-		LM_ERR("failed to parse To uri\n");
35
-		memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
36
-		set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing To uri");
37
-		set_err_reply(400, "bad To uri");
38
-		return NULL;
39
-	}
40
-
41
-	return &tb->parsed_uri;
42
-}
43
-
44
-
45
-struct sip_uri *parse_from_uri(struct sip_msg *msg)
46
-{
47
-	struct to_body *tb = NULL;
48
-        
49
-	if(msg==NULL)
50
-		return NULL;
51
-
52
-	if(parse_from_header(msg)<0)
53
-	{
54
-		LM_ERR("cannot parse FROM header\n");
55
-		return NULL;
56
-	}
57
-	
58
-	if(msg->from==NULL || get_from(msg)==NULL)
59
-		return NULL;
60
-
61
-	tb = get_from(msg);
62
-	
63
-	if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
64
-		return &tb->parsed_uri;
65
-	
66
-	if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
67
-	{
68
-		LM_ERR("failed to parse From uri\n");
69
-		memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
70
-		set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing From uri");
71
-		set_err_reply(400, "bad From uri");
72
-		return NULL;
73
-	}
74
-	return &tb->parsed_uri;
75
-}
76
-
77
-
78
-/*!
79
- * get first RR header and print comma separated bodies in oroute
80
- * - order = 0 normal; order = 1 reverse
81
- * - nb_recs - input=skip number of rr; output=number of printed rrs
82
- */
83
-int print_rr_body(struct hdr_field *iroute, str *oroute, int order,
84
-												unsigned int * nb_recs)
85
-{
86
-	rr_t *p;
87
-	int n = 0, nr=0;
88
-	int i = 0;
89
-	int route_len;
90
-#define MAX_RR_HDRS	64
91
-	static str route[MAX_RR_HDRS];
92
-	char *cp, *start;
93
-
94
-	if(iroute==NULL)
95
-		return 0;
96
-
97
-	route_len= 0;
98
-	memset(route, 0, MAX_RR_HDRS*sizeof(str));
99
-
100
-	while (iroute!=NULL) 
101
-	{
102
-		if (parse_rr(iroute) < 0) 
103
-		{
104
-			LM_ERR("failed to parse RR\n");
105
-			goto error;
106
-		}
107
-
108
-		p =(rr_t*)iroute->parsed;
109
-		while (p)
110
-		{
111
-			route[n].s = p->nameaddr.name.s;
112
-			route[n].len = p->len;
113
-			LM_DBG("current rr is %.*s\n", route[n].len, route[n].s);
114
-
115
-			n++;
116
-			if(n==MAX_RR_HDRS)
117
-			{
118
-				LM_ERR("too many RR\n");
119
-				goto error;
120
-			}
121
-			p = p->next;
122
-		}
123
-
124
-		iroute = next_sibling_hdr(iroute);
125
-	}
126
-
127
-	for(i=0;i<n;i++){
128
-		if(!nb_recs || (nb_recs && 
129
-		 ( (!order&& (i>=*nb_recs)) || (order && (i<=(n-*nb_recs)) )) ) )
130
-		{
131
-			route_len+= route[i].len;
132
-			nr++;
133
-		}
134
-	
135
-	}
136
-
137
-	if(nb_recs)
138
-		LM_DBG("skipping %i route records\n", *nb_recs);
139
-	
140
-	route_len += --nr; /* for commas */
141
-
142
-	oroute->s=(char*)pkg_malloc(route_len);
143
-
144
-
145
-	if(oroute->s==0)
146
-	{
147
-		LM_ERR("no more pkg mem\n");
148
-		goto error;
149
-	}
150
-	cp = start = oroute->s;
151
-	if(order==0)
152
-	{
153
-		i= (nb_recs == NULL) ? 0:*nb_recs;
154
-
155
-		while (i<n)
156
-		{
157
-			memcpy(cp, route[i].s, route[i].len);
158
-			cp += route[i].len;
159
-			if (++i<n)
160
-				*(cp++) = ',';
161
-		}
162
-	} else {
163
-		
164
-		i = (nb_recs == NULL) ? n-1 : (n-*nb_recs-1);
165
-			
166
-		while (i>=0)
167
-		{
168
-			memcpy(cp, route[i].s, route[i].len);
169
-			cp += route[i].len;
170
-			if (i-->0)
171
-				*(cp++) = ',';
172
-		}
173
-	}
174
-	oroute->len=cp - start;
175
-
176
-	LM_DBG("out rr [%.*s]\n", oroute->len, oroute->s);
177
-	LM_DBG("we have %i records\n", n);
178
-	if(nb_recs != NULL)
179
-		*nb_recs = (unsigned int)n; 
180
-
181
-	return 0;
182
-
183
-error:
184
-	return -1;
185
-}
186
-
187
-
188
-/*!
189
- * Path must be available. Function returns the first uri 
190
- * from Path without any duplication.
191
- */
192
-int get_path_dst_uri(str *_p, str *_dst)
193
-{
194
-	rr_t *route = 0;
195
-
196
-	LM_DBG("path for branch: '%.*s'\n", _p->len, _p->s);
197
-	if(parse_rr_body(_p->s, _p->len, &route) < 0) {	
198
-		LM_ERR("failed to parse Path body\n");
199
-		return -1;
200
-	}
201
-
202
-	if(!route) {
203
-		LM_ERR("failed to parse Path body no head found\n");
204
-		return -1;
205
-	}
206
-	*_dst = route->nameaddr.uri;
207
-
208
-	free_rr(&route);
209
-	
210
-	return 0;
211
-}
212 0
deleted file mode 100644
... ...
@@ -1,22 +0,0 @@
1
-#ifndef _PARSER_HELPERS_H
2
-#define _PARSER_HELPERS_H
3
-
4
-#include "../../parser/msg_parser.h"
5
-#include "../../parser/parse_uri.h"
6
-#include "../../str.h"
7
-
8
-struct sip_uri* parse_to_uri(struct sip_msg* msg);
9
-
10
-struct sip_uri* parse_from_uri(struct sip_msg* msg);
11
-
12
-/*!
13
- * get first RR header and print comma separated bodies in oroute
14
- * - order = 0 normal; order = 1 reverse
15
- * - nb_recs - input=skip number of rr; output=number of printed rrs
16
- */
17
-int print_rr_body(struct hdr_field *iroute, str *oroute, int order,
18
-				  unsigned int * nb_recs);
19
-
20
-int get_path_dst_uri(str *_p, str *_dst);
21
-
22
-#endif /* _PARSER_HELPERS_H */
... ...
@@ -61,7 +61,6 @@
61 61
 #include "../../qvalue.h"
62 62
 #include "enum_mod.h"
63 63
 #include "../../lib/kcore/regexp.h"
64
-#include "../../lib/kcore/parser_helpers.h"
65 64
 #include "../../pvar.h"
66 65
 
67 66
 /*
... ...
@@ -211,7 +211,6 @@
211 211
 #include "../../timer_proc.h"
212 212
 #include "../../lib/kmi/mi.h"
213 213
 #include "../../lib/kcore/km_ut.h"
214
-#include "../../lib/kcore/parser_helpers.h"
215 214
 #include "../../pvar.h"
216 215
 #include "../../lvalue.h"
217 216
 #include "../../msg_translator.h"
... ...
@@ -42,10 +42,11 @@
42 42
 #include "../../parser/digest/digest.h"
43 43
 #include "../../parser/hf.h"
44 44
 #include "../../parser/parser_f.h"
45
+#include "../../parser/parse_from.h"
46
+#include "../../parser/parse_to.h"
45 47
 #include "../../usr_avp.h"
46 48
 #include "../../mod_fix.h"
47 49
 #include "../../mem/mem.h"
48
-#include "../../lib/kcore/parser_helpers.h"
49 50
 #include "api.h"
50 51
 #include "authdb_mod.h"
51 52
 
... ...
@@ -38,7 +38,6 @@
38 38
 #include "../../parser/parse_uri.h"
39 39
 #include "../../parser/parse_from.h"
40 40
 #include "../../parser/parse_to.h"
41
-#include "../../lib/kcore/parser_helpers.h"
42 41
 #include "../../dprint.h"
43 42
 #include "../../ut.h"
44 43
 #include "../../pvar.h"
... ...
@@ -59,7 +59,6 @@
59 59
 #include "../../action.h"
60 60
 #include "../../script_cb.h"
61 61
 #include "../../lib/kcore/faked_msg.h"
62
-#include "../../lib/kcore/parser_helpers.h"
63 62
 #include "../../parser/parse_from.h"
64 63
 #include "../../parser/parse_cseq.h"
65 64
 #include "../../parser/contact/parse_contact.h"
... ...
@@ -34,7 +34,6 @@
34 34
 #include "../../lib/srdb1/db.h"
35 35
 #include "../../parser/parse_uri.h"
36 36
 #include "../../parser/parse_from.h"
37
-#include "../../lib/kcore/parser_helpers.h"
38 37
 #include "../../ut.h"
39 38
 #include "../../dset.h"
40 39
 #include "../../route.h"
... ...
@@ -42,7 +42,6 @@
42 42
 #include "../../parser/hf.h"            /* Header Field types */
43 43
 #include "../../parser/parse_from.h"    /* From parser */
44 44
 #include "../../parser/parse_uri.h"
45
-#include "../../lib/kcore/parser_helpers.h"
46 45
 #include "group.h"
47 46
 #include "group_mod.h"                   /* Module parameters */
48 47
 
... ...
@@ -213,7 +213,6 @@
213 213
 #include "../../lib/kmi/attr.h"
214 214
 #include "../../lib/kmi/mi.h"
215 215
 #include "../../lib/kcore/km_ut.h"
216
-#include "../../lib/kcore/parser_helpers.h"
217 216
 #include "../../pvar.h"
218 217
 #include "../../lvalue.h"
219 218
 #include "../../msg_translator.h"
... ...
@@ -41,7 +41,6 @@
41 41
 #include <stdlib.h>
42 42
 
43 43
 #include "../../lib/srdb1/db_op.h"
44
-#include "../../lib/kcore/parser_helpers.h"
45 44
 #include "../../sr_module.h"
46 45
 #include "../../lib/srdb1/db.h"
47 46
 #include "../../mem/shm_mem.h"
... ...
@@ -39,7 +39,6 @@
39 39
 #include "../../parser/parse_event.h"
40 40
 #include "../../parser/contact/parse_contact.h"
41 41
 #include "../../lib/kcore/hash_func.h"
42
-#include "../../lib/kcore/parser_helpers.h"
43 42
 #include "presence.h"
44 43
 #include "subscribe.h"
45 44
 #include "utils_func.h"
... ...
@@ -40,7 +40,6 @@
40 40
 #include "../../parser/parse_expires.h"
41 41
 #include "../presence/hash.h"
42 42
 #include "../../lib/kcore/hash_func.h"
43
-#include "../../lib/kcore/parser_helpers.h"
44 43
 #include "hash.h"
45 44
 #include "pua.h"
46 45
 #include "send_subscribe.h"
... ...
@@ -42,7 +42,6 @@
42 42
 #include "../../parser/parse_diversion.h"
43 43
 #include "../../lib/kcore/parse_ppi.h"
44 44
 #include "../../lib/kcore/parse_pai.h"
45
-#include "../../lib/kcore/parser_helpers.h"
46 45
 #include "../../parser/digest/digest.h"
47 46
 
48 47
 #include "pv_core.h"
... ...
@@ -40,7 +40,6 @@
40 40
 #include "../../action.h"
41 41
 #include "../../parser/parse_rr.h"
42 42
 #include "../usrloc/usrloc.h"
43
-#include "../../lib/kcore/parser_helpers.h"
44 43
 #include "common.h"
45 44
 #include "regtime.h"
46 45
 #include "reg_mod.h"
... ...
@@ -36,7 +36,6 @@
36 36
 #include "../../lib/kcore/cmpapi.h"
37 37
 #include "../../lib/kcore/hash_func.h"
38 38
 #include "../../lib/kcore/parse_supported.h"
39
-#include "../../lib/kcore/parser_helpers.h"
40 39
 #include "../../parser/msg_parser.h"
41 40
 #include "../../parser/parse_event.h"
42 41
 #include "../../parser/parse_expires.h"
... ...
@@ -48,7 +48,6 @@
48 48
 #include "../../parser/parse_param.h"
49 49
 #include "../../ut.h"                   /* Handy utilities */
50 50
 #include "../../lib/srdb1/db.h"                /* Database API */
51
-#include "../../lib/kcore/parser_helpers.h"
52 51
 #include "../../dset.h"
53 52
 #include "../../pvar.h"
54 53
 #include "../../lvalue.h"
... ...
@@ -35,7 +35,6 @@
35 35
 #include "../../parser/parse_uri.h"
36 36
 #include "../../parser/parse_from.h"
37 37
 #include "../../lib/srdb1/db.h"
38
-#include "../../lib/kcore/parser_helpers.h"
39 38
 
40 39
 #include "speeddial.h"
41 40
 #include "sdlookup.h"
... ...
@@ -35,7 +35,6 @@
35 35
 #include "../../parser/digest/digest.h" /* get_authorized_cred */
36 36
 #include "../../parser/parse_from.h"
37 37
 #include "../../parser/parse_uri.h"
38
-#include "../../lib/kcore/parser_helpers.h"
39 38
 #include "../../ut.h"                   /* Handy utilities */
40 39
 #include "../../lib/srdb1/db.h"                /* Database API */
41 40
 #include "uridb_mod.h"
... ...
@@ -37,6 +37,7 @@
37 37
 #include <string.h>
38 38
 #include "../dprint.h"
39 39
 #include "msg_parser.h"
40
+#include "parse_uri.h"
40 41
 #include "../ut.h"
41 42
 #include "../mem/mem.h"
42 43
 
... ...
@@ -88,4 +89,32 @@ error:
88 89
 	return -1;
89 90
 }
90 91
 
92
+sip_uri_t *parse_from_uri(sip_msg_t *msg)
93
+{
94
+	to_body_t *tb = NULL;
95
+        
96
+	if(msg==NULL)
97
+		return NULL;
98
+
99
+	if(parse_from_header(msg)<0)
100
+	{
101
+		LM_ERR("cannot parse FROM header\n");
102
+		return NULL;
103
+	}
104
+	
105
+	if(msg->from==NULL || get_from(msg)==NULL)
106
+		return NULL;
91 107
 
108
+	tb = get_from(msg);
109
+	
110
+	if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
111
+		return &tb->parsed_uri;
112
+	
113
+	if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
114
+	{
115
+		LM_ERR("failed to parse From uri\n");
116
+		memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
117
+		return NULL;
118
+	}
119
+	return &tb->parsed_uri;
120
+}
... ...
@@ -47,4 +47,6 @@
47 47
  */
48 48
 int parse_from_header( struct sip_msg *msg);
49 49
 
50
+sip_uri_t *parse_from_uri(sip_msg_t *msg);
51
+
50 52
 #endif
... ...
@@ -343,3 +343,138 @@ int shm_duplicate_rr(rr_t** _new, rr_t* _r)
343 343
 {
344 344
 	return do_duplicate_rr(_new, _r, 1);
345 345
 }
346
+
347
+/*!
348
+ * get first RR header and print comma separated bodies in oroute
349
+ * - order = 0 normal; order = 1 reverse
350
+ * - nb_recs - input=skip number of rr; output=number of printed rrs
351
+ */
352
+int print_rr_body(struct hdr_field *iroute, str *oroute, int order,
353
+												unsigned int * nb_recs)
354
+{
355
+	rr_t *p;
356
+	int n = 0, nr=0;
357
+	int i = 0;
358
+	int route_len;
359
+#define MAX_RR_HDRS	64
360
+	static str route[MAX_RR_HDRS];
361
+	char *cp, *start;
362
+
363
+	if(iroute==NULL)
364
+		return 0;
365
+
366
+	route_len= 0;
367
+	memset(route, 0, MAX_RR_HDRS*sizeof(str));
368
+
369
+	while (iroute!=NULL) 
370
+	{
371
+		if (parse_rr(iroute) < 0) 
372
+		{
373
+			LM_ERR("failed to parse RR\n");
374
+			goto error;
375
+		}
376
+
377
+		p =(rr_t*)iroute->parsed;
378
+		while (p)
379
+		{
380
+			route[n].s = p->nameaddr.name.s;
381
+			route[n].len = p->len;
382
+			LM_DBG("current rr is %.*s\n", route[n].len, route[n].s);
383
+
384
+			n++;
385
+			if(n==MAX_RR_HDRS)
386
+			{
387
+				LM_ERR("too many RR\n");
388
+				goto error;
389
+			}
390
+			p = p->next;
391
+		}
392
+
393
+		iroute = next_sibling_hdr(iroute);
394
+	}
395
+
396
+	for(i=0;i<n;i++){
397
+		if(!nb_recs || (nb_recs && 
398
+		 ( (!order&& (i>=*nb_recs)) || (order && (i<=(n-*nb_recs)) )) ) )
399
+		{
400
+			route_len+= route[i].len;
401
+			nr++;
402
+		}
403
+	
404
+	}
405
+
406
+	if(nb_recs)
407
+		LM_DBG("skipping %i route records\n", *nb_recs);
408
+	
409
+	route_len += --nr; /* for commas */
410
+
411
+	oroute->s=(char*)pkg_malloc(route_len);
412
+
413
+
414
+	if(oroute->s==0)
415
+	{
416
+		LM_ERR("no more pkg mem\n");
417
+		goto error;
418
+	}
419
+	cp = start = oroute->s;
420
+	if(order==0)
421
+	{
422
+		i= (nb_recs == NULL) ? 0:*nb_recs;
423
+
424
+		while (i<n)
425
+		{
426
+			memcpy(cp, route[i].s, route[i].len);
427
+			cp += route[i].len;
428
+			if (++i<n)
429
+				*(cp++) = ',';
430
+		}
431
+	} else {
432
+		
433
+		i = (nb_recs == NULL) ? n-1 : (n-*nb_recs-1);
434
+			
435
+		while (i>=0)
436
+		{
437
+			memcpy(cp, route[i].s, route[i].len);
438
+			cp += route[i].len;
439
+			if (i-->0)
440
+				*(cp++) = ',';
441
+		}
442
+	}
443
+	oroute->len=cp - start;
444
+
445
+	LM_DBG("out rr [%.*s]\n", oroute->len, oroute->s);
446
+	LM_DBG("we have %i records\n", n);
447
+	if(nb_recs != NULL)
448
+		*nb_recs = (unsigned int)n; 
449
+
450
+	return 0;
451
+
452
+error:
453
+	return -1;
454
+}
455
+
456
+
457
+/*!
458
+ * Path must be available. Function returns the first uri 
459
+ * from Path without any duplication.
460
+ */
461
+int get_path_dst_uri(str *_p, str *_dst)
462
+{
463
+	rr_t *route = 0;
464
+
465
+	LM_DBG("path for branch: '%.*s'\n", _p->len, _p->s);
466
+	if(parse_rr_body(_p->s, _p->len, &route) < 0) {	
467
+		LM_ERR("failed to parse Path body\n");
468
+		return -1;
469
+	}
470
+
471
+	if(!route) {
472
+		LM_ERR("failed to parse Path body no head found\n");
473
+		return -1;
474
+	}
475
+	*_dst = route->nameaddr.uri;
476
+
477
+	free_rr(&route);
478
+	
479
+	return 0;
480
+}
... ...
@@ -177,4 +177,14 @@ static inline int is_2rr(str* _params)
177 177
 	else return 0;
178 178
 }
179 179
 
180
+/*!
181
+ * get first RR header and print comma separated bodies in oroute
182
+ * - order = 0 normal; order = 1 reverse
183
+ * - nb_recs - input=skip number of rr; output=number of printed rrs
184
+ */
185
+int print_rr_body(struct hdr_field *iroute, str *oroute, int order,
186
+				  unsigned int * nb_recs);
187
+
188
+int get_path_dst_uri(str *_p, str *_dst);
189
+
180 190
 #endif /* PARSE_RR_H */
... ...
@@ -35,6 +35,7 @@
35 35
 #include <string.h>
36 36
 #include "../dprint.h"
37 37
 #include "msg_parser.h"
38
+#include "parse_uri.h"
38 39
 #include "../ut.h"
39 40
 #include "../mem/mem.h"
40 41
 
... ...
@@ -854,3 +855,34 @@ int parse_to_header(struct sip_msg *msg)
854 855
 	else
855 856
 		return -1;
856 857
 }
858
+
859
+sip_uri_t *parse_to_uri(sip_msg_t *msg)
860
+{
861
+	to_body_t *tb = NULL;
862
+	
863
+	if(msg==NULL)
864
+		return NULL;
865
+
866
+	if(parse_to_header(msg)<0)
867
+	{
868
+		LM_ERR("cannot parse TO header\n");
869
+		return NULL;
870
+	}
871
+
872
+	if(msg->to==NULL || get_to(msg)==NULL)
873
+		return NULL;
874
+
875
+	tb = get_to(msg);
876
+	
877
+	if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
878
+		return &tb->parsed_uri;
879
+
880
+	if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
881
+	{
882
+		LM_ERR("failed to parse To uri\n");
883
+		memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
884
+		return NULL;
885
+	}
886
+
887
+	return &tb->parsed_uri;
888
+}
... ...
@@ -73,4 +73,6 @@ void free_to(struct to_body* tb);
73 73
 
74 74
 int parse_to_header(struct sip_msg *msg);
75 75
 
76
+sip_uri_t *parse_to_uri(struct sip_msg* msg);
77
+
76 78
 #endif