mod_options.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * $Id$
  3. *
  4. * Options Reply Module
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser 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. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. * History:
  30. * -------
  31. * 2003-11-11: build_lump_rpl() removed, add_lump_rpl() has flags (bogdan)
  32. */
  33. #include <stdlib.h>
  34. #include "mod_options.h"
  35. #include "../../sr_module.h"
  36. #include "../../config.h"
  37. #include "../../modules/sl/sl.h"
  38. #include "../../mem/mem.h"
  39. #include "../../data_lump_rpl.h"
  40. #include "../../parser/msg_parser.h"
  41. #include "../../parser/parse_uri.h"
  42. MODULE_VERSION
  43. static str acpt_body = STR_STATIC_INIT("*/*");
  44. static str acpt_enc_body = STR_STATIC_INIT("");
  45. static str acpt_lan_body = STR_STATIC_INIT("en");
  46. static str supt_body = STR_STATIC_INIT("");
  47. static str contact = STR_STATIC_INIT("");
  48. static str cont_param = STR_STATIC_INIT("");
  49. #define ADD_CONT_OFF 0
  50. #define ADD_CONT_MODPARAM 1
  51. #define ADD_CONT_IP 2
  52. #define ADD_CONT_RURI 3
  53. static int add_cont = ADD_CONT_OFF;
  54. /*
  55. * sl API structure for stateless reply
  56. */
  57. sl_api_t slb;
  58. static int mod_init(void);
  59. static int opt_reply(struct sip_msg* _msg, char* _foo, char* _bar);
  60. /*
  61. * Exported functions
  62. */
  63. static cmd_export_t cmds[] = {
  64. {"options_reply", (cmd_function)opt_reply, 0, 0,
  65. REQUEST_ROUTE | FAILURE_ROUTE},
  66. {0, 0, 0, 0}
  67. };
  68. /*
  69. * Exported parameters
  70. */
  71. static param_export_t params[] = {
  72. {"accept", PARAM_STR, &acpt_body},
  73. {"accept_encoding", PARAM_STR, &acpt_enc_body},
  74. {"accept_language", PARAM_STR, &acpt_lan_body},
  75. {"supported", PARAM_STR, &supt_body},
  76. {"contact", PARAM_STR, &contact},
  77. {"contact_param", PARAM_STR, &cont_param},
  78. {0, 0, 0}
  79. };
  80. /*
  81. * Module description
  82. */
  83. struct module_exports exports = {
  84. "options", /* Module name */
  85. cmds, /* Exported functions */
  86. 0, /* RPC methods */
  87. params, /* Exported parameters */
  88. mod_init, /* Initialization function */
  89. 0, /* Response function */
  90. 0, /* Destroy function */
  91. 0, /* OnCancel function */
  92. 0 /* Child init function */
  93. };
  94. /*
  95. * initialize module
  96. */
  97. static int mod_init(void)
  98. {
  99. DBG("options - initializing\n");
  100. /* bind the SL API */
  101. if (sl_load_api(&slb)!=0) {
  102. LM_ERR("cannot bind to SL API\n");
  103. return -1;
  104. }
  105. if (contact.len > 0) {
  106. LOG(L_DBG, "contact: '%.*s'\n", contact.len, contact.s);
  107. add_cont = ADD_CONT_MODPARAM;
  108. if (strncasecmp("dstip", contact.s, contact.len) == 0) {
  109. contact.s = NULL;
  110. contact.len = 0;
  111. add_cont = ADD_CONT_IP;
  112. }
  113. else if (strncasecmp("ruri", contact.s, contact.len) == 0) {
  114. contact.s = NULL;
  115. contact.len = 0;
  116. add_cont = ADD_CONT_RURI;
  117. }
  118. /* more possible candidates here could be the To
  119. * or topmost Route URI */
  120. }
  121. if (cont_param.len > 0 && add_cont == ADD_CONT_OFF) {
  122. /* by default we add the IP */
  123. add_cont = ADD_CONT_IP;
  124. }
  125. return 0;
  126. }
  127. /*
  128. * calculates and returns the length of the Contact header to be inserted.
  129. */
  130. static int contact_length(struct sip_msg* _msg)
  131. {
  132. int ret = 0;
  133. if (add_cont == ADD_CONT_OFF) {
  134. return 0;
  135. }
  136. ret = CONT_STR_LEN + CRLF_LEN + 1;
  137. if (add_cont == ADD_CONT_MODPARAM) {
  138. ret += contact.len;
  139. }
  140. else if (add_cont == ADD_CONT_IP) {
  141. ret += _msg->rcv.bind_address->name.len;
  142. ret += 1;
  143. ret += _msg->rcv.bind_address->port_no_str.len;
  144. switch (_msg->rcv.bind_address->proto) {
  145. case PROTO_NONE:
  146. case PROTO_UDP:
  147. break;
  148. case PROTO_TCP:
  149. case PROTO_TLS:
  150. ret += TRANSPORT_PARAM_LEN + 3;
  151. break;
  152. case PROTO_SCTP:
  153. ret += TRANSPORT_PARAM_LEN + 4;
  154. break;
  155. default:
  156. LOG(L_CRIT, "contact_length(): unsupported proto (%d)\n",
  157. _msg->rcv.bind_address->proto);
  158. }
  159. }
  160. else if (add_cont == ADD_CONT_RURI) {
  161. if (parse_sip_msg_uri(_msg) != 1) {
  162. LOG(L_WARN, "add_contact(): failed to parse ruri\n");
  163. }
  164. if (_msg->parsed_orig_ruri_ok) {
  165. ret += _msg->parsed_orig_ruri.host.len;
  166. if (_msg->parsed_orig_ruri.port.len > 0) {
  167. ret += 1;
  168. ret += _msg->parsed_orig_ruri.port.len;
  169. }
  170. }
  171. else if (_msg->parsed_uri_ok){
  172. ret += _msg->parsed_uri.host.len;
  173. if (_msg->parsed_uri.port.len > 0) {
  174. ret += 1;
  175. ret += _msg->parsed_uri.port.len;
  176. }
  177. }
  178. }
  179. if (cont_param.len > 0) {
  180. if (*(cont_param.s) != ';') {
  181. ret += 1;
  182. }
  183. ret += cont_param.len;
  184. }
  185. return ret;
  186. }
  187. /*
  188. * inserts the Contact header at _dst and returns the number of written bytes
  189. */
  190. static int add_contact(struct sip_msg* _msg, char* _dst)
  191. {
  192. int ret = 0;
  193. memcpy(_dst, CONT_STR, CONT_STR_LEN);
  194. ret += CONT_STR_LEN;
  195. if (add_cont == ADD_CONT_MODPARAM) {
  196. memcpy(_dst + ret, contact.s, contact.len);
  197. ret += contact.len;
  198. }
  199. else if (add_cont == ADD_CONT_IP) {
  200. memcpy(_dst + ret, _msg->rcv.bind_address->name.s,
  201. _msg->rcv.bind_address->name.len);
  202. ret += _msg->rcv.bind_address->name.len;
  203. memcpy(_dst + ret, ":", 1);
  204. ret += 1;
  205. memcpy(_dst + ret, _msg->rcv.bind_address->port_no_str.s,
  206. _msg->rcv.bind_address->port_no_str.len);
  207. ret += _msg->rcv.bind_address->port_no_str.len;
  208. switch (_msg->rcv.bind_address->proto) {
  209. case PROTO_NONE:
  210. case PROTO_UDP:
  211. break;
  212. case PROTO_TCP:
  213. memcpy(_dst + ret, TRANSPORT_PARAM, TRANSPORT_PARAM_LEN);
  214. ret += TRANSPORT_PARAM_LEN;
  215. memcpy(_dst + ret, "tcp", 3);
  216. ret += 3;
  217. break;
  218. case PROTO_TLS:
  219. memcpy(_dst + ret, TRANSPORT_PARAM, TRANSPORT_PARAM_LEN);
  220. ret += TRANSPORT_PARAM_LEN;
  221. memcpy(_dst + ret, "tls", 3);
  222. ret += 3;
  223. break;
  224. case PROTO_SCTP:
  225. memcpy(_dst + ret, TRANSPORT_PARAM, TRANSPORT_PARAM_LEN);
  226. ret += TRANSPORT_PARAM_LEN;
  227. memcpy(_dst + ret, "sctp", 4);
  228. ret += 3;
  229. break;
  230. default:
  231. LOG(L_CRIT, "add_contact(): unknown transport protocol (%d)\n",
  232. _msg->rcv.bind_address->proto);
  233. }
  234. }
  235. else if (add_cont == ADD_CONT_RURI) {
  236. /* the parser was called by the contact_length function above */
  237. if (_msg->parsed_orig_ruri_ok) {
  238. memcpy(_dst + ret, _msg->parsed_orig_ruri.host.s,
  239. _msg->parsed_orig_ruri.host.len);
  240. ret += _msg->parsed_orig_ruri.host.len;
  241. if (_msg->parsed_orig_ruri.port.len > 0) {
  242. memcpy(_dst + ret, ":", 1);
  243. ret += 1;
  244. memcpy(_dst + ret, _msg->parsed_orig_ruri.port.s,
  245. _msg->parsed_orig_ruri.port.len);
  246. ret += _msg->parsed_orig_ruri.port.len;
  247. }
  248. }
  249. else if (_msg->parsed_uri_ok){
  250. memcpy(_dst + ret, _msg->parsed_uri.host.s,
  251. _msg->parsed_uri.host.len);
  252. ret += _msg->parsed_uri.host.len;
  253. if (_msg->parsed_uri.port.len > 0) {
  254. memcpy(_dst + ret, ":", 1);
  255. ret += 1;
  256. memcpy(_dst + ret, _msg->parsed_uri.port.s,
  257. _msg->parsed_uri.port.len);
  258. ret += _msg->parsed_uri.port.len;
  259. }
  260. }
  261. }
  262. if (cont_param.len > 0) {
  263. if (*(cont_param.s) != ';') {
  264. memcpy(_dst + ret, ";", 1);
  265. ret += 1;
  266. }
  267. memcpy(_dst + ret, cont_param.s, cont_param.len);
  268. ret += cont_param.len;
  269. }
  270. memcpy(_dst + ret, ">", 1);
  271. ret += 1;
  272. memcpy(_dst + ret, CRLF, CRLF_LEN);
  273. ret += CRLF_LEN;
  274. return ret;
  275. }
  276. /*
  277. * assembles and send the acctual reply
  278. */
  279. static int opt_reply(struct sip_msg* _msg, char* _foo, char* _bar)
  280. {
  281. str rpl_hf;
  282. int ret;
  283. int offset = 0;
  284. int cont_len = 0;
  285. if (_msg->REQ_METHOD != METHOD_OPTIONS) {
  286. LOG(L_ERR, "options_reply(): called for non-OPTIONS request\n");
  287. return 0;
  288. }
  289. /* ruri == server address check has to be done in the script */
  290. if (_msg->parsed_uri_ok != 1) {
  291. if (parse_sip_msg_uri(_msg) != 1) {
  292. LOG(L_WARN, "opt_reply(): failed to parse ruri\n");
  293. }
  294. }
  295. if (_msg->parsed_uri.user.len != 0) {
  296. LOG(L_ERR, "options_reply(): wont reply because ruri contains"
  297. " a username\n");
  298. return 0;
  299. }
  300. /* calculate the length and allocated the mem */
  301. rpl_hf.len = ACPT_STR_LEN + ACPT_ENC_STR_LEN + ACPT_LAN_STR_LEN +
  302. SUPT_STR_LEN + 4 * CRLF_LEN + acpt_body.len + acpt_enc_body.len +
  303. acpt_lan_body.len + supt_body.len;
  304. if (add_cont) {
  305. cont_len = contact_length(_msg);
  306. rpl_hf.len += cont_len;
  307. }
  308. rpl_hf.s = (char*)pkg_malloc(rpl_hf.len);
  309. if (!rpl_hf.s) {
  310. LOG(L_CRIT, "options_reply(): out of memory\n");
  311. goto error;
  312. }
  313. /* create the header fields */
  314. memcpy(rpl_hf.s, ACPT_STR, ACPT_STR_LEN);
  315. offset = ACPT_STR_LEN;
  316. memcpy(rpl_hf.s + offset, acpt_body.s, acpt_body.len);
  317. offset += acpt_body.len;
  318. memcpy(rpl_hf.s + offset, CRLF, CRLF_LEN);
  319. offset += CRLF_LEN;
  320. memcpy(rpl_hf.s + offset, ACPT_ENC_STR, ACPT_ENC_STR_LEN);
  321. offset += ACPT_ENC_STR_LEN;
  322. memcpy(rpl_hf.s + offset, acpt_enc_body.s, acpt_enc_body.len);
  323. offset += acpt_enc_body.len;
  324. memcpy(rpl_hf.s + offset, CRLF, CRLF_LEN);
  325. offset += CRLF_LEN;
  326. memcpy(rpl_hf.s + offset, ACPT_LAN_STR, ACPT_LAN_STR_LEN);
  327. offset += ACPT_LAN_STR_LEN;
  328. memcpy(rpl_hf.s + offset, acpt_lan_body.s, acpt_lan_body.len);
  329. offset += acpt_lan_body.len;
  330. memcpy(rpl_hf.s + offset, CRLF, CRLF_LEN);
  331. offset += CRLF_LEN;
  332. memcpy(rpl_hf.s + offset, SUPT_STR, SUPT_STR_LEN);
  333. offset += SUPT_STR_LEN;
  334. memcpy(rpl_hf.s + offset, supt_body.s, supt_body.len);
  335. offset += supt_body.len;
  336. memcpy(rpl_hf.s + offset, CRLF, CRLF_LEN);
  337. offset += CRLF_LEN;
  338. if (cont_len > 0) {
  339. ret = add_contact(_msg, rpl_hf.s + offset);
  340. if (ret != cont_len) {
  341. LOG(L_CRIT, "options_reply(): add_contact (%i) != contact_length" \
  342. "(%i)\n", ret, cont_len);
  343. goto error;
  344. }
  345. else {
  346. offset += cont_len;
  347. }
  348. }
  349. #ifdef EXTRA_DEBUG
  350. if (offset != rpl_hf.len) {
  351. LOG(L_CRIT, "options_reply(): headerlength (%i) != offset (%i)\n",
  352. rpl_hf.len, offset);
  353. abort();
  354. }
  355. else {
  356. DBG("options_reply(): successfully build OPTIONS reply\n");
  357. }
  358. #endif
  359. if (add_lump_rpl( _msg, rpl_hf.s, rpl_hf.len,
  360. LUMP_RPL_HDR|LUMP_RPL_NODUP)!=0) {
  361. /* the memory is freed when _msg is destroyed */
  362. if (slb.zreply(_msg, 200, "OK") < 0) {
  363. LOG(L_ERR, "options_reply(): failed to send 200 via sl reply\n");
  364. return 0;
  365. } else {
  366. return 1;
  367. }
  368. } else {
  369. LOG(L_ERR, "options_reply(): add_lump_rpl failed\n");
  370. }
  371. error:
  372. if (rpl_hf.s) {
  373. pkg_free(rpl_hf.s);
  374. }
  375. if (slb.zreply(_msg, 500, "Server internal error") < 0) {
  376. LOG(L_ERR, "options_reply(): failed to send 500 via send_reply\n");
  377. }
  378. return 0;
  379. }