-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip.c
More file actions
230 lines (194 loc) · 7.3 KB
/
Copy pathip.c
File metadata and controls
230 lines (194 loc) · 7.3 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "util.h"
#include "stack.h"
#include "icmp.h"
#include "ip.h"
#include "cs431vde.h"
#include "crc32.h"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <poll.h>
//function to extract and process ip header fields
void handle_packet(ssize_t len, struct frame_fields *frame_f, uint8_t *or_frame, struct ip_header *packet, struct packet_info *packet_inf, struct icmp *curr_icmp, struct table_r *routing_table, struct arp_cache *arp_cache__, struct interface *interface_list_, int *receiver_id, struct tcp_connection *tcp_connection_table_){
//receiver_id is the index of the interface that received the packet
//printf("handling ip packet...\n");
struct in_addr ip_addr;
int lg_pfx_idx = -1; //longest subnet prefix index
int lg_prefix = -1;
//int arp_idx;
int error = 0;
int transmitter_idx = *receiver_id;
uint8_t *final_dest_addr;
ssize_t curr_len = len; //remove the +4 for normal len (THIS IS FOR TCP TESTING)
int forwarding = 1;
int tcp_output = 0;
//convert dest address to ip str
inet_ntop(AF_INET, &(packet->dest_addr), packet_inf->dest_ip_addr, INET_ADDRSTRLEN);
//convert src address to ip str
inet_ntop(AF_INET, &(packet->src_addr), packet_inf->src_ip_addr, INET_ADDRSTRLEN);
//convert dest ip to bin
if(!inet_aton(packet_inf->dest_ip_addr, &ip_addr)){
printf("error converting ip to binary\n");
}
packet_inf->valid_length = (ntohs(packet->total_length) > 20) ? 1 : 0;
packet_inf->valid_checksum = (ip_checksum(or_frame + 14, 20) == 0) ? 1 : 0;
/*
* comment out for TCP testing
if(!packet_inf->valid_checksum) return;
*/
if(!packet_inf->valid_length) return;
if(packet->ttl == 1){
curr_icmp->type = 11;
curr_icmp->code = 0;
error = 1;
//return;
}else if(packet->ttl < 1){
curr_icmp->type = 11;
curr_icmp->code = 0;
error = 1;
printf("dropping packet from %s to %s (TTL exceeded)\n", packet_inf->src_ip_addr, packet_inf->dest_ip_addr);
return;
}
/*destination ip matches local interface*/
if(is_interface(interface_list_, packet->dest_addr) >= 0){
if(packet->protocol != 6){
//dest_addr = interface_list_[interface_idx].mac_addr;
printf("digesting packet\n");
return;
}else{
forwarding = 0;
final_dest_addr = frame_f->src_addr; //transmit back to original sender
}
}
if(!error && forwarding){
for(int i=0; i<TABLE_LENGTH; i++){
char genmask_str[INET_ADDRSTRLEN];
char dest_str[INET_ADDRSTRLEN];
struct in_addr dest_addr, genmask_addr, result;
int curr_lg_prefix;
//convert addresses to ip
inet_ntop(AF_INET, routing_table[i].genmask, genmask_str, INET_ADDRSTRLEN);
inet_ntop(AF_INET, routing_table[i].dest, dest_str, INET_ADDRSTRLEN);
//convert to bin
inet_aton(genmask_str, &genmask_addr);
inet_aton(dest_str, &dest_addr);
//bitwise-and
result.s_addr = ip_addr.s_addr & genmask_addr.s_addr;
//check matching dest addr
if(result.s_addr == dest_addr.s_addr){
curr_lg_prefix = __builtin_popcount(ntohl(genmask_addr.s_addr));
if(curr_lg_prefix > lg_prefix){
lg_prefix = curr_lg_prefix;
lg_pfx_idx = i;
}
}
}
char *real_path;
if(lg_pfx_idx != -1){
//save the interface id that matches the dest addr found in routing table
transmitter_idx = routing_table[lg_pfx_idx].interface_id;
//check if packet is destined to local network
if (memcmp(routing_table[lg_pfx_idx].gateway, "\x00\x00\x00\x00", 4) == 0 ){
real_path = (char *)packet->dest_addr;
}else{
real_path = (char *)routing_table[lg_pfx_idx].gateway;
}
// arp lookup
int found_mac = 0;
for(int i=0; i<CACHE_LENGTH; i++){
if(memcmp(arp_cache__[i].ip_addr, real_path, 4) == 0){
//arp_idx = i;
final_dest_addr = arp_cache__[i].mac_addr;
found_mac = 1;
}
}
//check for network errors
if(!found_mac){
//host unreachable
curr_icmp->type = 3;
curr_icmp->code = 1;
error = 1;
}
}else{
//network unreacheable
curr_icmp->type = 3;
curr_icmp->code = 0;
error = 1;
}
if(!error) packet->ttl--;
}
//check for error again
if(error){
transmitter_idx = *receiver_id; //use the interface that received the packet
}
// In handle_packet
if(packet->protocol == 6) {
int transmission_status = handle_tcp(len, frame_f, or_frame, packet, packet_inf, tcp_connection_table_, 0);
tcp_output = transmission_status == TCP_NONE ? 1 : 0; //only output stuff when sending ACKs
if(transmission_status >= 1){
//RCVD PSH/ACK
curr_len -= transmission_status; //decrease length
encapsulation(frame_f, packet, &curr_len, or_frame, final_dest_addr, curr_icmp,
interface_list_, error, packet_inf, transmitter_idx,
tcp_connection_table_, 0);
send_ethernet_frame(interface_list_[transmitter_idx].switch_[1], or_frame, curr_len);
open_connections(tcp_connection_table_);
// interaction mode after replying
printf("--------------------------------------\n");
printf("\n");
return;
}
// First encapsulation (for all cases)
encapsulation(frame_f, packet, &curr_len, or_frame, final_dest_addr, curr_icmp,
interface_list_, error, packet_inf, transmitter_idx,
tcp_connection_table_, 0);
switch(transmission_status) {
case TCP_NONE:
// No transmission
break;
case TCP_REG:
// Send the packet (already encapsulated)
send_ethernet_frame(interface_list_[transmitter_idx].switch_[1], or_frame, curr_len);
//printf("TCP REPLY\n");
printf("Sending packet to %s\n", packet_inf->dest_ip_addr);
break;
case -2:
// Send the packet (already encapsulated)
send_ethernet_frame(interface_list_[transmitter_idx].switch_[1], or_frame, curr_len);
//printf("TCP REPLY\n");
printf("Sending packet to %s\n", packet_inf->dest_ip_addr);
open_connections(tcp_connection_table_);
break;
case TCP_FIN_ACK:
// Send ACK (already encapsulated)
send_ethernet_frame(interface_list_[transmitter_idx].switch_[1], or_frame, curr_len);
//printf("Sending ACK to %s\n", packet_inf->dest_ip_addr);
// Prepare FIN
handle_tcp(len, frame_f, or_frame, packet, packet_inf, tcp_connection_table_, 1);
encapsulation(frame_f, packet, &curr_len, or_frame, final_dest_addr, curr_icmp, interface_list_, error, packet_inf, transmitter_idx, tcp_connection_table_, 1);
// Send FIN
send_ethernet_frame(interface_list_[transmitter_idx].switch_[1], or_frame, curr_len);
//printf("Sending FIN/ACK to %s\n", packet_inf->dest_ip_addr);
printf("Closing connection\n"); //should be more descriptive
break;
}
printf("\n");
if(tcp_output) open_connections(tcp_connection_table_);
// interaction begins here
printf("--------------------------------------\n");
printf("\n");
return;
}
//regular forwarding
encapsulation(frame_f, packet, &curr_len, or_frame, final_dest_addr, curr_icmp,
interface_list_, error, packet_inf, transmitter_idx,
tcp_connection_table_, 0);
send_ethernet_frame(interface_list_[transmitter_idx].switch_[1], or_frame, curr_len);
printf("forwading packet to %s\n", packet_inf->dest_ip_addr);
}