Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* vi: set sw=4 ts=4: */
- /*
- * Mostly stolen from: dhcpcd - DHCP client daemon
- * by Yoichi Hariguchi <yoichi@fore.com>
- *
- * Licensed under GPLv2, see file LICENSE in this source tree.
- */
- #include <netinet/if_ether.h>
- #include <net/if_arp.h>
- #include <stdint.h>
- #include <string.h>
- #include <sys/time.h>
- #include <poll.h>
- #include <netinet/in.h>
- #include <errno.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <linux/if.h>
- struct arpMsg {
- /* Ethernet header */
- uint8_t h_dest[6]; /* 00 destination ether addr */
- uint8_t h_source[6]; /* 06 source ether addr */
- uint16_t h_proto; /* 0c packet type ID field */
- /* ARP packet */
- uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */
- uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */
- uint8_t hlen; /* 12 hardware address length (must be 6) */
- uint8_t plen; /* 13 protocol address length (must be 4) */
- uint16_t operation; /* 14 ARP opcode */
- uint8_t sHaddr[6]; /* 16 sender's hardware address */
- uint8_t sInaddr[4]; /* 1c sender's IP address */
- uint8_t tHaddr[6]; /* 20 target's hardware address */
- uint8_t tInaddr[4]; /* 26 target's IP address */
- uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */
- } PACKED;
- enum {
- ARP_MSG_SIZE = 0x2a
- };
- const int const_int_1 = 1;
- /* Like strncpy but make sure the resulting string is always 0 terminated. */
- static
- char* safe_strncpy(char *dst, const char *src, size_t size)
- {
- if (!size) return dst;
- dst[--size] = '\0';
- return strncpy(dst, src, size);
- }
- static
- unsigned long long monotonic_ms(void)
- {
- struct timeval tv;
- gettimeofday (&tv, NULL);
- return tv.tv_sec * 1000ULL + tv.tv_usec/1000;
- }
- /* Wrapper which restarts poll on EINTR or ENOMEM.
- * On other errors does perror("poll") and returns.
- * Warning! May take longer than timeout_ms to return! */
- static
- int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout)
- {
- while (1) {
- int n = poll(ufds, nfds, timeout);
- if (n >= 0)
- return n;
- /* Make sure we inch towards completion */
- if (timeout > 0)
- timeout--;
- /* E.g. strace causes poll to return this */
- if (errno == EINTR)
- continue;
- /* Kernel is very low on memory. Retry. */
- /* I doubt many callers would handle this correctly! */
- if (errno == ENOMEM)
- continue;
- return n;
- }
- }
- static
- ssize_t safe_read(int fd, void *buf, size_t count)
- {
- ssize_t n;
- do {
- n = read(fd, buf, count);
- } while (n < 0 && errno == EINTR);
- return n;
- }
- static
- char* strncpy_IFNAMSIZ(char *dst, const char *src)
- {
- #ifndef IFNAMSIZ
- enum { IFNAMSIZ = 16 };
- #endif
- return strncpy(dst, src, IFNAMSIZ);
- }
- static
- int udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac)
- {
- /* char buffer instead of bona-fide struct avoids aliasing warning */
- char ifr_buf[sizeof(struct ifreq)];
- struct ifreq *const ifr = (void *)ifr_buf;
- int fd;
- struct sockaddr_in *our_ip;
- memset(ifr, 0, sizeof(*ifr));
- fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
- ifr->ifr_addr.sa_family = AF_INET;
- strncpy_IFNAMSIZ(ifr->ifr_name, interface);
- if (nip) {
- if (ioctl(fd, SIOCGIFADDR, ifr)) {
- close(fd);
- return -1;
- }
- our_ip = (struct sockaddr_in *) &ifr->ifr_addr;
- *nip = our_ip->sin_addr.s_addr;
- }
- if (ifindex) {
- if (ioctl(fd, SIOCGIFINDEX, ifr) != 0) {
- close(fd);
- return -1;
- }
- *ifindex = ifr->ifr_ifindex;
- }
- if (mac) {
- if (ioctl(fd, SIOCGIFHWADDR, ifr) != 0) {
- close(fd);
- return -1;
- }
- memcpy(mac, ifr->ifr_hwaddr.sa_data, 6);
- }
- close(fd);
- return 0;
- }
- /* Returns 1 if no reply received */
- int arpping(uint32_t test_nip,
- char *result_mac,
- const char *interface)
- {
- int timeout_ms;
- struct pollfd pfd[1];
- #define s (pfd[0].fd) /* socket */
- int rv = 1; /* "no reply received" yet */
- struct sockaddr addr; /* for interface name */
- struct arpMsg arp;
- uint32_t from_ip;
- uint8_t from_mac[6];
- udhcp_read_interface (interface, NULL, &from_ip, from_mac);
- s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
- if (s == -1) {
- //bb_perror_msg(bb_msg_can_not_create_raw_socket);
- return -1;
- }
- if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1)) == -1) {
- //bb_perror_msg("can't enable bcast on raw socket");
- goto ret;
- }
- /* send arp request */
- memset(&arp, 0, sizeof(arp));
- memset(arp.h_dest, 0xff, 6); /* MAC DA */
- memcpy(arp.h_source, from_mac, 6); /* MAC SA */
- arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
- arp.htype = htons(ARPHRD_ETHER); /* hardware type */
- arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
- arp.hlen = 6; /* hardware address length */
- arp.plen = 4; /* protocol address length */
- arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
- memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */
- memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */
- /* tHaddr is zero-filled */ /* target hardware address */
- memcpy(arp.tInaddr, &test_nip, sizeof(test_nip));/* target IP address */
- memset(&addr, 0, sizeof(addr));
- safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
- if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) {
- // TODO: error message? caller didn't expect us to fail,
- // just returning 1 "no reply received" misleads it.
- goto ret;
- }
- /* wait for arp reply, and check it */
- timeout_ms = 1000;
- do {
- typedef uint32_t aliased_uint32_t;
- int r;
- unsigned prevTime = monotonic_ms();
- pfd[0].events = POLLIN;
- r = safe_poll(pfd, 1, timeout_ms);
- if (r < 0)
- break;
- if (r) {
- r = safe_read(s, &arp, sizeof(arp));
- if (r < 0)
- break;
- if (r >= ARP_MSG_SIZE
- && arp.operation == htons(ARPOP_REPLY)
- /* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */
- /* && memcmp(arp.tHaddr, from_mac, 6) == 0 */
- && *(aliased_uint32_t*)arp.sInaddr == test_nip
- ) {
- /* if ARP source MAC matches safe_mac
- * (which is client's MAC), then it's not a conflict
- * (client simply already has this IP and replies to ARPs!)
- */
- sprintf(result_mac, "%02X:%02X:%02X:%02X:%02X:%02X",
- arp.sHaddr[0], arp.sHaddr[1], arp.sHaddr[2],
- arp.sHaddr[3], arp.sHaddr[4], arp.sHaddr[5]);
- break;
- }
- }
- timeout_ms -= (unsigned)monotonic_ms() - prevTime;
- } while (timeout_ms > 0);
- ret:
- close(s);
- return rv;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement