Skip to content

Commit

Permalink
formatting & readmes
Browse files Browse the repository at this point in the history
  • Loading branch information
jimfangx committed Jun 1, 2024
1 parent 487112a commit 9e54989
Show file tree
Hide file tree
Showing 27 changed files with 580 additions and 464 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# This was my first time writing a makefile so this is likely badly written.
# Guide: https://www.cs.colostate.edu/~cs157/LectureMakefile.pdf

# Compiler
CC := gcc

Expand Down
10 changes: 5 additions & 5 deletions include/arp.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef ARP_H_
#define ARP_H_

#include "syshead.h"
#include "skbuff.h"
#include "eth.h"
#include "dl_list.h"
#include "eth.h"
#include "net_dir.h"
#include "skbuff.h"
#include "syshead.h"

// https://datatracker.ietf.org/doc/html/rfc826#autoid-1
// https://en.wikipedia.org/wiki/Address_Resolution_Protocol#Packet_structure
Expand All @@ -19,7 +19,7 @@ struct ipv4_over_eth_arp_pkt {
uint32_t SPA;
uint8_t THA[6];
uint32_t TPA;
} __attribute__ ((packed));
} __attribute__((packed));

// https://www.auvik.com/franklyit/blog/what-is-an-arp-table/
// only implementing the stuff under ARP entry
Expand All @@ -36,6 +36,6 @@ struct arp_table_entry {
void arp_receive(struct sk_buff *skb, struct eth_self_properties *dev);
void arp_reply(struct sk_buff *skb, struct eth_self_properties *dev, struct ipv4_over_eth_arp_pkt *arp_pkt);
void arp_request(uint32_t TPA, struct eth_self_properties *dev);
uint8_t* arp_table_lookup(uint32_t proto_addr);
uint8_t *arp_table_lookup(uint32_t proto_addr);

#endif
47 changes: 26 additions & 21 deletions include/dl_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,46 @@ struct list_head {
// define all static inline functions in the header file:
// https://stackoverflow.com/questions/5526461/gcc-warning-function-used-but-not-defined

static inline void list_init(struct list_head *head) {
head->next = head;
head->prev = head;
static inline void list_init(struct list_head *head)
{
head->next = head;
head->prev = head;
}

static inline void add_first(struct list_head *new_element, struct list_head *head) {
new_element->next = head->next;
new_element->prev = head;
head->next->prev = new_element;
head->next = new_element;
static inline void add_first(struct list_head *new_element, struct list_head *head)
{
new_element->next = head->next;
new_element->prev = head;
head->next->prev = new_element;
head->next = new_element;
}

static inline void add_last(struct list_head *new_element, struct list_head *head) {
new_element->next = head;
new_element->prev = head->prev;
head->prev->next = new_element;
head->prev = new_element;
static inline void add_last(struct list_head *new_element, struct list_head *head)
{
new_element->next = head;
new_element->prev = head->prev;
head->prev->next = new_element;
head->prev = new_element;
}

static inline void remove_elem(struct list_head *elem_to_remove) {
elem_to_remove->prev->next = elem_to_remove->next;
elem_to_remove->next->prev = elem_to_remove->prev;
static inline void remove_elem(struct list_head *elem_to_remove)
{
elem_to_remove->prev->next = elem_to_remove->next;
elem_to_remove->next->prev = elem_to_remove->prev;
}

static inline int is_list_empty(struct list_head *head) {
return head->next == head;
static inline int is_list_empty(struct list_head *head)
{
return head->next == head;
}

#define get_list_item(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
((type *)((char *)(ptr) - offsetof(type, member)))

#define get_list_first_entry(ptr, type, member) \
get_list_item((ptr)->next, type, member)
get_list_item((ptr)->next, type, member)

#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
for (pos = (head)->next; pos != (head); pos = pos->next)

#endif
9 changes: 4 additions & 5 deletions include/eth.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Linux provides a struct for the ethernet frame header as a guide also:
#ifndef ETH_H_
#define ETH_H_

#include "syshead.h"
#include "skbuff.h"
#include "syshead.h"

#define ETH_ALEN 6

Expand All @@ -33,15 +33,14 @@ struct eth_hdr {
uint8_t ether_shost[ETH_ALEN]; // source mac
uint16_t ether_type;
// uint8_t payload[]; // not in official implementation
} __attribute__ ((packed));
} __attribute__((packed));


static inline struct eth_hdr *unpack_eth_hdr(struct sk_buff *skb) {
static inline struct eth_hdr *unpack_eth_hdr(struct sk_buff *skb)
{
struct eth_hdr *eth_hdr = (struct eth_hdr *)(skb->head);
// convert ether_type from big endian -> small endian
eth_hdr->ether_type = ntohs(eth_hdr->ether_type);
return eth_hdr;
};


#endif
6 changes: 3 additions & 3 deletions include/icmp.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef ICMP_H_
#define ICMP_H_

#include "syshead.h"
#include "skbuff.h"
#include "syshead.h"

#define ICMP_TYPE_DEST_UNREACHABLE 0x03
#define ICMP_TYPE_ECHO 0x08
Expand All @@ -14,14 +14,14 @@ struct icmp_base_hdr {
uint8_t code;
uint16_t checksum;
uint8_t data[];
} __attribute__ ((packed));
} __attribute__((packed));

// additional data for echos
struct icmp_echo_additions {
uint16_t id;
uint16_t seq_num;
uint8_t data[];
} __attribute__ ((packed));
} __attribute__((packed));

void icmp_receive(struct sk_buff *skb);
void icmp_reply(struct sk_buff *skb);
Expand Down
6 changes: 3 additions & 3 deletions include/ip.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef IP_H_
#define IP_H_

#include "syshead.h"
#include "skbuff.h"
#include "syshead.h"
#include "tcp_sockets.h"

#define IPV4 0x04 // for ip version in ip_hdr
Expand All @@ -13,7 +13,7 @@

struct ip_hdr {
// https://stackoverflow.com/questions/11197931/what-is-meaning-of-in-struct-c
uint8_t IHL : 4; // internet header length
uint8_t IHL : 4; // internet header length
uint8_t version : 4; // "but how can i get a 4-bit variable?" -- "Build your own microprocessor." - but u can pack bits (see above), though upper bit comes first so IHL & version is reversed
uint8_t tos;
uint16_t len;
Expand All @@ -27,7 +27,7 @@ struct ip_hdr {
uint32_t src_addr;
uint32_t dest_addr;
uint8_t data[];
} __attribute__ ((packed));
} __attribute__((packed));

void ip_rcv(struct sk_buff *skb);
int ip_transmit(struct tcp_sockets *sock, struct sk_buff *skb);
Expand Down
30 changes: 15 additions & 15 deletions include/net_dir.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#ifndef NET_DIR_H_
#define NET_DIR_H_

#include "syshead.h"
#include "skbuff.h"
#include "syshead.h"

// our device's properties
struct eth_self_properties {
// fields here are selected from RFC 826's packet reception algo
// https://datatracker.ietf.org/doc/html/rfc826#autoid-1
// Naming convention from:
// https://en.wikipedia.org/wiki/Address_Resolution_Protocol#Operating_scope
// stored in little endian
// sidenote: avoid u_int16_t to prevent suicide by coworker
// https://stackoverflow.com/a/23291438/9108905
uint16_t PTYPE; // so far we support ipv4 only
uint32_t PROTOCOL_ADDR; // we've put 2 x 2 bytes together
uint8_t HARDWARE_ADDR[6]; // xx:xx:xx:xx:xx:xx
uint32_t mtu;
// fields here are selected from RFC 826's packet reception algo
// https://datatracker.ietf.org/doc/html/rfc826#autoid-1
// Naming convention from:
// https://en.wikipedia.org/wiki/Address_Resolution_Protocol#Operating_scope
// stored in little endian
// sidenote: avoid u_int16_t to prevent suicide by coworker
// https://stackoverflow.com/a/23291438/9108905
uint16_t PTYPE; // so far we support ipv4 only
uint32_t PROTOCOL_ADDR; // we've put 2 x 2 bytes together
uint8_t HARDWARE_ADDR[6]; // xx:xx:xx:xx:xx:xx
uint32_t mtu;
};

struct eth_self_properties* init_eth_self(char* self_hardware_mac, char* self_protocol_ip,
int self_mtu);
// TODO: FIX:
struct eth_self_properties *init_eth_self(char *self_hardware_mac, char *self_protocol_ip,
int self_mtu);
// TODO: FIX:
// for some stupid reason uncommenting this leads to a conflicting types err, however not including it in header works
int net_dir_transmit(struct sk_buff *skb, uint8_t *ether_dhost, uint16_t ether_type);
void net_dir_receive(struct eth_self_properties *dev);
Expand Down
7 changes: 3 additions & 4 deletions include/route.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef ROUTE_H_
#define ROUTE_H_

#include "syshead.h"
#include "net_dir.h"
#include "dl_list.h"
#include "net_dir.h"
#include "syshead.h"

#define RT_GATEWAY 0x02
#define RT_HOST 0x04
Expand All @@ -19,8 +19,7 @@ struct routing_table_entry {
struct eth_self_properties *dev; // to store information for lower level network stack - upon correct routing, use info held here for sending
};

void rt_init(char* gateway_addr, struct eth_self_properties *dev);
void rt_init(char *gateway_addr, struct eth_self_properties *dev);
struct routing_table_entry *rt_lookup(uint32_t dest_addr);


#endif
4 changes: 2 additions & 2 deletions include/skbuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ what we need in our skbuff is documented here:
#define SK_BUFF_H_

#include "dl_list.h"
#include "route.h"
#include "net_dir.h"
#include "route.h"
#include <pthread.h>
#include <stdint.h>

Expand All @@ -19,7 +19,7 @@ struct sk_buff {
struct eth_self_properties *dev;
uint32_t interface;
uint16_t protocol; // 2 byte identifier
uint32_t len; // length of packet
uint32_t len; // length of packet
uint8_t *head;
uint8_t *data;
uint8_t *tail;
Expand Down
6 changes: 1 addition & 5 deletions include/syshead.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <errno.h>
#include <fcntl.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <linux/if_tun.h>
#include <linux/types.h>
#include <netdb.h>
Expand All @@ -29,10 +30,5 @@
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#include <linux/if_ether.h>

#endif




27 changes: 13 additions & 14 deletions include/tcp.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#include "syshead.h"
#include "skbuff.h"
#include "syshead.h"

// see rfc 793
struct tcp_hdr {
uint16_t src_port;
uint16_t dest_port;
uint32_t seq_num;
uint32_t ack_num;
uint8_t reserved: 4;
uint8_t data_offset: 4;
uint8_t fin: 1,
syn: 1,
rst: 1,
psh: 1,
ack: 1,
urg: 1,
rsv1: 1, // no congestion implementation - https://www.johnpfernandes.com/2018/12/17/tcp-flags-what-they-mean-and-how-they-help/
rsv0: 1;
uint8_t reserved : 4;
uint8_t data_offset : 4;
uint8_t fin : 1,
syn : 1,
rst : 1,
psh : 1,
ack : 1,
urg : 1,
rsv1 : 1, // no congestion implementation - https://www.johnpfernandes.com/2018/12/17/tcp-flags-what-they-mean-and-how-they-help/
rsv0 : 1;
uint16_t window;
uint16_t checksum;
uint16_t urgent_ptr;
uint8_t data[];
} __attribute__ ((packed));
} __attribute__((packed));

// for tcp checksum
struct tcp_pseudo_header {
Expand All @@ -30,7 +30,6 @@ struct tcp_pseudo_header {
uint8_t zero;
uint8_t protocol;
uint16_t tcp_len;
} __attribute__ ((packed));

} __attribute__((packed));

void tcp_incoming(struct sk_buff *skb);
Loading

0 comments on commit 9e54989

Please sign in to comment.