select_core.c 35 KB

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