F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
TcpServerSocket.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title TcpServerSocket.cpp
3 // \author mstarch
4 // \brief cpp file for TcpServerSocket 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 // ======================================================================
13 #include <Fw/Logger/Logger.hpp>
14 #include <FpConfig.hpp>
15 
16 #ifdef TGT_OS_TYPE_VXWORKS
17  #include <socket.h>
18  #include <inetLib.h>
19  #include <fioLib.h>
20  #include <hostLib.h>
21  #include <ioLib.h>
22  #include <vxWorks.h>
23  #include <sockLib.h>
24  #include <taskLib.h>
25  #include <sysLib.h>
26  #include <errnoLib.h>
27  #include <cstring>
28 #elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
29  #include <sys/socket.h>
30  #include <unistd.h>
31  #include <arpa/inet.h>
32 #else
33  #error OS not supported for IP Socket Communications
34 #endif
35 
36 #include <cstring>
37 
38 namespace Drv {
39 
41 
43  NATIVE_INT_TYPE serverFd = -1;
44  struct sockaddr_in address;
45  this->close();
46  // Acquire a socket, or return error
47  if ((serverFd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) {
49  }
50  // Set up the address port and name
51  address.sin_family = AF_INET;
52  address.sin_port = htons(this->m_port);
53 
54  // OS specific settings
55 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
56  address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
57 #endif
58  // First IP address to socket sin_addr
59  if (IpSocket::addressToIp4(m_hostname, &(address.sin_addr)) != SOCK_SUCCESS) {
60  ::close(serverFd);
62  };
63 
64  // TCP requires bind to an address to the socket
65  if (::bind(serverFd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) {
66  ::close(serverFd);
67  return SOCK_FAILED_TO_BIND;
68  }
69  Fw::Logger::logMsg("Listening for single client at %s:%hu\n", reinterpret_cast<POINTER_CAST>(m_hostname), m_port);
70  // TCP requires listening on a the socket. Second argument prevents queueing of anything more than a single client.
71  if (::listen(serverFd, 0) < 0) {
72  ::close(serverFd);
73  return SOCK_FAILED_TO_LISTEN; // What we have here is a failure to communicate
74  }
75  this->m_lock.lock();
76  m_base_fd = serverFd;
77  this->m_lock.unLock();
78 
79  return this->IpSocket::startup();
80 }
81 
83  this->m_lock.lock();
84  if (this->m_base_fd != -1) {
85  (void)::shutdown(this->m_base_fd, SHUT_RDWR);
86  (void)::close(this->m_base_fd);
87  this->m_base_fd = -1;
88  }
89  this->m_lock.unLock();
90  this->IpSocket::shutdown();
91 }
92 
94  NATIVE_INT_TYPE clientFd = -1;
95  NATIVE_INT_TYPE serverFd = -1;
96 
97  // Check started before allowing open
98  if (not this->isStarted()) {
99  return SOCK_NOT_STARTED;
100  }
101 
102  this->m_lock.lock();
103  serverFd = this->m_base_fd;
104  this->m_lock.unLock();
105 
106  // TCP requires accepting on a the socket to get the client socket file descriptor.
107  clientFd = ::accept(serverFd, nullptr, nullptr);
108  if (clientFd < 0) {
109  return SOCK_FAILED_TO_ACCEPT; // What we have here is a failure to communicate
110  }
111  // Setup client send timeouts
112  if (IpSocket::setupTimeouts(clientFd) != SOCK_SUCCESS) {
113  ::close(clientFd);
115  }
116 
117  Fw::Logger::logMsg("Accepted client at %s:%hu\n", reinterpret_cast<POINTER_CAST>(m_hostname), m_port);
118  fd = clientFd;
119  return SOCK_SUCCESS;
120 }
121 
122 I32 TcpServerSocket::sendProtocol(const U8* const data, const U32 size) {
123  return ::send(this->m_fd, data, size, SOCKET_IP_SEND_FLAGS);
124 }
125 
126 I32 TcpServerSocket::recvProtocol(U8* const data, const U32 size) {
127  return ::recv(this->m_fd, data, size, SOCKET_IP_RECV_FLAGS);
128 }
129 
130 } // namespace Drv
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_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:46
void close()
closes the socket
Definition: IpSocket.cpp:117
U16 m_port
IP address port used.
Definition: IpSocket.hpp:207
bool isStarted()
Returns true when the socket is started.
Definition: IpSocket.cpp:101
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
Definition: IpSocket.hpp:210
Os::Mutex m_lock
Definition: IpSocket.hpp:203
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:80
NATIVE_INT_TYPE m_fd
Definition: IpSocket.hpp:204
SocketIpStatus setupTimeouts(NATIVE_INT_TYPE socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition: IpSocket.cpp:63
virtual void shutdown()
shutdown the socket
Definition: IpSocket.cpp:128
virtual SocketIpStatus startup()
startup the socket, a no-op on unless this is server
Definition: IpSocket.cpp:135
TcpServerSocket()
Constructor for client socket tcp implementation.
I32 sendProtocol(const U8 *const data, const U32 size) override
Protocol specific implementation of send. Called directly with retry from send.
SocketIpStatus openProtocol(NATIVE_INT_TYPE &fd) override
Tcp specific implementation for opening a client socket connected to this server.
void shutdown() override
Shutdown and close the server socket followed by the open client.
I32 recvProtocol(U8 *const data, const U32 size) override
Protocol specific implementation of recv. Called directly with error handling from recv.
SocketIpStatus startup() override
Opens the server socket and listens, does not block.
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()
unlock the mutex
Definition: Mutex.cpp:13
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_FAILED_TO_ACCEPT
Failed to accept connection.
Definition: IpSocket.hpp:35
@ 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_SET_SOCKET_OPTIONS
Failed to configure socket.
Definition: IpSocket.hpp:29
@ SOCK_FAILED_TO_GET_SOCKET
Socket open failed.
Definition: IpSocket.hpp:25
@ SOCK_FAILED_TO_LISTEN
Failed to listen on socket.
Definition: IpSocket.hpp:34
@ SOCK_NOT_STARTED
Socket has not been started.
Definition: IpSocket.hpp:37