473,662 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ARP packet network program

Hi all~
I've written a network program in C.

This program catputures ARP request packet and reply wrong ARP packet.
Hi. I've written a small program to learn to write in C. But
unfortunately the output is all jumbled up and not nice
Caputuring is ok, but couldn't send ARP packet. What's wrong?

( using 2PC, this program run in Linux and moitoring other PC,Window
XP)
(generator packet prgram is BillSniff, monitoring program is
WireShark)


#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>

#include <asm/types.h>

#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>

#define BUF_SIZE 42
#define DEVICE "eth0"
#define ETH_P_NULL 0x0
#define ETH_MAC_LEN ETH_ALEN
#define ETH_ARP 0x0806

int s = 0; /*Socketdescript or*/
void* buffer = NULL;
long total_packets = 0;
long answered_packet s = 0;

void sigint(int signum);

struct __attribute__(( packed)) arp_header
{
unsigned short arp_hd;
unsigned short arp_pr;
unsigned char arp_hdl;
unsigned char arp_prl;
unsigned short arp_op;
unsigned char arp_sha[6];
unsigned char arp_spa[4];
unsigned char arp_dha[6];
unsigned char arp_dpa[4];
};
int main(void) {
buffer = (void*)malloc(B UF_SIZE); /*Buffer for Ethernet Frame*/
unsigned char* etherhead = buffer; /*Pointer to Ethenet Header*/
struct ethhdr *eh = (struct ethhdr *)etherhead; /*Another pointer to
ethernet header*/
unsigned char* arphead = buffer + 14;
struct arp_header *ah;
unsigned char src_mac[6]; /*our MAC address*/

struct ifreq ifr;
struct sockaddr_ll socket_address;
int ifindex = 0; /*Ethernet Interface index*/
int i;
int length; /*length of received packet*/
int sent;

printf("Server started, entering initialiation phase...\n");

/*open socket*/
s = socket(AF_PACKE T, SOCK_RAW, htons(ETH_P_ALL ));
if (s == -1) {
perror("socket( ):");
exit(1);
}
printf("Success fully opened socket: %i\n", s);

/*retrieve ethernet interface index*/
strncpy(ifr.ifr _name, DEVICE, IFNAMSIZ);
if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
perror("SIOCGIF INDEX");
exit(1);
}
ifindex = ifr.ifr_ifindex ;
printf("Success fully got interface index: %i\n", ifindex);

/*retrieve corresponding MAC*/
if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) {
perror("SIOCGIF INDEX");
exit(1);
}
for (i = 0; i < 6; i++) {
src_mac[i] = ifr.ifr_hwaddr. sa_data[i];
}
printf("Success fully got our MAC address: %02X:%02X:%02X: %02X:%02X:
%02X\n",
src_mac[0],src_mac[1],src_mac[2],src_mac[3],src_mac[4],src_mac[5]);

/*prepare sockaddr_ll*/
socket_address. sll_family = PF_PACKET;
socket_address. sll_protocol = htons(ETH_P_IP) ;
socket_address. sll_ifindex = ifindex;
socket_address. sll_hatype = ARPHRD_ETHER;
socket_address. sll_pkttype = PACKET_OTHERHOS T;
socket_address. sll_halen = ETH_ALEN;
socket_address. sll_addr[6] = 0x00;
socket_address. sll_addr[7] = 0x00;
/*establish signal handler*/
signal(SIGINT, sigint);
printf("Success fully established signal handler for SIGINT\n");

