ping.c 11 KB

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