Browse code

parser seems to work

Andrei Pelinescu-Onciul authored on 04/09/2001 01:41:39
Showing 18 changed files
1 1
deleted file mode 100644
2 2
Binary files a/.cfg_parser.c.swp and /dev/null differ
3 3
deleted file mode 100644
4 4
Binary files a/.cfg_parser.h.swp and /dev/null differ
5 5
deleted file mode 100644
6 6
Binary files a/.main.c.swp and /dev/null differ
7 7
deleted file mode 100644
8 8
Binary files a/.msg_parser.c.swp and /dev/null differ
9 9
deleted file mode 100644
10 10
Binary files a/.msg_parser.h.swp and /dev/null differ
11 11
deleted file mode 100644
12 12
Binary files a/.parser_f.c.swp and /dev/null differ
13 13
deleted file mode 100644
14 14
Binary files a/.parser_f.h.swp and /dev/null differ
15 15
deleted file mode 100644
16 16
Binary files a/.route.c.swp and /dev/null differ
17 17
deleted file mode 100644
18 18
Binary files a/.route.h.swp and /dev/null differ
19 19
deleted file mode 100644
20 20
Binary files a/.sip_router.cfg.swp and /dev/null differ
21 21
deleted file mode 100644
22 22
Binary files a/.test.c.swp and /dev/null differ
23 23
new file mode 100644
... ...
@@ -0,0 +1,137 @@
0
+/*
1
+ * $Id$
2
+ */
3
+
4
+
5
+#include <string.h>
6
+
7
+#include "forward.h"
8
+#include "msg_parser.h"
9
+#include "route.h"
10
+#include "dprint.h"
11
+
12
+#define MAX_VIA_LINE_SIZE      240
13
+#define MAX_RECEIVED_SIZE  57
14
+
15
+#define our_address "dorian.fokus.gmd.de"
16
+#define our_port 1234
17
+
18
+
19
+int forward_request(char * orig, char* buf, 
20
+					 unsigned int len,
21
+					 struct sip_msg* msg,
22
+					 struct route_elem* re)
23
+{
24
+	unsigned int new_len, via_len, received_len;
25
+	char line_buf[MAX_VIA_LINE_SIZE];
26
+	char received_buf[MAX_RECEIVED_SIZE];
27
+	char* new_buf;
28
+	int offset, s_offset, size;
29
+
30
+	received_len=0;
31
+	printf("0\n");
32
+
33
+	via_len=snprintf(line_buf, MAX_VIA_LINE_SIZE, "Via: SIP/2.0/UDP %s:%d\r\n",
34
+						our_address, our_port);
35
+	/* check if received needs to be added */
36
+	/* if check_address(source_ip, msg->via1.host) */
37
+	received_len=snprintf(received_buf, MAX_RECEIVED_SIZE, ";received=%s",
38
+							"10.11.12.13");
39
+	
40
+	new_len=len+via_len+received_len;
41
+	new_buf=(char*)malloc(new_len+1);
42
+	if (new_buf==0){
43
+		DPrint("ERROR: forward_request: out of memory\n");
44
+		goto error;
45
+	}
46
+	printf("1\n");
47
+ /* copy msg till first via */
48
+ 	offset=s_offset=0;
49
+ 	size=msg->via1.hdr-buf;
50
+ 	memcpy(new_buf, orig, size);
51
+	offset+=size;
52
+	s_offset+=size;
53
+	printf("2\n");
54
+ /* add our via */
55
+ 	memcpy(new_buf+offset, line_buf, via_len);
56
+	offset+=via_len;
57
+	printf("3\n");
58
+ /* modify original via if neccesarry (received=...)*/
59
+ 	if (received_len){
60
+		if (msg->via1.params){
61
+				size= msg->via1.params-msg->via1.hdr-1; /*compensate for ';' */
62
+		}else{
63
+				size= msg->via1.host-msg->via1.hdr+strlen(msg->via1.host);
64
+				if (msg->via1.port!=0){
65
+					size+=strlen(msg->via1.hdr+size+1);
66
+				}
67
+		}
68
+		memcpy(new_buf+offset, orig+s_offset, 
69
+								size);
70
+		offset+=size;
71
+		s_offset+=size;
72
+		printf("4\n");
73
+		memcpy(new_buf+offset, received_buf, received_len);
74
+		printf("5\n");
75
+		offset+=received_len;
76
+	}
77
+ 	/* copy the rest of the msg */
78
+ 	memcpy(new_buf+offset, orig+s_offset, len-s_offset);
79
+	printf("6\n");
80
+	new_buf[new_len]=0;
81
+
82
+	 /* send it! */
83
+	 printf("Sending:\n%s.\n", new_buf);
84
+	 
85
+	return 0;
86
+error:
87
+	return -1;
88
+
89
+}
90
+
91
+
92
+
93
+/* removes first via & sends msg to the second */
94
+int forward_reply(char * orig, char* buf, 
95
+					 unsigned int len,
96
+					 struct sip_msg* msg)
97
+{
98
+
99
+
100
+	unsigned int new_len, via_len;
101
+	char* new_buf;
102
+	int offset, s_offset, size;
103
+
104
+
105
+	/* we must remove first via */
106
+	via_len=msg->via1.size;
107
+	size=msg->via1.hdr-buf;
108
+	if (msg->via1.next){
109
+		via_len-=strlen(msg->via1.hdr)+1; /* +1 from ':' */
110
+		size+=strlen(msg->via1.hdr)+1;
111
+	}
112
+	new_len=len-size;
113
+	printf("r1\n");
114
+	new_buf=(char*)malloc(new_len);
115
+	if (new_buf==0){
116
+		DPrint("ERROR: forward_reply: out of memory\n");
117
+		goto error;
118
+	}
119
+	printf("r2\n");
120
+	memcpy(new_buf, orig, size);
121
+	offset=size;
122
+	s_offset=size+via_len;
123
+	printf("r3\n");
124
+	memcpy(new_buf+offset,orig+s_offset, len-s_offset);
125
+	printf("r4\n");
126
+	 /* send it! */
127
+	printf("Sending: to %s:%d, \n%s.\n",
128
+			msg->via2.host, 
129
+			(unsigned short)msg->via2.port,
130
+			new_buf);
131
+	
132
+	return 0;
133
+
134
+error:
135
+	return -1;
136
+}
0 137
new file mode 100644
... ...
@@ -0,0 +1,18 @@
0
+/*
1
+ *  $Id$
2
+ */
3
+
4
+
5
+#ifndef forward_h
6
+#define forward_h
7
+
8
+#include "msg_parser.h"
9
+#include "route.h"
10
+
11
+int forward_request(char * orig, char* buf, unsigned int len,
12
+					 struct sip_msg* msg,  struct route_elem* re);
13
+
14
+int forward_reply(char * orig, char* buf, unsigned int len, 
15
+					struct sip_msg* msg);
16
+
17
+#endif
... ...
@@ -12,6 +12,24 @@
12 12
 #define CFG_FILE "./sip_router.cfg"
