select_core.c 41 KB

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