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); }
int libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t *payload, uint32_t payload_s) { static BYTE dst[ETHER_ADDR_LEN]; static BYTE src[ETHER_ADDR_LEN]; uint8_t *packet = NULL; uint32_t packet_s; LPPACKET lpPacket = NULL; DWORD remoteip = 0; DWORD BytesTransfered; NetType type; struct libnet_ipv4_hdr *ip_hdr = NULL; memset(dst, 0, sizeof(dst)); memset(src, 0, sizeof(src)); packet_s = payload_s + l->link_offset; packet = (uint8_t *)malloc(packet_s); if (packet == NULL) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): failed to allocate packet\n", __func__); return (-1); } /* we have to do the IP checksum */ if (libnet_do_checksum(l, payload, IPPROTO_IP, LIBNET_IPV4_H) == -1) { /* error msg set in libnet_do_checksum */ return (-1); } /* MACs, IPs and other stuff... */ ip_hdr = (struct libnet_ipv4_hdr *)payload; memcpy(src, libnet_get_hwaddr(l), sizeof(src)); remoteip = ip_hdr->ip_dst.S_un.S_addr; /* check if the remote station is the local station */ if (remoteip == libnet_get_ipaddr4(l)) { memcpy(dst, src, sizeof(dst)); } else { memcpy(dst, libnet_win32_get_remote_mac(l, remoteip), sizeof(dst)); } PacketGetNetType(l->lpAdapter, &type); switch(type.LinkType) { case NdisMedium802_3: libnet_win32_build_fake_ethernet(dst, src, ETHERTYPE_IP, payload, payload_s, packet, l , 0); break; case NdisMedium802_5: libnet_win32_build_fake_token(dst, src, ETHERTYPE_IP, payload, payload_s, packet, l, 0); break; case NdisMediumFddi: break; case NdisMediumWan: case NdisMediumAtm: case NdisMediumArcnet878_2: default: snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): network type (%d) is not supported\n", __func__, type.LinkType); return (-1); break; } BytesTransfered = -1; if ((lpPacket = PacketAllocatePacket()) == NULL) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): failed to allocate the LPPACKET structure\n", __func__); return (-1); } PacketInitPacket(lpPacket, packet, packet_s); /* PacketSendPacket returns a BOOLEAN */ if (PacketSendPacket(l->lpAdapter, lpPacket, TRUE)) { BytesTransfered = packet_s; } PacketFreePacket(lpPacket); free(packet); return (BytesTransfered); }
pcap_t * pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf) { register pcap_t *p; NetType type; #ifdef REMOTE /* Retrofit; we have to make older applications compatible with the remote capture So, we're calling the pcap_open_remote() from here, that is a very dirty thing. Obviously, we cannot exploit all the new features; for instance, we cannot send authentication, we cannot use a UDP data connection, and so on. */ char host[PCAP_BUF_SIZE + 1]; char port[PCAP_BUF_SIZE + 1]; char name[PCAP_BUF_SIZE + 1]; int srctype; if (pcap_parsesrcstr(device, &srctype, host, port, name, ebuf) ) return NULL; if (srctype == PCAP_SRC_IFREMOTE) { p= pcap_opensource_remote(device, NULL, ebuf); if (p == NULL) return NULL; p->snapshot= snaplen; p->timeout= to_ms; p->rmt_flags= (promisc) ? PCAP_OPENFLAG_PROMISCUOUS : 0; return p; } #endif /* Init WinSock */ wsockinit(); p = (pcap_t *)malloc(sizeof(*p)); if (p == NULL) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); return (NULL); } memset(p, 0, sizeof(*p)); p->adapter=NULL; p->adapter=PacketOpenAdapter(device); if (p->adapter==NULL) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "Error opening adapter: %s", pcap_win32strerror()); return NULL; } /*get network type*/ if(PacketGetNetType (p->adapter,&type)==FALSE) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "Cannot determine the network type: %s", pcap_win32strerror()); goto bad; } /*Set the linktype*/ switch (type.LinkType) { case NdisMediumWan: p->linktype = DLT_EN10MB; break; case NdisMedium802_3: p->linktype = DLT_EN10MB; break; case NdisMediumFddi: p->linktype = DLT_FDDI; break; case NdisMedium802_5: p->linktype = DLT_IEEE802; break; case NdisMediumArcnetRaw: p->linktype = DLT_ARCNET; break; case NdisMediumArcnet878_2: p->linktype = DLT_ARCNET; break; case NdisMediumAtm: p->linktype = DLT_ATM_RFC1483; break; default: p->linktype = DLT_EN10MB; /*an unknown adapter is assumed to be ethernet*/ break; } /* Set promisquous mode */ if (promisc) PacketSetHwFilter(p->adapter,NDIS_PACKET_TYPE_PROMISCUOUS); else PacketSetHwFilter(p->adapter,NDIS_PACKET_TYPE_ALL_LOCAL); /* Set the buffer size */ p->bufsize = PcapBufSize; p->buffer = (u_char *)malloc(PcapBufSize); if (p->buffer == NULL) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); goto bad; } p->snapshot = snaplen; /* allocate Packet structure used during the capture */ if((p->Packet = PacketAllocatePacket())==NULL){ snprintf(ebuf, PCAP_ERRBUF_SIZE, "failed to allocate the PACKET structure"); goto bad; } PacketInitPacket(p->Packet,(BYTE*)p->buffer,p->bufsize); /* allocate the standard buffer in the driver */ if(PacketSetBuff( p->adapter, SIZE_BUF)==FALSE) { snprintf(ebuf, PCAP_ERRBUF_SIZE,"driver error: not enough memory to allocate the kernel buffer\n"); goto bad; } /* tell the driver to copy the buffer only if it contains at least 16K */ if(PacketSetMinToCopy(p->adapter,16000)==FALSE) { snprintf(ebuf, PCAP_ERRBUF_SIZE,"Error calling PacketSetMinToCopy: %s\n", pcap_win32strerror()); goto bad; } PacketSetReadTimeout(p->adapter, to_ms); return (p); bad: if (p->adapter) PacketCloseAdapter(p->adapter); if (p->buffer != NULL) free(p->buffer); free(p); return (NULL); }