Annotation of /trunk/mkinitrd-magellan/busybox/networking/udhcp/arpping.c
Parent Directory | Revision Log
Revision 532 -
(hide annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 3379 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 3379 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * arpping.c | ||
4 | * | ||
5 | * Mostly stolen from: dhcpcd - DHCP client daemon | ||
6 | * by Yoichi Hariguchi <yoichi@fore.com> | ||
7 | */ | ||
8 | |||
9 | #include <netinet/if_ether.h> | ||
10 | #include <net/if_arp.h> | ||
11 | |||
12 | #include "common.h" | ||
13 | #include "dhcpd.h" | ||
14 | |||
15 | |||
16 | struct arpMsg { | ||
17 | /* Ethernet header */ | ||
18 | uint8_t h_dest[6]; /* destination ether addr */ | ||
19 | uint8_t h_source[6]; /* source ether addr */ | ||
20 | uint16_t h_proto; /* packet type ID field */ | ||
21 | |||
22 | /* ARP packet */ | ||
23 | uint16_t htype; /* hardware type (must be ARPHRD_ETHER) */ | ||
24 | uint16_t ptype; /* protocol type (must be ETH_P_IP) */ | ||
25 | uint8_t hlen; /* hardware address length (must be 6) */ | ||
26 | uint8_t plen; /* protocol address length (must be 4) */ | ||
27 | uint16_t operation; /* ARP opcode */ | ||
28 | uint8_t sHaddr[6]; /* sender's hardware address */ | ||
29 | uint8_t sInaddr[4]; /* sender's IP address */ | ||
30 | uint8_t tHaddr[6]; /* target's hardware address */ | ||
31 | uint8_t tInaddr[4]; /* target's IP address */ | ||
32 | uint8_t pad[18]; /* pad for min. Ethernet payload (60 bytes) */ | ||
33 | } ATTRIBUTE_PACKED; | ||
34 | |||
35 | /* args: yiaddr - what IP to ping | ||
36 | * ip - our ip | ||
37 | * mac - our arp address | ||
38 | * interface - interface to use | ||
39 | * retn: 1 addr free | ||
40 | * 0 addr used | ||
41 | * -1 error | ||
42 | */ | ||
43 | |||
44 | /* FIXME: match response against chaddr */ | ||
45 | int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface) | ||
46 | { | ||
47 | int timeout = 2; | ||
48 | int s; /* socket */ | ||
49 | int rv = 1; /* return value */ | ||
50 | struct sockaddr addr; /* for interface name */ | ||
51 | struct arpMsg arp; | ||
52 | fd_set fdset; | ||
53 | struct timeval tm; | ||
54 | time_t prevTime; | ||
55 | |||
56 | |||
57 | s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)); | ||
58 | if (s == -1) { | ||
59 | bb_perror_msg(bb_msg_can_not_create_raw_socket); | ||
60 | return -1; | ||
61 | } | ||
62 | |||
63 | if (setsockopt_broadcast(s) == -1) { | ||
64 | bb_perror_msg("cannot setsocketopt on raw socket"); | ||
65 | close(s); | ||
66 | return -1; | ||
67 | } | ||
68 | |||
69 | /* send arp request */ | ||
70 | memset(&arp, 0, sizeof(arp)); | ||
71 | memcpy(arp.h_dest, MAC_BCAST_ADDR, 6); /* MAC DA */ | ||
72 | memcpy(arp.h_source, mac, 6); /* MAC SA */ | ||
73 | arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */ | ||
74 | arp.htype = htons(ARPHRD_ETHER); /* hardware type */ | ||
75 | arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */ | ||
76 | arp.hlen = 6; /* hardware address length */ | ||
77 | arp.plen = 4; /* protocol address length */ | ||
78 | arp.operation = htons(ARPOP_REQUEST); /* ARP op code */ | ||
79 | memcpy(arp.sInaddr, &ip, sizeof(ip)); /* source IP address */ | ||
80 | memcpy(arp.sHaddr, mac, 6); /* source hardware address */ | ||
81 | memcpy(arp.tInaddr, &yiaddr, sizeof(yiaddr)); /* target IP address */ | ||
82 | |||
83 | memset(&addr, 0, sizeof(addr)); | ||
84 | strcpy(addr.sa_data, interface); | ||
85 | if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) | ||
86 | rv = 0; | ||
87 | |||
88 | /* wait arp reply, and check it */ | ||
89 | tm.tv_usec = 0; | ||
90 | prevTime = uptime(); | ||
91 | while (timeout > 0) { | ||
92 | FD_ZERO(&fdset); | ||
93 | FD_SET(s, &fdset); | ||
94 | tm.tv_sec = timeout; | ||
95 | if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) { | ||
96 | bb_perror_msg("error on ARPING request"); | ||
97 | if (errno != EINTR) rv = 0; | ||
98 | } else if (FD_ISSET(s, &fdset)) { | ||
99 | if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0; | ||
100 | if (arp.operation == htons(ARPOP_REPLY) && | ||
101 | memcmp(arp.tHaddr, mac, 6) == 0 && | ||
102 | *((uint32_t *) arp.sInaddr) == yiaddr) { | ||
103 | DEBUG("Valid arp reply received for this address"); | ||
104 | rv = 0; | ||
105 | break; | ||
106 | } | ||
107 | } | ||
108 | timeout -= uptime() - prevTime; | ||
109 | prevTime = uptime(); | ||
110 | } | ||
111 | close(s); | ||
112 | DEBUG("%salid arp replies for this address", rv ? "No v" : "V"); | ||
113 | return rv; | ||
114 | } |