Skip to content

Commit

Permalink
More minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Mar 16, 2024
1 parent 33aa88a commit 428f64c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/icmp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define _DEFAULT_SOURCE // htobe16, htobe32
#include <string.h>
#include <assert.h>
#include "os-endian.h"

#include "icmp.h"
Expand All @@ -15,6 +16,9 @@ void icmp_checksum(const struct frame_ip *ipf, struct icmp_header *pkt, uint16_t
};
uint32_t csum = CHKSUM_INITIAL;

static_assert(sizeof(ph) == PSEUDO_HEADER_SIZE, "incorrect PSEUDO_HEADER_SIZE");
static_assert(sizeof(*pkt) == ICMP_HEADER_SIZE, "incorrect ICMP_HEADER_SIZE");

chksum(&csum, (uint16_t*) ipf->src, 16); // ph->src
chksum(&csum, (uint16_t*) ipf->dest, 16); // ph->dest
chksum(&csum, (uint16_t*) &ph.len, 8); // rest of ph
Expand Down
5 changes: 5 additions & 0 deletions src/rawsock-frame.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define _DEFAULT_SOURCE // htobe16
#include <string.h>
#include <assert.h>
#include "os-endian.h"

#include "rawsock.h"
Expand All @@ -17,6 +18,8 @@ void rawsock_eth_settings(const uint8_t *src, const uint8_t *dst)

void rawsock_eth_prepare(struct frame_eth *f, int type)
{
static_assert(sizeof(*f) == FRAME_ETH_SIZE, "incorrect FRAME_ETH_SIZE");

memcpy(f->dest, eth_dst, 6);
memcpy(f->src, eth_src, 6);
f->type = htobe16(type & 0xffff);
Expand All @@ -36,6 +39,8 @@ void rawsock_ip_settings(const uint8_t *src, int ttl)

void rawsock_ip_prepare(struct frame_ip *f, int type)
{
static_assert(sizeof(*f) == FRAME_IP_SIZE, "incorrect FRAME_IP_SIZE");

f->ver = 6;
f->traffic1 = 0;
f->traffic2 = 0;
Expand Down
17 changes: 9 additions & 8 deletions src/rawsock.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@
#include <stdint.h>
#include <netinet/in.h>

#define FRAME_ETH_SIZE 14
#define FRAME_IP_SIZE 40

#define ETH_TYPE_IPV6 0x86dd
#define IP_TYPE_TCP 0x06
#define IP_TYPE_UDP 0x11
#define IP_TYPE_ICMPV6 0x3a

#define RAWSOCK_FILTER_IPTYPE (1 << 0)
#define RAWSOCK_FILTER_DSTADDR (1 << 1)
#define RAWSOCK_FILTER_DSTPORT (1 << 2)
enum {
RAWSOCK_FILTER_IPTYPE = (1 << 0),
RAWSOCK_FILTER_DSTADDR = (1 << 1),
RAWSOCK_FILTER_DSTPORT = (1 << 2),
};

#define FRAME_ETH_SIZE 14
struct frame_eth {
uint8_t dest[6]; // Destination address
uint8_t src[6]; // Source address
uint16_t type; // Type of next header (usually IP)
} __attribute__((packed));

#define FRAME_IP_SIZE 40
struct frame_ip {
uint8_t
traffic1:4, // Traffic class
ver:4; // Version (== 0x06)
ver:4; // Version (== 6)
uint8_t
flow1:4, // Flow label
traffic2:4;
Expand All @@ -47,7 +48,7 @@ struct pseudo_header {
uint8_t ipproto;
} __attribute__((packed));

typedef void (*rawsock_callback)(uint64_t,int,const uint8_t*);
typedef void (*rawsock_callback)(uint64_t /* timestamp */, int /* length */, const uint8_t* /* packet */);

int rawsock_open(const char *dev, int buffersize);
int rawsock_has_ethernet_headers(void);
Expand Down
4 changes: 4 additions & 0 deletions src/tcp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define _DEFAULT_SOURCE // htobe16, htobe32
#include <string.h>
#include <assert.h>
#include "os-endian.h"

#include "tcp.h"
Expand Down Expand Up @@ -57,6 +58,9 @@ void tcp_checksum(const struct frame_ip *ipf, struct tcp_header *pkt, uint16_t d
};
uint32_t csum = CHKSUM_INITIAL;

static_assert(sizeof(ph) == PSEUDO_HEADER_SIZE, "incorrect PSEUDO_HEADER_SIZE");
static_assert(sizeof(*pkt) == TCP_HEADER_SIZE, "incorrect TCP_HEADER_SIZE");

chksum(&csum, (uint16_t*) ipf->src, 16); // ph->src
chksum(&csum, (uint16_t*) ipf->dest, 16); // ph->dest
chksum(&csum, (uint16_t*) &ph.len, 8); // rest of ph
Expand Down
4 changes: 4 additions & 0 deletions src/udp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define _DEFAULT_SOURCE // htobe16, htobe32
#include <string.h>
#include <assert.h>
#include "os-endian.h"

#include "udp.h"
Expand All @@ -26,6 +27,9 @@ void udp_checksum(const struct frame_ip *ipf, struct udp_header *pkt, uint16_t d
};
uint32_t csum = CHKSUM_INITIAL;

static_assert(sizeof(ph) == PSEUDO_HEADER_SIZE, "incorrect PSEUDO_HEADER_SIZE");
static_assert(sizeof(*pkt) == UDP_HEADER_SIZE, "incorrect UDP_HEADER_SIZE");

chksum(&csum, (uint16_t*) ipf->src, 16); // ph->src
chksum(&csum, (uint16_t*) ipf->dest, 16); // ph->dest
chksum(&csum, (uint16_t*) &ph.len, 8); // rest of ph
Expand Down
4 changes: 3 additions & 1 deletion src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ int realloc_if_needed(void **array, unsigned int elemsize, unsigned int used, un
{
if(used < *total)
return 0;
unsigned int new_total = *total ? *total * 2 : 64;
unsigned int new_total = *total * 3 / 2;
if (new_total < used)
new_total = used;
void *new_array = realloc(*array, new_total * elemsize);
if(new_array == NULL)
return -1;
Expand Down

0 comments on commit 428f64c

Please sign in to comment.