⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhcp.c

📁 此文档为采用FPGA实现的以太网MAC层
💻 C
字号:
#include "dhcp.h"#include "udp.h"#include "ip.h"#include "mac_low.h"#include "serial.h"extern IP_SETTINGS ipsettings;extern IP_TABLE iptable;volatile Uint16 dhcp_timer = 20;void dhcp_parse(DHCP_HEADER *dhcp_packet,Uint16 srcip_h,Uint16 srcip_l){    Uint8 x,*charptr;    Uint32 word;    Uint16 tmpint;    DHCP_OPT    * dhcp_option;    //Check if packet is an Offer reply    //dhcp_packet = (DHCP_HEADER *) word;    //DHCP packet points to start of BOOTp packet    //Check if boot reply    if (dhcp_packet->opcode != 2)        return;    //Check MAC address    for (x = 0; x < 6; x++)    {       //dbg(0xeeee);       //dbg_char(dhcp_packet->clienthw_addr[x]);        if (dhcp_packet->clienthw_addr[x] != ipsettings.mac_address[x])            return;    }    word = (Uint32) dhcp_packet;    //Seek to data    word += DHCP_SIZE;    //Set x to 0 to indicate no need to replace DNS server    x=0;    //Keep seeking until 0xff is hit in the opcode field    while (1)    {        dhcp_option = (DHCP_OPT *) word;        charptr = (Uint8 *)(word + 2);        switch(dhcp_option->opcode)        {          case 0xff:            return;            break;          case 0x35:               //DHCP message type field               if (charptr[0] == 2)               {                  //DHCP offer found, grap ip's                  dhcp_sendack(dhcp_packet->yourip_h, dhcp_packet->yourip_l,srcip_h,srcip_l);                  return;               }               if (charptr[0] == 6)               {                //NACK received, request again                dhcp_getip();               }               if (charptr[0] == 5)               {                //ACK received                //Search for DNS server                //Set x to 1 to indicate the DNS server needs to be written                x=1;               }               break;          case 0x6:               //Break if we don't need to write DNS server               if (!x) break;               //DNS server field               //First two bytes are high field               tmpint = (Uint16)((*(charptr++)) << 8);               tmpint |= (Uint16)((*(charptr++)) & 0xff);               iptable.dns_server.ip_h = tmpint;               dbg(tmpint);               tmpint = (Uint16)((*(charptr++)) << 8);               tmpint |= (Uint16)((*(charptr++)) & 0xff);               iptable.dns_server.ip_l = tmpint;               dbg(tmpint);               break;        }        word += DHCPOPT_SIZE + dhcp_option->len;    }}void dhcp_getip(void){    DHCP_HEADER * packet;    DHCP_OPT    * dhcpopt;    UDPHEADER   * udppacket;    Uint16      * buf, x;    Uint8       * charptr;    //Reset ip    ipsettings.ip_address_h = ipsettings.ip_address_l = 0;    packet = (DHCP_HEADER *)(EMAC_TX_PTR + DHCP_OFFSET);    udppacket = (UDPHEADER *)(EMAC_TX_PTR + UDP_OFFSET);    buf = (Uint16 *) packet;    //Zero packet    for (x = 0; x < (DHCP_SIZE / 2); x++)    {        buf[x] = 0;    }    packet->opcode = 1;    packet->hw_type = 1;    packet->hw_addrlen = 6;    packet->xid_h = 0xfaaf;    packet->xid_l = 0xeffe;    for (x = 0; x < 6; x++)    {        packet->clienthw_addr[x] = ipsettings.mac_address[x];    }    packet->magic_cookie_h = 0x6382;    packet->magic_cookie_l = 0x5363;    //Fill out Message type header    dhcpopt = (DHCP_OPT *)(EMAC_TX_PTR + DHCPOPT_ROOT);    dhcpopt->opcode = 0x35;    dhcpopt->len = 1;    charptr = (Uint8 *) dhcpopt;    charptr += DHCPOPT_SIZE;    * (charptr++) = 1;    //Fill out client identifier header    * (charptr++) = 0x3d;    * (charptr++) = 0x07;    * (charptr++) = 0x1;    //Copy in MAC address    for (x = 0; x < 6; x++)    {        * (charptr++) = ipsettings.mac_address[x];    }    //Fill out IP Field    * (charptr++) = 0x32;    * (charptr++) = 0x4;    * (charptr++) = ipsettings.ip_address_h >> 8;    * (charptr++) = ipsettings.ip_address_h & 0xff;    * (charptr++) = ipsettings.ip_address_l >> 8;    * (charptr++) = ipsettings.ip_address_l & 0xff;//    * (charptr++) = 0xc0;//    * (charptr++) = 0xa8;//    * (charptr++) = 0x01;//    * (charptr++) = 0x6e;      //End of packet    * (charptr++) = 0xff;    //Fill out udp packet information    udppacket->sourceport = 0x44;    udppacket->destport = 0x43;    //dbg((DHCP_SIZE+20)/2);    x = udp_packet(udppacket, 0xffff, 0xffff, (Uint16 *) packet, (DHCP_SIZE + 20) / 2);    //dbg(x);    //ip_send(0xc0a8,0x0101,x,PROTOCOL_UDP);    ip_send(0xffff, 0xffff, x, PROTOCOL_UDP);}void dhcp_sendack(Uint16 ip_h, Uint16 ip_l, Uint16 serverip_h, Uint16 serverip_l){    DHCP_HEADER * packet;    DHCP_OPT    * dhcpopt;    UDPHEADER   * udppacket;    Uint16      * buf, x;    Uint8       * charptr;    dbg(ip_h);    dbg(ip_l);    //Set ip_address    ipsettings.ip_address_h = ip_h;    ipsettings.ip_address_l = ip_l;    packet = (DHCP_HEADER *)(EMAC_TX_PTR + DHCP_OFFSET);    udppacket = (UDPHEADER *)(EMAC_TX_PTR + UDP_OFFSET);    buf = (Uint16 *) packet;    //Fill out Message type header    dhcpopt = (DHCP_OPT *)(EMAC_TX_PTR + DHCPOPT_ROOT);    dhcpopt->opcode = 0x35;    dhcpopt->len = 1;    for (x = 0; x < 6; x++)    {        packet->clienthw_addr[x] = ipsettings.mac_address[x];    }    charptr = (Uint8 *) dhcpopt;    charptr += DHCPOPT_SIZE;    * (charptr++) = 3;    //Fill out client identifier header    * (charptr++) = 0x3d;    * (charptr++) = 0x07;    //HW type to ethernet    * (charptr++) = 0x1;    //Fill out MAC adress    for (x = 0; x < 6; x++)    {        * (charptr++) = ipsettings.mac_address[x];    }    //Requested IP field    * (charptr++) = 0x32;    * (charptr++) = 0x4;    //Store obtained IP    * (charptr++) = (Uint8)(ip_h >> 8);    * (charptr++) = (Uint8)(ip_h & 0xff);    * (charptr++) = (Uint8)(ip_l >> 8);    * (charptr++) = (Uint8)(ip_l & 0xff);    * (charptr++) = 0xff;    //Fill out udp packet information    udppacket->sourceport = 0x44;    udppacket->destport = 0x43;    //dbg((DHCP_SIZE+20)/2);    x = udp_packet(udppacket, serverip_h, serverip_l, (Uint16 *) packet, (DHCP_SIZE + 20) / 2);    //dbg(x);    ip_send(serverip_h, serverip_l, x, PROTOCOL_UDP);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -