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) {
59 const U32 timeout_seconds,
60 const U32 timeout_microseconds) {
63 FW_ASSERT(timeout_microseconds < 1000000);
73 (void)::memset(&m_addr_recv, 0,
sizeof(m_addr_recv));
74 m_addr_recv.sin_family = AF_INET;
75 m_addr_recv.sin_port = htons(port);
83 this->m_recv_configured =
true;
88 return ntohs(this->m_addr_recv.sin_port);
93 struct sockaddr_in address = this->m_addr_recv;
96 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN 97 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) {
110 this->m_addr_recv.sin_port = address.sin_port;
116 if (this->
m_port == 0 && !this->m_recv_configured) {
124 struct sockaddr_in address;
125 (void)::memset(&address, 0,
sizeof(address));
128 U16 recv_port = ntohs(this->m_addr_recv.sin_port);
131 if ((socketFd = ::socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
138 address.sin_family = AF_INET;
139 address.sin_port = htons(this->
m_port);
142 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN 143 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
165 FW_ASSERT(
sizeof(this->m_addr_send) ==
sizeof(address), static_cast<FwAssertArgType>(
sizeof(this->m_addr_send)),
166 static_cast<FwAssertArgType>(
sizeof(address)));
167 (void)memcpy(&this->m_addr_send, &address,
sizeof(this->m_addr_send));
171 if (this->m_recv_configured) {
172 status = this->
bind(socketFd);
175 (void)::
close(socketFd);
181 char recv_addr[INET_ADDRSTRLEN];
182 const char* recv_addr_str = inet_ntop(AF_INET, &(this->m_addr_recv.sin_addr), recv_addr, INET_ADDRSTRLEN);
183 if (recv_addr_str ==
nullptr) {
187 if ((port == 0) && (recv_port > 0)) {
188 Fw::Logger::log(
"Setup to only receive udp at %s:%hu\n", recv_addr, recv_port);
189 }
else if ((port > 0) && (recv_port == 0)) {
191 }
else if ((port > 0) && (recv_port > 0)) {
196 socketDescriptor.
fd = socketFd;
201 const U8*
const data,
203 FW_ASSERT(this->m_addr_send.sin_family != 0);
209 reinterpret_cast<struct sockaddr*>(&this->m_addr_send),
sizeof(this->m_addr_send)));
215 FW_ASSERT(this->m_addr_recv.sin_family != 0);
220 struct sockaddr_in sender_addr;
221 (void)::memset(&sender_addr, 0,
sizeof(sender_addr));
223 socklen_t sender_addr_len =
sizeof(sender_addr);
226 reinterpret_cast<struct sockaddr*>(&sender_addr), &sender_addr_len));
228 if (received >= 0 && this->m_addr_send.sin_port == 0) {
229 this->m_addr_send = sender_addr;
230 this->
m_port = ntohs(sender_addr.sin_port);
231 Fw::Logger::log(
"Configured send port to %hu as specified by the last received packet.\n", this->
m_port);
238 FW_ASSERT((size == 0) || (data !=
nullptr));
245 if (errno == EINTR) {
252 if ((errno == EBADF) || (errno == ECONNRESET)) {
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.
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
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
int fd
Used for all sockets to track the communication file descriptor.
PlatformSignedSizeType FwSignedSizeType
SocketIpStatus configureRecv(const char *hostname, const U16 port)
configure the udp socket for incoming transmissions
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.
Socket operation successful.
UdpSocket()
Constructor for client socket udp implementation.
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 configureSend(const char *hostname, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds)
configure the udp socket for outgoing transmissions
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
virtual 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
void close(const SocketDescriptor &socketDescriptor)
closes the socket
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
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 configure(const char *hostname, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds) override
configure is disabled
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