Browse code

forward_request splited

Bogdan-Andrei Iancu authored on 28/11/2001 23:25:35
Showing 4 changed files
... ...
@@ -22,6 +22,7 @@
22 22
 #include "data_lump.h"
23 23
 #include "ut.h"
24 24
 #include "mem.h"
25
+#include "msg_translator.h"
25 26
 
26 27
 #ifdef DEBUG_DMALLOC
27 28
 #include <dmalloc.h>
... ...
@@ -35,128 +36,20 @@
35 35
 #endif
36 36
 
37 37
 
38
-/* faster than inet_ntoa */
39
-static inline char* q_inet_itoa(unsigned long ip)
40
-{
41
-	static char q_inet_itoa_buf[16]; /* 123.567.901.345\0 */
42
-	unsigned char* p;
43
-	unsigned char a,b,c;  /* abc.def.ghi.jkl */
44
-	int offset;
45
-	int r;
46
-	p=(unsigned char*)&ip;
47
-
48
-	offset=0;
49
-	/* unrolled loops (faster)*/
50
-	for(r=0;r<3;r++){
51
-		a=p[r]/100;
52
-		c=p[r]%10;
53
-		b=p[r]%100/10;
54
-		if (a){
55
-			q_inet_itoa_buf[offset]=a+'0';
56
-			q_inet_itoa_buf[offset+1]=b+'0';
57
-			q_inet_itoa_buf[offset+2]=c+'0';
58
-			q_inet_itoa_buf[offset+3]='.';
59
-			offset+=4;
60
-		}else if (b){
61
-			q_inet_itoa_buf[offset]=b+'0';
62
-			q_inet_itoa_buf[offset+1]=c+'0';
63
-			q_inet_itoa_buf[offset+2]='.';
64
-			offset+=3;
65
-		}else{
66
-			q_inet_itoa_buf[offset]=c+'0';
67
-			q_inet_itoa_buf[offset+1]='.';
68
-			offset+=2;
69
-		}
70
-	}
71
-	/* last number */
72
-	a=p[r]/100;
73
-	c=p[r]%10;
74
-	b=p[r]%100/10;
75
-	if (a){
76
-		q_inet_itoa_buf[offset]=a+'0';
77
-		q_inet_itoa_buf[offset+1]=b+'0';
78
-		q_inet_itoa_buf[offset+2]=c+'0';
79
-		q_inet_itoa_buf[offset+3]=0;
80
-	}else if (b){
81
-		q_inet_itoa_buf[offset]=b+'0';
82
-		q_inet_itoa_buf[offset+1]=c+'0';
83
-		q_inet_itoa_buf[offset+2]=0;
84
-	}else{
85
-		q_inet_itoa_buf[offset]=c+'0';
86
-		q_inet_itoa_buf[offset+1]=0;
87
-	}
88
-	
89
-	return q_inet_itoa_buf;
90
-}
91
-		
92
-		
93
-
94
-		
95
-		
96
-	
97
-
98
-/* checks if ip is in host(name) and ?host(ip)=name? 
99
- * ip must be in network byte order!
100
- *  resolver = DO_DNS | DO_REV_DNS; if 0 no dns check is made
101
- * return 0 if equal */
102
-int check_address(unsigned long ip, char *name, int resolver)
103
-{
104
-	struct hostent* he;
105
-	int i;
106
-	
107
-	/* maybe we are lucky and name it's an ip */
108
-	if (strcmp(name, q_inet_itoa( /* *(struct in_addr *)&*/ip ))==0)
109
-		return 0;
110
-	if (resolver&DO_DNS){ 
111
-		DBG("check_address: doing dns lookup\n");
112
-		/* try all names ips */
113
-		he=gethostbyname(name);
114
-		for(i=0;he && he->h_addr_list[i];i++){
115
-			if (*(unsigned long*)he->h_addr_list[i]==ip)
116
-				return 0;
117
-		}
118
-	}
119
-	if (resolver&DO_REV_DNS){
120
-		DBG("check_address: doing rev. dns lookup\n");
121
-		print_ip(ip);
122
-		/* try reverse dns */
123
-		he=gethostbyaddr((char*)&ip, sizeof(ip), AF_INET);
124
-		if (he && (strcmp(he->h_name, name)==0))
125
-			return 0;
126
-		for (i=0; he && he->h_aliases[i];i++){
127
-			if (strcmp(he->h_aliases[i],name)==0)
128
-				return 0;
129
-		}
130
-	}
131
-	return -1;
132
-}
133
-
134
-
135 38
 
