-
Notifications
You must be signed in to change notification settings - Fork 5
/
ip.c
169 lines (143 loc) · 3.41 KB
/
ip.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <netinet/in.h>
#include "tbuf.h"
#include "iface.h"
#include "icmp.h"
#include "ip.h"
#include "checksum.h"
#include "iface.h"
#include "arp.h"
void ip_to_string(char *ip, __u32 yiaddr)
{
int a, b, c, d;
if (ip == NULL) {
return;
}
a = yiaddr >> 24;
b = (yiaddr << 8) >> 24;
c = (yiaddr << 16) >> 24;
d = (yiaddr << 24) >> 24;
if (a > 255 || b > 255 || c > 255 || d > 255 || a < 0 || b < 0 || c < 0
|| d < 0)
return;
sprintf(ip, "%d.%d.%d.%d", d, c, b, a);
}
void dump_ip(__u32 addr)
{
char ip[30];
ip_to_string(ip, addr);
printf("ip: %s\n", ip);
}
int ip_in(struct tbuf *buf)
{
struct iphdr *ip;
__u16 checksum;
tbuf_header(buf, ETH_HLEN);
ip = (struct iphdr *) buf->payload;
checksum = checksum_generic((__u8 *)ip, ip->ihl * 4);
// Discard bogus packets
if (checksum) {
printf("screwed packet, dropped\n");
return 0;
}
if (!get_iface_by_ip(ip->daddr)) {
// Go forward processing
return 0;
}
if (l3protos[ip->protocol].type == ip->protocol) {
l3protos[ip->protocol].handler(buf);
}
return 0;
}
int ip_out(struct tbuf *buf, __u32 src, __u32 dst, __u8 ttl,
__u8 protocol)
{
struct sockaddr_in sin;
struct iphdr *ip;
struct ethhdr2 *ether;
struct route *dstroute;
struct iface *dstdev;
// rewind to the IP head of payload
tbuf_header(buf, ETH_HLEN);
ip = (struct iphdr *)buf->payload;
ip->version = 4;
ip->ttl = ttl;
ip->saddr = src;
ip->daddr = dst;
ip->protocol = protocol;
ip->check = 0;
ip->check = htons(checksum_generic((__u8 *)ip, ip->ihl * 4));
// rewind to the ethernet head of payload
tbuf_header(buf, 0);
ether = (struct ethhdr2 *) buf->payload;
memset(&sin, 0, sizeof(sin));
dstroute = ip_out_route(dst);
if (dstroute) {
ether_out(buf, ETHTYPE_IP, dstroute->dev);
} else {
printf("No route for %x\n", dst);
}
return 0;
}
void set_default_route(struct route* gw)
{
struct route* p = GETROUTE();
__u32 flag = 0;
while (p) {
if (gw == p) {
p->isdefault = 1;
flag = 1;
} else {
p->isdefault = 0;
}
p = p->next;
}
// Add this to linked list
if (!flag) {
gw->next = p->next;
p->next = gw;
gw->isdefault = 1;
}
default_route = gw;
}
struct route* ip_out_route(__u32 daddr)
{
struct route* p = GETROUTE();
while(p->next != NULL) {
printf("routing\n");
if (ip_netcmp(p->gateway, daddr, p->mask)) {
return p;
}
p = p->next;
}
return default_route;
}
int ip_init()
{
// Initialize route table
route_table = malloc(sizeof(struct route));
route_table->next = NULL;
memset(l3protos, 0, PROTO_NUM);
icmp_init();
return 0;
}
int main()
{
ip_init();
iface_init();
arp_init();
l2_init();
config_iface(get_iface_by_name("eth0"), 1500, htonl(0xC0A80109), htonl(0xffffff00), htonl(0XC0A80101));
struct route *gw = (struct route *) malloc(sizeof(struct route));
gw->dev = get_iface_by_name("eth0");
gw->isdefault = 1;
gw->gateway = htonl(0XC0A80101);
gw->mask = htonl(0XFFFFFF00);
ADDROUTE(gw);
set_default_route(gw);
while(1){
sleep(10);
}
}