|
|
|
@@ -150,8 +150,7 @@ struct sockaddr_in to;
|
|
|
|
|
*/
|
|
|
|
|
int ping_send(struct in_addr addr)
|
|
|
|
|
{
|
|
|
|
|
int len;
|
|
|
|
|
int isock;
|
|
|
|
|
int len, sentlen;
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
struct icmp_filter f;
|
|
|
|
|
#else
|
|
|
|
@@ -169,8 +168,7 @@ int ping_send(struct in_addr addr)
|
|
|
|
|
SOL_IP = pe->p_proto;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
isock = ping_isocket;
|
|
|
|
|
p_sequence = 1;
|
|
|
|
|
p_sequence++;
|
|
|
|
|
id = (unsigned short)get_rand16(); /* randomize a ping id */
|
|
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
@@ -181,9 +179,9 @@ int ping_send(struct in_addr addr)
|
|
|
|
|
/* This filter lets ECHO_REPLY (0), DEST_UNREACH(3) and TIME_EXCEEDED(11) pass. */
|
|
|
|
|
/* !(0000 1000 0000 1001) = 0xff ff f7 f6 */
|
|
|
|
|
f.data=0xfffff7f6;
|
|
|
|
|
if (setsockopt(isock, SOL_RAW, ICMP_FILTER, &f, sizeof(f)) == -1) {
|
|
|
|
|
if (setsockopt(ping_isocket, SOL_RAW, ICMP_FILTER, &f, sizeof(f)) == -1) {
|
|
|
|
|
if (icmp_errs < ICMP_MAX_ERRS)
|
|
|
|
|
tasklog('?', "$icmp ping: setsockopt() failed %d", isock);
|
|
|
|
|
tasklog('?', "$icmp ping: setsockopt() failed %d", ping_isocket);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
@@ -211,7 +209,10 @@ int ping_send(struct in_addr addr)
|
|
|
|
|
to.sin_port = 0;
|
|
|
|
|
to.sin_addr = addr;
|
|
|
|
|
SET_SOCKA_LEN4(to);
|
|
|
|
|
if (sendto(isock, &icmpd, ICMP4_ECHO_LEN, 0, (struct sockaddr *)&to, sizeof(to)) == -1) {
|
|
|
|
|
|
|
|
|
|
sentlen = sendto(ping_isocket, &icmpd, ICMP4_ECHO_LEN, 0, (struct sockaddr *)&to, sizeof(to));
|
|
|
|
|
if (sentlen != ICMP4_ECHO_LEN) {
|
|
|
|
|
tasklog('+', "ping: sent %d octets, ret %d", ICMP4_ECHO_LEN, sentlen);
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
@@ -219,10 +220,19 @@ int ping_send(struct in_addr addr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 0 = reply received Ok.
|
|
|
|
|
* -1 = reply packet not for us, this is Ok.
|
|
|
|
|
* -2 = destination unreachable.
|
|
|
|
|
* -3 = poll/select error.
|
|
|
|
|
* -4 = time exceeded.
|
|
|
|
|
* -5 = wrong packetlen received.
|
|
|
|
|
* -6 = no data received, this is Ok.
|
|
|
|
|
* -7 = icmp parameter problem.
|
|
|
|
|
*/
|
|
|
|
|
int ping_receive(struct in_addr addr)
|
|
|
|
|
{
|
|
|
|
|
char buf[1024];
|
|
|
|
|
int isock;
|
|
|
|
|
int len;
|
|
|
|
|
struct sockaddr_in ffrom;
|
|
|
|
|
struct icmphdr icmpp;
|
|
|
|
@@ -230,10 +240,9 @@ int ping_receive(struct in_addr addr)
|
|
|
|
|
socklen_t sl;
|
|
|
|
|
struct pollfd pfd;
|
|
|
|
|
|
|
|
|
|
isock = ping_isocket;
|
|
|
|
|
|
|
|
|
|
pfd.fd = isock;
|
|
|
|
|
pfd.fd = ping_isocket;
|
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 10 mSec is enough, this function is called at regular intervals.
|
|
|
|
|
*/
|
|
|
|
@@ -245,24 +254,28 @@ int ping_receive(struct in_addr addr)
|
|
|
|
|
|
|
|
|
|
if (pfd.revents & POLLIN || pfd.revents & POLLERR || pfd.revents & POLLHUP || pfd.revents & POLLNVAL) {
|
|
|
|
|
sl = sizeof(ffrom);
|
|
|
|
|
if ((len = recvfrom(isock, &buf, sizeof(buf), 0,(struct sockaddr *)&ffrom, &sl)) != -1) {
|
|
|
|
|
if ((len = recvfrom(ping_isocket, &buf, sizeof(buf)-1, 0,(struct sockaddr *)&ffrom, &sl)) != -1) {
|
|
|
|
|
if (len > sizeof(struct iphdr)) {
|
|
|
|
|
memcpy(&iph, buf, sizeof(iph));
|
|
|
|
|
if (len - iph.ip_hl * 4 >= ICMP_BASEHDR_LEN) {
|
|
|
|
|
memcpy(&icmpp, ((unsigned long int *)buf)+iph.ip_hl, sizeof(icmpp));
|
|
|
|
|
if (iph.ip_saddr == addr.s_addr &&
|
|
|
|
|
icmpp.icmp_type == ICMP_ECHOREPLY &&
|
|
|
|
|
ntohs(icmpp.icmp_id) == id &&
|
|
|
|
|
ntohs(icmpp.icmp_seq) <= p_sequence) {
|
|
|
|
|
if (iph.ip_saddr == addr.s_addr && icmpp.icmp_type == ICMP_ECHOREPLY &&
|
|
|
|
|
ntohs(icmpp.icmp_id) == id && ntohs(icmpp.icmp_seq) == p_sequence) {
|
|
|
|
|
// tasklog('-', "ping: valid reply received, id %d, sequence %d", ntohs(icmpp.icmp_id), ntohs(icmpp.icmp_seq));
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
/* No regular echo reply. Maybe an error? */
|
|
|
|
|
if (icmp4_errcmp((char *)&icmpd, ICMP4_ECHO_LEN,
|
|
|
|
|
&to.sin_addr, buf, len, ICMP_DEST_UNREACH) ||
|
|
|
|
|
icmp4_errcmp((char *)&icmpd, ICMP4_ECHO_LEN,
|
|
|
|
|
&to.sin_addr, buf, len, ICMP_TIME_EXCEEDED)) {
|
|
|
|
|
if (icmp4_errcmp((char *)&icmpd, ICMP4_ECHO_LEN, &to.sin_addr, buf, len, ICMP_DEST_UNREACH))
|
|
|
|
|
return -2;
|
|
|
|
|
if (icmp4_errcmp((char *)&icmpd, ICMP4_ECHO_LEN, &to.sin_addr, buf, len, ICMP_TIME_EXCEEDED))
|
|
|
|
|
return -4;
|
|
|
|
|
}
|
|
|
|
|
if (icmp4_errcmp((char *)&icmpd, ICMP4_ECHO_LEN, &to.sin_addr, buf, len, ICMP_PARAMETERPROB))
|
|
|
|
|
return -7;
|
|
|
|
|
/*
|
|
|
|
|
* No fatal problem, the return code will be -1 caused by other
|
|
|
|
|
* icmp trafic on the network (packets not for us).
|
|
|
|
|
*/
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -358,12 +371,16 @@ void state_ping(void)
|
|
|
|
|
switch (pingstate) {
|
|
|
|
|
case P_NONE: pingresult[pingnr] = TRUE;
|
|
|
|
|
break;
|
|
|
|
|
case P_SENT: rc = ping_receive(paddr);
|
|
|
|
|
case P_SENT: /*
|
|
|
|
|
* Quickly eat all packets not for us, we only want our
|
|
|
|
|
* packets and empty results (packet still underway).
|
|
|
|
|
*/
|
|
|
|
|
while ((rc = ping_receive(paddr)) == -1);
|
|
|
|
|
if (!rc) {
|
|
|
|
|
pingstate = P_OK;
|
|
|
|
|
pingresult[pingnr] = TRUE;
|
|
|
|
|
} else {
|
|
|
|
|
if (rc != -6)
|
|
|
|
|
// if (rc != -6)
|
|
|
|
|
tasklog('p', "ping recv %s id=%d rc=%d", pingaddress, id, rc);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
@@ -371,3 +388,28 @@ void state_ping(void)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Create the ping socket, called from main() durig init as root.
|
|
|
|
|
*/
|
|
|
|
|
void init_pingsocket(void)
|
|
|
|
|
{
|
|
|
|
|
if ((ping_isocket = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1) {
|
|
|
|
|
if (errno == EPERM) {
|
|
|
|
|
fprintf(stderr, "socket init failed, mbtask not installed setuid root\n");
|
|
|
|
|
} else {
|
|
|
|
|
fprintf(stderr, "socket init failed\n");
|
|
|
|
|
}
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If someone's messing with us, bail.
|
|
|
|
|
* It would be nice to issue an error message, but to where?
|
|
|
|
|
*/
|
|
|
|
|
if (ping_isocket == STDIN_FILENO || ping_isocket == STDOUT_FILENO || ping_isocket == STDERR_FILENO) {
|
|
|
|
|
exit(255);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|