136 39
 int forward_request( struct sip_msg* msg, struct proxy_l * p)
137 40
 {
138
-	unsigned int len, new_len, via_len, received_len, uri_len;
139
-	char* line_buf;
140
-	char* received_buf;
141
-	char* tmp;
142
-	int tmp_len;
143
-	char* new_buf;
144
-	char* orig;
41
+	unsigned int len;
145 42
 	char* buf;
146
-	unsigned int offset, s_offset, size;
147 43
 	struct sockaddr_in* to;
148
-	unsigned long source_ip;
149
-	struct lump *t,*r;
150
-	struct lump* anchor;
151 44
 
152
-	orig=msg->orig;
153
-	buf=msg->buf;
154
-	len=msg->len;
155
-	source_ip=msg->src_ip;
156
-	received_len=0;
157
-	new_buf=0;
158
-	line_buf=0;
159
-	received_buf=0;
45
+
46
+	buf = build_buf_from_sip_request( msg, &len);
47
+	if (!buf){
48
+		LOG(L_ERR, "ERROR: forward_reply: building failed\n");
49
+		goto error;
50
+	}
51
+
160 52
 	to=0;
161 53
 	to=(struct sockaddr_in*)malloc(sizeof(struct sockaddr));
162 54
 	if (to==0){
... ...
@@ -164,261 +57,9 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
164 164
 		goto error;
165 165
 	}
166 166
 
167
-	line_buf=pkg_malloc(sizeof(char)*MAX_VIA_LINE_SIZE);
168
-	if (line_buf==0){
169
-		LOG(L_ERR, "ERROR: forward_request: out of memory\n");
170
-		goto error1;
171
-	}
172
-/*
173
-	via_len=snprintf(line_buf, MAX_VIA_LINE_SIZE, "Via: SIP/2.0/UDP %s:%d\r\n",
174
-						names[0], port_no);
175
-*/
176
-	via_len=MY_VIA_LEN+names_len[0]; /* space included in MY_VIA*/
177
-	if ((via_len+port_no_str_len+CRLF_LEN)<MAX_VIA_LINE_SIZE){
178
-		memcpy(line_buf, MY_VIA, MY_VIA_LEN);
179
-		memcpy(line_buf+MY_VIA_LEN, names[0], names_len[0]);
180
-		if (port_no!=SIP_PORT){
181
-			memcpy(line_buf+via_len, port_no_str, port_no_str_len);
182
-			via_len+=port_no_str_len;
183
-		}
184
-		memcpy(line_buf+via_len, CRLF, CRLF_LEN);
185
-		via_len+=CRLF_LEN;
186
-		line_buf[via_len]=0; /* null terminate the string*/
187
-	}else{
188
-		LOG(L_ERR, "forward_request: ERROR: via too long (%d)\n",
189
-				via_len);
190
-		goto error1;
191
-	}
192
-
193
-		
194
-	
195
-	/* check if received needs to be added */
196
-	if (check_address(source_ip, msg->via1->host.s, received_dns)!=0){
197
-		received_buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
198
-		if (received_buf==0){
199
-			LOG(L_ERR, "ERROR: forward_request: out of memory\n");
200
-			goto error1;
201
-		}
202
-		/*
203
-		received_len=snprintf(received_buf, MAX_RECEIVED_SIZE,
204
-								";received=%s", 
205
-								inet_ntoa(*(struct in_addr *)&source_ip));
206
-		*/
207
-		memcpy(received_buf, RECEIVED, RECEIVED_LEN);
208
-		tmp=q_inet_itoa( /* *(struct in_addr *)& */source_ip);
209
-		tmp_len=strlen(tmp);
210
-		received_len=RECEIVED_LEN+tmp_len;
211
-		memcpy(received_buf+RECEIVED_LEN, tmp, tmp_len);
212
-		received_buf[received_len]=0; /*null terminate it */
213
-	}
214
-	
215
-	/* add via header to the list */
216
-	/* try to add it before msg. 1st via */
217
-	/*add first via, as an anchor for second via*/
218
-	anchor=anchor_lump(&(msg->add_rm), msg->via1->hdr.s-buf, 0, HDR_VIA);
219
-	if (anchor==0) goto error;
220
-	if (insert_new_lump_before(anchor, line_buf, via_len, HDR_VIA)==0)
221
-		goto error;
222
-	/* if received needs to be added, add anchor after host and add it */
223
-	if (received_len){
224
-		if (msg->via1->params.s){
225
-				size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate 
226
-															  for ';' */
227
-		}else{
228
-				size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
229
-				if (msg->via1->port!=0){
230
-					size+=strlen(msg->via1->hdr.s+size+1)+1; /* +1 for ':'*/
231
-				}
232
-		}
233
-		anchor=anchor_lump(&(msg->add_rm),msg->via1->hdr.s-buf+size,0,
234
-				HDR_VIA);
235
-		if (anchor==0) goto error;
236
-		if (insert_new_lump_after(anchor, received_buf, received_len, HDR_VIA) 
237
-				==0 ) goto error;
238
-	}
239
-	
240
-	
241
-	/* compute new msg len and fix overlapping zones*/
242
-	new_len=len;
243
-	s_offset=0;
244
-	for(t=msg->add_rm;t;t=t->next){
245
-		for(r=t->before;r;r=r->before){
246
-			switch(r->op){
247
-				case LUMP_ADD:
248
-					new_len+=r->len;
249
-					break;
250
-				default:
251
-					/* only ADD allowed for before/after */
252
-					LOG(L_CRIT, "BUG:forward_request: invalid op for"
253
-								" data lump (%x)\n", r->op);
254
-			}
255
-		}
256
-		switch(t->op){
257
-			case LUMP_ADD:
258
-				new_len+=t->len;
259
-				break;
260
-			case LUMP_DEL:
261
-				/* fix overlapping deleted zones */
262
-				if (t->u.offset < s_offset){
263
-					/* change len */
264
-					if (t->len>s_offset-t->u.offset) 
265
-							t->len-=s_offset-t->u.offset;
266
-					else t->len=0;
267
-					t->u.offset=s_offset;
268
-				}
269
-				s_offset=t->u.offset+t->len;
270
-				new_len-=t->len;
271
-				break;
272
-			case LUMP_NOP:
273
-				/* fix offset if overlapping on a deleted zone */
274
-				if (t->u.offset < s_offset){
275
-					t->u.offset=s_offset;
276
-				}else
277
-					s_offset=t->u.offset;
278
-				/* do nothing */
279
-				break;
280
-			debug:
281
-				LOG(L_CRIT,"BUG:forward_request: invalid" 
282
-							" op for data lump (%x)\n", r->op);
283
-		}
284
-		for (r=t->after;r;r=r->after){
285
-			switch(r->op){
286
-				case LUMP_ADD:
287
-					new_len+=r->len;
288
-					break;
289
-				default:
290
-					/* only ADD allowed for before/after */
291
-					LOG(L_CRIT, "BUG:forward_request: invalid"
292
-								" op for data lump (%x)\n", r->op);
293
-			}
294
-		}
295
-	}
296
-	
297
-	
298
-	if (msg->new_uri.s){ 
299
-		uri_len=msg->new_uri.len; 
300
-		new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
301
-	}
302
-	new_buf=(char*)malloc(new_len+1);
303
-	if (new_buf==0){
304
-		LOG(L_ERR, "ERROR: forward_request: out of memory\n");
305
-		goto error;
306
-	}
307
-
308
-	offset=s_offset=0;
309
-	if (msg->new_uri.s){
310
-		/* copy message up to uri */
311
-		size=msg->first_line.u.request.uri.s-buf;
312
-		memcpy(new_buf, orig, size);
313
-		offset+=size;
314
-		s_offset+=size;
315
-		/* add our uri */
316
-		memcpy(new_buf+offset, msg->new_uri.s, uri_len);
317
-		offset+=uri_len;
318
-		s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
319
-	}
320
-/* copy msg adding/removing lumps */
321
-	for (t=msg->add_rm;t;t=t->next){
322
-		switch(t->op){
323
-			case LUMP_ADD:
324
-				/* just add it here! */
325
-				/* process before  */
326
-				for(r=t->before;r;r=r->before){
327
-					switch (r->op){
328
-						case LUMP_ADD:
329
-							/*just add it here*/
330
-							memcpy(new_buf+offset, r->u.value, r->len);
331
-							offset+=r->len;
332
-							break;
333
-						default:
334
-							/* only ADD allowed for before/after */
335
-							LOG(L_CRIT, "BUG:forward_request: invalid op for"
336
-									" data lump (%x)\n", r->op);
337
-								
338
-					}
339
-				}
340
-				/* copy "main" part */
341
-				memcpy(new_buf+offset, t->u.value, t->len);
342
-				offset+=t->len;
343
-				/* process after */
344
-				for(r=t->after;r;r=r->after){
345
-					switch (r->op){
346
-						case LUMP_ADD:
347
-							/*just add it here*/
348
-							memcpy(new_buf+offset, r->u.value, r->len);
349
-							offset+=r->len;
350
-							break;
351
-						default:
352
-							/* only ADD allowed for before/after */
353
-							LOG(L_CRIT, "BUG:forward_request: invalid op for"
354
-									" data lump (%x)\n", r->op);
355
-					}
356
-				}
357
-				break;
358
-			case LUMP_NOP:
359
-			case LUMP_DEL:
360
-				/* copy till offset */
361
-				if (s_offset>t->u.offset){
362
-					DBG("Warning: (%d) overlapped lumps offsets,"
363
-						" ignoring(%x, %x)\n", t->op, s_offset,t->u.offset);
364
-					/* this should've been fixed above (when computing len) */
365
-					/* just ignore it*/
366
-					break;
367
-				}
368
-				size=t->u.offset-s_offset;
369
-				if (size){
370
-					memcpy(new_buf+offset, orig+s_offset,size);
371
-					offset+=size;
372
-					s_offset+=size;
373
-				}
374
-				/* process before  */
375
-				for(r=t->before;r;r=r->before){
376
-					switch (r->op){
377
-						case LUMP_ADD:
378
-							/*just add it here*/
379
-							memcpy(new_buf+offset, r->u.value, r->len);
380
-							offset+=r->len;
381
-							break;
382
-						default:
383
-							/* only ADD allowed for before/after */
384
-							LOG(L_CRIT, "BUG:forward_request: invalid op for"
385
-									" data lump (%x)\n", r->op);
386
-								
387
-					}
388
-				}
389
-				/* process main (del only) */
390
-				if (t->op==LUMP_DEL){
391
-					/* skip len bytes from orig msg */
392
-					s_offset+=t->len;
393
-				}
394
-				/* process after */
395
-				for(r=t->after;r;r=r->after){
396
-					switch (r->op){
397
-						case LUMP_ADD:
398
-							/*just add it here*/
399
-							memcpy(new_buf+offset, r->u.value, r->len);
400
-							offset+=r->len;
401
-							break;
402
-						default:
403
-							/* only ADD allowed for before/after */
404
-							LOG(L_CRIT, "BUG:forward_request: invalid op for"
405
-									" data lump (%x)\n", r->op);
406
-					}
407
-				}
408
-				break;
409
-			default:
410
-					LOG(L_CRIT, "BUG: forward_request: unknown op (%x)\n",
411
-							t->op);
412
-		}
413
-	}
414
-	/* copy the rest of the message */
415
-	memcpy(new_buf+offset, orig+s_offset, len-s_offset);
416
-	new_buf[new_len]=0;
417
-
418 167
 	 /* send it! */
419
-	DBG("Sending:\n%s.\n", new_buf);
420
-	DBG("orig. len=%d, new_len=%d, via_len=%d, received_len=%d\n",
421
-			len, new_len, via_len, received_len);
168
+	DBG("Sending:\n%s.\n", buf);
169
+	DBG("orig. len=%d, new_len=%d\n", msg->len, len );
422 170
 
423 171
 	to->sin_family = AF_INET;
424 172
 	to->sin_port = (p->port)?htons(p->port):htons(SIP_PORT);
... ...
@@ -432,9 +73,9 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
432 432
 	to->sin_addr.s_addr=*((long*)p->host.h_addr_list[p->addr_idx]);
433 433
 
434 434
 	p->tx++;
435
-	p->tx_bytes+=new_len;
435
+	p->tx_bytes+=len;
436 436
 
437
-	if (udp_send(new_buf, new_len, (struct sockaddr*) to,
437
+	if (udp_send( buf, len, (struct sockaddr*) to,
438 438
 				sizeof(struct sockaddr_in))==-1){
439 439
 			p->errors++;
440 440
 			p->ok=0;
... ...
@@ -442,20 +83,17 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
442 442
 			update_fail_on_send;
443 443
 #endif
444 444
 			goto error;
445
-	} 
445
+	}
446 446
 #ifdef STATS
447 447
 	/* sent requests stats */
448 448
 	else update_sent_request( msg->first_line.u.request.method_value );
449 449
 #endif
450
-	free(new_buf);
450
+	free(buf);
451 451
 	free(to);
452 452
 	/* received_buf & line_buf will be freed in receiv_msg by free_lump_list*/
453 453
 	return 0;
454
-error1:
455
-	if (line_buf) pkg_free(line_buf);
456
-	if (received_buf) pkg_free(received_buf);
457 454
 error:
458
-	if (new_buf) free(new_buf);
455
+	if (buf) free(buf);
459 456
 	if (to) free(to);
460 457
 	return -1;
461 458
 }
... ...
@@ -478,7 +116,7 @@ int forward_reply(struct sip_msg* msg)
478 478
 #ifdef DNS_IP_HACK
479 479
 	int err;
480 480
 #endif
481
-	
481
+
482 482
 
483 483
 	orig=msg->orig;
484 484
 	buf=msg->buf;
... ...
@@ -516,9 +154,9 @@ int forward_reply(struct sip_msg* msg)
516 516
 		via_len+=msg->via1->hdr.len+1;
517 517
 	}
