summaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
authorMiguel <m.i@gmx.at>2018-09-25 21:46:15 +0200
committerMiguel <m.i@gmx.at>2018-09-25 21:46:15 +0200
commitf93f24f3a5ca4692a28ab4970bbf158e1f486074 (patch)
tree2263b4291d470b8b606ed899d10d352d02ee93ea /net
parentb58e7bc7cb8ce4fc6f824761ac8ef3920e7dfcc1 (diff)
pinging fool os works
Diffstat (limited to 'net')
-rw-r--r--net/icmp.c41
-rw-r--r--net/icmp.h2
-rw-r--r--net/ipv4.c2
3 files changed, 40 insertions, 5 deletions
diff --git a/net/icmp.c b/net/icmp.c
index 038e32a..54dc67b 100644
--- a/net/icmp.c
+++ b/net/icmp.c
@@ -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;
diff --git a/net/icmp.h b/net/icmp.h
index c2c5e8f..50613bb 100644
--- a/net/icmp.h
+++ b/net/icmp.h
@@ -25,4 +25,4 @@ struct icmp_v4_dst_unreachable {
uint8_t data[];
} __attribute__((packed));
-bool icmp_incoming(struct netdev *dev,struct icmp_v4 *data, uint32_t count_bytes);
+bool icmp_incoming(struct netdev *dev,struct eth_hdr *hdr);
diff --git a/net/ipv4.c b/net/ipv4.c
index aa8502e..b7e6a53 100644
--- a/net/ipv4.c
+++ b/net/ipv4.c
@@ -13,7 +13,7 @@ bool ipv4_incoming(struct netdev *dev,struct eth_hdr *hdr)
klog("ipv4 header len=%d",ipv4->ihl);
if(ipv4->proto==IPV4_P_ICMP)
{
- icmp_incoming(dev,((uint32_t *)ipv4)+ipv4->ihl,ntohs(ipv4->len)-ipv4->ihl*4);
+ icmp_incoming(dev,hdr);
}
return true;