printf("We are in production state, waiting for incoming packets....
\n");

while (1) {
/*Wait for incoming packet...*/
length = recvfrom(s, buffer, BUF_SIZE, 0, NULL, NULL);
if (length == -1)
{
perror("recvfro m():");
exit(1);
}
if(htons(eh->h_proto) == 0x806)
{

unsigned char buf_arp_dha[6];
unsigned char buf_arp_dpa[4];

ah = (struct arp_header *)arphead;
if(htons(ah->arp_op) != 0x0001)
continue;

printf("H/D TYPE : %x PROTO TYPE : %x \n",ah->arp_hd,ah->arp_pr);
printf("H/D leng : %x PROTO leng : %x \n",ah->arp_hdl,ah->arp_prl);
printf("OPERATI ON : %x \n", ah->arp_op);
printf("SENDER MAC address: %02X:%02X:%02X: %02X:%02X:%02X\ n",
ah->arp_sha[0],
ah->arp_sha[1],
ah->arp_sha[2],
ah->arp_sha[3],
ah->arp_sha[4],
ah->arp_sha[5]
);
printf("SENDER IP address: %02d:%02d:%02d: %02d\n",
ah->arp_spa[0],
ah->arp_spa[1],
ah->arp_spa[2],
ah->arp_spa[3]
);
printf("TARGET MAC address: %02X:%02X:%02X: %02X:%02X:%02X\ n",
ah->arp_dha[0],
ah->arp_dha[1],
ah->arp_dha[2],
ah->arp_dha[3],
ah->arp_dha[4],
ah->arp_dha[5]
);
printf("TARGET IP address: %02d:%02d:%02d: %02d\n",
ah->arp_dpa[0],
ah->arp_dpa[1],
ah->arp_dpa[2],
ah->arp_dpa[3]
);

printf("+++++++ +++++++++++++++ +++++++++++++++ ++\n");
printf("ETHER DST MAC address: %02X:%02X:%02X: %02X:%02X:%02X\ n",
eh->h_dest[0],
eh->h_dest[1],
eh->h_dest[2],
eh->h_dest[3],
eh->h_dest[4],
eh->h_dest[5]
);
printf("ETHER SRC MAC address: %02X:%02X:%02X: %02X:%02X:%02X\ n",
eh->h_source[0],
eh->h_source[1],
eh->h_source[2],
eh->h_source[3],
eh->h_source[4],
eh->h_source[5]
);
memcpy( (void*)etherhea d, (const void*)(etherhea d+ETH_MAC_LEN),
ETH_MAC_LEN);
memcpy( (void*)(etherhe ad+ETH_MAC_LEN) , (const void*)src_mac,
ETH_MAC_LEN);
eh->h_proto = ETH_ARP;
printf("&&&&&&& &&&&&&&&&&&&&&& &&&&&&&&&&&&&&& &&&&&\n");
printf("ETHER DST MAC address: %02X:%02X:%02X: %02X:%02X:%02X\ n",
eh->h_dest[0],
eh->h_dest[1],
eh->h_dest[2],
eh->h_dest[3],
eh->h_dest[4],
eh->h_dest[5]
);
printf("ETHER SRC MAC address: %02X:%02X:%02X: %02X:%02X:%02X\ n",
eh->h_source[0],
eh->h_source[1],
eh->h_source[2],
eh->h_source[3],
eh->h_source[4],
eh->h_source[5]
);
ah->arp_hd = ntohs(ah->arp_hd);
ah->arp_pr = ntohs(ah->arp_pr);

ah->arp_op = 0x0002;

buf_arp_dpa[0] = ah->arp_dpa[0];
buf_arp_dpa[1] = ah->arp_dpa[1];
buf_arp_dpa[2] = ah->arp_dpa[2];
buf_arp_dpa[3] = ah->arp_dpa[3];

ah->arp_dha[0] = ah->arp_sha[0];
ah->arp_dha[1] = ah->arp_sha[1];
ah->arp_dha[2] = ah->arp_sha[2];
ah->arp_dha[3] = ah->arp_sha[3];
ah->arp_dha[4] = ah->arp_sha[4];
ah->arp_dha[5] = ah->arp_sha[5];

ah->arp_dpa[0] = ah->arp_spa[0];
ah->arp_dpa[1] = ah->arp_spa[1];
ah->arp_dpa[2] = ah->arp_spa[2];
ah->arp_dpa[3] = ah->arp_spa[3];

ah->arp_spa[0] = buf_arp_dpa[0];
ah->arp_spa[1] = buf_arp_dpa[1];
ah->arp_spa[2] = buf_arp_dpa[2];
ah->arp_spa[3] = buf_arp_dpa[3];

ah->arp_sha[0] = 0x12;
ah->arp_sha[1] = 0x34;
ah->arp_sha[2] = 0x56;
ah->arp_sha[3] = 0x78;
ah->arp_sha[4] = 0x9a;
ah->arp_sha[5] = 0xbc;

socket_address. sll_addr[0] = eh->h_dest[0];
socket_address. sll_addr[1] = eh->h_dest[1];
socket_address. sll_addr[2] = eh->h_dest[2];
socket_address. sll_addr[3] = eh->h_dest[3];
socket_address. sll_addr[4] = eh->h_dest[4];
socket_address. sll_addr[5] = eh->h_dest[5];
printf("======= =============== =============== ==\n");
printf("SENDER MAC address: %02X:%02X:%02X: %02X:%02X:%02X\ n",
ah->arp_sha[0],
ah->arp_sha[1],
ah->arp_sha[2],
ah->arp_sha[3],
ah->arp_sha[4],
ah->arp_sha[5]
);
printf("SENDER IP address: %02d:%02d:%02d: %02d\n",
ah->arp_spa[0],
ah->arp_spa[1],
ah->arp_spa[2],
ah->arp_spa[3]
);
printf("TARGET MAC address: %02X:%02X:%02X: %02X:%02X:%02X\ n",
ah->arp_dha[0],
ah->arp_dha[1],
ah->arp_dha[2],
ah->arp_dha[3],
ah->arp_dha[4],
ah->arp_dha[5]
);
printf("TARGET IP address: %02d:%02d:%02d: %02d\n",
ah->arp_dpa[0],
ah->arp_dpa[1],
ah->arp_dpa[2],
ah->arp_dpa[3]
);
printf("H/D TYPE : %x PROTO TYPE : %x \n",ah->arp_hd,ah->arp_pr);
printf("H/D leng : %x PROTO leng : %x \n",ah->arp_hdl,ah->arp_prl);
printf("OPERATI ON : %x \n", ah->arp_op);

sent = sendto(s, buffer, BUF_SIZE, 0, (struct
sockaddr*)&sock et_address, sizeof(socket_a ddress));
if (sent == -1)
{
perror("sendto( ):");
exit(1);
}

answered_packet s++;
}

total_packets++ ;

}
}
void sigint(int signum) {
/*Clean up.......*/

struct ifreq ifr;

if (s == -1)
return;

strncpy(ifr.ifr _name, DEVICE, IFNAMSIZ);
ioctl(s, SIOCGIFFLAGS, &ifr);
ifr.ifr_flags &= ~IFF_PROMISC;
ioctl(s, SIOCSIFFLAGS, &ifr);
close(s);

free(buffer);

printf("Server terminating.... \n");

printf("Totally received: %d packets\n", total_packets);
printf("Answere d %d packets\n", answered_packet s);
exit(0);
}

