[7899] | 1 | /** |
---|
[9304] | 2 | * @file ip.cc |
---|
| 3 | * @brief A IP class, that handles all about time. |
---|
[7899] | 4 | * |
---|
| 5 | * code taken from audiere. |
---|
| 6 | */ |
---|
| 7 | |
---|
[9304] | 8 | #include "ip.h" |
---|
[9305] | 9 | #include "multi_type.h" |
---|
[9306] | 10 | #include "substring.h" |
---|
[7899] | 11 | |
---|
[9307] | 12 | /** |
---|
| 13 | * @brief default constructor |
---|
| 14 | */ |
---|
[9304] | 15 | IP::IP() |
---|
[9306] | 16 | { |
---|
| 17 | this->_ip = 0; |
---|
| 18 | this->_port = 0; |
---|
| 19 | } |
---|
[7899] | 20 | |
---|
| 21 | |
---|
[9307] | 22 | /** |
---|
| 23 | * @brief constructor from ip and port |
---|
| 24 | * @param ip the IP |
---|
| 25 | * @param port the Port |
---|
| 26 | * @return self |
---|
| 27 | */ |
---|
[9306] | 28 | IP::IP(int ip, int port) |
---|
[9304] | 29 | { |
---|
| 30 | this->_ip = ip; |
---|
[9306] | 31 | this->_port = port; |
---|
[7909] | 32 | } |
---|
| 33 | |
---|
[9306] | 34 | |
---|
| 35 | |
---|
[9307] | 36 | /** |
---|
| 37 | * @brief constructor from a String |
---|
| 38 | * @param ip the IP as a String. |
---|
| 39 | * @return self |
---|
| 40 | */ |
---|
[9304] | 41 | IP::IP(const std::string& ip) |
---|
| 42 | { |
---|
[9306] | 43 | *this = IP::stringToIP(ip); |
---|
[9304] | 44 | } |
---|
[7899] | 45 | |
---|
[7909] | 46 | |
---|
[9307] | 47 | /** |
---|
| 48 | * @brief constructor from an SDL net structure IPaddress |
---|
| 49 | * @param ip the ip. |
---|
| 50 | * @return self |
---|
| 51 | */ |
---|
[9306] | 52 | IP::IP(const IPaddress& ip) |
---|
| 53 | { |
---|
| 54 | this->_ip = ip.host; |
---|
| 55 | this->_port = ip.port; |
---|
| 56 | } |
---|
[9305] | 57 | |
---|
| 58 | |
---|
[9307] | 59 | /** |
---|
| 60 | * @brief copy constructor. |
---|
| 61 | * @param ip the IP to copy. |
---|
| 62 | * @return self |
---|
| 63 | */ |
---|
[9306] | 64 | IP::IP(const IP& ip) |
---|
[9304] | 65 | { |
---|
[9306] | 66 | this->_ip = ip.ip(); |
---|
| 67 | this->_port = ip.port(); |
---|
| 68 | } |
---|
[7899] | 69 | |
---|
[9306] | 70 | |
---|
| 71 | |
---|
[9307] | 72 | /** |
---|
| 73 | * @brief copy operator |
---|
| 74 | * @param ip the IP to copy. |
---|
| 75 | * @return self. |
---|
| 76 | */ |
---|
[9306] | 77 | const IP& IP::operator=(const IP& ip) |
---|
| 78 | { |
---|
| 79 | this->_ip = ip.ip(); |
---|
| 80 | this->_port = ip.port(); |
---|
| 81 | return *this; |
---|
[9305] | 82 | } |
---|
[9304] | 83 | |
---|
[9306] | 84 | |
---|
| 85 | |
---|
[9307] | 86 | /** |
---|
| 87 | * @brief comparison operator |
---|
| 88 | * @param ip the IP to compare |
---|
| 89 | * @return true if ip _and_ port match. |
---|
| 90 | */ |
---|
[9306] | 91 | bool IP::operator==(const IP& ip) |
---|
| 92 | { |
---|
| 93 | return (this->_ip == ip.ip() && this->_port == ip.port()); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | /** |
---|
[9307] | 98 | * @brief converts a String into an IP Object. |
---|
| 99 | * @param ip The string holding an IP. |
---|
| 100 | * @return a constructed IP. |
---|
[9306] | 101 | */ |
---|
| 102 | IP IP::stringToIP(const std::string& ip) |
---|
| 103 | { |
---|
| 104 | IPaddress ipaddr; |
---|
| 105 | |
---|
| 106 | SDLNet_ResolveHost(&ipaddr, NULL, 2000); |
---|
| 107 | |
---|
| 108 | return IP(ipaddr); |
---|
| 109 | |
---|
| 110 | /* |
---|
| 111 | SubString addr(ip, '.'); |
---|
| 112 | if(ip.size() != 4 ) |
---|
| 113 | return -1; |
---|
| 114 | |
---|
| 115 | MultiType part0(ip[0]); |
---|
| 116 | MultiType part1(ip[1]); |
---|
| 117 | MultiType part2(ip[2]); |
---|
| 118 | MultiType part3(ip[3]); |
---|
| 119 | */ |
---|
| 120 | } |
---|
| 121 | |
---|
[9307] | 122 | |
---|
| 123 | /** |
---|
| 124 | * @brief if you want to have a specific part of an IP |
---|
| 125 | * @param part the n'th part of the IP addr (splitted by '.'). |
---|
| 126 | * @return the amount held in the designated part. |
---|
| 127 | */ |
---|
[9306] | 128 | int IP::ipPart(unsigned int part) const |
---|
| 129 | { |
---|
| 130 | switch (part) |
---|
| 131 | { |
---|
| 132 | case 0: |
---|
| 133 | return (_ip & 0xFF000000) >> 24; |
---|
| 134 | case 1: |
---|
| 135 | return (_ip & 0x00FF0000) >> 16; |
---|
| 136 | case 2: |
---|
| 137 | return (_ip & 0x0000FF00) >> 8; |
---|
| 138 | case 3: |
---|
| 139 | return (_ip & 0x000000FF); |
---|
| 140 | default: |
---|
| 141 | return -1; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
[9307] | 147 | /** |
---|
| 148 | * @return the Ip as a string. |
---|
| 149 | */ |
---|
[9306] | 150 | std::string IP::ipString() const |
---|
| 151 | { |
---|
| 152 | return IP::ipToString(this->_ip); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | |
---|
[9307] | 156 | /** |
---|
| 157 | * @brief converts an IPaddress struct into a String. |
---|
| 158 | * @param ipaddr the IP address as a SDL_net struct. |
---|
| 159 | * @return the string retrieved from the IP. |
---|
| 160 | */ |
---|
[9305] | 161 | std::string IP::ipToString(const IPaddress& ipaddr) |
---|
| 162 | { |
---|
| 163 | int ip = SDLNet_Read32 (ipaddr.host); |
---|
[9307] | 164 | return ipToString(ip, ipaddr.port); |
---|
[7899] | 165 | } |
---|
[9304] | 166 | |
---|
[9305] | 167 | |
---|
[9306] | 168 | |
---|
| 169 | |
---|
| 170 | |
---|
[9307] | 171 | /** |
---|
| 172 | * converts a IP into a String (without port). |
---|
| 173 | * @param ip the IP to put into the string. |
---|
| 174 | * @param port -1 if not wanted |
---|
| 175 | * @return the string of the ip. |
---|
| 176 | */ |
---|
| 177 | std::string IP::ipToString(int ip, int port) |
---|
[9304] | 178 | { |
---|
[9306] | 179 | MultiType part0((int) (ip & 0xFF000000) >> 24); |
---|
| 180 | MultiType part1((int) (ip & 0x00FF0000) >> 16); |
---|
| 181 | MultiType part2((int) (ip & 0x0000FF00) >> 8); |
---|
| 182 | MultiType part3((int) (ip & 0x000000FF) ); |
---|
[9304] | 183 | |
---|
[9307] | 184 | std::string addr = part0.getString() + "." + part1.getString() + "." + |
---|
| 185 | part2.getString() + "." + part3.getString(); |
---|
[9305] | 186 | |
---|
[9307] | 187 | if (port != -1) |
---|
| 188 | addr += MultiType(port).getString(); |
---|
| 189 | return addr; |
---|
[9304] | 190 | } |
---|
| 191 | |
---|