📄 tinydhcp.c
字号:
} } else { if (lfix(rxpacket.xid) != rand_xid) { /*sys_printf("Error: Received different XID.\n");*/ continue; } if ((message = get_option(&rxpacket, DHCP_MESSAGE_TYPE)) == NULL) { /*sys_printf("Error: couldn't get option from packet.\n");*/ continue; } switch (state) { case INIT : if (*message == DHCPOFFER) { if ((temp = get_option(&rxpacket, DHCP_SERVER_ID)) != NULL) { sys_memcpy(&server_addr, temp, 4); server_addr = lfix(server_addr); s_xid = lfix(rxpacket.xid); requested_ip = lfix(rxpacket.yiaddr); sys_printf("Received DHCPOFFER...\n"); sys_printf("Server ID: %d.%d.%d.%d\n", ((server_addr>>24) & 0xff), ((server_addr>>16) & 0xff), ((server_addr>>8) & 0xff), (server_addr & 0xff)); sys_printf("Offered IP: %d.%d.%d.%d\n", ((requested_ip>>24) & 0xff), ((requested_ip>>16) & 0xff), ((requested_ip>>8) & 0xff), (requested_ip & 0xff)); ticks = GetTicks((DHCP_TIMEOUT - 4)); state = REQUEST; packetnums = 0; } else sys_printf("Error: No Server ID in message.\n"); } break; case REQUEST : #ifdef DHCP_RENEW case RENEW : #endif if (*message == DHCPACK) { sys_printf("Received DHCP ACK\n"); if ((temp = get_option(&rxpacket, DHCP_LEASE_TIME)) == NULL) { /*sys_printf("No Lease time with ACK, using 1 hour lease \n");*/ lease = 60 * 60; } else { sys_memcpy(&lease, temp, 4); lease = lfix(lease); } requested_ip = lfix(rxpacket.yiaddr); sys_printf("Lease of %d.%d.%d.%d obtained, lease time %d.\n", ((requested_ip>>24) & 0xff), ((requested_ip>>16) & 0xff), ((requested_ip>>8) & 0xff), (requested_ip & 0xff), lease); if ((temp = get_option(&rxpacket, DHCP_ROUTER)) != NULL) { sys_memcpy(&router_addr, temp, 4); router_addr = lfix(router_addr); } if ((temp = get_option(&rxpacket, DHCP_SUBNET)) != NULL) { sys_memcpy(&subnet_addr, temp, 4); subnet_addr = lfix(subnet_addr); } #ifdef DHCP_RENEW packetnums = 0; state = BOUND; ticks = GetTicks((lease/2)); /* Renew time */ #else /* We just need the address, * no need to take care of renew/release. * So set this flag to 0 to exit from this while loop. */ dhcpflag = 0; ack_received = 1; #endif } else if (*message == DHCPNAK) { sys_printf("Received DHCP NAK\n"); requested_ip = 0; packetnums = 0; state = INIT; ticks = GetTicks((DHCP_TIMEOUT - 4)); } break; } } } if (ack_received) { if (requested_ip != 0) { sin_lclINAddr = requested_ip; sys_memset(cp, 0, sizeof(cp)); sys_sprintf(cp, "%d.%d.%d.%d", ((requested_ip>>24) & 0xff), ((requested_ip>>16) & 0xff), ((requested_ip>>8) & 0xff), (requested_ip & 0xff)); sys_setenv("my_ipaddress", cp); } if (router_addr != 0) { net_Gateway = router_addr; sys_memset(cp, 0, sizeof(cp)); sys_sprintf(cp, "%d.%d.%d.%d", ((router_addr>>24) & 0xff), ((router_addr>>16) & 0xff), ((router_addr>>8) & 0xff), (router_addr & 0xff)); sys_setenv("gateway", cp); } if (router_addr != 0) { net_IpMask = subnet_addr; sys_memset(cp, 0, sizeof(cp)); sys_sprintf(cp, "%d.%d.%d.%d", ((subnet_addr>>24) & 0xff), ((subnet_addr>>16) & 0xff), ((subnet_addr>>8) & 0xff), (subnet_addr & 0xff)); sys_setenv("ipmask", cp); } } if (!udplib_deinit()) { /*sys_printf("udplib_deinit: failed\n");*/ return 0; } return 1;}void init_header(dhcpMessage *packet, char type){ sys_memset(packet, 0, sizeof(dhcpMessage)); switch (type) { case DHCPDISCOVER: case DHCPREQUEST: case DHCPRELEASE: case DHCPINFORM: packet->op = BOOTREQUEST; break; case DHCPOFFER: case DHCPACK: case DHCPNAK: packet->op = BOOTREPLY; } packet->htype = ETH_10MB; packet->hlen = ETH_10MB_LEN; packet->cookie = lfix(DHCP_MAGIC); packet->options[0] = DHCP_END; add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);}/* initialize a packet with the proper defaults */void init_packet(dhcpMessage *packet, char type){ static unsigned char clientid[10]; static unsigned char hostnameoption[256]; char *hostname = NULL; struct vendor { char vendor, length; char str[sizeof("dhcp 0.0")]; } vendor_id = { DHCP_VENDOR, sizeof("dhcp 0.0") - 1, "dhcp 0.0"}; init_header(packet, type); sys_memcpy(packet->chaddr, sed_lclEthAddr, 6); clientid[OPT_CODE] = DHCP_CLIENT_ID; clientid[OPT_LEN] = 7; clientid[OPT_DATA] = 1; sys_memcpy(clientid + 3, sed_lclEthAddr, 6); add_option_string(packet->options, clientid); hostname = sys_getenv("hostname"); if (hostname == NULL) hostname = ProductIDStr; hostnameoption[OPT_CODE] = DHCP_HOST_NAME; hostnameoption[OPT_LEN] = strlen(hostname); sys_memcpy((hostnameoption + 2), hostname, strlen(hostname)); add_option_string(packet->options, hostnameoption); add_option_string(packet->options, (unsigned char *) &vendor_id);}/* Broadcast a DHCP discover packet to the network, with an optionally requested IP */void send_discover(void){ dhcpMessage packet; init_packet(&packet, DHCPDISCOVER); packet.xid = lfix(random_xid()); add_requests(&packet); sys_printf("Sending DISCOVER...\n"); udp_send(INADDR_BROADCAST, sizeof(dhcpMessage), &packet);} void send_selecting(void){ dhcpMessage packet; init_packet(&packet, DHCPREQUEST); packet.xid = lfix(s_xid); add_simple_option(packet.options, DHCP_REQUESTED_IP, lfix(requested_ip)); add_simple_option(packet.options, DHCP_SERVER_ID, lfix(server_addr)); add_requests(&packet); sys_printf("Sending DHCP REQUEST...\n"); udp_send(INADDR_BROADCAST, sizeof(dhcpMessage), &packet);}#ifdef DHCP_RENEWvoid send_renew(void){ dhcpMessage packet; init_packet(&packet, DHCPREQUEST); packet.xid = lfix(s_xid); packet.ciaddr = lfix(requested_ip); add_requests(&packet); /*sys_printf("Sending renew...\n"); */ udp_send(server_addr, sizeof(dhcpMessage), &packet);}#endif/* Create a random xid */unsigned long random_xid(void){ rand_xid++; return rand_xid;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -