Commit | Line | Data |
---|---|---|
3fd74a53 GMF |
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 | ||
4faba029 | 23 | #include <algorithm> |
3fd74a53 GMF |
24 | #include <map> |
25 | ||
780b0bca | 26 | #include "boost_assert_handler.h" |
3fd74a53 GMF |
27 | |
28 | using namespace std; | |
29 | ||
a698d7c7 | 30 | static map<string, PingProtocol> protocol_string_map; |
3fd74a53 | 31 | |
72e96142 GMF |
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 | */ | |
a8a2cefc | 39 | PingProtocol get_ping_protocol_from_string( const string & protocol_string ) |
3fd74a53 GMF |
40 | { |
41 | BOOST_ASSERT( !protocol_string.empty() ); | |
42 | ||
4faba029 GMF |
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(), | |
d4828254 | 47 | ::toupper ); //lint !e534 |
4faba029 | 48 | |
3fd74a53 GMF |
49 | // TODO move to an init method |
50 | protocol_string_map[ "ICMP" ] = PingProtocol_ICMP; | |
0a670586 | 51 | protocol_string_map[ "ICMPV4" ] = PingProtocol_ICMP; |
8a498ef7 | 52 | protocol_string_map[ "ICMPV6" ] = PingProtocol_ICMPv6; |
3fd74a53 | 53 | protocol_string_map[ "TCP" ] = PingProtocol_TCP; |
0a670586 | 54 | protocol_string_map[ "TCP_IPV4" ] = PingProtocol_TCP; |
8a498ef7 | 55 | protocol_string_map[ "TCP_IPV6" ] = PingProtocol_TCP_IPv6; |
0a670586 GMF |
56 | protocol_string_map[ "TCPV4" ] = PingProtocol_TCP; |
57 | protocol_string_map[ "TCPV6" ] = PingProtocol_TCP_IPv6; | |
3fd74a53 | 58 | |
6f799c07 GMF |
59 | PingProtocol protocol = protocol_string_map[ protocol_uppercase_string ]; |
60 | ||
61 | return protocol; | |
3fd74a53 | 62 | } |
8f00b3df CH |
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 | } |