示例#1
0
文件: eth-win32.c 项目: jwilkins/nmap
eth_t *
eth_open(const char *device)
{
	eth_t *eth;
	intf_t *intf;
	struct intf_entry ifent;
	eth_addr_t ea;
	char *p, *buf;
	ULONG len;

	/* Get interface entry. */
	memset(&ifent, 0, sizeof(ifent));
	if ((intf = intf_open()) != NULL) {
		strlcpy(ifent.intf_name, device, sizeof(ifent.intf_name));
		intf_get(intf, &ifent);
		intf_close(intf);
	}
	if (ifent.intf_link_addr.addr_type != ADDR_TYPE_ETH)
		return (NULL);

	/* Get Packet driver adapter name/desc lists. */
	buf = NULL;
	PacketGetAdapterNames(buf, &len);
	if (len > 0 && (buf = malloc(len)) != NULL) {
		if (!PacketGetAdapterNames(buf, &len)) {
			free(buf);
			buf = NULL;
		}
	}
	if (buf == NULL)
		return (NULL);
	
	/* XXX - find adapter with matching interface MAC address. */
	if ((eth = calloc(1, sizeof(*eth))) == NULL) {
		free(buf);
		return (NULL);
	}
	for (p = buf; *p != '\0'; p += strlen(p) + 1) {
		if ((eth->lpa = PacketOpenAdapter(p)) != NULL) {
			if (eth->lpa->hFile != INVALID_HANDLE_VALUE &&
			    eth_get(eth, &ea) == 0 &&
			    memcmp(&ea, &ifent.intf_link_addr.addr_eth,
				ETH_ADDR_LEN) == 0) {
				PacketSetBuff(eth->lpa, 512000);
				eth->pkt = PacketAllocatePacket();
				break;
			}
			PacketCloseAdapter(eth->lpa);
		}
	}
	free(buf);
	if (eth->pkt == NULL)
		eth = eth_close(eth);
	
	return (eth);
}
示例#2
0
long sockGetDevicesNum() {
	char AdapterName[8192]; // string that contains a list of the network adapters
	ULONG AdapterLength;
	char *temp,*temp1;
	int i;

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

	i=0;
	while (temp[0] != 0) {
		strcpy(AdapterList[i++].name, temp);
		temp+= strlen(temp)+1;
	}
	i=0; temp++;
	while (temp[0] != 0) {
		strcpy(AdapterList[i++].desc, temp);
		temp+= strlen(temp)+1;
	}

	return i;
}
示例#3
0
文件: pktdrv.c 项目: 10code/lwip
/** Get a list of adapters
 *
 * @param adapter_list void* array: list where the adapters are stored
 * @param list_len size of adapter_list (number of void*)
 * @param buffer here the actual data is stored, adapter_list points into this buffer
 * @param buf_len size of buffer in bytes
 * @return number of adapters found or negative on error
 */
static int
get_adapter_list(char** adapter_list, int list_len, void* buffer, size_t buf_len)
{
  int i;
  char *temp, *start;
  ULONG AdapterLength;

  memset(adapter_list, 0, list_len*sizeof(void*));
  memset(buffer, 0, buf_len);

  /* obtain the name of the adapters installed on this machine
     (a list of strings separated by '\0') */
  AdapterLength = buf_len;
  if (PacketGetAdapterNames((char*)buffer, &AdapterLength)==FALSE){
    printf("Unable to retrieve the list of the adapters!\n");
    return 0;
  }

  /* get the start of each adapter name in the list and put it into
   * the AdapterList array */
  i = 0;
  temp = (char*)buffer;
  start = (char*)buffer;
  while ((*temp != '\0') || (*(temp - 1) != '\0')) {
    if (*temp == '\0') {
      adapter_list[i] = start;
      start = temp + 1;
      i++;
      if (i >= list_len) {
        break;
      }
    }
    temp++;
  }
  return i;
}
//获取本机所有网卡的名字
int NetworkInterface::GetAdapterList(char AdapterList[][1024], int &AdapterNum)
{
	//ascii strings
	char AdapterName[8192]; // string that contains a list of the network adapters
	char *temp,*temp1;

	ULONG AdapterLength;

	int i = 0;	

	AdapterLength = sizeof(AdapterName);

	if (PacketGetAdapterNames(AdapterName,&AdapterLength) == FALSE)
	{
		strcpy_s(LastErrStr, "Unable to retrieve the list of the adapters!");
		LastErrCode = GetLastError();
		return -1;
	}

	temp  = AdapterName;
	temp1 = AdapterName;

	while ( (*temp != '\0') || (*(temp-1) != '\0') )
	{
		if (*temp == '\0') 
		{
			memcpy(AdapterList[i], temp1, temp - temp1 + 1);
			temp1 = temp + 1;
			i++;
		}
		temp++;
	}
		  
	AdapterNum = i;	//获取网卡个数
	return 0;
}
示例#5
0
文件: fad-win32.c 项目: ebichu/dd-wrt
/*
 * Get a list of all interfaces that are up and that we can open.
 * Returns -1 on error, 0 otherwise.
 * The list, as returned through "alldevsp", may be null if no interfaces
 * were up and could be opened.
 *
 * Win32 implementation, based on WinPcap
 */
