ipod.c 9.1 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1
/*
2
 * Copyright (c) 2000-2013 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
Leigh Stoller's avatar
Leigh Stoller committed
22 23
 */

24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 *  Send the magical ping of death ICMP type
 */


#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
37
#include <fcntl.h>
38 39 40 41 42 43 44 45 46 47
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <arpa/inet.h>

48 49 50 51 52
#define IPOD_ICMPTYPE	6
#define IPOD_ICMPCODE	6
#define IPOD_IPLEN	666
#define IPOD_IDLEN	32

53
int icmpid = 0;
54 55
static char myid[IPOD_IDLEN];
static int myidlen = 0;
56 57 58 59 60 61 62 63 64 65 66 67 68

u_short in_cksum(u_short *addr, int len);
void icmpmap_init();  /* For getting information */
void icmp_info(struct icmp *icmp, char *outbuf, int maxlen);

/*
 * We perform lookups on the hosts, and then store them in a chain
 * here.
 */

struct hostdesc {
	char *hostname;
	struct in_addr hostaddr;
69
	struct in_addr srcaddr;
70
	struct hostdesc *next;
71
	int s;
72
};
73 74
int use_hdrincl = 0;
int get_icmp_socket();
75 76 77 78 79 80 81 82

struct hostdesc *hostnames;
struct hostdesc *hosttail;

/*
 * Set up the list of hosts.  Return the count.
 */

83
int makehosts(char **hostlist, struct in_addr srcaddr)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
{
	int i;
	struct hostent *hp;
	struct in_addr tmpaddr;
	int hostcount = 0;
	
	for (i = 0; hostlist[i]; i++) {
#ifdef DEBUG
		printf("Resolving %s\n", hostlist[i]);
#endif
		if (!hostlist[i] ||
		    !hostlist[i][0] ||
		    strlen(hostlist[i]) > MAXHOSTNAMELEN) {
		    fprintf(stderr, "bad host entry, exiting\n");
		    exit(-1);
		}
		if (!inet_aton(hostlist[i], &tmpaddr)) {
			if ((hp = gethostbyname(hostlist[i])) == NULL) {
				/* Could not resolve it.  Skip it. */
				fprintf(stderr, "%s: unknown host\n",
					hostlist[i]);
				continue;
			}
			else {
				memcpy(&tmpaddr.s_addr,
				       hp->h_addr_list[0],
				       hp->h_length);
			}
		}

		/* The host has been resolved.  Put it in the chain */
		/* We want to stick it on the end. */
		if (hostnames == NULL) {
			hostnames = (struct hostdesc *)
				malloc(sizeof(*hostnames));
			if (hostnames == NULL) {
				perror("hostnames malloc failed");
				exit(-1);
			}
			hosttail = hostnames;
		} else {
			hosttail->next = (struct hostdesc *)
				malloc(sizeof(*hostnames));
			if (hosttail->next == NULL) {
				perror("hosttail->next malloc failed");
				exit(-1);
			}
			hosttail = hosttail->next;
		}
		hosttail->hostname = strdup(hostlist[i]);
		if (hosttail->hostname == NULL) {
			perror("strdup failed");
			exit(-1);
		}
		hosttail->hostaddr = tmpaddr;
		hosttail->next = NULL;
140 141
		hosttail->srcaddr = srcaddr;
		hosttail->s = get_icmp_socket();
142 143 144 145 146 147 148
		hostcount++;
	}
	return hostcount;
}

void usage(char *prog)
{
149 150
   fprintf(stderr,
	   "%s [ -s src ] [ -i hashfile ] [-h hash ] target [ target ... ]\n",
151
	   prog);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
}

/*
 * Set up a packet.  Returns the length of the ICMP portion.
 */

void initpacket(char *buf, int querytype, struct in_addr fromaddr)
{
   struct ip *ip = (struct ip *)buf;
   struct icmp *icmp = (struct icmp *)(ip + 1);

   /* things we customize */
   int icmplen = 0;

   ip->ip_src = fromaddr;	/* if 0,  have kernel fill in */
   ip->ip_v   = 4;		/* Always use ipv4 for now */
   ip->ip_hl  = sizeof *ip >> 2;
   ip->ip_tos = 0;
   ip->ip_id  = htons(4321);
   ip->ip_ttl = 255;
   ip->ip_p   = 1;
   ip->ip_sum = 0;                 /* kernel fills in */

   icmp->icmp_seq   = 1;
   icmp->icmp_cksum = 0;           /* We'll compute it later. */
   icmp->icmp_type  = querytype; 
178 179 180
   icmp->icmp_code  = IPOD_ICMPCODE;
   if (myidlen)
      memcpy(icmp->icmp_data, myid, myidlen);
181

182 183
   ip->ip_len = IPOD_IPLEN;
   icmplen = IPOD_IPLEN - sizeof(struct ip);
184 185 186 187 188 189 190
   icmp->icmp_cksum = in_cksum((u_short *)icmp, icmplen);
}

/*
 * Send all of the ICMP queries.
 */

191
void sendpings(int querytype, struct hostdesc *head, int delay,
192 193 194 195 196
	       struct in_addr fromaddr)
     
{
	char buf[1500];
	struct ip *ip = (struct ip *)buf;
197 198
	size_t len = IPOD_IPLEN;
	char *bufp = buf;
199 200 201

	bzero(buf, 1500);
	initpacket(buf, querytype, fromaddr);
202 203 204 205
	if (use_hdrincl == 0) {
	    bufp += sizeof(*ip);
	    len -= sizeof(*ip);
	}
206 207 208 209 210 211 212

