Browse code

Route & Record-Route parser

Jan Janak authored on 26/03/2003 13:50:44
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,171 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Route & Record-Route header field parser
4
+ *
5
+ * Copyright (C) 2001-2003 Fhg Fokus
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser 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
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ */
28
+
29
+#include <stdio.h>
30
+#include <string.h>
31
+#include "parse_rr.h"
32
+#include "../../mem/mem.h"
33
+#include "../../dprint.h"
34
+#include "../../trim.h"
35
+
36
+
37
+/*
38
+ * Parse Route and Record-Route header fields
39
+ */
40
+int parse_rr(str* _s, rr_t** _r)
41
+{
42
+	rr_t* r, *res, *last;
43
+	str s;
44
+	param_hooks_t hooks;
45
+
46
+	if (!_s || !_r) {
47
+		LOG(L_ERR, "parse_rr(): Invalid parameter value\n");
48
+		return -1;
49
+	}
50
+
51
+	     /* Make a temporary copy of the string */
52
+	s.s = _s->s;
53
+	s.len = _s->len;
54
+
55
+	res = last = 0;
56
+
57
+	while(1) {
58
+		     /* Allocate and clear rr stucture */
59
+		r = (rr_t*)pkg_malloc(sizeof(rr_t));
60
+		if (!r) {
61
+			LOG(L_ERR, "parse_rr(): No memory left\n");
62
+			goto error;
63
+		}
64
+
65
+		memset(r, 0, sizeof(rr_t));
66
+		
67
+		     /* Parse name-addr part of the header */
68
+		if (parse_nameaddr(_s, &r->nameaddr) < 0) {
69
+			LOG(L_ERR, "parse_rr(): Error while parsing name-addr\n");
70
+			goto error;
71
+		}
72
+		
73
+		     /* Shift just behind the closing > */
74
+		s.s = r->nameaddr.uri.s + r->nameaddr.uri.len + 1; /* Point just behind > */
75
+		s.len -= r->nameaddr.name.len + r->nameaddr.uri.len + 2;
76
+
77
+		     /* Nothing left, finish */
78
+		if (s.len == 0) goto ok;
79
+		
80
+		if (s.s[0] == ';') {         /* Contact parameter found */
81
+			s.s++;
82
+			s.len--;
83
+			trim_leading(&s);
84
+			
85
+			if (s.len == 0) {
86
+				LOG(L_ERR, "parse_rr(): Error while parsing params\n");
87
+				goto error;
88
+			}
89
+
90
+			     /* Parse all parameters */
91
+			if (parse_params(&s, CLASS_RR, &hooks, &r->params) < 0) {
92
+				LOG(L_ERR, "parse_rr(): Error while parsing params\n");
93
+				goto error;
94
+			}
95
+
96
+			     /* Copy hooks */
97
+			r->lr = hooks.rr.lr;
98
+			r->r2 = hooks.rr.r2;
99
+
100
+			if (s.len == 0) goto ok;
101
+		}
102
+
103
+		     /* Next character is comma or end of header*/
104
+		s.s++;
105
+		s.len--;
106
+		trim_leading(&s);
107
+
108
+		if (s.len == 0) {
109
+			LOG(L_ERR, "parse_rr(): Text after comma missing\n");
110
+			goto error;
111
+		}
112
+
113
+		     /* Append the structure as last parameter of the linked list */
114
+		if (!res) res = r;
115
+		if (last) last->next = r;
116
+		last = r;
117
+	}
118
+
119
+ error:
120
+	if (r) pkg_free(r);
121
+	free_rr(res); /* Free any contacts created so far */
122
+	return -1;
123
+
124
+ ok:
125
+	if (last) last->next = r;
126
+	*_r = res;
127
+	return 0;
128
+}
129
+
130
+
131
+/*
132
+ * Free list of rrs
133
+ * _r is head of the list
134
+ */
135
+void free_rr(rr_t* _r)
136
+{
137
+	rr_t* ptr;
138
+
139
+	while(_r) {
140
+		ptr = _r;
141
+		_r = _r->next;
142
+		if (ptr->params) {
143
+			free_params(ptr->params);
144
+		}
145
+		pkg_free(ptr);
146
+	}
147
+}
148
+
149
+
150
+/*
151
+ * Print list of RRs, just for debugging
152
+ */
153
+void print_rr(rr_t* _r)
154
+{
155
+	rr_t* ptr;
156
+
157
+	ptr = _r;
158
+
159
+	while(ptr) {
160
+		printf("---RR---\n");
161
+		print_nameaddr(&ptr->nameaddr);
162
+		printf("lr : %p\n", ptr->lr);
163
+		printf("r2 : %p\n", ptr->r2);
164
+		if (ptr->params) {
165
+			print_params(ptr->params);
166
+		}
167
+		printf("---/RR---\n");
168
+		ptr = ptr->next;
169
+	}
170
+}
0 171
new file mode 100644
... ...
@@ -0,0 +1,70 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Route & Record-Route Parser
4
+ *
5
+ * Copyright (C) 2001-2003 Fhg Fokus
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser 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
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ */
28
+
29
+
30
+#ifndef PARSE_RR_H
31
+#define PARSE_RR_H
32
+
33
+#include "../../str.h"
34
+#include "../parse_nameaddr.h"
35
+#include "../parse_param.h"
36
+
37
+
38
+/*
39
+ * Structure representing a Route & Record-Route HF body
40
+ */
41
+typedef struct rr {
42
+	name_addr_t nameaddr; /* Name-addr part */
43
+	param_t* lr;          /* Hook to lr parameter */
44
+	param_t* r2;          /* Hook to r2 parameter */
45
+	param_t* params;      /* Linked list of other parameters */
46
+        struct rr* next;      /* Next RR in the list */
47
+} rr_t;
48
+
49
+
50
+/*
51
+ * Parse Route & Record-Route header fields
52
+ */
53
+int parse_rr(str* _s, rr_t** _r);
54
+
55
+
56
+/*
57
+ * Free list of rr
58
+ * _c is head of the list
59
+ */
60
+void free_rr(rr_t* _r);
61
+
62
+
63
+/*
64
+ * Print list of rrs, just for debugging
65
+ */
66
+void print_rr(rr_t* _r);
67
+
68
+
69
+#endif /* PARSE_RR_H */