X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=hacks%2Fsonar.c;h=6b1453398ce7e8a92d96c77622f3877ff363c65f;hb=2d04c4f22466851aedb6ed0f2919d148f726b889;hp=3c492189ad505c093a57f8c46a666768892e9b23;hpb=f65151994eba80ecabcdac6eef6fa0dde7e2d45b;p=xscreensaver diff --git a/hacks/sonar.c b/hacks/sonar.c index 3c492189..6b145339 100644 --- a/hacks/sonar.c +++ b/hacks/sonar.c @@ -12,11 +12,14 @@ * of bogies that move around on the scope while the ping sensor can be * used to display hosts on your network. * - * The ping code is only compiled in if you define HAVE_PING, because, - * unfortunately, creating an ICMP socket is a privileged operation, the - * program needs to be installed SUID root if you want to use the ping - * mode. If you check the code you will see that this privilige is given up - * immediately after the socket is created. + * The ping code is only compiled in if you define HAVE_ICMP or HAVE_ICMPHDR, + * because, unfortunately, different systems have different ways of creating + * these sorts of packets. + * + * Also: creating an ICMP socket is a privileged operation, so the program + * needs to be installed SUID root if you want to use the ping mode. If you + * check the code you will see that this privilige is given up immediately + * after the socket is created. * * It should be easy to extend this code to support other sorts of sensors. * Some ideas: @@ -25,7 +28,8 @@ * - plot the process table, by process size, cpu usage, or total time; * - plot the logged on users by idle time or cpu usage. * - * Copyright (C) 1998 by Stephen Martin (smartin@canada.com). + * Copyright (C) 1998, 2001 + * by Stephen Martin (smartin@vanderfleet-martin.net). * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that @@ -34,7 +38,7 @@ * software for any purpose. It is provided "as is" without express or * implied warranty. * - * $Revision: 1.6 $ + * $Revision: 1.37 $ * * Version 1.0 April 27, 1998. * - Initial version @@ -72,40 +76,85 @@ * - Now need to define HAVE_PING to compile in the ping stuff. */ +/* These are computed by configure now: + #define HAVE_ICMP + #define HAVE_ICMPHDR + */ + + /* Include Files */ -#ifdef HAVE_PING -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#endif /* HAVE_PING */ -#include -#include #include +#include +#include +#include + #include "screenhack.h" #include "colors.h" #include "hsv.h" -#include + +#if defined(HAVE_ICMP) || defined(HAVE_ICMPHDR) +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +#endif /* HAVE_ICMP || HAVE_ICMPHDR */ + /* Defines */ -#ifndef MIN -#define MIN(a,b) ((a)<(b)?(a - 50):(b - 10)) -#endif /* MIN */ +#undef MY_MIN +#define MY_MIN(a,b) ((a)<(b)?(a - 50):(b - 10)) + +#ifndef LINE_MAX +# define LINE_MAX 2048 +#endif + +/* Frigging icmp */ + +#if defined(HAVE_ICMP) +# define HAVE_PING +# define ICMP icmp +# define ICMP_TYPE(p) (p)->icmp_type +# define ICMP_CODE(p) (p)->icmp_code +# define ICMP_CHECKSUM(p) (p)->icmp_cksum +# define ICMP_ID(p) (p)->icmp_id +# define ICMP_SEQ(p) (p)->icmp_seq +#elif defined(HAVE_ICMPHDR) +# define HAVE_PING +# define ICMP icmphdr +# define ICMP_TYPE(p) (p)->type +# define ICMP_CODE(p) (p)->code +# define ICMP_CHECKSUM(p) (p)->checksum +# define ICMP_ID(p) (p)->un.echo.id +# define ICMP_SEQ(p) (p)->un.echo.sequence +#else +# undef HAVE_PING +#endif + + +#ifdef HAVE_PING +# if defined(__DECC) || defined(_IP_VHL) + /* This is how you do it on DEC C, and possibly some BSD systems. */ +# define IP_HDRLEN(ip) ((ip)->ip_vhl & 0x0F) +# else + /* This is how you do it on everything else. */ +# define IP_HDRLEN(ip) ((ip)->ip_hl) +# endif +#endif /* HAVE_PING */ + /* Forward References */ @@ -114,16 +163,18 @@ static u_short checksum(u_short *, int); #endif static long delta(struct timeval *, struct timeval *); + /* Data Structures */ /* * The Bogie. * - * This represents an object that is visable on the scope. + * This represents an object that is visible on the scope. */ typedef struct Bogie { char *name; /* The name of the thing being displayed */ + char *desc; /* Beneath the name (e.g., ping time) */ int distance; /* The distance to this thing (0 - 100) */ int tick; /* The tick that it was found on */ int ttl; /* The time to live */ @@ -155,13 +206,20 @@ typedef struct { int width, height; /* Window dimensions */ int minx, miny, maxx, maxy, /* Bounds of the scope */ centrex, centrey, radius; /* Parts of the scope circle */ - Bogie *visable; /* List of visable objects */ + Bogie *visible; /* List of visible objects */ int current; /* Current position of sweep */ - + int sweepnum; /* The current id of the sweep */ int delay; /* how long between each frame of the anim */ + int TTL; /* The number of ticks that bogies are visible + on the screen before they fade away. */ } sonar_info; +static Bool debug_p = False; +static Bool resolve_p = True; +static Bool times_p = True; + + /* * Variables to support the differnt Sonar modes. */ @@ -173,13 +231,16 @@ void *sensor_info; /* Information about the sensor */ * A list of targets to ping. */ -#ifdef HAVE_PING typedef struct ping_target { char *name; /* The name of the target */ +#ifdef HAVE_PING struct sockaddr address; /* The address of the target */ +#endif /* HAVE_PING */ struct ping_target *next; /* The next one in the list */ } ping_target; + +#ifdef HAVE_PING /* * Ping Information. * @@ -199,7 +260,6 @@ typedef struct { static int timer_expired; - #endif /* HAVE_PING */ /* @@ -210,7 +270,7 @@ typedef struct sim_target { char *name; /* The name of the target */ int nexttick; /* The next tick that this will be seen */ int nextdist; /* The distance on that tick */ - int movedlasttick; /* Flag to indicate we just moved this one */ + int movedonsweep; /* The number of the sweep this last moved */ } sim_target; /* @@ -249,16 +309,17 @@ char *defaults [] = { "*textSteps: 80", /* npixels */ "*sweepSegments: 80", /* npixels */ -#ifdef HAVE_PING "*pingTimeout: 3000", - "*pingSource: file", - "*pingFile: /etc/hosts", - "*pingList: localhost", -#endif /* HAVE_PING */ + "*teamAName: F18", "*teamBName: MIG", "*teamACount: 4", "*teamBCount: 4", + + "*ping: default", + "*resolve: true", + "*showTimes: true", + ".debug: false", 0 }; @@ -271,28 +332,22 @@ XrmOptionDescRec options [] = { {"-grid-color", ".gridColor", XrmoptionSepArg, 0 }, {"-text-color", ".textColor", XrmoptionSepArg, 0 }, {"-ttl", ".ttl", XrmoptionSepArg, 0 }, - {"-mode", ".mode", XrmoptionSepArg, 0 }, {"-font", ".font", XrmoptionSepArg, 0 }, #ifdef HAVE_PING {"-ping-timeout", ".pingTimeout", XrmoptionSepArg, 0 }, - {"-ping-source", ".pingSource", XrmoptionSepArg, 0 }, - {"-ping-file", ".pingFile", XrmoptionSepArg, 0 }, - {"-ping-list", ".pingList", XrmoptionSepArg, 0 }, #endif /* HAVE_PING */ {"-team-a-name", ".teamAName", XrmoptionSepArg, 0 }, {"-team-b-name", ".teamBName", XrmoptionSepArg, 0 }, {"-team-a-count", ".teamACount", XrmoptionSepArg, 0 }, {"-team-b-count", ".teamBCount", XrmoptionSepArg, 0 }, + + {"-ping", ".ping", XrmoptionSepArg, 0 }, + {"-no-dns", ".resolve", XrmoptionNoArg, "False" }, + {"-no-times", ".showTimes", XrmoptionNoArg, "False" }, + {"-debug", ".debug", XrmoptionNoArg, "True" }, { 0, 0, 0, 0 } }; -/* - * The number of ticks that bogies are visable on the screen before they - * fade away. - */ - -static int TTL; - /* * Create a new Bogie and set some initial values. * @@ -314,10 +369,11 @@ newBogie(char *name, int distance, int tick, int ttl) Bogie *new; + distance *= 1000; /* Allocate a bogie and initialize it */ if ((new = (Bogie *) calloc(1, sizeof(Bogie))) == NULL) { - fprintf(stderr, "Out of Memory\n"); + fprintf(stderr, "%s: Out of Memory\n", progname); return NULL; } new->name = name; @@ -336,6 +392,7 @@ newBogie(char *name, int distance, int tick, int ttl) * b - The bogie to free. */ + static void freeBogie(Bogie *b) { @@ -386,6 +443,31 @@ findNode(Bogie *bl, char *name) #ifdef HAVE_PING +/* Packs an IP address quad into bigendian network order. */ +static unsigned long +pack_addr (unsigned int a, unsigned int b, unsigned int c, unsigned int d) +{ + unsigned long i = (((a & 255) << 24) | + ((b & 255) << 16) | + ((c & 255) << 8) | + ((d & 255) )); + return htonl (i); +} + +/* Unpacks an IP address quad from bigendian network order. */ +static void +unpack_addr (unsigned long addr, + unsigned int *a, unsigned int *b, + unsigned int *c, unsigned int *d) +{ + addr = ntohl (addr); + *a = (addr >> 24) & 255; + *b = (addr >> 16) & 255; + *c = (addr >> 8) & 255; + *d = (addr ) & 255; +} + + /* * Lookup the address for a ping target; * @@ -399,34 +481,94 @@ findNode(Bogie *bl, char *name) static int lookupHost(ping_target *target) { + struct hostent *hent; + struct sockaddr_in *iaddr; - /* Local Variables */ + unsigned int ip[4]; + char c; + + iaddr = (struct sockaddr_in *) &(target->address); + iaddr->sin_family = AF_INET; - struct sockaddr_in *iaddr; + if (4 == sscanf (target->name, " %u.%u.%u.%u %c", + &ip[0], &ip[1], &ip[2], &ip[3], &c)) + { + /* It's an IP address. + */ + if (ip[3] == 0) + { + if (debug_p > 1) + fprintf (stderr, "%s: ignoring bogus IP %s\n", + progname, target->name); + return 0; + } - /* Set up the target address we first assume that the name is the - IP address as a string */ + iaddr->sin_addr.s_addr = pack_addr (ip[0], ip[1], ip[2], ip[3]); + if (resolve_p) + hent = gethostbyaddr ((const char *) &iaddr->sin_addr.s_addr, + sizeof(iaddr->sin_addr.s_addr), + AF_INET); + else + hent = 0; + + if (debug_p > 1) + fprintf (stderr, "%s: %s => %s\n", + progname, target->name, + ((hent && hent->h_name && *hent->h_name) + ? hent->h_name : "")); + + if (hent && hent->h_name && *hent->h_name) + target->name = strdup (hent->h_name); + } + else + { + /* It's a host name. + */ - iaddr = (struct sockaddr_in *) &(target->address); - iaddr->sin_family = AF_INET; - if ((iaddr->sin_addr.s_addr = inet_addr(target->name)) == -1) { - /* Conversion of IP address failed, try to look the host up by name */ + /* don't waste time being confused by non-hostname tokens + in .ssh/known_hosts */ + if (!strcmp (target->name, "ssh-rsa") || + !strcmp (target->name, "ssh-dsa") || + !strcmp (target->name, "ssh-dss") || + strlen (target->name) >= 80) + return 0; - struct hostent *hent = gethostbyname(target->name); - if (hent == NULL) { - fprintf(stderr, "Could not resolve host %s\n", target->name); - return 0; - } - memcpy(&iaddr->sin_addr, hent->h_addr_list[0], - sizeof(iaddr->sin_addr)); + hent = gethostbyname (target->name); + if (!hent) + { + fprintf (stderr, "%s: could not resolve host: %s\n", + progname, target->name); + return 0; + } + + memcpy (&iaddr->sin_addr, hent->h_addr_list[0], + sizeof(iaddr->sin_addr)); + + if (debug_p > 1) + { + unsigned int a, b, c, d; + unpack_addr (iaddr->sin_addr.s_addr, &a, &b, &c, &d); + fprintf (stderr, "%s: %s => %d.%d.%d.%d\n", + progname, target->name, a, b, c, d); + } } + return 1; +} - /* Done */ - return 1; +static void +print_host (FILE *out, unsigned long ip, const char *name) +{ + char ips[50]; + unsigned int a, b, c, d; + unpack_addr (ip, &a, &b, &c, &d); /* ip is in network order */ + sprintf (ips, "%u.%u.%u.%u", a, b, c, d); + if (!name || !*name) name = ""; + fprintf (out, "%-16s %s\n", ips, name); } + /* * Create a target for a host. * @@ -448,11 +590,11 @@ newHost(char *name) /* Create the target */ if ((target = calloc(1, sizeof(ping_target))) == NULL) { - fprintf(stderr, "Out of Memory\n"); + fprintf(stderr, "%s: Out of Memory\n", progname); goto target_init_error; } if ((target->name = strdup(name)) == NULL) { - fprintf(stderr, "Out of Memory\n"); + fprintf(stderr, "%s: Out of Memory\n", progname); goto target_init_error; } @@ -461,8 +603,30 @@ newHost(char *name) if (! lookupHost(target)) goto target_init_error; + /* Don't ever use loopback (127.0.0.x) hosts */ + { + struct sockaddr_in *iaddr = (struct sockaddr_in *) &(target->address); + unsigned long ip = iaddr->sin_addr.s_addr; + + if ((ntohl (ip) & 0xFFFFFF00L) == 0x7f000000L) /* 127.0.0 */ + { + if (debug_p) + fprintf (stderr, "%s: ignoring loopback host %s\n", + progname, target->name); + goto target_init_error; + } + } + /* Done */ + if (debug_p) + { + struct sockaddr_in *iaddr = (struct sockaddr_in *) &(target->address); + unsigned long ip = iaddr->sin_addr.s_addr; + fprintf (stderr, "%s: added ", progname); + print_host (stderr, ip, target->name); + } + return target; /* Handle errors here */ @@ -487,7 +651,6 @@ target_init_error: static ping_target * readPingHostsFile(char *fname) { - /* Local Variables */ FILE *fp; @@ -500,7 +663,7 @@ readPingHostsFile(char *fname) /* Make sure we in fact have a file to process */ if ((fname == NULL) || (fname[0] == '\0')) { - fprintf(stderr, "Invalid ping host file name\n"); + fprintf(stderr, "%s: invalid ping host file name\n", progname); return NULL; } @@ -508,11 +671,14 @@ readPingHostsFile(char *fname) if ((fp = fopen(fname, "r")) == NULL) { char msg[1024]; - sprintf(msg, "Unable to open host file %s", fname); + sprintf(msg, "%s: unable to open host file %s", progname, fname); perror(msg); return NULL; } + if (debug_p) + fprintf (stderr, "%s: reading file %s\n", progname, fname); + /* Read the file line by line */ while ((p = fgets(buf, LINE_MAX, fp)) != NULL) { @@ -532,17 +698,48 @@ readPingHostsFile(char *fname) /* Get the name and address */ name = addr = NULL; - if ((addr = strtok(buf, " \t\n")) != NULL) - name = strtok(NULL, " \t\n"); + if ((addr = strtok(buf, " ,;\t\n")) != NULL) + name = strtok(NULL, " ,;\t\n"); else continue; + /* Check to see if the addr looks like an addr. If not, assume + the addr is a name and there is no addr. This way, we can + handle files whose lines have "xx.xx.xx.xx hostname" as their + first two tokens, and also files that have a hostname as their + first token (like .ssh/known_hosts and .rhosts.) + */ + { + int i; char c; + if (4 != sscanf(addr, "%d.%d.%d.%d%c", &i, &i, &i, &i, &c)) + { + name = addr; + addr = NULL; + } + } + + /* If the name is all digits, it's not a name. */ + if (name) + { + const char *s; + for (s = name; *s; s++) + if (*s < '0' || *s > '9') + break; + if (! *s) + { + if (debug_p > 1) + fprintf (stderr, "%s: skipping bogus name \"%s\" (%s)\n", + progname, name, addr); + name = NULL; + } + } + /* Create a new target using first the name then the address */ new = NULL; if (name != NULL) new = newHost(name); - if (new == NULL) + if (new == NULL && addr != NULL) new = newHost(addr); /* Add it to the list if we got one */ @@ -559,59 +756,59 @@ readPingHostsFile(char *fname) return list; } -/* - * Generate a list of ping targets from the entries in a string. - * - * Args: - * list - A list of comma separated host names. - * - * Returns: - * A list of targets to ping or null if an error occured. - */ static ping_target * -readPingHostsList(char *list) +delete_duplicate_hosts (ping_target *list) { + ping_target *head = list; + ping_target *rest; - /* Local Variables */ - - char *host; - ping_target *hostlist = NULL; - ping_target *new; + for (rest = head; rest; rest = rest->next) + { + struct sockaddr_in *i1 = (struct sockaddr_in *) &(rest->address); + unsigned long ip1 = i1->sin_addr.s_addr; - /* Check that there is a list */ + static ping_target *rest2; + for (rest2 = rest; rest2; rest2 = rest2->next) + { + if (rest2 && rest2->next) + { + struct sockaddr_in *i2 = (struct sockaddr_in *) + &(rest2->next->address); + unsigned long ip2 = i2->sin_addr.s_addr; - if ((list == NULL) || (list[0] == '\0')) - return NULL; + if (ip1 == ip2) + { + if (debug_p) + { + fprintf (stderr, "%s: deleted duplicate: ", progname); + print_host (stderr, ip2, rest2->next->name); + } + rest2->next = rest2->next->next; + } + } + } + } - /* Loop through the hosts and add them to the list to return */ + return head; +} - host = strtok(list, ","); - while (host != NULL) { - new = newHost(host); - if (new != NULL) { - new->next = hostlist; - hostlist = new; - } - host = strtok(NULL, ","); - } - /* Done */ - return hostlist; -} /* * Generate a list ping targets consisting of all of the entries on - * the same subnet. + * the same subnet. 'base' ip is in network order; 0 means localhost. * * Returns: * A list of all of the hosts on this net. */ static ping_target * -subnetHostsList(void) +subnetHostsList(unsigned long n_base, int subnet_width) { + unsigned long h_mask; /* host order */ + unsigned long h_base; /* host order */ /* Local Variables */ @@ -623,40 +820,104 @@ subnetHostsList(void) ping_target *new; ping_target *list = NULL; + if (subnet_width < 24) + { + fprintf (stderr, + "%s: pinging %lu hosts is a bad idea; please use a subnet mask of 24 bits\n" + " or more (255 hosts max.)\n", + progname, (unsigned long) (1L << (32 - subnet_width)) - 1); + exit (1); + } + else if (subnet_width > 30) + { + fprintf (stderr, "%s: a subnet of %d bits doesn't make sense:" + " try \"subnet/24\" or \"subnet/29\".\n", + progname, subnet_width); + exit (1); + } + + + if (debug_p) + fprintf (stderr, "%s: adding %d-bit subnet\n", progname, subnet_width); + /* Get our hostname */ if (gethostname(hostname, BUFSIZ)) { - fprintf(stderr, "Unable to get local hostname\n"); + fprintf(stderr, "%s: unable to get local hostname\n", progname); return NULL; } /* Get our IP address and convert it to a string */ if ((hent = gethostbyname(hostname)) == NULL) { - fprintf(stderr, "Unable to lookup our IP address\n"); + fprintf(stderr, "%s: unable to lookup our IP address\n", progname); return NULL; } strcpy(address, inet_ntoa(*((struct in_addr *)hent->h_addr_list[0]))); - /* Get a pointer to the last "." in the string */ + /* Construct targets for all addresses in this subnet */ + + h_mask = 0; + for (i = 0; i < subnet_width; i++) + h_mask |= (1L << (31-i)); - if ((p = strrchr(address, '.')) == NULL) { - fprintf(stderr, "Can't parse IP address %s\n", address); - return NULL; - } - p++; + /* If no base IP specified, assume localhost. */ + if (n_base == 0) + n_base = pack_addr (hent->h_addr_list[0][0], + hent->h_addr_list[0][1], + hent->h_addr_list[0][2], + hent->h_addr_list[0][3]); + h_base = ntohl (n_base); - /* Construct targets for all addresses in this subnet */ + if (h_base == 0x7F000001L) /* 127.0.0.1 in host order */ + { + unsigned int a, b, c, d; + unpack_addr (n_base, &a, &b, &c, &d); + fprintf (stderr, + "%s: unable to determine local subnet address: \"%s\"\n" + " resolves to loopback address %u.%u.%u.%u.\n", + progname, hostname, a, b, c, d); + return NULL; + } + + for (i = 255; i >= 0; i--) { + unsigned int a, b, c, d; + int ip = (h_base & 0xFFFFFF00L) | i; /* host order */ + + if ((ip & h_mask) != (h_base & h_mask)) /* not in mask range at all */ + continue; + if ((ip & ~h_mask) == 0) /* broadcast address */ + continue; + if ((ip & ~h_mask) == ~h_mask) /* broadcast address */ + continue; + + unpack_addr (htonl (ip), &a, &b, &c, &d); + sprintf (address, "%u.%u.%u.%u", a, b, c, d); + + if (debug_p > 1) + { + unsigned int aa, ab, ac, ad; + unsigned int ma, mb, mc, md; + unpack_addr (htonl (h_base & h_mask), &aa, &ab, &ac, &ad); + unpack_addr (htonl (h_mask), &ma, &mb, &mc, &md); + fprintf (stderr, + "%s: subnet: %s (%u.%u.%u.%u & %u.%u.%u.%u / %d)\n", + progname, address, + aa, ab, ac, ad, + ma, mb, mc, md, + subnet_width); + } - for (i = 254; i > 0; i--) { + p = address + strlen(address) + 1; sprintf(p, "%d", i); + new = newHost(address); if (new != NULL) { new->next = list; list = new; } } - + /* Done */ return list; @@ -669,74 +930,64 @@ subnetHostsList(void) * A newly allocated ping_info structure or null if an error occured. */ +static ping_target *parse_mode (Bool ping_works_p); + static ping_info * init_ping(void) { + Bool socket_initted_p = False; + /* Local Variables */ ping_info *pi = NULL; /* The new ping_info struct */ - char *src; /* The source of the ping hosts */ ping_target *pt; /* Used to count the targets */ /* Create the ping info structure */ if ((pi = (ping_info *) calloc(1, sizeof(ping_info))) == NULL) { - fprintf(stderr, "Out of memory\n"); + fprintf(stderr, "%s: Out of memory\n", progname); goto ping_init_error; } - /* Create the ICMP socket and turn off SUID */ + /* Create the ICMP socket */ - if ((pi->icmpsock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { - perror("Can't create ICMP socket"); - fprintf(stderr, - "%s: this program must be setuid to root for `ping mode' to work.\n", - progname); - goto ping_init_error; + if ((pi->icmpsock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) >= 0) { + socket_initted_p = True; } + + /* Disavow privs */ + setuid(getuid()); + + pi->pid = getpid() & 0xFFFF; pi->seq = 0; pi->timeout = get_integer_resource("pingTimeout", "PingTimeout"); /* Generate a list of targets */ - src = get_string_resource("pingSource", "PingSource"); - if (strcmp(src, "file") == 0) { - - /* - * The list of ping targets is to come from a file in - * /etc/hosts format - */ - - pi->targets = readPingHostsFile(get_string_resource("pingFile", - "PingFile")); - - } else if (strcmp(src, "list") == 0) { - - /* The list of hosts is to come from the pinghostlist resource */ - - pi->targets = readPingHostsList(get_string_resource("pingList", - "PingList")); + pi->targets = parse_mode (socket_initted_p); + pi->targets = delete_duplicate_hosts (pi->targets); - } else if (strcmp(src, "subnet") == 0) { - pi->targets = subnetHostsList(); - - } else { - - /* Unknown source */ - - fprintf(stderr, "Illegal pingSource: %s\n", src); - goto ping_init_error; - } + if (debug_p) + { + ping_target *t; + fprintf (stderr, "%s: Target list:\n", progname); + for (t = pi->targets; t; t = t->next) + { + struct sockaddr_in *iaddr = (struct sockaddr_in *) &(t->address); + unsigned long ip = iaddr->sin_addr.s_addr; + fprintf (stderr, "%s: ", progname); + print_host (stderr, ip, t->name); + } + } /* Make sure there is something to ping */ if (pi->targets == NULL) { - fprintf(stderr, "Nothing to ping"); - goto ping_init_error; + goto ping_init_error; } /* Count the targets */ @@ -760,6 +1011,7 @@ ping_init_error: return NULL; } + /* * Ping a host. * @@ -775,7 +1027,7 @@ sendping(ping_info *pi, ping_target *pt) /* Local Variables */ u_char *packet; - struct icmp *icmph; + struct ICMP *icmph; int result; /* @@ -784,24 +1036,29 @@ sendping(ping_info *pi, ping_target *pt) * name or do an address lookup when it comes back. */ - int pcktsiz = sizeof(struct icmp) + sizeof(struct timeval) + + int pcktsiz = sizeof(struct ICMP) + sizeof(struct timeval) + strlen(pt->name) + 1; /* Create the ICMP packet */ if ((packet = (u_char *) malloc(pcktsiz)) == (void *) 0) return; /* Out of memory */ - icmph = (struct icmp *) packet; - icmph->icmp_type = ICMP_ECHO; - icmph->icmp_code = 0; - icmph->icmp_cksum = 0; - icmph->icmp_id = pi->pid; - icmph->icmp_seq = pi->seq++; - gettimeofday((struct timeval *) &packet[sizeof(struct icmp)], + icmph = (struct ICMP *) packet; + ICMP_TYPE(icmph) = ICMP_ECHO; + ICMP_CODE(icmph) = 0; + ICMP_CHECKSUM(icmph) = 0; + ICMP_ID(icmph) = pi->pid; + ICMP_SEQ(icmph) = pi->seq++; +# ifdef GETTIMEOFDAY_TWO_ARGS + gettimeofday((struct timeval *) &packet[sizeof(struct ICMP)], (struct timezone *) 0); - strcpy((char *) &packet[sizeof(struct icmp) + sizeof(struct timeval)], +# else + gettimeofday((struct timeval *) &packet[sizeof(struct ICMP)]); +# endif + + strcpy((char *) &packet[sizeof(struct ICMP) + sizeof(struct timeval)], pt->name); - icmph->icmp_cksum = checksum((u_short *)packet, pcktsiz); + ICMP_CHECKSUM(icmph) = checksum((u_short *)packet, pcktsiz); /* Send it */ @@ -809,7 +1066,7 @@ sendping(ping_info *pi, ping_target *pt) &pt->address, sizeof(pt->address))) != pcktsiz) { #if 0 char errbuf[BUFSIZ]; - sprintf(errbuf, "Error sending ping to %s", pt->name); + sprintf(errbuf, "%s: error sending ping to %s", progname, pt->name); perror(errbuf); #endif } @@ -866,6 +1123,7 @@ checksum(u_short *packet, int size) if (nleft == 1) { *(u_char *)(&answer) = *(u_char *)w ; + *(1 + (u_char *)(&answer)) = 0; sum += answer; } @@ -895,25 +1153,27 @@ checksum(u_short *packet, int size) */ static Bogie * -getping(sonar_info *si, ping_info *pi, int ttl) +getping(sonar_info *si, ping_info *pi) { /* Local Variables */ struct sockaddr from; - int fromlen; + unsigned int fromlen; /* Posix says socklen_t, but that's not portable */ int result; u_char packet[1024]; struct timeval now; struct timeval *then; struct ip *ip; int iphdrlen; - struct icmp *icmph; + struct ICMP *icmph; Bogie *bl = NULL; Bogie *new; char *name; struct sigaction sa; struct itimerval it; + fd_set rfds; + struct timeval tv; /* Set up a signal to interupt our wait for a packet */ @@ -921,7 +1181,9 @@ getping(sonar_info *si, ping_info *pi, int ttl) sa.sa_flags = 0; sa.sa_handler = sigcatcher; if (sigaction(SIGALRM, &sa, 0) == -1) { - perror("Unable to trap sigalarm"); + char msg[1024]; + sprintf(msg, "%s: unable to trap SIGALRM", progname); + perror(msg); exit(1); } @@ -937,27 +1199,45 @@ getping(sonar_info *si, ping_info *pi, int ttl) /* Wait for a result packet */ fromlen = sizeof(from); - while (! timer_expired && - (result = recvfrom(pi->icmpsock, packet, sizeof(packet), - 0, &from, &fromlen)) > 0) { + while (! timer_expired) { + tv.tv_usec=pi->timeout; + tv.tv_sec=0; +#if 0 + /* This breaks on BSD, which uses bzero() in the definition of FD_ZERO */ + FD_ZERO(&rfds); +#else + memset (&rfds, 0, sizeof(rfds)); +#endif + FD_SET(pi->icmpsock,&rfds); + /* only wait a little while, in case we raced with the timer expiration. + From Valentijn Sessink */ + if (select(pi->icmpsock+1, &rfds, NULL, NULL, &tv) >0) { + result = recvfrom(pi->icmpsock, packet, sizeof(packet), + 0, &from, &fromlen); /* Check the packet */ +# ifdef GETTIMEOFDAY_TWO_ARGS gettimeofday(&now, (struct timezone *) 0); +# else + gettimeofday(&now); +# endif ip = (struct ip *) packet; - iphdrlen = ip->ip_hl << 2; - icmph = (struct icmp *) &packet[iphdrlen]; + iphdrlen = IP_HDRLEN(ip) << 2; + icmph = (struct ICMP *) &packet[iphdrlen]; + then = (struct timeval *) &packet[iphdrlen + sizeof(struct ICMP)]; + /* Was the packet a reply?? */ - if (icmph->icmp_type != ICMP_ECHOREPLY) { + if (ICMP_TYPE(icmph) != ICMP_ECHOREPLY) { /* Ignore anything but ICMP Replies */ continue; /* Nope */ } /* Was it for us? */ - if (icmph->icmp_id != pi->pid) { + if (ICMP_ID(icmph) != pi->pid) { /* Ignore packets not set from us */ continue; /* Nope */ } @@ -966,12 +1246,18 @@ getping(sonar_info *si, ping_info *pi, int ttl) if ((name = strdup((char *) &packet[iphdrlen + - + sizeof(struct icmp) + + sizeof(struct ICMP) + sizeof(struct timeval)])) == NULL) { - fprintf(stderr, "Out of memory\n"); + fprintf(stderr, "%s: Out of memory\n", progname); return bl; } +# if 0 /* Don't need to do this -- the host names are already as + resolved as they're going to get. (We stored the resolved + name in the outgoing ping packet, so that same string just + came back to us.) + */ + /* If the name is an IP addr, try to resolve it. */ { int iip[4]; @@ -979,10 +1265,16 @@ getping(sonar_info *si, ping_info *pi, int ttl) if (4 == sscanf(name, " %d.%d.%d.%d %c", &iip[0], &iip[1], &iip[2], &iip[3], &c)) { - unsigned char ip[4]; + struct sockaddr_in iaddr; struct hostent *h; - ip[0] = iip[0]; ip[1] = iip[1]; ip[2] = iip[2]; ip[3] = iip[3]; - h = gethostbyaddr ((char *) ip, 4, AF_INET); + iaddr.sin_addr.s_addr = pack_addr (iip[0],iip[1],iip[2],iip[3]); + if (resolve_p) + h = gethostbyaddr ((const char *) &iaddr.sin_addr.s_addr, + sizeof(iaddr.sin_addr.s_addr), + AF_INET); + else + h = 0; + if (h && h->h_name && *h->h_name) { free (name); @@ -990,21 +1282,58 @@ getping(sonar_info *si, ping_info *pi, int ttl) } } } +# endif /* 0 */ /* Create the new Bogie and add it to the list we are building */ - if ((new = newBogie(name, 0, si->current, ttl)) == NULL) + if ((new = newBogie(name, 0, si->current, si->TTL)) == NULL) return bl; new->next = bl; bl = new; - /* Compute the round trip time */ + { + float msec = delta(then, &now) / 1000.0; + + if (times_p) + { + if (new->desc) free (new->desc); + new->desc = (char *) malloc (30); + if (msec > 99) sprintf (new->desc, " %.0f ms ", msec); + else if (msec > 9) sprintf (new->desc, " %.1f ms ", msec); + else if (msec > 1) sprintf (new->desc, " %.2f ms ", msec); + else sprintf (new->desc, " %.3f ms ", msec); + } + + if (debug_p && times_p) /* print ping-like stuff to stdout */ + { + struct sockaddr_in *iaddr = (struct sockaddr_in *) &from; + unsigned int a, b, c, d; + char ipstr[20]; + char *s = strdup (new->desc); + char *s2 = s, *s3 = s; + while (*s2 == ' ') s2++; + s3 = strchr (s2, ' '); + if (s3) *s3 = 0; + + unpack_addr (iaddr->sin_addr.s_addr, &a, &b, &c, &d); + sprintf (ipstr, "%d.%d.%d.%d", a, b, c, d); + + fprintf (stdout, + "%3d bytes from %28s: " + "icmp_seq=%-4d ttl=%d time=%s ms\n", + result, + name, + /*ipstr,*/ + ICMP_SEQ(icmph), si->TTL, s2); + free (s); + } - then = (struct timeval *) &packet[iphdrlen + - sizeof(struct icmp)]; - new->distance = delta(then, &now) / 100; - if (new->distance == 0) - new->distance = 2; /* HACK */ + /* Don't put anyone *too* close to the center of the screen. */ + msec += 0.6; + + new->distance = msec * 10; + } + } } /* Done */ @@ -1027,6 +1356,11 @@ static Bogie * ping(sonar_info *si, void *vpi) { + /* + * This tries to distribute the targets evely around the field of the + * sonar. + */ + ping_info *pi = (ping_info *) vpi; static ping_target *ptr = NULL; @@ -1052,7 +1386,7 @@ ping(sonar_info *si, void *vpi) /* Get the results */ - return getping(si, pi, TTL); + return getping(si, pi); } #endif /* HAVE_PING */ @@ -1088,14 +1422,10 @@ init_sim(void) sim_info *si; int i; - /* Seed the random number generator */ - - srand((int) time(NULL)); - /* Create the simulation info structure */ if ((si = (sim_info *) calloc(1, sizeof(sim_info))) == NULL) { - fprintf(stderr, "Out of memory\n"); + fprintf(stderr, "%s: Out of memory\n", progname); return NULL; } @@ -1105,7 +1435,7 @@ init_sim(void) if ((si->teamA = (sim_target *)calloc(si->numA, sizeof(sim_target))) == NULL) { free(si); - fprintf(stderr, "Out of Memory\n"); + fprintf(stderr, "%s: Out of Memory\n", progname); return NULL; } si->teamAID = get_string_resource("teamAName", "TeamAName"); @@ -1113,12 +1443,13 @@ init_sim(void) if ((si->teamA[i].name = (char *) malloc(strlen(si->teamAID) + 4)) == NULL) { free(si); - fprintf(stderr, "Out of Memory\n"); + fprintf(stderr, "%s: Out of Memory\n", progname); return NULL; } sprintf(si->teamA[i].name, "%s%03d", si->teamAID, i+1); - si->teamA[i].nexttick = (int) (90.0 * rand() / RAND_MAX); - si->teamA[i].nextdist = (int) (100.0 * rand() / RAND_MAX); + si->teamA[i].nexttick = random() % 90; + si->teamA[i].nextdist = random() % 100; + si->teamA[i].movedonsweep = -1; } /* Team B */ @@ -1127,7 +1458,7 @@ init_sim(void) if ((si->teamB = (sim_target *)calloc(si->numB, sizeof(sim_target))) == NULL) { free(si); - fprintf(stderr, "Out of Memory\n"); + fprintf(stderr, "%s: Out of Memory\n", progname); return NULL; } si->teamBID = get_string_resource("teamBName", "TeamBName"); @@ -1135,12 +1466,13 @@ init_sim(void) if ((si->teamB[i].name = (char *) malloc(strlen(si->teamBID) + 4)) == NULL) { free(si); - fprintf(stderr, "Out of Memory\n"); + fprintf(stderr, "%s: Out of Memory\n", progname); return NULL; } sprintf(si->teamB[i].name, "%s%03d", si->teamBID, i+1); - si->teamB[i].nexttick = (int) (90.0 * rand() / RAND_MAX); - si->teamB[i].nextdist = (int) (100.0 * rand() / RAND_MAX); + si->teamB[i].nexttick = random() % 90; + si->teamB[i].nextdist = random() % 100; + si->teamB[i].movedonsweep = -1; } /* Done */ @@ -1148,6 +1480,49 @@ init_sim(void) return si; } +/* + * Creates and returns a drawing mask for the scope: + * mask out anything outside of the disc. + */ +static Pixmap +scope_mask (Display *dpy, Window win, sonar_info *si) +{ + XGCValues gcv; + Pixmap mask = XCreatePixmap(dpy, win, si->width, si->height, 1); + GC gc = XCreateGC (dpy, mask, 0, &gcv); + XSetFunction (dpy, gc, GXclear); + XFillRectangle (dpy, mask, gc, 0, 0, si->width, si->height); + XSetFunction (dpy, gc, GXset); + XFillArc(dpy, mask, gc, si->minx, si->miny, + si->maxx - si->minx, si->maxy - si->miny, + 0, 360 * 64); + return mask; +} + + +static void +reshape (sonar_info *si) +{ + XWindowAttributes xgwa; + Pixmap mask; + XGetWindowAttributes(si->dpy, si->win, &xgwa); + si->width = xgwa.width; + si->height = xgwa.height; + si->centrex = si->width / 2; + si->centrey = si->height / 2; + si->maxx = si->centrex + MY_MIN(si->centrex, si->centrey) - 10; + si->minx = si->centrex - MY_MIN(si->centrex, si->centrey) + 10; + si->maxy = si->centrey + MY_MIN(si->centrex, si->centrey) - 10; + si->miny = si->centrey - MY_MIN(si->centrex, si->centrey) + 10; + si->radius = si->maxx - si->centrex; + + /* Install the clip mask... */ + mask = scope_mask (si->dpy, si->win, si); + XSetClipMask(si->dpy, si->text, mask); + XSetClipMask(si->dpy, si->erase, mask); + XFreePixmap (si->dpy, mask); /* it's been copied into the GCs */ +} + /* * Initialize the Sonar. * @@ -1175,7 +1550,7 @@ init_sonar(Display *dpy, Window win) /* Create the Sonar information structure */ if ((si = (sonar_info *) calloc(1, sizeof(sonar_info))) == NULL) { - fprintf(stderr, "Out of memory\n"); + fprintf(stderr, "%s: Out of memory\n", progname); return NULL; } @@ -1183,33 +1558,29 @@ init_sonar(Display *dpy, Window win) si->dpy = dpy; si->win = win; - si->visable = NULL; + si->visible = NULL; + XGetWindowAttributes(dpy, win, &xwa); si->cmap = xwa.colormap; - si->width = xwa.width; - si->height = xwa.height; - si->centrex = si->width / 2; - si->centrey = si->height / 2; - si->maxx = si->centrex + MIN(si->centrex, si->centrey) - 10; - si->minx = si->centrex - MIN(si->centrex, si->centrey) + 10; - si->maxy = si->centrey + MIN(si->centrex, si->centrey) - 10; - si->miny = si->centrey - MIN(si->centrex, si->centrey) + 10; - si->radius = si->maxx - si->centrex; + si->current = 0; + si->sweepnum = 0; /* Get the font */ if (((si->font = XLoadQueryFont(dpy, get_string_resource ("font", "Font"))) == NULL) && ((si->font = XLoadQueryFont(dpy, "fixed")) == NULL)) { - fprintf(stderr, "Can't load an appropriate font\n"); + fprintf(stderr, "%s: can't load an appropriate font\n", progname); return NULL; } /* Get the delay between animation frames */ si->delay = get_integer_resource ("delay", "Integer"); + if (si->delay < 0) si->delay = 0; + si->TTL = get_integer_resource("ttl", "TTL"); /* Create the Graphics Contexts that will be used to draw things */ @@ -1225,6 +1596,8 @@ init_sonar(Display *dpy, Window win) dpy, si->cmap); si->grid = XCreateGC (dpy, win, GCForeground, &gcv); + reshape (si); + /* Compute pixel values for fading text on the display */ XParseColor(dpy, si->cmap, @@ -1268,6 +1641,9 @@ init_sonar(Display *dpy, Window win) si->sweep_colors, &si->sweep_segs, False, True, False); + if (si->sweep_segs <= 0) + si->sweep_segs = 1; + /* Done */ return si; @@ -1283,9 +1659,8 @@ updateLocation(sim_target *t) int xdist, xtick; - t->movedlasttick = 1; - xtick = (int) (3.0 * rand() / RAND_MAX) - 1; - xdist = (int) (11.0 * rand() / RAND_MAX) - 5; + xtick = (int) (random() % 3) - 1; + xdist = (int) (random() % 11) - 5; if (((t->nexttick + xtick) < 90) && ((t->nexttick + xtick) >= 0)) t->nexttick += xtick; else @@ -1322,29 +1697,30 @@ simulator(sonar_info *si, void *vinfo) for (i = 0; i < info->numA; i++) { t = &info->teamA[i]; - if (!t->movedlasttick && (t->nexttick == (si->current * -1))) { - new = newBogie(strdup(t->name), t->nextdist, si->current, TTL); + if ((t->movedonsweep != si->sweepnum) && + (t->nexttick == (si->current * -1))) { + new = newBogie(strdup(t->name), t->nextdist, si->current, si->TTL); if (list != NULL) new->next = list; list = new; updateLocation(t); - } else - t->movedlasttick = 0; + t->movedonsweep = si->sweepnum; + } } /* Team B */ for (i = 0; i < info->numB; i++) { t = &info->teamB[i]; - if (!t->movedlasttick && (t->nexttick == (si->current * -1))) { - new = newBogie(strdup(t->name), t->nextdist, si->current, TTL); + if ((t->movedonsweep != si->sweepnum) && + (t->nexttick == (si->current * -1))) { + new = newBogie(strdup(t->name), t->nextdist, si->current, si->TTL); if (list != NULL) new->next = list; list = new; - t->movedlasttick = 1; updateLocation(t); - } else - t->movedlasttick = 0; + t->movedonsweep = si->sweepnum; + } } /* Done */ @@ -1365,7 +1741,7 @@ simulator(sonar_info *si, void *vinfo) */ static int -computeStringX(sonar_info *si, char *label, int x) +computeStringX(sonar_info *si, const char *label, int x) { int width = XTextWidth(si->font, label, strlen(label)); @@ -1383,14 +1759,12 @@ computeStringX(sonar_info *si, char *label, int x) * The y coordinate of the start of the label. */ -/* TODO: Add smarts to keep label in sonar screen */ - static int computeStringY(sonar_info *si, int y) { - int fheight = si->font->ascent + si->font->descent; - return y + 5 + fheight; + int fheight = si->font->ascent /* + si->font->descent */; + return y + fheight; } /* @@ -1407,8 +1781,8 @@ computeStringY(sonar_info *si, int y) */ static void -DrawBogie(sonar_info *si, int draw, char *name, int degrees, - int distance, int ttl, int age) +DrawBogie(sonar_info *si, int draw, const char *name, const char *desc, + int degrees, int distance, int ttl, int age) { /* Local Variables */ @@ -1421,7 +1795,8 @@ DrawBogie(sonar_info *si, int draw, char *name, int degrees, /* Compute the coordinates of the object */ - distance = (log((double) distance) / 10.0) * si->radius; + if (distance != 0) + distance = (log((double) distance) / 10.0) * si->radius; x = ox + ((double) distance * cos(4.0 * ((double) degrees)/57.29578)); y = oy - ((double) distance * sin(4.0 * ((double) degrees)/57.29578)); @@ -1450,9 +1825,21 @@ DrawBogie(sonar_info *si, int draw, char *name, int degrees, /* Draw (or erase) the Bogie */ XFillArc(si->dpy, si->win, gc, x, y, 5, 5, 0, 360 * 64); + + x += 3; /* move away from the dot */ + y += 7; + y = computeStringY(si, y); XDrawString(si->dpy, si->win, gc, - computeStringX(si, name, x), - computeStringY(si, y), name, strlen(name)); + computeStringX(si, name, x), y, + name, strlen(name)); + + if (desc && *desc) + { + y = computeStringY(si, y); + XDrawString(si->dpy, si->win, gc, + computeStringX(si, desc, x), y, + desc, strlen(desc)); + } } @@ -1531,13 +1918,13 @@ Sonar(sonar_info *si, Bogie *bl) Bogie *bp, *prev; int i; - /* Check for expired tagets and remove them from the visable list */ + /* Check for expired tagets and remove them from the visible list */ prev = NULL; - for (bp = si->visable; bp != NULL; bp = bp->next) { + for (bp = si->visible; bp != NULL; bp = (bp ? bp->next : 0)) { /* - * Remove it from the visable list if it's expired or we have + * Remove it from the visible list if it's expired or we have * a new target with the same name. */ @@ -1545,13 +1932,14 @@ Sonar(sonar_info *si, Bogie *bl) if (((bp->tick == si->current) && (++bp->age >= bp->ttl)) || (findNode(bl, bp->name) != NULL)) { - DrawBogie(si, 0, bp->name, bp->tick, + DrawBogie(si, 0, bp->name, bp->desc, bp->tick, bp->distance, bp->ttl, bp->age); if (prev == NULL) - si->visable = bp->next; + si->visible = bp->next; else prev->next = bp->next; freeBogie(bp); + bp = prev; } else prev = bp; } @@ -1577,19 +1965,20 @@ Sonar(sonar_info *si, Bogie *bl) (4 * 64)); } - /* Move the new targets to the visable list */ + /* Move the new targets to the visible list */ for (bp = bl; bp != (Bogie *) 0; bp = bl) { bl = bl->next; - bp->next = si->visable; - si->visable = bp; + bp->next = si->visible; + si->visible = bp; } - /* Draw the visable targets */ + /* Draw the visible targets */ - for (bp = si->visable; bp != NULL; bp = bp->next) { + for (bp = si->visible; bp != NULL; bp = bp->next) { if (bp->age < bp->ttl) /* grins */ - DrawBogie(si, 1, bp->name, bp->tick, bp->distance, bp->ttl,bp->age); + DrawBogie(si, 1, bp->name, bp->desc, + bp->tick, bp->distance, bp->ttl,bp->age); } /* Redraw the grid */ @@ -1597,6 +1986,143 @@ Sonar(sonar_info *si, Bogie *bl) drawGrid(si); } + +static ping_target * +parse_mode (Bool ping_works_p) +{ + char *source = get_string_resource ("ping", "Ping"); + char *token, *end; + char dummy; + + ping_target *hostlist = 0; + + if (!source) source = strdup(""); + + if (!*source || !strcmp (source, "default")) + { +# ifdef HAVE_PING + if (ping_works_p) /* if root or setuid, ping will work. */ + source = strdup("subnet/29,/etc/hosts"); + else +# endif + source = strdup("simulation"); + } + + token = source; + end = source + strlen(source); + while (token < end) + { + char *next; +# ifdef HAVE_PING + ping_target *new; + struct stat st; + unsigned int n0=0, n1=0, n2=0, n3=0, m=0; + char d; +# endif /* HAVE_PING */ + + for (next = token; + *next && + *next != ',' && *next != ' ' && *next != '\t' && *next != '\n'; + next++) + ; + *next = 0; + + + if (debug_p) + fprintf (stderr, "%s: parsing %s\n", progname, token); + + if (!strcmp (token, "simulation")) + return 0; + + if (!ping_works_p) + { + fprintf(stderr, + "%s: this program must be setuid to root for `ping mode' to work.\n" + " Running in `simulation mode' instead.\n", + progname); + return 0; + } + +#ifdef HAVE_PING + if ((4 == sscanf (token, "%u.%u.%u/%u %c", &n0,&n1,&n2, &m,&d)) || + (5 == sscanf (token, "%u.%u.%u.%u/%u %c", &n0,&n1,&n2,&n3,&m,&d))) + { + /* subnet: A.B.C.D/M + subnet: A.B.C/M + */ + unsigned long ip = pack_addr (n0, n1, n2, n3); + new = subnetHostsList(ip, m); + } + else if (4 == sscanf (token, "%u.%u.%u.%u %c", &n0, &n1, &n2, &n3, &d)) + { + /* IP: A.B.C.D + */ + new = newHost (token); + } + else if (!strcmp (token, "subnet")) + { + new = subnetHostsList(0, 24); + } + else if (1 == sscanf (token, "subnet/%u %c", &m, &dummy)) + { + new = subnetHostsList(0, m); + } + else if (*token == '.' || *token == '/' || !stat (token, &st)) + { + /* file name + */ + new = readPingHostsFile (token); + } + else + { + /* not an existant file - must be a host name + */ + new = newHost (token); + } + + if (new) + { + ping_target *nn = new; + while (nn && nn->next) + nn = nn->next; + nn->next = hostlist; + hostlist = new; + + sensor = ping; + } +#endif /* HAVE_PING */ + + token = next + 1; + while (token < end && + (*token == ',' || *token == ' ' || + *token == '\t' || *token == '\n')) + token++; + } + + return hostlist; +} + + +static void +handle_events (sonar_info *si) +{ + while (XPending (si->dpy)) + { + XEvent event; + XNextEvent (si->dpy, &event); + + if (event.xany.type == ConfigureNotify) + { + XClearWindow (si->dpy, si->win); + reshape (si); + } + + screenhack_handle_event (si->dpy, &event); + } +} + + + /* * Main screen saver hack. * @@ -1615,73 +2141,59 @@ screenhack(Display *dpy, Window win) struct timeval start, finish; Bogie *bl; long sleeptime; - char *mode; - /* - * Initialize - * Adding new sensors would involve supporting more modes other than - * ping and initiailizing the sensor in the same way. - */ + debug_p = get_boolean_resource ("debug", "Debug"); + resolve_p = get_boolean_resource ("resolve", "Resolve"); + times_p = get_boolean_resource ("showTimes", "ShowTimes"); - mode = get_string_resource("mode", "Mode"); + sensor = 0; +# ifdef HAVE_PING + sensor_info = (void *) init_ping(); +# else /* !HAVE_PING */ + sensor_info = 0; + parse_mode (0); /* just to check argument syntax */ +# endif /* !HAVE_PING */ - if (!mode || !*mode || !strcmp(mode, "default")) /* Pick a good default. */ + if (sensor == 0) { -#ifdef HAVE_PING - if (geteuid() == 0) /* we're root or setuid -- ping will work. */ - mode = "ping"; - else -#endif - mode = "simulation"; + sensor = simulator; + if ((sensor_info = (void *) init_sim()) == NULL) + exit(1); } -#ifdef HAVE_PING - if (strcmp(mode, "ping") == 0) { - sensor = ping; - if ((sensor_info = (void *) init_ping()) == (void *) 0) - { - fprintf (stderr, "%s: running in `simulation mode' instead.\n", - progname); - goto SIM; - } - } else -#endif /* HAVE_PING */ - if (strcmp(mode, "simulation") == 0) { -#ifdef HAVE_PING - SIM: -#endif - sensor = simulator; - if ((sensor_info = (void *) init_sim()) == NULL) - exit(1); - } else { - fprintf(stderr, "Unsupported Sonar mode: %s\n", mode); - fprintf(stderr, - "\tCurrently supported modes are `ping' and `simulation'\n"); - exit(1); - } if ((si = init_sonar(dpy, win)) == (sonar_info *) 0) exit(1); - - /* Sonar loop */ - TTL = get_integer_resource("ttl", "TTL"); + + /* Sonar loop */ while (1) { /* Call the sensor and display the results */ +# ifdef GETTIMEOFDAY_TWO_ARGS gettimeofday(&start, (struct timezone *) 0); +# else + gettimeofday(&start); +# endif bl = sensor(si, sensor_info); Sonar(si, bl); /* Set up and sleep for the next one */ si->current = (si->current - 1) % 90; + if (si->current == 0) + si->sweepnum++; XSync (dpy, False); +# ifdef GETTIMEOFDAY_TWO_ARGS gettimeofday(&finish, (struct timezone *) 0); +# else + gettimeofday(&finish); +# endif sleeptime = si->delay - delta(&start, &finish); - screenhack_handle_events (dpy); + handle_events (si); if (sleeptime > 0L) usleep(sleeptime); + } }