int
pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
{
	pcap_if_t *devlist = NULL;
	int ret = 0;
	const char *desc;
	char *AdaptersName;
	ULONG NameLength;
	char *name;
	
	PacketGetAdapterNames(NULL, &NameLength);

	if (NameLength > 0)
		AdaptersName = (char*) malloc(NameLength);
	else
	{
		*alldevsp = NULL;
		return 0;
	}
	if (AdaptersName == NULL)
	{
		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot allocate enough memory to list the adapters.");
		return (-1);
	}			

	if (!PacketGetAdapterNames(AdaptersName, &NameLength)) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE,
			"PacketGetAdapterNames: %s",
			pcap_win32strerror());
		free(AdaptersName);
		return (-1);
	}
	
	/*
	 * "PacketGetAdapterNames()" returned a list of
	 * null-terminated ASCII interface name strings,
	 * terminated by a null string, followed by a list
	 * of null-terminated ASCII interface description
	 * strings, terminated by a null string.
	 * This means there are two ASCII nulls at the end
	 * of the first list.
	 *
	 * Find the end of the first list; that's the
	 * beginning of the second list.
	 */
	desc = &AdaptersName[0];
	while (*desc != '\0' || *(desc + 1) != '\0')
		desc++;
	
	/*
 	 * Found it - "desc" points to the first of the two
	 * nulls at the end of the list of names, so the
	 * first byte of the list of descriptions is two bytes
	 * after it.
	 */
	desc += 2;
	
	/*
	 * Loop over the elements in the first list.
	 */
	name = &AdaptersName[0];
	while (*name != '\0') {
	/*
	 * Add an entry for this interface.
	 */
	if (pcap_add_if_win32(&devlist, name, desc,
			errbuf) == -1) {
			/*
			* Failure.
			*/
			ret = -1;
			break;
		}
		name += strlen(name) + 1;
		desc += strlen(desc) + 1;
	}
	
	if (ret == -1) {
	/*
	 * We had an error; free the list we've been constructing.
	 */
	if (devlist != NULL) {
			pcap_freealldevs(devlist);
			devlist = NULL;
		}
	}
	
	*alldevsp = devlist;
	free(AdaptersName);
	return (ret);
}
示例#6
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);
}
示例#8
0
int main(int argc, char* argv[])
{

	// Packet.lib variables:
	LPADAPTER  lpAdapter = 0;
	PPACKET_OID_DATA pOidData = malloc( sizeof(PACKET_OID_DATA) + MAX_OID_DATA );
	NetType *type = malloc(sizeof(NetType));
	UINT speed_Mbps = 0;

	BOOL isWinNT = FALSE;
	int        i;

	DWORD dwVersion;
	DWORD dwWindowsMajorVersion;


	//unicode strings (winnt)
	WCHAR		AdapterName[8192]; // string that contains a list of the network adapters
	WCHAR		*temp,*temp1;

	//ascii strings (win95)
	char		AdapterNamea[8192]; // string that contains a list of the network adapters
	char		*tempa,*temp1a;


	int			AdapterNum=0,Open;
	unsigned long		AdapterLength;


	FILE * inifile;

	char		speed_value[10];
	char		*MAC_value = malloc( 13*sizeof(char) );
	char		*temp2 = malloc( 13*sizeof(char) );

	//interfaceArg specifies the interface # passed fron configchange.pl
	//set interfaceArg to argv[1] if arguments are passed
	int interfaceArg = 0;
	if ( argc>1 && argv[1] )
	{
		interfaceArg = atoi(argv[1]);
	}

	// obtain the name of the adapters installed on this machine
	//printf("Adapters installed:\n");
	i=0;

	// the data returned by PacketGetAdapterNames is different in Win95 and in WinNT.
	// We have to check the os on which we are running
	dwVersion=GetVersion();
	dwWindowsMajorVersion =  (DWORD)(LOBYTE(LOWORD(dwVersion)));
	if (!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4))
	{  // Windows NT
		isWinNT = TRUE;
		AdapterLength=sizeof(AdapterName);
		PacketGetAdapterNames((char*)AdapterName,&AdapterLength);
		temp=AdapterName;
		temp1=AdapterName;
		while ((*temp!='\0')||(*(temp-1)!='\0'))
		{
			if (*temp=='\0')
			{
				memcpy(AdapterList[i],temp1,(temp-temp1)*2);
				temp1=temp+1;
				i++;
			}

		temp++;
		}

		AdapterNum=i;
		// we want to automate the adaptor selection process
		// but if there are more than one to choose from, we can't
		// so
		if (AdapterNum>1)
		{
			for (i=0;i<AdapterNum;i++)
			{
				wprintf(L"\n%d- %s\n",i+1,AdapterList[i]);
   			}
			printf("\n");
  		}
	}
	else	//windows 95
	{
		AdapterLength=sizeof(AdapterNamea);
		PacketGetAdapterNames(AdapterNamea,&AdapterLength);
		tempa=AdapterNamea;
		temp1a=AdapterNamea;

		while ((*tempa!='\0')||(*(tempa-1)!='\0'))
		{
			if (*tempa=='\0')
			{
				memcpy(AdapterList[i],temp1a,tempa-temp1a);
				temp1a=tempa+1;
				i++;
			}
			tempa++;
		}

		AdapterNum=i;
		// we want to automate the adaptor selection process
		// but if there are more than one to choose from, we can't
		// so
		if (AdapterNum>1)
		{
			printf("Adapters installed:\n");
			for (i=0;i<AdapterNum;i++)
			{
				printf("\n%d- %s\n",i+1,AdapterList[i]);
   			}
			printf("\n");
  		}
	}

	// we want to automate the adaptor selection process
	// but if there are more than one to choose from, we can't
	// so
	//
	
	if (AdapterNum>1)
	{	
		if ( (interfaceArg>AdapterNum)||(interfaceArg<1) )
		{
			do
			{
				printf("Select the number of the adapter to use : ");scanf("%d",&Open);
				if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum);
			} while (Open>AdapterNum);
		}
		else
		{
			Open = interfaceArg;
		}
	}
	else
	{
		Open = AdapterNum;
 	}

	lpAdapter = PacketOpenAdapter(AdapterList[Open-1]);


	MAC_value[0] = '\0';
	temp2[0] = '\0';
	speed_value[0] = '\0';

	if( NULL != lpAdapter)
	{

		if ( TRUE == PacketGetNetType (lpAdapter, type) )
		{
			speed_Mbps = type->LinkSpeed / 1000000; // LinkSpeed is in bits per second
		}
		else
			printf("Could not read Ethernet card's speed\n");

		if ( type->LinkType != NdisMedium802_3)
			printf("NOT Ethernet802.3 card.\nNetwork Interface not supported\n");
		else
		{
			pOidData->Oid = OID_802_3_CURRENT_ADDRESS;

			pOidData->Length = MAX_OID_DATA;


			if ( TRUE == PacketRequest(lpAdapter, FALSE , pOidData) )
			{
				// get info obtained
//				printf("Physical address read: ");
	/*			printf("%x %x %x %x %x %x\n", pOidData->Data[0], pOidData->Data[1],
					pOidData->Data[2],pOidData->Data[3],
					pOidData->Data[4],pOidData->Data[5]
					);*/

				pOidData->Data[0] += 2; // changing address from global to local
				for( i=0; i<6; i++ )
				{
					strcpy( temp2, MAC_value);
					if( pOidData->Data[i] > 15 )
						// has 2 hex digits
						sprintf( MAC_value, "%s%x", temp2, pOidData->Data[i]);
					else
						sprintf( MAC_value, "%s0%x", temp2, pOidData->Data[i]);
				}
			}
			else
				printf("Failed to read physical address of Ethernet card\n");
		}

		free(pOidData);
		free(type);

		PacketCloseAdapter( lpAdapter );
	}
	else
	{
		// lpAdapter NULL
		printf("Problem with opening adapter (packet.lib issue)\n");
		return (1);
	}

	inifile = fopen(EPOC_INI_FILE, "a"); // to create if does exist
	if ( NULL != inifile )
		fclose(inifile);
	else
	{
		printf("Can't create or access %s.\n\n", EPOC_INI_FILE);
		return 0;
	}

	if ( 0 == replace_in_inifile( ETHER_NIF_ENTRY, AdapterList[Open-1], isWinNT ) )
		printf( "Netcards using adapter %d\n", Open );
	else
	{
		return 0;
	}


	if ( 0 != replace_in_inifile( ETHER_MAC_ENTRY, MAC_value, FALSE ) )
	{
		printf("Couldn't write MAC address to %s file\n", EPOC_INI_FILE);
		return (1);
	}


	if( 0 != speed_Mbps )
		sprintf( speed_value, "%dMbps", speed_Mbps);


	if ( 0 != replace_in_inifile( ETHER_SPEED_ENTRY, speed_value, FALSE ) )
	{
		printf("Couldn't write speed value to %s file\n", EPOC_INI_FILE);
		return (1);
	}

	//printf("Netcards has written settings to %s.\n\n", EPOC_INI_FILE);

	free(MAC_value);
	free(temp2);
	return (0);
}
示例#9
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 */
}
示例#10
0
int CMainFrame::GetAdapterList()
{
#define ADAPTER_LENGTH	(10000)
DWORD dwVersion, dwWindowsMajorVersion, dwAdapterLength = ADAPTER_LENGTH;
int iAdapterNum = 0;

// unicode strings (winnt) 
WCHAR		AdapterName[ADAPTER_LENGTH]; // string that contains a list of the network adapters
WCHAR		*temp;

// ascii strings (win95)
char		AdapterNamea[ADAPTER_LENGTH]; // string that contains a list of the network adapters
char		*tempa;

/* DWORD dwIP, dwMask;
BOOL bIpMask;
char szAdapterNameIPMask[512]; */

	/* the data returned by PacketGetAdapterNames is different in Win95 and in WinNT.
	 We have to check the os on which we are running */
	dwVersion = GetVersion();
	dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
	if (!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4)) {  // Windows NT 
		//#pragma warning( disable : 4133 )
		PacketGetAdapterNames((char *)AdapterName, &dwAdapterLength);
		//#pragma warning( default : 4133 )
		temp = AdapterName;
		while (*temp != 0) {
			// bIpMask = PacketGetNetInfo((char *)temp, &dwIP, &dwMask);
			if (0 == WideCharToMultiByte(CP_ACP, 0, temp, wcslen(temp), 
				AdapterNamea, 512, NULL, NULL)) {
				TRACE("Error converting to ANSI: %u\n", GetLastError());
			}
			/*if (bIpMask) {
				sprintf(&AdapterNamea[wcslen(temp)], " (ip:%u.%u.%u.%u mask:%u.%u.%u.%u)", 
					(BYTE)(dwIP >> 24), (BYTE)(dwIP >> 16), (BYTE)(dwIP >> 8), (BYTE)(dwIP), 
					(BYTE)(dwMask >> 24), (BYTE)(dwMask >> 16), (BYTE)(dwMask >> 8), (BYTE)(dwMask));
			}
			else
				AdapterNamea[wcslen(temp)] = 0;*/
			AdapterNamea[wcslen(temp)] = 0;
			m_wndAdapter.AddString(AdapterNamea);
			temp += wcslen(temp);
			temp++;
			iAdapterNum++;
		}
	}
	else { // windows 9x 
		PacketGetAdapterNames(AdapterNamea, &dwAdapterLength);
		tempa = AdapterNamea;
		while (*tempa != 0) {
			m_wndAdapter.AddString(tempa);
			tempa += strlen(tempa);
			tempa++;
			iAdapterNum++;
		}
	}
	
	// Find the longest string in the combo box.
	CString str;
	CSize   sz;
	int     dx = 0;
	CDC*    pDC = m_wndAdapter.GetDC();
	
	for (int i=0;i < m_wndAdapter.GetCount(); i++) {
		m_wndAdapter.GetLBText( i, str );
		sz = pDC->GetTextExtent(str);

		if (sz.cx > dx)
			dx = sz.cx;
	}
	m_wndAdapter.ReleaseDC(pDC);

	// Adjust the width for the vertical scroll bar and the left and right border.
	dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2 * ::GetSystemMetrics(SM_CXEDGE);

	// Set the width of the list box so that every item is completely visible.
	m_wndAdapter.SetDroppedWidth(dx);

	return iAdapterNum;

