473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Can anybody correct this code?

This is a C program compiled on gcc compiler that tries to set a new
entry in the arp cache,get an entry and also delete an entry from it.I
have run this program but in the set function Set_Entry function a call
to ether_aton(a, n) is made but the value of sa_data is not being
updated.This is why I think tht i am getting the error of invalid
argument in ioctl func..Plzz help me out.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <netinet/if_ether.h>
#include <sys/ioctl.h>

#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#endif
int main()
{
int i=Set_Entry();

return 0;
}
/*
void Del_Entry()
{
int sd;

struct arpreq arpreq;
struct sockaddr_in *sin;
struct in_addr ina;
char ip[] = "172.26.29.98";
unsigned char hw_addr[]="0:a:f4:36:12:e1";

int rc;

sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd < 0)
{
perror("socket() error\n");
exit(1);
}

sin = (struct sockaddr_in *) &arpreq.arp_pa;
memset(sin, 0, sizeof(struct sockaddr_in));
sin->sin_family = AF_INET;
ina.s_addr = inet_addr(ip);
memcpy(&sin->sin_addr, (char *)&ina, sizeof(struct in_addr));

strcpy(arpreq.arp_dev, "eth0");
/*
sin_h = (struct sockaddr_in *) &arpreq.arp_ha;
memset(sin_h, 0, sizeof(struct sockaddr_in));
sin_h->sin_family =AF_INET;
ina_h.s_addr = inet_addr(ip);
memcpy(&sin->sin_addr, (char *)&ina, sizeof(struct in_addr));*/
//arpreq.arp_flags=ATF_PERM;

//arpreq.arp_ha.sa_family=AF_INET;

/*rc = ioctl(sd, SIOCSARP, &arpreq);

if (rc < 0)
{
perror("Entry not available in cache...\n");
}
else
{
printf("\nentry has been successfully retreiveed");
hw_addr = (unsigned char *) arpreq.arp_ha.sa_data;
printf("HWAddr found : %x:%x:%x:%x:%x:%x\n", hw_addr[0],
hw_addr[1], hw_addr[2], hw_addr[3], hw_addr[4], hw_addr[5]);
}

}

*/


int Set_Entry()
{
int sd;
struct arpreq arpreq;
struct sockaddr_in *sin;
struct in_addr ina;
char ip[] = "172.26.28.77";
char eaddr[]="0:14:22:42:48:e6";//00-14-22-42-48-E6
int rc;
u_char *ea;
sd = socket(AF_INET,SOCK_DGRAM,0);
if (sd < 0)
{
perror("socket() error\n");
exit(1);
}
sin = (struct sockaddr_in *) &arpreq.arp_pa;
memset(sin, 0, sizeof(struct sockaddr_in));
sin->sin_family=AF_INET;
ina.s_addr = inet_addr(ip);
memcpy(&sin->sin_addr, (char *)&ina, sizeof(struct in_addr));
ea =(char*)arpreq.arp_ha.sa_data;
if(ether_aton(eaddr, ea))
{
printf("\nreturning 1");
return (1);
}
printf("ea = %s",ea);

arpreq.arp_flags=ATF_PERM;
strcpy(arpreq.arp_dev, "eth0");

printf("sa_data = %s",arpreq.arp_ha.sa_data);

rc = ioctl(sd, SIOCSARP, &arpreq);
if (rc < 0)
{
perror("Entry not set...\n");
}
else
{
printf("\nentry has been successfully set");
}
return 0;
}

int Get_Entry()
{
int sd;

struct arpreq arpreq;
struct sockaddr_in *sin;
struct in_addr ina;
char ip[] = "172.26.28.77";
unsigned char *hw_addr;

int rc;

sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd < 0)
{
perror("socket() error\n");
exit(1);
}

/* Try to find an entry in arp cache for the ip address specified */

printf("Find arp entry for IP : %s\n", ip);

sin = (struct sockaddr_in *) &arpreq.arp_pa;
memset(sin, 0, sizeof(struct sockaddr_in));
sin->sin_family = AF_INET;
ina.s_addr = inet_addr(ip);
memcpy(&sin->sin_addr, (char *)&ina, sizeof(struct in_addr));

