mf_funcs.c 3.9 KB

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