4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Thorsten Horstmann <tho@berlios.de>
29 \brief MmapedPacketSocketProtocol and MmapedPacketSocketHandle non-inline non-template implementation
32 #include "MmapedPacketSocketHandle.hh"
33 //#include "MmapedPacketSocketHandle.ih"
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <linux/if_packet.h>
40 #include <net/ethernet.h>
41 #include <netinet/in.h>
45 //#include "MmapedPacketSocketHandle.mpp"
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
49 prefix_ void senf::MmapedPacketSocketProtocol::init_client(SocketType type, int protocol)
52 req_.tp_block_size = 4096;
53 req_.tp_frame_size = 2048;
54 req_.tp_block_nr = 32;
55 req_.tp_frame_nr = 2*32;
57 int socktype = (type == RawSocket ? SOCK_RAW : SOCK_DGRAM);
60 int sock = ::socket(PF_PACKET, socktype, htons(protocol));
62 SENF_THROW_SYSTEM_EXCEPTION("::socket(...) failed.");
63 if ( (setsockopt(sock, SOL_PACKET, PACKET_RX_RING, reinterpret_cast<char *>(&req_), sizeof(req_))) != 0 )
64 SENF_THROW_SYSTEM_EXCEPTION("setsockopt(PACKET_RX_RING) failed.");
66 map_ = static_cast<char *>(
67 mmap( NULL, req_.tp_block_size * req_.tp_block_nr, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED, sock, 0));
68 if ( map_ == MAP_FAILED )
69 SENF_THROW_SYSTEM_EXCEPTION("mmap failed.");
70 /* Setup our ringbuffer */
71 ring_.resize( req_.tp_frame_nr);
72 for (unsigned i=0; i<req_.tp_frame_nr; i++) {
73 ring_[i].iov_base = (void *) (&map_[i*req_.tp_frame_size]);
74 ring_[i].iov_len = req_.tp_frame_size;
79 prefix_ unsigned senf::MmapedPacketSocketProtocol::available()
85 prefix_ bool senf::MmapedPacketSocketProtocol::eof()
92 //-/////////////////////////////////////////////////////////////////////////////////////////////////
94 //#include "PacketSocketHandle.mpp"
100 // c-file-style: "senf"
101 // indent-tabs-mode: nil
102 // ispell-local-dictionary: "american"
103 // compile-command: "scons -u test"
104 // comment-column: 40