summaryrefslogtreecommitdiffstats
path: root/arp.c
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2008-03-20 16:47:51 +0000
committerRoy Marples <roy@marples.name>2008-03-20 16:47:51 +0000
commit8d212424765794740d61d4b4fe5db3f2f6800eb9 (patch)
tree5ec4ff1d3eed924448276135fdd2d11504cb78c4 /arp.c
parent74befdac5d0d3c0803cd15c3db85c3ba81064bc8 (diff)
downloaddhcpcd-8d212424765794740d61d4b4fe5db3f2f6800eb9.tar.xz
Change code style to match the BSDs in the hope the might adpot it instead of dhclient.
Diffstat (limited to 'arp.c')
-rw-r--r--arp.c198
1 files changed, 99 insertions, 99 deletions
diff --git a/arp.c b/arp.c
index 794850c7..b390fa3d 100644
--- a/arp.c
+++ b/arp.c
@@ -29,6 +29,7 @@
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
+
#include <netinet/in_systm.h>
#ifdef __linux__
#include <netinet/ether.h>
@@ -37,6 +38,7 @@
#include <net/if.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
+
#include <errno.h>
#include <poll.h>
#include <stdlib.h>
@@ -59,55 +61,57 @@
/* Linux does not seem to define these handy macros */
#ifndef ar_sha
-#define ar_sha(ap) (((caddr_t) ((ap) + 1)) + 0)
-#define ar_spa(ap) (((caddr_t) ((ap) + 1)) + (ap)->ar_hln)
-#define ar_tha(ap) (((caddr_t) ((ap) + 1)) + (ap)->ar_hln + (ap)->ar_pln)
-#define ar_tpa(ap) (((caddr_t) ((ap) + 1)) + 2 * (ap)->ar_hln + (ap)->ar_pln)
+#define ar_sha(ap) (((caddr_t)((ap) + 1)) + 0)
+#define ar_spa(ap) (((caddr_t)((ap) + 1)) + (ap)->ar_hln)
+#define ar_tha(ap) (((caddr_t)((ap) + 1)) + (ap)->ar_hln + (ap)->ar_pln)
+#define ar_tpa(ap) (((caddr_t)((ap) + 1)) + 2 * (ap)->ar_hln + (ap)->ar_pln)
#endif
#ifndef arphdr_len
-#define arphdr_len2(ar_hln, ar_pln) (sizeof (struct arphdr) + \
+#define arphdr_len2(ar_hln, ar_pln) (sizeof(struct arphdr) + \
2 * (ar_hln) + 2 * (ar_pln))
-#define arphdr_len(ap) (arphdr_len2 ((ap)->ar_hln, (ap)->ar_pln))
+#define arphdr_len(ap) (arphdr_len2((ap)->ar_hln, (ap)->ar_pln))
#endif
#ifdef ENABLE_ARP
-static int send_arp (const interface_t *iface, int op, struct in_addr sip,
- const unsigned char *taddr, struct in_addr tip)
+static int
+send_arp(const struct interface *iface, int op, struct in_addr sip,
+ const unsigned char *taddr, struct in_addr tip)
{
struct arphdr *arp;
- size_t arpsize = arphdr_len2 (iface->hwlen, sizeof (sip));
+ size_t arpsize = arphdr_len2(iface->hwlen, sizeof(sip));
caddr_t tha;
int retval;
- arp = xzalloc (arpsize);
- arp->ar_hrd = htons (iface->family);
- arp->ar_pro = htons (ETHERTYPE_IP);
+ arp = xzalloc(arpsize);
+ arp->ar_hrd = htons(iface->family);
+ arp->ar_pro = htons(ETHERTYPE_IP);
arp->ar_hln = iface->hwlen;
- arp->ar_pln = sizeof (sip);
- arp->ar_op = htons (op);
- memcpy (ar_sha (arp), iface->hwaddr, (size_t) arp->ar_hln);
- memcpy (ar_spa (arp), &sip, (size_t) arp->ar_pln);
+ arp->ar_pln = sizeof(sip);
+ arp->ar_op = htons(op);
+ memcpy(ar_sha(arp), iface->hwaddr, (size_t)arp->ar_hln);
+ memcpy(ar_spa(arp), &sip, (size_t)arp->ar_pln);
if (taddr) {
/* NetBSD can return NULL from ar_tha, which is probably wrong
* but we still need to deal with it */
- if (! (tha = ar_tha (arp))) {
- free (arp);
+ if (! (tha = ar_tha(arp))) {
+ free(arp);
errno = EINVAL;
- return (-1);
+ return -1;
}
- memcpy (tha, taddr, (size_t) arp->ar_hln);
+ memcpy(tha, taddr, (size_t)arp->ar_hln);
}
- memcpy (ar_tpa (arp), &tip, (size_t) arp->ar_pln);
+ memcpy(ar_tpa(arp), &tip, (size_t)arp->ar_pln);
- retval = send_packet (iface, ETHERTYPE_ARP,
- (unsigned char *) arp, arphdr_len (arp));
- free (arp);
- return (retval);
+ retval = send_packet(iface, ETHERTYPE_ARP,
+ (unsigned char *) arp, arphdr_len(arp));
+ free(arp);
+ return retval;
}
-int arp_claim (interface_t *iface, struct in_addr address)
+int
+arp_claim(struct interface *iface, struct in_addr address)
{
struct arphdr *reply = NULL;
long timeout = 0;
@@ -120,54 +124,59 @@ int arp_claim (interface_t *iface, struct in_addr address)
{ -1, POLLIN, 0 },
{ -1, POLLIN, 0 }
};
-
- if (! iface)
- return (-1);
-
- if (! iface->arpable) {
- logger (LOG_DEBUG, "interface `%s' is not ARPable", iface->name);
- return (0);
+ size_t bufpos = 0;
+ size_t buflen = iface->buffer_length;
+ int bytes;
+ int s = 0;
+ struct timeval stopat;
+ struct timeval now;
+ union {
+ unsigned char *c;
+ struct in_addr *a;
+ } rp;
+ union {
+ unsigned char *c;
+ struct ether_addr *a;
+ } rh;
+
+ if (!iface->arpable) {
+ logger(LOG_DEBUG, "interface `%s' is not ARPable", iface->name);
+ return 0;
}
- if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr)) &&
- ! IN_LINKLOCAL (ntohl (address.s_addr)))
- logger (LOG_INFO,
- "checking %s is available on attached networks",
- inet_ntoa (address));
+ if (!IN_LINKLOCAL(ntohl(iface->previous_address.s_addr)) &&
+ !IN_LINKLOCAL(ntohl(address.s_addr)))
+ logger(LOG_INFO,
+ "checking %s is available on attached networks",
+ inet_ntoa(address));
- if (! open_socket (iface, ETHERTYPE_ARP))
- return (-1);
+ if (!open_socket(iface, ETHERTYPE_ARP))
+ return -1;
- fds[0].fd = signal_fd ();
+ fds[0].fd = signal_fd();
fds[1].fd = iface->fd;
-
- memset (&null_address, 0, sizeof (null_address));
-
- buffer = xmalloc (iface->buffer_length);
- reply = xmalloc (iface->buffer_length);
+ memset(&null_address, 0, sizeof(null_address));
+ buffer = xmalloc(iface->buffer_length);
+ reply = xmalloc(iface->buffer_length);
for (;;) {
- size_t bufpos = 0;
- size_t buflen = iface->buffer_length;
- int bytes;
- int s = 0;
- struct timeval stopat;
- struct timeval now;
+ bufpos = 0;
+ buflen = iface->buffer_length;
+ s = 0;
/* Only poll if we have a timeout */
if (timeout > 0) {
- s = poll (fds, 2, timeout);
+ s = poll(fds, 2, timeout);
if (s == -1) {
if (errno == EINTR) {
- if (signal_exists (NULL) == -1) {
+ if (signal_exists(NULL) == -1) {
errno = 0;
continue;
} else
break;
}
- logger (LOG_ERR, "poll: `%s'",
- strerror (errno));
+ logger(LOG_ERR, "poll: `%s'", strerror(errno));
break;
}
}
@@ -177,11 +186,11 @@ int arp_claim (interface_t *iface, struct in_addr address)
if (nprobes < NPROBES) {
nprobes ++;
timeout = PROBE_INTERVAL;
- logger (LOG_DEBUG, "sending ARP probe #%d",
- nprobes);
- if (send_arp (iface, ARPOP_REQUEST,
- null_address, NULL,
- address) == -1)
+ logger(LOG_DEBUG, "sending ARP probe #%d",
+ nprobes);
+ if (send_arp(iface, ARPOP_REQUEST,
+ null_address, NULL,
+ address) == -1)
break;
/* IEEE1394 cannot set ARP target address
@@ -192,11 +201,11 @@ int arp_claim (interface_t *iface, struct in_addr address)
} else if (nclaims < NCLAIMS) {
nclaims ++;
timeout = CLAIM_INTERVAL;
- logger (LOG_DEBUG, "sending ARP claim #%d",
- nclaims);
- if (send_arp (iface, ARPOP_REQUEST,
- address, iface->hwaddr,
- address) == -1)
+ logger(LOG_DEBUG, "sending ARP claim #%d",
+ nclaims);
+ if (send_arp(iface, ARPOP_REQUEST,
+ address, iface->hwaddr,
+ address) == -1)
break;
} else {
/* No replies, so done */
@@ -205,7 +214,7 @@ int arp_claim (interface_t *iface, struct in_addr address)
}
/* Setup our stop time */
- if (get_time (&stopat) != 0)
+ if (get_time(&stopat) != 0)
break;
stopat.tv_usec += timeout;
@@ -213,48 +222,39 @@ int arp_claim (interface_t *iface, struct in_addr address)
}
/* We maybe ARP flooded, so check our time */
- if (get_time (&now) != 0)
+ if (get_time(&now) != 0)
break;
- if (timercmp (&now, &stopat, >)) {
+ if (timercmp(&now, &stopat, >)) {
timeout = 0;
continue;
}
- if (! fds[1].revents & POLLIN)
+ if (!(fds[1].revents & POLLIN))
continue;
- memset (buffer, 0, buflen);
+ memset(buffer, 0, buflen);
do {
- union {
- unsigned char *c;
- struct in_addr *a;
- } rp;
- union {
- unsigned char *c;
- struct ether_addr *a;
- } rh;
-
- memset (reply, 0, iface->buffer_length);
- if ((bytes = get_packet (iface, (unsigned char *) reply,
- buffer,
- &buflen, &bufpos)) == -1)
+ memset(reply, 0, iface->buffer_length);
+ if ((bytes = get_packet(iface, (unsigned char *) reply,
+ buffer,
+ &buflen, &bufpos)) == -1)
break;
/* Only these types are recognised */
- if (reply->ar_op != htons (ARPOP_REPLY))
+ if (reply->ar_op != htons(ARPOP_REPLY))
continue;
/* Protocol must be IP. */
- if (reply->ar_pro != htons (ETHERTYPE_IP))
+ if (reply->ar_pro != htons(ETHERTYPE_IP))
continue;
- if (reply->ar_pln != sizeof (address))
+ if (reply->ar_pln != sizeof(address))
continue;
- if ((unsigned) bytes < sizeof (reply) +
- 2 * (4 + reply->ar_hln))
+ if ((unsigned)bytes < sizeof(reply) +
+ 2 * (4 +reply->ar_hln))
continue;
- rp.c = (unsigned char *) ar_spa (reply);
- rh.c = (unsigned char *) ar_sha (reply);
+ rp.c = (unsigned char *)ar_spa(reply);
+ rh.c = (unsigned char *)ar_sha(reply);
/* Ensure the ARP reply is for the our address */
if (rp.a->s_addr != address.s_addr)
@@ -263,22 +263,22 @@ int arp_claim (interface_t *iface, struct in_addr address)
/* Some systems send a reply back from our hwaddress,
* which is wierd */
if (reply->ar_hln == iface->hwlen &&
- memcmp (rh.c, iface->hwaddr, iface->hwlen) == 0)
+ memcmp(rh.c, iface->hwaddr, iface->hwlen) == 0)
continue;
- logger (LOG_ERR, "ARPOP_REPLY received from %s (%s)",
- inet_ntoa (*rp.a),
- hwaddr_ntoa (rh.c, (size_t) reply->ar_hln));
+ logger(LOG_ERR, "ARPOP_REPLY received from %s (%s)",
+ inet_ntoa(*rp.a),
+ hwaddr_ntoa(rh.c, (size_t)reply->ar_hln));
retval = -1;
goto eexit;
} while (bufpos != 0);
}
eexit:
- close (iface->fd);
+ close(iface->fd);
iface->fd = -1;
- free (buffer);
- free (reply);
- return (retval);
+ free(buffer);
+ free(reply);
+ return retval;
}
#endif