13 13
 
14 14
 
15
+/* debuging function */
16
+
17
+void receive_stdin_loop()
18
+{
19
+	#define BSIZE 1024
20
+	char buf[BSIZE+1];
21
+	int len;
22
+	
23
+	while(1){
24
+		len=fread(buf,1,BSIZE,stdin);
25
+		buf[len+1]=0;
26
+		receive_msg(buf, len);
27
+		printf("-------------------------\n");
28
+	}
29
+}
30
+
31
+
32
+
15 33
 int main(int argc, char** argv)
16 34
 {
17 35
 
... ...
@@ -40,10 +58,12 @@ int main(int argc, char** argv)
40 40
 
41 41
 
42 42
 
43
-	/* start other processes/threads ? */
43
+	/* start/init other processes/threads ? */
44 44
 
45 45
 	/* receive loop */
46 46
 
47
+	receive_stdin_loop();
48
+
47 49
 
48 50
 error:
49 51
 	return -1;
... ...
@@ -5,14 +5,18 @@
5 5
  *
6 6
  */
7 7
 
8
-#include "msg_parser.h"
9
-#include "string.h"
8
+#include <string.h>
10 9
 
10
+#include "msg_parser.h"
11 11
 #include "parser_f.h"
12 12
 #include "dprint.h"
13 13
 
14 14
 
15 15
 
16
+#define DEBUG
17
+
18
+
19
+
16 20
 /* parses the first line, returns pointer to  next line  & fills fl;
17 21
    also  modifies buffer (to avoid extra copy ops) */
18 22
 char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
... ...
@@ -313,3 +317,145 @@ error:
313 313
 	return tmp;
314 314
 }
315 315
 
