05eef453add6b1291aad4046d7e041edefae4a3f
[senf.git] / Socket / Protocols / Raw / PacketSocketHandle.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.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         throwErrno();
54     fd(sock);
55 }
56
57 prefix_ unsigned senf::PacketProtocol::available()
58     const
59 {
60     if (! fh().readable())
61         return 0;
62     ssize_t l = ::recv(fd(),0,0,MSG_PEEK | MSG_TRUNC);
63     if (l < 0)
64         throwErrno();
65     return l;
66 }
67
68 prefix_ bool senf::PacketProtocol::eof()
69     const
70 {
71     return false;
72 }
73
74 namespace {
75     
76     void do_mc(int fd, std::string const & interface, senf::MACAddress address, bool add)
77     {
78         struct packet_mreq mreq;
79         mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
80         if (mreq.mr_ifindex == 0)
81             senf::throwErrno(EINVAL);
82         mreq.mr_type = PACKET_MR_MULTICAST;
83         mreq.mr_alen = 6;
84         std::copy(address.begin(), address.end(), &mreq.mr_address[0]);
85         if (::setsockopt(fd, SOL_PACKET,
86                          add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
87                          &mreq, sizeof(mreq)) < 0)
88             senf::throwErrno();
89     }
90
91 }
92
93 prefix_ void senf::PacketProtocol::mcAdd(std::string const & interface,
94                                          MACAddress const & address)
95     const
96 {
97     do_mc(fd(),interface,address,true);
98 }
99
100 prefix_ void senf::PacketProtocol::mcDrop(std::string const & interface,
101                                           MACAddress const & address)
102     const
103 {
104     do_mc(fd(),interface,address,false);
105 }
106
107 ///////////////////////////////cc.e////////////////////////////////////////
108 #undef prefix_
109 //#include "PacketSocketHandle.mpp"
110
111 \f
112 // Local Variables:
113 // mode: c++
114 // fill-column: 100
115 // c-file-style: "senf"
116 // indent-tabs-mode: nil
117 // ispell-local-dictionary: "american"
118 // compile-command: "scons -u test"
119 // comment-column: 40
120 // End: