ping.c 10 KB

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