18 #ifdef TGT_OS_TYPE_VXWORKS
30 #elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
31 #include <sys/socket.h>
33 #include <arpa/inet.h>
35 #error OS not supported for IP Socket Communications
72 this->m_recv_port = port;
78 U16 port = this->m_recv_port;
84 struct sockaddr_in address;
88 address.sin_family = AF_INET;
89 address.sin_port = htons(this->m_recv_port);
91 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
92 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
100 if (::
bind(fd,
reinterpret_cast<struct sockaddr*
>(&address),
sizeof(address)) < 0) {
104 socklen_t size =
sizeof(address);
105 if (::getsockname(fd,
reinterpret_cast<struct sockaddr *
>(&address), &size) == -1) {
109 FW_ASSERT(
sizeof(this->m_state->
m_addr_recv) ==
sizeof(address),
sizeof(this->m_state->m_addr_recv),
sizeof(address));
110 memcpy(&this->m_state->
m_addr_recv, &address,
sizeof(this->m_state->m_addr_recv));
118 struct sockaddr_in address;
123 if ((socketFd = ::socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
130 address.sin_family = AF_INET;
131 address.sin_port = htons(this->
m_port);
134 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
135 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
149 FW_ASSERT(
sizeof(this->m_state->
m_addr_send) ==
sizeof(address),
sizeof(this->m_state->m_addr_send),
151 memcpy(&this->m_state->
m_addr_send, &address,
sizeof(this->m_state->m_addr_send));
159 U16 recv_port = this->m_recv_port;
172 socketDescriptor.
fd = socketFd;
179 reinterpret_cast<struct sockaddr *
>(&this->m_state->m_addr_send),
sizeof(this->m_state->m_addr_send)));
184 return static_cast<I32
>(::recvfrom(socketDescriptor.
fd, data, size,
SOCKET_IP_RECV_FLAGS,
nullptr,
nullptr));
PlatformIntType NATIVE_INT_TYPE
uint8_t U8
8-bit unsigned integer
int PlatformIntType
DefaultTypes.hpp provides fallback defaults for the platform types.
PlatformSizeType FwSizeType
C++-compatible configuration header for fprime configuration.
@ SOCKET_MAX_HOSTNAME_SIZE
Helper base-class for setting up Berkeley sockets.
U16 m_port
IP address port used.
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
SocketIpStatus setupTimeouts(PlatformIntType socketFd)
setup the socket timeout properties of the opened outgoing socket
SocketIpStatus configure(const char *hostname, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds)
configure the ip socket with host and transmission timeouts
static SocketIpStatus addressToIp4(const char *address, void *ip4)
converts a given address in dot form x.x.x.x to an ip address. ONLY works for IPv4.
virtual bool isValidPort(U16 port)
Check if the given port is valid for the socket.
void close(const SocketDescriptor &socketDescriptor)
closes the socket
I32 sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size) override
Protocol specific implementation of send. Called directly with retry from send.
SocketIpStatus bind(const PlatformIntType fd)
bind the UDP to a port such that it can receive packets at the previously configured port
U16 getRecvPort()
get the port being received on
UdpSocket()
Constructor for client socket udp implementation.
SocketIpStatus openProtocol(SocketDescriptor &socketDescriptor) override
udp specific implementation for opening a socket.
SocketIpStatus configureRecv(const char *hostname, const U16 port)
configure the udp socket for incoming transmissions
I32 recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const U32 size) override
Protocol specific implementation of recv. Called directly with error handling from recv.
SocketIpStatus configureSend(const char *hostname, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds)
configure the udp socket for outgoing transmissions
virtual ~UdpSocket()
to cleanup state created at instantiation
static void log(const char *format,...)
log a formated string with supplied arguments
SocketIpStatus
Status enumeration for socket return values.
@ SOCK_INVALID_IP_ADDRESS
Bad IP address supplied.
@ SOCK_SUCCESS
Socket operation successful.
@ SOCK_FAILED_TO_BIND
Failed to bind to socket.
@ SOCK_FAILED_TO_GET_SOCKET
Socket open failed.
@ SOCK_FAILED_TO_READ_BACK_PORT
Failed to read back port from connection.
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
PlatformIntType fd
Used for all sockets to track the communication file descriptor.
struct sockaddr_in m_addr_send
UDP server address, maybe unused.
struct sockaddr_in m_addr_recv
UDP server address, maybe unused.