| | |
ARP keep sending wrong IP address
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Solved Threads: 0
hi, i've a code shown like this..
Can anybody tell me why it keeps sending wrong ip address..
Any help will be greatly appreciated.. Thx..
c Syntax (Toggle Plain Text)
#include <stdlib.h> #include <stdio.h> #include <winsock2.h> #include <pcap.h> #include <remote-ext.h> #define ETH_IP 0x0800 // type: IP #define ETH_ARP 0x0806 // type: ARP #define ETH_ICMP 0x0800 // type: ICMP = IP #define ARP_HARDWARE 0x0001 // type for 802.3 frames #define ARP_REQUEST 0x0001 // ARP request #define ARP_REPLY 0x0002 // ARP reply typedef struct eth_header{ u_char eth_dst[6]; u_char eth_src[6]; u_short eth_type; }ETH_HEADER; typedef struct arp_header{ u_short arp_htype; u_short arp_ptype; u_char arp_hlen; u_char arp_plen; u_short arp_oper; u_char arp_sha[6]; u_long arp_spa; u_char arp_tha[6]; u_long arp_tpa; }ARP_HEADER; int main(){ pcap_if_t *alldevs; pcap_if_t *d; pcap_t *adhandle; ETH_HEADER eth_header; ARP_HEADER arp_header; char errbuf[PCAP_ERRBUF_SIZE]; int inum; int i = 0; unsigned char src_mac[6] = {0x10,0x11, 0x11, 0x11, 0x11, 0x11}; unsigned char dst_mac[6] = {0x00,0xff, 0xff, 0x00, 0x00, 0x00}; u_char sendbuf[1024]; /* Retrieve the device list */ if (pcap_findalldevs(&alldevs, errbuf) == -1){ fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf); return -1; } /* Print the list */ for(d=alldevs; d; d=d->next){ printf("%d. %s", ++i, d->name); if (d->description) printf(" (%s)\n", d->description); else printf(" (No description available)\n"); } if(i==0){ printf("\nNo interfaces found! Make sure WinPcap is installed.\n"); return -1; } printf("Enter the interface number (1-%d):",i); scanf("%d", &inum); //inum = 3; if(inum < 1 || inum > i){ printf("\nInterface number out of range.\n"); /* Free the device list */ pcap_freealldevs(alldevs); return -1; } /* Jump to the selected adapter */ for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++); /* Open the adapter */ if ( (adhandle= pcap_open_live(d->name, // name of the device 65536, // portion of the packet to capture. 1, // promiscuous mode 1000, // read timeout errbuf // error buffer ) ) == NULL){ fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n"); /* Free the device list */ pcap_freealldevs(alldevs); return -1; } // ARP /* Fill in eth hdr */ memcpy(eth_header.eth_dst, dst_mac, 6); memcpy(eth_header.eth_src, src_mac, 6); eth_header.eth_type = htons(ETH_ARP); /* Fill in arp hdr */ arp_header.arp_htype = htons(ARP_HARDWARE); arp_header.arp_ptype = htons(ETH_IP); arp_header.arp_hlen = 6; arp_header.arp_plen = 4; arp_header.arp_oper = htons(ARP_REQUEST); memcpy(arp_header.arp_sha, src_mac, 6); arp_header.arp_spa = htonl(inet_addr("192.168.0.1")); memcpy(arp_header.arp_tha, dst_mac, 6); arp_header.arp_tpa = htonl(inet_addr("192.168.0.2")); memset(sendbuf,0,sizeof(sendbuf)); memcpy(sendbuf, ð_header, sizeof(eth_header)); memcpy(sendbuf + sizeof(eth_header), &arp_header, sizeof(arp_header)); /* Send down the packet */ if (pcap_sendpacket(adhandle, sendbuf,sizeof(eth_header)+sizeof(arp_header)) != 0){ fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(adhandle)); return 0; } printf("\nSent ARP request\n"); return 0; }
Can anybody tell me why it keeps sending wrong ip address..
Any help will be greatly appreciated.. Thx..
Last edited by triadR; Jul 11th, 2008 at 1:17 pm.
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
Is sizeof(ARP_HEADER) what you expect?
In other words, is padding and alignment an issue?
no, i dont think there's a problem with the alignment.
I think it's a standard arp header struct.
when i used it to send packet the source address
arp_header.arp_spa = htonl(inet_addr("192.168.0.1"));
become
204.204.1.0
and the destination address
arp_header.arp_tpa = htonl(inet_addr("192.168.0.2"));
become
255.255.204.204
![]() |
Other Threads in the C Forum
- Previous Thread: shortcut to a large number of nested if-else statements
- Next Thread: srand trouble
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test testautomation testing threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






