Skip to content

Commit

Permalink
Now PCAP_IF_LOOPBACK flag in pcap_if_t struct will be set for "Npcap …
Browse files Browse the repository at this point in the history
…Loopback Adapter" both for DLT_NULL mode and Fake Ethernet mode.
  • Loading branch information
hsluoyz committed Dec 29, 2015
1 parent 9d43e8f commit 739468c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
1 change: 1 addition & 0 deletions Common/Packet32.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ extern "C"
BOOLEAN PacketGetStatsEx(LPADAPTER AdapterObject, struct bpf_stat* s);
BOOLEAN PacketSetBuff(LPADAPTER AdapterObject, int dim);
BOOLEAN PacketGetNetType(LPADAPTER AdapterObject, NetType* type);
BOOLEAN PacketIsLoopbackAdapter(LPADAPTER AdapterObject);
LPADAPTER PacketOpenAdapter(PCHAR AdapterName);
BOOLEAN PacketSendPacket(LPADAPTER AdapterObject, LPPACKET pPacket, BOOLEAN Sync);
INT PacketSendPackets(LPADAPTER AdapterObject, PVOID PacketBuff, ULONG Size, BOOLEAN Sync);
Expand Down
11 changes: 6 additions & 5 deletions packetWin7/Dll/AdInfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ static BOOLEAN PacketAddFakeNdisWanAdapter();
static BOOLEAN IsIPv4Enabled(LPCSTR AdapterNameA);
#endif

PADAPTER_INFO g_AdaptersInfoList = NULL; ///< Head of the adapter information list. This list is populated when packet.dll is linked by the application.
HANDLE g_AdaptersInfoMutex = NULL; ///< Mutex that protects the adapter information list. NOTE: every API that takes an ADAPTER_INFO as parameter assumes that it has been called with the mutex acquired.
CHAR g_LoopbackAdapterName[100] = ""; ///< The name of "Npcap Loopback Adapter", used for recording the NdisMediumNull link type for this adapter.
#define BUFSIZE 512
PADAPTER_INFO g_AdaptersInfoList = NULL; ///< Head of the adapter information list. This list is populated when packet.dll is linked by the application.
HANDLE g_AdaptersInfoMutex = NULL; ///< Mutex that protects the adapter information list. NOTE: every API that takes an ADAPTER_INFO as parameter assumes that it has been called with the mutex acquired.
CHAR g_LoopbackAdapterNameForDLTNull[BUFSIZE] = ""; ///< The name of "Npcap Loopback Adapter", used for recording the NdisMediumNull link type for this adapter.

extern FARPROC g_GetAdaptersAddressesPointer;

Expand Down Expand Up @@ -968,7 +969,7 @@ static BOOLEAN PacketAddAdapterIPH(PIP_ADAPTER_INFO IphAd)
}

// Set the NdisMediumNull value for "Npcap Loopback Adapter".
if (strcmp(g_LoopbackAdapterName, TmpAdInfo->Name) == 0)
if (strcmp(g_LoopbackAdapterNameForDLTNull, TmpAdInfo->Name) == 0)
{
TmpAdInfo->LinkLayer.LinkType = NdisMediumNull;
}
Expand Down Expand Up @@ -1173,7 +1174,7 @@ static BOOLEAN PacketAddAdapterNPF(PCHAR AdName, UINT flags)
// Record the name of "Npcap Loopback adapter", as we need it to set NdisMediumNull value for IPHelper version add adapter function.
if (TmpAdInfo->LinkLayer.LinkType == (UINT) NdisMediumNull)
{
strncpy(g_LoopbackAdapterName, TmpAdInfo->Name, sizeof(g_LoopbackAdapterName)/ sizeof(g_LoopbackAdapterName[0]) - 1);
strncpy(g_LoopbackAdapterNameForDLTNull, TmpAdInfo->Name, sizeof(g_LoopbackAdapterNameForDLTNull)/ sizeof(g_LoopbackAdapterNameForDLTNull[0]) - 1);
}

if (Status == FALSE)
Expand Down
1 change: 1 addition & 0 deletions packetWin7/Dll/Packet.def
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ EXPORTS
PacketGetStats
PacketGetStatsEx
PacketGetNetType
PacketIsLoopbackAdapter
PacketSetReadTimeout
PacketSetMode
PacketSetNumWrites
Expand Down
57 changes: 57 additions & 0 deletions packetWin7/Dll/Packet32.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ BOOL g_IsRunByAdmin = FALSE;
BOOL g_NPcapHelperTried = FALSE;
// The handle to this DLL.
HANDLE g_DllHandle = NULL;
// The name of "Npcap Loopback Adapter".
CHAR g_LoopbackAdapterName[BUFSIZE] = "";

