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));
160 FW_ASSERT(
sizeof(this->m_addr_send) ==
sizeof(address), static_cast<FwAssertArgType>(
sizeof(this->m_addr_send)),
161 static_cast<FwAssertArgType>(
sizeof(address)));
162 (void)memcpy(&this->m_addr_send, &address,
sizeof(this->m_addr_send));
166 if (this->m_recv_configured) {
167 status = this->
bind(socketFd);
170 (void)::
close(socketFd);
176 char recv_addr[INET_ADDRSTRLEN];
177 const char* recv_addr_str = inet_ntop(AF_INET, &(this->m_addr_recv.sin_addr), recv_addr, INET_ADDRSTRLEN);
178 if (recv_addr_str ==
nullptr) {
182 if ((port == 0) && (recv_port > 0)) {
183 Fw::Logger::log(
"Setup to only receive udp at %s:%hu\n", recv_addr, recv_port);
184 }
else if ((port > 0) && (recv_port == 0)) {
186 }
else if ((port > 0) && (recv_port > 0)) {
191 socketDescriptor.
fd = socketFd;
196 FW_ASSERT(this->m_addr_send.sin_family != 0);
201 reinterpret_cast<struct sockaddr*>(&this->m_addr_send),
202 sizeof(this->m_addr_send)));
206 FW_ASSERT(this->m_addr_recv.sin_family != 0);
211 struct sockaddr_in sender_addr;
212 (void)::memset(&sender_addr, 0,
sizeof(sender_addr));
214 socklen_t sender_addr_len =
sizeof(sender_addr);
216 reinterpret_cast<struct sockaddr*>(&sender_addr), &sender_addr_len));
218 if (received >= 0 && this->m_addr_send.sin_port == 0) {
219 this->m_addr_send = sender_addr;
220 this->
m_port = ntohs(sender_addr.sin_port);
221 Fw::Logger::log(
"Configured send port to %hu as specified by the last received packet.\n", this->
m_port);
228 FW_ASSERT((size == 0) || (data !=
nullptr));
233 I32 sent = this->
sendProtocol(socketDescriptor, data, 0);
235 if (errno == EINTR) {
242 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.
SocketIpStatus send(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size) override
UDP-specific implementation of send that handles zero-length datagrams correctly. ...
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.
virtual SocketIpStatus send(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size)
send data out the IP socket from the given buffer
SocketIpStatus setupTimeouts(int socketFd)
setup the socket timeout properties of the opened outgoing socket
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.
SocketIpStatus configureRecv(const char *hostname, const U16 port)
configure the udp socket for incoming transmissions
virtual ~UdpSocket()
to cleanup state created at instantiation
U16 m_port
IP address port used.
I32 sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size) override
Protocol specific implementation of send. Called directly with retry from send.
I32 recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const U32 size) override
Protocol specific implementation of recv. Called directly with error handling from recv...
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
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
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