File: parse_sst.c

package info (click to toggle)
openser 1.1.0-9etch1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,828 kB
  • ctags: 11,809
  • sloc: ansic: 120,528; sh: 5,249; yacc: 1,716; makefile: 1,261; php: 656; perl: 205; sql: 190
file content (241 lines) | stat: -rw-r--r-- 6,526 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* 
 * $Id: parse_sst.c,v 1.2 2006/05/29 10:47:46 bogdan_iancu Exp $
 * 
 * Copyright (c) 2006 SOMA Networks, Inc. <http://www.somanetworks.com/>
 *
 * This file is part of openser, a free SIP server.
 *
 * openser is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * openser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * History:
 * --------
 * 2006-02-17 Initial revision (dhsueh@somanetworks.com)
 */

#include "parse_sst.h"

#include "../error.h"
#include "../dprint.h"
#include "../mem/mem.h"


inline int/*bool*/  is_space( char c ) { return (c == ' ' || c == '\t'); }
inline int/*bool*/  is_num( char c ) { return (c >= '0' && c <= '9'); }

inline unsigned  lower_byte( char b ) { return b | 0x20; }
inline unsigned  lower_4bytes( unsigned d ) { return d | 0x20202020; }
inline unsigned  lower_3bytes( unsigned d ) { return d |   0x202020; }
inline unsigned  read_4bytes( char *val ) {
	return (*(val + 0) + (*(val + 1) << 8)
		+ (*(val + 2) << 16) + (*(val + 3) << 24));
}
inline unsigned  read_3bytes( char *val ) {
	return (*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16));
}

/* compile-time constants if called with constants */
#define  MAKE_4BYTES( a, b, c, d ) \
	( ((a)&0xFF) | (((b)&0xFF)<<8) | (((c)&0xFF)<<16) | (((d)&0xFF)<<24) )
#define  MAKE_3BYTES( a, b, c ) \
	( ((a)&0xFF) | (((b)&0xFF)<<8) | (((c)&0xFF)<<16) )


struct session_expires *
malloc_session_expires( void )
{
	struct session_expires *se = (struct session_expires *)
		pkg_malloc( sizeof(struct session_expires) );
	if ( se )
		memset( se, 0, sizeof(struct session_expires) );
	return se;
}


void
free_session_expires( struct session_expires *se )
{
	if ( se )
		pkg_free( se );
}


enum parse_sst_result
parse_session_expires_body( struct hdr_field *hf )
{
	register char *p = hf->body.s;
	int pos = 0;
	int len = hf->body.len;
	char *q;
	struct session_expires se = { 0, sst_refresher_unspecified };
	unsigned tok;

	if ( !p || len <= 0 ) {
		LOG( L_ERR, "parse_session_expires_body: no body for header field\n" );
		return parse_sst_header_not_found;
	}

	/* skip whitespace */
	for ( ; pos < len && is_space(*p); ++pos, ++p )
		/*nothing*/;

	/* collect a number */
	for ( q = p; pos < len && is_num(*q); ++pos, ++q )
		se.interval = se.interval*10/*radix*/ + (*q - '0');

	if ( q == p ) /*nothing parsed */ {
		LOG( L_ERR, "parse_session_expires_body: no expiry interval\n" );
		return parse_sst_no_value;
	}
	p = q;

	/* continue on with params */
	while ( pos < len ) {

		if ( *p == ';' ) {
			++p; ++pos;

			if ( pos + 4 < len ) {
				switch ( lower_4bytes(read_4bytes(p)) ) {
					case /*refr*/MAKE_4BYTES('r','e','f','r'):
						if ( pos + 9 <= len
							 && lower_4bytes(read_4bytes(p+4))
								== /*eshe*/MAKE_4BYTES('e','s','h','e')
							 && lower_byte(*(p+8)) == 'r'
							 && *(p+9) == '=' ) {
							tok = lower_3bytes( read_3bytes(p+10) );
							if ( tok == MAKE_3BYTES('u','a','c') ) {
								se.refresher = sst_refresher_uac;
								p += 13; pos += 13;
							}
							else if ( tok == MAKE_3BYTES('u','a','s') ) {
								se.refresher = sst_refresher_uas;
								p += 13; pos += 13;
							}
							else /* unrecognized refresher-param */ {
								LOG( L_ERR,
									 "parse_session_expires_body: "
									 "unrecognized refresher\n"
									 );
								return parse_sst_parse_error;
							}
						}
						else /* not "esher=" */ {
							/* there are no other se-params 
							   that start with "refr" */
							for ( ; pos < len && *p != ';'; ++pos, ++p )
								/*skip to ';'*/;
						}
						break;
					default:
						/* unrecognized se-param */
						for ( ; pos < len && *p != ';'; ++pos, ++p )
							/*skip to ';'*/;
						break;
				} /*switch*/
			} /* exist 4 bytes to check */
			else /* less than 4 bytes left */ {
				/* not enough text left for any of the recognized se-params */
				/* no other recognized se-param */
				for ( ; pos < len && *p != ';'; ++pos, ++p ) /*skip to ';'*/;
			}
		}
		else /* not ';' */ {
			LOG( L_ERR,
				 "parse_session_expires_body: "
				 "no semicolon separating se-params\n"
				 );
			return parse_sst_parse_error;
		} /* if ';' */
	} /* while */

	hf->parsed = malloc_session_expires();
	if ( !hf->parsed ) {
		LOG( L_ERR, "parse_session_expires_body: out of memory\n" );
		return parse_sst_out_of_mem;
	}
	*((struct session_expires *)hf->parsed) = se;

	return parse_sst_success;
}


enum parse_sst_result
parse_session_expires( struct sip_msg *msg, struct session_expires *se )
{
	enum parse_sst_result result;

	if ( msg->session_expires ) {
		if ( msg->session_expires->parsed == 0
			 && (result = parse_session_expires_body(msg->session_expires))
				!= parse_sst_success ) {
			return result;
		}
		if ( se ) {
			*se = *((struct session_expires *)msg->session_expires->parsed);
		}
		return parse_sst_success;
	}
	else {
		return parse_sst_header_not_found;
	}
}


enum parse_sst_result
parse_min_se_body( struct hdr_field *hf )
{
	int len = hf->body.len;
	char *p = hf->body.s;
	int pos = 0;
	unsigned int interval = 0;

	/* skip whitespace */
	for ( ; pos < len && is_space(*p); ++pos, ++p )
		/*nothing*/;
	if ( pos == len )
		return parse_sst_no_value;
	/* collect a number */
	for ( ; pos < len && is_num(*p); ++pos, ++p )
		interval = interval*10/*radix*/ + (*p - '0');
	/* skip whitespace */
	for ( ; pos < len && is_space(*p); ++pos, ++p )
		/*nothing*/;
	if ( pos != len ) /* shouldn't be any more junk */
		return parse_sst_parse_error;
	hf->parsed=(void*)(long)interval;
	return parse_sst_success;
}


enum parse_sst_result
parse_min_se( struct sip_msg *msg, unsigned int *min_se )
{
	enum parse_sst_result result;

	if ( msg->min_se ) {
		if ( msg->min_se->parsed == 0
			 && (result = parse_min_se_body(msg->min_se))
				!= parse_sst_success ) {
			return result;
		}
		if ( min_se ) {
			*min_se = (unsigned int)(long)msg->min_se->parsed;
		}
		return parse_sst_success;
	}
	else {
		return parse_sst_header_not_found;
	}
}