reqresp_parser.c 73 KB

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