ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
endian.h File Reference

🔄 Network byte order conversion helpers More...

Go to the source code of this file.

Macros

#define HOST_TO_NET_U32(val)   htonl((val))
 
#define NET_TO_HOST_U32(val)   ntohl((val))
 
#define HOST_TO_NET_U16(val)   htons((val))
 
#define NET_TO_HOST_U16(val)   ntohs((val))
 
#define CONVERT_ARRAY_HOST_TO_NET_U32(arr, count)
 
#define CONVERT_ARRAY_NET_TO_HOST_U32(arr, count)
 

Detailed Description

🔄 Network byte order conversion helpers

Provides convenient macros for converting between host and network byte order. Reduces repetitive htonl/ntohl calls and makes intent clearer.

Common Pattern: Throughout the protocol handlers, we convert numeric values between host and network byte order for transmission. This creates repetitive patterns like:

Bad (repetitive):

uint32_t width_net = htonl(img_width);
uint32_t height_net = htonl(img_height);
uint32_t channels_net = htonl(img_channels);
unsigned int uint32_t
Definition common.h:58

Good (using these helpers):

uint32_t width_net = HOST_TO_NET_U32(img_width);
uint32_t height_net = HOST_TO_NET_U32(img_height);
uint32_t channels_net = HOST_TO_NET_U32(img_channels);
#define HOST_TO_NET_U32(val)
Definition endian.h:71

Usage:

// Sending frame header
uint32_t width = frame->width;
uint32_t height = frame->height;
uint32_t size = frame->data_size;
frame_header_t hdr = {
.width = HOST_TO_NET_U32(width),
.height = HOST_TO_NET_U32(height),
.data_size = HOST_TO_NET_U32(size),
};
// Receiving and unpacking
const frame_header_t *hdr = (const frame_header_t *)packet_data;
uint32_t width = NET_TO_HOST_U32(hdr->width);
uint32_t height = NET_TO_HOST_U32(hdr->height);
uint32_t size = NET_TO_HOST_U32(hdr->data_size);
#define NET_TO_HOST_U32(val)
Definition endian.h:86

Definition in file endian.h.

Macro Definition Documentation

◆ CONVERT_ARRAY_HOST_TO_NET_U32

#define CONVERT_ARRAY_HOST_TO_NET_U32 (   arr,
  count 
)
Value:
do { \
for (size_t i = 0; i < (count); i++) { \
(arr)[i] = htonl((arr)[i]); \
} \
} while (0)

Convert an array of 32-bit values from host to network byte order in-place. Modifies the array directly.

Parameters
arrPointer to array of uint32_t values
countNumber of elements to convert

Usage:

uint32_t data[10] = {...};
// Now all values in 'data' are in network byte order
#define CONVERT_ARRAY_HOST_TO_NET_U32(arr, count)
Definition endian.h:132

Definition at line 132 of file endian.h.

133 { \
134 for (size_t i = 0; i < (count); i++) { \
135 (arr)[i] = htonl((arr)[i]); \
136 } \
137 } while (0)

◆ CONVERT_ARRAY_NET_TO_HOST_U32

#define CONVERT_ARRAY_NET_TO_HOST_U32 (   arr,
  count 
)
Value:
do { \
for (size_t i = 0; i < (count); i++) { \
(arr)[i] = ntohl((arr)[i]); \
} \
} while (0)

Convert an array of 32-bit values from network to host byte order in-place. Modifies the array directly.

Parameters
arrPointer to array of uint32_t values
countNumber of elements to convert

Usage:

uint32_t data[10];
memcpy(data, packet_data, sizeof(data));
// Now all values in 'data' are in host byte order
#define CONVERT_ARRAY_NET_TO_HOST_U32(arr, count)
Definition endian.h:154

Definition at line 154 of file endian.h.

155 { \
156 for (size_t i = 0; i < (count); i++) { \
157 (arr)[i] = ntohl((arr)[i]); \
158 } \
159 } while (0)

◆ HOST_TO_NET_U16

#define HOST_TO_NET_U16 (   val)    htons((val))

Convert a 16-bit value from host byte order to network byte order. Typically used for port numbers and other 16-bit fields.

Parameters
val16-bit unsigned integer in host byte order
Returns
Same value in network byte order (big-endian)

Usage:

uint16_t port = 27224;
addr.sin_port = HOST_TO_NET_U16(port);
#define HOST_TO_NET_U16(val)
Definition endian.h:101
unsigned short uint16_t
Definition common.h:57

Definition at line 101 of file endian.h.

◆ HOST_TO_NET_U32

#define HOST_TO_NET_U32 (   val)    htonl((val))

Convert a 32-bit value from host byte order to network byte order. Typically used when preparing data for transmission.

Parameters
val32-bit unsigned integer in host byte order
Returns
Same value in network byte order (big-endian)

Usage:

uint32_t count = 42;
packet.count = HOST_TO_NET_U32(count);

Definition at line 71 of file endian.h.

◆ NET_TO_HOST_U16

#define NET_TO_HOST_U16 (   val)    ntohs((val))

Convert a 16-bit value from network byte order to host byte order. Typically used for port numbers and other 16-bit fields.

Parameters
val16-bit unsigned integer in network byte order
Returns
Same value in host byte order

Usage:

const addr_t *addr = ...;
uint16_t port = NET_TO_HOST_U16(addr->sin_port);
#define NET_TO_HOST_U16(val)
Definition endian.h:116

Definition at line 116 of file endian.h.

◆ NET_TO_HOST_U32

#define NET_TO_HOST_U32 (   val)    ntohl((val))

Convert a 32-bit value from network byte order to host byte order. Typically used when receiving data from the network.

Parameters
val32-bit unsigned integer in network byte order
Returns
Same value in host byte order

Usage:

const packet_t *pkt = (const packet_t *)data;
uint32_t count = NET_TO_HOST_U32(pkt->count);

Definition at line 86 of file endian.h.