#ifdef _WINNT4
#if (defined(HAVE_NPFIM_API) || defined(HAVE_WANPACKET_API) || defined (HAVE_AIRPCAP_API) || defined(HAVE_IPHELPER_API))
Expand Down Expand Up @@ -456,6 +458,32 @@ HANDLE NPcapRequestHandle(char *sMsg, DWORD *pdwError)
#define NPCAP_SOFTWARE_REGISTRY_KEY "SOFTWARE\\Wow6432Node\\" NPF_SOFT_REGISTRY_NAME
#endif

void NPcapGetLoopbackInterfaceName()
{
HKEY hKey;
DWORD type;
char buffer[BUFSIZE];
int size = sizeof(buffer);

#ifndef _X86_
Wow64EnableWow64FsRedirection(FALSE);
#endif

if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, NPCAP_SOFTWARE_REGISTRY_KEY, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
if (RegQueryValueExA(hKey, "Loopback", 0, &type, (LPBYTE)buffer, &size) == ERROR_SUCCESS && type == REG_SZ)
{
strncpy(g_LoopbackAdapterName, buffer, sizeof(g_LoopbackAdapterName)/ sizeof(g_LoopbackAdapterName[0]) - 1);
}

RegCloseKey(hKey);
}

#ifndef _X86_
Wow64EnableWow64FsRedirection(TRUE);
#endif
}

BOOL NPcapIsAdminOnlyMode()
{
HKEY hKey;
Expand Down Expand Up @@ -846,6 +874,9 @@ BOOL APIENTRY DllMain(HANDLE DllHandle,DWORD Reason,LPVOID lpReserved)
// XXX We want to replace this with a constant. We leave it out for the moment
// TODO fixme. Those hardcoded strings are terrible...
PacketGetFileVersion(TEXT("drivers\\") TEXT(NPF_DRIVER_NAME) TEXT(".sys"), PacketDriverVersion, sizeof(PacketDriverVersion));

// Get the name for "Npcap Loopback Adapter"
NPcapGetLoopbackInterfaceName();

break;

Expand Down Expand Up @@ -4595,6 +4626,32 @@ BOOLEAN PacketGetNetType(LPADAPTER AdapterObject, NetType *type)
return ret;
}

/*!
\brief Returns whether an adapter is a loopback adapter (like Npcap loopback adapter).
\param AdapterObject The adapter on which information is needed.
\return TRUE if yes, FALSE if no.
*/
BOOLEAN PacketIsLoopbackAdapter(LPADAPTER AdapterObject)
{
PADAPTER_INFO TAdInfo;
BOOLEAN ret;
TRACE_ENTER("PacketIsLoopbackAdapter");

// Set the return value to TRUE for "Npcap Loopback Adapter".
if (strcmp(g_LoopbackAdapterName + sizeof(DEVICE_PREFIX) - 1,
AdapterObject->Name + sizeof(DEVICE_PREFIX) - 1 + sizeof(NPF_DEVICE_NAMES_PREFIX) - 1) == 0)
{
ret = TRUE;
}
else
{
ret = FALSE;
}

TRACE_EXIT("PacketIsLoopbackAdapter");
return ret;
}

/*!
\brief Returns the AirPcap handler associated with an adapter. This handler can be used to change
the wireless-related settings of the CACE Technologies AirPcap wireless capture adapters.
Expand Down
14 changes: 1 addition & 13 deletions wpcap/libpcap/inet.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,10 @@ add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, const char *name,
return (0);
}
else {
NetType type;
/*
* Set PCAP_IF_LOOPBACK for "Npcap Loopback Adapter".
*/
if (PacketGetNetType(p->adapter, &type) == FALSE) {
(void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
"Cannot determine the network type: %s", pcap_win32strerror());
}
else {
if (type.LinkType == NdisMediumNull) {
npcap_loopback = 1;
}
else {
npcap_loopback = 0;
}
}
npcap_loopback = PacketIsLoopbackAdapter(p->adapter);
}
pcap_close(p);

Expand Down

0 comments on commit 739468c

Please sign in to comment.