PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Noms de domaine > comp.protocols.tcp-ip > Can anybody correct this code that is used to get/set ARP entries and uses ioctl( )
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.protocols.tcp-ip TCP and IP network protocols.

Can anybody correct this code that is used to get/set ARP entries and uses ioctl( )

Réponse
 
LinkBack Outils de la discussion
Vieux 25/04/2006, 02h34   #1
jeniffer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Can anybody correct this code that is used to get/set ARP entries and uses ioctl( )

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 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);
}

  Réponse avec citation
Vieux 25/04/2006, 16h34   #2
George Wicks
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can anybody correct this code that is used to get/set ARP entries and uses ioctl( )

"jeniffer" <zenith.of.perfection@gmail.com> wrote in
news:1145928851.440863.7750@v46g2000cwv.googlegrou ps.com:

> 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 me out.
>


Not sure offhand, but you might need to change your socket call to
SOCK_RAW.

Also, you must have superuser priviledges to use ioctl for this.

One more thing: when you fill in the sa_data field, you're putting in
actual binary data, not ASCII equivalent data, so when you attempt to do
a printf of the data in ASCII string format, you'll probably just
garbage.
  Réponse avec citation
Vieux 25/04/2006, 16h37   #3
George Wicks
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can anybody correct this code that is used to get/set ARP entries and uses ioctl( )

"jeniffer" <zenith.of.perfection@gmail.com> wrote in
news:1145928851.440863.7750@v46g2000cwv.googlegrou ps.com:

> 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 me out.
>


One more thing: you might want to look for the arp.c source for linux -
your code sorta looks like it came from a bsd variant (but I could be
wrong).
  Réponse avec citation
Vieux 28/04/2006, 08h53   #4
jeniffer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can anybody correct this code that is used to get/set ARP entries and uses ioctl( )


George Wicks wrote:
> "jeniffer" <zenith.of.perfection@gmail.com> wrote in
> news:1145928851.440863.7750@v46g2000cwv.googlegrou ps.com:
>
> > 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 me out.
> >

>
> One more thing: you might want to look for the arp.c source for linux -
> your code sorta looks like it came from a bsd variant (but I could be
> wrong).



Thanks a lot for ur suggestions.
Plz tell me what is bsd .Cannot this code work on a linux machine? I
read the nettools implementation of arp.c in linux.I tried to cut short
this arp.c but i am not able to do this correctly.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h34.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14925 seconds with 12 queries