rem: received_buf=malloc(sizeof(char)*MAX_RECEIVED_SIZE);
add: received_buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
context: int forward_request( struct sip_msg* msg, struct proxy_l * p){	unsigned int len, new_len, via_len, received_len, uri_len;	char* line_buf;	char* received_buf;	char* tmp;	int tmp_len;	char* new_buf;	char* orig;	char* buf;	unsigned int offset, s_offset, size;	struct sockaddr_in* to;	unsigned long source_ip;	struct lump *t,*r;	struct lump* anchor;	orig=msg->orig;	buf=msg->buf;	len=msg->len;	source_ip=msg->src_ip;	received_len=0;	new_buf=0;	line_buf=0;	received_buf=0;	to=0;	to=(struct sockaddr_in*)malloc(sizeof(struct sockaddr));	if (to==0){		LOG(L_ERR, "ERROR: forward_reply: out of memory\n");		goto error;	}	line_buf=malloc(sizeof(char)*MAX_VIA_LINE_SIZE);	if (line_buf==0){		LOG(L_ERR, "ERROR: forward_request: out of memory\n");		goto error1;	}/*	via_len=snprintf(line_buf, MAX_VIA_LINE_SIZE, "Via: SIP/2.0/UDP %s:%d\r\n",						names[0], port_no);*/	via_len=MY_VIA_LEN+names_len[0]; /* space included in MY_VIA*/	if ((via_len+port_no_str_len+CRLF_LEN)<MAX_VIA_LINE_SIZE){		memcpy(line_buf, MY_VIA, MY_VIA_LEN);		memcpy(line_buf+MY_VIA_LEN, names[0], names_len[0]);		if (port_no!=SIP_PORT){			memcpy(line_buf+via_len, port_no_str, port_no_str_len);			via_len+=port_no_str_len;		}		memcpy(line_buf+via_len, CRLF, CRLF_LEN);		via_len+=CRLF_LEN;		line_buf[via_len]=0; /* null terminate the string*/	}else{		LOG(L_ERR, "forward_request: ERROR: via too long (%d)\n",				via_len);		goto error1;	}				/* check if received needs to be added */	if (check_address(source_ip, msg->via1->host.s, received_dns)!=0){		received_buf=malloc(sizeof(char)*MAX_RECEIVED_SIZE);		if (received_buf==0){			LOG(L_ERR, "ERROR: forward_request: out of memory\n");			goto error1;		}		/*		received_len=snprintf(received_buf, MAX_RECEIVED_SIZE,								";received=%s", 								inet_ntoa(*(struct in_addr *)&source_ip));		*/		memcpy(received_buf, RECEIVED, RECEIVED_LEN);		tmp=q_inet_itoa( /* *(struct in_addr *)& */source_ip);		tmp_len=strlen(tmp);		received_len=RECEIVED_LEN+tmp_len;		memcpy(received_buf+RECEIVED_LEN, tmp, tmp_len);		received_buf[received_len]=0; /*null terminate it */	}		/* add via header to the list */	/* try to add it before msg. 1st via */	/*add first via, as an anchor for second via*/	anchor=anchor_lump(&(msg->add_rm), msg->via1->hdr.s-buf, 0, HDR_VIA);	if (anchor==0) goto error;	if (insert_new_lump_before(anchor, line_buf, via_len, HDR_VIA)==0)		goto error;	/* if received needs to be added, add anchor after host and add it */	if (received_len){		if (msg->via1->params.s){				size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate 															  for ';' */		}else{				size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;				if (msg->via1->port!=0){					size+=strlen(msg->via1->hdr.s+size+1)+1; /* +1 for ':'*/				}		}		anchor=anchor_lump(&(msg->add_rm),msg->via1->hdr.s-buf+size,0,				HDR_VIA);		if (anchor==0) goto error;		if (insert_new_lump_after(anchor, received_buf, received_len, HDR_VIA) 				==0 ) goto error;	}			/* compute new msg len and fix overlapping zones*/	new_len=len;	s_offset=0;	for(t=msg->add_rm;t;t=t->next){		for(r=t->before;r;r=r->before){			switch(r->op){				case LUMP_ADD:					new_len+=r->len;					break;				default:					/* only ADD allowed for before/after */					LOG(L_CRIT, "BUG:forward_request: invalid op for"								" data lump (%x)\n", r->op);			}		}		switch(t->op){			case LUMP_ADD:				new_len+=t->len;				break;			case LUMP_DEL:				/* fix overlapping deleted zones */				if (t->u.offset < s_offset){					/* change len */					if (t->len>s_offset-t->u.offset) 							t->len-=s_offset-t->u.offset;					else t->len=0;					t->u.offset=s_offset;				}				s_offset=t->u.offset+t->len;				new_len-=t->len;				break;			case LUMP_NOP:				/* fix offset if overlapping on a deleted zone */				if (t->u.offset < s_offset){					t->u.offset=s_offset;				}else					s_offset=t->u.offset;				/* do nothing */				break;			debug:				LOG(L_CRIT,"BUG:forward_request: invalid" 							" op for data lump (%x)\n", r->op);		}		for (r=t->after;r;r=r->after){			switch(r->op){				case LUMP_ADD:					new_len+=r->len;					break;				default:					/* only ADD allowed for before/after */					LOG(L_CRIT, "BUG:forward_request: invalid"								" op for data lump (%x)\n", r->op);			}		}	}			if (msg->new_uri.s){ 		uri_len=msg->new_uri.len; 		new_len=new_len-msg->first_line.u.request.uri.len+uri_len;	}	new_buf=(char*)malloc(new_len+1);	if (new_buf==0){		LOG(L_ERR, "ERROR: forward_request: out of memory\n");		goto error;	}	offset=s_offset=0;	if (msg->new_uri.s){		/* copy message up to uri */		size=msg->first_line.u.request.uri.s-buf;		memcpy(new_buf, orig, size);		offset+=size;		s_offset+=size;		/* add our uri */		memcpy(new_buf+offset, msg->new_uri.s, uri_len);		offset+=uri_len;		s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */	}/* copy msg adding/removing lumps */	for (t=msg->add_rm;t;t=t->next){		switch(t->op){			case LUMP_ADD:				/* just add it here! */				/* process before  */				for(r=t->before;r;r=r->before){					switch (r->op){						case LUMP_ADD:							/*just add it here*/							memcpy(new_buf+offset, r->u.value, r->len);							offset+=r->len;							break;						default:							/* only ADD allowed for before/after */							LOG(L_CRIT, "BUG:forward_request: invalid op for"									" data lump (%x)\n", r->op);													}				}				/* copy "main" part */				memcpy(new_buf+offset, t->u.value, t->len);				offset+=t->len;				/* process after */				for(r=t->after;r;r=r->after){					switch (r->op){						case LUMP_ADD:							/*just add it here*/							memcpy(new_buf+offset, r->u.value, r->len);							offset+=r->len;							break;						default:							/* only ADD allowed for before/after */							LOG(L_CRIT, "BUG:forward_request: invalid op for"									" data lump (%x)\n", r->op);					}				}				break;			case LUMP_NOP:			case LUMP_DEL:				/* copy till offset */				if (s_offset>t->u.offset){					DBG("Warning: (%d) overlapped lumps offsets,"						" ignoring(%x, %x)\n", t->op, s_offset,t->u.offset);					/* this should've been fixed above (when computing len) */					/* just ignore it*/					break;				}				size=t->u.offset-s_offset;				if (size){					memcpy(new_buf+offset, orig+s_offset,size);					offset+=size;					s_offset+=size;				}				/* process before  */				for(r=t->before;r;r=r->before){					switch (r->op){						case LUMP_ADD:							/*just add it here*/							memcpy(new_buf+offset, r->u.value, r->len);							offset+=r->len;							break;						default:							/* only ADD allowed for before/after */							LOG(L_CRIT, "BUG:forward_request: invalid op for"									" data lump (%x)\n", r->op);													}				}				/* process main (del only) */				if (t->op==LUMP_DEL){					/* skip len bytes from orig msg */					s_offset+=t->len;				}				/* process after */				for(r=t->after;r;r=r->after){					switch (r->op){						case LUMP_ADD:							/*just add it here*/							memcpy(new_buf+offset, r->u.value, r->len);							offset+=r->len;							break;						default:							/* only ADD allowed for before/after */							LOG(L_CRIT, "BUG:forward_request: invalid op for"									" data lump (%x)\n", r->op);					}				}				break;			default:					LOG(L_CRIT, "BUG: forward_request: unknown op (%x)\n",							t->op);		}	}	/* copy the rest of the message */	memcpy(new_buf+offset, orig+s_offset, len-s_offset);	new_buf[new_len]=0;	 /* send it! */	DBG("Sending:\n%s.\n", new_buf);	DBG("orig. len=%d, new_len=%d, via_len=%d, received_len=%d\n",			len, new_len, via_len, received_len);	to->sin_family = AF_INET;	to->sin_port = (p->port)?htons(p->port):htons(SIP_PORT);	/* if error try next ip address if possible */	if (p->ok==0){		if (p->host.h_addr_list[p->addr_idx+1])			p->addr_idx++;		p->ok=1;	}	/* ? not 64bit clean?*/	to->sin_addr.s_addr=*((long*)p->host.h_addr_list[p->addr_idx]);	p->tx++;	p->tx_bytes+=new_len;	if (udp_send(new_buf, new_len, (struct sockaddr*) to,				sizeof(struct sockaddr_in))==-1){			p->errors++;			p->ok=0;#ifdef STATS			update_fail_on_send;#endif			goto error;	} #ifdef STATS	/* sent requests stats */	else update_sent_request( msg->first_line.u.request.method_value );#endif	free(new_buf);	free(to);	/* received_buf & line_buf will be freed in receiv_msg by free_lump_list*/	return 0;error1:	if (line_buf) free(line_buf);	if (received_buf) free(received_buf);error:	if (new_buf) free(new_buf);	if (to) free(to);	return -1;}