reqresp_parser.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  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[50];
  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_full(char *tmp,char **out,char **residue)
  832. {
  833. const char *parse = tmp;
  834. char *first_bracket;
  835. char *second_bracket;
  836. if (out) {
  837. *out = "";
  838. }
  839. if (residue) {
  840. *residue = "";
  841. }
  842. if (ast_strlen_zero(tmp)) {
  843. return 1;
  844. }
  845. /*
  846. * Skip any quoted text until we find the part in brackets.
  847. * On any error give up and return -1
  848. */
  849. while ( (first_bracket = strchr(parse, '<')) ) {
  850. char *first_quote = strchr(parse, '"');
  851. first_bracket++;
  852. if (!first_quote || first_quote >= first_bracket) {
  853. break; /* no need to look at quoted part */
  854. }
  855. /* the bracket is within quotes, so ignore it */
  856. parse = find_closing_quote(first_quote + 1, NULL);
  857. if (!*parse) {
  858. ast_log(LOG_WARNING, "No closing quote found in '%s'\n", tmp);
  859. return -1;
  860. }
  861. parse++;
  862. }
  863. /* If no first bracket then still look for a second bracket as some other parsing functions
  864. may overwrite first bracket with NULL when terminating a token based display-name. As this
  865. only affects token based display-names there is no danger of brackets being in quotes */
  866. if (first_bracket) {
  867. parse = first_bracket;
  868. } else {
  869. parse = tmp;
  870. }
  871. if ((second_bracket = strchr(parse, '>'))) {
  872. *second_bracket++ = '\0';
  873. if (out) {
  874. *out = (char *) parse;
  875. }
  876. if (residue) {
  877. *residue = second_bracket;
  878. }
  879. return 0;
  880. }
  881. if ((first_bracket)) {
  882. ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
  883. return -1;
  884. }
  885. if (out) {
  886. *out = tmp;
  887. }
  888. return 1;
  889. }
  890. char *get_in_brackets(char *tmp)
  891. {
  892. char *out;
  893. if ((get_in_brackets_full(tmp, &out, NULL))) {
  894. return tmp;
  895. }
  896. return out;
  897. }
  898. AST_TEST_DEFINE(get_in_brackets_test)
  899. {
  900. int res = AST_TEST_PASS;
  901. char in_brackets[] = "sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah";
  902. char no_name[] = "<sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
  903. char quoted_string[] = "\"I'm a quote stri><ng\" <sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
  904. char missing_end_quote[] = "\"I'm a quote string <sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
  905. char name_no_quotes[] = "name not in quotes <sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
  906. char no_end_bracket[] = "name not in quotes <sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah";
  907. char no_name_no_brackets[] = "sip:name@host";
  908. char missing_start_bracket[] = "sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
  909. char *uri = NULL;
  910. switch (cmd) {
  911. case TEST_INIT:
  912. info->name = "sip_get_in_brackets_test";
  913. info->category = "/channels/chan_sip/";
  914. info->summary = "Tests getting a sip uri in <> brackets within a sip header.";
  915. info->description =
  916. "Runs through various test situations in which a sip uri "
  917. "in angle brackets needs to be retrieved";
  918. return AST_TEST_NOT_RUN;
  919. case TEST_EXECUTE:
  920. break;
  921. }
  922. /* Test 1, simple get in brackets */
  923. if (!(uri = get_in_brackets(no_name)) || strcmp(uri, in_brackets)) {
  924. ast_test_status_update(test, "Test 1, simple get in brackets failed. %s\n", uri);
  925. res = AST_TEST_FAIL;
  926. }
  927. /* Test 2, starts with quoted string */
  928. if (!(uri = get_in_brackets(quoted_string)) || strcmp(uri, in_brackets)) {
  929. ast_test_status_update(test, "Test 2, get in brackets with quoted string in front failed. %s\n", uri);
  930. res = AST_TEST_FAIL;
  931. }
  932. /* Test 3, missing end quote */
  933. if (!(uri = get_in_brackets(missing_end_quote)) || !strcmp(uri, in_brackets)) {
  934. ast_test_status_update(test, "Test 3, missing end quote failed. %s\n", uri);
  935. res = AST_TEST_FAIL;
  936. }
  937. /* Test 4, starts with a name not in quotes */
  938. if (!(uri = get_in_brackets(name_no_quotes)) || strcmp(uri, in_brackets)) {
  939. ast_test_status_update(test, "Test 4, passing name not in quotes failed. %s\n", uri);
  940. res = AST_TEST_FAIL;
  941. }
  942. /* Test 5, no end bracket, should just return everything after the first '<' */
  943. if (!(uri = get_in_brackets(no_end_bracket)) || !strcmp(uri, in_brackets)) {
  944. ast_test_status_update(test, "Test 5, no end bracket failed. %s\n", uri);
  945. res = AST_TEST_FAIL;
  946. }
  947. /* Test 6, NULL input */
  948. if (get_in_brackets(NULL)) {
  949. ast_test_status_update(test, "Test 6, NULL input failed.\n");
  950. res = AST_TEST_FAIL;
  951. }
  952. /* Test 7, no name, and no brackets. */
  953. if (!(uri = get_in_brackets(no_name_no_brackets)) || strcmp(uri, "sip:name@host")) {
  954. ast_test_status_update(test, "Test 7 failed. %s\n", uri);
  955. res = AST_TEST_FAIL;
  956. }
  957. /* Test 8, no start bracket, but with ending bracket. */
  958. if (!(uri = get_in_brackets(missing_start_bracket)) || strcmp(uri, in_brackets)) {
  959. ast_test_status_update(test, "Test 8 failed. %s\n", uri);
  960. res = AST_TEST_FAIL;
  961. }
  962. return res;
  963. }
  964. int parse_name_andor_addr(char *uri, const char *scheme, char **name,
  965. char **user, char **pass, char **hostport,
  966. struct uriparams *params, char **headers,
  967. char **residue)
  968. {
  969. char buf[1024];
  970. char **residue2 = residue;
  971. char *orig_uri = uri;
  972. int ret;
  973. buf[0] = '\0';
  974. if (name) {
  975. uri = (char *) get_calleridname(uri, buf, sizeof(buf));
  976. }
  977. ret = get_in_brackets_full(uri, &uri, residue);
  978. if (ret == 0) {
  979. /*
  980. * The uri is in brackets so do not treat unknown trailing uri
  981. * parameters as potential message header parameters.
  982. */
  983. if (residue && **residue) {
  984. /* step over the first semicolon as per parse_uri_full residue */
  985. *residue = *residue + 1;
  986. }
  987. residue2 = NULL;
  988. }
  989. if (name) {
  990. if (buf[0]) {
  991. /*
  992. * There is always room at orig_uri for the display-name because
  993. * at least one character has always been removed. A '"' or '<'
  994. * has been removed.
  995. */
  996. strcpy(orig_uri, buf);
  997. *name = orig_uri;
  998. } else {
  999. *name = "";
  1000. }
  1001. }
  1002. return parse_uri_full(uri, scheme, user, pass, hostport, params, headers, residue2);
  1003. }
  1004. AST_TEST_DEFINE(parse_name_andor_addr_test)
  1005. {
  1006. int res = AST_TEST_PASS;
  1007. char uri[1024];
  1008. char *name, *user, *pass, *hostport, *headers, *residue;
  1009. struct uriparams params;
  1010. struct testdata {
  1011. char *desc;
  1012. char *uri;
  1013. char *name;
  1014. char *user;
  1015. char *pass;
  1016. char *hostport;
  1017. char *headers;
  1018. char *residue;
  1019. struct uriparams params;
  1020. AST_LIST_ENTRY(testdata) list;
  1021. };
  1022. struct testdata *testdataptr;
  1023. static AST_LIST_HEAD_NOLOCK(testdataliststruct, testdata) testdatalist;
  1024. struct testdata td1 = {
  1025. .desc = "quotes and brackets",
  1026. .uri = "\"name :@ \" <sip:user:secret@host:5060;param=discard;transport=tcp>;tag=tag",
  1027. .name = "name :@ ",
  1028. .user = "user",
  1029. .pass = "secret",
  1030. .hostport = "host:5060",
  1031. .headers = "",
  1032. .residue = "tag=tag",
  1033. .params.transport = "tcp",
  1034. .params.lr = 0,
  1035. .params.user = ""
  1036. };
  1037. struct testdata td2 = {
  1038. .desc = "no quotes",
  1039. .uri = "givenname familyname <sip:user:secret@host:5060;param=discard;transport=tcp>;expires=3600",
  1040. .name = "givenname familyname",
  1041. .user = "user",
  1042. .pass = "secret",
  1043. .hostport = "host:5060",
  1044. .headers = "",
  1045. .residue = "expires=3600",
  1046. .params.transport = "tcp",
  1047. .params.lr = 0,
  1048. .params.user = ""
  1049. };
  1050. struct testdata td3 = {
  1051. .desc = "no brackets",
  1052. .uri = "sip:user:secret@host:5060;param=discard;transport=tcp;q=1",
  1053. .name = "",
  1054. .user = "user",
  1055. .pass = "secret",
  1056. .hostport = "host:5060",
  1057. .headers = "",
  1058. .residue = "q=1",
  1059. .params.transport = "tcp",
  1060. .params.lr = 0,
  1061. .params.user = ""
  1062. };
  1063. struct testdata td4 = {
  1064. .desc = "just host",
  1065. .uri = "sips:host",
  1066. .name = "",
  1067. .user = "",
  1068. .pass = "",
  1069. .hostport = "host",
  1070. .headers = "",
  1071. .residue = "",
  1072. .params.transport = "",
  1073. .params.lr = 0,
  1074. .params.user = ""
  1075. };
  1076. AST_LIST_HEAD_SET_NOLOCK(&testdatalist, &td1);
  1077. AST_LIST_INSERT_TAIL(&testdatalist, &td2, list);
  1078. AST_LIST_INSERT_TAIL(&testdatalist, &td3, list);
  1079. AST_LIST_INSERT_TAIL(&testdatalist, &td4, list);
  1080. switch (cmd) {
  1081. case TEST_INIT:
  1082. info->name = "parse_name_andor_addr_test";
  1083. info->category = "/channels/chan_sip/";
  1084. info->summary = "tests parsing of name_andor_addr abnf structure";
  1085. info->description =
  1086. "Tests parsing of abnf name-andor-addr = name-addr / addr-spec "
  1087. "Verifies output matches expected behavior.";
  1088. return AST_TEST_NOT_RUN;
  1089. case TEST_EXECUTE:
  1090. break;
  1091. }
  1092. AST_LIST_TRAVERSE(&testdatalist, testdataptr, list) {
  1093. name = user = pass = hostport = headers = residue = NULL;
  1094. params.transport = params.user = params.method = params.ttl = params.maddr = NULL;
  1095. params.lr = 0;
  1096. ast_copy_string(uri,testdataptr->uri,sizeof(uri));
  1097. if (parse_name_andor_addr(uri, "sip:,sips:",
  1098. &name,
  1099. &user,
  1100. &pass,
  1101. &hostport,
  1102. &params,
  1103. &headers,
  1104. &residue) ||
  1105. (name && strcmp(testdataptr->name, name)) ||
  1106. (user && strcmp(testdataptr->user, user)) ||
  1107. (pass && strcmp(testdataptr->pass, pass)) ||
  1108. (hostport && strcmp(testdataptr->hostport, hostport)) ||
  1109. (headers && strcmp(testdataptr->headers, headers)) ||
  1110. (residue && strcmp(testdataptr->residue, residue)) ||
  1111. (strcmp(testdataptr->params.transport,params.transport)) ||
  1112. (strcmp(testdataptr->params.user,params.user))
  1113. ) {
  1114. ast_test_status_update(test, "Sub-Test: %s,failed.\n", testdataptr->desc);
  1115. res = AST_TEST_FAIL;
  1116. }
  1117. }
  1118. return res;
  1119. }
  1120. int get_comma(char *in, char **out)
  1121. {
  1122. char *c;
  1123. char *parse = in;
  1124. if (out) {
  1125. *out = in;
  1126. }
  1127. /* Skip any quoted text */
  1128. while (*parse) {
  1129. if ((c = strchr(parse, '"'))) {
  1130. in = (char *)find_closing_quote((const char *)c + 1, NULL);
  1131. if (!*in) {
  1132. ast_log(LOG_WARNING, "No closing quote found in '%s'\n", c);
  1133. return -1;
  1134. } else {
  1135. break;
  1136. }
  1137. } else {
  1138. break;
  1139. }
  1140. parse++;
  1141. }
  1142. parse = in;
  1143. /* Skip any userinfo components of a uri as they may contain commas */
  1144. if ((c = strchr(parse,'@'))) {
  1145. parse = c+1;
  1146. }
  1147. if ((out) && (c = strchr(parse,','))) {
  1148. *c++ = '\0';
  1149. *out = c;
  1150. return 0;
  1151. }
  1152. return 1;
  1153. }
  1154. int parse_contact_header(char *contactheader, struct contactliststruct *contactlist)
  1155. {
  1156. int res;
  1157. int last;
  1158. char *comma;
  1159. char *residue;
  1160. char *param;
  1161. char *value;
  1162. struct contact *split_contact = NULL;
  1163. if (*contactheader == '*') {
  1164. return 1;
  1165. }
  1166. split_contact = ast_calloc(1, sizeof(*split_contact));
  1167. AST_LIST_HEAD_SET_NOLOCK(contactlist, split_contact);
  1168. while ((last = get_comma(contactheader, &comma)) != -1) {
  1169. res = parse_name_andor_addr(contactheader, "sip:,sips:",
  1170. &split_contact->name, &split_contact->user,
  1171. &split_contact->pass, &split_contact->hostport,
  1172. &split_contact->params, &split_contact->headers,
  1173. &residue);
  1174. if (res == -1) {
  1175. return res;
  1176. }
  1177. /* parse contact params */
  1178. split_contact->expires = split_contact->q = "";
  1179. while ((value = strchr(residue,'='))) {
  1180. *value++ = '\0';
  1181. param = residue;
  1182. if ((residue = strchr(value,';'))) {
  1183. *residue++ = '\0';
  1184. } else {
  1185. residue = "";
  1186. }
  1187. if (!strcmp(param,"expires")) {
  1188. split_contact->expires = value;
  1189. } else if (!strcmp(param,"q")) {
  1190. split_contact->q = value;
  1191. }
  1192. }
  1193. if (last) {
  1194. return 0;
  1195. }
  1196. contactheader = comma;
  1197. split_contact = ast_calloc(1, sizeof(*split_contact));
  1198. AST_LIST_INSERT_TAIL(contactlist, split_contact, list);
  1199. }
  1200. return last;
  1201. }
  1202. AST_TEST_DEFINE(parse_contact_header_test)
  1203. {
  1204. int res = AST_TEST_PASS;
  1205. char contactheader[1024];
  1206. int star;
  1207. struct contactliststruct contactlist;
  1208. struct contactliststruct *contactlistptr=&contactlist;
  1209. struct testdata {
  1210. char *desc;
  1211. char *contactheader;
  1212. int star;
  1213. struct contactliststruct *contactlist;
  1214. AST_LIST_ENTRY(testdata) list;
  1215. };
  1216. struct testdata *testdataptr;
  1217. struct contact *tdcontactptr;
  1218. struct contact *contactptr;
  1219. static AST_LIST_HEAD_NOLOCK(testdataliststruct, testdata) testdatalist;
  1220. struct contactliststruct contactlist1, contactlist2;
  1221. struct testdata td1 = {
  1222. .desc = "single contact",
  1223. .contactheader = "\"name :@;?&,\" <sip:user:secret@host:5082;param=discard;transport=tcp>;expires=3600",
  1224. .contactlist = &contactlist1,
  1225. .star = 0
  1226. };
  1227. struct contact contact11 = {
  1228. .name = "name :@;?&,",
  1229. .user = "user",
  1230. .pass = "secret",
  1231. .hostport = "host:5082",
  1232. .params.transport = "tcp",
  1233. .params.ttl = "",
  1234. .params.lr = 0,
  1235. .headers = "",
  1236. .expires = "3600",
  1237. .q = ""
  1238. };
  1239. struct testdata td2 = {
  1240. .desc = "multiple contacts",
  1241. .contactheader = "sip:,user1,:,secret1,@host1;ttl=7;q=1;expires=3600,sips:host2",
  1242. .contactlist = &contactlist2,
  1243. .star = 0,
  1244. };
  1245. struct contact contact21 = {
  1246. .name = "",
  1247. .user = ",user1,",
  1248. .pass = ",secret1,",
  1249. .hostport = "host1",
  1250. .params.transport = "",
  1251. .params.ttl = "7",
  1252. .params.lr = 0,
  1253. .headers = "",
  1254. .expires = "3600",
  1255. .q = "1"
  1256. };
  1257. struct contact contact22 = {
  1258. .name = "",
  1259. .user = "",
  1260. .pass = "",
  1261. .hostport = "host2",
  1262. .params.transport = "",
  1263. .params.ttl = "",
  1264. .params.lr = 0,
  1265. .headers = "",
  1266. .expires = "",
  1267. .q = ""
  1268. };
  1269. struct testdata td3 = {
  1270. .desc = "star - all contacts",
  1271. .contactheader = "*",
  1272. .star = 1,
  1273. .contactlist = NULL
  1274. };
  1275. AST_LIST_HEAD_SET_NOLOCK(&testdatalist, &td1);
  1276. AST_LIST_INSERT_TAIL(&testdatalist, &td2, list);
  1277. AST_LIST_INSERT_TAIL(&testdatalist, &td3, list);
  1278. AST_LIST_HEAD_SET_NOLOCK(&contactlist1, &contact11);
  1279. AST_LIST_HEAD_SET_NOLOCK(&contactlist2, &contact21);
  1280. AST_LIST_INSERT_TAIL(&contactlist2, &contact22, list);
  1281. switch (cmd) {
  1282. case TEST_INIT:
  1283. info->name = "parse_contact_header_test";
  1284. info->category = "/channels/chan_sip/";
  1285. info->summary = "tests parsing of sip contact header";
  1286. info->description =
  1287. "Tests parsing of a contact header including those with multiple contacts "
  1288. "Verifies output matches expected behavior.";
  1289. return AST_TEST_NOT_RUN;
  1290. case TEST_EXECUTE:
  1291. break;
  1292. }
  1293. AST_LIST_TRAVERSE(&testdatalist, testdataptr, list) {
  1294. ast_copy_string(contactheader,testdataptr->contactheader,sizeof(contactheader));
  1295. star = parse_contact_header(contactheader,contactlistptr);
  1296. if (testdataptr->star) {
  1297. /* expecting star rather than list of contacts */
  1298. if (!star) {
  1299. ast_test_status_update(test, "Sub-Test: %s,failed.\n", testdataptr->desc);
  1300. res = AST_TEST_FAIL;
  1301. break;
  1302. }
  1303. } else {
  1304. contactptr = AST_LIST_FIRST(contactlistptr);
  1305. AST_LIST_TRAVERSE(testdataptr->contactlist, tdcontactptr, list) {
  1306. if (!contactptr ||
  1307. strcmp(tdcontactptr->name, contactptr->name) ||
  1308. strcmp(tdcontactptr->user, contactptr->user) ||
  1309. strcmp(tdcontactptr->pass, contactptr->pass) ||
  1310. strcmp(tdcontactptr->hostport, contactptr->hostport) ||
  1311. strcmp(tdcontactptr->headers, contactptr->headers) ||
  1312. strcmp(tdcontactptr->expires, contactptr->expires) ||
  1313. strcmp(tdcontactptr->q, contactptr->q) ||
  1314. strcmp(tdcontactptr->params.transport, contactptr->params.transport) ||
  1315. strcmp(tdcontactptr->params.ttl, contactptr->params.ttl) ||
  1316. (tdcontactptr->params.lr != contactptr->params.lr)
  1317. ) {
  1318. ast_test_status_update(test, "Sub-Test: %s,failed.\n", testdataptr->desc);
  1319. res = AST_TEST_FAIL;
  1320. break;
  1321. }
  1322. contactptr = AST_LIST_NEXT(contactptr,list);
  1323. }
  1324. }
  1325. }
  1326. return res;
  1327. }
  1328. /*!
  1329. * \brief Parse supported header in incoming packet
  1330. *
  1331. * \details This function parses through the options parameters and
  1332. * builds a bit field representing all the SIP options in that field. When an
  1333. * item is found that is not supported, it is copied to the unsupported
  1334. * out buffer.
  1335. *
  1336. * \param option list
  1337. * \param unsupported out buffer (optional)
  1338. * \param unsupported out buffer length (optional)
  1339. */
  1340. unsigned int parse_sip_options(const char *options, char *unsupported, size_t unsupported_len)
  1341. {
  1342. char *next, *sep;
  1343. char *temp;
  1344. int i, found, supported;
  1345. unsigned int profile = 0;
  1346. char *out = unsupported;
  1347. size_t outlen = unsupported_len;
  1348. char *cur_out = out;
  1349. if (out && (outlen > 0)) {
  1350. memset(out, 0, outlen);
  1351. }
  1352. if (ast_strlen_zero(options) )
  1353. return 0;
  1354. temp = ast_strdupa(options);
  1355. ast_debug(3, "Begin: parsing SIP \"Supported: %s\"\n", options);
  1356. for (next = temp; next; next = sep) {
  1357. found = FALSE;
  1358. supported = FALSE;
  1359. if ((sep = strchr(next, ',')) != NULL) {
  1360. *sep++ = '\0';
  1361. }
  1362. /* trim leading and trailing whitespace */
  1363. next = ast_strip(next);
  1364. if (ast_strlen_zero(next)) {
  1365. continue; /* if there is a blank argument in there just skip it */
  1366. }
  1367. ast_debug(3, "Found SIP option: -%s-\n", next);
  1368. for (i = 0; i < ARRAY_LEN(sip_options); i++) {
  1369. if (!strcasecmp(next, sip_options[i].text)) {
  1370. profile |= sip_options[i].id;
  1371. if (sip_options[i].supported == SUPPORTED) {
  1372. supported = TRUE;
  1373. }
  1374. found = TRUE;
  1375. ast_debug(3, "Matched SIP option: %s\n", next);
  1376. break;
  1377. }
  1378. }
  1379. /* If option is not supported, add to unsupported out buffer */
  1380. if (!supported && out && outlen) {
  1381. size_t copylen = strlen(next);
  1382. size_t cur_outlen = strlen(out);
  1383. /* Check to see if there is enough room to store this option.
  1384. * Copy length is string length plus 2 for the ',' and '\0' */
  1385. if ((cur_outlen + copylen + 2) < outlen) {
  1386. /* if this isn't the first item, add the ',' */
  1387. if (cur_outlen) {
  1388. *cur_out = ',';
  1389. cur_out++;
  1390. cur_outlen++;
  1391. }
  1392. ast_copy_string(cur_out, next, (outlen - cur_outlen));
  1393. cur_out += copylen;
  1394. }
  1395. }
  1396. if (!found) {
  1397. profile |= SIP_OPT_UNKNOWN;
  1398. if (!strncasecmp(next, "x-", 2))
  1399. ast_debug(3, "Found private SIP option, not supported: %s\n", next);
  1400. else
  1401. ast_debug(3, "Found no match for SIP option: %s (Please file bug report!)\n", next);
  1402. }
  1403. }
  1404. return profile;
  1405. }
  1406. AST_TEST_DEFINE(sip_parse_options_test)
  1407. {
  1408. int res = AST_TEST_PASS;
  1409. char unsupported[64];
  1410. unsigned int option_profile = 0;
  1411. struct testdata {
  1412. char *name;
  1413. char *input_options;
  1414. char *expected_unsupported;
  1415. unsigned int expected_profile;
  1416. AST_LIST_ENTRY(testdata) list;
  1417. };
  1418. struct testdata *testdataptr;
  1419. static AST_LIST_HEAD_NOLOCK(testdataliststruct, testdata) testdatalist;
  1420. struct testdata test1 = {
  1421. .name = "test_all_unsupported",
  1422. .input_options = "unsupported1,,, ,unsupported2,unsupported3,unsupported4",
  1423. .expected_unsupported = "unsupported1,unsupported2,unsupported3,unsupported4",
  1424. .expected_profile = SIP_OPT_UNKNOWN,
  1425. };
  1426. struct testdata test2 = {
  1427. .name = "test_all_unsupported_one_supported",
  1428. .input_options = " unsupported1, replaces, unsupported3 , , , ,unsupported4",
  1429. .expected_unsupported = "unsupported1,unsupported3,unsupported4",
  1430. .expected_profile = SIP_OPT_UNKNOWN | SIP_OPT_REPLACES
  1431. };
  1432. struct testdata test3 = {
  1433. .name = "test_two_supported_two_unsupported",
  1434. .input_options = ",, timer ,replaces ,unsupported3,unsupported4",
  1435. .expected_unsupported = "unsupported3,unsupported4",
  1436. .expected_profile = SIP_OPT_UNKNOWN | SIP_OPT_REPLACES | SIP_OPT_TIMER,
  1437. };
  1438. struct testdata test4 = {
  1439. .name = "test_all_supported",
  1440. .input_options = "timer,replaces",
  1441. .expected_unsupported = "",
  1442. .expected_profile = SIP_OPT_REPLACES | SIP_OPT_TIMER,
  1443. };
  1444. struct testdata test5 = {
  1445. .name = "test_all_supported_redundant",
  1446. .input_options = "timer,replaces,timer,replace,timer,replaces",
  1447. .expected_unsupported = "",
  1448. .expected_profile = SIP_OPT_REPLACES | SIP_OPT_TIMER,
  1449. };
  1450. struct testdata test6 = {
  1451. .name = "test_buffer_overflow",
  1452. .input_options = "unsupported1,replaces,timer,unsupported4,unsupported_huge____"
  1453. "____________________________________,__________________________________________"
  1454. "________________________________________________",
  1455. .expected_unsupported = "unsupported1,unsupported4",
  1456. .expected_profile = SIP_OPT_UNKNOWN | SIP_OPT_REPLACES | SIP_OPT_TIMER,
  1457. };
  1458. struct testdata test7 = {
  1459. .name = "test_null_input",
  1460. .input_options = NULL,
  1461. .expected_unsupported = "",
  1462. .expected_profile = 0,
  1463. };
  1464. struct testdata test8 = {
  1465. .name = "test_whitespace_input",
  1466. .input_options = " ",
  1467. .expected_unsupported = "",
  1468. .expected_profile = 0,
  1469. };
  1470. struct testdata test9 = {
  1471. .name = "test_whitespace_plus_option_input",
  1472. .input_options = " , , ,timer , , , , , ",
  1473. .expected_unsupported = "",
  1474. .expected_profile = SIP_OPT_TIMER,
  1475. };
  1476. switch (cmd) {
  1477. case TEST_INIT:
  1478. info->name = "sip_parse_options_test";
  1479. info->category = "/channels/chan_sip/";
  1480. info->summary = "Tests parsing of sip options";
  1481. info->description =
  1482. "Tests parsing of SIP options from supported and required "
  1483. "header fields. Verifies when unsupported options are encountered "
  1484. "that they are appended to the unsupported out buffer and that the "
  1485. "correct bit field representnig the option profile is returned.";
  1486. return AST_TEST_NOT_RUN;
  1487. case TEST_EXECUTE:
  1488. break;
  1489. }
  1490. AST_LIST_HEAD_SET_NOLOCK(&testdatalist, &test1);
  1491. AST_LIST_INSERT_TAIL(&testdatalist, &test2, list);
  1492. AST_LIST_INSERT_TAIL(&testdatalist, &test3, list);
  1493. AST_LIST_INSERT_TAIL(&testdatalist, &test4, list);
  1494. AST_LIST_INSERT_TAIL(&testdatalist, &test5, list);
  1495. AST_LIST_INSERT_TAIL(&testdatalist, &test6, list);
  1496. AST_LIST_INSERT_TAIL(&testdatalist, &test7, list);
  1497. AST_LIST_INSERT_TAIL(&testdatalist, &test8, list);
  1498. AST_LIST_INSERT_TAIL(&testdatalist, &test9, list);
  1499. /* Test with unsupported char buffer */
  1500. AST_LIST_TRAVERSE(&testdatalist, testdataptr, list) {
  1501. option_profile = parse_sip_options(testdataptr->input_options, unsupported, ARRAY_LEN(unsupported));
  1502. if (option_profile != testdataptr->expected_profile ||
  1503. strcmp(unsupported, testdataptr->expected_unsupported)) {
  1504. ast_test_status_update(test, "Test with output buffer \"%s\", expected unsupported: %s actual unsupported:"
  1505. "%s expected bit profile: %x actual bit profile: %x\n",
  1506. testdataptr->name,
  1507. testdataptr->expected_unsupported,
  1508. unsupported,
  1509. testdataptr->expected_profile,
  1510. option_profile);
  1511. res = AST_TEST_FAIL;
  1512. } else {
  1513. ast_test_status_update(test, "\"%s\" passed got expected unsupported: %s and bit profile: %x\n",
  1514. testdataptr->name,
  1515. unsupported,
  1516. option_profile);
  1517. }
  1518. option_profile = parse_sip_options(testdataptr->input_options, NULL, 0);
  1519. if (option_profile != testdataptr->expected_profile) {
  1520. ast_test_status_update(test, "NULL output test \"%s\", expected bit profile: %x actual bit profile: %x\n",
  1521. testdataptr->name,
  1522. testdataptr->expected_profile,
  1523. option_profile);
  1524. res = AST_TEST_FAIL;
  1525. } else {
  1526. ast_test_status_update(test, "\"%s\" with NULL output buf passed, bit profile: %x\n",
  1527. testdataptr->name,
  1528. option_profile);
  1529. }
  1530. }
  1531. return res;
  1532. }
  1533. /*! \brief helper routine for sip_uri_cmp to compare URI parameters
  1534. *
  1535. * This takes the parameters from two SIP URIs and determines
  1536. * if the URIs match. The rules for parameters *suck*. Here's a breakdown
  1537. * 1. If a parameter appears in both URIs, then they must have the same value
  1538. * in order for the URIs to match
  1539. * 2. If one URI has a user, maddr, ttl, or method parameter, then the other
  1540. * URI must also have that parameter and must have the same value
  1541. * in order for the URIs to match
  1542. * 3. All other headers appearing in only one URI are not considered when
  1543. * determining if URIs match
  1544. *
  1545. * \param input1 Parameters from URI 1
  1546. * \param input2 Parameters from URI 2
  1547. * \retval 0 URIs' parameters match
  1548. * \retval nonzero URIs' parameters do not match
  1549. */
  1550. static int sip_uri_params_cmp(const char *input1, const char *input2)
  1551. {
  1552. char *params1 = NULL;
  1553. char *params2 = NULL;
  1554. char *pos1;
  1555. char *pos2;
  1556. int zerolength1 = 0;
  1557. int zerolength2 = 0;
  1558. int maddrmatch = 0;
  1559. int ttlmatch = 0;
  1560. int usermatch = 0;
  1561. int methodmatch = 0;
  1562. if (ast_strlen_zero(input1)) {
  1563. zerolength1 = 1;
  1564. } else {
  1565. params1 = ast_strdupa(input1);
  1566. }
  1567. if (ast_strlen_zero(input2)) {
  1568. zerolength2 = 1;
  1569. } else {
  1570. params2 = ast_strdupa(input2);
  1571. }
  1572. /* Quick optimization. If both params are zero-length, then
  1573. * they match
  1574. */
  1575. if (zerolength1 && zerolength2) {
  1576. return 0;
  1577. }
  1578. for (pos1 = strsep(&params1, ";"); pos1; pos1 = strsep(&params1, ";")) {
  1579. char *value1 = pos1;
  1580. char *name1 = strsep(&value1, "=");
  1581. char *params2dup = NULL;
  1582. int matched = 0;
  1583. if (!value1) {
  1584. value1 = "";
  1585. }
  1586. /* Checkpoint reached. We have the name and value parsed for param1
  1587. * We have to duplicate params2 each time through this loop
  1588. * or else the inner loop below will not work properly.
  1589. */
  1590. if (!zerolength2) {
  1591. params2dup = ast_strdupa(params2);
  1592. }
  1593. for (pos2 = strsep(&params2dup, ";"); pos2; pos2 = strsep(&params2dup, ";")) {
  1594. char *name2 = pos2;
  1595. char *value2 = strchr(pos2, '=');
  1596. if (!value2) {
  1597. value2 = "";
  1598. } else {
  1599. *value2++ = '\0';
  1600. }
  1601. if (!strcasecmp(name1, name2)) {
  1602. if (strcasecmp(value1, value2)) {
  1603. goto fail;
  1604. } else {
  1605. matched = 1;
  1606. break;
  1607. }
  1608. }
  1609. }
  1610. /* Check to see if the parameter is one of the 'must-match' parameters */
  1611. if (!strcasecmp(name1, "maddr")) {
  1612. if (matched) {
  1613. maddrmatch = 1;
  1614. } else {
  1615. goto fail;
  1616. }
  1617. } else if (!strcasecmp(name1, "ttl")) {
  1618. if (matched) {
  1619. ttlmatch = 1;
  1620. } else {
  1621. goto fail;
  1622. }
  1623. } else if (!strcasecmp(name1, "user")) {
  1624. if (matched) {
  1625. usermatch = 1;
  1626. } else {
  1627. goto fail;
  1628. }
  1629. } else if (!strcasecmp(name1, "method")) {
  1630. if (matched) {
  1631. methodmatch = 1;
  1632. } else {
  1633. goto fail;
  1634. }
  1635. }
  1636. }
  1637. /* We've made it out of that horrible O(m*n) construct and there are no
  1638. * failures yet. We're not done yet, though, because params2 could have
  1639. * an maddr, ttl, user, or method header and params1 did not.
  1640. */
  1641. for (pos2 = strsep(&params2, ";"); pos2; pos2 = strsep(&params2, ";")) {
  1642. char *value2 = pos2;
  1643. char *name2 = strsep(&value2, "=");
  1644. if (!value2) {
  1645. value2 = "";
  1646. }
  1647. if ((!strcasecmp(name2, "maddr") && !maddrmatch) ||
  1648. (!strcasecmp(name2, "ttl") && !ttlmatch) ||
  1649. (!strcasecmp(name2, "user") && !usermatch) ||
  1650. (!strcasecmp(name2, "method") && !methodmatch)) {
  1651. goto fail;
  1652. }
  1653. }
  1654. return 0;
  1655. fail:
  1656. return 1;
  1657. }
  1658. /*! \brief helper routine for sip_uri_cmp to compare URI headers
  1659. *
  1660. * This takes the headers from two SIP URIs and determines
  1661. * if the URIs match. The rules for headers is simple. If a header
  1662. * appears in one URI, then it must also appear in the other URI. The
  1663. * order in which the headers appear does not matter.
  1664. *
  1665. * \param input1 Headers from URI 1
  1666. * \param input2 Headers from URI 2
  1667. * \retval 0 URI headers match
  1668. * \retval nonzero URI headers do not match
  1669. */
  1670. static int sip_uri_headers_cmp(const char *input1, const char *input2)
  1671. {
  1672. char *headers1 = NULL;
  1673. char *headers2 = NULL;
  1674. int zerolength1 = 0;
  1675. int zerolength2 = 0;
  1676. int different = 0;
  1677. char *header1;
  1678. if (ast_strlen_zero(input1)) {
  1679. zerolength1 = 1;
  1680. } else {
  1681. headers1 = ast_strdupa(input1);
  1682. }
  1683. if (ast_strlen_zero(input2)) {
  1684. zerolength2 = 1;
  1685. } else {
  1686. headers2 = ast_strdupa(input2);
  1687. }
  1688. /* If one URI contains no headers and the other
  1689. * does, then they cannot possibly match
  1690. */
  1691. if (zerolength1 != zerolength2) {
  1692. return 1;
  1693. }
  1694. if (zerolength1 && zerolength2)
  1695. return 0;
  1696. /* At this point, we can definitively state that both inputs are
  1697. * not zero-length. First, one more optimization. If the length
  1698. * of the headers is not equal, then we definitely have no match
  1699. */
  1700. if (strlen(headers1) != strlen(headers2)) {
  1701. return 1;
  1702. }
  1703. for (header1 = strsep(&headers1, "&"); header1; header1 = strsep(&headers1, "&")) {
  1704. if (!strcasestr(headers2, header1)) {
  1705. different = 1;
  1706. break;
  1707. }
  1708. }
  1709. return different;
  1710. }
  1711. /*!
  1712. * \brief Compare domain sections of SIP URIs
  1713. *
  1714. * For hostnames, a case insensitive string comparison is
  1715. * used. For IP addresses, a binary comparison is used. This
  1716. * is mainly because IPv6 addresses have many ways of writing
  1717. * the same address.
  1718. *
  1719. * For specifics about IP address comparison, see the following
  1720. * document: http://tools.ietf.org/html/draft-ietf-sip-ipv6-abnf-fix-05
  1721. *
  1722. * \param host1 The domain from the first URI
  1723. * \param host2 THe domain from the second URI
  1724. * \retval 0 The domains match
  1725. * \retval nonzero The domains do not match
  1726. */
  1727. static int sip_uri_domain_cmp(const char *host1, const char *host2)
  1728. {
  1729. struct ast_sockaddr addr1;
  1730. struct ast_sockaddr addr2;
  1731. int addr1_parsed;
  1732. int addr2_parsed;
  1733. addr1_parsed = ast_sockaddr_parse(&addr1, host1, 0);
  1734. addr2_parsed = ast_sockaddr_parse(&addr2, host2, 0);
  1735. if (addr1_parsed != addr2_parsed) {
  1736. /* One domain was an IP address and the other had
  1737. * a host name. FAIL!
  1738. */
  1739. return 1;
  1740. }
  1741. /* Both are host names. A string comparison will work
  1742. * perfectly here. Specifying the "C" locale ensures that
  1743. * The LC_CTYPE conventions use those defined in ANSI C,
  1744. * i.e. ASCII.
  1745. */
  1746. if (!addr1_parsed) {
  1747. #ifdef HAVE_XLOCALE_H
  1748. if(!c_locale) {
  1749. return strcasecmp(host1, host2);
  1750. } else {
  1751. return strcasecmp_l(host1, host2, c_locale);
  1752. }
  1753. #else
  1754. return strcasecmp(host1, host2);
  1755. #endif
  1756. }
  1757. /* Both contain IP addresses */
  1758. return ast_sockaddr_cmp(&addr1, &addr2);
  1759. }
  1760. int sip_uri_cmp(const char *input1, const char *input2)
  1761. {
  1762. char *uri1;
  1763. char *uri2;
  1764. char *uri_scheme1;
  1765. char *uri_scheme2;
  1766. char *host1;
  1767. char *host2;
  1768. char *params1;
  1769. char *params2;
  1770. char *headers1;
  1771. char *headers2;
  1772. /* XXX It would be really nice if we could just use parse_uri_full() here
  1773. * to separate the components of the URI, but unfortunately it is written
  1774. * in a way that can cause URI parameters to be discarded.
  1775. */
  1776. if (!input1 || !input2) {
  1777. return 1;
  1778. }
  1779. uri1 = ast_strdupa(input1);
  1780. uri2 = ast_strdupa(input2);
  1781. ast_uri_decode(uri1, ast_uri_sip_user);
  1782. ast_uri_decode(uri2, ast_uri_sip_user);
  1783. uri_scheme1 = strsep(&uri1, ":");
  1784. uri_scheme2 = strsep(&uri2, ":");
  1785. if (strcmp(uri_scheme1, uri_scheme2)) {
  1786. return 1;
  1787. }
  1788. /* This function is tailored for SIP and SIPS URIs. There's no
  1789. * need to check uri_scheme2 since we have determined uri_scheme1
  1790. * and uri_scheme2 are equivalent already.
  1791. */
  1792. if (strcmp(uri_scheme1, "sip") && strcmp(uri_scheme1, "sips")) {
  1793. return 1;
  1794. }
  1795. if (ast_strlen_zero(uri1) || ast_strlen_zero(uri2)) {
  1796. return 1;
  1797. }
  1798. if ((host1 = strchr(uri1, '@'))) {
  1799. *host1++ = '\0';
  1800. }
  1801. if ((host2 = strchr(uri2, '@'))) {
  1802. *host2++ = '\0';
  1803. }
  1804. /* Check for mismatched username and passwords. This is the
  1805. * only case-sensitive comparison of a SIP URI
  1806. */
  1807. if ((host1 && !host2) ||
  1808. (host2 && !host1) ||
  1809. (host1 && host2 && strcmp(uri1, uri2))) {
  1810. return 1;
  1811. }
  1812. if (!host1) {
  1813. host1 = uri1;
  1814. }
  1815. if (!host2) {
  1816. host2 = uri2;
  1817. }
  1818. /* Strip off the parameters and headers so we can compare
  1819. * host and port
  1820. */
  1821. if ((params1 = strchr(host1, ';'))) {
  1822. *params1++ = '\0';
  1823. }
  1824. if ((params2 = strchr(host2, ';'))) {
  1825. *params2++ = '\0';
  1826. }
  1827. /* Headers come after parameters, but there may be headers without
  1828. * parameters, thus the S_OR
  1829. */
  1830. if ((headers1 = strchr(S_OR(params1, host1), '?'))) {
  1831. *headers1++ = '\0';
  1832. }
  1833. if ((headers2 = strchr(S_OR(params2, host2), '?'))) {
  1834. *headers2++ = '\0';
  1835. }
  1836. if (sip_uri_domain_cmp(host1, host2)) {
  1837. return 1;
  1838. }
  1839. /* Headers have easier rules to follow, so do those first */
  1840. if (sip_uri_headers_cmp(headers1, headers2)) {
  1841. return 1;
  1842. }
  1843. /* And now the parameters. Ugh */
  1844. return sip_uri_params_cmp(params1, params2);
  1845. }
  1846. #define URI_CMP_MATCH 0
  1847. #define URI_CMP_NOMATCH 1
  1848. AST_TEST_DEFINE(sip_uri_cmp_test)
  1849. {
  1850. static const struct {
  1851. const char *uri1;
  1852. const char *uri2;
  1853. int expected_result;
  1854. } uri_cmp_tests [] = {
  1855. /* These are identical, so they match */
  1856. { "sip:bob@example.com", "sip:bob@example.com", URI_CMP_MATCH },
  1857. /* Different usernames. No match */
  1858. { "sip:alice@example.com", "sip:bob@example.com", URI_CMP_NOMATCH },
  1859. /* Different hosts. No match */
  1860. { "sip:bob@example.com", "sip:bob@examplez.com", URI_CMP_NOMATCH },
  1861. /* Now start using IP addresses. Identical, so they match */
  1862. { "sip:bob@1.2.3.4", "sip:bob@1.2.3.4", URI_CMP_MATCH },
  1863. /* Two identical IPv4 addresses represented differently. Match */
  1864. { "sip:bob@1.2.3.4", "sip:bob@001.002.003.004", URI_CMP_MATCH },
  1865. /* Logically equivalent IPv4 Address and hostname. No Match */
  1866. { "sip:bob@127.0.0.1", "sip:bob@localhost", URI_CMP_NOMATCH },
  1867. /* Logically equivalent IPv6 address and hostname. No Match */
  1868. { "sip:bob@[::1]", "sip:bob@localhost", URI_CMP_NOMATCH },
  1869. /* Try an IPv6 one as well */
  1870. { "sip:bob@[2001:db8::1234]", "sip:bob@[2001:db8::1234]", URI_CMP_MATCH },
  1871. /* Two identical IPv6 addresses represented differently. Match */
  1872. { "sip:bob@[2001:db8::1234]", "sip:bob@[2001:0db8::1234]", URI_CMP_MATCH },
  1873. /* Different ports. No match */
  1874. { "sip:bob@1.2.3.4:5060", "sip:bob@1.2.3.4:5061", URI_CMP_NOMATCH },
  1875. /* Same port logically, but only one address specifies it. No match */
  1876. { "sip:bob@1.2.3.4:5060", "sip:bob@1.2.3.4", URI_CMP_NOMATCH },
  1877. /* And for safety, try with IPv6 */
  1878. { "sip:bob@[2001:db8:1234]:5060", "sip:bob@[2001:db8:1234]", URI_CMP_NOMATCH },
  1879. /* User comparison is case sensitive. No match */
  1880. { "sip:bob@example.com", "sip:BOB@example.com", URI_CMP_NOMATCH },
  1881. /* Host comparison is case insensitive. Match */
  1882. { "sip:bob@example.com", "sip:bob@EXAMPLE.COM", URI_CMP_MATCH },
  1883. /* Add headers to the URI. Identical, so they match */
  1884. { "sip:bob@example.com?header1=value1&header2=value2", "sip:bob@example.com?header1=value1&header2=value2", URI_CMP_MATCH },
  1885. /* Headers in URI 1 are not in URI 2. No Match */
  1886. { "sip:bob@example.com?header1=value1&header2=value2", "sip:bob@example.com", URI_CMP_NOMATCH },
  1887. /* Header present in both URIs does not have matching values. No match */
  1888. { "sip:bob@example.com?header1=value1&header2=value2", "sip:bob@example.com?header1=value1&header2=value3", URI_CMP_NOMATCH },
  1889. /* Add parameters to the URI. Identical so they match */
  1890. { "sip:bob@example.com;param1=value1;param2=value2", "sip:bob@example.com;param1=value1;param2=value2", URI_CMP_MATCH },
  1891. /* Same parameters in both URIs but appear in different order. Match */
  1892. { "sip:bob@example.com;param2=value2;param1=value1", "sip:bob@example.com;param1=value1;param2=value2", URI_CMP_MATCH },
  1893. /* params in URI 1 are not in URI 2. Match */
  1894. { "sip:bob@example.com;param1=value1;param2=value2", "sip:bob@example.com", URI_CMP_MATCH },
  1895. /* param present in both URIs does not have matching values. No match */
  1896. { "sip:bob@example.com;param1=value1;param2=value2", "sip:bob@example.com;param1=value1;param2=value3", URI_CMP_NOMATCH },
  1897. /* URI 1 has a maddr param but URI 2 does not. No match */
  1898. { "sip:bob@example.com;param1=value1;maddr=192.168.0.1", "sip:bob@example.com;param1=value1", URI_CMP_NOMATCH },
  1899. /* URI 1 and URI 2 both have identical maddr params. Match */
  1900. { "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 },
  1901. /* URI 1 is a SIPS URI and URI 2 is a SIP URI. No Match */
  1902. { "sips:bob@example.com", "sip:bob@example.com", URI_CMP_NOMATCH },
  1903. /* No URI schemes. No match */
  1904. { "bob@example.com", "bob@example.com", URI_CMP_NOMATCH },
  1905. /* Crashiness tests. Just an address scheme. No match */
  1906. { "sip", "sips", URI_CMP_NOMATCH },
  1907. /* Still just an address scheme. Even though they're the same, No match */
  1908. { "sip", "sip", URI_CMP_NOMATCH },
  1909. /* Empty strings. No match */
  1910. { "", "", URI_CMP_NOMATCH },
  1911. /* An empty string and a NULL. No match */
  1912. { "", NULL, URI_CMP_NOMATCH },
  1913. };
  1914. int i;
  1915. int test_res = AST_TEST_PASS;
  1916. switch (cmd) {
  1917. case TEST_INIT:
  1918. info->name = "sip_uri_cmp_test";
  1919. info->category = "/channels/chan_sip/";
  1920. info->summary = "Tests comparison of SIP URIs";
  1921. info->description = "Several would-be tricky URI comparisons are performed";
  1922. return AST_TEST_NOT_RUN;
  1923. case TEST_EXECUTE:
  1924. break;
  1925. }
  1926. for (i = 0; i < ARRAY_LEN(uri_cmp_tests); ++i) {
  1927. int cmp_res1;
  1928. int cmp_res2;
  1929. if ((cmp_res1 = sip_uri_cmp(uri_cmp_tests[i].uri1, uri_cmp_tests[i].uri2))) {
  1930. /* URI comparison may return -1 or +1 depending on the failure. Standardize
  1931. * the return value to be URI_CMP_NOMATCH on any failure
  1932. */
  1933. cmp_res1 = URI_CMP_NOMATCH;
  1934. }
  1935. if (cmp_res1 != uri_cmp_tests[i].expected_result) {
  1936. ast_test_status_update(test, "Unexpected comparison result for URIs %s and %s. "
  1937. "Expected %s but got %s\n", uri_cmp_tests[i].uri1, uri_cmp_tests[i].uri2,
  1938. uri_cmp_tests[i].expected_result == URI_CMP_MATCH ? "Match" : "No Match",
  1939. cmp_res1 == URI_CMP_MATCH ? "Match" : "No Match");
  1940. test_res = AST_TEST_FAIL;
  1941. }
  1942. /* All URI comparisons are commutative, so for the sake of being thorough, we'll
  1943. * rerun the comparison with the parameters reversed
  1944. */
  1945. if ((cmp_res2 = sip_uri_cmp(uri_cmp_tests[i].uri2, uri_cmp_tests[i].uri1))) {
  1946. /* URI comparison may return -1 or +1 depending on the failure. Standardize
  1947. * the return value to be URI_CMP_NOMATCH on any failure
  1948. */
  1949. cmp_res2 = URI_CMP_NOMATCH;
  1950. }
  1951. if (cmp_res2 != uri_cmp_tests[i].expected_result) {
  1952. ast_test_status_update(test, "Unexpected comparison result for URIs %s and %s. "
  1953. "Expected %s but got %s\n", uri_cmp_tests[i].uri2, uri_cmp_tests[i].uri1,
  1954. uri_cmp_tests[i].expected_result == URI_CMP_MATCH ? "Match" : "No Match",
  1955. cmp_res2 == URI_CMP_MATCH ? "Match" : "No Match");
  1956. test_res = AST_TEST_FAIL;
  1957. }
  1958. }
  1959. return test_res;
  1960. }
  1961. void free_via(struct sip_via *v)
  1962. {
  1963. if (!v) {
  1964. return;
  1965. }
  1966. ast_free(v->via);
  1967. ast_free(v);
  1968. }
  1969. struct sip_via *parse_via(const char *header)
  1970. {
  1971. struct sip_via *v = ast_calloc(1, sizeof(*v));
  1972. char *via, *parm;
  1973. if (!v) {
  1974. return NULL;
  1975. }
  1976. v->via = ast_strdup(header);
  1977. v->ttl = 1;
  1978. via = v->via;
  1979. if (ast_strlen_zero(via)) {
  1980. ast_log(LOG_ERROR, "received request without a Via header\n");
  1981. free_via(v);
  1982. return NULL;
  1983. }
  1984. /* seperate the first via-parm */
  1985. via = strsep(&via, ",");
  1986. /* chop off sent-protocol */
  1987. v->protocol = strsep(&via, " \t\r\n");
  1988. if (ast_strlen_zero(v->protocol)) {
  1989. ast_log(LOG_ERROR, "missing sent-protocol in Via header\n");
  1990. free_via(v);
  1991. return NULL;
  1992. }
  1993. v->protocol = ast_skip_blanks(v->protocol);
  1994. if (via) {
  1995. via = ast_skip_blanks(via);
  1996. }
  1997. /* chop off sent-by */
  1998. v->sent_by = strsep(&via, "; \t\r\n");
  1999. if (ast_strlen_zero(v->sent_by)) {
  2000. ast_log(LOG_ERROR, "missing sent-by in Via header\n");
  2001. free_via(v);
  2002. return NULL;
  2003. }
  2004. v->sent_by = ast_skip_blanks(v->sent_by);
  2005. /* store the port, we have to handle ipv6 addresses containing ':'
  2006. * characters gracefully */
  2007. if (((parm = strchr(v->sent_by, ']')) && *(++parm) == ':') || (parm = strchr(v->sent_by, ':'))) {
  2008. char *endptr;
  2009. v->port = strtol(++parm, &endptr, 10);
  2010. }
  2011. /* evaluate any via-parms */
  2012. while ((parm = strsep(&via, "; \t\r\n"))) {
  2013. char *c;
  2014. if ((c = strstr(parm, "maddr="))) {
  2015. v->maddr = ast_skip_blanks(c + sizeof("maddr=") - 1);
  2016. } else if ((c = strstr(parm, "branch="))) {
  2017. v->branch = ast_skip_blanks(c + sizeof("branch=") - 1);
  2018. } else if ((c = strstr(parm, "ttl="))) {
  2019. char *endptr;
  2020. c = ast_skip_blanks(c + sizeof("ttl=") - 1);
  2021. v->ttl = strtol(c, &endptr, 10);
  2022. /* make sure we got a valid ttl value */
  2023. if (c == endptr) {
  2024. v->ttl = 1;
  2025. }
  2026. }
  2027. }
  2028. return v;
  2029. }
  2030. AST_TEST_DEFINE(parse_via_test)
  2031. {
  2032. int res = AST_TEST_PASS;
  2033. int i = 1;
  2034. struct sip_via *via;
  2035. struct testdata {
  2036. char *in;
  2037. char *expected_protocol;
  2038. char *expected_branch;
  2039. char *expected_sent_by;
  2040. char *expected_maddr;
  2041. unsigned int expected_port;
  2042. unsigned char expected_ttl;
  2043. int expected_null;
  2044. AST_LIST_ENTRY(testdata) list;
  2045. };
  2046. struct testdata *testdataptr;
  2047. static AST_LIST_HEAD_NOLOCK(testdataliststruct, testdata) testdatalist;
  2048. struct testdata t1 = {
  2049. .in = "SIP/2.0/UDP host:port;branch=thebranch",
  2050. .expected_protocol = "SIP/2.0/UDP",
  2051. .expected_sent_by = "host:port",
  2052. .expected_branch = "thebranch",
  2053. };
  2054. struct testdata t2 = {
  2055. .in = "SIP/2.0/UDP host:port",
  2056. .expected_protocol = "SIP/2.0/UDP",
  2057. .expected_sent_by = "host:port",
  2058. .expected_branch = "",
  2059. };
  2060. struct testdata t3 = {
  2061. .in = "SIP/2.0/UDP",
  2062. .expected_null = 1,
  2063. };
  2064. struct testdata t4 = {
  2065. .in = "BLAH/BLAH/BLAH host:port;branch=",
  2066. .expected_protocol = "BLAH/BLAH/BLAH",
  2067. .expected_sent_by = "host:port",
  2068. .expected_branch = "",
  2069. };
  2070. struct testdata t5 = {
  2071. .in = "SIP/2.0/UDP host:5060;branch=thebranch;maddr=224.0.0.1;ttl=1",
  2072. .expected_protocol = "SIP/2.0/UDP",
  2073. .expected_sent_by = "host:5060",
  2074. .expected_port = 5060,
  2075. .expected_branch = "thebranch",
  2076. .expected_maddr = "224.0.0.1",
  2077. .expected_ttl = 1,
  2078. };
  2079. struct testdata t6 = {
  2080. .in = "SIP/2.0/UDP host:5060;\n branch=thebranch;\r\n maddr=224.0.0.1; ttl=1",
  2081. .expected_protocol = "SIP/2.0/UDP",
  2082. .expected_sent_by = "host:5060",
  2083. .expected_port = 5060,
  2084. .expected_branch = "thebranch",
  2085. .expected_maddr = "224.0.0.1",
  2086. .expected_ttl = 1,
  2087. };
  2088. struct testdata t7 = {
  2089. .in = "SIP/2.0/UDP [::1]:5060",
  2090. .expected_protocol = "SIP/2.0/UDP",
  2091. .expected_sent_by = "[::1]:5060",
  2092. .expected_port = 5060,
  2093. .expected_branch = "",
  2094. };
  2095. switch (cmd) {
  2096. case TEST_INIT:
  2097. info->name = "parse_via_test";
  2098. info->category = "/channels/chan_sip/";
  2099. info->summary = "Tests parsing the Via header";
  2100. info->description =
  2101. "Runs through various test situations in which various "
  2102. " parameters parameter must be extracted from a VIA header";
  2103. return AST_TEST_NOT_RUN;
  2104. case TEST_EXECUTE:
  2105. break;
  2106. }
  2107. AST_LIST_HEAD_SET_NOLOCK(&testdatalist, &t1);
  2108. AST_LIST_INSERT_TAIL(&testdatalist, &t2, list);
  2109. AST_LIST_INSERT_TAIL(&testdatalist, &t3, list);
  2110. AST_LIST_INSERT_TAIL(&testdatalist, &t4, list);
  2111. AST_LIST_INSERT_TAIL(&testdatalist, &t5, list);
  2112. AST_LIST_INSERT_TAIL(&testdatalist, &t6, list);
  2113. AST_LIST_INSERT_TAIL(&testdatalist, &t7, list);
  2114. AST_LIST_TRAVERSE(&testdatalist, testdataptr, list) {
  2115. via = parse_via(testdataptr->in);
  2116. if (!via) {
  2117. if (!testdataptr->expected_null) {
  2118. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2119. "failed to parse header\n",
  2120. i, testdataptr->in);
  2121. res = AST_TEST_FAIL;
  2122. }
  2123. i++;
  2124. continue;
  2125. }
  2126. if (testdataptr->expected_null) {
  2127. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2128. "successfully parased invalid via header\n",
  2129. i, testdataptr->in);
  2130. res = AST_TEST_FAIL;
  2131. free_via(via);
  2132. i++;
  2133. continue;
  2134. }
  2135. if ((ast_strlen_zero(via->protocol) && !ast_strlen_zero(testdataptr->expected_protocol))
  2136. || (!ast_strlen_zero(via->protocol) && strcmp(via->protocol, testdataptr->expected_protocol))) {
  2137. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2138. "parsed protocol = \"%s\"\n"
  2139. "expected = \"%s\"\n"
  2140. "failed to parse protocol\n",
  2141. i, testdataptr->in, via->protocol, testdataptr->expected_protocol);
  2142. res = AST_TEST_FAIL;
  2143. }
  2144. if ((ast_strlen_zero(via->sent_by) && !ast_strlen_zero(testdataptr->expected_sent_by))
  2145. || (!ast_strlen_zero(via->sent_by) && strcmp(via->sent_by, testdataptr->expected_sent_by))) {
  2146. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2147. "parsed sent_by = \"%s\"\n"
  2148. "expected = \"%s\"\n"
  2149. "failed to parse sent-by\n",
  2150. i, testdataptr->in, via->sent_by, testdataptr->expected_sent_by);
  2151. res = AST_TEST_FAIL;
  2152. }
  2153. if (testdataptr->expected_port && testdataptr->expected_port != via->port) {
  2154. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2155. "parsed port = \"%d\"\n"
  2156. "expected = \"%d\"\n"
  2157. "failed to parse port\n",
  2158. i, testdataptr->in, via->port, testdataptr->expected_port);
  2159. res = AST_TEST_FAIL;
  2160. }
  2161. if ((ast_strlen_zero(via->branch) && !ast_strlen_zero(testdataptr->expected_branch))
  2162. || (!ast_strlen_zero(via->branch) && strcmp(via->branch, testdataptr->expected_branch))) {
  2163. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2164. "parsed branch = \"%s\"\n"
  2165. "expected = \"%s\"\n"
  2166. "failed to parse branch\n",
  2167. i, testdataptr->in, via->branch, testdataptr->expected_branch);
  2168. res = AST_TEST_FAIL;
  2169. }
  2170. if ((ast_strlen_zero(via->maddr) && !ast_strlen_zero(testdataptr->expected_maddr))
  2171. || (!ast_strlen_zero(via->maddr) && strcmp(via->maddr, testdataptr->expected_maddr))) {
  2172. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2173. "parsed maddr = \"%s\"\n"
  2174. "expected = \"%s\"\n"
  2175. "failed to parse maddr\n",
  2176. i, testdataptr->in, via->maddr, testdataptr->expected_maddr);
  2177. res = AST_TEST_FAIL;
  2178. }
  2179. if (testdataptr->expected_ttl && testdataptr->expected_ttl != via->ttl) {
  2180. ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
  2181. "parsed ttl = \"%d\"\n"
  2182. "expected = \"%d\"\n"
  2183. "failed to parse ttl\n",
  2184. i, testdataptr->in, via->ttl, testdataptr->expected_ttl);
  2185. res = AST_TEST_FAIL;
  2186. }
  2187. free_via(via);
  2188. i++;
  2189. }
  2190. return res;
  2191. }
  2192. void sip_request_parser_register_tests(void)
  2193. {
  2194. AST_TEST_REGISTER(get_calleridname_test);
  2195. AST_TEST_REGISTER(sip_parse_uri_test);
  2196. AST_TEST_REGISTER(get_in_brackets_test);
  2197. AST_TEST_REGISTER(get_name_and_number_test);
  2198. AST_TEST_REGISTER(sip_parse_uri_full_test);
  2199. AST_TEST_REGISTER(parse_name_andor_addr_test);
  2200. AST_TEST_REGISTER(parse_contact_header_test);
  2201. AST_TEST_REGISTER(sip_parse_options_test);
  2202. AST_TEST_REGISTER(sip_uri_cmp_test);
  2203. AST_TEST_REGISTER(parse_via_test);
  2204. }
  2205. void sip_request_parser_unregister_tests(void)
  2206. {
  2207. AST_TEST_UNREGISTER(sip_parse_uri_test);
  2208. AST_TEST_UNREGISTER(get_calleridname_test);
  2209. AST_TEST_UNREGISTER(get_in_brackets_test);
  2210. AST_TEST_UNREGISTER(get_name_and_number_test);
  2211. AST_TEST_UNREGISTER(sip_parse_uri_full_test);
  2212. AST_TEST_UNREGISTER(parse_name_andor_addr_test);
  2213. AST_TEST_UNREGISTER(parse_contact_header_test);
  2214. AST_TEST_UNREGISTER(sip_parse_options_test);
  2215. AST_TEST_UNREGISTER(sip_uri_cmp_test);
  2216. AST_TEST_UNREGISTER(parse_via_test);
  2217. }
  2218. int sip_reqresp_parser_init(void)
  2219. {
  2220. #ifdef HAVE_XLOCALE_H
  2221. c_locale = newlocale(LC_CTYPE_MASK, "C", NULL);
  2222. if (!c_locale) {
  2223. return -1;
  2224. }
  2225. #endif
  2226. return 0;
  2227. }
  2228. void sip_reqresp_parser_exit(void)
  2229. {
  2230. #ifdef HAVE_XLOCALE_H
  2231. if (c_locale) {
  2232. freelocale(c_locale);
  2233. c_locale = NULL;
  2234. }
  2235. #endif
  2236. }