Error reporting cleanup
Use warn/err where appropriate
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
diff --git a/inarp.c b/inarp.c
index ee99b92..66f331c 100644
--- a/inarp.c
+++ b/inarp.c
@@ -20,6 +20,7 @@
#include <errno.h>
#include <time.h>
#include <unistd.h>
+#include <err.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
@@ -54,10 +55,10 @@
unsigned char *dest_mac,
struct in_addr *dest_ip)
{
- int send_result = 0;
struct ethhdr *eh = ð_arp->eh;
struct arphdr *arp = ð_arp->arp;
struct sockaddr_ll socket_address;
+ int rc;
/* Prepare our link-layer address: raw packet interface,
* using the ifindex interface, receiving ARP packets
@@ -89,13 +90,13 @@
memcpy(ð_arp->dest_ip, dest_ip, sizeof(eth_arp->dest_ip));
/* send the packet */
- send_result = sendto(fd, eth_arp, ETH_ARP_FRAME_LEN, 0,
+ rc = sendto(fd, eth_arp, ETH_ARP_FRAME_LEN, 0,
(struct sockaddr *)&socket_address,
sizeof(socket_address));
- if (send_result == -1) {
- printf("sendto: [%s]\n", strerror(errno));
- }
- return send_result;
+ if (rc < 0)
+ warn("failure sending ARP response");
+
+ return rc;
}
static void show_mac_addr(const char *name, unsigned char *mac_addr)
@@ -130,29 +131,24 @@
ifname = argv[1];
- if (strlen(ifname) > IFNAMSIZ) {
- fprintf(stderr, "Interface name '%s' is invalid\n", ifname);
- return EXIT_FAILURE;
- }
+ if (strlen(ifname) > IFNAMSIZ)
+ errx(EXIT_FAILURE, "Interface name '%s' is invalid", ifname);
static unsigned char src_mac[6];
static struct in_addr src_ip;
int ifindex;
- if (((fd = socket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)))) == -1) {
- printf("socket: [%s]\n", strerror(errno));
- exit(-1);
- }
+ fd = socket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
+ if (fd < 0)
+ err(EXIT_FAILURE, "Error opening ARP socket");
/* Query local mac address */
memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer));
strcpy(ifreq_buffer.ifr_name, ifname);
ret = ioctl(fd, SIOCGIFHWADDR, &ifreq_buffer);
- if (ret == -1) {
- printf("ioctl2: [%s]\n", strerror(errno));
- close(fd);
- exit(-1);
- }
+ if (ret < 0)
+ err(EXIT_FAILURE, "Error querying local MAC address");
+
memcpy(src_mac, ifreq_buffer.ifr_hwaddr.sa_data, ETH_ALEN);
show_mac_addr(ifname, src_mac);
@@ -160,11 +156,9 @@
memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer));
strcpy(ifreq_buffer.ifr_name, ifname);
ret = ioctl(fd, SIOCGIFINDEX, &ifreq_buffer);
- if (ret == -1) {
- printf("ioctl4: [%s]\n", strerror(errno));
- close(fd);
- exit(-1);
- }
+ if (ret < 0)
+ err(EXIT_FAILURE, "Error querying interface %s", ifname);
+
ifindex = ifreq_buffer.ifr_ifindex;
/* length of the received frame */
@@ -186,7 +180,7 @@
memcpy(&src_ip, &ifreq_buffer.ifr_addr.sa_data[2],
sizeof(src_ip));
} else {
- printf("unknown address family [%d]!\n",
+ warnx("Unknown address family %d in request!",
ifreq_buffer.ifr_addr.sa_family);
sleep(1);
continue;
@@ -212,12 +206,10 @@
printf("src ip = %s\r\n",
inet_ntoa(inarp_req->src_ip));
int fd_1;
- if (((fd_1 =
- socket(AF_PACKET, SOCK_RAW, 0))) == -1) {
- printf("socket: [%s]\n",
- strerror(errno));
- exit(-1);
- }
+ fd_1 = socket(AF_PACKET, SOCK_RAW, 0);
+ if (fd_1 < 0)
+ err(EXIT_FAILURE,
+ "Error opening response socket");
send_result =
send_arp_packet(fd_1, ifindex, &inarp_resp,
ARPOP_InREPLY,
@@ -227,8 +219,7 @@
&inarp_req->src_ip);
close(fd_1);
if (send_result == -1) {
- printf("[Rsp] sendto: [%s]\n",
- strerror(errno));
+ warn("Error sending response");
sleep(1);
continue;
}