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 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Handling of the q value
4
+ *
5
+ * Copyright (C) 2004 FhG FOKUS
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ *
28
+ * History
29
+ * ------
30
+ * 2004-04-25 created (janakj)
31
+ */
32
+
33
+#include "error.h"
34
+#include "qvalue.h"
35
+
36
+
37
+/*
38
+ * Convert string representation of q parameter in qvalue_t
39
+ */
40
+int str2q(qvalue_t* q, char* s, int len)
41
+{
42
+	int i, digits;
43
+
44
+	     /* States and equivalent regular expressions of input */
45
+	enum {
46
+		ST_START,   /* (SPC|TAB)* */
47
+		ST_0,       /* 0+ */
48
+		ST_1,       /* 1 */
49
+		ST_0_PT,    /* 0*\. */
50
+		ST_1_PT,    /* 1\. */
51
+		ST_1_PT_0,  /* 1\.0+ */
52
+		ST_0_PT_N   /* 0*\.[0-9]+ */
53
+	} state = ST_START;
54
+
55
+	if (!q || !s) {
56
+		return E_INVALID_PARAMS;
57
+	}
58
+
59
+	digits = 1;
60
+	for(i = 0; i < len; i++) {
61
+		switch(state) {
62
+		case ST_START:
63
+			switch(s[i]) {
64
+			case ' ':
65
+			case '\t':
66
+				break;
67
+
68
+			case '0':
69
+				*q = 0;
70
+				state = ST_0;
71
+				break;
72
+
73
+			case '1':
74
+				*q = 1000;
75
+				state = ST_1;
76
+				break;
77
+
78
+			case '.':
79
+				state = ST_0_PT;
80
+				break;
81
+
82
+			default:
83
+				return E_Q_INV_CHAR;
84
+			}
85
+			break;
86
+
87
+		case ST_0:
88
+			switch(s[i]) {
89
+			case '0':
90
+				break;
91
+
92
+			case '.':
93
+				state = ST_0_PT;
94
+				break;
95
+
96
+			case '1':
97
+				*q = 1000;
98
+				state = ST_1;
99
+				break;
100
+
101
+			default:
102
+				if (s[i] >= '2' && s[i] <= '9') {
103
+					return E_Q_TOO_BIG;
104
+				} else {
105
+					return E_Q_INV_CHAR;
106
+				}
107
+			}
108
+			break;
109
+
110
+		case ST_1:
111
+			if (s[i] == '.') {
112
+				state = ST_1_PT;
113
+				break;
114
+			} else {
115
+				if (s[i] >= '0' && s[i] <= '9') {
116
+					return E_Q_TOO_BIG;
117
+				} else {
118
+					return E_Q_INV_CHAR;
119
+				}
120
+			}
121
+			break;
122
+
123
+		case ST_0_PT:
124
+			if (s[i] >= '0' && s[i] <= '9') {
125
+				*q =  s[i] - '0';
126
+				state = ST_0_PT_N;
127
+			} else {
128
+				return E_Q_INV_CHAR;
129
+			}
130
+			break;
131
+
132
+		case ST_1_PT:
133
+			if (s[i] == '0') {
134
+				state = ST_1_PT_0;
135
+			} else {
136
+				if (s[i] >= '1' && s[i] <= '9') {
137
+					return E_Q_TOO_BIG;
138
+				} else {
139
+					return E_Q_INV_CHAR;
140
+				}
141
+			}
142
+			break;
143
+
144
+		case ST_1_PT_0:
145
+			if (s[i] == '0') {
146
+				break;
147
+			} else {
148
+				if (s[i] >= '1' && s[i] <= '9') {
149
+					return E_Q_TOO_BIG;
150
+				} else {
151
+					return E_Q_INV_CHAR;
152
+				}
153
+			}
154
+			break;
155
+
156
+		case ST_0_PT_N:
157
+			if (s[i] >= '0' && s[i] <= '9') {
158
+				if (digits < 3) {
159
+					*q = *q * 10 + s[i] - '0';
160
+					digits++;
161
+				}
162
+			} else {
163
+				return E_Q_INV_CHAR;
164
+			}
165
+			break;
166
+		}
167
+	}
168
+
169
+	switch(state) {
170
+	case ST_START:
171
+		return E_Q_EMPTY;
172
+		
173
+	case ST_0_PT:
174
+	case ST_1_PT:
175
+		return E_Q_DEC_MISSING;
176
+		
177
+	default:
178
+		return 0;
179
+	}
180
+}
... ...
@@ -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 */