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: triadR is an unknown quantity at this point 
Solved Threads: 0
triadR triadR is offline Offline
Newbie Poster

ARP keep sending wrong IP address

 
0
  #1
Jul 11th, 2008
hi, i've a code shown like this..
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <winsock2.h>
  4. #include <pcap.h>
  5. #include <remote-ext.h>
  6.  
  7. #define ETH_IP 0x0800 // type: IP
  8. #define ETH_ARP 0x0806 // type: ARP
  9. #define ETH_ICMP 0x0800 // type: ICMP = IP
  10.  
  11. #define ARP_HARDWARE 0x0001 // type for 802.3 frames
  12. #define ARP_REQUEST 0x0001 // ARP request
  13. #define ARP_REPLY 0x0002 // ARP reply
  14.  
  15.  
  16. typedef struct eth_header{
  17. u_char eth_dst[6];
  18. u_char eth_src[6];
  19. u_short eth_type;
  20. }ETH_HEADER;
  21.  
  22. typedef struct arp_header{
  23. u_short arp_htype;
  24. u_short arp_ptype;
  25. u_char arp_hlen;
  26. u_char arp_plen;
  27. u_short arp_oper;
  28. u_char arp_sha[6];
  29. u_long arp_spa;
  30. u_char arp_tha[6];
  31. u_long arp_tpa;
  32. }ARP_HEADER;
  33.  
  34. int main(){
  35. pcap_if_t *alldevs;
  36. pcap_if_t *d;
  37. pcap_t *adhandle;
  38. ETH_HEADER eth_header;
  39. ARP_HEADER arp_header;
  40. char errbuf[PCAP_ERRBUF_SIZE];
  41. int inum;
  42. int i = 0;
  43. unsigned char src_mac[6] = {0x10,0x11, 0x11, 0x11, 0x11, 0x11};
  44. unsigned char dst_mac[6] = {0x00,0xff, 0xff, 0x00, 0x00, 0x00};
  45.  
  46. u_char sendbuf[1024];
  47.  
  48. /* Retrieve the device list */
  49. if (pcap_findalldevs(&alldevs, errbuf) == -1){
  50. fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
  51. return -1;
  52. }
  53.  
  54. /* Print the list */
  55. for(d=alldevs; d; d=d->next){
  56. printf("%d. %s", ++i, d->name);
  57. if (d->description)
  58. printf(" (%s)\n", d->description);
  59. else
  60. printf(" (No description available)\n");
  61. }
  62.  
  63. if(i==0){
  64. printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
  65. return -1;
  66. }
  67. printf("Enter the interface number (1-%d):",i);
  68. scanf("%d", &inum);
  69. //inum = 3;
  70.  
  71. if(inum < 1 || inum > i){
  72. printf("\nInterface number out of range.\n");
  73. /* Free the device list */
  74. pcap_freealldevs(alldevs);
  75. return -1;
  76. }
  77.  
  78. /* Jump to the selected adapter */
  79. for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
  80.  
  81. /* Open the adapter */
  82. if ( (adhandle= pcap_open_live(d->name, // name of the device
  83. 65536, // portion of the packet to capture.
  84. 1, // promiscuous mode
  85. 1000, // read timeout
  86. errbuf // error buffer
  87. ) ) == NULL){
  88. fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
  89. /* Free the device list */
  90. pcap_freealldevs(alldevs);
  91. return -1;
  92. }
  93.  
  94. // ARP
  95.  
  96. /* Fill in eth hdr */
  97. memcpy(eth_header.eth_dst, dst_mac, 6);
  98. memcpy(eth_header.eth_src, src_mac, 6);
  99. eth_header.eth_type = htons(ETH_ARP);
  100.  
  101. /* Fill in arp hdr */
  102. arp_header.arp_htype = htons(ARP_HARDWARE);
  103. arp_header.arp_ptype = htons(ETH_IP);
  104. arp_header.arp_hlen = 6;
  105. arp_header.arp_plen = 4;
  106. arp_header.arp_oper = htons(ARP_REQUEST);
  107. memcpy(arp_header.arp_sha, src_mac, 6);
  108. arp_header.arp_spa = htonl(inet_addr("192.168.0.1"));
  109. memcpy(arp_header.arp_tha, dst_mac, 6);
  110. arp_header.arp_tpa = htonl(inet_addr("192.168.0.2"));
  111.  
  112. memset(sendbuf,0,sizeof(sendbuf));
  113. memcpy(sendbuf, &eth_header, sizeof(eth_header));
  114. memcpy(sendbuf + sizeof(eth_header), &arp_header, sizeof(arp_header));
  115.  
  116. /* Send down the packet */
  117. if (pcap_sendpacket(adhandle, sendbuf,sizeof(eth_header)+sizeof(arp_header)) != 0){
  118. fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(adhandle));
  119. return 0;
  120. }
  121.  
  122. printf("\nSent ARP request\n");
  123.  
  124. return 0;
  125. }

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: ARP keep sending wrong IP address

 
0
  #2
Jul 12th, 2008
Is sizeof(ARP_HEADER) what you expect?
In other words, is padding and alignment an issue?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: triadR is an unknown quantity at this point 
Solved Threads: 0
triadR triadR is offline Offline
Newbie Poster

Re: ARP keep sending wrong IP address

 
0
  #3
Jul 22nd, 2008
Originally Posted by Salem View Post
Is sizeof(ARP_HEADER) what you expect?
In other words, is padding and alignment an issue?
sorry for replying late..
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: triadR is an unknown quantity at this point 
Solved Threads: 0
triadR triadR is offline Offline
Newbie Poster

Re: ARP keep sending wrong IP address

 
0
  #4
Jul 22nd, 2008
Originally Posted by Salem View Post
Is sizeof(ARP_HEADER) what you expect?
In other words, is padding and alignment an issue?
salem,
after i check it again..
yeah, i found that there's something about the arp_header
it seems like the type or something..
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: triadR is an unknown quantity at this point 
Solved Threads: 0
triadR triadR is offline Offline
Newbie Poster

Re: ARP keep sending wrong IP address

 
0
  #5
Jul 26th, 2008
Finally, i've found the answer...

the memcpy(arp_header.arp_sha, src_mac, 6); takes more place than just 6 space of array

my suggest is copy it one by one or just find another way...
Last edited by triadR; Jul 26th, 2008 at 4:51 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC