git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@129 270642c3-0616-0410...
[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 satcom::lib::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<satcom::lib::SocketProtocol> satcom::lib::PacketProtocol::clone()
56     const
57 {
58     return std::auto_ptr<SocketProtocol>(new PacketProtocol());
59 }
60
61 prefix_ unsigned satcom::lib::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 satcom::lib::PacketProtocol::eof()
73     const
74 {
75     return false;
76 }
77
78 prefix_ void satcom::lib::PacketProtocol::promisc(std::string interface, PromiscMode mode)
79     const
80 {
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
84     // semantics
85
86     struct packet_mreq mreq;
87     mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
88     if (mreq.mr_ifindex == 0)
89         throw SystemException(EINVAL);
90     mreq.mr_alen = 0;
91
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);
96
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);
101 }
102
103 prefix_ void satcom::lib::PacketProtocol::do_mc_i(std::string interface,
104                                                   detail::LLAddressCopier const & copier, bool add)
105     const
106 {
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);
117 }
118
119 ///////////////////////////////cc.e////////////////////////////////////////
120 #undef prefix_
121 //#include "PacketSocketHandle.mpp"
122
123 \f
124 // Local Variables:
125 // mode: c++
126 // c-file-style: "satcom"
127 // End: