4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief PacketSocketProtocol and PacketSocketHandle non-inline non-template implementation
27 #include "PacketSocketHandle.hh"
28 //#include "PacketSocketHandle.ih"
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netpacket/packet.h>
34 #include <net/ethernet.h>
35 #include <netinet/in.h>
39 //#include "PacketSocketHandle.mpp"
41 ///////////////////////////////cc.p////////////////////////////////////////
43 prefix_ void senf::PacketSocketProtocol::init_client(SocketType type, int protocol)
46 int socktype = SOCK_RAW;
47 if (type == DatagramSocket)
48 socktype = SOCK_DGRAM;
51 int sock = ::socket(PF_PACKET, socktype, htons(protocol));
53 SENF_THROW_SYSTEM_EXCEPTION("::socket(...) failed.");
57 prefix_ unsigned senf::PacketSocketProtocol::available()
60 if (! fh().readable())
62 ssize_t l = ::recv(fd(),0,0,MSG_PEEK | MSG_TRUNC);
64 SENF_THROW_SYSTEM_EXCEPTION("::recv(socket_fd) failed.");
68 prefix_ bool senf::PacketSocketProtocol::eof()
76 void do_mc(int fd, std::string const & interface, senf::MACAddress address, bool add)
78 struct packet_mreq mreq;
79 ::memset(&mreq, 0, sizeof(mreq));
80 mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
81 if (mreq.mr_ifindex == 0)
82 throw senf::SystemException(EINVAL);
83 mreq.mr_type = PACKET_MR_MULTICAST;
85 std::copy(address.begin(), address.end(), &mreq.mr_address[0]);
86 if (::setsockopt(fd, SOL_PACKET,
87 add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
88 &mreq, sizeof(mreq)) < 0)
89 throw senf::SystemException();
93 prefix_ void senf::PacketSocketProtocol::mcAdd(std::string const & interface,
94 MACAddress const & address)
97 do_mc(fd(), interface, address, true);
100 prefix_ void senf::PacketSocketProtocol::mcDrop(std::string const & interface,
101 MACAddress const & address)
104 do_mc(fd(), interface, address, false);
107 prefix_ void senf::PacketSocketProtocol::promisc(std::string const & interface, bool mode)
110 struct packet_mreq mreq;
111 ::memset(&mreq, 0, sizeof(mreq));
112 mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
113 if (mreq.mr_ifindex == 0)
114 throw senf::SystemException(EINVAL);
115 mreq.mr_type = PACKET_MR_PROMISC;
116 if (::setsockopt(fd(), SOL_PACKET,
117 mode ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
118 &mreq, sizeof(mreq)) < 0)
119 throw senf::SystemException();
122 ///////////////////////////////cc.e////////////////////////////////////////
124 //#include "PacketSocketHandle.mpp"
130 // c-file-style: "senf"
131 // indent-tabs-mode: nil
132 // ispell-local-dictionary: "american"
133 // compile-command: "scons -u test"
134 // comment-column: 40