BlinkenArea - GitList
Repositories
Blog
Wiki
mips_sys
Code
Commits
Branches
Tags
Search
Tree:
902aa40
Branches
Tags
master
mips_sys
fw
icmp.c
replace email address in headers with blinkenarea address
Stefan Schuermans
commited
902aa40
at 2012-05-21 17:42:50
icmp.c
Blame
History
Raw
/* MIPS I system * Copyright 2011-2012 Stefan Schuermans <stefan@blinkenarea.org> * Copyleft GNU public license V2 or later * http://www.gnu.org/copyleft/gpl.html */ #include "checksum.h" #include "ethernet.h" #include "icmp.h" #include "ip.h" #include "macros.h" #include "nethelp.h" /** * @brief send an ICMP packet * @param[in] ptr pointer to data of packet * @param[in] sz size of packet * * ptr must point to a icmp_packet * with icmp_hdr.type, icmp_hdr.code and ip_hdr.sest already initialized */ static void icmp_send(void *ptr, unsigned int sz) { struct icmp_packet *icmp_pack; unsigned short chk; // packet too short if (sz < sizeof(struct icmp_packet)) return; icmp_pack = ptr; // fill in header values icmp_pack->icmp_hdr.chk = 0x0000; // generate checksum chk = checksum(&icmp_pack->icmp_hdr, sz - sizeof(struct ethernet_header) - sizeof(struct ip_header), 0x0000, 0x0000); icmp_pack->icmp_hdr.chk = htons(chk); // send ICMP packet icmp_pack->ip_hdr.proto = 0x01; // ICMP ip_send(icmp_pack, sz); } /** * @brief process a received ICMP echo request packet * @param[in] ptr pointer to data of packet * @param[in] sz size of packet */ static void icmp_echo_req_recv(void *ptr, unsigned int sz) { struct icmp_echo_packet *icmp_echo_pack; // packet too short if (sz < sizeof(struct icmp_echo_packet)) return; icmp_echo_pack = ptr; // code not 0 if (icmp_echo_pack->icmp_hdr.code != 0x00) return; // send an ICMP echo reply // - use same buffer to send reply // - this saves us from needing to allocate a new buffer // - this saves us from needing to copy echo_hdr.id, echo_hdr.seq and data icmp_echo_pack->icmp_hdr.type = 0x00; // ICMP echo reply icmp_echo_pack->icmp_hdr.code = 0x00; // destination IP is source IP of request ip_cpy(icmp_echo_pack->ip_hdr.dest, icmp_echo_pack->ip_hdr.src); icmp_send(icmp_echo_pack, sz); } /** * @brief process a received ICMP packet * @param[in] ptr pointer to data of packet * @param[in] sz size of packet */ void icmp_recv(void *ptr, unsigned int sz) { struct icmp_packet *icmp_pack; // packet too short if (sz < sizeof(struct icmp_packet)) return; icmp_pack = ptr; // test checksum if (checksum(&icmp_pack->icmp_hdr, sz - sizeof(struct ethernet_header) - sizeof(struct ip_header), 0x0000, 0x0000)) return; // branch according to type switch (icmp_pack->icmp_hdr.type) { // ICMP echo request case 0x08: icmp_echo_req_recv(ptr, sz); break; } }