2 * Copyright (c) 2008 CACE Technologies, Davis (California)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of CACE Technologies nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 typedef TC_STATUS (TC_CALLCONV
*TcFcnQueryPortList
) (PTC_PORT
*ppPorts
, PULONG pLength
);
51 typedef TC_STATUS (TC_CALLCONV
*TcFcnFreePortList
) (TC_PORT
*pPorts
);
53 typedef PCHAR (TC_CALLCONV
*TcFcnStatusGetString
) (TC_STATUS status
);
55 typedef PCHAR (TC_CALLCONV
*TcFcnPortGetName
) (TC_PORT port
);
56 typedef PCHAR (TC_CALLCONV
*TcFcnPortGetDescription
) (TC_PORT port
);
58 typedef TC_STATUS (TC_CALLCONV
*TcFcnInstanceOpenByName
) (PCHAR name
, PTC_INSTANCE pInstance
);
59 typedef TC_STATUS (TC_CALLCONV
*TcFcnInstanceClose
) (TC_INSTANCE instance
);
60 typedef TC_STATUS (TC_CALLCONV
*TcFcnInstanceSetFeature
) (TC_INSTANCE instance
, ULONG feature
, ULONG value
);
61 typedef TC_STATUS (TC_CALLCONV
*TcFcnInstanceQueryFeature
) (TC_INSTANCE instance
, ULONG feature
, PULONG pValue
);
62 typedef TC_STATUS (TC_CALLCONV
*TcFcnInstanceReceivePackets
) (TC_INSTANCE instance
, PTC_PACKETS_BUFFER pBuffer
);
63 typedef HANDLE (TC_CALLCONV
*TcFcnInstanceGetReceiveWaitHandle
) (TC_INSTANCE instance
);
64 typedef TC_STATUS (TC_CALLCONV
*TcFcnInstanceTransmitPackets
) (TC_INSTANCE instance
, TC_PACKETS_BUFFER pBuffer
);
65 typedef TC_STATUS (TC_CALLCONV
*TcFcnInstanceQueryStatistics
) (TC_INSTANCE instance
, PTC_STATISTICS pStatistics
);
67 typedef TC_STATUS (TC_CALLCONV
*TcFcnPacketsBufferCreate
) (ULONG size
, PTC_PACKETS_BUFFER pBuffer
);
68 typedef VOID (TC_CALLCONV
*TcFcnPacketsBufferDestroy
) (TC_PACKETS_BUFFER buffer
);
69 typedef TC_STATUS (TC_CALLCONV
*TcFcnPacketsBufferQueryNextPacket
)(TC_PACKETS_BUFFER buffer
, PTC_PACKET_HEADER pHeader
, PVOID
*ppData
);
70 typedef TC_STATUS (TC_CALLCONV
*TcFcnPacketsBufferCommitNextPacket
)(TC_PACKETS_BUFFER buffer
, PTC_PACKET_HEADER pHeader
, PVOID pData
);
72 typedef VOID (TC_CALLCONV
*TcFcnStatisticsDestroy
) (TC_STATISTICS statistics
);
73 typedef TC_STATUS (TC_CALLCONV
*TcFcnStatisticsUpdate
) (TC_STATISTICS statistics
);
74 typedef TC_STATUS (TC_CALLCONV
*TcFcnStatisticsQueryValue
) (TC_STATISTICS statistics
, ULONG counterId
, PULONGLONG pValue
);
86 typedef struct _TC_FUNCTIONS
88 TC_API_LOAD_STATUS LoadStatus
;
90 HMODULE hTcApiDllHandle
;
92 TcFcnQueryPortList QueryPortList
;
93 TcFcnFreePortList FreePortList
;
94 TcFcnStatusGetString StatusGetString
;
96 TcFcnPortGetName PortGetName
;
97 TcFcnPortGetDescription PortGetDescription
;
99 TcFcnInstanceOpenByName InstanceOpenByName
;
100 TcFcnInstanceClose InstanceClose
;
101 TcFcnInstanceSetFeature InstanceSetFeature
;
102 TcFcnInstanceQueryFeature InstanceQueryFeature
;
103 TcFcnInstanceReceivePackets InstanceReceivePackets
;
105 TcFcnInstanceGetReceiveWaitHandle InstanceGetReceiveWaitHandle
;
107 TcFcnInstanceTransmitPackets InstanceTransmitPackets
;
108 TcFcnInstanceQueryStatistics InstanceQueryStatistics
;
110 TcFcnPacketsBufferCreate PacketsBufferCreate
;
111 TcFcnPacketsBufferDestroy PacketsBufferDestroy
;
112 TcFcnPacketsBufferQueryNextPacket PacketsBufferQueryNextPacket
;
113 TcFcnPacketsBufferCommitNextPacket PacketsBufferCommitNextPacket
;
115 TcFcnStatisticsDestroy StatisticsDestroy
;
116 TcFcnStatisticsUpdate StatisticsUpdate
;
117 TcFcnStatisticsQueryValue StatisticsQueryValue
;
121 static pcap_if_t
* TcCreatePcapIfFromPort(TC_PORT port
);
122 static int TcSetDatalink(pcap_t
*p
, int dlt
);
123 static int TcGetNonBlock(pcap_t
*p
);
124 static int TcSetNonBlock(pcap_t
*p
, int nonblock
);
125 static void TcCleanup(pcap_t
*p
);
126 static int TcInject(pcap_t
*p
, const void *buf
, int size
);
127 static int TcRead(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
);
128 static int TcStats(pcap_t
*p
, struct pcap_stat
*ps
);
130 static struct pcap_stat
*TcStatsEx(pcap_t
*p
, int *pcap_stat_size
);
131 static int TcSetBuff(pcap_t
*p
, int dim
);
132 static int TcSetMode(pcap_t
*p
, int mode
);
133 static int TcSetMinToCopy(pcap_t
*p
, int size
);
134 static HANDLE
TcGetReceiveWaitHandle(pcap_t
*p
);
135 static int TcOidGetRequest(pcap_t
*p
, bpf_u_int32 oid
, void *data
, size_t *lenp
);
136 static int TcOidSetRequest(pcap_t
*p
, bpf_u_int32 oid
, const void *data
, size_t *lenp
);
137 static u_int
TcSendqueueTransmit(pcap_t
*p
, pcap_send_queue
*queue
, int sync
);
138 static int TcSetUserBuffer(pcap_t
*p
, int size
);
139 static int TcLiveDump(pcap_t
*p
, char *filename
, int maxsize
, int maxpacks
);
140 static int TcLiveDumpEnded(pcap_t
*p
, int sync
);
141 static PAirpcapHandle
TcGetAirPcapHandle(pcap_t
*p
);
145 TC_FUNCTIONS g_TcFunctions
=
147 TC_API_UNLOADED
, /* LoadStatus */
148 NULL
, /* hTcApiDllHandle */
149 NULL
, /* QueryPortList */
150 NULL
, /* FreePortList */
151 NULL
, /* StatusGetString */
152 NULL
, /* PortGetName */
153 NULL
, /* PortGetDescription */
154 NULL
, /* InstanceOpenByName */
155 NULL
, /* InstanceClose */
156 NULL
, /* InstanceSetFeature */
157 NULL
, /* InstanceQueryFeature */
158 NULL
, /* InstanceReceivePackets */
159 NULL
, /* InstanceGetReceiveWaitHandle */
160 NULL
, /* InstanceTransmitPackets */
161 NULL
, /* InstanceQueryStatistics */
162 NULL
, /* PacketsBufferCreate */
163 NULL
, /* PacketsBufferDestroy */
164 NULL
, /* PacketsBufferQueryNextPacket */
165 NULL
, /* PacketsBufferCommitNextPacket */
166 NULL
, /* StatisticsDestroy */
167 NULL
, /* StatisticsUpdate */
168 NULL
/* StatisticsQueryValue */
171 TC_FUNCTIONS g_TcFunctions
=
173 TC_API_LOADED
, /* LoadStatus */
178 TcPortGetDescription
,
179 TcInstanceOpenByName
,
181 TcInstanceSetFeature
,
182 TcInstanceQueryFeature
,
183 TcInstanceReceivePackets
,
185 TcInstanceGetReceiveWaitHandle
,
187 TcInstanceTransmitPackets
,
188 TcInstanceQueryStatistics
,
189 TcPacketsBufferCreate
,
190 TcPacketsBufferDestroy
,
191 TcPacketsBufferQueryNextPacket
,
192 TcPacketsBufferCommitNextPacket
,
195 TcStatisticsQueryValue
,
199 #define MAX_TC_PACKET_SIZE 9500
201 #pragma pack(push, 1)
203 #define PPH_PH_FLAG_PADDING ((UCHAR)0x01)
204 #define PPH_PH_VERSION ((UCHAR)0x00)
206 typedef struct _PPI_PACKET_HEADER
213 PPI_PACKET_HEADER
, *PPPI_PACKET_HEADER
;
215 typedef struct _PPI_FIELD_HEADER
220 PPI_FIELD_HEADER
, *PPPI_FIELD_HEADER
;
223 #define PPI_FIELD_TYPE_AGGREGATION_EXTENSION ((UCHAR)0x08)
225 typedef struct _PPI_FIELD_AGGREGATION_EXTENSION
229 PPI_FIELD_AGGREGATION_EXTENSION
, *PPPI_FIELD_AGGREGATION_EXTENSION
;
232 #define PPI_FIELD_TYPE_802_3_EXTENSION ((UCHAR)0x09)
234 #define PPI_FLD_802_3_EXT_FLAG_FCS_PRESENT ((ULONG)0x00000001)
236 typedef struct _PPI_FIELD_802_3_EXTENSION
241 PPI_FIELD_802_3_EXTENSION
, *PPPI_FIELD_802_3_EXTENSION
;
243 typedef struct _PPI_HEADER
245 PPI_PACKET_HEADER PacketHeader
;
246 PPI_FIELD_HEADER AggregationFieldHeader
;
247 PPI_FIELD_AGGREGATION_EXTENSION AggregationField
;
248 PPI_FIELD_HEADER Dot3FieldHeader
;
249 PPI_FIELD_802_3_EXTENSION Dot3Field
;
251 PPI_HEADER
, *PPPI_HEADER
;
256 * NOTE: this function should be called by the pcap functions that can theoretically
257 * deal with the Tc library for the first time, namely listing the adapters and
258 * opening one. All the other ones (close, read, write, set parameters) work
259 * on an open instance of TC, so we do not care to call this function
261 TC_API_LOAD_STATUS
LoadTcFunctions(void)
263 TC_API_LOAD_STATUS currentStatus
;
267 currentStatus
= InterlockedCompareExchange((LONG
*)&g_TcFunctions
.LoadStatus
, TC_API_LOADING
, TC_API_UNLOADED
);
269 while(currentStatus
== TC_API_LOADING
)
271 currentStatus
= InterlockedCompareExchange((LONG
*)&g_TcFunctions
.LoadStatus
, TC_API_LOADING
, TC_API_LOADING
);
276 * at this point we are either in the LOADED state, unloaded state (i.e. we are the ones loading everything)
279 if(currentStatus
== TC_API_LOADED
)
281 return TC_API_LOADED
;
284 if (currentStatus
== TC_API_CANNOT_LOAD
)
286 return TC_API_CANNOT_LOAD
;
289 currentStatus
= TC_API_CANNOT_LOAD
;
291 g_TcFunctions
.hTcApiDllHandle
= pcap_load_code("TcApi.dll");
292 if (g_TcFunctions
.hTcApiDllHandle
== NULL
) break;
294 g_TcFunctions
.QueryPortList
= (TcFcnQueryPortList
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcQueryPortList");
295 g_TcFunctions
.FreePortList
= (TcFcnFreePortList
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcFreePortList");
297 g_TcFunctions
.StatusGetString
= (TcFcnStatusGetString
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcStatusGetString");
299 g_TcFunctions
.PortGetName
= (TcFcnPortGetName
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcPortGetName");
300 g_TcFunctions
.PortGetDescription
= (TcFcnPortGetDescription
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcPortGetDescription");
302 g_TcFunctions
.InstanceOpenByName
= (TcFcnInstanceOpenByName
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcInstanceOpenByName");
303 g_TcFunctions
.InstanceClose
= (TcFcnInstanceClose
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcInstanceClose");
304 g_TcFunctions
.InstanceSetFeature
= (TcFcnInstanceSetFeature
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcInstanceSetFeature");
305 g_TcFunctions
.InstanceQueryFeature
= (TcFcnInstanceQueryFeature
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcInstanceQueryFeature");
306 g_TcFunctions
.InstanceReceivePackets
= (TcFcnInstanceReceivePackets
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcInstanceReceivePackets");
307 g_TcFunctions
.InstanceGetReceiveWaitHandle
= (TcFcnInstanceGetReceiveWaitHandle
)pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcInstanceGetReceiveWaitHandle");
308 g_TcFunctions
.InstanceTransmitPackets
= (TcFcnInstanceTransmitPackets
)pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcInstanceTransmitPackets");
309 g_TcFunctions
.InstanceQueryStatistics
= (TcFcnInstanceQueryStatistics
)pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcInstanceQueryStatistics");
311 g_TcFunctions
.PacketsBufferCreate
= (TcFcnPacketsBufferCreate
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcPacketsBufferCreate");
312 g_TcFunctions
.PacketsBufferDestroy
= (TcFcnPacketsBufferDestroy
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcPacketsBufferDestroy");
313 g_TcFunctions
.PacketsBufferQueryNextPacket
= (TcFcnPacketsBufferQueryNextPacket
)pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcPacketsBufferQueryNextPacket");
314 g_TcFunctions
.PacketsBufferCommitNextPacket
= (TcFcnPacketsBufferCommitNextPacket
)pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcPacketsBufferCommitNextPacket");
316 g_TcFunctions
.StatisticsDestroy
= (TcFcnStatisticsDestroy
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcStatisticsDestroy");
317 g_TcFunctions
.StatisticsUpdate
= (TcFcnStatisticsUpdate
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcStatisticsUpdate");
318 g_TcFunctions
.StatisticsQueryValue
= (TcFcnStatisticsQueryValue
) pcap_find_function(g_TcFunctions
.hTcApiDllHandle
, "TcStatisticsQueryValue");
320 if ( g_TcFunctions
.QueryPortList
== NULL
321 || g_TcFunctions
.FreePortList
== NULL
322 || g_TcFunctions
.StatusGetString
== NULL
323 || g_TcFunctions
.PortGetName
== NULL
324 || g_TcFunctions
.PortGetDescription
== NULL
325 || g_TcFunctions
.InstanceOpenByName
== NULL
326 || g_TcFunctions
.InstanceClose
== NULL
327 || g_TcFunctions
.InstanceSetFeature
== NULL
328 || g_TcFunctions
.InstanceQueryFeature
== NULL
329 || g_TcFunctions
.InstanceReceivePackets
== NULL
330 || g_TcFunctions
.InstanceGetReceiveWaitHandle
== NULL
331 || g_TcFunctions
.InstanceTransmitPackets
== NULL
332 || g_TcFunctions
.InstanceQueryStatistics
== NULL
333 || g_TcFunctions
.PacketsBufferCreate
== NULL
334 || g_TcFunctions
.PacketsBufferDestroy
== NULL
335 || g_TcFunctions
.PacketsBufferQueryNextPacket
== NULL
336 || g_TcFunctions
.PacketsBufferCommitNextPacket
== NULL
337 || g_TcFunctions
.StatisticsDestroy
== NULL
338 || g_TcFunctions
.StatisticsUpdate
== NULL
339 || g_TcFunctions
.StatisticsQueryValue
== NULL
346 * everything got loaded, yay!!
348 currentStatus
= TC_API_LOADED
;
351 if (currentStatus
!= TC_API_LOADED
)
353 if (g_TcFunctions
.hTcApiDllHandle
!= NULL
)
355 FreeLibrary(g_TcFunctions
.hTcApiDllHandle
);
356 g_TcFunctions
.hTcApiDllHandle
= NULL
;
360 InterlockedExchange((LONG
*)&g_TcFunctions
.LoadStatus
, currentStatus
);
362 return currentStatus
;
366 TC_API_LOAD_STATUS
LoadTcFunctions(void)
368 return TC_API_LOADED
;
373 * Private data for capturing on TurboCap devices.
376 TC_INSTANCE TcInstance
;
377 TC_PACKETS_BUFFER TcPacketsBuffer
;
378 ULONG TcAcceptedCount
;
383 TcFindAllDevs(pcap_if_list_t
*devlist
, char *errbuf
)
385 TC_API_LOAD_STATUS loadStatus
;
387 PTC_PORT pPorts
= NULL
;
395 loadStatus
= LoadTcFunctions();
397 if (loadStatus
!= TC_API_LOADED
)
404 * enumerate the ports, and add them to the list
406 status
= g_TcFunctions
.QueryPortList(&pPorts
, &numPorts
);
408 if (status
!= TC_SUCCESS
)
414 for (i
= 0; i
< numPorts
; i
++)
417 * transform the port into an entry in the list
419 dev
= TcCreatePcapIfFromPort(pPorts
[i
]);
422 pcap_add_dev(devlist
, dev
->name
, dev
->flags
, dev
->description
, errbuf
);
428 * ignore the result here
430 status
= g_TcFunctions
.FreePortList(pPorts
);
438 static pcap_if_t
* TcCreatePcapIfFromPort(TC_PORT port
)
442 pcap_if_t
*newIf
= NULL
;
444 newIf
= (pcap_if_t
*)malloc(sizeof(*newIf
));
450 memset(newIf
, 0, sizeof(*newIf
));
452 name
= g_TcFunctions
.PortGetName(port
);
453 description
= g_TcFunctions
.PortGetDescription(port
);
455 newIf
->name
= (char*)malloc(strlen(name
) + 1);
456 if (newIf
->name
== NULL
)
462 newIf
->description
= (char*)malloc(strlen(description
) + 1);
463 if (newIf
->description
== NULL
)
470 strcpy(newIf
->name
, name
);
471 strcpy(newIf
->description
, description
);
473 newIf
->addresses
= NULL
;
482 TcActivate(pcap_t
*p
)
484 struct pcap_tc
*pt
= p
->priv
;
487 PPPI_HEADER pPpiHeader
;
492 * No monitor mode on Tc cards; they're Ethernet
495 return PCAP_ERROR_RFMON_NOTSUP
;
498 pt
->PpiPacket
= malloc(sizeof(PPI_HEADER
) + MAX_TC_PACKET_SIZE
);
500 if (pt
->PpiPacket
== NULL
)
502 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Error allocating memory");
507 * Turn a negative snapshot value (invalid), a snapshot value of
508 * 0 (unspecified), or a value bigger than the normal maximum
509 * value, into the maximum allowed value.
511 * If some application really *needs* a bigger snapshot
512 * length, we should just increase MAXIMUM_SNAPLEN.
514 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
515 p
->snapshot
= MAXIMUM_SNAPLEN
;
518 * Initialize the PPI fixed fields
520 pPpiHeader
= (PPPI_HEADER
)pt
->PpiPacket
;
521 pPpiHeader
->PacketHeader
.PphDlt
= DLT_EN10MB
;
522 pPpiHeader
->PacketHeader
.PphLength
= sizeof(PPI_HEADER
);
523 pPpiHeader
->PacketHeader
.PphFlags
= 0;
524 pPpiHeader
->PacketHeader
.PphVersion
= 0;
526 pPpiHeader
->AggregationFieldHeader
.PfhLength
= sizeof(PPI_FIELD_AGGREGATION_EXTENSION
);
527 pPpiHeader
->AggregationFieldHeader
.PfhType
= PPI_FIELD_TYPE_AGGREGATION_EXTENSION
;
529 pPpiHeader
->Dot3FieldHeader
.PfhLength
= sizeof(PPI_FIELD_802_3_EXTENSION
);
530 pPpiHeader
->Dot3FieldHeader
.PfhType
= PPI_FIELD_TYPE_802_3_EXTENSION
;
532 status
= g_TcFunctions
.InstanceOpenByName(p
->opt
.device
, &pt
->TcInstance
);
534 if (status
!= TC_SUCCESS
)
536 /* Adapter detected but we are not able to open it. Return failure. */
537 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Error opening TurboCap adapter: %s", g_TcFunctions
.StatusGetString(status
));
541 p
->linktype
= DLT_EN10MB
;
542 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
544 * If that fails, just leave the list empty.
546 if (p
->dlt_list
!= NULL
) {
547 p
->dlt_list
[0] = DLT_EN10MB
;
548 p
->dlt_list
[1] = DLT_PPI
;
553 * ignore promiscuous mode
559 * ignore all the buffer sizes
565 status
= g_TcFunctions
.InstanceSetFeature(pt
->TcInstance
, TC_INST_FT_RX_STATUS
, 1);
567 if (status
!= TC_SUCCESS
)
569 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,"Error enabling reception on a TurboCap instance: %s", g_TcFunctions
.StatusGetString(status
));
574 * enable transmission
576 status
= g_TcFunctions
.InstanceSetFeature(pt
->TcInstance
, TC_INST_FT_TX_STATUS
, 1);
578 * Ignore the error here.
581 p
->inject_op
= TcInject
;
583 * if the timeout is -1, it means immediate return, no timeout
584 * if the timeout is 0, it means INFINITE
587 if (p
->opt
.timeout
== 0)
589 timeout
= 0xFFFFFFFF;
592 if (p
->opt
.timeout
< 0)
595 * we insert a minimal timeout here
601 timeout
= p
->opt
.timeout
;
604 status
= g_TcFunctions
.InstanceSetFeature(pt
->TcInstance
, TC_INST_FT_READ_TIMEOUT
, timeout
);
606 if (status
!= TC_SUCCESS
)
608 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,"Error setting the read timeout a TurboCap instance: %s", g_TcFunctions
.StatusGetString(status
));
613 p
->setfilter_op
= pcap_install_bpf_program
;
614 p
->setdirection_op
= NULL
; /* Not implemented. */
615 p
->set_datalink_op
= TcSetDatalink
;
616 p
->getnonblock_op
= TcGetNonBlock
;
617 p
->setnonblock_op
= TcSetNonBlock
;
618 p
->stats_op
= TcStats
;
620 p
->stats_ex_op
= TcStatsEx
;
621 p
->setbuff_op
= TcSetBuff
;
622 p
->setmode_op
= TcSetMode
;
623 p
->setmintocopy_op
= TcSetMinToCopy
;
624 p
->getevent_op
= TcGetReceiveWaitHandle
;
625 p
->oid_get_request_op
= TcOidGetRequest
;
626 p
->oid_set_request_op
= TcOidSetRequest
;
627 p
->sendqueue_transmit_op
= TcSendqueueTransmit
;
628 p
->setuserbuffer_op
= TcSetUserBuffer
;
629 p
->live_dump_op
= TcLiveDump
;
630 p
->live_dump_ended_op
= TcLiveDumpEnded
;
631 p
->get_airpcap_handle_op
= TcGetAirPcapHandle
;
633 p
->selectable_fd
= -1;
636 p
->cleanup_op
= TcCleanup
;
645 TcCreate(const char *device
, char *ebuf
, int *is_ours
)
648 PTC_PORT pPorts
= NULL
;
654 if (LoadTcFunctions() != TC_API_LOADED
)
657 * XXX - report this as an error rather than as
658 * "not a TurboCap device"?
665 * enumerate the ports, and add them to the list
667 status
= g_TcFunctions
.QueryPortList(&pPorts
, &numPorts
);
669 if (status
!= TC_SUCCESS
)
672 * XXX - report this as an error rather than as
673 * "not a TurboCap device"?
680 for (i
= 0; i
< numPorts
; i
++)
682 if (strcmp(g_TcFunctions
.PortGetName(pPorts
[i
]), device
) == 0)
692 * ignore the result here
694 (void)g_TcFunctions
.FreePortList(pPorts
);
703 /* OK, it's probably ours. */
706 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_tc
);
710 p
->activate_op
= TcActivate
;
712 * Set these up front, so that, even if our client tries
713 * to set non-blocking mode before we're activated, or
714 * query the state of non-blocking mode, they get an error,
715 * rather than having the non-blocking mode option set
718 p
->getnonblock_op
= TcGetNonBlock
;
719 p
->setnonblock_op
= TcSetNonBlock
;
723 static int TcSetDatalink(pcap_t
*p
, int dlt
)
726 * We don't have to do any work here; pcap_set_datalink() checks
727 * whether the value is in the list of DLT_ values we
728 * supplied, so we don't have to, and, if it is valid, sets
729 * p->linktype to the new value; we don't have to do anything
730 * in hardware, we just use what's in p->linktype.
732 * We do have to have a routine, however, so that pcap_set_datalink()
733 * doesn't think we don't support setting the link-layer header
739 static int TcGetNonBlock(pcap_t
*p
)
741 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
742 "Non-blocking mode isn't supported for TurboCap ports");
746 static int TcSetNonBlock(pcap_t
*p
, int nonblock
)
748 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
749 "Non-blocking mode isn't supported for TurboCap ports");
753 static void TcCleanup(pcap_t
*p
)
755 struct pcap_tc
*pt
= p
->priv
;
757 if (pt
->TcPacketsBuffer
!= NULL
)
759 g_TcFunctions
.PacketsBufferDestroy(pt
->TcPacketsBuffer
);
760 pt
->TcPacketsBuffer
= NULL
;
762 if (pt
->TcInstance
!= NULL
)
765 * here we do not check for the error values
767 g_TcFunctions
.InstanceClose(pt
->TcInstance
);
768 pt
->TcInstance
= NULL
;
771 if (pt
->PpiPacket
!= NULL
)
774 pt
->PpiPacket
= NULL
;
777 pcap_cleanup_live_common(p
);
780 /* Send a packet to the network */
781 static int TcInject(pcap_t
*p
, const void *buf
, int size
)
783 struct pcap_tc
*pt
= p
->priv
;
785 TC_PACKETS_BUFFER buffer
;
786 TC_PACKET_HEADER header
;
790 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: the TurboCap API does not support packets larger than 64k");
794 status
= g_TcFunctions
.PacketsBufferCreate(sizeof(TC_PACKET_HEADER
) + TC_ALIGN_USHORT_TO_64BIT((USHORT
)size
), &buffer
);
796 if (status
!= TC_SUCCESS
)
798 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: TcPacketsBufferCreate failure: %s (%08x)", g_TcFunctions
.StatusGetString(status
), status
);
803 * we assume that the packet is without the checksum, as common with WinPcap
805 memset(&header
, 0, sizeof(header
));
807 header
.Length
= (USHORT
)size
;
808 header
.CapturedLength
= header
.Length
;
810 status
= g_TcFunctions
.PacketsBufferCommitNextPacket(buffer
, &header
, (PVOID
)buf
);
812 if (status
== TC_SUCCESS
)
814 status
= g_TcFunctions
.InstanceTransmitPackets(pt
->TcInstance
, buffer
);
816 if (status
!= TC_SUCCESS
)
818 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: TcInstanceTransmitPackets failure: %s (%08x)", g_TcFunctions
.StatusGetString(status
), status
);
823 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: TcPacketsBufferCommitNextPacket failure: %s (%08x)", g_TcFunctions
.StatusGetString(status
), status
);
826 g_TcFunctions
.PacketsBufferDestroy(buffer
);
828 if (status
!= TC_SUCCESS
)
838 static int TcRead(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
840 struct pcap_tc
*pt
= p
->priv
;
845 * Has "pcap_breakloop()" been called?
850 * Yes - clear the flag that indicates that it
851 * has, and return -2 to indicate that we were
852 * told to break out of the loop.
858 if (pt
->TcPacketsBuffer
== NULL
)
860 status
= g_TcFunctions
.InstanceReceivePackets(pt
->TcInstance
, &pt
->TcPacketsBuffer
);
861 if (status
!= TC_SUCCESS
)
863 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error, TcInstanceReceivePackets failure: %s (%08x)", g_TcFunctions
.StatusGetString(status
), status
);
870 struct pcap_pkthdr hdr
;
871 TC_PACKET_HEADER tcHeader
;
876 * Has "pcap_breakloop()" been called?
877 * If so, return immediately - if we haven't read any
878 * packets, clear the flag and return -2 to indicate
879 * that we were told to break out of the loop, otherwise
880 * leave the flag set, so that the *next* call will break
881 * out of the loop without having read any packets, and
882 * return the number of packets we've processed so far.
897 if (pt
->TcPacketsBuffer
== NULL
)
902 status
= g_TcFunctions
.PacketsBufferQueryNextPacket(pt
->TcPacketsBuffer
, &tcHeader
, &data
);
904 if (status
== TC_ERROR_END_OF_BUFFER
)
906 g_TcFunctions
.PacketsBufferDestroy(pt
->TcPacketsBuffer
);
907 pt
->TcPacketsBuffer
= NULL
;
911 if (status
!= TC_SUCCESS
)
913 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error, TcPacketsBufferQueryNextPacket failure: %s (%08x)", g_TcFunctions
.StatusGetString(status
), status
);
917 /* No underlying filtering system. We need to filter on our own */
918 if (p
->fcode
.bf_insns
)
920 filterResult
= pcap_filter(p
->fcode
.bf_insns
, data
, tcHeader
.Length
, tcHeader
.CapturedLength
);
922 if (filterResult
== 0)
927 if (filterResult
> tcHeader
.CapturedLength
)
929 filterResult
= tcHeader
.CapturedLength
;
934 filterResult
= tcHeader
.CapturedLength
;
937 pt
->TcAcceptedCount
++;
939 hdr
.ts
.tv_sec
= (bpf_u_int32
)(tcHeader
.Timestamp
/ (ULONGLONG
)(1000 * 1000 * 1000));
940 hdr
.ts
.tv_usec
= (bpf_u_int32
)((tcHeader
.Timestamp
% (ULONGLONG
)(1000 * 1000 * 1000)) / 1000);
942 if (p
->linktype
== DLT_EN10MB
)
944 hdr
.caplen
= filterResult
;
945 hdr
.len
= tcHeader
.Length
;
946 (*callback
)(user
, &hdr
, data
);
950 PPPI_HEADER pPpiHeader
= (PPPI_HEADER
)pt
->PpiPacket
;
951 PVOID data2
= pPpiHeader
+ 1;
953 pPpiHeader
->AggregationField
.InterfaceId
= TC_PH_FLAGS_RX_PORT_ID(tcHeader
.Flags
);
954 pPpiHeader
->Dot3Field
.Errors
= tcHeader
.Errors
;
955 if (tcHeader
.Flags
& TC_PH_FLAGS_CHECKSUM
)
957 pPpiHeader
->Dot3Field
.Flags
= PPI_FLD_802_3_EXT_FLAG_FCS_PRESENT
;
961 pPpiHeader
->Dot3Field
.Flags
= 0;
964 if (filterResult
<= MAX_TC_PACKET_SIZE
)
966 memcpy(data2
, data
, filterResult
);
967 hdr
.caplen
= sizeof(PPI_HEADER
) + filterResult
;
968 hdr
.len
= sizeof(PPI_HEADER
) + tcHeader
.Length
;
972 memcpy(data2
, data
, MAX_TC_PACKET_SIZE
);
973 hdr
.caplen
= sizeof(PPI_HEADER
) + MAX_TC_PACKET_SIZE
;
974 hdr
.len
= sizeof(PPI_HEADER
) + tcHeader
.Length
;
977 (*callback
)(user
, &hdr
, pt
->PpiPacket
);
981 if (++n
>= cnt
&& cnt
> 0)
991 TcStats(pcap_t
*p
, struct pcap_stat
*ps
)
993 struct pcap_tc
*pt
= p
->priv
;
994 TC_STATISTICS statistics
;
999 status
= g_TcFunctions
.InstanceQueryStatistics(pt
->TcInstance
, &statistics
);
1001 if (status
!= TC_SUCCESS
)
1003 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "TurboCap error in TcInstanceQueryStatistics: %s (%08x)", g_TcFunctions
.StatusGetString(status
), status
);
1007 memset(&s
, 0, sizeof(s
));
1009 status
= g_TcFunctions
.StatisticsQueryValue(statistics
, TC_COUNTER_INSTANCE_TOTAL_RX_PACKETS
, &counter
);
1010 if (status
!= TC_SUCCESS
)
1012 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "TurboCap error in TcStatisticsQueryValue: %s (%08x)", g_TcFunctions
.StatusGetString(status
), status
);
1015 if (counter
<= (ULONGLONG
)0xFFFFFFFF)
1017 s
.ps_recv
= (ULONG
)counter
;
1021 s
.ps_recv
= 0xFFFFFFFF;
1024 status
= g_TcFunctions
.StatisticsQueryValue(statistics
, TC_COUNTER_INSTANCE_RX_DROPPED_PACKETS
, &counter
);
1025 if (status
!= TC_SUCCESS
)
1027 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "TurboCap error in TcStatisticsQueryValue: %s (%08x)", g_TcFunctions
.StatusGetString(status
), status
);
1030 if (counter
<= (ULONGLONG
)0xFFFFFFFF)
1032 s
.ps_ifdrop
= (ULONG
)counter
;
1033 s
.ps_drop
= (ULONG
)counter
;
1037 s
.ps_ifdrop
= 0xFFFFFFFF;
1038 s
.ps_drop
= 0xFFFFFFFF;
1041 #if defined(_WIN32) && defined(ENABLE_REMOTE)
1042 s
.ps_capt
= pt
->TcAcceptedCount
;
1051 static struct pcap_stat
*
1052 TcStatsEx(pcap_t
*p
, int *pcap_stat_size
)
1054 struct pcap_tc
*pt
= p
->priv
;
1055 TC_STATISTICS statistics
;
1059 *pcap_stat_size
= sizeof (p
->stat
);
1061 status
= g_TcFunctions
.InstanceQueryStatistics(pt
->TcInstance
, &statistics
);
1063 if (status
!= TC_SUCCESS
)
1065 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "TurboCap error in TcInstanceQueryStatistics: %s (%08x)", g_TcFunctions
.StatusGetString(status
), status
);
1069 memset(&p
->stat
, 0, sizeof(p
->stat
));
1071 status
= g_TcFunctions
.StatisticsQueryValue(statistics
, TC_COUNTER_INSTANCE_TOTAL_RX_PACKETS
, &counter
);
1072 if (status
!= TC_SUCCESS
)
1074 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "TurboCap error in TcStatisticsQueryValue: %s (%08x)", g_TcFunctions
.StatusGetString(status
), status
);
1077 if (counter
<= (ULONGLONG
)0xFFFFFFFF)
1079 p
->stat
.ps_recv
= (ULONG
)counter
;
1083 p
->stat
.ps_recv
= 0xFFFFFFFF;
1086 status
= g_TcFunctions
.StatisticsQueryValue(statistics
, TC_COUNTER_INSTANCE_RX_DROPPED_PACKETS
, &counter
);
1087 if (status
!= TC_SUCCESS
)
1089 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "TurboCap error in TcStatisticsQueryValue: %s (%08x)", g_TcFunctions
.StatusGetString(status
), status
);
1092 if (counter
<= (ULONGLONG
)0xFFFFFFFF)
1094 p
->stat
.ps_ifdrop
= (ULONG
)counter
;
1095 p
->stat
.ps_drop
= (ULONG
)counter
;
1099 p
->stat
.ps_ifdrop
= 0xFFFFFFFF;
1100 p
->stat
.ps_drop
= 0xFFFFFFFF;
1103 #if defined(_WIN32) && defined(ENABLE_REMOTE)
1104 p
->stat
.ps_capt
= pt
->TcAcceptedCount
;
1110 /* Set the dimension of the kernel-level capture buffer */
1112 TcSetBuff(pcap_t
*p
, int dim
)
1115 * XXX turbocap has an internal way of managing buffers.
1116 * And at the moment it's not configurable, so we just
1117 * silently ignore the request to set the buffer.
1123 TcSetMode(pcap_t
*p
, int mode
)
1125 if (mode
!= MODE_CAPT
)
1127 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Mode %d not supported by TurboCap devices. TurboCap only supports capture.", mode
);
1135 TcSetMinToCopy(pcap_t
*p
, int size
)
1137 struct pcap_tc
*pt
= p
->priv
;
1142 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Mintocopy cannot be less than 0.");
1146 status
= g_TcFunctions
.InstanceSetFeature(pt
->TcInstance
, TC_INST_FT_MINTOCOPY
, (ULONG
)size
);
1148 if (status
!= TC_SUCCESS
)
1150 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "TurboCap error setting the mintocopy: %s (%08x)", g_TcFunctions
.StatusGetString(status
), status
);
1157 TcGetReceiveWaitHandle(pcap_t
*p
)
1159 struct pcap_tc
*pt
= p
->priv
;
1161 return g_TcFunctions
.InstanceGetReceiveWaitHandle(pt
->TcInstance
);
1165 TcOidGetRequest(pcap_t
*p
, bpf_u_int32 oid _U_
, void *data _U_
, size_t *lenp _U_
)
1167 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1168 "An OID get request cannot be performed on a TurboCap device");
1173 TcOidSetRequest(pcap_t
*p
, bpf_u_int32 oid _U_
, const void *data _U_
,
1176 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1177 "An OID set request cannot be performed on a TurboCap device");
1182 TcSendqueueTransmit(pcap_t
*p
, pcap_send_queue
*queue _U_
, int sync _U_
)
1184 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1185 "Packets cannot be bulk transmitted on a TurboCap device");
1190 TcSetUserBuffer(pcap_t
*p
, int size _U_
)
1192 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1193 "The user buffer cannot be set on a TurboCap device");
1198 TcLiveDump(pcap_t
*p
, char *filename _U_
, int maxsize _U_
, int maxpacks _U_
)
1200 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1201 "Live packet dumping cannot be performed on a TurboCap device");
1206 TcLiveDumpEnded(pcap_t
*p
, int sync _U_
)
1208 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1209 "Live packet dumping cannot be performed on a TurboCap device");
1213 static PAirpcapHandle
1214 TcGetAirPcapHandle(pcap_t
*p _U_
)