	while (head != NULL) {
		int rc;
#ifdef DEBUG
		printf("pinging %s\n", head->hostname);
#endif
		ip->ip_dst.s_addr = head->hostaddr.s_addr;
213 214 215
		rc = send(head->s, bufp, len, 0);
                if (rc != len) {
			perror("send");
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
		}
		/* Don't flood small pipes. */
		if (delay)
			usleep(delay);
		head = head->next;
	}
}

/*
 * Handles our timeout for us.  Called by the signal handler
 * when we get a SIGARLM.
 */

void myexit(int whatsig)
{
	exit(0);
}

234 235 236 237 238 239 240
void sockaddr_from_ip(struct sockaddr_in *sin, struct in_addr ipaddr) {
    bzero(sin, sizeof(*sin));
    sin->sin_len = sizeof(*sin);
    sin->sin_family = AF_INET;
    sin->sin_addr = ipaddr;
}

241 242 243 244 245 246 247 248 249
/*
 * Open a raw socket for receiving ICMP.  Tell the kernel we want
 * to supply the IP headers.
 */

int get_icmp_socket()
{
	int s;
	int on = 1;
250
	struct sockaddr_in src, dst;
251 252 253 254
	if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
		perror("socket");
		exit(1);
	}
255
	if (use_hdrincl && setsockopt(s, IPPROTO_IP, IP_HDRINCL,
256 257 258 259
		       (const char *)&on, sizeof(on)) < 0) {
		perror("IP_HDRINCL");
		exit(1);
	}
260 261 262 263 264 265 266 267 268 269 270 271
	if (*(u_int32_t *)(&hosttail->srcaddr)) {
	    sockaddr_from_ip(&src, hosttail->srcaddr);
	    if (bind(s, (const struct sockaddr *)&src, sizeof(src)) < 0) {
		perror("bind");
		exit(1);
	    }
	}
	sockaddr_from_ip(&dst, hosttail->hostaddr);
	if (connect(s, (const struct sockaddr *)&dst, sizeof(dst)) < 0) {
	    perror("connect");
	    exit(1);
	}
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	return s;
}

int
main(int argc, char **argv)
{
   int s;

   char *progname;
   extern char *optarg;         /* getopt variable declarations */
   extern int optind;
   char ch;                     /* Holds the getopt result */
   int hostcount;
   int delay = 0;
   int querytype = ICMP_TSTAMP;
   struct in_addr fromaddr;
   int timeout = 5;  /* Default to 5 seconds */
289
   int identityfile;
290
   char *fromname = NULL;
291 292 293

   progname = argv[0];

294 295
   querytype = IPOD_ICMPTYPE;  /* the magical death packet number */

296
   while ((ch = getopt(argc, argv, "s:i:h:")) != -1)
297 298
      switch(ch)
      {
299 300 301
      case 's':
	  fromname = optarg;
	  break;
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
      case 'i':
	 if (optarg[0] == '-')
	    identityfile = 0;
	 else if ((identityfile = open(optarg, 0)) < 0)
	 {
	    perror(optarg);
	    exit(1);
	 }
	 myidlen = read(identityfile, myid, IPOD_IDLEN);
	 if (optarg[0] != '-')
	    close(identityfile);
         if (myidlen != IPOD_IDLEN)
	 {
	    fprintf(stderr, "%s: cannot read %d-byte identity\n",
		    optarg[0] != '-' ? optarg : "<stdin>", IPOD_IDLEN);
	    exit(2);
	 }
         break;
320 321 322 323 324 325 326 327 328
      case 'h':
	 myidlen = strlen(optarg);
         if (myidlen != IPOD_IDLEN) {
	    fprintf(stderr, "-h: identity hash must be %d bytes\n",
		    IPOD_IDLEN);
	    exit(2);
	 }
	 memcpy(myid, optarg, IPOD_IDLEN);
	 break;
329 330 331 332 333 334 335
      default:
         usage(progname);
	 exit(-1);
      }

   argc -= optind;
   argv += optind;
336 337 338 339 340 341
   if (!argv[0] || !strlen(argv[0])) 
   {
      usage(progname);
      exit(-1);
   }

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
   bzero(&fromaddr, sizeof(fromaddr));
   if (fromname) {
       if (!inet_aton(fromname, &fromaddr)) {
	   struct hostent *hp;
	   if ((hp = gethostbyname(fromname)) == NULL) {
	       /* Could not resolve it.  Skip it. */
	       fprintf(stderr, "%s: unknown host\n",
		       fromname);
	       exit(-1);
	   }
	   else {
	       memcpy(&fromaddr.s_addr,
		      hp->h_addr_list[0],
		      hp->h_length);
	   }
       }
   }
359
   hostcount = makehosts(argv, fromaddr);
360 361 362

   signal(SIGALRM, myexit);
   alarm(timeout);
363
   sendpings(querytype, hostnames, delay, fromaddr);
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
   exit(0);
}
   
/*
 * in_cksum --
 *	Checksum routine for Internet Protocol family headers (C Version)
 *      From FreeBSD's ping.c
 */

u_short
in_cksum(addr, len)
	u_short *addr;
	int len;
{
	register int nleft = len;
	register u_short *w = addr;
	register int sum = 0;
	u_short answer = 0;

	/*
	 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
	 * sequential 16 bit words to it, and at the end, fold back all the
	 * carry bits from the top 16 bits into the lower 16 bits.
	 */
	while (nleft > 1)  {
		sum += *w++;
		nleft -= 2;
	}

	/* mop up an odd byte, if necessary */
	if (nleft == 1) {
		*(u_char *)(&answer) = *(u_char *)w ;
		sum += answer;
	}

	/* add back carry outs from top 16 bits to low 16 bits */
	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
	sum += (sum >> 16);			/* add carry */
	answer = ~sum;				/* truncate to 16 bits */
	return(answer);
}