1 /*
2  * Copyright (C) 2006 Andreas Granig <agranig@linguin.org>
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * Kamailio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  * Kamailio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21 
22 #ifndef OPTION_TAGS_H
23 #define OPTION_TAGS_H
24 
25 #include <strings.h>
26 #include "hf.h"
27 #include "keys.h"
28 
29 #define F_OPTION_TAG_PATH	(1 << 0)
30 #define F_OPTION_TAG_100REL	(1 << 1)
31 #define F_OPTION_TAG_TIMER	(1 << 2)
32 #define F_OPTION_TAG_EVENTLIST	(1 << 3)
33 #define F_OPTION_TAG_GRUU	(1 << 4)
34 #define F_OPTION_TAG_OUTBOUND	(1 << 5)
35 
36 #define OPTION_TAG_PATH_STR		"path"
37 #define OPTION_TAG_PATH_LEN		(sizeof(OPTION_TAG_PATH_STR)-1)
38 
39 /* RFC 3262 (PRACK) */
40 #define OPTION_TAG_100REL_STR		"100rel"
41 #define OPTION_TAG_100REL_LEN		(sizeof(OPTION_TAG_100REL_STR)-1)
42 
43 /* RFC 4028 */
44 #define OPTION_TAG_TIMER_STR		"timer"
45 #define OPTION_TAG_TIMER_LEN		(sizeof(OPTION_TAG_TIMER_STR)-1)
46 
47 /* RFC 4662 (RLS) */
48 #define OPTION_TAG_EVENTLIST_STR	"eventlist"
49 #define OPTION_TAG_EVENTLIST_LEN	(sizeof(OPTION_TAG_EVENTLIST_STR)-1)
50 
51 /* RFC 5627 */
52 #define OPTION_TAG_GRUU_STR		"gruu"
53 #define OPTION_TAG_GRUU_LEN		(sizeof(OPTION_TAG_GRUU_STR)-1)
54 
55 /* RFC 5626 */
56 #define OPTION_TAG_OUTBOUND_STR		"outbound"
57 #define OPTION_TAG_OUTBOUND_LEN		(sizeof(OPTION_TAG_OUTBOUND_STR)-1)
58 
59 
60 struct option_tag_body {
61 	hf_parsed_free_f hfree;        /* function to free the content */
62 	unsigned int option_tags;      /* option-tag mask for the current hdr */
63 	unsigned int option_tags_all;  /* option-tag mask for the all hdr
64 	                                *  - it's set only for the first hdr in
65 	                                *  sibling list*/
66 };
67 
68 
69 #define IS_DELIM(c) (*(c) == ' ' || *(c) == '\t' || *(c) == '\r' || *(c) == '\n' || *(c) == ',')
70 
71 /* from parser/parse_hname2.c: */
72 #define LOWER_BYTE(b) ((b) | 0x20)
73 #define LOWER_DWORD(d) ((d) | 0x20202020)
74 #define READ(val) \
75 	(*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16) + (*(val + 3) << 24))
76 
77 /*!
78  * Parse HF body containing option-tags.
79  */
parse_option_tag_body(str * body,unsigned int * tags)80 static inline int parse_option_tag_body(str *body, unsigned int *tags)
81 {
82 	register char* p;
83 	register unsigned int val;
84 	int len, pos = 0;
85 	int case_found;
86 
87 	*tags = 0;
88 
89 	p = body->s;
90 	len = body->len;
91 
92 	while (pos < len) {
93 		/* skip spaces and commas */
94 		for (; pos < len && IS_DELIM(p); ++pos, ++p);
95 
96 		val = LOWER_DWORD(READ(p));
97 		case_found = 0;
98 		switch (val) {
99 
100 			/* "path" */
101 			case _path_:
102 				if(pos + 4 <= len && IS_DELIM(p+4)) {
103 					*tags |= F_OPTION_TAG_PATH;
104 					pos += 5; p += 5;
105 					case_found = 1;
106 				}
107 				break;
108 
109 			/* "100rel" */
110 			case _100r_:
111 				if ( pos+6 <= len
112 					 && LOWER_BYTE(*(p+4))=='e' && LOWER_BYTE(*(p+5))=='l'
113 					 && IS_DELIM(p+6)) {
114 					*tags |= F_OPTION_TAG_100REL;
115 					pos += OPTION_TAG_100REL_LEN + 1;
116 					p   += OPTION_TAG_100REL_LEN + 1;
117 					case_found = 1;
118 				}
119 				break;
120 
121 			/* "timer" */
122 			case _time_:
123 				if ( pos+5 <= len && LOWER_BYTE(*(p+4))=='r'
124 					 && IS_DELIM(p+5) ) {
125 					*tags |= F_OPTION_TAG_TIMER;
126 					pos += OPTION_TAG_TIMER_LEN + 1;
127 					p   += OPTION_TAG_TIMER_LEN + 1;
128 					case_found = 1;
129 				}
130 				break;
131 		}
132 		if(case_found==0) {
133 			/* extra require or unknown */
134 			if(pos+OPTION_TAG_EVENTLIST_LEN<=len
135 					&& strncasecmp(p, OPTION_TAG_EVENTLIST_STR,
136 						OPTION_TAG_EVENTLIST_LEN)==0
137 					&& IS_DELIM(p+OPTION_TAG_EVENTLIST_LEN) ) {
138 				*tags |= F_OPTION_TAG_EVENTLIST;
139 				pos += OPTION_TAG_EVENTLIST_LEN + 1;
140 				p   += OPTION_TAG_EVENTLIST_LEN + 1;
141 			} else if(pos+OPTION_TAG_GRUU_LEN<=len
142 					&& strncasecmp(p, OPTION_TAG_GRUU_STR,
143 						OPTION_TAG_GRUU_LEN)==0
144 					&& IS_DELIM(p+OPTION_TAG_GRUU_LEN)) {
145 				*tags |= F_OPTION_TAG_GRUU;
146 				pos += OPTION_TAG_GRUU_LEN + 1;
147 				p   += OPTION_TAG_GRUU_LEN + 1;
148 			} else if(pos+OPTION_TAG_OUTBOUND_LEN<=len
149 					&& strncasecmp(p, OPTION_TAG_OUTBOUND_STR,
150 						OPTION_TAG_OUTBOUND_LEN)==0
151 					&& IS_DELIM(p+OPTION_TAG_OUTBOUND_LEN)) {
152 				*tags |= F_OPTION_TAG_OUTBOUND;
153 				pos += OPTION_TAG_OUTBOUND_LEN + 1;
154 				p   += OPTION_TAG_OUTBOUND_LEN + 1;
155 			} else {
156 				/* unknown (not needed) - skip element */
157 				for (; pos < len && !IS_DELIM(p); ++pos, ++p);
158 			}
159 		}
160 	}
161 
162 	return 0;
163 }
164 
165 
166 void hf_free_option_tag(void *parsed);
167 
168 #endif /* OPTION_TAGS_H */
169