Rework packet format
This commit is contained in:
parent
ed042b1d82
commit
77b6027feb
3 changed files with 39 additions and 23 deletions
|
@ -283,10 +283,12 @@ void expired_wait(struct evt_core_ctx* ctx, void* user) {
|
|||
// 2. We will not reactivate link for this packet
|
||||
if (ring_lt(rr->recv_id_late, pkt->id)) rr->recv_id_late = pkt->id;
|
||||
|
||||
/*
|
||||
// 3. Stop if packet has been received and delivered
|
||||
if (ring_le (pkt->id, rr->recv_id)) return;
|
||||
|
||||
printf("Timer reached for packet %d\n", pkt->id);
|
||||
*/
|
||||
|
||||
// 4. BLACKLIST LINK
|
||||
printf("Blacklist link=%d | ", pkt->link_num);
|
||||
|
|
24
src/packet.c
24
src/packet.c
|
@ -1,16 +1,16 @@
|
|||
#include "packet.h"
|
||||
|
||||
size_t get_full_size(struct buffer_packet* bp) {
|
||||
union abstract_packet* ap = &bp->ip.ap;
|
||||
struct abstract_packet* ap = &bp->ip.ap;
|
||||
for (int i = 0; i < bp->ap_count; i++) {
|
||||
ap = (union abstract_packet*)(&ap->raw + ap->str.size);
|
||||
ap = (struct abstract_packet*)(&ap->raw + ap->headers.size);
|
||||
}
|
||||
return &ap->raw - &bp->ip.ap.raw;
|
||||
}
|
||||
|
||||
enum FD_STATE read_packet_from_tcp(int fd, struct buffer_packet* bp) {
|
||||
ssize_t nread;
|
||||
size_t pkt_size_size = sizeof(bp->ip.ap.str.size);
|
||||
size_t pkt_size_size = sizeof(bp->ip.ap.headers.size);
|
||||
if (bp->mode != BP_READING) return FDS_ERR;
|
||||
|
||||
while (bp->aread < pkt_size_size) {
|
||||
|
@ -21,8 +21,8 @@ enum FD_STATE read_packet_from_tcp(int fd, struct buffer_packet* bp) {
|
|||
bp->aread += nread;
|
||||
}
|
||||
|
||||
while (bp->aread < bp->ip.ap.str.size) {
|
||||
nread = read(fd, &(bp->ip.ap.raw) + bp->aread, bp->ip.ap.str.size - bp->aread);
|
||||
while (bp->aread < bp->ip.ap.headers.size) {
|
||||
nread = read(fd, &(bp->ip.ap.raw) + bp->aread, bp->ip.ap.headers.size - bp->aread);
|
||||
if (nread == 0) return FDS_AGAIN;
|
||||
if (nread == -1 && errno == EAGAIN) return FDS_AGAIN;
|
||||
if (nread == -1) return FDS_ERR;
|
||||
|
@ -57,7 +57,8 @@ enum FD_STATE write_packet_to_tcp(int fd, struct buffer_packet* bp) {
|
|||
enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct udp_target* udp_t) {
|
||||
ssize_t nwrite;
|
||||
size_t bytes_to_send;
|
||||
size_t pkt_header_size = sizeof(bp->ip.ap.str) - sizeof(char);
|
||||
assert(bp->ip.ap.headers.cmd == CMD_CLEAR);
|
||||
size_t pkt_header_size = sizeof(bp->ip.ap.headers) + sizeof(bp->ip.ap.content.clear) - sizeof(char);
|
||||
struct sockaddr* addr = NULL;
|
||||
socklen_t addrlen = 0;
|
||||
if (udp_t->set) {
|
||||
|
@ -67,9 +68,9 @@ enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct udp_t
|
|||
|
||||
if (bp->mode != BP_WRITING) return FDS_ERR;
|
||||
|
||||
bytes_to_send = bp->ip.ap.str.size - pkt_header_size;
|
||||
bytes_to_send = bp->ip.ap.headers.size - pkt_header_size;
|
||||
nwrite = sendto(fd,
|
||||
&(bp->ip.ap.str.payload),
|
||||
&(bp->ip.ap.content.clear.payload),
|
||||
bytes_to_send,
|
||||
0,
|
||||
addr,
|
||||
|
@ -89,12 +90,12 @@ enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp, struct udp
|
|||
ssize_t nread;
|
||||
if (bp->mode != BP_READING) return FDS_ERR;
|
||||
|
||||
size_t pkt_header_size = sizeof(bp->ip.ap.str) - sizeof(char); // We remove the payload
|
||||
size_t pkt_header_size = sizeof(bp->ip.ap.headers) + sizeof(bp->ip.ap.content.clear) - sizeof(char); // We remove the payload
|
||||
size_t udp_packet_size = sizeof(struct internet_packet) - pkt_header_size;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_in);
|
||||
|
||||
nread = recvfrom(fd,
|
||||
&(bp->ip.ap.str.payload),
|
||||
&(bp->ip.ap.content.clear.payload),
|
||||
udp_packet_size,
|
||||
MSG_TRUNC,
|
||||
(struct sockaddr*)&udp_t->addr,
|
||||
|
@ -106,7 +107,8 @@ enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp, struct udp
|
|||
|
||||
udp_t->set = 1;
|
||||
udp_t->addrlen = addrlen;
|
||||
bp->ip.ap.str.size = nread + pkt_header_size;
|
||||
bp->ip.ap.headers.size = nread + pkt_header_size;
|
||||
bp->ip.ap.headers.cmd = CMD_CLEAR;
|
||||
|
||||
bp->mode = BP_WRITING;
|
||||
bp->awrite = 0;
|
||||
|
|
26
src/packet.h
26
src/packet.h
|
@ -1,4 +1,5 @@
|
|||
#pragma once
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
@ -28,26 +29,37 @@ enum BP_MODE {
|
|||
BP_WRITING
|
||||
};
|
||||
|
||||
enum PKT_FLAGS {
|
||||
PKT_CONTROL = 1 << 0
|
||||
enum PKT_CMD {
|
||||
CMD_HEALTH,
|
||||
CMD_CLEAR,
|
||||
CMD_XOR
|
||||
};
|
||||
|
||||
union abstract_packet {
|
||||
struct abstract_packet {
|
||||
char raw;
|
||||
|
||||
struct {
|
||||
enum PKT_CMD cmd;
|
||||
uint16_t size;
|
||||
uint16_t port;
|
||||
} headers;
|
||||
|
||||
union {
|
||||
struct {
|
||||
uint16_t id;
|
||||
uint8_t bitfield;
|
||||
uint8_t prevlink;
|
||||
uint16_t deltat;
|
||||
uint8_t flags;
|
||||
} health;
|
||||
struct {
|
||||
uint16_t id;
|
||||
uint16_t port;
|
||||
char payload;
|
||||
} str;
|
||||
} clear;
|
||||
} content;
|
||||
};
|
||||
|
||||
struct internet_packet {
|
||||
union abstract_packet ap;
|
||||
struct abstract_packet ap;
|
||||
char rest[1499]; // MTU = 1500, 1 byte in the union as payload
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue