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

📄 udp.c

📁 此文档为采用FPGA实现的以太网MAC层
💻 C
字号:
#include "ip.h"#include "udp.h"#include "serial.h"#include "mac.h"#include "mac_low.h"#include "ntp.h"#include "dhcp.h"extern Uint16 data_buffer[100];extern IP_SETTINGS ipsettings;UDPHEADER udp_header;void udp_process(UDPHEADER * packet, Uint16 datasize, Uint16 sourceip_h, Uint16 sourceip_l){    Uint16 datalen, x, y, * buf, * buf2;    Uint8 * charptr;    UDPHEADER   * packet2;    NTPHEADER   * ntp_packet;    DHCP_HEADER * dhcp_packet;    Uint32        word;    //Print src port    buf2 = (Uint16 *)(EMAC_TX_PTR + UDP_DATA_OFFSET);    word = (Uint32) packet;    word += UDP_SIZE;    if (packet->destport == 0x400)    {        datalen = udp_getlen(packet->length);        //Read back data        //mac_read(datalen,(Uint16 *)data_buffer);        packet2 = (UDPHEADER *)(EMAC_TX_PTR + UDP_OFFSET);        packet2->destport = packet2->sourceport = 0x400;        //Copy data from buf (RX) to buf2 (TX)        buf = (Uint16 *) word;        for (x = 0; x < datalen; x++)            buf2[x] = buf[x];        x = udp_packet(packet2, sourceip_h, sourceip_l, buf2, datalen);        ip_send(sourceip_h, sourceip_l, x, PROTOCOL_UDP);    }    else if (packet->destport == 123)    {        dbg(0xffff);        ntp_packet = (NTPHEADER *) word;        dbg_string((Uint16 *) & (ntp_packet->tx_timestamp_h), 2);        //dbg32(ntp_packet->tx_timestamp_h);    }    else if (packet->destport == 0x44)    {        dbg(0xffff);        dhcp_packet = (DHCP_HEADER *) word;        dhcp_parse(dhcp_packet,sourceip_h,sourceip_l);    }    else    {        //dbg(packet->destport);        //dbg(packet->sourceport);    }}Uint16 udp_getlen(Uint16 len){    return((len - 8) / 2);}//Expect source and destport to be filled out//Returns length of packet in wordsUint16 udp_packet(UDPHEADER * packet, Uint16 dest_ip_h, Uint16 dest_ip_l, Uint16 * databuf, Uint16 data_len){    Uint16 * intptr;    //Fill in length field    packet->length = 8 + (data_len * 2);    //Dummy checkum    packet->checksum = 0x0000;    //Append pseudo-header    //Set pointer to end of data buffer - data starts at packet+8    //intptr = ((Uint16 *) packet)+((packet->length)/2);    intptr = (Uint16 *)(databuf + data_len);    * (intptr++) = ipsettings.ip_address_h;    * (intptr++) = ipsettings.ip_address_l;    //High int of dest ip    * (intptr++) = dest_ip_h;    * (intptr++) = dest_ip_l;    //append length    * (intptr++) = packet->length;    //append protocol    * (intptr++) = PROTOCOL_UDP;    //pseudo header generated, calculate checksum    //packet->checksum = ip_chksum((Uint16 *) packet,((packet->length)/2)+6);    packet->checksum = ip_chksum2((Uint16 *) packet, UDP_SIZE / 2, databuf, data_len + 6);    return(packet->length) / 2;}

⌨️ 快捷键说明

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