This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kernel: Beginnings of networking stuff!
- Loading branch information
Showing
16 changed files
with
1,014 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* SPDX-License-Identifier: GPL-3.0-or-later */ | ||
/* Copyright © 2016-2024 Byteduck */ | ||
|
||
#pragma once | ||
|
||
#include "stdint.h" | ||
|
||
#ifdef __cplusplus | ||
|
||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ | ||
constexpr static bool __little_endian = true; | ||
#else | ||
constexpr static bool __little_endian = false; | ||
#endif | ||
|
||
template<typename T> | ||
inline constexpr T swap_endianness(T val) { | ||
if constexpr(sizeof(T) == 1) | ||
return val; | ||
else if constexpr (sizeof(T) == 2) | ||
return static_cast<T>(__builtin_bswap16(static_cast<uint16_t>(val))); | ||
else if constexpr (sizeof(T) == 4) | ||
return static_cast<T>(__builtin_bswap32(static_cast<uint32_t>(val))); | ||
else if constexpr (sizeof(T) == 8) | ||
return static_cast<T>(__builtin_bswap64(static_cast<uint64_t>(val))); | ||
else | ||
static_assert("Cannot swap endianness of anything larger than 8 bytes"); | ||
} | ||
|
||
template<typename T> | ||
inline constexpr T as_little_endian(T val) { | ||
if constexpr(__little_endian) | ||
return val; | ||
else | ||
return swap_endianness(val); | ||
} | ||
|
||
template<typename T> | ||
inline constexpr T as_big_endian(T val) { | ||
if constexpr(!__little_endian) | ||
return val; | ||
else | ||
return swap_endianness(val); | ||
} | ||
|
||
template<typename T> | ||
inline constexpr T from_little_endian(T val) { | ||
if constexpr(__little_endian) | ||
return val; | ||
else | ||
return swap_endianness(val); | ||
} | ||
|
||
template<typename T> | ||
inline constexpr T from_big_endian(T val) { | ||
if constexpr(!__little_endian) | ||
return val; | ||
else | ||
return swap_endianness(val); | ||
} | ||
|
||
template<typename T> | ||
class LittleEndian { | ||
public: | ||
constexpr LittleEndian() = default; | ||
constexpr LittleEndian(T val): m_val(as_little_endian(val)) {} | ||
constexpr operator T() const { return from_little_endian(m_val); } | ||
constexpr T val() const { return operator T(); } | ||
private: | ||
T m_val; | ||
} __attribute__((packed)); | ||
|
||
template<typename T> | ||
class BigEndian { | ||
public: | ||
constexpr BigEndian() = default; | ||
constexpr BigEndian(T val): m_val(as_big_endian(val)) {} | ||
constexpr operator T() const { return from_big_endian(m_val); } | ||
constexpr T val() const { return operator T(); } | ||
private: | ||
T m_val; | ||
} __attribute__((packed)); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* SPDX-License-Identifier: GPL-3.0-or-later */ | ||
/* Copyright © 2016-2024 Byteduck */ | ||
|
||
#pragma once | ||
|
||
#include "types.h" | ||
#include "endian.h" | ||
|
||
#ifdef __cplusplus | ||
class __attribute__((packed)) IPv4Address { | ||
public: | ||
constexpr IPv4Address() = default; | ||
|
||
constexpr IPv4Address(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { | ||
m_data[0] = a; | ||
m_data[1] = b; | ||
m_data[2] = c; | ||
m_data[3] = d; | ||
} | ||
|
||
inline constexpr uint8_t operator[](int idx) const { | ||
return m_data[idx]; | ||
} | ||
|
||
private: | ||
uint8_t m_data[4]; | ||
}; | ||
|
||
#define IPV4_ARGS(addr) (addr)[0], (addr)[1], (addr)[2], (addr)[3] | ||
|
||
struct __attribute__((packed)) IPv4Packet { | ||
uint8_t version_ihl = 0; | ||
uint8_t dscp_ecn = 0; | ||
BigEndian<uint16_t> length; | ||
BigEndian<uint16_t> identification; | ||
BigEndian<uint16_t> flags_fragment_offset; | ||
uint8_t ttl = 0; | ||
uint8_t proto; | ||
BigEndian<uint16_t> checksum; | ||
IPv4Address source_addr; | ||
IPv4Address dest_addr; | ||
uint8_t payload[]; | ||
}; | ||
|
||
static_assert(sizeof(IPv4Packet) == 20); | ||
|
||
enum IPv4Proto { | ||
ICMP = 0x1, | ||
TCP = 0x06, | ||
UDP = 0x11 | ||
}; | ||
|
||
#endif | ||
|
||
__DECL_BEGIN | ||
__DECL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* SPDX-License-Identifier: GPL-3.0-or-later */ | ||
/* Copyright © 2016-2024 Byteduck */ | ||
|
||
#pragma once | ||
|
||
#include "ipv4.h" | ||
|
||
#define IFNAMESIZ 16 | ||
|
||
#ifdef __cplusplus | ||
|
||
class __attribute__((packed)) MACAddress { | ||
public: | ||
MACAddress() = default; | ||
MACAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f) { | ||
m_data[0] = a; | ||
m_data[1] = b; | ||
m_data[2] = c; | ||
m_data[3] = d; | ||
m_data[4] = e; | ||
m_data[5] = f; | ||
} | ||
|
||
uint8_t operator[](size_t index) { | ||
return m_data[index]; | ||
} | ||
private: | ||
uint8_t m_data[6] = {0}; | ||
}; | ||
|
||
#define MAC_ARGS(addr) (addr)[0], (addr)[1], (addr)[2], (addr)[3], (addr)[4], (addr)[5] | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* SPDX-License-Identifier: GPL-3.0-or-later */ | ||
/* Copyright © 2016-2024 Byteduck */ | ||
|
||
#pragma once | ||
|
||
#include "../api/endian.h" | ||
#include "../api/net.h" | ||
|
||
enum ARPHWType { | ||
Ethernet = 1 | ||
}; | ||
|
||
enum ARPOp { | ||
Req = 1, | ||
Resp = 2 | ||
}; | ||
|
||
enum EtherProto { | ||
IPv4 = 0x0800, | ||
ARP = 0x0806, | ||
IPv6 = 0x86DD | ||
}; | ||
|
||
struct ARPPacket { | ||
BigEndian<uint16_t> hardware_type {ARPHWType::Ethernet}; | ||
BigEndian<uint16_t> protocol_type { EtherProto::IPv4 }; | ||
BigEndian<uint8_t> hwaddr_len { sizeof(MACAddress) }; | ||
BigEndian<uint8_t> protoaddr_len { sizeof(IPv4Address) }; | ||
BigEndian<uint16_t> operation; | ||
MACAddress sender_hwaddr; | ||
IPv4Address sender_protoaddr; | ||
MACAddress target_hwaddr; | ||
IPv4Address target_protoaddr; | ||
} __attribute__((packed)); | ||
|
||
static_assert(sizeof(ARPPacket) == 28); |
Oops, something went wrong.