7 #include "net/ethernet.h"
9 #include "net/endianess.h"
14 #define MAX_CONNECTIONS 64
18 int tcp_find_connection(uint8_t address[4],
size_t port) {
19 for(
int i = 0; i < MAX_CONNECTIONS; i++) {
20 if(
memcmp((uint8_t*)&tcp_connections[i].dest_ip_addr, address, 4) == 0
21 && tcp_connections[i].source_port == port
22 && tcp_connections[i].status != TCP_NONE) {
30 bool tcp_new_connection(
netcard_entry_t* card, uint8_t address[4],
size_t port,
size_t seq_nr) {
31 uint8_t empty_addr[4] = {0, 0, 0, 0};
34 for(
int i = 0; i < MAX_CONNECTIONS; i++) {
35 if(
memcmp((uint8_t*)&tcp_connections[i].dest_ip_addr, empty_addr, 4) == 0
36 && tcp_connections[i].source_port == 0
37 && tcp_connections[i].status == TCP_NONE) {
47 memcpy(&tcp_connections[index].dest_ip_addr, address, 4);
48 tcp_connections[index].source_port = port;
49 tcp_connections[index].status = TCP_CREATED;
50 tcp_connections[index].seq = seq_nr;
51 tcp_connections[index].card = card;
57 qemu_note(
"!!!!!!!!!!!!!!!!!!!!!!!! TCP !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
59 ETH_IPv4_PKG *ipv4 = (ETH_IPv4_PKG *)((
size_t)packet -
sizeof(ETH_IPv4_PKG));
60 size_t data_payload_size = ipv4->TotalLength -
sizeof(ETH_IPv4_PKG);
62 qemu_log(
"Data payload size: %d", data_payload_size);
64 packet->source = ntohs(packet->source);
65 packet->destination = ntohs(packet->destination);
67 qemu_log(
"FROM: %u.%u.%u.%u", ipv4->Source[0], ipv4->Source[1], ipv4->Source[2], ipv4->Source[3]);
69 qemu_note(
"SRC: %d; DEST: %d", packet->source, packet->destination);
71 qemu_note(
"FLAGS: SYN: %d; ACK: %d; PSH: %d; FIN: %d", packet->syn, packet->ack, packet->psh, packet->fin);
73 packet->ack_seq = ntohl(packet->ack_seq);
74 packet->seq = ntohl(packet->seq);
78 if(tcp_find_connection(ipv4->Source, packet->source) == -1) {
79 tcp_new_connection(card, ipv4->Source, packet->source, packet->seq);
80 qemu_ok(
"Created new connection!");
82 idx = tcp_find_connection(ipv4->Source, packet->source);
84 qemu_note(
"Connection idx: %d", idx);
86 bool is_stage_1 = packet->syn && !packet->ack && !packet->psh && !packet->fin;
87 bool is_stage_2 = !packet->syn && packet->ack && !packet->psh && !packet->fin;
88 bool is_push = !packet->syn && !packet->ack && packet->psh && !packet->fin;
94 char* options = (
char*)(sendable_packet) +
sizeof(
tcp_packet_t);
106 tcp_connections[idx].seq = rand();
107 tcp_connections[idx].ack = sendable_packet->seq + 1;
109 sendable_packet->ack = 1;
110 sendable_packet->seq = ntohl(tcp_connections[idx].seq);
111 sendable_packet->ack_seq = ntohl(tcp_connections[idx].ack);
113 uint16_t dest = ntohs(sendable_packet->destination);
114 uint16_t src = ntohs(sendable_packet->source);
115 sendable_packet->source = dest;
116 sendable_packet->destination = src;
118 sendable_packet->doff = 7;
120 ipv4_send_packet(tcp_connections[idx].card, ipv4->Source, sendable_packet,
sizeof(
tcp_packet_t) + 8, ETH_IPv4_HEAD_TCP);
123 kfree(sendable_packet);
Основные определения ядра
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
int32_t memcmp(const char *s1, const char *s2, size_t n)
Сравнение массивов