ping.c 10 KB

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