25 (void)::memset(&m_addr_send, 0,
sizeof(m_addr_send));
26 (void)::memset(&m_addr_recv, 0,
sizeof(m_addr_recv));
33 const U32 timeout_seconds,
34 const U32 timeout_microseconds) {
37 (void)timeout_seconds;
38 (void)timeout_microseconds;
45 const U32 timeout_seconds,
46 const U32 timeout_microseconds) {
49 FW_ASSERT(timeout_microseconds < 1000000);
60 (void)::memset(&m_addr_recv, 0,
sizeof(m_addr_recv));
61 m_addr_recv.sin_family = AF_INET;
62 m_addr_recv.sin_port = htons(port);
70 this->m_recv_configured =
true;
75 return ntohs(this->m_addr_recv.sin_port);
80 struct sockaddr_in address = this->m_addr_recv;
83 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN 84 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
87 if (::
bind(fd, reinterpret_cast<struct sockaddr*>(&address),
sizeof(address)) < 0) {
91 socklen_t size =
sizeof(address);
92 const int socknameStatus = ::getsockname(fd, reinterpret_cast<struct sockaddr*>(&address), &size);
93 if (socknameStatus == -1) {
98 this->m_addr_recv.sin_port = address.sin_port;
104 if (this->
m_port == 0 && !this->m_recv_configured) {
111 struct sockaddr_in address;
112 (void)::memset(&address, 0,
sizeof(address));
115 U16 recv_port = ntohs(this->m_addr_recv.sin_port);
118 int socketFd = ::socket(AF_INET, SOCK_DGRAM, 0);
119 if (socketFd == -1) {
128 (void)::
close(socketFd);
135 address.sin_family = AF_INET;
136 address.sin_port = htons(this->
m_port);
139 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN 140 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
147 (void)::
close(socketFd);
152 (void)::
close(socketFd);
156 static_assert(
sizeof(m_addr_send) ==
sizeof(address),
"Send address must match the local address structure");
157 (void)memcpy(&this->m_addr_send, &address,
sizeof(this->m_addr_send));
161 if (this->m_recv_configured) {
162 status = this->
bind(socketFd);
165 (void)::
close(socketFd);
171 char recv_addr[INET_ADDRSTRLEN];
172 const char* recv_addr_str = inet_ntop(AF_INET, &(this->m_addr_recv.sin_addr), recv_addr, INET_ADDRSTRLEN);
173 if (recv_addr_str ==
nullptr) {
177 if ((port == 0) && (recv_port > 0)) {
178 Fw::Logger::log(
"Setup to only receive udp at %s:%hu\n", recv_addr, recv_port);
179 }
else if ((port > 0) && (recv_port == 0)) {
181 }
else if ((port > 0) && (recv_port > 0)) {
182 Fw::Logger::log(
"Setup to receive udp at %s:%hu and send to %s:%hu\n", recv_addr, recv_port,
187 socketDescriptor.
fd = socketFd;
192 const U8*
const data,
195 FW_ASSERT((size == 0) || (data !=
nullptr));
199 if (this->m_addr_send.sin_family == 0) {
206 reinterpret_cast<struct sockaddr*>(&this->m_addr_send),
sizeof(this->m_addr_send)));
212 FW_ASSERT(this->m_addr_recv.sin_family != 0);
217 struct sockaddr_in sender_addr;
218 (void)::memset(&sender_addr, 0,
sizeof(sender_addr));
220 socklen_t sender_addr_len =
sizeof(sender_addr);
223 reinterpret_cast<struct sockaddr*>(&sender_addr), &sender_addr_len));
225 if (received >= 0 && this->m_addr_send.sin_port == 0) {
226 this->m_addr_send = sender_addr;
227 this->
m_port = ntohs(sender_addr.sin_port);
228 Fw::Logger::log(
"Configured send port to %hu as specified by the last received packet.\n", this->
m_port);
235 FW_ASSERT((size == 0) || (data !=
nullptr));
242 if (errno == EINTR) {
249 if ((errno == EBADF) || (errno == ECONNRESET)) {
251 }
else if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
Failed to send after configured retries.
U16 getRecvPort()
get the port being received on
Socket disconnected during read or send.
Interrupted status for retries.
SocketIpStatus send(const SocketDescriptor &socketDescriptor, const U8 *const data, const FwSizeType size) override
UDP-specific implementation of send that handles zero-length datagrams correctly. ...
Failed to configure socket.
PlatformSizeType FwSizeType
SocketIpStatus setupTimeouts(int socketFd)
setup the socket timeout properties of the opened outgoing socket
virtual SocketIpStatus configure(const char *const ipv4_address, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds)
configure the ip socket with an IPv4 address and transmission timeouts
FwSignedSizeType sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const FwSizeType size) override
Protocol specific implementation of send. Called directly with retry from send.
static void log(const char *format,...)
log a formated string with supplied arguments
static SocketIpStatus addressToIp4(const char *const ipv4_address, void *const out)
converts a given IPv4 address in dotted-quad form "x.x.x.x" to a network-order in_addr structure...
SocketIpStatus configureRecv(const char *const ipv4_address, const U16 port)
configure the udp socket for incoming transmissions
int fd
Used for all sockets to track the communication file descriptor.
PlatformSignedSizeType FwSignedSizeType
virtual SocketIpStatus send(const SocketDescriptor &socketDescriptor, const U8 *const data, const FwSizeType size)
send data out the IP socket from the given buffer
virtual ~UdpSocket()
to cleanup state created at instantiation
U16 m_port
IP address port used.
SocketIpStatus configure(const char *const ipv4_address, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds) override
configure is disabled
Socket operation successful.
UdpSocket()
Constructor for client socket udp implementation.
SocketIpStatus configureSend(const char *const ipv4_address, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds)
configure the udp socket for outgoing transmissions
SocketIpStatus bind(const int fd)
bind the UDP to a port such that it can receive packets at the previously configured port ...
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
FwSignedSizeType recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const FwSizeType size) override
Protocol specific implementation of recv. Called directly with error handling from recv...
SocketIpStatus setupSocketOptions(int socketFd)
setup the socket options of the input socket as defined in IpCfg.hpp
Failed to read back port from connection.
virtual bool isValidPort(U16 port) const
Check if the given port is valid for the socket.
uint8_t U8
8-bit unsigned integer
char m_ipv4_address[SOCKET_MAX_IPV4_ADDRESS_SIZE]
IPv4 address (dotted-quad "x.x.x.x")
void close(const SocketDescriptor &socketDescriptor)
closes the socket
SocketIpStatus
Status enumeration for socket return values.
Helper base-class for setting up Berkeley sockets.
Failed to bind to socket.
SocketIpStatus handleZeroReturn() override
Handle zero return from recvProtocol for UDP.
SocketIpStatus openProtocol(SocketDescriptor &socketDescriptor) override
udp specific implementation for opening a socket.
FwSizeType string_length(const CHAR *source, FwSizeType buffer_size)
get the length of the source string