reqresp_parser.c 76 KB

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