Exemple #1
0
const char *
pcap_lib_version(void)
{
	char *packet_version_string;
	size_t pcap_version_string_len;

	if (pcap_version_string == NULL) {
		/*
		 * Generate the version string.
		 */
		packet_version_string = PacketGetVersion();
		if (strcmp(wpcap_version_string, packet_version_string) == 0) {
			/*
			 * WinPcap version string and packet.dll version
			 * string are the same; just report the WinPcap
			 * version.
			 */
			pcap_version_string_len =
			    (sizeof pcap_version_string_fmt - 2) +
			    strlen(wpcap_version_string);
			pcap_version_string = malloc(pcap_version_string_len);
			sprintf(pcap_version_string, pcap_version_string_fmt,
			    wpcap_version_string);
		} else {
			/*
			 * WinPcap version string and packet.dll version
			 * string are different; that shouldn't be the
			 * case (the two libraries should come from the
			 * same version of WinPcap), so we report both
			 * versions.
			 */
			pcap_version_string_len =
			    (sizeof pcap_version_string_packet_dll_fmt - 4) +
			    strlen(wpcap_version_string) +
			    strlen(packet_version_string);
			pcap_version_string = malloc(pcap_version_string_len);
			sprintf(pcap_version_string,
			    pcap_version_string_packet_dll_fmt,
			    wpcap_version_string, packet_version_string);
		}
	}
	return (pcap_version_string);
}
Exemple #2
0
int iflist()
{
  pcap_if_t *alldevs;
  pcap_if_t *d;
  char errbuf[PCAP_ERRBUF_SIZE+1];
  int i;

  printf("WinPCap version: %s\n", PacketGetVersion());

  // Retrieve the interfaces list 
  if (pcap_findalldevs(&alldevs, errbuf) == -1)
  {
    fprintf(stderr, "Error in pcap_findalldevs: %s\n", errbuf);
    return 0;
  }

  // Scan the list printing every entry 
  for (i=0,d=alldevs; d; i++,d=d->next)
  {
    ifprint(i, d);
  }

  return i;
}
Exemple #3
0
int main()
{
//define a pointer to an ADAPTER structure

LPADAPTER  lpAdapter = 0;

//define a pointer to a PACKET structure

LPPACKET   lpPacket;

int        i;
DWORD      dwErrorCode;

//ascii strings
char		AdapterName[8192]; // string that contains a list of the network adapters
char		*temp,*temp1;


int			AdapterNum=0,Open;
ULONG		AdapterLength;

char buffer[256000];  // buffer to hold the data coming from the driver

struct bpf_stat stat;
	
	//
	// Obtain the name of the adapters installed on this machine
	//
	printf("Packet.dll test application. Library version:%s\n", PacketGetVersion());

	printf("Adapters installed:\n");
	i=0;	

	AdapterLength = sizeof(AdapterName);

	if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){
		printf("Unable to retrieve the list of the adapters!\n");
		return -1;
	}
	temp=AdapterName;
	temp1=AdapterName;

	while ((*temp!='\0')||(*(temp-1)!='\0'))
	{
		if (*temp=='\0') 
		{
			memcpy(AdapterList[i],temp1,temp-temp1);
			temp1=temp+1;
			i++;
		}
		temp++;
	}
		  
	AdapterNum=i;
	for (i=0;i<AdapterNum;i++)
		printf("\n%d- %s\n",i+1,AdapterList[i]);
	printf("\n");


	do 
	{
		printf("Select the number of the adapter to open : ");
		scanf("%d",&Open);
		if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum); 
	} while (Open>AdapterNum);
	

	
	
	lpAdapter =   PacketOpenAdapter(AdapterList[Open-1]);
	
	if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
	{
		dwErrorCode=GetLastError();
		printf("Unable to open the adapter, Error Code : %lx\n",dwErrorCode); 

		return -1;
	}	

	// set the network adapter in promiscuous mode
	
	if(PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_PROMISCUOUS)==FALSE){
			printf("Warning: unable to set promiscuous mode!\n");
	}

	// set a 512K buffer in the driver
	if(PacketSetBuff(lpAdapter,512000)==FALSE){
			printf("Unable to set the kernel buffer!\n");
			return -1;
	}

	// set a 1 second read timeout
	if(PacketSetReadTimeout(lpAdapter,1000)==FALSE){
			printf("Warning: unable to set the read tiemout!\n");
	}

	//allocate and initialize a packet structure that will be used to
	//receive the packets.
	if((lpPacket = PacketAllocatePacket())==NULL){
		printf("\nError: failed to allocate the LPPACKET structure.");
		return (-1);
	}
	PacketInitPacket(lpPacket,(char*)buffer,256000);
	
	//main capture loop
	while(!kbhit())
	{
	    // capture the packets
		if(PacketReceivePacket(lpAdapter,lpPacket,TRUE)==FALSE){
			printf("Error: PacketReceivePacket failed");
			return (-1);
		}

		PrintPackets(lpPacket);
	}


	//print the capture statistics
	if(PacketGetStats(lpAdapter,&stat)==FALSE){
			printf("Warning: unable to get stats from the kernel!\n");
	}
	else
		printf("\n\n%d packets received.\n%d Packets lost",stat.bs_recv,stat.bs_drop);

	PacketFreePacket(lpPacket);
	
	// close the adapter and exit

	PacketCloseAdapter(lpAdapter);
	return (0);
}
int main()
{
	LPADAPTER	lpAdapter = 0;
	int			i;
	DWORD		dwErrorCode;
	char		AdapterName[8192];
	char		*temp,*temp1;
	int			AdapterNum=0,Open;
	ULONG		AdapterLength;
	PPACKET_OID_DATA  OidData;
	BOOLEAN		Status;
	
	//
	// Obtain the name of the adapters installed on this machine
	//

	printf("Packet.dll test application. Library version:%s\n", PacketGetVersion());
	
	printf("Adapters installed:\n");
	i=0;	
	
	AdapterLength = sizeof(AdapterName);
	
	if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){
		printf("Unable to retrieve the list of the adapters!\n");
		return -1;
	}
	temp=AdapterName;
	temp1=AdapterName;

	while ((*temp!='\0')||(*(temp-1)!='\0'))
	{
		if (*temp=='\0') 
		{
			memcpy(AdapterList[i],temp1,temp-temp1);
			temp1=temp+1;
			i++;
		}
		temp++;
	}
		  
	AdapterNum=i;
	for (i=0;i<AdapterNum;i++)
		printf("\n%d- %s\n",i+1,AdapterList[i]);
	printf("\n");


	do 
	{
		printf("Select the number of the adapter to open : ");
		scanf_s("%d",&Open);
		if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum); 
	} while (Open>AdapterNum);
	

	//
	// Open the selected adapter
	//

	lpAdapter =   PacketOpenAdapter(AdapterList[Open-1]);
	
	if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
	{
		dwErrorCode=GetLastError();
		printf("Unable to open the adapter, Error Code : %lx\n",dwErrorCode); 

		return -1;
	}	

	// 
	// Allocate a buffer to get the MAC adress
	//

	OidData = malloc(6 + sizeof(PACKET_OID_DATA));
	if (OidData == NULL) 
	{
		printf("error allocating memory!\n");
		PacketCloseAdapter(lpAdapter);
		return -1;
	}

	// 
	// Retrieve the adapter MAC querying the NIC driver
	//

	OidData->Oid = OID_802_3_CURRENT_ADDRESS;

	OidData->Length = 6;
	ZeroMemory(OidData->Data, 6);
	
	Status = PacketRequest(lpAdapter, FALSE, OidData);
	if(Status)
	{
		printf("The MAC address of the adapter is %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
			(OidData->Data)[0],
			(OidData->Data)[1],
			(OidData->Data)[2],
			(OidData->Data)[3],
			(OidData->Data)[4],
			(OidData->Data)[5]);
	}
	else
	{
		printf("error retrieving the MAC address of the adapter!\n");
	}

	free(OidData);
	PacketCloseAdapter(lpAdapter);
	return (0);
}
Exemple #5
0
int main(int argc, char **argv)
{
	int i;
	HINSTANCE     hPacket;
	DWORD         dwVersion, dwMajorVersion;
	char			AdapterInfo[NIC_BUFFER_SIZE] = { '\0','\0' };
	unsigned long	AdapterLength = NIC_BUFFER_SIZE;
	LPWSTR			wstrName;
	LPSTR			strName, strDesc;
	int				nAdapterCount;
	PCHAR           dllVersion;
	PCHAR           testString;
	int		nDLLMajorVersion, nDLLMinorVersion;


	// Attemp to load the WinpCap packet library
	hPacket = LoadLibrary("PACKET.DLL");
        if(hPacket)
	{
		// Now look up the address
		PacketGetAdapterNames = (BOOLEAN (*)(PTSTR, PULONG))GetProcAddress(hPacket, "PacketGetAdapterNames");
		PacketGetVersion = (PCHAR (*)())GetProcAddress(hPacket, "PacketGetVersion");
	}
	else {
		printf("Could not load WinPCap driver!\n");
		printf ("You can download them for free from\n");
		printf ("http://www.winpcap.org/\n");
		myexit(1);
	}

	dwVersion      = GetVersion();
	dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));

        // Get DLL Version and Tokenize
        dllVersion = PacketGetVersion();
        nDLLMajorVersion = -1;
        nDLLMinorVersion = -1;
        for ( testString = strtok(dllVersion, ", ");
              testString != NULL;
              testString = strtok(NULL, ", ") )
        {
                // If Single Character, Convert
                if ( strlen( testString ) == 1 )
                {
                        // Check Major First
                        if ( nDLLMajorVersion == -1 )
                        {
                                nDLLMajorVersion = atoi(testString);
                        }
                        else if ( nDLLMinorVersion == -1 )
                        {
                                nDLLMinorVersion = atoi(testString);
                        }
                }
        }

	// Get out blob of adapter info
	PacketGetAdapterNames(AdapterInfo,&AdapterLength);

	// If this is Windows NT ... And DLL Returns UNICODE
	if(!(dwVersion >= 0x80000000 && dwMajorVersion >= 4) &&
           !(nDLLMajorVersion >= 3 && nDLLMinorVersion >= 1))
	{
		wstrName=(LPWSTR)AdapterInfo;

		// Obtain Names
		nAdapterCount = 0;
		while ((*wstrName))
		{
			// store pointer to name
			niNT[nAdapterCount].wstrName=wstrName;
			wstrName += lstrlenW(wstrName) +1;
			nAdapterCount++;
		}

		strDesc = (LPSTR)++wstrName;

		// Obtain descriptions ....
		for(i=0;i<nAdapterCount;i++)
		{
			// store pointer to description
			niNT[i].strDesc=strDesc;
			strDesc += lstrlen(strDesc) +1;

			// ... and display adapter info
			printf("\n%d: %s\n",i+1,niNT[i].strDesc);
			wprintf(L"     Device: %s",niNT[i].wstrName);
		}

		if(i)
		{
			printf("\n\nExample config for bochsrc:\n");
			wprintf(L"ne2k: ioaddr=0x300, irq=3, mac=b0:c4:20:00:00:00, ethmod=win32, ethdev=%s",niNT[0].wstrName);
			printf("\n");
		}

	}
	else
	{
		// Windows 9x
		strName=(LPSTR)AdapterInfo;

		// Obtain Names
		nAdapterCount = 0;
		while ((*strName))
		{
			// store pointer to name
			ni9X[nAdapterCount].strName=strName;
			strName += lstrlen(strName) +1;
			nAdapterCount++;
		}

		strDesc = (LPSTR)++strName;

		// Obtain descriptions ....
		for(i=0;i<nAdapterCount;i++)
		{
			// store pointer to description
			ni9X[i].strDesc=strDesc;
			strDesc += lstrlen(strDesc) +1;

			// ... and display adapter info
			printf("\n%d: %s\n",i+1,ni9X[i].strDesc);
			printf("     Device: %s",ni9X[i].strName);
		}

		if(i)
		{
			printf("\n\nExample config for bochsrc:\n");
			printf("ne2k: ioaddr=0x300, irq=3, mac=b0:c4:20:00:00:00, ethmod=win32, ethdev=%s",ni9X[0].strName);
			printf("\n");
		}

		printf("\n");
	}

	myexit (0);
	return 0; /* shut up stupid compilers */
}
Exemple #6
0
int main(int argc, char **argv)
{
    char AdapterInfo[NIC_BUFFER_SIZE] = { '\0','\0' };
    unsigned long AdapterLength = NIC_BUFFER_SIZE;
    LPWSTR wstrName;
    LPSTR strDesc;
    int nAdapterCount;
    int i;

	char* PacketLibraryVersion;


    // Attemp to load the WinPCap dynamic link library
    HINSTANCE hPacket = LoadLibrary("PACKET.DLL");
    if (hPacket) {
        PacketGetAdapterNames = (BOOLEAN (*)(PTSTR, PULONG))GetProcAddress(hPacket, "PacketGetAdapterNames");
        PacketGetVersion = (PCHAR (*)(VOID))GetProcAddress(hPacket, "PacketGetVersion");
    } else {
        printf("Could not load WinPCap driver! for more information goto:\n");
        printf ("http://netgroup-serv.polito.it/winpcap\n");
        return 1;
    }
    if (!(PacketLibraryVersion = PacketGetVersion())) {
        printf("ERROR: Could not get Packet DLL Version string.\n");
		return 2;
	}
	printf("Packet Library Version: %s\n", PacketLibraryVersion);

    if (!PacketGetAdapterNames(AdapterInfo, &AdapterLength)) {
        printf("ERROR: Could not get Packet Adaptor Names.\n");
		return 2;
	}
    wstrName = (LPWSTR)AdapterInfo;

    // Enumerate all the adapters names found...
    nAdapterCount = 0;
    while ((*wstrName)) {
        nic_info[nAdapterCount].wstrName = wstrName;
        wstrName += lstrlenW(wstrName) + 1;
        nAdapterCount++;
		if (nAdapterCount > 9) break;
    }
    strDesc = (LPSTR)++wstrName;

	if (!nAdapterCount) {
		printf("No Packet Adaptors found (%lu)\n", AdapterLength);
	} else {
	    printf("Adaptor count: %d\n", nAdapterCount);
	}

    // And obtain the adapter description strings....
    for (i = 0; i < nAdapterCount; i++) {
        nic_info[i].strDesc = strDesc;
        strDesc += lstrlen(strDesc) + 1;

        // display adapter info
        printf("%d: %s\n", i + 1, nic_info[i].strDesc);
        wprintf(L"     Device: %s\n", nic_info[i].wstrName);
    }
    return 0;
}