diff options
| author | Miguel <m.i@gmx.at> | 2018-09-25 21:46:15 +0200 |
|---|---|---|
| committer | Miguel <m.i@gmx.at> | 2018-09-25 21:46:15 +0200 |
| commit | f93f24f3a5ca4692a28ab4970bbf158e1f486074 (patch) | |
| tree | 2263b4291d470b8b606ed899d10d352d02ee93ea /net/icmp.c | |
| parent | b58e7bc7cb8ce4fc6f824761ac8ef3920e7dfcc1 (diff) | |
pinging fool os works
Diffstat (limited to 'net/icmp.c')
| -rw-r--r-- | net/icmp.c | 41 |
1 files changed, 38 insertions, 3 deletions
@@ -1,20 +1,55 @@ #include "inet.h" #include "icmp.h" +#include "ipv4.h" #include "log.h" -bool icmp_incoming(struct netdev *dev,struct icmp_v4 *data, uint32_t count_bytes) +#include "lib/string/string.h" + +bool icmp_reply(struct netdev *dev,struct eth_hdr *hdr) +{ + unsigned char tmp[6]; + memcpy(tmp,hdr->dmac,6); + memcpy(hdr->dmac,hdr->smac,6); + memcpy(hdr->smac,tmp,6); + + struct ipv4_hdr *ipv4=hdr->payload; + + + uint32_t tmp2; + tmp2=ipv4->saddr; + ipv4->saddr=ipv4->daddr; + ipv4->daddr=tmp2; + uint32_t daddr; // destination ip address + + struct icmp_v4 *data=(uint32_t *)ipv4+ipv4->ihl; + data->type=ICMP_ECHO_REPLY; + data->csum=0; + ipv4->csum=0; + + data->csum=htons(checksum(data,ipv4->len-ipv4->ihl*4)); + ipv4->csum=htons(checksum(ipv4,ntohs(ipv4->len))); + + dev->transmit(hdr,ntohs(ipv4->len)+14); // 14bytes for link2 + + return true; +} + +bool icmp_incoming(struct netdev *dev,struct eth_hdr *hdr) { + struct ipv4_hdr *ipv4=hdr->payload; + struct icmp_v4 *data=(uint32_t *)ipv4+ipv4->ihl; + klog ("icmp type=%d",data->type); klog ("icmp code=%d",data->code); klog ("icmp csum=0x%04x",ntohs(data->csum)); data->csum=0; - klog("expected checksum = 0x%04X",checksum(data,count_bytes)); + klog("expected checksum = 0x%04X",checksum(data,ipv4->len-ipv4->ihl*4)); if(data->type==ICMP_ECHO_REQUEST) // echo request { struct icmp_v4_echo *echo=data->data; klog ("received echo request id=%d, seq=%d, data=%d ",ntohs(echo->id),ntohs(echo->seq),echo->data); -// icmp_reply(); /// TODO watchout that this is the memory managed by the network card we are dealing with!!. fix this later. + icmp_reply(dev,hdr); /// TODO watchout that this is the memory managed by the network card we are dealing with!!. fix this later. } return true; |
