Again some doc-build fixes
[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 // Definition of non-inline non-template functions
24
25 #include "PacketSocketHandle.hh"
26 #include "PacketSocketHandle.ih"
27
28 // Custom includes
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>
34 #include <net/if.h>
35 #include <errno.h>
36
37 //#include "PacketSocketHandle.mpp"
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 prefix_ void senf::PacketProtocol::init_client(SocketType type, int protocol)
42     const
43 {
44     int socktype = SOCK_RAW;
45     if (type == DatagramSocket)
46         socktype = SOCK_DGRAM;
47     if (protocol == -1)
48         protocol = ETH_P_ALL;
49     int sock = ::socket(PF_PACKET, socktype, htons(protocol));
50     if (sock < 0)
51         throw SystemException(errno);
52     body().fd(sock);
53 }
54
55 prefix_ std::auto_ptr<senf::SocketProtocol> senf::PacketProtocol::clone()
56     const
57 {
58     return std::auto_ptr<SocketProtocol>(new PacketProtocol());
59 }
60
61 prefix_ unsigned senf::PacketProtocol::available()
62     const
63 {
64     if (! body().readable())
65         return 0;
66     ssize_t l = ::recv(body().fd(),0,0,MSG_PEEK | MSG_TRUNC);
67     if (l < 0)
68         throw SystemException(errno);
69     return l;
70 }
71
72 prefix_ bool senf::PacketProtocol::eof()
73     const
74 {
75     return false;
76 }
77
78 prefix_ void senf::PacketProtocol::promisc(std::string interface, PromiscMode mode)
79     const
80 {
81     /** \bug There are some failures here ... need to investigate */
82
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
86     // semantics
87
88     struct packet_mreq mreq;
89     mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
90     if (mreq.mr_ifindex == 0)
91         throw SystemException(EINVAL);
92     mreq.mr_alen = 0;
93
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);
98
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);
103 }
104
105 prefix_ void senf::PacketProtocol::do_mc_i(std::string interface,
106                                                   detail::LLAddressCopier const & copier, bool add)
107     const
108 {
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);
119 }
120
121 ///////////////////////////////cc.e////////////////////////////////////////
122 #undef prefix_
123 //#include "PacketSocketHandle.mpp"
124
125 \f
126 // Local Variables:
127 // mode: c++
128 // c-file-style: "senf"
129 // End: