static void dhcp_discover(u32 xid) { struct { dhcp_msg_t msg; u8 opt[128]; } s; u8 *opt = s.opt; const char *hostname = minip_get_hostname(); memset(&s, 0, sizeof(s)); s.msg.opcode = DHCP_REQUEST; s.msg.hwtype = HW_ETHERNET; s.msg.hwalen = 6; s.msg.xid = xid; s.msg.cookie = 0x63538263; minip_get_macaddr(s.msg.chaddr); *opt++ = OPT_MSG_TYPE; *opt++ = 1; *opt++ = OP_DHCPDISCOVER; if (hostname && hostname[0]) { size_t len = strlen(hostname); *opt++ = OPT_HOSTNAME; *opt++ = len; memcpy(opt, hostname, len); opt += len; } *opt++ = OPT_DONE; udp_send(&s.msg, sizeof(dhcp_msg_t) + (opt - s.opt), dhcp_udp_handle); status_t ret = udp_send(&s.msg, sizeof(dhcp_msg_t) + (opt - s.opt), dhcp_udp_handle); if (ret != NO_ERROR) { printf("DHCP_DISCOVER failed: %d\n", ret); } }
static int cmd_minip(int argc, const cmd_args *argv) { if (argc == 1) { minip_usage: printf("minip commands\n"); printf("mi [a]rp dump arp table\n"); printf("mi [s]tatus print ip status\n"); printf("mi [t]est [dest] [port] [cnt] send <cnt> test packets to the dest:port\n"); } else { switch (argv[1].str[0]) { case 'a': arp_cache_dump(); break; case 's': { uint32_t ipaddr = minip_get_ipaddr(); printf("hostname: %s\n", minip_get_hostname()); printf("ip: %u.%u.%u.%u\n", IPV4_SPLIT(ipaddr)); } break; case 't': { uint32_t count = 1; uint32_t host = 0x0100000A; // 10.0.0.1 uint32_t port = 1025; udp_socket_t *handle; switch (argc) { case 5: count = argv[4].u; case 4: port = argv[3].u; case 3: host = str_ip_to_int(argv[2].str, strlen(argv[2].str)); break; } if (udp_open(host, port, port, &handle) != NO_ERROR) { printf("udp_open to %u.%u.%u.%u:%u failed\n", IPV4_SPLIT(host), port); return -1; } #define BUFSIZE 1470 uint8_t *buf; buf = malloc(BUFSIZE); if (!buf) { udp_close(handle); return -1; } memset(buf, 0x00, BUFSIZE); printf("sending %u packet(s) to %u.%u.%u.%u:%u\n", count, IPV4_SPLIT(host), port); lk_time_t t = current_time(); uint32_t failures = 0; for (uint32_t i = 0; i < count; i++) { if (udp_send(buf, BUFSIZE, handle) != 0) { failures++; } buf[128]++; } t = current_time() - t; printf("%d pkts failed\n", failures); uint64_t total_count = (uint64_t)count * BUFSIZE; printf("wrote %llu bytes in %u msecs (%llu bytes/sec)\n", total_count, (uint32_t)t, total_count * 1000 / t); free(buf); udp_close(handle); #undef BUFSIZE } break; default: goto minip_usage; } } return 0; }