Fixed whitespace in all files (no tabs)
[senf.git] / Socket / PacketSocketHandle.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief PacketProtocol and PacketSocketHandle non-inline non-template implementation
25  */
26
27 #include "PacketSocketHandle.hh"
28 #include "PacketSocketHandle.ih"
29
30 // Custom includes
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>
36 #include <net/if.h>
37 #include <errno.h>
38
39 //#include "PacketSocketHandle.mpp"
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
42
43 prefix_ void senf::PacketProtocol::init_client(SocketType type, int protocol)
44     const
45 {
46     int socktype = SOCK_RAW;
47     if (type == DatagramSocket)
48         socktype = SOCK_DGRAM;
49     if (protocol == -1)
50         protocol = ETH_P_ALL;
51     int sock = ::socket(PF_PACKET, socktype, htons(protocol));
52     if (sock < 0)
53         throw SystemException(errno);
54     body().fd(sock);
55 }
56
57 prefix_ std::auto_ptr<senf::SocketProtocol> senf::PacketProtocol::clone()
58     const
59 {
60     return std::auto_ptr<SocketProtocol>(new PacketProtocol());
61 }
62
63 prefix_ unsigned senf::PacketProtocol::available()
64     const
65 {
66     if (! body().readable())
67         return 0;
68     ssize_t l = ::recv(body().fd(),0,0,MSG_PEEK | MSG_TRUNC);
69     if (l < 0)
70         throw SystemException(errno);
71     return l;
72 }
73
74 prefix_ bool senf::PacketProtocol::eof()
75     const
76 {
77     return false;
78 }
79
80 prefix_ void senf::PacketProtocol::promisc(std::string interface, PromiscMode mode)
81     const
82 {
83     /** \bug There are some failures here ... need to investigate */
84
85     // The interface is really stupid: as far as i understand, it is possible to
86     // enable PROMISC and ALLMULTI seperately, however PROMISC is really a superset
87     // of ALLMULTI ... grmpf ... therefore we allways set/reset both to implement sane
88     // semantics
89
90     struct packet_mreq mreq;
91     mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
92     if (mreq.mr_ifindex == 0)
93         throw SystemException(EINVAL);
94     mreq.mr_alen = 0;
95
96     mreq.mr_type = PACKET_MR_PROMISC;
97     int command = mode == Promiscuous ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP;
98     if (::setsockopt(body().fd(),SOL_PACKET,command,&mreq,sizeof(mreq)) < 0)
99         throw SystemException(errno);
100
101     mreq.mr_type = PACKET_MR_ALLMULTI;
102     command = mode == AllMulticast ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP;
103     if (::setsockopt(body().fd(),SOL_PACKET,command,&mreq,sizeof(mreq)) < 0)
104         throw SystemException(errno);
105 }
106
107 prefix_ void senf::PacketProtocol::do_mc_i(std::string interface,
108                                                   detail::LLAddressCopier const & copier, bool add)
109     const
110 {
111     struct packet_mreq mreq;
112     mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
113     if (mreq.mr_ifindex == 0)
114         throw SystemException(EINVAL);
115     mreq.mr_type = PACKET_MR_MULTICAST;
116     mreq.mr_alen = copier(&mreq.mr_address[0]);
117     if (::setsockopt(body().fd(),SOL_PACKET,
118                      add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
119                      &mreq, sizeof(mreq)) < 0)
120         throw SystemException(errno);
121 }
122
123 ///////////////////////////////cc.e////////////////////////////////////////
124 #undef prefix_
125 //#include "PacketSocketHandle.mpp"
126
127 \f
128 // Local Variables:
129 // mode: c++
130 // fill-column: 100
131 // c-file-style: "senf"
132 // indent-tabs-mode: nil
133 // ispell-local-dictionary: "american"
134 // End: