PacketSocketHandle.cc

Go to the documentation of this file.
00001 // $Id: PacketSocketHandle.cc 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2006
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00027 #include "PacketSocketHandle.hh"
00028 //#include "PacketSocketHandle.ih"
00029 
00030 // Custom includes
00031 #include <sys/types.h>
00032 #include <sys/socket.h>
00033 #include <netpacket/packet.h>
00034 #include <net/ethernet.h>
00035 #include <netinet/in.h>
00036 #include <net/if.h>
00037 #include <errno.h>
00038 
00039 //#include "PacketSocketHandle.mpp"
00040 #define prefix_
00041 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00042 
00043 prefix_ void senf::PacketSocketProtocol::init_client(SocketType type, int protocol)
00044     const
00045 {
00046     int socktype = SOCK_RAW;
00047     if (type == DatagramSocket)
00048         socktype = SOCK_DGRAM;
00049     if (protocol == -1)
00050         protocol = ETH_P_ALL;
00051     int sock = ::socket(PF_PACKET, socktype, htons(protocol));
00052     if (sock < 0)
00053         SENF_THROW_SYSTEM_EXCEPTION("::socket(...) failed.");
00054     fd(sock);
00055 }
00056 
00057 prefix_ unsigned senf::PacketSocketProtocol::available()
00058     const
00059 {
00060     if (! fh().readable())
00061         return 0;
00062     ssize_t l = ::recv(fd(),0,0,MSG_PEEK | MSG_TRUNC);
00063     if (l < 0)
00064         SENF_THROW_SYSTEM_EXCEPTION("::recv(socket_fd) failed.");
00065     return l;
00066 }
00067 
00068 prefix_ bool senf::PacketSocketProtocol::eof()
00069     const
00070 {
00071     return false;
00072 }
00073 
00074 namespace {
00075 
00076     void do_mc(int fd, std::string const & interface, senf::MACAddress address, bool add)
00077     {
00078         struct packet_mreq mreq;
00079         ::memset(&mreq, 0, sizeof(mreq));
00080         mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
00081         if (mreq.mr_ifindex == 0)
00082             throw senf::SystemException(EINVAL);
00083         mreq.mr_type = PACKET_MR_MULTICAST;
00084         mreq.mr_alen = 6;
00085         std::copy(address.begin(), address.end(), &mreq.mr_address[0]);
00086         if (::setsockopt(fd, SOL_PACKET,
00087                         add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
00088                         &mreq, sizeof(mreq)) < 0)
00089             throw senf::SystemException();
00090     }
00091 }
00092 
00093 prefix_ void senf::PacketSocketProtocol::mcAdd(std::string const & interface,
00094                                          MACAddress const & address)
00095     const
00096 {
00097     do_mc(fd(), interface, address, true);
00098 }
00099 
00100 prefix_ void senf::PacketSocketProtocol::mcDrop(std::string const & interface,
00101                                           MACAddress const & address)
00102     const
00103 {
00104     do_mc(fd(), interface, address, false);
00105 }
00106 
00107 prefix_ void senf::PacketSocketProtocol::promisc(std::string const & interface, bool mode)
00108     const
00109 {
00110     struct packet_mreq mreq;
00111     ::memset(&mreq, 0, sizeof(mreq));
00112     mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
00113     if (mreq.mr_ifindex == 0)
00114         throw senf::SystemException(EINVAL);
00115     mreq.mr_type = PACKET_MR_PROMISC;
00116     if (::setsockopt(fd(), SOL_PACKET,
00117                      mode ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
00118                      &mreq, sizeof(mreq)) < 0)
00119         throw senf::SystemException();
00120 }
00121 
00122 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00123 #undef prefix_
00124 //#include "PacketSocketHandle.mpp"
00125 
00126 
00127 // Local Variables:
00128 // mode: c++
00129 // fill-column: 100
00130 // c-file-style: "senf"
00131 // indent-tabs-mode: nil
00132 // ispell-local-dictionary: "american"
00133 // compile-command: "scons -u test"
00134 // comment-column: 40
00135 // End: