| 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 | #ifdef TGT_OS_TYPE_VXWORKS | ||
| 16 | #include <socket.h> | ||
| 17 | #elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN | ||
| 18 | #include <sys/socket.h> | ||
| 19 | #else | ||
| 20 | #error OS not supported for IP Socket Communications | ||
| 21 | #endif | ||
| 22 | |||
| 23 | enum IpCfg { | ||
| 24 | SOCKET_SEND_TIMEOUT_SECONDS = 1, // Seconds component of timeout to an individual send | ||
| 25 | SOCKET_SEND_TIMEOUT_MICROSECONDS = 0, // Milliseconds component of timeout to an individual send | ||
| 26 | SOCKET_IP_SEND_FLAGS = 0, // send, sendto FLAGS argument | ||
| 27 | SOCKET_IP_RECV_FLAGS = 0, // recv FLAGS argument | ||
| 28 | SOCKET_MAX_ITERATIONS = 0xFFFF, // Maximum send/recv attempts before an error is returned | ||
| 29 | // Maximum stored IPv4 address string. Sized to fit a full dotted-quad ("255.255.255.255") | ||
| 30 | // plus NUL terminator, with extra slack. The driver does NOT perform DNS resolution; the | ||
| 31 | // configure() call requires an IPv4 address in the form x.x.x.x. The buffer is kept at 256 | ||
| 32 | // bytes (rather than INET_ADDRSTRLEN==16) for backwards-compatibility with downstream | ||
| 33 | // projects that override this macro for their own purposes. | ||
| 34 | SOCKET_MAX_IPV4_ADDRESS_SIZE = 256, | ||
| 35 | // DEPRECATED: legacy alias retained for one minor release. New code should use | ||
| 36 | // SOCKET_MAX_IPV4_ADDRESS_SIZE. Removing this alias is a breaking change and must be | ||
| 37 | // accompanied by a major-version bump per the F' versioning policy. | ||
| 38 | SOCKET_MAX_HOSTNAME_SIZE = SOCKET_MAX_IPV4_ADDRESS_SIZE | ||
| 39 | }; | ||
| 40 | static const Fw::TimeInterval SOCKET_RETRY_INTERVAL = Fw::TimeInterval(1, 0); | ||
| 41 | |||
| 42 | // Value type enumeration | ||
| 43 | enum SocketOptionValueType { | ||
| 44 | SOCK_OPT_INT, // Integer value | ||
| 45 | SOCK_OPT_SIZE_T, // size_t value | ||
| 46 | }; | ||
| 47 | |||
| 48 | // Socket option structure with flexible value types | ||
| 49 | struct IpSocketOptions { | ||
| 50 | int option; // Socket option name | ||
| 51 | int level; // Socket level | ||
| 52 | SocketOptionValueType type; // Type of value stored | ||
| 53 | |||
| 54 | union { | ||
| 55 | int intVal; // Integer value | ||
| 56 | size_t sizeVal; // Size_t value | ||
| 57 | } value; | ||
| 58 | }; | ||
| 59 | |||
| 60 | // Helper functions to create different types of socket options | ||
| 61 | 6 | inline IpSocketOptions makeIntOption(int opt, int level, int val) { | |
| 62 | IpSocketOptions option; | ||
| 63 | 6 | option.option = opt; | |
| 64 | 6 | option.level = level; | |
| 65 | 6 | option.type = SOCK_OPT_INT; | |
| 66 | 6 | option.value.intVal = val; | |
| 67 | 6 | return option; | |
| 68 | } | ||
| 69 | |||
| 70 | inline IpSocketOptions makeSizeOption(int opt, int level, size_t val) { | ||
| 71 | IpSocketOptions option; | ||
| 72 | option.option = opt; | ||
| 73 | option.level = level; | ||
| 74 | option.type = SOCK_OPT_SIZE_T; | ||
| 75 | option.value.sizeVal = val; | ||
| 76 | return option; | ||
| 77 | } | ||
| 78 | |||
| 79 | // Array of socket options to explicitly set using setsockopt | ||
| 80 | // Use the dedicated helper functions to create options of the correct type | ||
| 81 | // makeIntOption -> for int values | ||
| 82 | // makeSizeOption -> for size_t values | ||
| 83 | // NOTE: Socket options should be chosen based on project needs and with | ||
| 84 | // consideration of their implications with regard to security. | ||
| 85 | // For example, if enabling S_REUSEADDR and there is a hostile actor on the same | ||
| 86 | // machine, they could potentially bind to the same port and intercept messages. | ||
| 87 | // Projects should evaluate their threat model and choose options accordingly. | ||
| 88 | static const IpSocketOptions IP_SOCKET_OPTIONS[] = { | ||
| 89 | // Default the TcpServer to reuse the same port, which prevents TIME_WAIT delays when rerunning | ||
| 90 | // flight software. Set to zero to turn off. | ||
| 91 | makeIntOption(SO_REUSEADDR, SOL_SOCKET, 1), | ||
| 92 | // Add other socket options as needed, and expand above helper functions | ||
| 93 | // if other types are needed | ||
| 94 | }; | ||
| 95 | |||
| 96 | #endif // REF_IPCFG_HPP | ||
| 97 |