]> git.tdb.fi Git - libs/net.git/blobdiff - source/inet.cpp
Add IPv6 support
[libs/net.git] / source / inet.cpp
index 181c73403cfa3606731d384abb90a056953f07b4..57d8085cd299f0fb2bb72e5a932652adf69e214f 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspnet
-Copyright © 2008  Mikkosoft Productions, Mikko Rasa
+Copyright © 2008, 2011  Mikkosoft Productions, Mikko Rasa
 Distributed under the LGPL
 */
 
@@ -18,7 +18,7 @@ InetAddr::InetAddr():
        port(0)
 { }
 
-InetAddr::InetAddr(sockaddr_in &sa):
+InetAddr::InetAddr(const sockaddr_in &sa):
        addr(sa.sin_addr.s_addr),
        port(sa.sin_port)
 { }
@@ -31,7 +31,10 @@ InetAddr::InetAddr(in_addr_t a, in_port_t p):
 string InetAddr::str() const
 {
        const unsigned char *ptr=reinterpret_cast<const unsigned char *>(&addr);
-       return format("%d.%d.%d.%d:%d", static_cast<int>(ptr[0]), static_cast<int>(ptr[1]), static_cast<int>(ptr[2]), static_cast<int>(ptr[3]), ntohs(port));
+       string result = format("%d.%d.%d.%d", ptr[0], ptr[1], ptr[2], ptr[3]);
+       if(port)
+               result += format(":%d", ntohs(port));
+       return result;
 }
 
 unsigned InetAddr::fill_sockaddr(sockaddr &sa) const