reqresp_parser.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*!
  17. * \file
  18. * \brief sip request parsing functions and unit tests
  19. */
  20. /*** MODULEINFO
  21. <support_level>extended</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  25. #include "include/sip.h"
  26. #include "include/sip_utils.h"
  27. #include "include/reqresp_parser.h"
  28. #ifdef HAVE_XLOCALE_H
  29. locale_t c_locale;
  30. #endif
  31. /*! \brief * parses a URI in its components.*/
  32. int parse_uri_full(char *uri, const char *scheme, char **user, char **pass,
  33. char **hostport, struct uriparams *params, char **headers,
  34. char **residue)
  35. {
  36. char *userinfo = NULL;
  37. char *parameters = NULL;
  38. char *endparams = NULL;
  39. char *c = NULL;
  40. int error = 0;
  41. int teluri_scheme = 0;
  42. /*
  43. * Initialize requested strings - some functions don't care if parse_uri fails
  44. * and will attempt to use string pointers passed into parse_uri even after a
  45. * parse_uri failure
  46. */
  47. if (user) {
  48. *user = "";
  49. }
  50. if (pass) {
  51. *pass = "";
  52. }
  53. if (hostport) {
  54. *hostport = "";
  55. }
  56. if (headers) {
  57. *headers = "";
  58. }
  59. if (residue) {
  60. *residue = "";
  61. }
  62. /* check for valid input */
  63. if (ast_strlen_zero(uri)) {
  64. return -1;
  65. }
  66. if (scheme) {
  67. int l;
  68. char *scheme2 = ast_strdupa(scheme);
  69. char *cur = strsep(&scheme2, ",");
  70. for (; !ast_strlen_zero(cur); cur = strsep(&scheme2, ",")) {
  71. l = strlen(cur);
  72. if (!strncasecmp(uri, cur, l)) {
  73. teluri_scheme = !strncasecmp(uri, "tel:", 4); /* TEL URI */
  74. uri += l;
  75. break;
  76. }
  77. }
  78. if (ast_strlen_zero(cur)) {
  79. ast_debug(1, "No supported scheme found in '%s' using the scheme[s] %s\n", uri, scheme);
  80. error = -1;
  81. }
  82. }
  83. if (!hostport) {
  84. /* if we don't want to split around hostport, keep everything as a
  85. * userinfo - cos thats how old parse_uri operated*/
  86. userinfo = uri;
  87. } else if (teluri_scheme) {
  88. /*
  89. * tel: TEL URI INVITE RFC 3966 patch
  90. * See http://www.ietf.org/rfc/rfc3966.txt
  91. *
  92. * Once the full RFC 3966 parsing is implemented,
  93. * the ext= or isub= parameters would be extracted from userinfo.
  94. * When this kind of subaddressing would be implemented, the userinfo must be further parsed.
  95. * Those parameters would be used for ISDN or PSTN local extensions.
  96. *
  97. * Current restrictions:
  98. * We currently consider the ";isub=" or the ";ext=" as part of the userinfo (unparsed).
  99. */
  100. if ((c = strstr(uri, ";phone-context="))) {
  101. /*
  102. * Local number with context or domain.
  103. * ext= or isub= TEL URI parameters should be upfront.
  104. * All other parameters should come after the ";phone-context=" parameter.
  105. * If other parameters would occur before ";phone-context=" they will be ignored.
  106. */
  107. *c = '\0';
  108. userinfo = uri;
  109. uri = c + 15;
  110. *hostport = uri;
  111. } else {
  112. userinfo = uri;
  113. *hostport = uri;
  114. }
  115. //change by dingyu for <tel:08723096401>;tag=sbc05072fe11f4eeb.rqoryuowwosqs-zqtq (RFC 3966 china mobile IMS)
  116. // } else if ('+' == uri[0]) {
  117. /* Global number without context or domain; possibly followed by RFC 3966 and optional other parameters. */
  118. // userinfo = uri;
  119. // *hostport = uri;
  120. // } else {
  121. // ast_debug(1, "No RFC 3966 global number or context found in '%s'; returning local number anyway\n", uri);
  122. // userinfo = uri; /* Return local number anyway */
  123. // error = -1;
  124. // }
  125. } else {
  126. char *dom = "";
  127. if ((c = strchr(uri, '@'))) {
  128. *c++ = '\0';
  129. dom = c;
  130. userinfo = uri;
  131. uri = c; /* userinfo can contain ? and ; chars so step forward before looking for params and headers */
  132. } else {
  133. /* domain-only URI, according to the SIP RFC. */
  134. dom = uri;
  135. userinfo = "";
  136. }
  137. *hostport = dom;
  138. }
  139. if (pass && (c = strchr(userinfo, ':'))) { /* user:password */
  140. *c++ = '\0';
  141. *pass = c;
  142. } else if (pass) {
  143. *pass = "";
  144. }
  145. if (user) {
  146. *user = userinfo;
  147. }
  148. parameters = uri;
  149. /* strip [?headers] from end of uri - even if no header pointer exists*/
  150. if ((c = strrchr(uri, '?'))) {
  151. *c++ = '\0';
  152. uri = c;
  153. if (headers) {
  154. *headers = c;
  155. }
  156. if ((c = strrchr(uri, ';'))) {
  157. *c++ = '\0';
  158. } else {
  159. c = strrchr(uri, '\0');
  160. }
  161. uri = c; /* residue */
  162. } else if (headers) {
  163. *headers = "";
  164. }
  165. /* parse parameters */
  166. endparams = strchr(parameters,'\0');
  167. if ((c = strchr(parameters, ';'))) {
  168. *c++ = '\0';
  169. parameters = c;
  170. } else {
  171. parameters = endparams;
  172. }
  173. if (params) {
  174. char *rem = parameters; /* unparsed or unrecognised remainder */
  175. char *label;
  176. char *value;
  177. int lr = 0;
  178. params->transport = "";
  179. params->user = "";
  180. params->method = "";
  181. params->ttl = "";
  182. params->maddr = "";
  183. params->lr = 0;
  184. rem = parameters;
  185. while ((value = strchr(parameters, '=')) || (lr = !strncmp(parameters, "lr", 2))) {
  186. /* The while condition will not continue evaluation to set lr if it matches "lr=" */
  187. if (lr) {
  188. value = parameters;
  189. } else {
  190. *value++ = '\0';
  191. }
  192. label = parameters;
  193. if ((c = strchr(value, ';'))) {
  194. *c++ = '\0';
  195. parameters = c;
  196. } else {
  197. parameters = endparams;
  198. }
  199. if (!strcmp(label, "transport")) {
  200. params->transport = value;
  201. rem = parameters;
  202. } else if (!strcmp(label, "user")) {
  203. params->user = value;
  204. rem = parameters;
  205. } else if (!strcmp(label, "method")) {
  206. params->method = value;
  207. rem = parameters;
  208. } else if (!strcmp(label, "ttl")) {
  209. params->ttl = value;
  210. rem = parameters;
  211. } else if (!strcmp(label, "maddr")) {
  212. params->maddr = value;
  213. rem = parameters;
  214. /* Treat "lr", "lr=yes", "lr=on", "lr=1", "lr=almostanything" as lr enabled and "", "lr=no", "lr=off", "lr=0", "lr=" and "lranything" as lr disabled */
  215. } else if ((!strcmp(label, "lr") && strcmp(value, "no") && strcmp(value, "off") && strcmp(value, "0") && strcmp(value, "")) || ((lr) && strcmp(value, "lr"))) {
  216. params->lr = 1;
  217. rem = parameters;
  218. } else {
  219. value--;
  220. *value = '=';
  221. if (c) {
  222. c--;
  223. *c = ';';
  224. }
  225. }
  226. }
  227. if (rem > uri) { /* no headers */
  228. uri = rem;
  229. }
  230. }
  231. if (residue) {
  232. *residue = uri;
  233. }
  234. return error;
  235. }
  236. #ifdef TEST_FRAMEWORK
  237. AST_TEST_DEFINE(sip_parse_uri_full_test)
  238. {
  239. int res = AST_TEST_PASS;
  240. char uri[1024];
  241. char *user, *pass, *hostport, *headers, *residue;
  242. struct uriparams params;
  243. struct testdata {
  244. char *desc;
  245. char *uri;
  246. char *user;
  247. char *pass;
  248. char *hostport;
  249. char *headers;
  250. char *residue;
  251. struct uriparams params;
  252. AST_LIST_ENTRY(testdata) list;
  253. };
  254. struct testdata *testdataptr;
  255. static AST_LIST_HEAD_NOLOCK(testdataliststruct, testdata) testdatalist;
  256. struct testdata td1 = {
  257. .desc = "no headers",
  258. .uri = "sip:user:secret@host:5060;param=discard;transport=tcp;param2=residue",
  259. .user = "user",
  260. .pass = "secret",
  261. .hostport = "host:5060",
  262. .headers = "",
  263. .residue = "param2=residue",
  264. .params.transport = "tcp",
  265. .params.lr = 0,
  266. .params.user = ""
  267. };
  268. struct testdata td2 = {
  269. .desc = "with headers",
  270. .uri = "sip:user:secret@host:5060;param=discard;transport=tcp;param2=discard2?header=blah&header2=blah2;param3=residue",
  271. .user = "user",
  272. .pass = "secret",
  273. .hostport = "host:5060",
  274. .headers = "header=blah&header2=blah2",
  275. .residue = "param3=residue",
  276. .params.transport = "tcp",
  277. .params.lr = 0,
  278. .params.user = ""
  279. };
  280. struct testdata td3 = {
  281. .desc = "difficult user",
  282. .uri = "sip:-_.!~*'()&=+$,;?/:secret@host:5060;transport=tcp",
  283. .user = "-_.!~*'()&=+$,;?/",
  284. .pass = "secret",
  285. .hostport = "host:5060",
  286. .headers = "",
  287. .residue = "",
  288. .params.transport = "tcp",
  289. .params.lr = 0,
  290. .params.user = ""
  291. };
  292. struct testdata td4 = {
  293. .desc = "difficult pass",
  294. .uri = "sip:user:-_.!~*'()&=+$,@host:5060;transport=tcp",
  295. .user = "user",
  296. .pass = "-_.!~*'()&=+$,",
  297. .hostport = "host:5060",
  298. .headers = "",
  299. .residue = "",
  300. .params.transport = "tcp",
  301. .params.lr = 0,
  302. .params.user = ""
  303. };
  304. struct testdata td5 = {
  305. .desc = "difficult host",
  306. .uri = "sip:user:secret@1-1.a-1.:5060;transport=tcp",
  307. .user = "user",
  308. .pass = "secret",
  309. .hostport = "1-1.a-1.:5060",
  310. .headers = "",
  311. .residue = "",
  312. .params.transport = "tcp",
  313. .params.lr = 0,
  314. .params.user = ""
  315. };
  316. struct testdata td6 = {
  317. .desc = "difficult params near transport",
  318. .uri = "sip:user:secret@host:5060;-_.!~*'()[]/:&+$=-_.!~*'()[]/:&+$;transport=tcp",
  319. .user = "user",
  320. .pass = "secret",
  321. .hostport = "host:5060",
  322. .headers = "",
  323. .residue = "",
  324. .params.transport = "tcp",
  325. .params.lr = 0,
  326. .params.user = ""
  327. };
  328. struct testdata td7 = {
  329. .desc = "difficult params near headers",
  330. .uri = "sip:user:secret@host:5060;-_.!~*'()[]/:&+$=-_.!~*'()[]/:&+$?header=blah&header2=blah2;-_.!~*'()[]/:&+$=residue",
  331. .user = "user",
  332. .pass = "secret",
  333. .hostport = "host:5060",
  334. .headers = "header=blah&header2=blah2",
  335. .residue = "-_.!~*'()[]/:&+$=residue",
  336. .params.transport = "",
  337. .params.lr = 0,
  338. .params.user = ""
  339. };
  340. struct testdata td8 = {
  341. .desc = "lr parameter",
  342. .uri = "sip:user:secret@host:5060;param=discard;lr?header=blah",
  343. .user = "user",
  344. .pass = "secret",
  345. .hostport = "host:5060",
  346. .headers = "header=blah",
  347. .residue = "",
  348. .params.transport = "",
  349. .params.lr = 1,
  350. .params.user = ""
  351. };
  352. struct testdata td9 = {
  353. .desc = "alternative lr parameter",
  354. .uri = "sip:user:secret@host:5060;param=discard;lr=yes?header=blah",
  355. .user = "user",
  356. .pass = "secret",
  357. .hostport = "host:5060",
  358. .headers = "header=blah",
  359. .residue = "",
  360. .params.transport = "",
  361. .params.lr = 1,
  362. .params.user = ""
  363. };
  364. struct testdata td10 = {
  365. .desc = "no lr parameter",
  366. .uri = "sip:user:secret@host:5060;paramlr=lr;lr=no;lr=off;lr=0;lr=;=lr;lrextra;lrparam2=lr?header=blah",
  367. .user = "user",
  368. .pass = "secret",
  369. .hostport = "host:5060",
  370. .headers = "header=blah",
  371. .residue = "",
  372. .params.transport = "",
  373. .params.lr = 0,
  374. .params.user = ""
  375. };
  376. /* RFC 3966 TEL URI INVITE */
  377. struct testdata td11 = {
  378. .desc = "tel local number",
  379. .uri = "tel:0987654321;phone-context=+32987654321",
  380. .user = "0987654321",
  381. .pass = "",
  382. .hostport = "+32987654321",
  383. .headers = "",
  384. .residue = "",
  385. .params.transport = "",
  386. .params.lr = 0,
  387. .params.user = ""
  388. };
  389. struct testdata td12 = {
  390. .desc = "tel global number",
  391. .uri = "tel:+32987654321",
  392. .user = "+32987654321",
  393. .pass = "",
  394. .hostport = "+32987654321",
  395. .headers = "",
  396. .residue = "",
  397. .params.transport = "",
  398. .params.lr = 0,
  399. .params.user = ""
  400. };
  401. /*
  402. * Once the full RFC 3966 parsing is implemented,
  403. * only the ext= or isub= parameters would be extracted from .user
  404. * Then the ;param=discard would be ignored,
  405. * and the .user would only contain "0987654321"
  406. */
  407. struct testdata td13 = {
  408. .desc = "tel local number",
  409. .uri = "tel:0987654321;ext=1234;param=discard;phone-context=+32987654321;transport=udp;param2=discard2?header=blah&header2=blah2;param3=residue",
  410. .user = "0987654321;ext=1234;param=discard",
  411. .pass = "",
  412. .hostport = "+32987654321",
  413. .headers = "header=blah&header2=blah2",
  414. .residue = "param3=residue",
  415. .params.transport = "udp",
  416. .params.lr = 0,
  417. .params.user = ""
  418. };
  419. AST_LIST_HEAD_SET_NOLOCK(&testdatalist, &td1);
  420. AST_LIST_INSERT_TAIL(&testdatalist, &td2, list);
  421. AST_LIST_INSERT_TAIL(&testdatalist, &td3, list);
  422. AST_LIST_INSERT_TAIL(&testdatalist, &td4, list);
  423. AST_LIST_INSERT_TAIL(&testdatalist, &td5, list);
  424. AST_LIST_INSERT_TAIL(&testdatalist, &td6, list);
  425. AST_LIST_INSERT_TAIL(&testdatalist, &td7, list);
  426. AST_LIST_INSERT_TAIL(&testdatalist, &td8, list);
  427. AST_LIST_INSERT_TAIL(&testdatalist, &td9, list);
  428. AST_LIST_INSERT_TAIL(&testdatalist, &td10, list);
  429. AST_LIST_INSERT_TAIL(&testdatalist, &td11, list);
  430. AST_LIST_INSERT_TAIL(&testdatalist, &td12, list);
  431. AST_LIST_INSERT_TAIL(&testdatalist, &td13, list);
  432. switch (cmd) {
  433. case TEST_INIT:
  434. info->name = "sip_uri_full_parse_test";
  435. info->category = "/channels/chan_sip/";
  436. info->summary = "tests sip full uri parsing";
  437. info->description =
  438. "Tests full parsing of various URIs "
  439. "Verifies output matches expected behavior.";
  440. return AST_TEST_NOT_RUN;
  441. case TEST_EXECUTE:
  442. break;
  443. }
  444. AST_LIST_TRAVERSE(&testdatalist, testdataptr, list) {
  445. user = pass = hostport = headers = residue = NULL;
  446. params.transport = params.user = params.method = params.ttl = params.maddr = NULL;
  447. params.lr = 0;
  448. ast_copy_string(uri,testdataptr->uri,sizeof(uri));
  449. if (parse_uri_full(uri, "sip:,sips:,tel:", &user,
  450. &pass, &hostport,
  451. &params,
  452. &headers,
  453. &residue) ||
  454. (user && strcmp(testdataptr->user, user)) ||
  455. (pass && strcmp(testdataptr->pass, pass)) ||
  456. (hostport && strcmp(testdataptr->hostport, hostport)) ||
  457. (headers && strcmp(testdataptr->headers, headers)) ||
  458. (residue && strcmp(testdataptr->residue, residue)) ||
  459. (strcmp(testdataptr->params.transport,params.transport)) ||
  460. (testdataptr->params.lr != params.lr) ||
  461. (strcmp(testdataptr->params.user,params.user))
  462. ) {
  463. ast_test_status_update(test, "Sub-Test: %s, failed.\n", testdataptr->desc);
  464. res = AST_TEST_FAIL;
  465. }
  466. }
  467. return res;
  468. }
  469. #endif
  470. int parse_uri(char *uri, const char *scheme, char **user, char **pass,
  471. char **hostport, char **transport) {
  472. int ret;
  473. char *headers;
  474. struct uriparams params;
  475. headers = NULL;
  476. ret = parse_uri_full(uri, scheme, user, pass, hostport, &params, &headers, NULL);
  477. if (transport) {
  478. *transport=params.transport;
  479. }
  480. return ret;
  481. }
  482. #ifdef TEST_FRAMEWORK
  483. AST_TEST_DEFINE(sip_parse_uri_test)
  484. {
  485. int res = AST_TEST_PASS;
  486. char *name, *pass, *hostport, *transport;
  487. char uri1[] = "sip:name@host";
  488. char uri2[] = "sip:name@host;transport=tcp";
  489. char uri3[] = "sip:name:secret@host;transport=tcp";
  490. char uri4[] = "sip:name:secret@host:port;transport=tcp?headers=%40%40testblah&headers2=blah%20blah";
  491. /* test 5 is for NULL input */
  492. char uri6[] = "sip:name:secret@host:port;transport=tcp?headers=%40%40testblah&headers2=blah%20blah";
  493. char uri7[] = "sip:name:secret@host:port;transport=tcp?headers=%40%40testblah&headers2=blah%20blah";
  494. char uri8[] = "sip:host";
  495. char uri9[] = "sip:host:port;transport=tcp?headers=%40%40testblah&headers2=blah%20blah";
  496. char uri10[] = "host:port;transport=tcp?headers=%40%40testblah&headers2=blah%20blah";
  497. char uri11[] = "host";
  498. char uri12[] = "tel:911"; /* TEL URI Local number without context or global number */
  499. switch (cmd) {
  500. case TEST_INIT:
  501. info->name = "sip_uri_parse_test";
  502. info->category = "/channels/chan_sip/";
  503. info->summary = "tests sip uri parsing";
  504. info->description =
  505. "Tests parsing of various URIs "
  506. "Verifies output matches expected behavior.";
  507. return AST_TEST_NOT_RUN;
  508. case TEST_EXECUTE:
  509. break;
  510. }
  511. /* Test 1, simple URI */
  512. name = pass = hostport = transport = NULL;
  513. if (parse_uri(uri1, "sip:,sips:", &name, &pass, &hostport, &transport) ||
  514. strcmp(name, "name") ||
  515. !ast_strlen_zero(pass) ||
  516. strcmp(hostport, "host") ||
  517. !ast_strlen_zero(transport)) {
  518. ast_test_status_update(test, "Test 1: simple uri failed. \n");
  519. res = AST_TEST_FAIL;
  520. }
  521. /* Test 2, add tcp transport */
  522. name = pass = hostport = transport = NULL;
  523. if (parse_uri(uri2, "sip:,sips:", &name, &pass, &hostport, &transport) ||
  524. strcmp(name, "name") ||
  525. !ast_strlen_zero(pass) ||
  526. strcmp(hostport, "host") ||
  527. strcmp(transport, "tcp")) {
  528. ast_test_status_update(test, "Test 2: uri with addtion of tcp transport failed. \n");
  529. res = AST_TEST_FAIL;
  530. }
  531. /* Test 3, add secret */
  532. name = pass = hostport = transport = NULL;
  533. if (parse_uri(uri3, "sip:,sips:", &name, &pass, &hostport, &transport) ||
  534. strcmp(name, "name") ||
  535. strcmp(pass, "secret") ||
  536. strcmp(hostport, "host") ||
  537. strcmp(transport, "tcp")) {
  538. ast_test_status_update(test, "Test 3: uri with addition of secret failed.\n");
  539. res = AST_TEST_FAIL;
  540. }
  541. /* Test 4, add port and unparsed header field*/
  542. name = pass = hostport = transport = NULL;
  543. if (parse_uri(uri4, "sip:,sips:", &name, &pass, &hostport, &transport) ||
  544. strcmp(name, "name") ||
  545. strcmp(pass, "secret") ||
  546. strcmp(hostport, "host:port") ||
  547. strcmp(transport, "tcp")) {
  548. ast_test_status_update(test, "Test 4: add port and unparsed header field failed.\n");
  549. res = AST_TEST_FAIL;
  550. }
  551. /* Test 5, verify parse_uri does not crash when given a NULL uri */
  552. name = pass = hostport = transport = NULL;
  553. if (!parse_uri(NULL, "sip:,sips:", &name, &pass, &hostport, &transport)) {
  554. ast_test_status_update(test, "Test 5: passing a NULL uri failed.\n");
  555. res = AST_TEST_FAIL;
  556. }
  557. /* Test 6, verify parse_uri does not crash when given a NULL output parameters */
  558. name = pass = hostport = transport = NULL;
  559. if (parse_uri(uri6, "sip:,sips:", NULL, NULL, NULL, NULL)) {
  560. ast_test_status_update(test, "Test 6: passing NULL output parameters failed.\n");
  561. res = AST_TEST_FAIL;
  562. }
  563. /* Test 7, verify parse_uri returns user:secret and hostport when no port or secret output parameters are supplied. */
  564. name = pass = hostport = transport = NULL;
  565. if (parse_uri(uri7, "sip:,sips:", &name, NULL, &hostport, NULL) ||
  566. strcmp(name, "name:secret") ||
  567. strcmp(hostport, "host:port")) {
  568. ast_test_status_update(test, "Test 7: providing no port and secret output parameters failed.\n");
  569. res = AST_TEST_FAIL;
  570. }
  571. /* Test 8, verify parse_uri can handle a hostport only uri */
  572. name = pass = hostport = transport = NULL;
  573. if (parse_uri(uri8, "sip:,sips:", &name, &pass, &hostport, &transport) ||
  574. strcmp(hostport, "host") ||
  575. !ast_strlen_zero(name)) {
  576. ast_test_status_update(test, "Test 8: add port and unparsed header field failed.\n");
  577. res = AST_TEST_FAIL;
  578. }
  579. /* Test 9, add port and unparsed header field with hostport only uri*/
  580. name = pass = hostport = transport = NULL;
  581. if (parse_uri(uri9, "sip:,sips:", &name, &pass, &hostport, &transport) ||
  582. !ast_strlen_zero(name) ||
  583. !ast_strlen_zero(pass) ||
  584. strcmp(hostport, "host:port") ||
  585. strcmp(transport, "tcp")) {
  586. ast_test_status_update(test, "Test 9: hostport only uri failed \n");
  587. res = AST_TEST_FAIL;
  588. }
  589. /* Test 10, handle invalid/missing "sip:,sips:" scheme
  590. * we expect parse_uri to return an error, but still parse
  591. * the results correctly here */
  592. name = pass = hostport = transport = NULL;
  593. if (!parse_uri(uri10, "sip:,sips:", &name, &pass, &hostport, &transport) ||
  594. !ast_strlen_zero(name) ||
  595. !ast_strlen_zero(pass) ||
  596. strcmp(hostport, "host:port") ||
  597. strcmp(transport, "tcp")) {
  598. ast_test_status_update(test, "Test 10: missing \"sip:sips:\" scheme failed\n");
  599. res = AST_TEST_FAIL;
  600. }
  601. /* Test 11, simple hostport only URI with missing scheme
  602. * we expect parse_uri to return an error, but still parse
  603. * the results correctly here */
  604. name = pass = hostport = transport = NULL;
  605. if (!parse_uri(uri11, "sip:,sips:", &name, &pass, &hostport, &transport) ||
  606. !ast_strlen_zero(name) ||
  607. !ast_strlen_zero(pass) ||
  608. strcmp(hostport, "host") ||
  609. !ast_strlen_zero(transport)) {
  610. ast_test_status_update(test, "Test 11: simple uri with missing scheme failed. \n");
  611. res = AST_TEST_FAIL;
  612. }
  613. /* Test 12, simple URI */
  614. name = pass = hostport = transport = NULL;
  615. if (!parse_uri(uri12, "sip:,sips:,tel:", &name, &pass, &hostport, &transport) ||
  616. strcmp(name, "911") || /* We return local number anyway */
  617. !ast_strlen_zero(pass) ||
  618. !ast_strlen_zero(hostport) || /* No global number nor context */
  619. !ast_strlen_zero(transport)) {
  620. ast_test_status_update(test, "Test 12: TEL URI INVITE failed.\n");
  621. res = AST_TEST_FAIL;
  622. }
  623. return res;
  624. }
  625. #endif
  626. /*! \brief Get caller id name from SIP headers, copy into output buffer
  627. *
  628. * \retval input string pointer placed after display-name field if possible
  629. */
  630. const char *get_calleridname(const char *input, char *output, size_t outputsize)
  631. {
  632. /* From RFC3261:
  633. *
  634. * From = ( "From" / "f" ) HCOLON from-spec
  635. * from-spec = ( name-addr / addr-spec ) *( SEMI from-param )
  636. * name-addr = [ display-name ] LAQUOT addr-spec RAQUOT
  637. * display-name = *(token LWS)/ quoted-string
  638. * token = 1*(alphanum / "-" / "." / "!" / "%" / "*"
  639. * / "_" / "+" / "`" / "'" / "~" )
  640. * quoted-string = SWS DQUOTE *(qdtext / quoted-pair ) DQUOTE
  641. * qdtext = LWS / %x21 / %x23-5B / %x5D-7E
  642. * / UTF8-NONASCII
  643. * quoted-pair = "\" (%x00-09 / %x0B-0C / %x0E-7F)
  644. *
  645. * HCOLON = *WSP ":" SWS
  646. * SWS = [LWS]
  647. * LWS = *[*WSP CRLF] 1*WSP
  648. * WSP = (SP / HTAB)
  649. *
  650. * Deviations from it:
  651. * - following CRLF's in LWS is not done (here at least)
  652. * - ascii NUL is never legal as it terminates the C-string
  653. * - utf8-nonascii is not checked for validity
  654. */
  655. char *orig_output = output;
  656. const char *orig_input = input;
  657. if (!output || !outputsize) {
  658. /* Bad output parameters. Should never happen. */
  659. return input;
  660. }
  661. /* clear any empty characters in the beginning */
  662. input = ast_skip_blanks(input);
  663. /* make sure the output buffer is initilized */
  664. *orig_output = '\0';
  665. /* make room for '\0' at the end of the output buffer */
  666. --outputsize;
  667. /* no data at all or no display name? */
  668. if (!input || *input == '<') {
  669. return input;
  670. }
  671. /* quoted-string rules */
  672. if (input[0] == '"') {
  673. input++; /* skip the first " */
  674. for (; *input; ++input) {
  675. if (*input == '"') { /* end of quoted-string */
  676. break;
  677. } else if (*input == 0x5c) { /* quoted-pair = "\" (%x00-09 / %x0B-0C / %x0E-7F) */
  678. ++input;
  679. if (!*input) {
  680. break;
  681. }
  682. if ((unsigned char) *input > 0x7f || *input == 0xa || *input == 0xd) {
  683. continue; /* not a valid quoted-pair, so skip it */
  684. }
  685. } else if ((*input != 0x9 && (unsigned char) *input < 0x20)
  686. || *input == 0x7f) {
  687. continue; /* skip this invalid character. */
  688. }
  689. if (0 < outputsize) {
  690. /* We still have room for the output display-name. */
  691. *output++ = *input;
  692. --outputsize;
  693. }
  694. }
  695. /* if this is successful, input should be at the ending quote */
  696. if (*input != '"') {
  697. ast_log(LOG_WARNING, "No ending quote for display-name was found\n");
  698. *orig_output = '\0';
  699. return orig_input;
  700. }
  701. /* make sure input is past the last quote */
  702. ++input;
  703. /* terminate output */
  704. *output = '\0';
  705. } else { /* either an addr-spec or tokenLWS-combo */
  706. for (; *input; ++input) {
  707. /* token or WSP (without LWS) */
  708. if ((*input >= '0' && *input <= '9') || (*input >= 'A' && *input <= 'Z')
  709. || (*input >= 'a' && *input <= 'z') || *input == '-' || *input == '.'
  710. || *input == '!' || *input == '%' || *input == '*' || *input == '_'
  711. || *input == '+' || *input == '`' || *input == '\'' || *input == '~'
  712. || *input == 0x9 || *input == ' ') {
  713. if (0 < outputsize) {
  714. /* We still have room for the output display-name. */
  715. *output++ = *input;
  716. --outputsize;
  717. }
  718. } else if (*input == '<') { /* end of tokenLWS-combo */
  719. /* we could assert that the previous char is LWS, but we don't care */
  720. break;
  721. } else if (*input == ':') {
  722. /* This invalid character which indicates this is addr-spec rather than display-name. */
  723. *orig_output = '\0';
  724. return orig_input;
  725. } else { /* else, invalid character we can skip. */
  726. continue; /* skip this character */
  727. }
  728. }
  729. if (*input != '<') { /* if we never found the start of addr-spec then this is invalid */
  730. *orig_output = '\0';
  731. return orig_input;
  732. }
  733. /* terminate output while trimming any trailing whitespace */
  734. do {
  735. *output-- = '\0';
  736. } while (orig_output <= output && (*output == 0x9 || *output == ' '));
  737. }
  738. return input;
  739. }
  740. #ifdef TEST_FRAMEWORK
  741. AST_TEST_DEFINE(get_calleridname_test)
  742. {
  743. int res = AST_TEST_PASS;
  744. const char *in1 = " \" quoted-text internal \\\" quote \"<stuff>";
  745. const char *in2 = " token text with no quotes <stuff>";
  746. const char *overflow1 = " \"quoted-text overflow 1234567890123456789012345678901234567890\" <stuff>";
  747. const char *overflow2 = " non-quoted text overflow 1234567890123456789012345678901234567890 <stuff>";
  748. const char *noendquote = " \"quoted-text no end <stuff>";
  749. const char *addrspec = " sip:blah@blah";
  750. const char *no_quotes_no_brackets = "blah@blah";
  751. const char *after_dname;
  752. char dname[40];
  753. switch (cmd) {
  754. case TEST_INIT:
  755. info->name = "sip_get_calleridname_test";
  756. info->category = "/channels/chan_sip/";
  757. info->summary = "decodes callerid name from sip header";
  758. info->description = "Decodes display-name field of sip header. Checks for valid output and expected failure cases.";
  759. return AST_TEST_NOT_RUN;
  760. case TEST_EXECUTE:
  761. break;
  762. }
  763. /* quoted-text with backslash escaped quote */
  764. after_dname = get_calleridname(in1, dname, sizeof(dname));
  765. ast_test_status_update(test, "display-name1: %s\nafter: %s\n", dname, after_dname);
  766. if (strcmp(dname, " quoted-text internal \" quote ")) {
  767. ast_test_status_update(test, "display-name1 test failed\n");
  768. res = AST_TEST_FAIL;
  769. }
  770. /* token text */
  771. after_dname = get_calleridname(in2, dname, sizeof(dname));
  772. ast_test_status_update(test, "display-name2: %s\nafter: %s\n", dname, after_dname);
  773. if (strcmp(dname, "token text with no quotes")) {
  774. ast_test_status_update(test, "display-name2 test failed\n");
  775. res = AST_TEST_FAIL;
  776. }
  777. /* quoted-text buffer overflow */
  778. after_dname = get_calleridname(overflow1, dname, sizeof(dname));
  779. ast_test_status_update(test, "overflow display-name1: %s\nafter: %s\n", dname, after_dname);
  780. if (strcmp(dname, "quoted-text overflow 123456789012345678")) {
  781. ast_test_status_update(test, "overflow display-name1 test failed\n");
  782. res = AST_TEST_FAIL;
  783. }
  784. /* non-quoted-text buffer overflow */
  785. after_dname = get_calleridname(overflow2, dname, sizeof(dname));
  786. ast_test_status_update(test, "overflow display-name2: %s\nafter: %s\n", dname, after_dname);
  787. if (strcmp(dname, "non-quoted text overflow 12345678901234")) {
  788. ast_test_status_update(test, "overflow display-name2 test failed\n");
  789. res = AST_TEST_FAIL;
  790. }
  791. /* quoted-text buffer with no terminating end quote */
  792. after_dname = get_calleridname(noendquote, dname, sizeof(dname));
  793. ast_test_status_update(test, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
  794. if (*dname != '\0' && after_dname != noendquote) {
  795. ast_test_status_update(test, "no end quote for quoted-text display-name failed\n");
  796. res = AST_TEST_FAIL;
  797. }
  798. /* addr-spec rather than display-name. */
  799. after_dname = get_calleridname(addrspec, dname, sizeof(dname));
  800. ast_test_status_update(test, "addr-spec display-name1: %s\nafter: %s\n", dname, after_dname);
  801. if (*dname != '\0' && after_dname != addrspec) {
  802. ast_test_status_update(test, "detection of addr-spec failed\n");
  803. res = AST_TEST_FAIL;
  804. }
  805. /* no quotes, no brackets */
  806. after_dname = get_calleridname(no_quotes_no_brackets, dname, sizeof(dname));
  807. ast_test_status_update(test, "no_quotes_no_brackets display-name1: %s\nafter: %s\n", dname, after_dname);
  808. if (*dname != '\0' && after_dname != no_quotes_no_brackets) {
  809. ast_test_status_update(test, "detection of addr-spec failed\n");
  810. res = AST_TEST_FAIL;
  811. }
  812. return res;
  813. }
  814. #endif
  815. int get_name_and_number(const char *hdr, char **name, char **number)
  816. {
  817. char header[256];
  818. char tmp_name[256];
  819. char *tmp_number = NULL;
  820. char *hostport = NULL;
  821. char *dummy = NULL;
  822. if (!name || !number || ast_strlen_zero(hdr)) {
  823. return -1;
  824. }
  825. *number = NULL;
  826. *name = NULL;
  827. ast_copy_string(header, hdr, sizeof(header));
  828. /* strip the display-name portion off the beginning of the header. */
  829. get_calleridname(header, tmp_name, sizeof(tmp_name));
  830. /* get uri within < > brackets */
  831. tmp_number = get_in_brackets(header);
  832. /* parse out the number here */
  833. if (parse_uri(tmp_number, "sip:,sips:", &tmp_number, &dummy, &hostport, NULL) || ast_strlen_zero(tmp_number)) {
  834. ast_log(LOG_ERROR, "can not parse name and number from sip header.\n");
  835. return -1;
  836. }
  837. /* number is not option, and must be present at this point */
  838. *number = ast_strdup(tmp_number);
  839. ast_uri_decode(*number, ast_uri_sip_user);
  840. /* name is optional and may not be present at this point */
  841. if (!ast_strlen_zero(tmp_name)) {
  842. *name = ast_strdup(tmp_name);
  843. }
  844. return 0;
  845. }
  846. #ifdef TEST_FRAMEWORK
  847. AST_TEST_DEFINE(get_name_and_number_test)
  848. {
  849. int res = AST_TEST_PASS;
  850. char *name = NULL;
  851. char *number = NULL;
  852. const char *in1 = "NAME <sip:NUMBER@place>";
  853. const char *in2 = "\"NA><ME\" <sip:NUMBER@place>";
  854. const char *in3 = "NAME";
  855. const char *in4 = "<sip:NUMBER@place>";
  856. const char *in5 = "This is a screwed up string <sip:LOLCLOWNS<sip:>@place>";
  857. switch (cmd) {
  858. case TEST_INIT:
  859. info->name = "sip_get_name_and_number_test";
  860. info->category = "/channels/chan_sip/";
  861. info->summary = "Tests getting name and number from sip header";
  862. info->description =
  863. "Runs through various test situations in which a name and "
  864. "and number can be retrieved from a sip header.";
  865. return AST_TEST_NOT_RUN;
  866. case TEST_EXECUTE:
  867. break;
  868. }
  869. /* Test 1. get name and number */
  870. number = name = NULL;
  871. if ((get_name_and_number(in1, &name, &number)) ||
  872. strcmp(name, "NAME") ||
  873. strcmp(number, "NUMBER")) {
  874. ast_test_status_update(test, "Test 1, simple get name and number failed.\n");
  875. res = AST_TEST_FAIL;
  876. }
  877. ast_free(name);
  878. ast_free(number);
  879. /* Test 2. get quoted name and number */
  880. number = name = NULL;
  881. if ((get_name_and_number(in2, &name, &number)) ||
  882. strcmp(name, "NA><ME") ||
  883. strcmp(number, "NUMBER")) {
  884. ast_test_status_update(test, "Test 2, get quoted name and number failed.\n");
  885. res = AST_TEST_FAIL;
  886. }
  887. ast_free(name);
  888. ast_free(number);
  889. /* Test 3. name only */
  890. number = name = NULL;
  891. if (!(get_name_and_number(in3, &name, &number))) {
  892. ast_test_status_update(test, "Test 3, get name only was expected to fail but did not.\n");
  893. res = AST_TEST_FAIL;
  894. }
  895. ast_free(name);
  896. ast_free(number);
  897. /* Test 4. number only */
  898. number = name = NULL;
  899. if ((get_name_and_number(in4, &name, &number)) ||
  900. !ast_strlen_zero(name) ||
  901. strcmp(number, "NUMBER")) {
  902. ast_test_status_update(test, "Test 4, get number with no name present failed.\n");
  903. res = AST_TEST_FAIL;
  904. }
  905. ast_free(name);
  906. ast_free(number);
  907. /* Test 5. malformed string, since number can not be parsed, this should return an error. */
  908. number = name = NULL;
  909. if (!(get_name_and_number(in5, &name, &number)) ||
  910. !ast_strlen_zero(name) ||
  911. !ast_strlen_zero(number)) {
  912. ast_test_status_update(test, "Test 5, processing malformed string failed.\n");
  913. res = AST_TEST_FAIL;
  914. }
  915. ast_free(name);
  916. ast_free(number);
  917. /* Test 6. NULL output parameters */
  918. number = name = NULL;
  919. if (!(get_name_and_number(in5, NULL, NULL))) {
  920. ast_test_status_update(test, "Test 6, NULL output parameters failed.\n");
  921. res = AST_TEST_FAIL;
  922. }
  923. /* Test 7. NULL input parameter */
  924. number = name = NULL;
  925. if (!(get_name_and_number(NULL, &name, &number)) ||
  926. !ast_strlen_zero(name) ||
  927. !ast_strlen_zero(number)) {
  928. ast_test_status_update(test, "Test 7, NULL input parameter failed.\n");
  929. res = AST_TEST_FAIL;
  930. }
  931. ast_free(name);
  932. ast_free(number);
  933. return res;
  934. }
  935. #endif
  936. int get_in_brackets_const(const char *src,const char **start,int *length)
  937. {
  938. const char *parse = src;
  939. const char *first_bracket;
  940. const char *second_bracket;
  941. if (start == NULL) {
  942. return -1;
  943. }
  944. if (length == NULL) {
  945. return -1;
  946. }
  947. *start = NULL;
  948. *length = -1;
  949. if (ast_strlen_zero(src)) {
  950. return 1;
  951. }
  952. /*
  953. * Skip any quoted text until we find the part in brackets.
  954. * On any error give up and return -1
  955. */
  956. while ( (first_bracket = strchr(parse, '<')) ) {
  957. const char *first_quote = strchr(parse, '"');
  958. first_bracket++;
  959. if (!first_quote || first_quote >= first_bracket) {
  960. break; /* no need to look at quoted part */
  961. }
  962. /* the bracket is within quotes, so ignore it */
  963. parse = find_closing_quote(first_quote + 1, NULL);
  964. if (!*parse) {
  965. ast_log(LOG_WARNING, "No closing quote found in '%s'\n", src);
  966. return -1;
  967. }
  968. parse++;
  969. }
  970. /* Require a first bracket. Unlike get_in_brackets_full, this procedure is passed a const,
  971. * so it can expect a pointer to an original value */
  972. if (!first_bracket) {
  973. ast_log(LOG_WARNING, "No opening bracket found in '%s'\n", src);
  974. return 1;
  975. }
  976. if ((second_bracket = strchr(first_bracket, '>'))) {
  977. *start = first_bracket;
  978. *length = second_bracket - first_bracket;
  979. return 0;
  980. }
  981. ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", src);
  982. return -1;
  983. }
  984. int get_in_brackets_full(char *tmp,char **out,char **residue)
  985. {
  986. const char *parse = tmp;
  987. char *first_bracket;
  988. char *second_bracket;
  989. if (out) {
  990. *out = "";
  991. }
  992. if (residue) {
  993. *residue = "";
  994. }
  995. if (ast_strlen_zero(tmp)) {
  996. return 1;
  997. }
  998. /*
  999. * Skip any quoted text until we find the part in brackets.
  1000. * On any error give up and return -1
  1001. */
  1002. while ( (first_bracket = strchr(parse, '<')) ) {
  1003. char *first_quote = strchr(parse, '"');
  1004. first_bracket++;
  1005. if (!first_quote || first_quote >= first_bracket) {
  1006. break; /* no need to look at quoted part */
  1007. }
  1008. /* the bracket is within quotes, so ignore it */
  1009. parse = find_closing_quote(first_quote + 1, NULL);
  1010. if (!*parse) {
  1011. ast_log(LOG_WARNING, "No closing quote found in '%s'\n", tmp);
  1012. return -1;
  1013. }
  1014. parse++;
  1015. }
  1016. /* If no first bracket then still look for a second bracket as some other parsing functions
  1017. may overwrite first bracket with NULL when terminating a token based display-name. As this
  1018. only affects token based display-names there is no danger of brackets being in quotes */
  1019. if (first_bracket) {
  1020. parse = first_bracket;
  1021. } else {
  1022. parse = tmp;
  1023. }
  1024. if ((second_bracket = strchr(parse, '>'))) {
  1025. *second_bracket++ = '\0';
  1026. if (out) {
  1027. *out = (char *) parse;
  1028. }
  1029. if (residue) {
  1030. *residue = second_bracket;
  1031. }
  1032. return 0;
  1033. }
  1034. if ((first_bracket)) {
  1035. ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
  1036. return -1;
  1037. }
  1038. if (out) {
  1039. *out = tmp;
  1040. }
  1041. return 1;
  1042. }
  1043. char *get_in_brackets(char *tmp)
  1044. {
  1045. char *out;
  1046. if ((get_in_brackets_full(tmp, &out, NULL))) {
  1047. return tmp;
  1048. }
  1049. return out;
  1050. }
  1051. #ifdef TEST_FRAMEWORK
  1052. AST_TEST_DEFINE(get_in_brackets_test)
  1053. {
  1054. int res = AST_TEST_PASS;
  1055. char in_brackets[] = "sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah";
  1056. char no_name[] = "<sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
  1057. char quoted_string[] = "\"I'm a quote stri><ng\" <sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
  1058. char missing_end_quote[] = "\"I'm a quote string <sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
  1059. char name_no_quotes[] = "name not in quotes <sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
  1060. char no_end_bracket[] = "name not in quotes <sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah";
  1061. char no_name_no_brackets[] = "sip:name@host";
  1062. char missing_start_bracket[] = "sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
  1063. char *uri = NULL;
  1064. switch (cmd) {
  1065. case TEST_INIT:
  1066. info->name = "sip_get_in_brackets_test";
  1067. info->category = "/channels/chan_sip/";
  1068. info->summary = "Tests getting a sip uri in <> brackets within a sip header.";
  1069. info->description =
  1070. "Runs through various test situations in which a sip uri "
  1071. "in angle brackets needs to be retrieved";
  1072. return AST_TEST_NOT_RUN;
  1073. case TEST_EXECUTE:
  1074. break;
  1075. }
  1076. /* Test 1, simple get in brackets */
  1077. if (!(uri = get_in_brackets(no_name)) || strcmp(uri, in_brackets)) {
  1078. ast_test_status_update(test, "Test 1, simple get in brackets failed. %s\n", uri);
  1079. res = AST_TEST_FAIL;
  1080. }
  1081. /* Test 2, starts with quoted string */
  1082. if (!(uri = get_in_brackets(quoted_string)) || strcmp(uri, in_brackets)) {
  1083. ast_test_status_update(test, "Test 2, get in brackets with quoted string in front failed. %s\n", uri);
  1084. res = AST_TEST_FAIL;
  1085. }
  1086. /* Test 3, missing end quote */
  1087. if (!(uri = get_in_brackets(missing_end_quote)) || !strcmp(uri, in_brackets)) {
  1088. ast_test_status_update(test, "Test 3, missing end quote failed. %s\n", uri);
  1089. res = AST_TEST_FAIL;
  1090. }
  1091. /* Test 4, starts with a name not in quotes */
  1092. if (!(uri = get_in_brackets(name_no_quotes)) || strcmp(uri, in_brackets)) {
  1093. ast_test_status_update(test, "Test 4, passing name not in quotes failed. %s\n", uri);
  1094. res = AST_TEST_FAIL;
  1095. }
  1096. /* Test 5, no end bracket, should just return everything after the first '<' */
  1097. if (!(uri = get_in_brackets(no_end_bracket)) || !strcmp(uri, in_brackets)) {
  1098. ast_test_status_update(test, "Test 5, no end bracket failed. %s\n", uri);
  1099. res = AST_TEST_FAIL;
  1100. }
  1101. /* Test 6, NULL input */
  1102. if (get_in_brackets(NULL)) {
  1103. ast_test_status_update(test, "Test 6, NULL input failed.\n");
  1104. res = AST_TEST_FAIL;
  1105. }
  1106. /* Test 7, no name, and no brackets. */
  1107. if (!(uri = get_in_brackets(no_name_no_brackets)) || strcmp(uri, "sip:name@host")) {
  1108. ast_test_status_update(test, "Test 7 failed. %s\n", uri);
  1109. res = AST_TEST_FAIL;
  1110. }
  1111. /* Test 8, no start bracket, but with ending bracket. */
  1112. if (!(uri = get_in_brackets(missing_start_bracket)) || strcmp(uri, in_brackets)) {
  1113. ast_test_status_update(test, "Test 8 failed. %s\n", uri);
  1114. res = AST_TEST_FAIL;
  1115. }
  1116. return res;
  1117. }
  1118. #endif
  1119. int parse_name_andor_addr(char *uri, const char *scheme, char **name,
  1120. char **user, char **pass, char **hostport,
  1121. struct uriparams *params, char **headers,
  1122. char **residue)
  1123. {
  1124. char buf[1024];
  1125. char **residue2 = residue;
  1126. char *orig_uri = uri;
  1127. int ret;
  1128. buf[0] = '\0';
  1129. if (name) {
  1130. uri = (char *) get_calleridname(uri, buf, sizeof(buf));
  1131. }
  1132. ret = get_in_brackets_full(uri, &uri, residue);
  1133. if (ret == 0) {
  1134. /*
  1135. * The uri is in brackets so do not treat unknown trailing uri
  1136. * parameters as potential message header parameters.
  1137. */
  1138. if (residue && **residue) {
  1139. /* step over the first semicolon as per parse_uri_full residue */
  1140. *residue = *residue + 1;
  1141. }
  1142. residue2 = NULL;
  1143. }
  1144. if (name) {
  1145. if (buf[0]) {
  1146. /*
  1147. * There is always room at orig_uri for the display-name because
  1148. * at least one character has always been removed. A '"' or '<'
  1149. * has been removed.
  1150. */
  1151. strcpy(orig_uri, buf);
  1152. *name = orig_uri;
  1153. } else {
  1154. *name = "";
  1155. }
  1156. }
  1157. return parse_uri_full(uri, scheme, user, pass, hostport, params, headers, residue2);
  1158. }
  1159. #ifdef TEST_FRAMEWORK
  1160. AST_TEST_DEFINE(parse_name_andor_addr_test)
  1161. {
  1162. int res = AST_TEST_PASS;
  1163. char uri[1024];
  1164. char *name, *user, *pass, *hostport, *headers, *residue;
  1165. struct uriparams params;
  1166. struct testdata {
  1167. char *desc;
  1168. char *uri;
  1169. char *name;
  1170. char *user;
  1171. char *pass;
  1172. char *hostport;
  1173. char *headers;
  1174. char *residue;
  1175. struct uriparams params;
  1176. AST_LIST_ENTRY(testdata) list;
  1177. };
  1178. struct testdata *testdataptr;
  1179. static AST_LIST_HEAD_NOLOCK(testdataliststruct, testdata) testdatalist;
  1180. struct testdata td1 = {
  1181. .desc = "quotes and brackets",
  1182. .uri = "\"name :@ \" <sip:user:secret@host:5060;param=discard;transport=tcp>;tag=tag",
  1183. .name = "name :@ ",
  1184. .user = "user",
  1185. .pass = "secret",
  1186. .hostport = "host:5060",
  1187. .headers = "",
  1188. .residue = "tag=tag",
  1189. .params.transport = "tcp",
  1190. .params.lr = 0,
  1191. .params.user = ""
  1192. };
  1193. struct testdata td2 = {
  1194. .desc = "no quotes",
  1195. .uri = "givenname familyname <sip:user:secret@host:5060;param=discard;transport=tcp>;expires=3600",
  1196. .name = "givenname familyname",
  1197. .user = "user",
  1198. .pass = "secret",
  1199. .hostport = "host:5060",
  1200. .headers = "",
  1201. .residue = "expires=3600",
  1202. .params.transport = "tcp",
  1203. .params.lr = 0,
  1204. .params.user = ""
  1205. };
  1206. struct testdata td3 = {
  1207. .desc = "no brackets",
  1208. .uri = "sip:user:secret@host:5060;param=discard;transport=tcp;q=1",
  1209. .name = "",
  1210. .user = "user",
  1211. .pass = "secret",
  1212. .hostport = "host:5060",
  1213. .headers = "",
  1214. .residue = "q=1",
  1215. .params.transport = "tcp",
  1216. .params.lr = 0,
  1217. .params.user = ""
  1218. };
  1219. struct testdata td4 = {
  1220. .desc = "just host",
  1221. .uri = "sips:host",
  1222. .name = "",
  1223. .user = "",
  1224. .pass = "",
  1225. .hostport = "host",
  1226. .headers = "",
  1227. .residue = "",
  1228. .params.transport = "",
  1229. .params.lr = 0,
  1230. .params.user = ""
  1231. };
  1232. AST_LIST_HEAD_SET_NOLOCK(&testdatalist, &td1);
  1233. AST_LIST_INSERT_TAIL(&testdatalist, &td2, list);
  1234. AST_LIST_INSERT_TAIL(&testdatalist, &td3, list);
  1235. AST_LIST_INSERT_TAIL(&testdatalist, &td4, list);
  1236. switch (cmd) {
  1237. case TEST_INIT:
  1238. info->name = "parse_name_andor_addr_test";
  1239. info->category = "/channels/chan_sip/";
  1240. info->summary = "tests parsing of name_andor_addr abnf structure";
  1241. info->description =
  1242. "Tests parsing of abnf name-andor-addr = name-addr / addr-spec "
  1243. "Verifies output matches expected behavior.";
  1244. return AST_TEST_NOT_RUN;
  1245. case TEST_EXECUTE:
  1246. break;
  1247. }
  1248. AST_LIST_TRAVERSE(&testdatalist, testdataptr, list) {
  1249. name = user = pass = hostport = headers = residue = NULL;
  1250. params.transport = params.user = params.method = params.ttl = params.maddr = NULL;
  1251. params.lr = 0;
  1252. ast_copy_string(uri,testdataptr->uri,sizeof(uri));
  1253. if (parse_name_andor_addr(uri, "sip:,sips:",
  1254. &name,
  1255. &user,
  1256. &pass,
  1257. &hostport,
  1258. &params,
  1259. &headers,
  1260. &residue) ||
  1261. (name && strcmp(testdataptr->name, name)) ||
  1262. (user && strcmp(testdataptr->user, user)) ||
  1263. (pass && strcmp(testdataptr->pass, pass)) ||
  1264. (hostport && strcmp(testdataptr->hostport, hostport)) ||
  1265. (headers && strcmp(testdataptr->headers, headers)) ||
  1266. (residue && strcmp(testdataptr->residue, residue)) ||
  1267. (strcmp(testdataptr->params.transport,params.transport)) ||
  1268. (strcmp(testdataptr->params.user,params.user))
  1269. ) {
  1270. ast_test_status_update(test, "Sub-Test: %s,failed.\n", testdataptr->desc);
  1271. res = AST_TEST_FAIL;
  1272. }
  1273. }
  1274. return res;
  1275. }
  1276. #endif
  1277. int get_comma(char *in, char **out)
  1278. {
  1279. char *c;
  1280. char *parse = in;
  1281. if (out) {
  1282. *out = in;
  1283. }
  1284. /* Skip any quoted text */
  1285. while (*parse) {
  1286. if ((c = strchr(parse, '"'))) {
  1287. in = (char *)find_closing_quote((const char *)c + 1, NULL);
  1288. if (!*in) {
  1289. ast_log(LOG_WARNING, "No closing quote found in '%s'\n", c);
  1290. return -1;
  1291. } else {
  1292. break;
  1293. }
  1294. } else {
  1295. break;
  1296. }
  1297. parse++;
  1298. }
  1299. parse = in;
  1300. /* Skip any userinfo components of a uri as they may contain commas */
  1301. if ((c = strchr(parse,'@'))) {
  1302. parse = c+1;
  1303. }
  1304. if ((out) && (c = strchr(parse,','))) {
  1305. *c++ = '\0';
  1306. *out = c;
  1307. return 0;
  1308. }
  1309. return 1;
  1310. }
  1311. int parse_contact_header(char *contactheader, struct contactliststruct *contactlist)
  1312. {
  1313. int res;
  1314. int last;
  1315. char *comma;
  1316. char *residue;
  1317. char *param;
  1318. char *value;
  1319. struct contact *split_contact = NULL;
  1320. if (*contactheader == '*') {
  1321. return 1;
  1322. }
  1323. split_contact = ast_calloc(1, sizeof(*split_contact));
  1324. AST_LIST_HEAD_SET_NOLOCK(contactlist, split_contact);
  1325. while ((last = get_comma(contactheader, &comma)) != -1) {
  1326. res = parse_name_andor_addr(contactheader, "sip:,sips:",
  1327. &split_contact->name, &split_contact->user,
  1328. &split_contact->pass, &split_contact->hostport,
  1329. &split_contact->params, &split_contact->headers,
  1330. &residue);
  1331. if (res == -1) {
  1332. return res;
  1333. }
  1334. /* parse contact params */
  1335. split_contact->expires = split_contact->q = "";
  1336. while ((value = strchr(residue,'='))) {
  1337. *value++ = '\0';
  1338. param = residue;
  1339. if ((residue = strchr(value,';'))) {
  1340. *residue++ = '\0';
  1341. } else {
  1342. residue = "";
  1343. }
  1344. if (!strcmp(param,"expires")) {
  1345. split_contact->expires = value;
  1346. } else if (!strcmp(param,"q")) {
  1347. split_contact->q = value;
  1348. }
  1349. }
  1350. if (last) {
  1351. return 0;
  1352. }
  1353. contactheader = comma;
  1354. split_contact = ast_calloc(1, sizeof(*split_contact));
  1355. AST_LIST_INSERT_TAIL(contactlist, split_contact, list);
  1356. }
  1357. return last;
  1358. }
  1359. #ifdef TEST_FRAMEWORK
  1360. AST_TEST_DEFINE(parse_contact_header_test)
  1361. {
  1362. int res = AST_TEST_PASS;
  1363. char contactheader[1024];
  1364. int star;
  1365. struct contactliststruct contactlist;
  1366. struct contactliststruct *contactlistptr=&contactlist;
  1367. struct testdata {
  1368. char *desc;
  1369. char *contactheader;
  1370. int star;
  1371. struct contactliststruct *contactlist;
  1372. AST_LIST_ENTRY(testdata) list;
  1373. };
  1374. struct testdata *testdataptr;
  1375. struct contact *tdcontactptr;
  1376. struct contact *contactptr;
  1377. static AST_LIST_HEAD_NOLOCK(testdataliststruct, testdata) testdatalist;
  1378. struct contactliststruct contactlist1, contactlist2;
  1379. struct testdata td1 = {
  1380. .desc = "single contact",
  1381. .contactheader = "\"name :@;?&,\" <sip:user:secret@host:5082;param=discard;transport=tcp>;expires=3600",
  1382. .contactlist = &contactlist1,
  1383. .star = 0
  1384. };
  1385. struct contact contact11 = {
  1386. .name = "name :@;?&,",
  1387. .user = "user",
  1388. .pass = "secret",
  1389. .hostport = "host:5082",
  1390. .params.transport = "tcp",
  1391. .params.ttl = "",
  1392. .params.lr = 0,
  1393. .headers = "",
  1394. .expires = "3600",
  1395. .q = ""
  1396. };
  1397. struct testdata td2 = {
  1398. .desc = "multiple contacts",
  1399. .contactheader = "sip:,user1,:,secret1,@host1;ttl=7;q=1;expires=3600,sips:host2",
  1400. .contactlist = &contactlist2,
  1401. .star = 0,
  1402. };
  1403. struct contact contact21 = {
  1404. .name = "",
  1405. .user = ",user1,",
  1406. .pass = ",secret1,",
  1407. .hostport = "host1",
  1408. .params.transport = "",
  1409. .params.ttl = "7",
  1410. .params.lr = 0,
  1411. .headers = "",
  1412. .expires = "3600",
  1413. .q = "1"
  1414. };
  1415. struct contact contact22 = {
  1416. .name = "",
  1417. .user = "",
  1418. .pass = "",
  1419. .hostport = "host2",
  1420. .params.transport = "",
  1421. .params.ttl = "",
  1422. .params.lr = 0,
  1423. .headers = "",
  1424. .expires = "",
  1425. .q = ""
  1426. };
  1427. struct testdata td3 = {
  1428. .desc = "star - all contacts",
  1429. .contactheader = "*",
  1430. .star = 1,
  1431. .contactlist = NULL
  1432. };
  1433. AST_LIST_HEAD_SET_NOLOCK(&testdatalist, &td1);
  1434. AST_LIST_INSERT_TAIL(&testdatalist, &td2, list);
  1435. AST_LIST_INSERT_TAIL(&testdatalist, &td3, list);
  1436. AST_LIST_HEAD_SET_NOLOCK(&contactlist1, &contact11);
  1437. AST_LIST_HEAD_SET_NOLOCK(&contactlist2, &contact21);
  1438. AST_LIST_INSERT_TAIL(&contactlist2, &contact22, list);
  1439. switch (cmd) {
  1440. case TEST_INIT:
  1441. info->name = "parse_contact_header_test";
  1442. info->category = "/channels/chan_sip/";
  1443. info->summary = "tests parsing of sip contact header";
  1444. info->description =
  1445. "Tests parsing of a contact header including those with multiple contacts "
  1446. "Verifies output matches expected behavior.";
  1447. return AST_TEST_NOT_RUN;
  1448. case TEST_EXECUTE:
  1449. break;
  1450. }
  1451. AST_LIST_TRAVERSE(&testdatalist, testdataptr, list) {
  1452. ast_copy_string(contactheader,testdataptr->contactheader,sizeof(contactheader));
  1453. star = parse_contact_header(contactheader,contactlistptr);
  1454. if (testdataptr->star) {
  1455. /* expecting star rather than list of contacts */
  1456. if (!star) {
  1457. ast_test_status_update(test, "Sub-Test: %s,failed.\n", testdataptr->desc);
  1458. res = AST_TEST_FAIL;
  1459. break;
  1460. }
  1461. } else {
  1462. contactptr = AST_LIST_FIRST(contactlistptr);
  1463. AST_LIST_TRAVERSE(testdataptr->contactlist, tdcontactptr, list) {
  1464. if (!contactptr ||
  1465. strcmp(tdcontactptr->name, contactptr->name) ||
  1466. strcmp(tdcontactptr->user, contactptr->user) ||
  1467. strcmp(tdcontactptr->pass, contactptr->pass) ||
  1468. strcmp(tdcontactptr->hostport, contactptr->hostport) ||
  1469. strcmp(tdcontactptr->headers, contactptr->headers) ||
  1470. strcmp(tdcontactptr->expires, contactptr->expires) ||
  1471. strcmp(tdcontactptr->q, contactptr->q) ||
  1472. strcmp(tdcontactptr->params.transport, contactptr->params.transport) ||
  1473. strcmp(tdcontactptr->params.ttl, contactptr->params.ttl) ||
  1474. (tdcontactptr->params.lr != contactptr->params.lr)
  1475. ) {
  1476. ast_test_status_update(test, "Sub-Test: %s,failed.\n", testdataptr->desc);
  1477. res = AST_TEST_FAIL;
  1478. break;
  1479. }
  1480. contactptr = AST_LIST_NEXT(contactptr,list);
  1481. }
  1482. while ((contactptr = AST_LIST_REMOVE_HEAD(contactlistptr,list))) {
  1483. ast_free(contactptr);
  1484. }
  1485. }
  1486. }
  1487. return res;
  1488. }
  1489. #endif
  1490. /*!
  1491. * \brief Parse supported header in incoming packet
  1492. *
  1493. * \details This function parses through the options parameters and
  1494. * builds a bit field representing all the SIP options in that field. When an
  1495. * item is found that is not supported, it is copied to the unsupported
  1496. * out buffer.
  1497. *
  1498. * \param options list
  1499. * \param unsupported out buffer (optional)
  1500. * \param unsupported_len out buffer length (optional)
  1501. */
  1502. static unsigned int _parse_sip_options(const char *options, char *unsupported, size_t unsupported_len)
  1503. {
  1504. char *next, *sep;
  1505. char *temp;
  1506. int i, found, supported;
  1507. unsigned int profile = 0;
  1508. char *out = unsupported;
  1509. size_t outlen = unsupported_len;
  1510. char *cur_out = out;
  1511. if (ast_strlen_zero(options) )
  1512. return 0;
  1513. temp = ast_strdupa(options);
  1514. ast_debug(3, "Begin: parsing SIP \"Required:\" or \"Supported: %s\"\n", options);
  1515. for (next = temp; next; next = sep) {
  1516. found = FALSE;
  1517. supported = FALSE;
  1518. if ((sep = strchr(next, ',')) != NULL) {
  1519. *sep++ = '\0';
  1520. }
  1521. /* trim leading and trailing whitespace */
  1522. next = ast_strip(next);
  1523. if (ast_strlen_zero(next)) {
  1524. continue; /* if there is a blank argument in there just skip it */
  1525. }
  1526. ast_debug(3, "Found SIP option: -%s-\n", next);
  1527. for (i = 0; i < ARRAY_LEN(sip_options); i++) {
  1528. if (!strcasecmp(next, sip_options[i].text)) {
  1529. profile |= sip_options[i].id;
  1530. if (sip_options[i].supported == SUPPORTED) {
  1531. supported = TRUE;
  1532. }
  1533. found = TRUE;
  1534. ast_debug(3, "Matched SIP option: %s\n", next);
  1535. break;
  1536. }
  1537. }
  1538. /* If option is not supported, add to unsupported out buffer */
  1539. if (!supported && out && outlen) {
  1540. size_t copylen = strlen(next);
  1541. size_t cur_outlen = strlen(out);
  1542. /* Check to see if there is enough room to store this option.
  1543. * Copy length is string length plus 2 for the ',' and '\0' */
  1544. if ((cur_outlen + copylen + 2) < outlen) {
  1545. /* if this isn't the first item, add the ',' */
  1546. if (cur_outlen) {
  1547. *cur_out = ',';
  1548. cur_out++;
  1549. cur_outlen++;
  1550. }
  1551. ast_copy_string(cur_out, next, (outlen - cur_outlen));
  1552. cur_out += copylen;
  1553. }
  1554. }
  1555. if (!found) {
  1556. profile |= SIP_OPT_UNKNOWN;
  1557. if (!strncasecmp(next, "x-", 2))
  1558. ast_debug(3, "Found private SIP option, not supported: %s\n", next);
  1559. else
  1560. ast_debug(3, "Found no match for SIP option: %s (Please file bug report!)\n", next);
  1561. }
  1562. }
  1563. return profile;
  1564. }
  1565. #ifdef TEST_FRAMEWORK
  1566. AST_TEST_DEFINE(sip_parse_options_test)
  1567. {
  1568. int res = AST_TEST_PASS;
  1569. char unsupported[64];
  1570. unsigned int option_profile = 0;
  1571. struct testdata {
  1572. char *name;
  1573. char *input_options;
  1574. char *expected_unsupported;
  1575. unsigned int expected_profile;
  1576. AST_LIST_ENTRY(testdata) list;
  1577. };
  1578. struct testdata *testdataptr;
  1579. static AST_LIST_HEAD_NOLOCK(testdataliststruct, testdata) testdatalist;
  1580. struct testdata test1 = {
  1581. .name = "test_all_unsupported",
  1582. .input_options = "unsupported1,,, ,unsupported2,unsupported3,unsupported4",
  1583. .expected_unsupported = "unsupported1,unsupported2,unsupported3,unsupported4",
  1584. .expected_profile = SIP_OPT_UNKNOWN,
  1585. };
  1586. struct testdata test2 = {
  1587. .name = "test_all_unsupported_one_supported",
  1588. .input_options = " unsupported1, replaces, unsupported3 , , , ,unsupported4",
  1589. .expected_unsupported = "unsupported1,unsupported3,unsupported4",
  1590. .expected_profile = SIP_OPT_UNKNOWN | SIP_OPT_REPLACES
  1591. };
  1592. struct testdata test3 = {
  1593. .name = "test_two_supported_two_unsupported",
  1594. .input_options = ",, timer ,replaces ,unsupported3,unsupported4",
  1595. .expected_unsupported = "unsupported3,unsupported4",
  1596. .expected_profile = SIP_OPT_UNKNOWN | SIP_OPT_REPLACES | SIP_OPT_TIMER,
  1597. };
  1598. struct testdata test4 = {
  1599. .name = "test_all_supported",
  1600. .input_options = "timer,replaces",
  1601. .expected_unsupported = "",
  1602. .expected_profile = SIP_OPT_REPLACES | SIP_OPT_TIMER,
  1603. };
  1604. struct testdata test5 = {
  1605. .name = "test_all_supported_redundant",
  1606. .input_options = "timer,replaces,timer,replace,timer,replaces",
  1607. .expected_unsupported = "",
  1608. .expected_profile = SIP_OPT_REPLACES | SIP_OPT_TIMER,
  1609. };
  1610. struct testdata test6 = {
  1611. .name = "test_buffer_overflow",
  1612. .input_options = "unsupported1,replaces,timer,unsupported4,unsupported_huge____"
  1613. "____________________________________,__________________________________________"
  1614. "________________________________________________",
  1615. .expected_unsupported = "unsupported1,unsupported4",
  1616. .expected_profile = SIP_OPT_UNKNOWN | SIP_OPT_REPLACES | SIP_OPT_TIMER,
  1617. };
  1618. struct testdata test7 = {
  1619. .name = "test_null_input",
  1620. .input_options = NULL,
  1621. .expected_unsupported = "",
  1622. .expected_profile = 0,
  1623. };
  1624. struct testdata test8 = {
  1625. .name = "test_whitespace_input",
  1626. .input_options = " ",
  1627. .expected_unsupported = "",
  1628. .expected_profile = 0,
  1629. };
  1630. struct testdata test9 = {
  1631. .name = "test_whitespace_plus_option_input",
  1632. .input_options = " , , ,timer , , , , , ",
  1633. .expected_unsupported = "",
  1634. .expected_profile = SIP_OPT_TIMER,
  1635. };
  1636. switch (cmd) {
  1637. case TEST_INIT:
  1638. info->name = "sip_parse_options_test";
  1639. info->category = "/channels/chan_sip/";
  1640. info->summary = "Tests parsing of sip options";
  1641. info->description =
  1642. "Tests parsing of SIP options from supported and required "
  1643. "header fields. Verifies when unsupported options are encountered "
  1644. "that they are appended to the unsupported out buffer and that the "
  1645. "correct bit field representnig the option profile is returned.";
  1646. return AST_TEST_NOT_RUN;
  1647. case TEST_EXECUTE:
  1648. break;
  1649. }
  1650. AST_LIST_HEAD_SET_NOLOCK(&testdatalist, &test1);
  1651. AST_LIST_INSERT_TAIL(&testdatalist, &test2, list);
  1652. AST_LIST_INSERT_TAIL(&testdatalist, &test3, list);
  1653. AST_LIST_INSERT_TAIL(&testdatalist, &test4, list);
  1654. AST_LIST_INSERT_TAIL(&testdatalist, &test5, list);
  1655. AST_LIST_INSERT_TAIL(&testdatalist, &test6, list);
  1656. AST_LIST_INSERT_TAIL(&testdatalist, &test7, list);
  1657. AST_LIST_INSERT_TAIL(&testdatalist, &test8, list);
  1658. AST_LIST_INSERT_TAIL(&testdatalist, &test9, list);
  1659. /* Test with unsupported char buffer */
  1660. AST_LIST_TRAVERSE(&testdatalist, testdataptr, list) {
  1661. memset(unsupported, 0, sizeof(unsupported));
  1662. option_profile = parse_sip_options(testdataptr->input_options, unsupported, ARRAY_LEN(unsupported));
  1663. if (option_profile != testdataptr->expected_profile ||
  1664. strcmp(unsupported, testdataptr->expected_unsupported)) {
  1665. ast_test_status_update(test, "Test with output buffer \"%s\", expected unsupported: %s actual unsupported:"
  1666. "%s expected bit profile: %x actual bit profile: %x\n",
  1667. testdataptr->name,
  1668. testdataptr->expected_unsupported,
  1669. unsupported,
  1670. testdataptr->expected_profile,
  1671. option_profile);
  1672. res = AST_TEST_FAIL;
  1673. } else {
  1674. ast_test_status_update(test, "\"%s\" passed got expected unsupported: %s and bit profile: %x\n",
  1675. testdataptr->name,
  1676. unsupported,
  1677. option_profile);
  1678. }
  1679. option_profile = parse_sip_options(testdataptr->input_options, NULL, 0);
  1680. if (option_profile != testdataptr->expected_profile) {
  1681. ast_test_status_update(test, "NULL output test \"%s\", expected bit profile: %x actual bit profile: %x\n",
  1682. testdataptr->name,
  1683. testdataptr->expected_profile,
  1684. option_profile);
  1685. res = AST_TEST_FAIL;
  1686. } else {
  1687. ast_test_status_update(test, "\"%s\" with NULL output buf passed, bit profile: %x\n",
  1688. testdataptr->name,
  1689. option_profile);
  1690. }
  1691. }
  1692. return res;
  1693. }
  1694. #endif
  1695. /*!
  1696. * \brief Parse supported header in incoming packet
  1697. *
  1698. * \details This function parses through the options parameters and
  1699. * builds a bit field representing all the SIP options in that field. When an
  1700. * item is found that is not supported, it is copied to the unsupported
  1701. * out buffer.
  1702. *
  1703. * \param option list
  1704. * \param unsupported out buffer (optional)
  1705. * \param unsupported out buffer length (optional)
  1706. */
  1707. unsigned int parse_sip_options(const char *options, char *unsupported, size_t unsupported_len)
  1708. {
  1709. return _parse_sip_options(options, unsupported, unsupported_len);
  1710. }
  1711. /*!
  1712. * \brief required header in incoming packet or response
  1713. * returns bitmap
  1714. *
  1715. * \param option list
  1716. */
  1717. unsigned int parse_required_sip_options(const char *options)
  1718. {
  1719. return _parse_sip_options(options, NULL, 0 );
  1720. }
  1721. /*! \brief helper routine for sip_uri_cmp to compare URI parameters
  1722. *
  1723. * This takes the parameters from two SIP URIs and determines
  1724. * if the URIs match. The rules for parameters *suck*. Here's a breakdown
  1725. * 1. If a parameter appears in both URIs, then they must have the same value
  1726. * in order for the URIs to match
  1727. * 2. If one URI has a user, maddr, ttl, or method parameter, then the other
  1728. * URI must also have that parameter and must have the same value
  1729. * in order for the URIs to match
  1730. * 3. All other headers appearing in only one URI are not considered when
  1731. * determining if URIs match
  1732. *
  1733. * \param input1 Parameters from URI 1
  1734. * \param input2 Parameters from URI 2
  1735. * \retval 0 URIs' parameters match
  1736. * \retval nonzero URIs' parameters do not match
  1737. */
  1738. static int sip_uri_params_cmp(const char *input1, const char *input2)
  1739. {
  1740. char *params1 = NULL;
  1741. char *params2 = NULL;
  1742. char *pos1;
  1743. char *pos2;
  1744. int zerolength1 = 0;
  1745. int zerolength2 = 0;
  1746. int maddrmatch = 0;
  1747. int ttlmatch = 0;
  1748. int usermatch = 0;
  1749. int methodmatch = 0;
  1750. if (ast_strlen_zero(input1)) {
  1751. zerolength1 = 1;
  1752. } else {
  1753. params1 = ast_strdupa(input1);
  1754. }
  1755. if (ast_strlen_zero(input2)) {
  1756. zerolength2 = 1;
  1757. } else {
  1758. params2 = ast_strdupa(input2);
  1759. }
  1760. /* Quick optimization. If both params are zero-length, then
  1761. * they match
  1762. */
  1763. if (zerolength1 && zerolength2) {
  1764. return 0;
  1765. }
  1766. for (pos1 = strsep(&params1, ";"); pos1; pos1 = strsep(&params1, ";")) {
  1767. char *value1 = pos1;
  1768. char *name1 = strsep(&value1, "=");
  1769. char *params2dup = NULL;
  1770. int matched = 0;
  1771. if (!value1) {
  1772. value1 = "";
  1773. }
  1774. /* Checkpoint reached. We have the name and value parsed for param1
  1775. * We have to duplicate params2 each time through this loop
  1776. * or else the inner loop below will not work properly.
  1777. */
  1778. if (!zerolength2) {
  1779. params2dup = ast_strdupa(params2);
  1780. }
  1781. for (pos2 = strsep(&params2dup, ";"); pos2; pos2 = strsep(&params2dup, ";")) {
  1782. char *name2 = pos2;
  1783. char *value2 = strchr(pos2, '=');
  1784. if (!value2) {
  1785. value2 = "";
  1786. } else {
  1787. *value2++ = '\0';
  1788. }
  1789. if (!strcasecmp(name1, name2)) {
  1790. if (strcasecmp(value1, value2)) {
  1791. goto fail;
  1792. } else {
  1793. matched = 1;
  1794. break;
  1795. }
  1796. }
  1797. }
  1798. /* Check to see if the parameter is one of the 'must-match' parameters */
  1799. if (!strcasecmp(name1, "maddr")) {
  1800. if (matched) {
  1801. maddrmatch = 1;
  1802. } else {
  1803. goto fail;
  1804. }
  1805. } else if (!strcasecmp(name1, "ttl")) {
  1806. if (matched) {
  1807. ttlmatch = 1;
  1808. } else {
  1809. goto fail;
  1810. }
  1811. } else if (!strcasecmp(name1, "user")) {
  1812. if (matched) {
  1813. usermatch = 1;
  1814. } else {
  1815. goto fail;
  1816. }
  1817. } else if (!strcasecmp(name1, "method")) {
  1818. if (matched) {
  1819. methodmatch = 1;
  1820. } else {
  1821. goto fail;
  1822. }
  1823. }
  1824. }
  1825. /* We've made it out of that horrible O(m*n) construct and there are no
  1826. * failures yet. We're not done yet, though, because params2 could have
  1827. * an maddr, ttl, user, or method header and params1 did not.
  1828. */
  1829. for (pos2 = strsep(&params2, ";"); pos2; pos2 = strsep(&params2, ";")) {
  1830. char *value2 = pos2;
  1831. char *name2 = strsep(&value2, "=");
  1832. if (!value2) {
  1833. value2 = "";
  1834. }
  1835. if ((!strcasecmp(name2, "maddr") && !maddrmatch) ||
  1836. (!strcasecmp(name2, "ttl") && !ttlmatch) ||
  1837. (!strcasecmp(name2, "user") && !usermatch) ||
  1838. (!strcasecmp(name2, "method") && !methodmatch)) {
  1839. goto fail;
  1840. }
  1841. }
  1842. return 0;
  1843. fail:
  1844. return 1;
  1845. }
  1846. /*! \brief helper routine for sip_uri_cmp to compare URI headers
  1847. *
  1848. * This takes the headers from two SIP URIs and determines
  1849. * if the URIs match. The rules for headers is simple. If a header
  1850. * appears in one URI, then it must also appear in the other URI. The
  1851. * order in which the headers appear does not matter.
  1852. *
  1853. * \param input1 Headers from URI 1
  1854. * \param input2 Headers from URI 2
  1855. * \retval 0 URI headers match
  1856. * \retval nonzero URI headers do not match
  1857. */
  1858. static int sip_uri_headers_cmp(const char *input1, const char *input2)
  1859. {
  1860. char *headers1 = NULL;
  1861. char *headers2 = NULL;
  1862. int zerolength1 = 0;
  1863. int zerolength2 = 0;
  1864. int different = 0;
  1865. char *header1;
  1866. if (ast_strlen_zero(input1)) {
  1867. zerolength1 = 1;
  1868. } else {
  1869. headers1 = ast_strdupa(input1);
  1870. }
  1871. if (ast_strlen_zero(input2)) {
  1872. zerolength2 = 1;
  1873. } else {
  1874. headers2 = ast_strdupa(input2);
  1875. }
  1876. /* If one URI contains no headers and the other
  1877. * does, then they cannot possibly match
  1878. */
  1879. if (zerolength1 != zerolength2) {
  1880. return 1;
  1881. }
  1882. if (zerolength1 && zerolength2)
  1883. return 0;
  1884. /* At this point, we can definitively state that both inputs are
  1885. * not zero-length. First, one more optimization. If the length
  1886. * of the headers is not equal, then we definitely have no match
  1887. */
  1888. if (strlen(headers1) != strlen(headers2)) {
  1889. return 1;
  1890. }
  1891. for (header1 = strsep(&headers1, "&"); header1; header1 = strsep(&headers1, "&")) {
  1892. if (!strcasestr(headers2, header1)) {
  1893. different = 1;
  1894. break;
  1895. }
  1896. }
  1897. return different;
  1898. }
  1899. /*!
  1900. * \brief Compare domain sections of SIP URIs
  1901. *
  1902. * For hostnames, a case insensitive string comparison is
  1903. * used. For IP addresses, a binary comparison is used. This
  1904. * is mainly because IPv6 addresses have many ways of writing
  1905. * the same address.
  1906. *
  1907. * For specifics about IP address comparison, see the following
  1908. * document: http://tools.ietf.org/html/draft-ietf-sip-ipv6-abnf-fix-05
  1909. *
  1910. * \param host1 The domain from the first URI
  1911. * \param host2 THe domain from the second URI
  1912. * \retval 0 The domains match
  1913. * \retval nonzero The domains do not match
  1914. */
  1915. static int sip_uri_domain_cmp(const char *host1, const char *host2)
  1916. {
  1917. struct ast_sockaddr addr1;
  1918. struct ast_sockaddr addr2;
  1919. int addr1_parsed;
  1920. int addr2_parsed;
  1921. addr1_parsed = ast_sockaddr_parse(&addr1, host1, 0);
  1922. addr2_parsed = ast_sockaddr_parse(&addr2, host2, 0);
  1923. if (addr1_parsed != addr2_parsed) {
  1924. /* One domain was an IP address and the other had
  1925. * a host name. FAIL!
  1926. */
  1927. return 1;
  1928. }
  1929. /* Both are host names. A string comparison will work
  1930. * perfectly here. Specifying the "C" locale ensures that
  1931. * The LC_CTYPE conventions use those defined in ANSI C,
  1932. * i.e. ASCII.
  1933. */
  1934. if (!addr1_parsed) {
  1935. #ifdef HAVE_XLOCALE_H
  1936. if(!c_locale) {
  1937. return strcasecmp(host1, host2);
  1938. } else {
  1939. return strcasecmp_l(host1, host2, c_locale);
  1940. }
  1941. #else
  1942. return strcasecmp(host1, host2);
  1943. #endif
  1944. }
  1945. /* Both contain IP addresses */
  1946. return ast_sockaddr_cmp(&addr1, &addr2);
  1947. }
  1948. int sip_uri_cmp(const char *input1, const char *input2)
  1949. {
  1950. char *uri1;
  1951. char *uri2;
  1952. char *uri_scheme1;
  1953. char *uri_scheme2;
  1954. char *host1;
  1955. char *host2;
  1956. char *params1;
  1957. char *params2;
  1958. char *headers1;
  1959. char *headers2;
  1960. /* XXX It would be really nice if we could just use parse_uri_full() here
  1961. * to separate the components of the URI, but unfortunately it is written
  1962. * in a way that can cause URI parameters to be discarded.
  1963. */
  1964. if (!input1 || !input2) {
  1965. return 1;
  1966. }
  1967. uri1 = ast_strdupa(input1);
  1968. uri2 = ast_strdupa(input2);
  1969. ast_uri_decode(uri1, ast_uri_sip_user);
  1970. ast_uri_decode(uri2, ast_uri_sip_user);
  1971. uri_scheme1 = strsep(&uri1, ":");
  1972. uri_scheme2 = strsep(&uri2, ":");
  1973. if (strcmp(uri_scheme1, uri_scheme2)) {
  1974. return 1;
  1975. }
  1976. /* This function is tailored for SIP and SIPS URIs. There's no
  1977. * need to check uri_scheme2 since we have determined uri_scheme1
  1978. * and uri_scheme2 are equivalent already.
  1979. */
  1980. if (strcmp(uri_scheme1, "sip") && strcmp(uri_scheme1, "sips")) {
  1981. return 1;
  1982. }
  1983. if (ast_strlen_zero(uri1) || ast_strlen_zero(uri2)) {
  1984. return 1;
  1985. }
  1986. if ((host1 = strchr(uri1, '@'))) {
  1987. *host1++ = '\0';
  1988. }
  1989. if ((host2 = strchr(uri2, '@'))) {
  1990. *host2++ = '\0';
  1991. }
  1992. /* Check for mismatched username and passwords. This is the
  1993. * only case-sensitive comparison of a SIP URI
  1994. */
  1995. if ((host1 && !host2) ||
  1996. (host2 && !host1) ||
  1997. (host1 && host2 && strcmp(uri1, uri2))) {
  1998. return 1;
  1999. }
  2000. if (!host1) {
  2001. host1 = uri1;
  2002. }
  2003. if (!host2) {
  2004. host2 = uri2;
  2005. }
  2006. /* Strip off the parameters and headers so we can compare
  2007. * host and port
  2008. */
  2009. if ((params1 = strchr(host1, ';'))) {
  2010. *params1++ = '\0';
  2011. }
  2012. if ((params2 = strchr(host2, ';'))) {
  2013. *params2++ = '\0';
  2014. }
  2015. /* Headers come after parameters, but there may be headers without
  2016. * parameters, thus the S_OR
  2017. */
  2018. if ((headers1 = strchr(S_OR(params1, host1), '?'))) {
  2019. *headers1++ = '\0';
  2020. }
  2021. if ((headers2 = strchr(S_OR(params2, host2), '?'))) {
  2022. *headers2++ = '\0';
  2023. }
  2024. if (sip_uri_domain_cmp(host1, host2)) {
  2025. return 1;
  2026. }
  2027. /* Headers have easier rules to follow, so do those first */
  2028. if (sip_uri_headers_cmp(headers1, headers2)) {
  2029. return 1;
  2030. }
  2031. /* And now the parameters. Ugh */
  2032. return sip_uri_params_cmp(params1, params2);
  2033. }
  2034. #define URI_CMP_MATCH 0
  2035. #define URI_CMP_NOMATCH 1
  2036. #ifdef TEST_FRAMEWORK
  2037. AST_TEST_DEFINE(sip_uri_cmp_test)
  2038. {
  2039. static const struct {
  2040. const char *uri1;
  2041. const char *uri2;
  2042. int expected_result;
  2043. } uri_cmp_tests [] = {
  2044. /* These are identical, so they match */
  2045. { "sip:bob@example.com", "sip:bob@example.com", URI_CMP_MATCH },
  2046. /* Different usernames. No match */
  2047. { "sip:alice@example.com", "sip:bob@example.com", URI_CMP_NOMATCH },
  2048. /* Different hosts. No match */
  2049. { "sip:bob@example.com", "sip:bob@examplez.com", URI_CMP_NOMATCH },
  2050. /* Now start using IP addresses. Identical, so they match */
  2051. { "sip:bob@1.2.3.4", "sip:bob@1.2.3.4", URI_CMP_MATCH },
  2052. /* Two identical IPv4 addresses represented differently. Match */
  2053. { "sip:bob@1.2.3.4", "sip:bob@001.002.003.004", URI_CMP_MATCH },
  2054. /* Logically equivalent IPv4 Address and hostname. No Match */
  2055. { "sip:bob@127.0.0.1", "sip:bob@localhost", URI_CMP_NOMATCH },
  2056. /* Logically equivalent IPv6 address and hostname. No Match */
  2057. { "sip:bob@[::1]", "sip:bob@localhost", URI_CMP_NOMATCH },
  2058. /* Try an IPv6 one as well */
  2059. { "sip:bob@[2001:db8::1234]", "sip:bob@[2001:db8::1234]", URI_CMP_MATCH },
  2060. /* Two identical IPv6 addresses represented differently. Match */
  2061. { "sip:bob@[2001:db8::1234]", "sip:bob@[2001:0db8::1234]", URI_CMP_MATCH },
  2062. /* Different ports. No match */
  2063. { "sip:bob@1.2.3.4:5060", "sip:bob@1.2.3.4:5061", URI_CMP_NOMATCH },
  2064. /* Same port logically, but only one address specifies it. No match */
  2065. { "sip:bob@1.2.3.4:5060", "sip:bob@1.2.3.4", URI_CMP_NOMATCH },
  2066. /* And for safety, try with IPv6 */
  2067. { "sip:bob@[2001:db8:1234]:5060", "sip:bob@[2001:db8:1234]", URI_CMP_NOMATCH },
  2068. /* User comparison is case sensitive. No match */
  2069. { "sip:bob@example.com", "sip:BOB@example.com", URI_CMP_NOMATCH },
  2070. /* Host comparison is case insensitive. Match */
  2071. { "sip:bob@example.com", "sip:bob@EXAMPLE.COM", URI_CMP_MATCH },
  2072. /* Add headers to the URI. Identical, so they match */
  2073. { "sip:bob@example.com?header1=value1&header2=value2", "sip:bob@example.com?header1=value1&header2=value2", URI_CMP_MATCH },
  2074. /* Headers in URI 1 are not in URI 2. No Match */
  2075. { "sip:bob@example.com?header1=value1&header2=value2", "sip:bob@example.com", URI_CMP_NOMATCH },
  2076. /* Header present in both URIs does not have matching values. No match */
  2077. { "sip:bob@example.com?header1=value1&header2=value2", "sip:bob@example.com?header1=value1&header2=value3", URI_CMP_NOMATCH },
  2078. /* Add parameters to the URI. Identical so they match */
  2079. { "sip:bob@example.com;param1=value1;param2=value2", "sip:bob@example.com;param1=value1;param2=value2", URI_CMP_MATCH },
  2080. /* Same parameters in both URIs but appear in different order. Match */
  2081. { "sip:bob@example.com;param2=value2;param1=value1", "sip:bob@example.com;param1=value1;param2=value2", URI_CMP_MATCH },
  2082. /* params in URI 1 are not in URI 2. Match */
  2083. { "sip:bob@example.com;param1=value1;param2=value2", "sip:bob@example.com", URI_CMP_MATCH },
  2084. /* param present in both URIs does not have matching values. No match */
  2085. { "sip:bob@example.com;param1=value1;param2=value2", "sip:bob@example.com;param1=value1;param2=value3", URI_CMP_NOMATCH },
  2086. /* URI 1 has a maddr param but URI 2 does not. No match */
  2087. { "sip:bob@example.com;param1=value1;maddr=192.168.0.1", "sip:bob@example.com;param1=value1", URI_CMP_NOMATCH },
  2088. /* URI 1 and URI 2 both have identical maddr params. Match */
  2089. { "sip:bob@example.com;param1=value1;maddr=192.168.0.1", "sip:bob@example.com;param1=value1;maddr=192.168.0.1", URI_CMP_MATCH },
  2090. /* URI 1 is a SIPS URI and URI 2 is a SIP URI. No Match */
  2091. { "sips:bob@example.com", "sip:bob@example.com", URI_CMP_NOMATCH },
  2092. /* No URI schemes. No match */
  2093. { "bob@example.com", "bob@example.com", URI_CMP_NOMATCH },
  2094. /* Crashiness tests. Just an address scheme. No match */
  2095. { "sip", "sips", URI_CMP_NOMATCH },
  2096. /* Still just an address scheme. Even though they're the same, No match */
  2097. { "sip", "sip", URI_CMP_NOMATCH },
  2098. /* Empty strings. No match */
  2099. { "", "", URI_CMP_NOMATCH },
  2100. /* An empty string and a NULL. No match */
  2101. { "", NULL, URI_CMP_NOMATCH },
  2102. };
  2103. int i;
  2104. int test_res = AST_TEST_PASS;
  2105. switch (cmd) {
  2106. case TEST_INIT:
  2107. info->name = "sip_uri_cmp_test";
  2108. info->category = "/channels/chan_sip/";
  2109. info->summary = "Tests comparison of SIP URIs";
  2110. info->description = "Several would-be tricky URI comparisons are performed";
  2111. return AST_TEST_NOT_RUN;
  2112. case TEST_EXECUTE:
  2113. break;
  2114. }
  2115. for (i = 0; i < ARRAY_LEN(uri_cmp_tests); ++i) {
  2116. int cmp_res1;
  2117. int cmp_res2;
  2118. if ((cmp_res1 = sip_uri_cmp(uri_cmp_tests[i].uri1, uri_cmp_tests[i].uri2))) {
  2119. /* URI comparison may return -1 or +1 depending on the failure. Standardize
  2120. * the return value to be URI_CMP_NOMATCH on any failure
  2121. */
  2122. cmp_res1 = URI_CMP_NOMATCH;
  2123. }
  2124. if (cmp_res1 != uri_cmp_tests[i].expected_result) {
  2125. ast_test_status_update(test, "Unexpected comparison result for URIs %s and %s. "
  2126. "Expected %s but got %s\n", uri_cmp_tests[i].uri1, uri_cmp_tests[i].uri2,
  2127. uri_cmp_tests[i].expected_result == URI_CMP_MATCH ? "Match" : "No Match",
  2128. cmp_res1 == URI_CMP_MATCH ? "Match" : "No Match");
  2129. test_res = AST_TEST_FAIL;
  2130. }
  2131. /* All URI comparisons are commutative, so for the sake of being thorough, we'll
  2132. * rerun the comparison with the parameters reversed
  2133. */
  2134. if ((cmp_res2 = sip_uri_cmp(uri_cmp_tests[i].uri2, uri_cmp_tests[i].uri1))) {
  2135. /* URI comparison may return -1 or +1 depending on the failure. Standardize
  2136. * the return value to be URI_CMP_NOMATCH on any failure
  2137. */
  2138. cmp_res2 = URI_CMP_NOMATCH;
  2139. }
  2140. if (cmp_res2 != uri_cmp_tests[i].expected_result) {
  2141. ast_test_status_update(test, "Unexpected comparison result for URIs %s and %s. "
  2142. "Expected %s but got %s\n", uri_cmp_tests[i].uri2, uri_cmp_tests[i].uri1,
  2143. uri_cmp_tests[i].expected_result == URI_CMP_MATCH ? "Match" : "No Match",
  2144. cmp_res2 == URI_CMP_MATCH ? "Match" : "No Match");
  2145. test_res = AST_TEST_FAIL;
  2146. }
  2147. }
  2148. return test_res;
  2149. }
  2150. #endif
  2151. void free_via(struct sip_via *v)
  2152. {
  2153. if (!v) {
  2154. return;
  2155. }
  2156. ast_free(v->via);
  2157. ast_free(v);
  2158. }
  2159. struct sip_via *parse_via(const char *header)
  2160. {
  2161. struct sip_via *v = ast_calloc(1, sizeof(*v));
  2162. char *via, *parm;
  2163. if (!v) {
  2164. return NULL;
  2165. }
  2166. v->via = ast_strdup(header);
  2167. v->ttl = 1;
  2168. via = v->via;
  2169. if (ast_strlen_zero(via)) {
  2170. ast_log(LOG_ERROR, "received request without a Via header\n");
  2171. free_via(v);
  2172. return NULL;
  2173. }
  2174. /* seperate the first via-parm */
  2175. via = strsep(&via, ",");
  2176. /* chop off sent-protocol */
  2177. v->protocol = strsep(&via, " \t\r\n");
  2178. if (ast_strlen_zero(v->protocol)) {
  2179. ast_log(LOG_ERROR, "missing sent-protocol in Via header\n");
  2180. free_via(v);
  2181. return NULL;
  2182. }
  2183. v->protocol = ast_skip_blanks(v->protocol);
  2184. if (via) {
  2185. via = ast_skip_blanks(via);
  2186. }
  2187. /* chop off sent-by */
  2188. v->sent_by = strsep(&via, "; \t\r\n");
  2189. if (ast_strlen_zero(v->sent_by)) {
  2190. ast_log(LOG_ERROR, "missing sent-by in Via header\n");
  2191. free_via(v);
  2192. return NULL;
  2193. }
  2194. v->sent_by = ast_skip_blanks(v->sent_by);
  2195. /* store the port, we have to handle ipv6 addresses containing ':'
  2196. * characters gracefully */
  2197. if (((parm = strchr(v->sent_by, ']')) && *(++parm) == ':') || (!(parm = strchr(v->sent_by, ']')) && (parm = strchr(v->sent_by, ':')))) {
  2198. char *endptr;
  2199. v->port = strtol(++parm, &endptr, 10);
  2200. }
  2201. /* evaluate any via-parms */
  2202. while ((parm = strsep(&via, "; \t\r\n"))) {
  2203. char *c;
  2204. if ((c = strstr(parm, "maddr="))) {
  2205. v->maddr = ast_skip_blanks(c + sizeof("maddr=") - 1);
  2206. } else if ((c = strstr(parm, "branch="))) {
  2207. v->branch = ast_skip_blanks(c + sizeof("branch=") - 1);
  2208. } else if ((c = strstr(parm, "ttl="))) {
  2209. char *endptr;
  2210. c = ast_skip_blanks(c + sizeof("ttl=") - 1);
  2211. v->ttl = strtol(c, &endptr, 10);
  2212. /* make sure we got a valid ttl value */
  2213. if (c == endptr) {
  2214. v->ttl = 1;
  2215. }
  2216. }
  2217. }
  2218. return v;
  2219. }
  2220. #ifdef TEST_FRAMEWORK
  2221. AST_TEST_DEFINE(parse_via_test)
  2222. {
  2223. int res = AST_TEST_PASS;
  2224. int i = 1;
  2225. struct sip_via *via;
  2226. struct testdata {
  2227. char *in;
  2228. char *expected_protocol;
  2229. char *expected_branch;
  2230. char *expected_sent_by;
  2231. char *expected_maddr;
  2232. unsigned int expected_port;
  2233. unsigned char expected_ttl;
  2234. int expected_null;
  2235. AST_LIST_ENTRY(testdata) list;
  2236. };
  2237. struct testdata *testdataptr;
  2238. static AST_LIST_HEAD_NOLOCK(testdataliststruct, testdata) testdatalist;
  2239. struct testdata t1 = {
  2240. .in = "SIP/2.0/UDP host:port;branch=thebranch",
  2241. .expected_protocol = "SIP/2.0/UDP",
  2242. .expected_sent_by = "host:port",
  2243. .expected_branch = "thebranch",
  2244. };
  2245. struct testdata t2 = {
  2246. .in = "SIP/2.0/UDP host:port",
  2247. .expected_protocol = "SIP/2.0/UDP",
  2248. .expected_sent_by = "host:port",
  2249. .expected_branch = "",
  2250. };
  2251. struct testdata t3 = {
  2252. .in = "SIP/2.0/UDP",
  2253. .expected_null = 1,
  2254. };
  2255. struct testdata t4 = {
  2256. .in = "BLAH/BLAH/BLAH host:port;branch=",
  2257. .expected_protocol = "BLAH/BLAH/BLAH",
  2258. .expected_sent_by = "host:port",
  2259. .expected_branch = "",
  2260. };
  2261. struct testdata t5 = {
  2262. .in = "SIP/2.0/UDP host:5060;branch=thebranch;maddr=224.0.0.1;ttl=1",
  2263. .expected_protocol = "SIP/2.0/UDP",
  2264. .expected_sent_by = "host:5060",
  2265. .expected_port = 5060,
  2266. .expected_branch = "thebranch",
  2267. .expected_maddr = "224.0.0.1",
  2268. .expected_ttl = 1,
  2269. };
  2270. struct testdata t6 = {
  2271. .in = "SIP/2.0/UDP host:5060;\n branch=thebranch;\r\n maddr=224.0.0.1; ttl=1",
  2272. .expected_protocol = "SIP/2.0/UDP",
  2273. .expected_sent_by = "host:5060",
  2274. .expected_port = 5060,
  2275. .expected_branch = "thebranch",
  2276. .expected_maddr = "224.0.0.1",
  2277. .expected_ttl = 1,
  2278. };
  2279. struct testdata t7 = {
  2280. .in = "SIP/2.0/UDP [::1]:5060",
  2281. .expected_protocol = "SIP/2.0/UDP",
  2282. .expected_sent_by = "[::1]:5060",
  2283. .expected_port = 5060,
  2284. .expected_branch = "",
  2285. };
  2286. switch (cmd) {
  2287. case TEST_INIT:
  2288. info->name = "parse_via_test";
  2289. info->category = "/channels/chan_sip/";
  2290. info->summary = "Tests parsing the Via header";
  2291. info->description =
  2292. "Runs through various test situations in which various "
  2293. " parameters parameter must be extracted from a VIA header";
  2294. return AST_TEST_NOT_RUN;
  2295. case TEST_EXECUTE:
  2296. break;
  2297. }
  2298. AST_LIST_HEAD_SET_NOLOCK(&testdatalist, &t1);
  2299. AST_LIST_INSERT_TAIL(&testdatalist, &t2, list);
  2300. AST_LIST_INSERT_TAIL(&testdatalist, &t3, list);
  2301. AST_LIST_INSERT_TAIL(&testdatalist, &t4, list);
  2302. AST_LIST_INSERT_TAIL(&testdatalist, &t5, list);
  2303. AST_LIST_INSERT_TAIL(&testdatalist, &t6, list);
  2304. AST_LIST_INSERT_TAIL(&testdatalist, &t7, list);
  2305. AST_LIST_TRAVERSE(&testdatalist, testdataptr, list) {
  2306. via = parse_via(testdataptr->in);
  2307. if (!via) {
  2308. if (!testdataptr->expected_null) {
  2309. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2310. "failed to parse header\n",
  2311. i, testdataptr->in);
  2312. res = AST_TEST_FAIL;
  2313. }
  2314. i++;
  2315. continue;
  2316. }
  2317. if (testdataptr->expected_null) {
  2318. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2319. "successfully parased invalid via header\n",
  2320. i, testdataptr->in);
  2321. res = AST_TEST_FAIL;
  2322. free_via(via);
  2323. i++;
  2324. continue;
  2325. }
  2326. if ((ast_strlen_zero(via->protocol) && !ast_strlen_zero(testdataptr->expected_protocol))
  2327. || (!ast_strlen_zero(via->protocol) && strcmp(via->protocol, testdataptr->expected_protocol))) {
  2328. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2329. "parsed protocol = \"%s\"\n"
  2330. "expected = \"%s\"\n"
  2331. "failed to parse protocol\n",
  2332. i, testdataptr->in, via->protocol, testdataptr->expected_protocol);
  2333. res = AST_TEST_FAIL;
  2334. }
  2335. if ((ast_strlen_zero(via->sent_by) && !ast_strlen_zero(testdataptr->expected_sent_by))
  2336. || (!ast_strlen_zero(via->sent_by) && strcmp(via->sent_by, testdataptr->expected_sent_by))) {
  2337. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2338. "parsed sent_by = \"%s\"\n"
  2339. "expected = \"%s\"\n"
  2340. "failed to parse sent-by\n",
  2341. i, testdataptr->in, via->sent_by, testdataptr->expected_sent_by);
  2342. res = AST_TEST_FAIL;
  2343. }
  2344. if (testdataptr->expected_port && testdataptr->expected_port != via->port) {
  2345. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2346. "parsed port = \"%u\"\n"
  2347. "expected = \"%u\"\n"
  2348. "failed to parse port\n",
  2349. i, testdataptr->in, via->port, testdataptr->expected_port);
  2350. res = AST_TEST_FAIL;
  2351. }
  2352. if ((ast_strlen_zero(via->branch) && !ast_strlen_zero(testdataptr->expected_branch))
  2353. || (!ast_strlen_zero(via->branch) && strcmp(via->branch, testdataptr->expected_branch))) {
  2354. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2355. "parsed branch = \"%s\"\n"
  2356. "expected = \"%s\"\n"
  2357. "failed to parse branch\n",
  2358. i, testdataptr->in, via->branch, testdataptr->expected_branch);
  2359. res = AST_TEST_FAIL;
  2360. }
  2361. if ((ast_strlen_zero(via->maddr) && !ast_strlen_zero(testdataptr->expected_maddr))
  2362. || (!ast_strlen_zero(via->maddr) && strcmp(via->maddr, testdataptr->expected_maddr))) {
  2363. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2364. "parsed maddr = \"%s\"\n"
  2365. "expected = \"%s\"\n"
  2366. "failed to parse maddr\n",
  2367. i, testdataptr->in, via->maddr, testdataptr->expected_maddr);
  2368. res = AST_TEST_FAIL;
  2369. }
  2370. if (testdataptr->expected_ttl && testdataptr->expected_ttl != via->ttl) {
  2371. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2372. "parsed ttl = \"%d\"\n"
  2373. "expected = \"%d\"\n"
  2374. "failed to parse ttl\n",
  2375. i, testdataptr->in, via->ttl, testdataptr->expected_ttl);
  2376. res = AST_TEST_FAIL;
  2377. }
  2378. free_via(via);
  2379. i++;
  2380. }
  2381. return res;
  2382. }
  2383. #endif
  2384. void sip_request_parser_register_tests(void)
  2385. {
  2386. AST_TEST_REGISTER(get_calleridname_test);
  2387. AST_TEST_REGISTER(sip_parse_uri_test);
  2388. AST_TEST_REGISTER(get_in_brackets_test);
  2389. AST_TEST_REGISTER(get_name_and_number_test);
  2390. AST_TEST_REGISTER(sip_parse_uri_full_test);
  2391. AST_TEST_REGISTER(parse_name_andor_addr_test);
  2392. AST_TEST_REGISTER(parse_contact_header_test);
  2393. AST_TEST_REGISTER(sip_parse_options_test);
  2394. AST_TEST_REGISTER(sip_uri_cmp_test);
  2395. AST_TEST_REGISTER(parse_via_test);
  2396. }
  2397. void sip_request_parser_unregister_tests(void)
  2398. {
  2399. AST_TEST_UNREGISTER(sip_parse_uri_test);
  2400. AST_TEST_UNREGISTER(get_calleridname_test);
  2401. AST_TEST_UNREGISTER(get_in_brackets_test);
  2402. AST_TEST_UNREGISTER(get_name_and_number_test);
  2403. AST_TEST_UNREGISTER(sip_parse_uri_full_test);
  2404. AST_TEST_UNREGISTER(parse_name_andor_addr_test);
  2405. AST_TEST_UNREGISTER(parse_contact_header_test);
  2406. AST_TEST_UNREGISTER(sip_parse_options_test);
  2407. AST_TEST_UNREGISTER(sip_uri_cmp_test);
  2408. AST_TEST_UNREGISTER(parse_via_test);
  2409. }
  2410. int sip_reqresp_parser_init(void)
  2411. {
  2412. #ifdef HAVE_XLOCALE_H
  2413. c_locale = newlocale(LC_CTYPE_MASK, "C", NULL);
  2414. if (!c_locale) {
  2415. return -1;
  2416. }
  2417. #endif
  2418. return 0;
  2419. }
  2420. void sip_reqresp_parser_exit(void)
  2421. {
  2422. #ifdef HAVE_XLOCALE_H
  2423. if (c_locale) {
  2424. freelocale(c_locale);
  2425. c_locale = NULL;
  2426. }
  2427. #endif
  2428. }