netcmd.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* Network test commands: netinfo, netrecv, netsend, netip, ping. */
  2. #include "io.h"
  3. #include "timer.h"
  4. #include "ne2000.h"
  5. #include "net.h"
  6. /* Shared frame buffers (file scope, not stack: the 4 KB process stack can't
  7. hold a full-size frame, and keeping them out of BSS-as-much-as-possible
  8. avoids bloating the kernel past its 0x40000 limit). Max Ethernet frame is
  9. 1518 bytes (14 header + 1500 payload + 4 VLAN). */
  10. #define NET_FRAME_MAX 1518
  11. static unsigned char net_rx_buf[NET_FRAME_MAX];
  12. static unsigned char net_reply_buf[NET_FRAME_MAX];
  13. static void put_hex2(unsigned char v)
  14. {
  15. static const char hex[] = "0123456789ABCDEF";
  16. io_putchar(hex[(v >> 4) & 0xF]);
  17. io_putchar(hex[v & 0xF]);
  18. }
  19. static void put_dec(unsigned int v)
  20. {
  21. char b[12];
  22. int i = 0;
  23. if (v == 0) {
  24. io_putchar('0');
  25. return;
  26. }
  27. while (v) {
  28. b[i++] = (char)('0' + (v % 10));
  29. v /= 10;
  30. }
  31. while (i)
  32. io_putchar(b[--i]);
  33. }
  34. void program_netinfo_main(void)
  35. {
  36. ne2000_init();
  37. ne2000_print_info();
  38. if (ne2000_present()) {
  39. io_putstr("ip=");
  40. net_print_ip();
  41. io_putchar('\n');
  42. }
  43. }
  44. void program_netrecv_main(void)
  45. {
  46. unsigned int deadline, count = 0;
  47. ne2000_init();
  48. if (!ne2000_present()) {
  49. io_putstr("netrecv: no NIC found\n");
  50. return;
  51. }
  52. io_putstr("netrecv: listening (8s)...\n");
  53. deadline = get_ticks_ms() + 8000;
  54. while (get_ticks_ms() < deadline) {
  55. int len = ne2000_rx(net_rx_buf, NET_FRAME_MAX);
  56. unsigned int i;
  57. if (len < 0)
  58. continue;
  59. net_decode_frame(net_rx_buf, (unsigned int)len);
  60. io_putchar('\n');
  61. /* Raw hex dump for closer inspection. */
  62. for (i = 0; i < (unsigned int)len; i++) {
  63. put_hex2(net_rx_buf[i]);
  64. io_putchar(' ');
  65. if ((i & 15) == 15)
  66. io_putchar('\n');
  67. }
  68. if (((unsigned int)len & 15) != 0)
  69. io_putchar('\n');
  70. if (++count >= 32)
  71. break;
  72. }
  73. io_putstr("netrecv: ");
  74. put_dec(count);
  75. io_putstr(" frame(s)\n");
  76. }
  77. void program_netsend_main(void)
  78. {
  79. unsigned char frame[64];
  80. unsigned char mac[6];
  81. unsigned int i;
  82. ne2000_init();
  83. if (!ne2000_present()) {
  84. io_putstr("netsend: no NIC found\n");
  85. return;
  86. }
  87. ne2000_get_mac(mac);
  88. /* Broadcast test frame: dst=ff:ff:ff:ff:ff:ff, src=our MAC,
  89. ethertype 0x88B5 (local experimental), payload "NEBS". */
  90. for (i = 0; i < 6; i++)
  91. frame[i] = 0xFF;
  92. for (i = 0; i < 6; i++)
  93. frame[6 + i] = mac[i];
  94. frame[12] = 0x88;
  95. frame[13] = 0xB5;
  96. frame[14] = 'N';
  97. frame[15] = 'E';
  98. frame[16] = 'B';
  99. frame[17] = 'S';
  100. for (i = 18; i < 64; i++)
  101. frame[i] = 0x00;
  102. if (ne2000_tx(frame, sizeof(frame)) != 0) {
  103. io_putstr("netsend: transmit failed\n");
  104. return;
  105. }
  106. io_putstr("netsend: sent 64-byte broadcast frame\n");
  107. }
  108. void program_netip_main(const char *arg)
  109. {
  110. unsigned char ip[4];
  111. if (arg[0] == '\0') {
  112. net_print_ip();
  113. io_putchar('\n');
  114. return;
  115. }
  116. if (net_parse_ip(arg, ip) != 0) {
  117. io_putstr("netip: usage netip <a.b.c.d>\n");
  118. return;
  119. }
  120. net_set_ip(ip);
  121. io_putstr("ip set to ");
  122. net_print_ip();
  123. io_putchar('\n');
  124. }
  125. void program_ping_main(void)
  126. {
  127. unsigned int deadline, count = 0;
  128. ne2000_init();
  129. if (!ne2000_present()) {
  130. io_putstr("ping: no NIC found\n");
  131. return;
  132. }
  133. io_putstr("ping: answering ARP/ICMP for 30s (ip=");
  134. net_print_ip();
  135. io_putstr(")...\n");
  136. deadline = get_ticks_ms() + 30000;
  137. while (get_ticks_ms() < deadline) {
  138. int len = ne2000_rx(net_rx_buf, NET_FRAME_MAX);
  139. int rlen;
  140. if (len < 0)
  141. continue;
  142. rlen = net_build_reply(net_rx_buf, (unsigned int)len,
  143. net_reply_buf, NET_FRAME_MAX);
  144. if (rlen > 0) {
  145. if (ne2000_tx(net_reply_buf, (unsigned int)rlen) == 0) {
  146. io_putstr("reply: ");
  147. net_decode_frame(net_reply_buf, (unsigned int)rlen);
  148. io_putchar('\n');
  149. count++;
  150. }
  151. }
  152. }
  153. io_putstr("ping: done (");
  154. put_dec(count);
  155. io_putstr(" replies)\n");
  156. }