F´ Flight Software - C/C++ Documentation  NASA-v2.0.1
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <Fw/Types/BasicTypes.hpp>
15 
16 
17 #ifdef TGT_OS_TYPE_VXWORKS
18  #include <socket.h>
19  #include <inetLib.h>
20  #include <fioLib.h>
21  #include <hostLib.h>
22  #include <ioLib.h>
23  #include <vxWorks.h>
24  #include <sockLib.h>
25  #include <taskLib.h>
26  #include <sysLib.h>
27  #include <errnoLib.h>
28  #include <string.h>
29 #elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
30  #include <sys/socket.h>
31  #include <unistd.h>
32  #include <arpa/inet.h>
33 #else
34  #error OS not supported for IP Socket Communications
35 #endif
36 
37 #include <string.h>
38 
39 namespace Drv {
40 
42 
44  NATIVE_INT_TYPE serverFd = -1;
45  struct sockaddr_in address;
46  this->close();
47 
48  // Acquire a socket, or return error
49  if ((serverFd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) {
51  }
52  // Set up the address port and name
53  address.sin_family = AF_INET;
54  address.sin_port = htons(this->m_port);
55 
56  // OS specific settings
57 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
58  address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
59 #endif
60  // First IP address to socket sin_addr
61  if (IpSocket::addressToIp4(m_hostname, &(address.sin_addr)) != SOCK_SUCCESS) {
62  ::close(serverFd);
64  };
65 
66  // TCP requires bind to an address to the socket
67  if (::bind(serverFd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) {
68  ::close(serverFd);
69  return SOCK_FAILED_TO_BIND;
70  }
71  m_base_fd = serverFd;
72  Fw::Logger::logMsg("Listening for single client at %s:%hu\n", reinterpret_cast<POINTER_CAST>(m_hostname), m_port);
73  // TCP requires listening on a the socket. Second argument prevents queueing of anything more than a single client.
74  if (::listen(serverFd, 0) < 0) {
75  ::close(serverFd);
76  return SOCK_FAILED_TO_LISTEN; // What we have here is a failure to communicate
77  }
78  return SOCK_SUCCESS;
79 }
80 
82  (void)::shutdown(this->m_base_fd, SHUT_RDWR);
83  (void)::close(this->m_base_fd);
84  m_base_fd = -1;
85  this->close();
86 }
87 
89  NATIVE_INT_TYPE clientFd = -1;
90  // TCP requires accepting on a the socket to get the client socket file descriptor.
91  if ((clientFd = ::accept(m_base_fd, NULL, NULL)) < 0) {
92  return SOCK_FAILED_TO_ACCEPT; // What we have here is a failure to communicate
93  }
94  // Setup client send timeouts
95  if (IpSocket::setupTimeouts(clientFd) != SOCK_SUCCESS) {
96  ::close(clientFd);
98  }
99  Fw::Logger::logMsg("Accepted client at %s:%hu\n", reinterpret_cast<POINTER_CAST>(m_hostname), m_port);
100  fd = clientFd;
101  return SOCK_SUCCESS;
102 }
103 
104 I32 TcpServerSocket::sendProtocol(const U8* const data, const U32 size) {
105  return ::send(this->m_fd, data, size, SOCKET_IP_SEND_FLAGS);
106 }
107 
108 I32 TcpServerSocket::recvProtocol(U8* const data, const U32 size) {
109  return ::recv(this->m_fd, data, size, SOCKET_IP_RECV_FLAGS);
110 }
111 
112 } // namespace Drv
Drv::SOCK_SUCCESS
@ SOCK_SUCCESS
Socket operation successful.
Definition: IpSocket.hpp:24
Drv::SOCK_FAILED_TO_SET_SOCKET_OPTIONS
@ SOCK_FAILED_TO_SET_SOCKET_OPTIONS
Failed to configure socket.
Definition: IpSocket.hpp:29
Drv::IpSocket::setupTimeouts
SocketIpStatus setupTimeouts(NATIVE_INT_TYPE socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition: IpSocket.cpp:62
SOCKET_IP_SEND_FLAGS
@ SOCKET_IP_SEND_FLAGS
Definition: IpCfg.hpp:19
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
Drv::IpSocket
Helper base-class for setting up Berkley sockets.
Definition: IpSocket.hpp:45
Drv
Definition: BlockDriverImpl.cpp:5
Drv::SOCK_FAILED_TO_ACCEPT
@ SOCK_FAILED_TO_ACCEPT
Failed to accept connection.
Definition: IpSocket.hpp:35
Drv::TcpServerSocket::TcpServerSocket
TcpServerSocket()
Constructor for client socket tcp implementation.
Definition: TcpServerSocket.cpp:41
Drv::SOCK_INVALID_IP_ADDRESS
@ SOCK_INVALID_IP_ADDRESS
Bad IP address supplied.
Definition: IpSocket.hpp:27
Drv::IpSocket::m_hostname
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
Definition: IpSocket.hpp:181
Drv::TcpServerSocket::sendProtocol
I32 sendProtocol(const U8 *const data, const U32 size)
Protocol specific implementation of send. Called directly with retry from send.
Definition: TcpServerSocket.cpp:104
Drv::SOCK_FAILED_TO_LISTEN
@ SOCK_FAILED_TO_LISTEN
Failed to listen on socket.
Definition: IpSocket.hpp:34
Drv::SOCK_FAILED_TO_GET_SOCKET
@ SOCK_FAILED_TO_GET_SOCKET
Socket open failed.
Definition: IpSocket.hpp:25
Drv::TcpServerSocket::startup
SocketIpStatus startup(void)
Opens the server socket and listens, does not block.
Definition: TcpServerSocket.cpp:43
Drv::TcpServerSocket::shutdown
void shutdown(void)
Shutdown client socket, and listening server socket.
Definition: TcpServerSocket.cpp:81
Drv::TcpServerSocket::recvProtocol
I32 recvProtocol(U8 *const data, const U32 size)
Protocol specific implementation of recv. Called directly with error handling from recv.
Definition: TcpServerSocket.cpp:108
Fw::Logger::logMsg
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
Drv::TcpServerSocket::openProtocol
SocketIpStatus openProtocol(NATIVE_INT_TYPE &fd)
Tcp specific implementation for opening a client socket connected to this server.
Definition: TcpServerSocket.cpp:88
Drv::SocketIpStatus
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:23
Drv::SOCK_FAILED_TO_BIND
@ SOCK_FAILED_TO_BIND
Failed to bind to socket.
Definition: IpSocket.hpp:33
TcpServerSocket.hpp
SOCKET_IP_RECV_FLAGS
@ SOCKET_IP_RECV_FLAGS
Definition: IpCfg.hpp:20
BasicTypes.hpp
Declares ISF basic types.
Drv::IpSocket::addressToIp4
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:79
Drv::IpSocket::m_fd
NATIVE_INT_TYPE m_fd
Definition: IpSocket.hpp:176
Drv::IpSocket::m_port
U16 m_port
IP address port used.
Definition: IpSocket.hpp:179
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
NULL
#define NULL
NULL.
Definition: BasicTypes.hpp:100
Drv::IpSocket::close
void close(void)
closes the socket
Definition: IpSocket.cpp:106
Logger.hpp