I'm using windows xp sp2 so I would like to know how to do it with winpcap, an example source will be helpful, thanks.
dougy83 74 Posting Whiz in Training
Here's some code from a while ago; it's for tcp packets, but if you change the data it'll be for arp packets.
I've also attached someone else's code for the ethernet crc32 calc
ULONG crc32_table[256]; // Lookup table array
// CRCdemo.cpp
ULONG Reflect(ULONG ref, char ch)
{// Used only by Init_CRC32_Table().
ULONG value(0);
// Swap bit 0 for bit 7
// bit 1 for bit 6, etc.
for(int i = 1; i < (ch + 1); i++)
{
if(ref & 1)
value |= 1 << (ch - i);
ref >>= 1;
}
return value;
}
void Init_CRC32_Table()
{// Call this function only once to initialize the CRC table.
// This is the official polynomial used by CRC-32
// in PKZip, WinZip and Ethernet.
ULONG ulPolynomial = 0x04c11db7;
// 256 values representing ASCII character codes.
for(int i = 0; i <= 0xFF; i++)
{
crc32_table[i]=Reflect(i, 8) << 24;
for (int j = 0; j < 8; j++)
crc32_table[i] = (crc32_table[i] << 1) ^ (crc32_table[i] & (1 << 31) ? ulPolynomial : 0);
crc32_table[i] = Reflect(crc32_table[i], 32);
}
}
int Get_CRC(char* text, int len) {// Pass a text string to this function and it will return the CRC.
// Once the lookup table has been filled in by the two functions above,
// this function creates all CRCs using only the lookup table.
// Be sure to use unsigned variables,
// because negative values introduce high bits
// where zero bits are required.
// Start out with all bits set high.
ULONG ulCRC(0xffffffff);
unsigned char* buffer;
// Save the text in the buffer.
buffer = (unsigned char*)text;
// Perform the algorithm on each character
// in the string, using the lookup table values.
while(len--)
ulCRC = (ulCRC >> 8) ^ crc32_table[(ulCRC & 0xFF) ^ *buffer++];
// Exclusive OR the result with the beginning value.
return ulCRC ^ 0xffffffff;
}
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <pcap.h>
#include <packet32.h>
#include <NTDDNDIS.h>
#include <remote-ext.h>
#include "ethernetFCS.cpp"
using namespace std;
int tp_=1;
#define tp printf("TEST POINT %i\n",tp_++);
int main(int argc, char **argv)
{
// argv[1] = "rpcap://\\Device\\NPF_{271E1CC8-BDAC-4134-AFDB-2792DF50075A}";
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
u_char packet[100];
int i;
/* Check the validity of the command line */
if (argc != 2)
{
printf("usage: %s interface (e.g. 'rpcap://eth0')\n", argv[0]);
pcap_if_t *alldevs;
pcap_if_t *d;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE];
/* Retrieve the device list from the local machine */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
exit(1);
}
/* Print the list */
for(d= alldevs; d != NULL; d= d->next)
{
printf("%d. %s\n", ++i, d->name);
if (d->description)
printf(" (%s)\n\n", d->description);
else
printf(" (No description available)\n");
}
if (i == 0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}
/* We don't need any more the device list. Free it */
pcap_freealldevs(alldevs);
getch();
return -1;
}
tp
/* Open the output device */
if ( (fp= pcap_open(argv[1], // name of the device
100, // portion of the packet to capture (only the first 100 bytes)
PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
1000, // read timeout
NULL, // authentication on the remote machine
errbuf // error buffer
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", argv[1]);
getch();
return -1;
}
tp
char *dstMac = "00-12-79-d1-e6-fb";
char *srcMac = "00-00-0c-07-ac-02";
// Supposing to be on ethernet, set mac destination to 1:1:1:1:1:1
for(int i = 0; i < 6; i++){
char *x;
packet[i] = strtol(&dstMac[i*3], &x, 16);
packet[i+6] = strtol(&srcMac[i*3], &x, 16);
}
tp
packet[12] = 0x08; // IP datagram
packet[13] = 0x00;
// this is the rawish tcp packet as dumped by tcpdump
char *tcpPacket =
"4500 0033 0301 4000 8006 d551 0ae7 0469 "
"0af1 0832 076c 0d1b a704 f1d2 016d 1aa8 "
"5018 ffff 76ad 0000 7465 7374 2073 7472 "
"696e 67 ";// bfb20828"; // the last number is the fcs
/* Fill the rest of the packet */
int pktSize = 14;
for(int j = 0; tcpPacket[j] && i+14 < sizeof(packet); j++){
if(tcpPacket[j] > 32){
char a[5] = {'0', 'x', tcpPacket[j], tcpPacket[j+1], 0}, *x;
j++;
packet[pktSize++] = strtol(a, &x, 16);
printf("atoi(%s) = %X\n", a, strtol(a, &x, 16));
}
}
tp
Init_CRC32_Table() ;
int fcs = Get_CRC((char*)packet, pktSize);
packet[pktSize++] = (char)(fcs>>0);
packet[pktSize++] = (char)(fcs>>8);
packet[pktSize++] = (char)(fcs>>16);
packet[pktSize++] = (char)(fcs>>24);
printf("FCS: %X\n", fcs);
tp
while(!kbhit()){
/* Send down the packet */
printf("sending packet\n");
for(int i = 0; i < pktSize; i++){
if(i & 0xF == 0)
printf("\n");
printf("%.2X", packet[i]);
if(i & 1)
printf(" ");
}
if (pcap_sendpacket(fp, packet, pktSize /* size */) != 0){
fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(fp));
getch();
return -1;
}
Sleep(500);
}
// getch();
return 0;
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.