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 satcom::lib::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<satcom::lib::SocketProtocol> satcom::lib::PacketProtocol::clone()
58 return std::auto_ptr<SocketProtocol>(new PacketProtocol());
61 prefix_ unsigned satcom::lib::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 satcom::lib::PacketProtocol::eof()
78 prefix_ void satcom::lib::PacketProtocol::promisc(std::string interface, PromiscMode mode)
81 // The interface is really stupid: as far as i understand, it is possible to
82 // enable PROMISC and ALLMULTI seperately, however PROMISC is really a superset
83 // of ALLMULTI ... grmpf ... therefore we allways set/reset both to implement sane
86 struct packet_mreq mreq;
87 mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
88 if (mreq.mr_ifindex == 0)
89 throw SystemException(EINVAL);
92 mreq.mr_type = PACKET_MR_PROMISC;
93 int command = mode == Promiscuous ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP;
94 if (::setsockopt(body().fd(),SOL_PACKET,command,&mreq,sizeof(mreq)) < 0)
95 throw SystemException(errno);
97 mreq.mr_type = PACKET_MR_ALLMULTI;
98 command = mode == AllMulticast ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP;
99 if (::setsockopt(body().fd(),SOL_PACKET,command,&mreq,sizeof(mreq)) < 0)
100 throw SystemException(errno);
103 prefix_ void satcom::lib::PacketProtocol::do_mc_i(std::string interface,
104 detail::LLAddressCopier const & copier, bool add)
107 struct packet_mreq mreq;
108 mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
109 if (mreq.mr_ifindex == 0)
110 throw SystemException(EINVAL);
111 mreq.mr_type = PACKET_MR_MULTICAST;
112 mreq.mr_alen = copier(&mreq.mr_address[0]);
113 if (::setsockopt(body().fd(),SOL_PACKET,
114 add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
115 &mreq, sizeof(mreq)) < 0)
116 throw SystemException(errno);
119 ///////////////////////////////cc.e////////////////////////////////////////
121 //#include "PacketSocketHandle.mpp"
126 // c-file-style: "satcom"