316
+
317
+
318
+/* returns 0 if ok, -1 for errors */
319
+int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
320
+{
321
+
322
+	char *tmp, *bar;
323
+	char* rest;
324
+	char* first_via;
325
+	char* second_via;
326
+	struct msg_start fl;
327
+	struct hdr_field hf;
328
+	struct via_body vb1, vb2;
329
+	int offset;
330
+	int r;
331
+
332
+	
333
+	/* eat crlf from the beginning */
334
+	for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
335
+			tmp-buf < len ; tmp++);
336
+	offset=tmp-buf;
337
+	rest=parse_first_line(tmp, len-offset, &fl);
338
+	offset+=rest-tmp;
339
+	tmp=rest;
340
+	switch(fl.type){
341
+		case SIP_INVALID:
342
+			DPrint("invalid message\n");
343
+			goto error;
344
+			break;
345
+		case SIP_REQUEST:
346
+			DPrint("SIP Request:\n");
347
+			DPrint(" method:  <%s>\n",fl.u.request.method);
348
+			DPrint(" uri:     <%s>\n",fl.u.request.uri);
349
+			DPrint(" version: <%s>\n",fl.u.request.version);
350
+			break;
351
+		case SIP_REPLY:
352
+			DPrint("SIP Reply  (status):\n");
353
+			DPrint(" version: <%s>\n",fl.u.reply.version);
354
+			DPrint(" status:  <%s>\n",fl.u.reply.status);
355
+			DPrint(" reason:  <%s>\n",fl.u.reply.reason);
356
+			break;
357
+		default:
358
+			DPrint("unknown type %d\n",fl.type);
359
+	}
360
+	
361
+	/*find first Via: */
362
+	hf.type=HDR_ERROR;
363
+	first_via=0;
364
+	second_via=0;
365
+	do{
366
+		rest=get_hdr_field(tmp, len-offset, &hf);
367
+		offset+=rest-tmp;
368
+		switch (hf.type){
369
+			case HDR_ERROR:
370
+				DPrint("ERROR: bad header  field\n");
371
+				goto  error;
372
+			case HDR_EOH: 
373
+				goto skip;
374
+			case HDR_VIA:
375
+				if (first_via==0){
376
+						first_via=hf.body;
377
+						vb1.hdr=hf.name;
378
+						/* replace cr/lf with space in first via */
379
+						for (bar=first_via;(first_via) && (*bar);bar++)
380
+							if ((*bar=='\r')||(*bar=='\n'))	*bar=' ';
381
+				#ifdef DEBUG
382
+						printf("first via: <%s>\n", first_via);
383
+				#endif
384
+						bar=parse_via_body(first_via, strlen(first_via), &vb1);
385
+						if (vb1.error!=VIA_PARSE_OK){
386
+							DPrint("ERROR: parsing via body: %s\n", first_via);
387
+							goto error;
388
+						}
389
+						vb1.size=bar-first_via+first_via-vb1.hdr;
390
+						
391
+						/* compact via */
392
+						if (vb1.next) {
393
+							second_via=vb1.next;
394
+							/* not interested in the rest of the header */
395
+							goto skip;
396
+						}
397
+				}else if (second_via==0){
398
+							second_via=hf.body;
399
+							vb2.hdr=hf.name;
400
+							goto skip;
401
+				}
402
+				break;
403
+		}
404
+	#ifdef DEBUG
405
+		printf("header field type %d, name=<%s>, body=<%s>\n",
406
+			hf.type, hf.name, hf.body);
407
+	#endif
408
+		tmp=rest;
409
+	}while(hf.type!=HDR_EOH && rest-buf < len);
410
+
411
+skip:
412
+	/* replace cr/lf with space in the second via */
413
+	for (tmp=second_via;(second_via) && (*tmp);tmp++)
414
+		if ((*tmp=='\r')||(*tmp=='\n'))	*tmp=' ';
415
+
416
+	if (second_via) {
417
+		tmp=parse_via_body(second_via, strlen(second_via), &vb2);
418
+		if (vb2.error!=VIA_PARSE_OK){
419
+			DPrint("ERROR: parsing via body: %s\n", second_via);
420
+			goto error;
421
+		}
422
+		vb2.size=tmp-second_via;
423
+		if (vb2.hdr) vb2.size+=second_via-vb2.hdr;
424
+	}
425
+	
426
+
427
+#ifdef DEBUG
428
+	/* dump parsed data */
429
+	printf(" first  via: <%s/%s/%s> <%s:%d>",
430
+			vb1.name, vb1.version, vb1.transport, vb1.host, vb1.port);
431
+	if (vb1.params) printf(";<%s>", vb1.params);
432
+	if (vb1.comment) printf(" <%s>", vb1.comment);
433
+	printf ("\n");
434
+	if (second_via){
435
+		printf(" second via: <%s/%s/%s> <%s:%d>",
436
+				vb2.name, vb2.version, vb2.transport, vb2.host, vb2.port);
437
+		if (vb2.params) printf(";<%s>", vb2.params);
438
+		if (vb2.comment) printf(" <%s>", vb2.comment);
439
+		printf ("\n");
440
+	}
441
+#endif
442
+	
443
+	/* copy data into msg */
444
+	memcpy(&(msg->first_line), &fl, sizeof(struct msg_start));
445
+	memcpy(&(msg->via1), &vb1, sizeof(struct via_body));
446
+	memcpy(&(msg->via2), &vb2, sizeof(struct via_body));
447
+
448
+#ifdef DEBUG
449
+	printf ("exiting parse_msg\n");
450
+#endif
451
+
452
+	return 0;
453
+	
454
+error:
455
+	return -1;
456
+}
457
+
... ...
@@ -48,6 +48,7 @@ struct hdr_field{   /* format: name':' body */
48 48
 
49 49
 struct via_body{  /* format: name/version/transport host:port;params comment */
50 50
 	int error;
51
+	char *hdr;   /* contains "Via" or "v" */
51 52
 	char* name;
52 53
 	char* version;
53 54
 	char* transport;
... ...
@@ -55,9 +56,16 @@ struct via_body{  /* format: name/version/transport host:port;params comment */
55 55
 	int port;
56 56
 	char* params;
57 57
 	char* comment;
58
+	int size;    /* full size, including hdr */
58 59
 	char* next; /* pointer to next via body string if compact via or null */
59 60
 };
60 61
 
62
+struct sip_msg{
63
+	struct msg_start first_line;
64
+	struct via_body via1;
65
+	struct via_body via2;
66
+};
67
+
61 68
 
62 69
 
63 70
 char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl);
... ...
@@ -65,11 +73,8 @@ char* get_hdr_field(char *buffer, unsigned int len, struct hdr_field*  hdr_f);
65 65
 int field_name(char *s);
66 66
 char* parse_hostport(char* buf, char** host, short int* port);
67 67
 char* parse_via_body(char* buffer,unsigned int len, struct via_body * vb);
68
-
69
-
68
+int parse_msg(char* buf, unsigned int len, struct sip_msg* msg);
70 69
 
71 70
 
72 71
 
73 72
 #endif
74
- 
75
-
76 73
new file mode 100644
... ...
@@ -0,0 +1,80 @@
0
+/* 
1
+ *$Id$
2
+ */
3
+
4
+#include <string.h>
5
+
6
+#include "receive.h"
7
+#include "dprint.h"
8
+#include "route.h"
9
+#include "msg_parser.h"
10
+#include "forward.h"
11
+
12
+
13
+int receive_msg(char* buf, unsigned int len)
14
+{
15
+	struct sip_msg msg;
16
+	struct route_elem *re;
17
+	char * orig;
18
+
19
+	/* make a copy of the message */
20
+	orig=(char*) malloc(len);
21
+	if (orig==0){
22
+		DPrint("ERROR: memory allocation failure\n");
23
+		goto error;
24
+	}
25
+	memcpy(orig, buf, len);
26
+	
27
+	if (parse_msg(buf,len, &msg)!=0){
28
+		goto error;
29
+	}
30
+	
31
+	if (msg.first_line.type==SIP_REQUEST){
32
+		/* sanity checks */
33
+		if (msg.via1.error!=VIA_PARSE_OK){
34
+			/* no via, send back error ? */
35
+			goto skip;
36
+		}
37
+		/* check if neccesarry to add receive? */
38
+		
39
+		/* find route */
40
+		re=route_match(  msg.first_line.u.request.method,
41
+						 msg.first_line.u.request.uri,
42
+						 &rlist
43
+					  );
44
+		if (re==0){
45
+			/* no route found, send back error msg? */
46
+			DPrint("WARNING: no route found!\n");
47
+			goto skip;
48
+		}
49
+		re->tx++;
50
+		/* send msg */
51
+		forward_request(orig, buf, len, &msg, re);
52
+		DPrint(" found route to: %s\n", re->host.h_name);
53
+	}else if (msg.first_line.type==SIP_REPLY){
54
+		/* sanity checks */
55
+		if (msg.via1.error!=VIA_PARSE_OK){
56
+			/* no via, send back error ? */
57
+			goto skip;
58
+		}
59
+		if (msg.via2.error!=VIA_PARSE_OK){
60
+			/* no second via => error? */
61
+			goto skip;
62
+		}
63
+		/* check if via1 == us */
64
+		
65
+		/* send the msg */
66
+		forward_reply(orig, buf, len, &msg);
67
+		DPrint(" reply forwarded to %s:%d\n", 
68
+					msg.via2.host,
69
+					(unsigned short) msg.via2.port
70
+				);
71
+	}
72
+skip:
73
+	free(orig);
74
+	return 0;
75
+error:
76
+	return -1;
77
+
78
+}
79
+
0 80
new file mode 100644
... ...
@@ -0,0 +1,12 @@
0
+/*
1
+ * $Id$
2
+ */
3
+
4
+
5
+#ifndef receive_h
6
+#define receive_h
7
+
8
+int receive_msg(char* buf, unsigned int len);
9
+
10
+
11
+#endif