parser/parse_via.c
0fcc2741
 /*
22d4aa5d
  * via parsing automaton
  * 
c32feee5
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
07ca8c05
  * This file is part of Kamailio, a free SIP server.
7dd0b342
  *
07ca8c05
  * Kamailio is free software; you can redistribute it and/or modify
7dd0b342
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
07ca8c05
  * Kamailio is distributed in the hope that it will be useful,
7dd0b342
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22d4aa5d
  */
 
1eb01c66
 /** @file
  * @brief Parser :: Via parsing automation
  *
  * @ingroup parser
  */
22d4aa5d
 
049f64c2
 #include "../comp_defs.h"
22d4aa5d
 #include <stdlib.h>
e22bbdb8
 #include <string.h>
3881f12c
 #include "../dprint.h"
 #include "../ut.h"
 #include "../mem/mem.h"
f2f969dd
 #include "../ip_addr.h"
2bb60634
 #include "parse_via.h"
 #include "parse_def.h"
a075952d
 #include "msg_parser.h"
22d4aa5d
 
 
 
1eb01c66
 /** \brief main via states (uri:port ...) */
2bb60634
 enum {	         
 	F_HOST, P_HOST,
 	L_PORT, F_PORT, P_PORT,
 	L_PARAM, F_PARAM, P_PARAM,
 	L_VIA, F_VIA,
 	F_COMMENT, P_COMMENT,
d5f3a5fb
 	F_IP6HOST, P_IP6HOST, 
2bb60634
 	F_CRLF,
 	F_LF,
 	F_CR,
 	END_OF_HEADER
 };
 
22d4aa5d
 
1eb01c66
 /** \brief first via part state */
2bb60634
 enum {
 	F_SIP = 100,
 	SIP1, SIP2, FIN_SIP,
 	L_VER, F_VER,
 	VER1, VER2, FIN_VER,
f2f969dd
 	UDP1, UDP2, FIN_UDP,
8e807134
 	TCP_TLS1, TCP2, FIN_TCP,
 	          TLS2, FIN_TLS,
dec9733a
 	SCTP1, SCTP2, SCTP3, FIN_SCTP,
7cd27114
 	WS_WSS1, WS_WSS2, FIN_WSS,
cf4651db
 	OTHER_PROTO,
f2f969dd
 	L_PROTO, F_PROTO
2bb60634
 };
 
22d4aa5d
 
1eb01c66
 /** \brief param related states
09f7cd2c
  * WARNING: keep in sync with parse_via.h, PARAM_HIDDEN, ...
  */
2bb60634
 enum {	
 	L_VALUE = 200, F_VALUE, P_VALUE, P_STRING,
 	HIDDEN1, HIDDEN2, HIDDEN3, HIDDEN4, HIDDEN5,
 	TTL1, TTL2,
 	BRANCH1, BRANCH2, BRANCH3, BRANCH4, BRANCH5,
 	MADDR1, MADDR2, MADDR3, MADDR4,
 	RECEIVED1, RECEIVED2, RECEIVED3, RECEIVED4, RECEIVED5, RECEIVED6,
 	RECEIVED7,
09f7cd2c
 	RPORT1, RPORT2, RPORT3,
59653eb8
 	ALIAS1, ALIAS2, ALIAS3, ALIAS4,
7a068ff3
 #ifdef USE_COMP
 	COMP1, COMP2, COMP3, 
 	/* values */
 	L_COMP_VALUE, F_COMP_VALUE,
 	V_COMP_S, V_SIGCOMP_I, V_SIGCOMP_G, V_SIGCOMP_C, V_SIGCOMP_O, V_SIGCOMP_M,
 	FIN_V_SIGCOMP_P,
 	V_SERGZ_E, V_SERGZ_R, V_SERGZ_G, FIN_V_SERGZ_Z,
 #endif
2bb60634
 	     /* fin states (227-...)*/
59653eb8
 	FIN_HIDDEN = 230, FIN_TTL, FIN_BRANCH,
 	FIN_MADDR, FIN_RECEIVED, FIN_RPORT, FIN_I, FIN_ALIAS
7a068ff3
 #ifdef USE_COMP
 	, FIN_COMP
 #endif
2bb60634
 	     /*GEN_PARAM,
 	       PARAM_ERROR*/ /* declared in msg_parser.h*/
 };
22d4aa5d
 
 
0fcc2741
 /* entry state must be F_PARAM, or saved_state=F_PARAM and
22d4aa5d
  * state=F_{LF,CR,CRLF}!
0fcc2741
  * output state = L_PARAM or F_PARAM or END_OF_HEADER
2bb60634
  * (and saved_state= last state); everything else => error 
51767629
  * WARNING: param->start must be filled before, it's used in param->size 
  * computation.
2bb60634
  */
f7b97e8a
 static /*inline*/ char* parse_via_param(char* const p, const char* const end,
 									unsigned char* const pstate, 
 				    					unsigned char* const psaved_state,
 									struct via_param* const param,
 									const struct via_body* vb)
