select_core.c 43 KB

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