Utils: Removed ErrnoException and implemented generic Exception base-class
[senf.git] / Socket / Protocols / INet / INetProtocol.cc
index 6ddc307..bf32dea 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 //
 // Copyright (C) 2006
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
-//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -29,6 +29,7 @@
 // Custom includes
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <net/if.h>
 #include "../../../Utils/Exception.hh"
 
 //#include "INetProtocol.mpp"
 ///////////////////////////////cc.p////////////////////////////////////////
 
 ///////////////////////////////////////////////////////////////////////////
-// senf::IPv4Protocol
+// senf::INetProtocol
 
-prefix_ void senf::IPv4Protocol::connect(INet4SocketAddress const & address)
+prefix_ void senf::INetProtocol::bindInterface(std::string const & iface)
     const
 {
-    if (::connect(fd(),address.sockaddr_p(), address.sockaddr_len()) < 0)
-        throwErrno();
+    if (::setsockopt(fd(), SOL_SOCKET, SO_BINDTODEVICE, iface.c_str(), iface.size()) < 0)
+        throw SystemException("::setsockopt(SO_BINDTODEVICE)");
 }
 
-prefix_ void senf::IPv4Protocol::bind(INet4SocketAddress const & address)
-    const
+prefix_ std::string senf::INetProtocol::bindInterface()
 {
-    if (::bind(fd(),address.sockaddr_p(), address.sockaddr_len()) < 0)
-        throwErrno();
+    char iface[IFNAMSIZ];
+    socklen_t size (sizeof(iface));
+    ::memset(iface, 0, sizeof(iface));
+    if (::getsockopt(fd(), SOL_SOCKET, SO_BINDTODEVICE, iface, &size) < 0)
+        throw SystemException("::getsockopt(SO_BINDTODEVICE)");
+    iface[size < IFNAMSIZ ? size : IFNAMSIZ-1] = 0;
+    return iface;
 }
 
+///////////////////////////////////////////////////////////////////////////
+// senf::IPv4Protocol
 
 ///////////////////////////////////////////////////////////////////////////
 // senf::IPv6Protocol
 
-prefix_ void senf::IPv6Protocol::connect(INet6SocketAddress const & address)
-    const
-{
-    if (::connect(fd(),address.sockaddr_p(), address.sockaddr_len()) < 0)
-        throwErrno();
-}
-
-prefix_ void senf::IPv6Protocol::bind(INet6SocketAddress const & address)
-    const
-{
-    if (::bind(fd(),address.sockaddr_p(), address.sockaddr_len()) < 0)
-        throwErrno();
-}
-
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 //#include "INetProtocol.mpp"