18 #ifdef TGT_OS_TYPE_VXWORKS 31 #include <arpa/inet.h> 32 #include <sys/socket.h> 43 (void)::memset(&m_addr_send, 0,
sizeof(m_addr_send));
44 (void)::memset(&m_addr_recv, 0,
sizeof(m_addr_recv));
51 const U32 timeout_seconds,
52 const U32 timeout_microseconds) {
55 (void)timeout_seconds;
56 (void)timeout_microseconds;
63 const U32 timeout_seconds,
64 const U32 timeout_microseconds) {
67 FW_ASSERT(timeout_microseconds < 1000000);
78 (void)::memset(&m_addr_recv, 0,
sizeof(m_addr_recv));
79 m_addr_recv.sin_family = AF_INET;
80 m_addr_recv.sin_port = htons(port);
88 this->m_recv_configured =
true;
93 return ntohs(this->m_addr_recv.sin_port);
98 struct sockaddr_in address = this->m_addr_recv;
101 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN 102 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
105 if (::
bind(fd, reinterpret_cast<struct sockaddr*>(&address),
sizeof(address)) < 0) {
109 socklen_t size =
sizeof(address);
110 const int socknameStatus = ::getsockname(fd, reinterpret_cast<struct sockaddr*>(&address), &size);
111 if (socknameStatus == -1) {
116 this->m_addr_recv.sin_port = address.sin_port;
122 if (this->
m_port == 0 && !this->m_recv_configured) {
129 struct sockaddr_in address;
130 (void)::memset(&address, 0,
sizeof(address));
133 U16 recv_port = ntohs(this->m_addr_recv.sin_port);
136 int socketFd = ::socket(AF_INET, SOCK_DGRAM, 0);
137 if (socketFd == -1) {
146 (void)::
close(socketFd);
153 address.sin_family = AF_INET;
154 address.sin_port = htons(this->
m_port);
157 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN 158 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
165 (void)::
close(socketFd);
170 (void)::
close(socketFd);
174 static_assert(
sizeof(m_addr_send) ==
sizeof(address),
"Send address must match the local address structure");
175 (void)memcpy(&this->m_addr_send, &address,
sizeof(this->m_addr_send));
179 if (this->m_recv_configured) {
180 status = this->
bind(socketFd);
183 (void)::
close(socketFd);
189 char recv_addr[INET_ADDRSTRLEN];
190 const char* recv_addr_str = inet_ntop(AF_INET, &(this->m_addr_recv.sin_addr), recv_addr, INET_ADDRSTRLEN);
191 if (recv_addr_str ==
nullptr) {
195 if ((port == 0) && (recv_port > 0)) {
196 Fw::Logger::log(
"Setup to only receive udp at %s:%hu\n", recv_addr, recv_port);
197 }
else if ((port > 0) && (recv_port == 0)) {
199 }
else if ((port > 0) && (recv_port > 0)) {
200 Fw::Logger::log(
"Setup to receive udp at %s:%hu and send to %s:%hu\n", recv_addr, recv_port,
205 socketDescriptor.
fd = socketFd;
210 const U8*
const data,
212 FW_ASSERT(this->m_addr_send.sin_family != 0);
218 reinterpret_cast<struct sockaddr*>(&this->m_addr_send),
sizeof(this->m_addr_send)));
224 FW_ASSERT(this->m_addr_recv.sin_family != 0);
229 struct sockaddr_in sender_addr;
230 (void)::memset(&sender_addr, 0,
sizeof(sender_addr));
232 socklen_t sender_addr_len =
sizeof(sender_addr);
235 reinterpret_cast<struct sockaddr*>(&sender_addr), &sender_addr_len));
237 if (received >= 0 && this->m_addr_send.sin_port == 0) {
238 this->m_addr_send = sender_addr;
239 this->
m_port = ntohs(sender_addr.sin_port);
240 Fw::Logger::log(
"Configured send port to %hu as specified by the last received packet.\n", this->
m_port);
247 FW_ASSERT((size == 0) || (data !=
nullptr));
254 if (errno == EINTR) {
261 if ((errno == EBADF) || (errno == ECONNRESET)) {
Failed to send after configured retries.
U16 getRecvPort()
get the port being received on
Failed to read socket with disconnect.
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