Use a context structure to encapsulate lifetime context data
We'll want to be adding more local context in an upcoming change, so
prepare for that by moving daemon-lifetime contect to a single
structure.
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
diff --git a/inarp.c b/inarp.c
index 2ef7d41..f24fb4a 100644
--- a/inarp.c
+++ b/inarp.c
@@ -46,6 +46,13 @@
struct in_addr dest_ip;
} __attribute__((packed));
+struct inarp_ctx {
+ int socket;
+ const char *ifname;
+ int ifindex;
+ struct eth_addr local_mac;
+};
+
static int send_arp_packet(int fd,
int ifindex,
const struct eth_addr *src_mac,
@@ -179,40 +186,41 @@
int main(int argc, char **argv)
{
- static struct eth_addr local_mac;
- static struct in_addr local_ip;
struct arp_packet inarp_req;
- int fd, ret, ifindex;
- const char *ifname;
+ struct in_addr local_ip;
+ struct inarp_ctx inarp;
ssize_t len;
+ int ret;
if (argc < 2) {
usage(argv[0]);
return EXIT_FAILURE;
}
- ifname = argv[1];
+ memset(&inarp, 0, sizeof(inarp));
+ inarp.ifname = argv[1];
- if (strlen(ifname) > IFNAMSIZ)
- errx(EXIT_FAILURE, "Interface name '%s' is invalid", ifname);
+ if (strlen(inarp.ifname) > IFNAMSIZ)
+ errx(EXIT_FAILURE, "Interface name '%s' is invalid",
+ inarp.ifname);
-
- fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
- if (fd < 0)
+ inarp.socket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
+ if (inarp.socket < 0)
err(EXIT_FAILURE, "Error opening ARP socket");
- ret = get_ifindex(fd, ifname, &ifindex);
+ ret = get_ifindex(inarp.socket, inarp.ifname, &inarp.ifindex);
if (ret)
exit(EXIT_FAILURE);
- ret = get_local_hwaddr(fd, ifname, &local_mac);
+ ret = get_local_hwaddr(inarp.socket, inarp.ifname, &inarp.local_mac);
if (ret)
exit(EXIT_FAILURE);
- printf("%s MAC address: %s\n", ifname, eth_mac_to_str(&local_mac));
+ printf("%s MAC address: %s\n", inarp.ifname,
+ eth_mac_to_str(&inarp.local_mac));
while (1) {
- len = recvfrom(fd, &inarp_req, sizeof(inarp_req), 0,
+ len = recvfrom(inarp.socket, &inarp_req, sizeof(inarp_req), 0,
NULL, NULL);
if (len <= 0) {
if (errno == EINTR)
@@ -229,13 +237,13 @@
continue;
/* ... for us? */
- if (memcmp(&local_mac, inarp_req.eh.h_dest, ETH_ALEN))
+ if (memcmp(&inarp.local_mac, inarp_req.eh.h_dest, ETH_ALEN))
continue;
printf("src mac: %s\n", eth_mac_to_str(&inarp_req.src_mac));
printf("src ip: %s\n", inet_ntoa(inarp_req.src_ip));
- ret = get_local_ipaddr(fd, ifname, &local_ip);
+ ret = get_local_ipaddr(inarp.socket, inarp.ifname, &local_ip);
/* if we don't have a local IP address to send, just drop the
* request */
if (ret)
@@ -243,12 +251,12 @@
printf("local ip: %s\n", inet_ntoa(local_ip));
- send_arp_packet(fd, ifindex,
+ send_arp_packet(inarp.socket, inarp.ifindex,
&inarp_req.dest_mac,
&local_ip,
&inarp_req.src_mac,
&inarp_req.src_ip);
}
- close(fd);
+ close(inarp.socket);
return 0;
}