F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
IpSocket.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title IpSocket.hpp
3 // \author mstarch
4 // \brief hpp file for IpSocket 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 #ifndef DRV_IP_IPHELPER_HPP_
13 #define DRV_IP_IPHELPER_HPP_
14 
15 #include <Fw/FPrimeBasicTypes.hpp>
16 #include <Os/Mutex.hpp>
17 #include <config/IpCfg.hpp>
18 
19 // MSG_NOSIGNAL must be visible when SEND_NO_SIGNAL_FLAGS is defined below, independent of what IpCfg.hpp includes
20 #ifdef TGT_OS_TYPE_VXWORKS
21 #include <sockLib.h>
22 #include <socket.h>
23 #elif defined(TGT_OS_TYPE_LINUX) || defined(TGT_OS_TYPE_DARWIN)
24 #include <sys/socket.h>
25 #endif
26 
27 namespace Drv {
28 
29 struct SocketDescriptor final {
30  int fd = -1;
31  int serverFd = -1;
32 };
33 
57 };
58 
65 class IpSocket {
66  public:
67  IpSocket();
68  virtual ~IpSocket() {};
95  virtual SocketIpStatus configure(const char* const ipv4_address,
96  const U16 port,
97  const U32 send_timeout_seconds,
98  const U32 send_timeout_microseconds);
99 
118  SocketIpStatus open(SocketDescriptor& socketDescriptor);
138  virtual SocketIpStatus send(const SocketDescriptor& socketDescriptor, const U8* const data, const FwSizeType size);
155  SocketIpStatus recv(const SocketDescriptor& fd, U8* const data, FwSizeType& size);
156 
165  void close(const SocketDescriptor& socketDescriptor);
166 
178  void shutdown(const SocketDescriptor& socketDescriptor);
179 
180  protected:
191  virtual bool isValidPort(U16 port) const;
192 
198  SocketIpStatus setupTimeouts(int socketFd);
199 
203 #ifdef MSG_NOSIGNAL
204  static constexpr int SEND_NO_SIGNAL_FLAGS = MSG_NOSIGNAL;
205 #else
206  static constexpr int SEND_NO_SIGNAL_FLAGS = 0;
207 #endif
208 
218  SocketIpStatus setupNoSigPipe(int socketFd);
219 
232  static SocketIpStatus addressToIp4(const char* const ipv4_address, void* const out);
246  virtual FwSignedSizeType sendProtocol(const SocketDescriptor& socketDescriptor,
247  const U8* const data,
248  const FwSizeType size) = 0;
249 
257  virtual FwSignedSizeType recvProtocol(const SocketDescriptor& socketDescriptor,
258  U8* const data,
259  const FwSizeType size) = 0;
260 
271 
277  SocketIpStatus setupSocketOptions(int socketFd);
278 
281  U16 m_port;
283 };
284 } // namespace Drv
285 
286 #endif /* DRV_SOCKETIPDRIVER_SOCKETHELPER_HPP_ */
static constexpr int SEND_NO_SIGNAL_FLAGS
Definition: IpSocket.hpp:206
Failed to send after configured retries.
Definition: IpSocket.hpp:50
Socket disconnected during read or send.
Definition: IpSocket.hpp:46
Socket open failed.
Definition: IpSocket.hpp:39
Interrupted status for retries.
Definition: IpSocket.hpp:44
Failed to connect socket.
Definition: IpSocket.hpp:42
Failed to configure socket.
Definition: IpSocket.hpp:43
PlatformSizeType FwSizeType
SocketIpStatus recv(const SocketDescriptor &fd, U8 *const data, FwSizeType &size)
receive data from the IP socket from the given buffer
Definition: IpSocket.cpp:201
Host IP lookup failed.
Definition: IpSocket.hpp:40
SocketIpStatus setupTimeouts(int socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition: IpSocket.cpp:75
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
Definition: IpSocket.cpp:51
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...
Definition: IpSocket.cpp:104
virtual ~IpSocket()
Definition: IpSocket.hpp:68
Automatic connections are disabled.
Definition: IpSocket.hpp:55
int fd
Used for all sockets to track the communication file descriptor.
Definition: IpSocket.hpp:30
PlatformSignedSizeType FwSignedSizeType
SocketIpStatus setupNoSigPipe(int socketFd)
prevent sends on a stream socket from raising SIGPIPE on platforms without MSG_NOSIGNAL ...
Definition: IpSocket.cpp:92
virtual SocketIpStatus send(const SocketDescriptor &socketDescriptor, const U8 *const data, const FwSizeType size)
send data out the IP socket from the given buffer
Definition: IpSocket.cpp:159
Bad IP address supplied.
Definition: IpSocket.hpp:41
Another thread is opening.
Definition: IpSocket.hpp:54
virtual SocketIpStatus openProtocol(SocketDescriptor &fd)=0
Protocol specific open implementation, called from open.
void shutdown(const SocketDescriptor &socketDescriptor)
shutdown the socket
Definition: IpSocket.cpp:138
Failed to listen on socket.
Definition: IpSocket.hpp:48
virtual FwSignedSizeType sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const FwSizeType size)=0
Protocol specific implementation of send. Called directly with retry from send.
U16 m_port
IP address port used.
Definition: IpSocket.hpp:281
Failed to accept connection.
Definition: IpSocket.hpp:49
Socket operation successful.
Definition: IpSocket.hpp:38
Failed to read socket.
Definition: IpSocket.hpp:45
SocketIpStatus setupSocketOptions(int socketFd)
setup the socket options of the input socket as defined in IpCfg.hpp
Definition: IpSocket.cpp:253
Failed to read back port from connection.
Definition: IpSocket.hpp:52
virtual bool isValidPort(U16 port) const
Check if the given port is valid for the socket.
Definition: IpSocket.cpp:71
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:54
Operation is invalid.
Definition: IpSocket.hpp:56
virtual SocketIpStatus handleZeroReturn()
Handle zero return from recvProtocol.
Definition: IpSocket.cpp:247
char m_ipv4_address[SOCKET_MAX_IPV4_ADDRESS_SIZE]
IPv4 address (dotted-quad "x.x.x.x")
Definition: IpSocket.hpp:282
void close(const SocketDescriptor &socketDescriptor)
closes the socket
Definition: IpSocket.cpp:132
virtual FwSignedSizeType recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const FwSizeType size)=0
Protocol specific implementation of recv. Called directly with error handling from recv...
SocketIpStatus open(SocketDescriptor &socketDescriptor)
open the IP socket for communications
Definition: IpSocket.cpp:147
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:37
Socket has not been started.
Definition: IpSocket.hpp:51
U32 m_timeoutMicroseconds
Definition: IpSocket.hpp:280
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:65
No data available or read operation would block.
Definition: IpSocket.hpp:53
Failed to bind to socket.
Definition: IpSocket.hpp:47
int serverFd
Used for server sockets to track the listening file descriptor.
Definition: IpSocket.hpp:31
U32 m_timeoutSeconds
Definition: IpSocket.hpp:279