diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2004-04-14 04:44:23 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2004-04-14 04:44:23 +0000 |
commit | 7772bccde661f81296bcb7528f0f28e63a2ae386 (patch) | |
tree | 2b5db0f915779e9b8c0d0a87d63d9650a35227e5 /usr.sbin/dhcpd/icmp.c | |
parent | a2321610c13907780f6dc4124b0b70b048bb6e13 (diff) |
ansi
Diffstat (limited to 'usr.sbin/dhcpd/icmp.c')
-rw-r--r-- | usr.sbin/dhcpd/icmp.c | 98 |
1 files changed, 47 insertions, 51 deletions
diff --git a/usr.sbin/dhcpd/icmp.c b/usr.sbin/dhcpd/icmp.c index 882f14638b6..d9bc592a136 100644 --- a/usr.sbin/dhcpd/icmp.c +++ b/usr.sbin/dhcpd/icmp.c @@ -1,7 +1,8 @@ -/* icmp.c - - ICMP Protocol engine - for sending out pings and receiving - responses. */ +/* + * icmp.c + * + * ICMP Protocol engine - for sending out pings and receiving responses. + */ /* * Copyright (c) 1997, 1998 The Internet Software Consortium. @@ -51,119 +52,114 @@ static int icmp_protocol_fd; /* Initialize the ICMP protocol. */ -void icmp_startup (routep, handler) - int routep; - void (*handler)(struct iaddr, u_int8_t *, int); +void +icmp_startup(int routep, void (*handler) (struct iaddr, u_int8_t *, int)) { struct protoent *proto; - int protocol = 1; - int state; + int protocol = 1, state; /* Only initialize icmp once. */ if (icmp_protocol_initialized) - error ("attempted to reinitialize icmp protocol"); + error("attempted to reinitialize icmp protocol"); icmp_protocol_initialized = 1; /* Get the protocol number (should be 1). */ - proto = getprotobyname ("icmp"); + proto = getprotobyname("icmp"); if (proto) - protocol = proto -> p_proto; + protocol = proto->p_proto; /* Get a raw socket for the ICMP protocol. */ - icmp_protocol_fd = socket (AF_INET, SOCK_RAW, protocol); + icmp_protocol_fd = socket(AF_INET, SOCK_RAW, protocol); if (icmp_protocol_fd < 0) - error ("unable to create icmp socket: %m"); + error("unable to create icmp socket: %m"); /* Make sure it does routing... */ state = 0; - if (setsockopt (icmp_protocol_fd, SOL_SOCKET, SO_DONTROUTE, - (char *)&state, sizeof state) < 0) - error ("Unable to disable SO_DONTROUTE on ICMP socket: %m"); + if (setsockopt(icmp_protocol_fd, SOL_SOCKET, SO_DONTROUTE, + (char *) &state, sizeof state) < 0) + error("Unable to disable SO_DONTROUTE on ICMP socket: %m"); - add_protocol ("icmp", icmp_protocol_fd, icmp_echoreply, - (void *)handler); + add_protocol("icmp", icmp_protocol_fd, icmp_echoreply, + (void *) handler); } -int icmp_echorequest (addr) - struct iaddr *addr; +int +icmp_echorequest(struct iaddr *addr) { struct sockaddr_in to; struct icmp icmp; int status; if (!icmp_protocol_initialized) - error ("attempt to use ICMP protocol before initialization."); + error("attempt to use ICMP protocol before initialization."); memset(&to, 0, sizeof to); to.sin_len = sizeof to; to.sin_family = AF_INET; - to.sin_port = 0; /* unused. */ - memcpy (&to.sin_addr, addr -> iabuf, sizeof to.sin_addr); /* XXX */ + to.sin_port = 0; /* unused. */ + memcpy(&to.sin_addr, addr->iabuf, sizeof to.sin_addr); /* XXX */ icmp.icmp_type = ICMP_ECHO; icmp.icmp_code = 0; icmp.icmp_cksum = 0; icmp.icmp_seq = 0; #ifdef PTRSIZE_64BIT - icmp.icmp_id = (((u_int32_t)(u_int64_t)addr) ^ - (u_int32_t)(((u_int64_t)addr) >> 32)); + icmp.icmp_id = (((u_int32_t) (u_int64_t) addr) ^ + (u_int32_t) (((u_int64_t) addr) >> 32)); #else - icmp.icmp_id = (u_int32_t)addr; + icmp.icmp_id = (u_int32_t) addr; #endif - icmp.icmp_cksum = wrapsum (checksum ((unsigned char *)&icmp, - sizeof icmp, 0)); + icmp.icmp_cksum = wrapsum(checksum((unsigned char *) &icmp, + sizeof icmp, 0)); /* Send the ICMP packet... */ - status = sendto (icmp_protocol_fd, (char *)&icmp, sizeof icmp, 0, - (struct sockaddr *)&to, sizeof to); + status = sendto(icmp_protocol_fd, (char *) &icmp, sizeof icmp, 0, + (struct sockaddr *) & to, sizeof to); if (status < 0) - warn ("icmp_echorequest %s: %m", inet_ntoa(to.sin_addr)); + warn("icmp_echorequest %s: %m", inet_ntoa(to.sin_addr)); if (status != sizeof icmp) return 0; return 1; } -void icmp_echoreply (protocol) - struct protocol *protocol; +void +icmp_echoreply(struct protocol *protocol) { - struct icmp *icfrom; + void (*handler)(struct iaddr, u_int8_t *, int); struct sockaddr_in from; - u_int8_t icbuf [1500]; + u_int8_t icbuf[1500]; + struct icmp *icfrom; int status, len; socklen_t salen; struct iaddr ia; - void (*handler)(struct iaddr, u_int8_t *, int); salen = sizeof from; - status = recvfrom (protocol -> fd, (char *)icbuf, sizeof icbuf, 0, - (struct sockaddr *)&from, &salen); + status = recvfrom(protocol->fd, (char *)icbuf, sizeof icbuf, 0, + (struct sockaddr *)&from, &salen); if (status < 0) { - warn ("icmp_echoreply: %m"); + warn("icmp_echoreply: %m"); return; } /* Probably not for us. */ - if (status < (sizeof (struct ip)) + (sizeof *icfrom)) { + if (status < (sizeof(struct ip)) + (sizeof *icfrom)) return; - } - len = status - sizeof (struct ip); - icfrom = (struct icmp *)(icbuf + sizeof (struct ip)); + len = status - sizeof(struct ip); + icfrom = (struct icmp *) (icbuf + sizeof(struct ip)); /* Silently discard ICMP packets that aren't echoreplies. */ - if (icfrom -> icmp_type != ICMP_ECHOREPLY) { + if (icfrom->icmp_type != ICMP_ECHOREPLY) return; - } /* If we were given a second-stage handler, call it. */ - if (protocol -> local) { + if (protocol->local) { handler = ((void (*)(struct iaddr, u_int8_t *, int)) - protocol -> local); - memcpy (ia.iabuf, &from.sin_addr, sizeof from.sin_addr); + protocol->local); + memcpy(ia.iabuf, &from.sin_addr, sizeof from.sin_addr); ia.len = sizeof from.sin_addr; - - (*handler) (ia, icbuf, len); + (*handler)(ia, icbuf, len); } } |