reactor-c
C Runtime for Lingua Franca
Loading...
Searching...
No Matches
net_util.h File Reference
#include <sys/socket.h>
#include <regex.h>
#include <sys/types.h>
#include <stdbool.h>
#include "low_level_platform.h"
#include "tag.h"

Go to the source code of this file.

Macros

#define NUM_SOCKET_RETRIES   10
 
#define DELAY_BETWEEN_SOCKET_RETRIES   MSEC(100)
 
#define HOST_LITTLE_ENDIAN   1
 
#define HOST_BIG_ENDIAN   2
 

Functions

int host_is_big_endian (void)
 
void encode_int64 (int64_t data, unsigned char *buffer)
 
void encode_int32 (int32_t data, unsigned char *buffer)
 
void encode_uint32 (uint32_t data, unsigned char *buffer)
 
void encode_uint16 (uint16_t data, unsigned char *buffer)
 
int32_t swap_bytes_if_big_endian_int32 (int32_t src)
 
int64_t swap_bytes_if_big_endian_int64 (int64_t src)
 
uint16_t swap_bytes_if_big_endian_uint16 (uint16_t src)
 
int32_t extract_int32 (unsigned char *bytes)
 
int64_t extract_int64 (unsigned char *bytes)
 
uint16_t extract_uint16 (unsigned char *bytes)
 

Detailed Description

Author
Edward A. Lee
Soroush Bateni

LICENSE

Copyright (c) 2020, The University of California at Berkeley.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

DESCRIPTION

Header file for network utility functions for Lingua Franca programs. Note that these functions do not acquire any mutexes. To use them, you must ensure either that only one thread ever sends on each socket and one thread receives on each socket (these two can be the same thread) or that the caller handles mutual exclusion to prevent more than one thread from accessing the socket at a time.

Macro Definition Documentation

◆ DELAY_BETWEEN_SOCKET_RETRIES

#define DELAY_BETWEEN_SOCKET_RETRIES   MSEC(100)

◆ HOST_BIG_ENDIAN

#define HOST_BIG_ENDIAN   2

◆ HOST_LITTLE_ENDIAN

#define HOST_LITTLE_ENDIAN   1

◆ NUM_SOCKET_RETRIES

#define NUM_SOCKET_RETRIES   10

Function Documentation

◆ encode_int32()

void encode_int32 ( int32_t data,
unsigned char * buffer )

Write the specified data as a sequence of bytes starting at the specified address. This encodes the data in little-endian order (lowest order byte first). This works for int32_t.

Parameters
dataThe data to write.
bufferThe location to start writing.

◆ encode_int64()

void encode_int64 ( int64_t data,
unsigned char * buffer )

Write the specified data as a sequence of bytes starting at the specified address. This encodes the data in little-endian order (lowest order byte first).

Parameters
dataThe data to write.
bufferThe location to start writing.

◆ encode_uint16()

void encode_uint16 ( uint16_t data,
unsigned char * buffer )

Write the specified data as a sequence of bytes starting at the specified address. This encodes the data in little-endian order (lowest order byte first).

Parameters
dataThe data to write.
bufferThe location to start writing.

◆ encode_uint32()

void encode_uint32 ( uint32_t data,
unsigned char * buffer )

Write the specified data as a sequence of bytes starting at the specified address. This encodes the data in little-endian order (lowest order byte first). This works for uint32_t.

Parameters
dataThe data to write.
bufferThe location to start writing.

◆ extract_int32()

int32_t extract_int32 ( unsigned char * bytes)

This will swap the order of the bytes if this machine is big endian.

Parameters
bytesThe address of the start of the sequence of bytes.

◆ extract_int64()

int64_t extract_int64 ( unsigned char * bytes)

This will swap the order of the bytes if this machine is big endian.

Parameters
bytesThe address of the start of the sequence of bytes.

◆ extract_uint16()

uint16_t extract_uint16 ( unsigned char * bytes)

Extract an uint16_t from the specified byte sequence. This will swap the order of the bytes if this machine is big endian.

Parameters
bytesThe address of the start of the sequence of bytes.

◆ host_is_big_endian()

int host_is_big_endian ( void )

Return true (1) if the host is big endian. Otherwise, return false.

◆ swap_bytes_if_big_endian_int32()

int32_t swap_bytes_if_big_endian_int32 ( int32_t src)

If this host is little endian, then reverse the order of the bytes of the argument. Otherwise, return the argument unchanged. This can be used to convert the argument to network order (big endian) and then back again. Network transmissions, by convention, are big endian, meaning that the high-order byte is sent first. But many platforms, including my Mac, are little endian, meaning that the low-order byte is first in memory.

Parameters
srcThe argument to convert.

◆ swap_bytes_if_big_endian_int64()

int64_t swap_bytes_if_big_endian_int64 ( int64_t src)

If this host is little endian, then reverse the order of the bytes of the argument. Otherwise, return the argument unchanged. This can be used to convert the argument to network order (big endian) and then back again. Network transmissions, by convention, are big endian, meaning that the high-order byte is sent first. But many platforms, including my Mac, are little endian, meaning that the low-order byte is first in memory.

Parameters
srcThe argument to convert.

◆ swap_bytes_if_big_endian_uint16()

uint16_t swap_bytes_if_big_endian_uint16 ( uint16_t src)

If this host is little endian, then reverse the order of the bytes of the argument. Otherwise, return the argument unchanged. This can be used to convert the argument to network order (big endian) and then back again. Network transmissions, by convention, are big endian, meaning that the high-order byte is sent first. But many platforms, including my Mac, are little endian, meaning that the low-order byte is first in memory.

Parameters
srcThe argument to convert.