📄 arp.c
字号:
// ***************************************************************************// An 8051 Based Web Server// arp.c: processing for ARP packets// By Mason Kidd 4/18/02// ***************************************************************************#include <string.h>#include "packets.h"#include "csio.h"#include "arp.h"void tx_arp_packet(unsigned int arp_oper){ unsigned char tx_buffer[100]; struct eth_hdr *tx_eth_hdr = (struct eth_hdr *)tx_buffer; struct eth_arp *tx_eth_arp = (struct eth_arp *)tx_buffer + sizeof(struct eth_hdr); unsigned char nLength, i; tx_eth_hdr->eth_arp_hdr.ar_op = arp_oper; tx_eth_hdr->eth_arp_hdr.ar_hrd = ARP_HRD_ETHER; tx_eth_hdr->eth_arp_hdr.ar_pro = ETHER_IP; tx_eth_hdr->eth_arp_hdr.ar_hln = mac_size; tx_eth_hdr->eth_arp_hdr.ar_pln = ip_size; memcpy(rx_eth_arp->ar_spa, myIP, sizeof(unsigned char) * ip_size); memcpy(rx_eth_arp->ar_sha, myMAC, sizeof(unsigned char) * mac_size); memcpy(rx_eth_arp->ar_tpa, targetIP, sizeof(unsigned char) * ip_size); memcpy(rx_eth_arp->ar_sha, targetMAC, sizeof(unsigned char) * mac_size); nLength = sizeof(struct eth_hdr) + sizeof(struct eth_arp); if (nLength < 60) { for (i = 0; i < (60 - nLength); i++) *(tx_eth_arp++) = 0x00; nLength = 60; } tx_packet(tx_buffer, nLength);}void rx_arp_packet(unsigned char *rx_buffer){ struct eth_hdr *rx_eth_hdr = (struct eth_hdr *)rx_buffer; struct eth_arp *rx_eth_arp = (struct eth_arp *)(rx_buffer + sizeof(struct eth_hdr)); // make sure the ARP packet is Ethernet and IP if ((rx_eth_arp->eth_arp_hdr.ar_hrd == ARP_HRD_ETHER) && (rx_eth_arp->eth_arp_hdr.ar_pro == ETHER_IP)) { // make sure the ARP packet is a request destined for us if ((rx_eth_arp->eth_arp_hdr.ar_op == ARP_REQUEST) && (!memcmp(myIP, rx_eth_arp->ar_tpa, sizeof(unsigned char) * ip_size))) { // send a reply memcpy(targetIP, rx_eth_arp->ar_spa, sizeof(unsigned char) * ip_size); memcpy(targetMAC, rx_eth_arp->ar_sha, sizeof(unsigned char) * mac_size); tx_arp_packet(ARP_REPLY); } // else discard packet } // else discard packet}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -