Skip to content

Commit

Permalink
chesums / improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Aug 9, 2024
1 parent 4d2fda4 commit c31cdb7
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 8 deletions.
8 changes: 7 additions & 1 deletion tunnels/adapters/device/tun/tun_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static void upStream(tunnel_t *self, context_t *c)
tun_device_state_t *state = TSTATE((tunnel_t *) self);

tun_device_t *tdev = state->tdev;
writeToTunDevce(tdev,c->payload);
writeToTunDevce(tdev, c->payload);

dropContexPayload(c);
destroyContext(c);
Expand Down Expand Up @@ -101,6 +101,12 @@ tunnel_t *newTunDevice(node_instance_context_t *instance_info)
tunnel_t *t = newTunnel();

state->tdev = createTunDevice(state->name, false, t, onIPPacketReceived);

if (state->tdev == NULL)
{
LOGF("TunDevice: could not create device");
return NULL;
}
assignIpToTunDevice(state->tdev, state->ip_present, state->subnet_mask);
bringTunDeviceUP(state->tdev);

Expand Down
63 changes: 61 additions & 2 deletions tunnels/layer3/sender/sender.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#include "hsocket.h"
#include "loggers/network_logger.h"
#include "managers/node_manager.h"
#include "packet_types.h"
#include "utils/jsonutils.h"
#include "utils/stringutils.h"
#include "utils/mathutils.h"

typedef struct layer3_senderstate_s
{
Expand All @@ -17,9 +18,68 @@ typedef struct layer3_sendercon_state_s
void *_;
} layer3_sendercon_state_t;

static void printSendingIPPacketInfo(const unsigned char *buffer, unsigned int len)
{
char src_ip[INET6_ADDRSTRLEN];
char dst_ip[INET6_ADDRSTRLEN];
char logbuf[2048];
int rem = sizeof(logbuf);
char *ptr = logbuf;
int ret;

uint8_t version = buffer[0] >> 4;

if (version == 4)
{
struct ipv4header *ip_header = (struct ipv4header *) buffer;

inet_ntop(AF_INET, &ip_header->saddr, src_ip, INET_ADDRSTRLEN);
inet_ntop(AF_INET, &ip_header->daddr, dst_ip, INET_ADDRSTRLEN);

ret = snprintf(ptr, rem, "Sending: => From %s to %s, Data: ", src_ip, dst_ip);
}
else if (version == 6)
{
struct ipv6header *ip6_header = (struct ipv6header *) buffer;

inet_ntop(AF_INET6, &ip6_header->saddr, src_ip, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &ip6_header->daddr, dst_ip, INET6_ADDRSTRLEN);

ret = snprintf(ptr, rem, "Sending: From %s to %s, Data: ", src_ip, dst_ip);
}
else
{
ret = snprintf(ptr, rem, "Sending: => Unknown IP version, Data: ");
}

ptr += ret;
rem -= ret;

for (int i = 0; i < (int) min(len,640); i++)
{
ret = snprintf(ptr, rem, "%02x ", buffer[i]);
ptr += ret;
rem -= ret;
}
*ptr = '\0';

LOGD(logbuf);
}

static void upStream(tunnel_t *self, context_t *c)
{
layer3_senderstate_t *state = TSTATE(self);
printSendingIPPacketInfo(rawBuf(c->payload),bufLen(c->payload));
// reuseContextPayload(c);
// shift_buffer_t* buf = popBuffer(getContextBufferPool(c));
// writeRaw(buf, const void *const restrict buffer, const unsigned int len)
packet_mask *packet = (packet_mask *) (rawBufMut(c->payload));

if (packet->ip4_header.version == 4)
{
int ip_header_len = packet->ip4_header.ihl * 4;
packet->ip4_header.check = standardCheckSum((void *) packet, ip_header_len);
}

state->tun_device_tunnel->upStream(state->tun_device_tunnel, c);
}
Expand Down Expand Up @@ -85,7 +145,6 @@ tunnel_t *newLayer3Sender(node_instance_context_t *instance_info)
t->upStream = &upStream;
t->downStream = &downStream;


return t;
}

Expand Down
22 changes: 17 additions & 5 deletions tunnels/layer3/tcp_manipulator/tcp_manipulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,29 @@ typedef struct layer3_tcp_manipulator_con_state_s

} layer3_tcp_manipulator_con_state_t;

static inline void handleResetBitAction(struct tcpheader *tcp_header, dynamic_value_t *reset_bit)
static void reCalculateCheckSum(struct tcpheader *tcp_header, int len)
{
tcp_header->check = tcpCheckSum(tcp_header, len);
}

static inline void handleResetBitAction(struct tcpheader *tcp_header, dynamic_value_t *reset_bit, int tlen)
{
switch ((enum bitaction_dynamic_value_status) reset_bit->status)
{
case kDvsOff:
tcp_header->rst = 0;
reCalculateCheckSum(tcp_header, tlen);

break;
case kDvsOn:
tcp_header->rst = 1;
reCalculateCheckSum(tcp_header, tlen);

break;
case kDvsToggle:
tcp_header->rst = ! tcp_header->rst;
reCalculateCheckSum(tcp_header, tlen);

break;
default:
case kDvsNothing:
Expand Down Expand Up @@ -66,7 +77,7 @@ static void upStream(tunnel_t *self, context_t *c)

if (packet->ip4_header.protocol != 6)
{
LOGD("TcpManipulator: ipv4 packet is not TCP");
// LOGD("TcpManipulator: ipv4 packet is not TCP");
self->up->upStream(self->up, c);
return;
}
Expand All @@ -85,7 +96,7 @@ static void upStream(tunnel_t *self, context_t *c)

if (packet->ip6_header.nexthdr != 6)
{
LOGD("TcpManipulator: ipv6 packet is not TCP");
// LOGD("TcpManipulator: ipv6 packet is not TCP");
self->up->upStream(self->up, c);
return;
}
Expand All @@ -96,9 +107,10 @@ static void upStream(tunnel_t *self, context_t *c)
exit(1);
}

struct tcpheader *tcp_header = (struct tcpheader *) (rawBufMut(c->payload) + ip_header_len);
struct tcpheader *tcp_header = (struct tcpheader *) (rawBufMut(c->payload) + ip_header_len);
const int transport_palyoad_len = (int) (bufLen(c->payload) - ip_header_len);

handleResetBitAction(tcp_header, &(state->reset_bit_action));
handleResetBitAction(tcp_header, &(state->reset_bit_action), transport_palyoad_len);

self->up->upStream(self->up, c);
}
Expand Down
19 changes: 19 additions & 0 deletions tunnels/shared/layer3/packet_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,22 @@ static uint16_t lwipStandardCheckSum(const void *dataptr, int len)
return (uint16_t)sum;
}

static unsigned short tcpCheckSum(void *b, int len) {
unsigned short *buf = b;
unsigned int sum = 0;
unsigned short result;

for (sum = 0; len > 1; len -= 2) {
sum += *buf++;
}

if (len == 1) {
sum += *(unsigned char *)buf;
}

sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;

return result;
}

0 comments on commit c31cdb7

Please sign in to comment.