]> git.tdb.fi Git - libs/net.git/blobdiff - source/net/inet6.cpp
Prepare for assimilating msphttp
[libs/net.git] / source / net / inet6.cpp
diff --git a/source/net/inet6.cpp b/source/net/inet6.cpp
new file mode 100644 (file)
index 0000000..79da1d2
--- /dev/null
@@ -0,0 +1,61 @@
+#ifdef WIN32
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#else
+#include <netinet/in.h>
+#endif
+#include <msp/strings/format.h>
+#include "inet6.h"
+#include "sockaddr_private.h"
+
+using namespace std;
+
+namespace Msp {
+namespace Net {
+
+Inet6Addr::Inet6Addr():
+       port(0)
+{
+       fill(addr, addr+16, 0);
+}
+
+Inet6Addr::Inet6Addr(const SysAddr &sa)
+{
+       const sockaddr_in6 &sai6 = reinterpret_cast<const sockaddr_in6 &>(sa.addr);
+       std::copy(sai6.sin6_addr.s6_addr, sai6.sin6_addr.s6_addr+16, addr);
+       port = htons(sai6.sin6_port);
+}
+
+SockAddr::SysAddr Inet6Addr::to_sys() const
+{
+       SysAddr sa;
+       sa.size = sizeof(sockaddr_in6);
+       sockaddr_in6 &sai6 = reinterpret_cast<sockaddr_in6 &>(sa.addr);
+       sai6.sin6_family = AF_INET6;
+       std::copy(addr, addr+16, sai6.sin6_addr.s6_addr);
+       sai6.sin6_port = htons(port);
+       sai6.sin6_flowinfo = 0;
+       sai6.sin6_scope_id = 0;
+
+       return sa;
+}
+
+string Inet6Addr::str() const
+{
+       string result = "[";
+       for(unsigned i=0; i<16; i+=2)
+       {
+               unsigned short part = (addr[i]<<8) | addr[i+1];
+               if(i>0)
+                       result += ':';
+               result += format("%x", part);
+       }
+       result += ']';
+       if(port)
+               result += format(":%d", port);
+
+       return result;
+}
+
+} // namespace Net
+} // namespace Msp