test_nta.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@internal
  25. * @CFILE test_nta.c
  26. *
  27. * Test functions for NTA.
  28. *
  29. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  30. *
  31. * @date Created: Tue Aug 21 15:18:26 2001 ppessi
  32. */
  33. #include "config.h"
  34. typedef struct agent_t agent_t;
  35. typedef struct client_t client_t;
  36. #define SU_ROOT_MAGIC_T agent_t
  37. #include <sofia-sip/su_wait.h>
  38. #include <msg_internal.h>
  39. #define NTA_AGENT_MAGIC_T agent_t
  40. #define NTA_LEG_MAGIC_T agent_t
  41. #define NTA_OUTGOING_MAGIC_T client_t
  42. #define NTA_INCOMING_MAGIC_T agent_t
  43. #define NTA_RELIABLE_MAGIC_T agent_t
  44. #include "sofia-sip/nta.h"
  45. #include "sofia-sip/nta_tport.h"
  46. #include <sofia-sip/sip_header.h>
  47. #include <sofia-sip/sip_tag.h>
  48. #include <sofia-sip/sip_status.h>
  49. #include <sofia-sip/tport.h>
  50. #include <sofia-sip/htable.h>
  51. #include <sofia-sip/sresolv.h>
  52. #include <sofia-sip/su_log.h>
  53. #include <sofia-sip/sofia_features.h>
  54. #include <sofia-sip/hostdomain.h>
  55. #include <sofia-sip/tport.h>
  56. #include <sofia-sip/su_string.h>
  57. #include <stddef.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <limits.h>
  61. #include <stdio.h>
  62. #include <assert.h>
  63. #include <unistd.h>
  64. #include "s2util.h"
  65. #if HAVE_OPEN_C
  66. #include <sys/param.h>
  67. #endif
  68. SOFIAPUBVAR su_log_t nta_log[];
  69. SOFIAPUBVAR su_log_t tport_log[];
  70. int tstflags = 0;
  71. #define TSTFLAGS tstflags
  72. #include <sofia-sip/tstdef.h>
  73. #if HAVE_FUNC
  74. #elif HAVE_FUNCTION
  75. #define __func__ __FUNCTION__
  76. #else
  77. #define __func__ name
  78. #endif
  79. #define NONE ((void *)-1)
  80. int expensive_checks, test_nta_sips;
  81. #define EXPENSIVE_CHECKS (expensive_checks)
  82. struct sigcomp_compartment;
  83. char const name[] = "test_nta";
  84. typedef struct invite_client_t invite_client_t;
  85. typedef int client_check_f(client_t *, nta_outgoing_t *, sip_t const *);
  86. typedef int client_deinit_f(client_t *);
  87. struct client_t {
  88. agent_t *c_ag;
  89. char const *c_name;
  90. client_check_f * c_check;
  91. client_check_f * const * c_checks;
  92. client_deinit_f * c_deinit;
  93. void *c_extra;
  94. nta_outgoing_t *c_orq;
  95. int c_status;
  96. int c_final;
  97. int c_errors;
  98. };
  99. struct invite_client_t {
  100. client_t ic_client[1];
  101. nta_outgoing_t *ic_orq; /* Original INVITE transaction */
  102. int ic_tag_status; /* Status for current branch */
  103. char *ic_tag;
  104. };
  105. struct agent_t {
  106. su_home_t ag_home[1];
  107. int ag_flags;
  108. su_root_t *ag_root;
  109. msg_mclass_t *ag_mclass;
  110. nta_agent_t *ag_agent;
  111. url_string_t *ag_obp; /**< Outbound proxy. */
  112. nta_leg_t *ag_server_leg; /**< Leg for sip:%@% */
  113. nta_leg_t *ag_default_leg; /**< Leg for rest */
  114. unsigned ag_drop;
  115. nta_outgoing_t *ag_orq;
  116. unsigned ag_running :1, ag_canceled:1, ag_acked:1, :0;
  117. char const *ag_comp;
  118. struct sigcomp_compartment *ag_client_compartment;
  119. /* Server side */
  120. int ag_response; /**< What we answer by default */
  121. nta_incoming_t *ag_irq;
  122. struct sigcomp_compartment *ag_server_compartment;
  123. char const *ag_m;
  124. sip_contact_t const *ag_contact;
  125. sip_from_t *ag_alice;
  126. sip_to_t *ag_bob;
  127. sip_contact_t *ag_m_alice;
  128. sip_contact_t *ag_m_bob;
  129. sip_contact_t *ag_aliases;
  130. nta_leg_t *ag_alice_leg;
  131. nta_leg_t *ag_bob_leg;
  132. msg_t *ag_request;
  133. nta_leg_t *ag_expect_leg;
  134. nta_leg_t *ag_latest_leg;
  135. nta_leg_t *ag_call_leg;
  136. nta_reliable_t *ag_reliable;
  137. sip_via_t *ag_in_via; /**< Incoming via */
  138. sip_content_type_t *ag_content_type;
  139. sip_payload_t *ag_payload;
  140. msg_t *ag_probe_msg;
  141. su_sockaddr_t ag_su_nta[1];
  142. socklen_t ag_su_nta_len;
  143. /* Dummy servers */
  144. char const *ag_sink_port;
  145. su_socket_t ag_sink_socket, ag_down_socket;
  146. su_wait_t ag_sink_wait[1];
  147. };
  148. static int test_init(agent_t *ag, char const *resolv_conf);
  149. static int test_deinit(agent_t *ag);
  150. static int test_bad_messages(agent_t *ag);
  151. static int test_routing(agent_t *ag);
  152. static int test_tports(agent_t *ag);
  153. static int test_resolv(agent_t *ag, char const *resolv_conf);
  154. static int test_dialog(agent_t *ag);
  155. static int test_call(agent_t *ag);
  156. static int test_prack(agent_t *ag);
  157. static int test_fix_467(agent_t *ag);
  158. static int test_for_ack(agent_t *ag,
  159. nta_incoming_t *irq,
  160. sip_t const *sip);
  161. static int test_for_ack_or_timeout(agent_t *ag,
  162. nta_incoming_t *irq,
  163. sip_t const *sip);
  164. static int wait_for_ack_or_cancel(agent_t *ag,
  165. nta_incoming_t *irq,
  166. sip_t const *sip);
  167. int agent_callback(agent_t *ag,
  168. nta_agent_t *nta,
  169. msg_t *msg,
  170. sip_t *sip)
  171. {
  172. if (tstflags & tst_verbatim) {
  173. if (sip->sip_request) {
  174. printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
  175. name, __func__, sip->sip_request->rq_method_name,
  176. URL_PRINT_ARGS(sip->sip_request->rq_url),
  177. sip->sip_request->rq_version);
  178. }
  179. else {
  180. printf("%s: %s: %s %03d %s\n", name, __func__,
  181. sip->sip_status->st_version,
  182. sip->sip_status->st_status,
  183. sip->sip_status->st_phrase);
  184. }
  185. }
  186. msg_destroy(msg);
  187. return 0;
  188. }
  189. static
  190. void leg_match(agent_t *ag, nta_leg_t *leg, int always, char const *func)
  191. {
  192. char const *match = "unknown leg";
  193. if (!always && (tstflags & tst_verbatim) != tst_verbatim)
  194. return;
  195. if (leg == ag->ag_default_leg)
  196. match = "ag_default_leg";
  197. else if (leg == ag->ag_server_leg)
  198. match = "ag_server_leg";
  199. else if (leg == ag->ag_alice_leg)
  200. match = "ag_alice_leg";
  201. else if (leg == ag->ag_bob_leg)
  202. match = "ag_bob_leg";
  203. printf("%s: %s: %smatched with %s\n", name, func,
  204. always ? "mis" : "", match);
  205. }
  206. static
  207. void leg_zap(agent_t *ag, nta_leg_t *leg)
  208. {
  209. if (leg == ag->ag_default_leg)
  210. ag->ag_default_leg = NULL;
  211. else if (leg == ag->ag_server_leg)
  212. ag->ag_server_leg = NULL;
  213. else if (leg == ag->ag_alice_leg)
  214. ag->ag_alice_leg = NULL;
  215. else if (leg == ag->ag_bob_leg)
  216. ag->ag_bob_leg = NULL;
  217. else
  218. printf("%s:%u: %s: did not exist\n",
  219. __FILE__, __LINE__, __func__);
  220. nta_leg_destroy(leg);
  221. }
  222. int leg_callback_200(agent_t *ag,
  223. nta_leg_t *leg,
  224. nta_incoming_t *irq,
  225. sip_t const *sip)
  226. {
  227. if (tstflags & tst_verbatim) {
  228. printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
  229. name, __func__, sip->sip_request->rq_method_name,
  230. URL_PRINT_ARGS(sip->sip_request->rq_url),
  231. sip->sip_request->rq_version);
  232. }
  233. if (!sip->sip_content_length ||
  234. !sip->sip_via ||
  235. !sip->sip_from || !sip->sip_from->a_tag)
  236. return 500;
  237. if (ag->ag_in_via == NULL)
  238. ag->ag_in_via = sip_via_dup(ag->ag_home, sip->sip_via);
  239. if (ag->ag_request == NULL)
  240. ag->ag_request = nta_incoming_getrequest(irq);
  241. ag->ag_latest_leg = leg;
  242. if (ag->ag_expect_leg && leg != ag->ag_expect_leg) {
  243. leg_match(ag, leg, 1, __func__);
  244. return 500;
  245. }
  246. leg_match(ag, leg, 0, __func__);
  247. if (sip->sip_request->rq_method == sip_method_bye) {
  248. leg_zap(ag, leg);
  249. }
  250. return 200;
  251. }
  252. int leg_callback_500(agent_t *ag,
  253. nta_leg_t *leg,
  254. nta_incoming_t *irq,
  255. sip_t const *sip)
  256. {
  257. if (tstflags & tst_verbatim) {
  258. printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
  259. name, __func__, sip->sip_request->rq_method_name,
  260. URL_PRINT_ARGS(sip->sip_request->rq_url),
  261. sip->sip_request->rq_version);
  262. }
  263. return 500;
  264. }
  265. int new_leg_callback_200(agent_t *ag,
  266. nta_leg_t *leg,
  267. nta_incoming_t *irq,
  268. sip_t const *sip)
  269. {
  270. if (tstflags & tst_verbatim) {
  271. printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
  272. name, __func__, sip->sip_request->rq_method_name,
  273. URL_PRINT_ARGS(sip->sip_request->rq_url),
  274. sip->sip_request->rq_version);
  275. }
  276. if (!sip->sip_content_length ||
  277. !sip->sip_via ||
  278. !sip->sip_from || !sip->sip_from->a_tag)
  279. return 500;
  280. ag->ag_latest_leg = leg;
  281. if (ag->ag_expect_leg && leg != ag->ag_expect_leg) {
  282. leg_match(ag, leg, 1, __func__);
  283. return 500;
  284. }
  285. leg_match(ag, leg, 0, __func__);
  286. ag->ag_bob_leg = nta_leg_tcreate(ag->ag_agent,
  287. leg_callback_200,
  288. ag,
  289. URLTAG_URL(sip->sip_request->rq_url),
  290. SIPTAG_CALL_ID(sip->sip_call_id),
  291. SIPTAG_FROM(sip->sip_to),
  292. SIPTAG_TO(sip->sip_from),
  293. TAG_END());
  294. if (!ag->ag_bob_leg ||
  295. !nta_leg_tag(ag->ag_bob_leg, NULL) ||
  296. !nta_leg_get_tag(ag->ag_bob_leg) ||
  297. !nta_incoming_tag(irq, nta_leg_get_tag(ag->ag_bob_leg)))
  298. return 500;
  299. return 200;
  300. }
  301. int new_leg_callback_180(agent_t *ag,
  302. nta_leg_t *leg,
  303. nta_incoming_t *irq,
  304. sip_t const *sip)
  305. {
  306. int status = new_leg_callback_200(ag, leg, irq, sip);
  307. if (status == 200) {
  308. ag->ag_irq = irq;
  309. status = 180;
  310. }
  311. return status;
  312. }
  313. static client_check_f client_check_to_tag;
  314. static client_check_f * const default_checks[] = {
  315. client_check_to_tag,
  316. NULL
  317. };
  318. static client_check_f * const no_default_checks[] = {
  319. NULL
  320. };
  321. /** Callback from client transaction */
  322. int outgoing_callback(client_t *ctx,
  323. nta_outgoing_t *orq,
  324. sip_t const *sip)
  325. {
  326. agent_t *ag = ctx->c_ag;
  327. int status = nta_outgoing_status(orq);
  328. client_check_f * const *checks;
  329. if (tstflags & tst_verbatim) {
  330. if (sip)
  331. printf("%s: %s: response %s %03d %s\n", name, ctx->c_name,
  332. sip->sip_status->st_version,
  333. sip->sip_status->st_status,
  334. sip->sip_status->st_phrase);
  335. else
  336. printf("%s: %s: callback %03d\n", name, ctx->c_name,
  337. status);
  338. }
  339. if (status >= 200 && ag->ag_comp) { /* XXX */
  340. nta_compartment_decref(&ag->ag_client_compartment);
  341. ag->ag_client_compartment = nta_outgoing_compartment(ctx->c_orq);
  342. }
  343. if (status > ctx->c_status)
  344. ctx->c_status = status;
  345. if (status >= 200)
  346. ctx->c_final = 1;
  347. if (ctx->c_check && ctx->c_check(ctx, orq, sip))
  348. ctx->c_errors++;
  349. checks = ctx->c_checks;
  350. for (checks = checks ? checks : default_checks; *checks; checks++)
  351. if ((*checks)(ctx, ctx->c_orq, sip))
  352. ctx->c_errors++;
  353. return 0;
  354. }
  355. /** Deinit client. Return nonzero if client checks failed. */
  356. static
  357. int client_deinit(client_t *c)
  358. {
  359. int errors = c->c_errors;
  360. if (c->c_deinit && c->c_deinit(c))
  361. errors++;
  362. if (c->c_orq) nta_outgoing_destroy(c->c_orq), c->c_orq = NULL;
  363. c->c_errors = 0;
  364. c->c_status = 0;
  365. return errors;
  366. }
  367. static
  368. void nta_test_run(agent_t *ag)
  369. {
  370. for (ag->ag_running = 1; ag->ag_running;) {
  371. if (tstflags & tst_verbatim) {
  372. fputs(".", stdout); fflush(stdout);
  373. }
  374. su_root_step(ag->ag_root, 500L);
  375. }
  376. }
  377. /** Run client test. Return nonzero if client checks failed. */
  378. static
  379. int client_run_with(client_t *c, int expected, void (*runner)(client_t *c))
  380. {
  381. int resulting;
  382. TEST_1(c->c_orq != NULL);
  383. runner(c);
  384. resulting = c->c_status;
  385. if (client_deinit(c))
  386. return 1;
  387. if (expected)
  388. TEST(resulting, expected);
  389. return 0;
  390. }
  391. static
  392. void until_final_received(client_t *c)
  393. {
  394. for (c->c_final = 0; !c->c_final; ) {
  395. if (tstflags & tst_verbatim) {
  396. fputs(".", stdout); fflush(stdout);
  397. }
  398. su_root_step(c->c_ag->ag_root, 500L);
  399. }
  400. }
  401. static
  402. void fast_final_received(client_t *c)
  403. {
  404. for (c->c_final = 0; !c->c_final; ) {
  405. if (tstflags & tst_verbatim) {
  406. fputs(".", stdout); fflush(stdout);
  407. }
  408. s2_fast_forward(500, c->c_ag->ag_root);
  409. }
  410. }
  411. static
  412. int client_run(client_t *c, int expected)
  413. {
  414. return client_run_with(c, expected, until_final_received);
  415. }
  416. static
  417. void until_server_acked(client_t *c)
  418. {
  419. agent_t *ag = c->c_ag;
  420. for (ag->ag_acked = 0; !ag->ag_acked;) {
  421. if (tstflags & tst_verbatim) {
  422. fputs(".", stdout); fflush(stdout);
  423. }
  424. su_root_step(ag->ag_root, 500L);
  425. }
  426. }
  427. static
  428. int client_run_until_acked(client_t *c, int expected)
  429. {
  430. return client_run_with(c, expected, until_server_acked);
  431. }
  432. void
  433. until_server_canceled(client_t *c)
  434. {
  435. agent_t *ag = c->c_ag;
  436. for (ag->ag_canceled = 0; !ag->ag_canceled || c->c_status < 200;) {
  437. if (tstflags & tst_verbatim) {
  438. fputs(".", stdout); fflush(stdout);
  439. }
  440. su_root_step(ag->ag_root, 500L);
  441. }
  442. }
  443. static
  444. int client_run_until_canceled(client_t *c, int expected)
  445. {
  446. return client_run_with(c, expected, until_server_canceled);
  447. }
  448. #include <sofia-sip/msg_mclass.h>
  449. int test_init(agent_t *ag, char const *resolv_conf)
  450. {
  451. char const *contact = "sip:*:*;comp=sigcomp";
  452. su_sockaddr_t su;
  453. socklen_t sulen, sulen0;
  454. su_socket_t s;
  455. int af, err = -1;
  456. BEGIN();
  457. ag->ag_root = su_root_create(ag);
  458. TEST_1(ag->ag_root);
  459. ag->ag_mclass = msg_mclass_clone(sip_default_mclass(), 0, 0);
  460. TEST_1(ag->ag_mclass);
  461. #if SU_HAVE_IN6
  462. if (su_strmatch(getenv("ipv6"), "true")) {
  463. contact = "sip:[::]:*;comp=sigcomp";
  464. af = AF_INET6, sulen0 = sizeof (struct sockaddr_in6);
  465. }
  466. else {
  467. af = AF_INET, sulen0 = sizeof (struct sockaddr_in);
  468. contact = "sip:0.0.0.0:*;comp=sigcomp";
  469. }
  470. #else
  471. af = AF_INET, sulen0 = sizeof (struct sockaddr_in);
  472. contact = "sip:0.0.0.0:*;comp=sigcomp";
  473. #endif
  474. if (ag->ag_m)
  475. contact = ag->ag_m;
  476. else if (getenv("SIPCONTACT"))
  477. contact = getenv("SIPCONTACT");
  478. /* Sink server */
  479. s = su_socket(af, SOCK_DGRAM, 0); TEST_1(s != INVALID_SOCKET);
  480. memset(&su, 0, sulen = sulen0);
  481. su.su_family = af;
  482. if (getenv("sink")) {
  483. su.su_port = htons(atoi(getenv("sink")));
  484. }
  485. TEST_1(bind(s, &su.su_sa, sulen) < 0 ? (perror("bind"), 0) : 1);
  486. TEST_1(getsockname(s, &su.su_sa, &sulen) == 0);
  487. ag->ag_sink_socket = s;
  488. su_wait_init(ag->ag_sink_wait);
  489. su_wait_create(ag->ag_sink_wait, ag->ag_sink_socket, SU_WAIT_IN);
  490. ag->ag_sink_port = su_sprintf(ag->ag_home, "%u", ntohs(su.su_sin.sin_port));
  491. /* Down server */
  492. s = su_socket(af, SOCK_STREAM, 0); TEST_1(s != INVALID_SOCKET);
  493. memset(&su, 0, sulen = sulen0);
  494. su.su_family = af;
  495. if (getenv("down")) {
  496. su.su_port = htons(atoi(getenv("down")));
  497. }
  498. TEST_1(bind(s, &su.su_sa, sulen) < 0 ? (perror("bind"), 0) : 1);
  499. ag->ag_down_socket = s;
  500. /* Create agent */
  501. ag->ag_agent = nta_agent_create(ag->ag_root,
  502. (url_string_t *)contact,
  503. NULL,
  504. NULL,
  505. NTATAG_MCLASS(ag->ag_mclass),
  506. NTATAG_USE_TIMESTAMP(1),
  507. SRESTAG_RESOLV_CONF(resolv_conf),
  508. NTATAG_USE_NAPTR(0),
  509. NTATAG_USE_SRV(0),
  510. NTATAG_PRELOAD(2048),
  511. TAG_END());
  512. TEST_1(ag->ag_agent);
  513. contact = getenv("SIPSCONTACT");
  514. if (contact) {
  515. if (nta_agent_add_tport(ag->ag_agent, URL_STRING_MAKE(contact),
  516. TPTAG_CERTIFICATE(getenv("TEST_NTA_CERTDIR")),
  517. TPTAG_TLS_VERIFY_POLICY(TPTLS_VERIFY_NONE),
  518. TAG_END()) == 0)
  519. test_nta_sips = 1;
  520. }
  521. {
  522. /* Initialize our headers */
  523. sip_from_t from[1];
  524. sip_to_t to[1];
  525. sip_contact_t m[1];
  526. su_sockaddr_t *su = ag->ag_su_nta;
  527. sip_from_init(from);
  528. sip_to_init(to);
  529. sip_contact_init(m);
  530. TEST_1(ag->ag_contact = nta_agent_contact(ag->ag_agent));
  531. *m->m_url = *ag->ag_contact->m_url;
  532. if (host_is_ip4_address(m->m_url->url_host)) {
  533. su_inet_pton(su->su_family = AF_INET,
  534. m->m_url->url_host,
  535. &su->su_sin.sin_addr);
  536. ag->ag_su_nta_len = (sizeof su->su_sin);
  537. }
  538. else {
  539. TEST_1(host_is_ip_address(m->m_url->url_host));
  540. su_inet_pton(su->su_family = AF_INET6,
  541. m->m_url->url_host,
  542. &su->su_sin6.sin6_addr);
  543. ag->ag_su_nta_len = (sizeof su->su_sin6);
  544. }
  545. su->su_port = htons(5060);
  546. if (m->m_url->url_port && strlen(m->m_url->url_port)) {
  547. unsigned long port = strtoul(m->m_url->url_port, NULL, 10);
  548. su->su_port = htons(port);
  549. }
  550. TEST_1(su->su_port != 0);
  551. m->m_url->url_user = "bob";
  552. TEST_1(ag->ag_m_bob = sip_contact_dup(ag->ag_home, m));
  553. to->a_display = "Bob";
  554. *to->a_url = *ag->ag_contact->m_url;
  555. to->a_url->url_user = "bob";
  556. to->a_url->url_port = NULL;
  557. TEST_1(ag->ag_bob = sip_to_dup(ag->ag_home, to));
  558. *m->m_url = *ag->ag_contact->m_url;
  559. m->m_url->url_user = "alice";
  560. TEST_1(ag->ag_m_alice = sip_contact_dup(ag->ag_home, m));
  561. from->a_display = "Alice";
  562. *from->a_url = *ag->ag_contact->m_url;
  563. from->a_url->url_user = "alice";
  564. from->a_url->url_port = NULL;
  565. TEST_1(ag->ag_alice = sip_from_dup(ag->ag_home, from));
  566. }
  567. {
  568. char const data[] =
  569. "v=0\r\n"
  570. "o=- 425432 423412 IN IP4 127.0.0.1\r\n"
  571. "s= \r\n"
  572. "c=IN IP4 127.0.0.1\r\n"
  573. "m=5004 audio 8 0\r\n";
  574. ag->ag_content_type = sip_content_type_make(ag->ag_home, "application/sdp");
  575. ag->ag_payload = sip_payload_make(ag->ag_home, data);
  576. }
  577. {
  578. sip_contact_t *m;
  579. ag->ag_aliases =
  580. sip_contact_make(ag->ag_home, "sip:127.0.0.1, sip:localhost, sip:[::1]");
  581. TEST_1(ag->ag_aliases);
  582. TEST_1(ag->ag_aliases->m_next);
  583. TEST_1(ag->ag_aliases->m_next->m_next);
  584. TEST_P(ag->ag_aliases->m_next->m_next->m_next, NULL);
  585. for (m = ag->ag_aliases; m; m = m->m_next)
  586. m->m_url->url_port = ag->ag_contact->m_url->url_port;
  587. TEST_1(m = sip_contact_dup(ag->ag_home, ag->ag_contact));
  588. m->m_next = ag->ag_aliases;
  589. ag->ag_aliases = m;
  590. err = nta_agent_set_params(ag->ag_agent,
  591. NTATAG_ALIASES(ag->ag_aliases),
  592. NTATAG_REL100(1),
  593. NTATAG_UA(1),
  594. NTATAG_MERGE_482(1),
  595. NTATAG_USE_NAPTR(1),
  596. NTATAG_USE_SRV(1),
  597. NTATAG_MAX_FORWARDS(20),
  598. TAG_END());
  599. TEST(err, 7);
  600. err = nta_agent_set_params(ag->ag_agent,
  601. NTATAG_ALIASES(ag->ag_aliases),
  602. NTATAG_DEFAULT_PROXY("sip:127.0.0.1"),
  603. TAG_END());
  604. TEST(err, 2);
  605. err = nta_agent_set_params(ag->ag_agent,
  606. NTATAG_ALIASES(ag->ag_aliases),
  607. NTATAG_DEFAULT_PROXY(NULL),
  608. TAG_END());
  609. TEST(err, 2);
  610. err = nta_agent_set_params(ag->ag_agent,
  611. NTATAG_DEFAULT_PROXY("tel:+35878008000"),
  612. TAG_END());
  613. TEST(err, -1);
  614. }
  615. {
  616. url_t url[1];
  617. /* Create the server leg */
  618. *url = *ag->ag_aliases->m_url;
  619. url->url_user = "%";
  620. ag->ag_server_leg = nta_leg_tcreate(ag->ag_agent,
  621. leg_callback_200,
  622. ag,
  623. NTATAG_NO_DIALOG(1),
  624. URLTAG_URL(url),
  625. TAG_END());
  626. TEST_1(ag->ag_server_leg);
  627. }
  628. END();
  629. }
  630. int test_reinit(agent_t *ag)
  631. {
  632. BEGIN();
  633. /* Create a new default leg */
  634. nta_leg_destroy(ag->ag_default_leg), ag->ag_default_leg = NULL;
  635. TEST_1(ag->ag_default_leg = nta_leg_tcreate(ag->ag_agent,
  636. leg_callback_200,
  637. ag,
  638. NTATAG_NO_DIALOG(1),
  639. TAG_END()));
  640. END();
  641. }
  642. int test_deinit(agent_t *ag)
  643. {
  644. BEGIN();
  645. if (ag->ag_request) msg_destroy(ag->ag_request), ag->ag_request = NULL;
  646. su_free(ag->ag_home, ag->ag_in_via), ag->ag_in_via = NULL;
  647. nta_leg_destroy(ag->ag_alice_leg);
  648. nta_leg_destroy(ag->ag_bob_leg);
  649. nta_leg_destroy(ag->ag_default_leg);
  650. nta_leg_destroy(ag->ag_server_leg);
  651. nta_agent_destroy(ag->ag_agent);
  652. su_root_destroy(ag->ag_root);
  653. if (ag->ag_sink_port) {
  654. su_free(ag->ag_home, (void *)ag->ag_sink_port), ag->ag_sink_port = NULL;
  655. su_wait_destroy(ag->ag_sink_wait);
  656. su_close(ag->ag_sink_socket);
  657. }
  658. free(ag->ag_mclass), ag->ag_mclass = NULL;
  659. END();
  660. }
  661. static
  662. int readfile(FILE *f, void **contents)
  663. {
  664. /* Read in whole (binary!) file */
  665. char *buffer = NULL;
  666. long size;
  667. size_t len;
  668. /* Read whole file in */
  669. if (fseek(f, 0, SEEK_END) < 0 ||
  670. (size = ftell(f)) < 0 ||
  671. fseek(f, 0, SEEK_SET) < 0 ||
  672. (long)(len = (size_t)size) != size) {
  673. fprintf(stderr, "%s: unable to determine file size (%s)\n",
  674. __func__, strerror(errno));
  675. return -1;
  676. }
  677. if (!(buffer = malloc(len + 2)) ||
  678. fread(buffer, 1, len, f) != len) {
  679. fprintf(stderr, "%s: unable to read file (%s)\n", __func__, strerror(errno));
  680. if (buffer)
  681. free(buffer);
  682. return -1;
  683. }
  684. buffer[len] = '\0';
  685. *contents = buffer;
  686. return (int)len;
  687. }
  688. #if HAVE_DIRENT_H
  689. #include <dirent.h>
  690. #endif
  691. static int test_bad_messages(agent_t *ag)
  692. {
  693. BEGIN();
  694. #if HAVE_DIRENT_H
  695. DIR *dir;
  696. struct dirent *d;
  697. char name[PATH_MAX + 1] = "../sip/tests/";
  698. size_t offset;
  699. char const *host, *port;
  700. su_addrinfo_t *ai, hints[1];
  701. su_socket_t s;
  702. su_sockaddr_t su[1];
  703. socklen_t sulen;
  704. char via[64];
  705. size_t vlen;
  706. int i;
  707. dir = opendir(name);
  708. if (dir == NULL && getenv("srcdir")) {
  709. strncpy(name, getenv("srcdir"), PATH_MAX);
  710. strncat(name, "/../sip/tests/", PATH_MAX);
  711. dir = opendir(name);
  712. }
  713. if (dir == NULL) {
  714. fprintf(stderr, "test_nta: cannot find sip torture messages\n");
  715. fprintf(stderr, "test_nta: tried %s\n", name);
  716. }
  717. offset = strlen(name);
  718. TEST_1(ag->ag_default_leg = nta_leg_tcreate(ag->ag_agent,
  719. leg_callback_500,
  720. ag,
  721. NTATAG_NO_DIALOG(1),
  722. TAG_END()));
  723. host = ag->ag_contact->m_url->url_host;
  724. if (host_is_ip6_reference(host)) {
  725. host = strcpy(via, host + 1);
  726. via[strlen(via) - 1] = '\0';
  727. }
  728. port = url_port(ag->ag_contact->m_url);
  729. memset(hints, 0, sizeof hints);
  730. hints->ai_socktype = SOCK_DGRAM;
  731. hints->ai_protocol = IPPROTO_UDP;
  732. TEST(su_getaddrinfo(host, port, hints, &ai), 0); TEST_1(ai);
  733. s = su_socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); TEST_1(s != -1);
  734. memset(su, 0, sulen = ai->ai_addrlen);
  735. su->su_len = sizeof su; su->su_family = ai->ai_family;
  736. TEST_1(bind(s, &su->su_sa, sulen) == 0);
  737. TEST_1(getsockname(s, &su->su_sa, &sulen) == 0);
  738. sprintf(via, "v: SIP/2.0/UDP is.invalid:%u\r\n", ntohs(su->su_port));
  739. vlen = strlen(via);
  740. for (d = dir ? readdir(dir) : NULL; d; d = readdir(dir)) {
  741. size_t len = strlen(d->d_name);
  742. FILE *f;
  743. int blen, n;
  744. void *buffer; char *r;
  745. if (len < strlen(".txt"))
  746. continue;
  747. if (strcmp(d->d_name + len - strlen(".txt"), ".txt"))
  748. continue;
  749. strncpy(name + offset, d->d_name, PATH_MAX - offset);
  750. TEST_1(f = fopen(name, "rb"));
  751. TEST_1((blen = readfile(f, &buffer)) > 0);
  752. fclose(f);
  753. r = buffer;
  754. if (strncmp(r, "JUNK ", 5) == 0) {
  755. TEST_SIZE(su_sendto(s, r, blen, 0, ai->ai_addr, ai->ai_addrlen), blen);
  756. }
  757. else if (strncmp(r, "INVITE ", 7) != 0) {
  758. su_iovec_t vec[3];
  759. n = strcspn(r, "\r\n"); n += strspn(r + n, "\r\n");
  760. vec[0].siv_base = r, vec[0].siv_len = n;
  761. vec[1].siv_base = via, vec[1].siv_len = vlen;
  762. vec[2].siv_base = r + n, vec[2].siv_len = blen - n;
  763. TEST_SIZE(su_vsend(s, vec, 3, 0, (void *)ai->ai_addr, ai->ai_addrlen),
  764. blen + vlen);
  765. }
  766. free(buffer);
  767. su_root_step(ag->ag_root, 1);
  768. }
  769. TEST_SIZE(su_sendto(s, "\r\n\r\n", 4, 0, (void *)ai->ai_addr, ai->ai_addrlen), 4);
  770. su_root_step(ag->ag_root, 1);
  771. TEST_SIZE(su_sendto(s, "", 0, 0, ai->ai_addr, ai->ai_addrlen), 0);
  772. su_close(s);
  773. for (i = 0; i < 20; i++)
  774. su_root_step(ag->ag_root, 1);
  775. nta_leg_destroy(ag->ag_default_leg), ag->ag_default_leg = NULL;
  776. if (dir)
  777. closedir(dir);
  778. #endif /* HAVE_DIRENT_H */
  779. END();
  780. }
  781. static unsigned char const code[] =
  782. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  783. #include <sofia-sip/su_uniqueid.h>
  784. sip_payload_t *test_payload(su_home_t *home, size_t size)
  785. {
  786. sip_payload_t *pl = sip_payload_create(home, NULL, (isize_t)size);
  787. if (pl) {
  788. size_t i;
  789. char *data = (char *)pl->pl_data;
  790. for (i = 0; i < size; i++) {
  791. if ((i & 63) != 63)
  792. data[i] = code[su_randint(0, 63)];
  793. else
  794. data[i] = '\n';
  795. }
  796. }
  797. return pl;
  798. }
  799. static
  800. int client_check_to_tag(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  801. {
  802. if (sip)
  803. TEST_1(sip->sip_to && sip->sip_to->a_tag);
  804. return 0;
  805. }
  806. static
  807. int check_magic_branch(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  808. {
  809. if (sip) {
  810. TEST_1(sip->sip_via);
  811. TEST_S(sip->sip_via->v_branch, "MagicalBranch");
  812. }
  813. return 0;
  814. }
  815. static
  816. int check_via_with_sigcomp(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  817. {
  818. if (sip && sip->sip_via) {
  819. TEST_S(sip->sip_via->v_comp, "sigcomp");
  820. }
  821. return 0;
  822. }
  823. static
  824. int check_via_without_sigcomp(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  825. {
  826. if (sip && sip->sip_via) {
  827. TEST_1(sip->sip_via->v_comp == NULL);
  828. }
  829. return 0;
  830. }
  831. static
  832. int check_via_with_tcp(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  833. {
  834. if (sip && sip->sip_via) {
  835. TEST_S(sip->sip_via->v_protocol, "SIP/2.0/TCP");
  836. }
  837. return 0;
  838. }
  839. static
  840. int check_via_with_sctp(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  841. {
  842. if (sip && sip->sip_via) {
  843. TEST_S(sip->sip_via->v_protocol, "SIP/2.0/SCTP");
  844. }
  845. return 0;
  846. }
  847. static
  848. int check_via_with_udp(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  849. {
  850. if (sip && sip->sip_via) {
  851. TEST_S(sip->sip_via->v_protocol, "SIP/2.0/UDP");
  852. }
  853. return 0;
  854. }
  855. static
  856. int save_and_check_tcp(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  857. {
  858. if (ctx->c_status >= 200 && ctx->c_extra) {
  859. tport_t *tport = nta_outgoing_transport(orq);
  860. TEST_1(tport);
  861. *(tport_t **)ctx->c_extra = tport;
  862. }
  863. return check_via_with_tcp(ctx, orq, sip);
  864. }
  865. /* Test transports */
  866. int test_tports(agent_t *ag)
  867. {
  868. int udp = 0, tcp = 0, sctp = 0, tls = 0;
  869. sip_via_t const *v, *v_udp_only = NULL;
  870. char const *udp_comp = NULL;
  871. char const *tcp_comp = NULL;
  872. tport_t *tcp_tport = NULL;
  873. url_t url[1];
  874. BEGIN();
  875. nta_leg_bind(ag->ag_server_leg, leg_callback_200, ag);
  876. *url = *ag->ag_contact->m_url;
  877. url->url_port = "*";
  878. url->url_params = "transport=tcp";
  879. url->url_params = "transport=udp";
  880. TEST_1(nta_agent_add_tport(ag->ag_agent, (url_string_t *)url,
  881. TAG_END()) == 0);
  882. TEST_1(v = nta_agent_via(ag->ag_agent));
  883. for (; v; v = v->v_next) {
  884. if (su_casematch(v->v_protocol, sip_transport_udp)) {
  885. if (udp)
  886. v_udp_only = v;
  887. udp = 1;
  888. if (udp_comp == NULL)
  889. udp_comp = v->v_comp;
  890. }
  891. else if (su_casematch(v->v_protocol, sip_transport_tcp)) {
  892. tcp = 1;
  893. if (tcp_comp == NULL)
  894. tcp_comp = v->v_comp;
  895. }
  896. else if (su_casematch(v->v_protocol, sip_transport_sctp)) {
  897. sctp = 1;
  898. }
  899. else if (su_casematch(v->v_protocol, sip_transport_tls)) {
  900. tls = 1;
  901. }
  902. }
  903. *url = *ag->ag_aliases->m_url;
  904. url->url_user = "bob";
  905. if (udp_comp || tcp_comp)
  906. ag->ag_comp = "sigcomp";
  907. {
  908. /* Test 0.1
  909. * Send a message from default leg to default leg
  910. */
  911. char const p_acid[] = "P-Access-Network-Info: IEEE-802.11g\n";
  912. url_t url[1];
  913. client_t ctx[1] = {{ ag, "Test 0.1", check_via_without_sigcomp }};
  914. *url = *ag->ag_contact->m_url;
  915. url->url_params = NULL;
  916. ag->ag_expect_leg = ag->ag_default_leg;
  917. ctx->c_orq =
  918. nta_outgoing_tcreate(ag->ag_default_leg,
  919. outgoing_callback, ctx,
  920. ag->ag_obp,
  921. SIP_METHOD_MESSAGE,
  922. (url_string_t *)url,
  923. SIPTAG_SUBJECT_STR(ctx->c_name),
  924. SIPTAG_FROM(ag->ag_alice),
  925. SIPTAG_TO(ag->ag_bob),
  926. SIPTAG_CONTACT(ag->ag_m_alice),
  927. SIPTAG_HEADER_STR(p_acid),
  928. TAG_END());
  929. TEST_1(!client_run(ctx, 200));
  930. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  931. TEST_1(ag->ag_request);
  932. msg_destroy(ag->ag_request), ag->ag_request = NULL;
  933. nta_leg_bind(ag->ag_default_leg, leg_callback_200, ag);
  934. }
  935. {
  936. /* Test 0.1.2: test url_headers
  937. *
  938. * Send a message from default leg to default leg.
  939. */
  940. url_t url[1];
  941. sip_t *sip;
  942. client_t ctx[1] = {{ ag, "Test 0.1.2", check_via_without_sigcomp }};
  943. *url = *ag->ag_contact->m_url;
  944. /* Test that method parameter is stripped and headers in query are used */
  945. url->url_params = "method=MESSAGE;user=IP";
  946. url->url_headers = "organization=United%20Testers";
  947. ag->ag_expect_leg = ag->ag_default_leg;
  948. ctx->c_orq =
  949. nta_outgoing_tcreate(ag->ag_default_leg,
  950. outgoing_callback, ctx,
  951. ag->ag_obp,
  952. SIP_METHOD_MESSAGE,
  953. (url_string_t *)url,
  954. SIPTAG_SUBJECT_STR(ctx->c_name),
  955. SIPTAG_FROM(ag->ag_alice),
  956. SIPTAG_TO(ag->ag_bob),
  957. SIPTAG_CONTACT(ag->ag_m_alice),
  958. TAG_END());
  959. TEST_1(!client_run(ctx, 200));
  960. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  961. TEST_1(ag->ag_request);
  962. TEST_1(sip = sip_object(ag->ag_request));
  963. TEST_1(sip->sip_organization);
  964. TEST_S(sip->sip_organization->g_string, "United Testers");
  965. TEST_S(sip->sip_request->rq_url->url_params, "user=IP");
  966. nta_leg_bind(ag->ag_default_leg, leg_callback_200, ag);
  967. }
  968. /* Test 0.1.3
  969. * Send a message from Bob to Alice using SIGCOMP and TCP
  970. */
  971. if (tcp_comp) {
  972. url_t url[1];
  973. sip_payload_t *pl;
  974. size_t size = 1024;
  975. client_t ctx[1] = {{ ag, "Test 0.1.3", check_via_with_sigcomp }};
  976. *url = *ag->ag_aliases->m_url;
  977. url->url_user = "alice";
  978. if (url->url_params)
  979. url->url_params = su_sprintf(NULL, "%s;transport=tcp", url->url_params);
  980. else
  981. url->url_params = "transport=tcp";
  982. TEST_1(pl = test_payload(ag->ag_home, size));
  983. ag->ag_expect_leg = ag->ag_server_leg;
  984. ctx->c_orq =
  985. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  986. ag->ag_obp,
  987. SIP_METHOD_MESSAGE,
  988. (url_string_t *)url,
  989. NTATAG_COMP("sigcomp"),
  990. SIPTAG_SUBJECT_STR(ctx->c_name),
  991. SIPTAG_FROM(ag->ag_bob),
  992. SIPTAG_TO(ag->ag_alice),
  993. SIPTAG_CONTACT(ag->ag_m_bob),
  994. SIPTAG_PAYLOAD(pl),
  995. TAG_END());
  996. su_free(ag->ag_home, pl);
  997. TEST_1(!client_run(ctx, 200));
  998. TEST_1(ag->ag_client_compartment);
  999. nta_compartment_decref(&ag->ag_client_compartment);
  1000. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  1001. }
  1002. /* Test 0.2
  1003. * Send a message from Bob to Alice
  1004. * This time specify a TCP URI, and include a large payload
  1005. * of 512 kB
  1006. */
  1007. if (tcp) {
  1008. client_t ctx[1] = {{ ag, "Test 0.2", save_and_check_tcp, }};
  1009. url_t url[1];
  1010. sip_payload_t *pl;
  1011. usize_t size = 512 * 1024;
  1012. ctx->c_extra = &tcp_tport;
  1013. *url = *ag->ag_aliases->m_url;
  1014. url->url_user = "alice";
  1015. url->url_params = "transport=tcp";
  1016. TEST_1(pl = test_payload(ag->ag_home, size));
  1017. ag->ag_expect_leg = ag->ag_server_leg;
  1018. ctx->c_orq =
  1019. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1020. NULL,
  1021. SIP_METHOD_MESSAGE,
  1022. (url_string_t *)url,
  1023. SIPTAG_SUBJECT_STR(ctx->c_name),
  1024. SIPTAG_FROM(ag->ag_bob),
  1025. SIPTAG_TO(ag->ag_alice),
  1026. SIPTAG_CONTACT(ag->ag_m_bob),
  1027. SIPTAG_PAYLOAD(pl),
  1028. NTATAG_DEFAULT_PROXY(ag->ag_obp),
  1029. TAG_END());
  1030. su_free(ag->ag_home, pl);
  1031. TEST_1(!client_run(ctx, 200));
  1032. TEST_1(tcp_tport);
  1033. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  1034. }
  1035. if (tcp_tport) {
  1036. /* Test 0.2.1 - always use transport connection from NTATAG_TPORT()
  1037. *
  1038. * Test bug reported by geaaru
  1039. * - NTATAG_TPORT() is not used if NTATAG_DEFAULT_PROXY() is given
  1040. */
  1041. client_t ctx[1] = {{ ag, "Test 0.2.1", save_and_check_tcp }};
  1042. url_t url[1];
  1043. sip_payload_t *pl;
  1044. tport_t *used_tport = NULL;
  1045. ctx->c_extra = &used_tport;
  1046. *url = *ag->ag_aliases->m_url;
  1047. url->url_user = "alice";
  1048. TEST(tport_shutdown(tcp_tport, 1), 0); /* Not going to send anymore */
  1049. TEST_1(pl = test_payload(ag->ag_home, 512));
  1050. ag->ag_expect_leg = ag->ag_server_leg;
  1051. ctx->c_orq =
  1052. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1053. NULL,
  1054. SIP_METHOD_MESSAGE,
  1055. (url_string_t *)url,
  1056. SIPTAG_SUBJECT_STR(ctx->c_name),
  1057. SIPTAG_FROM(ag->ag_bob),
  1058. SIPTAG_TO(ag->ag_alice),
  1059. SIPTAG_CONTACT(ag->ag_m_bob),
  1060. SIPTAG_PAYLOAD(pl),
  1061. NTATAG_DEFAULT_PROXY(ag->ag_obp),
  1062. NTATAG_TPORT(tcp_tport),
  1063. TAG_END());
  1064. su_free(ag->ag_home, pl);
  1065. TEST_1(!client_run(ctx, 503));
  1066. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  1067. TEST_1(used_tport == tcp_tport);
  1068. tport_unref(tcp_tport), tcp_tport = NULL;
  1069. if (v_udp_only) /* Prepare for next test */
  1070. TEST_1(tcp_tport = tport_ref(tport_parent(used_tport)));
  1071. tport_unref(used_tport);
  1072. }
  1073. if (tcp_tport) {
  1074. /* test 0.2.2 - select transport protocol using NTATAG_TPORT()
  1075. *
  1076. * Use primary NTATAG_TPORT() to select transport
  1077. */
  1078. client_t ctx[1] = {{ ag, "Test 0.2.2", save_and_check_tcp }};
  1079. url_t url[1];
  1080. sip_payload_t *pl;
  1081. tport_t *used_tport = NULL;
  1082. ctx->c_extra = &used_tport;
  1083. TEST_1(tport_is_primary(tcp_tport));
  1084. TEST_1(pl = test_payload(ag->ag_home, 512));
  1085. *url = *ag->ag_aliases->m_url;
  1086. url->url_user = "alice";
  1087. url->url_host = v_udp_only->v_host;
  1088. url->url_port = v_udp_only->v_port;
  1089. url->url_params = NULL; /* No sigcomp */
  1090. ag->ag_expect_leg = ag->ag_server_leg;
  1091. ctx->c_orq =
  1092. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1093. (url_string_t *)url,
  1094. SIP_METHOD_MESSAGE,
  1095. (url_string_t *)url,
  1096. SIPTAG_SUBJECT_STR(ctx->c_name),
  1097. SIPTAG_FROM(ag->ag_bob),
  1098. SIPTAG_TO(ag->ag_alice),
  1099. SIPTAG_CONTACT(ag->ag_m_bob),
  1100. SIPTAG_PAYLOAD(pl),
  1101. NTATAG_TPORT(tcp_tport),
  1102. TAG_END());
  1103. su_free(ag->ag_home, pl);
  1104. TEST_1(!client_run(ctx, 503));
  1105. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  1106. TEST_1(used_tport);
  1107. TEST_1(tport_is_tcp(used_tport));
  1108. tport_unref(used_tport);
  1109. tport_unref(tcp_tport), tcp_tport = NULL;
  1110. }
  1111. /* Test 0.3
  1112. * Send a message from Bob to Alice
  1113. * This time include a large payload of 512 kB, let NTA choose transport.
  1114. */
  1115. if (tcp) {
  1116. client_t ctx[1] = {{ ag, "Test 0.3" }};
  1117. url_t url[1];
  1118. sip_payload_t *pl;
  1119. usize_t size = 512 * 1024;
  1120. *url = *ag->ag_aliases->m_url;
  1121. url->url_user = "alice";
  1122. TEST_1(pl = test_payload(ag->ag_home, size));
  1123. ag->ag_expect_leg = ag->ag_server_leg;
  1124. ctx->c_orq =
  1125. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1126. ag->ag_obp,
  1127. SIP_METHOD_MESSAGE,
  1128. (url_string_t *)url,
  1129. SIPTAG_SUBJECT_STR(ctx->c_name),
  1130. SIPTAG_FROM(ag->ag_bob),
  1131. SIPTAG_TO(ag->ag_alice),
  1132. SIPTAG_CONTACT(ag->ag_m_bob),
  1133. SIPTAG_PAYLOAD(pl),
  1134. TAG_END());
  1135. su_free(ag->ag_home, pl);
  1136. TEST_1(!client_run(ctx, 200));
  1137. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  1138. }
  1139. /* Test 0.4.1:
  1140. * Send a message from Bob to Alice
  1141. * This time include a payload of 2 kB, let NTA choose transport.
  1142. */
  1143. {
  1144. client_t ctx[1] = {{ ag, "Test 0.4.1", check_via_with_tcp }};
  1145. url_t url[1];
  1146. sip_payload_t *pl;
  1147. usize_t size = 2 * 1024;
  1148. *url = *ag->ag_aliases->m_url;
  1149. url->url_user = "alice";
  1150. TEST_1(pl = test_payload(ag->ag_home, size));
  1151. ag->ag_expect_leg = ag->ag_server_leg;
  1152. ctx->c_orq =
  1153. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1154. ag->ag_obp,
  1155. SIP_METHOD_MESSAGE,
  1156. (url_string_t *)url,
  1157. SIPTAG_SUBJECT_STR(ctx->c_name),
  1158. SIPTAG_FROM(ag->ag_bob),
  1159. SIPTAG_TO(ag->ag_alice),
  1160. SIPTAG_CONTACT(ag->ag_m_bob),
  1161. SIPTAG_PAYLOAD(pl),
  1162. TAG_END());
  1163. su_free(ag->ag_home, pl);
  1164. TEST_1(!client_run(ctx, 200));
  1165. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  1166. su_free(ag->ag_home, ag->ag_in_via), ag->ag_in_via = NULL;
  1167. }
  1168. /* Test 0.4.2:
  1169. * Send a message from Bob to Alices UDP-only address
  1170. * This time include a payload of 2 kB, let NTA choose transport.
  1171. */
  1172. if (v_udp_only) {
  1173. client_t ctx[1] = {{ ag, "Test 0.4.2", check_via_with_udp }};
  1174. url_t url[1];
  1175. sip_payload_t *pl;
  1176. usize_t size = 2 * 1024;
  1177. *url = *ag->ag_aliases->m_url;
  1178. url->url_user = "alice";
  1179. url->url_host = v_udp_only->v_host;
  1180. url->url_port = v_udp_only->v_port;
  1181. url->url_params = NULL; /* No sigcomp */
  1182. TEST_1(pl = test_payload(ag->ag_home, size));
  1183. ag->ag_expect_leg = ag->ag_default_leg;
  1184. su_free(ag->ag_home, ag->ag_in_via), ag->ag_in_via = NULL;
  1185. ctx->c_orq =
  1186. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1187. ag->ag_obp,
  1188. SIP_METHOD_MESSAGE,
  1189. (url_string_t *)url,
  1190. SIPTAG_SUBJECT_STR(ctx->c_name),
  1191. SIPTAG_FROM(ag->ag_bob),
  1192. SIPTAG_TO(ag->ag_alice),
  1193. SIPTAG_CONTACT(ag->ag_m_bob),
  1194. SIPTAG_PAYLOAD(pl),
  1195. TAG_END());
  1196. su_free(ag->ag_home, pl);
  1197. TEST_1(!client_run(ctx, 200));
  1198. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1199. TEST_1(ag->ag_in_via);
  1200. TEST_1(su_casematch(ag->ag_in_via->v_protocol, "SIP/2.0/UDP"));
  1201. su_free(ag->ag_home, ag->ag_in_via), ag->ag_in_via = NULL;
  1202. }
  1203. /* Test 0.5:
  1204. * Send a message from Bob to Alice
  1205. * This time include a payload of 2 kB, try to use UDP.
  1206. */
  1207. if (udp) {
  1208. client_t ctx[1] = {{ ag, "Test 0.5", check_via_with_udp }};
  1209. url_t url[1];
  1210. sip_payload_t *pl;
  1211. usize_t size = 2 * 1024;
  1212. *url = *ag->ag_aliases->m_url;
  1213. url->url_user = "alice";
  1214. TEST_1(pl = test_payload(ag->ag_home, size));
  1215. ag->ag_expect_leg = ag->ag_server_leg;
  1216. ctx->c_orq =
  1217. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1218. ag->ag_obp,
  1219. SIP_METHOD_MESSAGE,
  1220. (url_string_t *)url,
  1221. SIPTAG_SUBJECT_STR(ctx->c_name),
  1222. SIPTAG_FROM(ag->ag_bob),
  1223. SIPTAG_TO(ag->ag_alice),
  1224. SIPTAG_CONTACT(ag->ag_m_bob),
  1225. SIPTAG_PAYLOAD(pl),
  1226. TPTAG_MTU(0xffffffff),
  1227. TAG_END());
  1228. su_free(ag->ag_home, pl);
  1229. TEST_1(!client_run(ctx, 200));
  1230. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  1231. }
  1232. if (udp) {
  1233. /* Test 0.6
  1234. * Send a message from default leg to server leg
  1235. * using a prefilled Via header
  1236. */
  1237. client_t ctx[1] = {{ ag, "Test 0.6", check_magic_branch }};
  1238. sip_via_t via[1];
  1239. sip_via_init(via);
  1240. via->v_protocol = sip_transport_udp;
  1241. via->v_host = ag->ag_contact->m_url->url_host;
  1242. via->v_port = ag->ag_contact->m_url->url_port;
  1243. sip_via_add_param(ag->ag_home, via, "branch=MagicalBranch");
  1244. nta_agent_set_params(ag->ag_agent,
  1245. NTATAG_ALIASES(ag->ag_aliases),
  1246. NTATAG_USER_VIA(1),
  1247. TAG_END());
  1248. ag->ag_expect_leg = ag->ag_server_leg;
  1249. ctx->c_orq =
  1250. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1251. ag->ag_obp,
  1252. SIP_METHOD_MESSAGE,
  1253. (url_string_t *)url,
  1254. SIPTAG_SUBJECT_STR(ctx->c_name),
  1255. SIPTAG_FROM(ag->ag_alice),
  1256. SIPTAG_TO(ag->ag_bob),
  1257. SIPTAG_CONTACT(ag->ag_m_alice),
  1258. SIPTAG_VIA(via),
  1259. TAG_END());
  1260. TEST_1(!client_run(ctx, 200));
  1261. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  1262. nta_agent_set_params(ag->ag_agent,
  1263. NTATAG_USER_VIA(0),
  1264. TAG_END());
  1265. }
  1266. /* Test 0.7
  1267. * Send a message from Bob to Alice using SCTP
  1268. */
  1269. if (sctp) {
  1270. url_t url[1];
  1271. sip_payload_t *pl;
  1272. usize_t size = 16 * 1024;
  1273. client_t ctx[1] = {{ ag, "Test 0.7", check_via_with_sctp }};
  1274. *url = *ag->ag_aliases->m_url;
  1275. url->url_user = "alice";
  1276. #if 0
  1277. if (url->url_params)
  1278. url->url_params = su_sprintf(NULL, "%s;transport=sctp", url->url_params);
  1279. else
  1280. #endif
  1281. url->url_params = "transport=sctp";
  1282. TEST_1(pl = test_payload(ag->ag_home, size));
  1283. ag->ag_expect_leg = ag->ag_server_leg;
  1284. ctx->c_orq =
  1285. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1286. ag->ag_obp,
  1287. SIP_METHOD_MESSAGE,
  1288. (url_string_t *)url,
  1289. SIPTAG_SUBJECT_STR(ctx->c_name),
  1290. SIPTAG_FROM(ag->ag_bob),
  1291. SIPTAG_TO(ag->ag_alice),
  1292. SIPTAG_CONTACT(ag->ag_m_bob),
  1293. SIPTAG_PAYLOAD(pl),
  1294. TAG_END());
  1295. su_free(ag->ag_home, pl);
  1296. TEST_1(!client_run(ctx, 200));
  1297. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  1298. }
  1299. /* Test 0.8: Send a too large message */
  1300. if (tcp) {
  1301. url_t url[1];
  1302. sip_payload_t *pl;
  1303. usize_t size = 128 * 1024;
  1304. client_t ctx[1] = {{ ag, "Test 0.8" }};
  1305. nta_agent_set_params(ag->ag_agent,
  1306. NTATAG_MAXSIZE(65536),
  1307. TAG_END());
  1308. *url = *ag->ag_aliases->m_url;
  1309. url->url_user = "alice";
  1310. TEST_1(pl = test_payload(ag->ag_home, size));
  1311. ag->ag_expect_leg = ag->ag_server_leg;
  1312. ag->ag_latest_leg = NULL;
  1313. ctx->c_orq =
  1314. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1315. ag->ag_obp,
  1316. SIP_METHOD_MESSAGE,
  1317. (url_string_t *)url,
  1318. SIPTAG_SUBJECT_STR(ctx->c_name),
  1319. SIPTAG_FROM(ag->ag_bob),
  1320. SIPTAG_TO(ag->ag_alice),
  1321. SIPTAG_CONTACT(ag->ag_m_bob),
  1322. SIPTAG_PAYLOAD(pl),
  1323. TAG_END());
  1324. su_free(ag->ag_home, pl);
  1325. TEST_1(!client_run(ctx, 413));
  1326. TEST_P(ag->ag_latest_leg, NULL);
  1327. nta_agent_set_params(ag->ag_agent,
  1328. NTATAG_MAXSIZE(2 * 1024 * 1024),
  1329. TAG_END());
  1330. }
  1331. /* Test 0.9: Timeout */
  1332. {
  1333. url_t url[1];
  1334. client_t ctx[1] = {{ ag, "Test 0.9" }};
  1335. nta_agent_set_params(ag->ag_agent,
  1336. NTATAG_TIMEOUT_408(1),
  1337. TAG_END());
  1338. *url = *ag->ag_aliases->m_url;
  1339. url->url_user = "timeout";
  1340. url->url_port = ag->ag_sink_port;
  1341. ag->ag_expect_leg = ag->ag_server_leg;
  1342. ag->ag_latest_leg = NULL;
  1343. ctx->c_orq =
  1344. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1345. ag->ag_obp,
  1346. SIP_METHOD_MESSAGE,
  1347. (url_string_t *)url,
  1348. SIPTAG_SUBJECT_STR(ctx->c_name),
  1349. SIPTAG_FROM(ag->ag_bob),
  1350. SIPTAG_TO(ag->ag_alice),
  1351. SIPTAG_CONTACT(ag->ag_m_bob),
  1352. TAG_END());
  1353. TEST_1(!client_run_with(ctx, 408, fast_final_received));
  1354. TEST_P(ag->ag_latest_leg, NULL);
  1355. nta_agent_set_params(ag->ag_agent,
  1356. TAG_END());
  1357. }
  1358. END();
  1359. }
  1360. int leg_callback_destroy(agent_t *ag,
  1361. nta_leg_t *leg,
  1362. nta_incoming_t *irq,
  1363. sip_t const *sip)
  1364. {
  1365. if (tstflags & tst_verbatim) {
  1366. printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
  1367. name, __func__, sip->sip_request->rq_method_name,
  1368. URL_PRINT_ARGS(sip->sip_request->rq_url),
  1369. sip->sip_request->rq_version);
  1370. }
  1371. ag->ag_latest_leg = leg;
  1372. nta_incoming_destroy(irq);
  1373. return 0;
  1374. }
  1375. int leg_callback_save(agent_t *ag,
  1376. nta_leg_t *leg,
  1377. nta_incoming_t *irq,
  1378. sip_t const *sip)
  1379. {
  1380. if (tstflags & tst_verbatim) {
  1381. printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
  1382. name, __func__, sip->sip_request->rq_method_name,
  1383. URL_PRINT_ARGS(sip->sip_request->rq_url),
  1384. sip->sip_request->rq_version);
  1385. }
  1386. ag->ag_latest_leg = leg;
  1387. ag->ag_irq = irq;
  1388. ag->ag_running = 0;
  1389. return 0;
  1390. }
  1391. int test_destroy_incoming(agent_t *ag)
  1392. {
  1393. BEGIN();
  1394. url_t url[1];
  1395. *url = *ag->ag_contact->m_url;
  1396. {
  1397. client_t ctx[1] = {{ ag, "Test 3.1" }};
  1398. /* Test 3.1
  1399. * Check that when a incoming request is destroyed in callback,
  1400. * a 500 response is sent
  1401. */
  1402. ag->ag_expect_leg = ag->ag_default_leg;
  1403. nta_leg_bind(ag->ag_default_leg, leg_callback_destroy, ag);
  1404. ctx->c_orq =
  1405. nta_outgoing_tcreate(ag->ag_default_leg,
  1406. outgoing_callback, ctx,
  1407. ag->ag_obp,
  1408. SIP_METHOD_MESSAGE,
  1409. (url_string_t *)url,
  1410. SIPTAG_SUBJECT_STR(ctx->c_name),
  1411. SIPTAG_FROM(ag->ag_alice),
  1412. SIPTAG_TO(ag->ag_bob),
  1413. TAG_END());
  1414. TEST_1(!client_run(ctx, 500));
  1415. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1416. }
  1417. {
  1418. /* Test 3.2
  1419. * Check that when an incoming request is destroyed, a 500 response is sent
  1420. */
  1421. client_t ctx[1] = {{ ag, "Test 3.2" }};
  1422. nta_leg_bind(ag->ag_default_leg, leg_callback_save, ag);
  1423. ctx->c_orq =
  1424. nta_outgoing_tcreate(ag->ag_default_leg,
  1425. outgoing_callback, ctx,
  1426. ag->ag_obp,
  1427. SIP_METHOD_MESSAGE,
  1428. (url_string_t *)url,
  1429. SIPTAG_SUBJECT_STR(ctx->c_name),
  1430. SIPTAG_FROM(ag->ag_alice),
  1431. SIPTAG_TO(ag->ag_bob),
  1432. TAG_END());
  1433. TEST_1(ctx->c_orq);
  1434. nta_test_run(ag);
  1435. TEST(ctx->c_status, 0);
  1436. TEST_1(ag->ag_irq);
  1437. TEST_1(ctx->c_orq);
  1438. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1439. nta_incoming_destroy(ag->ag_irq), ag->ag_irq = NULL;
  1440. TEST_1(!client_run(ctx, 500));
  1441. }
  1442. END();
  1443. }
  1444. int test_resolv(agent_t *ag, char const *resolv_conf)
  1445. {
  1446. int udp = 0, tcp = 0, sctp = 0, tls = 0;
  1447. sip_via_t const *v;
  1448. url_t *url;
  1449. if (!resolv_conf)
  1450. return 0;
  1451. BEGIN();
  1452. nta_leg_bind(ag->ag_default_leg, leg_callback_200, ag);
  1453. nta_agent_set_params(ag->ag_agent,
  1454. NTATAG_SIP_T1(8 * 25),
  1455. NTATAG_SIP_T1X64(64 * 25),
  1456. NTATAG_SIP_T4(10 * 25),
  1457. TAG_END());
  1458. TEST_1(v = nta_agent_via(ag->ag_agent));
  1459. for (; v; v = v->v_next) {
  1460. if (su_casematch(v->v_protocol, sip_transport_udp))
  1461. udp = 1;
  1462. else if (su_casematch(v->v_protocol, sip_transport_tcp))
  1463. tcp = 1;
  1464. else if (su_casematch(v->v_protocol, sip_transport_sctp))
  1465. sctp = 1;
  1466. else if (su_casematch(v->v_protocol, sip_transport_tls))
  1467. tls = 1;
  1468. }
  1469. url = url_hdup(ag->ag_home, (void *)"sip:example.org"); TEST_1(url);
  1470. {
  1471. /* Test 1.1
  1472. * Send a message to sip:example.org
  1473. */
  1474. client_t ctx[1] = {{ ag, "Test 1.1" }};
  1475. ag->ag_expect_leg = ag->ag_default_leg;
  1476. ctx->c_orq =
  1477. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1478. ag->ag_obp,
  1479. SIP_METHOD_MESSAGE,
  1480. (url_string_t *)url,
  1481. SIPTAG_SUBJECT_STR(ctx->c_name),
  1482. SIPTAG_FROM(ag->ag_alice),
  1483. SIPTAG_TO(ag->ag_bob),
  1484. SIPTAG_CONTACT(ag->ag_m_alice),
  1485. TAG_END());
  1486. TEST_1(!client_run(ctx, 200));
  1487. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1488. }
  1489. {
  1490. /* Test 1.2
  1491. * Send a message to sip:srv.example.org
  1492. */
  1493. client_t ctx[1] = {{ ag, "Test 1.2" }};
  1494. url->url_host = "srv.example.org";
  1495. ag->ag_expect_leg = ag->ag_default_leg;
  1496. ctx->c_orq =
  1497. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1498. ag->ag_obp,
  1499. SIP_METHOD_MESSAGE,
  1500. (url_string_t *)url,
  1501. SIPTAG_SUBJECT_STR(ctx->c_name),
  1502. SIPTAG_FROM(ag->ag_alice),
  1503. SIPTAG_TO(ag->ag_bob),
  1504. SIPTAG_CONTACT(ag->ag_m_alice),
  1505. TAG_END());
  1506. TEST_1(!client_run(ctx, 200));
  1507. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1508. }
  1509. {
  1510. /* Test 1.3
  1511. * Send a message to sip:ipv.example.org
  1512. */
  1513. client_t ctx[1] = {{ ag, "Test 1.3" }};
  1514. url->url_host = "ipv.example.org";
  1515. ag->ag_expect_leg = ag->ag_default_leg;
  1516. ctx->c_orq =
  1517. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1518. ag->ag_obp,
  1519. SIP_METHOD_MESSAGE,
  1520. (url_string_t *)url,
  1521. SIPTAG_SUBJECT_STR(ctx->c_name),
  1522. SIPTAG_FROM(ag->ag_alice),
  1523. SIPTAG_TO(ag->ag_bob),
  1524. SIPTAG_CONTACT(ag->ag_m_alice),
  1525. TAG_END());
  1526. TEST_1(!client_run(ctx, 200));
  1527. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1528. }
  1529. {
  1530. /* Test 1.4.1
  1531. * Send a message to sip:down.example.org
  1532. */
  1533. client_t ctx[1] = {{ ag, "Test 1.4.1" }};
  1534. url->url_host = "down.example.org";
  1535. ag->ag_expect_leg = ag->ag_default_leg;
  1536. ctx->c_orq =
  1537. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1538. ag->ag_obp,
  1539. SIP_METHOD_MESSAGE,
  1540. (url_string_t *)url,
  1541. SIPTAG_SUBJECT_STR(ctx->c_name),
  1542. SIPTAG_FROM(ag->ag_alice),
  1543. SIPTAG_TO(ag->ag_bob),
  1544. SIPTAG_CONTACT(ag->ag_m_alice),
  1545. TAG_END());
  1546. TEST_1(!client_run(ctx, 200));
  1547. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1548. }
  1549. {
  1550. /* Test 1.4.2
  1551. * Send a message to sip:na503.example.org
  1552. */
  1553. client_t ctx[1] = {{ ag, "Test 1.4.2" }};
  1554. url->url_host = "na503.example.org";
  1555. ag->ag_expect_leg = ag->ag_default_leg;
  1556. ctx->c_orq =
  1557. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1558. ag->ag_obp,
  1559. SIP_METHOD_MESSAGE,
  1560. (url_string_t *)url,
  1561. SIPTAG_SUBJECT_STR(ctx->c_name),
  1562. SIPTAG_FROM(ag->ag_alice),
  1563. SIPTAG_TO(ag->ag_bob),
  1564. SIPTAG_CONTACT(ag->ag_m_alice),
  1565. TAG_END());
  1566. TEST_1(!client_run(ctx, 503));
  1567. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1568. }
  1569. {
  1570. /* Test 1.4.3
  1571. * Send a message to sip:nona.example.org
  1572. */
  1573. client_t ctx[1] = {{ ag, "Test 1.4.3" }};
  1574. url->url_host = "nona.example.org";
  1575. ag->ag_expect_leg = ag->ag_default_leg;
  1576. ctx->c_orq =
  1577. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1578. ag->ag_obp,
  1579. SIP_METHOD_MESSAGE,
  1580. (url_string_t *)url,
  1581. SIPTAG_SUBJECT_STR(ctx->c_name),
  1582. SIPTAG_FROM(ag->ag_alice),
  1583. SIPTAG_TO(ag->ag_bob),
  1584. SIPTAG_CONTACT(ag->ag_m_alice),
  1585. TAG_END());
  1586. TEST_1(!client_run(ctx, 200));
  1587. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1588. }
  1589. {
  1590. /* Test 1.4.4
  1591. * Send a message to sip:nosrv.example.org
  1592. * After failing to find _sip._udp.nosrv.example.org,
  1593. * second SRV with _sip._udp.srv.example.org succeeds
  1594. */
  1595. client_t ctx[1] = {{ ag, "Test 1.4.4" }};
  1596. url->url_host = "nosrv.example.org";
  1597. ag->ag_expect_leg = ag->ag_default_leg;
  1598. ctx->c_orq =
  1599. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1600. ag->ag_obp,
  1601. SIP_METHOD_MESSAGE,
  1602. (url_string_t *)url,
  1603. SIPTAG_SUBJECT_STR(ctx->c_name),
  1604. SIPTAG_FROM(ag->ag_alice),
  1605. SIPTAG_TO(ag->ag_bob),
  1606. SIPTAG_CONTACT(ag->ag_m_alice),
  1607. TAG_END());
  1608. TEST_1(!client_run(ctx, 200));
  1609. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1610. }
  1611. {
  1612. /* Test 1.5.1
  1613. * Send a message to sip:srv.example.org;transport=tcp
  1614. * Test outgoing_make_srv_query()
  1615. */
  1616. client_t ctx[1] = {{ ag, "Test 1.5.1: outgoing_make_srv_query()" }};
  1617. url->url_host = "srv.example.org";
  1618. url->url_params = "transport=tcp";
  1619. ag->ag_expect_leg = ag->ag_default_leg;
  1620. ctx->c_orq =
  1621. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1622. ag->ag_obp,
  1623. SIP_METHOD_MESSAGE,
  1624. (url_string_t *)url,
  1625. SIPTAG_SUBJECT_STR(ctx->c_name),
  1626. SIPTAG_FROM(ag->ag_alice),
  1627. SIPTAG_TO(ag->ag_bob),
  1628. SIPTAG_CONTACT(ag->ag_m_alice),
  1629. TAG_END());
  1630. TEST_1(!client_run(ctx, 200));
  1631. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1632. url->url_params = NULL;
  1633. }
  1634. {
  1635. /* Test 1.5.2
  1636. * Send a message to sip:srv.example.org;transport=udp
  1637. * Test outgoing_make_srv_query()
  1638. */
  1639. client_t ctx[1] = {{ ag, "Test 1.5.2: outgoing_make_srv_query()" }};
  1640. url->url_host = "srv.example.org";
  1641. url->url_params = "transport=udp";
  1642. ag->ag_expect_leg = ag->ag_default_leg;
  1643. ctx->c_orq =
  1644. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1645. ag->ag_obp,
  1646. SIP_METHOD_MESSAGE,
  1647. (url_string_t *)url,
  1648. SIPTAG_SUBJECT_STR(ctx->c_name),
  1649. SIPTAG_FROM(ag->ag_alice),
  1650. SIPTAG_TO(ag->ag_bob),
  1651. SIPTAG_CONTACT(ag->ag_m_alice),
  1652. TAG_END());
  1653. TEST_1(!client_run(ctx, 200));
  1654. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1655. url->url_params = NULL;
  1656. }
  1657. {
  1658. /* Test 1.5.3
  1659. * Send a message to sip:srv2.example.org;transport=udp
  1660. * Test outgoing_query_srv_a()
  1661. */
  1662. client_t ctx[1] = {{ ag, "Test 1.5: outgoing_query_srv_a()" }};
  1663. url->url_host = "srv2.example.org";
  1664. url->url_params = "transport=udp";
  1665. ag->ag_expect_leg = ag->ag_default_leg;
  1666. ctx->c_orq =
  1667. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1668. ag->ag_obp,
  1669. SIP_METHOD_MESSAGE,
  1670. (url_string_t *)url,
  1671. SIPTAG_SUBJECT_STR(ctx->c_name),
  1672. SIPTAG_FROM(ag->ag_alice),
  1673. SIPTAG_TO(ag->ag_bob),
  1674. SIPTAG_CONTACT(ag->ag_m_alice),
  1675. TAG_END());
  1676. TEST_1(!client_run(ctx, 200));
  1677. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1678. url->url_params = NULL;
  1679. }
  1680. {
  1681. /* Test 1.6.1
  1682. * Send a message to sip:srv.example.org:$port
  1683. * Test outgoing_make_a_aaaa_query()
  1684. */
  1685. client_t ctx[1] = {{ ag, "Test 1.6.1: outgoing_make_a_aaaa_query()" }};
  1686. url->url_host = "srv.example.org";
  1687. url->url_port = ag->ag_contact->m_url->url_port;
  1688. ag->ag_expect_leg = ag->ag_default_leg;
  1689. ctx->c_orq =
  1690. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1691. ag->ag_obp,
  1692. SIP_METHOD_MESSAGE,
  1693. (url_string_t *)url,
  1694. SIPTAG_SUBJECT_STR(ctx->c_name),
  1695. SIPTAG_FROM(ag->ag_alice),
  1696. SIPTAG_TO(ag->ag_bob),
  1697. SIPTAG_CONTACT(ag->ag_m_alice),
  1698. TAG_END());
  1699. TEST_1(!client_run(ctx, 503));
  1700. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1701. }
  1702. {
  1703. /* Test 1.6.2
  1704. * Send a message to sip:a.example.org:$port
  1705. * Test outgoing_make_a_aaaa_query()
  1706. */
  1707. client_t ctx[1] = {{ ag, "Test 1.6.2: outgoing_make_a_aaaa_query()" }};
  1708. url->url_host = "a.example.org";
  1709. url->url_port = ag->ag_contact->m_url->url_port;
  1710. ag->ag_expect_leg = ag->ag_default_leg;
  1711. ctx->c_orq =
  1712. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1713. ag->ag_obp,
  1714. SIP_METHOD_MESSAGE,
  1715. (url_string_t *)url,
  1716. SIPTAG_SUBJECT_STR(ctx->c_name),
  1717. SIPTAG_FROM(ag->ag_alice),
  1718. SIPTAG_TO(ag->ag_bob),
  1719. SIPTAG_CONTACT(ag->ag_m_alice),
  1720. TAG_END());
  1721. TEST_1(!client_run(ctx, 200));
  1722. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1723. url->url_port = NULL;
  1724. }
  1725. #if 0 /* This must be run on host *without* proxy */
  1726. {
  1727. /* Test 1.6c
  1728. * Send a message to sip:na.example.org
  1729. * Test outgoing_query_all() with NAPTR "A" flag
  1730. */
  1731. client_t ctx[1] = {{ ag, "Test 1.6c" }};
  1732. url->url_host = "na.example.org";
  1733. ag->ag_expect_leg = ag->ag_default_leg;
  1734. TEST_1(ctx->c_orq =
  1735. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1736. ag->ag_obp,
  1737. SIP_METHOD_MESSAGE,
  1738. (url_string_t *)url,
  1739. SIPTAG_SUBJECT_STR(ctx->c_name),
  1740. SIPTAG_FROM(ag->ag_alice),
  1741. SIPTAG_TO(ag->ag_bob),
  1742. SIPTAG_CONTACT(ag->ag_m_alice),
  1743. TAG_END()));
  1744. TEST_1(!client_run(ctx, 503));
  1745. TEST(ag->ag_latest_leg, ag->ag_default_leg);
  1746. }
  1747. #endif
  1748. {
  1749. /* Test 1.7
  1750. * Send a message to sip:down2.example.org:$port
  1751. * Test A record failover.
  1752. */
  1753. client_t ctx[1] = {{ ag, "Test 1.7: outgoing_make_a_aaaa_query()" }};
  1754. url->url_host = "down2.example.org";
  1755. url->url_port = ag->ag_contact->m_url->url_port;
  1756. ag->ag_expect_leg = ag->ag_default_leg;
  1757. ctx->c_orq =
  1758. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1759. ag->ag_obp,
  1760. SIP_METHOD_MESSAGE,
  1761. (url_string_t *)url,
  1762. SIPTAG_SUBJECT_STR(ctx->c_name),
  1763. SIPTAG_FROM(ag->ag_alice),
  1764. SIPTAG_TO(ag->ag_bob),
  1765. SIPTAG_CONTACT(ag->ag_m_alice),
  1766. TAG_END());
  1767. TEST_1(!client_run(ctx, 200));
  1768. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1769. url->url_params = NULL;
  1770. }
  1771. if (test_nta_sips) {
  1772. /* Test 1.8 - Send a message to sips:example.org
  1773. *
  1774. * Tests sf.net bug #1292657 (SIPS resolving with NAPTR).
  1775. */
  1776. client_t ctx[1] = {{ ag, "Test 1.8" }};
  1777. ag->ag_expect_leg = ag->ag_default_leg;
  1778. ctx->c_orq =
  1779. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1780. ag->ag_obp,
  1781. SIP_METHOD_MESSAGE,
  1782. (url_string_t *)"sips:example.org",
  1783. SIPTAG_SUBJECT_STR(ctx->c_name),
  1784. SIPTAG_FROM(ag->ag_alice),
  1785. SIPTAG_TO(ag->ag_bob),
  1786. SIPTAG_CONTACT(ag->ag_m_alice),
  1787. TAG_END());
  1788. TEST_1(!client_run(ctx, 200));
  1789. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1790. }
  1791. nta_agent_set_params(ag->ag_agent,
  1792. NTATAG_SIP_T1(500),
  1793. NTATAG_SIP_T1X64(64 * 500),
  1794. NTATAG_SIP_T2(NTA_SIP_T2),
  1795. NTATAG_SIP_T4(NTA_SIP_T4),
  1796. TAG_END());
  1797. END();
  1798. }
  1799. /* Test default routing */
  1800. int test_routing(agent_t *ag)
  1801. {
  1802. url_t url[1];
  1803. *url = *ag->ag_aliases->m_url;
  1804. url->url_user = "bob";
  1805. nta_leg_bind(ag->ag_default_leg, leg_callback_200, ag);
  1806. nta_agent_set_params(ag->ag_agent,
  1807. NTATAG_MAXSIZE(2 * 1024 * 1024),
  1808. TAG_END());
  1809. BEGIN();
  1810. {
  1811. /*
  1812. * Send a message from default leg to default leg
  1813. *
  1814. * We are now using url with an explicit port that does not match with
  1815. * our own port number.
  1816. */
  1817. url_t url2[1];
  1818. client_t ctx[1] = {{ ag, "Test 1.2" }};
  1819. *url2 = *url;
  1820. url2->url_port = "9"; /* discard service */
  1821. ag->ag_expect_leg = ag->ag_default_leg;
  1822. ctx->c_orq =
  1823. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  1824. (url_string_t *)url,
  1825. SIP_METHOD_MESSAGE,
  1826. (url_string_t *)url2,
  1827. SIPTAG_SUBJECT_STR(ctx->c_name),
  1828. SIPTAG_FROM(ag->ag_alice),
  1829. SIPTAG_TO(ag->ag_bob),
  1830. SIPTAG_CONTACT(ag->ag_m_alice),
  1831. TAG_END());
  1832. TEST_1(!client_run(ctx, 200));
  1833. TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
  1834. }
  1835. END();
  1836. }
  1837. /* Test dialogs and the tag handling */
  1838. int test_dialog(agent_t *ag)
  1839. {
  1840. BEGIN();
  1841. /*
  1842. * Test establishing a dialog
  1843. *
  1844. * Alice sends a message to Bob, then Bob back to the Alice, and again
  1845. * Alice to Bob.
  1846. */
  1847. ag->ag_alice_leg = nta_leg_tcreate(ag->ag_agent,
  1848. leg_callback_200,
  1849. ag,
  1850. SIPTAG_FROM(ag->ag_alice),
  1851. SIPTAG_TO(ag->ag_bob),
  1852. TAG_END());
  1853. TEST_1(ag->ag_alice_leg);
  1854. TEST_1(nta_leg_tag(ag->ag_alice_leg, NULL));
  1855. {
  1856. client_t ctx[1] = {{ ag, "Test 2.1" }};
  1857. nta_leg_bind(ag->ag_server_leg, new_leg_callback_200, ag);
  1858. /* Send message from Alice to Bob establishing the dialog */
  1859. ag->ag_expect_leg = ag->ag_server_leg;
  1860. ctx->c_orq =
  1861. nta_outgoing_tcreate(ag->ag_alice_leg, outgoing_callback, ctx,
  1862. ag->ag_obp,
  1863. SIP_METHOD_MESSAGE,
  1864. (url_string_t *)ag->ag_m_bob->m_url,
  1865. SIPTAG_SUBJECT_STR(ctx->c_name),
  1866. SIPTAG_FROM(ag->ag_alice),
  1867. SIPTAG_TO(ag->ag_bob),
  1868. SIPTAG_CONTACT(ag->ag_m_alice),
  1869. TAG_END());
  1870. TEST_1(!client_run(ctx, 200));
  1871. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  1872. TEST_1(ag->ag_bob_leg != NULL);
  1873. }
  1874. {
  1875. /* Send message from Bob to Alice */
  1876. client_t ctx[1] = {{ ag, "Test 2.2" }};
  1877. nta_leg_bind(ag->ag_server_leg, leg_callback_200, ag);
  1878. ag->ag_expect_leg = ag->ag_alice_leg;
  1879. ctx->c_orq =
  1880. nta_outgoing_tcreate(ag->ag_bob_leg, outgoing_callback, ctx,
  1881. NULL,
  1882. SIP_METHOD_MESSAGE,
  1883. (url_string_t *)ag->ag_m_alice->m_url,
  1884. SIPTAG_SUBJECT_STR(ctx->c_name),
  1885. TAG_END());
  1886. TEST_1(!client_run(ctx, 200));
  1887. TEST_P(ag->ag_latest_leg, ag->ag_alice_leg);
  1888. }
  1889. {
  1890. /* Send again message from Alice to Bob */
  1891. client_t ctx[1] = {{ ag, "Test 2.3" }};
  1892. ag->ag_expect_leg = ag->ag_bob_leg;
  1893. ctx->c_orq =
  1894. nta_outgoing_tcreate(ag->ag_alice_leg, outgoing_callback, ctx,
  1895. NULL,
  1896. SIP_METHOD_MESSAGE,
  1897. (url_string_t *)ag->ag_m_bob->m_url,
  1898. SIPTAG_SUBJECT_STR(ctx->c_name),
  1899. TAG_END());
  1900. TEST_1(!client_run(ctx, 200));
  1901. TEST_P(ag->ag_latest_leg, ag->ag_bob_leg);
  1902. }
  1903. {
  1904. /* Send message from Bob to Alice
  1905. * This time, however, specify request URI
  1906. */
  1907. client_t ctx[1] = {{ ag, "Test 2.4" }};
  1908. ag->ag_expect_leg = ag->ag_alice_leg;
  1909. ctx->c_orq =
  1910. nta_outgoing_tcreate(ag->ag_bob_leg, outgoing_callback, ctx,
  1911. NULL,
  1912. SIP_METHOD_MESSAGE,
  1913. (url_string_t *)ag->ag_m_alice->m_url,
  1914. SIPTAG_SUBJECT_STR(ctx->c_name),
  1915. TAG_END());
  1916. TEST_1(!client_run(ctx, 200));
  1917. TEST_P(ag->ag_latest_leg, ag->ag_alice_leg);
  1918. }
  1919. nta_leg_destroy(ag->ag_alice_leg), ag->ag_alice_leg = NULL;
  1920. nta_leg_destroy(ag->ag_bob_leg), ag->ag_bob_leg = NULL;
  1921. END();
  1922. }
  1923. #ifndef MSG_TRUNC
  1924. #define MSG_TRUNC 0
  1925. #endif
  1926. static ssize_t recv_udp(agent_t *ag, void *b, size_t size)
  1927. {
  1928. ssize_t n;
  1929. memset(b, 0, size);
  1930. for (;;) {
  1931. su_root_step(ag->ag_root, 10L);
  1932. if (su_wait(ag->ag_sink_wait, 1, 0) == 0) {
  1933. n = su_recv(ag->ag_sink_socket, b, size, MSG_TRUNC);
  1934. if (n > 0)
  1935. return n;
  1936. }
  1937. }
  1938. }
  1939. /* Test merging */
  1940. int test_merging(agent_t *ag)
  1941. {
  1942. BEGIN();
  1943. /*
  1944. * Test merging: send two messages with same
  1945. * from tag/call-id/cseq number to nta,
  1946. * expect 200 and 408.
  1947. */
  1948. char const rfc3261prefix[] = "z9hG4bK";
  1949. char const template[] =
  1950. "%s " URL_PRINT_FORMAT " SIP/2.0\r\n"
  1951. "Via: SIP/2.0/UDP 127.0.0.1:%s;branch=%s.%p\r\n"
  1952. "Via: SIP/2.0/TCP fake.address.for.via.example.net;branch=z9hG4bK.%p\r\n"
  1953. "CSeq: %u %s\r\n"
  1954. "Call-ID: dfsjfhsduifhsjfsfjkfsd.%p@dfsdhfsjkhsdjk\r\n"
  1955. "From: Evil Forker <sip:evel@forker.com>;tag=test_nta-%s\r\n"
  1956. "To: Bob the Builder <sip:bob@example.net>%s\r\n"
  1957. "Content-Length: 0\r\n"
  1958. "\r\n";
  1959. url_t u1[1], u2[2];
  1960. char m1[1024], m2[1024];
  1961. char r1[1024], r2[1024];
  1962. size_t len, l1, l2;
  1963. su_sockaddr_t *su = ag->ag_su_nta;
  1964. socklen_t sulen = ag->ag_su_nta_len;
  1965. /* Empty sink socket */
  1966. su_setblocking(ag->ag_sink_socket, 0);
  1967. while (su_recv(ag->ag_sink_socket, m1, sizeof m1, MSG_TRUNC) >= 0)
  1968. ;
  1969. su_wait(ag->ag_sink_wait, 1, 0);
  1970. su_wait(ag->ag_sink_wait, 1, 0);
  1971. {
  1972. /* RFC 3261 8.2.2.2 Merged Requests:
  1973. If the request has no tag in the To header field, the UAS core MUST
  1974. check the request against ongoing transactions. If the From tag,
  1975. Call-ID, and CSeq exactly match those associated with an ongoing
  1976. transaction, but the request does not match that transaction (based
  1977. on the matching rules in Section 17.2.3), the UAS core SHOULD
  1978. generate a 482 (Loop Detected) response and pass it to the server
  1979. transaction.
  1980. */
  1981. nta_leg_bind(ag->ag_server_leg, leg_callback_200, ag);
  1982. ag->ag_expect_leg = ag->ag_server_leg;
  1983. ag->ag_latest_leg = NULL;
  1984. *u1 = *ag->ag_m_bob->m_url;
  1985. snprintf(m1, sizeof m1,
  1986. template,
  1987. "MESSAGE", URL_PRINT_ARGS(u1),
  1988. /* Via */ ag->ag_sink_port, rfc3261prefix, (void *)m1,
  1989. /* 2nd Via */ (void *)ag,
  1990. /* CSeq */ 13, "MESSAGE",
  1991. /* Call-ID */ (void *)ag,
  1992. /* From tag */ "2.5.1",
  1993. /* To tag */ "");
  1994. l1 = strlen(m1);
  1995. *u2 = *ag->ag_m_bob->m_url;
  1996. snprintf(m2, sizeof m2,
  1997. template,
  1998. "MESSAGE", URL_PRINT_ARGS(u2),
  1999. /* Via */ ag->ag_sink_port, rfc3261prefix, (void *)m2,
  2000. /* 2nd Via */ (void *)ag,
  2001. /* CSeq */ 13, "MESSAGE",
  2002. /* Call-ID */ (void *)ag,
  2003. /* From tag */ "2.5.1",
  2004. /* To tag */ "");
  2005. l2 = strlen(m2);
  2006. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m1, l1, 0, su, sulen) == l1);
  2007. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m2, l2, 0, su, sulen) == l2);
  2008. recv_udp(ag, r1, sizeof r1);
  2009. recv_udp(ag, r2, sizeof r2);
  2010. len = strlen("SIP/2.0 200 ");
  2011. TEST_1(memcmp(r1, "SIP/2.0 200 ", len) == 0);
  2012. TEST_1(memcmp(r2, "SIP/2.0 482 ", len) == 0);
  2013. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  2014. }
  2015. while (su_recv(ag->ag_sink_socket, m1, sizeof m1, MSG_TRUNC) >= 0)
  2016. ;
  2017. {
  2018. /*
  2019. * Check that request with same call-id, cseq and from-tag
  2020. * are not merged if the method is different.
  2021. */
  2022. nta_leg_bind(ag->ag_server_leg, leg_callback_200, ag);
  2023. ag->ag_expect_leg = ag->ag_server_leg;
  2024. ag->ag_latest_leg = NULL;
  2025. *u1 = *ag->ag_m_bob->m_url;
  2026. snprintf(m1, sizeof m1,
  2027. template,
  2028. "MESSAGE", URL_PRINT_ARGS(u1),
  2029. /* Via */ ag->ag_sink_port, rfc3261prefix, (void *)m1,
  2030. /* 2nd Via */ (void *)ag,
  2031. /* CSeq */ 14, "MESSAGE",
  2032. /* Call-ID */ (void *)ag,
  2033. /* From tag */ "2.5.2",
  2034. /* To tag */ "");
  2035. l1 = strlen(m1);
  2036. *u2 = *ag->ag_m_bob->m_url;
  2037. snprintf(m2, sizeof m2,
  2038. template,
  2039. "OPTIONS", URL_PRINT_ARGS(u2),
  2040. /* Via */ ag->ag_sink_port, rfc3261prefix, (void *)m2,
  2041. /* 2nd Via */ (void *)ag,
  2042. /* CSeq */ 14, "OPTIONS",
  2043. /* Call-ID */ (void *)ag,
  2044. /* From tag */ "2.5.2",
  2045. /* To tag */ "");
  2046. l2 = strlen(m2);
  2047. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m1, l1, 0, su, sulen) == l1);
  2048. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m2, l2, 0, su, sulen) == l2);
  2049. recv_udp(ag, r1, sizeof r1);
  2050. recv_udp(ag, r2, sizeof r2);
  2051. len = strlen("SIP/2.0 200 ");
  2052. TEST_1(memcmp(r1, "SIP/2.0 200 ", len) == 0);
  2053. TEST_1(memcmp(r2, "SIP/2.0 482 ", len) != 0);
  2054. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  2055. }
  2056. while (su_recv(ag->ag_sink_socket, m1, sizeof m1, MSG_TRUNC) >= 0)
  2057. ;
  2058. {
  2059. /* test with rfc2543 */
  2060. snprintf(m1, sizeof m1,
  2061. template,
  2062. "MASSAGE", URL_PRINT_ARGS(u1),
  2063. /* Via */ ag->ag_sink_port, "0.", (void *)0,
  2064. /* 2nd Via */ (void *)ag,
  2065. /* CSeq */ 14, "MASSAGE",
  2066. /* Call-ID */ (void *)(ag + 1),
  2067. /* From tag */ "2.5.3",
  2068. /* To tag */ "");
  2069. l1 = strlen(m1);
  2070. u2->url_user = "bob+2";
  2071. snprintf(m2, sizeof m2,
  2072. template,
  2073. "MASSAGE", URL_PRINT_ARGS(u2),
  2074. /* Via */ ag->ag_sink_port, "0.", (void *)0,
  2075. /* 2nd Via */ (void *)ag,
  2076. /* CSeq */ 14, "MASSAGE",
  2077. /* Call-ID */ (void *)(ag + 1),
  2078. /* From tag */ "2.5.3",
  2079. /* To tag */ "");
  2080. l2 = strlen(m2);
  2081. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m1, l1, 0, su, sulen) == l1);
  2082. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m2, l2, 0, su, sulen) == l2);
  2083. recv_udp(ag, r1, sizeof r1);
  2084. recv_udp(ag, r2, sizeof r2);
  2085. l1 = strlen("SIP/2.0 200 ");
  2086. TEST_1(memcmp(r1, "SIP/2.0 200 ", l1) == 0);
  2087. TEST_1(memcmp(r2, "SIP/2.0 482 ", l1) == 0);
  2088. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  2089. }
  2090. while (su_recv(ag->ag_sink_socket, m1, sizeof m1, MSG_TRUNC) >= 0)
  2091. ;
  2092. {
  2093. /* test with to-tag */
  2094. snprintf(m1, sizeof m1,
  2095. template,
  2096. "MESSAGE", URL_PRINT_ARGS(u1),
  2097. /* Via */ ag->ag_sink_port, rfc3261prefix, (void *)m1,
  2098. /* 2nd Via */ (void *)ag,
  2099. /* CSeq */ 15, "MESSAGE",
  2100. /* Call-ID */ (void *)(ag + 2),
  2101. /* From tag */ "2.5.4",
  2102. /* To tag */ ";tag=in-dialog");
  2103. l1 = strlen(m1);
  2104. u2->url_user = "bob+2";
  2105. snprintf(m2, sizeof m2,
  2106. template,
  2107. "MESSAGE", URL_PRINT_ARGS(u2),
  2108. /* Via */ ag->ag_sink_port, rfc3261prefix, (void *)m2,
  2109. /* 2nd Via */ (void *)ag,
  2110. /* CSeq */ 15, "MESSAGE",
  2111. /* Call-ID */ (void *)(ag + 2),
  2112. /* From tag */ "2.5.4",
  2113. /* To tag */ ";tag=in-dialog");
  2114. l2 = strlen(m2);
  2115. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m1, l1, 0, su, sulen) == l1);
  2116. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m2, l2, 0, su, sulen) == l2);
  2117. recv_udp(ag, r1, sizeof r1);
  2118. recv_udp(ag, r2, sizeof r2);
  2119. l1 = strlen("SIP/2.0 200 ");
  2120. TEST_1(memcmp(r1, "SIP/2.0 200 ", l1) == 0);
  2121. TEST_1(memcmp(r2, "SIP/2.0 482 ", l1) != 0);
  2122. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  2123. }
  2124. while (su_recv(ag->ag_sink_socket, m1, sizeof m1, MSG_TRUNC) >= 0)
  2125. ;
  2126. {
  2127. /* test with rfc2543 and to-tag */
  2128. snprintf(m1, sizeof m1,
  2129. template,
  2130. "MESSAGE", URL_PRINT_ARGS(u1),
  2131. /* Via */ ag->ag_sink_port, "0.", (void *)0,
  2132. /* 2nd Via */ (void *)ag,
  2133. /* CSeq */ 15, "MESSAGE",
  2134. /* Call-ID */ (void *)(ag + 2),
  2135. /* From tag */ "2.5.5",
  2136. /* To tag */ ";tag=in-dialog");
  2137. l1 = strlen(m1);
  2138. snprintf(m2, sizeof m2,
  2139. template,
  2140. "MESSAGE", URL_PRINT_ARGS(u2),
  2141. /* Via */ ag->ag_sink_port, "0.", (void *)0,
  2142. /* 2nd Via */ (void *)ag,
  2143. /* CSeq */ 15, "MESSAGE",
  2144. /* Call-ID */ (void *)(ag + 2),
  2145. /* From tag */ "2.5.5",
  2146. /* To tag */ ";tag=in-dialog");
  2147. l2 = strlen(m2);
  2148. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m1, l1, 0, su, sulen) == l1);
  2149. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m2, l2, 0, su, sulen) == l2);
  2150. recv_udp(ag, r1, sizeof r1);
  2151. recv_udp(ag, r2, sizeof r2);
  2152. l1 = strlen("SIP/2.0 200 ");
  2153. TEST_1(memcmp(r1, "SIP/2.0 200 ", l1) == 0);
  2154. TEST_1(memcmp(r2, "SIP/2.0 482 ", l1) != 0);
  2155. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  2156. }
  2157. while (su_recv(ag->ag_sink_socket, m1, sizeof m1, MSG_TRUNC) >= 0)
  2158. ;
  2159. {
  2160. /* test INVITE/CANCEL with rfc2543 */
  2161. char const template2[] =
  2162. "%s " URL_PRINT_FORMAT " SIP/2.0\r\n"
  2163. "Via: SIP/2.0/UDP 127.0.0.1:%s;x-kuik=%p\r\n"
  2164. "CSeq: %u %s\r\n"
  2165. "Call-ID: %p.dfsdhfsjkhsdjk.dfsjfhsduifhsjfsfjkfsd\r\n"
  2166. "From: Evil Forker <sip:evel@forker.com>;tag=test_nta-%s\r\n"
  2167. "To: Bob the Builder <sip:bob@example.net>%s\r\n"
  2168. "Content-Length: 0\r\n"
  2169. "\r\n";
  2170. nta_leg_bind(ag->ag_server_leg, new_leg_callback_180, ag);
  2171. snprintf(m1, sizeof m1,
  2172. template2,
  2173. "INVITE", URL_PRINT_ARGS(u1),
  2174. /* Via */ ag->ag_sink_port, m1,
  2175. /* CSeq */ 15, "INVITE",
  2176. /* Call-ID */ (void *)(ag + 2),
  2177. /* From tag */ "2.6.1",
  2178. /* To tag */ "");
  2179. l1 = strlen(m1);
  2180. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m1, l1, 0, su, sulen) == l1);
  2181. recv_udp(ag, r1, sizeof r1);
  2182. l1 = strlen("SIP/2.0 180 ");
  2183. TEST_1(memcmp(r1, "SIP/2.0 180 ", l1) == 0);
  2184. TEST_1(ag->ag_irq);
  2185. nta_incoming_bind(ag->ag_irq, wait_for_ack_or_cancel, ag);
  2186. snprintf(m2, sizeof m2,
  2187. template2,
  2188. "CANCEL", URL_PRINT_ARGS(u1),
  2189. /* Via */ ag->ag_sink_port, m1,
  2190. /* CSeq */ 15, "CANCEL",
  2191. /* Call-ID */ (void *)(ag + 2),
  2192. /* From tag */ "2.6.1",
  2193. /* To tag */ "");
  2194. l2 = strlen(m2);
  2195. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m2, l2, 0, su, sulen) == l2);
  2196. recv_udp(ag, r1, sizeof r1);
  2197. recv_udp(ag, r2, sizeof r2);
  2198. l1 = strlen("SIP/2.0 200 ");
  2199. TEST_1(strstr(r1, "15 CANCEL"));
  2200. TEST_1(memcmp(r1, "SIP/2.0 200 ", l1) == 0);
  2201. TEST_1(strstr(r2, "15 INVITE"));
  2202. TEST_1(memcmp(r2, "SIP/2.0 487 ", l1) == 0);
  2203. TEST_1(nta_incoming_status(ag->ag_irq) == 487);
  2204. snprintf(m2, sizeof m2,
  2205. template2,
  2206. "ACK", URL_PRINT_ARGS(u1),
  2207. /* Via */ ag->ag_sink_port, m1,
  2208. /* CSeq */ 15, "ACK",
  2209. /* Call-ID */ (void *)(ag + 2),
  2210. /* From tag */ "2.6.1",
  2211. /* To tag */ "");
  2212. l2 = strlen(m2);
  2213. TEST_1((size_t)su_sendto(ag->ag_sink_socket, m2, l2, 0, su, sulen) == l2);
  2214. nta_leg_destroy(ag->ag_bob_leg); ag->ag_bob_leg = NULL;
  2215. }
  2216. while (su_recv(ag->ag_sink_socket, m1, sizeof m1, MSG_TRUNC) >= 0)
  2217. ;
  2218. END();
  2219. }
  2220. static
  2221. int wait_for_ack_or_cancel(agent_t *ag,
  2222. nta_incoming_t *irq,
  2223. sip_t const *sip)
  2224. {
  2225. sip_method_t method;
  2226. method = sip ? sip->sip_request->rq_method : sip_method_unknown;
  2227. if (method == sip_method_cancel) {
  2228. nta_incoming_treply(ag->ag_irq, SIP_487_REQUEST_CANCELLED, TAG_END());
  2229. }
  2230. else if (method == sip_method_ack) {
  2231. nta_incoming_destroy(irq);
  2232. ag->ag_irq = NULL;
  2233. ag->ag_running = 0;
  2234. }
  2235. else { /* Timeout */
  2236. nta_incoming_destroy(irq);
  2237. ag->ag_irq = NULL;
  2238. ag->ag_running = 0;
  2239. }
  2240. return 0;
  2241. }
  2242. /* ---------------------------------------------------------------------- */
  2243. /* Test INVITE, dialogs */
  2244. static
  2245. int test_for_ack(agent_t *ag,
  2246. nta_incoming_t *irq,
  2247. sip_t const *sip)
  2248. {
  2249. sip_method_t method;
  2250. BEGIN();
  2251. method = sip ? sip->sip_request->rq_method : sip_method_unknown;
  2252. nta_incoming_destroy(irq);
  2253. TEST_P(irq, ag->ag_irq);
  2254. ag->ag_irq = NULL;
  2255. TEST(method, sip_method_ack);
  2256. ag->ag_running = 0;
  2257. END();
  2258. }
  2259. static
  2260. int test_for_prack(agent_t *ag,
  2261. nta_reliable_t *rel,
  2262. nta_incoming_t *prack,
  2263. sip_t const *sip)
  2264. {
  2265. sip_method_t method = sip ? sip->sip_request->rq_method : sip_method_unknown;
  2266. nta_incoming_treply(ag->ag_irq,
  2267. SIP_200_OK,
  2268. SIPTAG_CONTACT(ag->ag_m_alice),
  2269. TAG_END());
  2270. TEST(method, sip_method_prack);
  2271. return 200;
  2272. }
  2273. int alice_leg_callback(agent_t *ag,
  2274. nta_leg_t *leg,
  2275. nta_incoming_t *irq,
  2276. sip_t const *sip)
  2277. {
  2278. BEGIN();
  2279. if (tstflags & tst_verbatim) {
  2280. printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
  2281. name, __func__, sip->sip_request->rq_method_name,
  2282. URL_PRINT_ARGS(sip->sip_request->rq_url),
  2283. sip->sip_request->rq_version);
  2284. }
  2285. TEST_1(sip->sip_content_length);
  2286. TEST_1(sip->sip_via);
  2287. TEST_1(sip->sip_from && sip->sip_from->a_tag);
  2288. if (sip->sip_request->rq_method == sip_method_prack)
  2289. return 481;
  2290. ag->ag_latest_leg = leg;
  2291. if (leg != ag->ag_alice_leg) {
  2292. leg_match(ag, leg, 1, __func__);
  2293. return 500;
  2294. }
  2295. if (sip->sip_request->rq_method == sip_method_invite) {
  2296. TEST_1(sip_has_feature(sip->sip_supported, "100rel"));
  2297. nta_incoming_bind(irq, test_for_ack, ag);
  2298. nta_incoming_treply(irq, SIP_100_TRYING, TAG_END());
  2299. nta_agent_set_params(ag->ag_agent,
  2300. NTATAG_DEBUG_DROP_PROB(ag->ag_drop),
  2301. TAG_END());
  2302. ag->ag_reliable =
  2303. nta_reliable_treply(irq,
  2304. NULL, NULL,
  2305. SIP_183_SESSION_PROGRESS,
  2306. SIPTAG_CONTENT_TYPE(ag->ag_content_type),
  2307. SIPTAG_PAYLOAD(ag->ag_payload),
  2308. SIPTAG_CONTACT(ag->ag_m_alice),
  2309. TAG_END());
  2310. TEST_1(ag->ag_reliable);
  2311. ag->ag_reliable =
  2312. nta_reliable_treply(irq,
  2313. NULL, NULL,
  2314. 184, "Next",
  2315. SIPTAG_CONTACT(ag->ag_m_alice),
  2316. TAG_END());
  2317. TEST_1(ag->ag_reliable);
  2318. ag->ag_reliable =
  2319. nta_reliable_treply(irq,
  2320. test_for_prack, ag,
  2321. 185, "Last",
  2322. SIPTAG_CONTACT(ag->ag_m_alice),
  2323. TAG_END());
  2324. TEST_1(ag->ag_reliable);
  2325. ag->ag_irq = irq;
  2326. return 0;
  2327. }
  2328. if (sip->sip_request->rq_method == sip_method_bye) {
  2329. leg_zap(ag, leg);
  2330. }
  2331. if (sip)
  2332. return 200;
  2333. END();
  2334. }
  2335. int bob_leg_callback(agent_t *ag,
  2336. nta_leg_t *leg,
  2337. nta_incoming_t *irq,
  2338. sip_t const *sip)
  2339. {
  2340. BEGIN();
  2341. if (tstflags & tst_verbatim) {
  2342. printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
  2343. name, __func__, sip->sip_request->rq_method_name,
  2344. URL_PRINT_ARGS(sip->sip_request->rq_url),
  2345. sip->sip_request->rq_version);
  2346. }
  2347. TEST_1(sip->sip_content_length);
  2348. TEST_1(sip->sip_via);
  2349. TEST_1(sip->sip_from && sip->sip_from->a_tag);
  2350. if (sip->sip_request->rq_method == sip_method_prack)
  2351. return 481;
  2352. ag->ag_latest_leg = leg;
  2353. if (ag->ag_bob_leg && leg != ag->ag_bob_leg) {
  2354. leg_match(ag, leg, 1, __func__);
  2355. return 500;
  2356. }
  2357. if (ag->ag_bob_leg == NULL) {
  2358. nta_leg_bind(leg, leg_callback_500, ag);
  2359. ag->ag_bob_leg = nta_leg_tcreate(ag->ag_agent,
  2360. bob_leg_callback,
  2361. ag,
  2362. SIPTAG_CALL_ID(sip->sip_call_id),
  2363. SIPTAG_FROM(sip->sip_to),
  2364. SIPTAG_TO(sip->sip_from),
  2365. TAG_END());
  2366. TEST_1(ag->ag_bob_leg);
  2367. TEST_1(nta_leg_tag(ag->ag_bob_leg, NULL));
  2368. TEST_1(nta_leg_get_tag(ag->ag_bob_leg));
  2369. TEST_1(nta_incoming_tag(irq, nta_leg_get_tag(ag->ag_bob_leg)));
  2370. TEST(nta_leg_server_route(ag->ag_bob_leg,
  2371. sip->sip_record_route,
  2372. sip->sip_contact), 0);
  2373. }
  2374. if (sip->sip_request->rq_method != sip_method_invite) {
  2375. return 200;
  2376. } else {
  2377. nta_incoming_bind(irq, test_for_ack, ag);
  2378. #if 1
  2379. nta_incoming_treply(irq,
  2380. SIP_180_RINGING,
  2381. SIPTAG_CONTACT(ag->ag_m_bob),
  2382. TAG_END());
  2383. nta_incoming_treply(irq,
  2384. SIP_180_RINGING,
  2385. SIPTAG_CONTACT(ag->ag_m_bob),
  2386. TAG_END());
  2387. #endif
  2388. nta_incoming_treply(irq,
  2389. SIP_200_OK,
  2390. SIPTAG_CONTENT_TYPE(ag->ag_content_type),
  2391. SIPTAG_PAYLOAD(ag->ag_payload),
  2392. SIPTAG_CONTACT(ag->ag_m_bob),
  2393. TAG_END());
  2394. ag->ag_irq = irq;
  2395. }
  2396. END();
  2397. }
  2398. static
  2399. int invite_client_deinit(client_t *c)
  2400. {
  2401. agent_t *ag = c->c_ag;
  2402. invite_client_t *ic = (invite_client_t *)c;
  2403. if (ic->ic_orq) nta_outgoing_destroy(ic->ic_orq), ic->ic_orq = NULL;
  2404. if (ic->ic_tag) su_free(ag->ag_home, ic->ic_tag), ic->ic_tag = NULL;
  2405. return 0;
  2406. }
  2407. static
  2408. int check_prack_sending(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  2409. {
  2410. agent_t *ag = ctx->c_ag;
  2411. int status = ctx->c_status;
  2412. if (100 < status && status < 200) {
  2413. if (sip->sip_require && sip_has_feature(sip->sip_require, "100rel")) {
  2414. nta_outgoing_t *prack = NULL;
  2415. TEST_1(sip->sip_rseq);
  2416. prack = nta_outgoing_prack(ag->ag_call_leg, orq, NULL, NULL,
  2417. NULL,
  2418. sip,
  2419. TAG_END());
  2420. nta_outgoing_destroy(prack);
  2421. TEST_1(prack != NULL);
  2422. }
  2423. }
  2424. return 0;
  2425. }
  2426. static
  2427. int check_leg_tagging(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  2428. {
  2429. agent_t *ag = ctx->c_ag;
  2430. int status = ctx->c_status;
  2431. if (200 <= status && status < 300) {
  2432. TEST_1(nta_leg_rtag(ag->ag_call_leg, sip->sip_to->a_tag));
  2433. TEST(nta_leg_client_route(ag->ag_call_leg,
  2434. sip->sip_record_route,
  2435. sip->sip_contact), 0);
  2436. }
  2437. return 0;
  2438. }
  2439. static
  2440. int check_tu_ack(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  2441. {
  2442. agent_t *ag = ctx->c_ag;
  2443. int status = ctx->c_status;
  2444. if (200 <= status && status < 300) {
  2445. nta_outgoing_t *ack;
  2446. ack = nta_outgoing_tcreate(ag->ag_call_leg, NULL, NULL,
  2447. NULL,
  2448. SIP_METHOD_ACK,
  2449. NULL,
  2450. SIPTAG_CSEQ(sip->sip_cseq),
  2451. TAG_END());
  2452. nta_outgoing_destroy(ack);
  2453. TEST_1(ack);
  2454. }
  2455. return 0;
  2456. }
  2457. static
  2458. int check_final_error(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  2459. {
  2460. agent_t *ag = ctx->c_ag;
  2461. int status = ctx->c_status;
  2462. if (status >= 300)
  2463. ag->ag_call_leg = NULL;
  2464. return 0;
  2465. }
  2466. /** Cancel call after receiving 1XX response */
  2467. static
  2468. int cancel_invite(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
  2469. {
  2470. int status = ctx->c_status;
  2471. if (100 < status && status < 200) {
  2472. nta_outgoing_cancel(orq);
  2473. ctx->c_status = 0;
  2474. }
  2475. else if (status >= 200) {
  2476. TEST_1(status == 487 || status == 504);
  2477. }
  2478. return 0;
  2479. }
  2480. static client_check_f * const checks_for_invite[] = {
  2481. client_check_to_tag,
  2482. check_leg_tagging,
  2483. check_tu_ack,
  2484. check_final_error,
  2485. NULL,
  2486. };
  2487. static client_check_f * const checks_for_reinvite[] = {
  2488. client_check_to_tag,
  2489. check_prack_sending,
  2490. check_leg_tagging,
  2491. check_tu_ack,
  2492. NULL,
  2493. };
  2494. int test_call(agent_t *ag)
  2495. {
  2496. sip_content_type_t *ct = ag->ag_content_type;
  2497. sip_payload_t *sdp = ag->ag_payload;
  2498. nta_leg_t *old_leg;
  2499. sip_replaces_t *r1, *r2;
  2500. BEGIN();
  2501. {
  2502. invite_client_t ic[1] =
  2503. {{{{ ag, "Call 1", NULL, checks_for_invite, invite_client_deinit }}}};
  2504. client_t *ctx = ic->ic_client;
  2505. /*
  2506. * Test establishing a call
  2507. *
  2508. * Alice sends a INVITE to Bob, then Bob sends 200 Ok.
  2509. */
  2510. ag->ag_alice_leg = nta_leg_tcreate(ag->ag_agent,
  2511. alice_leg_callback,
  2512. ag,
  2513. SIPTAG_FROM(ag->ag_alice),
  2514. SIPTAG_TO(ag->ag_bob),
  2515. TAG_END());
  2516. TEST_1(ag->ag_alice_leg);
  2517. TEST_1(nta_leg_tag(ag->ag_alice_leg, NULL));
  2518. nta_leg_bind(ag->ag_server_leg, bob_leg_callback, ag);
  2519. /* Send INVITE */
  2520. ag->ag_expect_leg = ag->ag_server_leg;
  2521. ctx->c_orq =
  2522. nta_outgoing_tcreate(ag->ag_call_leg = ag->ag_alice_leg,
  2523. outgoing_callback, ctx,
  2524. ag->ag_obp,
  2525. SIP_METHOD_INVITE,
  2526. (url_string_t *)ag->ag_m_bob->m_url,
  2527. SIPTAG_SUBJECT_STR(ctx->c_name),
  2528. SIPTAG_CONTACT(ag->ag_m_alice),
  2529. SIPTAG_CONTENT_TYPE(ct),
  2530. SIPTAG_ACCEPT_CONTACT_STR("*;audio"),
  2531. SIPTAG_PAYLOAD(sdp),
  2532. NTATAG_USE_TIMESTAMP(1),
  2533. NTATAG_PASS_100(1),
  2534. TAG_END());
  2535. TEST_1(ctx->c_orq);
  2536. /* Try to CANCEL it immediately */
  2537. TEST_1(nta_outgoing_cancel(ctx->c_orq) == 0);
  2538. /* As Bob immediately answers INVITE with 200 Ok,
  2539. cancel should be answered with 481 and 200 Ok is returned to INVITE. */
  2540. TEST_1(!client_run(ctx, 200));
  2541. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  2542. TEST_1(ag->ag_bob_leg != NULL);
  2543. }
  2544. TEST_1(r1 = nta_leg_make_replaces(ag->ag_alice_leg, ag->ag_home, 0));
  2545. TEST_1(r2 = sip_replaces_format(ag->ag_home,
  2546. "%s;from-tag=%s;to-tag=%s",
  2547. r1->rp_call_id,
  2548. r1->rp_to_tag,
  2549. r1->rp_from_tag));
  2550. TEST_P(ag->ag_alice_leg, nta_leg_by_replaces(ag->ag_agent, r2));
  2551. TEST_P(ag->ag_bob_leg, nta_leg_by_replaces(ag->ag_agent, r1));
  2552. {
  2553. invite_client_t ic[1] =
  2554. {{{{
  2555. ag, "Re-INVITE in Call 1",
  2556. NULL, checks_for_reinvite, invite_client_deinit
  2557. }}}};
  2558. client_t *ctx = ic->ic_client;
  2559. /* Re-INVITE from Bob to Alice.
  2560. *
  2561. * Alice first sends 183, waits for PRACK, then sends 184 and 185,
  2562. * waits for PRACKs, then sends 200, waits for ACK.
  2563. */
  2564. ag->ag_expect_leg = ag->ag_alice_leg;
  2565. ctx->c_orq =
  2566. nta_outgoing_tcreate(ag->ag_call_leg = ag->ag_bob_leg,
  2567. outgoing_callback, ctx,
  2568. NULL,
  2569. SIP_METHOD_INVITE,
  2570. NULL,
  2571. SIPTAG_SUBJECT_STR(ctx->c_name),
  2572. SIPTAG_CONTACT(ag->ag_m_bob),
  2573. SIPTAG_SUPPORTED_STR("foo"),
  2574. SIPTAG_CONTENT_TYPE(ct),
  2575. SIPTAG_PAYLOAD(sdp),
  2576. TAG_END());
  2577. TEST_1(!client_run(ctx, 200));
  2578. TEST_P(ag->ag_latest_leg, ag->ag_alice_leg);
  2579. }
  2580. {
  2581. client_t ctx[1] = {{ ag, "Hangup" }};
  2582. nta_agent_set_params(ag->ag_agent,
  2583. NTATAG_DEBUG_DROP_PROB(0),
  2584. TAG_END());
  2585. /* Send BYE from Bob to Alice */
  2586. old_leg = ag->ag_expect_leg = ag->ag_alice_leg;
  2587. ctx->c_orq =
  2588. nta_outgoing_tcreate(ag->ag_bob_leg, outgoing_callback, ctx,
  2589. NULL,
  2590. SIP_METHOD_BYE,
  2591. NULL,
  2592. SIPTAG_SUBJECT_STR(ctx->c_name),
  2593. SIPTAG_FROM(ag->ag_alice),
  2594. SIPTAG_TO(ag->ag_bob),
  2595. SIPTAG_CONTACT(ag->ag_m_alice),
  2596. SIPTAG_CONTENT_TYPE(ct),
  2597. SIPTAG_PAYLOAD(sdp),
  2598. TAG_END());
  2599. TEST_1(!client_run(ctx, 200));
  2600. TEST_P(ag->ag_latest_leg, old_leg);
  2601. TEST_P(ag->ag_alice_leg, NULL);
  2602. }
  2603. nta_leg_destroy(ag->ag_bob_leg), ag->ag_bob_leg = NULL;
  2604. ag->ag_latest_leg = NULL;
  2605. ag->ag_call_leg = NULL;
  2606. END();
  2607. }
  2608. /* ========================================================================== */
  2609. /* Test early dialogs, PRACK */
  2610. int test_for_ack_or_timeout(agent_t *ag,
  2611. nta_incoming_t *irq,
  2612. sip_t const *sip)
  2613. {
  2614. BEGIN();
  2615. sip_method_t method = sip ? sip->sip_request->rq_method : sip_method_unknown;
  2616. if (method == sip_method_ack) {
  2617. TEST(method, sip_method_ack);
  2618. ag->ag_acked = 1;
  2619. }
  2620. else if (method == sip_method_cancel) {
  2621. nta_incoming_treply(irq, SIP_487_REQUEST_CANCELLED, TAG_END());
  2622. ag->ag_canceled = 1;
  2623. }
  2624. else {
  2625. if (ag->ag_bob_leg) {
  2626. nta_leg_destroy(ag->ag_bob_leg), ag->ag_bob_leg = NULL;
  2627. }
  2628. }
  2629. nta_incoming_destroy(irq);
  2630. TEST_P(irq, ag->ag_irq);
  2631. ag->ag_irq = NULL;
  2632. END();
  2633. }
  2634. /* */
  2635. int bob_leg_callback2(agent_t *ag,
  2636. nta_leg_t *leg,
  2637. nta_incoming_t *irq,
  2638. sip_t const *sip)
  2639. {
  2640. BEGIN();
  2641. if (tstflags & tst_verbatim) {
  2642. printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
  2643. name, __func__, sip->sip_request->rq_method_name,
  2644. URL_PRINT_ARGS(sip->sip_request->rq_url),
  2645. sip->sip_request->rq_version);
  2646. }
  2647. TEST_1(sip->sip_content_length);
  2648. TEST_1(sip->sip_via);
  2649. TEST_1(sip->sip_from && sip->sip_from->a_tag);
  2650. ag->ag_latest_leg = leg;
  2651. if (ag->ag_bob_leg && leg != ag->ag_bob_leg) {
  2652. leg_match(ag, leg, 1, __func__);
  2653. return 500;
  2654. }
  2655. if (ag->ag_bob_leg == NULL) {
  2656. nta_leg_bind(leg, leg_callback_500, ag);
  2657. ag->ag_bob_leg = nta_leg_tcreate(ag->ag_agent,
  2658. bob_leg_callback,
  2659. ag,
  2660. SIPTAG_CALL_ID(sip->sip_call_id),
  2661. SIPTAG_FROM(sip->sip_to),
  2662. SIPTAG_TO(sip->sip_from),
  2663. TAG_END());
  2664. TEST_1(ag->ag_bob_leg);
  2665. TEST_1(nta_leg_tag(ag->ag_bob_leg, NULL));
  2666. TEST_1(nta_leg_get_tag(ag->ag_bob_leg));
  2667. TEST_1(nta_incoming_tag(irq, nta_leg_get_tag(ag->ag_bob_leg)));
  2668. TEST(nta_leg_server_route(ag->ag_bob_leg,
  2669. sip->sip_record_route,
  2670. sip->sip_contact), 0);
  2671. }
  2672. if (sip->sip_request->rq_method != sip_method_invite) {
  2673. return 200;
  2674. }
  2675. nta_incoming_bind(irq, test_for_ack_or_timeout, ag);
  2676. nta_incoming_treply(irq,
  2677. SIP_183_SESSION_PROGRESS,
  2678. SIPTAG_CONTENT_TYPE(ag->ag_content_type),
  2679. SIPTAG_PAYLOAD(ag->ag_payload),
  2680. SIPTAG_CONTACT(ag->ag_m_bob),
  2681. TAG_END());
  2682. if (0)
  2683. nta_incoming_treply(irq,
  2684. SIP_180_RINGING,
  2685. SIPTAG_CONTENT_TYPE(ag->ag_content_type),
  2686. SIPTAG_PAYLOAD(ag->ag_payload),
  2687. SIPTAG_CONTACT(ag->ag_m_bob),
  2688. TAG_END());
  2689. nta_incoming_treply(irq,
  2690. SIP_200_OK,
  2691. SIPTAG_CONTACT(ag->ag_m_bob),
  2692. TAG_END());
  2693. ag->ag_irq = irq;
  2694. END();
  2695. }
  2696. /** Fork the original INVITE. */
  2697. static
  2698. int check_orq_tagging(client_t *ctx,
  2699. nta_outgoing_t *orq,
  2700. sip_t const *sip)
  2701. {
  2702. agent_t *ag = ctx->c_ag;
  2703. int status = ctx->c_status;
  2704. invite_client_t *ic = (invite_client_t *)ctx;
  2705. if (100 < status && status < 200) {
  2706. TEST_1(sip->sip_rseq);
  2707. TEST_1(sip->sip_to->a_tag);
  2708. TEST_1(orq == ctx->c_orq);
  2709. TEST_1(ic); TEST_1(ic->ic_orq == NULL);
  2710. TEST_1(ic->ic_tag == NULL);
  2711. ic->ic_orq = orq;
  2712. ic->ic_tag = su_strdup(ag->ag_home, sip->sip_to->a_tag); TEST_1(ic->ic_tag);
  2713. ic->ic_tag_status = status;
  2714. TEST_S(nta_leg_rtag(ag->ag_call_leg, ic->ic_tag), ic->ic_tag);
  2715. TEST(nta_leg_client_route(ag->ag_call_leg,
  2716. sip->sip_record_route,
  2717. sip->sip_contact), 0);
  2718. orq = nta_outgoing_tagged(orq,
  2719. outgoing_callback,
  2720. ctx,
  2721. ic->ic_tag,
  2722. sip->sip_rseq);
  2723. TEST_1(orq);
  2724. if (ic->ic_orq != ctx->c_orq)
  2725. nta_outgoing_destroy(ctx->c_orq);
  2726. ctx->c_orq = orq;
  2727. TEST_1(ctx->c_checks && ctx->c_checks[0] == check_orq_tagging);
  2728. ctx->c_checks++;
  2729. }
  2730. return 0;
  2731. }
  2732. static client_check_f * const checks_for_100rel[] = {
  2733. check_orq_tagging,
  2734. client_check_to_tag,
  2735. check_prack_sending,
  2736. check_leg_tagging,
  2737. check_tu_ack,
  2738. NULL,
  2739. };
  2740. static int process_prack(nta_reliable_magic_t *arg,
  2741. nta_reliable_t *rel,
  2742. nta_incoming_t *irq,
  2743. sip_t const *sip)
  2744. {
  2745. agent_t *ag = (agent_t *)arg;
  2746. if (irq) {
  2747. return 200;
  2748. }
  2749. else if (ag->ag_irq) {
  2750. nta_incoming_treply(ag->ag_irq,
  2751. 504, "Reliable Response Timeout",
  2752. TAG_END());
  2753. nta_incoming_destroy(ag->ag_irq);
  2754. return 487;
  2755. }
  2756. return 487;
  2757. }
  2758. /* respond with 183 when receiving invite */
  2759. int bob_leg_callback3(agent_t *ag,
  2760. nta_leg_t *leg,
  2761. nta_incoming_t *irq,
  2762. sip_t const *sip)
  2763. {
  2764. BEGIN();
  2765. if (tstflags & tst_verbatim) {
  2766. printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
  2767. name, __func__, sip->sip_request->rq_method_name,
  2768. URL_PRINT_ARGS(sip->sip_request->rq_url),
  2769. sip->sip_request->rq_version);
  2770. }
  2771. TEST_1(sip->sip_content_length);
  2772. TEST_1(sip->sip_via);
  2773. TEST_1(sip->sip_from && sip->sip_from->a_tag);
  2774. ag->ag_latest_leg = leg;
  2775. if (ag->ag_bob_leg && leg != ag->ag_bob_leg) {
  2776. leg_match(ag, leg, 1, __func__);
  2777. return 500;
  2778. }
  2779. if (ag->ag_bob_leg == NULL) {
  2780. nta_leg_bind(leg, leg_callback_500, ag);
  2781. ag->ag_bob_leg = nta_leg_tcreate(ag->ag_agent,
  2782. bob_leg_callback,
  2783. ag,
  2784. SIPTAG_CALL_ID(sip->sip_call_id),
  2785. SIPTAG_FROM(sip->sip_to),
  2786. SIPTAG_TO(sip->sip_from),
  2787. TAG_END());
  2788. TEST_1(ag->ag_bob_leg);
  2789. TEST_1(nta_leg_tag(ag->ag_bob_leg, NULL));
  2790. TEST_1(nta_leg_get_tag(ag->ag_bob_leg));
  2791. TEST_1(nta_incoming_tag(irq, nta_leg_get_tag(ag->ag_bob_leg)));
  2792. TEST(nta_leg_server_route(ag->ag_bob_leg,
  2793. sip->sip_record_route,
  2794. sip->sip_contact), 0);
  2795. }
  2796. if (sip->sip_request->rq_method != sip_method_invite) {
  2797. return 200;
  2798. }
  2799. else {
  2800. nta_reliable_t *rel;
  2801. nta_incoming_bind(irq, test_for_ack_or_timeout, ag);
  2802. rel = nta_reliable_treply(irq, process_prack, ag,
  2803. SIP_183_SESSION_PROGRESS,
  2804. SIPTAG_CONTENT_TYPE(ag->ag_content_type),
  2805. SIPTAG_PAYLOAD(ag->ag_payload),
  2806. SIPTAG_CONTACT(ag->ag_m_bob),
  2807. TAG_END());
  2808. ag->ag_irq = irq;
  2809. }
  2810. END();
  2811. }
  2812. /*
  2813. * Test establishing a call with an early dialog / 100 rel / timeout
  2814. *
  2815. * Alice sends a INVITE to Bob, then Bob sends 183, Alice sends PRACK,
  2816. * Bob sends 200 to PRACK, Bob sends 200 to INVITE.
  2817. * Bob sends BYE, Alice 200.
  2818. */
  2819. int test_prack(agent_t *ag)
  2820. {
  2821. BEGIN();
  2822. sip_content_type_t *ct = ag->ag_content_type;
  2823. sip_payload_t *sdp = ag->ag_payload;
  2824. nta_leg_t *old_leg;
  2825. {
  2826. /* Send a PRACK from default leg, NTA responds to it with error */
  2827. url_t url[1];
  2828. client_t ctx[1] = {{ ag, "Test 1.1" }};
  2829. *url = *ag->ag_aliases->m_url;
  2830. url->url_user = "bob";
  2831. ag->ag_expect_leg = ag->ag_server_leg;
  2832. ag->ag_latest_leg = NULL;
  2833. ctx->c_orq =
  2834. nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
  2835. ag->ag_obp,
  2836. SIP_METHOD_PRACK,
  2837. (url_string_t *)url,
  2838. SIPTAG_SUBJECT_STR(ctx->c_name),
  2839. SIPTAG_FROM(ag->ag_alice),
  2840. SIPTAG_TO(ag->ag_bob),
  2841. SIPTAG_CONTACT(ag->ag_m_alice),
  2842. SIPTAG_RACK_STR("1432432 42332432 INVITE"),
  2843. TAG_END());
  2844. TEST_1(!client_run(ctx, 481));
  2845. TEST_P(ag->ag_latest_leg, NULL);
  2846. }
  2847. ag->ag_alice_leg = nta_leg_tcreate(ag->ag_agent,
  2848. alice_leg_callback,
  2849. ag,
  2850. SIPTAG_FROM(ag->ag_alice),
  2851. SIPTAG_TO(ag->ag_bob),
  2852. TAG_END());
  2853. TEST_1(ag->ag_alice_leg);
  2854. TEST_1(nta_leg_tag(ag->ag_alice_leg, NULL));
  2855. /* Send INVITE */
  2856. {
  2857. invite_client_t ic[1] =
  2858. {{{{ ag, "Call 2", NULL, checks_for_100rel, invite_client_deinit }}}};
  2859. client_t *ctx = ic->ic_client;
  2860. nta_leg_bind(ag->ag_server_leg, bob_leg_callback2, ag);
  2861. ag->ag_expect_leg = ag->ag_server_leg;
  2862. ctx->c_orq =
  2863. nta_outgoing_tcreate(ag->ag_call_leg = ag->ag_alice_leg,
  2864. outgoing_callback, ctx,
  2865. ag->ag_obp,
  2866. SIP_METHOD_INVITE,
  2867. (url_string_t *)ag->ag_m_bob->m_url,
  2868. SIPTAG_SUBJECT_STR(ctx->c_name),
  2869. SIPTAG_CONTACT(ag->ag_m_alice),
  2870. SIPTAG_REQUIRE_STR("100rel"),
  2871. SIPTAG_CONTENT_TYPE(ct),
  2872. SIPTAG_PAYLOAD(sdp),
  2873. TAG_END());
  2874. TEST_1(!client_run_until_acked(ctx, 200));
  2875. /*TEST(ic->ic_tag_status, 183); */
  2876. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  2877. TEST_1(ag->ag_bob_leg != NULL);
  2878. }
  2879. {
  2880. client_t ctx[1] = {{ ag, "Hangup" }};
  2881. /* Send BYE from Bob to Alice */
  2882. old_leg = ag->ag_expect_leg = ag->ag_alice_leg;
  2883. ctx->c_orq =
  2884. nta_outgoing_tcreate(ag->ag_bob_leg, outgoing_callback, ctx,
  2885. NULL,
  2886. SIP_METHOD_BYE,
  2887. NULL,
  2888. SIPTAG_SUBJECT_STR(ctx->c_name),
  2889. SIPTAG_FROM(ag->ag_alice),
  2890. SIPTAG_TO(ag->ag_bob),
  2891. SIPTAG_CONTACT(ag->ag_m_alice),
  2892. SIPTAG_CONTENT_TYPE(ct),
  2893. SIPTAG_PAYLOAD(sdp),
  2894. TAG_END());
  2895. TEST_1(!client_run(ctx, 200));
  2896. TEST_P(ag->ag_latest_leg, old_leg);
  2897. TEST_P(ag->ag_alice_leg, NULL);
  2898. }
  2899. nta_leg_destroy(ag->ag_bob_leg), ag->ag_bob_leg = NULL;
  2900. ag->ag_latest_leg = NULL;
  2901. ag->ag_call_leg = NULL;
  2902. /* Test CANCELing a call after receiving 100rel response */
  2903. ag->ag_alice_leg = nta_leg_tcreate(ag->ag_agent,
  2904. alice_leg_callback,
  2905. ag,
  2906. SIPTAG_FROM(ag->ag_alice),
  2907. SIPTAG_TO(ag->ag_bob),
  2908. TAG_END());
  2909. TEST_1(ag->ag_alice_leg);
  2910. TEST_1(nta_leg_tag(ag->ag_alice_leg, NULL));
  2911. {
  2912. invite_client_t ic[1] =
  2913. {{{{
  2914. ag, "Call 2b",
  2915. cancel_invite, checks_for_invite, invite_client_deinit
  2916. }}}};
  2917. client_t *ctx = ic->ic_client;
  2918. /* Send INVITE */
  2919. nta_leg_bind(ag->ag_server_leg, bob_leg_callback3, ag);
  2920. ag->ag_expect_leg = ag->ag_server_leg;
  2921. ctx->c_orq =
  2922. nta_outgoing_tcreate(ag->ag_call_leg = ag->ag_alice_leg,
  2923. outgoing_callback, ctx,
  2924. ag->ag_obp,
  2925. SIP_METHOD_INVITE,
  2926. (url_string_t *)ag->ag_m_bob->m_url,
  2927. SIPTAG_SUBJECT_STR(ctx->c_name),
  2928. SIPTAG_CONTACT(ag->ag_m_alice),
  2929. SIPTAG_REQUIRE_STR("100rel"),
  2930. SIPTAG_CONTENT_TYPE(ct),
  2931. SIPTAG_PAYLOAD(sdp),
  2932. TAG_END());
  2933. TEST_1(!client_run(ctx, 0));
  2934. }
  2935. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  2936. TEST_1(ag->ag_bob_leg != NULL);
  2937. nta_leg_destroy(ag->ag_bob_leg), ag->ag_bob_leg = NULL;
  2938. ag->ag_latest_leg = NULL;
  2939. ag->ag_call_leg = NULL;
  2940. if (EXPENSIVE_CHECKS) {
  2941. printf("%s: starting 100rel timeout test, test will complete in 4 seconds\n",
  2942. name);
  2943. TEST(nta_agent_set_params(ag->ag_agent,
  2944. NTATAG_SIP_T1(25),
  2945. NTATAG_SIP_T1X64(64 * 25),
  2946. TAG_END()), 2);
  2947. ag->ag_alice_leg = nta_leg_tcreate(ag->ag_agent,
  2948. alice_leg_callback,
  2949. ag,
  2950. SIPTAG_FROM(ag->ag_alice),
  2951. SIPTAG_TO(ag->ag_bob),
  2952. TAG_END());
  2953. TEST_1(ag->ag_alice_leg);
  2954. TEST_1(nta_leg_tag(ag->ag_alice_leg, NULL));
  2955. {
  2956. invite_client_t ic[1] =
  2957. {{{{ ag, "Call 3", NULL, checks_for_invite, invite_client_deinit }}}};
  2958. client_t *ctx = ic->ic_client;
  2959. /* Send INVITE,
  2960. * send precious provisional response
  2961. * do not send PRACK,
  2962. * timeout (after 64 * t1 ~ 3.2 seconds),
  2963. */
  2964. nta_leg_bind(ag->ag_server_leg, bob_leg_callback2, ag);
  2965. ag->ag_expect_leg = ag->ag_server_leg;
  2966. ctx->c_orq =
  2967. nta_outgoing_tcreate(ag->ag_call_leg = ag->ag_alice_leg,
  2968. outgoing_callback, ctx,
  2969. ag->ag_obp,
  2970. SIP_METHOD_INVITE,
  2971. (url_string_t *)ag->ag_m_bob->m_url,
  2972. SIPTAG_SUBJECT_STR(ctx->c_name),
  2973. SIPTAG_CONTACT(ag->ag_m_alice),
  2974. SIPTAG_REQUIRE_STR("100rel"),
  2975. SIPTAG_CONTENT_TYPE(ct),
  2976. SIPTAG_PAYLOAD(sdp),
  2977. TAG_END());
  2978. TEST_1(ctx->c_orq);
  2979. nta_test_run(ag);
  2980. TEST(ctx->c_status, 503);
  2981. TEST_P(ctx->c_orq, NULL);
  2982. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  2983. TEST_1(ag->ag_bob_leg == NULL);
  2984. }
  2985. TEST(nta_agent_set_params(ag->ag_agent,
  2986. NTATAG_SIP_T1(500),
  2987. NTATAG_SIP_T1X64(64 * 500),
  2988. TAG_END()), 2);
  2989. }
  2990. if (EXPENSIVE_CHECKS || 1) {
  2991. /*
  2992. * client sends INVITE,
  2993. * server sends provisional response,
  2994. * client PRACKs it,
  2995. * client timeouts after timer C
  2996. */
  2997. invite_client_t ic[1] =
  2998. {{{{ ag, "Call 4", NULL, checks_for_100rel, invite_client_deinit }}}};
  2999. client_t *ctx = ic->ic_client;
  3000. printf("%s: starting timer C, test will complete in 1 seconds\n",
  3001. name);
  3002. TEST(nta_agent_set_params(ag->ag_agent,
  3003. NTATAG_TIMER_C(1000),
  3004. TAG_END()), 1);
  3005. TEST_1(ag->ag_alice_leg = nta_leg_tcreate(ag->ag_agent,
  3006. alice_leg_callback,
  3007. ag,
  3008. SIPTAG_FROM(ag->ag_alice),
  3009. SIPTAG_TO(ag->ag_bob),
  3010. TAG_END()));
  3011. TEST_1(nta_leg_tag(ag->ag_alice_leg, NULL));
  3012. nta_leg_bind(ag->ag_server_leg, bob_leg_callback3, ag);
  3013. ag->ag_expect_leg = ag->ag_server_leg;
  3014. TEST_1(ctx->c_orq =
  3015. nta_outgoing_tcreate(ag->ag_call_leg = ag->ag_alice_leg,
  3016. outgoing_callback, ic->ic_client,
  3017. ag->ag_obp,
  3018. SIP_METHOD_INVITE,
  3019. (url_string_t *)ag->ag_m_bob->m_url,
  3020. SIPTAG_SUBJECT_STR(ctx->c_name),
  3021. SIPTAG_CONTACT(ag->ag_m_alice),
  3022. SIPTAG_REQUIRE_STR("100rel"),
  3023. SIPTAG_CONTENT_TYPE(ct),
  3024. SIPTAG_PAYLOAD(sdp),
  3025. TAG_END()));
  3026. /* Run until 1) server gets CANCEL and 2) client gets 487 */
  3027. /* Note: this has been changed in 1.12.11 */
  3028. TEST_1(!client_run_until_canceled(ctx, 487));
  3029. TEST_1(ag->ag_canceled != 0);
  3030. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  3031. TEST_1(ag->ag_bob_leg);
  3032. nta_leg_destroy(ag->ag_bob_leg), ag->ag_bob_leg = NULL;
  3033. TEST(nta_agent_set_params(ag->ag_agent,
  3034. NTATAG_TIMER_C(185 * 1000),
  3035. TAG_END()), 1);
  3036. nta_leg_destroy(ag->ag_bob_leg), ag->ag_bob_leg = NULL;
  3037. ag->ag_latest_leg = NULL;
  3038. ag->ag_call_leg = NULL;
  3039. }
  3040. END();
  3041. }
  3042. int alice_leg_callback2(agent_t *ag,
  3043. nta_leg_t *leg,
  3044. nta_incoming_t *irq,
  3045. sip_t const *sip)
  3046. {
  3047. BEGIN();
  3048. if (tstflags & tst_verbatim) {
  3049. printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
  3050. name, __func__, sip->sip_request->rq_method_name,
  3051. URL_PRINT_ARGS(sip->sip_request->rq_url),
  3052. sip->sip_request->rq_version);
  3053. }
  3054. TEST_1(sip->sip_content_length);
  3055. TEST_1(sip->sip_via);
  3056. TEST_1(sip->sip_from && sip->sip_from->a_tag);
  3057. if (sip->sip_request->rq_method == sip_method_prack)
  3058. return 481;
  3059. ag->ag_latest_leg = leg;
  3060. if (leg != ag->ag_alice_leg) {
  3061. leg_match(ag, leg, 1, __func__);
  3062. return 500;
  3063. }
  3064. if (sip->sip_request->rq_method == sip_method_invite) {
  3065. TEST_1(sip_has_feature(sip->sip_supported, "100rel"));
  3066. nta_incoming_bind(irq, test_for_ack, ag);
  3067. nta_incoming_treply(irq, SIP_100_TRYING, TAG_END());
  3068. nta_agent_set_params(ag->ag_agent,
  3069. NTATAG_DEBUG_DROP_PROB(ag->ag_drop),
  3070. TAG_END());
  3071. ag->ag_reliable =
  3072. nta_reliable_treply(irq,
  3073. NULL, NULL,
  3074. SIP_183_SESSION_PROGRESS,
  3075. SIPTAG_CONTENT_TYPE(ag->ag_content_type),
  3076. SIPTAG_PAYLOAD(ag->ag_payload),
  3077. SIPTAG_CONTACT(ag->ag_m_alice),
  3078. TAG_END());
  3079. TEST_1(ag->ag_reliable);
  3080. ag->ag_reliable =
  3081. nta_reliable_treply(irq,
  3082. NULL, NULL,
  3083. 184, "Next",
  3084. SIPTAG_CONTACT(ag->ag_m_alice),
  3085. TAG_END());
  3086. TEST_1(ag->ag_reliable);
  3087. ag->ag_reliable =
  3088. nta_reliable_treply(irq,
  3089. NULL, NULL,
  3090. 185, "Last",
  3091. SIPTAG_CONTACT(ag->ag_m_alice),
  3092. TAG_END());
  3093. TEST_1(ag->ag_reliable);
  3094. TEST(nta_incoming_treply(irq, SIP_200_OK, TAG_END()), 0);
  3095. ag->ag_irq = irq;
  3096. return 0;
  3097. }
  3098. if (sip->sip_request->rq_method == sip_method_bye) {
  3099. leg_zap(ag, leg);
  3100. }
  3101. if(sip)
  3102. return 200;
  3103. END();
  3104. }
  3105. /*
  3106. * Test establishing a call with an early dialog / 100 rel / timeout
  3107. *
  3108. * Alice sends a INVITE to Bob, then Bob sends 183, 184, 185, and 200.
  3109. * Bob sends BYE, Alice 200.
  3110. *
  3111. * See bug #467.
  3112. */
  3113. int test_fix_467(agent_t *ag)
  3114. {
  3115. sip_content_type_t *ct = ag->ag_content_type;
  3116. sip_payload_t *sdp = ag->ag_payload;
  3117. nta_leg_t *old_leg;
  3118. BEGIN();
  3119. ag->ag_alice_leg = nta_leg_tcreate(ag->ag_agent,
  3120. alice_leg_callback2,
  3121. ag,
  3122. SIPTAG_FROM(ag->ag_alice),
  3123. SIPTAG_TO(ag->ag_bob),
  3124. TAG_END());
  3125. TEST_1(ag->ag_alice_leg);
  3126. TEST_1(nta_leg_tag(ag->ag_alice_leg, NULL));
  3127. ag->ag_bob_leg = NULL;
  3128. {
  3129. invite_client_t ic[1] =
  3130. {{{{ ag, "Call 5", NULL, checks_for_100rel, invite_client_deinit }}}};
  3131. client_t *ctx = ic->ic_client;
  3132. /* Send INVITE */
  3133. nta_leg_bind(ag->ag_server_leg, bob_leg_callback2, ag);
  3134. ag->ag_expect_leg = ag->ag_server_leg;
  3135. ctx->c_orq =
  3136. nta_outgoing_tcreate(ag->ag_call_leg = ag->ag_alice_leg,
  3137. outgoing_callback, ic->ic_client,
  3138. ag->ag_obp,
  3139. SIP_METHOD_INVITE,
  3140. (url_string_t *)ag->ag_m_bob->m_url,
  3141. SIPTAG_SUBJECT_STR(ctx->c_name),
  3142. SIPTAG_CONTACT(ag->ag_m_alice),
  3143. SIPTAG_REQUIRE_STR("100rel"),
  3144. SIPTAG_CONTENT_TYPE(ct),
  3145. SIPTAG_PAYLOAD(sdp),
  3146. TAG_END());
  3147. TEST_1(!client_run(ctx, 200));
  3148. /*TEST(ag->ag_tag_status, 183);*/
  3149. TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  3150. TEST_1(ag->ag_bob_leg != NULL);
  3151. }
  3152. old_leg = ag->ag_expect_leg = ag->ag_alice_leg;
  3153. {
  3154. client_t ctx[1] = {{ ag, "Hangup" }};
  3155. /* Send BYE from Bob to Alice */
  3156. ctx->c_orq =
  3157. nta_outgoing_tcreate(ag->ag_bob_leg, outgoing_callback, ctx,
  3158. NULL,
  3159. SIP_METHOD_BYE,
  3160. NULL,
  3161. SIPTAG_SUBJECT_STR(ctx->c_name),
  3162. SIPTAG_FROM(ag->ag_alice),
  3163. SIPTAG_TO(ag->ag_bob),
  3164. SIPTAG_CONTACT(ag->ag_m_alice),
  3165. SIPTAG_CONTENT_TYPE(ct),
  3166. SIPTAG_PAYLOAD(sdp),
  3167. TAG_END());
  3168. TEST_1(!client_run(ctx, 200));
  3169. TEST_P(ag->ag_latest_leg, old_leg);
  3170. TEST_P(ag->ag_alice_leg, NULL);
  3171. }
  3172. END();
  3173. /*
  3174. nta_leg_destroy(ag->ag_bob_leg), ag->ag_bob_leg = NULL;
  3175. ag->ag_latest_leg = NULL;
  3176. ag->ag_call_leg = NULL;
  3177. */
  3178. }
  3179. #if HAVE_ALARM
  3180. #include <unistd.h>
  3181. #include <signal.h>
  3182. static RETSIGTYPE sig_alarm(int s)
  3183. {
  3184. fprintf(stderr, "%s: FAIL! test timeout!\n", name);
  3185. exit(1);
  3186. }
  3187. #endif
  3188. static
  3189. char const nta_test_usage[] =
  3190. "usage: %s OPTIONS\n"
  3191. "where OPTIONS are\n"
  3192. " -v | --verbose be verbose\n"
  3193. " -a | --abort abort() on error\n"
  3194. " -q | --quiet be quiet\n"
  3195. " --expensive run expensive tests, too\n"
  3196. " -1 quit on first error\n"
  3197. " -l level set logging level (0 by default)\n"
  3198. " -p uri specify uri of outbound proxy\n"
  3199. " -m uri bind to local uri\n"
  3200. " --attach print pid, wait for a debugger to be attached\n"
  3201. #if HAVE_ALARM
  3202. " --no-alarm don't ask for guard ALARM\n"
  3203. #endif
  3204. ;
  3205. void usage(int exitcode)
  3206. {
  3207. fprintf(stderr, nta_test_usage, name);
  3208. exit(exitcode);
  3209. }
  3210. #if HAVE_OPEN_C
  3211. int posix_main(int argc, char *argv[]);
  3212. int main(int argc, char *argv[])
  3213. {
  3214. int retval;
  3215. tstflags |= tst_verbatim;
  3216. su_log_set_level(su_log_default, 9);
  3217. su_log_set_level(nta_log, 9);
  3218. su_log_set_level(tport_log, 9);
  3219. retval = posix_main(argc, argv);
  3220. sleep(7);
  3221. return retval;
  3222. }
  3223. #define main posix_main
  3224. #endif
  3225. int main(int argc, char *argv[])
  3226. {
  3227. int retval = 0, quit_on_single_failure = 0;
  3228. int i, o_attach = 0, o_alarm = 1;
  3229. agent_t ag[1] = {{ { SU_HOME_INIT(ag) }, 0, NULL }};
  3230. expensive_checks = getenv("EXPENSIVE_CHECKS") != NULL;
  3231. for (i = 1; argv[i]; i++) {
  3232. if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0)
  3233. tstflags |= tst_verbatim;
  3234. else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--abort") == 0)
  3235. tstflags |= tst_abort;
  3236. else if (strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "--quiet") == 0)
  3237. tstflags &= ~tst_verbatim;
  3238. else if (strcmp(argv[i], "--expensive") == 0)
  3239. expensive_checks = 1;
  3240. else if (strcmp(argv[i], "-1") == 0)
  3241. quit_on_single_failure = 1;
  3242. else if (strncmp(argv[i], "-l", 2) == 0) {
  3243. int level = 3;
  3244. char *rest = NULL;
  3245. if (argv[i][2])
  3246. level = strtol(argv[i] + 2, &rest, 10);
  3247. else if (argv[i + 1])
  3248. level = strtol(argv[i + 1], &rest, 10), i++;
  3249. else
  3250. level = 3, rest = "";
  3251. if (rest == NULL || *rest)
  3252. usage(1);
  3253. su_log_set_level(nta_log, level);
  3254. su_log_set_level(tport_log, level);
  3255. }
  3256. else if (strncmp(argv[i], "-p", 2) == 0) {
  3257. if (argv[i][2])
  3258. ag->ag_obp = (url_string_t *)(argv[i] + 2);
  3259. else if (argv[i + 1])
  3260. ag->ag_obp = (url_string_t *)(argv[++i]);
  3261. else
  3262. usage(1);
  3263. }
  3264. else if (strncmp(argv[i], "-m", 2) == 0) {
  3265. if (argv[i][2])
  3266. ag->ag_m = argv[i] + 2;
  3267. else if (argv[i + 1])
  3268. ag->ag_m = argv[++i];
  3269. else
  3270. usage(1);
  3271. }
  3272. else if (strcmp(argv[i], "--attach") == 0) {
  3273. o_attach = 1;
  3274. }
  3275. else if (strcmp(argv[i], "--no-alarm") == 0) {
  3276. o_alarm = 0;
  3277. }
  3278. else if (strcmp(argv[i], "-") == 0) {
  3279. i++; break;
  3280. }
  3281. else if (argv[i][0] != '-') {
  3282. break;
  3283. }
  3284. else
  3285. usage(1);
  3286. }
  3287. if (o_attach) {
  3288. char line[10], *got;
  3289. printf("nua_test: pid %u\n", getpid());
  3290. printf("<Press RETURN to continue>\n");
  3291. got = fgets(line, sizeof line, stdin); (void)got;
  3292. }
  3293. #if HAVE_ALARM
  3294. else if (o_alarm) {
  3295. alarm(60);
  3296. signal(SIGALRM, sig_alarm);
  3297. }
  3298. #endif
  3299. su_init();
  3300. if (!(TSTFLAGS & tst_verbatim)) {
  3301. su_log_soft_set_level(nta_log, 0);
  3302. su_log_soft_set_level(tport_log, 0);
  3303. }
  3304. #define SINGLE_FAILURE_CHECK() \
  3305. do { fflush(stdout); \
  3306. if (retval && quit_on_single_failure) { su_deinit(); return retval; } \
  3307. } while(0)
  3308. retval |= test_init(ag, argv[i]); SINGLE_FAILURE_CHECK();
  3309. if (retval == 0) {
  3310. retval |= test_bad_messages(ag); SINGLE_FAILURE_CHECK();
  3311. retval |= test_reinit(ag); SINGLE_FAILURE_CHECK();
  3312. retval |= test_merging(ag); SINGLE_FAILURE_CHECK();
  3313. retval |= test_tports(ag); SINGLE_FAILURE_CHECK();
  3314. retval |= test_destroy_incoming(ag); SINGLE_FAILURE_CHECK();
  3315. retval |= test_resolv(ag, argv[i]); SINGLE_FAILURE_CHECK();
  3316. retval |= test_routing(ag); SINGLE_FAILURE_CHECK();
  3317. retval |= test_dialog(ag); SINGLE_FAILURE_CHECK();
  3318. retval |= test_call(ag); SINGLE_FAILURE_CHECK();
  3319. retval |= test_prack(ag); SINGLE_FAILURE_CHECK();
  3320. retval |= test_fix_467(ag); SINGLE_FAILURE_CHECK();
  3321. }
  3322. s2_fast_forward(64000, ag->ag_root);
  3323. retval |= test_deinit(ag); fflush(stdout);
  3324. su_home_deinit(ag->ag_home);
  3325. su_deinit();
  3326. return retval;
  3327. }