parse_uri.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  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. * 2003-04-04 convenience inbound-uri parser parse_orig_ruri
  30. * introduced (jiri)
  31. * 2003-04-11 new parse_uri introduced (better, parses also some parameters,
  32. * works in one pass) (andrei)
  33. * 2003-04-11 ser_error is now set in parse_uri (andrei)
  34. * 2003-04-26 ZSW (jiri)
  35. * 2003-07-03 sips:, r2, lr=on support added (andrei)
  36. * 2005-02-25 preliminary tel uri support (andrei)
  37. * 2005-03-03 more tel uri fixes (andrei)
  38. * 2006-04-20 comp uri param. support (rfc3486) if defined USE_COMP (andrei)
  39. * 2007-09-10 introduced phone2tel option which allows NOT to consider
  40. * user=phone URIs as TEL URIs
  41. *
  42. */
  43. /** @file
  44. * @brief Parser :: Parse URI's
  45. *
  46. * @ingroup parser
  47. */
  48. #include "../globals.h"
  49. #include "parse_uri.h"
  50. #include <string.h>
  51. #include "../dprint.h"
  52. /* #ifdef PARSE_URI_OLD */ /* ZSW */
  53. #include "../ut.h" /* q_memchr */
  54. /* #endif */
  55. #include "../error.h"
  56. /* buf= pointer to begining of uri (sip:[email protected]:5060;a=b?h=i)
  57. * len= len of uri
  58. * returns: fills uri & returns <0 on error or 0 if ok
  59. */
  60. #ifndef PARSE_URI_OLD
  61. int parse_uri(char* buf, int len, struct sip_uri* uri)
  62. {
  63. enum states { URI_INIT, URI_USER, URI_PASSWORD, URI_PASSWORD_ALPHA,
  64. URI_HOST, URI_HOST_P,
  65. URI_HOST6_P, URI_HOST6_END, URI_PORT,
  66. URI_PARAM, URI_PARAM_P, URI_VAL_P, URI_HEADERS,
  67. /* param states */
  68. /* transport */
  69. PT_T, PT_R, PT_A, PT_N, PT_S, PT_P, PT_O, PT_R2, PT_T2,
  70. PT_eq,
  71. /* ttl */
  72. PTTL_T2, PTTL_L, PTTL_eq,
  73. /* user */
  74. PU_U, PU_S, PU_E, PU_R, PU_eq,
  75. /* method */
  76. PM_M, PM_E, PM_T, PM_H, PM_O, PM_D, PM_eq,
  77. /* maddr */
  78. PMA_A, PMA_D, PMA_D2, PMA_R, PMA_eq,
  79. /* lr */
  80. PLR_L, PLR_R_FIN, PLR_eq,
  81. /* r2 */
  82. PR2_R, PR2_2_FIN, PR2_eq,
  83. #ifdef USE_COMP
  84. /* comp */
  85. PCOMP_C, PCOMP_O, PCOMP_M, PCOMP_P, PCOMP_eq,
  86. /* comp values */
  87. /* sigcomp */
  88. VCOMP_S, VCOMP_SIGC_I, VCOMP_SIGC_G,
  89. VCOMP_SIGC_C, VCOMP_SIGC_O, VCOMP_SIGC_M,
  90. VCOMP_SIGC_P_FIN,
  91. /* sergz */
  92. VCOMP_SGZ_E, VCOMP_SGZ_R, VCOMP_SGZ_G,
  93. VCOMP_SGZ_Z_FIN,
  94. #endif
  95. /* transport values */
  96. /* udp */
  97. VU_U, VU_D, VU_P_FIN,
  98. /* tcp */
  99. VT_T, VT_C, VT_P_FIN,
  100. /* tls */
  101. VTLS_L, VTLS_S_FIN,
  102. /* sctp */
  103. VS_S, VS_C, VS_T, VS_P_FIN
  104. };
  105. register enum states state;
  106. char* s;
  107. char* b; /* param start */
  108. char *v; /* value start */
  109. str* param; /* current param */
  110. str* param_val; /* current param val */
  111. str user;
  112. str password;
  113. int port_no;
  114. register char* p;
  115. char* end;
  116. char* pass;
  117. int found_user;
  118. int error_headers;
  119. unsigned int scheme;
  120. uri_type backup_urit;
  121. uri_flags backup_urif;
  122. #ifdef USE_COMP
  123. str comp_str; /* not returned for now */
  124. str comp_val; /* not returned for now */
  125. #endif
  126. #define SIP_SCH 0x3a706973
  127. #define SIPS_SCH 0x73706973
  128. #define TEL_SCH 0x3a6c6574
  129. #define case_port( ch, var) \
  130. case ch: \
  131. (var)=(var)*10+ch-'0'; \
  132. break
  133. #define still_at_user \
  134. if (found_user==0){ \
  135. user.s=uri->host.s; \
  136. if (pass){\
  137. user.len=pass-user.s; \
  138. password.s=pass+1; \
  139. password.len=p-password.s; \
  140. }else{ \
  141. user.len=p-user.s; \
  142. }\
  143. /* save the uri type/scheme */ \
  144. backup_urit=uri->type; \
  145. backup_urif=uri->flags; \
  146. /* everything else is 0 */ \
  147. memset(uri, 0, sizeof(struct sip_uri)); \
  148. /* restore the scheme & flags, copy user & pass */ \
  149. uri->type=backup_urit; \
  150. uri->flags=backup_urif; \
  151. uri->user=user; \
  152. if (pass) uri->passwd=password; \
  153. s=p+1; \
  154. found_user=1;\
  155. error_headers=0; \
  156. state=URI_HOST; \
  157. }else goto error_bad_char
  158. #define check_host_end \
  159. case ':': \
  160. /* found the host */ \
  161. uri->host.s=s; \
  162. uri->host.len=p-s; \
  163. state=URI_PORT; \
  164. s=p+1; \
  165. break; \
  166. case ';': \
  167. uri->host.s=s; \
  168. uri->host.len=p-s; \
  169. state=URI_PARAM; \
  170. s=p+1; \
  171. break; \
  172. case '?': \
  173. uri->host.s=s; \
  174. uri->host.len=p-s; \
  175. state=URI_HEADERS; \
  176. s=p+1; \
  177. break; \
  178. case '&': \
  179. case '@': \
  180. goto error_bad_char
  181. #define param_set(t_start, v_start) \
  182. param->s=(t_start);\
  183. param->len=(p-(t_start));\
  184. param_val->s=(v_start); \
  185. param_val->len=(p-(v_start))
  186. #define semicolon_case \
  187. case';': \
  188. if (pass){ \
  189. found_user=1;/* no user, pass cannot contain ';'*/ \
  190. pass=0; \
  191. } \
  192. state=URI_PARAM /* new param */
  193. #define question_case \
  194. case '?': \
  195. uri->params.s=s; \
  196. uri->params.len=p-s; \
  197. state=URI_HEADERS; \
  198. s=p+1; \
  199. if (pass){ \
  200. found_user=1;/* no user, pass cannot contain '?'*/ \
  201. pass=0; \
  202. }
  203. #define colon_case \
  204. case ':': \
  205. if (found_user==0){ \
  206. /*might be pass but only if user not found yet*/ \
  207. if (pass){ \
  208. found_user=1; /* no user */ \
  209. pass=0; \
  210. }else{ \
  211. pass=p; \
  212. } \
  213. } \
  214. state=URI_PARAM_P /* generic param */
  215. #define param_common_cases \
  216. case '@': \
  217. /* ughhh, this is still the user */ \
  218. still_at_user; \
  219. break; \
  220. semicolon_case; \
  221. break; \
  222. question_case; \
  223. break; \
  224. colon_case; \
  225. break
  226. #define value_common_cases \
  227. case '@': \
  228. /* ughhh, this is still the user */ \
  229. still_at_user; \
  230. break; \
  231. semicolon_case; \
  232. param_set(b, v); \
  233. break; \
  234. question_case; \
  235. param_set(b, v); \
  236. break; \
  237. colon_case; \
  238. state=URI_VAL_P; \
  239. break
  240. #define param_switch(old_state, c1, c2, new_state) \
  241. case old_state: \
  242. switch(*p){ \
  243. case c1: \
  244. case c2: \
  245. state=(new_state); \
  246. break; \
  247. param_common_cases; \
  248. default: \
  249. state=URI_PARAM_P; \
  250. } \
  251. break
  252. #define param_switch1(old_state, c1, new_state) \
  253. case old_state: \
  254. switch(*p){ \
  255. case c1: \
  256. state=(new_state); \
  257. break; \
  258. param_common_cases; \
  259. default: \
  260. state=URI_PARAM_P; \
  261. } \
  262. break
  263. #define param_switch_big(old_state, c1, c2, d1, d2, new_state_c, new_state_d) \
  264. case old_state : \
  265. switch(*p){ \
  266. case c1: \
  267. case c2: \
  268. state=(new_state_c); \
  269. break; \
  270. case d1: \
  271. case d2: \
  272. state=(new_state_d); \
  273. break; \
  274. param_common_cases; \
  275. default: \
  276. state=URI_PARAM_P; \
  277. } \
  278. break
  279. #define value_switch(old_state, c1, c2, new_state) \
  280. case old_state: \
  281. switch(*p){ \
  282. case c1: \
  283. case c2: \
  284. state=(new_state); \
  285. break; \
  286. value_common_cases; \
  287. default: \
  288. state=URI_VAL_P; \
  289. } \
  290. break
  291. #define value_switch_big(old_state, c1, c2, d1, d2, new_state_c, new_state_d) \
  292. case old_state: \
  293. switch(*p){ \
  294. case c1: \
  295. case c2: \
  296. state=(new_state_c); \
  297. break; \
  298. case d1: \
  299. case d2: \
  300. state=(new_state_d); \
  301. break; \
  302. value_common_cases; \
  303. default: \
  304. state=URI_VAL_P; \
  305. } \
  306. break
  307. #define transport_fin(c_state, proto_no) \
  308. case c_state: \
  309. switch(*p){ \
  310. case '@': \
  311. still_at_user; \
  312. break; \
  313. semicolon_case; \
  314. param_set(b, v); \
  315. uri->proto=(proto_no); \
  316. break; \
  317. question_case; \
  318. param_set(b, v); \
  319. uri->proto=(proto_no); \
  320. break; \
  321. colon_case; \
  322. default: \
  323. state=URI_VAL_P; \
  324. break; \
  325. } \
  326. break
  327. #ifdef USE_COMP
  328. #define comp_fin(c_state, comp_no) \
  329. case c_state: \
  330. switch(*p){ \
  331. case '@': \
  332. still_at_user; \
  333. break; \
  334. semicolon_case; \
  335. /* param_set(b, v); */ \
  336. uri->comp=(comp_no); \
  337. break; \
  338. question_case; \
  339. /* param_set(b, v) */; \
  340. uri->comp=(comp_no); \
  341. break; \
  342. colon_case; \
  343. default: \
  344. state=URI_VAL_P; \
  345. break; \
  346. } \
  347. break
  348. #endif
  349. /* init */
  350. end=buf+len;
  351. p=buf+4;
  352. found_user=0;
  353. error_headers=0;
  354. b=v=0;
  355. param=param_val=0;
  356. pass=0;
  357. password.s=0; /* fixes gcc 4.0 warning */
  358. password.len=0;
  359. port_no=0;
  360. state=URI_INIT;
  361. memset(uri, 0, sizeof(struct sip_uri)); /* zero it all, just to be sure*/
  362. /*look for sip:, sips: or tel:*/
  363. if (len<5) goto error_too_short;
  364. scheme=buf[0]+(buf[1]<<8)+(buf[2]<<16)+(buf[3]<<24);
  365. scheme|=0x20202020;
  366. if (scheme==SIP_SCH){
  367. uri->type=SIP_URI_T;
  368. }else if(scheme==SIPS_SCH){
  369. if(buf[4]==':'){ p++; uri->type=SIPS_URI_T;}
  370. else goto error_bad_uri;
  371. }else if (scheme==TEL_SCH){
  372. uri->type=TEL_URI_T;
  373. }else goto error_bad_uri;
  374. s=p;
  375. for(;p<end; p++){
  376. switch((unsigned char)state){
  377. case URI_INIT:
  378. switch(*p){
  379. case '[':
  380. /* uri = [ipv6address]... */
  381. state=URI_HOST6_P;
  382. s=p;
  383. break;
  384. case ']':
  385. /* invalid, no uri can start with ']' */
  386. case ':':
  387. /* the same as above for ':' */
  388. goto error_bad_char;
  389. case '@': /* error no user part, or
  390. be forgiving and accept it ? */
  391. default:
  392. state=URI_USER;
  393. }
  394. break;
  395. case URI_USER:
  396. switch(*p){
  397. case '@':
  398. /* found the user*/
  399. uri->user.s=s;
  400. uri->user.len=p-s;
  401. state=URI_HOST;
  402. found_user=1;
  403. s=p+1; /* skip '@' */
  404. break;
  405. case ':':
  406. /* found the user, or the host? */
  407. uri->user.s=s;
  408. uri->user.len=p-s;
  409. state=URI_PASSWORD;
  410. s=p+1; /* skip ':' */
  411. break;
  412. case ';':
  413. /* this could be still the user or
  414. * params?*/
  415. uri->host.s=s;
  416. uri->host.len=p-s;
  417. state=URI_PARAM;
  418. s=p+1;
  419. break;
  420. case '?': /* still user or headers? */
  421. uri->host.s=s;
  422. uri->host.len=p-s;
  423. state=URI_HEADERS;
  424. s=p+1;
  425. break;
  426. /* almost anything permitted in the user part */
  427. case '.':
  428. case '-':
  429. case '(':
  430. case ')':
  431. /* tel uri visual separators, set flag meaning, that
  432. * user should be normalized before usage
  433. */
  434. uri->flags|=URI_USER_NORMALIZE;
  435. break;
  436. case '[':
  437. case ']': /* the user part cannot contain "[]" */
  438. goto error_bad_char;
  439. }
  440. break;
  441. case URI_PASSWORD: /* this can also be the port (missing user)*/
  442. switch(*p){
  443. case '@':
  444. /* found the password*/
  445. uri->passwd.s=s;
  446. uri->passwd.len=p-s;
  447. port_no=0;
  448. state=URI_HOST;
  449. found_user=1;
  450. s=p+1; /* skip '@' */
  451. break;
  452. case ';':
  453. /* upps this is the port */
  454. uri->port.s=s;
  455. uri->port.len=p-s;
  456. uri->port_no=port_no;
  457. /* user contains in fact the host */
  458. uri->host.s=uri->user.s;
  459. uri->host.len=uri->user.len;
  460. uri->user.s=0;
  461. uri->user.len=0;
  462. state=URI_PARAM;
  463. found_user=1; /* there is no user part */
  464. s=p+1;
  465. break;
  466. case '?':
  467. /* upps this is the port */
  468. uri->port.s=s;
  469. uri->port.len=p-s;
  470. uri->port_no=port_no;
  471. /* user contains in fact the host */
  472. uri->host.s=uri->user.s;
  473. uri->host.len=uri->user.len;
  474. uri->user.s=0;
  475. uri->user.len=0;
  476. state=URI_HEADERS;
  477. found_user=1; /* there is no user part */
  478. s=p+1;
  479. break;
  480. case_port('0', port_no);
  481. case_port('1', port_no);
  482. case_port('2', port_no);
  483. case_port('3', port_no);
  484. case_port('4', port_no);
  485. case_port('5', port_no);
  486. case_port('6', port_no);
  487. case_port('7', port_no);
  488. case_port('8', port_no);
  489. case_port('9', port_no);
  490. case '[':
  491. case ']':
  492. case ':':
  493. goto error_bad_char;
  494. default:
  495. /* it can't be the port, non number found */
  496. port_no=0;
  497. state=URI_PASSWORD_ALPHA;
  498. }
  499. break;
  500. case URI_PASSWORD_ALPHA:
  501. switch(*p){
  502. case '@':
  503. /* found the password*/
  504. uri->passwd.s=s;
  505. uri->passwd.len=p-s;
  506. state=URI_HOST;
  507. found_user=1;
  508. s=p+1; /* skip '@' */
  509. break;
  510. case ';': /* contains non-numbers => cannot be port no*/
  511. case '?':
  512. goto error_bad_port;
  513. case '[':
  514. case ']':
  515. case ':':
  516. goto error_bad_char;
  517. }
  518. break;
  519. case URI_HOST:
  520. switch(*p){
  521. case '[':
  522. state=URI_HOST6_P;
  523. break;
  524. case ':':
  525. case ';':
  526. case '?': /* null host name ->invalid */
  527. case '&':
  528. case '@': /*chars not allowed in hosts names */
  529. goto error_bad_host;
  530. default:
  531. state=URI_HOST_P;
  532. }
  533. break;
  534. case URI_HOST_P:
  535. switch(*p){
  536. check_host_end;
  537. }
  538. break;
  539. case URI_HOST6_END:
  540. switch(*p){
  541. check_host_end;
  542. default: /*no chars allowed after [ipv6] */
  543. goto error_bad_host;
  544. }
  545. break;
  546. case URI_HOST6_P:
  547. switch(*p){
  548. case ']':
  549. state=URI_HOST6_END;
  550. break;
  551. case '[':
  552. case '&':
  553. case '@':
  554. case ';':
  555. case '?':
  556. goto error_bad_host;
  557. }
  558. break;
  559. case URI_PORT:
  560. switch(*p){
  561. case ';':
  562. uri->port.s=s;
  563. uri->port.len=p-s;
  564. uri->port_no=port_no;
  565. state=URI_PARAM;
  566. s=p+1;
  567. break;
  568. case '?':
  569. uri->port.s=s;
  570. uri->port.len=p-s;
  571. uri->port_no=port_no;
  572. state=URI_HEADERS;
  573. s=p+1;
  574. break;
  575. case_port('0', port_no);
  576. case_port('1', port_no);
  577. case_port('2', port_no);
  578. case_port('3', port_no);
  579. case_port('4', port_no);
  580. case_port('5', port_no);
  581. case_port('6', port_no);
  582. case_port('7', port_no);
  583. case_port('8', port_no);
  584. case_port('9', port_no);
  585. case '&':
  586. case '@':
  587. case ':':
  588. default:
  589. goto error_bad_port;
  590. }
  591. break;
  592. case URI_PARAM: /* beginning of a new param */
  593. switch(*p){
  594. param_common_cases;
  595. /* recognized params */
  596. case 't':
  597. case 'T':
  598. b=p;
  599. state=PT_T;
  600. break;
  601. case 'u':
  602. case 'U':
  603. b=p;
  604. state=PU_U;
  605. break;
  606. case 'm':
  607. case 'M':
  608. b=p;
  609. state=PM_M;
  610. break;
  611. case 'l':
  612. case 'L':
  613. b=p;
  614. state=PLR_L;
  615. break;
  616. case 'r':
  617. case 'R':
  618. b=p;
  619. state=PR2_R;
  620. break;
  621. #ifdef USE_COMP
  622. case 'c':
  623. case 'C':
  624. b=p;
  625. state=PCOMP_C;
  626. break;
  627. #endif
  628. default:
  629. state=URI_PARAM_P;
  630. }
  631. break;
  632. case URI_PARAM_P: /* ignore current param */
  633. /* supported params:
  634. * maddr, transport, ttl, lr, user, method, r2 */
  635. switch(*p){
  636. param_common_cases;
  637. };
  638. break;
  639. /* ugly but fast param names parsing */
  640. /*transport */
  641. param_switch_big(PT_T, 'r', 'R', 't', 'T', PT_R, PTTL_T2);
  642. param_switch(PT_R, 'a', 'A', PT_A);
  643. param_switch(PT_A, 'n', 'N', PT_N);
  644. param_switch(PT_N, 's', 'S', PT_S);
  645. param_switch(PT_S, 'p', 'P', PT_P);
  646. param_switch(PT_P, 'o', 'O', PT_O);
  647. param_switch(PT_O, 'r', 'R', PT_R2);
  648. param_switch(PT_R2, 't', 'T', PT_T2);
  649. param_switch1(PT_T2, '=', PT_eq);
  650. /* value parsing */
  651. case PT_eq:
  652. param=&uri->transport;
  653. param_val=&uri->transport_val;
  654. switch (*p){
  655. param_common_cases;
  656. case 'u':
  657. case 'U':
  658. v=p;
  659. state=VU_U;
  660. break;
  661. case 't':
  662. case 'T':
  663. v=p;
  664. state=VT_T;
  665. break;
  666. case 's':
  667. case 'S':
  668. v=p;
  669. state=VS_S;
  670. break;
  671. default:
  672. v=p;
  673. state=URI_VAL_P;
  674. }
  675. break;
  676. /* generic value */
  677. case URI_VAL_P:
  678. switch(*p){
  679. value_common_cases;
  680. }
  681. break;
  682. /* udp */
  683. value_switch(VU_U, 'd', 'D', VU_D);
  684. value_switch(VU_D, 'p', 'P', VU_P_FIN);
  685. transport_fin(VU_P_FIN, PROTO_UDP);
  686. /* tcp */
  687. value_switch_big(VT_T, 'c', 'C', 'l', 'L', VT_C, VTLS_L);
  688. value_switch(VT_C, 'p', 'P', VT_P_FIN);
  689. transport_fin(VT_P_FIN, PROTO_TCP);
  690. /* tls */
  691. value_switch(VTLS_L, 's', 'S', VTLS_S_FIN);
  692. transport_fin(VTLS_S_FIN, PROTO_TLS);
  693. /* sctp */
  694. value_switch(VS_S, 'c', 'C', VS_C);
  695. value_switch(VS_C, 't', 'T', VS_T);
  696. value_switch(VS_T, 'p', 'P', VS_P_FIN);
  697. transport_fin(VS_P_FIN, PROTO_SCTP);
  698. /* ttl */
  699. param_switch(PTTL_T2, 'l', 'L', PTTL_L);
  700. param_switch1(PTTL_L, '=', PTTL_eq);
  701. case PTTL_eq:
  702. param=&uri->ttl;
  703. param_val=&uri->ttl_val;
  704. switch(*p){
  705. param_common_cases;
  706. default:
  707. v=p;
  708. state=URI_VAL_P;
  709. }
  710. break;
  711. /* user param */
  712. param_switch(PU_U, 's', 'S', PU_S);
  713. param_switch(PU_S, 'e', 'E', PU_E);
  714. param_switch(PU_E, 'r', 'R', PU_R);
  715. param_switch1(PU_R, '=', PU_eq);
  716. case PU_eq:
  717. param=&uri->user_param;
  718. param_val=&uri->user_param_val;
  719. switch(*p){
  720. param_common_cases;
  721. default:
  722. v=p;
  723. state=URI_VAL_P;
  724. }
  725. break;
  726. /* method*/
  727. param_switch_big(PM_M, 'e', 'E', 'a', 'A', PM_E, PMA_A);
  728. param_switch(PM_E, 't', 'T', PM_T);
  729. param_switch(PM_T, 'h', 'H', PM_H);
  730. param_switch(PM_H, 'o', 'O', PM_O);
  731. param_switch(PM_O, 'd', 'D', PM_D);
  732. param_switch1(PM_D, '=', PM_eq);
  733. case PM_eq:
  734. param=&uri->method;
  735. param_val=&uri->method_val;
  736. switch(*p){
  737. param_common_cases;
  738. default:
  739. v=p;
  740. state=URI_VAL_P;
  741. }
  742. break;
  743. /*maddr*/
  744. param_switch(PMA_A, 'd', 'D', PMA_D);
  745. param_switch(PMA_D, 'd', 'D', PMA_D2);
  746. param_switch(PMA_D2, 'r', 'R', PMA_R);
  747. param_switch1(PMA_R, '=', PMA_eq);
  748. case PMA_eq:
  749. param=&uri->maddr;
  750. param_val=&uri->maddr_val;
  751. switch(*p){
  752. param_common_cases;
  753. default:
  754. v=p;
  755. state=URI_VAL_P;
  756. }
  757. break;
  758. /* lr */
  759. param_switch(PLR_L, 'r', 'R', PLR_R_FIN);
  760. case PLR_R_FIN:
  761. switch(*p){
  762. case '@':
  763. still_at_user;
  764. break;
  765. case '=':
  766. state=PLR_eq;
  767. break;
  768. semicolon_case;
  769. uri->lr.s=b;
  770. uri->lr.len=(p-b);
  771. break;
  772. question_case;
  773. uri->lr.s=b;
  774. uri->lr.len=(p-b);
  775. break;
  776. colon_case;
  777. break;
  778. default:
  779. state=URI_PARAM_P;
  780. }
  781. break;
  782. /* handle lr=something case */
  783. case PLR_eq:
  784. param=&uri->lr;
  785. param_val=&uri->lr_val;
  786. switch(*p){
  787. param_common_cases;
  788. default:
  789. v=p;
  790. state=URI_VAL_P;
  791. }
  792. break;
  793. /* r2 */
  794. param_switch1(PR2_R, '2', PR2_2_FIN);
  795. case PR2_2_FIN:
  796. switch(*p){
  797. case '@':
  798. still_at_user;
  799. break;
  800. case '=':
  801. state=PR2_eq;
  802. break;
  803. semicolon_case;
  804. uri->r2.s=b;
  805. uri->r2.len=(p-b);
  806. break;
  807. question_case;
  808. uri->r2.s=b;
  809. uri->r2.len=(p-b);
  810. break;
  811. colon_case;
  812. break;
  813. default:
  814. state=URI_PARAM_P;
  815. }
  816. break;
  817. /* handle lr=something case */
  818. case PR2_eq:
  819. param=&uri->r2;
  820. param_val=&uri->r2_val;
  821. switch(*p){
  822. param_common_cases;
  823. default:
  824. v=p;
  825. state=URI_VAL_P;
  826. }
  827. break;
  828. #ifdef USE_COMP
  829. param_switch(PCOMP_C, 'o', 'O' , PCOMP_O);
  830. param_switch(PCOMP_O, 'm', 'M' , PCOMP_M);
  831. param_switch(PCOMP_M, 'p', 'P' , PCOMP_P);
  832. param_switch1(PCOMP_P, '=', PCOMP_eq);
  833. /* value */
  834. case PCOMP_eq:
  835. param=&comp_str;
  836. param_val=&comp_val;
  837. switch (*p){
  838. param_common_cases;
  839. case 's':
  840. case 'S':
  841. v=p;
  842. state=VCOMP_S;
  843. break;
  844. default:
  845. v=p;
  846. state=URI_VAL_P;
  847. }
  848. break;
  849. /* sigcomp*/
  850. value_switch_big(VCOMP_S, 'i', 'I', 'e', 'E',
  851. VCOMP_SIGC_I, VCOMP_SGZ_E);
  852. value_switch(VCOMP_SIGC_I, 'g', 'G', VCOMP_SIGC_G);
  853. value_switch(VCOMP_SIGC_G, 'c', 'C', VCOMP_SIGC_C);
  854. value_switch(VCOMP_SIGC_C, 'o', 'O', VCOMP_SIGC_O);
  855. value_switch(VCOMP_SIGC_O, 'm', 'M', VCOMP_SIGC_M);
  856. value_switch(VCOMP_SIGC_M, 'p', 'P', VCOMP_SIGC_P_FIN);
  857. comp_fin(VCOMP_SIGC_P_FIN, COMP_SIGCOMP);
  858. /* sergz*/
  859. value_switch(VCOMP_SGZ_E, 'r', 'R', VCOMP_SGZ_R);
  860. value_switch(VCOMP_SGZ_R, 'g', 'G', VCOMP_SGZ_G);
  861. value_switch(VCOMP_SGZ_G, 'z', 'Z', VCOMP_SGZ_Z_FIN);
  862. comp_fin(VCOMP_SGZ_Z_FIN, COMP_SERGZ);
  863. #endif
  864. case URI_HEADERS:
  865. /* for now nobody needs them so we completely ignore the
  866. * headers (they are not allowed in request uri) --andrei */
  867. switch(*p){
  868. case '@':
  869. /* yak, we are still at user */
  870. still_at_user;
  871. break;
  872. case ';':
  873. /* we might be still parsing user, try it */
  874. if (found_user) goto error_bad_char;
  875. error_headers=1; /* if this is not the user
  876. we have an error */
  877. /* if pass is set => it cannot be user:pass
  878. * => error (';') is illegal in a header */
  879. if (pass) goto error_headers;
  880. break;
  881. case ':':
  882. if (found_user==0){
  883. /*might be pass but only if user not found yet*/
  884. if (pass){
  885. found_user=1; /* no user */
  886. pass=0;
  887. }else{
  888. pass=p;
  889. }
  890. }
  891. break;
  892. case '?':
  893. if (pass){
  894. found_user=1; /* no user, pass cannot contain '?'*/
  895. pass=0;
  896. }
  897. break;
  898. }
  899. break;
  900. default:
  901. goto error_bug;
  902. }
  903. }
  904. /*end of uri */
  905. switch (state){
  906. case URI_INIT: /* error empty uri */
  907. goto error_too_short;
  908. case URI_USER:
  909. /* this is the host, it can't be the user */
  910. if (found_user) goto error_bad_uri;
  911. uri->host.s=s;
  912. uri->host.len=p-s;
  913. state=URI_HOST;
  914. break;
  915. case URI_PASSWORD:
  916. /* this is the port, it can't be the passwd */
  917. if (found_user) goto error_bad_port;
  918. uri->port.s=s;
  919. uri->port.len=p-s;
  920. uri->port_no=port_no;
  921. uri->host=uri->user;
  922. uri->user.s=0;
  923. uri->user.len=0;
  924. break;
  925. case URI_PASSWORD_ALPHA:
  926. /* this is the port, it can't be the passwd */
  927. goto error_bad_port;
  928. case URI_HOST_P:
  929. case URI_HOST6_END:
  930. uri->host.s=s;
  931. uri->host.len=p-s;
  932. break;
  933. case URI_HOST: /* error: null host */
  934. case URI_HOST6_P: /* error: unterminated ipv6 reference*/
  935. goto error_bad_host;
  936. case URI_PORT:
  937. uri->port.s=s;
  938. uri->port.len=p-s;
  939. uri->port_no=port_no;
  940. break;
  941. case URI_PARAM:
  942. case URI_PARAM_P:
  943. /* intermediate param states */
  944. case PT_T: /* transport */
  945. case PT_R:
  946. case PT_A:
  947. case PT_N:
  948. case PT_S:
  949. case PT_P:
  950. case PT_O:
  951. case PT_R2:
  952. case PT_T2:
  953. case PT_eq: /* ignore empty transport params */
  954. case PTTL_T2: /* ttl */
  955. case PTTL_L:
  956. case PTTL_eq:
  957. case PU_U: /* user */
  958. case PU_S:
  959. case PU_E:
  960. case PU_R:
  961. case PU_eq:
  962. case PM_M: /* method */
  963. case PM_E:
  964. case PM_T:
  965. case PM_H:
  966. case PM_O:
  967. case PM_D:
  968. case PM_eq:
  969. case PLR_L: /* lr */
  970. case PR2_R: /* r2 */
  971. #ifdef USE_COMP
  972. case PCOMP_C:
  973. case PCOMP_O:
  974. case PCOMP_M:
  975. case PCOMP_P:
  976. case PCOMP_eq:
  977. #endif
  978. uri->params.s=s;
  979. uri->params.len=p-s;
  980. break;
  981. /* fin param states */
  982. case PLR_R_FIN:
  983. case PLR_eq:
  984. uri->params.s=s;
  985. uri->params.len=p-s;
  986. uri->lr.s=b;
  987. uri->lr.len=p-b;
  988. break;
  989. case PR2_2_FIN:
  990. case PR2_eq:
  991. uri->params.s=s;
  992. uri->params.len=p-s;
  993. uri->r2.s=b;
  994. uri->r2.len=p-b;
  995. break;
  996. case URI_VAL_P:
  997. /* intermediate value states */
  998. case VU_U:
  999. case VU_D:
  1000. case VT_T:
  1001. case VT_C:
  1002. case VTLS_L:
  1003. case VS_S:
  1004. case VS_C:
  1005. case VS_T:
  1006. uri->params.s=s;
  1007. uri->params.len=p-s;
  1008. param_set(b, v);
  1009. break;
  1010. #ifdef USE_COMP
  1011. case VCOMP_S:
  1012. case VCOMP_SIGC_I:
  1013. case VCOMP_SIGC_G:
  1014. case VCOMP_SIGC_C:
  1015. case VCOMP_SIGC_O:
  1016. case VCOMP_SIGC_M:
  1017. case VCOMP_SGZ_E:
  1018. case VCOMP_SGZ_R:
  1019. case VCOMP_SGZ_G:
  1020. /* unrecognized comp method, assume none */
  1021. uri->params.s=s;
  1022. uri->params.len=p-s;
  1023. /* uri->comp=COMP_NONE ; */
  1024. break;
  1025. #endif
  1026. /* fin value states */
  1027. case VU_P_FIN:
  1028. uri->params.s=s;
  1029. uri->params.len=p-s;
  1030. param_set(b, v);
  1031. uri->proto=PROTO_UDP;
  1032. break;
  1033. case VT_P_FIN:
  1034. uri->params.s=s;
  1035. uri->params.len=p-s;
  1036. param_set(b, v);
  1037. uri->proto=PROTO_TCP;
  1038. break;
  1039. case VTLS_S_FIN:
  1040. uri->params.s=s;
  1041. uri->params.len=p-s;
  1042. param_set(b, v);
  1043. uri->proto=PROTO_TLS;
  1044. break;
  1045. case VS_P_FIN:
  1046. uri->params.s=s;
  1047. uri->params.len=p-s;
  1048. param_set(b, v);
  1049. uri->proto=PROTO_SCTP;
  1050. break;
  1051. #ifdef USE_COMP
  1052. case VCOMP_SIGC_P_FIN:
  1053. uri->params.s=s;
  1054. uri->params.len=p-s;
  1055. /* param_set(b, v); */
  1056. uri->comp=COMP_SIGCOMP;
  1057. break;
  1058. case VCOMP_SGZ_Z_FIN:
  1059. uri->params.s=s;
  1060. uri->params.len=p-s;
  1061. /* param_set(b, v); */
  1062. uri->comp=COMP_SERGZ;
  1063. break;
  1064. #endif
  1065. /* headers */
  1066. case URI_HEADERS:
  1067. uri->headers.s=s;
  1068. uri->headers.len=p-s;
  1069. if (error_headers) goto error_headers;
  1070. break;
  1071. default:
  1072. goto error_bug;
  1073. }
  1074. switch(uri->type){
  1075. case SIPS_URI_T:
  1076. case SIP_URI_T:
  1077. /* save the original sip: URI parameters in sip_params */
  1078. uri->sip_params=uri->params;
  1079. if ((phone2tel) &&
  1080. (uri->user_param_val.len == 5) &&
  1081. (strncmp(uri->user_param_val.s, "phone", 5) == 0)
  1082. ) {
  1083. uri->type = TEL_URI_T;
  1084. uri->flags |= URI_SIP_USER_PHONE;
  1085. /* move params from user into uri->params */
  1086. p=q_memchr(uri->user.s, ';', uri->user.len);
  1087. if (p){
  1088. /* NOTE:
  1089. * specialized uri params (user, maddr, etc.) still hold
  1090. * the values from the sip-uri envelope
  1091. * while uri->params point to the params in the embedded tel uri
  1092. */
  1093. uri->params.s=p+1;
  1094. uri->params.len=uri->user.s+uri->user.len-uri->params.s;
  1095. uri->user.len=p-uri->user.s;
  1096. } else {
  1097. uri->params.s=0;
  1098. uri->params.len=0;
  1099. }
  1100. } else {
  1101. uri->flags&=~URI_USER_NORMALIZE;
  1102. }
  1103. break;
  1104. case TEL_URI_T:
  1105. case TELS_URI_T:
  1106. /* fix tel uris, move the number in uri and empty the host */
  1107. uri->user=uri->host;
  1108. uri->host.s="";
  1109. uri->host.len=0;
  1110. break;
  1111. case ERROR_URI_T:
  1112. LOG(L_ERR, "ERROR: parse_uri unexpected error (BUG?)\n");
  1113. goto error_bad_uri;
  1114. break; /* do nothing, avoids a compilation warning */
  1115. }
  1116. #ifdef EXTRA_DEBUG
  1117. /* do stuff */
  1118. DBG("parsed uri:\n type=%d user=<%.*s>(%d)\n passwd=<%.*s>(%d)\n"
  1119. " host=<%.*s>(%d)\n port=<%.*s>(%d): %d\n params=<%.*s>(%d)\n"
  1120. " headers=<%.*s>(%d)\n",
  1121. uri->type,
  1122. uri->user.len, ZSW(uri->user.s), uri->user.len,
  1123. uri->passwd.len, ZSW(uri->passwd.s), uri->passwd.len,
  1124. uri->host.len, ZSW(uri->host.s), uri->host.len,
  1125. uri->port.len, ZSW(uri->port.s), uri->port.len, uri->port_no,
  1126. uri->params.len, ZSW(uri->params.s), uri->params.len,
  1127. uri->headers.len, ZSW(uri->headers.s), uri->headers.len
  1128. );
  1129. DBG(" uri flags : ");
  1130. if (uri->flags & URI_USER_NORMALIZE) DBG("user_need_norm ");
  1131. if (uri->flags & URI_SIP_USER_PHONE) DBG("sip_user_phone ");
  1132. DBG(" value=%d\n",uri->flags);
  1133. DBG(" uri params:\n transport=<%.*s>, val=<%.*s>, proto=%d\n",
  1134. uri->transport.len, ZSW(uri->transport.s), uri->transport_val.len,
  1135. ZSW(uri->transport_val.s), uri->proto);
  1136. DBG(" user-param=<%.*s>, val=<%.*s>\n",
  1137. uri->user_param.len, ZSW(uri->user_param.s),
  1138. uri->user_param_val.len, ZSW(uri->user_param_val.s));
  1139. DBG(" method=<%.*s>, val=<%.*s>\n",
  1140. uri->method.len, ZSW(uri->method.s),
  1141. uri->method_val.len, ZSW(uri->method_val.s));
  1142. DBG(" ttl=<%.*s>, val=<%.*s>\n",
  1143. uri->ttl.len, ZSW(uri->ttl.s),
  1144. uri->ttl_val.len, ZSW(uri->ttl_val.s));
  1145. DBG(" maddr=<%.*s>, val=<%.*s>\n",
  1146. uri->maddr.len, ZSW(uri->maddr.s),
  1147. uri->maddr_val.len, ZSW(uri->maddr_val.s));
  1148. DBG(" lr=<%.*s>\n", uri->lr.len, ZSW(uri->lr.s));
  1149. DBG(" r2=<%.*s>\n", uri->r2.len, ZSW(uri->r2.s));
  1150. #ifdef USE_COMP
  1151. DBG(" comp=%d\n", uri->comp);
  1152. #endif
  1153. #endif
  1154. return 0;
  1155. error_too_short:
  1156. DBG("parse_uri: uri too short: <%.*s> (%d)\n",
  1157. len, ZSW(buf), len);
  1158. goto error_exit;
  1159. error_bad_char:
  1160. DBG("parse_uri: bad char '%c' in state %d"
  1161. " parsed: <%.*s> (%d) / <%.*s> (%d)\n",
  1162. *p, state, (int)(p-buf), ZSW(buf), (int)(p-buf),
  1163. len, ZSW(buf), len);
  1164. goto error_exit;
  1165. error_bad_host:
  1166. DBG("parse_uri: bad host in uri (error at char %c in"
  1167. " state %d) parsed: <%.*s>(%d) /<%.*s> (%d)\n",
  1168. *p, state, (int)(p-buf), ZSW(buf), (int)(p-buf),
  1169. len, ZSW(buf), len);
  1170. goto error_exit;
  1171. error_bad_port:
  1172. DBG("parse_uri: bad port in uri (error at char %c in"
  1173. " state %d) parsed: <%.*s>(%d) /<%.*s> (%d)\n",
  1174. *p, state, (int)(p-buf), ZSW(buf), (int)(p-buf),
  1175. len, ZSW(buf), len);
  1176. goto error_exit;
  1177. error_bad_uri:
  1178. DBG("parse_uri: bad uri, state %d"
  1179. " parsed: <%.*s> (%d) / <%.*s> (%d)\n",
  1180. state, (int)(p-buf), ZSW(buf), (int)(p-buf), len,
  1181. ZSW(buf), len);
  1182. goto error_exit;
  1183. error_headers:
  1184. DBG("parse_uri: bad uri headers: <%.*s>(%d)"
  1185. " / <%.*s>(%d)\n",
  1186. uri->headers.len, ZSW(uri->headers.s), uri->headers.len,
  1187. len, ZSW(buf), len);
  1188. goto error_exit;
  1189. error_bug:
  1190. LOG(L_CRIT, "BUG: parse_uri: bad state %d"
  1191. " parsed: <%.*s> (%d) / <%.*s> (%d)\n",
  1192. state, (int)(p-buf), ZSW(buf), (int)(p-buf), len, ZSW(buf), len);
  1193. error_exit:
  1194. ser_error=E_BAD_URI;
  1195. uri->type=ERROR_URI_T;
  1196. return E_BAD_URI;
  1197. }
  1198. #else /* PARSE_URI_OLD */
  1199. int parse_uri(char *buf, int len, struct sip_uri* uri)
  1200. {
  1201. char* next, *end;
  1202. char *user, *passwd, *host, *port, *params, *headers, *ipv6;
  1203. int host_len, port_len, params_len, headers_len;
  1204. int err;
  1205. int ret;
  1206. ret=0;
  1207. host_len=0;
  1208. end=buf+len;
  1209. memset(uri, 0, sizeof(struct sip_uri)); /* zero it all, just to be sure */
  1210. /* look for "sip:"*/;
  1211. next=q_memchr(buf, ':', len);
  1212. if ((next==0)||(strncasecmp(buf,"sip",next-buf)!=0)){
  1213. LOG(L_DBG, "ERROR: parse_uri: bad sip uri\n");
  1214. ser_error=ret=E_BAD_URI;
  1215. return ret;
  1216. }
  1217. buf=next+1; /* next char after ':' */
  1218. if (buf>end){
  1219. LOG(L_DBG, "ERROR: parse_uri: uri too short\n");
  1220. ser_error=ret=E_BAD_URI;
  1221. return ret;
  1222. }
  1223. /*look for '@' */
  1224. next=q_memchr(buf,'@', end-buf);
  1225. if (next==0){
  1226. /* no '@' found, => no userinfo */
  1227. uri->user.s=0;
  1228. uri->passwd.s=0;
  1229. host=buf;
  1230. }else{
  1231. /* found it */
  1232. user=buf;
  1233. /* try to find passwd */
  1234. passwd=q_memchr(user,':', next-user);
  1235. if (passwd==0){
  1236. /* no ':' found => no password */
  1237. uri->passwd.s=0;
  1238. uri->user.s=user;
  1239. uri->user.len=next-user;
  1240. }else{
  1241. uri->user.s=user;
  1242. uri->user.len=passwd-user;
  1243. passwd++; /*skip ':' */
  1244. uri->passwd.s=passwd;
  1245. uri->passwd.len=next-passwd;
  1246. }
  1247. host=next+1; /* skip '@' */
  1248. }
  1249. /* try to find the rest */
  1250. if(host>=end){
  1251. LOG(L_DBG, "ERROR: parse_uri: missing hostport\n");
  1252. ser_error=ret=E_UNSPEC;
  1253. return ret;
  1254. }
  1255. next=host;
  1256. ipv6=q_memchr(host, '[', end-host);
  1257. if (ipv6){
  1258. host=ipv6+1; /* skip '[' in "[3ffe::abbcd]" */
  1259. if (host>=end){
  1260. LOG(L_DBG, "ERROR: parse_uri: bad ipv6 uri\n");
  1261. ret=E_UNSPEC;
  1262. return ret;
  1263. }
  1264. ipv6=q_memchr(host, ']', end-host);
  1265. if ((ipv6==0)||(ipv6==host)){
  1266. LOG(L_DBG, "ERROR: parse_uri: bad ipv6 uri - null address"
  1267. " or missing ']'\n");
  1268. ret=E_UNSPEC;
  1269. return ret;
  1270. }
  1271. host_len=ipv6-host;
  1272. next=ipv6;
  1273. }
  1274. headers=q_memchr(next,'?',end-next);
  1275. params=q_memchr(next,';',end-next);
  1276. port=q_memchr(next,':',end-next);
  1277. if (host_len==0){ /* host not ipv6 addr */
  1278. host_len=(port)?port-host:(params)?params-host:(headers)?headers-host:
  1279. end-host;
  1280. }
  1281. /* get host */
  1282. uri->host.s=host;
  1283. uri->host.len=host_len;
  1284. /* get port*/
  1285. if ((port)&&(port+1<end)){
  1286. port++;
  1287. if ( ((params) &&(params<port))||((headers) &&(headers<port)) ){
  1288. /* error -> invalid uri we found ';' or '?' before ':' */
  1289. LOG(L_DBG, "ERROR: parse_uri: malformed sip uri\n");
  1290. ser_error=ret=E_BAD_URI;
  1291. return ret;
  1292. }
  1293. port_len=(params)?params-port:(headers)?headers-port:end-port;
  1294. uri->port.s=port;
  1295. uri->port.len=port_len;
  1296. }else uri->port.s=0;
  1297. /* get params */
  1298. if ((params)&&(params+1<end)){
  1299. params++;
  1300. if ((headers) && (headers<params)){
  1301. /* error -> invalid uri we found '?' or '?' before ';' */
  1302. LOG(L_DBG, "ERROR: parse_uri: malformed sip uri\n");
  1303. ser_error=ret=E_BAD_URI;
  1304. return ret;
  1305. }
  1306. params_len=(headers)?headers-params:end-params;
  1307. uri->params.s=params;
  1308. uri->params.len=params_len;
  1309. }else uri->params.s=0;
  1310. /*get headers */
  1311. if ((headers)&&(headers+1<end)){
  1312. headers++;
  1313. headers_len=end-headers;
  1314. uri->headers.s=headers;
  1315. uri->headers.len=headers_len;
  1316. }else uri->headers.s=0;
  1317. err=0;
  1318. if (uri->port.s) uri->port_no=str2s(uri->port.s, uri->port.len, &err);
  1319. if (err){
  1320. LOG(L_DBG, "ERROR: parse_uri: bad port number in sip uri: %.*s\n",
  1321. uri->port.len, ZSW(uri->port.s));
  1322. ser_error=ret=E_BAD_URI;
  1323. return ret;
  1324. }
  1325. return ret;
  1326. }
  1327. #endif
  1328. static inline int _parse_ruri(str *uri,
  1329. int *status, struct sip_uri *parsed_uri)
  1330. {
  1331. if (*status) return 1;
  1332. if (parse_uri(uri->s, uri->len, parsed_uri)<0) {
  1333. LOG(L_ERR, "ERROR: _parse_ruri: bad uri <%.*s>\n",
  1334. uri->len, ZSW(uri->s));
  1335. *status=0;
  1336. return -1;
  1337. }
  1338. *status=1;
  1339. return 1;
  1340. }
  1341. int parse_sip_msg_uri(struct sip_msg* msg)
  1342. {
  1343. char* tmp;
  1344. int tmp_len;
  1345. if (msg->parsed_uri_ok) return 1;
  1346. if (msg->new_uri.s){
  1347. tmp=msg->new_uri.s;
  1348. tmp_len=msg->new_uri.len;
  1349. }else{
  1350. tmp=msg->first_line.u.request.uri.s;
  1351. tmp_len=msg->first_line.u.request.uri.len;
  1352. }
  1353. if (parse_uri(tmp, tmp_len, &msg->parsed_uri)<0){
  1354. DBG("ERROR: parse_sip_msg_uri: bad uri <%.*s>\n",
  1355. tmp_len, tmp);
  1356. msg->parsed_uri_ok=0;
  1357. return -1;
  1358. }
  1359. msg->parsed_uri_ok=1;
  1360. return 1;
  1361. }
  1362. int parse_orig_ruri(struct sip_msg* msg)
  1363. {
  1364. int ret;
  1365. ret=_parse_ruri(&REQ_LINE(msg).uri,
  1366. &msg->parsed_orig_ruri_ok, &msg->parsed_orig_ruri);
  1367. if (ret<0) LOG(L_ERR, "ERROR: parse_orig_ruri failed\n");
  1368. return ret;
  1369. }
  1370. inline int normalize_tel_user(char* res, str* src) {
  1371. int i, l;
  1372. l=0;
  1373. for (i=0; i<src->len; i++) {
  1374. switch (src->s[i]) {
  1375. case '-':
  1376. case '.':
  1377. case '(':
  1378. case ')':
  1379. break;
  1380. default:
  1381. res[l++]=src->s[i];
  1382. }
  1383. }
  1384. return l;
  1385. }
  1386. str s_sip = STR_STATIC_INIT("sip");
  1387. str s_sips = STR_STATIC_INIT("sips");
  1388. str s_tel = STR_STATIC_INIT("tel");
  1389. str s_tels = STR_STATIC_INIT("tels");
  1390. static str s_null = STR_STATIC_INIT("");
  1391. inline void uri_type_to_str(uri_type type, str *s) {
  1392. switch (type) {
  1393. case SIP_URI_T:
  1394. *s = s_sip;
  1395. break;
  1396. case SIPS_URI_T:
  1397. *s = s_sips;
  1398. break;
  1399. case TEL_URI_T:
  1400. *s = s_tel;
  1401. break;
  1402. case TELS_URI_T:
  1403. *s = s_tels;
  1404. break;
  1405. default:
  1406. *s = s_null;
  1407. }
  1408. }
  1409. static str s_udp = STR_STATIC_INIT("udp");
  1410. static str s_tcp = STR_STATIC_INIT("tcp");
  1411. static str s_tls = STR_STATIC_INIT("tls");
  1412. static str s_sctp = STR_STATIC_INIT("sctp");
  1413. inline void proto_type_to_str(unsigned short type, str *s) {
  1414. switch (type) {
  1415. case PROTO_UDP:
  1416. *s = s_udp;
  1417. break;
  1418. case PROTO_TCP:
  1419. *s = s_tcp;
  1420. break;
  1421. case PROTO_TLS:
  1422. *s = s_tls;
  1423. break;
  1424. case PROTO_SCTP:
  1425. *s = s_sctp;
  1426. break;
  1427. default:
  1428. *s = s_null;
  1429. }
  1430. }