From db1f6609d13a888dd7212c9ed2840846e70a284e Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Wed, 2 Aug 2017 18:26:23 +0200 Subject: added an IPv4 module, currently containg an address to string function started to add ARP packet definition, reading and printing ARP request now some renames in networking layer --- src/net/arp.c | 1 + src/net/arp.h | 36 ++++++++++++++++++++++++++++++++++++ src/net/ethernet.c | 2 +- src/net/ethernet.h | 14 +++++++++----- src/net/ipv4.c | 13 +++++++++++++ src/net/ipv4.h | 18 ++++++++++++++++++ 6 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 src/net/arp.c create mode 100644 src/net/arp.h create mode 100644 src/net/ipv4.c create mode 100644 src/net/ipv4.h (limited to 'src/net') diff --git a/src/net/arp.c b/src/net/arp.c new file mode 100644 index 0000000..1f30b09 --- /dev/null +++ b/src/net/arp.c @@ -0,0 +1 @@ +#include "arp.h" diff --git a/src/net/arp.h b/src/net/arp.h new file mode 100644 index 0000000..ba8de42 --- /dev/null +++ b/src/net/arp.h @@ -0,0 +1,36 @@ +#ifndef ARP_H +#define ARP_H + +#include "ethernet.h" +#include "ipv4.h" + +#if defined( __TINYC__ ) +#pragma pack(1) +#endif + +typedef uint16_t network_hardware_type_t; + +// TODO: network order not host oder, so for now swap it +#define ARP_HARDWARE_TYPE_ETHERNET (network_hardware_type_t)0x0100 + +#define ARP_OPERATION_REQUEST 0x0100 +#define ARP_OPERATION_ANSWER 0x0200 + +// TODO: fixed size structure, works for Ethernet MACs and IPv4 addresses +typedef struct { + network_hardware_type_t hardware_type; + network_ether_type_t protocol_type; + uint8_t hardware_address_len; + uint8_t protocol_address_len; + uint16_t operation; + network_mac_address_t source_hardware_address; + network_ipv4_address_t source_protocol_address; + network_mac_address_t destination_hardware_address; + network_ipv4_address_t destination_protocol_address; +} __attribute__( ( packed ) ) network_arp_packet_t; + +#if defined( __TINYC__ ) +#pragma pack() +#endif + +#endif // ARP_H diff --git a/src/net/ethernet.c b/src/net/ethernet.c index 48db20c..bf8bb93 100644 --- a/src/net/ethernet.c +++ b/src/net/ethernet.c @@ -2,7 +2,7 @@ #include "stdio.h" -char *network_mac_to_string( network_mac_address_t mac, char *buf, size_t buflen ) +char *network_mac_address_to_string( network_mac_address_t mac, char *buf, size_t buflen ) { snprintf( buf, buflen, "%X:%X:%X:%X:%X:%X", mac.byte[0], mac.byte[1], mac.byte[2], diff --git a/src/net/ethernet.h b/src/net/ethernet.h index a2726e7..faeddf5 100644 --- a/src/net/ethernet.h +++ b/src/net/ethernet.h @@ -5,14 +5,18 @@ #define NETWORK_ETHERNET_MAC_ADDR_STRING 6 * 3 + 1 +// hardware_address_len +#define NETWORK_HARDWARE_ETHERNET_ADDRESS_LENGTH 6 + typedef struct { - uint8_t byte[6]; + uint8_t byte[NETWORK_HARDWARE_ETHERNET_ADDRESS_LENGTH]; } network_mac_address_t; -typedef uint16_t ether_type_t; +typedef uint16_t network_ether_type_t; // TODO: network order not host oder, so for now swap it -#define NETWORK_ETHER_TYPE_ARP 0x0608 +#define NETWORK_ETHER_PROTOCOL_TYPE_ARP (network_ether_type_t)0x0608 +#define NETWORK_ETHER_PROTOCOL_TYPE_IPV4 (network_ether_type_t)0x0008 #if defined( __TINYC__ ) #pragma pack(1) @@ -21,7 +25,7 @@ typedef uint16_t ether_type_t; typedef struct { network_mac_address_t dst_addr; network_mac_address_t src_addr; - ether_type_t type; + network_ether_type_t type; } __attribute__( ( packed ) ) network_ethernet_packet_header_t; typedef struct { @@ -33,6 +37,6 @@ typedef struct { #pragma pack() #endif -char *network_mac_to_string( network_mac_address_t mac, char *buf, size_t buflen ); +char *network_mac_address_to_string( network_mac_address_t mac, char *buf, size_t buflen ); #endif // ETHERNET_H diff --git a/src/net/ipv4.c b/src/net/ipv4.c new file mode 100644 index 0000000..38ae7da --- /dev/null +++ b/src/net/ipv4.c @@ -0,0 +1,13 @@ +#include "ipv4.h" + +#include "stdio.h" + +char *network_ipv4_address_to_string( network_ipv4_address_t addr, char *buf, size_t buflen ) +{ + snprintf( buf, buflen, "%d.%d.%d.%d", + addr.byte[0], addr.byte[1], + addr.byte[2], addr.byte[3] ); + + return buf; +} + diff --git a/src/net/ipv4.h b/src/net/ipv4.h new file mode 100644 index 0000000..acf7f7f --- /dev/null +++ b/src/net/ipv4.h @@ -0,0 +1,18 @@ +#ifndef IPV4_H +#define IPV4_H + +#include "stdint.h" +#include "stddef.h" + +#define NETWORK_PROTOCOL_IPV4_ADDR_STRING 4 * 4 + 1 + +// protocol_address_len +#define NETWORK_PROTOCOL_IPV4_ADDRESS_LENGTH 4 + +typedef struct { + uint8_t byte[NETWORK_PROTOCOL_IPV4_ADDRESS_LENGTH]; +} network_ipv4_address_t; + +char *network_ipv4_address_to_string( network_ipv4_address_t addr, char *buf, size_t buflen ); + +#endif // IPV4_H -- cgit v1.2.3-54-g00ecf