index 4da6ea166cecb98859f42f923f7a9ea0f5bea22b..6d8ba09980a141848e2c21d6441ec4c9aa1617ce 100644 (file)
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <signal.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <signal.h>
+#include <float.h>
/** sometimes undefined system macros (quite a few, actually) **/
/** sometimes undefined system macros (quite a few, actually) **/
static u_int get_timevaldiff(struct timeval *, struct timeval *);
static in_addr_t get_ip_address(const char *);
static int wait_for_reply(int, u_int);
static u_int get_timevaldiff(struct timeval *, struct timeval *);
static in_addr_t get_ip_address(const char *);
static int wait_for_reply(int, u_int);
-static int recvfrom_wto(int, char *, unsigned int, struct sockaddr *, u_int *);
+static int recvfrom_wto(int, void *, unsigned int, struct sockaddr *, u_int *);
static int send_icmp_ping(int, struct rta_host *);
static int get_threshold(char *str, threshold *th);
static void run_checks(void);
static void set_source_ip(char *);
static int add_target(char *);
static int add_target_ip(char *, struct in_addr *);
static int send_icmp_ping(int, struct rta_host *);
static int get_threshold(char *str, threshold *th);
static void run_checks(void);
static void set_source_ip(char *);
static int add_target(char *);
static int add_target_ip(char *, struct in_addr *);
-static int handle_random_icmp(char *, struct sockaddr_in *);
+static int handle_random_icmp(unsigned char *, struct sockaddr_in *);
static unsigned short icmp_checksum(unsigned short *, int);
static void finish(int);
static void crash(const char *, ...);
static unsigned short icmp_checksum(unsigned short *, int);
static void finish(int);
static void crash(const char *, ...);
}
static int
}
static int
-handle_random_icmp(char *packet, struct sockaddr_in *addr)
+handle_random_icmp(unsigned char *packet, struct sockaddr_in *addr)
{
struct icmp p, sent_icmp;
struct rta_host *host = NULL;
memcpy(&p, packet, sizeof(p));
{
struct icmp p, sent_icmp;
struct rta_host *host = NULL;
memcpy(&p, packet, sizeof(p));
- if(p.icmp_type == ICMP_ECHO && p.icmp_id == pid) {
+ if(p.icmp_type == ICMP_ECHO && ntohs(p.icmp_id) == pid) {
/* echo request from us to us (pinging localhost) */
return 0;
}
/* echo request from us to us (pinging localhost) */
return 0;
}
/* might be for us. At least it holds the original package (according
* to RFC 792). If it isn't, just ignore it */
memcpy(&sent_icmp, packet + 28, sizeof(sent_icmp));
/* might be for us. At least it holds the original package (according
* to RFC 792). If it isn't, just ignore it */
memcpy(&sent_icmp, packet + 28, sizeof(sent_icmp));
- if(sent_icmp.icmp_type != ICMP_ECHO || sent_icmp.icmp_id != pid ||
- sent_icmp.icmp_seq >= targets)
+ if(sent_icmp.icmp_type != ICMP_ECHO || ntohs(sent_icmp.icmp_id) != pid ||
+ ntohs(sent_icmp.icmp_seq) >= targets*packets)
{
if(debug) printf("Packet is no response to a packet we sent\n");
return 0;
}
/* it is indeed a response for us */
{
if(debug) printf("Packet is no response to a packet we sent\n");
return 0;
}
/* it is indeed a response for us */
- host = table[sent_icmp.icmp_seq];
+ host = table[ntohs(sent_icmp.icmp_seq)/packets];
if(debug) {
printf("Received \"%s\" from %s for ICMP ECHO sent to %s.\n",
get_icmp_error_msg(p.icmp_type, p.icmp_code),
if(debug) {
printf("Received \"%s\" from %s for ICMP ECHO sent to %s.\n",
get_icmp_error_msg(p.icmp_type, p.icmp_code),
table = malloc(sizeof(struct rta_host **) * (argc - 1));
i = 0;
while(host) {
table = malloc(sizeof(struct rta_host **) * (argc - 1));
i = 0;
while(host) {
- host->id = i;
+ host->id = i*packets;
table[i] = host;
host = host->next;
i++;
table[i] = host;
host = host->next;
i++;
wait_for_reply(int sock, u_int t)
{
int n, hlen;
wait_for_reply(int sock, u_int t)
{
int n, hlen;
- static char buf[4096];
+ static unsigned char buf[4096];
struct sockaddr_in resp_addr;
struct ip *ip;
struct icmp icp;
struct sockaddr_in resp_addr;
struct ip *ip;
struct icmp icp;
/* check the response */
memcpy(&icp, buf + hlen, sizeof(icp));
/* check the response */
memcpy(&icp, buf + hlen, sizeof(icp));
- if(icp.icmp_id != pid) {
- handle_random_icmp(buf + hlen, &resp_addr);
- continue;
- }
-
- if(icp.icmp_type != ICMP_ECHOREPLY || icp.icmp_seq >= targets) {
+ if(ntohs(icp.icmp_id) != pid || icp.icmp_type != ICMP_ECHOREPLY ||
+ ntohs(icp.icmp_seq) >= targets*packets) {
if(debug > 2) printf("not a proper ICMP_ECHOREPLY\n");
handle_random_icmp(buf + hlen, &resp_addr);
continue;
if(debug > 2) printf("not a proper ICMP_ECHOREPLY\n");
handle_random_icmp(buf + hlen, &resp_addr);
continue;
/* this is indeed a valid response */
memcpy(&data, icp.icmp_data, sizeof(data));
/* this is indeed a valid response */
memcpy(&data, icp.icmp_data, sizeof(data));
+ if (debug > 2)
+ printf("ICMP echo-reply of len %u, id %u, seq %u, cksum 0x%X\n",
+ sizeof(data), ntohs(icp.icmp_id), ntohs(icp.icmp_seq), icp.icmp_cksum);
- host = table[icp.icmp_seq];
+ host = table[ntohs(icp.icmp_seq)/packets];
gettimeofday(&now, &tz);
tdiff = get_timevaldiff(&data.stime, &now);
gettimeofday(&now, &tz);
tdiff = get_timevaldiff(&data.stime, &now);
send_icmp_ping(int sock, struct rta_host *host)
{
static union {
send_icmp_ping(int sock, struct rta_host *host)
{
static union {
- char *buf; /* re-use so we prevent leaks */
+ void *buf; /* re-use so we prevent leaks */
struct icmp *icp;
u_short *cksum_in;
} packet = { NULL };
struct icmp *icp;
u_short *cksum_in;
} packet = { NULL };
packet.icp->icmp_type = ICMP_ECHO;
packet.icp->icmp_code = 0;
packet.icp->icmp_cksum = 0;
packet.icp->icmp_type = ICMP_ECHO;
packet.icp->icmp_code = 0;
packet.icp->icmp_cksum = 0;
- packet.icp->icmp_id = pid;
- packet.icp->icmp_seq = host->id;
+ packet.icp->icmp_id = htons(pid);
+ packet.icp->icmp_seq = htons(host->id++);
packet.icp->icmp_cksum = icmp_checksum(packet.cksum_in, icmp_pkt_size);
packet.icp->icmp_cksum = icmp_checksum(packet.cksum_in, icmp_pkt_size);
+ if (debug > 2)
+ printf("Sending ICMP echo-request of len %u, id %u, seq %u, cksum 0x%X to host %s\n",
+ sizeof(data), ntohs(packet.icp->icmp_id), ntohs(packet.icp->icmp_seq), packet.icp->icmp_cksum, host->name);
+
len = sendto(sock, packet.buf, icmp_pkt_size, 0, (struct sockaddr *)addr,
sizeof(struct sockaddr));
len = sendto(sock, packet.buf, icmp_pkt_size, 0, (struct sockaddr *)addr,
sizeof(struct sockaddr));
}
static int
}
static int
-recvfrom_wto(int sock, char *buf, unsigned int len, struct sockaddr *saddr,
+recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr *saddr,
u_int *timo)
{
u_int slen;
u_int *timo)
{
u_int slen;
if(!early) early = &prog_start;
/* if early > later we return 0 so as to indicate a timeout */
if(!early) early = &prog_start;
/* if early > later we return 0 so as to indicate a timeout */
- if(early->tv_sec > early->tv_sec ||
+ if(early->tv_sec > later->tv_sec ||
(early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec))
{
return 0;
(early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec))
{
return 0;
print_usage ();
print_usage ();
- printf (_(UT_HELP_VRSN));
- printf (_(UT_EXTRA_OPTS));
+ printf (UT_HELP_VRSN);
+ printf (UT_EXTRA_OPTS);
printf (" %s\n", "-H");
printf (" %s\n", _("specify a target"));
printf (" %s\n", "-H");
printf (" %s\n", _("specify a target"));
/* printf ("%s\n", _("Long options are currently unsupported."));
printf ("%s\n", _("Options marked with * require an argument"));
*/
/* printf ("%s\n", _("Long options are currently unsupported."));
printf ("%s\n", _("Options marked with * require an argument"));
*/
-#ifdef NP_EXTRA_OPTS
- printf ("\n");
- printf (_(UT_EXTRA_OPTS_NOTES));
-#endif
- printf (_(UT_SUPPORT));
+ printf (UT_SUPPORT);
}
}
void
print_usage (void)
{
void
print_usage (void)
{
- printf (_("Usage:"));
+ printf ("%s\n", _("Usage:"));
printf(" %s [options] [-H] host1 host2 hostN\n", progname);
}
printf(" %s [options] [-H] host1 host2 hostN\n", progname);
}