📄 ip.c
字号:
/*
* ip.c
*
* IP Level Specific functions
*
*/
//Structure definitions
_IP_HEADER IP_HEADER;
//Have you ever heard about little endian and big endian machines and how they store data?
//Well, we have to deal with it here, inverting the IP values
static void swapIPHeader(_IP_HEADER* header) {
header->TotalLength = swap16(header->TotalLength);
header->Identification = swap16(header->Identification);
header->FragmentInfo = swap16(header->FragmentInfo);
header->HeaderChecksum = swap16(header->HeaderChecksum);
}
//Process IP request reading from NIC and putting in correct vars
void processIP(void) {
//Get 20 Bytes IP Header from nic, corrects it and put in IP_HEADER Structure
getNicData(&IP_HEADER,20);
swapIPHeader(&IP_HEADER);
//Set other needed vars with sizes
hdr_len = (IP_HEADER.VersionIHL & 0x0F) << 2;
opt_len = hdr_len - 20;
version = IP_HEADER.VersionIHL >> 4;
#ifdef DEBUG
printf("Src IP: %u.%u.%u.%u \n\r", IP_HEADER.SourceAddress.ip0, IP_HEADER.SourceAddress.ip1,
IP_HEADER.SourceAddress.ip2, IP_HEADER.SourceAddress.ip3);
printf("Prot: %x\n\r", IP_HEADER.Protocol);
printf("Ident: %Lx\n\r", IP_HEADER.Identification);
#endif
}
//Send IP Header to the correct place in nic's buffer
void load_IP_header(long int IP_packet_length, int IP_send_protocol) {
//Vars
int i;
long int prev_chksum;
//Set IP on the Ethernet Field for Protocol Type
outport(NIC_DATA, IP >> 8); // IP packet
outport(NIC_DATA, IP);
//Go filling the IP Header fields
chksum = 0;
outportw(NIC_DATA, 0x4500);
outportw(NIC_DATA, IP_packet_length);
outportw(NIC_DATA, IP_HEADER.Identification); //
outportw(NIC_DATA, 0x0000); // Okay to fragment
outportw(NIC_DATA, 0x8000 + IP_send_protocol); // Time-To-Live set a 255
//Compute and send header checksum
calc_chksum( make16(my_ip[0],my_ip[1]));
calc_chksum( make16(my_ip[2],my_ip[3]));
calc_chksum( make16(IP_HEADER.SourceAddress.ip0,IP_HEADER.SourceAddress.ip1));
calc_chksum( make16(IP_HEADER.SourceAddress.ip2,IP_HEADER.SourceAddress.ip3));
outportw(NIC_DATA, ~chksum); // Header Checksum
//And Write to the NIC the source IP (picnic ip) and destination IP (client)
writeNicTxBuffer(my_ip,4);
writeNicTxBuffer(&IP_HEADER.SourceAddress,4);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -