icmp.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1993-2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <fcntl.h>
#include <errno.h>
#include <syslog.h>
#include <string.h>
#include <time.h>
#include <thread.h>
#include <netinet/in_systm.h>
#include "dhcpd.h"
#include "per_dnet.h"
#include "interfaces.h"
#include <v4_sum_impl.h>
#include <locale.h>
/*
* An implementation of ICMP ECHO for use in detecting addresses already
* in use. Address argument expected in network order. Result is set to
* B_TRUE if a ICMP ECHO reply is received, B_FALSE if not. Returns 0 if
* no errors were encountered, nonzero otherwise.
*
* NOTES: Not interface specific. We use our routing tables to route the
* messages correctly, and collect responses. This may mean that we
* receive an ICMP ECHO reply thru an interface the daemon has not been
* directed to watch. However, I believe that *ANY* echo reply means
* trouble, regardless of the route taken!
*
* 'cip' is expected in network order.
*/
int
{
icmp_identifier, error = 0;
char ntoab[INET_ADDRSTRLEN];
"Error opening raw socket for ICMP (ping %s).\n", ntoab);
return (error);
}
"Error setting ICMP socket to no delay. (ping %s)\n",
ntoab);
(void) close(s);
return (error);
}
/*
* We make icmp_tries attempts to contact the target. We
* wait the same length of time for a response in both cases.
*/
for (i = 0; i < icmp_tries; i++) {
icp->icmp_cksum = 0;
/* Deliver our ECHO. */
"Error sending ICMP message. (ping %s).\n",
ntoab);
(void) close(s);
return (error);
}
/* Collect replies. */
recv_intrvl = gethrtime() +
while (gethrtime() < recv_intrvl) {
/* EINTR is masked - must be serious */
ntoab);
(void) close(s);
return (error);
}
continue; /* no data, timeout */
}
&fromlen)) < 0) {
error = 0;
continue;
}
/* EINTR is masked - must be serious */
"recvfrom: ICMP reply for %s.\n",
ntoab);
(void) close(s);
return (error);
}
continue; /* Not from the IP of interest */
/*
* We know we got an ICMP message of some type from
* the IP of interest. Be conservative and
* consider it in use. The following logic is just
* for identifying problems in the response.
*/
if (!debug)
break;
/* bogus IP header */
"received from host %s: len %d != %d\n",
break;
}
"from host %s is too small.\n", ntoab);
break;
}
"ICMP echo reply. (ping %s)\n", ntoab);
}
"Unexpected ICMP type %d from %s.\n",
}
"ICMP message id mismatch (from %s).\n",
ntoab);
}
}
break;
}
}
(void) close(s);
return (error);
}