Re: read entry from arp table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Giscard Wépiwé wrote:
> 
> Hallo everybody,
> i write a small program to read from the arp table. It
> functions but the output is corrupted.
> I would want to know why?
> 
> The output of my program:
> [root@heghta fleetnet]# ./arp
> 00 ffffffe0 2b 6f 1c 00
> 
> The output of the well-know routine arp:
> [root@heghta fleetnet]# arp -a
> pern.fokus.gmd.de (193.175.135.3) at 08:00:20:8F:7C:50
> [ether] on eth0
> buran.fokus.gmd.de (193.175.135.1) at
> 00:E0:2B:6F:1C:00 [ether] on eth0
> 
> The program:
> 
> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>
> #include <sys/socket.h>
> #include <linux/if_arp.h>
> #include <sys/ioctl.h>
> #include <netinet/in.h>
> #include <arpa/inet.h>
> 
> int main(int argc, char **argv)
> {
>    struct sockaddr_in gw_addr;
>    struct arpreq myreq;
>    int sockfd, i;
>    FILE *logfd;
> 
>    logfd = fopen("logfile", "w+");
>    memset(&gw_addr, '\0', sizeof(gw_addr));
>    gw_addr.sin_family = AF_INET;
>    gw_addr.sin_addr.s_addr =
> inet_addr("193.175.135.1");
> 
>    memset(&myreq, '\0', sizeof(myreq));
>    memcpy(&myreq.arp_pa, &gw_addr, sizeof(gw_addr));
>    memcpy(&myreq.arp_dev, "eth0",
> sizeof(myreq.arp_dev));
> 
>    if ((sockfd = socket(AF_INET,SOCK_DGRAM, 0)) < 0) {
>       perror("socket");
>       exit(-1);
>    }
> 
>    if (ioctl(sockfd,SIOCGARP,(char *)&myreq) < 0) {
>       perror("ioctl");
>       exit(-1);
>    }
> 
>    for ( i = 0; i < 6; i++)
>    fprintf(stdout,"%02x%s",myreq.arp_ha.sa_data[i], i
> == 5 ?"\n": " ");
> 
>    return 0;
> }
> 
> Please try it on your machine and tell me if it
> functions well.
> Thanks.
> 
> __________________________________________________________________
> Do You Yahoo!?
> Gesendet von Yahoo! Mail - http://mail.yahoo.de
> -
> : send the line "unsubscribe linux-net" in
> the body of a message to majordomo@vger.kernel.org

Hi,

What you are getting is correct (at least according to your code).

You should replace myreq.arp_ha.sa_data[i] with
(myreq.arp_ha.sa_data[i] & 0xff).

myreq.arp_ha.sa_data[i] is a char and is implicitely
converted to an int when passed to fprintf. This is
normal C behavior.

Regards
--
Joseph Bueno
NetClub/Trader.com
-
: send the line "unsubscribe linux-net" in
the body of a message to majordomo@vger.kernel.org


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux