Browse code

name-addr (see RFC3261) parser

Jan Janak authored on 24/03/2003 16:42:21
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,86 @@
0
+/* 
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2001-2003 Fhg Fokus
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
+ * History
27
+ * --------
28
+ * 2003-03-24 Created by janakj
29
+ */
30
+
31
+#include <stdio.h>
32
+#include <string.h>
33
+#include "../dprint.h"
34
+#include "parse_nameaddr.h"
35
+#include "parser_f.h"
36
+
37
+/*
38
+ * Parse name-addr part, the given string can be longer,
39
+ * it will be updated to point right behind the name-addr part
40
+ */
41
+int parse_nameaddr(str* _s, name_addr_t* _a)
42
+{
43
+	char* uri_end;
44
+
45
+	if (!_s || !_a) {
46
+		LOG(L_ERR, "parse_nameaddr(): Invalid parameter value\n");
47
+		return -1;
48
+	}
49
+
50
+	memset(_a, 0, sizeof(name_addr_t));
51
+
52
+	_a->name.s = _s->s;
53
+
54
+	_a->uri.s = find_not_quoted(_s, '<'); 
55
+	if (_a->uri.s) {
56
+		_a->name.len = _a->uri.s - _a->name.s;
57
+		_a->uri.s++; /* We will skip < character */
58
+	} else {
59
+		LOG(L_ERR, "parse_nameaddr(): No < found\n");
60
+		return -3;
61
+	}
62
+	
63
+	_a->uri.len = _s->len - _a->name.len - 1;
64
+	uri_end = find_not_quoted(&_a->uri, '>');
65
+	
66
+	if (!uri_end) {
67
+		LOG(L_ERR, "parse_nameaddr(): No > found\n");
68
+		return -4;
69
+	}
70
+	
71
+	_a->uri.len = uri_end - _a->uri.s;
72
+	return 0;
73
+}
74
+
75
+
76
+/*
77
+ * Print a name-addr structure, just for debugging
78
+ */
79
+void print_nameaddr(name_addr_t* _a)
80
+{
81
+	printf("---name-addr---\n");
82
+	printf("name: '%.*s'\n", _a->name.len, _a->name.s);
83
+	printf("uri : '%.*s'\n", _a->uri.len, _a->uri.s);
84
+	printf("---/name-addr---\n");
85
+}
0 86
new file mode 100644
... ...
@@ -0,0 +1,59 @@
0
+/* 
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2001-2003 Fhg Fokus
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
+ * History
27
+ * --------
28
+ * 2003-03-24 Created by janakj
29
+ */
30
+
31
+#ifndef PARSE_NAMEADDR_H
32
+#define PARSE_NAMEADDR_H
33
+
34
+#include "../str.h"
35
+
36
+/*
37
+ * Name-addr structure, see RFC3261 for more details
38
+ */
39
+typedef struct name_addr {
40
+	str name;   /* Display name part */
41
+	str uri;    /* Uri part without surrounding <> */
42
+} name_addr_t;
43
+
44
+
45
+/*
46
+ * Parse name-addr part, the given string can be longer,
47
+ * it will be updated to point right behind the name-addr part
48
+ */
49
+int parse_nameaddr(str* _s, name_addr_t* _a);
50
+
51
+
52
+/*
53
+ * Print a name-addr structure, just for debugging
54
+ */
55
+void print_nameaddr(name_addr_t* _a);
56
+
57
+
58
+#endif /* PARSE_NAMEADDR_H */
... ...
@@ -34,6 +34,7 @@
34 34
 #define parser_f_h
35 35
 
36 36
 #include "../comp_defs.h"
37
+#include "../str.h"
37 38
 
38 39
 char* eat_line(char* buffer, unsigned int len);
39 40
 
... ...
@@ -83,4 +84,22 @@ inline static int is_empty_end(char* p, char* pend )
83 83
 }
84 84
 
85 85
 
86
+/*
87
+ * Find a character occurence that is not quoted
88
+ */
89
+inline static char* find_not_quoted(str* _s, char _c)
90
+{
91
+	int quoted = 0, i;
92
+	
93
+	for(i = 0; i < _s->len; i++) {
94
+		if (!quoted) {
95
+			if (_s->s[i] == '\"') quoted = 1;
96
+			else if (_s->s[i] == _c) return _s->s + i;
97
+		} else {
98
+			if ((_s->s[i] == '\"') && (_s->s[i - 1] != '\\')) quoted = 0;
99
+		}
100
+	}
101
+	return 0;
102
+}
103
+
86 104
 #endif /* parser_f_h */