ipod.c 7.53 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2 3 4 5 6
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2000-2002 University of Utah and the Flux Group.
 * All rights reserved.
 */

7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  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>
20
#include <fcntl.h>
21 22 23 24 25 26 27 28 29 30
#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>

31 32 33 34 35
#define IPOD_ICMPTYPE	6
#define IPOD_ICMPCODE	6
#define IPOD_IPLEN	666
#define IPOD_IDLEN	32

36
int icmpid = 0;
37 38
static char myid[IPOD_IDLEN];
static int myidlen = 0;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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

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;
	struct hostdesc *next;
};

struct hostdesc *hostnames;
struct hostdesc *hosttail;

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

int makehosts(char **hostlist)
{
	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;
		hostcount++;
	}
	return hostcount;
}

void usage(char *prog)
{
126 127
   fprintf(stderr, "%s [ -s src ] [ -i identityfile ] target [ target ... ]\n",
	   prog);
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
}

/*
 * 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; 
154 155 156
   icmp->icmp_code  = IPOD_ICMPCODE;
   if (myidlen)
      memcpy(icmp->icmp_data, myid, myidlen);
157

158 159
   ip->ip_len = IPOD_IPLEN;
   icmplen = IPOD_IPLEN - sizeof(struct ip);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
   icmp->icmp_cksum = in_cksum((u_short *)icmp, icmplen);
}

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

void sendpings(int s, int querytype, struct hostdesc *head, int delay,
	       struct in_addr fromaddr)
     
{
	char buf[1500];
	struct ip *ip = (struct ip *)buf;
	struct sockaddr_in dst;

	bzero(buf, 1500);
	initpacket(buf, querytype, fromaddr);
	dst.sin_family = AF_INET;
#ifdef DA_HAS_SIN_LEN
	dst.sin_len = sizeof(dst);
#endif

	while (head != NULL) {
		int rc;
#ifdef DEBUG
		printf("pinging %s\n", head->hostname);
#endif
		ip->ip_dst.s_addr = head->hostaddr.s_addr;
		dst.sin_addr = head->hostaddr;
		rc = sendto(s, buf, ip->ip_len, 0,
			   (struct sockaddr *)&dst,
			   sizeof(dst));
                if (rc != ip->ip_len) {
			perror("sendto");
		}
		/* 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);
}

/*
 * 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;
	if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
		perror("socket");
		exit(1);
	}
	if (setsockopt(s, IPPROTO_IP, IP_HDRINCL,
		       (const char *)&on, sizeof(on)) < 0) {
		perror("IP_HDRINCL");
		exit(1);
	}
	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 */
247
   int identityfile;
248
   char *fromname = NULL;
249 250 251

   progname = argv[0];

252 253
   querytype = IPOD_ICMPTYPE;  /* the magical death packet number */

254
   while ((ch = getopt(argc, argv, "s:i:")) != -1)
255 256
      switch(ch)
      {
257 258 259
      case 's':
	  fromname = optarg;
	  break;
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
      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;
      default:
         usage(progname);
	 exit(-1);
      }

   argc -= optind;
   argv += optind;
285 286 287 288 289 290
   if (!argv[0] || !strlen(argv[0])) 
   {
      usage(progname);
      exit(-1);
   }

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
   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);
	   }
       }
   }
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
   hostcount = makehosts(argv);

   s = get_icmp_socket();

   signal(SIGALRM, myexit);
   alarm(timeout);
   sendpings(s, querytype, hostnames, delay, fromaddr);
   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);
}