... | ... |
@@ -33,8 +33,14 @@ |
33 | 33 |
|
34 | 34 |
|
35 | 35 |
#define TCP_BUF_SIZE 65535 |
36 |
-enum {TCP_REQ_INIT, TCP_REQ_OK, TCP_READ_ERROR, TCP_REQ_OVERRUN }; |
|
37 |
-enum {H_PARSING, H_LF, H_LFCR, H_BODY }; |
|
36 |
+enum { TCP_REQ_INIT, TCP_REQ_OK, TCP_READ_ERROR, TCP_REQ_OVERRUN, |
|
37 |
+ TCP_REQ_BAD_LEN }; |
|
38 |
+enum { H_SKIP, H_LF, H_LFCR, H_BODY, H_STARTWS, |
|
39 |
+ H_CONT_LEN1, H_CONT_LEN2, H_CONT_LEN3, H_CONT_LEN4, H_CONT_LEN5, |
|
40 |
+ H_CONT_LEN6, H_CONT_LEN7, H_CONT_LEN8, H_CONT_LEN9, H_CONT_LEN10, |
|
41 |
+ H_CONT_LEN11, H_CONT_LEN12, H_CONT_LEN13, H_L_COLON, |
|
42 |
+ H_CONT_LEN_BODY, H_CONT_LEN_BODY_PARSE |
|
43 |
+ }; |
|
38 | 44 |
|
39 | 45 |
struct tcp_req{ |
40 | 46 |
struct tcp_req* next; |
... | ... |
@@ -44,6 +50,10 @@ struct tcp_req{ |
44 | 50 |
char* pos; /* current position in buf */ |
45 | 51 |
char* parsed; /* last parsed position */ |
46 | 52 |
char* body; /* body position */ |
53 |
+ int content_len; |
|
54 |
+ int has_content_len; /* 1 if content_length was parsed ok*/ |
|
55 |
+ int complete; /* 1 if one req has been fully read, 0 otherwise*/ |
|
56 |
+ int bytes_to_go; /* how many bytes we have still to read from the body*/ |
|
47 | 57 |
int error; |
48 | 58 |
int state; |
49 | 59 |
}; |
... | ... |
@@ -55,7 +65,7 @@ struct tcp_req{ |
55 | 65 |
(r)->fd=(f); \ |
56 | 66 |
(r)->parsed=(r)->pos=(r)->buf; \ |
57 | 67 |
(r)->error=TCP_REQ_OK;\ |
58 |
- (r)->state=H_PARSING; \ |
|
68 |
+ (r)->state=H_STARTWS; \ |
|
59 | 69 |
}while(0) |
60 | 70 |
|
61 | 71 |
|
... | ... |
@@ -193,6 +193,7 @@ void tcp_main_loop() |
193 | 193 |
struct tcp_connection* tcpconn; |
194 | 194 |
long response[2]; |
195 | 195 |
int state; |
196 |
+ int bytes; |
|
196 | 197 |
socklen_t su_len; |
197 | 198 |
|
198 | 199 |
/*init */ |
... | ... |
@@ -271,8 +272,23 @@ void tcp_main_loop() |
271 | 272 |
for (r=0; r<tcp_children_no && n; r++){ |
272 | 273 |
if (FD_ISSET(tcp_children[r].s, &sel_set)){ |
273 | 274 |
n--; |
274 |
- /* errno==EINTR !!! todo*/ |
|
275 |
- read(tcp_children[r].s, response, sizeof(response)); |
|
275 |
+ /* errno==EINTR !!! TODO*/ |
|
276 |
+read_again: |
|
277 |
+ bytes=read(tcp_children[r].s, response, sizeof(response)); |
|
278 |
+ if (bytes==0){ |
|
279 |
+ /* EOF -> bad, chidl has died */ |
|
280 |
+ LOG(L_CRIT, "BUG: tcp_main_loop: dead child %d\n", r); |
|
281 |
+ /* terminating everybody */ |
|
282 |
+ exit(-1); |
|
283 |
+ }else if (bytes<0){ |
|
284 |
+ if (errno==EINTR) goto read_again; |
|
285 |
+ else{ |
|
286 |
+ LOG(L_CRIT, "ERROR: tcp_main_loop: read from child: " |
|
287 |
+ " %s\n", strerror(errno)); |
|
288 |
+ /* try to continue ? */ |
|
289 |
+ } |
|
290 |
+ } |
|
291 |
+ |
|
276 | 292 |
DBG("tcp__main_loop: read response= %lx, %ld\n", |
277 | 293 |
response[0], response[1]); |
278 | 294 |
tcp_children[r].busy=0; |
... | ... |
@@ -40,8 +40,11 @@ |
40 | 40 |
#include <unistd.h> |
41 | 41 |
|
42 | 42 |
|
43 |
+#include "dprint.h" |
|
43 | 44 |
#include "tcp_conn.h" |
44 | 45 |
#include "pass_fd.h" |
46 |
+#include "globals.h" |
|
47 |
+#include "receive.h" |
|
45 | 48 |
|
46 | 49 |
|
47 | 50 |
#define q_memchr memchr |
... | ... |
@@ -80,24 +83,76 @@ again: |
80 | 83 |
|
81 | 84 |
|
82 | 85 |
|
83 |
-/* reads all headers (until double crlf), |
|
86 |
+/* reads all headers (until double crlf), & parses the content-length header |
|
87 |
+ * (WARNING: highly ineficient, tries to reuse receive_msg but will parse |
|
88 |
+ * all the header names twice [once here & once in receive_msg]; a more |
|
89 |
+ * speed eficient version will result in either major code duplication or |
|
90 |
+ * major changes to the receive code - TODO) |
|
84 | 91 |
* returns number of bytes read & sets r->state & r->body |
85 | 92 |
* when either r->body!=0 or r->state==H_BODY => |
86 | 93 |
* all headers have been read. It should be called in a while loop. |
87 | 94 |
* returns < 0 if error or 0 if EOF */ |
88 | 95 |
int tcp_read_headers(struct tcp_req *r) |
89 | 96 |
{ |
90 |
- int bytes; |
|
97 |
+ int bytes, remaining; |
|
91 | 98 |
char *p; |
92 | 99 |
|
100 |
+ #define crlf_default_skip_case \ |
|
101 |
+ case '\n': \ |
|
102 |
+ r->state=H_LF; \ |
|
103 |
+ break; \ |
|
104 |
+ default: \ |
|
105 |
+ r->state=H_SKIP |
|
106 |
+ |
|
107 |
+ #define content_len_beg_case \ |
|
108 |
+ case ' ': \ |
|
109 |
+ case '\t': \ |
|
110 |
+ if (!r->has_content_len) r->state=H_STARTWS; \ |
|
111 |
+ else r->state=H_SKIP; \ |
|
112 |
+ /* not interested if we already found one */ \ |
|
113 |
+ break; \ |
|
114 |
+ case 'C': \ |
|
115 |
+ case 'c': \ |
|
116 |
+ if(!r->has_content_len) r->state=H_CONT_LEN1; \ |
|
117 |
+ else r->state=H_SKIP; \ |
|
118 |
+ break |
|
119 |
+ |
|
120 |
+ #define change_state(upper, lower, newstate)\ |
|
121 |
+ switch(*p){ \ |
|
122 |
+ case upper: \ |
|
123 |
+ case lower: \ |
|
124 |
+ r->state=(newstate); break; \ |
|
125 |
+ crlf_default_skip_case; \ |
|
126 |
+ } |
|
127 |
+ |
|
128 |
+ #define change_state_case(state0, upper, lower, newstate)\ |
|
129 |
+ case state0: \ |
|
130 |
+ change_state(upper, lower, newstate); \ |
|
131 |
+ p++; \ |
|
132 |
+ break |
|
133 |
+ |
|
134 |
+ |
|
135 |
+ |
|
93 | 136 |
bytes=tcp_read(r); |
94 | 137 |
if (bytes<=0) return bytes; |
95 | 138 |
p=r->parsed; |
96 | 139 |
|
97 |
- while(p<r->pos && r->state!=H_BODY){ |
|
140 |
+ while(p<r->pos && r->error==TCP_REQ_OK){ |
|
98 | 141 |
switch(r->state){ |
99 |
- case H_PARSING: |
|
100 |
- /* find lf */ |
|
142 |
+ case H_BODY: /* read the body*/ |
|
143 |
+ remaining=r->pos-p; |
|
144 |
+ if (remaining>r->bytes_to_go) remaining=r->bytes_to_go; |
|
145 |
+ r->bytes_to_go-=remaining; |
|
146 |
+ p+=remaining; |
|
147 |
+ if (r->bytes_to_go==0){ |
|
148 |
+ r->complete=1; |
|
149 |
+ goto skip; |
|
150 |
+ } |
|
151 |
+ break; |
|
152 |
+ |
|
153 |
+ case H_SKIP: |
|
154 |
+ /* find lf, we are in this state if we are not interested |
|
155 |
+ * in anything till end of line*/ |
|
101 | 156 |
p=q_memchr(p, '\n', r->pos-r->parsed); |
102 | 157 |
if (p){ |
103 | 158 |
p++; |
... | ... |
@@ -109,31 +164,146 @@ int tcp_read_headers(struct tcp_req *r) |
109 | 164 |
|
110 | 165 |
case H_LF: |
111 | 166 |
/* terminate on LF CR LF or LF LF */ |
112 |
- if (*p=='\r'){ |
|
113 |
- r->state=H_LFCR; |
|
114 |
- }else if (*p=='\n'){ |
|
115 |
- /* found LF LF */ |
|
116 |
- r->state=H_BODY; |
|
117 |
- r->body=p+1; |
|
118 |
- }else r->state=H_PARSING; |
|
167 |
+ switch (*p){ |
|
168 |
+ case '\r': |
|
169 |
+ r->state=H_LFCR; |
|
170 |
+ break; |
|
171 |
+ case '\n': |
|
172 |
+ /* found LF LF */ |
|
173 |
+ r->state=H_BODY; |
|
174 |
+ if (r->has_content_len){ |
|
175 |
+ r->body=p+1; |
|
176 |
+ r->bytes_to_go=r->content_len; |
|
177 |
+ if (r->bytes_to_go==0){ |
|
178 |
+ r->complete=1; |
|
179 |
+ goto skip; |
|
180 |
+ } |
|
181 |
+ }else{ |
|
182 |
+ r->error=TCP_REQ_BAD_LEN; |
|
183 |
+ } |
|
184 |
+ break; |
|
185 |
+ content_len_beg_case; |
|
186 |
+ default: |
|
187 |
+ r->state=H_SKIP; |
|
188 |
+ } |
|
119 | 189 |
p++; |
120 | 190 |
break; |
121 |
- |
|
122 | 191 |
case H_LFCR: |
123 | 192 |
if (*p=='\n'){ |
124 | 193 |
/* found LF CR LF */ |
125 | 194 |
r->state=H_BODY; |
126 |
- r->body=p+1; |
|
127 |
- }else r->state=H_PARSING; |
|
195 |
+ if (r->has_content_len){ |
|
196 |
+ r->body=p+1; |
|
197 |
+ r->bytes_to_go=r->content_len; |
|
198 |
+ if (r->bytes_to_go==0){ |
|
199 |
+ r->complete=1; |
|
200 |
+ goto skip; |
|
201 |
+ } |
|
202 |
+ }else{ |
|
203 |
+ r->error=TCP_REQ_BAD_LEN; |
|
204 |
+ } |
|
205 |
+ }else r->state=H_SKIP; |
|
128 | 206 |
p++; |
129 | 207 |
break; |
130 | 208 |
|
209 |
+ case H_STARTWS: |
|
210 |
+ switch (*p){ |
|
211 |
+ content_len_beg_case; |
|
212 |
+ crlf_default_skip_case; |
|
213 |
+ } |
|
214 |
+ p++; |
|
215 |
+ break; |
|
216 |
+ |
|
217 |
+ change_state_case(H_CONT_LEN1, 'O', 'o', H_CONT_LEN2); |
|
218 |
+ change_state_case(H_CONT_LEN2, 'N', 'n', H_CONT_LEN3); |
|
219 |
+ change_state_case(H_CONT_LEN3, 'T', 't', H_CONT_LEN4); |
|
220 |
+ change_state_case(H_CONT_LEN4, 'E', 'e', H_CONT_LEN5); |
|
221 |
+ change_state_case(H_CONT_LEN5, 'N', 'n', H_CONT_LEN6); |
|
222 |
+ change_state_case(H_CONT_LEN6, 'T', 't', H_CONT_LEN7); |
|
223 |
+ change_state_case(H_CONT_LEN7, '-', '_', H_CONT_LEN8); |
|
224 |
+ change_state_case(H_CONT_LEN8, 'L', 'l', H_CONT_LEN9); |
|
225 |
+ change_state_case(H_CONT_LEN9, 'E', 'e', H_CONT_LEN10); |
|
226 |
+ change_state_case(H_CONT_LEN10, 'N', 'n', H_CONT_LEN11); |
|
227 |
+ change_state_case(H_CONT_LEN11, 'G', 'g', H_CONT_LEN12); |
|
228 |
+ change_state_case(H_CONT_LEN12, 'T', 't', H_CONT_LEN13); |
|
229 |
+ change_state_case(H_CONT_LEN13, 'H', 'h', H_L_COLON); |
|
230 |
+ |
|
231 |
+ case H_L_COLON: |
|
232 |
+ switch(*p){ |
|
233 |
+ case ' ': |
|
234 |
+ case '\t': |
|
235 |
+ break; /* skip space */ |
|
236 |
+ case ':': |
|
237 |
+ r->state=H_CONT_LEN_BODY; |
|
238 |
+ break; |
|
239 |
+ crlf_default_skip_case; |
|
240 |
+ }; |
|
241 |
+ p++; |
|
242 |
+ break; |
|
243 |
+ |
|
244 |
+ case H_CONT_LEN_BODY: |
|
245 |
+ switch(*p){ |
|
246 |
+ case ' ': |
|
247 |
+ case '\t': |
|
248 |
+ break; /* eat space */ |
|
249 |
+ case '0': |
|
250 |
+ case '1': |
|
251 |
+ case '2': |
|
252 |
+ case '3': |
|
253 |
+ case '4': |
|
254 |
+ case '5': |
|
255 |
+ case '6': |
|
256 |
+ case '7': |
|
257 |
+ case '8': |
|
258 |
+ case '9': |
|
259 |
+ r->state=H_CONT_LEN_BODY_PARSE; |
|
260 |
+ r->content_len=(*p-'0'); |
|
261 |
+ break; |
|
262 |
+ /*FIXME: content lenght on different lines ! */ |
|
263 |
+ crlf_default_skip_case; |
|
264 |
+ } |
|
265 |
+ p++; |
|
266 |
+ break; |
|
267 |
+ |
|
268 |
+ case H_CONT_LEN_BODY_PARSE: |
|
269 |
+ switch(*p){ |
|
270 |
+ case '0': |
|
271 |
+ case '1': |
|
272 |
+ case '2': |
|
273 |
+ case '3': |
|
274 |
+ case '4': |
|
275 |
+ case '5': |
|
276 |
+ case '6': |
|
277 |
+ case '7': |
|
278 |
+ case '8': |
|
279 |
+ r->content_len=r->content_len*10+(*p-'0'); |
|
280 |
+ break; |
|
281 |
+ case '\r': |
|
282 |
+ case ' ': |
|
283 |
+ case '\t': /* FIXME: check if line contains only WS */ |
|
284 |
+ r->state=H_SKIP; |
|
285 |
+ r->has_content_len=1; |
|
286 |
+ break; |
|
287 |
+ case '\n': |
|
288 |
+ /* end of line, parse succesfull */ |
|
289 |
+ r->state=H_LF; |
|
290 |
+ r->has_content_len=1; |
|
291 |
+ break; |
|
292 |
+ default: |
|
293 |
+ LOG(L_ERR, "ERROR: tcp_read_headers: bad " |
|
294 |
+ "Content-Length header value, unexpected " |
|
295 |
+ "char %c in state %d\n", *p, r->state); |
|
296 |
+ r->state=H_SKIP; /* try to find another?*/ |
|
297 |
+ } |
|
298 |
+ p++; |
|
299 |
+ break; |
|
300 |
+ |
|
131 | 301 |
default: |
132 | 302 |
fprintf(stderr, "BUG: unexpected state %d\n", r->state); |
133 | 303 |
abort(); |
134 | 304 |
} |
135 | 305 |
} |
136 |
- |
|
306 |
+skip: |
|
137 | 307 |
r->parsed=p; |
138 | 308 |
return bytes; |
139 | 309 |
} |
... | ... |
@@ -184,7 +354,7 @@ void tcp_receive_loop(int unix_sock) |
184 | 354 |
|
185 | 355 |
|
186 | 356 |
again: |
187 |
- while(req.body==0){ |
|
357 |
+ while(req.complete==0 && req.error==TCP_REQ_OK){ |
|
188 | 358 |
bytes=tcp_read_headers(&req); |
189 | 359 |
/* if timeout state=0; goto end__req; */ |
190 | 360 |
fprintf(stderr, "read= %d bytes, parsed=%d, state=%d, error=%d\n", |
... | ... |
@@ -201,17 +371,31 @@ void tcp_receive_loop(int unix_sock) |
201 | 371 |
} |
202 | 372 |
|
203 | 373 |
} |
374 |
+ if (req.error!=TCP_REQ_OK){ |
|
375 |
+ fprintf(stderr, "bad request, state=%d, error=%d\n", |
|
376 |
+ req.state, req.error); |
|
377 |
+ state=-1; |
|
378 |
+ goto end_req; |
|
379 |
+ } |
|
204 | 380 |
fprintf(stderr, "end of header part\n"); |
205 | 381 |
fprintf(stderr, "headers:\n%.*s.\n",req.body-req.buf, req.buf); |
382 |
+ if (req.has_content_len){ |
|
383 |
+ fprintf(stderr, "content-length= %d\n", req.content_len); |
|
384 |
+ fprintf(stderr, "body:\n%.*s\n", req.content_len, req.body); |
|
385 |
+ }else{ |
|
386 |
+ req.error=TCP_REQ_BAD_LEN; |
|
387 |
+ fprintf(stderr, "content length not present or unparsable\n"); |
|
388 |
+ state=-1; |
|
389 |
+ goto end_req; |
|
390 |
+ } |
|
206 | 391 |
|
207 |
- /* just debugging*/ |
|
392 |
+ /* if we are here everything is nice and ok*/ |
|
208 | 393 |
state=0; |
209 |
- goto end_req; |
|
210 |
- /* parse headers ... */ |
|
211 |
- |
|
212 |
- /* get body */ |
|
213 |
- |
|
214 |
- /* copy request */ |
|
394 |
+ /* just for debugging use sendipv4 as receiving socket */ |
|
395 |
+ DBG("calling receive_msg(%p, %d, %p)\n", |
|
396 |
+ req.buf, (int)(req.parsed-req.buf), &sendipv4->su); |
|
397 |
+ bind_address=sendipv4; |
|
398 |
+ receive_msg(req.buf, req.parsed-req.buf, &sendipv4->su); |
|
215 | 399 |
|
216 | 400 |
/* prepare for next request */ |
217 | 401 |
size=req.pos-req.body; |
... | ... |
@@ -221,7 +405,9 @@ void tcp_receive_loop(int unix_sock) |
221 | 405 |
req.parsed=req.buf; |
222 | 406 |
req.body=0; |
223 | 407 |
req.error=TCP_REQ_OK; |
224 |
- req.state=H_PARSING; |
|
408 |
+ req.state=H_STARTWS; |
|
409 |
+ req.complete=req.content_len=req.has_content_len=0; |
|
410 |
+ req.bytes_to_go=0; |
|
225 | 411 |
|
226 | 412 |
/* process last req. */ |
227 | 413 |
|