22d4aa5d
 {
 	char* tmp;
f2f969dd
 	register unsigned char state;
 	unsigned char saved_state;
0fcc2741
 
7a068ff3
 #define value_case(c, C, oldstate, newstate, start_of_value) \
 			case (c): \
 			case (C): \
 				switch(state){ \
 					case (oldstate): \
 						state=(newstate); \
 						if ((start_of_value))  param->value.s=tmp; \
 						break; \
 					default_value_cases \
 				} \
 				break
 
 #define value_case_double(c, C, oldstate1, newstate1, oldstate2, newstate2) \
 			case (c): \
 			case (C): \
 				switch(state){ \
 					case (oldstate1): \
 						state=(newstate1); \
 						break; \
 					case (oldstate2): \
 						state=(newstate2); \
 						break; \
 					default_value_cases \
 				} \
 				break
 
 #define default_value_cases \
 					case F_VALUE: \
 						state=P_VALUE; \
 						param->value.s=tmp; \
 						break; \
 					case P_VALUE: \
 					case P_STRING: \
 						break; \
 					case F_LF: \
 					case F_CR:  \
 					case F_CRLF: \
 						state=END_OF_HEADER; \
 						goto end_via; \
 					default: \
 						switch(state){ \
 							case F_COMP_VALUE: \
 								comp_unexpected_char; \
 								state=P_VALUE; \
 								param->value.s=tmp; \
 								break; \
 							comp_states_cases \
 							comp_fin_states_cases \
 								comp_unexpected_char; \
 								state=P_VALUE; \
 								break; \
 							default: \
 								LOG(L_ERR, "ERROR: parse_via_param: invalid " \
 									"char <%c> in state %d\n", *tmp, state); \
 								goto error; \
 						}
 
 #define comp_states_cases \
 					case V_COMP_S: \
 					case V_SIGCOMP_I: \
 					case V_SIGCOMP_G: \
 					case V_SIGCOMP_C: \
 					case V_SIGCOMP_O: \
 					case V_SIGCOMP_M: \
 					case V_SERGZ_E: \
 					case V_SERGZ_R: \
 					case V_SERGZ_G: 
 
 #define comp_fin_states_cases \
 					case FIN_V_SIGCOMP_P: \
 					case FIN_V_SERGZ_Z:
 
 
 /* if unrecognized/bad comp, don't return error, just ignore comp */
 #define comp_unexpected_char \
 							LOG(L_ERR, "parse_via_param: bad/unrecognized" \
 									" comp method\n"); \
 							vb->comp_no=0
 								
 
 
22d4aa5d
 	state=*pstate;
 	saved_state=*psaved_state;
89e74c57
 	param->type=PARAM_ERROR;
0fcc2741
 
22d4aa5d
 	for (tmp=p;tmp<end;tmp++){
 		switch(*tmp){
 			case ' ':
 			case '\t':
 				switch(state){
 					case FIN_HIDDEN:
59653eb8
 					case FIN_ALIAS:
89e74c57
 						param->type=state;
 						param->name.len=tmp-param->name.s;
22d4aa5d
 						state=L_PARAM;
 						goto endofparam;
 					case FIN_BRANCH:
 					case FIN_TTL:
 					case FIN_MADDR:
 					case FIN_RECEIVED:
09f7cd2c
 					case FIN_RPORT:
 					case FIN_I:
89e74c57
 						param->type=state;
 						param->name.len=tmp-param->name.s;
22d4aa5d
 						state=L_VALUE;
 						goto find_value;
7a068ff3
 #ifdef USE_COMP
 					case FIN_COMP:
 						param->type=state;
 						param->name.len=tmp-param->name.s;
 						state=L_COMP_VALUE;
 						goto find_value;
 #endif
22d4aa5d
 					case F_PARAM:
 						break;
 					case F_LF:
 					case F_CR:
 					case F_CRLF:
 						state=saved_state;
 						break;
 					case GEN_PARAM:
 					default:
89e74c57
 						param->type=GEN_PARAM;
 						param->name.len=tmp-param->name.s;
22d4aa5d
 						state=L_VALUE;
 						goto find_value;
 				}
 				break;
 			/* \n and \r*/
 			case '\n':
 				switch(state){
 					case FIN_HIDDEN:
59653eb8
 					case FIN_ALIAS:
89e74c57
 						param->type=state;
 						param->name.len=tmp-param->name.s;
51767629
 						param->size=tmp-param->start; 
22d4aa5d
 						saved_state=L_PARAM;
 						state=F_LF;
 						goto endofparam;
 					case FIN_BRANCH:
 					case FIN_TTL:
 					case FIN_MADDR:
 					case FIN_RECEIVED:
09f7cd2c
 					case FIN_I:
51767629
 					case FIN_RPORT:
89e74c57
 						param->type=state;
 						param->name.len=tmp-param->name.s;
51767629
 						param->size=tmp-param->start; 
22d4aa5d
 						saved_state=L_VALUE;
 						state=F_LF;
 						goto find_value;
7a068ff3
 #ifdef USE_COMP
 					case FIN_COMP:
 						param->type=state;
 						param->name.len=tmp-param->name.s;
 						param->size=tmp-param->start; 
 						saved_state=L_COMP_VALUE;
 						state=F_LF;
 						goto find_value;
 #endif
22d4aa5d
 					case F_PARAM:
 						saved_state=state;
 						state=F_LF;
 						break;
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					case F_CR:
 						state=F_CRLF;
 						break;
 					case GEN_PARAM:
 					default:
89e74c57
 						param->type=GEN_PARAM;
22d4aa5d
 						saved_state=L_VALUE;
89e74c57
 						param->name.len=tmp-param->name.s;
51767629
 						param->size=tmp-param->start; 
22d4aa5d
 						state=F_LF;
 						goto find_value;
 				}
 				break;
 			case '\r':
 				switch(state){
 					case FIN_HIDDEN:
59653eb8
 					case FIN_ALIAS:
89e74c57
 						param->type=state;
 						param->name.len=tmp-param->name.s;
51767629
 						param->size=tmp-param->start; 
22d4aa5d
 						saved_state=L_PARAM;
 						state=F_CR;
 						goto endofparam;
 					case FIN_BRANCH:
 					case FIN_TTL:
 					case FIN_MADDR:
 					case FIN_RECEIVED:
09f7cd2c
 					case FIN_I:
51767629
 					case FIN_RPORT:
89e74c57
 						param->type=state;
 						param->name.len=tmp-param->name.s;
51767629
 						param->size=tmp-param->start; 
22d4aa5d
 						saved_state=L_VALUE;
 						state=F_CR;
 						goto find_value;
7a068ff3
 #ifdef USE_COMP
 					case FIN_COMP:
 						param->type=state;
 						param->name.len=tmp-param->name.s;
 						param->size=tmp-param->start; 
 						saved_state=L_COMP_VALUE;
 						state=F_LF;
 						goto find_value;
 #endif
22d4aa5d
 					case F_PARAM:
 						saved_state=state;
 						state=F_CR;
 						break;
 					case F_CR:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					case GEN_PARAM:
 					default:
89e74c57
 						param->type=GEN_PARAM;
 						param->name.len=tmp-param->name.s;
51767629
 						param->size=tmp-param->start; 
22d4aa5d
 						saved_state=L_VALUE;
 						state=F_CR;
 						goto find_value;
 				}
 				break;
 
 			case '=':
 				switch(state){
 					case FIN_BRANCH:
 					case FIN_TTL:
 					case FIN_MADDR:
 					case FIN_RECEIVED:
09f7cd2c
 					case FIN_RPORT:
 					case FIN_I:
89e74c57
 						param->type=state;
 						param->name.len=tmp-param->name.s;
22d4aa5d
 						state=F_VALUE;
 						goto find_value;
7a068ff3
 #ifdef USE_COMP
 					case FIN_COMP:
 						param->type=state;
 						param->name.len=tmp-param->name.s;
 						state=F_COMP_VALUE;
 						goto find_value;
 #endif
22d4aa5d
 					case F_PARAM:
 					case FIN_HIDDEN:
59653eb8
 					case FIN_ALIAS:
22d4aa5d
 						LOG(L_ERR, "ERROR: parse_via: invalid char <%c> in"
e22bbdb8
 								" state %d\n", *tmp, state);
22d4aa5d
 						goto error;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					case GEN_PARAM:
 					default:
89e74c57
 						param->type=GEN_PARAM;
 						param->name.len=tmp-param->name.s;
22d4aa5d
 						state=F_VALUE;
 						goto find_value;
 				}
 				break;
 			case ';':
 				switch(state){
 					case FIN_HIDDEN:
09f7cd2c
 					case FIN_RPORT: /* rport can appear w/o a value */
59653eb8
 					case FIN_ALIAS:
89e74c57
 						param->type=state;
 						param->name.len=tmp-param->name.s;
22d4aa5d
 						state=F_PARAM;
 						goto endofparam;
 					case FIN_BRANCH:
 					case FIN_MADDR:
 					case FIN_TTL:
 					case FIN_RECEIVED:
09f7cd2c
 					case FIN_I:
7a068ff3
 #ifdef USE_COMP
 					case FIN_COMP:
 #endif
22d4aa5d
 						LOG(L_ERR, "ERROR: parse_via: invalid char <%c> in"
e22bbdb8
 								" state %d\n", *tmp, state);
22d4aa5d
 						goto error;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					case GEN_PARAM:
 					default:
89e74c57
 						param->type=GEN_PARAM;
 						param->name.len=tmp-param->name.s;
22d4aa5d
 						state=F_PARAM;
 						goto endofparam;
 				}
 				break;
9d81cbd6
 			case ',':
 				switch(state){
 					case FIN_HIDDEN:
09f7cd2c
 					case FIN_RPORT:
59653eb8
 					case FIN_ALIAS:
9d81cbd6
 						param->type=state;
 						param->name.len=tmp-param->name.s;
 						state=F_VIA;
 						goto endofvalue;
 					case FIN_BRANCH:
 					case FIN_MADDR:
 					case FIN_TTL:
 					case FIN_RECEIVED:
09f7cd2c
 					case FIN_I:
7a068ff3
 #ifdef USE_COMP
 					case FIN_COMP:
 #endif
9d81cbd6
 						LOG(L_ERR, "ERROR: parse_via_param: new via found" 
 								"(',') when '=' expected (state %d=)\n",
 								state);
 						goto error; /* or we could ignore this bad param*/
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					case GEN_PARAM:
 					default:
 						param->type=GEN_PARAM;
 						param->name.len=tmp-param->name.s;
 						state=F_VIA;
 						goto endofvalue;
 				}
 				break; 
0fcc2741
 
22d4aa5d
 				/* param names */
 			case 'h':
 			case 'H':
 				switch(state){
 					case F_PARAM:
 						state=HIDDEN1;
89e74c57
 						param->name.s=tmp;
22d4aa5d
 						break;
eaf48c3f
 					case BRANCH5:
 						state=FIN_BRANCH;
 						break;
22d4aa5d
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 'i':
 			case 'I':
 				switch(state){
89e74c57
 					case F_PARAM:
09f7cd2c
 						state=FIN_I;
89e74c57
 						param->name.s=tmp;
 						break;
22d4aa5d
 					case HIDDEN1:
 						state=HIDDEN2;
 						break;
 					case RECEIVED4:
 						state=RECEIVED5;
 						break;
59653eb8
 					case ALIAS2:
 						state=ALIAS3;
 						break;
22d4aa5d
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 'd':
 			case 'D':
 				switch(state){
89e74c57
 					case F_PARAM:
 						state=GEN_PARAM;
 						param->name.s=tmp;
 						break;
22d4aa5d
 					case HIDDEN2:
 						state=HIDDEN3;
 						break;
 					case HIDDEN3:
 						state=HIDDEN4;
 						break;
 					case MADDR2:
 						state=MADDR3;
 						break;
 					case MADDR3:
 						state=MADDR4;
 						break;
 					case RECEIVED7:
 						state=FIN_RECEIVED;
 						break;
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 'e':
 			case 'E':
 				switch(state){
89e74c57
 					case F_PARAM:
 						state=GEN_PARAM;
 						param->name.s=tmp;
 						break;
22d4aa5d
 					case HIDDEN4:
 						state=HIDDEN5;
 						break;
 					case RECEIVED1:
 						state=RECEIVED2;
 						break;
 					case RECEIVED3:
 						state=RECEIVED4;
 						break;
 					case RECEIVED6:
 						state=RECEIVED7;
 						break;
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 'n':
 			case 'N':
 				switch(state){
89e74c57
 					case F_PARAM:
 						state=GEN_PARAM;
 						param->name.s=tmp;
 						break;
22d4aa5d
 					case HIDDEN5:
 						state=FIN_HIDDEN;
 						break;
 					case BRANCH3:
 						state=BRANCH4;
 						break;
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 't':
 			case 'T':
 				switch(state){
 					case F_PARAM:
 						state=TTL1;
89e74c57
 						param->name.s=tmp;
22d4aa5d
 						break;
 					case TTL1:
 						state=TTL2;
 						break;
09f7cd2c
 					case RPORT3:
 						state=FIN_RPORT;
 						break;
22d4aa5d
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 'l':
 			case 'L':
 				switch(state){
89e74c57
 					case F_PARAM:
 						state=GEN_PARAM;
 						param->name.s=tmp;
 						break;
22d4aa5d
 					case TTL2:
 						state=FIN_TTL;
 						break;
59653eb8
 					case ALIAS1:
 						state=ALIAS2;
 						break;
22d4aa5d
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 'm':
 			case 'M':
 				switch(state){
 					case F_PARAM:
 						state=MADDR1;
89e74c57
 						param->name.s=tmp;
22d4aa5d
 						break;
7a068ff3
 #ifdef USE_COMP
 					case COMP2:
 						state=COMP3;
 						break;
 #endif
22d4aa5d
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 'a':
 			case 'A':
 				switch(state){
89e74c57
 					case F_PARAM:
59653eb8
 						state=ALIAS1;
89e74c57
 						param->name.s=tmp;
 						break;
22d4aa5d
 					case MADDR1:
 						state=MADDR2;
 						break;
 					case BRANCH2:
 						state=BRANCH3;
 						break;
59653eb8
 					case ALIAS3:
 						state=ALIAS4;
 						break;
22d4aa5d
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 'r':
 			case 'R':
 				switch(state){
 					case MADDR4:
 						state=FIN_MADDR;
 						break;
 					case F_PARAM:
 						state=RECEIVED1;
89e74c57
 						param->name.s=tmp;
22d4aa5d
 						break;
 					case BRANCH1:
 						state=BRANCH2;
 						break;
09f7cd2c
 					case RPORT2:
 						state=RPORT3;
 						break;
22d4aa5d
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 'c':
 			case 'C':
 				switch(state){
89e74c57
 					case F_PARAM:
7a068ff3
 #ifdef USE_COMP
 						state=COMP1;
 #else
89e74c57
 						state=GEN_PARAM;
7a068ff3
 #endif
89e74c57
 						param->name.s=tmp;
 						break;
22d4aa5d
 					case RECEIVED2:
 						state=RECEIVED3;
 						break;
 					case BRANCH4:
 						state=BRANCH5;
 						break;
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 'v':
 			case 'V':
 				switch(state){
89e74c57
 					case F_PARAM:
 						state=GEN_PARAM;
 						param->name.s=tmp;
 						break;
22d4aa5d
 					case RECEIVED5:
 						state=RECEIVED6;
 						break;
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 'b':
 			case 'B':
 				switch(state){
 					case F_PARAM:
 						state=BRANCH1;
89e74c57
 						param->name.s=tmp;
22d4aa5d
 						break;
 					case GEN_PARAM:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
09f7cd2c
 			case 'p':
 			case 'P':
 				switch(state){
ff76544d
 					case F_PARAM:
 						state=GEN_PARAM;
 						param->name.s=tmp;
 						break;
09f7cd2c
 					case RECEIVED1:
 						state=RPORT1;
 						break;
7a068ff3
 #ifdef USE_COMP
 					case COMP3:
 						state=FIN_COMP;
 						break;
 #endif
09f7cd2c
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
 			case 'o':
 			case 'O':
 				switch(state){
ff76544d
 					case F_PARAM:
 						state=GEN_PARAM;
 						param->name.s=tmp;
 						break;
09f7cd2c
 					case RPORT1:
 						state=RPORT2;
 						break;
7a068ff3
 #ifdef USE_COMP
 					case COMP1:
 						state=COMP2;
 						break;
 #endif
09f7cd2c
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
59653eb8
 			case 's':
 			case 'S':
 				switch(state){
 					case F_PARAM:
 						state=GEN_PARAM;
 						param->name.s=tmp;
 						break;
 					case ALIAS4:
 						state=FIN_ALIAS;
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 				break;
22d4aa5d
 			default:
 				switch(state){
 					case F_PARAM:
 						state=GEN_PARAM;
89e74c57
 						param->name.s=tmp;
22d4aa5d
 						break;
09f7cd2c
 					case GEN_PARAM:
22d4aa5d
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						state=GEN_PARAM;
 				}
 		}
 	}/* for tmp*/
 
2bb60634
 	/* end of packet? => error, no cr/lf,',' found!!!*/
 	saved_state=state;
 	state=END_OF_HEADER;
 	goto error;
 	
  find_value:
22d4aa5d
 	tmp++;
e22bbdb8
 	for(;*tmp;tmp++){
22d4aa5d
 		switch(*tmp){
 			case ' ':
 			case '\t':
 				switch(state){
 					case L_VALUE:
 					case F_VALUE: /*eat space*/
 						break; 
 					case P_VALUE:
 						state=L_PARAM;
89e74c57
 						param->value.len=tmp-param->value.s;
22d4aa5d
 						goto endofvalue;
7a068ff3
 #ifdef USE_COMP
 					case L_COMP_VALUE:
 					case F_COMP_VALUE:
 						break; /* eat space */
 					case FIN_V_SIGCOMP_P:
 						state=L_PARAM;
 						param->value.len=tmp-param->value.s;
 						vb->comp_no=COMP_SIGCOMP;
 						goto endofvalue;
 					case FIN_V_SERGZ_Z:
 						state=L_PARAM;
 						param->value.len=tmp-param->value.s;
 						vb->comp_no=COMP_SIGCOMP;
 						goto endofvalue;
 					comp_states_cases
 						state=L_PARAM;
 						param->value.len=tmp-param->value.s;
 						comp_unexpected_char;
 						goto endofvalue;
 #endif
22d4aa5d
 					case P_STRING:
 						break;
 					case F_CR:
 					case F_LF:
 					case F_CRLF:
 						state=saved_state;
 						break;
 					default:
 						LOG(L_ERR, "ERROR: parse_via: invalid char <%c>"
e22bbdb8
 								" in state %d\n", *tmp, state);
22d4aa5d
 						goto error;
 				}
 				break;
 			case '\n':
 				switch(state){
 					case L_VALUE:
 					case F_VALUE: /*eat space*/
7a068ff3
 #ifdef USE_COMP
 					case L_COMP_VALUE:
 					case F_COMP_VALUE:
 #endif
22d4aa5d
 					case P_STRING:
 						saved_state=state;
51767629
 						param->size=tmp-param->start;
22d4aa5d
 						state=F_LF;
0fcc2741
 						break;
22d4aa5d
 					case P_VALUE:
 						saved_state=L_PARAM;
 						state=F_LF;
89e74c57
 						param->value.len=tmp-param->value.s;
22d4aa5d
 						goto endofvalue;
7a068ff3
 #ifdef USE_COMP
 					case FIN_V_SIGCOMP_P:
 						saved_state=L_PARAM;
 						state=F_LF;
 						param->value.len=tmp-param->value.s;
 						vb->comp_no=COMP_SIGCOMP;
 						goto endofvalue;
 					case FIN_V_SERGZ_Z:
 						saved_state=L_PARAM;
 						state=F_LF;
 						param->value.len=tmp-param->value.s;
 						vb->comp_no=COMP_SIGCOMP;
 						goto endofvalue;
 					comp_states_cases
 						saved_state=L_PARAM;
 						state=F_LF;
 						param->value.len=tmp-param->value.s;
 						comp_unexpected_char;
 						goto endofvalue;
 #endif
22d4aa5d
 					case F_LF:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					case F_CR:
 						state=F_CRLF;
 						break;
 					default:
 						LOG(L_ERR, "ERROR: parse_via: invalid char <%c>"
e22bbdb8
 								" in state %d\n", *tmp, state);
22d4aa5d
 						goto error;
 				}
 				break;
 			case '\r':
 				switch(state){
 					case L_VALUE:
 					case F_VALUE: /*eat space*/
7a068ff3
 #ifdef USE_COMP
 					case L_COMP_VALUE:
 					case F_COMP_VALUE:
 #endif
22d4aa5d
 					case P_STRING:
 						saved_state=state;
51767629
 						param->size=tmp-param->start;
22d4aa5d
 						state=F_CR;
0fcc2741
 						break;
22d4aa5d
 					case P_VALUE:
89e74c57
 						param->value.len=tmp-param->value.s;
22d4aa5d
 						saved_state=L_PARAM;
 						state=F_CR;
 						goto endofvalue;
7a068ff3
 #ifdef USE_COMP
 					case FIN_V_SIGCOMP_P:
 						saved_state=L_PARAM;
 						state=F_CR;
 						param->value.len=tmp-param->value.s;
 						vb->comp_no=COMP_SIGCOMP;
 						goto endofvalue;
 					case FIN_V_SERGZ_Z:
 						saved_state=L_PARAM;
 						state=F_CR;
 						param->value.len=tmp-param->value.s;
 						vb->comp_no=COMP_SIGCOMP;
 						goto endofvalue;
 					comp_states_cases
 						saved_state=L_PARAM;
 						state=F_CR;
 						param->value.len=tmp-param->value.s;
 						comp_unexpected_char;
 						goto endofvalue;
 #endif
22d4aa5d
 					case F_LF:
 					case F_CR:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						LOG(L_ERR, "ERROR: parse_via: invalid char <%c>"
e22bbdb8
 								" in state %d\n", *tmp, state);
22d4aa5d
 						goto error;
 				}
 				break;
 
 			case '=':
 				switch(state){
 					case L_VALUE:
 						state=F_VALUE;
 						break;
7a068ff3
 #ifdef USE_COMP
 					case L_COMP_VALUE:
 						state=F_COMP_VALUE;
 						break;
 					/* '=' in any other COMP value state is an error,
 					 * and it will be catched by the default branch */
 #endif
22d4aa5d
 					case P_STRING:
 						break;
 					case F_LF:
 					case F_CR:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						LOG(L_ERR, "ERROR: parse_via: invalid char <%c>"
e22bbdb8
 								" in state %d\n", *tmp, state);
22d4aa5d
 						goto error;
 				}
 				break;
 			case ';':
 				switch(state){
 					case P_VALUE:
89e74c57
 						param->value.len=tmp-param->value.s;
22d4aa5d
 						state=F_PARAM;
 						goto endofvalue;
09f7cd2c
 					case F_VALUE:
 						param->value.len=0;
 						state=F_PARAM;
 						goto endofvalue;
22d4aa5d
 					case P_STRING:
 						break; /* what to do? */
 					case F_LF:
 					case F_CR:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
7a068ff3
 #ifdef USE_COMP
 					case L_COMP_VALUE:
 						comp_unexpected_char;
 						/* we want to contine with no comp */
 						state=F_PARAM;
 						param->value.len=0;
 						param->value.s=0;
 						goto endofvalue;
 					case F_COMP_VALUE:
 						comp_unexpected_char;
 						param->value.len=0;
 						state=F_PARAM;
 						goto endofvalue;
 					comp_states_cases
 						comp_unexpected_char;
 						param->value.len=tmp-param->value.s;
 						state=F_PARAM;
 						goto endofvalue;
 					case FIN_V_SIGCOMP_P:
 						vb->comp_no=COMP_SIGCOMP;
 						param->value.len=tmp-param->value.s;
 						state=F_PARAM;
 						goto endofvalue;
 					case FIN_V_SERGZ_Z:
 						vb->comp_no=COMP_SIGCOMP;
 						param->value.len=tmp-param->value.s;
 						state=F_PARAM;
 						goto endofvalue;
 #endif
09f7cd2c
 					case L_VALUE:
362b6f13
 						param->value.len=0;
 						param->value.s=0; /* null value */
 						state=F_PARAM;
 						goto endofvalue;
22d4aa5d
 					default:
 						LOG(L_ERR, "ERROR: parse_via: invalid char <%c>"
e22bbdb8
 								" in state %d\n", *tmp, state);
22d4aa5d
 						goto error;
 				}
 				break;
490ff008
 			case ',':
 				switch(state){
 					case P_VALUE:
 						param->value.len=tmp-param->value.s;
 						state=F_VIA;
 						goto endofvalue;
 					case P_STRING:
09f7cd2c
 					case F_LF:
490ff008
 					case F_CR:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
7a068ff3
 #ifdef USE_COMP
 					case L_COMP_VALUE:
 						comp_unexpected_char;
 						/* we want to contine with no comp */
 						state=F_VIA;
 						param->value.len=0;
 						param->value.s=0;
 						goto endofvalue;
 					case F_COMP_VALUE:
 						comp_unexpected_char;
 						param->value.len=0;
 						state=F_VIA;
 						goto endofvalue;
 					comp_states_cases
 						comp_unexpected_char;
 						param->value.len=tmp-param->value.s;
 						state=F_VIA;
 						goto endofvalue;
 					case FIN_V_SIGCOMP_P:
 						vb->comp_no=COMP_SIGCOMP;
 						param->value.len=tmp-param->value.s;
 						state=F_VIA;
 						goto endofvalue;
 					case FIN_V_SERGZ_Z:
 						vb->comp_no=COMP_SIGCOMP;
 						param->value.len=tmp-param->value.s;
 						state=F_VIA;
 						goto endofvalue;
 #endif
09f7cd2c
 					case L_VALUE:
 						if (param->type==FIN_RPORT){
 							param->value.len=0;
 							param->value.s=0; /* null value */
 							state=F_VIA;
 							goto endofvalue;
 						};
 						/* no break */
490ff008
 					default:
 						LOG(L_ERR, "ERROR: parse_via: invalid char <%c>"
e22bbdb8
 								" in state %d\n", *tmp, state);
490ff008
 						goto error;
 				}
 				break; /* what to do? */
22d4aa5d
 			case '"':
 				switch(state){
 					case F_VALUE:
 						state=P_STRING;
89e74c57
 						param->value.s=tmp+1;
22d4aa5d
 						break;
 					case P_STRING:
 						state=L_PARAM;
89e74c57
 						param->value.len=tmp-param->value.s;
22d4aa5d
 						goto endofvalue;
 					case F_LF:
 					case F_CR:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
 						LOG(L_ERR, "ERROR: parse_via: invalid char <%c>"
e22bbdb8
 								" in state %d\n", *tmp, state);
22d4aa5d
 						goto error;
 				}
 				break;
7a068ff3
 #ifdef USE_COMP
 			value_case('s', 'S', F_COMP_VALUE, V_COMP_S, 1);
 			value_case('i', 'I', V_COMP_S, V_SIGCOMP_I, 0);
 			value_case_double('g', 'G', V_SIGCOMP_I, V_SIGCOMP_G,
 					                    V_SERGZ_R,   V_SERGZ_G);
 			value_case('c', 'C', V_SIGCOMP_G, V_SIGCOMP_C, 0);
 			value_case('o', 'O', V_SIGCOMP_C, V_SIGCOMP_O, 0);
 			value_case('m', 'M', V_SIGCOMP_O, V_SIGCOMP_M, 0);
 			value_case('p', 'P', V_SIGCOMP_M, FIN_V_SIGCOMP_P, 0);
 			
 			value_case('e', 'E', V_COMP_S, V_SERGZ_E, 0);
 			value_case('r', 'R', V_SERGZ_E, V_SERGZ_R, 0);
 			value_case('z', 'Z', V_SERGZ_G, FIN_V_SERGZ_Z, 0);
 #endif /* USE_COMP */
 				
22d4aa5d
 			default:
 				switch(state){
 					case F_VALUE:
 						state=P_VALUE;
89e74c57
 						param->value.s=tmp;
22d4aa5d
 						break;
 					case P_VALUE:
 					case P_STRING:
 						break;
 					case F_LF:
 					case F_CR:
 					case F_CRLF:
 						state=END_OF_HEADER;
 						goto end_via;
 					default:
7a068ff3
 #ifdef USE_COMP
 						switch(state){
 							case F_COMP_VALUE:
 								comp_unexpected_char;
 								state=P_VALUE;
 								param->value.s=tmp;
 								break;
 							comp_states_cases
 							comp_fin_states_cases
 								comp_unexpected_char;
 								state=P_VALUE;
 								break;
 							default:
 								LOG(L_ERR, "ERROR: parse_via_param: invalid "
 									"char <%c> in state %d\n", *tmp, state);
 								goto error;
 						}
 #else
22d4aa5d
 						LOG(L_ERR, "ERROR: parse_via: invalid char <%c>"
e22bbdb8
 								" in state %d\n", *tmp, state);
22d4aa5d
 						goto error;
7a068ff3
 #endif /* USE_COMP */
22d4aa5d
 				}
 		}
 	} /* for2 tmp*/
 
89e74c57
 	/* end of buff and no CR/LF =>error*/
2bb60634
 	saved_state=state;
 	state=END_OF_HEADER;
 	goto error;
 	
  endofparam:
  endofvalue:
51767629
 	param->size=tmp-param->start;
 normal_exit:
22d4aa5d
 	*pstate=state;
 	*psaved_state=saved_state;
049f64c2
 	DBG("Found param type %d, <%.*s> = <%.*s>; state=%d\n", param->type,
6b6f226f
 			param->name.len, ZSW(param->name.s), 
6419a43f
 			(param->value.len?param->value.len:3),
 			(param->value.len?param->value.s:"n/a"), state);
22d4aa5d
 	return tmp;
2bb60634
 	
  end_via:
 	     /* if we are here we found an "unexpected" end of via
09f7cd2c
 	      *  (cr/lf). This is valid only if the param type is GEN_PARAM or
 		  *  RPORT (the only ones which can miss the value; HIDDEN is a 
 		  *  special case )*/
 	if ((param->type==GEN_PARAM)||(param->type==PARAM_RPORT)){
6b0c00a0
 		saved_state=L_PARAM; /* change the saved_state, we have an unknown
 		                        param. w/o a value */
51767629
 		/* param->size should be computed before */
 		goto normal_exit;
6b0c00a0
 	}
22d4aa5d
 	*pstate=state;
 	*psaved_state=saved_state;
049f64c2
 	DBG("Error on  param type %d, <%.*s>, state=%d, saved_state=%d\n",
6b6f226f
 		param->type, param->name.len, ZSW(param->name.s), state, saved_state);
89e74c57
 
2bb60634
  error:
89e74c57
 	LOG(L_ERR, "error: parse_via_param\n");
 	param->type=PARAM_ERROR;
 	*pstate=PARAM_ERROR;
 	*psaved_state=state;
22d4aa5d
 	return tmp;
 }
 
 
 
7418afcf
 /*
  * call it with a vb initialized to 0
  * returns: pointer after the parsed parts and sets vb->error
  * WARNING: don't forget to cleanup on error with free_via_list(vb)!
  */
f7b97e8a
 char* parse_via(char* buffer, const char* const end, struct via_body* const vbody)
22d4aa5d
 {
 	char* tmp;
51767629
 	char* param_start;
f2f969dd
 	unsigned char state;
 	unsigned char saved_state;
22d4aa5d
 	int c_nest;
 	int err;
7418afcf
 	struct via_body* vb;
89e74c57
 	struct via_param* param;
22d4aa5d
 
7418afcf
 	vb=vbody; /* keep orignal vbody value, needed to set the error member
 				 in case of multiple via bodies in the same header */
22d4aa5d
 parse_again:
34af87af
 	vb->error=PARSE_ERROR;
22d4aa5d
 	/* parse start of via ( SIP/2.0/UDP    )*/
 	state=F_SIP;
e0931fb0
 	saved_state=F_SIP; /* fixes gcc 4.0 warning */
51767629
 	param_start=0;
22d4aa5d
 	for(tmp=buffer;tmp<end;tmp++){
 		switch(*tmp){
 			case ' ':
 			case'\t':
 				switch(state){
 					case L_VER: /* eat space */
 					case L_PROTO:
 					case F_SIP:
 					case F_VER:
 					case F_PROTO:
 						break;
f2f969dd
 					case FIN_UDP:
22d4aa5d
 						vb->transport.len=tmp-vb->transport.s;
f2f969dd
 						vb->proto=PROTO_UDP;
 						state=F_HOST; /* start looking for host*/
 						goto main_via;
 					case FIN_TCP:
18dbc018
 						/* finished proto parsing */
f2f969dd
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_TCP;
22d4aa5d
 						state=F_HOST; /* start looking for host*/
 						goto main_via;
8e807134
 					case FIN_TLS:
 						/* finished proto parsing */
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_TLS;
 						state=F_HOST; /* start looking for host*/
 						goto main_via;
dec9733a
 					case FIN_SCTP:
 						/* finished proto parsing */
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_SCTP;
 						state=F_HOST; /* start looking for host*/
 						goto main_via;
7cd27114
 					case WS_WSS2:
 						/* finished proto parsing */
 						vb->transport.len=tmp-vb->transport.s;
8393efff
 						vb->proto=PROTO_WS;
7cd27114
 						state=F_HOST; /* start looking for host*/
 						goto main_via;
 					case FIN_WSS:
 						/* finished proto parsing */
 						vb->transport.len=tmp-vb->transport.s;
8393efff
 						vb->proto=PROTO_WSS;
7cd27114
 						state=F_HOST; /* start looking for host*/
 						goto main_via;
cf4651db
 					case OTHER_PROTO:
 						/* finished proto parsing */
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_OTHER;
 						state=F_HOST; /* start looking for host*/
 						goto main_via;
 					case UDP1:
 					case UDP2:
 					case TCP_TLS1:
 					case TCP2:
 					case TLS2:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
7cd27114
 					case WS_WSS1:
cf4651db
 						/* finished proto parsing */
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_OTHER;
 						state=F_HOST; /* start looking for host*/
 						goto main_via;
22d4aa5d
 					case FIN_SIP:
 						vb->name.len=tmp-vb->name.s;
 						state=L_VER;
 						break;
 					case FIN_VER:
 						vb->version.len=tmp-vb->version.s;
 						state=L_PROTO;
 						break;
 					case F_LF:
 					case F_CRLF:
 					case F_CR: /* header continues on this line */
 						state=saved_state;
 						break;
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			case '\n':
 				switch(state){
 					case L_VER:
 					case F_SIP:
 					case F_VER:
 					case F_PROTO:
 					case L_PROTO:
 						saved_state=state;
 						state=F_LF;
 						break;
f2f969dd
 					case FIN_UDP:
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_UDP;
 						state=F_LF;
 						saved_state=F_HOST; /* start looking for host*/
 						goto main_via;
 					case FIN_TCP:
22d4aa5d
 						vb->transport.len=tmp-vb->transport.s;
f2f969dd
 						vb->proto=PROTO_TCP;
22d4aa5d
 						state=F_LF;
 						saved_state=F_HOST; /* start looking for host*/
 						goto main_via;
8e807134
 					case FIN_TLS:
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_TLS;
 						state=F_LF;
 						saved_state=F_HOST; /* start looking for host*/
 						goto main_via;
cf4651db
 					case FIN_SCTP:
 						/* finished proto parsing */
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_SCTP;
 						state=F_LF;
 						saved_state=F_HOST; /* start looking for host*/
 						goto main_via;
7cd27114
 					case WS_WSS2:
 						/* finished proto parsing */
 						vb->transport.len=tmp-vb->transport.s;
8393efff
 						vb->proto=PROTO_WS;
7cd27114
 						state=F_LF;
 						saved_state=F_HOST; /* start looking for host*/
 						goto main_via;
 					case FIN_WSS:
 						/* finished proto parsing */
 						vb->transport.len=tmp-vb->transport.s;
8393efff
 						vb->proto=PROTO_WSS;
7cd27114
 						state=F_LF;
 						saved_state=F_HOST; /* start looking for host*/
 						goto main_via;
cf4651db
 					case OTHER_PROTO:
 						/* finished proto parsing */
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_OTHER;
 						state=F_LF;
 						saved_state=F_HOST; /* start looking for host*/
 						goto main_via;
 					case UDP1:
 					case UDP2:
 					case TCP_TLS1:
 					case TCP2:
 					case TLS2:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
7cd27114
 					case WS_WSS1:
cf4651db
 						/* finished proto parsing */
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_OTHER;
45541927
 						state=F_LF;
 						saved_state=F_HOST; /* start looking for host*/
cf4651db
 						goto main_via;
22d4aa5d
 					case FIN_SIP:
 						vb->name.len=tmp-vb->name.s;
 						state=F_LF;
 						saved_state=L_VER;
 						break;
 					case FIN_VER:
 						vb->version.len=tmp-vb->version.s;
 						state=F_LF;
 						saved_state=L_PROTO;
 						break;
 					case F_CR:
 						state=F_CRLF;
 						break;
 					case F_LF:
 					case F_CRLF:
 						state=saved_state;
 						goto endofheader;
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			case '\r':
 				switch(state){
 					case L_VER:
 					case F_SIP:
 					case F_VER:
 					case F_PROTO:
 					case L_PROTO:
 						saved_state=state;
 						state=F_CR;
 						break;
f2f969dd
 					case FIN_UDP:
22d4aa5d
 						vb->transport.len=tmp-vb->transport.s;
f2f969dd
 						vb->proto=PROTO_UDP;
 						state=F_CR;
 						saved_state=F_HOST;
 						goto main_via;
 					case FIN_TCP:
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_TCP;
22d4aa5d
 						state=F_CR;
 						saved_state=F_HOST;
 						goto main_via;
8e807134
 					case FIN_TLS:
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_TLS;
 						state=F_CR;
 						saved_state=F_HOST;
 						goto main_via;
cf4651db
 					case FIN_SCTP:
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_SCTP;
 						state=F_CR;
 						saved_state=F_HOST;
 						goto main_via;
7cd27114
 					case WS_WSS2:
 						vb->transport.len=tmp-vb->transport.s;
8393efff
 						vb->proto=PROTO_WS;
7cd27114
 						state=F_CR;
 						saved_state=F_HOST;
 						goto main_via;
 					case FIN_WSS:
 						vb->transport.len=tmp-vb->transport.s;
8393efff
 						vb->proto=PROTO_WSS;
7cd27114
 						state=F_CR;
 						saved_state=F_HOST;
 						goto main_via;
cf4651db
 					case OTHER_PROTO:
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_OTHER;
 						state=F_CR;
 						saved_state=F_HOST;
 						goto main_via;
 					case UDP1:
 					case UDP2:
 					case TCP_TLS1:
 					case TCP2:
 					case TLS2:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
7cd27114
 					case WS_WSS1:
cf4651db
 						/* finished proto parsing */
 						vb->transport.len=tmp-vb->transport.s;
 						vb->proto=PROTO_OTHER;
45541927
 						state=F_CR;
 						saved_state=F_HOST;
cf4651db
 						goto main_via;
22d4aa5d
 					case FIN_SIP:
 						vb->name.len=tmp-vb->name.s;
 						state=F_CR;
 						saved_state=L_VER;
 						break;
 					case FIN_VER:
 						vb->version.len=tmp-vb->version.s;
 						state=F_CR;
 						saved_state=L_PROTO;
 						break;
 					case F_LF: /*end of line ?next header?*/
 					case F_CR:
 					case F_CRLF:
 						state=saved_state;
 						goto endofheader;
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			
 			case '/':
 				switch(state){
 					case FIN_SIP:
 						vb->name.len=tmp-vb->name.s;
 						state=F_VER;
 						break;
 					case FIN_VER:
 						vb->version.len=tmp-vb->version.s;
 						state=F_PROTO;
 						break;
 					case L_VER:
 						state=F_VER;
 						break;
 					case L_PROTO:
 						state=F_PROTO;
 						break;
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 				/* match SIP*/
 			case 'S':
 			case 's':
 				switch(state){
 					case F_SIP:
 						state=SIP1;
 						vb->name.s=tmp;
 						break;
8e807134
 					case TLS2:
 						state=FIN_TLS;
 						break;
7cd27114
 					case WS_WSS1:
 						state=WS_WSS2;
 						break;
 					case WS_WSS2:
 						state=FIN_WSS;
 						break;
dec9733a
 					case F_PROTO:
 						state=SCTP1;
 						vb->transport.s=tmp;
 						break;
cf4651db
 					case OTHER_PROTO:
 						break;
 					case UDP1:
 					case UDP2:
 					case FIN_UDP:
 					case TCP_TLS1:
 					case TCP2:
 					case FIN_TCP:
 					case FIN_TLS:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
7cd27114
 					case FIN_WSS:
cf4651db
 					case FIN_SCTP:
 						state=OTHER_PROTO;
 						break;
22d4aa5d
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			case 'I':
 			case 'i':
 				switch(state){
 					case SIP1:
 						state=SIP2;
 						break;
cf4651db
 					case OTHER_PROTO:
 						break;
 					case UDP1:
 					case UDP2:
 					case FIN_UDP:
 					case TCP_TLS1:
 					case TCP2:
 					case FIN_TCP:
 					case TLS2:
 					case FIN_TLS:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
 					case FIN_SCTP:
7cd27114
 					case WS_WSS1:
 					case WS_WSS2:
 					case FIN_WSS:
cf4651db
 						state=OTHER_PROTO;
 						break;
22d4aa5d
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			case 'p':
 			case 'P':
 				switch(state){
 					case SIP2:
 						state=FIN_SIP;
 						break;
 					/* allow p in PROTO */
f2f969dd
 					case UDP2:
 						state=FIN_UDP;
 						break;
 					case TCP2:
 						state=FIN_TCP;
 						break;
dec9733a
 					case SCTP3:
 						state=FIN_SCTP;
 						break;
cf4651db
 					case OTHER_PROTO:
 						break;
 					case UDP1:
 					case FIN_UDP:
 					case TCP_TLS1:
 					case FIN_TCP:
 					case TLS2:
 					case FIN_TLS:
 					case SCTP1:
 					case SCTP2:
 					case FIN_SCTP:
7cd27114
 					case WS_WSS1:
 					case WS_WSS2:
 					case FIN_WSS:
cf4651db
 						state=OTHER_PROTO;
 						break;
f2f969dd
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			case 'U':
 			case 'u':
 				switch(state){
 					case F_PROTO:
 						state=UDP1;
09f7cd2c
 						vb->transport.s=tmp;
f2f969dd
 						break;
cf4651db
 					case OTHER_PROTO:
 						break;
 					case UDP1:
 					case UDP2:
 					case FIN_UDP:
 					case TCP_TLS1:
 					case TCP2:
 					case FIN_TCP:
 					case TLS2:
 					case FIN_TLS:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
 					case FIN_SCTP:
7cd27114
 					case WS_WSS1:
 					case WS_WSS2:
 					case FIN_WSS:
cf4651db
 						state=OTHER_PROTO;
 						break;
f2f969dd
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			case 'D':
 			case 'd':
 				switch(state){
 					case UDP1:
 						state=UDP2;
 						break;
cf4651db
 					case OTHER_PROTO:
 						break;
 					case UDP2:
 					case FIN_UDP:
 					case TCP_TLS1:
 					case TCP2:
 					case FIN_TCP:
 					case TLS2:
 					case FIN_TLS:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
 					case FIN_SCTP:
7cd27114
 					case WS_WSS1:
 					case WS_WSS2:
 					case FIN_WSS:
cf4651db
 						state=OTHER_PROTO;
 						break;
f2f969dd
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			case 'T':
 			case 't':
 				switch(state){
22d4aa5d
 					case F_PROTO:
8e807134
 						state=TCP_TLS1;
09f7cd2c
 						vb->transport.s=tmp;
22d4aa5d
 						break;
dec9733a
 					case SCTP2:
 						state=SCTP3;
 						break;
cf4651db
 					case OTHER_PROTO:
 						break;
 					case UDP1:
 					case UDP2:
 					case FIN_UDP:
 					case TCP_TLS1:
 					case TCP2:
 					case FIN_TCP:
 					case TLS2:
 					case FIN_TLS:
 					case SCTP1:
 					case SCTP3:
 					case FIN_SCTP:
7cd27114
 					case WS_WSS1:
 					case WS_WSS2:
 					case FIN_WSS:
cf4651db
 						state=OTHER_PROTO;
 						break;
f2f969dd
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			case 'C':
 			case 'c':
 				switch(state){
8e807134
 					case TCP_TLS1:
f2f969dd
 						state=TCP2;
22d4aa5d
 						break;
dec9733a
 					case SCTP1:
 						state=SCTP2;
 						break;
cf4651db
 					case OTHER_PROTO:
 						break;
 					case UDP1:
 					case UDP2:
 					case FIN_UDP:
 					case TCP2:
 					case FIN_TCP:
 					case TLS2:
 					case FIN_TLS:
 					case SCTP2:
 					case SCTP3:
 					case FIN_SCTP:
7cd27114
 					case WS_WSS1:
 					case WS_WSS2:
 					case FIN_WSS:
cf4651db
 						state=OTHER_PROTO;
 						break;
22d4aa5d
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
8e807134
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			case 'L':
 			case 'l':
 				switch(state){
 					case TCP_TLS1:
 						state=TLS2;
 						break;
cf4651db
 					case OTHER_PROTO:
 						break;
 					case UDP1:
 					case UDP2:
 					case FIN_UDP:
 					case TCP2:
 					case FIN_TCP:
 					case TLS2:
 					case FIN_TLS:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
 					case FIN_SCTP:
7cd27114
 					case WS_WSS1:
 					case WS_WSS2:
 					case FIN_WSS:
 						state=OTHER_PROTO;
 						break;
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			case 'W':
 			case 'w':
 				switch(state){
 					case F_PROTO:
 						state=WS_WSS1;
 						vb->transport.s=tmp;
 						break;
 					case OTHER_PROTO:
 						break;
 					case UDP1:
 					case UDP2:
 					case FIN_UDP:
 					case TCP_TLS1:
 					case TCP2:
 					case FIN_TCP:
 					case TLS2:
 					case FIN_TLS:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
 					case FIN_SCTP:
 					case WS_WSS1:
 					case WS_WSS2:
 					case FIN_WSS:
cf4651db
 						state=OTHER_PROTO;
 						break;
8e807134
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
22d4aa5d
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			/*match 2.0*/
 			case '2':
 				switch(state){
 					case F_VER:
 						state=VER1;
 						vb->version.s=tmp;
 						break;
cf4651db
 					case OTHER_PROTO:
 						break;
 					case UDP1:
 					case UDP2:
 					case FIN_UDP:
 					case TCP_TLS1:
 					case TCP2:
 					case FIN_TCP:
 					case TLS2:
 					case FIN_TLS:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
 					case FIN_SCTP:
7cd27114
 					case WS_WSS1:
 					case WS_WSS2:
 					case FIN_WSS:
cf4651db
 						state=OTHER_PROTO;
 						break;
22d4aa5d
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			case '.':
 				switch(state){
 					case VER1:
 						state=VER2;
 						break;
cf4651db
 					case OTHER_PROTO:
 						break;
 					case UDP1:
 					case UDP2:
 					case FIN_UDP:
 					case TCP_TLS1:
 					case TCP2:
 					case FIN_TCP:
 					case TLS2:
 					case FIN_TLS:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
 					case FIN_SCTP:
7cd27114
 					case WS_WSS1:
 					case WS_WSS2:
 					case FIN_WSS:
cf4651db
 						state=OTHER_PROTO;
 						break;
22d4aa5d
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				 break;
 			case '0':
 				switch(state){
 					case VER2:
 						state=FIN_VER;
 						break;
cf4651db
 					case OTHER_PROTO:
 						break;
 					case UDP1:
 					case UDP2:
 					case FIN_UDP:
 					case TCP_TLS1:
 					case TCP2:
 					case FIN_TCP:
 					case TLS2:
 					case FIN_TLS:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
 					case FIN_SCTP:
7cd27114
 					case WS_WSS1:
 					case WS_WSS2:
 					case FIN_WSS:
cf4651db
 						state=OTHER_PROTO;
 						break;
22d4aa5d
 					default:
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
 								" state %d\n", *tmp, state);
 						goto error;
 				}
 				break;
 			default:
cf4651db
 				switch(state){
 					case F_PROTO:
 						state=OTHER_PROTO;
 						vb->transport.s=tmp;
 						break;
 					case OTHER_PROTO:
 						break;
 					case UDP1:
 					case UDP2:
 					case FIN_UDP:
 					case TCP_TLS1:
 					case TCP2:
 					case FIN_TCP:
 					case TLS2:
 					case FIN_TLS:
 					case SCTP1:
 					case SCTP2:
 					case SCTP3:
 					case FIN_SCTP:
7cd27114
 					case WS_WSS1:
 					case WS_WSS2:
 					case FIN_WSS:
cf4651db
 						state=OTHER_PROTO;
 						break;
 					default:
22d4aa5d
 						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
cf4651db
 						" state %d\n", *tmp, state);
22d4aa5d
 						goto error;
cf4651db
 				}
22d4aa5d
 				break;
 		}
 	} /* for tmp*/
 
2bb60634
 	/* we should not be here! if everything is ok > main_via*/
22d4aa5d
 	LOG(L_ERR, "ERROR: parse_via: bad via: end of packet on state=%d\n",
 			state);
 	goto error;
 
2bb60634
  main_via:
 	/* inc tmp to point to the next char*/
22d4aa5d
 	tmp++;
 	c_nest=0;
 	/*state should always be F_HOST here*/;
 	for(;*tmp;tmp++){
 		switch(*tmp){
2bb60634
 		case ' ':
 		case '\t':
 			switch(state){
22d4aa5d
 					case F_HOST:/*eat the spaces*/
 						break;
 					case P_HOST:
18dbc018
 						 /*mark end of host*/
22d4aa5d
 						 vb->host.len=tmp-vb->host.s;
 						 state=L_PORT;
 						 break;
 					case L_PORT: /*eat the spaces*/
 					case F_PORT:
 						break;
 					case P_PORT:
18dbc018
 						/*end of port */
22d4aa5d
 						vb->port_str.len=tmp-vb->port_str.s;
 						state=L_PARAM;
 						break;
 					case L_PARAM: /* eat the space */
 					case F_PARAM:
 						break;
 					case P_PARAM:
 					/*	*tmp=0;*/ /*!?end of param*/
 						state=L_PARAM;
 						break;
 					case L_VIA:
 					case F_VIA: /* eat the space */
 						break;
 					case F_COMMENT:
 					case P_COMMENT:
 						break;
d5f3a5fb
 					case F_IP6HOST: /*no spaces allowed*/
22d4aa5d
 					case P_IP6HOST:
d5f3a5fb
 						LOG(L_ERR, "ERROR:parse_via: bad ipv6 reference\n");
 						goto error;
22d4aa5d
 					case F_CRLF:
 					case F_LF:
 					case F_CR:
 						/*previous=crlf and now =' '*/
 						state=saved_state;
 						break;
 					default:
 						LOG(L_CRIT,"BUG: parse_via"
 							" on <%c>, state=%d\n",*tmp, state);
 						goto  error;
 				}
 			break;
 			case '\n':
 				switch(state){
 					case F_HOST:/*eat the spaces*/
 					case L_PORT: /*eat the spaces*/
 					case F_PORT:
 					case L_PARAM: /* eat the space */
 					case F_PARAM:
 					case F_VIA: /* eat the space */
 					case L_VIA:
 					case F_COMMENT:
 					case P_COMMENT:
 					case F_IP6HOST:
 					case P_IP6HOST:
 						saved_state=state;
 						state=F_LF;
 						break;
 					case P_HOST:
18dbc018
 						 /*mark end of host*/
22d4aa5d
 						 vb->host.len=tmp-vb->host.s;
 						 saved_state=L_PORT;
 						 state=F_LF;
 						 break;
 					case P_PORT:
18dbc018
 						/*end of port */
22d4aa5d
 						vb->port_str.len=tmp-vb->port_str.s;
 						saved_state=L_PARAM;
 						state=F_LF;
 						break;
 					case P_PARAM:
 					/*	*tmp=0;*/ /*!?end of param*/
 						saved_state=L_PARAM;
 						state=F_LF;
 						break;
 					case F_CR:
 						state=F_CRLF;
 						break;
 					case F_CRLF:
 					case F_LF:
 						state=saved_state;
 						goto endofheader;
 					default:
 						LOG(L_CRIT,"BUG: parse_via"
 							" on <%c>\n",*tmp);
 						goto  error;
 				}
 			break;
 		case '\r':
 				switch(state){
 					case F_HOST:/*eat the spaces*/
 					case L_PORT: /*eat the spaces*/
 					case F_PORT:
 					case L_PARAM: /* eat the space */
 					case F_PARAM:
 					case F_VIA: /* eat the space */
 					case L_VIA:
 					case F_COMMENT:
 					case P_COMMENT:
 					case F_IP6HOST:
 					case P_IP6HOST:
 						saved_state=state;
 						state=F_CR;
 						break;
 					case P_HOST:
18dbc018
 						 /*mark end of host*/
22d4aa5d
 						 vb->host.len=tmp-vb->host.s;
 						 saved_state=L_PORT;
 						 state=F_CR;
 						 break;
 					case P_PORT:
18dbc018
 						/*end of port */
22d4aa5d
 						vb->port_str.len=tmp-vb->port_str.s;
 						saved_state=L_PARAM;
 						state=F_CR;
 						break;
 					case P_PARAM:
 					/*	*tmp=0;*/ /*!?end of param*/
 						saved_state=L_PARAM;
 						state=F_CR;
 						break;
 					case F_CRLF:
 					case F_CR:
 					case F_LF:
 						state=saved_state;
 						goto endofheader;
 					default:
 						LOG(L_CRIT,"BUG: parse_via"
 							" on <%c>\n",*tmp);
 						goto  error;
 				}
 			break;
 			
 			case ':':
 				switch(state){
 					case F_HOST:
 					case F_IP6HOST:
d5f3a5fb
 						state=P_IP6HOST;
 						break;
22d4aa5d
 					case P_IP6HOST:
4e2fdd79
 						break;
22d4aa5d
 					case P_HOST:
18dbc018
 						/*mark  end of host*/
22d4aa5d
 						vb->host.len=tmp-vb->host.s;
 						state=F_PORT;
 						break;
 					case L_PORT:
 						state=F_PORT;
 						break;
 					case P_PORT:
 						LOG(L_ERR, "ERROR:parse_via:"
 							" bad port\n");
 						goto error;
 					case L_PARAM:
 					case F_PARAM:
fbc37598
 					case F_PORT:
22d4aa5d
 					case P_PARAM:
 						LOG(L_ERR, "ERROR:parse_via:"
 						" bad char <%c> in state %d\n",
 							*tmp,state);
 						goto error;
 					case L_VIA:
 					case F_VIA:
 						LOG(L_ERR, "ERROR:parse_via:"
 						" bad char in compact via\n");
 						goto error;
 					case F_CRLF:
 					case F_LF:
 					case F_CR:
 						/*previous=crlf and now !=' '*/
 						goto endofheader;
 					case F_COMMENT:/*everything is allowed in a comment*/
 						vb->comment.s=tmp;
 						state=P_COMMENT;
 						break;
 					case P_COMMENT: /*everything is allowed in a comment*/
 						break;
 					default:
 						LOG(L_CRIT,"BUG: parse_via"
 							" on <%c> state %d\n",
 							*tmp, state);
 						goto error;
 				}
 				break;
 			case ';':
 				switch(state){
 					case F_HOST:
 					case F_IP6HOST:
 						LOG(L_ERR,"ERROR:parse_via:"
 							" no host found\n");
 						goto error;
 					case P_IP6HOST:
 						LOG(L_ERR, "ERROR:parse_via: bad ipv6 reference\n");
 						goto error;
 					case P_HOST:
 						vb->host.len=tmp-vb->host.s;
 						state=F_PARAM;
51767629
 						param_start=tmp+1;
22d4aa5d
 						break;
 					case P_PORT:
18dbc018
 						/*mark the end*/
22d4aa5d
 						vb->port_str.len=tmp-vb->port_str.s;
 					case L_PORT:
 					case L_PARAM:
 						state=F_PARAM;
51767629
 						param_start=tmp+1;
22d4aa5d
 						break;
 					case F_PORT:
 						LOG(L_ERR, "ERROR:parse_via:"
 						" bad char <%c> in state %d\n",
 							*tmp,state);
 						goto error;
 					case F_PARAM:
 						LOG(L_ERR,  "ERROR:parse_via:"
 							" null param?\n");
 						goto error;
 					case P_PARAM:
 						/*hmm next, param?*/
 						state=F_PARAM;
51767629
 						param_start=tmp+1;
22d4aa5d
 						break;
 					case L_VIA:
 					case F_VIA:
 						LOG(L_ERR, "ERROR:parse_via:"
 						" bad char <%c> in next via\n",
 							*tmp);
 						goto error;
 					case F_CRLF:
 					case F_LF:
 					case F_CR:
 						/*previous=crlf and now !=' '*/
 						goto endofheader;
 					case F_COMMENT:/*everything is allowed in a comment*/
 						vb->comment.s=tmp;
 						state=P_COMMENT;
 						break;
 					case P_COMMENT: /*everything is allowed in a comment*/
 						break;
 					
 					default:
 						LOG(L_CRIT,"BUG: parse_via"
 							" on <%c> state %d\n",
 							*tmp, state);
 						goto  error;
 				}
 			break;
 			case ',':
 				switch(state){
 					case F_HOST:
 					case F_IP6HOST:
 						LOG(L_ERR,"ERROR:parse_via:"
 							" no host found\n");
 						goto error;
 					case P_IP6HOST:
 						LOG(L_ERR, "ERROR:parse_via: bad ipv6 reference\n");
 						goto error;
 					case P_HOST:
18dbc018
 						/*mark the end*/
22d4aa5d
 						vb->host.len=tmp-vb->host.s;
 						state=F_VIA;
 						break;
 					case P_PORT:
18dbc018
 						/*mark the end*/
22d4aa5d
 						vb->port_str.len=tmp-vb->port_str.s;
 						state=F_VIA;
 						break;
 					case L_PORT:
 					case L_PARAM:
 					case P_PARAM:
 					case L_VIA:
 						state=F_VIA;
 						break;
 					case F_PORT:
 					case F_PARAM:
 						LOG(L_ERR, "ERROR:parse_via:"
 						" invalid char <%c> in state"
 						" %d\n", *tmp,state);
 						goto error;
 					case F_VIA:
 						/* do  nothing,  eat ","*/
 						break;	
 					case F_CRLF:
 					case F_LF:
 					case F_CR:
 						/*previous=crlf and now !=' '*/
 						goto endofheader;
 					case F_COMMENT:/*everything is allowed in a comment*/
 						vb->comment.s=tmp;
 						state=P_COMMENT;
 						break;
 					case P_COMMENT: /*everything is allowed in a comment*/
 						break;
 					default:
 						LOG(L_CRIT,"BUG: parse_via"
 							" on <%c> state %d\n",
 							*tmp, state);
 						goto  error;
 				}
 			break;
 			case '(':
 				switch(state){
 					case F_HOST:
 					case F_PORT:
 					case F_PARAM:
 					case F_VIA:
 					case F_IP6HOST:
 					case P_IP6HOST: /*must be terminated in ']'*/
 						LOG(L_ERR,"ERROR:parse_via"
 							" on <%c> state %d\n",
 							*tmp, state);
 						goto  error;
 					case P_HOST:
18dbc018
 						/*mark the end*/
22d4aa5d
 						vb->host.len=tmp-vb->host.s;
 						state=F_COMMENT;
 						c_nest++;
 						break;
 					case P_PORT:
18dbc018
 						/*mark the end*/
22d4aa5d
 						vb->port_str.len=tmp-vb->port_str.s;
 						state=F_COMMENT;
 						c_nest++;
 						break;
 					case P_PARAM:
18dbc018
 						/*mark the end*/
22d4aa5d
 						vb->params.len=tmp-vb->params.s;
 						state=F_COMMENT;
 						c_nest++;
 						break;
 					case L_PORT:
 					case L_PARAM:
 					case L_VIA:
 						state=F_COMMENT;
 						vb->params.len=tmp-vb->params.s;
 						c_nest++;
 						break;
 					case P_COMMENT:
 					case F_COMMENT:
 						c_nest++;
 						break;
 					case F_CRLF:
 					case F_LF:
 					case F_CR:
 						/*previous=crlf and now !=' '*/
 						goto endofheader;
 					default:
 						LOG(L_CRIT,"BUG: parse_via"
 							" on <%c> state %d\n",
 							*tmp, state);
 						goto  error;
 				}
 			break;
 			case ')':
 				switch(state){
 					case F_COMMENT:
 					case P_COMMENT:
 						if (c_nest){
 							c_nest--;
 							if(c_nest==0){
 								state=L_VIA;
 								vb->comment.len=tmp-vb->comment.s;
 								break;
 							}
 						}else{
 							LOG(L_ERR,"ERROR:"
 							    "parse_via: "
 							    "missing '(' - "
 							    "nesting = %d\n",
 							    c_nest);
 							 goto error;
 						}
 						break;
 					case F_HOST:
 					case F_PORT:
 					case F_PARAM:
 					case F_VIA:
 					case P_HOST:
 					case P_PORT:
 					case P_PARAM:
 					case L_PORT:
 					case L_PARAM:
 					case L_VIA:
 					case F_IP6HOST:
 					case P_IP6HOST:
 						LOG(L_ERR,"ERROR:parse_via"
 							" on <%c> state %d\n",
 							*tmp, state);
 						goto  error;
 					case F_CRLF:
 					case F_LF:
 					case F_CR:
 						/*previous=crlf and now !=' '*/
 						goto endofheader;
 					default:
 						LOG(L_CRIT,"BUG: parse_via"
 							" on <%c> state %d\n",
 							*tmp, state);
 						goto  error;
 				}
 				break;
 			case '[':
 				switch(state){
 					case F_HOST:
d5f3a5fb
 						vb->host.s=tmp; /* mark start here (include [])*/
22d4aa5d
 						state=F_IP6HOST;
 						break;
 					case F_COMMENT:/*everything is allowed in a comment*/
 						vb->comment.s=tmp;
 						state=P_COMMENT;
 						break;
 					case P_COMMENT:
 						break;
 					case F_CRLF:
 					case F_LF:
 					case F_CR:
 						/*previous=crlf and now !=' '*/
 						goto endofheader;
 					default:
 						LOG(L_ERR,"ERROR:parse_via"
 							" on <%c> state %d\n",
 							*tmp, state);
 						goto  error;
 				}
 				break;
 			case ']':
 				switch(state){
 					case P_IP6HOST:
18dbc018
 						/*mark the end*/
d5f3a5fb
 						vb->host.len=(tmp-vb->host.s)+1; /* include "]" */
22d4aa5d
 						state=L_PORT;
 						break;
 					case F_CRLF:
 					case F_LF:
 					case F_CR:
 						/*previous=crlf and now !=' '*/
 						goto endofheader;
 					case F_COMMENT:/*everything is allowed in a comment*/
 						vb->comment.s=tmp;
 						state=P_COMMENT;
 						break;
 					case P_COMMENT:
 						break;
 					default:
 						LOG(L_ERR,"ERROR:parse_via"
 							" on <%c> state %d\n",
 							*tmp, state);
 						goto  error;
 				}
 				break;
 						
 			default:
 				switch(state){
 					case F_HOST:
 						state=P_HOST;
 						vb->host.s=tmp;
 						break;
 					case P_HOST:
 						break;
 					case F_PORT:
 						state=P_PORT;
 						vb->port_str.s=tmp;
 						break;
 					case P_PORT:
 						/*check if number?*/
 						break;
 					case F_PARAM:
 						/*state=P_PARAM*/;
51767629
 						if(vb->params.s==0) vb->params.s=param_start;
89e74c57
 						param=pkg_malloc(sizeof(struct via_param));
 						if (param==0){
 							LOG(L_ERR, "ERROR:parse_via: mem. allocation"
 									" error\n");
 							goto error;
 						}
 						memset(param,0, sizeof(struct via_param));
51767629
 						param->start=param_start;
89e74c57
 						tmp=parse_via_param(tmp, end, &state, &saved_state,
7a068ff3
 											param, vb);
0fcc2741
 
22d4aa5d
 						switch(state){
 							case F_PARAM:
51767629
 								param_start=tmp+1;
 							case L_PARAM:
22d4aa5d
 							case F_LF:
 							case F_CR:
 								break;
 							case F_VIA:
51767629
 								vb->params.len=param->start+param->size
 												-vb->params.s;
22d4aa5d
 								break;
 							case END_OF_HEADER:
51767629
 								vb->params.len=param->start+param->size
 												-vb->params.s;
4b33e625
 								break;
89e74c57
 							case PARAM_ERROR:
7f858173
 								pkg_free(param);
89e74c57
 								goto error;
22d4aa5d
 							default:
6c9b2a39
 								pkg_free(param);
22d4aa5d
 								LOG(L_ERR, "ERROR: parse_via after"
 										" parse_via_param: invalid"
 										" char <%c> on state %d\n",
 										*tmp, state);
 								goto error;
 						}
eaf48c3f
 						/*add param to the list*/
 						if (vb->last_param)	vb->last_param->next=param;
 						else				vb->param_lst=param;
 						vb->last_param=param;
59653eb8
 						/* update param. shortcuts */
 						switch(param->type){
 							case PARAM_BRANCH:
 								vb->branch=param;
 								break;
 							case PARAM_RECEIVED:
 								vb->received=param;
 								break;
 							case PARAM_RPORT:
 								vb->rport=param;
 								break;
 							case PARAM_I:
74f6d2e3
 								vb->i=param;
59653eb8
 								break;
 							case PARAM_ALIAS:
 								vb->alias=param;
 								break;
7a068ff3
 #ifdef USE_COMP
 							case PARAM_COMP:
 								vb->comp=param;
 								/*  moved comp value parsing in via_param */
 								/*
 								if  ((param->value.len==SIGCOMP_NAME_LEN) &&
 									(strncasecmp(param->value.s, SIGCOMP_NAME,
 												SIGCOMP_NAME_LEN)==0)){
 									vb->comp_no=COMP_SIGCOMP;
 								}else if ((param->value.len==SERGZ_NAME_LEN) &&
 										(strncasecmp(param->value.s,
 													SERGZ_NAME,
 													SERGZ_NAME_LEN)==0)){
 									vb->comp_no=COMP_SERGZ;
 								}else{
 									LOG(L_ERR, "ERROR: parse_via: unrecognized"
 										" compression method in comp=%.*s\n",
 										param->value.len, param->value.s);
 								}
 								*/
 								break;
 #endif
59653eb8
 						}
4b33e625
 						
 						if (state==END_OF_HEADER){
 							state=saved_state;
 							goto endofheader;
 						}
22d4aa5d
 						break;
 					case P_PARAM:
 						break;
 					case F_VIA:
 						/*vb->next=tmp;*/ /*???*/
 						goto nextvia;
 					case L_PORT:
 					case L_PARAM:
 					case L_VIA:
 						LOG(L_ERR,"ERROR:parse_via"
 							" on <%c> state %d (default)\n",
 							*tmp, state);
 						goto  error;
 					case F_COMMENT:
 						state=P_COMMENT;
 						vb->comment.s=tmp;
 						break;
 					case P_COMMENT:
 						break;
 					case F_IP6HOST:
 						state=P_IP6HOST;
 						break;
 					case P_IP6HOST:
 						break;
 					case F_CRLF:
 					case F_LF:
 					case F_CR:
 						/*previous=crlf and now !=' '*/
 						goto endofheader;
 					default:
 						LOG(L_ERR, "BUG:parse_via:"
 							" invalid char <%c>"
 							" in state %d\n",
 							*tmp, state);
 						goto error;
 				}
0fcc2741
 
 
 		}
22d4aa5d
 	}
 
 	DBG("end of packet reached, state=%d\n", state);
 	goto endofpacket; /*end of packet, probably should be goto error*/
 	
 endofheader:
 	state=saved_state;
 	DBG("end of header reached, state=%d\n", state);
 endofpacket:
 	/* check if error*/
 	switch(state){
 		case P_HOST:
 		case L_PORT:
 		case P_PORT:
 		case L_PARAM:
 		case P_PARAM:
 		case P_VALUE:
 		case GEN_PARAM:
 		case FIN_HIDDEN:
 		case L_VIA:
 			break;
 		default:
 			LOG(L_ERR, "ERROR: parse_via: invalid via - end of header in"
 					" state %d\n", state);
 			goto error;
 	}
0fcc2741
 
22d4aa5d
 
 	/*
 	if (proto) printf("<SIP/2.0/%s>\n", proto);
 	if (host) printf("host= <%s>\n", host);
 	if (port_str) printf("port= <%s>\n", port_str);
 	if (param) printf("params= <%s>\n", param);
 	if (comment) printf("comment= <%s>\n", comment);
 	if(next_via) printf("next_via= <%s>\n", next_via);
 	*/
 	/*DBG("parse_via: rest=<%s>\n", tmp);*/
0fcc2741
 
34af87af
 	vb->error=PARSE_OK;
22d4aa5d
 	vb->bsize=tmp-buffer;
 	if (vb->port_str.s){
6419a43f
 		vb->port=str2s(vb->port_str.s, vb->port_str.len, &err);
22d4aa5d
 		if (err){
049f64c2
 					LOG(L_ERR, "ERROR: parse_via: invalid port number <%.*s>\n",
6b6f226f
 						vb->port_str.len, ZSW(vb->port_str.s));
22d4aa5d
 					goto error;
 		}
 	}
 	return tmp;
 nextvia:
eaf48c3f
 	DBG("parse_via: next_via\n");
34af87af
 	vb->error=PARSE_OK;
22d4aa5d
 	vb->bsize=tmp-buffer;
 	if (vb->port_str.s){
6419a43f
 		vb->port=str2s(vb->port_str.s, vb->port_str.len, &err);
22d4aa5d
 		if (err){
049f64c2
 					LOG(L_ERR, "ERROR: parse_via: invalid port number <%.*s>\n",
6b6f226f
 						vb->port_str.len, ZSW(vb->port_str.s));
22d4aa5d
 					goto error;
 		}
 	}
 	vb->next=pkg_malloc(sizeof(struct via_body));
 	if (vb->next==0){
 		LOG(L_ERR, "ERROR: parse_via: out of memory\n");
 		goto error;
 	}
 	vb=vb->next;
 	memset(vb, 0, sizeof(struct via_body));
 	buffer=tmp;
 	goto parse_again;
 
 error:
ad04ce27
 	if (end>buffer){
6b6f226f
 		LOG(L_ERR, "ERROR: parse_via on: <%.*s>\n", (int)(end-buffer), ZSW(buffer));
ad04ce27
 	}
 	if ((tmp>buffer)&&(tmp<end)){
 		LOG(L_ERR, "ERROR: parse_via parse error, parsed so far:<%.*s>\n",
6b6f226f
 				(int)(tmp-buffer), ZSW(buffer) );
ad04ce27
 	}else{
 		LOG(L_ERR, "ERROR: parse_via: via parse error\n");
 	}
34af87af
 	vb->error=PARSE_ERROR;
7418afcf
 	vbody->error=PARSE_ERROR; /* make sure the first via body is marked
 								 as bad also */
22d4aa5d
 	return tmp;
 }
 
 
2bb60634
 static inline void free_via_param_list(struct via_param* vp)
 {
 	struct via_param* foo;
 	while(vp){
 		foo=vp;
 		vp=vp->next;
 		pkg_free(foo);
 	}
 }
 
 
 void free_via_list(struct via_body* vb)
 {
 	struct via_body* foo;
 	while(vb){
 		foo=vb;
 		vb=vb->next;
 		if (foo->param_lst) free_via_param_list(foo->param_lst);
 		pkg_free(foo);
 	}
 }
a075952d
 
 int parse_via_header( struct sip_msg *msg, int n, struct via_body** q)
 {
 	struct hdr_field *p;
 	struct via_body *pp = NULL;
 	int i;
 	
 	switch (n) {
 	case 1:
 	case 2:
 		if (!msg->h_via1 && (parse_headers(msg,HDR_VIA_F,0)==-1 || !msg->h_via1)) {
                         DBG("bad msg or missing VIA1 header \n");
                         return -1;
                 }
 		pp = msg->h_via1->parsed;
 		if (n==1) break;
 		pp = pp->next;
 		if (pp) break;
 		
                 if (!msg->h_via2 && (parse_headers(msg,HDR_VIA2_F,0)==-1 || !msg->h_via2)) {
                         DBG("bad msg or missing VIA2 header \n");
                         return -1;
                 }
                 pp = msg->h_via2->parsed;
                 break;
 	default:	
 	        if (!msg->eoh && (parse_headers(msg,HDR_EOH_F,0)==-1 || !msg->eoh)) {
         	        ERR("bad msg while parsing to EOH \n");
 	                return -1;
 		}
 		p = msg->h_via1;
 		i = n;
 		while (i && p) {
 		        if (p->type == HDR_VIA_T) {
 		        	i--;
 		        	pp = p->parsed;
 		        	while (i && (pp->next)) {
 		        		i--;
 		        		pp = pp->next;
 		        	}
 		        }
 			p = p->next;
 		}
 		if (i > 0) {
 			DBG("missing VIA[%d] header\n", n);
 			return -1;
 		}
 	}
 	if (pp) {
 		*q = pp;
 		return 0;
 	} else
 		return -1;
 }