📄 bootp.c
字号:
#include <setup.h>#include <stdio.h>#include <string.h>#include <command.h>#include <time.h>#include <network.h>#define PORT_BOOTPS 67 // BOOTP server UDP port.#define PORT_BOOTPC 68 // BOOTP client UDP port.static void bootp_usage(void);static bool do_bootp(int argc, char **argv);static int set_bootp_header(void *packet);static bool wait = false;static uint32 bootp_id;struct command_t cmd_bootp = { .name = "bootp", .run = do_bootp, .usage = bootp_usage};static void bootp_usage(void){ usage_format("bootp", "run bootp. get ip and host infomation"); return;}static bool do_bootp(int argc, char **argv){ int i, n, len; bool res; uchar pktbuff[1024], *txpkt; time_t start, now; const uchar broadcast_mac[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; const uint32 broadcast_ip = 0xFFFFFFFF; /* create boop packet */ memset(pktbuff, 0, sizeof(pktbuff)); txpkt = pktbuff + 100; len = set_bootp_header(txpkt); n = set_udp_header(txpkt, PORT_BOOTPS, PORT_BOOTPC, len); txpkt -= n; len += n; n = set_ip_header(txpkt, 0x00, broadcast_ip, IP_P_UDP, len); txpkt -= n; len += n; n = set_ether_header(txpkt, broadcast_mac, PROT_IP); txpkt -= n; len += n; /* view information */ printf(" my mac address is %s\n", mac_ntoa(setup->myhaddr)); printf(" try to send bootp packet"); /* send request packet and wait reply packet */ wait = true; setup->myipaddr = 0; for (i=0; i < 5; i++){ res = net_send_packet(txpkt, len); if (!res) break; printf("."); time(&start); while (time(&now) - start < 1){ net_recv_poll(); if (!wait) return true; } } printf("\n failed : bootp packet is not received.\n"); wait = false; return false;}extern bool bootp_recv(void *packet, int len){ struct bootp *bhp = (struct bootp *)packet; if (len < sizeof(struct bootp)) goto invalid; if (!wait) goto invalid; if (bhp->bh_opcode != OP_BOOTREPLY) goto invalid; if (bhp->bh_htype != HWT_ETHER) goto invalid; if (bhp->bh_hlen != HWL_ETHER) goto invalid; if (memcmp(&bhp->bh_tid, &bootp_id, 4)) goto invalid; //if (memcmp(&bhp->bh_chaddr, net_get_mac_addr(0), 6)) goto invalid; if (memcmp(&bhp->bh_chaddr, setup->myhaddr, 6)) goto invalid; printf("\n"); printf(" reply packet is received from %s (%s)\n", bhp->bh_sname, inet_ntoa(bhp->bh_siaddr)); printf(" my ip address is %s\n", inet_ntoa(bhp->bh_yiaddr)); // printf("the ip is 0x%08x, serip is 0x%08x\n", bhp->bh_yiaddr, bhp->bh_siaddr); setup->myipaddr = bhp->bh_yiaddr; setup->destipaddr = bhp->bh_siaddr; wait = false; return true;invalid : return false;}static int set_bootp_header(void *packet){ uchar *smac; time_t now; struct bootp *bhp = (struct bootp *)packet; bhp->bh_opcode = OP_BOOTREQUEST; // 1 : request 2 : reply. bhp->bh_htype = HWT_ETHER; // 10 Base Ethernet : 1. bhp->bh_hlen = HWL_ETHER; // 10 Base Ethernet : 6. bhp->bh_hops = 0; // client俊辑 0栏肺 setting. gateway啊 荤侩. bhp->bh_secs = htons(time(&now)); memset(&(bhp->bh_ciaddr), 0, 4); memset(&(bhp->bh_yiaddr), 0, 4); memset(&(bhp->bh_siaddr), 0, 4); memset(&(bhp->bh_giaddr), 0, 4); //smac = net_get_mac_addr(0); smac = setup->myhaddr; memcpy((char *)bhp->bh_chaddr, smac, 6); bootp_id = (smac[2] << 24) | (smac[3] << 16) | (smac[4] << 8) | smac[5]; bootp_id += clock(); memcpy(&(bhp->bh_tid), &(bootp_id), 4); return sizeof(struct bootp);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -