-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathicmp.c
81 lines (69 loc) · 1.76 KB
/
icmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#include "icmp.h"
#include "ip.h"
#include "tbuf.h"
// for test
#include "arp.h"
static void icmp_echo(struct tbuf *in);
static void icmp_tsr(struct tbuf *in);
int icmp_out(struct tbuf *buf)
{
}
void icmp_in(struct tbuf *buf)
{
struct ethhdr2 *eth;
struct icmphdr *icmp;
tbuf_header(buf, 0);
eth = (struct ethhdr2 *) buf->payload;
tbuf_header(buf, IP_HLEN);
icmp = (struct icmphdr *)buf->payload;
switch (icmp->type) {
case ICMP_ECHO:
dump_mac(eth->sm);
printf("type:%d, code:%d\n", icmp->type, icmp->code);
icmp_echo(buf);
break;
case ICMP_TS:
icmp_tsr(buf);
break;
}
}
void icmp_init()
{
l3protos[IP_ICMP].type = IP_ICMP;
l3protos[IP_ICMP].handler = (void *) &icmp_in;
printf("Initializing ICMP\n");
}
static void icmp_echo(struct tbuf *in)
{
struct iphdr *in_ip;
struct iphdr *out_ip;
struct icmphdr *in_icmp;
struct icmphdr *out_icmp;
struct tbuf *out;
__u32 saddr;
__u32 daddr;
__u16 tot_len;
tbuf_header(in, ETH_HLEN);
in_ip = (struct iphdr *) in->payload;
saddr = in_ip->daddr;
daddr = in_ip->saddr;
tot_len = htons(in_ip->tot_len);
out = tbuf_malloc(in->len);
// just take the input's payload
memcpy(out->payload, in->start, in->len);
tbuf_header(out, IP_HLEN);
out_icmp = (struct icmphdr *) out->payload;
out_icmp->type = ICMP_ER;
out_icmp->check = 0;
out_icmp->check = htons(checksum_generic(out_icmp, in->len - IP_HLEN));
printf("ICMP out: ");
dump_ip(in_ip->saddr);
ip_out(out, in_ip->daddr, in_ip->saddr, 32, IP_ICMP);
}
static void icmp_tsr(struct tbuf *in)
{
}