]> git.tdb.fi Git - libs/net.git/blobdiff - source/net/unix.cpp
Add support for AF_UNIX
[libs/net.git] / source / net / unix.cpp
diff --git a/source/net/unix.cpp b/source/net/unix.cpp
new file mode 100644 (file)
index 0000000..373b65a
--- /dev/null
@@ -0,0 +1,82 @@
+#include <stdexcept>
+#ifndef WIN32
+#include <sys/un.h>
+#endif
+#include "sockaddr_private.h"
+#include "unix.h"
+
+using namespace std;
+
+namespace Msp {
+namespace Net {
+
+UnixAddr::UnixAddr():
+       abstract(false)
+{
+#ifdef WIN32
+       throw logic_error("no unix sockets on windows");
+#endif
+}
+
+UnixAddr::UnixAddr(const SysAddr &sa):
+       abstract(false)
+{
+#ifdef WIN32
+       (void)sa;
+       throw logic_error("no unix sockets on windows");
+#else
+       const sockaddr_un &sau = reinterpret_cast<const sockaddr_un &>(sa.addr);
+       if(sa.size>sizeof(sa_family_t))
+       {
+               abstract = (sau.sun_path[0]==0);
+               path.assign(sau.sun_path+abstract, sa.size-sizeof(sa_family_t)-abstract);
+       }
+#endif
+}
+
+UnixAddr::UnixAddr(const string &p, bool a):
+       path(p),
+       abstract(a)
+{
+#ifdef WIN32
+       throw logic_error("no unix sockets on windows");
+#else
+       if(sizeof(sa_family_t)+path.size()+1>sizeof(sockaddr_storage))
+               throw invalid_argument("UnixAddr");
+#endif
+}
+
+SockAddr::SysAddr UnixAddr::to_sys() const
+{
+       SysAddr sa;
+#ifndef WIN32
+       sa.size = sizeof(sa_family_t);
+       if(!path.empty())
+               sa.size += path.size()+1;
+
+       sockaddr_un &sau = reinterpret_cast<sockaddr_un &>(sa.addr);
+       sau.sun_family = AF_UNIX;
+       if(!path.empty())
+       {
+               if(abstract)
+                       sau.sun_path[0] = 0;
+               std::copy(path.begin(), path.end(), sau.sun_path+abstract);
+               if(!abstract)
+                       sau.sun_path[path.size()] = 0;
+       }
+#endif
+
+       return sa;
+}
+
+string UnixAddr::str() const
+{
+       string result = "unix:";
+       if(abstract)
+               result += '@';
+       result += path;
+       return result;
+}
+
+} // namespace Net
+} // namespace Msp