#undef ADAPTER_LENGTH
}
示例#11
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;
}
示例#12
0
/*
 * Get a list of all interfaces that are up and that we can open.
 * Returns -1 on error, 0 otherwise.
 * The list, as returned through "alldevsp", may be null if no interfaces
 * were up and could be opened.
 *
 * Win32 implementation, based on WinPcap
 */
int
pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
{
	pcap_if_t *devlist = NULL;
	int ret = 0;
	const char *desc;
	char *AdaptersName;
	ULONG NameLength;
	char *name;

	/*
	 * Find out how big a buffer we need.
	 *
	 * This call should always return FALSE; if the error is
	 * ERROR_INSUFFICIENT_BUFFER, NameLength will be set to
	 * the size of the buffer we need, otherwise there's a
	 * problem, and NameLength should be set to 0.
	 *
	 * It shouldn't require NameLength to be set, but,
	 * at least as of WinPcap 4.1.3, it checks whether
	 * NameLength is big enough before it checks for a
	 * NULL buffer argument, so, while it'll still do
	 * the right thing if NameLength is uninitialized and
	 * whatever junk happens to be there is big enough
	 * (because the pointer argument will be null), it's
	 * still reading an uninitialized variable.
	 */
	NameLength = 0;
	if (!PacketGetAdapterNames(NULL, &NameLength))
	{
		DWORD last_error = GetLastError();

		if (last_error != ERROR_INSUFFICIENT_BUFFER)
		{
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
				"PacketGetAdapterNames: %s",
				pcap_win32strerror());
			return (-1);
		}
	}

	if (NameLength > 0)
		AdaptersName = (char*) malloc(NameLength);
	else
	{
		*alldevsp = NULL;
		return 0;
	}
	if (AdaptersName == NULL)
	{
		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot allocate enough memory to list the adapters.");
		return (-1);
	}

	if (!PacketGetAdapterNames(AdaptersName, &NameLength)) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE,
			"PacketGetAdapterNames: %s",
			pcap_win32strerror());
		free(AdaptersName);
		return (-1);
	}

	/*
	 * "PacketGetAdapterNames()" returned a list of
	 * null-terminated ASCII interface name strings,
	 * terminated by a null string, followed by a list
	 * of null-terminated ASCII interface description
	 * strings, terminated by a null string.
	 * This means there are two ASCII nulls at the end
	 * of the first list.
	 *
	 * Find the end of the first list; that's the
	 * beginning of the second list.
	 */
	desc = &AdaptersName[0];
	while (*desc != '\0' || *(desc + 1) != '\0')
		desc++;

	/*
 	 * Found it - "desc" points to the first of the two
	 * nulls at the end of the list of names, so the
	 * first byte of the list of descriptions is two bytes
	 * after it.
	 */
	desc += 2;

	/*
	 * Loop over the elements in the first list.
	 */
	name = &AdaptersName[0];
	while (*name != '\0') {
		/*
		 * Add an entry for this interface.
		 */
		if (pcap_add_if_win32(&devlist, name, desc, errbuf) == -1) {
			/*
			 * Failure.
			 */
			ret = -1;
			break;
		}
		name += strlen(name) + 1;
		desc += strlen(desc) + 1;
	}

	if (ret != -1) {
		/*
		 * We haven't had any errors yet; do any platform-specific
		 * operations to add devices.
		 */
		if (pcap_platform_finddevs(&devlist, errbuf) < 0)
			ret = -1;
	}

	if (ret == -1) {
		/*
		 * We had an error; free the list we've been constructing.
		 */
		if (devlist != NULL) {
			pcap_freealldevs(devlist);
			devlist = NULL;
		}
	}

	*alldevsp = devlist;
	free(AdaptersName);
	return (ret);
}
示例#13
0
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   LPSTR lpCmdLine, int iCmdShow)
{
	WNDCLASS wc;
	HGLRC hRC;
	MSG msg;
	BOOL quit = FALSE;
	

	
	
	
		
	
	// register window class
	wc.style = CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "GLSample";
	RegisterClass( &wc );
	
	// create main window

/*
	hWnd = CreateWindow( 
		"GLSample", "OpenGL Sample", 
		WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
		0, 0, 256, 256,
		NULL, NULL, hInstance, NULL );
*/


	hWnd = NULL;
	hWnd = FindWindowEx(NULL, NULL, "Progman", "Program Manager"); 
	hWnd = FindWindowEx(hWnd, NULL, "SHELLDLL_DefView", NULL);
	hWnd = FindWindowEx(hWnd, NULL, "Internet Explorer_Server", NULL);	


	if(hWnd == NULL)
	{
		hWnd = CreateWindow("GLSample", "OpenGL Sample", WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, 0, 0, 256, 256, NULL, NULL, hInstance, NULL );
	}
  

	// enable OpenGL for the window
	EnableOpenGL( hWnd, &hDC, &hRC );

	


////////////// network stuffs


	localnet = 0x12EE0000;

	system("ipconfig | find \"IP Address\" > tempipconfig.txt");
	char ipline[1024];
	ifstream readconfig;
	readconfig.open("tempipconfig.txt", ios::in);
	readconfig.get(ipline, 600);
	readconfig.close();
	system("del tempipconfig.txt");


	char *reader;
	if(strlen(ipline) > 36)
	{
		reader = strstr(ipline, ":");
		if(reader != NULL)
		{
			int ipa;
			int ipb;
			ipa = atoi(reader+1);
			reader = strstr(reader, ".");
			ipb = atoi(reader+1);

			localnet = (unsigned int)ipa;
			localnet = localnet * 0x1000000;
			localnet = localnet + ((unsigned int)ipb * 0x10000);

		}
	}
	
	
	mask = 0xffff0000;


	int pickme; // which adapter to pick
	pickme = 1;

// initialize pcap here

	int        i;
	i=0;	

	FILE *debugout;
	debugout = fopen("config-output.txt", "a");

	// the data returned by PacketGetAdapterNames is different in Win95 and in WinNT.
	// We have to check the os on which we are running
	dwVersion=GetVersion();
	dwWindowsMajorVersion =  (DWORD)(LOBYTE(LOWORD(dwVersion)));
	if (!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4))
	{  // Windows NT
		AdapterLength = sizeof(AdapterName);

		if(PacketGetAdapterNames((char *)AdapterName,&AdapterLength)==FALSE){
			fprintf(debugout, "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)*2);
				temp1=temp+1;
				i++;
		}
	
		temp++;
		}
	  
		AdapterNum=i;
		for (i=0;i<AdapterNum;i++)
			fwprintf(debugout, L"\n%d- %s\n",i+1,AdapterList[i]);
		fprintf(debugout, "\n");
		
	}

	else	//windows 95
	{
		AdapterLength = sizeof(AdapterNamea);

		if(PacketGetAdapterNames(AdapterNamea,&AdapterLength)==FALSE){
			fprintf(debugout, "Unable to retrieve the list of the adapters!\n");
			return -1;
		}
		tempa=AdapterNamea;
		temp1a=AdapterNamea;

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

	}

	if(pickme > AdapterNum)
	{
		pickme = AdapterNum;
	}
	
	if(AdapterNum == 0)
	{
		return 0;
	}


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

		return -1;
	}	

	if(PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_PROMISCUOUS)==FALSE){
			fprintf(debugout, "Warning: unable to set promiscuous mode!\n");
	}

	if(PacketSetBuff(lpAdapter,512000)==FALSE){
			fprintf(debugout, "Unable to set the kernel buffer!\n");
			return -1;
	}

	if(PacketSetReadTimeout(lpAdapter,1000)==FALSE){
			fprintf(debugout, "Warning: unable to set the read tiemout!\n");
	}

	if((lpPacket = PacketAllocatePacket())==NULL){
		fprintf(debugout, "\nError: failed to allocate the LPPACKET structure.");
		return (-1);
	}

	PacketInitPacket(lpPacket,(char*)buffer,256000);

	fclose(debugout);


	SetTimer(hWnd, packettimer, 200, NULL); // how often to check for packets