518 518
 	new_len=len-via_len;
519
-	
519
+
520 520
 	DBG(" old size: %d, new size: %d\n", len, new_len);
521
-	new_buf=(char*)malloc(new_len+1);/* +1 is for debugging 
521
+	new_buf=(char*)malloc(new_len+1);/* +1 is for debugging
522 522
 											(\0 to print it )*/
523 523
 	if (new_buf==0){
524 524
 		LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
... ...
@@ -531,10 +169,10 @@ int forward_reply(struct sip_msg* msg)
531 531
 	memcpy(new_buf+offset,orig+s_offset, len-s_offset);
532 532
 	 /* send it! */
533 533
 	DBG(" copied size: orig:%d, new: %d, rest: %d\n",
534
-			s_offset, offset, 
534
+			s_offset, offset,
535 535
 			len-s_offset );
536 536
 	DBG("Sending: to %s:%d, \n%s.\n",
537
-			msg->via2->host.s, 
537
+			msg->via2->host.s,
538 538
 			(unsigned short)msg->via2->port,
539 539
 			new_buf);
540 540
 
... ...
@@ -561,8 +199,8 @@ int forward_reply(struct sip_msg* msg)
561 561
 #ifdef DNS_IP_HACK
562 562
 	}
563 563
 #endif
564
-	if (udp_send(new_buf,new_len, (struct sockaddr*) to, 
565
-					sizeof(struct sockaddr_in))==-1) 
564
+	if (udp_send(new_buf,new_len, (struct sockaddr*) to,
565
+					sizeof(struct sockaddr_in))==-1)
566 566
 	{
567 567
 #ifdef STATS
568 568
 		update_fail_on_send;
... ...
@@ -11,8 +11,6 @@
11 11
 #include "proxy.h"
12 12
 
13 13
 
14
-int check_address(unsigned long ip, char *name, int resolver);
15
-
16 14
 int forward_request( struct sip_msg* msg,  struct proxy_l* p);
17 15
 
18 16
 int forward_reply( struct sip_msg* msg);
... ...
@@ -16,7 +16,6 @@ int relay_lowest_reply_upstream( struct cell *Trans , struct sip_msg *p_msg );
16 16
 int push_reply_from_uac_to_uas( struct sip_msg * , unsigned int );
17 17
 int t_cancel_branch(unsigned int branch); //TO DO
18 18
 
19
-int send_udp_to( char *buf, unsigned buflen, struct sockaddr_in*  to, unsigned tolen );
20 19
 
21 20
 void retransmission_handler( void *);
22 21
 void final_response_handler( void *);
... ...
@@ -40,10 +39,6 @@ int tm_startup()
40 40
    hash_table->timers[WT_TIMER_LIST].timeout_handler             = wait_handler;
41 41
    hash_table->timers[DELETE_LIST].timeout_handler                 = delete_handler;
42 42
 
43
-    /* creating socket for forwarding and retransmision*/
44
-    if ( (sock_fd=socket(AF_INET, SOCK_DGRAM,0))<0 )
45
-       return -1;
46
-
47 43
    return 0;
48 44
 }
