functions.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * script functions of utils module
  3. *
  4. * Copyright (C) 2008 Juha Heinanen
  5. * Copyright (C) 2013 Carsten Bock, ng-voice GmbH
  6. *
  7. * This file is part of Kamailio, a free SIP server.
  8. *
  9. * Kamailio is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * Kamailio is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. /*!
  25. * \file
  26. * \brief SIP-router utils ::
  27. * \ingroup utils
  28. * Module: \ref utils
  29. */
  30. #include <curl/curl.h>
  31. #include "../../mod_fix.h"
  32. #include "../../pvar.h"
  33. #include "../../route_struct.h"
  34. #include "../../ut.h"
  35. #include "../../mem/mem.h"
  36. #include "../../parser/msg_parser.h"
  37. #include "../../lvalue.h"
  38. #include "utils.h"
  39. /*
  40. * curl write function that saves received data as zero terminated
  41. * to stream. Returns the amount of data taken care of.
  42. */
  43. size_t write_function( void *ptr, size_t size, size_t nmemb, void *stream)
  44. {
  45. /* Allocate memory and copy */
  46. char* data;
  47. data = (char*)pkg_malloc((size* nmemb) + 1);
  48. if (data == NULL) {
  49. LM_ERR("cannot allocate memory for stream\n");
  50. return CURLE_WRITE_ERROR;
  51. }
  52. memcpy(data, (char*)ptr, size* nmemb);
  53. data[nmemb] = '\0';
  54. *((char**) stream) = data;
  55. return size* nmemb;
  56. }
  57. /*
  58. * Performs http_query and saves possible result (first body line of reply)
  59. * to pvar.
  60. */
  61. int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post)
  62. {
  63. CURL *curl;
  64. CURLcode res;
  65. str value, post_value;
  66. char *url, *at, *post;
  67. char* stream;
  68. long stat;
  69. pv_spec_t *dst;
  70. pv_value_t val;
  71. double download_size;
  72. if (fixup_get_svalue(_m, (gparam_p)_url, &value) != 0) {
  73. LM_ERR("cannot get page value\n");
  74. return -1;
  75. }
  76. curl = curl_easy_init();
  77. if (curl == NULL) {
  78. LM_ERR("failed to initialize curl\n");
  79. return -1;
  80. }
  81. url = pkg_malloc(value.len + 1);
  82. if (url == NULL) {
  83. curl_easy_cleanup(curl);
  84. LM_ERR("cannot allocate pkg memory for url\n");
  85. return -1;
  86. }
  87. memcpy(url, value.s, value.len);
  88. *(url + value.len) = (char)0;
  89. curl_easy_setopt(curl, CURLOPT_URL, url);
  90. if (_post) {
  91. /* Now specify we want to POST data */
  92. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  93. if (fixup_get_svalue(_m, (gparam_p)_post, &post_value) != 0) {
  94. LM_ERR("cannot get post value\n");
  95. pkg_free(url);
  96. return -1;
  97. }
  98. post = pkg_malloc(post_value.len + 1);
  99. if (post == NULL) {
  100. curl_easy_cleanup(curl);
  101. pkg_free(url);
  102. LM_ERR("cannot allocate pkg memory for post\n");
  103. return -1;
  104. }
  105. memcpy(post, post_value.s, post_value.len);
  106. *(post + post_value.len) = (char)0;
  107. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
  108. }
  109. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, (long)1);
  110. curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)http_query_timeout);
  111. stream = NULL;
  112. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_function);
  113. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);
  114. res = curl_easy_perform(curl);
  115. pkg_free(url);
  116. if (_post) {
  117. pkg_free(post);
  118. }
  119. if (res != CURLE_OK) {
  120. /* http://curl.haxx.se/libcurl/c/libcurl-errors.html */
  121. if (res == CURLE_COULDNT_CONNECT) {
  122. LM_WARN("failed to connect() to host\n");
  123. } else if ( res == CURLE_COULDNT_RESOLVE_HOST ) {
  124. LM_WARN("couldn't resolve host\n");
  125. } else {
  126. LM_ERR("failed to perform curl (%d)\n", res);
  127. }
  128. curl_easy_cleanup(curl);
  129. if(stream)
  130. pkg_free(stream);
  131. return -1;
  132. }
  133. curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &stat);
  134. if ((stat >= 200) && (stat < 500)) {
  135. curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &download_size);
  136. LM_DBG("http_query download size: %u\n", (unsigned int)download_size);
  137. /* search for line feed */
  138. at = memchr(stream, (char)10, download_size);
  139. if (at == NULL) {
  140. /* not found: use whole stream */
  141. at = stream + (unsigned int)download_size;
  142. }
  143. val.rs.s = stream;
  144. val.rs.len = at - stream;
  145. LM_DBG("http_query result: %.*s\n", val.rs.len, val.rs.s);
  146. val.flags = PV_VAL_STR;
  147. dst = (pv_spec_t *)_dst;
  148. dst->setf(_m, &dst->pvp, (int)EQ_T, &val);
  149. }
  150. curl_easy_cleanup(curl);
  151. pkg_free(stream);
  152. return stat;
  153. }