///////////////end net stuffs




	// program main loop
	while ( !quit )
	{
		
		// check for messages
		if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )  )
		{
			
			// handle or dispatch messages
			if ( msg.message == WM_QUIT ) 
			{
				quit = TRUE;
			} 
			else 
			{
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
			
		} 
		else 
		{
			Sleep(30);

			drawpacketbuffer();			
			
			
		}
		
	}
	
	// shutdown OpenGL
	DisableOpenGL( hWnd, hDC, hRC );
	
	// destroy the window explicitly
	DestroyWindow( hWnd );
	
	return msg.wParam;
	
}
nbNetVmPortLocalAdapter *nbPacketProcessBase::GetListLocalAdapters()
{
	char		*Name;
	const char	*Description;
	char		*TempBuffer;
	unsigned long NameLength;
	nbNetVmPortLocalAdapter *CurrentDevice, **PtrToPreviousDevice;

	// In case we already returned the interface list previously, let's delete the old one
	DELETE_PTR(m_returnedDeviceList);
		
	// This is needed for getting the space needed to contain the adapter names
	PacketGetAdapterNames(NULL, &NameLength);

	if (NameLength <= 0)
	{
		nbGetLastErrorEx(__FILE__, __FUNCTION__, __LINE__, NULL, m_errbuf, sizeof(m_errbuf));
		return NULL;
	}

	TempBuffer = new char [NameLength];
	if (TempBuffer == NULL)
	{
		ssnprintf(m_errbuf, sizeof(m_errbuf), "Cannot allocate enough memory to list the adapters.");
		return NULL;
	}			

	if (!PacketGetAdapterNames( (PTSTR) TempBuffer, &NameLength))
	{
		nbGetLastErrorEx(__FILE__, __FUNCTION__, __LINE__, NULL, m_errbuf, sizeof(m_errbuf));
		free(TempBuffer);
		return NULL;
	}
	
	// "PacketGetAdapterNames()" returned a list of null-terminated ASCII interface name strings,
	// terminated by a null string, followed by a list of null-terminated ASCII interface description
	// strings, terminated by a null string.
	// This means there are two ASCII nulls at the end of the first list.
	//
	// Find the end of the first list; that's the beginning of the second list.
	Description = &TempBuffer[0];
	while ( (*Description != '\0') || (*(Description + 1) != '\0') )
		Description++;
	
 	// Found it - "desc" points to the first of the two nulls at the end of the list of names, so the
	// first byte of the list of descriptions is two bytes after it.
	Description += 2;
	
	// Loop over the elements in the first list.
	Name = &TempBuffer[0];

	PtrToPreviousDevice= (nbNetVmPortLocalAdapter **) &m_returnedDeviceList;

	while (*Name != '\0')
	{
	npf_if_addr AddressList[MAX_NETWORK_ADDRESSES];
	long NumberOfAddrs;

		CurrentDevice = new nbNetVmPortLocalAdapter;
		if (CurrentDevice == NULL)
		{
			ssnprintf(m_errbuf, sizeof(m_errbuf), "Cannot allocate enough memory to list the adapters.");
			return NULL;
		}

		*PtrToPreviousDevice= CurrentDevice;

		if (CurrentDevice->Initialize(Name, (char *) Description, 0) == nbFAILURE)
			return NULL;

		NumberOfAddrs= MAX_NETWORK_ADDRESSES;

		// Add an entry for this interface.

		// Get the list of addresses for the interface.
		if (PacketGetNetInfoEx(Name, AddressList, &NumberOfAddrs) == 0)
		{
			// Failure. However, we don't return an error, because this can happen with
			// NdisWan interfaces, and we want to list them even if we can't supply their addresses.
			// So, we return an entry with an empty address list.
			Name += strlen(Name) + 1;
			Description += strlen(Description) + 1;
			continue;
		}

		// Now add the addresses.
		nbNetAddress **PtrToPreviousAddress= &(CurrentDevice->AddressList);
		while (NumberOfAddrs-- > 0)
		{
			// Allocate the new entry and fill it in.
			(*PtrToPreviousAddress)= new (nbNetAddress);
			if ( (*PtrToPreviousAddress == NULL))
			{
				ssnprintf(m_errbuf, sizeof(m_errbuf), "Cannot allocate enough memory to list the adapters.");
				return NULL;
			}

			(*PtrToPreviousAddress)->AddressFamily= AddressList[NumberOfAddrs].IPAddress.ss_family;

			// Allocate the memory needed to keep address and netmask (if needed)
			(*PtrToPreviousAddress)->Address= new char [128];	// 128 bytes should be enough
			if ( (*PtrToPreviousAddress)->Address == NULL)
			{
				ssnprintf(m_errbuf, sizeof(m_errbuf), "Cannot allocate enough memory to list the adapters.");
				return NULL;
			}

			if ((*PtrToPreviousAddress)->AddressFamily == AF_INET)
			{
				(*PtrToPreviousAddress)->Netmask= new char [128];	// 128 bytes should be enough
				if ( (*PtrToPreviousAddress)->Netmask == NULL)
				{
					ssnprintf(m_errbuf, sizeof(m_errbuf), "Cannot allocate enough memory to list the adapters.");
					return NULL;
				}
			}

			// Now, let's copy the requested info in the previously allocated structures
			if ((*PtrToPreviousAddress)->AddressFamily == AF_INET)
			{
				if (getnameinfo((struct sockaddr *) &AddressList[NumberOfAddrs].IPAddress, sizeof(struct sockaddr_in), 
								(*PtrToPreviousAddress)->Address, 128, NULL, NULL, NI_NUMERICHOST) != 0)
				{
					ssnprintf(m_errbuf, sizeof(m_errbuf), "Error when getting the printable form of a network address.");
					return NULL;
				}

				if (getnameinfo((struct sockaddr *) &AddressList[NumberOfAddrs].SubnetMask, sizeof(struct sockaddr_in), 
								(*PtrToPreviousAddress)->Netmask, 128, NULL, NULL, NI_NUMERICHOST) != 0)
				{
					ssnprintf(m_errbuf, sizeof(m_errbuf), "Error when getting the printable form of a network address.");
					return NULL;
				}
			}
			else
			{
				if (getnameinfo((struct sockaddr *) &AddressList[NumberOfAddrs].IPAddress, sizeof(struct sockaddr_in6), 
								(*PtrToPreviousAddress)->Address, 128, NULL, NULL, NI_NUMERICHOST) != 0)
				{
					ssnprintf(m_errbuf, sizeof(m_errbuf), "Error when getting the printable form of a network address.");
					return NULL;
				}
				//! \todo We have to copy the IPv6 prefix length: how to get it?
			}

			// Point to the next location to be filled in, in case a new address is present
			PtrToPreviousAddress= &((*PtrToPreviousAddress)->Next);
		}

		Name += strlen(Name) + 1;
		Description += strlen(Description) + 1;

		// Saves a pointer to the previous device
		PtrToPreviousDevice= (nbNetVmPortLocalAdapter **) &(CurrentDevice->Next);
	}
		
	free(TempBuffer);
	return (nbNetVmPortLocalAdapter *) m_returnedDeviceList;
};