perfdhcp.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523
  1. /*
  2. * Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <config.h>
  17. #ifndef HAVE_GETIFADDRS
  18. /*
  19. * Solaris 10 does not have the getifaddrs() function available (although it
  20. * is present on Solaris 11 and later). For the present we will not implement
  21. * a replacement (as we do with clock_gettime) as the implementation is
  22. * relatively complex. Just output a message saying that the utility is not
  23. * supported on this operating system.
  24. */
  25. #include <stdio.h>
  26. int
  27. main(const int argc, char* const argv[])
  28. {
  29. fprintf(stderr, "perfdhcp is not supported on this version of the operating system\n");
  30. return (1);
  31. }
  32. #else
  33. /* getifaddrs() present, so the code should compile */
  34. #ifdef __linux__
  35. #define _GNU_SOURCE
  36. #endif
  37. #include <sys/types.h>
  38. #include <sys/select.h>
  39. #include <sys/socket.h>
  40. #include <sys/wait.h>
  41. #include <net/if.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include <ctype.h>
  45. #include <errno.h>
  46. #include <fcntl.h>
  47. #include <ifaddrs.h>
  48. #include <math.h>
  49. #include <netdb.h>
  50. #include <signal.h>
  51. #include <stdint.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <time.h>
  56. #include <unistd.h>
  57. /* DHCPv4 defines (to be moved/shared) */
  58. #define DHCP_OFF_OPCODE 0
  59. #define DHCP_OFF_HTYPE 1
  60. #define DHCP_OFF_HLEN 2
  61. #define DHCP_OFF_HOPS 3
  62. #define DHCP_OFF_XID 4
  63. #define DHCP_OFF_SECS 8
  64. #define DHCP_OFF_FLAGS 10
  65. #define DHCP_OFF_CIADDR 12
  66. #define DHCP_OFF_YIADDR 16
  67. #define DHCP_OFF_SADDR 20
  68. #define DHCP_OFF_GIADDR 24
  69. #define DHCP_OFF_CHADDR 28
  70. #define DHCP_OFF_SNAME 44
  71. #define DHCP_OFF_FILE 108
  72. #define DHCP_OFF_COOKIE 236
  73. #define DHCP_OFF_OPTIONS 240
  74. #define BOOTP_OP_REQUEST 1
  75. #define BOOTP_OP_REPLY 2
  76. #define BOOTP_MIN_LEN 300
  77. #define DHCP_OP_DISCOVER 1
  78. #define DHCP_OP_OFFER 2
  79. #define DHCP_OP_REQUEST 3
  80. #define DHCP_OP_DECLINE 4
  81. #define DHCP_OP_ACK 5
  82. #define DHCP_OP_NAK 6
  83. #define DHCP_OP_RELEASE 7
  84. #define DHCP_OP_INFORM 8
  85. #define DHCP_HTYPE_ETHER 1
  86. #define DHCP_OPT_PAD 0
  87. #define DHCP_OPT_SUBNET_MASK 1
  88. #define DHCP_OPT_TIME_OFFSET 2
  89. #define DHCP_OPT_ROUTERS 3
  90. #define DHCP_OPT_DNS_SERVERS 6
  91. #define DHCP_OPT_HOST_NAME 12
  92. #define DHCP_OPT_DOMAIN_NAME 15
  93. #define DHCP_OPT_BROADCAST 28
  94. #define DHCP_OPT_DHCP_ADDRESS 50
  95. #define DHCP_OPT_DHCP_LEASE 51
  96. #define DHCP_OPT_DHCP_MSGTYPE 53
  97. #define DHCP_OPT_DHCP_SRVID 54
  98. #define DHCP_OPT_DHCP_PRL 55
  99. #define DHCP_OPT_END 255
  100. #define DHCP_OPTLEN_SRVID 6
  101. /* DHCPv6 defines (to be moved/shared) */
  102. #define DHCP6_OFF_MSGTYP 0
  103. #define DHCP6_OFF_XID 1
  104. #define DHCP6_OFF_OPTIONS 4
  105. #define DHCP6_OP_SOLICIT 1
  106. #define DHCP6_OP_ADVERTISE 2
  107. #define DHCP6_OP_REQUEST 3
  108. #define DHCP6_OP_REPLY 7
  109. #define DHCP6_OPT_CLIENTID 1
  110. #define DHCP6_OPT_SERVERID 2
  111. #define DHCP6_OPT_IA_NA 3
  112. #define DHCP6_OPT_ORO 6
  113. #define DHCP6_OPT_ELAPSED_TIME 8
  114. #define DHCP6_OPT_STATUS_CODE 13
  115. #define DHCP6_OPT_RAPID_COMMIT 14
  116. #define DHCP6_OPT_NAME_SERVERS 23
  117. #define DHCP6_OPT_DOMAIN_SEARCH 24
  118. #define DHCP6_ST_SUCCESS 0
  119. #define DHCP6_ST_NOADDRSAVAIL 2
  120. #define DHCP6_DUID_LLT 1
  121. #define DHCP6_DUID_EPOCH 946684800
  122. /* tail queue macros (from FreeBSD 8.2 /sys/sys/queue.h, to be moved/shared) */
  123. #define ISC_TAILQ_HEAD(name, type) \
  124. struct name { \
  125. struct type *tqh_first; \
  126. struct type **tqh_last; \
  127. }
  128. #define ISC_TAILQ_ENTRY(type) \
  129. struct { \
  130. struct type *tqe_next; \
  131. struct type **tqe_prev; \
  132. }
  133. #define ISC_TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  134. #define ISC_TAILQ_FIRST(head) ((head)->tqh_first)
  135. #define ISC_TAILQ_LAST(head, headname) \
  136. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  137. #define ISC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  138. #define ISC_TAILQ_PREV(elm, headname, field) \
  139. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  140. #define ISC_TAILQ_INIT(head) do { \
  141. ISC_TAILQ_FIRST((head)) = NULL; \
  142. (head)->tqh_last = &ISC_TAILQ_FIRST((head)); \
  143. } while (0)
  144. #define ISC_TAILQ_INSERT_HEAD(head, elm, field) do { \
  145. ISC_TAILQ_NEXT((elm), field) = ISC_TAILQ_FIRST((head)); \
  146. if (!ISC_TAILQ_EMPTY((head))) \
  147. ISC_TAILQ_FIRST((head))->field.tqe_prev = \
  148. &ISC_TAILQ_NEXT((elm), field); \
  149. else \
  150. (head)->tqh_last = &ISC_TAILQ_NEXT((elm), field); \
  151. ISC_TAILQ_FIRST((head)) = (elm); \
  152. (elm)->field.tqe_prev = &ISC_TAILQ_FIRST((head)); \
  153. } while (0)
  154. #define ISC_TAILQ_INSERT_TAIL(head, elm, field) do { \
  155. ISC_TAILQ_NEXT((elm), field) = NULL; \
  156. (elm)->field.tqe_prev = (head)->tqh_last; \
  157. *(head)->tqh_last = (elm); \
  158. (head)->tqh_last = &ISC_TAILQ_NEXT((elm), field); \
  159. } while (0)
  160. #define ISC_TAILQ_REMOVE(head, elm, field) do { \
  161. if ((ISC_TAILQ_NEXT((elm), field)) != NULL) \
  162. ISC_TAILQ_NEXT((elm), field)->field.tqe_prev = \
  163. (elm)->field.tqe_prev; \
  164. else \
  165. (head)->tqh_last = (elm)->field.tqe_prev; \
  166. *(elm)->field.tqe_prev = ISC_TAILQ_NEXT((elm), field); \
  167. } while (0)
  168. #define ISC_TAILQ_FOREACH(var, head, field) \
  169. for ((var) = ISC_TAILQ_FIRST((head)); \
  170. (var); \
  171. (var) = ISC_TAILQ_NEXT((var), field))
  172. #define ISC_TAILQ_FOREACH_SAFE(var, head, field, tvar) \
  173. for ((var) = ISC_TAILQ_FIRST((head)); \
  174. (var) && ((tvar) = ISC_TAILQ_NEXT((var), field), 1); \
  175. (var) = (tvar))
  176. /*
  177. * Data structures
  178. */
  179. /*
  180. * exchange:
  181. * - per exchange values:
  182. * * order (for debugging)
  183. * * xid (even/odd for 4 packet exchanges)
  184. * * random (for debugging)
  185. * * time-stamps
  186. * * server ID (for 3rd packet)
  187. * * IA_NA (for IPv6 3rd packet)
  188. *
  189. * sent/rcvd global chains, "next to be received" on entry cache,
  190. * and hash table for xid -> data structure fast matching
  191. * (using the assumption collisions are unlikely, cf birthday problem)
  192. */
  193. struct exchange { /* per exchange structure */
  194. ISC_TAILQ_ENTRY(exchange) gchain; /* global chaining */
  195. ISC_TAILQ_ENTRY(exchange) hchain; /* hash table chaining */
  196. uint64_t order0, order2; /* number of this exchange */
  197. uint32_t xid; /* transaction ID */
  198. uint32_t rnd; /* random part */
  199. struct timespec ts0, ts1, ts2, ts3; /* timespecs */
  200. uint8_t *sid; /* server ID */
  201. size_t sidlen; /* server ID length */
  202. uint8_t *iana; /* (IPv6) IA_NA */
  203. size_t ianalen; /* (IPv6) IA_NA length */
  204. };
  205. struct exchange *xnext0, *xnext2; /* next to be received */
  206. ISC_TAILQ_HEAD(xlist, exchange); /* exchange list */
  207. struct xlist xsent0, xsent2, xrcvd0, xrcvd2; /* sent and received lists */
  208. uint64_t xscount0, xscount2; /* sent counters */
  209. uint64_t xrcount0, xrcount2; /* received counters */
  210. caddr_t exchanges0, exchanges2; /* hash tables */
  211. uint32_t hashsize0, hashsize2; /* hash table sizes */
  212. /*
  213. * statictics counters and accumulators
  214. */
  215. uint64_t tooshort, orphans, locallimit; /* error counters */
  216. uint64_t latesent, compsend, latercvd; /* rate stats */
  217. uint64_t multrcvd, shortwait, collected[2]; /* rate stats (cont) */
  218. double dmin0 = 999999999., dmin2 = 999999999.; /* minimum delays */
  219. double dmax0 = 0., dmax2 = 0.; /* maximum delays */
  220. double dsum0 = 0., dsum2 = 0.; /* delay sums */
  221. double dsumsq0 = 0., dsumsq2 = 0.; /* square delay sums */
  222. /*
  223. * command line parameters
  224. */
  225. int ipversion = 0; /* IP version */
  226. int simple; /* DO/SA in place of DORR/SARR */
  227. int rate; /* rate in exchange per second */
  228. int report; /* delay between two reports */
  229. uint32_t range; /* randomization range */
  230. uint32_t maxrandom; /* maximum random value */
  231. int basecnt; /* base count */
  232. char *base[4]; /* bases */
  233. int gotnumreq; /* numreq[0] was set */
  234. int numreq[2]; /* number of exchange */
  235. int period; /* test period */
  236. int gotlosttime; /* losttime[0] was set */
  237. double losttime[2] = {1., 1.}; /* time after a request is lost */
  238. int gotmaxdrop; /* max{p}drop[0] was set */
  239. int maxdrop[2]; /* maximum number of lost requests */
  240. double maxpdrop[2] = { 0., 0.}; /* maximum percentage */
  241. char *localname; /* local address or interface */
  242. int isinterface; /* interface vs local address */
  243. int preload; /* preload exchanges */
  244. int aggressivity = 1; /* back to back exchanges */
  245. int localport; /* local port number (host endian) */
  246. int seeded; /* is a seed provided */
  247. unsigned int seed; /* randomization seed */
  248. int isbroadcast; /* use broadcast */
  249. int rapidcommit; /* add rapid commit option */
  250. int usefirst; /* where to take the server-ID */
  251. char *templatefile[2]; /* template file name */
  252. int xidoffset[2] = {-1, -1}; /* template offsets (xid)*/
  253. int rndoffset[2] = {-1, -1}; /* template offsets (random) */
  254. int elpoffset = -1; /* template offset (elapsed time) */
  255. int sidoffset = -1; /* template offset (server ID) */
  256. int ripoffset = -1; /* template offset (requested IP) */
  257. char *diags; /* diagnostic selectors */
  258. char *wrapped; /* wrapped command */
  259. char *servername; /* server */
  260. /*
  261. * global variables
  262. */
  263. struct sockaddr_storage localaddr; /* local socket address */
  264. struct sockaddr_storage serveraddr; /* server socket address */
  265. int sock; /* socket descriptor */
  266. int interrupted, fatal; /* to finish flags */
  267. uint8_t obuf[4096], ibuf[4096]; /* I/O buffers */
  268. char tbuf[8200]; /* template buffer */
  269. struct timespec boot; /* the date of boot */
  270. struct timespec last; /* the date of last send */
  271. struct timespec due; /* the date of next send */
  272. struct timespec dreport; /* the date of next reporting */
  273. struct timespec finished; /* the date of finish */
  274. uint8_t *gsrvid; /* global server id */
  275. size_t gsrvidlen; /* and its length */
  276. uint8_t gsrvidbuf[64]; /* and its storage */
  277. /* MAC address */
  278. uint8_t mac_prefix[6] = { 0x00, 0x0c, 0x01, 0x02, 0x03, 0x04 };
  279. /* DUID prefix */
  280. uint8_t *duid_prefix;
  281. int duid_length;
  282. /* magic cookie for BOOTP/DHCPv4 */
  283. uint8_t dhcp_cookie[4] = { 0x63, 0x82, 0x53, 0x63 };
  284. /*
  285. * templates
  286. *
  287. * note: the only hard point is what are the offsets:
  288. * - xid_discover4 and xid_request4: first of the 4 octet long
  289. * transaction ID (default DHCP_OFF_XID = 4)
  290. * - random_discover4 and random_request4: last of the 6 octet long
  291. * MAC address (default DHCP_OFF_CHADDR + 6 = 28 + 6)
  292. * - elapsed_request4: first of the 2 octet long secs field
  293. * (default DHCP_OFF_SECS = 8, 0 means disabled)
  294. * - serverid_request4: first of the 6 octet long server ID option
  295. * (no default, required)
  296. * - reqaddr_request4: first of the 4 octet long requested IP address
  297. * option content (i.e., the address itself, btw OFFER yiaddr)
  298. * (no default, required)
  299. * - xid_solicit6 and xid_request6: first of the 3 octet long
  300. * transaction ID (default DHCP6_OFF_XID = 1)
  301. * - random_solicit6 and random_request6: last of the DUID in the
  302. * client ID option (no default, required when rate is set)
  303. * - elapsed_request6: first of the 2 octet long content of
  304. * the option elapsed time option (no default, 0 means disabled)
  305. * - serverid_request6: position where the variable length server ID
  306. * option is inserted (no default, required, set to length means append)
  307. * - reqaddr_request6: position where of the variable length requested
  308. * IP address option is inserted (no default, required, set to
  309. * length means append)
  310. */
  311. size_t length_discover4;
  312. uint8_t template_discover4[4096];
  313. size_t xid_discover4;
  314. size_t random_discover4;
  315. size_t length_request4;
  316. uint8_t template_request4[4096];
  317. size_t xid_request4;
  318. size_t elapsed_request4;
  319. size_t random_request4;
  320. size_t serverid_request4;
  321. size_t reqaddr_request4;
  322. size_t length_solicit6;
  323. uint8_t template_solicit6[4096];
  324. size_t xid_solicit6;
  325. size_t random_solicit6;
  326. size_t length_request6;
  327. uint8_t template_request6[4096];
  328. size_t xid_request6;
  329. size_t elapsed_request6;
  330. size_t random_request6;
  331. size_t serverid_request6;
  332. size_t reqaddr_request6;
  333. // use definition of CLOCK_REALTIME (or lack of thereof) as an indicator
  334. // if the code is being compiled or Linux (or somewhere else)
  335. // Perhaps this should be based on OS_LINUX define?
  336. #if !defined (CLOCK_REALTIME)
  337. #define CLOCK_REALTIME 0
  338. /// @brief clock_gettime implementation for non-Linux systems
  339. ///
  340. /// This implementation lacks nanosecond resolution. It is intended
  341. /// to be used on non-Linux systems that does not provide clock_gettime
  342. /// implementation.
  343. ///
  344. /// @param clockid ignored (kept for Linux prototype compatibility)
  345. /// @param tp timespec structure
  346. ///
  347. /// @return always zero (kept for compatibility reasons)
  348. int clock_gettime(int clockid, struct timespec *tp) {
  349. struct timeval tv;
  350. gettimeofday(&tv, NULL);
  351. tp->tv_sec = tv.tv_sec;
  352. tp->tv_nsec = tv.tv_usec*1000;
  353. return (0);
  354. }
  355. #endif
  356. /*
  357. * initialize data structures handling exchanges
  358. */
  359. void
  360. inits(void)
  361. {
  362. struct xlist *bucket;
  363. caddr_t p;
  364. size_t len, i;
  365. ISC_TAILQ_INIT(&xsent0);
  366. ISC_TAILQ_INIT(&xsent2);
  367. ISC_TAILQ_INIT(&xrcvd0);
  368. ISC_TAILQ_INIT(&xrcvd2);
  369. /// compute hashsizes
  370. hashsize0 = 1024;
  371. len = sizeof(*bucket) * hashsize0;
  372. exchanges0 = malloc(len);
  373. if (exchanges0 == NULL) {
  374. perror("malloc(exchanges0)");
  375. exit(1);
  376. }
  377. for (i = 0, p = exchanges0; i < hashsize0; i++, p += sizeof(*bucket)) {
  378. bucket = (struct xlist *) p;
  379. ISC_TAILQ_INIT(bucket);
  380. }
  381. if (simple != 0)
  382. return;
  383. hashsize2 = 1024;
  384. len = sizeof(*bucket) * hashsize2;
  385. exchanges2 = malloc(len);
  386. if (exchanges2 == NULL) {
  387. perror("malloc(exchanges2)");
  388. exit(1);
  389. }
  390. for (i = 0, p = exchanges2; i < hashsize2; i++, p += sizeof(*bucket)) {
  391. bucket = (struct xlist *) p;
  392. ISC_TAILQ_INIT(bucket);
  393. }
  394. }
  395. /*
  396. * randomize the value of the given field:
  397. * - offset of the field
  398. * - random seed (used as it when suitable)
  399. * - returns the random value which was used
  400. */
  401. uint32_t
  402. randomize(size_t offset, uint32_t r)
  403. {
  404. uint32_t v;
  405. if (range == 0)
  406. return 0;
  407. if (range == UINT32_MAX)
  408. return r;
  409. if (maxrandom != 0)
  410. while (r >= maxrandom)
  411. r = (uint32_t) random();
  412. r %= range + 1;
  413. v = r;
  414. v += obuf[offset];
  415. obuf[offset] = v;
  416. if (v < 256)
  417. return r;
  418. v >>= 8;
  419. v += obuf[offset - 1];
  420. obuf[offset - 1] = v;
  421. if (v < 256)
  422. return r;
  423. v >>= 8;
  424. v += obuf[offset - 2];
  425. obuf[offset - 2] = v;
  426. if (v < 256)
  427. return r;
  428. v >>= 8;
  429. v += obuf[offset - 3];
  430. obuf[offset - 3] = v;
  431. return r;
  432. }
  433. /*
  434. * receive a reply (4th packet), shared between IPv4 and IPv6:
  435. * - transaction ID xid
  436. * - receiving time-stamp now
  437. * called from receive[46]() when the xid is odd
  438. */
  439. void
  440. receive_reply(uint32_t xid, struct timespec *now)
  441. {
  442. struct exchange *x, *t;
  443. struct xlist *bucket;
  444. uint32_t hash;
  445. int checklost;
  446. double delta;
  447. /* bucket is needed even when the next cache matches */
  448. hash = (xid >> 1) & (hashsize2 - 1);
  449. bucket = (struct xlist *) (exchanges2 + hash * sizeof(*bucket));
  450. /* try the 'next to be received' cache */
  451. if ((xnext2 != NULL) && (xnext2->xid == xid)) {
  452. x = xnext2;
  453. goto found;
  454. }
  455. /* usually the lost probability is low for request/reply */
  456. checklost = 1;
  457. /* look for the exchange */
  458. ISC_TAILQ_FOREACH_SAFE(x, bucket, hchain, t) {
  459. double waited;
  460. if (x->xid == xid)
  461. goto found;
  462. if (checklost <= 0)
  463. continue;
  464. checklost = 0;
  465. /* check for a timed-out exchange */
  466. waited = now->tv_sec - x->ts2.tv_sec;
  467. waited += (now->tv_nsec - x->ts2.tv_nsec) / 1e9;
  468. if (waited < losttime[1])
  469. continue;
  470. /* garbage collect timed-out exchange */
  471. ISC_TAILQ_REMOVE(bucket, x, hchain);
  472. ISC_TAILQ_REMOVE(&xsent2, x, gchain);
  473. free(x);
  474. collected[1] += 1;
  475. }
  476. /* no match? very late or not for us */
  477. orphans++;
  478. return;
  479. /* got it: update stats and move to the received queue */
  480. found:
  481. xrcount2++;
  482. x->ts3 = *now;
  483. delta = x->ts3.tv_sec - x->ts2.tv_sec;
  484. delta += (x->ts3.tv_nsec - x->ts2.tv_nsec) / 1e9;
  485. if (delta < dmin2)
  486. dmin2 = delta;
  487. if (delta > dmax2)
  488. dmax2 = delta;
  489. dsum2 += delta;
  490. dsumsq2 += delta * delta;
  491. xnext2 = ISC_TAILQ_NEXT(x, gchain);
  492. ISC_TAILQ_REMOVE(bucket, x, hchain);
  493. ISC_TAILQ_REMOVE(&xsent2, x, gchain);
  494. ISC_TAILQ_INSERT_TAIL(&xrcvd2, x, gchain);
  495. }
  496. /*
  497. * get the DHCPv4 socket descriptor
  498. * (the only complexity is broadcast enabling: there is no easy way to
  499. * recognize broadcast addresses, so the command line -B flag)
  500. */
  501. void
  502. getsock4(void)
  503. {
  504. int ret;
  505. /* update local port */
  506. if (localport != 0) {
  507. uint16_t lp = htons((uint16_t) localport);
  508. ((struct sockaddr_in *) &localaddr)->sin_port = lp;
  509. }
  510. sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  511. if (sock < 0) {
  512. perror("socket");
  513. exit(1);
  514. }
  515. ret = bind(sock,
  516. (struct sockaddr *) &localaddr,
  517. sizeof(struct sockaddr_in));
  518. if (ret < 0) {
  519. perror("bind");
  520. exit(1);
  521. }
  522. /* enable broadcast if needed or required */
  523. if (isbroadcast != 0) {
  524. int on = 1;
  525. ret = setsockopt(sock,
  526. SOL_SOCKET, SO_BROADCAST,
  527. &on, sizeof(on));
  528. if (ret < 0) {
  529. perror("setsockopt(SO_BROADCAST)");
  530. exit(1);
  531. }
  532. }
  533. }
  534. /*
  535. * build a DHCPv4 DISCOVER from a relay template
  536. * (implicit parameters are the local (giaddr) and MAC addresses (chaddr))
  537. * (assume the link is Ethernet)
  538. */
  539. void
  540. build_template_discover4(void)
  541. {
  542. uint8_t *p = template_discover4;
  543. length_discover4 = BOOTP_MIN_LEN;
  544. xid_discover4 = DHCP_OFF_XID;
  545. random_discover4 = DHCP_OFF_CHADDR + 6;
  546. /* opcode */
  547. p[DHCP_OFF_OPCODE] = BOOTP_OP_REQUEST;
  548. /* hardware address type */
  549. p[DHCP_OFF_HTYPE] = DHCP_HTYPE_ETHER;
  550. /* hardware address length */
  551. p[DHCP_OFF_HLEN] = 6;
  552. /* hops */
  553. p[DHCP_OFF_HOPS] = 1;
  554. /* gateway address */
  555. memcpy(p + DHCP_OFF_GIADDR,
  556. &((struct sockaddr_in *) &localaddr)->sin_addr,
  557. 4);
  558. /* hardware address */
  559. memcpy(p + DHCP_OFF_CHADDR, mac_prefix, 6);
  560. /* cookie */
  561. memcpy(p + DHCP_OFF_COOKIE, dhcp_cookie, 4);
  562. /* options */
  563. p += DHCP_OFF_OPTIONS;
  564. /* inline DHCP message type */
  565. *p++ = DHCP_OPT_DHCP_MSGTYPE;
  566. *p++ = 1;
  567. *p++ = DHCP_OP_DISCOVER;
  568. /* inline DHCP parameter request list (default) */
  569. *p++ = DHCP_OPT_DHCP_PRL;
  570. *p++ = 7;
  571. *p++ = DHCP_OPT_SUBNET_MASK;
  572. *p++ = DHCP_OPT_BROADCAST;
  573. *p++ = DHCP_OPT_TIME_OFFSET;
  574. *p++ = DHCP_OPT_ROUTERS;
  575. *p++ = DHCP_OPT_DOMAIN_NAME;
  576. *p++ = DHCP_OPT_DNS_SERVERS;
  577. *p++ = DHCP_OPT_HOST_NAME;
  578. /* end */
  579. *p = DHCP_OPT_END;
  580. }
  581. /*
  582. * get a DHCPv4 client/relay first packet (usually a DISCOVER) template
  583. * from the file given in the command line (-T<template-file>)
  584. * and xid/rnd offsets (-X<xid-offset> and -O<random-offset>)
  585. */
  586. void
  587. get_template_discover4(void)
  588. {
  589. uint8_t *p = template_discover4;
  590. int fd, cc, i, j;
  591. fd = open(templatefile[0], O_RDONLY);
  592. if (fd < 0) {
  593. fprintf(stderr, "open(%s): %s\n",
  594. templatefile[0], strerror(errno));
  595. exit(2);
  596. }
  597. cc = read(fd, tbuf, sizeof(tbuf));
  598. (void) close(fd);
  599. if (cc < 0) {
  600. fprintf(stderr, "read(%s): %s\n",
  601. templatefile[0], strerror(errno));
  602. exit(1);
  603. }
  604. if (cc < 100) {
  605. fprintf(stderr, "file '%s' too short\n", templatefile[0]);
  606. exit(2);
  607. }
  608. if (cc > 8193) {
  609. fprintf(stderr,"file '%s' too large\n", templatefile[0]);
  610. exit(2);
  611. }
  612. j = 0;
  613. for (i = 0; i < cc; i++) {
  614. if (isspace((int) tbuf[i]))
  615. continue;
  616. if (!isxdigit((int) tbuf[i])) {
  617. fprintf(stderr,
  618. "illegal char[%d]='%c' in file '%s'\n",
  619. i, (int) tbuf[i], templatefile[0]);
  620. exit(2);
  621. }
  622. tbuf[j] = tbuf[i];
  623. j++;
  624. }
  625. cc = j;
  626. if ((cc & 1) != 0) {
  627. fprintf(stderr,
  628. "odd number of hexadecimal digits in file '%s'\n",
  629. templatefile[0]);
  630. exit(2);
  631. }
  632. length_discover4 = cc >> 1;
  633. for (i = 0; i < cc; i += 2)
  634. (void) sscanf(tbuf + i, "%02hhx", &p[i >> 1]);
  635. if (xidoffset[0] >= 0)
  636. xid_discover4 = (size_t) xidoffset[0];
  637. else
  638. xid_discover4 = DHCP_OFF_XID;
  639. if (xid_discover4 + 4 > length_discover4) {
  640. fprintf(stderr,
  641. "xid (at %zu) outside the template (length %zu)?\n",
  642. xid_discover4, length_discover4);
  643. exit(2);
  644. }
  645. if (rndoffset[0] >= 0)
  646. random_discover4 = (size_t) rndoffset[0];
  647. else
  648. random_discover4 = DHCP_OFF_CHADDR + 6;
  649. if (random_discover4 > length_discover4) {
  650. fprintf(stderr,
  651. "random (at %zu) outside the template (length %zu)?\n",
  652. random_discover4, length_discover4);
  653. exit(2);
  654. }
  655. }
  656. /*
  657. * build a DHCPv4 REQUEST from a relay template
  658. * (implicit parameters are the local (giaddr) and MAC addresses (chaddr))
  659. * (assume the link is Ethernet)
  660. */
  661. void
  662. build_template_request4(void)
  663. {
  664. uint8_t *p = template_request4;
  665. length_request4 = BOOTP_MIN_LEN;
  666. xid_request4 = DHCP_OFF_XID;
  667. elapsed_request4 = DHCP_OFF_SECS;
  668. random_request4 = DHCP_OFF_CHADDR + 6;
  669. /* opcode */
  670. p[DHCP_OFF_OPCODE] = BOOTP_OP_REQUEST;
  671. /* hardware address type */
  672. p[DHCP_OFF_HTYPE] = DHCP_HTYPE_ETHER;
  673. /* hardware address length */
  674. p[DHCP_OFF_HLEN] = 6;
  675. /* hops */
  676. p[DHCP_OFF_HOPS] = 1;
  677. /* gateway address */
  678. memcpy(p + DHCP_OFF_GIADDR,
  679. &((struct sockaddr_in *) &localaddr)->sin_addr,
  680. 4);
  681. /* hardware address */
  682. memcpy(p + DHCP_OFF_CHADDR, mac_prefix, 6);
  683. /* cookie */
  684. memcpy(p + DHCP_OFF_COOKIE, dhcp_cookie, 4);
  685. /* options */
  686. p += DHCP_OFF_OPTIONS;
  687. /* inline DHCP message type */
  688. *p++ = DHCP_OPT_DHCP_MSGTYPE;
  689. *p++ = 1;
  690. *p++ = DHCP_OP_REQUEST;
  691. /* place for DHCP server id (option) */
  692. serverid_request4 = p - template_request4;
  693. p += DHCP_OPTLEN_SRVID;
  694. /* place for DHCP requested IP address (address) */
  695. *p++ = DHCP_OPT_DHCP_ADDRESS;
  696. *p++ = 4;
  697. reqaddr_request4 = p - template_request4;
  698. p += 4;
  699. /* inline DHCP parameter request list (default) */
  700. *p++ = DHCP_OPT_DHCP_PRL;
  701. *p++ = 7;
  702. *p++ = DHCP_OPT_SUBNET_MASK;
  703. *p++ = DHCP_OPT_BROADCAST;
  704. *p++ = DHCP_OPT_TIME_OFFSET;
  705. *p++ = DHCP_OPT_ROUTERS;
  706. *p++ = DHCP_OPT_DOMAIN_NAME;
  707. *p++ = DHCP_OPT_DNS_SERVERS;
  708. *p++ = DHCP_OPT_HOST_NAME;
  709. /* end */
  710. *p = DHCP_OPT_END;
  711. }
  712. /*
  713. * get a DHCPv4 client/relay third packet (usually a REQUEST) template
  714. * from the file given in the command line (-T<template-file>)
  715. * and offsets (-X,-O,-E,-S,-I).
  716. */
  717. void
  718. get_template_request4(void)
  719. {
  720. uint8_t *p = template_request4;
  721. int fd, cc, i, j;
  722. fd = open(templatefile[1], O_RDONLY);
  723. if (fd < 0) {
  724. fprintf(stderr, "open(%s): %s\n",
  725. templatefile[1], strerror(errno));
  726. exit(2);
  727. }
  728. cc = read(fd, tbuf, sizeof(tbuf));
  729. (void) close(fd);
  730. if (cc < 0) {
  731. fprintf(stderr, "read(%s): %s\n",
  732. templatefile[1], strerror(errno));
  733. exit(1);
  734. }
  735. if (cc < 100) {
  736. fprintf(stderr, "file '%s' too short\n", templatefile[1]);
  737. exit(2);
  738. }
  739. if (cc > 8193) {
  740. fprintf(stderr,"file '%s' too large\n", templatefile[1]);
  741. exit(2);
  742. }
  743. j = 0;
  744. for (i = 0; i < cc; i++) {
  745. if (isspace((int) tbuf[i]))
  746. continue;
  747. if (!isxdigit((int) tbuf[i])) {
  748. fprintf(stderr,
  749. "illegal char[%d]='%c' in file '%s'\n",
  750. i, (int) tbuf[i], templatefile[1]);
  751. exit(2);
  752. }
  753. tbuf[j] = tbuf[i];
  754. j++;
  755. }
  756. cc = j;
  757. if ((cc & 1) != 0) {
  758. fprintf(stderr,
  759. "odd number of hexadecimal digits in file '%s'\n",
  760. templatefile[1]);
  761. exit(2);
  762. }
  763. length_request4 = cc >> 1;
  764. for (i = 0; i < cc; i += 2)
  765. (void) sscanf(tbuf + i, "%02hhx", &p[i >> 1]);
  766. if (xidoffset[1] >= 0)
  767. xid_request4 = (size_t) xidoffset[1];
  768. else
  769. xid_request4 = DHCP_OFF_XID;
  770. if (xid_request4 + 4 > length_request4) {
  771. fprintf(stderr,
  772. "xid (at %zu) outside the template (length %zu)?\n",
  773. xid_request4, length_request4);
  774. exit(2);
  775. }
  776. if (rndoffset[1] >= 0)
  777. random_request4 = (size_t) rndoffset[1];
  778. else
  779. random_request4 = DHCP_OFF_CHADDR + 6;
  780. if (random_request4 > length_request4) {
  781. fprintf(stderr,
  782. "random (at %zu) outside the template (length %zu)?\n",
  783. random_request4, length_request4);
  784. exit(2);
  785. }
  786. if (elpoffset >= 0)
  787. elapsed_request4 = (size_t) elpoffset;
  788. else
  789. elapsed_request4 = DHCP_OFF_SECS;
  790. if (elapsed_request4 + 2 > length_request4) {
  791. fprintf(stderr,
  792. "secs (at %zu) outside the template (length %zu)?\n",
  793. elapsed_request4, length_request4);
  794. exit(2);
  795. }
  796. serverid_request4 = (size_t) sidoffset;
  797. if (serverid_request4 + 6 > length_request4) {
  798. fprintf(stderr,
  799. "server-id option (at %zu) outside the template "
  800. "(length %zu)?\n",
  801. serverid_request4, length_request4);
  802. exit(2);
  803. }
  804. reqaddr_request4 = (size_t) ripoffset;
  805. if (reqaddr_request4 + 4 > length_request4) {
  806. fprintf(stderr,
  807. "requested-ip-address option (at %zu) outside "
  808. "the template (length %zu)?\n",
  809. reqaddr_request4, length_request4);
  810. exit(2);
  811. }
  812. }
  813. /*
  814. * send the DHCPv4 REQUEST third packet
  815. * (the transaction ID is odd)
  816. * (TODO: check for errors in the OFFER)
  817. */
  818. void
  819. send_request4(struct exchange *x0)
  820. {
  821. struct exchange *x;
  822. struct xlist *bucket;
  823. uint32_t hash;
  824. ssize_t ret;
  825. x = (struct exchange *) malloc(sizeof(*x));
  826. if (x == NULL) {
  827. locallimit++;
  828. perror("send2");
  829. return;
  830. }
  831. memcpy(x, x0, sizeof(*x));
  832. x->order2 = xscount2++;
  833. x->xid |= 1;
  834. hash = x->xid >> 1;
  835. ISC_TAILQ_INSERT_TAIL(&xsent2, x, gchain);
  836. hash &= hashsize2 - 1;
  837. bucket = (struct xlist *) (exchanges2 + hash * sizeof(*bucket));
  838. ISC_TAILQ_INSERT_TAIL(bucket, x, hchain);
  839. memcpy(obuf, template_request4, length_request4);
  840. /* xid */
  841. memcpy(obuf + xid_request4, &x->xid, 4);
  842. /* random */
  843. randomize(random_request4, x->rnd);
  844. /* secs */
  845. if (elapsed_request4 > 0) {
  846. int secs;
  847. secs = x->ts1.tv_sec - x->ts0.tv_sec;
  848. if (x->ts1.tv_nsec < x->ts0.tv_nsec)
  849. secs += 1;
  850. if (secs > 0) {
  851. obuf[elapsed_request4] = secs >> 8;
  852. obuf[elapsed_request4 + 1] = secs & 0xff;
  853. }
  854. }
  855. /* server ID */
  856. memcpy(obuf + serverid_request4, x->sid, x->sidlen);
  857. /* requested IP address */
  858. memcpy(obuf + reqaddr_request4, ibuf + DHCP_OFF_YIADDR, 4);
  859. /* timestamp */
  860. ret = clock_gettime(CLOCK_REALTIME, &x->ts2);
  861. if (ret < 0) {
  862. perror("clock_gettime(send2)");
  863. fatal = 1;
  864. return;
  865. }
  866. ret = sendto(sock, obuf, length_request4, 0,
  867. (struct sockaddr *) &serveraddr,
  868. sizeof(struct sockaddr_in));
  869. if (ret >= 0)
  870. return;
  871. if ((errno == EAGAIN) || (errno == EWOULDBLOCK) ||
  872. (errno == ENOBUFS) || (errno == ENOMEM))
  873. locallimit++;
  874. perror("send2");
  875. }
  876. /*
  877. * send the DHCPv4 DISCOVER first packet
  878. * (for 4-exchange, the transaction ID xid is even)
  879. */
  880. int
  881. send4(void)
  882. {
  883. struct exchange *x;
  884. struct xlist *bucket;
  885. uint32_t hash;
  886. ssize_t ret;
  887. x = (struct exchange *) malloc(sizeof(*x));
  888. if (x == NULL)
  889. return -ENOMEM;
  890. memset(x, 0, sizeof(*x));
  891. x->order0 = xscount0++;
  892. hash = x->rnd = (uint32_t) random();
  893. if (simple == 0)
  894. x->xid = hash << 1;
  895. else
  896. x->xid = hash;
  897. ISC_TAILQ_INSERT_TAIL(&xsent0, x, gchain);
  898. hash &= hashsize0 - 1;
  899. bucket = (struct xlist *) (exchanges0 + hash * sizeof(*bucket));
  900. ISC_TAILQ_INSERT_TAIL(bucket, x, hchain);
  901. memcpy(obuf, template_discover4, length_discover4);
  902. /* xid */
  903. memcpy(obuf + xid_discover4, &x->xid, 4);
  904. /* random */
  905. x->rnd = randomize(random_discover4, x->rnd);
  906. /* timestamp */
  907. ret = clock_gettime(CLOCK_REALTIME, &last);
  908. if (ret < 0) {
  909. perror("clock_gettime(send)");
  910. fatal = 1;
  911. return -errno;
  912. }
  913. x->ts0 = last;
  914. errno = 0;
  915. ret = sendto(sock, obuf, length_discover4, 0,
  916. (struct sockaddr *) &serveraddr,
  917. sizeof(struct sockaddr_in));
  918. if (ret == (ssize_t) length_discover4)
  919. return 0;
  920. return -errno;
  921. }
  922. /*
  923. * scan a DHCPv4 OFFER to get its server-id option
  924. */
  925. int
  926. scan_for_srvid4(struct exchange *x, size_t cc)
  927. {
  928. size_t off = DHCP_OFF_OPTIONS;
  929. for (;;) {
  930. if (off + DHCP_OPTLEN_SRVID > cc) {
  931. fprintf(stderr, "truncated\n");
  932. return -1;
  933. }
  934. if (ibuf[off] == DHCP_OPT_DHCP_SRVID)
  935. break;
  936. if (ibuf[off] == DHCP_OPT_END) {
  937. fprintf(stderr, "server-id not found\n");
  938. return -1;
  939. }
  940. if (ibuf[off] == DHCP_OPT_PAD) {
  941. off++;
  942. continue;
  943. }
  944. off += 2 + ibuf[off + 1];
  945. }
  946. /* check length */
  947. if (ibuf[off + 1] != DHCP_OPTLEN_SRVID - 2) {
  948. fprintf(stderr,
  949. "bad server-id length (%hhu)\n",
  950. ibuf[off + 1]);
  951. return -1;
  952. }
  953. /* cache it in the global variables when required and not yet done */
  954. if ((usefirst != 0) && (gsrvid == NULL)) {
  955. memcpy(gsrvidbuf, ibuf + off, DHCP_OPTLEN_SRVID);
  956. gsrvid = gsrvidbuf;
  957. gsrvidlen = DHCP_OPTLEN_SRVID;
  958. }
  959. x->sid = ibuf + off;
  960. x->sidlen = DHCP_OPTLEN_SRVID;
  961. return 0;
  962. }
  963. /*
  964. * receive a DHCPv4 packet
  965. */
  966. void
  967. receive4(void)
  968. {
  969. struct exchange *x, *t;
  970. struct xlist *bucket;
  971. struct timespec now;
  972. ssize_t cc;
  973. uint32_t xid, hash;
  974. int checklost = 0;
  975. double delta;
  976. cc = recv(sock, ibuf, sizeof(ibuf), 0);
  977. if (cc < 0) {
  978. if ((errno == EAGAIN) ||
  979. (errno == EWOULDBLOCK) ||
  980. (errno == EINTR))
  981. return;
  982. perror("recv");
  983. fatal = 1;
  984. return;
  985. }
  986. /* enforce a reasonable length */
  987. if (cc < BOOTP_MIN_LEN) {
  988. tooshort++;
  989. return;
  990. }
  991. /* must be a BOOTP REPLY */
  992. if (ibuf[DHCP_OFF_OPCODE] != BOOTP_OP_REPLY)
  993. return;
  994. if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
  995. perror("clock_gettime(receive)");
  996. fatal = 1;
  997. return;
  998. }
  999. memcpy(&xid, ibuf + xid_discover4, 4);
  1000. /* 4-packet exchange even/odd xid */
  1001. if (simple == 0) {
  1002. if ((xid & 1) != 0) {
  1003. receive_reply(xid, &now);
  1004. return;
  1005. }
  1006. hash = (xid >> 1) & (hashsize0 - 1);
  1007. } else
  1008. hash = xid & (hashsize0 - 1);
  1009. /* now it is the second packet, get the bucket which is needed */
  1010. bucket = (struct xlist *) (exchanges0 + hash * sizeof(*bucket));
  1011. /* try the 'next to be received' cache */
  1012. if ((xnext0 != NULL) && (xnext0->xid == xid)) {
  1013. x = xnext0;
  1014. goto found;
  1015. }
  1016. /* if the rate is not illimited, garbage collect up to 3
  1017. timed-out exchanges */
  1018. if (rate != 0)
  1019. checklost = 3;
  1020. /* look for the exchange */
  1021. ISC_TAILQ_FOREACH_SAFE(x, bucket, hchain, t) {
  1022. double waited;
  1023. if (x->xid == xid)
  1024. goto found;
  1025. if (checklost <= 0)
  1026. continue;
  1027. /* check for a timed-out exchange */
  1028. waited = now.tv_sec - x->ts0.tv_sec;
  1029. waited += (now.tv_nsec - x->ts0.tv_nsec) / 1e9;
  1030. if (waited < losttime[0]) {
  1031. checklost = 0;
  1032. continue;
  1033. }
  1034. /* garbage collect timed-out exchange */
  1035. checklost--;
  1036. ISC_TAILQ_REMOVE(bucket, x, hchain);
  1037. ISC_TAILQ_REMOVE(&xsent0, x, gchain);
  1038. free(x);
  1039. collected[0] += 1;
  1040. }
  1041. /* no match? very late or not for us */
  1042. orphans++;
  1043. return;
  1044. /* got it: update stats and move to the received queue */
  1045. found:
  1046. xrcount0++;
  1047. x->ts1 = now;
  1048. delta = x->ts1.tv_sec - x->ts0.tv_sec;
  1049. delta += (x->ts1.tv_nsec - x->ts0.tv_nsec) / 1e9;
  1050. if (delta < dmin0)
  1051. dmin0 = delta;
  1052. if (delta > dmax0)
  1053. dmax0 = delta;
  1054. dsum0 += delta;
  1055. dsumsq0 += delta * delta;
  1056. xnext0 = ISC_TAILQ_NEXT(x, gchain);
  1057. ISC_TAILQ_REMOVE(bucket, x, hchain);
  1058. ISC_TAILQ_REMOVE(&xsent0, x, gchain);
  1059. ISC_TAILQ_INSERT_TAIL(&xrcvd0, x, gchain);
  1060. /* if the exchange is not finished, go to the second part */
  1061. if (simple == 0) {
  1062. int ret = 0;
  1063. /* the server-ID option is needed */
  1064. if ((usefirst != 0) && (gsrvid != NULL)) {
  1065. x->sid = gsrvid;
  1066. x->sidlen = gsrvidlen;
  1067. } else
  1068. ret = scan_for_srvid4(x, cc);
  1069. if (ret >= 0)
  1070. send_request4(x);
  1071. }
  1072. }
  1073. /*
  1074. * get the DHCPv6 socket descriptor
  1075. */
  1076. void
  1077. getsock6(void)
  1078. {
  1079. struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &serveraddr;
  1080. int ret;
  1081. /* update local port */
  1082. if (localport != 0) {
  1083. uint16_t lp = htons((uint16_t) localport);
  1084. ((struct sockaddr_in6 *) &localaddr)->sin6_port = lp;
  1085. }
  1086. sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  1087. if (sock < 0) {
  1088. perror("socket");
  1089. exit(1);
  1090. }
  1091. /* perform the multicast stuff when the destination is multicast */
  1092. if (IN6_IS_ADDR_MULTICAST(&s6->sin6_addr)) {
  1093. int hops = 1;
  1094. ret = setsockopt(sock,
  1095. IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  1096. &hops, sizeof(hops));
  1097. if (ret < 0) {
  1098. perror("setsockopt(IPV6_MULTICAST_HOPS)");
  1099. exit(1);
  1100. }
  1101. }
  1102. if (isinterface && IN6_IS_ADDR_MULTICAST(&s6->sin6_addr)) {
  1103. unsigned idx = if_nametoindex(localname);
  1104. if (idx == 0) {
  1105. fprintf(stderr,
  1106. "if_nametoindex(%s) failed\n",
  1107. localname);
  1108. exit(1);
  1109. }
  1110. ret = setsockopt(sock,
  1111. IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1112. &idx, sizeof(idx));
  1113. if (ret < 0) {
  1114. perror("setsockopt(IPV6_MULTICAST_IF)");
  1115. exit(1);
  1116. }
  1117. }
  1118. }
  1119. /*
  1120. * build a DHCPv6 SOLICIT template
  1121. * (implicit parameter is the DUID, don't assume an Ethernet link)
  1122. */
  1123. void
  1124. build_template_solicit6(void)
  1125. {
  1126. uint8_t *p = template_solicit6;
  1127. xid_solicit6 = DHCP6_OFF_XID;
  1128. /* message type */
  1129. p[DHCP6_OFF_MSGTYP] = DHCP6_OP_SOLICIT;
  1130. /* options */
  1131. p += DHCP6_OFF_OPTIONS;
  1132. /* elapsed time */
  1133. p[1] = DHCP6_OPT_ELAPSED_TIME;
  1134. p[3] = 2;
  1135. p += 6;
  1136. /* rapid commit */
  1137. if (rapidcommit != 0) {
  1138. p[1] = DHCP6_OPT_RAPID_COMMIT;
  1139. p += 4;
  1140. }
  1141. /* client ID */
  1142. p[1] = DHCP6_OPT_CLIENTID;
  1143. p[3] = duid_length;
  1144. memcpy(p + 4, duid_prefix, duid_length);
  1145. p += 4 + duid_length;
  1146. random_solicit6 = p - template_solicit6;
  1147. /* option request option */
  1148. p[1] = DHCP6_OPT_ORO;
  1149. p[3] = 4;
  1150. p[5] = DHCP6_OPT_NAME_SERVERS;
  1151. p[7] = DHCP6_OPT_DOMAIN_SEARCH;
  1152. p += 8;
  1153. /* IA_NA (IAID = 1, T1 = 3600, T2 = 5400) */
  1154. p[1] = DHCP6_OPT_IA_NA;
  1155. p[3] = 12;
  1156. p[7] = 1;
  1157. p[10] = 3600 >> 8;
  1158. p[11] = 3600 & 0xff;
  1159. p[14] = 5400 >> 8;
  1160. p[15] = 5400 & 0xff;
  1161. p += 16;
  1162. /* set length */
  1163. length_solicit6 = p - template_solicit6;
  1164. }
  1165. /*
  1166. * get a DHCPv6 first packet (usually a SOLICIT) template
  1167. * from the file given in the command line (-T<template-file>)
  1168. * and xid/rnd offsets (-X<xid-offset> and -O<random-offset>)
  1169. */
  1170. void
  1171. get_template_solicit6(void)
  1172. {
  1173. uint8_t *p = template_solicit6;
  1174. int fd, cc, i, j;
  1175. fd = open(templatefile[0], O_RDONLY);
  1176. if (fd < 0) {
  1177. fprintf(stderr, "open(%s): %s\n",
  1178. templatefile[0], strerror(errno));
  1179. exit(2);
  1180. }
  1181. cc = read(fd, tbuf, sizeof(tbuf));
  1182. (void) close(fd);
  1183. if (cc < 0) {
  1184. fprintf(stderr, "read(%s): %s\n",
  1185. templatefile[0], strerror(errno));
  1186. exit(1);
  1187. }
  1188. if (cc < 10) {
  1189. fprintf(stderr, "file '%s' too short\n", templatefile[0]);
  1190. exit(2);
  1191. }
  1192. if (cc > 8193) {
  1193. fprintf(stderr,"file '%s' too large\n", templatefile[0]);
  1194. exit(2);
  1195. }
  1196. j = 0;
  1197. for (i = 0; i < cc; i++) {
  1198. if (isspace((int) tbuf[i]))
  1199. continue;
  1200. if (!isxdigit((int) tbuf[i])) {
  1201. fprintf(stderr,
  1202. "illegal char[%d]='%c' in file '%s'\n",
  1203. i, (int) tbuf[i], templatefile[0]);
  1204. exit(2);
  1205. }
  1206. tbuf[j] = tbuf[i];
  1207. j++;
  1208. }
  1209. cc = j;
  1210. if ((cc & 1) != 0) {
  1211. fprintf(stderr,
  1212. "odd number of hexadecimal digits in file '%s'\n",
  1213. templatefile[0]);
  1214. exit(2);
  1215. }
  1216. length_solicit6 = cc >> 1;
  1217. for (i = 0; i < cc; i += 2)
  1218. (void) sscanf(tbuf + i, "%02hhx", &p[i >> 1]);
  1219. if (xidoffset[0] >= 0)
  1220. xid_solicit6 = (size_t) xidoffset[0];
  1221. else
  1222. xid_solicit6 = DHCP6_OFF_XID;
  1223. if (xid_solicit6 + 3 > length_solicit6) {
  1224. fprintf(stderr,
  1225. "xid (at %zu) is outside the template (length %zu)?\n",
  1226. xid_solicit6, length_solicit6);
  1227. exit(2);
  1228. }
  1229. if (rndoffset[0] >= 0)
  1230. random_solicit6 = (size_t) rndoffset[0];
  1231. else
  1232. random_solicit6 = 0;
  1233. if (random_solicit6 > length_solicit6) {
  1234. fprintf(stderr,
  1235. "random (at %zu) outside the template (length %zu)?\n",
  1236. random_solicit6, length_solicit6);
  1237. exit(2);
  1238. }
  1239. }
  1240. /*
  1241. * build a DHCPv6 REQUEST template
  1242. * (implicit parameter is the DUID, don't assume an Ethernet link)
  1243. */
  1244. void
  1245. build_template_request6(void)
  1246. {
  1247. uint8_t *p = template_request6;
  1248. xid_request6 = DHCP6_OFF_XID;
  1249. /* message type */
  1250. p[DHCP6_OFF_MSGTYP] = DHCP6_OP_REQUEST;
  1251. /* options */
  1252. p += DHCP6_OFF_OPTIONS;
  1253. /* elapsed time */
  1254. p[1] = DHCP6_OPT_ELAPSED_TIME;
  1255. p[3] = 2;
  1256. p += 4;
  1257. elapsed_request6 = p - template_request6;
  1258. p += 2;
  1259. /* client ID */
  1260. p[1] = DHCP6_OPT_CLIENTID;
  1261. p[3] = duid_length;
  1262. memcpy(p + 4, duid_prefix, duid_length);
  1263. p += 4 + duid_length;
  1264. random_request6 = p - template_request6;
  1265. /* option request option */
  1266. p[1] = DHCP6_OPT_ORO;
  1267. p[3] = 4;
  1268. p[5] = DHCP6_OPT_NAME_SERVERS;
  1269. p[7] = DHCP6_OPT_DOMAIN_SEARCH;
  1270. p += 8;
  1271. /* server ID and IA_NA */
  1272. serverid_request6 = p - template_request6;
  1273. reqaddr_request6 = p - template_request6;
  1274. /* set length */
  1275. length_request6 = p - template_request6;
  1276. }
  1277. /*
  1278. * get a DHCPv6 third packet (usually a REQUEST) template
  1279. * from the file given in the command line (-T<template-file>)
  1280. * and offsets (-X,-O,-E,-S,-I).
  1281. */
  1282. void
  1283. get_template_request6(void)
  1284. {
  1285. uint8_t *p = template_request6;
  1286. int fd, cc, i, j;
  1287. fd = open(templatefile[1], O_RDONLY);
  1288. if (fd < 0) {
  1289. fprintf(stderr, "open(%s): %s\n",
  1290. templatefile[1], strerror(errno));
  1291. exit(2);
  1292. }
  1293. cc = read(fd, tbuf, sizeof(tbuf));
  1294. (void) close(fd);
  1295. if (cc < 0) {
  1296. fprintf(stderr, "read(%s): %s\n",
  1297. templatefile[1], strerror(errno));
  1298. exit(1);
  1299. }
  1300. if (cc < 10) {
  1301. fprintf(stderr, "file '%s' too short\n", templatefile[1]);
  1302. exit(2);
  1303. }
  1304. if (cc > 8193) {
  1305. fprintf(stderr,"file '%s' too large\n", templatefile[1]);
  1306. exit(2);
  1307. }
  1308. j = 0;
  1309. for (i = 0; i < cc; i++) {
  1310. if (isspace((int) tbuf[i]))
  1311. continue;
  1312. if (!isxdigit((int) tbuf[i])) {
  1313. fprintf(stderr,
  1314. "illegal char[%d]='%c' in file '%s'\n",
  1315. i, (int) tbuf[i], templatefile[1]);
  1316. exit(2);
  1317. }
  1318. tbuf[j] = tbuf[i];
  1319. j++;
  1320. }
  1321. cc = j;
  1322. if ((cc & 1) != 0) {
  1323. fprintf(stderr,
  1324. "odd number of hexadecimal digits in file '%s'\n",
  1325. templatefile[1]);
  1326. exit(2);
  1327. }
  1328. length_request6 = cc >> 1;
  1329. for (i = 0; i < cc; i += 2)
  1330. (void) sscanf(tbuf + i, "%02hhx", &p[i >> 1]);
  1331. if (xidoffset[1] >= 0)
  1332. xid_request6 = (size_t) xidoffset[1];
  1333. else
  1334. xid_request6 = DHCP6_OFF_XID;
  1335. if (xid_request6 + 3 > length_request6) {
  1336. fprintf(stderr,
  1337. "xid (at %zu) is outside the template (length %zu)?\n",
  1338. xid_request6, length_request6);
  1339. exit(2);
  1340. }
  1341. if (rndoffset[1] >= 0)
  1342. random_request6 = (size_t) rndoffset[1];
  1343. else
  1344. random_request6 = 0;
  1345. if (random_request6 > length_request6) {
  1346. fprintf(stderr,
  1347. "random (at %zu) outside the template (length %zu)?\n",
  1348. random_request6, length_request6);
  1349. exit(2);
  1350. }
  1351. if (elpoffset >= 0)
  1352. elapsed_request6 = (size_t) elpoffset;
  1353. if (elapsed_request6 + 2 > length_request6) {
  1354. fprintf(stderr,
  1355. "secs (at %zu) outside the template (length %zu)?\n",
  1356. elapsed_request6, length_request6);
  1357. exit(2);
  1358. }
  1359. serverid_request6 = (size_t) sidoffset;
  1360. if (serverid_request6 > length_request6) {
  1361. fprintf(stderr,
  1362. "server-id option (at %zu) outside the template "
  1363. "(length %zu)?\n",
  1364. serverid_request6, length_request6);
  1365. exit(2);
  1366. }
  1367. reqaddr_request6 = (size_t) ripoffset;
  1368. if (reqaddr_request6 > length_request6) {
  1369. fprintf(stderr,
  1370. "requested-ip-address option (at %zu) outside "
  1371. "the template (length %zu)?\n",
  1372. reqaddr_request6, length_request6);
  1373. exit(2);
  1374. }
  1375. }
  1376. /*
  1377. * send the DHCPv6 REQUEST third packet
  1378. * (the transaction ID is odd)
  1379. * (TODO: check for errors in the ADVERTISE)
  1380. */
  1381. void
  1382. send_request6(struct exchange *x0)
  1383. {
  1384. struct exchange *x;
  1385. struct xlist *bucket;
  1386. size_t len;
  1387. uint32_t hash;
  1388. ssize_t ret;
  1389. x = (struct exchange *) malloc(sizeof(*x));
  1390. if (x == NULL) {
  1391. locallimit++;
  1392. perror("send2");
  1393. return;
  1394. }
  1395. memcpy(x, x0, sizeof(*x));
  1396. x->order2 = xscount2++;
  1397. x->xid |= 1;
  1398. hash = x->xid >> 1;
  1399. ISC_TAILQ_INSERT_TAIL(&xsent2, x, gchain);
  1400. hash &= hashsize2 - 1;
  1401. bucket = (struct xlist *) (exchanges2 + hash * sizeof(*bucket));
  1402. ISC_TAILQ_INSERT_TAIL(bucket, x, hchain);
  1403. len = length_request6;
  1404. memcpy(obuf, template_request6, len);
  1405. /* xid */
  1406. obuf[xid_request6] = x->xid >> 16;
  1407. obuf[xid_request6 + 1] = x->xid >> 8;
  1408. obuf[xid_request6 + 2] = x->xid;
  1409. /* random */
  1410. randomize(random_request6, x->rnd);
  1411. /* elapsed time */
  1412. if (elapsed_request6 > 0) {
  1413. int et;
  1414. et = (x->ts1.tv_sec - x->ts0.tv_sec) * 100;
  1415. et += (x->ts1.tv_nsec - x->ts0.tv_nsec) / 10000000;
  1416. if (et > 65535) {
  1417. obuf[elapsed_request6] = 0xff;
  1418. obuf[elapsed_request6 + 1] = 0xff;
  1419. } else if (et > 0) {
  1420. obuf[elapsed_request6] = et >> 8;
  1421. obuf[elapsed_request6 + 1] = et & 0xff;
  1422. }
  1423. }
  1424. /* server ID */
  1425. if (serverid_request6 < length_request6)
  1426. memmove(obuf + serverid_request6 + x->sidlen,
  1427. obuf + serverid_request6,
  1428. x->sidlen);
  1429. memcpy(obuf + serverid_request6, x->sid, x->sidlen);
  1430. len += x->sidlen;
  1431. /* IA_NA */
  1432. if (reqaddr_request6 < serverid_request6) {
  1433. memmove(obuf + reqaddr_request6 + x->ianalen,
  1434. obuf + reqaddr_request6,
  1435. x->ianalen);
  1436. memcpy(obuf + reqaddr_request6, x->iana, x->ianalen);
  1437. } else if (reqaddr_request6 < length_request6) {
  1438. memmove(obuf + reqaddr_request6 + x->sidlen + x->ianalen,
  1439. obuf + reqaddr_request6 + x->sidlen,
  1440. x->ianalen);
  1441. memcpy(obuf + reqaddr_request6+ x->sidlen,
  1442. x->iana,
  1443. x->ianalen);
  1444. } else
  1445. memcpy(obuf + len, x->iana, x->ianalen);
  1446. len += x->ianalen;
  1447. /* timestamp */
  1448. ret = clock_gettime(CLOCK_REALTIME, &x->ts2);
  1449. if (ret < 0) {
  1450. perror("clock_gettime(send2)");
  1451. fatal = 1;
  1452. return;
  1453. }
  1454. ret = sendto(sock, obuf, len, 0,
  1455. (struct sockaddr *) &serveraddr,
  1456. sizeof(struct sockaddr_in6));
  1457. if (ret >= 0)
  1458. return;
  1459. if ((errno == EAGAIN) || (errno == EWOULDBLOCK) ||
  1460. (errno == ENOBUFS) || (errno == ENOMEM))
  1461. locallimit++;
  1462. perror("send2");
  1463. }
  1464. /*
  1465. * send the DHCPv6 SOLICIT first packet
  1466. * (for 4-exchange, the transaction ID xid is even)
  1467. */
  1468. int
  1469. send6(void)
  1470. {
  1471. struct exchange *x;
  1472. struct xlist *bucket;
  1473. uint32_t hash;
  1474. ssize_t ret;
  1475. x = (struct exchange *) malloc(sizeof(*x));
  1476. if (x == NULL)
  1477. return -ENOMEM;
  1478. memset(x, 0, sizeof(*x));
  1479. x->order0 = xscount0++;
  1480. hash = x->rnd = (uint32_t) random();
  1481. if (simple == 0)
  1482. x->xid = (hash << 1) & 0x00ffffff;
  1483. else
  1484. x->xid = hash & 0x00ffffff;
  1485. ISC_TAILQ_INSERT_TAIL(&xsent0, x, gchain);
  1486. hash &= hashsize0 - 1;
  1487. bucket = (struct xlist *) (exchanges0 + hash * sizeof(*bucket));
  1488. ISC_TAILQ_INSERT_TAIL(bucket, x, hchain);
  1489. memcpy(obuf, template_solicit6, length_solicit6);
  1490. /* xid */
  1491. obuf[xid_solicit6] = x->xid >> 16;
  1492. obuf[xid_solicit6 + 1] = x->xid >> 8;
  1493. obuf[xid_solicit6 + 2] = x->xid;
  1494. /* random */
  1495. x->rnd = randomize(random_solicit6, x->rnd);
  1496. /* timestamp */
  1497. ret = clock_gettime(CLOCK_REALTIME, &last);
  1498. if (ret < 0) {
  1499. perror("clock_gettime(send)");
  1500. fatal = 1;
  1501. return -errno;
  1502. }
  1503. x->ts0 = last;
  1504. errno = 0;
  1505. ret = sendto(sock, obuf, length_solicit6, 0,
  1506. (struct sockaddr *) &serveraddr,
  1507. sizeof(struct sockaddr_in6));
  1508. if (ret == (ssize_t) length_solicit6)
  1509. return 0;
  1510. return -errno;
  1511. }
  1512. /*
  1513. * scan a DHCPv6 ADVERTISE to get its server-id option
  1514. */
  1515. int
  1516. scan_for_srvid6(struct exchange *x, size_t cc)
  1517. {
  1518. size_t off = DHCP6_OFF_OPTIONS, len;
  1519. for (;;) {
  1520. if (off + 4 > cc) {
  1521. fprintf(stderr, "server-id not found\n");
  1522. return -1;
  1523. }
  1524. if ((ibuf[off] == 0) && (ibuf[off + 1] == DHCP6_OPT_SERVERID))
  1525. break;
  1526. off += 4 + (ibuf[off + 2] << 8) + ibuf[off + 3];
  1527. }
  1528. len = 4 + (ibuf[off + 2] << 8) + ibuf[off + 3];
  1529. /* cache it in the global variables when required and not yet done */
  1530. if ((usefirst != 0) && (gsrvid == NULL)) {
  1531. if (len <= sizeof(gsrvidbuf)) {
  1532. memcpy(gsrvidbuf, ibuf + off, len);
  1533. gsrvid = gsrvidbuf;
  1534. gsrvidlen = len;
  1535. } else {
  1536. gsrvid = (uint8_t *) malloc(len);
  1537. if (gsrvid == NULL) {
  1538. perror("malloc(gsrvid");
  1539. return -1;
  1540. }
  1541. memcpy(gsrvid, ibuf + off, len);
  1542. gsrvidlen = len;
  1543. }
  1544. }
  1545. x->sid = ibuf + off;
  1546. x->sidlen = len;
  1547. return 0;
  1548. }
  1549. /*
  1550. * scan a DHCPv6 ADVERTISE to get its IA_NA option
  1551. * (TODO: check for errors)
  1552. */
  1553. int
  1554. scan_for_ia_na(struct exchange *x, size_t cc)
  1555. {
  1556. size_t off = DHCP6_OFF_OPTIONS, len;
  1557. for (;;) {
  1558. if (off + 4 > cc) {
  1559. fprintf(stderr, "ia-na not found\n");
  1560. return -1;
  1561. }
  1562. if ((ibuf[off] == 0) && (ibuf[off + 1] == DHCP6_OPT_IA_NA))
  1563. break;
  1564. off += 4 + (ibuf[off + 2] << 8) + ibuf[off + 3];
  1565. }
  1566. len = 4 + (ibuf[off + 2] << 8) + ibuf[off + 3];
  1567. x->iana = ibuf + off;
  1568. x->ianalen = len;
  1569. return 0;
  1570. }
  1571. /*
  1572. * receive a DHCPv6 packet
  1573. */
  1574. void
  1575. receive6(void)
  1576. {
  1577. struct exchange *x, *t;
  1578. struct xlist *bucket;
  1579. struct timespec now;
  1580. ssize_t cc;
  1581. uint32_t xid, hash;
  1582. int checklost = 0;
  1583. double delta;
  1584. cc = recv(sock, ibuf, sizeof(ibuf), 0);
  1585. if (cc < 0) {
  1586. if ((errno == EAGAIN) ||
  1587. (errno == EWOULDBLOCK) ||
  1588. (errno == EINTR))
  1589. return;
  1590. perror("recv");
  1591. fatal = 1;
  1592. return;
  1593. }
  1594. /* enforce a reasonable length */
  1595. if (cc < 22) {
  1596. tooshort++;
  1597. return;
  1598. }
  1599. if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
  1600. perror("clock_gettime(receive)");
  1601. fatal = 1;
  1602. return;
  1603. }
  1604. xid = ibuf[xid_solicit6] << 16;
  1605. xid |= ibuf[xid_solicit6 + 1] << 8;
  1606. xid |= ibuf[xid_solicit6 + 2];
  1607. /* 4-packet exchange even/odd xid */
  1608. if (simple == 0) {
  1609. if ((xid & 1) != 0) {
  1610. receive_reply(xid, &now);
  1611. return;
  1612. }
  1613. hash = (xid >> 1) & (hashsize0 - 1);
  1614. } else
  1615. hash = xid & (hashsize0 - 1);
  1616. /* now it is the second packet, get the bucket which is needed */
  1617. bucket = (struct xlist *) (exchanges0 + hash * sizeof(*bucket));
  1618. /* try the 'next to be received' cache */
  1619. if ((xnext0 != NULL) && (xnext0->xid == xid)) {
  1620. x = xnext0;
  1621. goto found;
  1622. }
  1623. /* if the rate is not illimited, garbage collect up to 3
  1624. timed-out exchanges */
  1625. if (rate != 0)
  1626. checklost = 3;
  1627. /* look for the exchange */
  1628. ISC_TAILQ_FOREACH_SAFE(x, bucket, hchain, t) {
  1629. double waited;
  1630. if (x->xid == xid)
  1631. goto found;
  1632. if (checklost <= 0)
  1633. continue;
  1634. /* check for a timed-out exchange */
  1635. waited = now.tv_sec - x->ts0.tv_sec;
  1636. waited += (now.tv_nsec - x->ts0.tv_nsec) / 1e9;
  1637. if (waited < losttime[0]) {
  1638. checklost = 0;
  1639. continue;
  1640. }
  1641. /* garbage collect timed-out exchange */
  1642. checklost--;
  1643. ISC_TAILQ_REMOVE(bucket, x, hchain);
  1644. ISC_TAILQ_REMOVE(&xsent0, x, gchain);
  1645. free(x);
  1646. collected[0] += 1;
  1647. }
  1648. /* no match? very late or not for us */
  1649. orphans++;
  1650. return;
  1651. /* got it: update stats and move to the received queue */
  1652. found:
  1653. xrcount0++;
  1654. x->ts1 = now;
  1655. delta = x->ts1.tv_sec - x->ts0.tv_sec;
  1656. delta += (x->ts1.tv_nsec - x->ts0.tv_nsec) / 1e9;
  1657. if (delta < dmin0)
  1658. dmin0 = delta;
  1659. if (delta > dmax0)
  1660. dmax0 = delta;
  1661. dsum0 += delta;
  1662. dsumsq0 += delta * delta;
  1663. xnext0 = ISC_TAILQ_NEXT(x, gchain);
  1664. ISC_TAILQ_REMOVE(bucket, x, hchain);
  1665. ISC_TAILQ_REMOVE(&xsent0, x, gchain);
  1666. ISC_TAILQ_INSERT_TAIL(&xrcvd0, x, gchain);
  1667. /* if the exchange is not finished, go to the second part */
  1668. if (simple == 0) {
  1669. int ret = 0;
  1670. /* the server-ID option is needed */
  1671. if ((usefirst != 0) && (gsrvid != NULL)) {
  1672. x->sid = gsrvid;
  1673. x->sidlen = gsrvidlen;
  1674. } else
  1675. ret = scan_for_srvid6(x, cc);
  1676. /* and the IA_NA option too */
  1677. if (ret >= 0)
  1678. ret = scan_for_ia_na(x, cc);
  1679. if (ret >= 0)
  1680. send_request6(x);
  1681. }
  1682. }
  1683. /*
  1684. * decode a base command line parameter
  1685. * (currently only MAC address and DUID are supported)
  1686. */
  1687. void
  1688. decodebase(void)
  1689. {
  1690. char *b0 = base[basecnt];
  1691. /* MAC address (alias Ethernet address) */
  1692. if ((strncasecmp(b0, "mac=", 4) == 0) ||
  1693. (strncasecmp(b0, "ether=", 6) == 0)) {
  1694. char *b;
  1695. b = strchr(b0, '=') + 1;
  1696. if (sscanf(b, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  1697. &mac_prefix[0], &mac_prefix[1],
  1698. &mac_prefix[2], &mac_prefix[3],
  1699. &mac_prefix[4], &mac_prefix[5]) == 6) {
  1700. if ((diags != NULL) && (strchr(diags, 'a') != NULL))
  1701. printf("set MAC to %s\n", b);
  1702. return;
  1703. }
  1704. fprintf(stderr,
  1705. "expected base in '%*s=xx:xx:xx:xx:xx:xx' format\n",
  1706. (int) (b - b0 - 1), b0);
  1707. exit(2);
  1708. }
  1709. /* DUID */
  1710. if (strncasecmp(b0, "duid=", 5) == 0) {
  1711. char *b;
  1712. size_t i, l;
  1713. if (duid_prefix != NULL) {
  1714. fprintf(stderr, "duid was already set?\n");
  1715. exit(2);
  1716. }
  1717. b = strchr(b0, '=') + 1;
  1718. l = 0;
  1719. while (*b != '\0') {
  1720. if (!isxdigit((int) *b)) {
  1721. fprintf(stderr,
  1722. "illegal char '%c' in duid\n",
  1723. (int) *b);
  1724. exit(2);
  1725. }
  1726. b++;
  1727. l++;
  1728. }
  1729. if ((l & 1) != 0) {
  1730. fprintf(stderr,
  1731. "odd number of hexadecimal digits in duid\n");
  1732. exit(2);
  1733. }
  1734. l /= 2;
  1735. if (l > 64) {
  1736. fprintf(stderr, "duid too large\n");
  1737. exit(2);
  1738. }
  1739. duid_prefix = (uint8_t *) malloc(l);
  1740. if (duid_prefix == NULL) {
  1741. perror("malloc(duid)");
  1742. exit(1);
  1743. }
  1744. b = strchr(b0, '=') + 1;
  1745. for (i = 0; i < l; i++, b += 2)
  1746. (void) sscanf(b, "%02hhx", &duid_prefix[i]);
  1747. duid_length = l;
  1748. if ((diags != NULL) && (strchr(diags, 'a') != NULL)) {
  1749. b = strchr(b0, '=') + 1;
  1750. printf("set DUID[%d] to %s\n",
  1751. duid_length, b);
  1752. }
  1753. return;
  1754. }
  1755. /* other */
  1756. fprintf(stderr, "not yet supported base '%s'\n", b0);
  1757. exit(2);
  1758. }
  1759. /*
  1760. * get the server socket address from the command line:
  1761. * - flags: inherited from main, 0 or AI_NUMERICHOST (for literals)
  1762. */
  1763. void
  1764. getserveraddr(const int flags)
  1765. {
  1766. struct addrinfo hints, *res;
  1767. char *service;
  1768. int ret;
  1769. memset(&hints, 0, sizeof(hints));
  1770. if (ipversion == 4) {
  1771. hints.ai_family = AF_INET;
  1772. service = "67";
  1773. } else {
  1774. hints.ai_family = AF_INET6;
  1775. service = "547";
  1776. }
  1777. hints.ai_socktype = SOCK_DGRAM;
  1778. hints.ai_flags = AI_NUMERICSERV | flags;
  1779. #if defined(AI_ADDRCONFIG)
  1780. hints.ai_flags |= AI_ADDRCONFIG;
  1781. #endif
  1782. hints.ai_protocol = IPPROTO_UDP;
  1783. ret = getaddrinfo(servername, service, &hints, &res);
  1784. if (ret != 0) {
  1785. fprintf(stderr, "bad server=%s: %s\n",
  1786. servername, gai_strerror(ret));
  1787. exit(2);
  1788. }
  1789. if (res->ai_next != NULL) {
  1790. fprintf(stderr, "ambiguous server=%s\n", servername);
  1791. exit(2);
  1792. }
  1793. memcpy(&serveraddr, res->ai_addr, res->ai_addrlen);
  1794. freeaddrinfo(res);
  1795. }
  1796. /*
  1797. * get the interface from the command line:
  1798. * - name of the interface
  1799. * - socket address to fill
  1800. * (in IPv6, get the first link-local address)
  1801. */
  1802. void
  1803. getinterface(const char *name, struct sockaddr_storage *ss)
  1804. {
  1805. struct ifaddrs *ifaddr, *ifa;
  1806. int ret;
  1807. size_t len;
  1808. ret = getifaddrs(&ifaddr);
  1809. if (ret < 0) {
  1810. perror("getifaddrs");
  1811. exit(1);
  1812. }
  1813. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  1814. if (ifa->ifa_name == NULL)
  1815. continue;
  1816. if (strcmp(ifa->ifa_name, name) != 0)
  1817. continue;
  1818. if ((ifa->ifa_flags & IFF_UP) == 0)
  1819. continue;
  1820. if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
  1821. continue;
  1822. if (ifa->ifa_addr == NULL)
  1823. continue;
  1824. if ((ipversion == 4) &&
  1825. (ifa->ifa_addr->sa_family != AF_INET))
  1826. continue;
  1827. if (ipversion == 6) {
  1828. struct sockaddr_in6 *a6;
  1829. a6 = (struct sockaddr_in6 *) ifa->ifa_addr;
  1830. if (a6->sin6_family != AF_INET6)
  1831. continue;
  1832. if (!IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr))
  1833. continue;
  1834. }
  1835. if (ipversion == 4)
  1836. len = sizeof(struct sockaddr_in);
  1837. else
  1838. len = sizeof(struct sockaddr_in6);
  1839. memcpy(ss, ifa->ifa_addr, len);
  1840. /* fill the server port */
  1841. if (ipversion == 4)
  1842. ((struct sockaddr_in *) ss)->sin_port = htons(67);
  1843. else
  1844. ((struct sockaddr_in6 *) ss)->sin6_port = htons(546);
  1845. return;
  1846. }
  1847. fprintf(stderr, "can't find interface %s\n", name);
  1848. exit(1);
  1849. }
  1850. /*
  1851. * get the local socket address from the command line
  1852. * (if it doesn't work, try an interface name)
  1853. */
  1854. void
  1855. getlocaladdr(void)
  1856. {
  1857. struct addrinfo hints, *res;
  1858. char *service;
  1859. int ret;
  1860. memset(&hints, 0, sizeof(hints));
  1861. if (ipversion == 4) {
  1862. hints.ai_family = AF_INET;
  1863. service = "67";
  1864. } else {
  1865. hints.ai_family = AF_INET6;
  1866. service = "546";
  1867. }
  1868. hints.ai_socktype = SOCK_DGRAM;
  1869. hints.ai_flags = AI_NUMERICSERV;
  1870. #if defined(AI_ADDRCONFIG)
  1871. hints.ai_flags |= AI_ADDRCONFIG;
  1872. #endif
  1873. hints.ai_protocol = IPPROTO_UDP;
  1874. ret = getaddrinfo(localname, service, &hints, &res);
  1875. if ((ret == EAI_NONAME)
  1876. #ifdef EAI_NODATA
  1877. || (ret == EAI_NODATA)
  1878. #endif
  1879. ) {
  1880. isinterface = 1;
  1881. getinterface(localname, &localaddr);
  1882. return;
  1883. }
  1884. if (ret != 0) {
  1885. fprintf(stderr,
  1886. "bad -l<local-addr=%s>: %s\n",
  1887. localname,
  1888. gai_strerror(ret));
  1889. exit(2);
  1890. }
  1891. /* refuse multiple addresses */
  1892. if (res->ai_next != NULL) {
  1893. fprintf(stderr,
  1894. "ambiguous -l<local-addr=%s>\n",
  1895. localname);
  1896. exit(2);
  1897. }
  1898. memcpy(&localaddr, res->ai_addr, res->ai_addrlen);
  1899. freeaddrinfo(res);
  1900. return;
  1901. }
  1902. /*
  1903. * get the local socket address from the server one
  1904. */
  1905. void
  1906. getlocal(void)
  1907. {
  1908. int ret, s;
  1909. socklen_t l;
  1910. if (ipversion == 4) {
  1911. l = sizeof(struct sockaddr_in);
  1912. s = socket(PF_INET, SOCK_DGRAM, 0);
  1913. } else {
  1914. l = sizeof(struct sockaddr_in6);
  1915. s = socket(PF_INET6, SOCK_DGRAM, 0);
  1916. }
  1917. if (s < 0) {
  1918. perror("socket");
  1919. exit(1);
  1920. }
  1921. if ((ipversion == 4) && (isbroadcast != 0)) {
  1922. int on = 1;
  1923. ret = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
  1924. if (ret < 0) {
  1925. perror("setsockopt(SO_BROADCAST)");
  1926. exit(1);
  1927. }
  1928. }
  1929. ret = connect(s, (struct sockaddr *) &serveraddr, l);
  1930. if (ret < 0) {
  1931. perror("connect");
  1932. exit(1);
  1933. }
  1934. l = sizeof(localaddr);
  1935. ret = getsockname(s, (struct sockaddr *) &localaddr, &l);
  1936. if (ret < 0) {
  1937. perror("getsockname");
  1938. exit(1);
  1939. }
  1940. (void) close(s);
  1941. /* fill the local port */
  1942. if (ipversion == 4)
  1943. ((struct sockaddr_in *) &localaddr)->sin_port = htons(67);
  1944. else
  1945. ((struct sockaddr_in6 *) &localaddr)->sin6_port = htons(546);
  1946. }
  1947. /*
  1948. * intermediate reporting
  1949. * (note: an in-transit packet can be reported as dropped)
  1950. */
  1951. void
  1952. reporting(void)
  1953. {
  1954. dreport.tv_sec += report;
  1955. if (xscount2 == 0) {
  1956. printf("sent: %llu, received: %llu (drops: %lld)",
  1957. (unsigned long long) xscount0,
  1958. (unsigned long long) xrcount0,
  1959. (long long) (xscount0 - xrcount0));
  1960. if (!ISC_TAILQ_EMPTY(&xrcvd0)) {
  1961. double avg;
  1962. avg = dsum0 / xrcount0;
  1963. printf(" average: %.3f ms", avg * 1e3);
  1964. }
  1965. } else {
  1966. printf("sent: %llu/%llu received: %llu/%llu "
  1967. "(drops: %lld/%lld)",
  1968. (unsigned long long) xscount0,
  1969. (unsigned long long) xscount2,
  1970. (unsigned long long) xrcount0,
  1971. (unsigned long long) xrcount2,
  1972. (long long) (xscount0 - xrcount0),
  1973. (long long) (xscount2 - xrcount2));
  1974. if (!ISC_TAILQ_EMPTY(&xrcvd0)) {
  1975. double avg0, avg2;
  1976. avg0 = dsum0 / xrcount0;
  1977. if (xrcount2 != 0)
  1978. avg2 = dsum2 / xrcount2;
  1979. else
  1980. avg2 = 0.;
  1981. printf(" average: %.3f/%.3f ms",
  1982. avg0 * 1e3, avg2 * 1e3);
  1983. }
  1984. }
  1985. printf("\n");
  1986. }
  1987. /*
  1988. * SIGCHLD handler
  1989. */
  1990. void
  1991. reapchild(int sig)
  1992. {
  1993. int status;
  1994. sig = sig;
  1995. while (wait3(&status, WNOHANG, NULL) > 0)
  1996. /* continue */;
  1997. }
  1998. /*
  1999. * SIGINT handler
  2000. */
  2001. void
  2002. interrupt(int sig)
  2003. {
  2004. sig = sig;
  2005. interrupted = 1;
  2006. }
  2007. /*
  2008. * '-v' handler
  2009. */
  2010. void
  2011. version(void)
  2012. {
  2013. fprintf(stderr, "version 0.01\n");
  2014. }
  2015. /*
  2016. * usage (from the wiki)
  2017. */
  2018. void
  2019. usage(void)
  2020. {
  2021. fprintf(stderr, "%s",
  2022. "perfdhcp [-hv] [-4|-6] [-r<rate>] [-t<report>] [-R<range>] [-b<base>]\n"
  2023. " [-n<num-request>] [-p<test-period>] [-d<drop-time>] [-D<max-drop>]\n"
  2024. " [-l<local-addr|interface>] [-P<preload>] [-a<aggressivity>]\n"
  2025. " [-L<local-port>] [-s<seed>] [-i] [-B] [-c] [-1]\n"
  2026. " [-T<template-file>] [-X<xid-offset>] [-O<random-offset]\n"
  2027. " [-E<time-offset>] [-S<srvid-offset>] [-I<ip-offset>]\n"
  2028. " [-x<diagnostic-selector>] [-w<wrapped>] [server]\n"
  2029. "\f\n"
  2030. "The [server] argument is the name/address of the DHCP server to\n"
  2031. "contact. For DHCPv4 operation, exchanges are initiated by\n"
  2032. "transmitting a DHCP DISCOVER to this address.\n"
  2033. "\n"
  2034. "For DHCPv6 operation, exchanges are initiated by transmitting a DHCP\n"
  2035. "SOLICIT to this address. In the DHCPv6 case, the special name 'all'\n"
  2036. "can be used to refer to All_DHCP_Relay_Agents_and_Servers (the\n"
  2037. "multicast address FF02::1:2), or the special name 'servers' to refer\n"
  2038. "to All_DHCP_Servers (the multicast address FF05::1:3). The [server]\n"
  2039. "argument is optional only in the case that -l is used to specify an\n"
  2040. "interface, in which case [server] defaults to 'all'.\n"
  2041. "\n"
  2042. "The default is to perform a single 4-way exchange, effectively pinging\n"
  2043. "the server.\n"
  2044. "The -r option is used to set up a performance test, without\n"
  2045. "it exchanges are initiated as fast as possible.\n"
  2046. "\n"
  2047. "Options:\n"
  2048. "-1: Take the server-ID option from the first received message.\n"
  2049. "-4: DHCPv4 operation (default). This is incompatible with the -6 option.\n"
  2050. "-6: DHCPv6 operation. This is incompatible with the -4 option.\n"
  2051. "-a<aggressivity>: When the target sending rate is not yet reached,\n"
  2052. " control how many exchanges are initiated before the next pause.\n"
  2053. "-b<base>: The base MAC, DUID, IP, etc, used to simulate different\n"
  2054. " clients. This can be specified multiple times, each instance is\n"
  2055. " in the <type>=<value> form, for instance:\n"
  2056. " (and default) MAC=00:0c:01:02:03:04.\n"
  2057. "-d<drop-time>: Specify the time after which a request is treated as\n"
  2058. " having been lost. The value is given in seconds and may contain a\n"
  2059. " fractional component. The default is 1 second.\n"
  2060. "-E<time-offset>: Offset of the (DHCPv4) secs field / (DHCPv6)\n"
  2061. " elapsed-time option in the (second/request) template.\n"
  2062. " The value 0 disables it.\n"
  2063. "-h: Print this help.\n"
  2064. "-i: Do only the initial part of an exchange: DO or SA, depending on\n"
  2065. " whether -6 is given.\n"
  2066. "-I<ip-offset>: Offset of the (DHCPv4) IP address in the requested-IP\n"
  2067. " option / (DHCPv6) IA_NA option in the (second/request) template.\n"
  2068. "-l<local-addr|interface>: For DHCPv4 operation, specify the local\n"
  2069. " hostname/address to use when communicating with the server. By\n"
  2070. " default, the interface address through which traffic would\n"
  2071. " normally be routed to the server is used.\n"
  2072. " For DHCPv6 operation, specify the name of the network interface\n"
  2073. " via which exchanges are initiated.\n"
  2074. "-L<local-port>: Specify the local port to use\n"
  2075. " (the value 0 means to use the default).\n"
  2076. "-O<random-offset>: Offset of the last octet to randomize in the template.\n"
  2077. "-P<preload>: Initiate first <preload> exchanges back to back at startup.\n"
  2078. "-r<rate>: Initiate <rate> DORA/SARR (or if -i is given, DO/SA)\n"
  2079. " exchanges per second. A periodic report is generated showing the\n"
  2080. " number of exchanges which were not completed, as well as the\n"
  2081. " average response latency. The program continues until\n"
  2082. " interrupted, at which point a final report is generated.\n"
  2083. "-R<range>: Specify how many different clients are used. With 1\n"
  2084. " (the default), all requests seem to come from the same client.\n"
  2085. "-s<seed>: Specify the seed for randomization, making it repeatable.\n"
  2086. "-S<srvid-offset>: Offset of the server-ID option in the\n"
  2087. " (second/request) template.\n"
  2088. "-T<template-file>: The name of a file containing the template to use\n"
  2089. " as a stream of hexadecimal digits.\n"
  2090. "-v: Report the version number of this program.\n"
  2091. "-w<wrapped>: Command to call with start/stop at the beginning/end of\n"
  2092. " the program.\n"
  2093. "-x<diagnostic-selector>: Include extended diagnostics in the output.\n"
  2094. " <diagnostic-selector> is a string of single-keywords specifying\n"
  2095. " the operations for which verbose output is desired. The selector\n"
  2096. " keyletters are:\n"
  2097. " * 'a': print the decoded command line arguments\n"
  2098. " * 'e': print the exit reason\n"
  2099. " * 'i': print rate processing details\n"
  2100. " * 'r': print randomization details\n"
  2101. " * 's': print first server-id\n"
  2102. " * 't': when finished, print timers of all successful exchanges\n"
  2103. " * 'T': when finished, print templates\n"
  2104. "-X<xid-offset>: Transaction ID (aka. xid) offset in the template.\n"
  2105. "\n"
  2106. "DHCPv4 only options:\n"
  2107. "-B: Force broadcast handling.\n"
  2108. "\n"
  2109. "DHCPv6 only options:\n"
  2110. "-c: Add a rapid commit option (exchanges will be SA).\n"
  2111. "\n"
  2112. "The remaining options are used only in conjunction with -r:\n"
  2113. "\n"
  2114. "-D<max-drop>: Abort the test if more than <max-drop> requests have\n"
  2115. " been dropped. Use -D0 to abort if even a single request has been\n"
  2116. " dropped. If <max-drop> includes the suffix '%', it specifies a\n"
  2117. " maximum percentage of requests that may be dropped before abort.\n"
  2118. " In this case, testing of the threshold begins after 10 requests\n"
  2119. " have been expected to be received.\n"
  2120. "-n<num-request>: Initiate <num-request> transactions. No report is\n"
  2121. " generated until all transactions have been initiated/waited-for,\n"
  2122. " after which a report is generated and the program terminates.\n"
  2123. "-p<test-period>: Send requests for the given test period, which is\n"
  2124. " specified in the same manner as -d. This can be used as an\n"
  2125. " alternative to -n, or both options can be given, in which case the\n"
  2126. " testing is completed when either limit is reached.\n"
  2127. "-t<report>: Delay in seconds between two periodic reports.\n"
  2128. "\n"
  2129. "Errors:\n"
  2130. "- tooshort: received a too short message\n"
  2131. "- orphans: received a message which doesn't match an exchange\n"
  2132. " (duplicate, late or not related)\n"
  2133. "- locallimit: reached to local system limits when sending a message.\n"
  2134. "\n"
  2135. "Exit status:\n"
  2136. "The exit status is:\n"
  2137. "0 on complete success.\n"
  2138. "1 for a general error.\n"
  2139. "2 if an error is found in the command line arguments.\n"
  2140. "3 if there are no general failures in operation, but one or more\n"
  2141. " exchanges are not successfully completed.\n");
  2142. }
  2143. /*
  2144. * main function / entry point
  2145. */
  2146. int
  2147. main(const int argc, char * const argv[])
  2148. {
  2149. int opt, flags = 0, ret, i;
  2150. long long r;
  2151. char *pc;
  2152. extern char *optarg;
  2153. extern int optind;
  2154. #define OPTIONS "hv46r:t:R:b:n:p:d:D:l:P:a:L:s:iBc1T:X:O:E:S:I:x:w:"
  2155. /* decode options */
  2156. while ((opt = getopt(argc, argv, OPTIONS)) != -1)
  2157. switch (opt) {
  2158. case 'h':
  2159. usage();
  2160. exit(0);
  2161. case 'v':
  2162. version();
  2163. exit(0);
  2164. case '4':
  2165. if (ipversion == 6) {
  2166. fprintf(stderr, "IP version already set to 6\n");
  2167. usage();
  2168. exit(2);
  2169. }
  2170. ipversion = 4;
  2171. break;
  2172. case '6':
  2173. if (ipversion == 4) {
  2174. fprintf(stderr, "IP version already set to 4\n");
  2175. usage();
  2176. exit(2);
  2177. }
  2178. ipversion = 6;
  2179. break;
  2180. case 'r':
  2181. rate = atoi(optarg);
  2182. if (rate <= 0) {
  2183. fprintf(stderr, "rate must be a positive integer\n");
  2184. usage();
  2185. exit(2);
  2186. }
  2187. break;
  2188. case 't':
  2189. report = atoi(optarg);
  2190. if (report <= 0) {
  2191. fprintf(stderr, "report must be a positive integer\n");
  2192. usage();
  2193. exit(2);
  2194. }
  2195. break;
  2196. case 'R':
  2197. r = atoll(optarg);
  2198. if (r < 0) {
  2199. fprintf(stderr,
  2200. "range must not be a negative integer\n");
  2201. usage();
  2202. exit(2);
  2203. }
  2204. range = (uint32_t) r;
  2205. if ((range != 0) && (range != UINT32_MAX)) {
  2206. uint32_t s = range + 1;
  2207. uint64_t b = UINT32_MAX + 1, m;
  2208. m = (b / s) * s;
  2209. if (m == b)
  2210. maxrandom = 0;
  2211. else
  2212. maxrandom = (uint32_t) m;
  2213. }
  2214. break;
  2215. case 'b':
  2216. if (basecnt > 3) {
  2217. fprintf(stderr, "too many bases\n");
  2218. usage();
  2219. exit(2);
  2220. }
  2221. base[basecnt] = optarg;
  2222. decodebase();
  2223. basecnt++;
  2224. break;
  2225. case 'n':
  2226. numreq[gotnumreq] = atoi(optarg);
  2227. if (numreq[gotnumreq] <= 0) {
  2228. fprintf(stderr,
  2229. "num-request must be a positive integer\n");
  2230. usage();
  2231. exit(2);
  2232. }
  2233. gotnumreq = 1;
  2234. break;
  2235. case 'p':
  2236. period = atoi(optarg);
  2237. if (period <= 0) {
  2238. fprintf(stderr,
  2239. "test-period must be a positive integer\n");
  2240. usage();
  2241. exit(2);
  2242. }
  2243. break;
  2244. case 'd':
  2245. losttime[gotlosttime] = atof(optarg);
  2246. if (losttime[gotlosttime] <= 0.) {
  2247. fprintf(stderr,
  2248. "drop-time must be a positive number\n");
  2249. usage();
  2250. exit(2);
  2251. }
  2252. gotlosttime = 1;
  2253. break;
  2254. case 'D':
  2255. pc = strchr(optarg, '%');
  2256. if (pc != NULL) {
  2257. *pc = '\0';
  2258. maxpdrop[gotmaxdrop] = atof(optarg);
  2259. if ((maxpdrop[gotmaxdrop] <= 0) ||
  2260. (maxpdrop[gotmaxdrop] >= 100)) {
  2261. fprintf(stderr,
  2262. "invalid drop-time percentage\n");
  2263. usage();
  2264. exit(2);
  2265. }
  2266. gotmaxdrop = 1;
  2267. break;
  2268. }
  2269. maxdrop[gotmaxdrop] = atoi(optarg);
  2270. if (maxdrop[gotmaxdrop] <= 0) {
  2271. fprintf(stderr,
  2272. "max-drop must be a positive integer\n");
  2273. usage();
  2274. exit(2);
  2275. }
  2276. gotmaxdrop = 1;
  2277. break;
  2278. case 'l':
  2279. localname = optarg;
  2280. break;
  2281. case 'P':
  2282. preload = atoi(optarg);
  2283. if (preload < 0) {
  2284. fprintf(stderr,
  2285. "preload must not be a negative integer\n");
  2286. usage();
  2287. exit(2);
  2288. }
  2289. break;
  2290. case 'a':
  2291. aggressivity = atoi(optarg);
  2292. if (aggressivity <= 0) {
  2293. fprintf(stderr,
  2294. "aggressivity must be a positive integer\n");
  2295. usage();
  2296. exit(2);
  2297. }
  2298. break;
  2299. case 'L':
  2300. localport = atoi(optarg);
  2301. if (localport < 0) {
  2302. fprintf(stderr,
  2303. "local-port must not be a negative integer\n");
  2304. usage();
  2305. exit(2);
  2306. }
  2307. if (localport > (int) UINT16_MAX) {
  2308. fprintf(stderr,
  2309. "local-port must be lower than %d\n",
  2310. (int) UINT16_MAX);
  2311. usage();
  2312. exit(2);
  2313. }
  2314. break;
  2315. case 's':
  2316. seeded = 1;
  2317. seed = (unsigned int) atol(optarg);
  2318. break;
  2319. case 'i':
  2320. simple = 1;
  2321. break;
  2322. case 'B':
  2323. isbroadcast = 1;
  2324. break;
  2325. case 'c':
  2326. rapidcommit = 1;
  2327. break;
  2328. case '1':
  2329. usefirst = 1;
  2330. break;
  2331. case 'T':
  2332. if (templatefile[0] != NULL) {
  2333. if (templatefile[1] != NULL) {
  2334. fprintf(stderr,
  2335. "template-files are already set\n");
  2336. usage();
  2337. exit(2);
  2338. }
  2339. templatefile[1] = optarg;
  2340. } else
  2341. templatefile[0] = optarg;
  2342. break;
  2343. case 'X':
  2344. if (xidoffset[0] >= 0)
  2345. i = 1;
  2346. else
  2347. i = 0;
  2348. xidoffset[i] = atoi(optarg);
  2349. if (xidoffset[i] <= 0) {
  2350. fprintf(stderr,
  2351. "xid-offset must be a positive integer\n");
  2352. usage();
  2353. exit(2);
  2354. }
  2355. break;
  2356. case 'O':
  2357. if (rndoffset[0] >= 0)
  2358. i = 1;
  2359. else
  2360. i = 0;
  2361. rndoffset[i] = atoi(optarg);
  2362. if (rndoffset[i] < 3) {
  2363. fprintf(stderr,
  2364. "random-offset must be greater than 3\n");
  2365. usage();
  2366. exit(2);
  2367. }
  2368. break;
  2369. case 'E':
  2370. elpoffset = atoi(optarg);
  2371. if (elpoffset < 0) {
  2372. fprintf(stderr,
  2373. "time-offset must not be a "
  2374. "negative integer\n");
  2375. usage();
  2376. exit(2);
  2377. }
  2378. break;
  2379. case 'S':
  2380. sidoffset = atoi(optarg);
  2381. if (sidoffset <= 0) {
  2382. fprintf(stderr,
  2383. "srvid-offset must be a positive integer\n");
  2384. usage();
  2385. exit(2);
  2386. }
  2387. break;
  2388. case 'I':
  2389. ripoffset = atoi(optarg);
  2390. if (ripoffset <= 0) {
  2391. fprintf(stderr,
  2392. "ip-offset must be a positive integer\n");
  2393. usage();
  2394. exit(2);
  2395. }
  2396. break;
  2397. case 'x':
  2398. diags = optarg;
  2399. break;
  2400. case 'w':
  2401. wrapped = optarg;
  2402. break;
  2403. default:
  2404. usage();
  2405. exit(2);
  2406. }
  2407. /* adjust some global variables */
  2408. if (ipversion == 0)
  2409. ipversion = 4;
  2410. if (templatefile[1] != NULL) {
  2411. if (xidoffset[1] < 0)
  2412. xidoffset[1] = xidoffset[0];
  2413. if (rndoffset[1] < 0)
  2414. rndoffset[1] = rndoffset[0];
  2415. }
  2416. /* when required, print the internal view of the command line */
  2417. if ((diags != NULL) && (strchr(diags, 'a') != NULL)) {
  2418. printf("IPv%d", ipversion);
  2419. if (simple != 0) {
  2420. if (ipversion == 4)
  2421. printf(" DO only");
  2422. else
  2423. printf(" SA only");
  2424. }
  2425. if (rate != 0)
  2426. printf(" rate=%d", rate);
  2427. if (report != 0)
  2428. printf(" report=%d", report);
  2429. if (range != 0) {
  2430. if (strchr(diags, 'r') != NULL)
  2431. printf(" range=0..%d [0x%x]",
  2432. range,
  2433. (unsigned int) maxrandom);
  2434. else
  2435. printf(" range=0..%d", range);
  2436. }
  2437. if (basecnt != 0)
  2438. for (i = 0; i < basecnt; i++)
  2439. printf(" base[%d]='%s'", i, base[i]);
  2440. if (gotnumreq != 0)
  2441. printf(" num-request=%d,%d", numreq[0], numreq[1]);
  2442. if (period != 0)
  2443. printf(" test-period=%d", period);
  2444. printf(" drop-time=%g,%g", losttime[0], losttime[1]);
  2445. if ((maxdrop[0] != 0) || (maxdrop[1] != 0))
  2446. printf(" max-drop=%d,%d", maxdrop[0], maxdrop[1]);
  2447. if ((maxpdrop[0] != 0.) || (maxpdrop[1] != 0.))
  2448. printf(" max-drop=%2.2f%%,%2.2f%%",
  2449. maxpdrop[0], maxpdrop[1]);
  2450. if (preload != 0)
  2451. printf(" preload=%d", preload);
  2452. printf(" aggressivity=%d", aggressivity);
  2453. if (localport != 0)
  2454. printf(" local-port=%d", localport);
  2455. if (seeded)
  2456. printf(" seed=%u", seed);
  2457. if (isbroadcast != 0)
  2458. printf(" broadcast");
  2459. if (rapidcommit != 0)
  2460. printf(" rapid-commit");
  2461. if (usefirst != 0)
  2462. printf(" use-first");
  2463. if ((templatefile[0] != NULL) && (templatefile[1] == NULL))
  2464. printf(" template-file='%s'", templatefile[0]);
  2465. else if (templatefile[1] != NULL)
  2466. printf(" template-file='%s','%s'",
  2467. templatefile[0], templatefile[1]);
  2468. if ((xidoffset[0] >= 0) && (xidoffset[1] < 0))
  2469. printf(" xid-offset=%d", xidoffset[0]);
  2470. else if (xidoffset[1] >= 0)
  2471. printf(" xid-offset=%d,%d",
  2472. xidoffset[0], xidoffset[1]);
  2473. if ((rndoffset[0] >= 0) && (rndoffset[1] < 0))
  2474. printf(" xid-offset=%d", rndoffset[0]);
  2475. else if (rndoffset[1] >= 0)
  2476. printf(" xid-offset=%d,%d",
  2477. rndoffset[0], rndoffset[1]);
  2478. if (elpoffset >= 0)
  2479. printf(" time-offset=%d", elpoffset);
  2480. if (sidoffset >= 0)
  2481. printf(" srvid-offset=%d", sidoffset);
  2482. if (ripoffset >= 0)
  2483. printf(" ip-offset=%d", ripoffset);
  2484. printf(" diagnotic-selectors='%s'", diags);
  2485. if (wrapped != NULL)
  2486. printf(" wrapped='%s'", wrapped);
  2487. printf("\n");
  2488. }
  2489. /* check DHCPv4 only options */
  2490. if ((ipversion != 4) && (isbroadcast != 0)) {
  2491. fprintf(stderr, "-b is not compatible with IPv6 (-6)\n");
  2492. usage();
  2493. exit(2);
  2494. }
  2495. /* check DHCPv6 only options */
  2496. if ((ipversion != 6) && (rapidcommit != 0)) {
  2497. fprintf(stderr, "-6 (IPv6) must be set to use -c\n");
  2498. usage();
  2499. exit(2);
  2500. }
  2501. /* check 4-packet (aka not simple) mode options */
  2502. if ((simple != 0) && (numreq[1] != 0)) {
  2503. fprintf(stderr,
  2504. "second -n<num-request> is not compatible with -i\n");
  2505. usage();
  2506. exit(2);
  2507. }
  2508. if ((simple != 0) && (losttime[1] != 1.)) {
  2509. fprintf(stderr,
  2510. "second -d<drop-time> is not compatible with -i\n");
  2511. usage();
  2512. exit(2);
  2513. }
  2514. if ((simple != 0) &&
  2515. ((maxdrop[1] != 0) || (maxpdrop[1] != 0.))) {
  2516. fprintf(stderr,
  2517. "second -D<max-drop> is not compatible with -i\n");
  2518. usage();
  2519. exit(2);
  2520. }
  2521. if ((simple != 0) && (usefirst != 0)) {
  2522. fprintf(stderr,
  2523. "-1 is not compatible with -i\n");
  2524. usage();
  2525. exit(2);
  2526. }
  2527. if ((simple != 0) && (templatefile[1] != NULL)) {
  2528. fprintf(stderr,
  2529. "second -T<template-file> is not "
  2530. "compatible with -i\n");
  2531. usage();
  2532. exit(2);
  2533. }
  2534. if ((simple != 0) && (xidoffset[1] >= 0)) {
  2535. fprintf(stderr,
  2536. "second -X<xid-offset> is not compatible with -i\n");
  2537. usage();
  2538. exit(2);
  2539. }
  2540. if ((simple != 0) && (rndoffset[1] >= 0)) {
  2541. fprintf(stderr,
  2542. "second -O<random-offset is not compatible with -i\n");
  2543. usage();
  2544. exit(2);
  2545. }
  2546. if ((simple != 0) && (elpoffset >= 0)) {
  2547. fprintf(stderr,
  2548. "-E<time-offset> is not compatible with -i\n");
  2549. usage();
  2550. exit(2);
  2551. }
  2552. if ((simple != 0) && (sidoffset >= 0)) {
  2553. fprintf(stderr,
  2554. "-S<srvid-offset> is not compatible with -i\n");
  2555. usage();
  2556. exit(2);
  2557. }
  2558. if ((simple != 0) && (ripoffset >= 0)) {
  2559. fprintf(stderr,
  2560. "-I<ip-offset> is not compatible with -i\n");
  2561. usage();
  2562. exit(2);
  2563. }
  2564. /* check simple mode options */
  2565. if ((simple == 0) && (rapidcommit != 0)) {
  2566. fprintf(stderr, "-i must be set to use -c\n");
  2567. usage();
  2568. exit(2);
  2569. }
  2570. /* check rate '-r' options */
  2571. if ((rate == 0) && (report != 0)) {
  2572. fprintf(stderr,
  2573. "-r<rate> must be set to use -t<report>\n");
  2574. usage();
  2575. exit(2);
  2576. }
  2577. if ((rate == 0) && ((numreq[0] != 0) || (numreq[1] != 0))) {
  2578. fprintf(stderr,
  2579. "-r<rate> must be set to use -n<num-request>\n");
  2580. usage();
  2581. exit(2);
  2582. }
  2583. if ((rate == 0) && (period != 0)) {
  2584. fprintf(stderr,
  2585. "-r<rate> must be set to use -p<test-period>\n");
  2586. usage();
  2587. exit(2);
  2588. }
  2589. if ((rate == 0) &&
  2590. ((maxdrop[0] != 0) || (maxdrop[1] != 0) ||
  2591. (maxpdrop[0] != 0.) || (maxpdrop[1] != 0.))) {
  2592. fprintf(stderr,
  2593. "-r<rate> must be set to use -D<max-drop>\n");
  2594. usage();
  2595. exit(2);
  2596. }
  2597. /* check (first) template file options */
  2598. if ((templatefile[0] == NULL) && (xidoffset[0] >= 0)) {
  2599. fprintf(stderr,
  2600. "-T<template-file> must be set to "
  2601. "use -X<xid-offset>\n");
  2602. usage();
  2603. exit(2);
  2604. }
  2605. if ((templatefile[0] == NULL) && (rndoffset[0] >= 0)) {
  2606. fprintf(stderr,
  2607. "-T<template-file> must be set to "
  2608. "use -O<random-offset>\n");
  2609. usage();
  2610. exit(2);
  2611. }
  2612. /* check (second) template file options */
  2613. if ((templatefile[1] == NULL) && (elpoffset >= 0)) {
  2614. fprintf(stderr,
  2615. "second/request -T<template-file> must be set to "
  2616. "use -E<time-offset>\n");
  2617. usage();
  2618. exit(2);
  2619. }
  2620. if ((templatefile[1] == NULL) && (sidoffset >= 0)) {
  2621. fprintf(stderr,
  2622. "second/request -T<template-file> must be set to "
  2623. "use -S<srvid-offset>\n");
  2624. usage();
  2625. exit(2);
  2626. }
  2627. if ((templatefile[1] == NULL) && (ripoffset >= 0)) {
  2628. fprintf(stderr,
  2629. "second/request -T<template-file> must be set to "
  2630. "use -I<ip-offset>\n");
  2631. usage();
  2632. exit(2);
  2633. }
  2634. /* check various template file(s) and other condition(s) options */
  2635. if ((templatefile[0] != NULL) && (range > 0) && (rndoffset[0] < 0)) {
  2636. fprintf(stderr,
  2637. "-O<random-offset> must be set when "
  2638. "-T<template-file> and -R<range> are used\n");
  2639. usage();
  2640. exit(2);
  2641. }
  2642. if ((templatefile[1] != NULL) && (sidoffset < 0)) {
  2643. fprintf(stderr,
  2644. "-S<srvid-offset> must be set when second "
  2645. "-T<template-file> is used\n");
  2646. usage();
  2647. exit(2);
  2648. }
  2649. if ((templatefile[1] != NULL) && (ripoffset < 0)) {
  2650. fprintf(stderr,
  2651. "-I<ip-offset> must be set when second "
  2652. "-T<template-file> is used\n");
  2653. usage();
  2654. exit(2);
  2655. }
  2656. /* get the server argument */
  2657. if (optind < argc - 1) {
  2658. fprintf(stderr, "extra arguments?\n");
  2659. usage();
  2660. exit(2);
  2661. }
  2662. if (optind == argc - 1) {
  2663. servername = argv[optind];
  2664. /* decode special cases */
  2665. if ((ipversion == 4) &&
  2666. (strcmp(servername, "all") == 0)) {
  2667. flags = AI_NUMERICHOST;
  2668. isbroadcast = 1;
  2669. servername = "255.255.255.255";
  2670. } else if ((ipversion == 6) &&
  2671. (strcmp(servername, "all") == 0)) {
  2672. flags = AI_NUMERICHOST;
  2673. servername = "FF02::1:2";
  2674. } else if ((ipversion == 6) &&
  2675. (strcmp(servername, "servers") == 0)) {
  2676. flags = AI_NUMERICHOST;
  2677. servername = "FF05::1:3";
  2678. }
  2679. }
  2680. /* handle the local '-l' address/interface */
  2681. if (localname != NULL) {
  2682. /* given */
  2683. getlocaladdr();
  2684. if ((diags != NULL) && (strchr(diags, 'a') != NULL)) {
  2685. if (isinterface)
  2686. printf("interface='%s'\n", localname);
  2687. else
  2688. printf("local-addr='%s'\n", localname);
  2689. }
  2690. /* get the not given server from it */
  2691. if (servername == NULL) {
  2692. if (isinterface && (ipversion == 4)) {
  2693. flags = AI_NUMERICHOST;
  2694. isbroadcast = 1;
  2695. servername = "255.255.255.255";
  2696. } else if (isinterface && (ipversion == 6)) {
  2697. flags = AI_NUMERICHOST;
  2698. servername = "FF02::1:2";
  2699. } else {
  2700. fprintf(stderr,
  2701. "without an interface "
  2702. "server is required\n");
  2703. usage();
  2704. exit(2);
  2705. }
  2706. }
  2707. } else if (servername == NULL) {
  2708. fprintf(stderr, "without an interface server is required\n");
  2709. usage();
  2710. exit(2);
  2711. }
  2712. /* get the server socket address */
  2713. getserveraddr(flags);
  2714. /* finish local/server socket address stuff and print it */
  2715. if ((diags != NULL) && (strchr(diags, 'a') != NULL))
  2716. printf("server='%s'\n", servername);
  2717. if (localname == NULL)
  2718. getlocal();
  2719. if ((diags != NULL) && (strchr(diags, 'a') != NULL)) {
  2720. char addr[NI_MAXHOST];
  2721. ret = getnameinfo((struct sockaddr *) &localaddr,
  2722. sizeof(localaddr),
  2723. addr,
  2724. NI_MAXHOST,
  2725. NULL,
  2726. 0,
  2727. NI_NUMERICHOST);
  2728. if (ret != 0) {
  2729. fprintf(stderr,
  2730. "can't get the local address: %s\n",
  2731. gai_strerror(ret));
  2732. exit(1);
  2733. }
  2734. printf("local address='%s'\n", addr);
  2735. }
  2736. /* initialize exchange structures */
  2737. inits();
  2738. /* get the socket descriptor and template(s) */
  2739. if (ipversion == 4) {
  2740. getsock4();
  2741. if (templatefile[0] == NULL)
  2742. build_template_discover4();
  2743. else
  2744. get_template_discover4();
  2745. if (simple == 0) {
  2746. if (templatefile[1] == NULL)
  2747. build_template_request4();
  2748. else
  2749. get_template_request4();
  2750. }
  2751. } else {
  2752. getsock6();
  2753. if (duid_prefix != NULL) {
  2754. if (templatefile[0] == NULL)
  2755. build_template_solicit6();
  2756. else
  2757. get_template_solicit6();
  2758. if (simple == 0) {
  2759. if (templatefile[1] == NULL)
  2760. build_template_request6();
  2761. else
  2762. get_template_request6();
  2763. }
  2764. }
  2765. }
  2766. /* sanity check */
  2767. if ((unsigned) sock > FD_SETSIZE) {
  2768. fprintf(stderr, "socket descriptor (%d) too large?!\n", sock);
  2769. exit(1);
  2770. }
  2771. /* make the socket descriptor not blocking */
  2772. flags = fcntl(sock, F_GETFL, 0);
  2773. if (flags < 0) {
  2774. perror("fcntl(F_GETFL)");
  2775. exit(1);
  2776. }
  2777. if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
  2778. perror("fcntl(F_SETFL)");
  2779. exit(1);
  2780. }
  2781. /* wrapped start */
  2782. if (wrapped != NULL) {
  2783. pid_t pid;
  2784. (void) signal(SIGCHLD, reapchild);
  2785. pid = fork();
  2786. if (pid < 0) {
  2787. perror("fork");
  2788. exit(1);
  2789. } else if (pid == 0)
  2790. (void) execlp(wrapped, "start", (char *) NULL);
  2791. }
  2792. /* boot is done! */
  2793. if (clock_gettime(CLOCK_REALTIME, &boot) < 0) {
  2794. perror("clock_gettime(boot)");
  2795. exit(1);
  2796. }
  2797. /* compute the next intermediate reporting date */
  2798. if (report != 0) {
  2799. dreport.tv_sec = boot.tv_sec + report;
  2800. dreport.tv_nsec = boot.tv_nsec;
  2801. }
  2802. /* compute the DUID (the current date is needed) */
  2803. if ((ipversion == 6) && (duid_prefix == NULL)) {
  2804. uint32_t curdate;
  2805. duid_length = 14;
  2806. duid_prefix = (uint8_t *) malloc(duid_length);
  2807. if (duid_prefix == NULL) {
  2808. perror("malloc(duid)");
  2809. exit(1);
  2810. }
  2811. duid_prefix[0] = DHCP6_DUID_LLT >> 8;
  2812. duid_prefix[1] = DHCP6_DUID_LLT;
  2813. duid_prefix[2] = DHCP_HTYPE_ETHER >> 8;
  2814. duid_prefix[3] = DHCP_HTYPE_ETHER;
  2815. curdate = htonl(boot.tv_sec - DHCP6_DUID_EPOCH);
  2816. memcpy(duid_prefix + 4, &curdate, 4);
  2817. memcpy(duid_prefix + 8, mac_prefix, 6);
  2818. /* the DUID is in template(s) */
  2819. if (templatefile[0] == NULL)
  2820. build_template_solicit6();
  2821. else
  2822. get_template_solicit6();
  2823. if (simple == 0) {
  2824. if (templatefile[1] == NULL)
  2825. build_template_request6();
  2826. else
  2827. get_template_request6();
  2828. }
  2829. }
  2830. /* seed the random generator */
  2831. if (seeded == 0)
  2832. seed = (unsigned int) (boot.tv_sec + boot.tv_nsec);
  2833. srandom(seed);
  2834. /* preload the server with at least one packet */
  2835. compsend = preload + 1;
  2836. for (i = 0; i <= preload; i++) {
  2837. if (ipversion == 4)
  2838. ret = send4();
  2839. else
  2840. ret = send6();
  2841. if (ret < 0) {
  2842. /* failure at the first packet is fatal */
  2843. if (i == 0) {
  2844. fprintf(stderr,
  2845. "initial send failed: %s\n",
  2846. strerror(-ret));
  2847. exit(1);
  2848. }
  2849. if ((errno == EAGAIN) ||
  2850. (errno == EWOULDBLOCK) ||
  2851. (errno == ENOBUFS) ||
  2852. (errno == ENOMEM))
  2853. locallimit++;
  2854. fprintf(stderr, "preload send: %s\n", strerror(-ret));
  2855. break;
  2856. }
  2857. }
  2858. /* required only before the interrupted flag check */
  2859. (void) signal(SIGINT, interrupt);
  2860. /* main loop */
  2861. for (;;) {
  2862. struct timespec now, ts;
  2863. fd_set rfds;
  2864. /* immediate loop exit conditions */
  2865. if (interrupted) {
  2866. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2867. printf("interrupted\n");
  2868. break;
  2869. }
  2870. if (fatal) {
  2871. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2872. printf("got a fatal error\n");
  2873. break;
  2874. }
  2875. /* get the date and use it */
  2876. if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
  2877. perror("clock_gettime(now)");
  2878. fatal = 1;
  2879. continue;
  2880. }
  2881. if ((period != 0) &&
  2882. ((boot.tv_sec + period < now.tv_sec) ||
  2883. ((boot.tv_sec + period == now.tv_sec) &&
  2884. (boot.tv_nsec < now.tv_nsec)))) {
  2885. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2886. printf("reached test-period\n");
  2887. break;
  2888. }
  2889. if ((report != 0) &&
  2890. ((dreport.tv_sec < now.tv_sec) ||
  2891. ((dreport.tv_sec == now.tv_sec) &&
  2892. (dreport.tv_nsec < now.tv_nsec))))
  2893. reporting();
  2894. /* compute the delay for the next send */
  2895. due = last;
  2896. if (rate == 1)
  2897. due.tv_sec += 1;
  2898. else if (rate != 0)
  2899. due.tv_nsec += 1010000000 / rate;
  2900. else
  2901. due.tv_nsec += 1;
  2902. while (due.tv_nsec >= 1000000000) {
  2903. due.tv_sec += 1;
  2904. due.tv_nsec -= 1000000000;
  2905. }
  2906. ts = due;
  2907. ts.tv_sec -= now.tv_sec;
  2908. ts.tv_nsec -= now.tv_nsec;
  2909. while (ts.tv_nsec < 0) {
  2910. ts.tv_sec -= 1;
  2911. ts.tv_nsec += 1000000000;
  2912. }
  2913. /* the send was already due? */
  2914. if (ts.tv_sec < 0) {
  2915. ts.tv_sec = ts.tv_nsec = 0;
  2916. latesent++;
  2917. }
  2918. /* pselect() */
  2919. FD_ZERO(&rfds);
  2920. FD_SET(sock, &rfds);
  2921. ret = pselect(sock + 1, &rfds, NULL, NULL, &ts, NULL);
  2922. if (ret < 0) {
  2923. if (errno == EINTR)
  2924. continue;
  2925. perror("pselect");
  2926. fatal = 1;
  2927. continue;
  2928. }
  2929. /* packet(s) to receive */
  2930. while (ret == 1) {
  2931. if (ipversion == 4)
  2932. receive4();
  2933. else
  2934. receive6();
  2935. if (recv(sock, ibuf, sizeof(ibuf), MSG_PEEK) <= 0)
  2936. ret = 0;
  2937. else
  2938. multrcvd++;
  2939. }
  2940. if (fatal)
  2941. continue;
  2942. /* check receive loop exit conditions */
  2943. if ((numreq[0] != 0) && ((int) xscount0 >= numreq[0])) {
  2944. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2945. printf("reached num-request0\n");
  2946. break;
  2947. }
  2948. if ((numreq[1] != 0) && ((int) xscount2 >= numreq[1])) {
  2949. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2950. printf("reached num-request2\n");
  2951. break;
  2952. }
  2953. if ((maxdrop[0] != 0) &&
  2954. ((int) (xscount0 - xrcount0) > maxdrop[0])) {
  2955. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2956. printf("reached max-drop%s (absolute)\n",
  2957. simple != 0 ? "" : "0");
  2958. break;
  2959. }
  2960. if ((maxdrop[1] != 0) &&
  2961. ((int) (xscount2 - xrcount2) > maxdrop[1])) {
  2962. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2963. printf("reached max-drop2 (absolute)\n");
  2964. break;
  2965. }
  2966. if ((maxpdrop[0] != 0.) &&
  2967. (xscount0 > 10) &&
  2968. (((100. * (xscount0 - xrcount0)) / xscount0)
  2969. > maxpdrop[0])) {
  2970. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2971. printf("reached max-drop%s (percent)\n",
  2972. simple != 0 ? "" : "0");
  2973. break;
  2974. }
  2975. if ((maxpdrop[1] != 0.) &&
  2976. (xscount2 > 10) &&
  2977. (((100. * (xscount2 - xrcount2)) / xscount2)
  2978. > maxpdrop[1])) {
  2979. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2980. printf("reached max-drop2 (percent)\n");
  2981. break;
  2982. }
  2983. /* compute how many packets to send */
  2984. if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
  2985. perror("clock_gettime(now2)");
  2986. fatal = 1;
  2987. continue;
  2988. }
  2989. if ((now.tv_sec > due.tv_sec) ||
  2990. ((now.tv_sec == due.tv_sec) &&
  2991. (now.tv_nsec >= due.tv_nsec))) {
  2992. double tosend;
  2993. if (rate != 0) {
  2994. tosend = (now.tv_nsec - due.tv_nsec) / 1e9;
  2995. tosend += now.tv_sec - due.tv_sec;
  2996. tosend *= rate;
  2997. tosend += 1;
  2998. if (tosend > (double) aggressivity)
  2999. i = aggressivity;
  3000. else
  3001. i = (int) tosend;
  3002. } else
  3003. i = aggressivity;
  3004. compsend += i;
  3005. /* send packets */
  3006. for (;;) {
  3007. if (ipversion == 4)
  3008. ret = send4();
  3009. else
  3010. ret = send6();
  3011. if (ret < 0) {
  3012. if ((errno == EAGAIN) ||
  3013. (errno == EWOULDBLOCK) ||
  3014. (errno == ENOBUFS) ||
  3015. (errno == ENOMEM))
  3016. locallimit++;
  3017. fprintf(stderr,
  3018. "send: %s\n", strerror(-ret));
  3019. break;
  3020. }
  3021. i--;
  3022. if (i == 0)
  3023. break;
  3024. /* check for late packets to receive */
  3025. if (recv(sock, ibuf, sizeof(ibuf),
  3026. MSG_PEEK) > 0) {
  3027. latercvd++;
  3028. if (ipversion == 4)
  3029. receive4();
  3030. else
  3031. receive6();
  3032. }
  3033. }
  3034. } else
  3035. /* there was no packet to send */
  3036. shortwait++;
  3037. }
  3038. /* after main loop: finished */
  3039. if (clock_gettime(CLOCK_REALTIME, &finished) < 0)
  3040. perror("clock_gettime(finished)");
  3041. /* wrapped stop */
  3042. if (wrapped != NULL) {
  3043. pid_t pid;
  3044. pid = fork();
  3045. if (pid == 0)
  3046. (void) execlp(wrapped, "stop", (char *) NULL);
  3047. }
  3048. /* main statictics */
  3049. if (xscount2 == 0)
  3050. printf("sent: %llu, received: %llu (drops: %lld)\n",
  3051. (unsigned long long) xscount0,
  3052. (unsigned long long) xrcount0,
  3053. (long long) (xscount0 - xrcount0));
  3054. else
  3055. printf("sent: %llu/%llu, received: %llu/%llu "
  3056. "(drops: %lld/%lld)\n",
  3057. (unsigned long long) xscount0,
  3058. (unsigned long long) xscount2,
  3059. (unsigned long long) xrcount0,
  3060. (unsigned long long) xrcount2,
  3061. (long long) (xscount0 - xrcount0),
  3062. (long long) (xscount2 - xrcount2));
  3063. printf("tooshort: %llu, orphans: %llu, local limits: %llu\n",
  3064. (unsigned long long) tooshort,
  3065. (unsigned long long) orphans,
  3066. (unsigned long long) locallimit);
  3067. /* print the rate */
  3068. if (finished.tv_sec != 0) {
  3069. double dall, erate;
  3070. dall = (finished.tv_nsec - boot.tv_nsec) / 1e9;
  3071. dall += finished.tv_sec - boot.tv_sec;
  3072. erate = xrcount0 / dall;
  3073. if (rate != 0)
  3074. printf("rate: %f (expected %d)\n", erate, rate);
  3075. else
  3076. printf("rate: %f\n", erate);
  3077. }
  3078. /* rate processing instrumentation */
  3079. if ((diags != NULL) && (strchr(diags, 'i') != NULL)) {
  3080. printf("latesent: %llu, compsend: %llu, shortwait: %llu\n"
  3081. "multrcvd: %llu, latercvd: %llu, collected:%llu/%llu\n",
  3082. (unsigned long long) latesent,
  3083. (unsigned long long) compsend,
  3084. (unsigned long long) shortwait,
  3085. (unsigned long long) multrcvd,
  3086. (unsigned long long) latercvd,
  3087. (unsigned long long) collected[0],
  3088. (unsigned long long) collected[1]);
  3089. }
  3090. /* round-time trip statistics */
  3091. if (xrcount2 != 0) {
  3092. double avg0, avg2, stddev0, stddev2;
  3093. avg0 = dsum0 / xrcount0;
  3094. avg2 = dsum2 / xrcount2;
  3095. stddev0 = sqrt(dsumsq0 / xrcount0 - avg0 * avg0);
  3096. stddev2 = sqrt(dsumsq2 / xrcount2 - avg2 * avg2);
  3097. printf("RTT0: min/avg/max/stddev: %.3f/%.3f/%.3f/%.3f ms\n",
  3098. dmin0 * 1e3, avg0 * 1e3, dmax0 * 1e3, stddev0 * 1e3);
  3099. printf("RTT2: min/avg/max/stddev: %.3f/%.3f/%.3f/%.3f ms\n",
  3100. dmin2 * 1e3, avg2 * 1e3, dmax2 * 1e3, stddev2 * 1e3);
  3101. } else if (xrcount0 != 0) {
  3102. double avg, stddev;
  3103. avg = dsum0 / xrcount0;
  3104. stddev = sqrt(dsumsq0 / xrcount0 - avg * avg);
  3105. printf("RTT%s: min/avg/max/stddev: %.3f/%.3f/%.3f/%.3f ms\n",
  3106. simple != 0 ? "" : "0",
  3107. dmin0 * 1e3, avg * 1e3, dmax0 * 1e3, stddev * 1e3);
  3108. }
  3109. /* (first) server-ID option content */
  3110. if ((diags != NULL) && (strchr(diags, 's') != NULL) &&
  3111. !ISC_TAILQ_EMPTY(&xrcvd0)) {
  3112. struct exchange *x;
  3113. size_t n;
  3114. printf("server-id: ");
  3115. x = ISC_TAILQ_FIRST(&xrcvd0);
  3116. if (ipversion == 4)
  3117. n = 2;
  3118. else
  3119. n = 4;
  3120. for (; n < x->sidlen; n++)
  3121. printf("%02hhx", x->sid[n]);
  3122. printf("\n");
  3123. }
  3124. /* all time-stamps */
  3125. if ((diags != NULL) && (strchr(diags, 't') != NULL) &&
  3126. !ISC_TAILQ_EMPTY(&xrcvd0)) {
  3127. struct exchange *x;
  3128. printf("\n\n");
  3129. ISC_TAILQ_FOREACH(x, &xrcvd0, gchain)
  3130. printf("%ld.%09ld %ld.%09ld\n",
  3131. (long) x->ts0.tv_sec, x->ts0.tv_nsec,
  3132. (long) x->ts1.tv_sec, x->ts1.tv_nsec);
  3133. }
  3134. if ((diags != NULL) && (strchr(diags, 't') != NULL) &&
  3135. !ISC_TAILQ_EMPTY(&xrcvd2)) {
  3136. struct exchange *x;
  3137. printf("--\n");
  3138. ISC_TAILQ_FOREACH(x, &xrcvd2, gchain)
  3139. printf("%ld.%09ld %ld.%09ld %ld.%09ld %ld.%09ld\n",
  3140. (long) x->ts0.tv_sec, x->ts0.tv_nsec,
  3141. (long) x->ts1.tv_sec, x->ts1.tv_nsec,
  3142. (long) x->ts2.tv_sec, x->ts2.tv_nsec,
  3143. (long) x->ts3.tv_sec, x->ts3.tv_nsec);
  3144. }
  3145. if ((diags != NULL) && (strchr(diags, 't') != NULL) &&
  3146. !ISC_TAILQ_EMPTY(&xrcvd0))
  3147. printf("\n\n");
  3148. /* template(s) */
  3149. if ((diags != NULL) && (strchr(diags, 'T') != NULL)) {
  3150. size_t n;
  3151. if (ipversion == 4) {
  3152. printf("length = %zu\n", length_discover4);
  3153. printf("xid offset = %d\n", DHCP_OFF_XID);
  3154. printf("xid length = 4\n");
  3155. printf("random offset = %zu\n", random_discover4);
  3156. printf("content:\n");
  3157. for (n = 0; n < length_discover4; n++) {
  3158. printf("%s%02hhx",
  3159. (n & 15) == 0 ? "" : " ",
  3160. template_discover4[n]);
  3161. if ((n & 15) == 15)
  3162. printf("\n");
  3163. }
  3164. if ((n & 15) != 15)
  3165. printf("\n");
  3166. if (simple != 0)
  3167. goto doneT;
  3168. printf("--\n");
  3169. printf("length = %zu\n", length_request4);
  3170. printf("xid offset = %d\n", DHCP_OFF_XID);
  3171. printf("xid length = 4\n");
  3172. printf("random offset = %zu\n", random_request4);
  3173. if (elapsed_request4 > 0)
  3174. printf("secs offset = %zu\n",
  3175. elapsed_request4);
  3176. printf("server-id offset = %zu\n", serverid_request4);
  3177. printf("server-id length = %d\n", DHCP_OPTLEN_SRVID);
  3178. printf("content:\n");
  3179. printf("requested-ip-address offset = %zu\n",
  3180. reqaddr_request4);
  3181. printf("requested-ip-address length = %d\n", 4);
  3182. for (n = 0; n < length_request4; n++) {
  3183. printf("%s%02hhx",
  3184. (n & 15) == 0 ? "" : " ",
  3185. template_request4[n]);
  3186. if ((n & 15) == 15)
  3187. printf("\n");
  3188. }
  3189. printf("\n");
  3190. } else {
  3191. printf("length = %zu\n", length_solicit6);
  3192. printf("xid offset = %d\n", DHCP6_OFF_XID);
  3193. printf("xid length = 3\n");
  3194. printf("random offset = %zu\n", random_solicit6);
  3195. for (n = 0; n < length_solicit6; n++) {
  3196. printf("%s%02hhx",
  3197. (n & 15) == 0 ? "" : " ",
  3198. template_solicit6[n]);
  3199. if ((n & 15) == 15)
  3200. printf("\n");
  3201. }
  3202. if ((n & 15) != 15)
  3203. printf("\n");
  3204. if (simple != 0)
  3205. goto doneT;
  3206. printf("--\n");
  3207. printf("length = %zu\n", length_request6);
  3208. printf("xid offset = %d\n", DHCP_OFF_XID);
  3209. printf("xid length = 4\n");
  3210. printf("random offset = %zu\n", random_request6);
  3211. if (elapsed_request6 > 0)
  3212. printf("secs offset = %zu\n",
  3213. elapsed_request6);
  3214. printf("server-id offset = %zu\n", serverid_request6);
  3215. printf("content:\n");
  3216. printf("requested-ip-address offset = %zu\n",
  3217. reqaddr_request6);
  3218. for (n = 0; n < length_request6; n++) {
  3219. printf("%s%02hhx",
  3220. (n & 15) == 0 ? "" : " ",
  3221. template_request6[n]);
  3222. if ((n & 15) == 15)
  3223. printf("\n");
  3224. }
  3225. printf("\n");
  3226. }
  3227. }
  3228. doneT:
  3229. /* compute the exit code (and exit) */
  3230. if (fatal)
  3231. exit(1);
  3232. else if ((xscount0 == xrcount0) && (xscount2 == xrcount2))
  3233. exit(0);
  3234. else
  3235. exit(3);
  3236. }
  3237. #endif /* HAVE_GETIFADDRS */