| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title IpCfg.hpp | ||
| 3 | // \author mstarch | ||
| 4 | // \brief hpp file for SocketIpDriver component implementation class | ||
| 5 | // | ||
| 6 | // \copyright | ||
| 7 | // Copyright 2009-2015, by the California Institute of Technology. | ||
| 8 | // ALL RIGHTS RESERVED. United States Government Sponsorship | ||
| 9 | // acknowledged. | ||
| 10 | // | ||
| 11 | // ====================================================================== | ||
| 12 | #include <Fw/Time/TimeInterval.hpp> | ||
| 13 | #ifndef REF_IPCFG_HPP | ||
| 14 | #define REF_IPCFG_HPP | ||
| 15 | // Platform socket headers used by Drv/Ip. Projects targeting other platforms override this file. | ||
| 16 | #ifdef TGT_OS_TYPE_VXWORKS | ||
| 17 | #include <errnoLib.h> | ||
| 18 | #include <fioLib.h> | ||
| 19 | #include <hostLib.h> | ||
| 20 | #include <inetLib.h> | ||
| 21 | #include <ioLib.h> | ||
| 22 | #include <sockLib.h> | ||
| 23 | #include <socket.h> | ||
| 24 | #include <sysLib.h> | ||
| 25 | #include <taskLib.h> | ||
| 26 | #include <vxWorks.h> | ||
| 27 | // Undefine VxWorks macros (from inetLib.h) that collide with F Prime member variables | ||
| 28 | #undef m_type | ||
| 29 | #undef m_data | ||
| 30 | #undef m_len | ||
| 31 | #elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN | ||
| 32 | #include <arpa/inet.h> | ||
| 33 | #include <sys/socket.h> | ||
| 34 | #include <sys/time.h> | ||
| 35 | #include <unistd.h> | ||
| 36 | #else | ||
| 37 | #error OS not supported for IP Socket Communications | ||
| 38 | #endif | ||
| 39 | |||
| 40 | enum IpCfg { | ||
| 41 | SOCKET_SEND_TIMEOUT_SECONDS = 1, // Seconds component of timeout to an individual send | ||
| 42 | SOCKET_SEND_TIMEOUT_MICROSECONDS = 0, // Microseconds component of timeout to an individual send | ||
| 43 | SOCKET_IP_SEND_FLAGS = 0, // send, sendto FLAGS argument | ||
| 44 | SOCKET_IP_RECV_FLAGS = 0, // recv FLAGS argument | ||
| 45 | SOCKET_MAX_ITERATIONS = 0xFFFF, // Maximum send/recv attempts before an error is returned | ||
| 46 | // Maximum stored IPv4 address string. Sized to fit a full dotted-quad ("255.255.255.255") | ||
| 47 | // plus NUL terminator, with extra slack. The driver does NOT perform DNS resolution; the | ||
| 48 | // configure() call requires an IPv4 address in the form x.x.x.x. The buffer is kept at 256 | ||
| 49 | // bytes (rather than INET_ADDRSTRLEN==16) for backwards-compatibility with downstream | ||
| 50 | // projects that override this macro for their own purposes. | ||
| 51 | SOCKET_MAX_IPV4_ADDRESS_SIZE = 256, | ||
| 52 | // DEPRECATED: legacy alias retained for one minor release. New code should use | ||
| 53 | // SOCKET_MAX_IPV4_ADDRESS_SIZE. Removing this alias is a breaking change and must be | ||
| 54 | // accompanied by a major-version bump per the F' versioning policy. | ||
| 55 | SOCKET_MAX_HOSTNAME_SIZE = SOCKET_MAX_IPV4_ADDRESS_SIZE | ||
| 56 | }; | ||
| 57 | static const Fw::TimeInterval SOCKET_RETRY_INTERVAL = Fw::TimeInterval(1, 0); | ||
| 58 | |||
| 59 | // Value type enumeration | ||
| 60 | enum SocketOptionValueType { | ||
| 61 | SOCK_OPT_INT, // Integer value | ||
| 62 | SOCK_OPT_SIZE_T, // size_t value | ||
| 63 | }; | ||
| 64 | |||
| 65 | // Socket option structure with flexible value types | ||
| 66 | struct IpSocketOptions { | ||
| 67 | int option; // Socket option name | ||
| 68 | int level; // Socket level | ||
| 69 | SocketOptionValueType type; // Type of value stored | ||
| 70 | |||
| 71 | union { | ||
| 72 | int intVal; // Integer value | ||
| 73 | size_t sizeVal; // Size_t value | ||
| 74 | } value; | ||
| 75 | }; | ||
| 76 | |||
| 77 | // Helper functions to create different types of socket options | ||
| 78 | 6 | inline IpSocketOptions makeIntOption(int opt, int level, int val) { | |
| 79 | IpSocketOptions option; | ||
| 80 | 6 | option.option = opt; | |
| 81 | 6 | option.level = level; | |
| 82 | 6 | option.type = SOCK_OPT_INT; | |
| 83 | 6 | option.value.intVal = val; | |
| 84 | 6 | return option; | |
| 85 | } | ||
| 86 | |||
| 87 | inline IpSocketOptions makeSizeOption(int opt, int level, size_t val) { | ||
| 88 | IpSocketOptions option; | ||
| 89 | option.option = opt; | ||
| 90 | option.level = level; | ||
| 91 | option.type = SOCK_OPT_SIZE_T; | ||
| 92 | option.value.sizeVal = val; | ||
| 93 | return option; | ||
| 94 | } | ||
| 95 | |||
| 96 | // Array of socket options to explicitly set using setsockopt | ||
| 97 | // Use the dedicated helper functions to create options of the correct type | ||
| 98 | // makeIntOption -> for int values | ||
| 99 | // makeSizeOption -> for size_t values | ||
| 100 | // NOTE: Socket options should be chosen based on project needs and with | ||
| 101 | // consideration of their implications with regard to security. | ||
| 102 | // For example, if enabling S_REUSEADDR and there is a hostile actor on the same | ||
| 103 | // machine, they could potentially bind to the same port and intercept messages. | ||
| 104 | // Projects should evaluate their threat model and choose options accordingly. | ||
| 105 | static const IpSocketOptions IP_SOCKET_OPTIONS[] = { | ||
| 106 | // Default the TcpServer to reuse the same port, which prevents TIME_WAIT delays when rerunning | ||
| 107 | // flight software. Set to zero to turn off. | ||
| 108 | makeIntOption(SO_REUSEADDR, SOL_SOCKET, 1), | ||
| 109 | // Add other socket options as needed, and expand above helper functions | ||
| 110 | // if other types are needed | ||
| 111 | }; | ||
| 112 | |||
| 113 | #endif // REF_IPCFG_HPP | ||
| 114 |