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