]> git.tdb.fi Git - libs/net.git/blobdiff - source/inet.cpp
Initial revision
[libs/net.git] / source / inet.cpp
diff --git a/source/inet.cpp b/source/inet.cpp
new file mode 100644 (file)
index 0000000..181c734
--- /dev/null
@@ -0,0 +1,48 @@
+/* $Id$
+
+This file is part of libmspnet
+Copyright © 2008  Mikkosoft Productions, Mikko Rasa
+Distributed under the LGPL
+*/
+
+#include <msp/strings/formatter.h>
+#include "inet.h"
+
+using namespace std;
+
+namespace Msp {
+namespace Net {
+
+InetAddr::InetAddr():
+       addr(0),
+       port(0)
+{ }
+
+InetAddr::InetAddr(sockaddr_in &sa):
+       addr(sa.sin_addr.s_addr),
+       port(sa.sin_port)
+{ }
+
+InetAddr::InetAddr(in_addr_t a, in_port_t p):
+       addr(htonl(a)),
+       port(htons(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));
+}
+
+unsigned InetAddr::fill_sockaddr(sockaddr &sa) const
+{
+       sockaddr_in &sai=reinterpret_cast<sockaddr_in &>(sa);
+       sai.sin_family=AF_INET;
+       sai.sin_addr.s_addr=addr;
+       sai.sin_port=port;
+
+       return sizeof(sockaddr_in);
+}
+
+} // namespace Net
+} // namespace Msp