X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=hacks%2Fglx%2Fsonar-icmp.c;h=dfa277d45f7bf89c8d2ce4fab7dcc684703063cc;hb=f8cf5ac7b2f53510f80a0eaf286a25298be17bfe;hp=b389f5e2ec700ce1c8d8aeedb0efcb867cf001d0;hpb=7b34ef992563d7bcbb64cc5597dc45fa24470b05;p=xscreensaver diff --git a/hacks/glx/sonar-icmp.c b/hacks/glx/sonar-icmp.c index b389f5e2..dfa277d4 100644 --- a/hacks/glx/sonar-icmp.c +++ b/hacks/glx/sonar-icmp.c @@ -1,4 +1,4 @@ -/* sonar, Copyright (c) 1998-2008 Jamie Zawinski and Stephen Martin +/* sonar, Copyright (c) 1998-2012 Jamie Zawinski and Stephen Martin * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that @@ -17,6 +17,28 @@ #undef usleep /* conflicts with unistd.h on OSX */ +#ifdef USE_IPHONE + /* Note: to get this to compile for iPhone, you need to fix Xcode! + The icmp headers exist for the simulator build environment, but + not for the real-device build environment. This appears to + just be an Apple bug, not intentional. + + xc=/Applications/Xcode.app/Contents + for path in /Developer/Platforms/iPhone*?/Developer/SDKs/?* \ + $xc/Developer/Platforms/iPhone*?/Developer/SDKs/?* ; do + for file in \ + /usr/include/netinet/ip.h \ + /usr/include/netinet/in_systm.h \ + /usr/include/netinet/ip_icmp.h \ + /usr/include/netinet/ip_var.h \ + /usr/include/netinet/udp.h + do + ln -s "$file" "$path$file" + done + done + */ +#endif + #if defined(HAVE_ICMP) || defined(HAVE_ICMPHDR) # include # include @@ -60,7 +82,7 @@ #ifndef HAVE_PING sonar_sensor_data * -init_ping (Display *dpy, const char *subnet, int timeout, +init_ping (Display *dpy, char **error_ret, const char *subnet, int timeout, Bool resolve_p, Bool times_p, Bool debug_p) { if (! (!subnet || !*subnet || !strcmp(subnet, "default"))) @@ -205,11 +227,30 @@ resolve_bogie_hostname (ping_data *pd, sonar_bogie *sb, Bool resolve_p) strlen (sb->name) >= 80) return 0; + /* .ssh/known_hosts sometimes contains weirdness like "[host]:port". + Ignore it. */ + if (strchr (sb->name, '[')) + { + if (pd->debug_p) + fprintf (stderr, "%s: ignoring bogus address \"%s\"\n", + progname, sb->name); + return 0; + } + + /* If the name contains a colon, it's probably IPv6. */ + if (strchr (sb->name, ':')) + { + if (pd->debug_p) + fprintf (stderr, "%s: ignoring ipv6 address \"%s\"\n", + progname, sb->name); + return 0; + } + hent = gethostbyname (sb->name); if (!hent) { if (pd->debug_p) - fprintf (stderr, "%s: could not resolve host: %s\n", + fprintf (stderr, "%s: could not resolve host: %s\n", progname, sb->name); return 0; } @@ -327,7 +368,7 @@ read_hosts_file (sonar_sensor_data *ssd, const char *filename) if (!fp) { char buf[1024]; - sprintf(buf, "%s: %s", progname, filename); + sprintf(buf, "%s: %s", progname, filename); #ifdef HAVE_COCOA if (pd->debug_p) /* on OSX don't syslog this */ #endif @@ -347,7 +388,6 @@ read_hosts_file (sonar_sensor_data *ssd, const char *filename) /* Get the name and address */ - name = addr = 0; if ((addr = strtok(buf, " ,;\t\n"))) name = strtok(0, " ,;\t\n"); else @@ -503,8 +543,19 @@ subnet_hosts (sonar_sensor_data *ssd, char **error_ret, /* Get our IP address and convert it to a string */ - if (! (hent = gethostbyname(hostname))) + hent = gethostbyname(hostname); + if (! hent) + { + strcat (hostname, ".local"); /* Necessary on iphone */ + hent = gethostbyname(hostname); + } + + if (! hent) { + /* Without being able to resolve localhost to an IP, we don't know + what our local subnet is. I don't know another way to find that, + short of running "ifconfig" and parsing the output... + */ sprintf(buf, "Unable to resolve\n" "local host \"%s\"", @@ -512,6 +563,7 @@ subnet_hosts (sonar_sensor_data *ssd, char **error_ret, *error_ret = strdup(buf); return 0; } + strcpy (address, inet_ntoa(*((struct in_addr *)hent->h_addr_list[0]))); /* Construct targets for all addresses in this subnet */ @@ -595,7 +647,6 @@ send_ping (ping_data *pd, const sonar_bogie *b) ping_bogie *pb = (ping_bogie *) b->closure; u_char *packet; struct ICMP *icmph; - int result; const char *token = "org.jwz.xscreensaver.sonar"; int pcktsiz = (sizeof(struct ICMP) + sizeof(struct timeval) + @@ -634,8 +685,8 @@ send_ping (ping_data *pd, const sonar_bogie *b) /* Send it */ - if ((result = sendto(pd->icmpsock, packet, pcktsiz, 0, - &pb->address, sizeof(pb->address))) + if (sendto(pd->icmpsock, packet, pcktsiz, 0, + &pb->address, sizeof(pb->address)) != pcktsiz) { #if 0 @@ -1037,7 +1088,7 @@ parse_mode (sonar_sensor_data *ssd, char **error_ret, if (new) { sonar_bogie *nn = new; - while (nn && nn->next) + while (nn->next) nn = nn->next; nn->next = hostlist; hostlist = new;