ping.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /* ping for ip v4 and v6 */
  10. #include <u.h>
  11. #include <libc.h>
  12. #include <ctype.h>
  13. #include <ip.h>
  14. #include <bio.h>
  15. #include <ndb.h>
  16. #include "icmp.h"
  17. enum {
  18. MAXMSG = 32,
  19. SLEEPMS = 1000,
  20. SECOND = 1000000000LL,
  21. };
  22. // dumb gcc enum.
  23. #define MINUTE (60LL*SECOND)
  24. typedef struct Req Req;
  25. struct Req
  26. {
  27. uint16_t seq; /* sequence number */
  28. int64_t time; /* time sent */
  29. int64_t rtt;
  30. int ttl;
  31. int replied;
  32. Req *next;
  33. };
  34. typedef struct {
  35. int version;
  36. char *net;
  37. int echocmd;
  38. int echoreply;
  39. unsigned iphdrsz;
  40. void (*prreply)(Req *r, void *v);
  41. void (*prlost)(uint16_t seq, void *v);
  42. } Proto;
  43. Req *first; /* request list */
  44. Req *last; /* ... */
  45. Lock listlock;
  46. char *argv0;
  47. int addresses;
  48. int debug;
  49. int done;
  50. int flood;
  51. int lostmsgs;
  52. int lostonly;
  53. int quiet;
  54. int rcvdmsgs;
  55. int rint;
  56. uint16_t firstseq;
  57. int64_t sum;
  58. int waittime = 5000;
  59. static char *network, *target;
  60. void lost(Req*, void*);
  61. void reply(Req*, void*);
  62. static void
  63. usage(void)
  64. {
  65. fprint(2,
  66. "usage: %s [-6alq] [-s msgsize] [-i millisecs] [-n #pings] dest\n",
  67. argv0);
  68. exits("usage");
  69. }
  70. static void
  71. catch(void *a, char *msg)
  72. {
  73. USED(a);
  74. if(strstr(msg, "alarm"))
  75. noted(NCONT);
  76. else if(strstr(msg, "die"))
  77. exits("errors");
  78. else
  79. noted(NDFLT);
  80. }
  81. static void
  82. prlost4(uint16_t seq, void *v)
  83. {
  84. Ip4hdr *ip4 = v;
  85. print("lost %u: %V -> %V\n", seq, ip4->src, ip4->dst);
  86. }
  87. static void
  88. prlost6(uint16_t seq, void *v)
  89. {
  90. Ip6hdr *ip6 = v;
  91. print("lost %u: %I -> %I\n", seq, ip6->src, ip6->dst);
  92. }
  93. static void
  94. prreply4(Req *r, void *v)
  95. {
  96. Ip4hdr *ip4 = v;
  97. print("%u: %V -> %V rtt %lld µs, avg rtt %lld µs, ttl = %d\n",
  98. r->seq - firstseq, ip4->src, ip4->dst, r->rtt, sum/rcvdmsgs,
  99. r->ttl);
  100. }
  101. static void
  102. prreply6(Req *r, void *v)
  103. {
  104. Ip6hdr *ip6 = v;
  105. print("%u: %I -> %I rtt %lld µs, avg rtt %lld µs, ttl = %d\n",
  106. r->seq - firstseq, ip6->src, ip6->dst, r->rtt, sum/rcvdmsgs,
  107. r->ttl);
  108. }
  109. static Proto v4pr = {
  110. 4, "icmp",
  111. EchoRequest, EchoReply,
  112. IPV4HDR_LEN,
  113. prreply4, prlost4,
  114. };
  115. static Proto v6pr = {
  116. 6, "icmpv6",
  117. EchoRequestV6, EchoReplyV6,
  118. IPV6HDR_LEN,
  119. prreply6, prlost6,
  120. };
  121. static Proto *proto = &v4pr;
  122. Icmphdr *
  123. geticmp(void *v)
  124. {
  125. char *p = v;
  126. return (Icmphdr *)(p + proto->iphdrsz);
  127. }
  128. void
  129. clean(uint16_t seq, int64_t now, void *v)
  130. {
  131. int ttl;
  132. Req **l, *r;
  133. ttl = 0;
  134. if (v) {
  135. if (proto->version == 4)
  136. ttl = ((Ip4hdr *)v)->ttl;
  137. else
  138. ttl = ((Ip6hdr *)v)->ttl;
  139. }
  140. lock(&listlock);
  141. last = nil;
  142. for(l = &first; *l; ){
  143. r = *l;
  144. if(v && r->seq == seq){
  145. r->rtt = now-r->time;
  146. r->ttl = ttl;
  147. reply(r, v);
  148. }
  149. if(now-r->time > MINUTE){
  150. *l = r->next;
  151. r->rtt = now-r->time;
  152. if(v)
  153. r->ttl = ttl;
  154. if(r->replied == 0)
  155. lost(r, v);
  156. free(r);
  157. }else{
  158. last = r;
  159. l = &r->next;
  160. }
  161. }
  162. unlock(&listlock);
  163. }
  164. static uint8_t loopbacknet[IPaddrlen] = {
  165. 0, 0, 0, 0,
  166. 0, 0, 0, 0,
  167. 0, 0, 0xff, 0xff,
  168. 127, 0, 0, 0
  169. };
  170. static uint8_t loopbackmask[IPaddrlen] = {
  171. 0xff, 0xff, 0xff, 0xff,
  172. 0xff, 0xff, 0xff, 0xff,
  173. 0xff, 0xff, 0xff, 0xff,
  174. 0xff, 0, 0, 0
  175. };
  176. /*
  177. * find first ip addr suitable for proto and
  178. * that isn't the friggin loopback address.
  179. * deprecate link-local and multicast addresses.
  180. */
  181. static int
  182. myipvnaddr(uint8_t *ip, Proto *proto, char *net)
  183. {
  184. int ipisv4, wantv4;
  185. Ipifc *nifc;
  186. Iplifc *lifc;
  187. uint8_t mynet[IPaddrlen], linklocal[IPaddrlen];
  188. static Ipifc *ifc;
  189. ipmove(linklocal, IPnoaddr);
  190. wantv4 = proto->version == 4;
  191. ifc = readipifc(net, ifc, -1);
  192. for(nifc = ifc; nifc; nifc = nifc->next)
  193. for(lifc = nifc->lifc; lifc; lifc = lifc->next){
  194. maskip(lifc->ip, loopbackmask, mynet);
  195. if(ipcmp(mynet, loopbacknet) == 0)
  196. continue;
  197. if(ISIPV6MCAST(lifc->ip) || ISIPV6LINKLOCAL(lifc->ip)) {
  198. ipmove(linklocal, lifc->ip);
  199. continue;
  200. }
  201. ipisv4 = isv4(lifc->ip) != 0;
  202. if(ipcmp(lifc->ip, IPnoaddr) != 0 && wantv4 == ipisv4){
  203. ipmove(ip, lifc->ip);
  204. return 0;
  205. }
  206. }
  207. /* no global unicast addrs found, fall back to link-local, if any */
  208. ipmove(ip, linklocal);
  209. return ipcmp(ip, IPnoaddr) == 0? -1: 0;
  210. }
  211. void
  212. sender(int fd, int msglen, int interval, int n)
  213. {
  214. int i, extra;
  215. uint16_t seq;
  216. char buf[64*1024+512];
  217. uint8_t me[IPaddrlen], mev4[IPv4addrlen];
  218. Icmphdr *icmp;
  219. Req *r;
  220. srand(time(0));
  221. firstseq = seq = rand();
  222. icmp = geticmp(buf);
  223. memset(buf, 0, proto->iphdrsz + ICMP_HDRSIZE);
  224. for(i = proto->iphdrsz + ICMP_HDRSIZE; i < msglen; i++)
  225. buf[i] = i;
  226. icmp->type = proto->echocmd;
  227. icmp->code = 0;
  228. /* arguably the kernel should fill in the right src addr. */
  229. myipvnaddr(me, proto, network);
  230. if (proto->version == 4) {
  231. v6tov4(mev4, me);
  232. memmove(((Ip4hdr *)buf)->src, mev4, IPv4addrlen);
  233. } else
  234. ipmove(((Ip6hdr *)buf)->src, me);
  235. if (addresses)
  236. print("\t%I -> %s\n", me, target);
  237. if(rint != 0 && interval <= 0)
  238. rint = 0;
  239. extra = 0;
  240. for(i = 0; i < n; i++){
  241. if(i != 0){
  242. if(rint != 0)
  243. extra = nrand(interval);
  244. sleep(interval + extra);
  245. }
  246. r = malloc(sizeof *r);
  247. if (r == nil)
  248. continue;
  249. hnputs(icmp->seq, seq);
  250. r->seq = seq;
  251. r->next = nil;
  252. r->replied = 0;
  253. r->time = nsec(); /* avoid early free in reply! */
  254. lock(&listlock);
  255. if(first == nil)
  256. first = r;
  257. else
  258. last->next = r;
  259. last = r;
  260. unlock(&listlock);
  261. r->time = nsec();
  262. if(write(fd, buf, msglen) < msglen){
  263. fprint(2, "%s: write failed: %r\n", argv0);
  264. return;
  265. }
  266. seq++;
  267. }
  268. done = 1;
  269. }
  270. void
  271. rcvr(int fd, int msglen, int interval, int nmsg)
  272. {
  273. int i, n, munged;
  274. uint16_t x;
  275. int64_t now;
  276. uint8_t buf[64*1024+512];
  277. Icmphdr *icmp;
  278. Req *r;
  279. sum = 0;
  280. while(lostmsgs+rcvdmsgs < nmsg){
  281. alarm((nmsg-lostmsgs-rcvdmsgs)*interval+waittime);
  282. n = read(fd, buf, sizeof buf);
  283. alarm(0);
  284. now = nsec();
  285. if(n <= 0){ /* read interrupted - time to go */
  286. clean(0, now+MINUTE, nil);
  287. continue;
  288. }
  289. if(n < msglen){
  290. print("bad len %d/%d\n", n, msglen);
  291. continue;
  292. }
  293. icmp = geticmp(buf);
  294. munged = 0;
  295. for(i = proto->iphdrsz + ICMP_HDRSIZE; i < msglen; i++)
  296. if(buf[i] != (uint8_t)i)
  297. munged++;
  298. if(munged)
  299. print("corrupted reply\n");
  300. x = nhgets(icmp->seq);
  301. if(icmp->type != proto->echoreply || icmp->code != 0) {
  302. print("bad type/code/sequence %d/%d/%d (want %d/%d/%d)\n",
  303. icmp->type, icmp->code, x,
  304. proto->echoreply, 0, x);
  305. continue;
  306. }
  307. clean(x, now, buf);
  308. }
  309. lock(&listlock);
  310. for(r = first; r; r = r->next)
  311. if(r->replied == 0)
  312. lostmsgs++;
  313. unlock(&listlock);
  314. if(!quiet && lostmsgs)
  315. print("%d out of %d messages lost\n", lostmsgs,
  316. lostmsgs+rcvdmsgs);
  317. }
  318. static int
  319. isdottedquad(char *name)
  320. {
  321. int dot = 0, digit = 0;
  322. for (; *name != '\0'; name++)
  323. if (*name == '.')
  324. dot++;
  325. else if (isdigit(*name))
  326. digit++;
  327. else
  328. return 0;
  329. return dot && digit;
  330. }
  331. static int
  332. isv6lit(char *name)
  333. {
  334. int colon = 0, hex = 0;
  335. for (; *name != '\0'; name++)
  336. if (*name == ':')
  337. colon++;
  338. else if (isxdigit(*name))
  339. hex++;
  340. else
  341. return 0;
  342. return colon;
  343. }
  344. /* from /sys/src/libc/9sys/dial.c */
  345. enum
  346. {
  347. Maxstring = 128,
  348. Maxpath = 256,
  349. };
  350. typedef struct DS DS;
  351. struct DS {
  352. /* dist string */
  353. char buf[Maxstring];
  354. char *netdir;
  355. char *proto;
  356. char *rem;
  357. /* other args */
  358. char *local;
  359. char *dir;
  360. int *cfdp;
  361. };
  362. /*
  363. * parse a dial string
  364. */
  365. static void
  366. _dial_string_parse(char *str, DS *ds)
  367. {
  368. char *p, *p2;
  369. strncpy(ds->buf, str, Maxstring);
  370. ds->buf[Maxstring-1] = 0;
  371. p = strchr(ds->buf, '!');
  372. if(p == 0) {
  373. ds->netdir = 0;
  374. ds->proto = "net";
  375. ds->rem = ds->buf;
  376. } else {
  377. if(*ds->buf != '/' && *ds->buf != '#'){
  378. ds->netdir = 0;
  379. ds->proto = ds->buf;
  380. } else {
  381. for(p2 = p; *p2 != '/'; p2--)
  382. ;
  383. *p2++ = 0;
  384. ds->netdir = ds->buf;
  385. ds->proto = p2;
  386. }
  387. *p = 0;
  388. ds->rem = p + 1;
  389. }
  390. }
  391. /* end excerpt from /sys/src/libc/9sys/dial.c */
  392. /* side effect: sets network & target */
  393. static int
  394. isv4name(char *name)
  395. {
  396. int r = 1;
  397. char *root, *ip, *pr;
  398. DS ds;
  399. _dial_string_parse(name, &ds);
  400. /* cope with leading /net.alt/icmp! and the like */
  401. root = nil;
  402. if (ds.netdir != nil) {
  403. pr = strrchr(ds.netdir, '/');
  404. if (pr == nil)
  405. pr = ds.netdir;
  406. else {
  407. *pr++ = '\0';
  408. root = ds.netdir;
  409. network = strdup(root);
  410. }
  411. if (strcmp(pr, v4pr.net) == 0)
  412. return 1;
  413. if (strcmp(pr, v6pr.net) == 0)
  414. return 0;
  415. }
  416. /* if it's a literal, it's obvious from syntax which proto it is */
  417. free(target);
  418. target = strdup(ds.rem);
  419. if (isdottedquad(ds.rem))
  420. return 1;
  421. else if (isv6lit(ds.rem))
  422. return 0;
  423. /* map name to ip and look at its syntax */
  424. ip = csgetvalue(root, "sys", ds.rem, "ip", nil);
  425. if (ip == nil)
  426. ip = csgetvalue(root, "dom", ds.rem, "ip", nil);
  427. if (ip == nil)
  428. ip = csgetvalue(root, "sys", ds.rem, "ipv6", nil);
  429. if (ip == nil)
  430. ip = csgetvalue(root, "dom", ds.rem, "ipv6", nil);
  431. if (ip != nil)
  432. r = isv4name(ip);
  433. free(ip);
  434. return r;
  435. }
  436. void
  437. main(int argc, char **argv)
  438. {
  439. int fd, msglen, interval, nmsg;
  440. char *ds;
  441. nsec(); /* make sure time file is already open */
  442. fmtinstall('V', eipfmt);
  443. fmtinstall('I', eipfmt);
  444. msglen = interval = 0;
  445. nmsg = MAXMSG;
  446. ARGBEGIN {
  447. case '6':
  448. proto = &v6pr;
  449. break;
  450. case 'a':
  451. addresses = 1;
  452. break;
  453. case 'd':
  454. debug++;
  455. break;
  456. case 'f':
  457. flood = 1;
  458. break;
  459. case 'i':
  460. interval = atoi(EARGF(usage()));
  461. if(interval < 0)
  462. usage();
  463. break;
  464. case 'l':
  465. lostonly++;
  466. break;
  467. case 'n':
  468. nmsg = atoi(EARGF(usage()));
  469. if(nmsg < 0)
  470. usage();
  471. break;
  472. case 'q':
  473. quiet = 1;
  474. break;
  475. case 'r':
  476. rint = 1;
  477. break;
  478. case 's':
  479. msglen = atoi(EARGF(usage()));
  480. break;
  481. case 'w':
  482. waittime = atoi(EARGF(usage()));
  483. if(waittime < 0)
  484. usage();
  485. break;
  486. default:
  487. usage();
  488. break;
  489. } ARGEND;
  490. if(msglen < proto->iphdrsz + ICMP_HDRSIZE)
  491. msglen = proto->iphdrsz + ICMP_HDRSIZE;
  492. if(msglen < 64)
  493. msglen = 64;
  494. if(msglen >= 64*1024)
  495. msglen = 64*1024-1;
  496. if(interval <= 0 && !flood)
  497. interval = SLEEPMS;
  498. if(argc < 1)
  499. usage();
  500. notify(catch);
  501. if (!isv4name(argv[0]))
  502. proto = &v6pr;
  503. ds = netmkaddr(argv[0], proto->net, "1");
  504. fd = dial(ds, 0, 0, 0);
  505. if(fd < 0){
  506. fprint(2, "%s: couldn't dial %s: %r\n", argv0, ds);
  507. exits("dialing");
  508. }
  509. if (!quiet)
  510. print("sending %d %d byte messages %d ms apart to %s\n",
  511. nmsg, msglen, interval, ds);
  512. switch(rfork(RFPROC|RFMEM|RFFDG)){
  513. case -1:
  514. fprint(2, "%s: can't fork: %r\n", argv0);
  515. /* fallthrough */
  516. case 0:
  517. rcvr(fd, msglen, interval, nmsg);
  518. exits(0);
  519. default:
  520. sender(fd, msglen, interval, nmsg);
  521. wait();
  522. exits(lostmsgs ? "lost messages" : "");
  523. }
  524. }
  525. void
  526. reply(Req *r, void *v)
  527. {
  528. r->rtt /= 1000LL;
  529. sum += r->rtt;
  530. if(!r->replied)
  531. rcvdmsgs++;
  532. if(!quiet && !lostonly)
  533. if(addresses)
  534. (*proto->prreply)(r, v);
  535. else
  536. print("%u: rtt %lld µs, avg rtt %lld µs, ttl = %d\n",
  537. r->seq - firstseq, r->rtt, sum/rcvdmsgs, r->ttl);
  538. r->replied = 1;
  539. }
  540. void
  541. lost(Req *r, void *v)
  542. {
  543. if(!quiet)
  544. if(addresses && v != nil)
  545. (*proto->prlost)(r->seq - firstseq, v);
  546. else
  547. print("lost %u\n", r->seq - firstseq);
  548. lostmsgs++;
  549. }