File: arp_lookup_linux.c

package info (click to toggle)
libnet-arp-perl 1.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 192 kB
  • sloc: ansic: 314; perl: 43; makefile: 3
file content (106 lines) | stat: -rwxr-xr-x 3,166 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Perl ARP Extension
Lookup the MAC address of an ip address
Linux code

Programmed by Bastian Ballmann and Alexander Mueller
Last update: 20.09.2006

This program is free software; you can redistribute
it and/or modify it under the terms of the
GNU General Public License version 2 as published
by the Free Software Foundation.

This program is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if_arp.h>

/*
 * Search for a hardware address linked to an IP address on a device
 *
 * @device:  network interface name we are going to query
 * @ip:      ip address (IPv4 numbers-and-dots notation) whose hardware address
 *           is going to be looked for
 * @hw_addr: buffer containing the hardware mac_address
 *
 * \returns 0 if a hardware address has been found. @mac is set accordingly as
 *            a null terminated string.
 *          1 if an error occured
 */
int
arp_lookup_linux (
        const char *device, const char *ip, char *hw_addr)
{
    int                 s;
    unsigned char       err;
    struct in_addr      ipaddr;
    struct arpreq       areq;
    struct sockaddr_in *sin;

    err = 1;

    /* A device name must be a null terminated string whose length is less
     * than 16 bytes */
    if ( !strlen(device) || (strlen(device) >= 16) )
        fprintf(stderr, "No valid device name found.\n");

    /* Is there a buffer allocated to store the hardware address? */
    else if (hw_addr == NULL)
        fprintf(stderr, "No memory allocated to store the hardware address.\n");

    /* Make sure the ip address is valid */
    else if ( !strlen(ip) || (inet_aton(ip, &ipaddr) == 0) )
        fprintf(stderr, "Invalid ip address.\n");

    /* Create the socket */
    else if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
        perror("Socket");

    else {

        /* Set up the protocol address */
        memset(&areq, 0, sizeof(areq));
        sin = (struct sockaddr_in *) &areq.arp_pa;
        sin->sin_family = AF_INET;
        sin->sin_addr = ipaddr;

        /* Set up the hardware address */
        sin = (struct sockaddr_in *) &areq.arp_ha;
        sin->sin_family = ARPHRD_ETHER;
        strcpy(areq.arp_dev, device);

        /* Carry out the request */
        if (ioctl(s, SIOCGARP, &areq) == -1)
            perror("SIOCGARP");

        else {
            sprintf(hw_addr, "%02x:%02x:%02x:%02x:%02x:%02x",
                        areq.arp_ha.sa_data[0] & 0xFF,
                        areq.arp_ha.sa_data[1] & 0xFF,
                        areq.arp_ha.sa_data[2] & 0xFF,
                        areq.arp_ha.sa_data[3] & 0xFF,
                        areq.arp_ha.sa_data[4] & 0xFF,
                        areq.arp_ha.sa_data[5] & 0xFF);
            err = 0;
        }

	/* Close the current socket */
	close(s);
    }

    return err;
}