netcmd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /* Network commands: netinfo, netrecv, netsend, netip, ping (responder) and
  2. * netping (active ARP + ICMP echo), over the netdev NIC layer. */
  3. #include "io.h"
  4. #include "timer.h"
  5. #include "netdev.h"
  6. #include "net.h"
  7. #include "netlog.h"
  8. /* Shared frame buffers (file scope, not stack: the 4 KB process stack can't
  9. hold a full-size frame, and keeping them out of BSS-as-much-as-possible
  10. avoids bloating the kernel past its 0x40000 limit). Max Ethernet frame is
  11. 1518 bytes (14 header + 1500 payload + 4 VLAN). */
  12. #define NET_FRAME_MAX 1518
  13. static unsigned char net_rx_buf[NET_FRAME_MAX];
  14. static unsigned char net_reply_buf[NET_FRAME_MAX];
  15. static void put_hex2(unsigned char v)
  16. {
  17. static const char hex[] = "0123456789ABCDEF";
  18. io_putchar(hex[(v >> 4) & 0xF]);
  19. io_putchar(hex[v & 0xF]);
  20. }
  21. static void put_dec(unsigned int v)
  22. {
  23. char b[12];
  24. int i = 0;
  25. if (v == 0) {
  26. io_putchar('0');
  27. return;
  28. }
  29. while (v) {
  30. b[i++] = (char)('0' + (v % 10));
  31. v /= 10;
  32. }
  33. while (i)
  34. io_putchar(b[--i]);
  35. }
  36. static void put_mac(const unsigned char *m)
  37. {
  38. unsigned int i;
  39. for (i = 0; i < 6; i++) {
  40. if (i)
  41. io_putchar(':');
  42. put_hex2(m[i]);
  43. }
  44. }
  45. static void put_ip(const unsigned char *ip)
  46. {
  47. unsigned int i;
  48. for (i = 0; i < 4; i++) {
  49. if (i)
  50. io_putchar('.');
  51. put_dec(ip[i]);
  52. }
  53. }
  54. static int ip_eq(const unsigned char *a, const unsigned char *b)
  55. {
  56. return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
  57. }
  58. /* ---- netinfo ---- */
  59. void program_netinfo_main(void)
  60. {
  61. const struct netdev *nd = netdev_active();
  62. if (!nd) {
  63. io_putstr("netinfo: no NIC found\n");
  64. return;
  65. }
  66. io_putstr("netdev: ");
  67. io_putstr(nd->name);
  68. io_putstr(" (");
  69. io_putstr(nd->chip);
  70. io_putstr(")\n");
  71. nd->print_info();
  72. io_putstr("ip=");
  73. net_print_ip();
  74. io_putchar('\n');
  75. }
  76. /* ---- netregs: driver register dump ---- */
  77. void program_netregs_main(void)
  78. {
  79. const struct netdev *nd = netdev_active();
  80. if (!nd) {
  81. io_putstr("netregs: no NIC found\n");
  82. return;
  83. }
  84. if (!nd->dump_regs) {
  85. io_putstr("netregs: no register dump for ");
  86. io_putstr(nd->name);
  87. io_putchar('\n');
  88. return;
  89. }
  90. nd->dump_regs();
  91. }
  92. /* ---- netrecv ---- */
  93. void program_netrecv_main(void)
  94. {
  95. const struct netdev *nd = netdev_active();
  96. unsigned int deadline, count = 0;
  97. if (!nd) {
  98. io_putstr("netrecv: no NIC found\n");
  99. return;
  100. }
  101. io_putstr("netrecv: listening (8s)...\n");
  102. netdev_acquire();
  103. deadline = get_ticks_ms() + 8000;
  104. while (get_ticks_ms() < deadline) {
  105. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  106. unsigned int i;
  107. if (len < 0)
  108. continue;
  109. net_decode_frame(net_rx_buf, (unsigned int)len);
  110. io_putchar('\n');
  111. /* Raw hex dump for closer inspection. */
  112. for (i = 0; i < (unsigned int)len; i++) {
  113. put_hex2(net_rx_buf[i]);
  114. io_putchar(' ');
  115. if ((i & 15) == 15)
  116. io_putchar('\n');
  117. }
  118. if (((unsigned int)len & 15) != 0)
  119. io_putchar('\n');
  120. if (++count >= 32)
  121. break;
  122. }
  123. io_putstr("netrecv: ");
  124. put_dec(count);
  125. io_putstr(" frame(s)\n");
  126. netdev_release();
  127. }
  128. /* ---- netsend ---- */
  129. void program_netsend_main(void)
  130. {
  131. const struct netdev *nd = netdev_active();
  132. unsigned char frame[64];
  133. unsigned char mac[6];
  134. unsigned int i;
  135. if (!nd) {
  136. io_putstr("netsend: no NIC found\n");
  137. return;
  138. }
  139. nd->get_mac(mac);
  140. /* Broadcast test frame: dst=ff:ff:ff:ff:ff:ff, src=our MAC,
  141. ethertype 0x88B5 (local experimental), payload "NEBS". */
  142. for (i = 0; i < 6; i++)
  143. frame[i] = 0xFF;
  144. for (i = 0; i < 6; i++)
  145. frame[6 + i] = mac[i];
  146. frame[12] = 0x88;
  147. frame[13] = 0xB5;
  148. frame[14] = 'N';
  149. frame[15] = 'E';
  150. frame[16] = 'B';
  151. frame[17] = 'S';
  152. for (i = 18; i < 64; i++)
  153. frame[i] = 0x00;
  154. if (nd->tx(frame, sizeof(frame)) != 0) {
  155. io_putstr("netsend: transmit failed\n");
  156. return;
  157. }
  158. io_putstr("netsend: sent 64-byte broadcast frame\n");
  159. }
  160. /* ---- netip ---- */
  161. void program_netip_main(const char *arg)
  162. {
  163. unsigned char ip[4];
  164. if (arg[0] == '\0') {
  165. net_print_ip();
  166. io_putchar('\n');
  167. return;
  168. }
  169. if (net_parse_ip(arg, ip) != 0) {
  170. io_putstr("netip: usage netip <a.b.c.d>\n");
  171. return;
  172. }
  173. net_set_ip(ip);
  174. io_putstr("ip set to ");
  175. net_print_ip();
  176. io_putchar('\n');
  177. }
  178. /* ---- ping: answer ARP/ICMP for our IP (responder mode) ---- */
  179. void program_ping_main(void)
  180. {
  181. const struct netdev *nd = netdev_active();
  182. unsigned int deadline, count = 0;
  183. if (!nd) {
  184. io_putstr("ping: no NIC found\n");
  185. return;
  186. }
  187. io_putstr("ping: answering ARP/ICMP for 30s (ip=");
  188. net_print_ip();
  189. io_putstr(")...\n");
  190. netdev_acquire();
  191. deadline = get_ticks_ms() + 30000;
  192. while (get_ticks_ms() < deadline) {
  193. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  194. int rlen;
  195. if (len < 0)
  196. continue;
  197. rlen = net_build_reply(net_rx_buf, (unsigned int)len,
  198. net_reply_buf, NET_FRAME_MAX);
  199. if (rlen > 0) {
  200. if (nd->tx(net_reply_buf, (unsigned int)rlen) == 0) {
  201. io_putstr("reply: ");
  202. net_decode_frame(net_reply_buf, (unsigned int)rlen);
  203. io_putchar('\n');
  204. count++;
  205. }
  206. }
  207. }
  208. io_putstr("ping: done (");
  209. put_dec(count);
  210. io_putstr(" replies)\n");
  211. netdev_release();
  212. }
  213. /* ---- netlog: send a text line to the LAN as a UDP datagram ---- */
  214. void program_netlog_main(const char *arg)
  215. {
  216. static const unsigned char bcast_mac[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  217. unsigned char dst[4];
  218. unsigned int len = 0;
  219. if (arg[0] == '\0') {
  220. io_putstr("netlog: usage netlog <text>\n");
  221. return;
  222. }
  223. while (arg[len] && len < NET_UDP_MAX)
  224. len++;
  225. netlog_get_host(dst);
  226. if (net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT,
  227. arg, len) != 0) {
  228. io_putstr("netlog: send failed\n");
  229. return;
  230. }
  231. io_putstr("netlog: sent ");
  232. put_dec(len);
  233. io_putstr(" bytes to UDP port ");
  234. put_dec(NETLOG_PORT);
  235. io_putchar('\n');
  236. }
  237. /* ---- netconsole: the debug-output mirror (see netlog.c) ---- */
  238. static int arg_is(const char *arg, const char *word)
  239. {
  240. unsigned int i = 0;
  241. while (arg[i] && word[i] && arg[i] == word[i])
  242. i++;
  243. return arg[i] == '\0' && word[i] == '\0';
  244. }
  245. void program_netconsole_main(const char *arg)
  246. {
  247. unsigned char ip[4];
  248. if (arg[0] == '\0') {
  249. io_putstr("netconsole: mirror ");
  250. io_putstr(netlog_get_enabled() ? "on" : "off");
  251. io_putstr(", host ");
  252. netlog_get_host(ip);
  253. put_ip(ip);
  254. io_putstr(", udp port ");
  255. put_dec(NETLOG_PORT);
  256. io_putchar('\n');
  257. io_putstr("netconsole: usage netconsole on|off|host <a.b.c.d>\n");
  258. return;
  259. }
  260. if (arg_is(arg, "on")) {
  261. netlog_set_enabled(1);
  262. io_putstr("netconsole: mirror on\n");
  263. return;
  264. }
  265. if (arg_is(arg, "off")) {
  266. netlog_set_enabled(0);
  267. io_putstr("netconsole: mirror off\n");
  268. return;
  269. }
  270. if (arg[0] == 'h' && arg[1] == 'o' && arg[2] == 's' && arg[3] == 't') {
  271. const char *p = arg + 4;
  272. while (*p == ' ')
  273. p++;
  274. if (*p == '\0' || net_parse_ip(p, ip) != 0) {
  275. io_putstr("netconsole: usage netconsole host <a.b.c.d>\n");
  276. return;
  277. }
  278. netlog_set_host(ip);
  279. io_putstr("netconsole: host ");
  280. put_ip(ip);
  281. io_putchar('\n');
  282. return;
  283. }
  284. io_putstr("netconsole: usage netconsole on|off|host <a.b.c.d>\n");
  285. }
  286. /* ---- netreset: ask the driver for a full NIC re-init ---- */
  287. void program_netreset_main(void)
  288. {
  289. const struct netdev *nd = netdev_active();
  290. if (!nd) {
  291. io_putstr("netreset: no NIC found\n");
  292. return;
  293. }
  294. if (!nd->reset) {
  295. io_putstr("netreset: not supported by ");
  296. io_putstr(nd->name);
  297. io_putchar('\n');
  298. return;
  299. }
  300. io_putstr("netreset: re-initializing ");
  301. io_putstr(nd->name);
  302. io_putstr("...\n");
  303. nd->reset();
  304. nd->print_info();
  305. }
  306. /* ---- netloop: PHY loopback self-test (no cable/peer needed) ---- */
  307. static void netloop_run(void)
  308. {
  309. const struct netdev *nd = netdev_active();
  310. unsigned char frame[64], mac[6];
  311. unsigned int i, deadline;
  312. int got = 0;
  313. if (!nd) {
  314. io_putstr("netloop: no NIC found\n");
  315. return;
  316. }
  317. if (!nd->set_loopback) {
  318. io_putstr("netloop: not supported by ");
  319. io_putstr(nd->name);
  320. io_putchar('\n');
  321. return;
  322. }
  323. nd->get_mac(mac);
  324. for (i = 0; i < 6; i++)
  325. frame[i] = 0xFF; /* broadcast */
  326. for (i = 0; i < 6; i++)
  327. frame[6 + i] = mac[i];
  328. frame[12] = 0x88;
  329. frame[13] = 0xB6; /* local experimental */
  330. frame[14] = 'N'; frame[15] = 'E'; frame[16] = 'B';
  331. frame[17] = 'O'; frame[18] = 'S'; frame[19] = 'L';
  332. frame[20] = 'O'; frame[21] = 'O';
  333. for (i = 22; i < 64; i++)
  334. frame[i] = 0x00;
  335. io_putstr("netloop: PHY loopback on, sending...\n");
  336. nd->set_loopback(1);
  337. if (nd->tx(frame, sizeof(frame)) != 0) {
  338. io_putstr("netloop: transmit failed\n");
  339. nd->set_loopback(0);
  340. return;
  341. }
  342. deadline = get_ticks_ms() + 1000;
  343. while (get_ticks_ms() < deadline) {
  344. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  345. if (len < 0)
  346. continue;
  347. if (len >= 22 &&
  348. net_rx_buf[12] == 0x88 && net_rx_buf[13] == 0xB6 &&
  349. net_rx_buf[14] == 'N' && net_rx_buf[15] == 'E' &&
  350. net_rx_buf[16] == 'B' && net_rx_buf[17] == 'O' &&
  351. net_rx_buf[18] == 'S') {
  352. got = 1;
  353. break;
  354. }
  355. }
  356. nd->set_loopback(0);
  357. io_putstr(got ? "netloop: PASS (frame came back)\n"
  358. : "netloop: FAIL (no frame came back)\n");
  359. }
  360. void program_netloop_main(void)
  361. {
  362. netdev_acquire();
  363. netloop_run();
  364. netdev_release();
  365. }
  366. /* ---- netping: active ARP + ICMP echo ---- */
  367. #define NETPING_COUNT 4
  368. #define NETPING_ID 0x4E42 /* "NB" */
  369. #define NETPING_WAIT_MS 1000
  370. static unsigned int netping_sent_ms[NETPING_COUNT];
  371. /* ARP request "who has target? tell my_ip" -> 42-byte frame. */
  372. static unsigned int build_arp_request(const unsigned char *my_mac,
  373. const unsigned char *my_ip,
  374. const unsigned char *target_ip,
  375. unsigned char *out)
  376. {
  377. unsigned char *a = out + 14;
  378. unsigned int i;
  379. for (i = 0; i < 6; i++)
  380. out[i] = 0xFF; /* broadcast */
  381. for (i = 0; i < 6; i++)
  382. out[6 + i] = my_mac[i];
  383. out[12] = 0x08;
  384. out[13] = 0x06;
  385. a[0] = 0; a[1] = 1; /* htype Ethernet */
  386. a[2] = 0x08; a[3] = 0; /* ptype IPv4 */
  387. a[4] = 6; a[5] = 4;
  388. a[6] = 0; a[7] = 1; /* opcode request */
  389. for (i = 0; i < 6; i++) /* sha */
  390. a[8 + i] = my_mac[i];
  391. for (i = 0; i < 4; i++) /* spa */
  392. a[14 + i] = my_ip[i];
  393. for (i = 0; i < 6; i++) /* tha */
  394. a[18 + i] = 0;
  395. for (i = 0; i < 4; i++) /* tpa */
  396. a[24 + i] = target_ip[i];
  397. return 42;
  398. }
  399. /* 1 + peer MAC if frame is an ARP reply from target_ip. */
  400. static int parse_arp_reply(const unsigned char *f, unsigned int len,
  401. const unsigned char *target_ip, unsigned char *mac)
  402. {
  403. const unsigned char *a = f + 14;
  404. unsigned int i;
  405. if (len < 14 + 28)
  406. return 0;
  407. if ((((unsigned int)f[12] << 8) | f[13]) != ET_ARP)
  408. return 0;
  409. if ((((unsigned int)a[6] << 8) | a[7]) != 2) /* opcode reply */
  410. return 0;
  411. if (a[4] != 6 || a[5] != 4)
  412. return 0;
  413. if (!ip_eq(a + 14, target_ip)) /* sender IP == target */
  414. return 0;
  415. for (i = 0; i < 6; i++)
  416. mac[i] = a[8 + i];
  417. return 1;
  418. }
  419. /* ICMP echo request (14 + 20 + 16 bytes; the driver pads to 60). */
  420. static unsigned int build_icmp_echo(const unsigned char *src_mac,
  421. const unsigned char *dst_mac,
  422. const unsigned char *src_ip,
  423. const unsigned char *dst_ip,
  424. unsigned short seq, unsigned char *out)
  425. {
  426. unsigned char *ip = out + 14;
  427. unsigned char *ic = ip + 20;
  428. unsigned char *pl = ic + 8;
  429. unsigned short cs;
  430. unsigned int i;
  431. for (i = 0; i < 6; i++)
  432. out[i] = dst_mac[i];
  433. for (i = 0; i < 6; i++)
  434. out[6 + i] = src_mac[i];
  435. out[12] = 0x08;
  436. out[13] = 0x00;
  437. for (i = 0; i < 20; i++)
  438. ip[i] = 0;
  439. ip[0] = 0x45;
  440. ip[2] = 0; ip[3] = 36; /* total length */
  441. ip[8] = 64; /* TTL */
  442. ip[9] = IPPROTO_ICMP;
  443. for (i = 0; i < 4; i++)
  444. ip[12 + i] = src_ip[i];
  445. for (i = 0; i < 4; i++)
  446. ip[16 + i] = dst_ip[i];
  447. cs = net_checksum(ip, 20);
  448. ip[10] = (unsigned char)(cs >> 8);
  449. ip[11] = (unsigned char)(cs & 0xFF);
  450. ic[0] = ICMP_ECHO_REQUEST;
  451. ic[1] = 0;
  452. ic[2] = 0; ic[3] = 0;
  453. ic[4] = (unsigned char)(NETPING_ID >> 8);
  454. ic[5] = (unsigned char)(NETPING_ID & 0xFF);
  455. ic[6] = (unsigned char)(seq >> 8);
  456. ic[7] = (unsigned char)(seq & 0xFF);
  457. pl[0] = 'n'; pl[1] = 'e'; pl[2] = 'b'; pl[3] = 'o';
  458. pl[4] = 's'; pl[5] = 0; pl[6] = 0; pl[7] = 0;
  459. cs = net_checksum(ic, 16);
  460. ic[2] = (unsigned char)(cs >> 8);
  461. ic[3] = (unsigned char)(cs & 0xFF);
  462. return 14 + 20 + 16;
  463. }
  464. /* 1 + ICMP echo-reply seq for our IP from peer_ip. */
  465. static int parse_icmp_reply(const unsigned char *f, unsigned int len,
  466. const unsigned char *my_ip,
  467. const unsigned char *peer_ip,
  468. unsigned short *seq)
  469. {
  470. const unsigned char *ip = f + 14;
  471. const unsigned char *ic;
  472. unsigned int ihl;
  473. unsigned short ip_len, id;
  474. if (len < 14 + 20)
  475. return 0;
  476. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  477. return 0;
  478. if ((ip[0] >> 4) != 4)
  479. return 0;
  480. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  481. if (ihl < 20 || ip[9] != IPPROTO_ICMP)
  482. return 0;
  483. ip_len = ((unsigned short)ip[2] << 8) | ip[3];
  484. if (ip_len < ihl + 8 || (unsigned int)ip_len > len - 14)
  485. return 0;
  486. if (!ip_eq(ip + 12, peer_ip)) /* src == peer */
  487. return 0;
  488. if (!ip_eq(ip + 16, my_ip)) /* dst == us */
  489. return 0;
  490. ic = ip + ihl;
  491. if (ic[0] != ICMP_ECHO_REPLY || ic[1] != 0)
  492. return 0;
  493. id = ((unsigned short)ic[4] << 8) | ic[5];
  494. if (id != NETPING_ID)
  495. return 0;
  496. *seq = ((unsigned short)ic[6] << 8) | ic[7];
  497. if (*seq < 1 || *seq > NETPING_COUNT)
  498. return 0;
  499. return 1;
  500. }
  501. /* Resolve target_ip's MAC by ARP (3 tries, 1 s each).
  502. * Returns 0 = resolved, -1 = no reply, -2 = transmit failed.
  503. * Prints the frames seen while waiting (capped) so a silent peer, a filtered
  504. * reply and a reply we refuse to parse can be told apart on screen. */
  505. static int netping_arp(const struct netdev *nd, const unsigned char *my_mac,
  506. const unsigned char *my_ip, const unsigned char *target_ip,
  507. unsigned char *target_mac)
  508. {
  509. unsigned char frame[64];
  510. unsigned int tries, seen = 0;
  511. for (tries = 0; tries < 3; tries++) {
  512. unsigned int deadline = get_ticks_ms() + NETPING_WAIT_MS;
  513. unsigned int flen = build_arp_request(my_mac, my_ip, target_ip, frame);
  514. if (nd->tx(frame, flen) != 0)
  515. return -2;
  516. io_putstr("netping: ARP request sent (try ");
  517. put_dec(tries + 1);
  518. io_putstr("/3)\n");
  519. while (get_ticks_ms() < deadline) {
  520. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  521. if (len < 0)
  522. continue;
  523. if (seen < 8) {
  524. io_putstr("netping: rx ");
  525. net_decode_frame(net_rx_buf, (unsigned int)len);
  526. io_putchar('\n');
  527. seen++;
  528. }
  529. if (parse_arp_reply(net_rx_buf, (unsigned int)len,
  530. target_ip, target_mac))
  531. return 0;
  532. }
  533. }
  534. io_putstr("netping: saw ");
  535. put_dec(seen);
  536. io_putstr(" frame(s) while waiting\n");
  537. return -1;
  538. }
  539. static void netping_run(const char *arg)
  540. {
  541. const struct netdev *nd;
  542. unsigned char target[4], my_ip[4], my_mac[6], target_mac[6];
  543. unsigned int i, replies = 0;
  544. if (arg[0] == '\0' || net_parse_ip(arg, target) != 0) {
  545. io_putstr("netping: usage netping <a.b.c.d>\n");
  546. return;
  547. }
  548. nd = netdev_active();
  549. if (!nd) {
  550. io_putstr("netping: no NIC found\n");
  551. return;
  552. }
  553. nd->get_mac(my_mac);
  554. net_get_ip(my_ip);
  555. {
  556. int r = netping_arp(nd, my_mac, my_ip, target, target_mac);
  557. if (r == -2) {
  558. io_putstr("netping: transmit failed (see netregs)\n");
  559. return;
  560. }
  561. if (r != 0) {
  562. io_putstr("netping: no ARP reply from ");
  563. put_ip(target);
  564. io_putchar('\n');
  565. return;
  566. }
  567. }
  568. io_putstr("netping: ");
  569. put_ip(target);
  570. io_putstr(" is-at ");
  571. put_mac(target_mac);
  572. io_putchar('\n');
  573. for (i = 0; i < NETPING_COUNT; i++) {
  574. unsigned char frame[64];
  575. unsigned int flen, deadline;
  576. int got = 0;
  577. flen = build_icmp_echo(my_mac, target_mac, my_ip, target,
  578. (unsigned short)(i + 1), frame);
  579. netping_sent_ms[i] = get_ticks_ms();
  580. if (nd->tx(frame, flen) != 0) {
  581. io_putstr("netping: transmit failed\n");
  582. return;
  583. }
  584. deadline = get_ticks_ms() + NETPING_WAIT_MS;
  585. while (get_ticks_ms() < deadline && !got) {
  586. unsigned short seq;
  587. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  588. unsigned int rtt;
  589. if (len < 0)
  590. continue;
  591. if (!parse_icmp_reply(net_rx_buf, (unsigned int)len,
  592. my_ip, target, &seq))
  593. continue;
  594. rtt = get_ticks_ms() - netping_sent_ms[seq - 1];
  595. io_putstr("reply: seq=");
  596. put_dec(seq);
  597. io_putstr(" rtt=");
  598. if (rtt == 0)
  599. io_putstr("<1");
  600. else
  601. put_dec(rtt);
  602. io_putstr("ms\n");
  603. got = 1;
  604. replies++;
  605. }
  606. if (!got) {
  607. io_putstr("netping: timeout, seq=");
  608. put_dec(i + 1);
  609. io_putchar('\n');
  610. }
  611. }
  612. io_putstr("netping: ");
  613. put_dec(replies);
  614. io_putstr("/");
  615. put_dec(NETPING_COUNT);
  616. io_putstr(" replies from ");
  617. put_ip(target);
  618. io_putchar('\n');
  619. }
  620. void program_netping_main(const char *arg)
  621. {
  622. netdev_acquire();
  623. netping_run(arg);
  624. netdev_release();
  625. }