http.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2005 iptelorg GmbH
  5. * Written by Jan Janak <[email protected]>
  6. *
  7. * This file is part of SER, a free SIP server.
  8. *
  9. * SER is free software; you can redistribute it and/or modify it under the
  10. * terms of the GNU General Public License as published by the Free Software
  11. * Foundation; either version 2 of the License, or (at your option) any later
  12. * version
  13. *
  14. * For a license to use the SER software under conditions other than those
  15. * described here, or to purchase support for this software, please contact
  16. * iptel.org by e-mail at the following addresses: [email protected]
  17. *
  18. * SER is distributed in the hope that it will be useful, but WITHOUT ANY
  19. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  21. * details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc., 59
  25. * Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. /** @addtogroup xmlrpc
  28. * @{
  29. */
  30. /** @file
  31. * XML-RPC requests are carried in the body of HTTP requests, but HTTP
  32. * requests does not contain some mandatory SIP headers and thus cannot
  33. * be processed by SER directly. This file contains functions that can
  34. * turn HTTP requests into SIP request by inserting fake mandatory SIP
  35. * headers, such as Via. This allows SER to process such HTTP requests
  36. * and extract the body of the request, which contains the XML-RPC
  37. * document.
  38. */
  39. #include "http.h"
  40. #include "../../mem/mem.h"
  41. #include "../../dprint.h"
  42. #include "../../parser/parse_via.h"
  43. #include "../../data_lump.h"
  44. #include "../../ip_addr.h"
  45. #include "../../msg_translator.h"
  46. #include "../../ut.h"
  47. #include <string.h>
  48. /** Insert fake Via header field into SIP message.
  49. *
  50. * This function takes a SIP message and a Via header field
  51. * as text and inserts the Via header field into the SIP
  52. * message, modifying the data structures within the SIP
  53. * message to make it look that the Via header field was
  54. * received with the message.
  55. *
  56. * @param msg a pointer to the currently processed SIP message
  57. * @param via the Via header field text to be inserted
  58. * @param via_len size of the Via header field being inserted
  59. * @return 0 on succes, a negative number on error.
  60. */
  61. static int insert_fake_via(sip_msg_t* msg, char* via, int via_len)
  62. {
  63. struct via_body* vb = 0;
  64. via_cnt++;
  65. vb = pkg_malloc(sizeof(struct via_body));
  66. if (vb == 0){
  67. ERR("insert_fake_via: Out of memory\n");
  68. goto error;
  69. }
  70. msg->h_via1 = pkg_malloc(sizeof(hdr_field_t));
  71. if (!msg->h_via1) {
  72. ERR("No memory left\n");
  73. goto error;
  74. }
  75. memset(msg->h_via1, 0, sizeof(hdr_field_t));
  76. memset(vb, 0, sizeof(struct via_body));
  77. /* FIXME: The code below would break if the VIA prefix
  78. * gets changed in config.h
  79. */
  80. msg->h_via1->name.s = via;
  81. msg->h_via1->name.len = 3;
  82. msg->h_via1->body.s = via + 5;
  83. msg->h_via1->body.len = via_len - 5 - CRLF_LEN;
  84. msg->h_via1->type = HDR_VIA_T;
  85. msg->h_via1->parsed = vb;
  86. /* This field is used by the msg translator to add a new
  87. * via when forwarding the request. It must point to an existing
  88. * header field because otherwise call to anchor_lump, which does
  89. * hdr.s - buf, would crash
  90. */
  91. vb->hdr.s = msg->headers->name.s;
  92. vb->hdr.len = 0;
  93. msg->via1 = vb;
  94. /* We have to replace the zero terminating character right behind
  95. * CRLF because otherwise the parser will return an error.
  96. * It expects that there is either a next header field or another
  97. * CRLF delimiter
  98. */
  99. via[via_len] = 'a';
  100. parse_via(via + 5, via + via_len + 1, vb);
  101. if (vb->error == PARSE_ERROR){
  102. ERR("Bad via\n");
  103. goto error;
  104. }
  105. if (msg->last_header == 0) {
  106. msg->headers = msg->h_via1;
  107. msg->last_header = msg->h_via1;
  108. } else {
  109. msg->last_header->next = msg->h_via1;
  110. msg->last_header = msg->h_via1;
  111. }
  112. return 0;
  113. error:
  114. if (vb) {
  115. free_via_list(vb);
  116. pkg_free(vb);
  117. }
  118. if (msg->h_via1) pkg_free(msg->h_via1);
  119. return -1;
  120. }
  121. static int insert_via_lump(sip_msg_t* msg, char* via, int via_len)
  122. {
  123. struct lump* anchor;
  124. anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, HDR_VIA_T);
  125. if (anchor == 0) {
  126. ERR("Unable to create anchor\n");
  127. return -1;
  128. }
  129. if (insert_new_lump_after(anchor, via, via_len, HDR_VIA_T) == 0) {
  130. ERR("Unable to insert via lump\n");
  131. return -1;
  132. }
  133. return 0;
  134. }
  135. /** Create a fake Via header field.
  136. *
  137. * This function creates a fake Via header field and inserts
  138. * the fake header field into the header of the HTTP request.
  139. * The fake Via header field contains the source IP address
  140. * and port of the TCP/IP connection.
  141. */
  142. int create_via(sip_msg_t* msg, char* s1, char* s2)
  143. {
  144. char* via;
  145. unsigned int via_len;
  146. str ip, port;
  147. struct hostport hp;
  148. struct dest_info dst;
  149. ip.s = ip_addr2a(&msg->rcv.src_ip);
  150. ip.len = strlen(ip.s);
  151. port.s = int2str(msg->rcv.src_port, &port.len);
  152. hp.host = &ip;
  153. hp.port = &port;
  154. init_dst_from_rcv(&dst, &msg->rcv);
  155. via = via_builder(&via_len, &dst, 0, 0, &hp);
  156. if (!via) {
  157. ERR("Unable to build Via header field\n");
  158. return -1;
  159. }
  160. if (insert_fake_via(msg, via, via_len) < 0) {
  161. pkg_free(via);
  162. return -1;
  163. }
  164. if (insert_via_lump(msg, via, via_len - CRLF_LEN) < 0) {
  165. pkg_free(via);
  166. return -1;
  167. }
  168. return 1;
  169. }
  170. /** @} */