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

📄 arp.c

📁 Pic Nic 16f877/18f452控制 rtl8019as 原代码
💻 C
字号:
/*

 * arp.c

 *

 * Address Resolution Protocol (ARP) specific functions

 *

 */



//The arp packet structure

_ARP_PACKET ARP_PACKET;



//Load Ethernet Frame Header to the NIC

void load_ethernet_header(void) {



   //Put nic in the beggining of tx buffer area

   remote_dma_setup(WRITE,XMT_BUF_START);



   //Write source and dst mac to the nic

   writeNicTxBuffer(ETHERNET_HEADER.mac_source,6);

   writeNicTxBuffer(physical_address,6);

}



//Swap ARP Packet Bytes

static void swapARP(_ARP_PACKET*  header) {

   header->HardwareType = swap16(header->HardwareType);

   header->Protocol = swap16(header->Protocol);

   header->Operation = swap32(header->Operation);

}



//Sends an ARP answer telling the client what mac address our IP have

void arpResponse(void) {

   int i;



   //Prepares and ARP Response Packet

   load_ethernet_header();



   //Send Protocol Specific definitions

   outportw(NIC_DATA, ARP );

   outportw(NIC_DATA, 0x0001 );

   outportw(NIC_DATA, 0x0800 );

   outportw(NIC_DATA, 0x0604 );

   outportw(NIC_DATA, 0x0002 );



   //Source and Destination MAC and IP Addresses

   writeNicTxBuffer(physical_address,6);

   writeNicTxBuffer(my_ip,4);

   writeNicTxBuffer(ETHERNET_HEADER.mac_source,6);

   writeNicTxBuffer(&ARP_PACKET.SenderIPAddr,4);



   //Pad to 46 bytes (46-28=18) cause we have a minimum size to send

   for (i=0 ; i<18 ;i++)

      outport(NIC_DATA, 0x00);



   //Packet assmebled so let's send it

   sendPacket(60);

}



//Get an ARP request, check some conditions and send the ARP answer

//Computer asks "WHO HAS ARP X"

//We answer with our MAC if it's searching for us

void procesa_arp(void) {



   //Get ARP Packet Data and adjust it

   getNicData(&ARP_PACKET,24);

   swapARP(&ARP_PACKET);



   //Is the ARP Packet asking for picnic's ip address MAC?

   //If not just abort

   if (my_ip[0] != ARP_PACKET.TargetIPAddr.ip0

    || my_ip[1] != ARP_PACKET.TargetIPAddr.ip1

    || my_ip[2] != ARP_PACKET.TargetIPAddr.ip2

    || my_ip[3] != ARP_PACKET.TargetIPAddr.ip3) {

      fin_rx();

      break;

   }



   #ifdef DEBUG

      printf("IP: %u.%u.%u.%u \n\r",ARP_PACKET.SenderIPAddr.ip0, ARP_PACKET.SenderIPAddr.ip1,

            ARP_PACKET.SenderIPAddr.ip2, ARP_PACKET.SenderIPAddr.ip3);

      printf("Dst IP: %u.%u.%u.%u \n\r",ARP_PACKET.TargetIPAddr.ip0, ARP_PACKET.TargetIPAddr.ip1,

            ARP_PACKET.TargetIPAddr.ip2, ARP_PACKET.TargetIPAddr.ip3);

      printf("Type: %Lx\n\r", ARP_PACKET.Operation);

   #endif



   //If we arrived here, we shall answer politely

   fin_rx();

   arpResponse();

}

⌨️ 快捷键说明

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