49 45
 
... ...
@@ -270,7 +265,7 @@ int t_forward( struct sip_msg* p_msg , unsigned int dest_ip_param , unsigned int
270 270
    T->outbound_request[0]->max_retrans = (T->inbound_request->first_line.u.request.method_value==METHOD_INVITE) ? MAX_INVITE_RETR : MAX_NON_INVITE_RETR;
271 271
    T->outbound_request[0]->timeout         = RETR_T1;
272 272
    /* send the request */
273
-   send_udp_to( T->outbound_request[0]->buffer , T->outbound_request[0]->bufflen , &(T->outbound_request[0]->to) , sizeof(struct sockaddr_in) );
273
+   send_udp( T->outbound_request[0]->buffer , T->outbound_request[0]->bufflen , &(T->outbound_request[0]->to) , sizeof(struct sockaddr_in) );
274 274
 }
275 275
 
276 276
 
... ...
@@ -401,7 +396,7 @@ int t_retransmit_reply( struct sip_msg* p_msg )
401 401
    /* if no transaction exists or no reply to be resend -> out */
402 402
    if ( T  && T->inbound_response )
403 403
    {
404
-      send_udp_to( T->inbound_response->buffer , T->inbound_response->bufflen , &(T->inbound_response->to) , sizeof(struct sockaddr_in) );
404
+      send_udp( T->inbound_response->buffer , T->inbound_response->bufflen , &(T->inbound_response->to) , sizeof(struct sockaddr_in) );
405 405
       return 0;
406 406
    }
407 407
 
... ...
@@ -760,7 +755,7 @@ int t_build_and_send_ACK( struct cell *Trans, unsigned int branch)
760 760
    *(p++) = '\n';
761 761
 
762 762
    /* sends the ACK message to the same destination as the INVITE */
763
-   send_udp_to( ack_buf, p-ack_buf, &(T->outbound_request[branch]->to) , sizeof(struct sockaddr_in) );
763
+   send_udp( ack_buf, p-ack_buf, &(T->outbound_request[branch]->to) , sizeof(struct sockaddr_in) );
764 764
 
765 765
    /* free mem*/
766 766
    free( ack_buf );
... ...
@@ -771,23 +766,6 @@ int t_build_and_send_ACK( struct cell *Trans, unsigned int branch)
771 771
 
772 772
 
773 773
 
774
-/* sends a buffer as a datagram
775
-  */
776
-int send_udp_to( char *buf, unsigned buflen, struct sockaddr_in*  to, unsigned tolen )
777
-{
778
-   int res;
779
-
780
-    do
781
-    {
782
-       res = sendto(sock_fd, buf, buflen, 0, to, tolen);
783
-    }while( res==-1 && errno==EINTR );
784
-
785
-   return res;
786
-}
787
-
788
-
789
-
790
-
791 774
 
792 775
 
793 776
 
... ...
@@ -6,6 +6,7 @@
6 6
 #include <netdb.h>
7 7
 #include "../../msg_parser.h"
8 8
 #include "../../globals.h"
9
+#include "../../udp_server.h"
9 10
 
10 11
 struct s_table;
11 12
 struct timer;