4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.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.
23 // Definition of non-inline non-template functions
25 #include "PacketSocketHandle.hh"
26 #include "PacketSocketHandle.ih"
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netpacket/packet.h>
32 #include <net/ethernet.h>
33 #include <netinet/in.h>
37 //#include "PacketSocketHandle.mpp"
39 ///////////////////////////////cc.p////////////////////////////////////////
41 prefix_ void senf::PacketProtocol::init_client(SocketType type, int protocol)
44 int socktype = SOCK_RAW;
45 if (type == DatagramSocket)
46 socktype = SOCK_DGRAM;
49 int sock = ::socket(PF_PACKET, socktype, htons(protocol));
51 throw SystemException(errno);
55 prefix_ std::auto_ptr<senf::SocketProtocol> senf::PacketProtocol::clone()
58 return std::auto_ptr<SocketProtocol>(new PacketProtocol());
61 prefix_ unsigned senf::PacketProtocol::available()
64 if (! body().readable())
66 ssize_t l = ::recv(body().fd(),0,0,MSG_PEEK | MSG_TRUNC);
68 throw SystemException(errno);
72 prefix_ bool senf::PacketProtocol::eof()
78 prefix_ void senf::PacketProtocol::promisc(std::string interface, PromiscMode mode)
81 /** \bug There are some failures here ... need to investigate */
83 // The interface is really stupid: as far as i understand, it is possible to
84 // enable PROMISC and ALLMULTI seperately, however PROMISC is really a superset
85 // of ALLMULTI ... grmpf ... therefore we allways set/reset both to implement sane
88 struct packet_mreq mreq;
89 mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
90 if (mreq.mr_ifindex == 0)
91 throw SystemException(EINVAL);
94 mreq.mr_type = PACKET_MR_PROMISC;
95 int command = mode == Promiscuous ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP;
96 if (::setsockopt(body().fd(),SOL_PACKET,command,&mreq,sizeof(mreq)) < 0)
97 throw SystemException(errno);
99 mreq.mr_type = PACKET_MR_ALLMULTI;
100 command = mode == AllMulticast ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP;
101 if (::setsockopt(body().fd(),SOL_PACKET,command,&mreq,sizeof(mreq)) < 0)
102 throw SystemException(errno);
105 prefix_ void senf::PacketProtocol::do_mc_i(std::string interface,
106 detail::LLAddressCopier const & copier, bool add)
109 struct packet_mreq mreq;
110 mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
111 if (mreq.mr_ifindex == 0)
112 throw SystemException(EINVAL);
113 mreq.mr_type = PACKET_MR_MULTICAST;
114 mreq.mr_alen = copier(&mreq.mr_address[0]);
115 if (::setsockopt(body().fd(),SOL_PACKET,
116 add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
117 &mreq, sizeof(mreq)) < 0)
118 throw SystemException(errno);
121 ///////////////////////////////cc.e////////////////////////////////////////
123 //#include "PacketSocketHandle.mpp"
128 // c-file-style: "senf"