select_core.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2005-2006 iptelorg GmbH
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. * History:
  28. * --------
  29. * 2005-12-19 select framework, basic core functions (mma)
  30. * 2006-01-19 multiple nested calls, IS_ALIAS -> NESTED flag renamed (mma)
  31. * 2006-02-17 fixup call for select_anyhdr (mma)
  32. * 2007-01-26 date, identity, identity_info support added (gergo)
  33. */
  34. /*!
  35. * \file
  36. * \brief SIP-router core ::
  37. * \ingroup core
  38. * Module: \ref core
  39. */
  40. #include <stdlib.h>
  41. #include "select.h"
  42. #include "select_core.h"
  43. #include "select_buf.h"
  44. #include "dprint.h"
  45. #include "trim.h"
  46. #include "ut.h"
  47. #include "globals.h"
  48. #include "parser/parser_f.h"
  49. #include "parser/hf.h"
  50. #include "parser/parse_from.h"
  51. #include "parser/parse_to.h"
  52. #include "parser/contact/parse_contact.h"
  53. #include "parser/contact/contact.h"
  54. #include "parser/parse_via.h"
  55. #include "parser/parse_uri.h"
  56. #include "parser/parse_event.h"
  57. #include "parser/parse_rr.h"
  58. #include "parser/digest/digest.h"
  59. #include "mem/mem.h"
  60. #include "parser/parse_hname2.h"
  61. #include "ip_addr.h"
  62. #include "parser/parse_expires.h"
  63. #include "parser/parse_refer_to.h"
  64. #include "parser/parse_rpid.h"
  65. #include "parser/parse_content.h"
  66. #include "parser/parse_body.h"
  67. #include "dset.h"
  68. #include "sr_module.h"
  69. #include "resolve.h"
  70. #include "forward.h"
  71. #define RETURN0_res(x) {*res=(x);return 0;}
  72. #define TRIM_RET0_res(x) {*res=(x);trim(res);return 0;}
  73. #define TEST_RET_res_body(x) if (x){*res=(x)->body;return 0;}else return 1;
  74. #define TEST_RET_res_value(x) if (x){*res=(x)->value;return 0;}else return 1;
  75. int select_ruri(str* res, select_t* s, struct sip_msg* msg)
  76. {
  77. /* Parse the RURI even if it is not needed right now
  78. * because the nested select calls can access the
  79. * parsed URI in this case.
  80. * Go ahead even if the parsing fails, so the
  81. * value of the broken RURI can be accessed at least.
  82. * Subsequent select calls will fail when they try to parse
  83. * the URI anyway. (Miklos)
  84. */
  85. parse_sip_msg_uri(msg);
  86. if (msg->parsed_uri_ok)
  87. select_uri_p = &msg->parsed_uri;
  88. if (msg->first_line.type==SIP_REQUEST) {
  89. if(msg->new_uri.s) {
  90. RETURN0_res(msg->new_uri);
  91. }
  92. else {
  93. RETURN0_res(msg->first_line.u.request.uri);
  94. }
  95. }
  96. return -1;
  97. }
  98. int select_dst_uri(str* res, select_t* s, struct sip_msg* msg)
  99. {
  100. if (msg->first_line.type!=SIP_REQUEST)
  101. return -1;
  102. RETURN0_res(msg->dst_uri);
  103. }
  104. int select_next_hop(str* res, select_t* s, struct sip_msg* msg)
  105. {
  106. if (msg->first_line.type==SIP_REQUEST) {
  107. if(msg->dst_uri.s) {
  108. RETURN0_res(msg->dst_uri);
  109. }
  110. else if(msg->new_uri.s) {
  111. if (msg->parsed_uri_ok)
  112. select_uri_p = &msg->parsed_uri;
  113. RETURN0_res(msg->new_uri);
  114. }
  115. else {
  116. if (msg->parsed_uri_ok)
  117. select_uri_p = &msg->parsed_uri;
  118. else if (msg->parsed_orig_ruri_ok)
  119. select_uri_p = &msg->parsed_orig_ruri;
  120. RETURN0_res(msg->first_line.u.request.uri);
  121. }
  122. }
  123. return -1;
  124. }
  125. int select_next_hop_src_ip(str* res, select_t* s, struct sip_msg* msg) {
  126. struct socket_info* socket_info;
  127. union sockaddr_union to;
  128. char proto;
  129. struct sip_uri *u, next_hop;
  130. str *dst_host;
  131. if (msg->first_line.type!=SIP_REQUEST)
  132. return -1;
  133. if (msg->force_send_socket) {
  134. *res = msg->force_send_socket->address_str;
  135. return 0;
  136. }
  137. if (msg->dst_uri.len) {
  138. if (parse_uri(msg->dst_uri.s, msg->dst_uri.len, &next_hop) < 0)
  139. return -1;
  140. u = &next_hop;
  141. }
  142. else {
  143. if (parse_sip_msg_uri(msg) < 0)
  144. return -1;
  145. u = &msg->parsed_uri;
  146. }
  147. #ifdef USE_TLS
  148. if (u->type==SIPS_URI_T)
  149. proto = PROTO_TLS;
  150. else
  151. #endif
  152. proto = u->proto;
  153. #ifdef HONOR_MADDR
  154. if (u->maddr_val.s && u->maddr_val.len)
  155. dst_host = &u->maddr_val;
  156. else
  157. #endif
  158. dst_host = &u->host;
  159. if (sip_hostport2su(&to, dst_host, u->port_no, &proto) < 0)
  160. return -1;
  161. socket_info = get_send_socket(msg, &to, proto);
  162. if (!socket_info)
  163. return -1;
  164. *res = socket_info->address_str;
  165. return 0;
  166. }
  167. #define SELECT_uri_header(_name_) \
  168. int select_##_name_(str* res, select_t* s, struct sip_msg* msg) \
  169. { \
  170. if (parse_##_name_##_header(msg)<0) \
  171. return -1; \
  172. RETURN0_res(msg->_name_->body); \
  173. } \
  174. \
  175. int select_##_name_##_uri(str* res, select_t* s, struct sip_msg* msg) \
  176. { \
  177. if (parse_##_name_##_header(msg)<0) \
  178. return -1; \
  179. RETURN0_res(get_##_name_(msg)->uri); \
  180. } \
  181. \
  182. int select_##_name_##_tag(str* res, select_t* s, struct sip_msg* msg) \
  183. { \
  184. if (parse_##_name_##_header(msg)<0) \
  185. return -1; \
  186. RETURN0_res(get_##_name_(msg)->tag_value); \
  187. } \
  188. \
  189. int select_##_name_##_name(str* res, select_t* s, struct sip_msg* msg) \
  190. { \
  191. if (parse_##_name_##_header(msg)<0) \
  192. return -1; \
  193. RETURN0_res(get_##_name_(msg)->display); \
  194. } \
  195. \
  196. int select_##_name_##_params(str* res, select_t* s, struct sip_msg* msg) \
  197. { \
  198. struct to_param* p; \
  199. if (parse_##_name_##_header(msg)<0) \
  200. return -1; \
  201. \
  202. p = get_##_name_(msg)->param_lst; \
  203. while (p) { \
  204. if ((p->name.len==s->params[s->n-1].v.s.len) \
  205. && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) { \
  206. RETURN0_res(p->value); \
  207. } \
  208. p = p->next; \
  209. } \
  210. return 1; \
  211. }
  212. SELECT_uri_header(to)
  213. SELECT_uri_header(from)
  214. SELECT_uri_header(refer_to)
  215. SELECT_uri_header(rpid)
  216. int parse_contact_header( struct sip_msg *msg)
  217. {
  218. if ( !msg->contact && ( parse_headers(msg,HDR_CONTACT_F,0)==-1 || !msg->contact)) {
  219. DBG("bad msg or missing CONTACT header\n");
  220. return -1;
  221. }
  222. if (msg->contact->parsed)
  223. return 0;
  224. return parse_contact(msg->contact);
  225. }
  226. #define get_contact(msg) ((contact_body_t*)(msg->contact->parsed))
  227. int select_contact(str* res, select_t* s, struct sip_msg* msg)
  228. {
  229. if (parse_contact_header(msg)<0)
  230. return -1;
  231. RETURN0_res(msg->contact->body);
  232. }
  233. int select_contact_uri(str* res, select_t* s, struct sip_msg* msg)
  234. {
  235. contact_t* c;
  236. if (parse_contact_header(msg)<0)
  237. return -1;
  238. c = get_contact(msg)->contacts;
  239. if (!c)
  240. return 1;
  241. RETURN0_res(c->uri);
  242. }
  243. int select_contact_name(str* res, select_t* s, struct sip_msg* msg)
  244. {
  245. contact_t* c;
  246. if (parse_contact_header(msg)<0)
  247. return -1;
  248. c = get_contact(msg)->contacts;
  249. if (!c)
  250. return 1;
  251. RETURN0_res(c->name);
  252. }
  253. int select_contact_params_spec(str* res, select_t* s, struct sip_msg* msg)
  254. {
  255. contact_t* c;
  256. if (s->params[s->n-1].type != SEL_PARAM_DIV) {
  257. BUG("Last parameter should have type DIV (converted)\n");
  258. return -1;
  259. }
  260. if (parse_contact_header(msg)<0)
  261. return -1;
  262. c = get_contact(msg)->contacts;
  263. if (!c)
  264. return 1;
  265. switch (s->params[s->n-1].v.i) {
  266. case SEL_PARAM_Q:
  267. TEST_RET_res_body(c->q);
  268. case SEL_PARAM_EXPIRES:
  269. TEST_RET_res_body(c->expires);
  270. case SEL_PARAM_METHODS:
  271. TEST_RET_res_body(c->methods);
  272. case SEL_PARAM_RECEIVED:
  273. TEST_RET_res_body(c->received);
  274. case SEL_PARAM_INSTANCE:
  275. TEST_RET_res_body(c->instance);
  276. default:
  277. BUG("Unexpected parameter value \"%d\"\n", s->params[s->n-1].v.i);
  278. return -1;
  279. }
  280. return -1;
  281. }
  282. int select_contact_params(str* res, select_t* s, struct sip_msg* msg)
  283. {
  284. contact_t* c;
  285. param_t* p;
  286. if (parse_contact_header(msg)<0)
  287. return -1;
  288. c = get_contact(msg)->contacts;
  289. if (!c)
  290. return 1;
  291. p = c->params;
  292. while (p) {
  293. if ((p->name.len==s->params[s->n-1].v.s.len)
  294. && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) {
  295. RETURN0_res(p->body)
  296. }
  297. p = p->next;
  298. }
  299. return 1;
  300. }
  301. int select_via(str* res, select_t* s, struct sip_msg* msg)
  302. {
  303. struct via_body *p = NULL;
  304. if ((s->n == 1) || (s->params[1].type == SEL_PARAM_STR)) {
  305. if (parse_via_header(msg, 1, &p)<0) return -1;
  306. } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
  307. if (!p) return -1;
  308. res->s=p->name.s;
  309. res->len=p->bsize;
  310. trim(res);
  311. return 0;
  312. }
  313. int select_via_name(str* res, select_t* s, struct sip_msg* msg)
  314. {
  315. struct via_body *p = NULL;
  316. // it's not neccessary to test if (s->n > 1)
  317. if (s->params[1].type == SEL_PARAM_STR) {
  318. if(parse_via_header(msg, 1, &p)<0) return -1;
  319. } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
  320. if (!p) return -1;
  321. RETURN0_res(p->name);
  322. }
  323. int select_via_version(str* res, select_t* s, struct sip_msg* msg)
  324. {
  325. struct via_body *p = NULL;
  326. // it's not neccessary to test if (s->n > 1)
  327. if (s->params[1].type == SEL_PARAM_STR) {
  328. if (parse_via_header(msg, 1, &p)<0) return -1;
  329. } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
  330. if (!p) return -1;
  331. RETURN0_res(p->version);
  332. }
  333. int select_via_transport(str* res, select_t* s, struct sip_msg* msg)
  334. {
  335. struct via_body *p = NULL;
  336. // it's not neccessary to test if (s->n > 1)
  337. if (s->params[1].type == SEL_PARAM_STR) {
  338. if(parse_via_header(msg, 1, &p)<0) return -1;
  339. } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
  340. if (!p) return -1;
  341. RETURN0_res(p->transport);
  342. }
  343. int select_via_host(str* res, select_t* s, struct sip_msg* msg)
  344. {
  345. struct via_body *p = NULL;
  346. // it's not neccessary to test if (s->n > 1)
  347. if (s->params[1].type == SEL_PARAM_STR) {
  348. if (parse_via_header(msg, 1, &p)<0) return -1;
  349. } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
  350. if (!p) return -1;
  351. RETURN0_res(p->host);
  352. }
  353. int select_via_port(str* res, select_t* s, struct sip_msg* msg)
  354. {
  355. struct via_body *p = NULL;
  356. // it's not neccessary to test if (s->n > 1)
  357. if (s->params[1].type == SEL_PARAM_STR) {
  358. if (parse_via_header(msg, 1, &p)<0) return -1;
  359. } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
  360. if (!p) return -1;
  361. RETURN0_res(p->port_str);
  362. }
  363. int select_via_comment(str* res, select_t* s, struct sip_msg* msg)
  364. {
  365. struct via_body *p = NULL;
  366. // it's not neccessary to test if (s->n > 1)
  367. if (s->params[1].type == SEL_PARAM_STR) {
  368. if(parse_via_header(msg, 1, &p)<0) return -1;
  369. } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
  370. if (!p) return -1;
  371. RETURN0_res(p->comment);
  372. }
  373. int select_via_params(str* res, select_t* s, struct sip_msg* msg)
  374. {
  375. struct via_body *p = NULL;
  376. struct via_param *q;
  377. // it's not neccessary to test if (s->n > 1)
  378. if (s->params[1].type == SEL_PARAM_STR) {
  379. if (parse_via_header(msg, 1, &p)<0) return -1;
  380. } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
  381. if (!p) return -1;
  382. for (q = p->param_lst;q;q=q->next) {
  383. if ((q->name.len==s->params[s->n-1].v.s.len)
  384. && !strncasecmp(q->name.s, s->params[s->n-1].v.s.s,q->name.len)) {
  385. RETURN0_res(q->value);
  386. }
  387. }
  388. return 1;
  389. }
  390. int select_via_params_spec(str* res, select_t* s, struct sip_msg* msg)
  391. {
  392. struct via_body *p = NULL;
  393. if (s->params[s->n-1].type != SEL_PARAM_DIV) {
  394. BUG("Last parameter should have type DIV (converted)\n");
  395. return -1;
  396. }
  397. // it's not neccessary to test if (s->n > 1)
  398. if (s->params[1].type != SEL_PARAM_INT) {
  399. if(parse_via_header(msg, 1, &p)<0) return -1;
  400. } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
  401. if (!p) return -1;
  402. switch (s->params[s->n-1].v.i) {
  403. case SEL_PARAM_BRANCH:
  404. TEST_RET_res_value(p->branch);
  405. case SEL_PARAM_RECEIVED:
  406. TEST_RET_res_value(p->received);
  407. case SEL_PARAM_RPORT:
  408. TEST_RET_res_value(p->rport);
  409. case SEL_PARAM_I:
  410. TEST_RET_res_value(p->i);
  411. case SEL_PARAM_ALIAS:
  412. TEST_RET_res_value(p->alias);
  413. default:
  414. BUG("Unexpected parameter value \"%d\"\n", s->params[s->n-1].v.i);
  415. return -1;
  416. }
  417. return -1;
  418. }
  419. int select_msg(str* res, select_t* s, struct sip_msg* msg)
  420. {
  421. res->s = msg->buf;
  422. res->len = msg->len;
  423. return 0;
  424. }
  425. int select_msg_first_line(str* res, select_t* s, struct sip_msg* msg)
  426. {
  427. res->s=SIP_MSG_START(msg);
  428. res->len=msg->first_line.len;
  429. trim_trailing(res);
  430. return 0;
  431. }
  432. int select_msg_type(str* res, select_t* s, struct sip_msg* msg) {
  433. return uint_to_static_buffer(res, msg->first_line.type);
  434. }
  435. int select_msg_len(str* res, select_t* s, struct sip_msg* msg) {
  436. return uint_to_static_buffer(res, msg->len);
  437. }
  438. int select_msg_id(str* res, select_t* s, struct sip_msg* msg) {
  439. return uint_to_static_buffer(res, msg->id);
  440. }
  441. int select_msg_id_hex(str* res, select_t* s, struct sip_msg* msg) {
  442. return uint_to_static_buffer_ex(res, msg->id, 16, 8);
  443. }
  444. int select_msg_flags(str* res, select_t* s, struct sip_msg* msg) {
  445. return uint_to_static_buffer(res, msg->flags);
  446. }
  447. int select_msg_body(str* res, select_t* s, struct sip_msg* msg)
  448. {
  449. res->s = get_body(msg);
  450. res->len = msg->buf+msg->len - res->s;
  451. return 0;
  452. }
  453. /* returns the sdp part of the message body */
  454. int select_msg_body_sdp(str* res, select_t* sel, struct sip_msg* msg)
  455. {
  456. /* try to get the body part with application/sdp */
  457. if ((res->s = get_body_part(msg,
  458. TYPE_APPLICATION, SUBTYPE_SDP,
  459. &res->len))
  460. )
  461. return 0;
  462. else
  463. return -1;
  464. }
  465. /* returns the value of the requested SDP line */
  466. int select_sdp_line(str* res, select_t* sel, struct sip_msg* msg)
  467. {
  468. int len;
  469. char *buf;
  470. char *buf_end, *line_end;
  471. char line;
  472. if (msg == NULL) {
  473. if (res!=NULL) return -1;
  474. if (sel->n < 5) return -1;
  475. if (sel->params[4].type != SEL_PARAM_STR) {
  476. ERR("wrong parameter type");
  477. return -1;
  478. }
  479. if ((sel->params[4].v.s.len < 1) ||
  480. (sel->params[4].v.s.len > 2) ||
  481. ((sel->params[4].v.s.len == 2) && (sel->params[4].v.s.s[1] != '='))
  482. ) {
  483. ERR("wrong sdp line format: %.*s\n",
  484. sel->params[4].v.s.len, sel->params[4].v.s.s);
  485. return -1;
  486. }
  487. return 0;
  488. }
  489. /* try to get the body part with application/sdp */
  490. if (!(buf = get_body_part(msg,
  491. TYPE_APPLICATION, SUBTYPE_SDP,
  492. &len))
  493. )
  494. return -1;
  495. buf_end = buf + len;
  496. line = *(sel->params[4].v.s.s);
  497. while (buf < buf_end) {
  498. if (*buf == line) {
  499. /* the requested SDP line is found, return its value */
  500. buf++;
  501. if ((buf >= buf_end) || (*buf != '=')) {
  502. ERR("wrong SDP line format\n");
  503. return -1;
  504. }
  505. buf++;
  506. line_end = buf;
  507. while ((line_end < buf_end) && (*line_end != '\n'))
  508. line_end++;
  509. if (line_end >= buf_end) {
  510. ERR("wrong SDP line format\n");
  511. return -1;
  512. }
  513. line_end--;
  514. if (*line_end == '\r') line_end--;
  515. if (line_end < buf) {
  516. ERR("wrong SDP line format\n");
  517. return -1;
  518. }
  519. res->s = buf;
  520. res->len = line_end - buf + 1;
  521. return 0;
  522. }
  523. while ((buf < buf_end) && (*buf != '\n'))
  524. buf++;
  525. buf++;
  526. }
  527. return -1;
  528. }
  529. int select_msg_header(str* res, select_t* s, struct sip_msg* msg)
  530. {
  531. /* get all headers */
  532. char *c;
  533. res->s = SIP_MSG_START(msg) + msg->first_line.len;
  534. c = get_body(msg);
  535. res->len = c - res->s;
  536. return 0;
  537. }
  538. int select_anyheader(str* res, select_t* s, struct sip_msg* msg)
  539. {
  540. struct hdr_field *hf, *hf0;
  541. int hi;
  542. char c;
  543. struct hdr_field hdr;
  544. if(msg==NULL) {
  545. if (res!=NULL) return -1;
  546. /* "fixup" call, res & msg are NULL */
  547. if (s->n <3) return -1;
  548. if (s->params[2].type==SEL_PARAM_STR) {
  549. /* replace _ with - (for P-xxx headers) */
  550. for (hi=s->params[2].v.s.len-1; hi>0; hi--)
  551. if (s->params[2].v.s.s[hi]=='_')
  552. s->params[2].v.s.s[hi]='-';
  553. /* if header name is parseable, parse it and set SEL_PARAM_DIV */
  554. c=s->params[2].v.s.s[s->params[2].v.s.len];
  555. s->params[2].v.s.s[s->params[2].v.s.len]=':';
  556. if (parse_hname2(s->params[2].v.s.s,s->params[2].v.s.s+(s->params[2].v.s.len<3?4:s->params[2].v.s.len+1),
  557. &hdr)==0) {
  558. ERR("select_anyhdr:fixup_call:parse error\n");
  559. return -1;
  560. }
  561. s->params[2].v.s.s[s->params[2].v.s.len]=c;
  562. if (hdr.type!=HDR_OTHER_T && hdr.type!=HDR_ERROR_T) {
  563. /* pkg_free(s->params[1].v.s.s); */
  564. /* don't free it (the mem can leak only once at startup)
  565. * the parsed string can live inside larger string block
  566. * e.g. when xlog's select is parsed
  567. */
  568. s->params[2].type = SEL_PARAM_DIV;
  569. s->params[2].v.i = hdr.type;
  570. }
  571. }
  572. return 1;
  573. }
  574. hf0 = NULL;
  575. /* extract header index if present */
  576. if (s->param_offset[select_level+1] == 4) {
  577. if (s->params[3].type == SEL_PARAM_INT) {
  578. hi = s->params[3].v.i;
  579. } else {
  580. hi = -1;
  581. }
  582. } else {
  583. hi = 1;
  584. }
  585. /* we need to be sure we have parsed all headers */
  586. if (!msg->eoh && (parse_headers(msg,HDR_EOH_F,0)==-1 || !msg->eoh)) {
  587. ERR("bad msg while parsing to EOH \n");
  588. return -1;
  589. }
  590. for (hf=msg->headers; hf; hf=hf->next) {
  591. if(s->params[2].type==SEL_PARAM_DIV) {
  592. if (s->params[2].v.i!=hf->type) continue;
  593. } else if(s->params[2].type==SEL_PARAM_STR) {
  594. if (s->params[2].v.s.len!=hf->name.len) continue;
  595. if (strncasecmp(s->params[2].v.s.s, hf->name.s, hf->name.len)!=0) continue;
  596. }
  597. hf0 = hf;
  598. hi--;
  599. if (!hi) break;
  600. }
  601. if(hf0==NULL || hi>0)
  602. return 1;
  603. res->s = hf0->body.s;
  604. res->len = hf0->body.len;
  605. trim(res);
  606. return 0;
  607. }
  608. //ABSTRACT_F(select_anyheader_params)
  609. // Instead of ABSTRACT_F(select_anyheader_params)
  610. // use function which uses select_core_table
  611. // to avoid gcc warning about not used
  612. int select_anyheader_params(str* res, select_t* s, struct sip_msg* msg)
  613. {
  614. if (select_core_table.next)
  615. return -1;
  616. else
  617. return -1;
  618. }
  619. ABSTRACT_F(select_any_uri)
  620. static struct sip_uri uri;
  621. int select_uri_type(str* res, select_t* s, struct sip_msg* msg)
  622. {
  623. if (select_uri_p == NULL) {
  624. trim(res);
  625. if (parse_uri(res->s, res->len, &uri)<0)
  626. return -1;
  627. select_uri_p = &uri;
  628. }
  629. if (select_uri_p->type==ERROR_URI_T)
  630. return -1;
  631. uri_type_to_str(select_uri_p->type, res);
  632. return 0;
  633. }
  634. int select_uri_user(str* res, select_t* s, struct sip_msg* msg)
  635. {
  636. if (select_uri_p == NULL) {
  637. if (parse_uri(res->s, res->len, &uri)<0)
  638. return -1;
  639. select_uri_p = &uri;
  640. }
  641. if (select_uri_p->flags & URI_USER_NORMALIZE) {
  642. if (!(res->s=get_static_buffer(select_uri_p->user.len)))
  643. return -1;
  644. if ((res->len=normalize_tel_user(res->s, (&select_uri_p->user)))==0)
  645. return 1;
  646. return 0;
  647. }
  648. RETURN0_res(select_uri_p->user);
  649. }
  650. /* search for a parameter with "name"
  651. * Return value:
  652. * 0: not found
  653. * 1: found
  654. * -1: error
  655. *
  656. * val is set to the value of the parameter.
  657. */
  658. static inline int search_param(str params, char *name, int name_len,
  659. str *val)
  660. {
  661. param_hooks_t h;
  662. param_t *p, *list;
  663. if (params.s == NULL)
  664. return 0;
  665. if (parse_params(&params, CLASS_ANY, &h, &list) < 0)
  666. return -1;
  667. for (p = list; p; p=p->next) {
  668. if ((p->name.len == name_len)
  669. && (strncasecmp(p->name.s, name, name_len) == 0)
  670. ) {
  671. *val=p->body;
  672. free_params(list);
  673. return 1;
  674. }
  675. }
  676. free_params(list);
  677. return 0;
  678. }
  679. /* Return the value of the "rn" parameter if exists, otherwise the user name.
  680. * The user name is normalized if needed, i.e. visual separators are removed,
  681. * the "rn" param is always normalized. */
  682. int select_uri_rn_user(str* res, select_t* s, struct sip_msg* msg)
  683. {
  684. int ret;
  685. str val;
  686. if (select_uri_p == NULL) {
  687. if (parse_uri(res->s, res->len, &uri)<0)
  688. return -1;
  689. select_uri_p = &uri;
  690. }
  691. /* search for the "rn" parameter */
  692. if ((ret = search_param(select_uri_p->params, "rn", 2, &val)) != 0)
  693. goto done;
  694. if (select_uri_p->sip_params.s != select_uri_p->params.s) {
  695. /* check also the original sip: URI parameters */
  696. if ((ret = search_param(select_uri_p->sip_params, "rn", 2, &val)) != 0)
  697. goto done;
  698. }
  699. if ((select_uri_p->flags & URI_USER_NORMALIZE) == 0)
  700. RETURN0_res(select_uri_p->user);
  701. /* else normalize the user name */
  702. val = select_uri_p->user;
  703. done:
  704. if (ret < 0)
  705. return -1; /* error */
  706. if (!(res->s=get_static_buffer(val.len)))
  707. return -1;
  708. if ((res->len=normalize_tel_user(res->s, &val))==0)
  709. return 1;
  710. return 0;
  711. }
  712. int select_uri_pwd(str* res, select_t* s, struct sip_msg* msg)
  713. {
  714. if (select_uri_p == NULL) {
  715. if (parse_uri(res->s, res->len, &uri)<0)
  716. return -1;
  717. select_uri_p = &uri;
  718. }
  719. RETURN0_res(select_uri_p->passwd);
  720. }
  721. int select_uri_host(str* res, select_t* s, struct sip_msg* msg)
  722. {
  723. if (select_uri_p == NULL) {
  724. if (parse_uri(res->s, res->len, &uri)<0)
  725. return -1;
  726. select_uri_p = &uri;
  727. }
  728. RETURN0_res(select_uri_p->host);
  729. }
  730. int select_uri_port(str* res, select_t* s, struct sip_msg* msg)
  731. {
  732. if (select_uri_p == NULL) {
  733. if (parse_uri(res->s, res->len, &uri)<0)
  734. return -1;
  735. select_uri_p = &uri;
  736. }
  737. RETURN0_res(select_uri_p->port);
  738. }
  739. int select_uri_hostport(str* res, select_t* s, struct sip_msg* msg)
  740. {
  741. char* p;
  742. int size;
  743. if (select_uri_p == NULL) {
  744. if (parse_uri(res->s, res->len, &uri)<0)
  745. return -1;
  746. select_uri_p = &uri;
  747. }
  748. if (!select_uri_p->host.len)
  749. return -1;
  750. if (select_uri_p->port.len) {
  751. res->s=select_uri_p->host.s;
  752. res->len=select_uri_p->host.len+select_uri_p->port.len+1;
  753. return 0;
  754. }
  755. size=select_uri_p->host.len+5;
  756. if (!(p = get_static_buffer(size)))
  757. return -1;
  758. strncpy(p, select_uri_p->host.s, select_uri_p->host.len);
  759. switch (select_uri_p->type) {
  760. case SIPS_URI_T:
  761. case TELS_URI_T:
  762. strncpy(p+select_uri_p->host.len, ":5061", 5);
  763. break;
  764. case SIP_URI_T:
  765. case TEL_URI_T:
  766. case URN_URI_T:
  767. strncpy(p+select_uri_p->host.len, ":5060", 5);
  768. break;
  769. case ERROR_URI_T:
  770. return -1;
  771. }
  772. res->s = p;
  773. res->len = size;
  774. return 0;
  775. }
  776. int select_uri_proto(str* res, select_t* s, struct sip_msg* msg)
  777. {
  778. if (select_uri_p == NULL) {
  779. if (parse_uri(res->s, res->len, &uri)<0)
  780. return -1;
  781. select_uri_p = &uri;
  782. }
  783. if (select_uri_p->proto != PROTO_NONE) {
  784. proto_type_to_str(select_uri_p->proto, res);
  785. } else {
  786. switch (select_uri_p->type) {
  787. case SIPS_URI_T:
  788. case TELS_URI_T:
  789. proto_type_to_str(PROTO_TLS, res);
  790. break;
  791. case SIP_URI_T:
  792. case TEL_URI_T:
  793. case URN_URI_T:
  794. proto_type_to_str(PROTO_UDP, res);
  795. break;
  796. case ERROR_URI_T:
  797. return -1;
  798. }
  799. }
  800. return 0;
  801. }
  802. int select_uri_params(str* res, select_t* s, struct sip_msg* msg)
  803. {
  804. int ret;
  805. if (!msg || !res) {
  806. return select_any_params(res, s, msg);
  807. }
  808. if (select_uri_p == NULL) {
  809. if (parse_uri(res->s, res->len, &uri)<0)
  810. return -1;
  811. select_uri_p = &uri;
  812. }
  813. if (s->param_offset[select_level+1]-s->param_offset[select_level]==1)
  814. RETURN0_res(select_uri_p->params);
  815. *res=select_uri_p->params;
  816. ret = select_any_params(res, s, msg);
  817. if ((ret < 0)
  818. && (select_uri_p->sip_params.s != NULL)
  819. && (select_uri_p->sip_params.s != select_uri_p->params.s)
  820. ) {
  821. /* Search also in the original sip: uri parameters. */
  822. *res = select_uri_p->sip_params;
  823. ret = select_any_params(res, s, msg);
  824. }
  825. return ret;
  826. }
  827. int select_any_params(str* res, select_t* s, struct sip_msg* msg)
  828. {
  829. str* wanted;
  830. int i;
  831. if (!msg || !res) {
  832. if (s->param_offset[select_level+1]-s->param_offset[select_level]==1) return 0;
  833. if (s->params[s->param_offset[select_level]+1].type!=SEL_PARAM_STR) return -1;
  834. wanted=&s->params[s->param_offset[select_level]+1].v.s;
  835. for (i=0; i<wanted->len; i++)
  836. if (wanted->s[i]=='_')
  837. wanted->s[i]='-';
  838. return 0;
  839. }
  840. if (s->params[s->param_offset[select_level]+1].type!=SEL_PARAM_STR) return -1;
  841. wanted=&s->params[s->param_offset[select_level]+1].v.s;
  842. if (!res->len) return -1;
  843. if (search_param(*res, wanted->s, wanted->len, res) <= 0) {
  844. DBG("SELECT ...uri.params.%s NOT FOUND !\n", wanted->s);
  845. return -1;
  846. } else {
  847. return (res->len) ? 0 : 1;
  848. }
  849. }
  850. int select_event(str* res, select_t* s, struct sip_msg* msg)
  851. {
  852. if (!msg->event && parse_headers(msg, HDR_EVENT_F, 0) == -1) {
  853. ERR("Error while searching Event header field\n");
  854. return -1;
  855. }
  856. if (!msg->event) {
  857. DBG("Event header field not found\n");
  858. return -1;
  859. }
  860. if (parse_event(msg->event) < 0) {
  861. ERR("Error while parsing Event header field\n");
  862. return -1;
  863. }
  864. *res = ((event_t*)msg->event->parsed)->name;
  865. return 0;
  866. }
  867. static int parse_rr_header(struct sip_msg *msg)
  868. {
  869. if ( !msg->record_route && ( parse_headers(msg,HDR_RECORDROUTE_F,0) == -1)) {
  870. ERR("bad msg or missing Record-Route header\n");
  871. return -1;
  872. }
  873. if (!msg->record_route) {
  874. DBG("No Record-Route header field found\n");
  875. return -1;
  876. }
  877. return parse_rr(msg->record_route);
  878. }
  879. #define get_rr(msg) ((rr_t*)(msg->record_route->parsed))
  880. int select_rr(str* res, select_t* s, struct sip_msg* msg)
  881. {
  882. if (parse_rr_header(msg)<0)
  883. return -1;
  884. RETURN0_res(msg->record_route->body);
  885. }
  886. int select_rr_uri(str* res, select_t* s, struct sip_msg* msg)
  887. {
  888. rr_t* r;
  889. if (parse_rr_header(msg)<0)
  890. return -1;
  891. r = get_rr(msg);
  892. if (!r)
  893. return 1;
  894. RETURN0_res(r->nameaddr.uri);
  895. }
  896. int select_rr_name(str* res, select_t* s, struct sip_msg* msg)
  897. {
  898. rr_t* r;
  899. if (parse_rr_header(msg)<0)
  900. return -1;
  901. r = get_rr(msg);
  902. if (!r)
  903. return 1;
  904. RETURN0_res(r->nameaddr.name);
  905. }
  906. int select_rr_params(str* res, select_t* s, struct sip_msg* msg)
  907. {
  908. rr_t* r;
  909. param_t* p;
  910. if (parse_rr_header(msg)<0)
  911. return -1;
  912. r = get_rr(msg);
  913. if (!r)
  914. return 1;
  915. p = r->params;
  916. while (p) {
  917. if ((p->name.len==s->params[s->n-1].v.s.len)
  918. && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) {
  919. RETURN0_res(p->body)
  920. }
  921. p = p->next;
  922. }
  923. return 0;
  924. }
  925. static inline struct cseq_body* sel_parse_cseq(struct sip_msg* msg)
  926. {
  927. if (!msg->cseq && (parse_headers(msg, HDR_CSEQ_F, 0) == -1)) {
  928. ERR("Unable to parse CSeq header\n");
  929. return 0;
  930. }
  931. if (!msg->cseq) {
  932. DBG("No CSeqheader field found\n");
  933. return 0;
  934. }
  935. return get_cseq(msg);
  936. }
  937. int select_cseq(str* res, select_t* s, struct sip_msg* msg)
  938. {
  939. struct cseq_body* cs;
  940. cs = sel_parse_cseq(msg);
  941. if (!cs) return -1;
  942. *res = msg->cseq->body;
  943. return 0;
  944. }
  945. int select_cseq_num(str* res, select_t* s, struct sip_msg* msg)
  946. {
  947. struct cseq_body* cs;
  948. cs = sel_parse_cseq(msg);
  949. if (!cs) return -1;
  950. *res = cs->number;
  951. return 0;
  952. }
  953. int select_cseq_method(str* res, select_t* s, struct sip_msg* msg)
  954. {
  955. struct cseq_body* cs;
  956. cs = sel_parse_cseq(msg);
  957. if (!cs) return -1;
  958. *res = cs->method;
  959. return 0;
  960. }
  961. int get_credentials(struct sip_msg* msg, select_t* s, struct hdr_field** hdr)
  962. {
  963. int ret;
  964. str realm;
  965. hdr_types_t hdr_type;
  966. *hdr = NULL;
  967. if (!msg) {
  968. /* fix-up call check domain for fparam conversion */
  969. void * ptr;
  970. char chr;
  971. ptr=(void *)(s->params[1].v.s.s);
  972. chr=s->params[1].v.s.s[s->params[1].v.s.len];
  973. s->params[1].v.s.s[s->params[1].v.s.len]=0;
  974. ret=fixup_var_str_12(&ptr,0);
  975. if (ret>=0) {
  976. s->params[1].v.s.s[s->params[1].v.s.len]=chr;
  977. s->params[1].v.p=ptr;
  978. s->params[1].type=SEL_PARAM_PTR;
  979. }
  980. return ret;
  981. }
  982. /* Try to find credentials with corresponding realm
  983. * in the message, parse them and return pointer to
  984. * parsed structure
  985. */
  986. if (s->params[1].type==SEL_PARAM_PTR) {
  987. if (get_str_fparam(&realm, msg, s->params[1].v.p)<0)
  988. return -1;
  989. } else {
  990. realm = s->params[1].v.s;
  991. }
  992. switch (s->params[0].v.i) {
  993. case SEL_AUTH_WWW:
  994. hdr_type = HDR_AUTHORIZATION_T;
  995. break;
  996. case SEL_AUTH_PROXY:
  997. hdr_type = HDR_PROXYAUTH_T;
  998. break;
  999. default:
  1000. BUG("Unexpected parameter value \"%d\"\n", s->params[0].v.i);
  1001. return -1;
  1002. }
  1003. ret = find_credentials(msg, &realm, hdr_type, hdr);
  1004. return ret;
  1005. }
  1006. int select_auth(str* res, select_t* s, struct sip_msg* msg)
  1007. {
  1008. int ret;
  1009. struct hdr_field* hdr;
  1010. if (s->n != 2 && s->params[1].type != SEL_PARAM_STR
  1011. && s->params[1].type != SEL_PARAM_PTR) return -1;
  1012. if (s->params[0].type != SEL_PARAM_DIV) {
  1013. BUG("Last parameter should have type DIV (converted)\n");
  1014. return -1;
  1015. }
  1016. ret = get_credentials(msg, s, &hdr);
  1017. if (!hdr) return ret;
  1018. RETURN0_res(hdr->body);
  1019. }
  1020. int select_auth_param(str* res, select_t* s, struct sip_msg* msg)
  1021. {
  1022. int ret;
  1023. struct hdr_field* hdr;
  1024. dig_cred_t* cred;
  1025. if ((s->n != 3 && s->n != 4) || (s->params[s->n - 1].type != SEL_PARAM_DIV)) return -1;
  1026. ret = get_credentials(msg, s, &hdr);
  1027. if (!hdr) return ret;
  1028. cred = &((auth_body_t*)hdr->parsed)->digest;
  1029. switch(s->params[s->n - 1].v.i) {
  1030. case SEL_AUTH_USER: RETURN0_res(cred->username.user);
  1031. case SEL_AUTH_DOMAIN: RETURN0_res(cred->username.domain);
  1032. case SEL_AUTH_USERNAME: RETURN0_res(cred->username.whole);
  1033. case SEL_AUTH_REALM: RETURN0_res(cred->realm);
  1034. case SEL_AUTH_NONCE: RETURN0_res(cred->nonce);
  1035. case SEL_AUTH_URI: RETURN0_res(cred->uri);
  1036. case SEL_AUTH_CNONCE: RETURN0_res(cred->cnonce);
  1037. case SEL_AUTH_NC: RETURN0_res(cred->nc);
  1038. case SEL_AUTH_RESPONSE: RETURN0_res(cred->response);
  1039. case SEL_AUTH_OPAQUE: RETURN0_res(cred->opaque);
  1040. case SEL_AUTH_ALG: RETURN0_res(cred->alg.alg_str);
  1041. case SEL_AUTH_QOP: RETURN0_res(cred->qop.qop_str);
  1042. default:
  1043. BUG("Unsupported digest credentials parameter in select\n");
  1044. return -1;
  1045. }
  1046. }
  1047. int select_auth_username(str* res, select_t* s, struct sip_msg* msg)
  1048. {
  1049. return select_auth_param(res, s, msg);
  1050. }
  1051. int select_auth_username_comp(str* res, select_t* s, struct sip_msg* msg)
  1052. {
  1053. return select_auth_param(res, s, msg);
  1054. }
  1055. ABSTRACT_F(select_any_nameaddr)
  1056. int select_nameaddr_name(str* res, select_t* s, struct sip_msg* msg)
  1057. {
  1058. char *p;
  1059. p=find_not_quoted(res, '<');
  1060. if (!p) {
  1061. DBG("select_nameaddr_name: no < found, whole string is uri\n");
  1062. res->len=0;
  1063. return 1;
  1064. }
  1065. res->len=p-res->s;
  1066. while (res->len && SP(res->s[res->len-1])) res->len--;
  1067. return 0;
  1068. }
  1069. int select_nameaddr_uri(str* res, select_t* s, struct sip_msg* msg)
  1070. {
  1071. char *p;
  1072. p=find_not_quoted(res, '<');
  1073. if (!p) {
  1074. DBG("select_nameaddr_uri: no < found, string up to first semicolon is uri\n");
  1075. p = q_memchr(res->s, ';', res->len);
  1076. if (p)
  1077. res->len = p-res->s;
  1078. return 0;
  1079. }
  1080. res->len=res->len - (p-res->s) -1;
  1081. res->s=p +1;
  1082. p=find_not_quoted(res, '>');
  1083. if (!p) {
  1084. ERR("select_nameaddr_uri: no > found, invalid nameaddr value\n");
  1085. return -1;
  1086. }
  1087. res->len=p-res->s;
  1088. return 0;
  1089. }
  1090. int select_nameaddr_params(str* res, select_t* s, struct sip_msg* msg)
  1091. {
  1092. char *p;
  1093. p=find_not_quoted(res, '<');
  1094. if (!p) {
  1095. p=find_not_quoted(res, ';');
  1096. } else {
  1097. res->len=res->len - (p-res->s) -1;
  1098. res->s=p +1;
  1099. p=find_not_quoted(res, '>');
  1100. if (!p) {
  1101. ERR("select_nameaddr_params: no > found, invalid nameaddr value\n");
  1102. return -1;
  1103. }
  1104. res->len=res->len - (p-res->s) -1;
  1105. res->s=p +1;
  1106. p=find_not_quoted(res, ';');
  1107. }
  1108. if (!p) return 1;
  1109. res->len=res->len - (p-res->s) -1;
  1110. res->s=p +1;
  1111. if (s->param_offset[select_level+1]-s->param_offset[select_level]==1)
  1112. return (res->len ? 0 : 1);
  1113. return select_any_params(res, s, msg);
  1114. }
  1115. ABSTRACT_F(select_src)
  1116. ABSTRACT_F(select_dst)
  1117. ABSTRACT_F(select_rcv)
  1118. int select_ip_port(str* res, select_t* s, struct sip_msg* msg)
  1119. {
  1120. str ip_str=STR_NULL, port_str=STR_NULL, proto_str=STR_NULL;
  1121. int param, pos;
  1122. if ((s->n != 2) || (s->params[1].type != SEL_PARAM_DIV)) return -1;
  1123. param=s->params[1].v.i;
  1124. if (param & SEL_SRC) {
  1125. if (param & SEL_IP) {
  1126. ip_str.s = ip_addr2a(&msg->rcv.src_ip);
  1127. ip_str.len = strlen(ip_str.s);
  1128. }
  1129. if (param & SEL_PORT) {
  1130. port_str.s = int2str(msg->rcv.src_port, &port_str.len);
  1131. }
  1132. } else if (param & SEL_DST) {
  1133. if (param & SEL_IP) {
  1134. ip_str.s = ip_addr2a(&msg->rcv.dst_ip);
  1135. ip_str.len = strlen(ip_str.s);
  1136. }
  1137. if (param & SEL_PORT) {
  1138. port_str.s = int2str(msg->rcv.dst_port, &port_str.len);
  1139. }
  1140. } else if (param & SEL_RCV) {
  1141. if (param & SEL_IP) {
  1142. ip_str = msg->rcv.bind_address->address_str;
  1143. }
  1144. if (param & SEL_PORT) {
  1145. port_str = msg->rcv.bind_address->port_no_str;
  1146. }
  1147. if (param & SEL_PROTO) {
  1148. switch (msg->rcv.proto) {
  1149. case PROTO_NONE:
  1150. proto_str.s = 0;
  1151. proto_str.len = 0;
  1152. break;
  1153. case PROTO_UDP:
  1154. proto_str.s = "udp";
  1155. proto_str.len = 3;
  1156. break;
  1157. case PROTO_TCP:
  1158. proto_str.s = "tcp";
  1159. proto_str.len = 3;
  1160. break;
  1161. case PROTO_TLS:
  1162. proto_str.s = "tls";
  1163. proto_str.len = 3;
  1164. break;
  1165. case PROTO_SCTP:
  1166. proto_str.s = "sctp";
  1167. proto_str.len = 4;
  1168. break;
  1169. default:
  1170. ERR("BUG: select_ip_port: Unknown transport protocol\n");
  1171. return -1;
  1172. }
  1173. }
  1174. } else {
  1175. return -1;
  1176. }
  1177. res->s = get_static_buffer(ip_str.len+port_str.len+proto_str.len+3);
  1178. if (!res->s) return -1;
  1179. pos=0;
  1180. if (param & SEL_PROTO) {
  1181. memcpy(res->s, proto_str.s, proto_str.len);
  1182. pos += proto_str.len;
  1183. }
  1184. if (param & SEL_IP) {
  1185. if (pos) res->s[pos++] = ':';
  1186. memcpy(res->s+pos, ip_str.s, ip_str.len);
  1187. pos += ip_str.len;
  1188. }
  1189. if (param & SEL_PORT) {
  1190. if (pos) res->s[pos++] = ':';
  1191. memcpy(res->s+pos, port_str.s, port_str.len);
  1192. pos += port_str.len;
  1193. }
  1194. res->s[pos] = 0;
  1195. res->len = pos;
  1196. return (pos==0 ? 1 : 0);
  1197. }
  1198. int select_expires(str* res, select_t* s, struct sip_msg* msg)
  1199. {
  1200. if (!msg->expires && (parse_headers(msg, HDR_EXPIRES_F, 0) == -1)) {
  1201. return -1; /* error */
  1202. }
  1203. if (!msg->expires) {
  1204. return 1; /* null */
  1205. }
  1206. if (msg->expires->parsed == NULL && parse_expires(msg->expires) < 0) {
  1207. return -1;
  1208. }
  1209. RETURN0_res(((struct exp_body*)msg->expires->parsed)->text);
  1210. }
  1211. #define SELECT_plain_header(_sel_name_,_fld_name_,_hdr_f_) \
  1212. int select_##_sel_name_(str* res, select_t* s, struct sip_msg* msg) \
  1213. { \
  1214. if (!msg->_fld_name_ && (parse_headers(msg, _hdr_f_, 0) == -1)) { \
  1215. return -1; \
  1216. } \
  1217. if (!msg->_fld_name_) { \
  1218. return 1; \
  1219. } \
  1220. RETURN0_res(msg->_fld_name_->body); \
  1221. }
  1222. SELECT_plain_header(call_id, callid, HDR_CALLID_F)
  1223. SELECT_plain_header(max_forwards, maxforwards, HDR_MAXFORWARDS_F)
  1224. SELECT_plain_header(content_type, content_type, HDR_CONTENTTYPE_F)
  1225. SELECT_plain_header(content_length, content_length, HDR_CONTENTLENGTH_F)
  1226. SELECT_plain_header(user_agent, user_agent, HDR_USERAGENT_F)
  1227. SELECT_plain_header(subject, subject, HDR_SUBJECT_F)
  1228. SELECT_plain_header(organization, organization, HDR_ORGANIZATION_F)
  1229. SELECT_plain_header(priority, priority, HDR_PRIORITY_F)
  1230. SELECT_plain_header(session_expires, session_expires, HDR_SESSIONEXPIRES_F)
  1231. SELECT_plain_header(min_se, min_se, HDR_MIN_SE_F)
  1232. SELECT_plain_header(sip_if_match, sipifmatch, HDR_SIPIFMATCH_F)
  1233. SELECT_plain_header(date, date, HDR_DATE_F)
  1234. SELECT_plain_header(identity, identity, HDR_IDENTITY_F)
  1235. SELECT_plain_header(identity_info, identity_info, HDR_IDENTITY_INFO_F)
  1236. int select_msg_request(str* res, select_t* s, struct sip_msg* msg)
  1237. {
  1238. if (msg->first_line.type==SIP_REQUEST) {
  1239. return select_msg(res, s, msg);
  1240. }
  1241. else
  1242. return -1;
  1243. }
  1244. int select_msg_response(str* res, select_t* s, struct sip_msg* msg)
  1245. {
  1246. if (msg->first_line.type==SIP_REPLY) {
  1247. return select_msg(res, s, msg);
  1248. }
  1249. else
  1250. return -1;
  1251. }
  1252. #define SELECT_first_line(_sel_name_,_type_,_fld_) \
  1253. int select_msg_##_sel_name_(str* res, select_t* s, struct sip_msg* msg) {\
  1254. if (msg->first_line.type==_type_) { \
  1255. RETURN0_res(msg->first_line._fld_); \
  1256. } else return -1; \
  1257. }
  1258. SELECT_first_line(request_method,SIP_REQUEST,u.request.method)
  1259. SELECT_first_line(request_uri,SIP_REQUEST,u.request.uri)
  1260. SELECT_first_line(request_version,SIP_REQUEST,u.request.version)
  1261. SELECT_first_line(response_version,SIP_REPLY,u.reply.version)
  1262. SELECT_first_line(response_status,SIP_REPLY,u.reply.status)
  1263. SELECT_first_line(response_reason,SIP_REPLY,u.reply.reason)
  1264. int select_version(str* res, select_t* s, struct sip_msg* msg)
  1265. {
  1266. switch (msg->first_line.type) {
  1267. case SIP_REQUEST:
  1268. RETURN0_res(msg->first_line.u.request.version)
  1269. break;
  1270. case SIP_REPLY:
  1271. RETURN0_res(msg->first_line.u.reply.version)
  1272. break;
  1273. default:
  1274. return -1;
  1275. }
  1276. }
  1277. ABSTRACT_F(select_sys)
  1278. int select_sys_pid(str* res, select_t* s, struct sip_msg* msg) {
  1279. return uint_to_static_buffer(res, getpid());
  1280. }
  1281. int select_sys_server_id(str* res, select_t* s, struct sip_msg* msg) {
  1282. return int_to_static_buffer(res, server_id);
  1283. }
  1284. int select_sys_unique(str* res, select_t* s, struct sip_msg* msg) {
  1285. #define UNIQUE_ID_PID_LEN 4
  1286. #define UNIQUE_ID_TIME_LEN 8
  1287. #define UNIQUE_ID_FIX_LEN (UNIQUE_ID_PID_LEN+1+UNIQUE_ID_TIME_LEN+1)
  1288. #define UNIQUE_ID_RAND_LEN 8
  1289. static char uniq_id[UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN];
  1290. static int uniq_for_pid = -1;
  1291. int i;
  1292. if (uniq_for_pid != getpid()) {
  1293. /* first call for this process */
  1294. int cb, rb, x, l;
  1295. char *c;
  1296. /* init gloabally uniq part */
  1297. c = int2str_base_0pad(getpid(), &l, 16, UNIQUE_ID_PID_LEN);
  1298. memcpy(uniq_id, c, UNIQUE_ID_PID_LEN);
  1299. uniq_id[UNIQUE_ID_PID_LEN] = '-';
  1300. c = int2str_base_0pad(time(NULL), &l, 16, UNIQUE_ID_TIME_LEN);
  1301. memcpy(uniq_id+UNIQUE_ID_PID_LEN+1, c, UNIQUE_ID_TIME_LEN);
  1302. uniq_id[UNIQUE_ID_PID_LEN+1+UNIQUE_ID_TIME_LEN] = '-';
  1303. /* init random part */
  1304. for (i = RAND_MAX, rb=0; i; rb++, i>>=1);
  1305. for (i = UNIQUE_ID_FIX_LEN, cb = 0, x = 0; i < UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN; i++) {
  1306. if (!cb) {
  1307. cb = rb;
  1308. x = rand();
  1309. }
  1310. uniq_id[i] = fourbits2char[x & 0x0F];
  1311. x >>= rb;
  1312. cb -= rb;
  1313. }
  1314. uniq_for_pid = getpid();
  1315. }
  1316. for (i = UNIQUE_ID_FIX_LEN + UNIQUE_ID_RAND_LEN - 1; i >= UNIQUE_ID_FIX_LEN; i--) {
  1317. switch (uniq_id[i]) {
  1318. case '9':
  1319. uniq_id[i]='a';
  1320. i = 0;
  1321. break;
  1322. case 'f':
  1323. uniq_id[i]='0';
  1324. /* go on */
  1325. break;
  1326. default:
  1327. uniq_id[i]++;
  1328. i = 0;
  1329. break;
  1330. }
  1331. }
  1332. res->s = uniq_id; /* I think it's not worth copying at static buffer, I hope there is no real meaning of @[email protected] */
  1333. res->len = UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN;
  1334. return 0;
  1335. }
  1336. int select_sys_now(str* res, select_t* s, struct sip_msg* msg) {
  1337. return uint_to_static_buffer(res, time(NULL));
  1338. }
  1339. int select_sys_now_fmt(str* res, select_t* s, struct sip_msg* msg)
  1340. {
  1341. #define SEL_POS 2
  1342. time_t t;
  1343. struct tm *tm;
  1344. t = time(NULL);
  1345. switch (s->params[SEL_POS].v.i) {
  1346. case SEL_NOW_GMT:
  1347. tm = gmtime(&t);
  1348. break;
  1349. case SEL_NOW_LOCAL:
  1350. tm = localtime(&t);
  1351. break;
  1352. default:
  1353. BUG("Unexpected parameter value 'now' \"%d\"\n", s->params[SEL_POS].v.i);
  1354. return -1;
  1355. }
  1356. if (s->n <= SEL_POS+1) {
  1357. char *c;
  1358. c = asctime(tm);
  1359. res->len = strlen(c);
  1360. while (res->len && c[res->len-1] < ' ') res->len--; /* rtrim */
  1361. res->s = get_static_buffer(res->len);
  1362. if (!res->s) return -1;
  1363. memcpy(res->s, c, res->len);
  1364. }
  1365. else {
  1366. char c, buff[80];
  1367. c = s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len];
  1368. s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len] = '\0';
  1369. res->len = strftime(buff, sizeof(buff)-1, s->params[SEL_POS+1].v.s.s, tm);
  1370. s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len] = c;
  1371. res->s = get_static_buffer(res->len);
  1372. if (!res->s) return -1;
  1373. memcpy(res->s, buff, res->len);
  1374. }
  1375. return 0;
  1376. #undef SEL_POS
  1377. }
  1378. ABSTRACT_F(select_branch)
  1379. int select_branch_count(str* res, select_t* s, struct sip_msg* msg) {
  1380. return uint_to_static_buffer(res, nr_branches);
  1381. }
  1382. int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
  1383. #define SEL_POS 1
  1384. #define Q_PARAM ">;q="
  1385. #define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
  1386. qvalue_t q;
  1387. int l, n;
  1388. str dst_uri;
  1389. if (s->n <= SEL_POS+1 && nr_branches > 1) { /* get all branches, if nr_branches==1 then use faster algorithm */
  1390. int len;
  1391. unsigned l2;
  1392. char *c;
  1393. init_branch_iterator();
  1394. len = 0;
  1395. while ((c = next_branch(&l, &q, &dst_uri, 0, 0, 0, 0, 0, 0))) {
  1396. if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
  1397. l = dst_uri.len;
  1398. c = dst_uri.s;
  1399. }
  1400. if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI) ) {
  1401. len += l;
  1402. }
  1403. if (q != Q_UNSPECIFIED && (s->params[SEL_POS].v.i & SEL_BRANCH_Q)) {
  1404. len += len_q(q);
  1405. if (s->params[SEL_POS].v.i & SEL_BRANCH_URI) {
  1406. len += 1 + Q_PARAM_LEN;
  1407. }
  1408. }
  1409. len += 1;
  1410. }
  1411. if (!len) return 1;
  1412. res->s = get_static_buffer(len);
  1413. if (!res->s) return -1;
  1414. init_branch_iterator();
  1415. res->len = 0;
  1416. n = 0;
  1417. while ((c = next_branch(&l, &q, &dst_uri, 0, 0, 0, 0, 0, 0))) {
  1418. if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
  1419. l = dst_uri.len;
  1420. c = dst_uri.s;
  1421. }
  1422. if (n) {
  1423. res->s[res->len] = ',';
  1424. res->len++;
  1425. }
  1426. if ((s->params[SEL_POS].v.i & SEL_BRANCH_Q) == 0) {
  1427. q = Q_UNSPECIFIED;
  1428. }
  1429. if ((s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) && q != Q_UNSPECIFIED) {
  1430. res->s[res->len] = '<';
  1431. res->len++;
  1432. memcpy(res->s+res->len, c, l);
  1433. res->len += l;
  1434. memcpy(res->s+res->len, Q_PARAM, Q_PARAM_LEN);
  1435. res->len += Q_PARAM_LEN;
  1436. c = q2str(q, &l2); l = l2;
  1437. memcpy(res->s+res->len, c, l);
  1438. res->len += l;
  1439. }
  1440. else if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) {
  1441. memcpy(res->s+res->len, c, l);
  1442. res->len += l;
  1443. }
  1444. else if (q != Q_UNSPECIFIED) {
  1445. c = q2str(q, &l2); l = l2;
  1446. memcpy(res->s+res->len, c, l);
  1447. res->len += l;
  1448. }
  1449. n++;
  1450. }
  1451. }
  1452. else {
  1453. unsigned l2;
  1454. char *c;
  1455. n = s->params[SEL_POS+1].v.i;
  1456. if (n < 0 || n >= nr_branches)
  1457. return -1;
  1458. init_branch_iterator();
  1459. for (; (c = next_branch(&l, &q, &dst_uri, 0, 0, 0, 0, 0, 0)) && n; n--);
  1460. if (!c) return 1;
  1461. if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
  1462. l = dst_uri.len;
  1463. c = dst_uri.s;
  1464. }
  1465. if ((s->params[SEL_POS].v.i & SEL_BRANCH_Q) == 0) {
  1466. q = Q_UNSPECIFIED;
  1467. }
  1468. if ((s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) && q != Q_UNSPECIFIED) {
  1469. res->s = get_static_buffer(l + 1 + Q_PARAM_LEN + len_q(q));
  1470. if (!res->s) return -1;
  1471. res->len = 1;
  1472. res->s[0] = '<';
  1473. memcpy(res->s+res->len, c, l);
  1474. res->len += l;
  1475. memcpy(res->s+res->len, Q_PARAM, Q_PARAM_LEN);
  1476. res->len += Q_PARAM_LEN;
  1477. c = q2str(q, &l2); l = l2;
  1478. memcpy(res->s+res->len, c, l);
  1479. res->len += l;
  1480. }
  1481. else if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) {
  1482. res->s = c; /* not necessary to copy to static buffer */
  1483. res->len = l;
  1484. }
  1485. else if (q != Q_UNSPECIFIED) {
  1486. c = q2str(q, &l2);
  1487. res->len = l2;
  1488. res->s = get_static_buffer(res->len);
  1489. if (!res->s) return -1;
  1490. memcpy(res->s, c, res->len);
  1491. }
  1492. else {
  1493. res->len = 0;
  1494. }
  1495. }
  1496. return 0;
  1497. #undef SEL_POS
  1498. }
  1499. int select_branch_uriq(str* res, select_t* s, struct sip_msg* msg) {
  1500. return select_branch_uri(res, s, msg);
  1501. }
  1502. int select_branch_q(str* res, select_t* s, struct sip_msg* msg) {
  1503. return select_branch_uri(res, s, msg);
  1504. }
  1505. int select_branch_dst_uri(str* res, select_t* s, struct sip_msg* msg) {
  1506. return select_branch_uri(res, s, msg);
  1507. }