Ke Dong | cebe794 | 2012-01-19 15:40:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2005, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Ke Dong | 868574c | 2012-01-09 20:46:05 -0800 | [diff] [blame] | 28 | #ifndef BRUNO_BASE_SOCKETADDRESS_H_ |
| 29 | #define BRUNO_BASE_SOCKETADDRESS_H_ |
Ke Dong | cebe794 | 2012-01-19 15:40:01 -0800 | [diff] [blame] | 30 | |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | #include <iosfwd> |
Ke Dong | 868574c | 2012-01-09 20:46:05 -0800 | [diff] [blame] | 34 | #include "basictypes.h" |
Ke Dong | cebe794 | 2012-01-19 15:40:01 -0800 | [diff] [blame] | 35 | #undef SetPort |
| 36 | |
| 37 | struct sockaddr_in; |
| 38 | |
Ke Dong | 868574c | 2012-01-09 20:46:05 -0800 | [diff] [blame] | 39 | namespace bruno_base { |
Ke Dong | cebe794 | 2012-01-19 15:40:01 -0800 | [diff] [blame] | 40 | |
| 41 | // Records an IP address and port, which are 32 and 16 bit integers, |
| 42 | // respectively, both in <b>host byte-order</b>. |
| 43 | class SocketAddress { |
| 44 | public: |
| 45 | // Creates a nil address. |
| 46 | SocketAddress(); |
| 47 | |
| 48 | // Creates the address with the given host and port. If use_dns is true, |
| 49 | // the hostname will be immediately resolved to an IP (which may block for |
| 50 | // several seconds if DNS is not available). Alternately, set use_dns to |
| 51 | // false, and then call Resolve() to complete resolution later, or use |
| 52 | // SetResolvedIP to set the IP explictly. |
| 53 | SocketAddress(const std::string& hostname, int port); |
| 54 | |
| 55 | // Creates the address with the given IP and port. |
| 56 | SocketAddress(uint32 ip, int port); |
| 57 | |
| 58 | // Creates a copy of the given address. |
| 59 | SocketAddress(const SocketAddress& addr); |
| 60 | |
| 61 | // Resets to the nil address. |
| 62 | void Clear(); |
| 63 | |
| 64 | // Determines if this is a nil address (empty hostname, any IP, null port) |
| 65 | bool IsNil() const; |
| 66 | |
| 67 | // Returns true if ip and port are set. |
| 68 | bool IsComplete() const; |
| 69 | |
| 70 | // Replaces our address with the given one. |
| 71 | SocketAddress& operator=(const SocketAddress& addr); |
| 72 | |
| 73 | // Changes the IP of this address to the given one, and clears the hostname. |
| 74 | void SetIP(uint32 ip); |
| 75 | |
| 76 | // Changes the hostname of this address to the given one. |
| 77 | // Does not resolve the address; use Resolve to do so. |
| 78 | void SetIP(const std::string& hostname); |
| 79 | |
| 80 | // Sets the IP address while retaining the hostname. Useful for bypassing |
| 81 | // DNS for a pre-resolved IP. |
| 82 | void SetResolvedIP(uint32 ip); |
| 83 | |
| 84 | // Changes the port of this address to the given one. |
| 85 | void SetPort(int port); |
| 86 | |
| 87 | // Returns the hostname |
| 88 | const std::string& hostname() const { return hostname_; } |
| 89 | |
| 90 | // Returns the IP address. |
| 91 | uint32 ip() const; |
| 92 | |
| 93 | // Returns the port part of this address. |
| 94 | uint16 port() const; |
| 95 | |
| 96 | // Returns the IP address in dotted form. |
| 97 | std::string IPAsString() const; |
| 98 | |
| 99 | // Returns the port as a string |
| 100 | std::string PortAsString() const; |
| 101 | |
| 102 | // Returns hostname:port |
| 103 | std::string ToString() const; |
| 104 | |
| 105 | // Parses hostname:port |
| 106 | bool FromString(const std::string& str); |
| 107 | |
| 108 | friend std::ostream& operator<<(std::ostream& os, const SocketAddress& addr); |
| 109 | |
| 110 | // Determines whether this represents a missing / any IP address. Hostname |
| 111 | // and/or port may be set. |
| 112 | bool IsAnyIP() const; |
| 113 | inline bool IsAny() const { return IsAnyIP(); } // deprecated |
| 114 | |
| 115 | // Determines whether the IP address refers to a loopback address, i.e. within |
| 116 | // the range 127.0.0.0/8. |
| 117 | bool IsLoopbackIP() const; |
| 118 | |
| 119 | // Determines wither the IP address refers to any adapter on the local |
| 120 | // machine, including the loopback adapter. |
| 121 | bool IsLocalIP() const; |
| 122 | |
| 123 | // Determines whether the IP address is in one of the private ranges: |
| 124 | // 127.0.0.0/8 10.0.0.0/8 192.168.0.0/16 172.16.0.0/12. |
| 125 | bool IsPrivateIP() const; |
| 126 | |
| 127 | // Determines whether the hostname has been resolved to an IP. |
| 128 | bool IsUnresolvedIP() const; |
| 129 | inline bool IsUnresolved() const { return IsUnresolvedIP(); } // deprecated |
| 130 | |
| 131 | // Attempt to resolve a hostname to IP address. |
| 132 | // Returns false if resolution is required but failed, and sets error. |
| 133 | // 'force' will cause re-resolution of hostname. |
| 134 | bool ResolveIP(bool force = false, int* error = NULL); |
| 135 | |
| 136 | // Determines whether this address is identical to the given one. |
| 137 | bool operator ==(const SocketAddress& addr) const; |
| 138 | inline bool operator !=(const SocketAddress& addr) const { |
| 139 | return !this->operator ==(addr); |
| 140 | } |
| 141 | |
| 142 | // Compares based on IP and then port. |
| 143 | bool operator <(const SocketAddress& addr) const; |
| 144 | |
| 145 | // Determines whether this address has the same IP as the one given. |
| 146 | bool EqualIPs(const SocketAddress& addr) const; |
| 147 | |
| 148 | // Determines whether this address has the same port as the one given. |
| 149 | bool EqualPorts(const SocketAddress& addr) const; |
| 150 | |
| 151 | // Hashes this address into a small number. |
| 152 | size_t Hash() const; |
| 153 | |
| 154 | // Returns the size of this address when written. |
| 155 | size_t Size_() const; |
| 156 | |
| 157 | // Writes this address into the given buffer, according to RFC 3489. |
| 158 | bool Write_(char* buf, int len) const; |
| 159 | |
| 160 | // Reads this address from the given buffer, according to RFC 3489. |
| 161 | bool Read_(const char* buf, int len); |
| 162 | |
| 163 | // Write this address to a sockaddr_in. |
| 164 | void ToSockAddr(sockaddr_in* saddr) const; |
| 165 | |
| 166 | // Read this address from a sockaddr_in. |
| 167 | bool FromSockAddr(const sockaddr_in& saddr); |
| 168 | |
| 169 | // Converts the IP address given in compact form into dotted form. |
| 170 | static std::string IPToString(uint32 ip); |
| 171 | |
| 172 | // Converts the IP address given in dotted form into compact form. |
| 173 | // Only dotted names (A.B.C.D) are resolved. |
| 174 | static bool StringToIP(const std::string& str, uint32* ip); |
| 175 | static uint32 StringToIP(const std::string& str); // deprecated |
| 176 | |
| 177 | // Get local machine's hostname |
| 178 | static std::string GetHostname(); |
| 179 | |
| 180 | // Get a list of the local machine's ip addresses |
| 181 | static bool GetLocalIPs(std::vector<uint32>& ips); |
| 182 | |
| 183 | private: |
| 184 | std::string hostname_; |
| 185 | uint32 ip_; |
| 186 | uint16 port_; |
| 187 | }; |
| 188 | |
Ke Dong | 868574c | 2012-01-09 20:46:05 -0800 | [diff] [blame] | 189 | } // namespace bruno_base |
Ke Dong | cebe794 | 2012-01-19 15:40:01 -0800 | [diff] [blame] | 190 | |
Ke Dong | 868574c | 2012-01-09 20:46:05 -0800 | [diff] [blame] | 191 | #endif // BRUNO_BASE_SOCKETADDRESS_H_ |