pcap.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955
  1. /*
  2. * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the Computer Systems
  16. * Engineering Group at Lawrence Berkeley Laboratory.
  17. * 4. Neither the name of the University nor of the Laboratory may be used
  18. * to endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #ifdef HAVE_CONFIG_H
  34. #include <config.h>
  35. #endif
  36. #include <pcap-types.h>
  37. #ifndef _WIN32
  38. #include <sys/param.h>
  39. #ifndef MSDOS
  40. #include <sys/file.h>
  41. #endif
  42. #include <sys/ioctl.h>
  43. #include <sys/socket.h>
  44. #ifdef HAVE_SYS_SOCKIO_H
  45. #include <sys/sockio.h>
  46. #endif
  47. struct mbuf; /* Squelch compiler warnings on some platforms for */
  48. struct rtentry; /* declarations in <net/if.h> */
  49. #include <net/if.h>
  50. #include <netinet/in.h>
  51. #endif /* _WIN32 */
  52. #include <ctype.h>
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__MINGW32__)
  57. #include <unistd.h>
  58. #endif
  59. #include <fcntl.h>
  60. #include <errno.h>
  61. #ifdef HAVE_LIMITS_H
  62. #include <limits.h>
  63. #else
  64. #define INT_MAX 2147483647
  65. #endif
  66. #ifdef HAVE_OS_PROTO_H
  67. #include "os-proto.h"
  68. #endif
  69. #ifdef MSDOS
  70. #include "pcap-dos.h"
  71. #endif
  72. #include "pcap-int.h"
  73. #include "optimize.h"
  74. #ifdef HAVE_DAG_API
  75. #include "pcap-dag.h"
  76. #endif /* HAVE_DAG_API */
  77. #ifdef HAVE_SEPTEL_API
  78. #include "pcap-septel.h"
  79. #endif /* HAVE_SEPTEL_API */
  80. #ifdef HAVE_SNF_API
  81. #include "pcap-snf.h"
  82. #endif /* HAVE_SNF_API */
  83. #ifdef HAVE_TC_API
  84. #include "pcap-tc.h"
  85. #endif /* HAVE_TC_API */
  86. #ifdef PCAP_SUPPORT_USB
  87. #include "pcap-usb-linux.h"
  88. #endif
  89. #ifdef PCAP_SUPPORT_BT
  90. #include "pcap-bt-linux.h"
  91. #endif
  92. #ifdef PCAP_SUPPORT_BT_MONITOR
  93. #include "pcap-bt-monitor-linux.h"
  94. #endif
  95. #ifdef PCAP_SUPPORT_NETFILTER
  96. #include "pcap-netfilter-linux.h"
  97. #endif
  98. #ifdef PCAP_SUPPORT_NETMAP
  99. #include "pcap-netmap.h"
  100. #endif
  101. #ifdef PCAP_SUPPORT_DBUS
  102. #include "pcap-dbus.h"
  103. #endif
  104. #ifdef PCAP_SUPPORT_RDMASNIFF
  105. #include "pcap-rdmasniff.h"
  106. #endif
  107. #ifdef _WIN32
  108. /*
  109. * DllMain(), required when built as a Windows DLL.
  110. */
  111. BOOL WINAPI DllMain(
  112. HANDLE hinstDLL,
  113. DWORD dwReason,
  114. LPVOID lpvReserved
  115. )
  116. {
  117. return (TRUE);
  118. }
  119. /*
  120. * Start WinSock.
  121. * Exported in case some applications using WinPcap called it,
  122. * even though it wasn't exported.
  123. */
  124. int
  125. wsockinit(void)
  126. {
  127. WORD wVersionRequested;
  128. WSADATA wsaData;
  129. static int err = -1;
  130. static int done = 0;
  131. if (done)
  132. return (err);
  133. wVersionRequested = MAKEWORD( 1, 1);
  134. err = WSAStartup( wVersionRequested, &wsaData );
  135. atexit ((void(*)(void))WSACleanup);
  136. done = 1;
  137. if ( err != 0 )
  138. err = -1;
  139. return (err);
  140. }
  141. /*
  142. * This is the exported function; new programs should call this.
  143. */
  144. int
  145. pcap_wsockinit(void)
  146. {
  147. return (wsockinit());
  148. }
  149. #endif /* _WIN32 */
  150. /*
  151. * String containing the library version.
  152. * Not explicitly exported via a header file - the right API to use
  153. * is pcap_lib_version() - but some programs included it, so we
  154. * provide it.
  155. *
  156. * We declare it here, right before defining it, to squelch any
  157. * warnings we might get from compilers about the lack of a
  158. * declaration.
  159. */
  160. PCAP_API char pcap_version[];
  161. PCAP_API_DEF char pcap_version[] = PACKAGE_VERSION;
  162. static int
  163. pcap_not_initialized(pcap_t *pcap)
  164. {
  165. if (pcap->activated) {
  166. /* A module probably forgot to set the function pointer */
  167. (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
  168. "This operation isn't properly handled by that device");
  169. return (PCAP_ERROR);
  170. }
  171. /* in case the caller doesn't check for PCAP_ERROR_NOT_ACTIVATED */
  172. (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
  173. "This handle hasn't been activated yet");
  174. /* this means 'not initialized' */
  175. return (PCAP_ERROR_NOT_ACTIVATED);
  176. }
  177. #ifdef _WIN32
  178. static void *
  179. pcap_not_initialized_ptr(pcap_t *pcap)
  180. {
  181. if (pcap->activated) {
  182. /* A module probably forgot to set the function pointer */
  183. (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
  184. "This operation isn't properly handled by that device");
  185. return (NULL);
  186. }
  187. (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
  188. "This handle hasn't been activated yet");
  189. return (NULL);
  190. }
  191. static HANDLE
  192. pcap_getevent_not_initialized(pcap_t *pcap)
  193. {
  194. if (pcap->activated) {
  195. /* A module probably forgot to set the function pointer */
  196. (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
  197. "This operation isn't properly handled by that device");
  198. return (INVALID_HANDLE_VALUE);
  199. }
  200. (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
  201. "This handle hasn't been activated yet");
  202. return (INVALID_HANDLE_VALUE);
  203. }
  204. static u_int
  205. pcap_sendqueue_transmit_not_initialized(pcap_t *pcap, pcap_send_queue* queue, int sync)
  206. {
  207. if (pcap->activated) {
  208. /* A module probably forgot to set the function pointer */
  209. (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
  210. "This operation isn't properly handled by that device");
  211. return (0);
  212. }
  213. (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
  214. "This handle hasn't been activated yet");
  215. return (0);
  216. }
  217. static PAirpcapHandle
  218. pcap_get_airpcap_handle_not_initialized(pcap_t *pcap)
  219. {
  220. if (pcap->activated) {
  221. /* A module probably forgot to set the function pointer */
  222. (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
  223. "This operation isn't properly handled by that device");
  224. return (NULL);
  225. }
  226. (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
  227. "This handle hasn't been activated yet");
  228. return (NULL);
  229. }
  230. #endif
  231. /*
  232. * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't,
  233. * a PCAP_ERROR value on an error.
  234. */
  235. int
  236. pcap_can_set_rfmon(pcap_t *p)
  237. {
  238. return (p->can_set_rfmon_op(p));
  239. }
  240. /*
  241. * For systems where rfmon mode is never supported.
  242. */
  243. static int
  244. pcap_cant_set_rfmon(pcap_t *p _U_)
  245. {
  246. return (0);
  247. }
  248. /*
  249. * Sets *tstamp_typesp to point to an array 1 or more supported time stamp
  250. * types; the return value is the number of supported time stamp types.
  251. * The list should be freed by a call to pcap_free_tstamp_types() when
  252. * you're done with it.
  253. *
  254. * A return value of 0 means "you don't get a choice of time stamp type",
  255. * in which case *tstamp_typesp is set to null.
  256. *
  257. * PCAP_ERROR is returned on error.
  258. */
  259. int
  260. pcap_list_tstamp_types(pcap_t *p, int **tstamp_typesp)
  261. {
  262. if (p->tstamp_type_count == 0) {
  263. /*
  264. * We don't support multiple time stamp types.
  265. */
  266. *tstamp_typesp = NULL;
  267. } else {
  268. *tstamp_typesp = (int*)calloc(sizeof(**tstamp_typesp),
  269. p->tstamp_type_count);
  270. if (*tstamp_typesp == NULL) {
  271. pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
  272. errno, "malloc");
  273. return (PCAP_ERROR);
  274. }
  275. (void)memcpy(*tstamp_typesp, p->tstamp_type_list,
  276. sizeof(**tstamp_typesp) * p->tstamp_type_count);
  277. }
  278. return (p->tstamp_type_count);
  279. }
  280. /*
  281. * In Windows, you might have a library built with one version of the
  282. * C runtime library and an application built with another version of
  283. * the C runtime library, which means that the library might use one
  284. * version of malloc() and free() and the application might use another
  285. * version of malloc() and free(). If so, that means something
  286. * allocated by the library cannot be freed by the application, so we
  287. * need to have a pcap_free_tstamp_types() routine to free up the list
  288. * allocated by pcap_list_tstamp_types(), even though it's just a wrapper
  289. * around free().
  290. */
  291. void
  292. pcap_free_tstamp_types(int *tstamp_type_list)
  293. {
  294. free(tstamp_type_list);
  295. }
  296. /*
  297. * Default one-shot callback; overridden for capture types where the
  298. * packet data cannot be guaranteed to be available after the callback
  299. * returns, so that a copy must be made.
  300. */
  301. void
  302. pcap_oneshot(u_char *user, const struct pcap_pkthdr *h, const u_char *pkt)
  303. {
  304. struct oneshot_userdata *sp = (struct oneshot_userdata *)user;
  305. *sp->hdr = *h;
  306. *sp->pkt = pkt;
  307. }
  308. const u_char *
  309. pcap_next(pcap_t *p, struct pcap_pkthdr *h)
  310. {
  311. struct oneshot_userdata s;
  312. const u_char *pkt;
  313. s.hdr = h;
  314. s.pkt = &pkt;
  315. s.pd = p;
  316. if (pcap_dispatch(p, 1, p->oneshot_callback, (u_char *)&s) <= 0)
  317. return (0);
  318. return (pkt);
  319. }
  320. int
  321. pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
  322. const u_char **pkt_data)
  323. {
  324. struct oneshot_userdata s;
  325. s.hdr = &p->pcap_header;
  326. s.pkt = pkt_data;
  327. s.pd = p;
  328. /* Saves a pointer to the packet headers */
  329. *pkt_header= &p->pcap_header;
  330. if (p->rfile != NULL) {
  331. int status;
  332. /* We are on an offline capture */
  333. status = pcap_offline_read(p, 1, p->oneshot_callback,
  334. (u_char *)&s);
  335. /*
  336. * Return codes for pcap_offline_read() are:
  337. * - 0: EOF
  338. * - -1: error
  339. * - >1: OK
  340. * The first one ('0') conflicts with the return code of
  341. * 0 from pcap_read() meaning "no packets arrived before
  342. * the timeout expired", so we map it to -2 so you can
  343. * distinguish between an EOF from a savefile and a
  344. * "no packets arrived before the timeout expired, try
  345. * again" from a live capture.
  346. */
  347. if (status == 0)
  348. return (-2);
  349. else
  350. return (status);
  351. }
  352. /*
  353. * Return codes for pcap_read() are:
  354. * - 0: timeout
  355. * - -1: error
  356. * - -2: loop was broken out of with pcap_breakloop()
  357. * - >1: OK
  358. * The first one ('0') conflicts with the return code of 0 from
  359. * pcap_offline_read() meaning "end of file".
  360. */
  361. return (p->read_op(p, 1, p->oneshot_callback, (u_char *)&s));
  362. }
  363. /*
  364. * Implementation of a pcap_if_list_t.
  365. */
  366. struct pcap_if_list {
  367. pcap_if_t *beginning;
  368. };
  369. static struct capture_source_type {
  370. int (*findalldevs_op)(pcap_if_list_t *, char *);
  371. pcap_t *(*create_op)(const char *, char *, int *);
  372. } capture_source_types[] = {
  373. #ifdef HAVE_DAG_API
  374. { dag_findalldevs, dag_create },
  375. #endif
  376. #ifdef HAVE_SEPTEL_API
  377. { septel_findalldevs, septel_create },
  378. #endif
  379. #ifdef HAVE_SNF_API
  380. { snf_findalldevs, snf_create },
  381. #endif
  382. #ifdef HAVE_TC_API
  383. { TcFindAllDevs, TcCreate },
  384. #endif
  385. #ifdef PCAP_SUPPORT_BT
  386. { bt_findalldevs, bt_create },
  387. #endif
  388. #ifdef PCAP_SUPPORT_BT_MONITOR
  389. { bt_monitor_findalldevs, bt_monitor_create },
  390. #endif
  391. #ifdef PCAP_SUPPORT_USB
  392. { usb_findalldevs, usb_create },
  393. #endif
  394. #ifdef PCAP_SUPPORT_NETFILTER
  395. { netfilter_findalldevs, netfilter_create },
  396. #endif
  397. #ifdef PCAP_SUPPORT_NETMAP
  398. { pcap_netmap_findalldevs, pcap_netmap_create },
  399. #endif
  400. #ifdef PCAP_SUPPORT_DBUS
  401. { dbus_findalldevs, dbus_create },
  402. #endif
  403. #ifdef PCAP_SUPPORT_RDMASNIFF
  404. { rdmasniff_findalldevs, rdmasniff_create },
  405. #endif
  406. { NULL, NULL }
  407. };
  408. /*
  409. * Get a list of all capture sources that are up and that we can open.
  410. * Returns -1 on error, 0 otherwise.
  411. * The list, as returned through "alldevsp", may be null if no interfaces
  412. * were up and could be opened.
  413. */
  414. int
  415. pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
  416. {
  417. size_t i;
  418. pcap_if_list_t devlist;
  419. /*
  420. * Find all the local network interfaces on which we
  421. * can capture.
  422. */
  423. devlist.beginning = NULL;
  424. if (pcap_platform_finddevs(&devlist, errbuf) == -1) {
  425. /*
  426. * Failed - free all of the entries we were given
  427. * before we failed.
  428. */
  429. if (devlist.beginning != NULL)
  430. pcap_freealldevs(devlist.beginning);
  431. *alldevsp = NULL;
  432. return (-1);
  433. }
  434. /*
  435. * Ask each of the non-local-network-interface capture
  436. * source types what interfaces they have.
  437. */
  438. for (i = 0; capture_source_types[i].findalldevs_op != NULL; i++) {
  439. if (capture_source_types[i].findalldevs_op(&devlist, errbuf) == -1) {
  440. /*
  441. * We had an error; free the list we've been
  442. * constructing.
  443. */
  444. if (devlist.beginning != NULL)
  445. pcap_freealldevs(devlist.beginning);
  446. *alldevsp = NULL;
  447. return (-1);
  448. }
  449. }
  450. /*
  451. * Return the first entry of the list of all devices.
  452. */
  453. *alldevsp = devlist.beginning;
  454. return (0);
  455. }
  456. static struct sockaddr *
  457. dup_sockaddr(struct sockaddr *sa, size_t sa_length)
  458. {
  459. struct sockaddr *newsa;
  460. if ((newsa = malloc(sa_length)) == NULL)
  461. return (NULL);
  462. return (memcpy(newsa, sa, sa_length));
  463. }
  464. /*
  465. * Construct a "figure of merit" for an interface, for use when sorting
  466. * the list of interfaces, in which interfaces that are up are superior
  467. * to interfaces that aren't up, interfaces that are up and running are
  468. * superior to interfaces that are up but not running, and non-loopback
  469. * interfaces that are up and running are superior to loopback interfaces,
  470. * and interfaces with the same flags have a figure of merit that's higher
  471. * the lower the instance number.
  472. *
  473. * The goal is to try to put the interfaces most likely to be useful for
  474. * capture at the beginning of the list.
  475. *
  476. * The figure of merit, which is lower the "better" the interface is,
  477. * has the uppermost bit set if the interface isn't running, the bit
  478. * below that set if the interface isn't up, the bit below that set
  479. * if the interface is a loopback interface, and the interface index
  480. * in the 29 bits below that. (Yes, we assume u_int is 32 bits.)
  481. */
  482. static u_int
  483. get_figure_of_merit(pcap_if_t *dev)
  484. {
  485. const char *cp;
  486. u_int n;
  487. if (strcmp(dev->name, "any") == 0) {
  488. /*
  489. * Give the "any" device an artificially high instance
  490. * number, so it shows up after all other non-loopback
  491. * interfaces.
  492. */
  493. n = 0x1FFFFFFF; /* 29 all-1 bits */
  494. } else {
  495. /*
  496. * A number at the end of the device name string is
  497. * assumed to be an instance number. Add 1 to the
  498. * instance number, and use 0 for "no instance
  499. * number", so we don't put "no instance number"
  500. * devices and "instance 0" devices together.
  501. */
  502. cp = dev->name + strlen(dev->name) - 1;
  503. while (cp-1 >= dev->name && *(cp-1) >= '0' && *(cp-1) <= '9')
  504. cp--;
  505. if (*cp >= '0' && *cp <= '9')
  506. n = atoi(cp) + 1;
  507. else
  508. n = 0;
  509. }
  510. if (!(dev->flags & PCAP_IF_RUNNING))
  511. n |= 0x80000000;
  512. if (!(dev->flags & PCAP_IF_UP))
  513. n |= 0x40000000;
  514. /*
  515. * Give non-wireless interfaces that aren't disconnected a better
  516. * figure of merit than interfaces that are disconnected, as
  517. * "disconnected" should indicate that the interface isn't
  518. * plugged into a network and thus won't give you any traffic.
  519. *
  520. * For wireless interfaces, it means "associated with a network",
  521. * which we presume not to necessarily prevent capture, as you
  522. * might run the adapter in some flavor of monitor mode.
  523. */
  524. if (!(dev->flags & PCAP_IF_WIRELESS) &&
  525. (dev->flags & PCAP_IF_CONNECTION_STATUS) == PCAP_IF_CONNECTION_STATUS_DISCONNECTED)
  526. n |= 0x20000000;
  527. /*
  528. * Sort loopback devices after non-loopback devices, *except* for
  529. * disconnected devices.
  530. */
  531. if (dev->flags & PCAP_IF_LOOPBACK)
  532. n |= 0x10000000;
  533. return (n);
  534. }
  535. #ifndef _WIN32
  536. /*
  537. * Try to get a description for a given device.
  538. * Returns a mallocated description if it could and NULL if it couldn't.
  539. *
  540. * XXX - on FreeBSDs that support it, should it get the sysctl named
  541. * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
  542. * of the adapter? Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
  543. * with my Cisco 350 card, so the name isn't entirely descriptive. The
  544. * "dev.an.0.%pnpinfo" has a better description, although one might argue
  545. * that the problem is really a driver bug - if it can find out that it's
  546. * a Cisco 340 or 350, rather than an old Aironet card, it should use
  547. * that in the description.
  548. *
  549. * Do NetBSD, DragonflyBSD, or OpenBSD support this as well? FreeBSD
  550. * and OpenBSD let you get a description, but it's not generated by the OS,
  551. * it's set with another ioctl that ifconfig supports; we use that to get
  552. * a description in FreeBSD and OpenBSD, but if there is no such
  553. * description available, it still might be nice to get some description
  554. * string based on the device type or something such as that.
  555. *
  556. * In macOS, the System Configuration framework can apparently return
  557. * names in 10.4 and later.
  558. *
  559. * It also appears that freedesktop.org's HAL offers an "info.product"
  560. * string, but the HAL specification says it "should not be used in any
  561. * UI" and "subsystem/capability specific properties" should be used
  562. * instead and, in any case, I think HAL is being deprecated in
  563. * favor of other stuff such as DeviceKit. DeviceKit doesn't appear
  564. * to have any obvious product information for devices, but maybe
  565. * I haven't looked hard enough.
  566. *
  567. * Using the System Configuration framework, or HAL, or DeviceKit, or
  568. * whatever, would require that libpcap applications be linked with
  569. * the frameworks/libraries in question. That shouldn't be a problem
  570. * for programs linking with the shared version of libpcap (unless
  571. * you're running on AIX - which I think is the only UN*X that doesn't
  572. * support linking a shared library with other libraries on which it
  573. * depends, and having an executable linked only with the first shared
  574. * library automatically pick up the other libraries when started -
  575. * and using HAL or whatever). Programs linked with the static
  576. * version of libpcap would have to use pcap-config with the --static
  577. * flag in order to get the right linker flags in order to pick up
  578. * the additional libraries/frameworks; those programs need that anyway
  579. * for libpcap 1.1 and beyond on Linux, as, by default, it requires
  580. * -lnl.
  581. *
  582. * Do any other UN*Xes, or desktop environments support getting a
  583. * description?
  584. */
  585. static char *
  586. #ifdef SIOCGIFDESCR
  587. get_if_description(const char *name)
  588. {
  589. char *description = NULL;
  590. int s;
  591. struct ifreq ifrdesc;
  592. #ifndef IFDESCRSIZE
  593. size_t descrlen = 64;
  594. #else
  595. size_t descrlen = IFDESCRSIZE;
  596. #endif /* IFDESCRSIZE */
  597. /*
  598. * Get the description for the interface.
  599. */
  600. memset(&ifrdesc, 0, sizeof ifrdesc);
  601. strlcpy(ifrdesc.ifr_name, name, sizeof ifrdesc.ifr_name);
  602. s = socket(AF_INET, SOCK_DGRAM, 0);
  603. if (s >= 0) {
  604. #ifdef __FreeBSD__
  605. /*
  606. * On FreeBSD, if the buffer isn't big enough for the
  607. * description, the ioctl succeeds, but the description
  608. * isn't copied, ifr_buffer.length is set to the description
  609. * length, and ifr_buffer.buffer is set to NULL.
  610. */
  611. for (;;) {
  612. free(description);
  613. if ((description = malloc(descrlen)) != NULL) {
  614. ifrdesc.ifr_buffer.buffer = description;
  615. ifrdesc.ifr_buffer.length = descrlen;
  616. if (ioctl(s, SIOCGIFDESCR, &ifrdesc) == 0) {
  617. if (ifrdesc.ifr_buffer.buffer ==
  618. description)
  619. break;
  620. else
  621. descrlen = ifrdesc.ifr_buffer.length;
  622. } else {
  623. /*
  624. * Failed to get interface description.
  625. */
  626. free(description);
  627. description = NULL;
  628. break;
  629. }
  630. } else
  631. break;
  632. }
  633. #else /* __FreeBSD__ */
  634. /*
  635. * The only other OS that currently supports
  636. * SIOCGIFDESCR is OpenBSD, and it has no way
  637. * to get the description length - it's clamped
  638. * to a maximum of IFDESCRSIZE.
  639. */
  640. if ((description = malloc(descrlen)) != NULL) {
  641. ifrdesc.ifr_data = (caddr_t)description;
  642. if (ioctl(s, SIOCGIFDESCR, &ifrdesc) != 0) {
  643. /*
  644. * Failed to get interface description.
  645. */
  646. free(description);
  647. description = NULL;
  648. }
  649. }
  650. #endif /* __FreeBSD__ */
  651. close(s);
  652. if (description != NULL && strlen(description) == 0) {
  653. /*
  654. * Description is empty, so discard it.
  655. */
  656. free(description);
  657. description = NULL;
  658. }
  659. }
  660. #ifdef __FreeBSD__
  661. /*
  662. * For FreeBSD, if we didn't get a description, and this is
  663. * a device with a name of the form usbusN, label it as a USB
  664. * bus.
  665. */
  666. if (description == NULL) {
  667. if (strncmp(name, "usbus", 5) == 0) {
  668. /*
  669. * OK, it begins with "usbus".
  670. */
  671. long busnum;
  672. char *p;
  673. errno = 0;
  674. busnum = strtol(name + 5, &p, 10);
  675. if (errno == 0 && p != name + 5 && *p == '\0' &&
  676. busnum >= 0 && busnum <= INT_MAX) {
  677. /*
  678. * OK, it's a valid number that's not
  679. * bigger than INT_MAX. Construct
  680. * a description from it.
  681. */
  682. static const char descr_prefix[] = "USB bus number ";
  683. size_t descr_size;
  684. /*
  685. * Allow enough room for a 32-bit bus number.
  686. * sizeof (descr_prefix) includes the
  687. * terminating NUL.
  688. */
  689. descr_size = sizeof (descr_prefix) + 10;
  690. description = malloc(descr_size);
  691. if (description != NULL) {
  692. pcap_snprintf(description, descr_size,
  693. "%s%ld", descr_prefix, busnum);
  694. }
  695. }
  696. }
  697. }
  698. #endif
  699. return (description);
  700. #else /* SIOCGIFDESCR */
  701. get_if_description(const char *name _U_)
  702. {
  703. return (NULL);
  704. #endif /* SIOCGIFDESCR */
  705. }
  706. /*
  707. * Look for a given device in the specified list of devices.
  708. *
  709. * If we find it, return a pointer to its entry.
  710. *
  711. * If we don't find it, attempt to add an entry for it, with the specified
  712. * IFF_ flags and description, and, if that succeeds, return a pointer to
  713. * the new entry, otherwise return NULL and set errbuf to an error message.
  714. */
  715. pcap_if_t *
  716. find_or_add_if(pcap_if_list_t *devlistp, const char *name,
  717. bpf_u_int32 if_flags, get_if_flags_func get_flags_func, char *errbuf)
  718. {
  719. bpf_u_int32 pcap_flags;
  720. /*
  721. * Convert IFF_ flags to pcap flags.
  722. */
  723. pcap_flags = 0;
  724. #ifdef IFF_LOOPBACK
  725. if (if_flags & IFF_LOOPBACK)
  726. pcap_flags |= PCAP_IF_LOOPBACK;
  727. #else
  728. /*
  729. * We don't have IFF_LOOPBACK, so look at the device name to
  730. * see if it looks like a loopback device.
  731. */
  732. if (name[0] == 'l' && name[1] == 'o' &&
  733. (isdigit((unsigned char)(name[2])) || name[2] == '\0')
  734. pcap_flags |= PCAP_IF_LOOPBACK;
  735. #endif
  736. #ifdef IFF_UP
  737. if (if_flags & IFF_UP)
  738. pcap_flags |= PCAP_IF_UP;
  739. #endif
  740. #ifdef IFF_RUNNING
  741. if (if_flags & IFF_RUNNING)
  742. pcap_flags |= PCAP_IF_RUNNING;
  743. #endif
  744. /*
  745. * Attempt to find an entry for this device; if we don't find one,
  746. * attempt to add one.
  747. */
  748. return (find_or_add_dev(devlistp, name, pcap_flags,
  749. get_flags_func, get_if_description(name), errbuf));
  750. }
  751. /*
  752. * Look for a given device in the specified list of devices.
  753. *
  754. * If we find it, then, if the specified address isn't null, add it to
  755. * the list of addresses for the device and return 0.
  756. *
  757. * If we don't find it, attempt to add an entry for it, with the specified
  758. * IFF_ flags and description, and, if that succeeds, add the specified
  759. * address to its list of addresses if that address is non-null, and
  760. * return 0, otherwise return -1 and set errbuf to an error message.
  761. *
  762. * (We can get called with a null address because we might get a list
  763. * of interface name/address combinations from the underlying OS, with
  764. * the address being absent in some cases, rather than a list of
  765. * interfaces with each interface having a list of addresses, so this
  766. * call may be the only call made to add to the list, and we want to
  767. * add interfaces even if they have no addresses.)
  768. */
  769. int
  770. add_addr_to_if(pcap_if_list_t *devlistp, const char *name,
  771. bpf_u_int32 if_flags, get_if_flags_func get_flags_func,
  772. struct sockaddr *addr, size_t addr_size,
  773. struct sockaddr *netmask, size_t netmask_size,
  774. struct sockaddr *broadaddr, size_t broadaddr_size,
  775. struct sockaddr *dstaddr, size_t dstaddr_size,
  776. char *errbuf)
  777. {
  778. pcap_if_t *curdev;
  779. /*
  780. * Check whether the device exists and, if not, add it.
  781. */
  782. curdev = find_or_add_if(devlistp, name, if_flags, get_flags_func,
  783. errbuf);
  784. if (curdev == NULL) {
  785. /*
  786. * Error - give up.
  787. */
  788. return (-1);
  789. }
  790. if (addr == NULL) {
  791. /*
  792. * There's no address to add; this entry just meant
  793. * "here's a new interface".
  794. */
  795. return (0);
  796. }
  797. /*
  798. * "curdev" is an entry for this interface, and we have an
  799. * address for it; add an entry for that address to the
  800. * interface's list of addresses.
  801. */
  802. return (add_addr_to_dev(curdev, addr, addr_size, netmask,
  803. netmask_size, broadaddr, broadaddr_size, dstaddr,
  804. dstaddr_size, errbuf));
  805. }
  806. #endif /* _WIN32 */
  807. /*
  808. * Add an entry to the list of addresses for an interface.
  809. * "curdev" is the entry for that interface.
  810. */
  811. int
  812. add_addr_to_dev(pcap_if_t *curdev,
  813. struct sockaddr *addr, size_t addr_size,
  814. struct sockaddr *netmask, size_t netmask_size,
  815. struct sockaddr *broadaddr, size_t broadaddr_size,
  816. struct sockaddr *dstaddr, size_t dstaddr_size,
  817. char *errbuf)
  818. {
  819. pcap_addr_t *curaddr, *prevaddr, *nextaddr;
  820. /*
  821. * Allocate the new entry and fill it in.
  822. */
  823. curaddr = (pcap_addr_t *)malloc(sizeof(pcap_addr_t));
  824. if (curaddr == NULL) {
  825. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  826. errno, "malloc");
  827. return (-1);
  828. }
  829. curaddr->next = NULL;
  830. if (addr != NULL && addr_size != 0) {
  831. curaddr->addr = (struct sockaddr *)dup_sockaddr(addr, addr_size);
  832. if (curaddr->addr == NULL) {
  833. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  834. errno, "malloc");
  835. free(curaddr);
  836. return (-1);
  837. }
  838. } else
  839. curaddr->addr = NULL;
  840. if (netmask != NULL && netmask_size != 0) {
  841. curaddr->netmask = (struct sockaddr *)dup_sockaddr(netmask, netmask_size);
  842. if (curaddr->netmask == NULL) {
  843. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  844. errno, "malloc");
  845. if (curaddr->addr != NULL)
  846. free(curaddr->addr);
  847. free(curaddr);
  848. return (-1);
  849. }
  850. } else
  851. curaddr->netmask = NULL;
  852. if (broadaddr != NULL && broadaddr_size != 0) {
  853. curaddr->broadaddr = (struct sockaddr *)dup_sockaddr(broadaddr, broadaddr_size);
  854. if (curaddr->broadaddr == NULL) {
  855. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  856. errno, "malloc");
  857. if (curaddr->netmask != NULL)
  858. free(curaddr->netmask);
  859. if (curaddr->addr != NULL)
  860. free(curaddr->addr);
  861. free(curaddr);
  862. return (-1);
  863. }
  864. } else
  865. curaddr->broadaddr = NULL;
  866. if (dstaddr != NULL && dstaddr_size != 0) {
  867. curaddr->dstaddr = (struct sockaddr *)dup_sockaddr(dstaddr, dstaddr_size);
  868. if (curaddr->dstaddr == NULL) {
  869. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  870. errno, "malloc");
  871. if (curaddr->broadaddr != NULL)
  872. free(curaddr->broadaddr);
  873. if (curaddr->netmask != NULL)
  874. free(curaddr->netmask);
  875. if (curaddr->addr != NULL)
  876. free(curaddr->addr);
  877. free(curaddr);
  878. return (-1);
  879. }
  880. } else
  881. curaddr->dstaddr = NULL;
  882. /*
  883. * Find the end of the list of addresses.
  884. */
  885. for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
  886. nextaddr = prevaddr->next;
  887. if (nextaddr == NULL) {
  888. /*
  889. * This is the end of the list.
  890. */
  891. break;
  892. }
  893. }
  894. if (prevaddr == NULL) {
  895. /*
  896. * The list was empty; this is the first member.
  897. */
  898. curdev->addresses = curaddr;
  899. } else {
  900. /*
  901. * "prevaddr" is the last member of the list; append
  902. * this member to it.
  903. */
  904. prevaddr->next = curaddr;
  905. }
  906. return (0);
  907. }
  908. /*
  909. * Look for a given device in the specified list of devices.
  910. *
  911. * If we find it, return 0 and set *curdev_ret to point to it.
  912. *
  913. * If we don't find it, attempt to add an entry for it, with the specified
  914. * flags and description, and, if that succeeds, return 0, otherwise
  915. * return -1 and set errbuf to an error message.
  916. */
  917. pcap_if_t *
  918. find_or_add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags,
  919. get_if_flags_func get_flags_func, const char *description, char *errbuf)
  920. {
  921. pcap_if_t *curdev;
  922. /*
  923. * Is there already an entry in the list for this device?
  924. */
  925. curdev = find_dev(devlistp, name);
  926. if (curdev != NULL) {
  927. /*
  928. * Yes, return it.
  929. */
  930. return (curdev);
  931. }
  932. /*
  933. * No, we didn't find it.
  934. */
  935. /*
  936. * Try to get additional flags for the device.
  937. */
  938. if ((*get_flags_func)(name, &flags, errbuf) == -1) {
  939. /*
  940. * Failed.
  941. */
  942. return (NULL);
  943. }
  944. /*
  945. * Now, try to add it to the list of devices.
  946. */
  947. return (add_dev(devlistp, name, flags, description, errbuf));
  948. }
  949. /*
  950. * Look for a given device in the specified list of devices, and return
  951. * the entry for it if we find it or NULL if we don't.
  952. */
  953. pcap_if_t *
  954. find_dev(pcap_if_list_t *devlistp, const char *name)
  955. {
  956. pcap_if_t *curdev;
  957. /*
  958. * Is there an entry in the list for this device?
  959. */
  960. for (curdev = devlistp->beginning; curdev != NULL;
  961. curdev = curdev->next) {
  962. if (strcmp(name, curdev->name) == 0) {
  963. /*
  964. * We found it, so, yes, there is. No need to
  965. * add it. Provide the entry we found to our
  966. * caller.
  967. */
  968. return (curdev);
  969. }
  970. }
  971. /*
  972. * No.
  973. */
  974. return (NULL);
  975. }
  976. /*
  977. * Attempt to add an entry for a device, with the specified flags
  978. * and description, and, if that succeeds, return 0 and return a pointer
  979. * to the new entry, otherwise return NULL and set errbuf to an error
  980. * message.
  981. *
  982. * If we weren't given a description, try to get one.
  983. */
  984. pcap_if_t *
  985. add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags,
  986. const char *description, char *errbuf)
  987. {
  988. pcap_if_t *curdev, *prevdev, *nextdev;
  989. u_int this_figure_of_merit, nextdev_figure_of_merit;
  990. curdev = malloc(sizeof(pcap_if_t));
  991. if (curdev == NULL) {
  992. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  993. errno, "malloc");
  994. return (NULL);
  995. }
  996. /*
  997. * Fill in the entry.
  998. */
  999. curdev->next = NULL;
  1000. curdev->name = strdup(name);
  1001. if (curdev->name == NULL) {
  1002. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1003. errno, "malloc");
  1004. free(curdev);
  1005. return (NULL);
  1006. }
  1007. if (description == NULL) {
  1008. /*
  1009. * We weren't handed a description for the interface.
  1010. */
  1011. curdev->description = NULL;
  1012. } else {
  1013. /*
  1014. * We were handed a description; make a copy.
  1015. */
  1016. curdev->description = strdup(description);
  1017. if (curdev->description == NULL) {
  1018. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1019. errno, "malloc");
  1020. free(curdev->name);
  1021. free(curdev);
  1022. return (NULL);
  1023. }
  1024. }
  1025. curdev->addresses = NULL; /* list starts out as empty */
  1026. curdev->flags = flags;
  1027. /*
  1028. * Add it to the list, in the appropriate location.
  1029. * First, get the "figure of merit" for this interface.
  1030. */
  1031. this_figure_of_merit = get_figure_of_merit(curdev);
  1032. /*
  1033. * Now look for the last interface with an figure of merit
  1034. * less than or equal to the new interface's figure of merit.
  1035. *
  1036. * We start with "prevdev" being NULL, meaning we're before
  1037. * the first element in the list.
  1038. */
  1039. prevdev = NULL;
  1040. for (;;) {
  1041. /*
  1042. * Get the interface after this one.
  1043. */
  1044. if (prevdev == NULL) {
  1045. /*
  1046. * The next element is the first element.
  1047. */
  1048. nextdev = devlistp->beginning;
  1049. } else
  1050. nextdev = prevdev->next;
  1051. /*
  1052. * Are we at the end of the list?
  1053. */
  1054. if (nextdev == NULL) {
  1055. /*
  1056. * Yes - we have to put the new entry after "prevdev".
  1057. */
  1058. break;
  1059. }
  1060. /*
  1061. * Is the new interface's figure of merit less
  1062. * than the next interface's figure of merit,
  1063. * meaning that the new interface is better
  1064. * than the next interface?
  1065. */
  1066. nextdev_figure_of_merit = get_figure_of_merit(nextdev);
  1067. if (this_figure_of_merit < nextdev_figure_of_merit) {
  1068. /*
  1069. * Yes - we should put the new entry
  1070. * before "nextdev", i.e. after "prevdev".
  1071. */
  1072. break;
  1073. }
  1074. prevdev = nextdev;
  1075. }
  1076. /*
  1077. * Insert before "nextdev".
  1078. */
  1079. curdev->next = nextdev;
  1080. /*
  1081. * Insert after "prevdev" - unless "prevdev" is null,
  1082. * in which case this is the first interface.
  1083. */
  1084. if (prevdev == NULL) {
  1085. /*
  1086. * This is the first interface. Make it
  1087. * the first element in the list of devices.
  1088. */
  1089. devlistp->beginning = curdev;
  1090. } else
  1091. prevdev->next = curdev;
  1092. return (curdev);
  1093. }
  1094. /*
  1095. * Free a list of interfaces.
  1096. */
  1097. void
  1098. pcap_freealldevs(pcap_if_t *alldevs)
  1099. {
  1100. pcap_if_t *curdev, *nextdev;
  1101. pcap_addr_t *curaddr, *nextaddr;
  1102. for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
  1103. nextdev = curdev->next;
  1104. /*
  1105. * Free all addresses.
  1106. */
  1107. for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
  1108. nextaddr = curaddr->next;
  1109. if (curaddr->addr)
  1110. free(curaddr->addr);
  1111. if (curaddr->netmask)
  1112. free(curaddr->netmask);
  1113. if (curaddr->broadaddr)
  1114. free(curaddr->broadaddr);
  1115. if (curaddr->dstaddr)
  1116. free(curaddr->dstaddr);
  1117. free(curaddr);
  1118. }
  1119. /*
  1120. * Free the name string.
  1121. */
  1122. free(curdev->name);
  1123. /*
  1124. * Free the description string, if any.
  1125. */
  1126. if (curdev->description != NULL)
  1127. free(curdev->description);
  1128. /*
  1129. * Free the interface.
  1130. */
  1131. free(curdev);
  1132. }
  1133. }
  1134. /*
  1135. * pcap-npf.c has its own pcap_lookupdev(), for compatibility reasons, as
  1136. * it actually returns the names of all interfaces, with a NUL separator
  1137. * between them; some callers may depend on that.
  1138. *
  1139. * MS-DOS has its own pcap_lookupdev(), but that might be useful only
  1140. * as an optimization.
  1141. *
  1142. * In all other cases, we just use pcap_findalldevs() to get a list of
  1143. * devices, and pick from that list.
  1144. */
  1145. #if !defined(HAVE_PACKET32) && !defined(MSDOS)
  1146. /*
  1147. * Return the name of a network interface attached to the system, or NULL
  1148. * if none can be found. The interface must be configured up; the
  1149. * lowest unit number is preferred; loopback is ignored.
  1150. */
  1151. char *
  1152. pcap_lookupdev(char *errbuf)
  1153. {
  1154. pcap_if_t *alldevs;
  1155. #ifdef _WIN32
  1156. /*
  1157. * Windows - use the same size as the old WinPcap 3.1 code.
  1158. * XXX - this is probably bigger than it needs to be.
  1159. */
  1160. #define IF_NAMESIZE 8192
  1161. #else
  1162. /*
  1163. * UN*X - use the system's interface name size.
  1164. * XXX - that might not be large enough for capture devices
  1165. * that aren't regular network interfaces.
  1166. */
  1167. /* for old BSD systems, including bsdi3 */
  1168. #ifndef IF_NAMESIZE
  1169. #define IF_NAMESIZE IFNAMSIZ
  1170. #endif
  1171. #endif
  1172. static char device[IF_NAMESIZE + 1];
  1173. char *ret;
  1174. if (pcap_findalldevs(&alldevs, errbuf) == -1)
  1175. return (NULL);
  1176. if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
  1177. /*
  1178. * There are no devices on the list, or the first device
  1179. * on the list is a loopback device, which means there
  1180. * are no non-loopback devices on the list. This means
  1181. * we can't return any device.
  1182. *
  1183. * XXX - why not return a loopback device? If we can't
  1184. * capture on it, it won't be on the list, and if it's
  1185. * on the list, there aren't any non-loopback devices,
  1186. * so why not just supply it as the default device?
  1187. */
  1188. (void)strlcpy(errbuf, "no suitable device found",
  1189. PCAP_ERRBUF_SIZE);
  1190. ret = NULL;
  1191. } else {
  1192. /*
  1193. * Return the name of the first device on the list.
  1194. */
  1195. (void)strlcpy(device, alldevs->name, sizeof(device));
  1196. ret = device;
  1197. }
  1198. pcap_freealldevs(alldevs);
  1199. return (ret);
  1200. }
  1201. #endif /* !defined(HAVE_PACKET32) && !defined(MSDOS) */
  1202. #if !defined(_WIN32) && !defined(MSDOS)
  1203. /*
  1204. * We don't just fetch the entire list of devices, search for the
  1205. * particular device, and use its first IPv4 address, as that's too
  1206. * much work to get just one device's netmask.
  1207. *
  1208. * If we had an API to get attributes for a given device, we could
  1209. * use that.
  1210. */
  1211. int
  1212. pcap_lookupnet(const char *device, bpf_u_int32 *netp, bpf_u_int32 *maskp,
  1213. char *errbuf)
  1214. {
  1215. register int fd;
  1216. register struct sockaddr_in *sin4;
  1217. struct ifreq ifr;
  1218. /*
  1219. * The pseudo-device "any" listens on all interfaces and therefore
  1220. * has the network address and -mask "0.0.0.0" therefore catching
  1221. * all traffic. Using NULL for the interface is the same as "any".
  1222. */
  1223. if (!device || strcmp(device, "any") == 0
  1224. #ifdef HAVE_DAG_API
  1225. || strstr(device, "dag") != NULL
  1226. #endif
  1227. #ifdef HAVE_SEPTEL_API
  1228. || strstr(device, "septel") != NULL
  1229. #endif
  1230. #ifdef PCAP_SUPPORT_BT
  1231. || strstr(device, "bluetooth") != NULL
  1232. #endif
  1233. #ifdef PCAP_SUPPORT_USB
  1234. || strstr(device, "usbmon") != NULL
  1235. #endif
  1236. #ifdef HAVE_SNF_API
  1237. || strstr(device, "snf") != NULL
  1238. #endif
  1239. #ifdef PCAP_SUPPORT_NETMAP
  1240. || strncmp(device, "netmap:", 7) == 0
  1241. || strncmp(device, "vale", 4) == 0
  1242. #endif
  1243. ) {
  1244. *netp = *maskp = 0;
  1245. return 0;
  1246. }
  1247. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1248. if (fd < 0) {
  1249. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1250. errno, "socket");
  1251. return (-1);
  1252. }
  1253. memset(&ifr, 0, sizeof(ifr));
  1254. #ifdef linux
  1255. /* XXX Work around Linux kernel bug */
  1256. ifr.ifr_addr.sa_family = AF_INET;
  1257. #endif
  1258. (void)strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  1259. if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
  1260. if (errno == EADDRNOTAVAIL) {
  1261. (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  1262. "%s: no IPv4 address assigned", device);
  1263. } else {
  1264. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1265. errno, "SIOCGIFADDR: %s", device);
  1266. }
  1267. (void)close(fd);
  1268. return (-1);
  1269. }
  1270. sin4 = (struct sockaddr_in *)&ifr.ifr_addr;
  1271. *netp = sin4->sin_addr.s_addr;
  1272. memset(&ifr, 0, sizeof(ifr));
  1273. #ifdef linux
  1274. /* XXX Work around Linux kernel bug */
  1275. ifr.ifr_addr.sa_family = AF_INET;
  1276. #endif
  1277. (void)strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  1278. if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
  1279. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1280. errno, "SIOCGIFNETMASK: %s", device);
  1281. (void)close(fd);
  1282. return (-1);
  1283. }
  1284. (void)close(fd);
  1285. *maskp = sin4->sin_addr.s_addr;
  1286. if (*maskp == 0) {
  1287. if (IN_CLASSA(*netp))
  1288. *maskp = IN_CLASSA_NET;
  1289. else if (IN_CLASSB(*netp))
  1290. *maskp = IN_CLASSB_NET;
  1291. else if (IN_CLASSC(*netp))
  1292. *maskp = IN_CLASSC_NET;
  1293. else {
  1294. (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  1295. "inet class for 0x%x unknown", *netp);
  1296. return (-1);
  1297. }
  1298. }
  1299. *netp &= *maskp;
  1300. return (0);
  1301. }
  1302. #endif /* !defined(_WIN32) && !defined(MSDOS) */
  1303. #ifdef ENABLE_REMOTE
  1304. #include "pcap-rpcap.h"
  1305. /*
  1306. * Extract a substring from a string.
  1307. */
  1308. static char *
  1309. get_substring(const char *p, size_t len, char *ebuf)
  1310. {
  1311. char *token;
  1312. token = malloc(len + 1);
  1313. if (token == NULL) {
  1314. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1315. errno, "malloc");
  1316. return (NULL);
  1317. }
  1318. memcpy(token, p, len);
  1319. token[len] = '\0';
  1320. return (token);
  1321. }
  1322. /*
  1323. * Parse a capture source that might be a URL.
  1324. *
  1325. * If the source is not a URL, *schemep, *userinfop, *hostp, and *portp
  1326. * are set to NULL, *pathp is set to point to the source, and 0 is
  1327. * returned.
  1328. *
  1329. * If source is a URL, and the URL refers to a local device (a special
  1330. * case of rpcap:), *schemep, *userinfop, *hostp, and *portp are set
  1331. * to NULL, *pathp is set to point to the device name, and 0 is returned.
  1332. *
  1333. * If source is a URL, and it's not a special case that refers to a local
  1334. * device, and the parse succeeds:
  1335. *
  1336. * *schemep is set to point to an allocated string containing the scheme;
  1337. *
  1338. * if user information is present in the URL, *userinfop is set to point
  1339. * to an allocated string containing the user information, otherwise
  1340. * it's set to NULL;
  1341. *
  1342. * if host information is present in the URL, *hostp is set to point
  1343. * to an allocated string containing the host information, otherwise
  1344. * it's set to NULL;
  1345. *
  1346. * if a port number is present in the URL, *portp is set to point
  1347. * to an allocated string containing the port number, otherwise
  1348. * it's set to NULL;
  1349. *
  1350. * *pathp is set to point to an allocated string containing the
  1351. * path;
  1352. *
  1353. * and 0 is returned.
  1354. *
  1355. * If the parse fails, ebuf is set to an error string, and -1 is returned.
  1356. */
  1357. static int
  1358. pcap_parse_source(const char *source, char **schemep, char **userinfop,
  1359. char **hostp, char **portp, char **pathp, char *ebuf)
  1360. {
  1361. char *colonp;
  1362. size_t scheme_len;
  1363. char *scheme;
  1364. const char *endp;
  1365. size_t authority_len;
  1366. char *authority;
  1367. char *parsep, *atsignp, *bracketp;
  1368. char *userinfo, *host, *port, *path;
  1369. /*
  1370. * Start out returning nothing.
  1371. */
  1372. *schemep = NULL;
  1373. *userinfop = NULL;
  1374. *hostp = NULL;
  1375. *portp = NULL;
  1376. *pathp = NULL;
  1377. /*
  1378. * RFC 3986 says:
  1379. *
  1380. * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  1381. *
  1382. * hier-part = "//" authority path-abempty
  1383. * / path-absolute
  1384. * / path-rootless
  1385. * / path-empty
  1386. *
  1387. * authority = [ userinfo "@" ] host [ ":" port ]
  1388. *
  1389. * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
  1390. *
  1391. * Step 1: look for the ":" at the end of the scheme.
  1392. * A colon in the source is *NOT* sufficient to indicate that
  1393. * this is a URL, as interface names on some platforms might
  1394. * include colons (e.g., I think some Solaris interfaces
  1395. * might).
  1396. */
  1397. colonp = strchr(source, ':');
  1398. if (colonp == NULL) {
  1399. /*
  1400. * The source is the device to open.
  1401. * Return a NULL pointer for the scheme, user information,
  1402. * host, and port, and return the device as the path.
  1403. */
  1404. *pathp = strdup(source);
  1405. if (*pathp == NULL) {
  1406. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1407. errno, "malloc");
  1408. return (-1);
  1409. }
  1410. return (0);
  1411. }
  1412. /*
  1413. * All schemes must have "//" after them, i.e. we only support
  1414. * hier-part = "//" authority path-abempty, not
  1415. * hier-part = path-absolute
  1416. * hier-part = path-rootless
  1417. * hier-part = path-empty
  1418. *
  1419. * We need that in order to distinguish between a local device
  1420. * name that happens to contain a colon and a URI.
  1421. */
  1422. if (strncmp(colonp + 1, "//", 2) != 0) {
  1423. /*
  1424. * The source is the device to open.
  1425. * Return a NULL pointer for the scheme, user information,
  1426. * host, and port, and return the device as the path.
  1427. */
  1428. *pathp = strdup(source);
  1429. if (*pathp == NULL) {
  1430. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1431. errno, "malloc");
  1432. return (-1);
  1433. }
  1434. return (0);
  1435. }
  1436. /*
  1437. * XXX - check whether the purported scheme could be a scheme?
  1438. */
  1439. /*
  1440. * OK, this looks like a URL.
  1441. * Get the scheme.
  1442. */
  1443. scheme_len = colonp - source;
  1444. scheme = malloc(scheme_len + 1);
  1445. if (scheme == NULL) {
  1446. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1447. errno, "malloc");
  1448. return (-1);
  1449. }
  1450. memcpy(scheme, source, scheme_len);
  1451. scheme[scheme_len] = '\0';
  1452. /*
  1453. * Treat file: specially - take everything after file:// as
  1454. * the pathname.
  1455. */
  1456. if (pcap_strcasecmp(scheme, "file") == 0) {
  1457. *schemep = scheme;
  1458. *pathp = strdup(colonp + 3);
  1459. if (*pathp == NULL) {
  1460. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1461. errno, "malloc");
  1462. return (-1);
  1463. }
  1464. return (0);
  1465. }
  1466. /*
  1467. * The WinPcap documentation says you can specify a local
  1468. * interface with "rpcap://{device}"; we special-case
  1469. * that here. If the scheme is "rpcap", and there are
  1470. * no slashes past the "//", we just return the device.
  1471. *
  1472. * XXX - %-escaping?
  1473. */
  1474. if (pcap_strcasecmp(scheme, "rpcap") == 0 &&
  1475. strchr(colonp + 3, '/') == NULL) {
  1476. /*
  1477. * Local device.
  1478. *
  1479. * Return a NULL pointer for the scheme, user information,
  1480. * host, and port, and return the device as the path.
  1481. */
  1482. free(scheme);
  1483. *pathp = strdup(colonp + 3);
  1484. if (*pathp == NULL) {
  1485. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1486. errno, "malloc");
  1487. return (-1);
  1488. }
  1489. return (0);
  1490. }
  1491. /*
  1492. * OK, now start parsing the authority.
  1493. * Get token, terminated with / or terminated at the end of
  1494. * the string.
  1495. */
  1496. authority_len = strcspn(colonp + 3, "/");
  1497. authority = get_substring(colonp + 3, authority_len, ebuf);
  1498. if (authority == NULL) {
  1499. /*
  1500. * Error.
  1501. */
  1502. free(scheme);
  1503. return (-1);
  1504. }
  1505. endp = colonp + 3 + authority_len;
  1506. /*
  1507. * Now carve the authority field into its components.
  1508. */
  1509. parsep = authority;
  1510. /*
  1511. * Is there a userinfo field?
  1512. */
  1513. atsignp = strchr(parsep, '@');
  1514. if (atsignp != NULL) {
  1515. /*
  1516. * Yes.
  1517. */
  1518. size_t userinfo_len;
  1519. userinfo_len = atsignp - parsep;
  1520. userinfo = get_substring(parsep, userinfo_len, ebuf);
  1521. if (userinfo == NULL) {
  1522. /*
  1523. * Error.
  1524. */
  1525. free(authority);
  1526. free(scheme);
  1527. return (-1);
  1528. }
  1529. parsep = atsignp + 1;
  1530. } else {
  1531. /*
  1532. * No.
  1533. */
  1534. userinfo = NULL;
  1535. }
  1536. /*
  1537. * Is there a host field?
  1538. */
  1539. if (*parsep == '\0') {
  1540. /*
  1541. * No; there's no host field or port field.
  1542. */
  1543. host = NULL;
  1544. port = NULL;
  1545. } else {
  1546. /*
  1547. * Yes.
  1548. */
  1549. size_t host_len;
  1550. /*
  1551. * Is it an IP-literal?
  1552. */
  1553. if (*parsep == '[') {
  1554. /*
  1555. * Yes.
  1556. * Treat verything up to the closing square
  1557. * bracket as the IP-Literal; we don't worry
  1558. * about whether it's a valid IPv6address or
  1559. * IPvFuture.
  1560. */
  1561. bracketp = strchr(parsep, ']');
  1562. if (bracketp == NULL) {
  1563. /*
  1564. * There's no closing square bracket.
  1565. */
  1566. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1567. "IP-literal in URL doesn't end with ]");
  1568. free(userinfo);
  1569. free(authority);
  1570. free(scheme);
  1571. return (-1);
  1572. }
  1573. if (*(bracketp + 1) != '\0' &&
  1574. *(bracketp + 1) != ':') {
  1575. /*
  1576. * There's extra crud after the
  1577. * closing square bracketn.
  1578. */
  1579. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1580. "Extra text after IP-literal in URL");
  1581. free(userinfo);
  1582. free(authority);
  1583. free(scheme);
  1584. return (-1);
  1585. }
  1586. host_len = (bracketp - 1) - parsep;
  1587. host = get_substring(parsep + 1, host_len, ebuf);
  1588. if (host == NULL) {
  1589. /*
  1590. * Error.
  1591. */
  1592. free(userinfo);
  1593. free(authority);
  1594. free(scheme);
  1595. return (-1);
  1596. }
  1597. parsep = bracketp + 1;
  1598. } else {
  1599. /*
  1600. * No.
  1601. * Treat everything up to a : or the end of
  1602. * the string as the host.
  1603. */
  1604. host_len = strcspn(parsep, ":");
  1605. host = get_substring(parsep, host_len, ebuf);
  1606. if (host == NULL) {
  1607. /*
  1608. * Error.
  1609. */
  1610. free(userinfo);
  1611. free(authority);
  1612. free(scheme);
  1613. return (-1);
  1614. }
  1615. parsep = parsep + host_len;
  1616. }
  1617. /*
  1618. * Is there a port field?
  1619. */
  1620. if (*parsep == ':') {
  1621. /*
  1622. * Yes. It's the rest of the authority field.
  1623. */
  1624. size_t port_len;
  1625. parsep++;
  1626. port_len = strlen(parsep);
  1627. port = get_substring(parsep, port_len, ebuf);
  1628. if (port == NULL) {
  1629. /*
  1630. * Error.
  1631. */
  1632. free(host);
  1633. free(userinfo);
  1634. free(authority);
  1635. free(scheme);
  1636. return (-1);
  1637. }
  1638. } else {
  1639. /*
  1640. * No.
  1641. */
  1642. port = NULL;
  1643. }
  1644. }
  1645. free(authority);
  1646. /*
  1647. * Everything else is the path. Strip off the leading /.
  1648. */
  1649. if (*endp == '\0')
  1650. path = strdup("");
  1651. else
  1652. path = strdup(endp + 1);
  1653. if (path == NULL) {
  1654. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1655. errno, "malloc");
  1656. free(port);
  1657. free(host);
  1658. free(userinfo);
  1659. free(scheme);
  1660. return (-1);
  1661. }
  1662. *schemep = scheme;
  1663. *userinfop = userinfo;
  1664. *hostp = host;
  1665. *portp = port;
  1666. *pathp = path;
  1667. return (0);
  1668. }
  1669. int
  1670. pcap_createsrcstr(char *source, int type, const char *host, const char *port,
  1671. const char *name, char *errbuf)
  1672. {
  1673. switch (type) {
  1674. case PCAP_SRC_FILE:
  1675. strlcpy(source, PCAP_SRC_FILE_STRING, PCAP_BUF_SIZE);
  1676. if (name != NULL && *name != '\0') {
  1677. strlcat(source, name, PCAP_BUF_SIZE);
  1678. return (0);
  1679. } else {
  1680. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  1681. "The file name cannot be NULL.");
  1682. return (-1);
  1683. }
  1684. case PCAP_SRC_IFREMOTE:
  1685. strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE);
  1686. if (host != NULL && *host != '\0') {
  1687. if (strchr(host, ':') != NULL) {
  1688. /*
  1689. * The host name contains a colon, so it's
  1690. * probably an IPv6 address, and needs to
  1691. * be included in square brackets.
  1692. */
  1693. strlcat(source, "[", PCAP_BUF_SIZE);
  1694. strlcat(source, host, PCAP_BUF_SIZE);
  1695. strlcat(source, "]", PCAP_BUF_SIZE);
  1696. } else
  1697. strlcat(source, host, PCAP_BUF_SIZE);
  1698. if (port != NULL && *port != '\0') {
  1699. strlcat(source, ":", PCAP_BUF_SIZE);
  1700. strlcat(source, port, PCAP_BUF_SIZE);
  1701. }
  1702. strlcat(source, "/", PCAP_BUF_SIZE);
  1703. } else {
  1704. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  1705. "The host name cannot be NULL.");
  1706. return (-1);
  1707. }
  1708. if (name != NULL && *name != '\0')
  1709. strlcat(source, name, PCAP_BUF_SIZE);
  1710. return (0);
  1711. case PCAP_SRC_IFLOCAL:
  1712. strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE);
  1713. if (name != NULL && *name != '\0')
  1714. strlcat(source, name, PCAP_BUF_SIZE);
  1715. return (0);
  1716. default:
  1717. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  1718. "The interface type is not valid.");
  1719. return (-1);
  1720. }
  1721. }
  1722. int
  1723. pcap_parsesrcstr(const char *source, int *type, char *host, char *port,
  1724. char *name, char *errbuf)
  1725. {
  1726. char *scheme, *tmpuserinfo, *tmphost, *tmpport, *tmppath;
  1727. /* Initialization stuff */
  1728. if (host)
  1729. *host = '\0';
  1730. if (port)
  1731. *port = '\0';
  1732. if (name)
  1733. *name = '\0';
  1734. /* Parse the source string */
  1735. if (pcap_parse_source(source, &scheme, &tmpuserinfo, &tmphost,
  1736. &tmpport, &tmppath, errbuf) == -1) {
  1737. /*
  1738. * Fail.
  1739. */
  1740. return (-1);
  1741. }
  1742. if (scheme == NULL) {
  1743. /*
  1744. * Local device.
  1745. */
  1746. if (name && tmppath)
  1747. strlcpy(name, tmppath, PCAP_BUF_SIZE);
  1748. if (type)
  1749. *type = PCAP_SRC_IFLOCAL;
  1750. free(tmppath);
  1751. free(tmpport);
  1752. free(tmphost);
  1753. free(tmpuserinfo);
  1754. return (0);
  1755. }
  1756. if (strcmp(scheme, "rpcap") == 0) {
  1757. /*
  1758. * rpcap://
  1759. *
  1760. * pcap_parse_source() has already handled the case of
  1761. * rpcap://device
  1762. */
  1763. if (host && tmphost) {
  1764. if (tmpuserinfo)
  1765. pcap_snprintf(host, PCAP_BUF_SIZE, "%s@%s",
  1766. tmpuserinfo, tmphost);
  1767. else
  1768. strlcpy(host, tmphost, PCAP_BUF_SIZE);
  1769. }
  1770. if (port && tmpport)
  1771. strlcpy(port, tmpport, PCAP_BUF_SIZE);
  1772. if (name && tmppath)
  1773. strlcpy(name, tmppath, PCAP_BUF_SIZE);
  1774. if (type)
  1775. *type = PCAP_SRC_IFREMOTE;
  1776. free(tmppath);
  1777. free(tmpport);
  1778. free(tmphost);
  1779. free(tmpuserinfo);
  1780. free(scheme);
  1781. return (0);
  1782. }
  1783. if (strcmp(scheme, "file") == 0) {
  1784. /*
  1785. * file://
  1786. */
  1787. if (name && tmppath)
  1788. strlcpy(name, tmppath, PCAP_BUF_SIZE);
  1789. if (type)
  1790. *type = PCAP_SRC_FILE;
  1791. free(tmppath);
  1792. free(tmpport);
  1793. free(tmphost);
  1794. free(tmpuserinfo);
  1795. free(scheme);
  1796. return (0);
  1797. }
  1798. /*
  1799. * Neither rpcap: nor file:; just treat the entire string
  1800. * as a local device.
  1801. */
  1802. if (name)
  1803. strlcpy(name, source, PCAP_BUF_SIZE);
  1804. if (type)
  1805. *type = PCAP_SRC_IFLOCAL;
  1806. free(tmppath);
  1807. free(tmpport);
  1808. free(tmphost);
  1809. free(tmpuserinfo);
  1810. free(scheme);
  1811. return (0);
  1812. }
  1813. #endif
  1814. pcap_t *
  1815. pcap_create(const char *device, char *errbuf)
  1816. {
  1817. size_t i;
  1818. int is_theirs;
  1819. pcap_t *p;
  1820. char *device_str;
  1821. /*
  1822. * A null device name is equivalent to the "any" device -
  1823. * which might not be supported on this platform, but
  1824. * this means that you'll get a "not supported" error
  1825. * rather than, say, a crash when we try to dereference
  1826. * the null pointer.
  1827. */
  1828. if (device == NULL)
  1829. device_str = strdup("any");
  1830. else {
  1831. #ifdef _WIN32
  1832. /*
  1833. * If the string appears to be little-endian UCS-2/UTF-16,
  1834. * convert it to ASCII.
  1835. *
  1836. * XXX - to UTF-8 instead? Or report an error if any
  1837. * character isn't ASCII?
  1838. */
  1839. if (device[0] != '\0' && device[1] == '\0') {
  1840. size_t length;
  1841. length = wcslen((wchar_t *)device);
  1842. device_str = (char *)malloc(length + 1);
  1843. if (device_str == NULL) {
  1844. pcap_fmt_errmsg_for_errno(errbuf,
  1845. PCAP_ERRBUF_SIZE, errno,
  1846. "malloc");
  1847. return (NULL);
  1848. }
  1849. pcap_snprintf(device_str, length + 1, "%ws",
  1850. (const wchar_t *)device);
  1851. } else
  1852. #endif
  1853. device_str = strdup(device);
  1854. }
  1855. if (device_str == NULL) {
  1856. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1857. errno, "malloc");
  1858. return (NULL);
  1859. }
  1860. /*
  1861. * Try each of the non-local-network-interface capture
  1862. * source types until we find one that works for this
  1863. * device or run out of types.
  1864. */
  1865. for (i = 0; capture_source_types[i].create_op != NULL; i++) {
  1866. is_theirs = 0;
  1867. p = capture_source_types[i].create_op(device_str, errbuf,
  1868. &is_theirs);
  1869. if (is_theirs) {
  1870. /*
  1871. * The device name refers to a device of the
  1872. * type in question; either it succeeded,
  1873. * in which case p refers to a pcap_t to
  1874. * later activate for the device, or it
  1875. * failed, in which case p is null and we
  1876. * should return that to report the failure
  1877. * to create.
  1878. */
  1879. if (p == NULL) {
  1880. /*
  1881. * We assume the caller filled in errbuf.
  1882. */
  1883. free(device_str);
  1884. return (NULL);
  1885. }
  1886. p->opt.device = device_str;
  1887. return (p);
  1888. }
  1889. }
  1890. /*
  1891. * OK, try it as a regular network interface.
  1892. */
  1893. p = pcap_create_interface(device_str, errbuf);
  1894. if (p == NULL) {
  1895. /*
  1896. * We assume the caller filled in errbuf.
  1897. */
  1898. free(device_str);
  1899. return (NULL);
  1900. }
  1901. p->opt.device = device_str;
  1902. return (p);
  1903. }
  1904. /*
  1905. * Set nonblocking mode on an unactivated pcap_t; this sets a flag
  1906. * checked by pcap_activate(), which sets the mode after calling
  1907. * the activate routine.
  1908. */
  1909. static int
  1910. pcap_setnonblock_unactivated(pcap_t *p, int nonblock)
  1911. {
  1912. p->opt.nonblock = nonblock;
  1913. return (0);
  1914. }
  1915. static void
  1916. initialize_ops(pcap_t *p)
  1917. {
  1918. /*
  1919. * Set operation pointers for operations that only work on
  1920. * an activated pcap_t to point to a routine that returns
  1921. * a "this isn't activated" error.
  1922. */
  1923. p->read_op = (read_op_t)pcap_not_initialized;
  1924. p->inject_op = (inject_op_t)pcap_not_initialized;
  1925. p->setfilter_op = (setfilter_op_t)pcap_not_initialized;
  1926. p->setdirection_op = (setdirection_op_t)pcap_not_initialized;
  1927. p->set_datalink_op = (set_datalink_op_t)pcap_not_initialized;
  1928. p->getnonblock_op = (getnonblock_op_t)pcap_not_initialized;
  1929. p->stats_op = (stats_op_t)pcap_not_initialized;
  1930. #ifdef _WIN32
  1931. p->stats_ex_op = (stats_ex_op_t)pcap_not_initialized_ptr;
  1932. p->setbuff_op = (setbuff_op_t)pcap_not_initialized;
  1933. p->setmode_op = (setmode_op_t)pcap_not_initialized;
  1934. p->setmintocopy_op = (setmintocopy_op_t)pcap_not_initialized;
  1935. p->getevent_op = pcap_getevent_not_initialized;
  1936. p->oid_get_request_op = (oid_get_request_op_t)pcap_not_initialized;
  1937. p->oid_set_request_op = (oid_set_request_op_t)pcap_not_initialized;
  1938. p->sendqueue_transmit_op = pcap_sendqueue_transmit_not_initialized;
  1939. p->setuserbuffer_op = (setuserbuffer_op_t)pcap_not_initialized;
  1940. p->live_dump_op = (live_dump_op_t)pcap_not_initialized;
  1941. p->live_dump_ended_op = (live_dump_ended_op_t)pcap_not_initialized;
  1942. p->get_airpcap_handle_op = pcap_get_airpcap_handle_not_initialized;
  1943. #endif
  1944. /*
  1945. * Default cleanup operation - implementations can override
  1946. * this, but should call pcap_cleanup_live_common() after
  1947. * doing their own additional cleanup.
  1948. */
  1949. p->cleanup_op = pcap_cleanup_live_common;
  1950. /*
  1951. * In most cases, the standard one-shot callback can
  1952. * be used for pcap_next()/pcap_next_ex().
  1953. */
  1954. p->oneshot_callback = pcap_oneshot;
  1955. }
  1956. static pcap_t *
  1957. pcap_alloc_pcap_t(char *ebuf, size_t size)
  1958. {
  1959. char *chunk;
  1960. pcap_t *p;
  1961. /*
  1962. * Allocate a chunk of memory big enough for a pcap_t
  1963. * plus a structure following it of size "size". The
  1964. * structure following it is a private data structure
  1965. * for the routines that handle this pcap_t.
  1966. *
  1967. * The structure following it must be aligned on
  1968. * the appropriate alignment boundary for this platform.
  1969. * We align on an 8-byte boundary as that's probably what
  1970. * at least some platforms do, even with 32-bit integers,
  1971. * and because we can't be sure that some values won't
  1972. * require 8-byte alignment even on platforms with 32-bit
  1973. * integers.
  1974. */
  1975. #define PCAP_T_ALIGNED_SIZE ((sizeof(pcap_t) + 7) & ~0x7)
  1976. chunk = malloc(PCAP_T_ALIGNED_SIZE + size);
  1977. if (chunk == NULL) {
  1978. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1979. errno, "malloc");
  1980. return (NULL);
  1981. }
  1982. memset(chunk, 0, PCAP_T_ALIGNED_SIZE + size);
  1983. /*
  1984. * Get a pointer to the pcap_t at the beginning.
  1985. */
  1986. p = (pcap_t *)chunk;
  1987. #ifdef _WIN32
  1988. p->handle = INVALID_HANDLE_VALUE; /* not opened yet */
  1989. #else /* _WIN32 */
  1990. p->fd = -1; /* not opened yet */
  1991. #ifndef MSDOS
  1992. p->selectable_fd = -1;
  1993. p->required_select_timeout = NULL;
  1994. #endif /* MSDOS */
  1995. #endif /* _WIN32 */
  1996. if (size == 0) {
  1997. /* No private data was requested. */
  1998. p->priv = NULL;
  1999. } else {
  2000. /*
  2001. * Set the pointer to the private data; that's the structure
  2002. * of size "size" following the pcap_t.
  2003. */
  2004. p->priv = (void *)(chunk + PCAP_T_ALIGNED_SIZE);
  2005. }
  2006. return (p);
  2007. }
  2008. pcap_t *
  2009. pcap_create_common(char *ebuf, size_t size)
  2010. {
  2011. pcap_t *p;
  2012. p = pcap_alloc_pcap_t(ebuf, size);
  2013. if (p == NULL)
  2014. return (NULL);
  2015. /*
  2016. * Default to "can't set rfmon mode"; if it's supported by
  2017. * a platform, the create routine that called us can set
  2018. * the op to its routine to check whether a particular
  2019. * device supports it.
  2020. */
  2021. p->can_set_rfmon_op = pcap_cant_set_rfmon;
  2022. /*
  2023. * If pcap_setnonblock() is called on a not-yet-activated
  2024. * pcap_t, default to setting a flag and turning
  2025. * on non-blocking mode when activated.
  2026. */
  2027. p->setnonblock_op = pcap_setnonblock_unactivated;
  2028. initialize_ops(p);
  2029. /* put in some defaults*/
  2030. p->snapshot = 0; /* max packet size unspecified */
  2031. p->opt.timeout = 0; /* no timeout specified */
  2032. p->opt.buffer_size = 0; /* use the platform's default */
  2033. p->opt.promisc = 0;
  2034. p->opt.rfmon = 0;
  2035. p->opt.immediate = 0;
  2036. p->opt.tstamp_type = -1; /* default to not setting time stamp type */
  2037. p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
  2038. /*
  2039. * Platform-dependent options.
  2040. */
  2041. #ifdef __linux__
  2042. p->opt.protocol = 0;
  2043. #endif
  2044. #ifdef _WIN32
  2045. p->opt.nocapture_local = 0;
  2046. #endif
  2047. /*
  2048. * Start out with no BPF code generation flags set.
  2049. */
  2050. p->bpf_codegen_flags = 0;
  2051. return (p);
  2052. }
  2053. int
  2054. pcap_check_activated(pcap_t *p)
  2055. {
  2056. if (p->activated) {
  2057. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't perform "
  2058. " operation on activated capture");
  2059. return (-1);
  2060. }
  2061. return (0);
  2062. }
  2063. int
  2064. pcap_set_snaplen(pcap_t *p, int snaplen)
  2065. {
  2066. if (pcap_check_activated(p))
  2067. return (PCAP_ERROR_ACTIVATED);
  2068. p->snapshot = snaplen;
  2069. return (0);
  2070. }
  2071. int
  2072. pcap_set_promisc(pcap_t *p, int promisc)
  2073. {
  2074. if (pcap_check_activated(p))
  2075. return (PCAP_ERROR_ACTIVATED);
  2076. p->opt.promisc = promisc;
  2077. return (0);
  2078. }
  2079. int
  2080. pcap_set_rfmon(pcap_t *p, int rfmon)
  2081. {
  2082. if (pcap_check_activated(p))
  2083. return (PCAP_ERROR_ACTIVATED);
  2084. p->opt.rfmon = rfmon;
  2085. return (0);
  2086. }
  2087. int
  2088. pcap_set_timeout(pcap_t *p, int timeout_ms)
  2089. {
  2090. if (pcap_check_activated(p))
  2091. return (PCAP_ERROR_ACTIVATED);
  2092. p->opt.timeout = timeout_ms;
  2093. return (0);
  2094. }
  2095. int
  2096. pcap_set_tstamp_type(pcap_t *p, int tstamp_type)
  2097. {
  2098. int i;
  2099. if (pcap_check_activated(p))
  2100. return (PCAP_ERROR_ACTIVATED);
  2101. /*
  2102. * The argument should have been u_int, but that's too late
  2103. * to change now - it's an API.
  2104. */
  2105. if (tstamp_type < 0)
  2106. return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP);
  2107. /*
  2108. * If p->tstamp_type_count is 0, we only support PCAP_TSTAMP_HOST;
  2109. * the default time stamp type is PCAP_TSTAMP_HOST.
  2110. */
  2111. if (p->tstamp_type_count == 0) {
  2112. if (tstamp_type == PCAP_TSTAMP_HOST) {
  2113. p->opt.tstamp_type = tstamp_type;
  2114. return (0);
  2115. }
  2116. } else {
  2117. /*
  2118. * Check whether we claim to support this type of time stamp.
  2119. */
  2120. for (i = 0; i < p->tstamp_type_count; i++) {
  2121. if (p->tstamp_type_list[i] == (u_int)tstamp_type) {
  2122. /*
  2123. * Yes.
  2124. */
  2125. p->opt.tstamp_type = tstamp_type;
  2126. return (0);
  2127. }
  2128. }
  2129. }
  2130. /*
  2131. * We don't support this type of time stamp.
  2132. */
  2133. return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP);
  2134. }
  2135. int
  2136. pcap_set_immediate_mode(pcap_t *p, int immediate)
  2137. {
  2138. if (pcap_check_activated(p))
  2139. return (PCAP_ERROR_ACTIVATED);
  2140. p->opt.immediate = immediate;
  2141. return (0);
  2142. }
  2143. int
  2144. pcap_set_buffer_size(pcap_t *p, int buffer_size)
  2145. {
  2146. if (pcap_check_activated(p))
  2147. return (PCAP_ERROR_ACTIVATED);
  2148. if (buffer_size <= 0) {
  2149. /*
  2150. * Silently ignore invalid values.
  2151. */
  2152. return (0);
  2153. }
  2154. p->opt.buffer_size = buffer_size;
  2155. return (0);
  2156. }
  2157. int
  2158. pcap_set_tstamp_precision(pcap_t *p, int tstamp_precision)
  2159. {
  2160. int i;
  2161. if (pcap_check_activated(p))
  2162. return (PCAP_ERROR_ACTIVATED);
  2163. /*
  2164. * The argument should have been u_int, but that's too late
  2165. * to change now - it's an API.
  2166. */
  2167. if (tstamp_precision < 0)
  2168. return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP);
  2169. /*
  2170. * If p->tstamp_precision_count is 0, we only support setting
  2171. * the time stamp precision to microsecond precision; every
  2172. * pcap module *MUST* support microsecond precision, even if
  2173. * it does so by converting the native precision to
  2174. * microseconds.
  2175. */
  2176. if (p->tstamp_precision_count == 0) {
  2177. if (tstamp_precision == PCAP_TSTAMP_PRECISION_MICRO) {
  2178. p->opt.tstamp_precision = tstamp_precision;
  2179. return (0);
  2180. }
  2181. } else {
  2182. /*
  2183. * Check whether we claim to support this precision of
  2184. * time stamp.
  2185. */
  2186. for (i = 0; i < p->tstamp_precision_count; i++) {
  2187. if (p->tstamp_precision_list[i] == (u_int)tstamp_precision) {
  2188. /*
  2189. * Yes.
  2190. */
  2191. p->opt.tstamp_precision = tstamp_precision;
  2192. return (0);
  2193. }
  2194. }
  2195. }
  2196. /*
  2197. * We don't support this time stamp precision.
  2198. */
  2199. return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP);
  2200. }
  2201. int
  2202. pcap_get_tstamp_precision(pcap_t *p)
  2203. {
  2204. return (p->opt.tstamp_precision);
  2205. }
  2206. int
  2207. pcap_activate(pcap_t *p)
  2208. {
  2209. int status;
  2210. /*
  2211. * Catch attempts to re-activate an already-activated
  2212. * pcap_t; this should, for example, catch code that
  2213. * calls pcap_open_live() followed by pcap_activate(),
  2214. * as some code that showed up in a Stack Exchange
  2215. * question did.
  2216. */
  2217. if (pcap_check_activated(p))
  2218. return (PCAP_ERROR_ACTIVATED);
  2219. status = p->activate_op(p);
  2220. if (status >= 0) {
  2221. /*
  2222. * If somebody requested non-blocking mode before
  2223. * calling pcap_activate(), turn it on now.
  2224. */
  2225. if (p->opt.nonblock) {
  2226. status = p->setnonblock_op(p, 1);
  2227. if (status < 0) {
  2228. /*
  2229. * Failed. Undo everything done by
  2230. * the activate operation.
  2231. */
  2232. p->cleanup_op(p);
  2233. initialize_ops(p);
  2234. return (status);
  2235. }
  2236. }
  2237. p->activated = 1;
  2238. } else {
  2239. if (p->errbuf[0] == '\0') {
  2240. /*
  2241. * No error message supplied by the activate routine;
  2242. * for the benefit of programs that don't specially
  2243. * handle errors other than PCAP_ERROR, return the
  2244. * error message corresponding to the status.
  2245. */
  2246. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s",
  2247. pcap_statustostr(status));
  2248. }
  2249. /*
  2250. * Undo any operation pointer setting, etc. done by
  2251. * the activate operation.
  2252. */
  2253. initialize_ops(p);
  2254. }
  2255. return (status);
  2256. }
  2257. pcap_t *
  2258. pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, char *errbuf)
  2259. {
  2260. pcap_t *p;
  2261. int status;
  2262. #ifdef ENABLE_REMOTE
  2263. char host[PCAP_BUF_SIZE + 1];
  2264. char port[PCAP_BUF_SIZE + 1];
  2265. char name[PCAP_BUF_SIZE + 1];
  2266. int srctype;
  2267. /*
  2268. * Retrofit - we have to make older applications compatible with
  2269. * remote capture.
  2270. * So we're calling pcap_open_remote() from here; this is a very
  2271. * dirty hack.
  2272. * Obviously, we cannot exploit all the new features; for instance,
  2273. * we cannot send authentication, we cannot use a UDP data connection,
  2274. * and so on.
  2275. */
  2276. if (pcap_parsesrcstr(device, &srctype, host, port, name, errbuf))
  2277. return (NULL);
  2278. if (srctype == PCAP_SRC_IFREMOTE) {
  2279. /*
  2280. * Although we already have host, port and iface, we prefer
  2281. * to pass only 'device' to pcap_open_rpcap(), so that it has
  2282. * to call pcap_parsesrcstr() again.
  2283. * This is less optimized, but much clearer.
  2284. */
  2285. return (pcap_open_rpcap(device, snaplen,
  2286. promisc ? PCAP_OPENFLAG_PROMISCUOUS : 0, to_ms,
  2287. NULL, errbuf));
  2288. }
  2289. if (srctype == PCAP_SRC_FILE) {
  2290. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown URL scheme \"file\"");
  2291. return (NULL);
  2292. }
  2293. if (srctype == PCAP_SRC_IFLOCAL) {
  2294. /*
  2295. * If it starts with rpcap://, that refers to a local device
  2296. * (no host part in the URL). Remove the rpcap://, and
  2297. * fall through to the regular open path.
  2298. */
  2299. if (strncmp(device, PCAP_SRC_IF_STRING, strlen(PCAP_SRC_IF_STRING)) == 0) {
  2300. size_t len = strlen(device) - strlen(PCAP_SRC_IF_STRING) + 1;
  2301. if (len > 0)
  2302. device += strlen(PCAP_SRC_IF_STRING);
  2303. }
  2304. }
  2305. #endif /* ENABLE_REMOTE */
  2306. p = pcap_create(device, errbuf);
  2307. if (p == NULL)
  2308. return (NULL);
  2309. status = pcap_set_snaplen(p, snaplen);
  2310. if (status < 0)
  2311. goto fail;
  2312. status = pcap_set_promisc(p, promisc);
  2313. if (status < 0)
  2314. goto fail;
  2315. status = pcap_set_timeout(p, to_ms);
  2316. if (status < 0)
  2317. goto fail;
  2318. /*
  2319. * Mark this as opened with pcap_open_live(), so that, for
  2320. * example, we show the full list of DLT_ values, rather
  2321. * than just the ones that are compatible with capturing
  2322. * when not in monitor mode. That allows existing applications
  2323. * to work the way they used to work, but allows new applications
  2324. * that know about the new open API to, for example, find out the
  2325. * DLT_ values that they can select without changing whether
  2326. * the adapter is in monitor mode or not.
  2327. */
  2328. p->oldstyle = 1;
  2329. status = pcap_activate(p);
  2330. if (status < 0)
  2331. goto fail;
  2332. return (p);
  2333. fail:
  2334. if (status == PCAP_ERROR)
  2335. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", device,
  2336. p->errbuf);
  2337. else if (status == PCAP_ERROR_NO_SUCH_DEVICE ||
  2338. status == PCAP_ERROR_PERM_DENIED ||
  2339. status == PCAP_ERROR_PROMISC_PERM_DENIED)
  2340. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%s)", device,
  2341. pcap_statustostr(status), p->errbuf);
  2342. else
  2343. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", device,
  2344. pcap_statustostr(status));
  2345. pcap_close(p);
  2346. return (NULL);
  2347. }
  2348. pcap_t *
  2349. pcap_open_offline_common(char *ebuf, size_t size)
  2350. {
  2351. pcap_t *p;
  2352. p = pcap_alloc_pcap_t(ebuf, size);
  2353. if (p == NULL)
  2354. return (NULL);
  2355. p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
  2356. return (p);
  2357. }
  2358. int
  2359. pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  2360. {
  2361. return (p->read_op(p, cnt, callback, user));
  2362. }
  2363. int
  2364. pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  2365. {
  2366. register int n;
  2367. for (;;) {
  2368. if (p->rfile != NULL) {
  2369. /*
  2370. * 0 means EOF, so don't loop if we get 0.
  2371. */
  2372. n = pcap_offline_read(p, cnt, callback, user);
  2373. } else {
  2374. /*
  2375. * XXX keep reading until we get something
  2376. * (or an error occurs)
  2377. */
  2378. do {
  2379. n = p->read_op(p, cnt, callback, user);
  2380. } while (n == 0);
  2381. }
  2382. if (n <= 0)
  2383. return (n);
  2384. if (!PACKET_COUNT_IS_UNLIMITED(cnt)) {
  2385. cnt -= n;
  2386. if (cnt <= 0)
  2387. return (0);
  2388. }
  2389. }
  2390. }
  2391. /*
  2392. * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
  2393. */
  2394. void
  2395. pcap_breakloop(pcap_t *p)
  2396. {
  2397. p->break_loop = 1;
  2398. }
  2399. int
  2400. pcap_datalink(pcap_t *p)
  2401. {
  2402. if (!p->activated)
  2403. return (PCAP_ERROR_NOT_ACTIVATED);
  2404. return (p->linktype);
  2405. }
  2406. int
  2407. pcap_datalink_ext(pcap_t *p)
  2408. {
  2409. if (!p->activated)
  2410. return (PCAP_ERROR_NOT_ACTIVATED);
  2411. return (p->linktype_ext);
  2412. }
  2413. int
  2414. pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
  2415. {
  2416. if (!p->activated)
  2417. return (PCAP_ERROR_NOT_ACTIVATED);
  2418. if (p->dlt_count == 0) {
  2419. /*
  2420. * We couldn't fetch the list of DLTs, which means
  2421. * this platform doesn't support changing the
  2422. * DLT for an interface. Return a list of DLTs
  2423. * containing only the DLT this device supports.
  2424. */
  2425. *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
  2426. if (*dlt_buffer == NULL) {
  2427. pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
  2428. errno, "malloc");
  2429. return (PCAP_ERROR);
  2430. }
  2431. **dlt_buffer = p->linktype;
  2432. return (1);
  2433. } else {
  2434. *dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count);
  2435. if (*dlt_buffer == NULL) {
  2436. pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
  2437. errno, "malloc");
  2438. return (PCAP_ERROR);
  2439. }
  2440. (void)memcpy(*dlt_buffer, p->dlt_list,
  2441. sizeof(**dlt_buffer) * p->dlt_count);
  2442. return (p->dlt_count);
  2443. }
  2444. }
  2445. /*
  2446. * In Windows, you might have a library built with one version of the
  2447. * C runtime library and an application built with another version of
  2448. * the C runtime library, which means that the library might use one
  2449. * version of malloc() and free() and the application might use another
  2450. * version of malloc() and free(). If so, that means something
  2451. * allocated by the library cannot be freed by the application, so we
  2452. * need to have a pcap_free_datalinks() routine to free up the list
  2453. * allocated by pcap_list_datalinks(), even though it's just a wrapper
  2454. * around free().
  2455. */
  2456. void
  2457. pcap_free_datalinks(int *dlt_list)
  2458. {
  2459. free(dlt_list);
  2460. }
  2461. int
  2462. pcap_set_datalink(pcap_t *p, int dlt)
  2463. {
  2464. int i;
  2465. const char *dlt_name;
  2466. if (dlt < 0)
  2467. goto unsupported;
  2468. if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
  2469. /*
  2470. * We couldn't fetch the list of DLTs, or we don't
  2471. * have a "set datalink" operation, which means
  2472. * this platform doesn't support changing the
  2473. * DLT for an interface. Check whether the new
  2474. * DLT is the one this interface supports.
  2475. */
  2476. if (p->linktype != dlt)
  2477. goto unsupported;
  2478. /*
  2479. * It is, so there's nothing we need to do here.
  2480. */
  2481. return (0);
  2482. }
  2483. for (i = 0; i < p->dlt_count; i++)
  2484. if (p->dlt_list[i] == (u_int)dlt)
  2485. break;
  2486. if (i >= p->dlt_count)
  2487. goto unsupported;
  2488. if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB &&
  2489. dlt == DLT_DOCSIS) {
  2490. /*
  2491. * This is presumably an Ethernet device, as the first
  2492. * link-layer type it offers is DLT_EN10MB, and the only
  2493. * other type it offers is DLT_DOCSIS. That means that
  2494. * we can't tell the driver to supply DOCSIS link-layer
  2495. * headers - we're just pretending that's what we're
  2496. * getting, as, presumably, we're capturing on a dedicated
  2497. * link to a Cisco Cable Modem Termination System, and
  2498. * it's putting raw DOCSIS frames on the wire inside low-level
  2499. * Ethernet framing.
  2500. */
  2501. p->linktype = dlt;
  2502. return (0);
  2503. }
  2504. if (p->set_datalink_op(p, dlt) == -1)
  2505. return (-1);
  2506. p->linktype = dlt;
  2507. return (0);
  2508. unsupported:
  2509. dlt_name = pcap_datalink_val_to_name(dlt);
  2510. if (dlt_name != NULL) {
  2511. (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf),
  2512. "%s is not one of the DLTs supported by this device",
  2513. dlt_name);
  2514. } else {
  2515. (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf),
  2516. "DLT %d is not one of the DLTs supported by this device",
  2517. dlt);
  2518. }
  2519. return (-1);
  2520. }
  2521. /*
  2522. * This array is designed for mapping upper and lower case letter
  2523. * together for a case independent comparison. The mappings are
  2524. * based upon ascii character sequences.
  2525. */
  2526. static const u_char charmap[] = {
  2527. (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
  2528. (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
  2529. (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
  2530. (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
  2531. (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
  2532. (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
  2533. (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
  2534. (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
  2535. (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
  2536. (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
  2537. (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
  2538. (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
  2539. (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
  2540. (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
  2541. (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
  2542. (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
  2543. (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
  2544. (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
  2545. (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
  2546. (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
  2547. (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
  2548. (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
  2549. (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
  2550. (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
  2551. (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
  2552. (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
  2553. (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
  2554. (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
  2555. (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
  2556. (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
  2557. (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
  2558. (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
  2559. (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
  2560. (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
  2561. (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
  2562. (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
  2563. (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
  2564. (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
  2565. (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
  2566. (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
  2567. (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
  2568. (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
  2569. (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
  2570. (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
  2571. (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
  2572. (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
  2573. (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
  2574. (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
  2575. (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
  2576. (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
  2577. (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
  2578. (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
  2579. (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
  2580. (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
  2581. (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
  2582. (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
  2583. (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
  2584. (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
  2585. (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
  2586. (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
  2587. (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
  2588. (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
  2589. (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
  2590. (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
  2591. };
  2592. int
  2593. pcap_strcasecmp(const char *s1, const char *s2)
  2594. {
  2595. register const u_char *cm = charmap,
  2596. *us1 = (const u_char *)s1,
  2597. *us2 = (const u_char *)s2;
  2598. while (cm[*us1] == cm[*us2++])
  2599. if (*us1++ == '\0')
  2600. return(0);
  2601. return (cm[*us1] - cm[*--us2]);
  2602. }
  2603. struct dlt_choice {
  2604. const char *name;
  2605. const char *description;
  2606. int dlt;
  2607. };
  2608. #define DLT_CHOICE(code, description) { #code, description, DLT_ ## code }
  2609. #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
  2610. static struct dlt_choice dlt_choices[] = {
  2611. DLT_CHOICE(NULL, "BSD loopback"),
  2612. DLT_CHOICE(EN10MB, "Ethernet"),
  2613. DLT_CHOICE(IEEE802, "Token ring"),
  2614. DLT_CHOICE(ARCNET, "BSD ARCNET"),
  2615. DLT_CHOICE(SLIP, "SLIP"),
  2616. DLT_CHOICE(PPP, "PPP"),
  2617. DLT_CHOICE(FDDI, "FDDI"),
  2618. DLT_CHOICE(ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"),
  2619. DLT_CHOICE(RAW, "Raw IP"),
  2620. DLT_CHOICE(SLIP_BSDOS, "BSD/OS SLIP"),
  2621. DLT_CHOICE(PPP_BSDOS, "BSD/OS PPP"),
  2622. DLT_CHOICE(ATM_CLIP, "Linux Classical IP-over-ATM"),
  2623. DLT_CHOICE(PPP_SERIAL, "PPP over serial"),
  2624. DLT_CHOICE(PPP_ETHER, "PPPoE"),
  2625. DLT_CHOICE(SYMANTEC_FIREWALL, "Symantec Firewall"),
  2626. DLT_CHOICE(C_HDLC, "Cisco HDLC"),
  2627. DLT_CHOICE(IEEE802_11, "802.11"),
  2628. DLT_CHOICE(FRELAY, "Frame Relay"),
  2629. DLT_CHOICE(LOOP, "OpenBSD loopback"),
  2630. DLT_CHOICE(ENC, "OpenBSD encapsulated IP"),
  2631. DLT_CHOICE(LINUX_SLL, "Linux cooked"),
  2632. DLT_CHOICE(LTALK, "Localtalk"),
  2633. DLT_CHOICE(PFLOG, "OpenBSD pflog file"),
  2634. DLT_CHOICE(PFSYNC, "Packet filter state syncing"),
  2635. DLT_CHOICE(PRISM_HEADER, "802.11 plus Prism header"),
  2636. DLT_CHOICE(IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
  2637. DLT_CHOICE(SUNATM, "Sun raw ATM"),
  2638. DLT_CHOICE(IEEE802_11_RADIO, "802.11 plus radiotap header"),
  2639. DLT_CHOICE(ARCNET_LINUX, "Linux ARCNET"),
  2640. DLT_CHOICE(JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
  2641. DLT_CHOICE(JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
  2642. DLT_CHOICE(JUNIPER_ES, "Juniper Encryption Services PIC"),
  2643. DLT_CHOICE(JUNIPER_GGSN, "Juniper GGSN PIC"),
  2644. DLT_CHOICE(JUNIPER_MFR, "Juniper FRF.16 Frame Relay"),
  2645. DLT_CHOICE(JUNIPER_ATM2, "Juniper ATM2 PIC"),
  2646. DLT_CHOICE(JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
  2647. DLT_CHOICE(JUNIPER_ATM1, "Juniper ATM1 PIC"),
  2648. DLT_CHOICE(APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
  2649. DLT_CHOICE(MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"),
  2650. DLT_CHOICE(MTP2, "SS7 MTP2"),
  2651. DLT_CHOICE(MTP3, "SS7 MTP3"),
  2652. DLT_CHOICE(SCCP, "SS7 SCCP"),
  2653. DLT_CHOICE(DOCSIS, "DOCSIS"),
  2654. DLT_CHOICE(LINUX_IRDA, "Linux IrDA"),
  2655. DLT_CHOICE(IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
  2656. DLT_CHOICE(JUNIPER_MONITOR, "Juniper Passive Monitor PIC"),
  2657. DLT_CHOICE(BACNET_MS_TP, "BACnet MS/TP"),
  2658. DLT_CHOICE(PPP_PPPD, "PPP for pppd, with direction flag"),
  2659. DLT_CHOICE(JUNIPER_PPPOE, "Juniper PPPoE"),
  2660. DLT_CHOICE(JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"),
  2661. DLT_CHOICE(GPRS_LLC, "GPRS LLC"),
  2662. DLT_CHOICE(GPF_T, "GPF-T"),
  2663. DLT_CHOICE(GPF_F, "GPF-F"),
  2664. DLT_CHOICE(JUNIPER_PIC_PEER, "Juniper PIC Peer"),
  2665. DLT_CHOICE(ERF_ETH, "Ethernet with Endace ERF header"),
  2666. DLT_CHOICE(ERF_POS, "Packet-over-SONET with Endace ERF header"),
  2667. DLT_CHOICE(LINUX_LAPD, "Linux vISDN LAPD"),
  2668. DLT_CHOICE(JUNIPER_ETHER, "Juniper Ethernet"),
  2669. DLT_CHOICE(JUNIPER_PPP, "Juniper PPP"),
  2670. DLT_CHOICE(JUNIPER_FRELAY, "Juniper Frame Relay"),
  2671. DLT_CHOICE(JUNIPER_CHDLC, "Juniper C-HDLC"),
  2672. DLT_CHOICE(MFR, "FRF.16 Frame Relay"),
  2673. DLT_CHOICE(JUNIPER_VP, "Juniper Voice PIC"),
  2674. DLT_CHOICE(A429, "Arinc 429"),
  2675. DLT_CHOICE(A653_ICM, "Arinc 653 Interpartition Communication"),
  2676. DLT_CHOICE(USB_FREEBSD, "USB with FreeBSD header"),
  2677. DLT_CHOICE(BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"),
  2678. DLT_CHOICE(IEEE802_16_MAC_CPS, "IEEE 802.16 MAC Common Part Sublayer"),
  2679. DLT_CHOICE(USB_LINUX, "USB with Linux header"),
  2680. DLT_CHOICE(CAN20B, "Controller Area Network (CAN) v. 2.0B"),
  2681. DLT_CHOICE(IEEE802_15_4_LINUX, "IEEE 802.15.4 with Linux padding"),
  2682. DLT_CHOICE(PPI, "Per-Packet Information"),
  2683. DLT_CHOICE(IEEE802_16_MAC_CPS_RADIO, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"),
  2684. DLT_CHOICE(JUNIPER_ISM, "Juniper Integrated Service Module"),
  2685. DLT_CHOICE(IEEE802_15_4, "IEEE 802.15.4 with FCS"),
  2686. DLT_CHOICE(SITA, "SITA pseudo-header"),
  2687. DLT_CHOICE(ERF, "Endace ERF header"),
  2688. DLT_CHOICE(RAIF1, "Ethernet with u10 Networks pseudo-header"),
  2689. DLT_CHOICE(IPMB, "IPMB"),
  2690. DLT_CHOICE(JUNIPER_ST, "Juniper Secure Tunnel"),
  2691. DLT_CHOICE(BLUETOOTH_HCI_H4_WITH_PHDR, "Bluetooth HCI UART transport layer plus pseudo-header"),
  2692. DLT_CHOICE(AX25_KISS, "AX.25 with KISS header"),
  2693. DLT_CHOICE(IEEE802_15_4_NONASK_PHY, "IEEE 802.15.4 with non-ASK PHY data"),
  2694. DLT_CHOICE(MPLS, "MPLS with label as link-layer header"),
  2695. DLT_CHOICE(LINUX_EVDEV, "Linux evdev events"),
  2696. DLT_CHOICE(USB_LINUX_MMAPPED, "USB with padded Linux header"),
  2697. DLT_CHOICE(DECT, "DECT"),
  2698. DLT_CHOICE(AOS, "AOS Space Data Link protocol"),
  2699. DLT_CHOICE(WIHART, "Wireless HART"),
  2700. DLT_CHOICE(FC_2, "Fibre Channel FC-2"),
  2701. DLT_CHOICE(FC_2_WITH_FRAME_DELIMS, "Fibre Channel FC-2 with frame delimiters"),
  2702. DLT_CHOICE(IPNET, "Solaris ipnet"),
  2703. DLT_CHOICE(CAN_SOCKETCAN, "CAN-bus with SocketCAN headers"),
  2704. DLT_CHOICE(IPV4, "Raw IPv4"),
  2705. DLT_CHOICE(IPV6, "Raw IPv6"),
  2706. DLT_CHOICE(IEEE802_15_4_NOFCS, "IEEE 802.15.4 without FCS"),
  2707. DLT_CHOICE(DBUS, "D-Bus"),
  2708. DLT_CHOICE(JUNIPER_VS, "Juniper Virtual Server"),
  2709. DLT_CHOICE(JUNIPER_SRX_E2E, "Juniper SRX E2E"),
  2710. DLT_CHOICE(JUNIPER_FIBRECHANNEL, "Juniper Fibre Channel"),
  2711. DLT_CHOICE(DVB_CI, "DVB-CI"),
  2712. DLT_CHOICE(MUX27010, "MUX27010"),
  2713. DLT_CHOICE(STANAG_5066_D_PDU, "STANAG 5066 D_PDUs"),
  2714. DLT_CHOICE(JUNIPER_ATM_CEMIC, "Juniper ATM CEMIC"),
  2715. DLT_CHOICE(NFLOG, "Linux netfilter log messages"),
  2716. DLT_CHOICE(NETANALYZER, "Ethernet with Hilscher netANALYZER pseudo-header"),
  2717. DLT_CHOICE(NETANALYZER_TRANSPARENT, "Ethernet with Hilscher netANALYZER pseudo-header and with preamble and SFD"),
  2718. DLT_CHOICE(IPOIB, "RFC 4391 IP-over-Infiniband"),
  2719. DLT_CHOICE(MPEG_2_TS, "MPEG-2 transport stream"),
  2720. DLT_CHOICE(NG40, "ng40 protocol tester Iub/Iur"),
  2721. DLT_CHOICE(NFC_LLCP, "NFC LLCP PDUs with pseudo-header"),
  2722. DLT_CHOICE(INFINIBAND, "InfiniBand"),
  2723. DLT_CHOICE(SCTP, "SCTP"),
  2724. DLT_CHOICE(USBPCAP, "USB with USBPcap header"),
  2725. DLT_CHOICE(RTAC_SERIAL, "Schweitzer Engineering Laboratories RTAC packets"),
  2726. DLT_CHOICE(BLUETOOTH_LE_LL, "Bluetooth Low Energy air interface"),
  2727. DLT_CHOICE(NETLINK, "Linux netlink"),
  2728. DLT_CHOICE(BLUETOOTH_LINUX_MONITOR, "Bluetooth Linux Monitor"),
  2729. DLT_CHOICE(BLUETOOTH_BREDR_BB, "Bluetooth Basic Rate/Enhanced Data Rate baseband packets"),
  2730. DLT_CHOICE(BLUETOOTH_LE_LL_WITH_PHDR, "Bluetooth Low Energy air interface with pseudo-header"),
  2731. DLT_CHOICE(PROFIBUS_DL, "PROFIBUS data link layer"),
  2732. DLT_CHOICE(PKTAP, "Apple DLT_PKTAP"),
  2733. DLT_CHOICE(EPON, "Ethernet with 802.3 Clause 65 EPON preamble"),
  2734. DLT_CHOICE(IPMI_HPM_2, "IPMI trace packets"),
  2735. DLT_CHOICE(ZWAVE_R1_R2, "Z-Wave RF profile R1 and R2 packets"),
  2736. DLT_CHOICE(ZWAVE_R3, "Z-Wave RF profile R3 packets"),
  2737. DLT_CHOICE(WATTSTOPPER_DLM, "WattStopper Digital Lighting Management (DLM) and Legrand Nitoo Open protocol"),
  2738. DLT_CHOICE(ISO_14443, "ISO 14443 messages"),
  2739. DLT_CHOICE(RDS, "IEC 62106 Radio Data System groups"),
  2740. DLT_CHOICE(USB_DARWIN, "USB with Darwin header"),
  2741. DLT_CHOICE(OPENFLOW, "OpenBSD DLT_OPENFLOW"),
  2742. DLT_CHOICE(SDLC, "IBM SDLC frames"),
  2743. DLT_CHOICE(TI_LLN_SNIFFER, "TI LLN sniffer frames"),
  2744. DLT_CHOICE(VSOCK, "Linux vsock"),
  2745. DLT_CHOICE(NORDIC_BLE, "Nordic Semiconductor Bluetooth LE sniffer frames"),
  2746. DLT_CHOICE(DOCSIS31_XRA31, "Excentis XRA-31 DOCSIS 3.1 RF sniffer frames"),
  2747. DLT_CHOICE(ETHERNET_MPACKET, "802.3br mPackets"),
  2748. DLT_CHOICE(DISPLAYPORT_AUX, "DisplayPort AUX channel monitoring data"),
  2749. DLT_CHOICE_SENTINEL
  2750. };
  2751. int
  2752. pcap_datalink_name_to_val(const char *name)
  2753. {
  2754. int i;
  2755. for (i = 0; dlt_choices[i].name != NULL; i++) {
  2756. if (pcap_strcasecmp(dlt_choices[i].name, name) == 0)
  2757. return (dlt_choices[i].dlt);
  2758. }
  2759. return (-1);
  2760. }
  2761. const char *
  2762. pcap_datalink_val_to_name(int dlt)
  2763. {
  2764. int i;
  2765. for (i = 0; dlt_choices[i].name != NULL; i++) {
  2766. if (dlt_choices[i].dlt == dlt)
  2767. return (dlt_choices[i].name);
  2768. }
  2769. return (NULL);
  2770. }
  2771. const char *
  2772. pcap_datalink_val_to_description(int dlt)
  2773. {
  2774. int i;
  2775. for (i = 0; dlt_choices[i].name != NULL; i++) {
  2776. if (dlt_choices[i].dlt == dlt)
  2777. return (dlt_choices[i].description);
  2778. }
  2779. return (NULL);
  2780. }
  2781. struct tstamp_type_choice {
  2782. const char *name;
  2783. const char *description;
  2784. int type;
  2785. };
  2786. static struct tstamp_type_choice tstamp_type_choices[] = {
  2787. { "host", "Host", PCAP_TSTAMP_HOST },
  2788. { "host_lowprec", "Host, low precision", PCAP_TSTAMP_HOST_LOWPREC },
  2789. { "host_hiprec", "Host, high precision", PCAP_TSTAMP_HOST_HIPREC },
  2790. { "adapter", "Adapter", PCAP_TSTAMP_ADAPTER },
  2791. { "adapter_unsynced", "Adapter, not synced with system time", PCAP_TSTAMP_ADAPTER_UNSYNCED },
  2792. { NULL, NULL, 0 }
  2793. };
  2794. int
  2795. pcap_tstamp_type_name_to_val(const char *name)
  2796. {
  2797. int i;
  2798. for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
  2799. if (pcap_strcasecmp(tstamp_type_choices[i].name, name) == 0)
  2800. return (tstamp_type_choices[i].type);
  2801. }
  2802. return (PCAP_ERROR);
  2803. }
  2804. const char *
  2805. pcap_tstamp_type_val_to_name(int tstamp_type)
  2806. {
  2807. int i;
  2808. for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
  2809. if (tstamp_type_choices[i].type == tstamp_type)
  2810. return (tstamp_type_choices[i].name);
  2811. }
  2812. return (NULL);
  2813. }
  2814. const char *
  2815. pcap_tstamp_type_val_to_description(int tstamp_type)
  2816. {
  2817. int i;
  2818. for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
  2819. if (tstamp_type_choices[i].type == tstamp_type)
  2820. return (tstamp_type_choices[i].description);
  2821. }
  2822. return (NULL);
  2823. }
  2824. int
  2825. pcap_snapshot(pcap_t *p)
  2826. {
  2827. if (!p->activated)
  2828. return (PCAP_ERROR_NOT_ACTIVATED);
  2829. return (p->snapshot);
  2830. }
  2831. int
  2832. pcap_is_swapped(pcap_t *p)
  2833. {
  2834. if (!p->activated)
  2835. return (PCAP_ERROR_NOT_ACTIVATED);
  2836. return (p->swapped);
  2837. }
  2838. int
  2839. pcap_major_version(pcap_t *p)
  2840. {
  2841. if (!p->activated)
  2842. return (PCAP_ERROR_NOT_ACTIVATED);
  2843. return (p->version_major);
  2844. }
  2845. int
  2846. pcap_minor_version(pcap_t *p)
  2847. {
  2848. if (!p->activated)
  2849. return (PCAP_ERROR_NOT_ACTIVATED);
  2850. return (p->version_minor);
  2851. }
  2852. int
  2853. pcap_bufsize(pcap_t *p)
  2854. {
  2855. if (!p->activated)
  2856. return (PCAP_ERROR_NOT_ACTIVATED);
  2857. return (p->bufsize);
  2858. }
  2859. FILE *
  2860. pcap_file(pcap_t *p)
  2861. {
  2862. return (p->rfile);
  2863. }
  2864. int
  2865. pcap_fileno(pcap_t *p)
  2866. {
  2867. #ifndef _WIN32
  2868. return (p->fd);
  2869. #else
  2870. if (p->handle != INVALID_HANDLE_VALUE)
  2871. return ((int)(DWORD)p->handle);
  2872. else
  2873. return (PCAP_ERROR);
  2874. #endif
  2875. }
  2876. #if !defined(_WIN32) && !defined(MSDOS)
  2877. int
  2878. pcap_get_selectable_fd(pcap_t *p)
  2879. {
  2880. return (p->selectable_fd);
  2881. }
  2882. struct timeval *
  2883. pcap_get_required_select_timeout(pcap_t *p)
  2884. {
  2885. return (p->required_select_timeout);
  2886. }
  2887. #endif
  2888. void
  2889. pcap_perror(pcap_t *p, const char *prefix)
  2890. {
  2891. fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
  2892. }
  2893. char *
  2894. pcap_geterr(pcap_t *p)
  2895. {
  2896. return (p->errbuf);
  2897. }
  2898. int
  2899. pcap_getnonblock(pcap_t *p, char *errbuf)
  2900. {
  2901. int ret;
  2902. ret = p->getnonblock_op(p);
  2903. if (ret == -1) {
  2904. /*
  2905. * The get nonblock operation sets p->errbuf; this
  2906. * function *shouldn't* have had a separate errbuf
  2907. * argument, as it didn't need one, but I goofed
  2908. * when adding it.
  2909. *
  2910. * We copy the error message to errbuf, so callers
  2911. * can find it in either place.
  2912. */
  2913. strlcpy(errbuf, p->errbuf, PCAP_ERRBUF_SIZE);
  2914. }
  2915. return (ret);
  2916. }
  2917. /*
  2918. * Get the current non-blocking mode setting, under the assumption that
  2919. * it's just the standard POSIX non-blocking flag.
  2920. */
  2921. #if !defined(_WIN32) && !defined(MSDOS)
  2922. int
  2923. pcap_getnonblock_fd(pcap_t *p)
  2924. {
  2925. int fdflags;
  2926. fdflags = fcntl(p->fd, F_GETFL, 0);
  2927. if (fdflags == -1) {
  2928. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2929. errno, "F_GETFL");
  2930. return (-1);
  2931. }
  2932. if (fdflags & O_NONBLOCK)
  2933. return (1);
  2934. else
  2935. return (0);
  2936. }
  2937. #endif
  2938. int
  2939. pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
  2940. {
  2941. int ret;
  2942. ret = p->setnonblock_op(p, nonblock);
  2943. if (ret == -1) {
  2944. /*
  2945. * The set nonblock operation sets p->errbuf; this
  2946. * function *shouldn't* have had a separate errbuf
  2947. * argument, as it didn't need one, but I goofed
  2948. * when adding it.
  2949. *
  2950. * We copy the error message to errbuf, so callers
  2951. * can find it in either place.
  2952. */
  2953. strlcpy(errbuf, p->errbuf, PCAP_ERRBUF_SIZE);
  2954. }
  2955. return (ret);
  2956. }
  2957. #if !defined(_WIN32) && !defined(MSDOS)
  2958. /*
  2959. * Set non-blocking mode, under the assumption that it's just the
  2960. * standard POSIX non-blocking flag. (This can be called by the
  2961. * per-platform non-blocking-mode routine if that routine also
  2962. * needs to do some additional work.)
  2963. */
  2964. int
  2965. pcap_setnonblock_fd(pcap_t *p, int nonblock)
  2966. {
  2967. int fdflags;
  2968. fdflags = fcntl(p->fd, F_GETFL, 0);
  2969. if (fdflags == -1) {
  2970. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2971. errno, "F_GETFL");
  2972. return (-1);
  2973. }
  2974. if (nonblock)
  2975. fdflags |= O_NONBLOCK;
  2976. else
  2977. fdflags &= ~O_NONBLOCK;
  2978. if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
  2979. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2980. errno, "F_SETFL");
  2981. return (-1);
  2982. }
  2983. return (0);
  2984. }
  2985. #endif
  2986. #ifdef _WIN32
  2987. /*
  2988. * Generate a string for a Win32-specific error (i.e. an error generated when
  2989. * calling a Win32 API).
  2990. * For errors occurred during standard C calls, we still use pcap_strerror()
  2991. */
  2992. void
  2993. pcap_win32_err_to_str(DWORD error, char *errbuf)
  2994. {
  2995. size_t errlen;
  2996. char *p;
  2997. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
  2998. PCAP_ERRBUF_SIZE, NULL);
  2999. /*
  3000. * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
  3001. * message. Get rid of it.
  3002. */
  3003. errlen = strlen(errbuf);
  3004. if (errlen >= 2) {
  3005. errbuf[errlen - 1] = '\0';
  3006. errbuf[errlen - 2] = '\0';
  3007. }
  3008. p = strchr(errbuf, '\0');
  3009. pcap_snprintf (p, PCAP_ERRBUF_SIZE+1-(p-errbuf), " (%lu)", error);
  3010. }
  3011. #endif
  3012. /*
  3013. * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values.
  3014. */
  3015. const char *
  3016. pcap_statustostr(int errnum)
  3017. {
  3018. static char ebuf[15+10+1];
  3019. switch (errnum) {
  3020. case PCAP_WARNING:
  3021. return("Generic warning");
  3022. case PCAP_WARNING_TSTAMP_TYPE_NOTSUP:
  3023. return ("That type of time stamp is not supported by that device");
  3024. case PCAP_WARNING_PROMISC_NOTSUP:
  3025. return ("That device doesn't support promiscuous mode");
  3026. case PCAP_ERROR:
  3027. return("Generic error");
  3028. case PCAP_ERROR_BREAK:
  3029. return("Loop terminated by pcap_breakloop");
  3030. case PCAP_ERROR_NOT_ACTIVATED:
  3031. return("The pcap_t has not been activated");
  3032. case PCAP_ERROR_ACTIVATED:
  3033. return ("The setting can't be changed after the pcap_t is activated");
  3034. case PCAP_ERROR_NO_SUCH_DEVICE:
  3035. return ("No such device exists");
  3036. case PCAP_ERROR_RFMON_NOTSUP:
  3037. return ("That device doesn't support monitor mode");
  3038. case PCAP_ERROR_NOT_RFMON:
  3039. return ("That operation is supported only in monitor mode");
  3040. case PCAP_ERROR_PERM_DENIED:
  3041. return ("You don't have permission to capture on that device");
  3042. case PCAP_ERROR_IFACE_NOT_UP:
  3043. return ("That device is not up");
  3044. case PCAP_ERROR_CANTSET_TSTAMP_TYPE:
  3045. return ("That device doesn't support setting the time stamp type");
  3046. case PCAP_ERROR_PROMISC_PERM_DENIED:
  3047. return ("You don't have permission to capture in promiscuous mode on that device");
  3048. case PCAP_ERROR_TSTAMP_PRECISION_NOTSUP:
  3049. return ("That device doesn't support that time stamp precision");
  3050. }
  3051. (void)pcap_snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
  3052. return(ebuf);
  3053. }
  3054. /*
  3055. * Not all systems have strerror().
  3056. */
  3057. const char *
  3058. pcap_strerror(int errnum)
  3059. {
  3060. #ifdef HAVE_STRERROR
  3061. #ifdef _WIN32
  3062. static char errbuf[PCAP_ERRBUF_SIZE];
  3063. errno_t err = strerror_s(errbuf, PCAP_ERRBUF_SIZE, errnum);
  3064. if (err != 0) /* err = 0 if successful */
  3065. strlcpy(errbuf, "strerror_s() error", PCAP_ERRBUF_SIZE);
  3066. return (errbuf);
  3067. #else
  3068. return (strerror(errnum));
  3069. #endif /* _WIN32 */
  3070. #else
  3071. extern int sys_nerr;
  3072. extern const char *const sys_errlist[];
  3073. static char errbuf[PCAP_ERRBUF_SIZE];
  3074. if ((unsigned int)errnum < sys_nerr)
  3075. return ((char *)sys_errlist[errnum]);
  3076. (void)pcap_snprintf(errbuf, sizeof errbuf, "Unknown error: %d", errnum);
  3077. return (errbuf);
  3078. #endif
  3079. }
  3080. int
  3081. pcap_setfilter(pcap_t *p, struct bpf_program *fp)
  3082. {
  3083. return (p->setfilter_op(p, fp));
  3084. }
  3085. /*
  3086. * Set direction flag, which controls whether we accept only incoming
  3087. * packets, only outgoing packets, or both.
  3088. * Note that, depending on the platform, some or all direction arguments
  3089. * might not be supported.
  3090. */
  3091. int
  3092. pcap_setdirection(pcap_t *p, pcap_direction_t d)
  3093. {
  3094. if (p->setdirection_op == NULL) {
  3095. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3096. "Setting direction is not implemented on this platform");
  3097. return (-1);
  3098. } else
  3099. return (p->setdirection_op(p, d));
  3100. }
  3101. int
  3102. pcap_stats(pcap_t *p, struct pcap_stat *ps)
  3103. {
  3104. return (p->stats_op(p, ps));
  3105. }
  3106. #ifdef _WIN32
  3107. struct pcap_stat *
  3108. pcap_stats_ex(pcap_t *p, int *pcap_stat_size)
  3109. {
  3110. return (p->stats_ex_op(p, pcap_stat_size));
  3111. }
  3112. int
  3113. pcap_setbuff(pcap_t *p, int dim)
  3114. {
  3115. return (p->setbuff_op(p, dim));
  3116. }
  3117. int
  3118. pcap_setmode(pcap_t *p, int mode)
  3119. {
  3120. return (p->setmode_op(p, mode));
  3121. }
  3122. int
  3123. pcap_setmintocopy(pcap_t *p, int size)
  3124. {
  3125. return (p->setmintocopy_op(p, size));
  3126. }
  3127. HANDLE
  3128. pcap_getevent(pcap_t *p)
  3129. {
  3130. return (p->getevent_op(p));
  3131. }
  3132. int
  3133. pcap_oid_get_request(pcap_t *p, bpf_u_int32 oid, void *data, size_t *lenp)
  3134. {
  3135. return (p->oid_get_request_op(p, oid, data, lenp));
  3136. }
  3137. int
  3138. pcap_oid_set_request(pcap_t *p, bpf_u_int32 oid, const void *data, size_t *lenp)
  3139. {
  3140. return (p->oid_set_request_op(p, oid, data, lenp));
  3141. }
  3142. pcap_send_queue *
  3143. pcap_sendqueue_alloc(u_int memsize)
  3144. {
  3145. pcap_send_queue *tqueue;
  3146. /* Allocate the queue */
  3147. tqueue = (pcap_send_queue *)malloc(sizeof(pcap_send_queue));
  3148. if (tqueue == NULL){
  3149. return (NULL);
  3150. }
  3151. /* Allocate the buffer */
  3152. tqueue->buffer = (char *)malloc(memsize);
  3153. if (tqueue->buffer == NULL) {
  3154. free(tqueue);
  3155. return (NULL);
  3156. }
  3157. tqueue->maxlen = memsize;
  3158. tqueue->len = 0;
  3159. return (tqueue);
  3160. }
  3161. void
  3162. pcap_sendqueue_destroy(pcap_send_queue *queue)
  3163. {
  3164. free(queue->buffer);
  3165. free(queue);
  3166. }
  3167. int
  3168. pcap_sendqueue_queue(pcap_send_queue *queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)
  3169. {
  3170. if (queue->len + sizeof(struct pcap_pkthdr) + pkt_header->caplen > queue->maxlen){
  3171. return (-1);
  3172. }
  3173. /* Copy the pcap_pkthdr header*/
  3174. memcpy(queue->buffer + queue->len, pkt_header, sizeof(struct pcap_pkthdr));
  3175. queue->len += sizeof(struct pcap_pkthdr);
  3176. /* copy the packet */
  3177. memcpy(queue->buffer + queue->len, pkt_data, pkt_header->caplen);
  3178. queue->len += pkt_header->caplen;
  3179. return (0);
  3180. }
  3181. u_int
  3182. pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue, int sync)
  3183. {
  3184. return (p->sendqueue_transmit_op(p, queue, sync));
  3185. }
  3186. int
  3187. pcap_setuserbuffer(pcap_t *p, int size)
  3188. {
  3189. return (p->setuserbuffer_op(p, size));
  3190. }
  3191. int
  3192. pcap_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks)
  3193. {
  3194. return (p->live_dump_op(p, filename, maxsize, maxpacks));
  3195. }
  3196. int
  3197. pcap_live_dump_ended(pcap_t *p, int sync)
  3198. {
  3199. return (p->live_dump_ended_op(p, sync));
  3200. }
  3201. PAirpcapHandle
  3202. pcap_get_airpcap_handle(pcap_t *p)
  3203. {
  3204. PAirpcapHandle handle;
  3205. handle = p->get_airpcap_handle_op(p);
  3206. if (handle == NULL) {
  3207. (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf),
  3208. "This isn't an AirPcap device");
  3209. }
  3210. return (handle);
  3211. }
  3212. #endif
  3213. /*
  3214. * On some platforms, we need to clean up promiscuous or monitor mode
  3215. * when we close a device - and we want that to happen even if the
  3216. * application just exits without explicitl closing devices.
  3217. * On those platforms, we need to register a "close all the pcaps"
  3218. * routine to be called when we exit, and need to maintain a list of
  3219. * pcaps that need to be closed to clean up modes.
  3220. *
  3221. * XXX - not thread-safe.
  3222. */
  3223. /*
  3224. * List of pcaps on which we've done something that needs to be
  3225. * cleaned up.
  3226. * If there are any such pcaps, we arrange to call "pcap_close_all()"
  3227. * when we exit, and have it close all of them.
  3228. */
  3229. static struct pcap *pcaps_to_close;
  3230. /*
  3231. * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
  3232. * be called on exit.
  3233. */
  3234. static int did_atexit;
  3235. static void
  3236. pcap_close_all(void)
  3237. {
  3238. struct pcap *handle;
  3239. while ((handle = pcaps_to_close) != NULL)
  3240. pcap_close(handle);
  3241. }
  3242. int
  3243. pcap_do_addexit(pcap_t *p)
  3244. {
  3245. /*
  3246. * If we haven't already done so, arrange to have
  3247. * "pcap_close_all()" called when we exit.
  3248. */
  3249. if (!did_atexit) {
  3250. if (atexit(pcap_close_all) != 0) {
  3251. /*
  3252. * "atexit()" failed; let our caller know.
  3253. */
  3254. strlcpy(p->errbuf, "atexit failed", PCAP_ERRBUF_SIZE);
  3255. return (0);
  3256. }
  3257. did_atexit = 1;
  3258. }
  3259. return (1);
  3260. }
  3261. void
  3262. pcap_add_to_pcaps_to_close(pcap_t *p)
  3263. {
  3264. p->next = pcaps_to_close;
  3265. pcaps_to_close = p;
  3266. }
  3267. void
  3268. pcap_remove_from_pcaps_to_close(pcap_t *p)
  3269. {
  3270. pcap_t *pc, *prevpc;
  3271. for (pc = pcaps_to_close, prevpc = NULL; pc != NULL;
  3272. prevpc = pc, pc = pc->next) {
  3273. if (pc == p) {
  3274. /*
  3275. * Found it. Remove it from the list.
  3276. */
  3277. if (prevpc == NULL) {
  3278. /*
  3279. * It was at the head of the list.
  3280. */
  3281. pcaps_to_close = pc->next;
  3282. } else {
  3283. /*
  3284. * It was in the middle of the list.
  3285. */
  3286. prevpc->next = pc->next;
  3287. }
  3288. break;
  3289. }
  3290. }
  3291. }
  3292. void
  3293. pcap_cleanup_live_common(pcap_t *p)
  3294. {
  3295. if (p->buffer != NULL) {
  3296. free(p->buffer);
  3297. p->buffer = NULL;
  3298. }
  3299. if (p->dlt_list != NULL) {
  3300. free(p->dlt_list);
  3301. p->dlt_list = NULL;
  3302. p->dlt_count = 0;
  3303. }
  3304. if (p->tstamp_type_list != NULL) {
  3305. free(p->tstamp_type_list);
  3306. p->tstamp_type_list = NULL;
  3307. p->tstamp_type_count = 0;
  3308. }
  3309. if (p->tstamp_precision_list != NULL) {
  3310. free(p->tstamp_precision_list);
  3311. p->tstamp_precision_list = NULL;
  3312. p->tstamp_precision_count = 0;
  3313. }
  3314. pcap_freecode(&p->fcode);
  3315. #if !defined(_WIN32) && !defined(MSDOS)
  3316. if (p->fd >= 0) {
  3317. close(p->fd);
  3318. p->fd = -1;
  3319. }
  3320. p->selectable_fd = -1;
  3321. #endif
  3322. }
  3323. /*
  3324. * API compatible with WinPcap's "send a packet" routine - returns -1
  3325. * on error, 0 otherwise.
  3326. *
  3327. * XXX - what if we get a short write?
  3328. */
  3329. int
  3330. pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
  3331. {
  3332. if (p->inject_op(p, buf, size) == -1)
  3333. return (-1);
  3334. return (0);
  3335. }
  3336. /*
  3337. * API compatible with OpenBSD's "send a packet" routine - returns -1 on
  3338. * error, number of bytes written otherwise.
  3339. */
  3340. int
  3341. pcap_inject(pcap_t *p, const void *buf, size_t size)
  3342. {
  3343. return (p->inject_op(p, buf, size));
  3344. }
  3345. void
  3346. pcap_close(pcap_t *p)
  3347. {
  3348. if (p->opt.device != NULL)
  3349. free(p->opt.device);
  3350. p->cleanup_op(p);
  3351. free(p);
  3352. }
  3353. /*
  3354. * Given a BPF program, a pcap_pkthdr structure for a packet, and the raw
  3355. * data for the packet, check whether the packet passes the filter.
  3356. * Returns the return value of the filter program, which will be zero if
  3357. * the packet doesn't pass and non-zero if the packet does pass.
  3358. */
  3359. int
  3360. pcap_offline_filter(const struct bpf_program *fp, const struct pcap_pkthdr *h,
  3361. const u_char *pkt)
  3362. {
  3363. const struct bpf_insn *fcode = fp->bf_insns;
  3364. if (fcode != NULL)
  3365. return (bpf_filter(fcode, pkt, h->len, h->caplen));
  3366. else
  3367. return (0);
  3368. }
  3369. static int
  3370. pcap_can_set_rfmon_dead(pcap_t *p)
  3371. {
  3372. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3373. "Rfmon mode doesn't apply on a pcap_open_dead pcap_t");
  3374. return (PCAP_ERROR);
  3375. }
  3376. static int
  3377. pcap_read_dead(pcap_t *p, int cnt _U_, pcap_handler callback _U_,
  3378. u_char *user _U_)
  3379. {
  3380. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3381. "Packets aren't available from a pcap_open_dead pcap_t");
  3382. return (-1);
  3383. }
  3384. static int
  3385. pcap_inject_dead(pcap_t *p, const void *buf _U_, size_t size _U_)
  3386. {
  3387. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3388. "Packets can't be sent on a pcap_open_dead pcap_t");
  3389. return (-1);
  3390. }
  3391. static int
  3392. pcap_setfilter_dead(pcap_t *p, struct bpf_program *fp _U_)
  3393. {
  3394. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3395. "A filter cannot be set on a pcap_open_dead pcap_t");
  3396. return (-1);
  3397. }
  3398. static int
  3399. pcap_setdirection_dead(pcap_t *p, pcap_direction_t d _U_)
  3400. {
  3401. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3402. "The packet direction cannot be set on a pcap_open_dead pcap_t");
  3403. return (-1);
  3404. }
  3405. static int
  3406. pcap_set_datalink_dead(pcap_t *p, int dlt _U_)
  3407. {
  3408. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3409. "The link-layer header type cannot be set on a pcap_open_dead pcap_t");
  3410. return (-1);
  3411. }
  3412. static int
  3413. pcap_getnonblock_dead(pcap_t *p)
  3414. {
  3415. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3416. "A pcap_open_dead pcap_t does not have a non-blocking mode setting");
  3417. return (-1);
  3418. }
  3419. static int
  3420. pcap_setnonblock_dead(pcap_t *p, int nonblock _U_)
  3421. {
  3422. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3423. "A pcap_open_dead pcap_t does not have a non-blocking mode setting");
  3424. return (-1);
  3425. }
  3426. static int
  3427. pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_)
  3428. {
  3429. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3430. "Statistics aren't available from a pcap_open_dead pcap_t");
  3431. return (-1);
  3432. }
  3433. #ifdef _WIN32
  3434. struct pcap_stat *
  3435. pcap_stats_ex_dead(pcap_t *p, int *pcap_stat_size _U_)
  3436. {
  3437. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3438. "Statistics aren't available from a pcap_open_dead pcap_t");
  3439. return (NULL);
  3440. }
  3441. static int
  3442. pcap_setbuff_dead(pcap_t *p, int dim)
  3443. {
  3444. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3445. "The kernel buffer size cannot be set on a pcap_open_dead pcap_t");
  3446. return (-1);
  3447. }
  3448. static int
  3449. pcap_setmode_dead(pcap_t *p, int mode)
  3450. {
  3451. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3452. "impossible to set mode on a pcap_open_dead pcap_t");
  3453. return (-1);
  3454. }
  3455. static int
  3456. pcap_setmintocopy_dead(pcap_t *p, int size)
  3457. {
  3458. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3459. "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t");
  3460. return (-1);
  3461. }
  3462. static HANDLE
  3463. pcap_getevent_dead(pcap_t *p)
  3464. {
  3465. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3466. "A pcap_open_dead pcap_t has no event handle");
  3467. return (INVALID_HANDLE_VALUE);
  3468. }
  3469. static int
  3470. pcap_oid_get_request_dead(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_,
  3471. size_t *lenp _U_)
  3472. {
  3473. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3474. "An OID get request cannot be performed on a pcap_open_dead pcap_t");
  3475. return (PCAP_ERROR);
  3476. }
  3477. static int
  3478. pcap_oid_set_request_dead(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
  3479. size_t *lenp _U_)
  3480. {
  3481. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3482. "An OID set request cannot be performed on a pcap_open_dead pcap_t");
  3483. return (PCAP_ERROR);
  3484. }
  3485. static u_int
  3486. pcap_sendqueue_transmit_dead(pcap_t *p, pcap_send_queue *queue, int sync)
  3487. {
  3488. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3489. "Packets cannot be transmitted on a pcap_open_dead pcap_t");
  3490. return (0);
  3491. }
  3492. static int
  3493. pcap_setuserbuffer_dead(pcap_t *p, int size)
  3494. {
  3495. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3496. "The user buffer cannot be set on a pcap_open_dead pcap_t");
  3497. return (-1);
  3498. }
  3499. static int
  3500. pcap_live_dump_dead(pcap_t *p, char *filename, int maxsize, int maxpacks)
  3501. {
  3502. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3503. "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
  3504. return (-1);
  3505. }
  3506. static int
  3507. pcap_live_dump_ended_dead(pcap_t *p, int sync)
  3508. {
  3509. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  3510. "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
  3511. return (-1);
  3512. }
  3513. static PAirpcapHandle
  3514. pcap_get_airpcap_handle_dead(pcap_t *p)
  3515. {
  3516. return (NULL);
  3517. }
  3518. #endif /* _WIN32 */
  3519. static void
  3520. pcap_cleanup_dead(pcap_t *p _U_)
  3521. {
  3522. /* Nothing to do. */
  3523. }
  3524. pcap_t *
  3525. pcap_open_dead_with_tstamp_precision(int linktype, int snaplen, u_int precision)
  3526. {
  3527. pcap_t *p;
  3528. switch (precision) {
  3529. case PCAP_TSTAMP_PRECISION_MICRO:
  3530. case PCAP_TSTAMP_PRECISION_NANO:
  3531. break;
  3532. default:
  3533. /*
  3534. * This doesn't really matter, but we don't have any way
  3535. * to report particular errors, so the only failure we
  3536. * should have is a memory allocation failure. Just
  3537. * pick microsecond precision.
  3538. */
  3539. precision = PCAP_TSTAMP_PRECISION_MICRO;
  3540. break;
  3541. }
  3542. p = malloc(sizeof(*p));
  3543. if (p == NULL)
  3544. return NULL;
  3545. memset (p, 0, sizeof(*p));
  3546. p->snapshot = snaplen;
  3547. p->linktype = linktype;
  3548. p->opt.tstamp_precision = precision;
  3549. p->can_set_rfmon_op = pcap_can_set_rfmon_dead;
  3550. p->read_op = pcap_read_dead;
  3551. p->inject_op = pcap_inject_dead;
  3552. p->setfilter_op = pcap_setfilter_dead;
  3553. p->setdirection_op = pcap_setdirection_dead;
  3554. p->set_datalink_op = pcap_set_datalink_dead;
  3555. p->getnonblock_op = pcap_getnonblock_dead;
  3556. p->setnonblock_op = pcap_setnonblock_dead;
  3557. p->stats_op = pcap_stats_dead;
  3558. #ifdef _WIN32
  3559. p->stats_ex_op = pcap_stats_ex_dead;
  3560. p->setbuff_op = pcap_setbuff_dead;
  3561. p->setmode_op = pcap_setmode_dead;
  3562. p->setmintocopy_op = pcap_setmintocopy_dead;
  3563. p->getevent_op = pcap_getevent_dead;
  3564. p->oid_get_request_op = pcap_oid_get_request_dead;
  3565. p->oid_set_request_op = pcap_oid_set_request_dead;
  3566. p->sendqueue_transmit_op = pcap_sendqueue_transmit_dead;
  3567. p->setuserbuffer_op = pcap_setuserbuffer_dead;
  3568. p->live_dump_op = pcap_live_dump_dead;
  3569. p->live_dump_ended_op = pcap_live_dump_ended_dead;
  3570. p->get_airpcap_handle_op = pcap_get_airpcap_handle_dead;
  3571. #endif
  3572. p->cleanup_op = pcap_cleanup_dead;
  3573. /*
  3574. * A "dead" pcap_t never requires special BPF code generation.
  3575. */
  3576. p->bpf_codegen_flags = 0;
  3577. p->activated = 1;
  3578. return (p);
  3579. }
  3580. pcap_t *
  3581. pcap_open_dead(int linktype, int snaplen)
  3582. {
  3583. return (pcap_open_dead_with_tstamp_precision(linktype, snaplen,
  3584. PCAP_TSTAMP_PRECISION_MICRO));
  3585. }
  3586. #ifdef YYDEBUG
  3587. /*
  3588. * Set the internal "debug printout" flag for the filter expression parser.
  3589. * The code to print that stuff is present only if YYDEBUG is defined, so
  3590. * the flag, and the routine to set it, are defined only if YYDEBUG is
  3591. * defined.
  3592. *
  3593. * This is intended for libpcap developers, not for general use.
  3594. * If you want to set these in a program, you'll have to declare this
  3595. * routine yourself, with the appropriate DLL import attribute on Windows;
  3596. * it's not declared in any header file, and won't be declared in any
  3597. * header file provided by libpcap.
  3598. */
  3599. PCAP_API void pcap_set_parser_debug(int value);
  3600. PCAP_API_DEF void
  3601. pcap_set_parser_debug(int value)
  3602. {
  3603. pcap_debug = value;
  3604. }
  3605. #endif