| 1 | /* |
| 2 | The software in this package is distributed under the GNU General |
| 3 | Public License version 2 (with a special exception described below). |
| 4 | |
| 5 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 6 | in the file COPYING.GPL. |
| 7 | |
| 8 | As a special exception, if other files instantiate templates or use macros |
| 9 | or inline functions from this file, or you compile this file and link it |
| 10 | with other works to produce a work based on this file, this file |
| 11 | does not by itself cause the resulting work to be covered |
| 12 | by the GNU General Public License. |
| 13 | |
| 14 | However the source code for this file must still be made available |
| 15 | in accordance with section (3) of the GNU General Public License. |
| 16 | |
| 17 | This exception does not invalidate any other reasons why a work based |
| 18 | on this file might be covered by the GNU General Public License. |
| 19 | */ |
| 20 | |
| 21 | #include "host/pingprotocol.h" |
| 22 | |
| 23 | #include <algorithm> |
| 24 | #include <map> |
| 25 | |
| 26 | #include "boost_assert_handler.h" |
| 27 | |
| 28 | using namespace std; |
| 29 | |
| 30 | static map<string, PingProtocol> protocol_string_map; |
| 31 | |
| 32 | /** |
| 33 | * @brief Transform the @a protocol_string into a @c PingProtocol. |
| 34 | * |
| 35 | * @param protocol_string The string to be parsed. |
| 36 | * |
| 37 | * @return The @c PingProtocol corresponding to the @a protocol_string. |
| 38 | */ |
| 39 | PingProtocol get_ping_protocol_from_string( const string & protocol_string ) |
| 40 | { |
| 41 | BOOST_ASSERT( !protocol_string.empty() ); |
| 42 | |
| 43 | // convert to uppercase to allow the protocol to be case insensitive |
| 44 | string protocol_uppercase_string( protocol_string ); |
| 45 | transform( protocol_string.begin(), protocol_string.end(), |
| 46 | protocol_uppercase_string.begin(), |
| 47 | ::toupper ); //lint !e534 |
| 48 | |
| 49 | // TODO move to an init method |
| 50 | protocol_string_map[ "ICMP" ] = PingProtocol_ICMP; |
| 51 | protocol_string_map[ "ICMPV4" ] = PingProtocol_ICMP; |
| 52 | protocol_string_map[ "ICMPV6" ] = PingProtocol_ICMPv6; |
| 53 | protocol_string_map[ "TCP" ] = PingProtocol_TCP; |
| 54 | protocol_string_map[ "TCP_IPV4" ] = PingProtocol_TCP; |
| 55 | protocol_string_map[ "TCP_IPV6" ] = PingProtocol_TCP_IPv6; |
| 56 | protocol_string_map[ "TCPV4" ] = PingProtocol_TCP; |
| 57 | protocol_string_map[ "TCPV6" ] = PingProtocol_TCP_IPv6; |
| 58 | |
| 59 | PingProtocol protocol = protocol_string_map[ protocol_uppercase_string ]; |
| 60 | |
| 61 | return protocol; |
| 62 | } |
| 63 | |
| 64 | std::string ping_protocol_to_string( const PingProtocol &protocol) |
| 65 | { |
| 66 | switch(protocol) |
| 67 | { |
| 68 | case PingProtocol_ICMP: return "ICMPv4"; break; |
| 69 | case PingProtocol_ICMPv6: return "ICMPv6"; break; |
| 70 | case PingProtocol_TCP: return "TCPv4"; break; |
| 71 | case PingProtocol_TCP_IPv6: return "TCPv6"; break; |
| 72 | default: |
| 73 | BOOST_ASSERT( |
| 74 | !"unexpected PingProtocol in ping_protocol_to_string!" ); |
| 75 | break; |
| 76 | } |
| 77 | } |