modules_k/maxfwd/mf_funcs.c
31ccf6a2
 /*
  * $Id$
  *
  * Copyright (C) 2001-2003 FhG Fokus
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
31ccf6a2
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
31ccf6a2
  * 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
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
31ccf6a2
  * 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 
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  * 
  * History:
  * ----------
  * 2003-02-28 scratchpad compatibility abandoned (jiri)
  * 2002-01-28 scratchpad removed (jiri)
  * 2004-08-15 max value of max-fwd header is configurable (bogdan)
9202af99
  * 2005-11-03 MF value saved in msg->maxforwards->parsed (bogdan)
31ccf6a2
  */
 
 
 #include <stdlib.h>
 #include <string.h>
 
 #include "mf_funcs.h"
 #include "../../mem/mem.h"
 #include "../../ut.h"
 #include "../../data_lump.h"
 
 
 #define MF_HDR "Max-Forwards: "
 #define MF_HDR_LEN (sizeof(MF_HDR) - 1)
 
9202af99
 /* do a tricky thing and keep the parsed value of MAXFWD hdr incremented 
  * by one in order to make difference between 0 (not set)
  * and 0 (zero value) - bogdan */
 #define IS_MAXWD_STORED(_msg_) \
 	((_msg_)->maxforwards->parsed)
 #define STORE_MAXWD_VAL(_msg_,_val_) \
d537f7cb
 	(_msg_)->maxforwards->parsed = ((void*)(long)((_val_)+1))
9202af99
 #define FETCH_MAXWD_VAL(_msg_) \
 	(((int)(long)(_msg_)->maxforwards->parsed)-1)
31ccf6a2
 
 /* looks for the MAX FORWARDS header
    returns the its value, -1 if is not present or -2 for error */
 int is_maxfwd_present( struct sip_msg* msg , str *foo)
 {
 	int x, err;
 
 	/* lookup into the message for MAX FORWARDS header*/
 	if ( !msg->maxforwards ) {
b68fbcd1
 		if  ( parse_headers( msg , HDR_MAXFORWARDS_F, 0 )==-1 ){
e4a6f52c
 			LM_ERR("parsing MAX_FORWARD header failed!\n");
31ccf6a2
 			return -2;
 		}
 		if (!msg->maxforwards) {
e4a6f52c
 			LM_DBG("max_forwards header not found!\n");
31ccf6a2
 			return -1;
 		}
9202af99
 	} else if (IS_MAXWD_STORED(msg)) {
498cdbc3
 		trim_len( foo->len , foo->s , msg->maxforwards->body );
9202af99
 		return FETCH_MAXWD_VAL(msg);
31ccf6a2
 	}
 
 	/* if header is present, trim to get only the string containing numbers */
 	trim_len( foo->len , foo->s , msg->maxforwards->body );
 
 	/* convert from string to number */
 	x = str2s( foo->s,foo->len,&err);
 	if (err){
e4a6f52c
 		LM_ERR("unable to parse the max forwards number\n");
31ccf6a2
 		return -2;
 	}
9202af99
 	/* store the parsed values */
 	STORE_MAXWD_VAL(msg, x);
e4a6f52c
 	LM_DBG("value = %d \n",x);
31ccf6a2
 	return x;
 }
 
 
 
 int decrement_maxfwd( struct sip_msg* msg , int x, str *s)
 {
 	int i;
 
498cdbc3
 	/* decrement the value */
31ccf6a2
 	x--;
498cdbc3
 
 	/* update the stored value */
 	STORE_MAXWD_VAL(msg, x);
 
 	/*rewriting the max-fwd value in the message (buf and orig)*/
b68fbcd1
 	for(i = s->len - 1; i >= 0; i--) {
31ccf6a2
 		s->s[i] = (x % 10) + '0';
 		x /= 10;
 		if (x==0) {
 			i = i - 1;
 			break;
 		}
 	}
 	while(i >= 0) s->s[i--] = ' ';
 
9202af99
 	return 0;
31ccf6a2
 }
 
 int add_maxfwd_header( struct sip_msg* msg , unsigned int val )
 {
 	unsigned int  len;
 	char          *buf;
 	struct lump*  anchor;
 
 	/* constructing the header */
 	len = MF_HDR_LEN /*"MAX-FORWARDS: "*/+ CRLF_LEN + 3/*val max on 3 digits*/;
 
 	buf = (char*)pkg_malloc( len );
 	if (!buf) {
6b66dff2
 		LM_ERR("add_maxfwd_header: no more pkg memory\n");
772dbe96
 		goto error;
31ccf6a2
 	}
 	memcpy( buf , MF_HDR, MF_HDR_LEN );
 	len = MF_HDR_LEN ;
 	len += btostr( buf+len , val );
 	memcpy( buf+len , CRLF , CRLF_LEN );
 	len +=CRLF_LEN;
 
 	/*inserts the header at the beginning of the message*/
 	anchor = anchor_lump(msg, msg->headers->name.s - msg->buf, 0 , 0);
 	if (anchor == 0) {
6b66dff2
 		LM_ERR("add_maxfwd_header: failed to get anchor\n");
31ccf6a2
 		goto error1;
 	}
 
 	if (insert_new_lump_before(anchor, buf, len, 0) == 0) {
6b66dff2
 		LM_ERR("add_maxfwd_header: failed to insert MAX-FORWARDS lump\n");
31ccf6a2
 		goto error1;
 	}
 
9202af99
 	return 0;
31ccf6a2
 error1:
 	pkg_free( buf );
 error:
 	return -1;
 }