F´ Flight Software - C/C++ Documentation  devel
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::configureSend(const char* const hostname, const U16 port, const U32 timeout_seconds, const U32 timeout_microseconds) {
63  //Timeout is for the send, so configure send will work with the base class
64  FW_ASSERT(port != 0, port); // Send cannot be on port 0
65  return this->IpSocket::configure(hostname, port, timeout_seconds, timeout_microseconds);
66 }
67 
68 SocketIpStatus UdpSocket::configureRecv(const char* hostname, const U16 port) {
69  FW_ASSERT(this->isValidPort(port));
70  this->m_lock.lock();
71  this->m_recv_port = port;
72  (void) Fw::StringUtils::string_copy(this->m_recv_hostname, hostname, SOCKET_MAX_HOSTNAME_SIZE);
73  this->m_lock.unlock();
74  return SOCK_SUCCESS;
75 }
76 
78  this->m_lock.lock();
79  U16 port = this->m_recv_port;
80  this->m_lock.unlock();
81  return port;
82 }
83 
84 
86  struct sockaddr_in address;
87  FW_ASSERT(-1 != fd);
88 
89  // Set up the address port and name
90  address.sin_family = AF_INET;
91  this->m_lock.lock();
92  address.sin_port = htons(m_recv_port);
93  this->m_lock.unlock();
94  // OS specific settings
95 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
96  address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
97 #endif
98 
99  // First IP address to socket sin_addr
100  if (IpSocket::addressToIp4(m_recv_hostname, &address.sin_addr) != SOCK_SUCCESS) {
102  };
103  // UDP (for receiving) requires bind to an address to the socket
104  if (::bind(fd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) {
105  return SOCK_FAILED_TO_BIND;
106  }
107 
108  socklen_t size = sizeof(address);
109  if (::getsockname(fd, reinterpret_cast<struct sockaddr *>(&address), &size) == -1) {
111  }
112  U16 port = ntohs(address.sin_port);
113 
114  this->m_lock.lock();
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  this->m_recv_port = port;
118  this->m_lock.unlock();
119 
120  return SOCK_SUCCESS;
121 }
122 
124  SocketIpStatus status = SOCK_SUCCESS;
125  NATIVE_INT_TYPE socketFd = -1;
126  struct sockaddr_in address;
127 
128  this->m_lock.lock();
129  U16 port = this->m_port;
130  this->m_lock.unlock();
131 
132  // Acquire a socket, or return error
133  if ((socketFd = ::socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
135  }
136 
137  // May not be sending in all cases
138  if (port != 0) {
139  // Set up the address port and name
140  address.sin_family = AF_INET;
141  this->m_lock.lock();
142  address.sin_port = htons(this->m_port);
143  this->m_lock.unlock();
144 
145  // OS specific settings
146 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
147  address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
148 #endif
149 
150  // First IP address to socket sin_addr
151  if ((status = IpSocket::addressToIp4(m_hostname, &(address.sin_addr))) != SOCK_SUCCESS) {
152  ::close(socketFd);
153  return status;
154  };
155 
156  // Now apply timeouts
157  if ((status = IpSocket::setupTimeouts(socketFd)) != SOCK_SUCCESS) {
158  ::close(socketFd);
159  return status;
160  }
161  this->m_lock.lock();
162  FW_ASSERT(sizeof(this->m_state->m_addr_send) == sizeof(address), sizeof(this->m_state->m_addr_send),
163  sizeof(address));
164  memcpy(&this->m_state->m_addr_send, &address, sizeof(this->m_state->m_addr_send));
165  this->m_lock.unlock();
166  }
167 
168  // When we are setting up for receiving as well, then we must bind to a port
169  if ((status = this->bind(socketFd)) != SOCK_SUCCESS) {
170  ::close(socketFd);
171  return status; // Not closing FD as it is still a valid send FD
172  }
173  this->m_lock.lock();
174  U16 recv_port = this->m_recv_port;
175  this->m_lock.unlock();
176  // Log message for UDP
177  if (port == 0) {
178  Fw::Logger::logMsg("Setup to receive udp at %s:%hu\n", reinterpret_cast<POINTER_CAST>(m_recv_hostname),
179  recv_port);
180  } else {
181  Fw::Logger::logMsg("Setup to receive udp at %s:%hu and send to %s:%hu\n",
182  reinterpret_cast<POINTER_CAST>(m_recv_hostname),
183  recv_port,
184  reinterpret_cast<POINTER_CAST>(m_hostname),
185  port);
186  }
187  FW_ASSERT(status == SOCK_SUCCESS, status);
188  fd = socketFd;
189  return status;
190 }
191 
192 I32 UdpSocket::sendProtocol(const U8* const data, const U32 size) {
193  FW_ASSERT(this->m_state->m_addr_send.sin_family != 0); // Make sure the address was previously setup
194  return static_cast<I32>(::sendto(this->m_fd, data, size, SOCKET_IP_SEND_FLAGS,
195  reinterpret_cast<struct sockaddr *>(&this->m_state->m_addr_send), sizeof(this->m_state->m_addr_send)));
196 }
197 
198 I32 UdpSocket::recvProtocol(U8* const data, const U32 size) {
199  FW_ASSERT(this->m_state->m_addr_recv.sin_family != 0); // Make sure the address was previously setup
200  return static_cast<I32>(::recvfrom(this->m_fd, data, size, SOCKET_IP_RECV_FLAGS, nullptr, nullptr));
201 }
202 
203 } // namespace Drv
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformPointerCastType POINTER_CAST
Definition: BasicTypes.h:53
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
C++-compatible configuration header for fprime configuration.
@ SOCKET_MAX_HOSTNAME_SIZE
Definition: IpCfg.hpp:22
@ SOCKET_IP_RECV_FLAGS
Definition: IpCfg.hpp:20
@ SOCKET_IP_SEND_FLAGS
Definition: IpCfg.hpp:19
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:47
void close()
closes the socket
Definition: IpSocket.cpp:123
U16 m_port
IP address port used.
Definition: IpSocket.hpp:219
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
Definition: IpSocket.hpp:222
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
Os::Mutex m_lock
Definition: IpSocket.hpp:215
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:86
virtual bool isValidPort(U16 port)
Check if the given port is valid for the socket.
Definition: IpSocket.cpp:65
NATIVE_INT_TYPE m_fd
Definition: IpSocket.hpp:216
SocketIpStatus setupTimeouts(NATIVE_INT_TYPE socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition: IpSocket.cpp:69
U16 getRecvPort()
get the port being received on
Definition: UdpSocket.cpp:77
SocketIpStatus bind(NATIVE_INT_TYPE fd)
bind the UDP to a port such that it can receive packets at the previously configured port
Definition: UdpSocket.cpp:85
UdpSocket()
Constructor for client socket udp implementation.
Definition: UdpSocket.cpp:53
SocketIpStatus openProtocol(NATIVE_INT_TYPE &fd) override
udp specific implementation for opening a socket.
Definition: UdpSocket.cpp:123
I32 sendProtocol(const U8 *const data, const U32 size) override
Protocol specific implementation of send. Called directly with retry from send.
Definition: UdpSocket.cpp:192
SocketIpStatus configureRecv(const char *hostname, const U16 port)
configure the udp socket for incoming transmissions
Definition: UdpSocket.cpp:68
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:62
I32 recvProtocol(U8 *const data, const U32 size) override
Protocol specific implementation of recv. Called directly with error handling from recv.
Definition: UdpSocket.cpp:198
virtual ~UdpSocket()
to cleanup state created at instantiation
Definition: UdpSocket.cpp:57
static void logMsg(const char *fmt, POINTER_CAST a0=0, POINTER_CAST a1=0, POINTER_CAST a2=0, POINTER_CAST a3=0, POINTER_CAST a4=0, POINTER_CAST a5=0, POINTER_CAST a6=0, POINTER_CAST a7=0, POINTER_CAST a8=0, POINTER_CAST a9=0)
Definition: Logger.cpp:18
void unlock()
alias for unLock to meet BasicLockable requirements
Definition: Mutex.hpp:16
void lock()
lock the mutex
Definition: Mutex.cpp:12
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:23
@ SOCK_INVALID_IP_ADDRESS
Bad IP address supplied.
Definition: IpSocket.hpp:27
@ SOCK_SUCCESS
Socket operation successful.
Definition: IpSocket.hpp:24
@ SOCK_FAILED_TO_BIND
Failed to bind to socket.
Definition: IpSocket.hpp:33
@ SOCK_FAILED_TO_GET_SOCKET
Socket open failed.
Definition: IpSocket.hpp:25
@ SOCK_FAILED_TO_READ_BACK_PORT
Failed to read back port from connection.
Definition: IpSocket.hpp:38
char * string_copy(char *destination, const char *source, U32 num)
copy string with null-termination guaranteed
Definition: StringUtils.cpp:6
struct sockaddr_in m_addr_send
UDP server address, maybe unused.
Definition: UdpSocket.cpp:44
struct sockaddr_in m_addr_recv
UDP server address, maybe unused.
Definition: UdpSocket.cpp:45