completed partial IPv6 compatibility in DNS; does retrieve and Cache IPv6 IPs
[pingcheck] / src / host / pingprotocol.cpp
CommitLineData
3fd74a53
GMF
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on 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
28using namespace std;
29
a698d7c7 30static 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 39PingProtocol 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
64std::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}