Browse code

str2q implemented append_branch("sip:a@iptel.org", "0.23") implemented append_branch("sip:a@iptel.org", 230) removed (it was not very intuitive)

Jan Janak authored on 26/04/2004 17:11:55
Showing 5 changed files
... ...
@@ -1432,8 +1432,14 @@ cmd:		FORWARD LPAREN host RPAREN	{ $$=mk_action(	FORWARD_T,
1432 1432
 		| STRIP error { $$=0; yyerror("missing '(' or ')' ?"); }
1433 1433
 		| STRIP LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1434 1434
 														"number expected"); }
1435
-                | APPEND_BRANCH LPAREN STRING COMMA NUMBER RPAREN { $$=mk_action( APPEND_BRANCH_T,
1436
-										                        STRING_ST, NUMBER_ST, $3, (void *)$5) ; }
1435
+                | APPEND_BRANCH LPAREN STRING COMMA STRING RPAREN { 
1436
+		    {   qvalue_t q;
1437
+			if (str2q(&q, $5, strlen($5)) < 0) {
1438
+				yyerror("bad arqument, q value expected");
1439
+			}
1440
+			$$=mk_action(APPEND_BRANCH_T, STRING_ST, NUMBER_ST, $3, (void *)q); } 
1441
+		}
1442
+	
1437 1443
 		| APPEND_BRANCH LPAREN STRING RPAREN { $$=mk_action( APPEND_BRANCH_T,
1438 1444
 													STRING_ST, NUMBER_ST, $3, (void *)Q_UNSPECIFIED) ; }
