options.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * $Id$
  3. *
  4. * Options Reply Module
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of SIP-router, a free SIP server.
  9. *
  10. * SIP-router is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * SIP-router is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * -------
  26. * 2003-11-11: build_lump_rpl() removed, add_lump_rpl() has flags (bogdan)
  27. */
  28. /*!
  29. * \file siputils/options.c
  30. * \brief SIP-utils :: Options reply modules
  31. * \ingroup siputils
  32. * Module: \ref siputils
  33. */
  34. #ifdef EXTRA_DEBUG
  35. #include <stdlib.h> /* required by abort() */
  36. #endif
  37. #include "options.h"
  38. #include "../../ut.h"
  39. #include "../../mem/mem.h"
  40. #include "../../data_lump_rpl.h"
  41. #include "../../parser/parse_uri.h"
  42. static str opt_200_rpl = str_init("OK");
  43. static str opt_500_rpl = str_init("Server internal error");
  44. int opt_reply(struct sip_msg* _msg, char* _foo, char* _bar) {
  45. str rpl_hf;
  46. int offset = 0;
  47. /* check if it is called for an OPTIONS request */
  48. if (_msg->REQ_METHOD!=METHOD_OPTIONS) {
  49. LM_ERR("called for non-OPTIONS request\n");
  50. return -1;
  51. }
  52. if(_msg->parsed_uri_ok==0 && parse_sip_msg_uri(_msg)<0)
  53. {
  54. LM_ERR("ERROR while parsing the R-URI\n");
  55. return -1;
  56. }
  57. /* FIXME: should we additionally check if ruri == server addresses ?! */
  58. if (_msg->parsed_uri.user.len != 0) {
  59. LM_ERR("ruri contains username\n");
  60. return -1;
  61. }
  62. /* calculate the length and allocated the mem */
  63. rpl_hf.len = ACPT_STR_LEN + ACPT_ENC_STR_LEN + ACPT_LAN_STR_LEN +
  64. SUPT_STR_LEN + 4*HF_SEP_STR_LEN + opt_accept.len + opt_accept_enc.len
  65. + opt_accept_lang.len + opt_supported.len;
  66. rpl_hf.s = (char*)pkg_malloc(rpl_hf.len);
  67. if (!rpl_hf.s) {
  68. LM_CRIT("out of pkg memory\n");
  69. goto error;
  70. }
  71. /* create the header fields */
  72. memcpy(rpl_hf.s, ACPT_STR, ACPT_STR_LEN);
  73. offset = ACPT_STR_LEN;
  74. memcpy(rpl_hf.s + offset, opt_accept.s, opt_accept.len);
  75. offset += opt_accept.len;
  76. memcpy(rpl_hf.s + offset, HF_SEP_STR, HF_SEP_STR_LEN);
  77. offset += HF_SEP_STR_LEN;
  78. memcpy(rpl_hf.s + offset, ACPT_ENC_STR, ACPT_ENC_STR_LEN);
  79. offset += ACPT_ENC_STR_LEN;
  80. memcpy(rpl_hf.s + offset, opt_accept_enc.s, opt_accept_enc.len);
  81. offset += opt_accept_enc.len;
  82. memcpy(rpl_hf.s + offset, HF_SEP_STR, HF_SEP_STR_LEN);
  83. offset += HF_SEP_STR_LEN;
  84. memcpy(rpl_hf.s + offset, ACPT_LAN_STR, ACPT_LAN_STR_LEN);
  85. offset += ACPT_LAN_STR_LEN;
  86. memcpy(rpl_hf.s + offset, opt_accept_lang.s, opt_accept_lang.len);
  87. offset += opt_accept_lang.len;
  88. memcpy(rpl_hf.s + offset, HF_SEP_STR, HF_SEP_STR_LEN);
  89. offset += HF_SEP_STR_LEN;
  90. memcpy(rpl_hf.s + offset, SUPT_STR, SUPT_STR_LEN);
  91. offset += SUPT_STR_LEN;
  92. memcpy(rpl_hf.s + offset, opt_supported.s, opt_supported.len);
  93. offset += opt_supported.len;
  94. memcpy(rpl_hf.s + offset, HF_SEP_STR, HF_SEP_STR_LEN);
  95. #ifdef EXTRA_DEBUG
  96. offset += HF_SEP_STR_LEN;
  97. if (offset != rpl_hf.len) {
  98. LM_CRIT("headerlength (%i) != offset (%i)\n", rpl_hf.len, offset);
  99. abort();
  100. }
  101. #endif
  102. if (add_lump_rpl( _msg, rpl_hf.s, rpl_hf.len,
  103. LUMP_RPL_HDR|LUMP_RPL_NODUP)!=0) {
  104. if (opt_slb.freply(_msg, 200, &opt_200_rpl) == -1) {
  105. LM_ERR("failed to send 200 via send_reply\n");
  106. return -1;
  107. }
  108. else
  109. return 0;
  110. } else {
  111. pkg_free(rpl_hf.s);
  112. LM_ERR("add_lump_rpl failed\n");
  113. }
  114. error:
  115. if (opt_slb.freply(_msg, 500, &opt_500_rpl) == -1) {
  116. LM_ERR("failed to send 500 via send_reply\n");
  117. return -1;
  118. }
  119. else
  120. return 0;
  121. }