]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/HostAddress_p.h
Updated file headers (filename, license, description, etc)
[bamtools.git] / src / api / internal / io / HostAddress_p.h
1 // ***************************************************************************
2 // HostAddress_p.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 10 November 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides a generic IP address container
8 // ***************************************************************************
9
10 #ifndef HOSTADDRESS_P_H
11 #define HOSTADDRESS_P_H
12
13 //  -------------
14 //  W A R N I N G
15 //  -------------
16 //
17 // This file is not part of the BamTools API.  It exists purely as an
18 // implementation detail. This header file may change from version to version
19 // without notice, or even be removed.
20 //
21 // We mean it.
22
23 #include "api/api_global.h"
24 #include <cstring>
25 #include <string>
26
27 namespace BamTools {
28 namespace Internal {
29
30 struct IPv6Address {
31
32     // ctor
33     inline IPv6Address(void) { memset(&data, 0, sizeof(uint8_t)*16); }
34
35     // data access (no bounds checking)
36     inline uint8_t& operator[](size_t index)       { return data[index]; }
37     inline uint8_t  operator[](size_t index) const { return data[index]; }
38
39     // data
40     uint8_t data[16];
41 };
42
43 class HostAddress {
44
45     // enums
46     public:
47         enum NetworkProtocol { UnknownNetworkProtocol = -1
48                              , IPv4Protocol = 0
49                              , IPv6Protocol
50                              };
51
52     // ctors & dtor
53     public:
54         HostAddress(void);
55         explicit HostAddress(const uint32_t ip4Address);
56         explicit HostAddress(const uint8_t* ip6Address);
57         explicit HostAddress(const IPv6Address& ip6Address);
58         explicit HostAddress(const std::string& address);
59         HostAddress(const HostAddress& other);
60         ~HostAddress(void);
61
62     // HostAddress interface
63     public:
64         void Clear(void);
65         bool HasIPAddress(void) const; // returns whether string address could be converted to IP address
66         bool IsNull(void) const;
67
68         uint32_t    GetIPv4Address(void) const;
69         IPv6Address GetIPv6Address(void) const;
70         std::string GetIPString(void) const;
71         HostAddress::NetworkProtocol GetProtocol(void) const;
72
73         void SetAddress(const uint32_t ip4Address);
74         void SetAddress(const uint8_t* ip6Address);
75         void SetAddress(const IPv6Address& ip6Address);
76         void SetAddress(const std::string& address);
77
78     // HostAddress comparison operators
79     public:
80         bool operator==(const HostAddress& other) const;
81         bool operator!=(const HostAddress& other) const { return !( operator==(other) ); }
82         bool operator<(const HostAddress& other) const;
83
84     // internal methods
85     private:
86         bool ParseAddress(void);
87
88     // data members
89     private:
90         HostAddress::NetworkProtocol m_protocol;
91         uint32_t    m_ip4Address;
92         IPv6Address m_ip6Address;
93         std::string m_ipString;
94         bool        m_hasIpAddress; // true until string passed in, then signifies whether string was an IP
95 };
96
97 } // namespace Internal
98 } // namespace BamTools
99
100 #endif // HOSTADDRESS_P_H