Dec 15 '07 #1
1 12120
An********@gmai l.com wrote, On 15/12/07 07:48:
Hi all~
I've written a network program in C.
C does not have networking support, you have written a program in C +
extra libraries.
This program catputures ARP request packet and reply wrong ARP packet.
Hi. I've written a small program to learn to write in C. But
unfortunately the output is all jumbled up and not nice
Caputuring is ok, but couldn't send ARP packet. What's wrong?

( using 2PC, this program run in Linux and moitoring other PC,Window
XP)
(generator packet prgram is BillSniff, monitoring program is
WireShark)
Since you mention Linux, try compiling the program using gcc with the
options "-std=c99 -pedantic -Wall -Wextra". The compiler will tell you
about lots of problems than.
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>

#include <asm/types.h>
All the above headers are not standard C and so not topical here.
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
unistd.c is also not standard C and so not topical here.
#include <signal.h>

#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
The above three headers are Linux specific and so not topical here and
possibly not topical in comp.programmer .unix where the earlier headers
would have been topical. However, there are plenty of Linux groups.
#define BUF_SIZE 42
#define DEVICE "eth0"
#define ETH_P_NULL 0x0
#define ETH_MAC_LEN ETH_ALEN
#define ETH_ARP 0x0806

int s = 0; /*Socketdescript or*/
void* buffer = NULL;
long total_packets = 0;
long answered_packet s = 0;
Global variable. Yuck. Horrible. Redesign your program so you do to
avoid them since as you start working on larger projects they make
things harder and messier.

Also void* is generally not a good type for a buffer, unsigned char*
would be more sensible if it is binary data.
void sigint(int signum);