1439 1445
 		| APPEND_BRANCH LPAREN RPAREN { $$=mk_action( APPEND_BRANCH_T,
... ...
@@ -86,6 +86,27 @@ int err2reason_phrase(
86 86
 			error_txt="Forking capacity exceeded";
87 87
 			*sip_error=-E_BAD_SERVER;
88 88
 			break;
89
+
90
+	        case E_Q_INV_CHAR:
91
+			error_txt="Invalid character in q parameter";
92
+			*sip_error=-E_BAD_REQ;
93
+			break;
94
+
95
+	        case E_Q_EMPTY:
96
+			error_txt="Empty q parameter";
97
+			*sip_error=-E_BAD_REQ;
98
+			break;;
99
+
100
+	        case E_Q_TOO_BIG:
101
+			error_txt="q parameter too big";
102
+			*sip_error=-E_BAD_REQ;
103
+			break;
104
+
105
+	        case E_Q_DEC_MISSING:
106
+			error_txt="Decimal part missing in q";
107
+			*sip_error=-E_BAD_REQ;
108
+			break;
109
+
89 110
 		case E_OUT_OF_MEM:
90 111
 		/* dont disclose lack of mem in release mode */
91 112
 #ifdef DEBUG
... ...
@@ -50,6 +50,12 @@
50 50
 /* invalid params */
51 51
 #define E_INVALID_PARAMS -14
52 52
 
53
+#define E_Q_INV_CHAR    -15 /* Invalid character in q */
54
+#define E_Q_EMPTY       -16 /* Empty q */
55
+#define E_Q_TOO_BIG     -17 /* q too big (> 1) */
56
+#define E_Q_DEC_MISSING -18 /* Decimal part missing */
57
+
58
+
53 59
 #define E_SEND		  -477
54 60
 /* unresolveable next-hop address */
55 61
 #define E_BAD_ADDRESS -478
56 62
new file mode 100644
... ...
@@ -0,0 +1,181 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Handling of the q value
5
+ *
6
+ * Copyright (C) 2004 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
+ * History
30
+ * ------
31
+ * 2004-04-25 created (janakj)
32
+ */
33
+
34
+#include "error.h"
35
+#include "qvalue.h"
36
+
37
+
38
+/*
39
+ * Convert string representation of q parameter in qvalue_t
40
+ */
41
+int str2q(qvalue_t* q, char* s, int len)
42
+{
43
+	int i, digits;
44
+
45
+	     /* States and equivalent regular expressions of input */
46
+	enum {
47
+		ST_START,   /* (SPC|TAB)* */
48
+		ST_0,       /* 0+ */
49
+		ST_1,       /* 1 */
50
+		ST_0_PT,    /* 0*\. */
51
+		ST_1_PT,    /* 1\. */
52
+		ST_1_PT_0,  /* 1\.0+ */
53
+		ST_0_PT_N   /* 0*\.[0-9]+ */
54
+	} state = ST_START;
55
+
56
+	if (!q || !s) {
57
+		return E_INVALID_PARAMS;
58
+	}
59
+
60
+	digits = 1;
61
+	for(i = 0; i < len; i++) {
62
+		switch(state) {
63
+		case ST_START:
64
+			switch(s[i]) {
65
+			case ' ':
66
+			case '\t':
67
+				break;
68
+
69
+			case '0':
70
+				*q = 0;
71
+				state = ST_0;
72
+				break;
73
+
74
+			case '1':
75
+				*q = 1000;
76
+				state = ST_1;
77
+				break;
78
+
79
+			case '.':
80
+				state = ST_0_PT;
81
+				break;
82
+
83
+			default:
84
+				return E_Q_INV_CHAR;
85
+			}
86
+			break;
87
+
88
+		case ST_0:
89
+			switch(s[i]) {
90
+			case '0':
91
+				break;
92
+
93
+			case '.':
94
+				state = ST_0_PT;
95
+				break;
96
+
97
+			case '1':
98
+				*q = 1000;
99
+				state = ST_1;
100
+				break;
101
+
102
+			default:
103
+				if (s[i] >= '2' && s[i] <= '9') {
104
+					return E_Q_TOO_BIG;
105
+				} else {
106
+					return E_Q_INV_CHAR;
107
+				}
108
+			}
109
+			break;
110
+
111
+		case ST_1:
112
+			if (s[i] == '.') {
113
+				state = ST_1_PT;
114
+				break;
115
+			} else {
116
+				if (s[i] >= '0' && s[i] <= '9') {
117
+					return E_Q_TOO_BIG;
118
+				} else {
119
+					return E_Q_INV_CHAR;
120
+				}
121
+			}
122
+			break;
123
+
124
+		case ST_0_PT:
125
+			if (s[i] >= '0' && s[i] <= '9') {
126
+				*q =  s[i] - '0';
127
+				state = ST_0_PT_N;
128
+			} else {
129
+				return E_Q_INV_CHAR;
130
+			}
131
+			break;
132
+
133
+		case ST_1_PT:
134
+			if (s[i] == '0') {
135
+				state = ST_1_PT_0;
136
+			} else {
137
+				if (s[i] >= '1' && s[i] <= '9') {
138
+					return E_Q_TOO_BIG;
139
+				} else {
140
+					return E_Q_INV_CHAR;
141
+				}
142
+			}
143
+			break;
144
+
145
+		case ST_1_PT_0:
146
+			if (s[i] == '0') {
147
+				break;
148
+			} else {
149
+				if (s[i] >= '1' && s[i] <= '9') {
150
+					return E_Q_TOO_BIG;
151
+				} else {
152
+					return E_Q_INV_CHAR;
153
+				}
154
+			}
155
+			break;
156
+
157
+		case ST_0_PT_N:
158
+			if (s[i] >= '0' && s[i] <= '9') {
159
+				if (digits < 3) {
160
+					*q = *q * 10 + s[i] - '0';
161
+					digits++;
162
+				}
163
+			} else {
164
+				return E_Q_INV_CHAR;
165
+			}
166
+			break;
167
+		}
168
+	}
169
+
170
+	switch(state) {
171
+	case ST_START:
172
+		return E_Q_EMPTY;
173
+		
174
+	case ST_0_PT:
175
+	case ST_1_PT:
176
+		return E_Q_DEC_MISSING;
177
+		
178
+	default:
179
+		return 0;
180
+	}
181
+}
... ...
@@ -129,4 +129,11 @@ static inline size_t print_q(char* p, qvalue_t q)
129 129
 	return Q_PREFIX_LEN + 3;
130 130
 }
131 131
 
132
+
133
+/*
134
+ * Convert string representation of q parameter in qvalue_t
135
+ */
136
+int str2q(qvalue_t* q, char* s, int len);
137
+
138
+
132 139
 #endif /* _QVALUE_H */