mf_funcs.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00044
00045
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
00054
00055 int is_maxfwd_present( struct sip_msg* msg , str *foo)
00056 {
00057 int x, err;
00058
00059
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
00075 trim_len( foo->len , foo->s , msg->maxforwards->body );
00076
00077
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
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
00096 x--;
00097
00098
00099 STORE_MAXWD_VAL(msg, x);
00100
00101
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';
00121 if ( (b=val%100/10)!=0 || a)
00122 *(p+(i++)) = b+'0';
00123 *(p+(i++)) = '0'+val%10;
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
00135 len = MF_HDR_LEN + CRLF_LEN + 3;
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
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 }