struct __attribute__(( packed)) arp_header
__attribute__ is gcc specific (well, Intel's compiler that aims to be
gcc compatible will accept it as well).
{
unsigned short arp_hd;
unsigned short arp_pr;
unsigned char arp_hdl;
unsigned char arp_prl;
unsigned short arp_op;
unsigned char arp_sha[6];
unsigned char arp_spa[4];
unsigned char arp_dha[6];
unsigned char arp_dpa[4];
};
int main(void) {
buffer = (void*)malloc(B UF_SIZE); /*Buffer for Ethernet Frame*/
Don't cast the return value of malloc. It is not required and in general
casts should be avoided unless you *really* know *exactly* why they are
needed, since normally when casts are added they are not the correct
solution to the problem.
unsigned char* etherhead = buffer; /*Pointer to Ethenet Header*/
struct ethhdr *eh = (struct ethhdr *)etherhead; /*Another pointer to
ethernet header*/
unsigned char* arphead = buffer + 14;
Standard C does not allow arithmetic on void* pointers, one reason for
using unsigned char* as the type for buffer.

<snip>
printf("Totally received: %d packets\n", total_packets);
printf("Answere d %d packets\n", answered_packet s);
long and int are not the same type, %d is for int not long.
exit(0);
}
Most of the above problems, and a few more besides, where shown by using
the compiler options I have suggested. If you are nice to it your
compiler can be a very good friend and help you a lot.
--
Flash Gordon
Dec 15 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2439
by: grzybek | last post by:
Hi, I'm doing network program which send TCP packet. I have problem to send such a packet. I create socket: s = WSASocket(AF_INET, SOCK_RAW, IPPROTO_RAW , NULL, 0,0); then: ret = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&bOpt, sizeof(bOpt));
3
2555
by: Simon | last post by:
Hi everyone, I'd quite like to create a small application that could listen on a port and take a copy of any communications going in or out. The end goal is to be able to offer statistics and stuff regarding the packets, and also to join them up and allow for easy reading of the packets and headers etc. The question is, how is it done?
3
3652
by: nexus024 | last post by:
I am trying to write a program that will continuously sniff eth0 for a specific UDP packet thats being sent to a specific destination IP, alter the data of the packet, and finally transmit it to the destination. My script compiles fine and runs fine until it finds the specific packet and tries to alter the payload of the data. Hopefully someone can give me some insight on why it might be doing this... #!/usr/bin/perl -w # # Custom script:...
1
3378
by: sangith | last post by:
Hi, I tried the packet capture module program. I did a file transfer using ftp from this host to another server. But when I ran the program, it was just hanging off and it did not print the src ip, dst ip, src port, dst port. Should I run this program as a Daemon? If so, how do I do that? I would appreciate your response.
3
3656
by: haribuvanesh | last post by:
Im working with a packet sniffer program and i get following exception on compilation. Plz help me out.... Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method loopPacket(int, PacketReceiver) in the type JpcapCaptor is not applicable for the arguments (int, JpcapTip)
1
20609
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but I don't see the error): "System.IO.IOException: Unable to read data from the transport connection:A blocking operation was interrupted by a call to WSACancelBlockingCall"
8
1736
by: mohi | last post by:
hello everyone, i have a problem regarding networking apis of unix link systems ....i don know this is the best place this request but i cudnt get one .(so also suggest where to ask such questions) the problem is that after finally connecting to the server when i send a get request the server returns bad request error ...i use something like:
3
6096
by: Sidra Nisar | last post by:
Hello……I am working on voice transmission control through linux as my project in high school……I need to know if the comments I have made on the following program are correct…. Object of the perl program is to build a network analyzer that captures local area network (LAN) traffic destined for the local domain name system (DNS) server and logs information on which IP addresses request which IP name resolutions. <1 #!/usr/bin/perl -w 2...
1
2143
by: kardon33 | last post by:
Hello Bytes, I am writing a network program in c++ on Linux Debian x86. I am using tcp sockets to communicate with other processors all very standard stuff. However Im stuck at one hump, when I try to call send() on a socket where the other machine loss power of crashed. Send crashes my program, no return of -1 just crash with no output. My Send Command:
0
8435
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.