strcpy(arpreq.arp_dev, "eth0");
arpreq.arp_ha.sa_family = AF_UNSPEC;

rc = ioctl(sd, SIOCGARP, &arpreq);

if (rc < 0)
{
perror("Entry not available in cache...\n");
}
else
{
printf("\nentry has been successfully retreived");
hw_addr = (unsigned char *) arpreq.arp_ha.sa_data;
printf("HWAddr found : %x:%x:%x:%x:%x:%x\n", hw_addr[0], hw_addr[1],
hw_addr[2], hw_addr[3], hw_addr[4], hw_addr[5]);
}

return 0;
}

ether_aton(a, n)
char *a;
char *n;
{
int i, o[6];

i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
&o[3], &o[4], &o[5]);

printf("\n i=%d\n",i);

printf("%x %x %x %x %x %x",o[0],o[1],o[2],o[3],o[4],o[5]);

if (i != 6) {
perror("arp: invalid Ethernet address");
return (1);
}
for (i=0; i<6; i++)
n[i]=o[i];
printf("\n");
for(i=0;i<6;i++)
printf("%x ",n[i]);
printf("\n");
return (0);
}

Apr 24 '06 #1
1 5655
jeniffer wrote:
This is a C program compiled on gcc compiler that tries to set a new
entry in the arp cache,get an entry and also delete an entry from it.I
have run this program but in the set function Set_Entry function a call
to ether_aton(a, n) is made but the value of sa_data is not being
updated.This is why I think tht i am getting the error of invalid
argument in ioctl func..Plzz help me out.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <netinet/if_ether.h>
#include <sys/ioctl.h>

#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#endif
[snip]


That's enough to determine that you aren't using standard C. You need to
post in comp.unix.programmer or possibly a networking group.
Apr 24 '06 #2

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

Similar topics

11
by: Don Bruder | last post by:
Got a stumper here. I imagine that for someone experienced in C++, this is too pathetic for words. For a rookie, using this project as a sort of "midterm exam" in his self-taught "how to program in...
5
by: PeteCresswell | last post by:
In another thread, somebody ventured the idea that this was possible under A2k's ADP but they did not sound sure of it. Anybody done it? My agenda is to use DAO for work tables bound to forms.
1
by: ~~~ .NET Ed ~~~ | last post by:
I tried to create an ASP.NET web project on my machine and this is what I did: System: WinXP Professional SP1 FS: NTFS Visual Studio .NET 2003 Framework 1.1 IIS Installed 1. I (Developer)...
5
by: clintonG | last post by:
Neither MSDN code examples nor will function. Has anybody figured out how to use the 2.0 classes, methods and properties to dynamically create HTML in the HTML <head> element? I've burned...
4
by: Henry | last post by:
Does anybody have a real-world sample of buiding a treeview control using data from database tables? All the sample code I have found either builds the treeview manually or uses a file directory...
1
by: liav.ezer | last post by:
This is the error: Microsoft OLE DB Provider for ODBC Drivers (0x80004005) No database selected given after trying to browse to my login.asp. my DB is mysql & i have a successful ODBC...
2
by: rocksoft | last post by:
Hi I am working in asp.net with C# web application, I have used visual studio 2003, i have used dropdownlist in my application to populate the data from mysql database, i have got problem while...
15
by: Konstantin Andreev | last post by:
I'm almost sure I've found bad bug, but for a while I can't neither confirm nor reject this. If anybody could make an independent test on it's own system, I'd appreciate it very much. The...
11
by: Gabriel | last post by:
Hi anybody intested in developing JSLibrary by yourslef??The project "Knut" has risen up,intend to building a new JSLibrary,more detail visit http://groups.google.com/group/knutDN ,we hope you to...
6
by: raylopez99 | last post by:
Anybody use Strong Name Signing? I think this is used by default for Resource files, which is one reason perhaps I can't get my resource files to work (somehow the public key is messed up, perhaps...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.