[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: (how to fetch mac address) arp

> dedicated machine which is alloted him/her. so i have made my authentication
> based on IP. Now smart guy changes their IP address to use other person's
> account. So now i want to restrict them at their mac address.

ok i get the point... since you already have the IP address, all you
want to do is to get the MAC address for it... i suggest that you do the
following - at start maintain a database of all IPs and their respective
MAC addressess... next whenever a request for login comes from a
particular machine, you use a system call to map the IP to its MAC
address... and then cross-check this information from the database
you've maintained... (by database i mean a simple text file...)

for the IP->ARP mapping you could use the following code... (i've
borrowed it from Steven's Unix Net Prog vol 1.. refer to that book for
more details...) note: unp.h is the author's own header - it contains
declarations for all system header files to be included and some other

source code follows...

#include        "unp.h"
#include        <net/if_arp.h>

main(int argc, char **argv)
        int                                     family, sockfd;
        char                            str[INET6_ADDRSTRLEN];
        char                            **pptr;
        unsigned char           *ptr;
        struct arpreq           arpreq;
        struct sockaddr_in      *sin;

        pptr = my_addrs(&family);
        for ( ; *pptr != NULL; pptr++) {
                printf("%s: ", Inet_ntop(family, *pptr, str,
                switch (family) {
                case AF_INET:
                        sockfd = Socket(AF_INET, SOCK_DGRAM, 0);

                        sin = (struct sockaddr_in *) &arpreq.arp_pa;
                        bzero(sin, sizeof(struct sockaddr_in));
                        sin->sin_family = AF_INET;
                        memcpy(&sin->sin_addr, *pptr, sizeof(struct

                        Ioctl(sockfd, SIOCGARP, &arpreq);

                        ptr = &arpreq.arp_ha.sa_data[0];
                        printf("%x:%x:%x:%x:%x:%x\n", *ptr, *(ptr+1),
                                   *(ptr+2), *(ptr+3), *(ptr+4),

                        err_quit("unsupported address family: %d",

hope this helps....

Abhas Abhinav
DeepRoot Linux
Ahmedabad, INDIA.