1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 126 127 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 154 155 156 157 158 159 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 247 248 249 250 251 252 253 254 255 256 257 258 259 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/*
* Ping service routines used by MacGate
*
* MacGate - User Space interface to Appletalk-IP decapsulation.
* - Node IP registration and routing daemon.
* Written By: Jay Schulist <Jay.Schulist@spacs.k12.wi.us>
* Copyright (C) 1997-1998 Jay Schulist
*
* Parts stolen from ping.c
*
* This software may be used and distributed according to the terms
* of the GNU Public License, incorporated herein by reference.
*/
/*
* Notes on Pingre operation.
* MacPinger (Pinger) provides route verification for MacGated.
* Each route that is added to MacRouteList MacGated spins a child
* this child is called MacPinger. Pinger is forked with the routing
* entry information. Immediately following fork() MacPinger sets
* an alarm for verification of this route based on Route->verify.
* The alarm goes off every Route->verify seconds. Upon waking the
* child (Pinger) sends Route->icmp_fail ICMP pings or untill a response
* is received. If a response is received then the routing entry is
* left in the list and the next alarm is set to go off in Route->verify
* seconds. If no ICMP response is received then the child terminates but
* first signaling MacGated (Parent) to remove the respective routing
* entry from the list. (As no ICMP ping response was received, either
* the Mac is down or defunct). This opens up that IP to be routed to
* another Mac.
*
* It is possible that many child can be spawned. This should not be a problem
* as processing for each MacPinger is minimal.
*
* I have used this method since it makes each route very configurable.
* Seperate timer, ping retries, etc can be used for each route. Possibly
* this may be going over-board but it guarantees expandability and a near
* 0 performace cost.
*/
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <syslog.h>
#define DEFDATALEN (64 - 8) /* default data length */
#define MAXIPLEN 60
#define MAXICMPLEN 76
#define MAXPACKET (65536 - 60 - 8)/* max packet size */
#define MAXWAIT 10 /* max seconds to wait for response */
#define NROUTES 9 /* number of record route slots */
#ifndef ICMP_MINLEN
#define ICMP_MINLEN 8
#endif
#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
#define SET(bit) (A(bit) |= B(bit))
#define CLR(bit) (A(bit) &= (~B(bit)))
#define TST(bit) (A(bit) & B(bit))
/*
* MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
* number of received sequence numbers we can keep track of. Change 128
* to 8192 for complete accuracy...
*/
#define MAX_DUP_CHK (8 * 128)
int mx_dup_ck = MAX_DUP_CHK;
char rcvd_tbl[MAX_DUP_CHK / 8];
struct sockaddr whereto; /* who to ping */
int datalen = DEFDATALEN;
int s; /* socket file descriptor */
u_char outpack[MAXPACKET];
static char *hostname;
static int ident; /* process id to identify our packets */
/* counters */
static long ntransmitted; /* sequence # for outbound packets = #sent */
/* timing */
static int timing; /* flag to do timing */
#define ICMP_MAXPACKET 150
struct pinger_rt
{
unsigned long ipaddr;
int verify;
int retries;
int timeout;
};
static const char *PingerName = "MacPinger v1.0";
int tries = 1;
struct pinger_rt rt;
/* protos */
static int in_cksum(u_short *addr, int len);
static void pinger(void);
void PingerRun(int crap);
void PingerDie(int crap);
extern char *in_ntoa(unsigned long in);
extern unsigned long in_aton(const char *str);
int main(int argc, char *argv[])
{
struct hostent *hp;
struct sockaddr_in *to;
struct protoent *proto;
int i, c;
int packlen;
u_char *datap, *packet;
char *target, hnamebuf[MAXHOSTNAMELEN];
struct sockaddr_in from;
register int cc;
size_t fromlen;
register struct icmphdr *icp;
struct iphdr *ip;
int hlen;
static char *null = NULL;
__environ = &null;
while((c=getopt(argc, argv, "ivrt")) != EOF)
{
switch(c)
{
/* Grab IP */
case 'i' :
rt.ipaddr = in_aton(argv[optind++]);
break;
/* Get time in seconds between checks */
case 'v' :
rt.verify = atoi(argv[optind++]);
break;
/* Get number of ICMP retires before route goes puff */
case 'r' :
rt.retries = atoi(argv[optind++]);
break;
/* Get timeout between ICMP request and reply */
case 't' :
rt.timeout = atoi(argv[optind++]);
break;
default :
syslog(LOG_ERR, "Bad Command Option(s)");
printf("%s: Bad Option\n", PingerName);
printf(" -i xxx.xxx.xxx.xxx (IP number)\n");
printf(" -v 60 (ICMP verify interval in seconds)\n");
printf(" -r 5 (ICMP retry value before route marked fail)\n");
printf(" -t 2 (Timeout between ICMP echo and reply)\n");
exit(1);
}
}
if(!(proto = getprotobyname("icmp")))
{
syslog(LOG_ERR, "unknown protocol icmp");
return -1;
}
if((s = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0)
{
syslog(LOG_ERR, "socket() failed");
return -1;
}
datap = &outpack[8 + sizeof(struct timeval)];
/* Set packet size */
datalen = ICMP_MAXPACKET;
if(datalen > MAXPACKET || datalen <= 0)
{
syslog(LOG_ERR, "Illegal packet size");
return -1;
}
/* Host to ping */
target = in_ntoa(rt.ipaddr);
memset(&whereto, 0, sizeof(struct sockaddr));
to = (struct sockaddr_in *)&whereto;
to->sin_family = AF_INET;
if (inet_aton(target, &to->sin_addr))
hostname = target;
else
{
hp = gethostbyname(target);
if(!hp)
{
syslog(LOG_ERR, "ping: unknown host %s\n", target);
return -1;
}
to->sin_family = hp->h_addrtype;
if (hp->h_length > (int)sizeof(to->sin_addr))
hp->h_length = sizeof(to->sin_addr);
memcpy(&to->sin_addr, hp->h_addr, hp->h_length);
(void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
hostname = hnamebuf;
}
if (datalen >= (int)sizeof(struct timeval)) /* can we time transfer */
timing = 1;
packlen = datalen + MAXIPLEN + MAXICMPLEN;
packet = malloc((u_int)packlen);
if(!packet)
{
syslog(LOG_ERR, "out of memory");
return -1;
}
/* Fill the packet */
for (i = 8; i < datalen; ++i)
*datap++ = i;
ident = getpid() & 0xFFFF;
signal(SIGINT, PingerDie);
signal(SIGTERM, PingerDie);
signal(SIGALRM, PingerRun);
PingerRun(0); /* start things going */
/* We sit and wait forever for a response */
for(;;)
{
fromlen = sizeof(from);
if ((cc = recvfrom(s, (char *)packet, packlen, 0,
(struct sockaddr *)&from, &fromlen)) < 0) {
if (errno == EINTR)
continue;
perror("ping: recvfrom");
continue;
}
/* Check the IP header */
ip = (struct iphdr *)(char *)packet;
hlen = ip->ihl << 2;
if (cc < datalen + ICMP_MINLEN)
continue; /* Bad packet */
/* Now the ICMP part */
icp = (struct icmphdr *)((char *)packet + hlen);
if(icp->type == ICMP_ECHOREPLY)
{
if (icp->un.echo.id != ident)
continue; /* 'Twas not our ECHO */
}
else
continue;
/* We recieved a packet, reset tries to 0 */
tries = 0;
}
/* Never get here */
return -1;
}
/*
* Send another packet. Exit if max tries used.
*/
void PingerRun(int crap)
{
/* Ran out of tries, route dead */
if(tries >= rt.retries)
PingerDie(0);
/* We recieved packet and were reset to 0 tries */
if(tries == 0)
sleep(rt.verify);
/* Run next verify */
pinger();
tries++;
signal(SIGALRM, PingerRun);
alarm(rt.timeout);
}
static void pinger(void)
{
register struct icmphdr *icp;
register int cc;
int i;
icp = (struct icmphdr *)outpack;
icp->type = ICMP_ECHO;
icp->code = 0;
icp->checksum = 0;
icp->un.echo.sequence = ntransmitted++;
icp->un.echo.id = ident; /* ID */
CLR(icp->un.echo.sequence % mx_dup_ck);
if (timing)
(void)gettimeofday((struct timeval *)&outpack[8],
(struct timezone *)NULL);
cc = datalen + 8; /* skips ICMP portion */
/* compute ICMP checksum here */
icp->checksum = in_cksum((u_short *)icp, cc);
i = sendto(s, (char *)outpack, cc, 0, &whereto,
sizeof(struct sockaddr));
if (i < 0 || i != cc) {
if (i < 0)
perror("ping: sendto");
(void)printf("ping: wrote %s %d chars, ret=%d\n",
hostname, cc, i);
}
}
void PingerDie(int crap)
{
exit(1);
}
/*
* in_cksum --
* Checksum routine for Internet Protocol family headers (C Version)
*/
static int in_cksum(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);
}
|