F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
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>
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
41namespace Drv {
42
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
53UdpSocket::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
62SocketIpStatus 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 FW_ASSERT(hostname != nullptr);
66 return this->IpSocket::configure(hostname, port, timeout_seconds, timeout_microseconds);
67}
68
69SocketIpStatus UdpSocket::configureRecv(const char* hostname, const U16 port) {
70 FW_ASSERT(this->isValidPort(port));
71 FW_ASSERT(hostname != nullptr);
72 this->m_recv_port = port;
73 (void) Fw::StringUtils::string_copy(this->m_recv_hostname, hostname, static_cast<FwSizeType>(SOCKET_MAX_HOSTNAME_SIZE));
74 return SOCK_SUCCESS;
75}
76
78 U16 port = this->m_recv_port;
79 return port;
80}
81
82
84 struct sockaddr_in address;
85 FW_ASSERT(fd != -1);
86
87 // Set up the address port and name
88 address.sin_family = AF_INET;
89 address.sin_port = htons(this->m_recv_port);
90 // OS specific settings
91#if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
92 address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
93#endif
94
95 // First IP address to socket sin_addr
96 if (IpSocket::addressToIp4(m_recv_hostname, &address.sin_addr) != SOCK_SUCCESS) {
98 };
99 // UDP (for receiving) requires bind to an address to the socket
100 if (::bind(fd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) {
101 return SOCK_FAILED_TO_BIND;
102 }
103
104 socklen_t size = sizeof(address);
105 if (::getsockname(fd, reinterpret_cast<struct sockaddr *>(&address), &size) == -1) {
107 }
108
109 FW_ASSERT(sizeof(this->m_state->m_addr_recv) == sizeof(address), sizeof(this->m_state->m_addr_recv), sizeof(address));
110 memcpy(&this->m_state->m_addr_recv, &address, sizeof(this->m_state->m_addr_recv));
111
112 return SOCK_SUCCESS;
113}
114
117 NATIVE_INT_TYPE socketFd = -1;
118 struct sockaddr_in address;
119
120 U16 port = this->m_port;
121
122 // Acquire a socket, or return error
123 if ((socketFd = ::socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
125 }
126
127 // May not be sending in all cases
128 if (port != 0) {
129 // Set up the address port and name
130 address.sin_family = AF_INET;
131 address.sin_port = htons(this->m_port);
132
133 // OS specific settings
134#if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
135 address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
136#endif
137
138 // First IP address to socket sin_addr
139 if ((status = IpSocket::addressToIp4(m_hostname, &(address.sin_addr))) != SOCK_SUCCESS) {
140 ::close(socketFd);
141 return status;
142 };
143
144 // Now apply timeouts
145 if ((status = IpSocket::setupTimeouts(socketFd)) != SOCK_SUCCESS) {
146 ::close(socketFd);
147 return status;
148 }
149 FW_ASSERT(sizeof(this->m_state->m_addr_send) == sizeof(address), sizeof(this->m_state->m_addr_send),
150 sizeof(address));
151 memcpy(&this->m_state->m_addr_send, &address, sizeof(this->m_state->m_addr_send));
152 }
153
154 // When we are setting up for receiving as well, then we must bind to a port
155 if ((status = this->bind(socketFd)) != SOCK_SUCCESS) {
156 ::close(socketFd);
157 return status; // Not closing FD as it is still a valid send FD
158 }
159 U16 recv_port = this->m_recv_port;
160 // Log message for UDP
161 if (port == 0) {
162 Fw::Logger::log("Setup to receive udp at %s:%hu\n", m_recv_hostname,
163 recv_port);
164 } else {
165 Fw::Logger::log("Setup to receive udp at %s:%hu and send to %s:%hu\n",
166 m_recv_hostname,
167 recv_port,
169 port);
170 }
171 FW_ASSERT(status == SOCK_SUCCESS, status);
172 socketDescriptor.fd = socketFd;
173 return status;
174}
175
176I32 UdpSocket::sendProtocol(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) {
177 FW_ASSERT(this->m_state->m_addr_send.sin_family != 0); // Make sure the address was previously setup
178 return static_cast<I32>(::sendto(socketDescriptor.fd, data, size, SOCKET_IP_SEND_FLAGS,
179 reinterpret_cast<struct sockaddr *>(&this->m_state->m_addr_send), sizeof(this->m_state->m_addr_send)));
180}
181
182I32 UdpSocket::recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) {
183 FW_ASSERT(this->m_state->m_addr_recv.sin_family != 0); // Make sure the address was previously setup
184 return static_cast<I32>(::recvfrom(socketDescriptor.fd, data, size, SOCKET_IP_RECV_FLAGS, nullptr, nullptr));
185}
186
187} // namespace Drv
#define FW_ASSERT(...)
Definition Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition BasicTypes.h:55
uint8_t U8
8-bit unsigned integer
Definition BasicTypes.h:30
int PlatformIntType
DefaultTypes.hpp provides fallback defaults for the platform types.
PlatformSizeType FwSizeType
Definition FpConfig.h:35
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:55
U16 m_port
IP address port used.
Definition IpSocket.hpp:210
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
Definition IpSocket.hpp:211
SocketIpStatus setupTimeouts(PlatformIntType socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition IpSocket.cpp:68
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
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:85
virtual bool isValidPort(U16 port)
Check if the given port is valid for the socket.
Definition IpSocket.cpp:64
void close(const SocketDescriptor &socketDescriptor)
closes the socket
Definition IpSocket.cpp:106
I32 sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size) override
Protocol specific implementation of send. Called directly with retry from send.
SocketIpStatus bind(const PlatformIntType fd)
bind the UDP to a port such that it can receive packets at the previously configured port
Definition UdpSocket.cpp:83
U16 getRecvPort()
get the port being received on
Definition UdpSocket.cpp:77
UdpSocket()
Constructor for client socket udp implementation.
Definition UdpSocket.cpp:53
SocketIpStatus openProtocol(SocketDescriptor &socketDescriptor) override
udp specific implementation for opening a socket.
SocketIpStatus configureRecv(const char *hostname, const U16 port)
configure the udp socket for incoming transmissions
Definition UdpSocket.cpp:69
I32 recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const U32 size) override
Protocol specific implementation of recv. Called directly with error handling from recv.
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
virtual ~UdpSocket()
to cleanup state created at instantiation
Definition UdpSocket.cpp:57
static void log(const char *format,...)
log a formated string with supplied arguments
Definition Logger.cpp:21
SocketIpStatus
Status enumeration for socket return values.
Definition IpSocket.hpp:29
@ SOCK_INVALID_IP_ADDRESS
Bad IP address supplied.
Definition IpSocket.hpp:33
@ SOCK_SUCCESS
Socket operation successful.
Definition IpSocket.hpp:30
@ SOCK_FAILED_TO_BIND
Failed to bind to socket.
Definition IpSocket.hpp:39
@ SOCK_FAILED_TO_GET_SOCKET
Socket open failed.
Definition IpSocket.hpp:31
@ SOCK_FAILED_TO_READ_BACK_PORT
Failed to read back port from connection.
Definition IpSocket.hpp:44
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
PlatformIntType fd
Used for all sockets to track the communication file descriptor.
Definition IpSocket.hpp:22
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