F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
TcpClientSocket.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title TcpClientSocket.cpp
3 // \author mstarch
4 // \brief cpp file for TcpClientSocket 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 
14 #include <Fw/FPrimeBasicTypes.hpp>
15 #include <Fw/Logger/Logger.hpp>
16 #include <Fw/Types/Assert.hpp>
17 
18 #ifdef TGT_OS_TYPE_VXWORKS
19 #include <errnoLib.h>
20 #include <fioLib.h>
21 #include <hostLib.h>
22 #include <inetLib.h>
23 #include <ioLib.h>
24 #include <sockLib.h>
25 #include <socket.h>
26 #include <sysLib.h>
27 #include <taskLib.h>
28 #include <vxWorks.h>
29 #include <cstring>
30 #elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
31 #include <arpa/inet.h>
32 #include <sys/socket.h>
33 #include <unistd.h>
34 #else
35 #error OS not supported for IP Socket Communications
36 #endif
37 
38 #include <cstdio>
39 #include <cstring>
40 
41 namespace Drv {
42 
44 
45 bool TcpClientSocket::isValidPort(U16 port) const {
46  return port != 0;
47 }
48 
50  struct sockaddr_in address;
51 
52  // Acquire a socket, or return error
53  int socketFd = ::socket(AF_INET, SOCK_STREAM, 0);
54  if (socketFd == -1) {
56  }
57  // Set up the address port and name
58  address.sin_family = AF_INET;
59  address.sin_port = htons(this->m_port);
60 
61  // OS specific settings
62 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
63  address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
64 #endif
65 
66  // Convert the configured IPv4 address (dotted-quad) to a network-order in_addr.
67 
68  const SocketIpStatus addressStatus = IpSocket::addressToIp4(this->m_ipv4_address, &(address.sin_addr));
69  if (addressStatus != SOCK_SUCCESS) {
70  (void)::close(socketFd);
72  };
73 
74  if (IpSocket::setupSocketOptions(socketFd) != SOCK_SUCCESS) {
75  (void)::close(socketFd);
77  }
78 
79  // Now apply timeouts
80  if (IpSocket::setupTimeouts(socketFd) != SOCK_SUCCESS) {
81  (void)::close(socketFd);
83  }
84 
85  // TCP requires connect to the socket to allow for communication
86  if (::connect(socketFd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) {
87  (void)::close(socketFd);
89  }
90  socketDescriptor.fd = socketFd;
91  Fw::Logger::log("Connected to %s:%hu as a tcp client\n", this->m_ipv4_address, this->m_port);
92  return SOCK_SUCCESS;
93 }
94 
96  const U8* const data,
97  const FwSizeType size) {
98  return static_cast<FwSignedSizeType>(
99  ::send(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_SEND_FLAGS));
100 }
101 
103  U8* const data,
104  const FwSizeType size) {
105  return static_cast<FwSignedSizeType>(
106  ::recv(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_RECV_FLAGS));
107 }
108 
109 } // namespace Drv
SocketIpStatus openProtocol(SocketDescriptor &socketDescriptor) override
Tcp specific implementation for opening a client socket.
Socket open failed.
Definition: IpSocket.hpp:31
Failed to connect socket.
Definition: IpSocket.hpp:34
Failed to configure socket.
Definition: IpSocket.hpp:35
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:182
SocketIpStatus setupTimeouts(int socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition: IpSocket.cpp:75
static void log(const char *format,...)
log a formated string with supplied arguments
Definition: Logger.cpp:21
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:92
int fd
Used for all sockets to track the communication file descriptor.
Definition: IpSocket.hpp:22
PlatformSignedSizeType FwSignedSizeType
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:147
Bad IP address supplied.
Definition: IpSocket.hpp:33
U16 m_port
IP address port used.
Definition: IpSocket.hpp:250
TcpClientSocket()
Constructor for client socket tcp implementation.
Socket operation successful.
Definition: IpSocket.hpp:30
FwSignedSizeType sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const FwSizeType size) override
Protocol specific implementation of send. Called directly with retry from send.
SocketIpStatus setupSocketOptions(int socketFd)
setup the socket options of the input socket as defined in IpCfg.hpp
Definition: IpSocket.cpp:234
FwSignedSizeType recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const FwSizeType size) override
Protocol specific implementation of recv. Called directly with error handling from recv...
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:54
char m_ipv4_address[SOCKET_MAX_IPV4_ADDRESS_SIZE]
IPv4 address (dotted-quad "x.x.x.x")
Definition: IpSocket.hpp:251
void close(const SocketDescriptor &socketDescriptor)
closes the socket
Definition: IpSocket.cpp:120
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:29
bool isValidPort(U16 port) const override
Check if the given port is valid for the socket.
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:57