ping.c 9.5 KB

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