reactor-c
C Runtime for Lingua Franca
Loading...
Searching...
No Matches
net_util.c File Reference
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include "net_util.h"
#include "util.h"

Functions

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)
 
int host_is_big_endian ()
 
int32_t swap_bytes_if_big_endian_int32 (int32_t src)
 
uint32_t swap_bytes_if_big_endian_uint32 (uint32_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)
 
uint32_t extract_uint32 (unsigned char *bytes)
 
int64_t extract_int64 (unsigned char *bytes)
 
uint16_t extract_uint16 (unsigned char *bytes)
 

Detailed Description

Author
Edward A. Lee (eal@b.nosp@m.erke.nosp@m.ley.e.nosp@m.du)
Soroush Bateni (sorou.nosp@m.sh@u.nosp@m.tdall.nosp@m.as.e.nosp@m.du)

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

Utility functions for a federate in a federated execution.

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.

◆ extract_uint32()

uint32_t extract_uint32 ( unsigned char * 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.

◆ swap_bytes_if_big_endian_uint32()

uint32_t swap_bytes_if_big_endian_uint32 ( uint32_t src)