"Fossies" - the Fresh Open Source Software Archive

Member "opensips-3.0.1/modules/maxfwd/mf_funcs.c" (16 Apr 2019, 4005 Bytes) of package /linux/misc/opensips-3.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mf_funcs.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) 2001-2003 FhG Fokus
    3  *
    4  * This file is part of opensips, a free SIP server.
    5  *
    6  * opensips is free software; you can redistribute it and/or modify
    7  * it under the terms of the GNU General Public License as published by
    8  * the Free Software Foundation; either version 2 of the License, or
    9  * (at your option) any later version
   10  *
   11  * opensips is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public License
   17  * along with this program; if not, write to the Free Software
   18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
   19  *
   20  * History:
   21  * ----------
   22  * 2003-02-28 scratchpad compatibility abandoned (jiri)
   23  * 2002-01-28 scratchpad removed (jiri)
   24  * 2004-08-15 max value of max-fwd header is configurable (bogdan)
   25  * 2005-11-03 MF value saved in msg->maxforwards->parsed (bogdan)
   26  */
   27 
   28 
   29 #include <stdlib.h>
   30 #include <string.h>
   31 
   32 #include "mf_funcs.h"
   33 #include "../../mem/mem.h"
   34 #include "../../ut.h"
   35 #include "../../data_lump.h"
   36 
   37 
   38 #define MF_HDR "Max-Forwards: "
   39 #define MF_HDR_LEN (sizeof(MF_HDR) - 1)
   40 
   41 /* do a tricky thing and keep the parsed value of MAXFWD hdr incremented
   42  * by one in order to make difference between 0 (not set)
   43  * and 0 (zero value) - bogdan */
   44 #define IS_MAXWD_STORED(_msg_) \
   45     ((_msg_)->maxforwards->parsed)
   46 #define STORE_MAXWD_VAL(_msg_,_val_) \
   47     (_msg_)->maxforwards->parsed = ((void*)(long)((_val_)+1))
   48 #define FETCH_MAXWD_VAL(_msg_) \
   49     (((int)(long)(_msg_)->maxforwards->parsed)-1)
   50 
   51 /* looks for the MAX FORWARDS header
   52    returns the its value, -1 if is not present or -2 for error */
   53 int is_maxfwd_present( struct sip_msg* msg , str *foo)
   54 {
   55     int x, err;
   56 
   57     /* lookup into the message for MAX FORWARDS header*/
   58     if ( !msg->maxforwards ) {
   59         if  ( parse_headers( msg , HDR_MAXFORWARDS_F, 0 )==-1 ){
   60             LM_ERR("parsing MAX_FORWARD header failed!\n");
   61             return -2;
   62         }
   63         if (!msg->maxforwards) {
   64             LM_DBG("max_forwards header not found!\n");
   65             return -1;
   66         }
   67     } else if (IS_MAXWD_STORED(msg)) {
   68         trim_len( foo->len , foo->s , msg->maxforwards->body );
   69         return FETCH_MAXWD_VAL(msg);
   70     }
   71 
   72     /* if header is present, trim to get only the string containing numbers */
   73     trim_len( foo->len , foo->s , msg->maxforwards->body );
   74 
   75     /* convert from string to number */
   76     x = str2s( foo->s,foo->len,&err);
   77     if (err){
   78         LM_ERR("unable to parse the max forwards number\n");
   79         return -2;
   80     }
   81     /* store the parsed values */
   82     STORE_MAXWD_VAL(msg, x);
   83     LM_DBG("value = %d \n",x);
   84     return x;
   85 }
   86 
   87 
   88 
   89 int decrement_maxfwd( struct sip_msg* msg , int x, str *s)
   90 {
   91     int i;
   92 
   93     /* decrement the value */
   94     x--;
   95 
   96     /* update the stored value */
   97     STORE_MAXWD_VAL(msg, x);
   98 
   99     /* rewriting the max-fwd value in the message (buf and orig) */
  100     for(i = s->len - 1; i >= 0; i--) {
  101         s->s[i] = (x % 10) + '0';
  102         x /= 10;
  103         if (x==0) {
  104             i = i - 1;
  105             break;
  106         }
  107     }
  108     while(i >= 0) s->s[i--] = ' ';
  109 
  110     return 0;
  111 }
  112 
  113 
  114 
  115 int add_maxfwd_header( struct sip_msg* msg , unsigned int val )
  116 {
  117     unsigned int  len;
  118     char          *buf;
  119     struct lump*  anchor;
  120 
  121     /* constructing the header */
  122     len = MF_HDR_LEN /*"MAX-FORWARDS: "*/+ CRLF_LEN + 3/*val max on 3 digits*/;
  123 
  124     buf = (char*)pkg_malloc( len );
  125     if (!buf) {
  126         LM_ERR("add_maxfwd_header: no more pkg memory\n");
  127         goto error;
  128     }
  129     memcpy( buf , MF_HDR, MF_HDR_LEN );
  130     len = MF_HDR_LEN ;
  131     len += btostr( buf+len , val );
  132     memcpy( buf+len , CRLF , CRLF_LEN );
  133     len +=CRLF_LEN;
  134 
  135     /*inserts the header at the beginning of the message*/
  136     anchor = anchor_lump(msg, msg->headers->name.s - msg->buf, 0);
  137     if (anchor == 0) {
  138         LM_ERR("add_maxfwd_header: failed to get anchor\n");
  139         goto error1;
  140     }
  141 
  142     if (insert_new_lump_before(anchor, buf, len, 0) == 0) {
  143         LM_ERR("add_maxfwd_header: failed to insert MAX-FORWARDS lump\n");
  144         goto error1;
  145     }
  146 
  147     return 0;
  148 error1:
  149     pkg_free( buf );
  150 error:
  151     return -1;
  152 }