char_msg_val.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2010 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * char_msg_val.h
  20. */
  21. /*
  22. * History:
  23. * --------
  24. * 2010-02-10 moved from parser/msg_parser.h and added tag only mode
  25. * by default (andrei)
  26. */
  27. /** compute the characteristic value of a message.
  28. * @file
  29. * @ingroup core
  30. */
  31. /* Defines:
  32. * BRANCH_INCLUDE_FROMTO_BODY - if defined the old (pre 3.1) mode of
  33. * including the full from & to bodies will be used (instead of only the
  34. * tags).
  35. * BRANCH_IGNORE_3261_VIA - if defined, no check and special/simpler handling
  36. * of messages with 3261 cookies in the via branch will be made (same
  37. * behaviour as in pre 3.1 versions).
  38. */
  39. #ifndef __char_msg_val_h
  40. #define __char_msg_val_h
  41. #include "comp_defs.h"
  42. #include "compiler_opt.h"
  43. #include "str.h"
  44. #include "parser/msg_parser.h"
  45. #include "parser/parse_to.h"
  46. #include "parser/parse_from.h"
  47. #include "md5utils.h"
  48. /*! \brief calculate characteristic value of a message -- this value
  49. is used to identify a transaction during the process of
  50. reply matching
  51. */
  52. inline static int char_msg_val( struct sip_msg *msg, char *cv )
  53. {
  54. str src[8];
  55. if (unlikely(!check_transaction_quadruple(msg))) {
  56. LM_ERR("can't calculate char_value due to a parsing error\n");
  57. memset( cv, '0', MD5_LEN );
  58. return 0;
  59. }
  60. #ifndef BRANCH_IGNORE_3261_VIA
  61. if (likely(msg->via1->branch && msg->via1->branch->value.len>MCOOKIE_LEN &&
  62. memcmp(msg->via1->branch->value.s, MCOOKIE, MCOOKIE_LEN)==0)){
  63. /* 3261 branch cookie present => hash only the received branch ID */
  64. src[0]=msg->via1->branch->value;
  65. MD5StringArray ( cv, src, 1 );
  66. return 1; /* success */
  67. }
  68. #endif /* BRANCH_IGNORE_3261_VIA */
  69. #ifdef BRANCH_INCLUDE_FROMTO_BODY
  70. /* use the from & to full bodies */
  71. src[0]= msg->from->body;
  72. src[1]= msg->to->body;
  73. #else
  74. /* to body is automatically parsed (via check_transactionquadruple /
  75. parse_header), but the from body has to be parsed manually */
  76. if (msg->from->parsed==0){
  77. /* parse from body */
  78. if (unlikely(parse_from_header(msg) == -1)){
  79. LM_ERR("error while parsing From header\n");
  80. return 0;
  81. }
  82. }
  83. /* use only the from & to tags */
  84. src[0]=get_from(msg)->tag_value;
  85. src[1]=get_to(msg)->tag_value;
  86. #endif /* BRANCH_INCLUDE_FROMTO_BODY */
  87. src[2]= msg->callid->body;
  88. src[3]= msg->first_line.u.request.uri;
  89. src[4]= get_cseq( msg )->number;
  90. /* topmost Via is part of transaction key as well ! */
  91. src[5]= msg->via1->host;
  92. src[6]= msg->via1->port_str;
  93. if (likely(msg->via1->branch)) {
  94. src[7]= msg->via1->branch->value;
  95. MD5StringArray ( cv, src, 8 );
  96. } else {
  97. MD5StringArray( cv, src, 7 );
  98. }
  99. return 1;
  100. }
  101. #endif /*__char_msg_val_h*/
  102. /* vi: set ts=4 sw=4 tw=79:ai:cindent: */