Browse code

Date Header Field parsing funcions and macros

First release

Gergely Kovacs authored on 08/03/2007 15:19:47
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,41 @@
1
+/* 
2
+ * $Id$ 
3
+ *
4
+ * Date Header Field Name Parsing Macros
5
+ *
6
+ * Copyright (c) 2007 iptelorg GmbH
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
+
30
+
31
+#ifndef CASE_DATE_H
32
+#define CASE_DATE_H
33
+
34
+
35
+#define date_CASE          \
36
+     hdr->type = HDR_DATE_T; \
37
+     p += 4;               \
38
+     goto dc_end
39
+
40
+
41
+#endif /* CASE_DATE_H */
0 42
new file mode 100644
... ...
@@ -0,0 +1,182 @@
1
+/*
2
+ * $Id$ 
3
+ *
4
+ * Copyright (c) 2007 iptelorg GmbH
5
+ *
6
+ * This file is part of ser, a free SIP server.
7
+ *
8
+ * ser is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * For a license to use the ser software under conditions
14
+ * other than those described here, or to purchase support for this
15
+ * software, please contact iptel.org by e-mail at the following addresses:
16
+ *    info@iptel.org
17
+ *
18
+ * ser is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License
24
+ * along with this program; if not, write to the Free Software
25
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
+ */
27
+
28
+
29
+#include <string.h>
30
+#include "parse_date.h"
31
+#include "parse_def.h"
32
+#include "parser_f.h"  /* eat_space_end and so on */
33
+#include "../mem/mem.h"
34
+
35
+/*
36
+ * Parse Date header field
37
+ */
38
+
39
+#define READ(val) \
40
+(*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16) + (*(val + 3) << 24))
41
+
42
+/*
43
+ * Converts a RFC 1123 formatted date string to stuct tm
44
+ */
45
+int rfc1123totm (char *stime, struct tm *ttm ) {
46
+	char *ptime = stime;
47
+	unsigned int uval;
48
+	int ires;
49
+
50
+	int char2int (char *p, int *t){
51
+		if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') return -1;
52
+		*t = (*p - '0')*10 + *(p + 1) - '0';
53
+
54
+		return 0;
55
+	}
56
+
57
+	uval = READ(ptime);
58
+	ptime+=4;
59
+	switch (uval) {
60
+		/* Sun, */
61
+		case 0x2c6e7553: ttm->tm_wday = 0; break;
62
+		/* Mon, */
63
+		case 0x2c6e6f4d: ttm->tm_wday = 1; break;
64
+		/* Tue, */
65
+		case 0x2c657554: ttm->tm_wday = 2; break;
66
+		/* Wed, */
67
+		case 0x2c646557: ttm->tm_wday = 3; break;
68
+		/* Thu, */
69
+		case 0x2c756854: ttm->tm_wday = 4; break;
70
+		/* Fri, */
71
+		case 0x2c697246: ttm->tm_wday = 5; break;
72
+		/* Sat, */
73
+		case 0x2c746153: ttm->tm_wday = 6; break;
74
+		default: return -2;
75
+	}
76
+
77
+	if (*(ptime++)!=' ') return -3;
78
+
79
+
80
+	if (char2int(ptime,&ttm->tm_mday) || ttm->tm_mday > 31) return -4;
81
+	ptime+=2;
82
+
83
+	if (*(ptime++) != ' ') return -5;
84
+
85
+	uval = READ(ptime);
86
+	ptime+=4;
87
+	switch (uval) {
88
+		/* Jan, */
89
+		case 0x206e614a: ttm->tm_mon = 0; break;
90
+		/* Feb, */
91
+		case 0x20626546: ttm->tm_mon = 1; break;
92
+		/* Mar, */
93
+		case 0x2072614d: ttm->tm_mon = 2; break;
94
+		/* Apr, */
95
+		case 0x20727041: ttm->tm_mon = 3; break;
96
+		/* May, */
97
+		case 0x2079614d: ttm->tm_mon = 4; break;
98
+		/* Jun, */
99
+		case 0x206e754a: ttm->tm_mon = 5; break;
100
+		/* Jul, */
101
+		case 0x206c754a: ttm->tm_mon = 6; break;
102
+		/* Aug, */
103
+		case 0x20677541: ttm->tm_mon = 7; break;
104
+		/* Sep, */
105
+		case 0x20706553: ttm->tm_mon = 8; break;
106
+		/* Oct, */
107
+		case 0x2074634f: ttm->tm_mon = 9; break;
108
+		/* Nov, */
109
+		case 0x20766f4e: ttm->tm_mon = 10; break;
110
+		/* Dec, */
111
+		case 0x20636544: ttm->tm_mon = 11; break;
112
+		default: return -6;
113
+	}
114
+
115
+	if (char2int(ptime,&ires)) return -7;
116
+	ptime+=2;
117
+	if (char2int(ptime,&ttm->tm_year)) return -8;
118
+	ptime+=2;
119
+	ttm->tm_year+=(ires-19)*100;
120
+
121
+	if (*(ptime++) != ' ') return -9;
122
+
123
+	if (char2int(ptime,&ttm->tm_hour) || ttm->tm_hour > 23) return -10;
124
+	ptime+=2;
125
+	if (*(ptime++) != ':') return -11;
126
+
127
+	if (char2int(ptime,&ttm->tm_min) || ttm->tm_min > 59) return -12;
128
+	ptime+=2;
129
+	if (*(ptime++) != ':') return -13;
130
+
131
+	if (char2int(ptime,&ttm->tm_sec) || ttm->tm_sec > 59) return -14;
132
+	ptime+=2;
133
+
134
+	/* " GMT" */
135
+	if (memcmp(ptime," GMT", strlen(" GMT"))) return -15;
136
+
137
+	return 0;
138
+}
139
+
140
+char* parse_date(char *buffer, char *end, struct date_body *db)
141
+{
142
+	char *p;
143
+	int i1;
144
+
145
+	db->error=PARSE_ERROR;
146
+	p = buffer;
147
+
148
+	/* check whether enough characters are available */
149
+	for (i1 = 0; i1 < RFC1123DATELENGTH || p[i1] == '\n' || p + i1 >= end;i1++);
150
+	if (i1 < RFC1123DATELENGTH)
151
+		goto error;
152
+
153
+	if (rfc1123totm(buffer,&db->date))
154
+		goto error;
155
+
156
+	p+=RFC1123DATELENGTH;
157
+
158
+	p=eat_lws_end(p, end);
159
+	/*check if the header ends here*/
160
+	if (p>=end) {
161
+		LOG(L_ERR, "ERROR: parse_date: strange EoHF\n");
162
+		goto error;
163
+	}
164
+	if (*p=='\r' && p+1<end && *(p+1)=='\n') {
165
+		db->error=PARSE_OK;
166
+		return p+2;
167
+	}
168
+	if (*p=='\n') {
169
+		db->error=PARSE_OK;
170
+		return p+1;
171
+	}
172
+	LOG(L_ERR, "ERROR: Date EoL expected\n");
173
+error:
174
+	LOG(L_ERR,"ERROR: parse_date: parse error\n");
175
+	return p;
176
+}
177
+
178
+
179
+void free_date(struct date_body *db)
180
+{
181
+	pkg_free(db);
182
+}
0 183
new file mode 100644
... ...
@@ -0,0 +1,58 @@
1
+/*
2
+ * $Id$ 
3
+ *
4
+ * Copyright (c) 2007 iptelorg GmbH
5
+ *
6
+ * This file is part of ser, a free SIP server.
7
+ *
8
+ * ser is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * For a license to use the ser software under conditions
14
+ * other than those described here, or to purchase support for this
15
+ * software, please contact iptel.org by e-mail at the following addresses:
16
+ *    info@iptel.org
17
+ *
18
+ * ser is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License
24
+ * along with this program; if not, write to the Free Software
25
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
+ */
27
+
28
+
29
+#ifndef PARSE_DATE
30
+#define PARSE_DATE
31
+
32
+#include <time.h>
33
+
34
+#define RFC1123DATELENGTH	29
35
+
36
+struct date_body{
37
+	int error;  /* Error code */
38
+	struct tm date;
39
+};
40
+
41
+
42
+/* casting macro for accessing DATE body */
43
+#define get_date(p_msg) ((struct date_body*)(p_msg)->date->parsed)
44
+
45
+
46
+/*
47
+ * Parse Date header field
48
+ */
49
+char* parse_date(char *buf, char *end, struct date_body *db);
50
+
51
+
52
+/*
53
+ * Free all associated memory
54
+ */
55
+void free_date(struct date_body *db);
56
+
57
+
58
+#endif