| 1 | /** |
|---|
| 2 | ** \file Ipv6Address.cpp |
|---|
| 3 | ** \date 2006-09-21 |
|---|
| 4 | ** \author grymse@alhem.net |
|---|
| 5 | **/ |
|---|
| 6 | /* |
|---|
| 7 | Copyright (C) 2007 Anders Hedstrom |
|---|
| 8 | |
|---|
| 9 | This program is free software; you can redistribute it and/or |
|---|
| 10 | modify it under the terms of the GNU General Public License |
|---|
| 11 | as published by the Free Software Foundation; either version 2 |
|---|
| 12 | of the License, or (at your option) any later version. |
|---|
| 13 | |
|---|
| 14 | This program is distributed in the hope that it will be useful, |
|---|
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | GNU General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU General Public License |
|---|
| 20 | along with this program; if not, write to the Free Software |
|---|
| 21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | #include "Ipv6Address.h" |
|---|
| 24 | #ifdef ENABLE_IPV6 |
|---|
| 25 | |
|---|
| 26 | #include "Utility.h" |
|---|
| 27 | #include "Parse.h" |
|---|
| 28 | #ifndef _WIN32 |
|---|
| 29 | #include <netdb.h> |
|---|
| 30 | #endif |
|---|
| 31 | #ifdef IPPROTO_IPV6 |
|---|
| 32 | |
|---|
| 33 | #ifdef SOCKETS_NAMESPACE |
|---|
| 34 | namespace SOCKETS_NAMESPACE { |
|---|
| 35 | #endif |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | Ipv6Address::Ipv6Address(port_t port) : m_valid(true) |
|---|
| 39 | { |
|---|
| 40 | memset(&m_addr, 0, sizeof(m_addr)); |
|---|
| 41 | m_addr.sin6_family = AF_INET6; |
|---|
| 42 | m_addr.sin6_port = htons( port ); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | Ipv6Address::Ipv6Address(struct in6_addr& a,port_t port) : m_valid(true) |
|---|
| 47 | { |
|---|
| 48 | memset(&m_addr, 0, sizeof(m_addr)); |
|---|
| 49 | m_addr.sin6_family = AF_INET6; |
|---|
| 50 | m_addr.sin6_port = htons( port ); |
|---|
| 51 | m_addr.sin6_addr = a; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | Ipv6Address::Ipv6Address(const std::string& host,port_t port) : m_valid(false) |
|---|
| 56 | { |
|---|
| 57 | memset(&m_addr, 0, sizeof(m_addr)); |
|---|
| 58 | m_addr.sin6_family = AF_INET6; |
|---|
| 59 | m_addr.sin6_port = htons( port ); |
|---|
| 60 | { |
|---|
| 61 | struct in6_addr a; |
|---|
| 62 | if (Utility::u2ip(host, a)) |
|---|
| 63 | { |
|---|
| 64 | m_addr.sin6_addr = a; |
|---|
| 65 | m_valid = true; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | Ipv6Address::Ipv6Address(struct sockaddr_in6& sa) |
|---|
| 72 | { |
|---|
| 73 | m_addr = sa; |
|---|
| 74 | m_valid = sa.sin6_family == AF_INET6; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | Ipv6Address::~Ipv6Address() |
|---|
| 79 | { |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | Ipv6Address::operator struct sockaddr *() |
|---|
| 84 | { |
|---|
| 85 | return (struct sockaddr *)&m_addr; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | Ipv6Address::operator socklen_t() |
|---|
| 90 | { |
|---|
| 91 | return sizeof(struct sockaddr_in6); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | void Ipv6Address::SetPort(port_t port) |
|---|
| 96 | { |
|---|
| 97 | m_addr.sin6_port = htons( port ); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | port_t Ipv6Address::GetPort() |
|---|
| 102 | { |
|---|
| 103 | return ntohs( m_addr.sin6_port ); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | bool Ipv6Address::Resolve(const std::string& hostname,struct in6_addr& a) |
|---|
| 108 | { |
|---|
| 109 | struct sockaddr_in6 sa; |
|---|
| 110 | memset(&a, 0, sizeof(a)); |
|---|
| 111 | if (Utility::isipv6(hostname)) |
|---|
| 112 | { |
|---|
| 113 | if (!Utility::u2ip(hostname, sa, AI_NUMERICHOST)) |
|---|
| 114 | return false; |
|---|
| 115 | a = sa.sin6_addr; |
|---|
| 116 | return true; |
|---|
| 117 | } |
|---|
| 118 | if (!Utility::u2ip(hostname, sa)) |
|---|
| 119 | return false; |
|---|
| 120 | a = sa.sin6_addr; |
|---|
| 121 | return true; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | bool Ipv6Address::Reverse(struct in6_addr& a,std::string& name) |
|---|
| 126 | { |
|---|
| 127 | struct sockaddr_in6 sa; |
|---|
| 128 | memset(&sa, 0, sizeof(sa)); |
|---|
| 129 | sa.sin6_family = AF_INET6; |
|---|
| 130 | sa.sin6_addr = a; |
|---|
| 131 | return Utility::reverse((struct sockaddr *)&sa, sizeof(sa), name); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | std::string Ipv6Address::Convert(bool include_port) |
|---|
| 136 | { |
|---|
| 137 | if (include_port) |
|---|
| 138 | return Convert(m_addr.sin6_addr) + ":" + Utility::l2string(GetPort()); |
|---|
| 139 | return Convert(m_addr.sin6_addr); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | std::string Ipv6Address::Convert(struct in6_addr& a,bool mixed) |
|---|
| 144 | { |
|---|
| 145 | char slask[100]; // l2ip temporary |
|---|
| 146 | *slask = 0; |
|---|
| 147 | unsigned int prev = 0; |
|---|
| 148 | bool skipped = false; |
|---|
| 149 | bool ok_to_skip = true; |
|---|
| 150 | if (mixed) |
|---|
| 151 | { |
|---|
| 152 | unsigned short x; |
|---|
| 153 | unsigned short addr16[8]; |
|---|
| 154 | memcpy(addr16, &a, sizeof(addr16)); |
|---|
| 155 | for (size_t i = 0; i < 6; i++) |
|---|
| 156 | { |
|---|
| 157 | x = ntohs(addr16[i]); |
|---|
| 158 | if (*slask && (x || !ok_to_skip || prev)) |
|---|
| 159 | strcat(slask,":"); |
|---|
| 160 | if (x || !ok_to_skip) |
|---|
| 161 | { |
|---|
| 162 | sprintf(slask + strlen(slask),"%x", x); |
|---|
| 163 | if (x && skipped) |
|---|
| 164 | ok_to_skip = false; |
|---|
| 165 | } |
|---|
| 166 | else |
|---|
| 167 | { |
|---|
| 168 | skipped = true; |
|---|
| 169 | } |
|---|
| 170 | prev = x; |
|---|
| 171 | } |
|---|
| 172 | x = ntohs(addr16[6]); |
|---|
| 173 | sprintf(slask + strlen(slask),":%u.%u",x / 256,x & 255); |
|---|
| 174 | x = ntohs(addr16[7]); |
|---|
| 175 | sprintf(slask + strlen(slask),".%u.%u",x / 256,x & 255); |
|---|
| 176 | } |
|---|
| 177 | else |
|---|
| 178 | { |
|---|
| 179 | struct sockaddr_in6 sa; |
|---|
| 180 | memset(&sa, 0, sizeof(sa)); |
|---|
| 181 | sa.sin6_family = AF_INET6; |
|---|
| 182 | sa.sin6_addr = a; |
|---|
| 183 | std::string name; |
|---|
| 184 | Utility::reverse((struct sockaddr *)&sa, sizeof(sa), name, NI_NUMERICHOST); |
|---|
| 185 | return name; |
|---|
| 186 | } |
|---|
| 187 | return slask; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | void Ipv6Address::SetAddress(struct sockaddr *sa) |
|---|
| 192 | { |
|---|
| 193 | memcpy(&m_addr, sa, sizeof(struct sockaddr_in6)); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | int Ipv6Address::GetFamily() |
|---|
| 198 | { |
|---|
| 199 | return m_addr.sin6_family; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | void Ipv6Address::SetFlowinfo(uint32_t x) |
|---|
| 204 | { |
|---|
| 205 | m_addr.sin6_flowinfo = x; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | uint32_t Ipv6Address::GetFlowinfo() |
|---|
| 210 | { |
|---|
| 211 | return m_addr.sin6_flowinfo; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | #ifndef _WIN32 |
|---|
| 216 | void Ipv6Address::SetScopeId(uint32_t x) |
|---|
| 217 | { |
|---|
| 218 | m_addr.sin6_scope_id = x; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | uint32_t Ipv6Address::GetScopeId() |
|---|
| 223 | { |
|---|
| 224 | return m_addr.sin6_scope_id; |
|---|
| 225 | } |
|---|
| 226 | #endif |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | bool Ipv6Address::IsValid() |
|---|
| 230 | { |
|---|
| 231 | return m_valid; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | bool Ipv6Address::operator==(SocketAddress& a) |
|---|
| 236 | { |
|---|
| 237 | if (a.GetFamily() != GetFamily()) |
|---|
| 238 | return false; |
|---|
| 239 | if ((socklen_t)a != sizeof(m_addr)) |
|---|
| 240 | return false; |
|---|
| 241 | struct sockaddr *sa = a; |
|---|
| 242 | struct sockaddr_in6 *p = (struct sockaddr_in6 *)sa; |
|---|
| 243 | if (p -> sin6_port != m_addr.sin6_port) |
|---|
| 244 | return false; |
|---|
| 245 | if (memcmp(&p -> sin6_addr, &m_addr.sin6_addr, sizeof(struct in6_addr))) |
|---|
| 246 | return false; |
|---|
| 247 | return true; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | std::auto_ptr<SocketAddress> Ipv6Address::GetCopy() |
|---|
| 252 | { |
|---|
| 253 | return std::auto_ptr<SocketAddress>(new Ipv6Address(m_addr)); |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | std::string Ipv6Address::Reverse() |
|---|
| 258 | { |
|---|
| 259 | std::string tmp; |
|---|
| 260 | Reverse(m_addr.sin6_addr, tmp); |
|---|
| 261 | return tmp; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | #ifdef SOCKETS_NAMESPACE |
|---|
| 266 | } // namespace SOCKETS_NAMESPACE { |
|---|
| 267 | #endif |
|---|
| 268 | #endif // IPPROTO_IPV6 |
|---|
| 269 | #endif // ENABLE_IPV6 |
|---|
| 270 | |
|---|