Smart Home firewall
Profile-based Smart Home firewall
packet_utils.h
Go to the documentation of this file.
1 
10 #ifndef _PROTOCOL_PARSERS_PACKET_UTILS_
11 #define _PROTOCOL_PARSERS_PACKET_UTILS_
12 
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include <string.h>
18 #include <arpa/inet.h>
19 #include "sha256.h"
20 
21 #define MAC_ADDR_LENGTH 6
22 #define MAC_ADDR_STRLEN 18
23 #define IPV4_ADDR_LENGTH 4
24 #define IPV6_ADDR_LENGTH 16
25 
29 typedef union {
30  uint32_t ipv4; // IPv4 address, as a 32-bit unsigned integer in network byte order
31  uint8_t ipv6[IPV6_ADDR_LENGTH]; // IPv6 address, as a 16-byte array
32 } ip_val_t;
33 
37 typedef struct {
38  uint8_t version; // IP version (4 or 6, 0 if not set)
39  ip_val_t value; // IP address value (0 if not set)
40 } ip_addr_t;
41 
48 void print_payload(int length, uint8_t *data);
49 
57 size_t hexstr_to_payload(char *hexstring, uint8_t **payload);
58 
66 char *mac_hex_to_str(uint8_t mac_hex[]);
67 
75 uint8_t *mac_str_to_hex(char *mac_str);
76 
85 char* ipv4_net_to_str(uint32_t ipv4_net);
86 
95 uint32_t ipv4_str_to_net(char *ipv4_str);
96 
104 char* ipv4_hex_to_str(char *ipv4_hex);
105 
113 char* ipv4_str_to_hex(char *ipv4_str);
114 
121 char* ipv6_net_to_str(uint8_t ipv6[]);
122 
130 uint8_t* ipv6_str_to_net(char *ipv6_str);
131 
138 char* ip_net_to_str(ip_addr_t ip_addr);
139 
147 ip_addr_t ip_str_to_net(char *ip_str, uint8_t version);
148 
156 bool compare_ipv6(uint8_t *ipv6_1, uint8_t *ipv6_2);
157 
165 bool compare_ip(ip_addr_t ip_1, ip_addr_t ip_2);
166 
174 uint8_t* compute_hash(uint8_t *payload, int payload_len);
175 
181 void print_hash(uint8_t *hash);
182 
183 
184 #endif /* _PROTOCOL_PARSERS_PACKET_UTILS_ */
char * ip_net_to_str(ip_addr_t ip_addr)
Converts an IP (v4 or v6) address to its string representation.
Definition: packet_utils.c:205
char * ipv6_net_to_str(uint8_t ipv6[])
Converts an IPv6 address to its string representation.
Definition: packet_utils.c:168
char * mac_hex_to_str(uint8_t mac_hex[])
Definition: packet_utils.c:63
void print_payload(int length, uint8_t *data)
Definition: packet_utils.c:19
ip_addr_t ip_str_to_net(char *ip_str, uint8_t version)
Definition: packet_utils.c:226
bool compare_ipv6(uint8_t *ipv6_1, uint8_t *ipv6_2)
Compare two IPv6 addresses.
Definition: packet_utils.c:248
char * ipv4_hex_to_str(char *ipv4_hex)
Definition: packet_utils.c:131
size_t hexstr_to_payload(char *hexstring, uint8_t **payload)
Definition: packet_utils.c:44
bool compare_ip(ip_addr_t ip_1, ip_addr_t ip_2)
Compare two IP (v4 or v6) addresses.
Definition: packet_utils.c:259
char * ipv4_net_to_str(uint32_t ipv4_net)
Definition: packet_utils.c:106
uint8_t * ipv6_str_to_net(char *ipv6_str)
Definition: packet_utils.c:185
uint8_t * compute_hash(uint8_t *payload, int payload_len)
Compute SHA256 hash of a given payload.
Definition: packet_utils.c:276
uint8_t * mac_str_to_hex(char *mac_str)
Definition: packet_utils.c:84
uint32_t ipv4_str_to_net(char *ipv4_str)
Definition: packet_utils.c:118
void print_hash(uint8_t *hash)
Print a SHA256 hash.
Definition: packet_utils.c:290
char * ipv4_str_to_hex(char *ipv4_str)
Definition: packet_utils.c:150
IP (v4 or v6) address.
Definition: packet_utils.h:37
IP (v4 or v6) address value.
Definition: packet_utils.h:29