pcap-npf.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098
  1. /*
  2. * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
  3. * Copyright (c) 2005 - 2010 CACE Technologies, Davis (California)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the Politecnico di Torino, CACE Technologies
  16. * nor the names of its contributors may be used to endorse or promote
  17. * products derived from this software without specific prior written
  18. * permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #ifdef HAVE_CONFIG_H
  34. #include <config.h>
  35. #endif
  36. #include <errno.h>
  37. #define PCAP_DONT_INCLUDE_PCAP_BPF_H
  38. #include <Packet32.h>
  39. #include <pcap-int.h>
  40. #include <pcap/dlt.h>
  41. /* Old-school MinGW have these headers in a different place.
  42. */
  43. #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
  44. #include <ddk/ntddndis.h>
  45. #include <ddk/ndis.h>
  46. #else
  47. #include <ntddndis.h> /* MSVC/TDM-MinGW/MinGW64 */
  48. #endif
  49. #ifdef HAVE_DAG_API
  50. #include <dagnew.h>
  51. #include <dagapi.h>
  52. #endif /* HAVE_DAG_API */
  53. static int pcap_setfilter_npf(pcap_t *, struct bpf_program *);
  54. static int pcap_setfilter_win32_dag(pcap_t *, struct bpf_program *);
  55. static int pcap_getnonblock_npf(pcap_t *);
  56. static int pcap_setnonblock_npf(pcap_t *, int);
  57. /*dimension of the buffer in the pcap_t structure*/
  58. #define WIN32_DEFAULT_USER_BUFFER_SIZE 256000
  59. /*dimension of the buffer in the kernel driver NPF */
  60. #define WIN32_DEFAULT_KERNEL_BUFFER_SIZE 1000000
  61. /* Equivalent to ntohs(), but a lot faster under Windows */
  62. #define SWAPS(_X) ((_X & 0xff) << 8) | (_X >> 8)
  63. /*
  64. * Private data for capturing on WinPcap devices.
  65. */
  66. struct pcap_win {
  67. ADAPTER *adapter; /* the packet32 ADAPTER for the device */
  68. int nonblock;
  69. int rfmon_selfstart; /* a flag tells whether the monitor mode is set by itself */
  70. int filtering_in_kernel; /* using kernel filter */
  71. #ifdef HAVE_DAG_API
  72. int dag_fcs_bits; /* Number of checksum bits from link layer */
  73. #endif
  74. #ifdef ENABLE_REMOTE
  75. int samp_npkt; /* parameter needed for sampling, with '1 out of N' method has been requested */
  76. struct timeval samp_time; /* parameter needed for sampling, with '1 every N ms' method has been requested */
  77. #endif
  78. };
  79. /*
  80. * Define stub versions of the monitor-mode support routines if this
  81. * isn't Npcap. HAVE_NPCAP_PACKET_API is defined by Npcap but not
  82. * WinPcap.
  83. */
  84. #ifndef HAVE_NPCAP_PACKET_API
  85. static int
  86. PacketIsMonitorModeSupported(PCHAR AdapterName _U_)
  87. {
  88. /*
  89. * We don't support monitor mode.
  90. */
  91. return (0);
  92. }
  93. static int
  94. PacketSetMonitorMode(PCHAR AdapterName _U_, int mode _U_)
  95. {
  96. /*
  97. * This should never be called, as PacketIsMonitorModeSupported()
  98. * will return 0, meaning "we don't support monitor mode, so
  99. * don't try to turn it on or off".
  100. */
  101. return (0);
  102. }
  103. static int
  104. PacketGetMonitorMode(PCHAR AdapterName _U_)
  105. {
  106. /*
  107. * This should fail, so that pcap_activate_npf() returns
  108. * PCAP_ERROR_RFMON_NOTSUP if our caller requested monitor
  109. * mode.
  110. */
  111. return (-1);
  112. }
  113. #endif
  114. /*
  115. * Sigh. PacketRequest() will have made a DeviceIoControl()
  116. * call to the NPF driver to perform the OID request, with a
  117. * BIOCQUERYOID ioctl. The kernel code should get back one
  118. * of NDIS_STATUS_INVALID_OID, NDIS_STATUS_NOT_SUPPORTED,
  119. * or NDIS_STATUS_NOT_RECOGNIZED if the OID request isn't
  120. * supported by the OS or the driver, but that doesn't seem
  121. * to make it to the caller of PacketRequest() in a
  122. * reliable fashion.
  123. */
  124. #define NDIS_STATUS_INVALID_OID 0xc0010017
  125. #define NDIS_STATUS_NOT_SUPPORTED 0xc00000bb /* STATUS_NOT_SUPPORTED */
  126. #define NDIS_STATUS_NOT_RECOGNIZED 0x00010001
  127. static int
  128. oid_get_request(ADAPTER *adapter, bpf_u_int32 oid, void *data, size_t *lenp,
  129. char *errbuf)
  130. {
  131. PACKET_OID_DATA *oid_data_arg;
  132. /*
  133. * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
  134. * It should be big enough to hold "*lenp" bytes of data; it
  135. * will actually be slightly larger, as PACKET_OID_DATA has a
  136. * 1-byte data array at the end, standing in for the variable-length
  137. * data that's actually there.
  138. */
  139. oid_data_arg = malloc(sizeof (PACKET_OID_DATA) + *lenp);
  140. if (oid_data_arg == NULL) {
  141. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  142. "Couldn't allocate argument buffer for PacketRequest");
  143. return (PCAP_ERROR);
  144. }
  145. /*
  146. * No need to copy the data - we're doing a fetch.
  147. */
  148. oid_data_arg->Oid = oid;
  149. oid_data_arg->Length = (ULONG)(*lenp); /* XXX - check for ridiculously large value? */
  150. if (!PacketRequest(adapter, FALSE, oid_data_arg)) {
  151. char errmsgbuf[PCAP_ERRBUF_SIZE+1];
  152. pcap_win32_err_to_str(GetLastError(), errmsgbuf);
  153. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  154. "Error calling PacketRequest: %s", errmsgbuf);
  155. free(oid_data_arg);
  156. return (-1);
  157. }
  158. /*
  159. * Get the length actually supplied.
  160. */
  161. *lenp = oid_data_arg->Length;
  162. /*
  163. * Copy back the data we fetched.
  164. */
  165. memcpy(data, oid_data_arg->Data, *lenp);
  166. free(oid_data_arg);
  167. return (0);
  168. }
  169. static int
  170. pcap_stats_npf(pcap_t *p, struct pcap_stat *ps)
  171. {
  172. struct pcap_win *pw = p->priv;
  173. struct bpf_stat bstats;
  174. char errbuf[PCAP_ERRBUF_SIZE+1];
  175. /*
  176. * Try to get statistics.
  177. *
  178. * (Please note - "struct pcap_stat" is *not* the same as
  179. * WinPcap's "struct bpf_stat". It might currently have the
  180. * same layout, but let's not cheat.
  181. *
  182. * Note also that we don't fill in ps_capt, as we might have
  183. * been called by code compiled against an earlier version of
  184. * WinPcap that didn't have ps_capt, in which case filling it
  185. * in would stomp on whatever comes after the structure passed
  186. * to us.
  187. */
  188. if (!PacketGetStats(pw->adapter, &bstats)) {
  189. pcap_win32_err_to_str(GetLastError(), errbuf);
  190. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  191. "PacketGetStats error: %s", errbuf);
  192. return (-1);
  193. }
  194. ps->ps_recv = bstats.bs_recv;
  195. ps->ps_drop = bstats.bs_drop;
  196. /*
  197. * XXX - PacketGetStats() doesn't fill this in, so we just
  198. * return 0.
  199. */
  200. #if 0
  201. ps->ps_ifdrop = bstats.ps_ifdrop;
  202. #else
  203. ps->ps_ifdrop = 0;
  204. #endif
  205. return (0);
  206. }
  207. /*
  208. * Win32-only routine for getting statistics.
  209. *
  210. * This way is definitely safer than passing the pcap_stat * from the userland.
  211. * In fact, there could happen than the user allocates a variable which is not
  212. * big enough for the new structure, and the library will write in a zone
  213. * which is not allocated to this variable.
  214. *
  215. * In this way, we're pretty sure we are writing on memory allocated to this
  216. * variable.
  217. *
  218. * XXX - but this is the wrong way to handle statistics. Instead, we should
  219. * have an API that returns data in a form like the Options section of a
  220. * pcapng Interface Statistics Block:
  221. *
  222. * http://xml2rfc.tools.ietf.org/cgi-bin/xml2rfc.cgi?url=https://raw.githubusercontent.com/pcapng/pcapng/master/draft-tuexen-opsawg-pcapng.xml&modeAsFormat=html/ascii&type=ascii#rfc.section.4.6
  223. *
  224. * which would let us add new statistics straightforwardly and indicate which
  225. * statistics we are and are *not* providing, rather than having to provide
  226. * possibly-bogus values for statistics we can't provide.
  227. */
  228. struct pcap_stat *
  229. pcap_stats_ex_npf(pcap_t *p, int *pcap_stat_size)
  230. {
  231. struct pcap_win *pw = p->priv;
  232. struct bpf_stat bstats;
  233. char errbuf[PCAP_ERRBUF_SIZE+1];
  234. *pcap_stat_size = sizeof (p->stat);
  235. /*
  236. * Try to get statistics.
  237. *
  238. * (Please note - "struct pcap_stat" is *not* the same as
  239. * WinPcap's "struct bpf_stat". It might currently have the
  240. * same layout, but let's not cheat.)
  241. */
  242. if (!PacketGetStatsEx(pw->adapter, &bstats)) {
  243. pcap_win32_err_to_str(GetLastError(), errbuf);
  244. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  245. "PacketGetStatsEx error: %s", errbuf);
  246. return (NULL);
  247. }
  248. p->stat.ps_recv = bstats.bs_recv;
  249. p->stat.ps_drop = bstats.bs_drop;
  250. p->stat.ps_ifdrop = bstats.ps_ifdrop;
  251. #ifdef ENABLE_REMOTE
  252. p->stat.ps_capt = bstats.bs_capt;
  253. #endif
  254. return (&p->stat);
  255. }
  256. /* Set the dimension of the kernel-level capture buffer */
  257. static int
  258. pcap_setbuff_npf(pcap_t *p, int dim)
  259. {
  260. struct pcap_win *pw = p->priv;
  261. if(PacketSetBuff(pw->adapter,dim)==FALSE)
  262. {
  263. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");
  264. return (-1);
  265. }
  266. return (0);
  267. }
  268. /* Set the driver working mode */
  269. static int
  270. pcap_setmode_npf(pcap_t *p, int mode)
  271. {
  272. struct pcap_win *pw = p->priv;
  273. if(PacketSetMode(pw->adapter,mode)==FALSE)
  274. {
  275. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: working mode not recognized");
  276. return (-1);
  277. }
  278. return (0);
  279. }
  280. /*set the minimum amount of data that will release a read call*/
  281. static int
  282. pcap_setmintocopy_npf(pcap_t *p, int size)
  283. {
  284. struct pcap_win *pw = p->priv;
  285. if(PacketSetMinToCopy(pw->adapter, size)==FALSE)
  286. {
  287. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: unable to set the requested mintocopy size");
  288. return (-1);
  289. }
  290. return (0);
  291. }
  292. static HANDLE
  293. pcap_getevent_npf(pcap_t *p)
  294. {
  295. struct pcap_win *pw = p->priv;
  296. return (PacketGetReadEvent(pw->adapter));
  297. }
  298. static int
  299. pcap_oid_get_request_npf(pcap_t *p, bpf_u_int32 oid, void *data, size_t *lenp)
  300. {
  301. struct pcap_win *pw = p->priv;
  302. return (oid_get_request(pw->adapter, oid, data, lenp, p->errbuf));
  303. }
  304. static int
  305. pcap_oid_set_request_npf(pcap_t *p, bpf_u_int32 oid, const void *data,
  306. size_t *lenp)
  307. {
  308. struct pcap_win *pw = p->priv;
  309. PACKET_OID_DATA *oid_data_arg;
  310. char errbuf[PCAP_ERRBUF_SIZE+1];
  311. /*
  312. * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
  313. * It should be big enough to hold "*lenp" bytes of data; it
  314. * will actually be slightly larger, as PACKET_OID_DATA has a
  315. * 1-byte data array at the end, standing in for the variable-length
  316. * data that's actually there.
  317. */
  318. oid_data_arg = malloc(sizeof (PACKET_OID_DATA) + *lenp);
  319. if (oid_data_arg == NULL) {
  320. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  321. "Couldn't allocate argument buffer for PacketRequest");
  322. return (PCAP_ERROR);
  323. }
  324. oid_data_arg->Oid = oid;
  325. oid_data_arg->Length = (ULONG)(*lenp); /* XXX - check for ridiculously large value? */
  326. memcpy(oid_data_arg->Data, data, *lenp);
  327. if (!PacketRequest(pw->adapter, TRUE, oid_data_arg)) {
  328. pcap_win32_err_to_str(GetLastError(), errbuf);
  329. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  330. "Error calling PacketRequest: %s", errbuf);
  331. free(oid_data_arg);
  332. return (PCAP_ERROR);
  333. }
  334. /*
  335. * Get the length actually copied.
  336. */
  337. *lenp = oid_data_arg->Length;
  338. /*
  339. * No need to copy the data - we're doing a set.
  340. */
  341. free(oid_data_arg);
  342. return (0);
  343. }
  344. static u_int
  345. pcap_sendqueue_transmit_npf(pcap_t *p, pcap_send_queue *queue, int sync)
  346. {
  347. struct pcap_win *pw = p->priv;
  348. u_int res;
  349. char errbuf[PCAP_ERRBUF_SIZE+1];
  350. if (pw->adapter==NULL) {
  351. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  352. "Cannot transmit a queue to an offline capture or to a TurboCap port");
  353. return (0);
  354. }
  355. res = PacketSendPackets(pw->adapter,
  356. queue->buffer,
  357. queue->len,
  358. (BOOLEAN)sync);
  359. if(res != queue->len){
  360. pcap_win32_err_to_str(GetLastError(), errbuf);
  361. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  362. "Error opening adapter: %s", errbuf);
  363. }
  364. return (res);
  365. }
  366. static int
  367. pcap_setuserbuffer_npf(pcap_t *p, int size)
  368. {
  369. unsigned char *new_buff;
  370. if (size<=0) {
  371. /* Bogus parameter */
  372. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  373. "Error: invalid size %d",size);
  374. return (-1);
  375. }
  376. /* Allocate the buffer */
  377. new_buff=(unsigned char*)malloc(sizeof(char)*size);
  378. if (!new_buff) {
  379. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  380. "Error: not enough memory");
  381. return (-1);
  382. }
  383. free(p->buffer);
  384. p->buffer=new_buff;
  385. p->bufsize=size;
  386. return (0);
  387. }
  388. static int
  389. pcap_live_dump_npf(pcap_t *p, char *filename, int maxsize, int maxpacks)
  390. {
  391. struct pcap_win *pw = p->priv;
  392. BOOLEAN res;
  393. /* Set the packet driver in dump mode */
  394. res = PacketSetMode(pw->adapter, PACKET_MODE_DUMP);
  395. if(res == FALSE){
  396. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  397. "Error setting dump mode");
  398. return (-1);
  399. }
  400. /* Set the name of the dump file */
  401. res = PacketSetDumpName(pw->adapter, filename, (int)strlen(filename));
  402. if(res == FALSE){
  403. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  404. "Error setting kernel dump file name");
  405. return (-1);
  406. }
  407. /* Set the limits of the dump file */
  408. res = PacketSetDumpLimits(pw->adapter, maxsize, maxpacks);
  409. if(res == FALSE) {
  410. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  411. "Error setting dump limit");
  412. return (-1);
  413. }
  414. return (0);
  415. }
  416. static int
  417. pcap_live_dump_ended_npf(pcap_t *p, int sync)
  418. {
  419. struct pcap_win *pw = p->priv;
  420. return (PacketIsDumpEnded(pw->adapter, (BOOLEAN)sync));
  421. }
  422. static PAirpcapHandle
  423. pcap_get_airpcap_handle_npf(pcap_t *p)
  424. {
  425. #ifdef HAVE_AIRPCAP_API
  426. struct pcap_win *pw = p->priv;
  427. return (PacketGetAirPcapHandle(pw->adapter));
  428. #else
  429. return (NULL);
  430. #endif /* HAVE_AIRPCAP_API */
  431. }
  432. static int
  433. pcap_read_npf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  434. {
  435. PACKET Packet;
  436. int cc;
  437. int n = 0;
  438. register u_char *bp, *ep;
  439. u_char *datap;
  440. struct pcap_win *pw = p->priv;
  441. cc = p->cc;
  442. if (p->cc == 0) {
  443. /*
  444. * Has "pcap_breakloop()" been called?
  445. */
  446. if (p->break_loop) {
  447. /*
  448. * Yes - clear the flag that indicates that it
  449. * has, and return PCAP_ERROR_BREAK to indicate
  450. * that we were told to break out of the loop.
  451. */
  452. p->break_loop = 0;
  453. return (PCAP_ERROR_BREAK);
  454. }
  455. /*
  456. * Capture the packets.
  457. *
  458. * The PACKET structure had a bunch of extra stuff for
  459. * Windows 9x/Me, but the only interesting data in it
  460. * in the versions of Windows that we support is just
  461. * a copy of p->buffer, a copy of p->buflen, and the
  462. * actual number of bytes read returned from
  463. * PacketReceivePacket(), none of which has to be
  464. * retained from call to call, so we just keep one on
  465. * the stack.
  466. */
  467. PacketInitPacket(&Packet, (BYTE *)p->buffer, p->bufsize);
  468. if (!PacketReceivePacket(pw->adapter, &Packet, TRUE)) {
  469. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed");
  470. return (PCAP_ERROR);
  471. }
  472. cc = Packet.ulBytesReceived;
  473. bp = p->buffer;
  474. }
  475. else
  476. bp = p->bp;
  477. /*
  478. * Loop through each packet.
  479. */
  480. #define bhp ((struct bpf_hdr *)bp)
  481. ep = bp + cc;
  482. for (;;) {
  483. register int caplen, hdrlen;
  484. /*
  485. * Has "pcap_breakloop()" been called?
  486. * If so, return immediately - if we haven't read any
  487. * packets, clear the flag and return PCAP_ERROR_BREAK
  488. * to indicate that we were told to break out of the loop,
  489. * otherwise leave the flag set, so that the *next* call
  490. * will break out of the loop without having read any
  491. * packets, and return the number of packets we've
  492. * processed so far.
  493. */
  494. if (p->break_loop) {
  495. if (n == 0) {
  496. p->break_loop = 0;
  497. return (PCAP_ERROR_BREAK);
  498. } else {
  499. p->bp = bp;
  500. p->cc = (int) (ep - bp);
  501. return (n);
  502. }
  503. }
  504. if (bp >= ep)
  505. break;
  506. caplen = bhp->bh_caplen;
  507. hdrlen = bhp->bh_hdrlen;
  508. datap = bp + hdrlen;
  509. /*
  510. * Short-circuit evaluation: if using BPF filter
  511. * in kernel, no need to do it now - we already know
  512. * the packet passed the filter.
  513. *
  514. * XXX - bpf_filter() should always return TRUE if
  515. * handed a null pointer for the program, but it might
  516. * just try to "run" the filter, so we check here.
  517. */
  518. if (pw->filtering_in_kernel ||
  519. p->fcode.bf_insns == NULL ||
  520. bpf_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) {
  521. #ifdef ENABLE_REMOTE
  522. switch (p->rmt_samp.method) {
  523. case PCAP_SAMP_1_EVERY_N:
  524. pw->samp_npkt = (pw->samp_npkt + 1) % p->rmt_samp.value;
  525. /* Discard all packets that are not '1 out of N' */
  526. if (pw->samp_npkt != 0) {
  527. bp += Packet_WORDALIGN(caplen + hdrlen);
  528. continue;
  529. }
  530. break;
  531. case PCAP_SAMP_FIRST_AFTER_N_MS:
  532. {
  533. struct pcap_pkthdr *pkt_header = (struct pcap_pkthdr*) bp;
  534. /*
  535. * Check if the timestamp of the arrived
  536. * packet is smaller than our target time.
  537. */
  538. if (pkt_header->ts.tv_sec < pw->samp_time.tv_sec ||
  539. (pkt_header->ts.tv_sec == pw->samp_time.tv_sec && pkt_header->ts.tv_usec < pw->samp_time.tv_usec)) {
  540. bp += Packet_WORDALIGN(caplen + hdrlen);
  541. continue;
  542. }
  543. /*
  544. * The arrived packet is suitable for being
  545. * delivered to our caller, so let's update
  546. * the target time.
  547. */
  548. pw->samp_time.tv_usec = pkt_header->ts.tv_usec + p->rmt_samp.value * 1000;
  549. if (pw->samp_time.tv_usec > 1000000) {
  550. pw->samp_time.tv_sec = pkt_header->ts.tv_sec + pw->samp_time.tv_usec / 1000000;
  551. pw->samp_time.tv_usec = pw->samp_time.tv_usec % 1000000;
  552. }
  553. }
  554. }
  555. #endif /* ENABLE_REMOTE */
  556. /*
  557. * XXX A bpf_hdr matches a pcap_pkthdr.
  558. */
  559. (*callback)(user, (struct pcap_pkthdr*)bp, datap);
  560. bp += Packet_WORDALIGN(caplen + hdrlen);
  561. if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
  562. p->bp = bp;
  563. p->cc = (int) (ep - bp);
  564. return (n);
  565. }
  566. } else {
  567. /*
  568. * Skip this packet.
  569. */
  570. bp += Packet_WORDALIGN(caplen + hdrlen);
  571. }
  572. }
  573. #undef bhp
  574. p->cc = 0;
  575. return (n);
  576. }
  577. #ifdef HAVE_DAG_API
  578. static int
  579. pcap_read_win32_dag(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  580. {
  581. struct pcap_win *pw = p->priv;
  582. PACKET Packet;
  583. u_char *dp = NULL;
  584. int packet_len = 0, caplen = 0;
  585. struct pcap_pkthdr pcap_header;
  586. u_char *endofbuf;
  587. int n = 0;
  588. dag_record_t *header;
  589. unsigned erf_record_len;
  590. ULONGLONG ts;
  591. int cc;
  592. unsigned swt;
  593. unsigned dfp = pw->adapter->DagFastProcess;
  594. cc = p->cc;
  595. if (cc == 0) /* Get new packets only if we have processed all the ones of the previous read */
  596. {
  597. /*
  598. * Get new packets from the network.
  599. *
  600. * The PACKET structure had a bunch of extra stuff for
  601. * Windows 9x/Me, but the only interesting data in it
  602. * in the versions of Windows that we support is just
  603. * a copy of p->buffer, a copy of p->buflen, and the
  604. * actual number of bytes read returned from
  605. * PacketReceivePacket(), none of which has to be
  606. * retained from call to call, so we just keep one on
  607. * the stack.
  608. */
  609. PacketInitPacket(&Packet, (BYTE *)p->buffer, p->bufsize);
  610. if (!PacketReceivePacket(pw->adapter, &Packet, TRUE)) {
  611. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed");
  612. return (-1);
  613. }
  614. cc = Packet.ulBytesReceived;
  615. if(cc == 0)
  616. /* The timeout has expired but we no packets arrived */
  617. return (0);
  618. header = (dag_record_t*)pw->adapter->DagBuffer;
  619. }
  620. else
  621. header = (dag_record_t*)p->bp;
  622. endofbuf = (char*)header + cc;
  623. /*
  624. * Cycle through the packets
  625. */
  626. do
  627. {
  628. erf_record_len = SWAPS(header->rlen);
  629. if((char*)header + erf_record_len > endofbuf)
  630. break;
  631. /* Increase the number of captured packets */
  632. p->stat.ps_recv++;
  633. /* Find the beginning of the packet */
  634. dp = ((u_char *)header) + dag_record_size;
  635. /* Determine actual packet len */
  636. switch(header->type)
  637. {
  638. case TYPE_ATM:
  639. packet_len = ATM_SNAPLEN;
  640. caplen = ATM_SNAPLEN;
  641. dp += 4;
  642. break;
  643. case TYPE_ETH:
  644. swt = SWAPS(header->wlen);
  645. packet_len = swt - (pw->dag_fcs_bits);
  646. caplen = erf_record_len - dag_record_size - 2;
  647. if (caplen > packet_len)
  648. {
  649. caplen = packet_len;
  650. }
  651. dp += 2;
  652. break;
  653. case TYPE_HDLC_POS:
  654. swt = SWAPS(header->wlen);
  655. packet_len = swt - (pw->dag_fcs_bits);
  656. caplen = erf_record_len - dag_record_size;
  657. if (caplen > packet_len)
  658. {
  659. caplen = packet_len;
  660. }
  661. break;
  662. }
  663. if(caplen > p->snapshot)
  664. caplen = p->snapshot;
  665. /*
  666. * Has "pcap_breakloop()" been called?
  667. * If so, return immediately - if we haven't read any
  668. * packets, clear the flag and return -2 to indicate
  669. * that we were told to break out of the loop, otherwise
  670. * leave the flag set, so that the *next* call will break
  671. * out of the loop without having read any packets, and
  672. * return the number of packets we've processed so far.
  673. */
  674. if (p->break_loop)
  675. {
  676. if (n == 0)
  677. {
  678. p->break_loop = 0;
  679. return (-2);
  680. }
  681. else
  682. {
  683. p->bp = (char*)header;
  684. p->cc = endofbuf - (char*)header;
  685. return (n);
  686. }
  687. }
  688. if(!dfp)
  689. {
  690. /* convert between timestamp formats */
  691. ts = header->ts;
  692. pcap_header.ts.tv_sec = (int)(ts >> 32);
  693. ts = (ts & 0xffffffffi64) * 1000000;
  694. ts += 0x80000000; /* rounding */
  695. pcap_header.ts.tv_usec = (int)(ts >> 32);
  696. if (pcap_header.ts.tv_usec >= 1000000) {
  697. pcap_header.ts.tv_usec -= 1000000;
  698. pcap_header.ts.tv_sec++;
  699. }
  700. }
  701. /* No underlaying filtering system. We need to filter on our own */
  702. if (p->fcode.bf_insns)
  703. {
  704. if (bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen) == 0)
  705. {
  706. /* Move to next packet */
  707. header = (dag_record_t*)((char*)header + erf_record_len);
  708. continue;
  709. }
  710. }
  711. /* Fill the header for the user suppplied callback function */
  712. pcap_header.caplen = caplen;
  713. pcap_header.len = packet_len;
  714. /* Call the callback function */
  715. (*callback)(user, &pcap_header, dp);
  716. /* Move to next packet */
  717. header = (dag_record_t*)((char*)header + erf_record_len);
  718. /* Stop if the number of packets requested by user has been reached*/
  719. if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt))
  720. {
  721. p->bp = (char*)header;
  722. p->cc = endofbuf - (char*)header;
  723. return (n);
  724. }
  725. }
  726. while((u_char*)header < endofbuf);
  727. return (1);
  728. }
  729. #endif /* HAVE_DAG_API */
  730. /* Send a packet to the network */
  731. static int
  732. pcap_inject_npf(pcap_t *p, const void *buf, size_t size)
  733. {
  734. struct pcap_win *pw = p->priv;
  735. PACKET pkt;
  736. PacketInitPacket(&pkt, (PVOID)buf, size);
  737. if(PacketSendPacket(pw->adapter,&pkt,TRUE) == FALSE) {
  738. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send error: PacketSendPacket failed");
  739. return (-1);
  740. }
  741. /*
  742. * We assume it all got sent if "PacketSendPacket()" succeeded.
  743. * "pcap_inject()" is expected to return the number of bytes
  744. * sent.
  745. */
  746. return ((int)size);
  747. }
  748. static void
  749. pcap_cleanup_npf(pcap_t *p)
  750. {
  751. struct pcap_win *pw = p->priv;
  752. if (pw->adapter != NULL) {
  753. PacketCloseAdapter(pw->adapter);
  754. pw->adapter = NULL;
  755. }
  756. if (pw->rfmon_selfstart)
  757. {
  758. PacketSetMonitorMode(p->opt.device, 0);
  759. }
  760. pcap_cleanup_live_common(p);
  761. }
  762. static int
  763. pcap_activate_npf(pcap_t *p)
  764. {
  765. struct pcap_win *pw = p->priv;
  766. NetType type;
  767. int res;
  768. char errbuf[PCAP_ERRBUF_SIZE+1];
  769. if (p->opt.rfmon) {
  770. /*
  771. * Monitor mode is supported on Windows Vista and later.
  772. */
  773. if (PacketGetMonitorMode(p->opt.device) == 1)
  774. {
  775. pw->rfmon_selfstart = 0;
  776. }
  777. else
  778. {
  779. if ((res = PacketSetMonitorMode(p->opt.device, 1)) != 1)
  780. {
  781. pw->rfmon_selfstart = 0;
  782. // Monitor mode is not supported.
  783. if (res == 0)
  784. {
  785. return PCAP_ERROR_RFMON_NOTSUP;
  786. }
  787. else
  788. {
  789. return PCAP_ERROR;
  790. }
  791. }
  792. else
  793. {
  794. pw->rfmon_selfstart = 1;
  795. }
  796. }
  797. }
  798. /* Init WinSock */
  799. pcap_wsockinit();
  800. pw->adapter = PacketOpenAdapter(p->opt.device);
  801. if (pw->adapter == NULL)
  802. {
  803. /* Adapter detected but we are not able to open it. Return failure. */
  804. pcap_win32_err_to_str(GetLastError(), errbuf);
  805. if (pw->rfmon_selfstart)
  806. {
  807. PacketSetMonitorMode(p->opt.device, 0);
  808. }
  809. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  810. "Error opening adapter: %s", errbuf);
  811. return (PCAP_ERROR);
  812. }
  813. /*get network type*/
  814. if(PacketGetNetType (pw->adapter,&type) == FALSE)
  815. {
  816. pcap_win32_err_to_str(GetLastError(), errbuf);
  817. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  818. "Cannot determine the network type: %s", errbuf);
  819. goto bad;
  820. }
  821. /*Set the linktype*/
  822. switch (type.LinkType)
  823. {
  824. case NdisMediumWan:
  825. p->linktype = DLT_EN10MB;
  826. break;
  827. case NdisMedium802_3:
  828. p->linktype = DLT_EN10MB;
  829. /*
  830. * This is (presumably) a real Ethernet capture; give it a
  831. * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
  832. * that an application can let you choose it, in case you're
  833. * capturing DOCSIS traffic that a Cisco Cable Modem
  834. * Termination System is putting out onto an Ethernet (it
  835. * doesn't put an Ethernet header onto the wire, it puts raw
  836. * DOCSIS frames out on the wire inside the low-level
  837. * Ethernet framing).
  838. */
  839. p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
  840. /*
  841. * If that fails, just leave the list empty.
  842. */
  843. if (p->dlt_list != NULL) {
  844. p->dlt_list[0] = DLT_EN10MB;
  845. p->dlt_list[1] = DLT_DOCSIS;
  846. p->dlt_count = 2;
  847. }
  848. break;
  849. case NdisMediumFddi:
  850. p->linktype = DLT_FDDI;
  851. break;
  852. case NdisMedium802_5:
  853. p->linktype = DLT_IEEE802;
  854. break;
  855. case NdisMediumArcnetRaw:
  856. p->linktype = DLT_ARCNET;
  857. break;
  858. case NdisMediumArcnet878_2:
  859. p->linktype = DLT_ARCNET;
  860. break;
  861. case NdisMediumAtm:
  862. p->linktype = DLT_ATM_RFC1483;
  863. break;
  864. case NdisMediumCHDLC:
  865. p->linktype = DLT_CHDLC;
  866. break;
  867. case NdisMediumPPPSerial:
  868. p->linktype = DLT_PPP_SERIAL;
  869. break;
  870. case NdisMediumNull:
  871. p->linktype = DLT_NULL;
  872. break;
  873. case NdisMediumBare80211:
  874. p->linktype = DLT_IEEE802_11;
  875. break;
  876. case NdisMediumRadio80211:
  877. p->linktype = DLT_IEEE802_11_RADIO;
  878. break;
  879. case NdisMediumPpi:
  880. p->linktype = DLT_PPI;
  881. break;
  882. default:
  883. p->linktype = DLT_EN10MB; /*an unknown adapter is assumed to be ethernet*/
  884. break;
  885. }
  886. /*
  887. * Turn a negative snapshot value (invalid), a snapshot value of
  888. * 0 (unspecified), or a value bigger than the normal maximum
  889. * value, into the maximum allowed value.
  890. *
  891. * If some application really *needs* a bigger snapshot
  892. * length, we should just increase MAXIMUM_SNAPLEN.
  893. */
  894. if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
  895. p->snapshot = MAXIMUM_SNAPLEN;
  896. /* Set promiscuous mode */
  897. if (p->opt.promisc)
  898. {
  899. if (PacketSetHwFilter(pw->adapter,NDIS_PACKET_TYPE_PROMISCUOUS) == FALSE)
  900. {
  901. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "failed to set hardware filter to promiscuous mode");
  902. goto bad;
  903. }
  904. }
  905. else
  906. {
  907. /* NDIS_PACKET_TYPE_ALL_LOCAL selects "All packets sent by installed
  908. * protocols and all packets indicated by the NIC" but if no protocol
  909. * drivers (like TCP/IP) are installed, NDIS_PACKET_TYPE_DIRECTED,
  910. * NDIS_PACKET_TYPE_BROADCAST, and NDIS_PACKET_TYPE_MULTICAST are needed to
  911. * capture incoming frames.
  912. */
  913. if (PacketSetHwFilter(pw->adapter,
  914. NDIS_PACKET_TYPE_ALL_LOCAL |
  915. NDIS_PACKET_TYPE_DIRECTED |
  916. NDIS_PACKET_TYPE_BROADCAST |
  917. NDIS_PACKET_TYPE_MULTICAST) == FALSE)
  918. {
  919. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "failed to set hardware filter to non-promiscuous mode");
  920. goto bad;
  921. }
  922. }
  923. /* Set the buffer size */
  924. p->bufsize = WIN32_DEFAULT_USER_BUFFER_SIZE;
  925. if(!(pw->adapter->Flags & INFO_FLAG_DAG_CARD))
  926. {
  927. /*
  928. * Traditional Adapter
  929. */
  930. /*
  931. * If the buffer size wasn't explicitly set, default to
  932. * WIN32_DEFAULT_KERNEL_BUFFER_SIZE.
  933. */
  934. if (p->opt.buffer_size == 0)
  935. p->opt.buffer_size = WIN32_DEFAULT_KERNEL_BUFFER_SIZE;
  936. if(PacketSetBuff(pw->adapter,p->opt.buffer_size)==FALSE)
  937. {
  938. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");
  939. goto bad;
  940. }
  941. p->buffer = malloc(p->bufsize);
  942. if (p->buffer == NULL)
  943. {
  944. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  945. errno, "malloc");
  946. goto bad;
  947. }
  948. if (p->opt.immediate)
  949. {
  950. /* tell the driver to copy the buffer as soon as data arrives */
  951. if(PacketSetMinToCopy(pw->adapter,0)==FALSE)
  952. {
  953. pcap_win32_err_to_str(GetLastError(), errbuf);
  954. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  955. "Error calling PacketSetMinToCopy: %s",
  956. errbuf);
  957. goto bad;
  958. }
  959. }
  960. else
  961. {
  962. /* tell the driver to copy the buffer only if it contains at least 16K */
  963. if(PacketSetMinToCopy(pw->adapter,16000)==FALSE)
  964. {
  965. pcap_win32_err_to_str(GetLastError(), errbuf);
  966. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  967. "Error calling PacketSetMinToCopy: %s",
  968. errbuf);
  969. goto bad;
  970. }
  971. }
  972. } else {
  973. /*
  974. * Dag Card
  975. */
  976. #ifdef HAVE_DAG_API
  977. /*
  978. * We have DAG support.
  979. */
  980. LONG status;
  981. HKEY dagkey;
  982. DWORD lptype;
  983. DWORD lpcbdata;
  984. int postype = 0;
  985. char keyname[512];
  986. pcap_snprintf(keyname, sizeof(keyname), "%s\\CardParams\\%s",
  987. "SYSTEM\\CurrentControlSet\\Services\\DAG",
  988. strstr(_strlwr(p->opt.device), "dag"));
  989. do
  990. {
  991. status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &dagkey);
  992. if(status != ERROR_SUCCESS)
  993. break;
  994. status = RegQueryValueEx(dagkey,
  995. "PosType",
  996. NULL,
  997. &lptype,
  998. (char*)&postype,
  999. &lpcbdata);
  1000. if(status != ERROR_SUCCESS)
  1001. {
  1002. postype = 0;
  1003. }
  1004. RegCloseKey(dagkey);
  1005. }
  1006. while(FALSE);
  1007. p->snapshot = PacketSetSnapLen(pw->adapter, p->snapshot);
  1008. /* Set the length of the FCS associated to any packet. This value
  1009. * will be subtracted to the packet length */
  1010. pw->dag_fcs_bits = pw->adapter->DagFcsLen;
  1011. #else /* HAVE_DAG_API */
  1012. /*
  1013. * No DAG support.
  1014. */
  1015. goto bad;
  1016. #endif /* HAVE_DAG_API */
  1017. }
  1018. PacketSetReadTimeout(pw->adapter, p->opt.timeout);
  1019. /* disable loopback capture if requested */
  1020. if (p->opt.nocapture_local)
  1021. {
  1022. if (!PacketSetLoopbackBehavior(pw->adapter, NPF_DISABLE_LOOPBACK))
  1023. {
  1024. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1025. "Unable to disable the capture of loopback packets.");
  1026. goto bad;
  1027. }
  1028. }
  1029. #ifdef HAVE_DAG_API
  1030. if(pw->adapter->Flags & INFO_FLAG_DAG_CARD)
  1031. {
  1032. /* install dag specific handlers for read and setfilter */
  1033. p->read_op = pcap_read_win32_dag;
  1034. p->setfilter_op = pcap_setfilter_win32_dag;
  1035. }
  1036. else
  1037. {
  1038. #endif /* HAVE_DAG_API */
  1039. /* install traditional npf handlers for read and setfilter */
  1040. p->read_op = pcap_read_npf;
  1041. p->setfilter_op = pcap_setfilter_npf;
  1042. #ifdef HAVE_DAG_API
  1043. }
  1044. #endif /* HAVE_DAG_API */
  1045. p->setdirection_op = NULL; /* Not implemented. */
  1046. /* XXX - can this be implemented on some versions of Windows? */
  1047. p->inject_op = pcap_inject_npf;
  1048. p->set_datalink_op = NULL; /* can't change data link type */
  1049. p->getnonblock_op = pcap_getnonblock_npf;
  1050. p->setnonblock_op = pcap_setnonblock_npf;
  1051. p->stats_op = pcap_stats_npf;
  1052. p->stats_ex_op = pcap_stats_ex_npf;
  1053. p->setbuff_op = pcap_setbuff_npf;
  1054. p->setmode_op = pcap_setmode_npf;
  1055. p->setmintocopy_op = pcap_setmintocopy_npf;
  1056. p->getevent_op = pcap_getevent_npf;
  1057. p->oid_get_request_op = pcap_oid_get_request_npf;
  1058. p->oid_set_request_op = pcap_oid_set_request_npf;
  1059. p->sendqueue_transmit_op = pcap_sendqueue_transmit_npf;
  1060. p->setuserbuffer_op = pcap_setuserbuffer_npf;
  1061. p->live_dump_op = pcap_live_dump_npf;
  1062. p->live_dump_ended_op = pcap_live_dump_ended_npf;
  1063. p->get_airpcap_handle_op = pcap_get_airpcap_handle_npf;
  1064. p->cleanup_op = pcap_cleanup_npf;
  1065. /*
  1066. * XXX - this is only done because WinPcap supported
  1067. * pcap_fileno() returning the hFile HANDLE from the
  1068. * ADAPTER structure. We make no general guarantees
  1069. * that the caller can do anything useful with it.
  1070. *
  1071. * (Not that we make any general guarantee of that
  1072. * sort on UN*X, either, any more, given that not
  1073. * all capture devices are regular OS network
  1074. * interfaces.)
  1075. */
  1076. p->handle = pw->adapter->hFile;
  1077. return (0);
  1078. bad:
  1079. pcap_cleanup_npf(p);
  1080. return (PCAP_ERROR);
  1081. }
  1082. /*
  1083. * Check if rfmon mode is supported on the pcap_t for Windows systems.
  1084. */
  1085. static int
  1086. pcap_can_set_rfmon_npf(pcap_t *p)
  1087. {
  1088. return (PacketIsMonitorModeSupported(p->opt.device) == 1);
  1089. }
  1090. pcap_t *
  1091. pcap_create_interface(const char *device _U_, char *ebuf)
  1092. {
  1093. pcap_t *p;
  1094. p = pcap_create_common(ebuf, sizeof(struct pcap_win));
  1095. if (p == NULL)
  1096. return (NULL);
  1097. p->activate_op = pcap_activate_npf;
  1098. p->can_set_rfmon_op = pcap_can_set_rfmon_npf;
  1099. return (p);
  1100. }
  1101. static int
  1102. pcap_setfilter_npf(pcap_t *p, struct bpf_program *fp)
  1103. {
  1104. struct pcap_win *pw = p->priv;
  1105. if(PacketSetBpf(pw->adapter,fp)==FALSE){
  1106. /*
  1107. * Kernel filter not installed.
  1108. *
  1109. * XXX - we don't know whether this failed because:
  1110. *
  1111. * the kernel rejected the filter program as invalid,
  1112. * in which case we should fall back on userland
  1113. * filtering;
  1114. *
  1115. * the kernel rejected the filter program as too big,
  1116. * in which case we should again fall back on
  1117. * userland filtering;
  1118. *
  1119. * there was some other problem, in which case we
  1120. * should probably report an error.
  1121. *
  1122. * For NPF devices, the Win32 status will be
  1123. * STATUS_INVALID_DEVICE_REQUEST for invalid
  1124. * filters, but I don't know what it'd be for
  1125. * other problems, and for some other devices
  1126. * it might not be set at all.
  1127. *
  1128. * So we just fall back on userland filtering in
  1129. * all cases.
  1130. */
  1131. /*
  1132. * install_bpf_program() validates the program.
  1133. *
  1134. * XXX - what if we already have a filter in the kernel?
  1135. */
  1136. if (install_bpf_program(p, fp) < 0)
  1137. return (-1);
  1138. pw->filtering_in_kernel = 0; /* filtering in userland */
  1139. return (0);
  1140. }
  1141. /*
  1142. * It worked.
  1143. */
  1144. pw->filtering_in_kernel = 1; /* filtering in the kernel */
  1145. /*
  1146. * Discard any previously-received packets, as they might have
  1147. * passed whatever filter was formerly in effect, but might
  1148. * not pass this filter (BIOCSETF discards packets buffered
  1149. * in the kernel, so you can lose packets in any case).
  1150. */
  1151. p->cc = 0;
  1152. return (0);
  1153. }
  1154. /*
  1155. * We filter at user level, since the kernel driver does't process the packets
  1156. */
  1157. static int
  1158. pcap_setfilter_win32_dag(pcap_t *p, struct bpf_program *fp) {
  1159. if(!fp)
  1160. {
  1161. strlcpy(p->errbuf, "setfilter: No filter specified", sizeof(p->errbuf));
  1162. return (-1);
  1163. }
  1164. /* Install a user level filter */
  1165. if (install_bpf_program(p, fp) < 0)
  1166. return (-1);
  1167. return (0);
  1168. }
  1169. static int
  1170. pcap_getnonblock_npf(pcap_t *p)
  1171. {
  1172. struct pcap_win *pw = p->priv;
  1173. /*
  1174. * XXX - if there were a PacketGetReadTimeout() call, we
  1175. * would use it, and return 1 if the timeout is -1
  1176. * and 0 otherwise.
  1177. */
  1178. return (pw->nonblock);
  1179. }
  1180. static int
  1181. pcap_setnonblock_npf(pcap_t *p, int nonblock)
  1182. {
  1183. struct pcap_win *pw = p->priv;
  1184. int newtimeout;
  1185. char win_errbuf[PCAP_ERRBUF_SIZE+1];
  1186. if (nonblock) {
  1187. /*
  1188. * Set the packet buffer timeout to -1 for non-blocking
  1189. * mode.
  1190. */
  1191. newtimeout = -1;
  1192. } else {
  1193. /*
  1194. * Restore the timeout set when the device was opened.
  1195. * (Note that this may be -1, in which case we're not
  1196. * really leaving non-blocking mode. However, although
  1197. * the timeout argument to pcap_set_timeout() and
  1198. * pcap_open_live() is an int, you're not supposed to
  1199. * supply a negative value, so that "shouldn't happen".)
  1200. */
  1201. newtimeout = p->opt.timeout;
  1202. }
  1203. if (!PacketSetReadTimeout(pw->adapter, newtimeout)) {
  1204. pcap_win32_err_to_str(GetLastError(), win_errbuf);
  1205. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1206. "PacketSetReadTimeout: %s", win_errbuf);
  1207. return (-1);
  1208. }
  1209. pw->nonblock = (newtimeout == -1);
  1210. return (0);
  1211. }
  1212. static int
  1213. pcap_add_if_npf(pcap_if_list_t *devlistp, char *name, bpf_u_int32 flags,
  1214. const char *description, char *errbuf)
  1215. {
  1216. pcap_if_t *curdev;
  1217. npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
  1218. LONG if_addr_size;
  1219. int res = 0;
  1220. if_addr_size = MAX_NETWORK_ADDRESSES;
  1221. /*
  1222. * Add an entry for this interface, with no addresses.
  1223. */
  1224. curdev = add_dev(devlistp, name, flags, description, errbuf);
  1225. if (curdev == NULL) {
  1226. /*
  1227. * Failure.
  1228. */
  1229. return (-1);
  1230. }
  1231. /*
  1232. * Get the list of addresses for the interface.
  1233. */
  1234. if (!PacketGetNetInfoEx((void *)name, if_addrs, &if_addr_size)) {
  1235. /*
  1236. * Failure.
  1237. *
  1238. * We don't return an error, because this can happen with
  1239. * NdisWan interfaces, and we want to supply them even
  1240. * if we can't supply their addresses.
  1241. *
  1242. * We return an entry with an empty address list.
  1243. */
  1244. return (0);
  1245. }
  1246. /*
  1247. * Now add the addresses.
  1248. */
  1249. while (if_addr_size-- > 0) {
  1250. /*
  1251. * "curdev" is an entry for this interface; add an entry for
  1252. * this address to its list of addresses.
  1253. */
  1254. res = add_addr_to_dev(curdev,
  1255. (struct sockaddr *)&if_addrs[if_addr_size].IPAddress,
  1256. sizeof (struct sockaddr_storage),
  1257. (struct sockaddr *)&if_addrs[if_addr_size].SubnetMask,
  1258. sizeof (struct sockaddr_storage),
  1259. (struct sockaddr *)&if_addrs[if_addr_size].Broadcast,
  1260. sizeof (struct sockaddr_storage),
  1261. NULL,
  1262. 0,
  1263. errbuf);
  1264. if (res == -1) {
  1265. /*
  1266. * Failure.
  1267. */
  1268. break;
  1269. }
  1270. }
  1271. return (res);
  1272. }
  1273. static int
  1274. get_if_flags(const char *name, bpf_u_int32 *flags, char *errbuf)
  1275. {
  1276. char *name_copy;
  1277. ADAPTER *adapter;
  1278. int status;
  1279. size_t len;
  1280. NDIS_HARDWARE_STATUS hardware_status;
  1281. #ifdef OID_GEN_PHYSICAL_MEDIUM
  1282. NDIS_PHYSICAL_MEDIUM phys_medium;
  1283. bpf_u_int32 gen_physical_medium_oids[] = {
  1284. #ifdef OID_GEN_PHYSICAL_MEDIUM_EX
  1285. OID_GEN_PHYSICAL_MEDIUM_EX,
  1286. #endif
  1287. OID_GEN_PHYSICAL_MEDIUM
  1288. };
  1289. #define N_GEN_PHYSICAL_MEDIUM_OIDS (sizeof gen_physical_medium_oids / sizeof gen_physical_medium_oids[0])
  1290. size_t i;
  1291. #endif /* OID_GEN_PHYSICAL_MEDIUM */
  1292. #ifdef OID_GEN_LINK_STATE
  1293. NDIS_LINK_STATE link_state;
  1294. #endif
  1295. int connect_status;
  1296. if (*flags & PCAP_IF_LOOPBACK) {
  1297. /*
  1298. * Loopback interface, so the connection status doesn't
  1299. * apply. and it's not wireless (or wired, for that
  1300. * matter...). We presume it's up and running.
  1301. */
  1302. *flags |= PCAP_IF_UP | PCAP_IF_RUNNING | PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
  1303. return (0);
  1304. }
  1305. /*
  1306. * We need to open the adapter to get this information.
  1307. *
  1308. * XXX - PacketOpenAdapter() takes a non-const pointer
  1309. * as an argument, so we make a copy of the argument and
  1310. * pass that to it.
  1311. */
  1312. name_copy = strdup(name);
  1313. adapter = PacketOpenAdapter(name_copy);
  1314. free(name_copy);
  1315. if (adapter == NULL) {
  1316. /*
  1317. * Give up; if they try to open this device, it'll fail.
  1318. */
  1319. return (0);
  1320. }
  1321. #ifdef HAVE_AIRPCAP_API
  1322. /*
  1323. * Airpcap.sys do not support the below 'OID_GEN_x' values.
  1324. * Just set these flags (and none of the '*flags' entered with).
  1325. */
  1326. if (PacketGetAirPcapHandle(adapter)) {
  1327. /*
  1328. * Must be "up" and "running" if the above if succeeded.
  1329. */
  1330. *flags = PCAP_IF_UP | PCAP_IF_RUNNING;
  1331. /*
  1332. * An airpcap device is a wireless device (duh!)
  1333. */
  1334. *flags |= PCAP_IF_WIRELESS;
  1335. /*
  1336. * A "network assosiation state" makes no sense for airpcap.
  1337. */
  1338. *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
  1339. PacketCloseAdapter(adapter);
  1340. return (0);
  1341. }
  1342. #endif
  1343. /*
  1344. * Get the hardware status, and derive "up" and "running" from
  1345. * that.
  1346. */
  1347. len = sizeof (hardware_status);
  1348. status = oid_get_request(adapter, OID_GEN_HARDWARE_STATUS,
  1349. &hardware_status, &len, errbuf);
  1350. if (status == 0) {
  1351. switch (hardware_status) {
  1352. case NdisHardwareStatusReady:
  1353. /*
  1354. * "Available and capable of sending and receiving
  1355. * data over the wire", so up and running.
  1356. */
  1357. *flags |= PCAP_IF_UP | PCAP_IF_RUNNING;
  1358. break;
  1359. case NdisHardwareStatusInitializing:
  1360. case NdisHardwareStatusReset:
  1361. /*
  1362. * "Initializing" or "Resetting", so up, but
  1363. * not running.
  1364. */
  1365. *flags |= PCAP_IF_UP;
  1366. break;
  1367. case NdisHardwareStatusClosing:
  1368. case NdisHardwareStatusNotReady:
  1369. /*
  1370. * "Closing" or "Not ready", so neither up nor
  1371. * running.
  1372. */
  1373. break;
  1374. }
  1375. } else {
  1376. /*
  1377. * Can't get the hardware status, so assume both up and
  1378. * running.
  1379. */
  1380. *flags |= PCAP_IF_UP | PCAP_IF_RUNNING;
  1381. }
  1382. /*
  1383. * Get the network type.
  1384. */
  1385. #ifdef OID_GEN_PHYSICAL_MEDIUM
  1386. /*
  1387. * Try the OIDs we have for this, in order.
  1388. */
  1389. for (i = 0; i < N_GEN_PHYSICAL_MEDIUM_OIDS; i++) {
  1390. len = sizeof (phys_medium);
  1391. status = oid_get_request(adapter, gen_physical_medium_oids[i],
  1392. &phys_medium, &len, errbuf);
  1393. if (status == 0) {
  1394. /*
  1395. * Success.
  1396. */
  1397. break;
  1398. }
  1399. /*
  1400. * Failed. We can't determine whether it failed
  1401. * because that particular OID isn't supported
  1402. * or because some other problem occurred, so we
  1403. * just drive on and try the next OID.
  1404. */
  1405. }
  1406. if (status == 0) {
  1407. /*
  1408. * We got the physical medium.
  1409. */
  1410. switch (phys_medium) {
  1411. case NdisPhysicalMediumWirelessLan:
  1412. case NdisPhysicalMediumWirelessWan:
  1413. case NdisPhysicalMediumNative802_11:
  1414. case NdisPhysicalMediumBluetooth:
  1415. case NdisPhysicalMediumUWB:
  1416. case NdisPhysicalMediumIrda:
  1417. /*
  1418. * Wireless.
  1419. */
  1420. *flags |= PCAP_IF_WIRELESS;
  1421. break;
  1422. default:
  1423. /*
  1424. * Not wireless.
  1425. */
  1426. break;
  1427. }
  1428. }
  1429. #endif
  1430. /*
  1431. * Get the connection status.
  1432. */
  1433. #ifdef OID_GEN_LINK_STATE
  1434. len = sizeof(link_state);
  1435. status = oid_get_request(adapter, OID_GEN_LINK_STATE, &link_state,
  1436. &len, errbuf);
  1437. if (status == 0) {
  1438. /*
  1439. * NOTE: this also gives us the receive and transmit
  1440. * link state.
  1441. */
  1442. switch (link_state.MediaConnectState) {
  1443. case MediaConnectStateConnected:
  1444. /*
  1445. * It's connected.
  1446. */
  1447. *flags |= PCAP_IF_CONNECTION_STATUS_CONNECTED;
  1448. break;
  1449. case MediaConnectStateDisconnected:
  1450. /*
  1451. * It's disconnected.
  1452. */
  1453. *flags |= PCAP_IF_CONNECTION_STATUS_DISCONNECTED;
  1454. break;
  1455. }
  1456. }
  1457. #else
  1458. /*
  1459. * OID_GEN_LINK_STATE isn't supported because it's not in our SDK.
  1460. */
  1461. status = -1;
  1462. #endif
  1463. if (status == -1) {
  1464. /*
  1465. * OK, OID_GEN_LINK_STATE didn't work, try
  1466. * OID_GEN_MEDIA_CONNECT_STATUS.
  1467. */
  1468. status = oid_get_request(adapter, OID_GEN_MEDIA_CONNECT_STATUS,
  1469. &connect_status, &len, errbuf);
  1470. if (status == 0) {
  1471. switch (connect_status) {
  1472. case NdisMediaStateConnected:
  1473. /*
  1474. * It's connected.
  1475. */
  1476. *flags |= PCAP_IF_CONNECTION_STATUS_CONNECTED;
  1477. break;
  1478. case NdisMediaStateDisconnected:
  1479. /*
  1480. * It's disconnected.
  1481. */
  1482. *flags |= PCAP_IF_CONNECTION_STATUS_DISCONNECTED;
  1483. break;
  1484. }
  1485. }
  1486. }
  1487. PacketCloseAdapter(adapter);
  1488. return (0);
  1489. }
  1490. int
  1491. pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
  1492. {
  1493. int ret = 0;
  1494. const char *desc;
  1495. char *AdaptersName;
  1496. ULONG NameLength;
  1497. char *name;
  1498. char our_errbuf[PCAP_ERRBUF_SIZE+1];
  1499. /*
  1500. * Find out how big a buffer we need.
  1501. *
  1502. * This call should always return FALSE; if the error is
  1503. * ERROR_INSUFFICIENT_BUFFER, NameLength will be set to
  1504. * the size of the buffer we need, otherwise there's a
  1505. * problem, and NameLength should be set to 0.
  1506. *
  1507. * It shouldn't require NameLength to be set, but,
  1508. * at least as of WinPcap 4.1.3, it checks whether
  1509. * NameLength is big enough before it checks for a
  1510. * NULL buffer argument, so, while it'll still do
  1511. * the right thing if NameLength is uninitialized and
  1512. * whatever junk happens to be there is big enough
  1513. * (because the pointer argument will be null), it's
  1514. * still reading an uninitialized variable.
  1515. */
  1516. NameLength = 0;
  1517. if (!PacketGetAdapterNames(NULL, &NameLength))
  1518. {
  1519. DWORD last_error = GetLastError();
  1520. if (last_error != ERROR_INSUFFICIENT_BUFFER)
  1521. {
  1522. pcap_win32_err_to_str(last_error, our_errbuf);
  1523. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  1524. "PacketGetAdapterNames: %s", our_errbuf);
  1525. return (-1);
  1526. }
  1527. }
  1528. if (NameLength <= 0)
  1529. return 0;
  1530. AdaptersName = (char*) malloc(NameLength);
  1531. if (AdaptersName == NULL)
  1532. {
  1533. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot allocate enough memory to list the adapters.");
  1534. return (-1);
  1535. }
  1536. if (!PacketGetAdapterNames(AdaptersName, &NameLength)) {
  1537. pcap_win32_err_to_str(GetLastError(), our_errbuf);
  1538. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "PacketGetAdapterNames: %s",
  1539. our_errbuf);
  1540. free(AdaptersName);
  1541. return (-1);
  1542. }
  1543. /*
  1544. * "PacketGetAdapterNames()" returned a list of
  1545. * null-terminated ASCII interface name strings,
  1546. * terminated by a null string, followed by a list
  1547. * of null-terminated ASCII interface description
  1548. * strings, terminated by a null string.
  1549. * This means there are two ASCII nulls at the end
  1550. * of the first list.
  1551. *
  1552. * Find the end of the first list; that's the
  1553. * beginning of the second list.
  1554. */
  1555. desc = &AdaptersName[0];
  1556. while (*desc != '\0' || *(desc + 1) != '\0')
  1557. desc++;
  1558. /*
  1559. * Found it - "desc" points to the first of the two
  1560. * nulls at the end of the list of names, so the
  1561. * first byte of the list of descriptions is two bytes
  1562. * after it.
  1563. */
  1564. desc += 2;
  1565. /*
  1566. * Loop over the elements in the first list.
  1567. */
  1568. name = &AdaptersName[0];
  1569. while (*name != '\0') {
  1570. bpf_u_int32 flags = 0;
  1571. #ifdef HAVE_PACKET_IS_LOOPBACK_ADAPTER
  1572. /*
  1573. * Is this a loopback interface?
  1574. */
  1575. if (PacketIsLoopbackAdapter(name)) {
  1576. /* Yes */
  1577. flags |= PCAP_IF_LOOPBACK;
  1578. }
  1579. #endif
  1580. /*
  1581. * Get additional flags.
  1582. */
  1583. if (get_if_flags(name, &flags, errbuf) == -1) {
  1584. /*
  1585. * Failure.
  1586. */
  1587. ret = -1;
  1588. break;
  1589. }
  1590. /*
  1591. * Add an entry for this interface.
  1592. */
  1593. if (pcap_add_if_npf(devlistp, name, flags, desc,
  1594. errbuf) == -1) {
  1595. /*
  1596. * Failure.
  1597. */
  1598. ret = -1;
  1599. break;
  1600. }
  1601. name += strlen(name) + 1;
  1602. desc += strlen(desc) + 1;
  1603. }
  1604. free(AdaptersName);
  1605. return (ret);
  1606. }
  1607. /*
  1608. * Return the name of a network interface attached to the system, or NULL
  1609. * if none can be found. The interface must be configured up; the
  1610. * lowest unit number is preferred; loopback is ignored.
  1611. *
  1612. * In the best of all possible worlds, this would be the same as on
  1613. * UN*X, but there may be software that expects this to return a
  1614. * full list of devices after the first device.
  1615. */
  1616. #define ADAPTERSNAME_LEN 8192
  1617. char *
  1618. pcap_lookupdev(char *errbuf)
  1619. {
  1620. DWORD dwVersion;
  1621. DWORD dwWindowsMajorVersion;
  1622. char our_errbuf[PCAP_ERRBUF_SIZE+1];
  1623. #pragma warning (push)
  1624. #pragma warning (disable: 4996) /* disable MSVC's GetVersion() deprecated warning here */
  1625. dwVersion = GetVersion(); /* get the OS version */
  1626. #pragma warning (pop)
  1627. dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
  1628. if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
  1629. /*
  1630. * Windows 95, 98, ME.
  1631. */
  1632. ULONG NameLength = ADAPTERSNAME_LEN;
  1633. static char AdaptersName[ADAPTERSNAME_LEN];
  1634. if (PacketGetAdapterNames(AdaptersName,&NameLength) )
  1635. return (AdaptersName);
  1636. else
  1637. return NULL;
  1638. } else {
  1639. /*
  1640. * Windows NT (NT 4.0 and later).
  1641. * Convert the names to Unicode for backward compatibility.
  1642. */
  1643. ULONG NameLength = ADAPTERSNAME_LEN;
  1644. static WCHAR AdaptersName[ADAPTERSNAME_LEN];
  1645. size_t BufferSpaceLeft;
  1646. char *tAstr;
  1647. WCHAR *Unameptr;
  1648. char *Adescptr;
  1649. size_t namelen, i;
  1650. WCHAR *TAdaptersName = (WCHAR*)malloc(ADAPTERSNAME_LEN * sizeof(WCHAR));
  1651. int NAdapts = 0;
  1652. if(TAdaptersName == NULL)
  1653. {
  1654. (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
  1655. return NULL;
  1656. }
  1657. if ( !PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength) )
  1658. {
  1659. pcap_win32_err_to_str(GetLastError(), our_errbuf);
  1660. (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  1661. "PacketGetAdapterNames: %s", our_errbuf);
  1662. free(TAdaptersName);
  1663. return NULL;
  1664. }
  1665. BufferSpaceLeft = ADAPTERSNAME_LEN * sizeof(WCHAR);
  1666. tAstr = (char*)TAdaptersName;
  1667. Unameptr = AdaptersName;
  1668. /*
  1669. * Convert the device names to Unicode into AdapterName.
  1670. */
  1671. do {
  1672. /*
  1673. * Length of the name, including the terminating
  1674. * NUL.
  1675. */
  1676. namelen = strlen(tAstr) + 1;
  1677. /*
  1678. * Do we have room for the name in the Unicode
  1679. * buffer?
  1680. */
  1681. if (BufferSpaceLeft < namelen * sizeof(WCHAR)) {
  1682. /*
  1683. * No.
  1684. */
  1685. goto quit;
  1686. }
  1687. BufferSpaceLeft -= namelen * sizeof(WCHAR);
  1688. /*
  1689. * Copy the name, converting ASCII to Unicode.
  1690. * namelen includes the NUL, so we copy it as
  1691. * well.
  1692. */
  1693. for (i = 0; i < namelen; i++)
  1694. *Unameptr++ = *tAstr++;
  1695. /*
  1696. * Count this adapter.
  1697. */
  1698. NAdapts++;
  1699. } while (namelen != 1);
  1700. /*
  1701. * Copy the descriptions, but don't convert them from
  1702. * ASCII to Unicode.
  1703. */
  1704. Adescptr = (char *)Unameptr;
  1705. while(NAdapts--)
  1706. {
  1707. size_t desclen;
  1708. desclen = strlen(tAstr) + 1;
  1709. /*
  1710. * Do we have room for the name in the Unicode
  1711. * buffer?
  1712. */
  1713. if (BufferSpaceLeft < desclen) {
  1714. /*
  1715. * No.
  1716. */
  1717. goto quit;
  1718. }
  1719. /*
  1720. * Just copy the ASCII string.
  1721. * namelen includes the NUL, so we copy it as
  1722. * well.
  1723. */
  1724. memcpy(Adescptr, tAstr, desclen);
  1725. Adescptr += desclen;
  1726. tAstr += desclen;
  1727. BufferSpaceLeft -= desclen;
  1728. }
  1729. quit:
  1730. free(TAdaptersName);
  1731. return (char *)(AdaptersName);
  1732. }
  1733. }
  1734. /*
  1735. * We can't use the same code that we use on UN*X, as that's doing
  1736. * UN*X-specific calls.
  1737. *
  1738. * We don't just fetch the entire list of devices, search for the
  1739. * particular device, and use its first IPv4 address, as that's too
  1740. * much work to get just one device's netmask.
  1741. */
  1742. int
  1743. pcap_lookupnet(const char *device, bpf_u_int32 *netp, bpf_u_int32 *maskp,
  1744. char *errbuf)
  1745. {
  1746. /*
  1747. * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
  1748. * in order to skip non IPv4 (i.e. IPv6 addresses)
  1749. */
  1750. npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
  1751. LONG if_addr_size = MAX_NETWORK_ADDRESSES;
  1752. struct sockaddr_in *t_addr;
  1753. LONG i;
  1754. if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
  1755. *netp = *maskp = 0;
  1756. return (0);
  1757. }
  1758. for(i = 0; i < if_addr_size; i++)
  1759. {
  1760. if(if_addrs[i].IPAddress.ss_family == AF_INET)
  1761. {
  1762. t_addr = (struct sockaddr_in *) &(if_addrs[i].IPAddress);
  1763. *netp = t_addr->sin_addr.S_un.S_addr;
  1764. t_addr = (struct sockaddr_in *) &(if_addrs[i].SubnetMask);
  1765. *maskp = t_addr->sin_addr.S_un.S_addr;
  1766. *netp &= *maskp;
  1767. return (0);
  1768. }
  1769. }
  1770. *netp = *maskp = 0;
  1771. return (0);
  1772. }
  1773. static const char *pcap_lib_version_string;
  1774. #ifdef HAVE_VERSION_H
  1775. /*
  1776. * libpcap being built for Windows, as part of a WinPcap/Npcap source
  1777. * tree. Include version.h from that source tree to get the WinPcap/Npcap
  1778. * version.
  1779. *
  1780. * XXX - it'd be nice if we could somehow generate the WinPcap version number
  1781. * when building WinPcap. (It'd be nice to do so for the packet.dll version
  1782. * number as well.)
  1783. */
  1784. #include "../../version.h"
  1785. static const char pcap_version_string[] =
  1786. WINPCAP_PRODUCT_NAME " version " WINPCAP_VER_STRING ", based on " PCAP_VERSION_STRING;
  1787. static const char pcap_version_string_packet_dll_fmt[] =
  1788. WINPCAP_PRODUCT_NAME " version " WINPCAP_VER_STRING " (packet.dll version %s), based on " PCAP_VERSION_STRING;
  1789. const char *
  1790. pcap_lib_version(void)
  1791. {
  1792. char *packet_version_string;
  1793. size_t full_pcap_version_string_len;
  1794. char *full_pcap_version_string;
  1795. if (pcap_lib_version_string == NULL) {
  1796. /*
  1797. * Generate the version string.
  1798. */
  1799. packet_version_string = PacketGetVersion();
  1800. if (strcmp(WINPCAP_VER_STRING, packet_version_string) == 0) {
  1801. /*
  1802. * WinPcap version string and packet.dll version
  1803. * string are the same; just report the WinPcap
  1804. * version.
  1805. */
  1806. pcap_lib_version_string = pcap_version_string;
  1807. } else {
  1808. /*
  1809. * WinPcap version string and packet.dll version
  1810. * string are different; that shouldn't be the
  1811. * case (the two libraries should come from the
  1812. * same version of WinPcap), so we report both
  1813. * versions.
  1814. *
  1815. * The -2 is for the %s in the format string,
  1816. * which will be replaced by packet_version_string.
  1817. */
  1818. full_pcap_version_string_len =
  1819. (sizeof pcap_version_string_packet_dll_fmt - 2) +
  1820. strlen(packet_version_string);
  1821. full_pcap_version_string = malloc(full_pcap_version_string_len);
  1822. if (full_pcap_version_string == NULL)
  1823. return (NULL);
  1824. pcap_snprintf(full_pcap_version_string,
  1825. full_pcap_version_string_len,
  1826. pcap_version_string_packet_dll_fmt,
  1827. packet_version_string);
  1828. }
  1829. pcap_lib_version_string = full_pcap_version_string;
  1830. }
  1831. return (pcap_lib_version_string);
  1832. }
  1833. #else /* HAVE_VERSION_H */
  1834. /*
  1835. * libpcap being built for Windows, not as part of a WinPcap/Npcap source
  1836. * tree.
  1837. */
  1838. static const char pcap_version_string_packet_dll_fmt[] =
  1839. PCAP_VERSION_STRING " (packet.dll version %s)";
  1840. const char *
  1841. pcap_lib_version(void)
  1842. {
  1843. char *packet_version_string;
  1844. size_t full_pcap_version_string_len;
  1845. char *full_pcap_version_string;
  1846. if (pcap_lib_version_string == NULL) {
  1847. /*
  1848. * Generate the version string. Report the packet.dll
  1849. * version.
  1850. *
  1851. * The -2 is for the %s in the format string, which will
  1852. * be replaced by packet_version_string.
  1853. */
  1854. packet_version_string = PacketGetVersion();
  1855. full_pcap_version_string_len =
  1856. (sizeof pcap_version_string_packet_dll_fmt - 2) +
  1857. strlen(packet_version_string);
  1858. full_pcap_version_string = malloc(full_pcap_version_string_len);
  1859. if (full_pcap_version_string == NULL)
  1860. return (NULL);
  1861. pcap_snprintf(full_pcap_version_string,
  1862. full_pcap_version_string_len,
  1863. pcap_version_string_packet_dll_fmt,
  1864. packet_version_string);
  1865. pcap_lib_version_string = full_pcap_version_string;
  1866. }
  1867. return (pcap_lib_version_string);
  1868. }
  1869. #endif /* HAVE_VERSION_H */