Smart Home firewall
Profile-based Smart Home firewall
Classes | Macros | Functions
packet_utils.h File Reference

Utilitaries for payload manipulation and display. More...

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <arpa/inet.h>
#include "sha256.h"
Include dependency graph for packet_utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

union  ip_val_t
 IP (v4 or v6) address value. More...
 
struct  ip_addr_t
 IP (v4 or v6) address. More...
 

Macros

#define MAC_ADDR_LENGTH   6
 
#define MAC_ADDR_STRLEN   18
 
#define IPV4_ADDR_LENGTH   4
 
#define IPV6_ADDR_LENGTH   16
 

Functions

void print_payload (int length, uint8_t *data)
 
size_t hexstr_to_payload (char *hexstring, uint8_t **payload)
 
char * mac_hex_to_str (uint8_t mac_hex[])
 
uint8_t * mac_str_to_hex (char *mac_str)
 
char * ipv4_net_to_str (uint32_t ipv4_net)
 
uint32_t ipv4_str_to_net (char *ipv4_str)
 
char * ipv4_hex_to_str (char *ipv4_hex)
 
char * ipv4_str_to_hex (char *ipv4_str)
 
char * ipv6_net_to_str (uint8_t ipv6[])
 Converts an IPv6 address to its string representation. More...
 
uint8_t * ipv6_str_to_net (char *ipv6_str)
 
char * ip_net_to_str (ip_addr_t ip_addr)
 Converts an IP (v4 or v6) address to its string representation. More...
 
ip_addr_t ip_str_to_net (char *ip_str, uint8_t version)
 
bool compare_ipv6 (uint8_t *ipv6_1, uint8_t *ipv6_2)
 Compare two IPv6 addresses. More...
 
bool compare_ip (ip_addr_t ip_1, ip_addr_t ip_2)
 Compare two IP (v4 or v6) addresses. More...
 
uint8_t * compute_hash (uint8_t *payload, int payload_len)
 Compute SHA256 hash of a given payload. More...
 
void print_hash (uint8_t *hash)
 Print a SHA256 hash. More...
 

Detailed Description

Utilitaries for payload manipulation and display.

Date
2022-09-09

Function Documentation

◆ compare_ip()

bool compare_ip ( ip_addr_t  ip_1,
ip_addr_t  ip_2 
)

Compare two IP (v4 or v6) addresses.

Parameters
ip_1first IP address
ip_2second IP address
Returns
true if the two addresses are equal, false otherwise

◆ compare_ipv6()

bool compare_ipv6 ( uint8_t *  ipv6_1,
uint8_t *  ipv6_2 
)

Compare two IPv6 addresses.

Parameters
ipv6_1first IPv6 address
ipv6_2second IPv6 address
Returns
true if the two addresses are equal, false otherwise

◆ compute_hash()

uint8_t* compute_hash ( uint8_t *  payload,
int  payload_len 
)

Compute SHA256 hash of a given payload.

Parameters
payloadPayload to hash
payload_lenPayload length, including padding (in bytes)
Returns
uint8_t* SHA256 hash of the payload

◆ hexstr_to_payload()

size_t hexstr_to_payload ( char *  hexstring,
uint8_t **  payload 
)

Converts a hexstring payload to a data buffer.

Parameters
hexstringthe hexstring to convert
payloada double pointer to the payload, which will be set to the start of the payload
Returns
the length of the payload in bytes

◆ ip_net_to_str()

char* ip_net_to_str ( ip_addr_t  ip_addr)

Converts an IP (v4 or v6) address to its string representation.

Parameters
ip_addrthe IP address, as an ip_addr_t struct
Returns
the same IP address in string representation

Converts an IP (v4 or v6) address to its string representation. If it is an IPv6 address, it must be freed after use.

Parameters
ip_addrthe IP address, as an ip_addr_t struct
Returns
the same IP address in string representation

◆ ip_str_to_net()

ip_addr_t ip_str_to_net ( char *  ip_str,
uint8_t  version 
)

Converts an IP (v4 or v6) address from its string representation to an ip_addr_t struct.

Parameters
ip_strIP (v4 or v6) address in string representation
Returns
the same IP address as a ip_addr_t struct

◆ ipv4_hex_to_str()

char* ipv4_hex_to_str ( char *  ipv4_hex)

Converts an IPv4 addres from its hexadecimal representation to its string representation.

Parameters
ipv4_hexIPv4 address in hexadecimal representation
Returns
the same IPv4 address in string representation

◆ ipv4_net_to_str()

char* ipv4_net_to_str ( uint32_t  ipv4_net)

Converts an IPv4 address from its network order numerical representation to its string representation. (Wrapper arount inet_ntoa)

Parameters
ipv4_netIPv4 address in hexadecimal representation
Returns
the same IPv4 address in string representation

◆ ipv4_str_to_hex()

char* ipv4_str_to_hex ( char *  ipv4_str)

Converts an IPv4 address from its string representation to its hexadecimal representation.

Parameters
ipv4_strIPv4 address in string representation
Returns
the same IPv4 address in hexadecimal representation

◆ ipv4_str_to_net()

uint32_t ipv4_str_to_net ( char *  ipv4_str)

Converts an IPv4 address from its string representation to its network order numerical representation. (Wrapper arount inet_aton)

Parameters
ipv4_strIPv4 address in string representation
Returns
the same IPv4 address in network order numerical representation

◆ ipv6_net_to_str()

char* ipv6_net_to_str ( uint8_t  ipv6[])

Converts an IPv6 address to its string representation.

Parameters
ipv6the IPv6 address
Returns
the same IPv6 address in string representation

Converts an IPv6 address to its string representation.

Parameters
ipv6the IPv6 address
Returns
the same IPv6 address in string representation

◆ ipv6_str_to_net()

uint8_t* ipv6_str_to_net ( char *  ipv6_str)

Converts an IPv6 address from its string representation to its network representation (a 16-byte array).

Parameters
ipv6_strIPv6 address in string representation
Returns
the same IPv6 address as a 16-byte array

◆ mac_hex_to_str()

char* mac_hex_to_str ( uint8_t  mac_hex[])

Converts a MAC address from its hexadecimal representation to its string representation.

Parameters
mac_hexMAC address in hexadecimal representation
Returns
the same MAC address in string representation

◆ mac_str_to_hex()

uint8_t* mac_str_to_hex ( char *  mac_str)

Converts a MAC address from its string representation to its hexadecimal representation.

Parameters
mac_strMAC address in string representation
Returns
the same MAC address in hexadecimal representation

◆ print_hash()

void print_hash ( uint8_t *  hash)

Print a SHA256 hash.

Parameters
hashSHA256 hash to print

◆ print_payload()

void print_payload ( int  length,
uint8_t *  data 
)

Print a packet payload.

Parameters
lengthlength of the payload in bytes
datapointer to the start of the payload