F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
UdpSocket.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title UdpSocket.cpp
3 // \author mstarch
4 // \brief cpp file for UdpSocket core implementation classes
5 //
6 // \copyright
7 // Copyright 2009-2020, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 #include <Drv/Ip/UdpSocket.hpp>
13 #include <Fw/Logger/Logger.hpp>
14 #include <Fw/Types/Assert.hpp>
15 #include <FpConfig.hpp>
16 #include <Fw/Types/StringUtils.hpp>
17 
18 #ifdef TGT_OS_TYPE_VXWORKS
19  #include <socket.h>
20  #include <inetLib.h>
21  #include <fioLib.h>
22  #include <hostLib.h>
23  #include <ioLib.h>
24  #include <vxWorks.h>
25  #include <sockLib.h>
26  #include <taskLib.h>
27  #include <sysLib.h>
28  #include <errnoLib.h>
29  #include <cstring>
30 #elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
31  #include <sys/socket.h>
32  #include <unistd.h>
33  #include <arpa/inet.h>
34 #else
35  #error OS not supported for IP Socket Communications
36 #endif
37 
38 #include <cstring>
39 #include <new>
40 
41 namespace Drv {
42 
43 struct SocketState {
44  struct sockaddr_in m_addr_send;
45  struct sockaddr_in m_addr_recv;
46 
48  ::memset(&m_addr_send, 0, sizeof(m_addr_send));
49  ::memset(&m_addr_recv, 0, sizeof(m_addr_recv));
50  }
51 };
52 
53 UdpSocket::UdpSocket() : IpSocket(), m_state(new(std::nothrow) SocketState), m_recv_port(0) {
54  FW_ASSERT(m_state != nullptr);
55 }
56 
58  FW_ASSERT(m_state);
59  delete m_state;
60 }
61 
62 SocketIpStatus UdpSocket::configure(const char* const hostname, const U16 port, const U32 timeout_seconds, const U32 timeout_microseconds) {
63  FW_ASSERT(0); // Must use configureSend and/or configureRecv
65 }
66 
67 
68 SocketIpStatus UdpSocket::configureSend(const char* const hostname, const U16 port, const U32 timeout_seconds, const U32 timeout_microseconds) {
69  //Timeout is for the send, so configure send will work with the base class
70  FW_ASSERT(port != 0, port); // Send cannot be on port 0
71  FW_ASSERT(hostname != nullptr);
72  return this->IpSocket::configure(hostname, port, timeout_seconds, timeout_microseconds);
73 }
74 
75 SocketIpStatus UdpSocket::configureRecv(const char* hostname, const U16 port) {
76  FW_ASSERT(this->isValidPort(port));
77  FW_ASSERT(hostname != nullptr);
78  this->m_recv_port = port;
79  (void) Fw::StringUtils::string_copy(this->m_recv_hostname, hostname, static_cast<FwSizeType>(SOCKET_MAX_HOSTNAME_SIZE));
80  return SOCK_SUCCESS;
81 }
82 
84  U16 port = this->m_recv_port;
85  return port;
86 }
87 
88 
90  struct sockaddr_in address;
91  FW_ASSERT(fd != -1);
92 
93  // Set up the address port and name
94  address.sin_family = AF_INET;
95  address.sin_port = htons(this->m_recv_port);
96  // OS specific settings
97 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
98  address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
99 #endif
100 
101  // First IP address to socket sin_addr
102  if (IpSocket::addressToIp4(m_recv_hostname, &address.sin_addr) != SOCK_SUCCESS) {
104  };
105  // UDP (for receiving) requires bind to an address to the socket
106  if (::bind(fd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) {
107  return SOCK_FAILED_TO_BIND;
108  }
109 
110  socklen_t size = sizeof(address);
111  if (::getsockname(fd, reinterpret_cast<struct sockaddr *>(&address), &size) == -1) {
113  }
114 
115  FW_ASSERT(sizeof(this->m_state->m_addr_recv) == sizeof(address), sizeof(this->m_state->m_addr_recv), sizeof(address));
116  memcpy(&this->m_state->m_addr_recv, &address, sizeof(this->m_state->m_addr_recv));
117 
118  return SOCK_SUCCESS;
119 }
120 
122  SocketIpStatus status = SOCK_SUCCESS;
123  NATIVE_INT_TYPE socketFd = -1;
124  struct sockaddr_in address;
125 
126  U16 port = this->m_port;
127 
128  // Acquire a socket, or return error
129  if ((socketFd = ::socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
131  }
132 
133  // May not be sending in all cases
134  if (port != 0) {
135  // Set up the address port and name
136  address.sin_family = AF_INET;
137  address.sin_port = htons(this->m_port);
138 
139  // OS specific settings
140 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
141  address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
142 #endif
143 
144  // First IP address to socket sin_addr
145  if ((status = IpSocket::addressToIp4(m_hostname, &(address.sin_addr))) != SOCK_SUCCESS) {
146  ::close(socketFd);
147  return status;
148  };
149 
150  // Now apply timeouts
151  if ((status = IpSocket::setupTimeouts(socketFd)) != SOCK_SUCCESS) {
152  ::close(socketFd);
153  return status;
154  }
155  FW_ASSERT(sizeof(this->m_state->m_addr_send) == sizeof(address), sizeof(this->m_state->m_addr_send),
156  sizeof(address));
157  memcpy(&this->m_state->m_addr_send, &address, sizeof(this->m_state->m_addr_send));
158  }
159 
160  // Receive port set up only done when configure receive was called
161  U16 recv_port = this->m_recv_port;
162  if (recv_port != 0) {
163  status = this->bind(socketFd);
164  // When we are setting up for receiving as well, then we must bind to a port
165  if (status != SOCK_SUCCESS) {
166  (void) ::close(socketFd); // Closing FD as a retry will reopen send side
167  return status;
168  }
169  }
170 
171  // Log message for UDP
172  if ((port == 0) && (recv_port > 0)) {
173  Fw::Logger::log("Setup to only receive udp at %s:%hu\n", m_recv_hostname,
174  recv_port);
175  } else if ((port > 0) && (recv_port == 0)) {
176  Fw::Logger::log("Setup to only send udp at %s:%hu\n", m_hostname,
177  port);
178  } else if ((port > 0) && (recv_port > 0)) {
179  Fw::Logger::log("Setup to receive udp at %s:%hu and send to %s:%hu\n",
180  m_recv_hostname,
181  recv_port,
182  m_hostname,
183  port);
184  }
185  // Neither configuration method was called
186  else {
187  FW_ASSERT(port > 0 || recv_port > 0, port, recv_port);
188  }
189  FW_ASSERT(status == SOCK_SUCCESS, status);
190  socketDescriptor.fd = socketFd;
191  return status;
192 }
193 
194 I32 UdpSocket::sendProtocol(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) {
195  FW_ASSERT(this->m_state->m_addr_send.sin_family != 0); // Make sure the address was previously setup
196  return static_cast<I32>(::sendto(socketDescriptor.fd, data, size, SOCKET_IP_SEND_FLAGS,
197  reinterpret_cast<struct sockaddr *>(&this->m_state->m_addr_send), sizeof(this->m_state->m_addr_send)));
198 }
199 
200 I32 UdpSocket::recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) {
201  FW_ASSERT(this->m_state->m_addr_recv.sin_family != 0); // Make sure the address was previously setup
202  return static_cast<I32>(::recvfrom(socketDescriptor.fd, data, size, SOCKET_IP_RECV_FLAGS, nullptr, nullptr));
203 }
204 
205 } // namespace Drv
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.
Definition: IpSocket.cpp:85
U16 getRecvPort()
get the port being received on
Definition: UdpSocket.cpp:83
virtual bool isValidPort(U16 port)
Check if the given port is valid for the socket.
Definition: IpSocket.cpp:64
Socket open failed.
Definition: IpSocket.hpp:31
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
static void log(const char *format,...)
log a formated string with supplied arguments
Definition: Logger.cpp:21
SocketIpStatus configureRecv(const char *hostname, const U16 port)
configure the udp socket for incoming transmissions
Definition: UdpSocket.cpp:75
struct sockaddr_in m_addr_recv
UDP server address, maybe unused.
Definition: UdpSocket.cpp:45
Bad IP address supplied.
Definition: IpSocket.hpp:33
virtual ~UdpSocket()
to cleanup state created at instantiation
Definition: UdpSocket.cpp:57
SocketIpStatus bind(const PlatformIntType fd)
bind the UDP to a port such that it can receive packets at the previously configured port ...
Definition: UdpSocket.cpp:89
int PlatformIntType
DefaultTypes.hpp provides fallback defaults for the platform types.
struct sockaddr_in m_addr_send
UDP server address, maybe unused.
Definition: UdpSocket.cpp:44
U16 m_port
IP address port used.
Definition: IpSocket.hpp:212
I32 sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size) override
Protocol specific implementation of send. Called directly with retry from send.
Definition: UdpSocket.cpp:194
I32 recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const U32 size) override
Protocol specific implementation of recv. Called directly with error handling from recv...
Definition: UdpSocket.cpp:200
Socket operation successful.
Definition: IpSocket.hpp:30
UdpSocket()
Constructor for client socket udp implementation.
Definition: UdpSocket.cpp:53
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
Definition: StringUtils.cpp:7
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
Definition: UdpSocket.cpp:68
Failed to read back port from connection.
Definition: IpSocket.hpp:44
SocketIpStatus setupTimeouts(PlatformIntType socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition: IpSocket.cpp:68
C++-compatible configuration header for fprime configuration.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
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
Definition: IpSocket.cpp:53
Operation is invalid.
Definition: IpSocket.hpp:48
PlatformIntType fd
Used for all sockets to track the communication file descriptor.
Definition: IpSocket.hpp:22
void close(const SocketDescriptor &socketDescriptor)
closes the socket
Definition: IpSocket.cpp:106
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
Definition: IpSocket.hpp:213
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:29
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:57
Failed to bind to socket.
Definition: IpSocket.hpp:39
#define FW_ASSERT(...)
Definition: Assert.hpp:14
SocketIpStatus configure(const char *hostname, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds) override
configure is disabled
Definition: UdpSocket.cpp:62
SocketIpStatus openProtocol(SocketDescriptor &socketDescriptor) override
udp specific implementation for opening a socket.
Definition: UdpSocket.cpp:121