Skip to content

Commit e47f6fd

Browse files
committedJun 24, 2013
Fix ipv6 on windows
1 parent 30d6d4c commit e47f6fd

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed
 

Diff for: ‎src/socket.cpp

+31-1
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,41 @@ void Address::Resolve(const char *name)
185185
// IP address -> textual representation
186186
std::string Address::serializeString() const
187187
{
188+
// windows XP doesnt have inet_ntop, maybe use better func
189+
#ifdef _WIN32
190+
if(m_addr_family == AF_INET)
191+
{
192+
u8 a, b, c, d, addr;
193+
addr = ntohl(m_address.ipv4.sin_addr.s_addr);
194+
a = (addr & 0xFF000000) >> 24;
195+
b = (addr & 0x00FF0000) >> 16;
196+
c = (addr & 0x0000FF00) >> 8;
197+
d = (addr & 0x000000FF);
198+
return itos(a) + "." + itos(b) + "." + itos(c) + "." + itos(d);
199+
}
200+
else if(m_addr_family == AF_INET6)
201+
{
202+
std::ostringstream os;
203+
for(int i = 0; i < 16; i += 2)
204+
{
205+
u16 section =
206+
(m_address.ipv6.sin6_addr.s6_addr[i] << 8) |
207+
(m_address.ipv6.sin6_addr.s6_addr[i + 1]);
208+
os << std::hex << section;
209+
if(i < 14)
210+
os << ":";
211+
}
212+
return os.str();
213+
}
214+
else
215+
return std::string("");
216+
#else
188217
char str[INET6_ADDRSTRLEN];
189218
if (inet_ntop(m_addr_family, (m_addr_family == AF_INET) ? (void*)&(m_address.ipv4.sin_addr) : (void*)&(m_address.ipv6.sin6_addr), str, INET6_ADDRSTRLEN) == NULL) {
190-
return std::string("");
219+
return std::string("");
191220
}
192221
return std::string(str);
222+
#endif
193223
}
194224

195225
struct sockaddr_in Address::getAddress() const

0 commit comments

Comments
 (0)
Please sign in to comment.