Browse code

parse_methods, a function to parse the list of comma separated method names, contributed by Juha Heinanen

Jan Janak authored on 13/08/2004 13:08:54
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,276 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (c) 2004 Juha Heinanen
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+
27
+#include <strings.h>
28
+#include "../dprint.h"
29
+#include "../trim.h"
30
+#include "parse_methods.h"
31
+
32
+
33
+/*
34
+ * Check if argument is valid RFC3261 token character.
35
+ */
36
+static int token_char(char _c)
37
+{
38
+ 	return 	(_c >= 65 && _c <= 90) ||        /* upper alpha */
39
+ 		(_c >= 97 && _c <= 122) ||       /* lower aplha */
40
+ 		(_c >= 48 && _c <= 57) ||        /* digits */
41
+ 		(_c == '-') || (_c == '.') || (_c == '!') || (_c == '%') ||
42
+ 		(_c == '*') || (_c == '_') || (_c == '+') || (_c == '`') ||
43
+ 		(_c == '\'') || (_c == '~') || (_c == '+') || (_c == '`');
44
+ }
45
+ 
46
+ 
47
+ /*
48
+  * Parse a method pointed by _next, assign its enum bit to _method, and update
49
+  * _next past the method. Returns 1 if parse succeeded and 0 otherwise.
50
+  */
51
+static int parse_method(str* _next, unsigned int* _method) 
52
+ {
53
+	 if (!_next || !_method) {
54
+		 LOG(L_ERR, "parse_method: Invalid parameter value\n");
55
+		 return 0;
56
+	 }
57
+	 
58
+	 if (!_next->len || !_next->s) {
59
+		 DBG("parse_method: No input\n");
60
+		 return 1;
61
+	 }
62
+
63
+	 switch ((_next->s)[0]) {
64
+	 case 'A':
65
+	 case 'a':
66
+		 if ((_next->len > 2) && !strncasecmp(_next->s + 1, "ck", 2)) {
67
+ 			*_method = METHOD_ACK;
68
+ 			_next->len -= 3;
69
+ 			_next->s += 3;
70
+ 			return 1;
71
+ 		} else {
72
+ 			goto unknown;
73
+ 		}
74
+
75
+ 	case 'B':
76
+ 	case 'b':
77
+ 		if ((_next->len > 2) && !strncasecmp(_next->s + 1, "ye", 2)) {
78
+ 			*_method = METHOD_BYE;
79
+ 			_next->len -= 3;
80
+ 			_next->s += 3;
81
+ 			return 1;
82
+ 		} else {
83
+ 			goto unknown;
84
+ 		}
85
+
86
+ 	case 'C':
87
+ 	case 'c':
88
+ 		if ((_next->len > 5) && !strncasecmp(_next->s + 1, "ancel", 5)) {
89
+ 			*_method = METHOD_CANCEL;
90
+ 			_next->len -= 6;
91
+ 			_next->s += 6;
92
+ 			return 1;
93
+ 		} else {
94
+ 			goto unknown;
95
+ 		}
96
+
97
+ 	case 'I':
98
+ 	case 'i':
99
+ 		if ((_next->len > 3) &&
100
+ 		    ((*(_next->s + 1) == 'N') || (*(_next->s + 1) == 'n'))) {
101
+ 			if (!strncasecmp(_next->s + 2, "fo", 2)) {
102
+ 				*_method = METHOD_INFO;
103
+ 				_next->len -= 4;
104
+ 				_next->s += 4;
105
+ 				return 1;
106
+ 			}
107
+
108
+ 			if ((_next->len > 5) && !strncasecmp(_next->s + 2, "vite", 4)) {
109
+ 				*_method = METHOD_INVITE;
110
+ 				_next->len -= 6;
111
+ 				_next->s += 6;
112
+ 				return 1;
113
+ 			}
114
+ 		}
115
+ 		goto unknown;
116
+
117
+ 	case 'M':
118
+ 	case 'm':
119
+ 		if ((_next->len > 6) && !strncasecmp(_next->s + 1, "essage", 6)) {
120
+ 			*_method = METHOD_MESSAGE;
121
+ 			_next->len -= 7;
122
+ 			_next->s += 7;
123
+ 			return 1;
124
+ 		} else {
125
+ 			goto unknown;
126
+ 		}
127
+
128
+ 	case 'N':
129
+ 	case 'n':
130
+ 		if ((_next->len > 5) && !strncasecmp(_next->s + 1, "otify", 5)) {
131
+ 			*_method = METHOD_NOTIFY;
132
+ 			_next->len -= 6;
133
+ 			_next->s += 6;
134
+ 			return 1;
135
+ 		} else {
136
+ 			goto unknown;
137
+ 		}
138
+
139
+ 	case 'O':
140
+ 	case 'o':
141
+ 		if ((_next->len > 6) && !strncasecmp(_next->s + 1, "ptions", 6)) {
142
+ 			*_method = METHOD_OPTIONS;
143
+ 			_next->len -= 7;
144
+ 			_next->s += 7;
145
+ 			return 1;
146
+ 		} else {
147
+ 			goto unknown;
148
+ 		}
149
+
150
+ 	case 'P':
151
+ 	case 'p':
152
+ 		if ((_next->len > 4) && !strncasecmp(_next->s + 1, "rack", 4)) {
153
+ 			*_method = METHOD_PRACK;
154
+ 			_next->len -= 5;
155
+ 			_next->s += 5;
156
+ 			return 1;
157
+ 		} else {
158
+ 			goto unknown;
159
+ 		}
160
+
161
+ 	case 'R':
162
+ 	case 'r':
163
+ 		if ((_next->len > 4) &&
164
+ 		    ((*(_next->s + 1) == 'E') || (*(_next->s + 1) == 'e'))) {
165
+ 			if (!strncasecmp(_next->s + 2, "fer", 3)) {
166
+ 				*_method = METHOD_REFER;
167
+ 				_next->len -= 5;
168
+ 				_next->s += 5;
169
+ 				return 1;
170
+ 			}
171
+
172
+ 			if ((_next->len > 7) && !strncasecmp(_next->s + 2, "gister", 6)) {
173
+ 				*_method = METHOD_REGISTER;
174
+ 				_next->len -= 8;
175
+ 				_next->s += 8;
176
+ 				return 1;
177
+ 			}
178
+ 		}
179
+ 		goto unknown;
180
+
181
+ 	case 'S':
182
+ 	case 's':
183
+ 		if ((_next->len > 8) && !strncasecmp(_next->s + 1, "ubscribe", 8)) {
184
+ 			*_method = METHOD_SUBSCRIBE;
185
+ 			_next->len -= 9;
186
+ 			_next->s += 9;
187
+ 			return 1;
188
+ 		} else {
189
+ 			goto unknown;
190
+ 		}
191
+
192
+ 	case 'U':
193
+ 	case 'u':
194
+ 		if ((_next->len > 5) && !strncasecmp(_next->s + 1, "pdate", 5)) {
195
+ 			*_method = METHOD_UPDATE;
196
+ 			_next->len -= 6;
197
+ 			_next->s += 6;
198
+ 			return 1;
199
+ 		} else {
200
+ 			goto unknown;
201
+ 		}
202
+
203
+ 	default:
204
+ 		goto unknown;
205
+ 	}
206
+ 
207
+ unknown:
208
+ 	if (token_char(*(_next->s))) {
209
+ 		do { 
210
+ 			_next->s++;
211
+ 			_next->len--;
212
+ 		} while (_next->len && token_char(*(_next->s)));
213
+ 		*_method = METHOD_UNKNOWN;
214
+ 		return 1;
215
+ 	} else {
216
+ 		return 0;
217
+ 	}
218
+ }
219
+ 
220
+ 
221
+ /* 
222
+  * Parse comma separated list of methods pointed by _body and assign their
223
+  * enum bits to _methods.  Returns 1 on success and 0 on failure.
224
+  */
225
+ int parse_methods(str* _body, unsigned int* _methods)
226
+ {
227
+ 	str next;
228
+ 	unsigned int method;
229
+ 
230
+	if (!_body || !_methods) {
231
+		LOG(L_ERR, "parse_methods: Invalid parameter value\n");
232
+		return 0;
233
+	}
234
+
235
+	next.len = _body->len;
236
+	next.s = _body->s;
237
+ 
238
+ 	trim_leading(&next);
239
+ 
240
+ 	if (next.len == 0) {
241
+ 		LOG(L_ERR, "ERROR: parse_methods: Empty body\n");
242
+ 		return 0;
243
+ 	}
244
+
245
+  	*_methods = 0;
246
+ 
247
+ 	while (1) {
248
+ 		if (parse_method(&next, &method)) {
249
+ 			*_methods |= method;
250
+ 		} else {
251
+ 			LOG(L_ERR, "ERROR: parse_methods: Invalid method\n");
252
+ 			return 0;
253
+ 		}
254
+		
255
+ 		trim_leading(&next);
256
+ 		if (next.len) {
257
+ 			if (next.s[0] == ',') {
258
+ 				next.len--;
259
+ 				next.s++;
260
+ 				trim_leading(&next);
261
+ 				if (next.len == 0) {
262
+ 					LOG(L_ERR, "ERROR: parse_methods: Method expected\n");
263
+ 					return 0;
264
+ 				}
265
+ 			} else {
266
+ 				LOG(L_ERR, "ERROR: parse_methods: Comma expected\n");
267
+ 				return 0;
268
+ 			}
269
+ 		} else {
270
+ 			break;
271
+ 		}
272
+ 	}
273
+
274
+ 	return 1;
275
+ }
0 276
new file mode 100644
... ...
@@ -0,0 +1,61 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (c) 2004 Juha Heinanen
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+
27
+#ifndef PARSE_METHODS_H
28
+#define PARSE_METHODS_H
29
+
30
+#include "../str.h"
31
+
32
+/*
33
+ * Methods found in methods Contact parameter or Allow header.
34
+ */
35
+enum method {
36
+	METHOD_UNKNOWN = 1, 
37
+	METHOD_ACK = 2, 
38
+	METHOD_BYE = 4, 
39
+	METHOD_CANCEL = 8,
40
+	METHOD_INFO = 16, 
41
+	METHOD_INVITE = 32, 
42
+	METHOD_NOTIFY = 64,
43
+	METHOD_OPTIONS = 128, 
44
+	METHOD_PRACK = 256, 
45
+	METHOD_REGISTER  = 512,
46
+	METHOD_SUBSCRIBE = 1024, 
47
+	METHOD_UPDATE = 2048, 
48
+	METHOD_MESSAGE = 4096,
49
+	METHOD_REFER = 8192
50
+};
51
+
52
+
53
+/* 
54
+ * Parse comma separated list of methods pointed by _body and assign their
55
+ * enum bits to _methods.  Returns 1 on success and 0 on failure.
56
+ */
57
+int parse_methods(str* _body, unsigned int* _methods);
58
+
59
+
60
+#endif /* PARSE_METHODS_H */