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 if (::getsockname(fd, reinterpret_cast<struct sockaddr*>(&address), &size) == -1) {
115 this->m_addr_recv.sin_port = address.sin_port;
121 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 if ((socketFd = ::socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
143 address.sin_family = AF_INET;
144 address.sin_port = htons(this->
m_port);
147 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN 148 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
170 FW_ASSERT(
sizeof(this->m_addr_send) ==
sizeof(address), static_cast<FwAssertArgType>(
sizeof(this->m_addr_send)),
171 static_cast<FwAssertArgType>(
sizeof(address)));
172 (void)memcpy(&this->m_addr_send, &address,
sizeof(this->m_addr_send));
176 if (this->m_recv_configured) {
177 status = this->
bind(socketFd);
180 (void)::
close(socketFd);
186 char recv_addr[INET_ADDRSTRLEN];
187 const char* recv_addr_str = inet_ntop(AF_INET, &(this->m_addr_recv.sin_addr), recv_addr, INET_ADDRSTRLEN);
188 if (recv_addr_str ==
nullptr) {
192 if ((port == 0) && (recv_port > 0)) {
193 Fw::Logger::log(
"Setup to only receive udp at %s:%hu\n", recv_addr, recv_port);
194 }
else if ((port > 0) && (recv_port == 0)) {
196 }
else if ((port > 0) && (recv_port > 0)) {
197 Fw::Logger::log(
"Setup to receive udp at %s:%hu and send to %s:%hu\n", recv_addr, recv_port,
202 socketDescriptor.
fd = socketFd;
207 const U8*
const data,
209 FW_ASSERT(this->m_addr_send.sin_family != 0);
215 reinterpret_cast<struct sockaddr*>(&this->m_addr_send),
sizeof(this->m_addr_send)));
221 FW_ASSERT(this->m_addr_recv.sin_family != 0);
226 struct sockaddr_in sender_addr;
227 (void)::memset(&sender_addr, 0,
sizeof(sender_addr));
229 socklen_t sender_addr_len =
sizeof(sender_addr);
232 reinterpret_cast<struct sockaddr*>(&sender_addr), &sender_addr_len));
234 if (received >= 0 && this->m_addr_send.sin_port == 0) {
235 this->m_addr_send = sender_addr;
236 this->
m_port = ntohs(sender_addr.sin_port);
237 Fw::Logger::log(
"Configured send port to %hu as specified by the last received packet.\n", this->
m_port);
244 FW_ASSERT((size == 0) || (data !=
nullptr));
251 if (errno == EINTR) {
258 if ((errno == EBADF) || (errno == ECONNRESET)) {
Failed to send after configured retries.
U16 getRecvPort()
get the port being received on
virtual bool isValidPort(U16 port)
Check if the given port is valid for the socket.
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.
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