mf_funcs.c

Go to the documentation of this file.
00001 /*
00002  * $Id: mf_funcs.c 5252 2008-11-24 17:50:16Z henningw $
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  * 
00022  * History:
00023  * ----------
00024  * 2003-02-28 scratchpad compatibility abandoned (jiri)
00025  * 2002-01-28 scratchpad removed (jiri)
00026  * 2004-08-15 max value of max-fwd header is configurable (bogdan)
00027  * 2005-11-03 MF value saved in msg->maxforwards->parsed (bogdan)
00028  */
00029 
00030 
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 #include "mf_funcs.h"
00035 #include "../../mem/mem.h"
00036 #include "../../ut.h"
00037 #include "../../data_lump.h"
00038 
00039 
00040 #define MF_HDR "Max-Forwards: "
00041 #define MF_HDR_LEN (sizeof(MF_HDR) - 1)
00042 
00043 /* do a tricky thing and keep the parsed value of MAXFWD hdr incremented 
00044  * by one in order to make difference between 0 (not set)
00045  * and 0 (zero value) - bogdan */
00046 #define IS_MAXWD_STORED(_msg_) \
00047    ((_msg_)->maxforwards->parsed)
00048 #define STORE_MAXWD_VAL(_msg_,_val_) \
00049    (_msg_)->maxforwards->parsed = ((void*)(long)((_val_)+1))
00050 #define FETCH_MAXWD_VAL(_msg_) \
00051    (((int)(long)(_msg_)->maxforwards->parsed)-1)
00052 
00053 /* looks for the MAX FORWARDS header
00054    returns the its value, -1 if is not present or -2 for error */
00055 int is_maxfwd_present( struct sip_msg* msg , str *foo)
00056 {
00057    int x, err;
00058 
00059    /* lookup into the message for MAX FORWARDS header*/
00060    if ( !msg->maxforwards ) {
00061       if  ( parse_headers( msg , HDR_MAXFORWARDS_F, 0 )==-1 ){
00062          LM_ERR("parsing MAX_FORWARD header failed!\n");
00063          return -2;
00064       }
00065       if (!msg->maxforwards) {
00066          LM_DBG("max_forwards header not found!\n");
00067          return -1;
00068       }
00069    } else if (IS_MAXWD_STORED(msg)) {
00070       trim_len( foo->len , foo->s , msg->maxforwards->body );
00071       return FETCH_MAXWD_VAL(msg);
00072    }
00073 
00074    /* if header is present, trim to get only the string containing numbers */
00075    trim_len( foo->len , foo->s , msg->maxforwards->body );
00076 
00077    /* convert from string to number */
00078    x = str2s( foo->s,foo->len,&err);
00079    if (err){
00080       LM_ERR("unable to parse the max forwards number\n");
00081       return -2;
00082    }
00083    /* store the parsed values */
00084    STORE_MAXWD_VAL(msg, x);
00085    LM_DBG("value = %d \n",x);
00086    return x;
00087 }
00088 
00089 
00090 
00091 int decrement_maxfwd( struct sip_msg* msg , int x, str *s)
00092 {
00093    int i;
00094 
00095    /* decrement the value */
00096    x--;
00097 
00098    /* update the stored value */
00099    STORE_MAXWD_VAL(msg, x);
00100 
00101    /*rewriting the max-fwd value in the message (buf and orig)*/
00102    for(i = s->len - 1; i >= 0; i--) {
00103       s->s[i] = (x % 10) + '0';
00104       x /= 10;
00105       if (x==0) {
00106          i = i - 1;
00107          break;
00108       }
00109    }
00110    while(i >= 0) s->s[i--] = ' ';
00111 
00112    return 0;
00113 }
00114 
00115 static inline int btostr( char *p,  unsigned char val)
00116 {
00117    unsigned int a,b,i =0;
00118 
00119    if ( (a=val/100)!=0 )
00120       *(p+(i++)) = a+'0';         /*first digit*/
00121    if ( (b=val%100/10)!=0 || a)
00122       *(p+(i++)) = b+'0';        /*second digit*/
00123    *(p+(i++)) = '0'+val%10;              /*third digit*/
00124 
00125    return i;
00126 }
00127 
00128 int add_maxfwd_header( struct sip_msg* msg , unsigned int val )
00129 {
00130    unsigned int  len;
00131    char          *buf;
00132    struct lump*  anchor;
00133 
00134    /* constructing the header */
00135    len = MF_HDR_LEN /*"MAX-FORWARDS: "*/+ CRLF_LEN + 3/*val max on 3 digits*/;
00136 
00137    buf = (char*)pkg_malloc( len );
00138    if (!buf) {
00139       LM_ERR("add_maxfwd_header: no more pkg memory\n");
00140       goto error;
00141    }
00142    memcpy( buf , MF_HDR, MF_HDR_LEN );
00143    len = MF_HDR_LEN ;
00144    len += btostr( buf+len , val );
00145    memcpy( buf+len , CRLF , CRLF_LEN );
00146    len +=CRLF_LEN;
00147 
00148    /*inserts the header at the beginning of the message*/
00149    anchor = anchor_lump(msg, msg->headers->name.s - msg->buf, 0 , 0);
00150    if (anchor == 0) {
00151       LM_ERR("add_maxfwd_header: failed to get anchor\n");
00152       goto error1;
00153    }
00154 
00155    if (insert_new_lump_before(anchor, buf, len, 0) == 0) {
00156       LM_ERR("add_maxfwd_header: failed to insert MAX-FORWARDS lump\n");
00157       goto error1;
00158    }
00159 
00160    return 0;
00161 error1:
00162    pkg_free( buf );
00163 error:
00164    return -1;
00165 }

Generated on Wed May 23 20:00:27 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6