63f3459e8c486f2f9584870fdb5aa54d859dce8a
[senf.git] / senf / Socket / Protocols / Raw / MmapedPacketSocketHandle.cc
1 // $Id$
2 //
3 // Copyright (C) 2011
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
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
10 //
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.
14 //
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.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
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.
24 //
25 // Contributor(s):
26 //   Thorsten Horstmann <tho@berlios.de>
27
28 /** \file
29     \brief MmapedPacketSocketProtocol and MmapedPacketSocketHandle non-inline non-template implementation
30  */
31
32 #include "MmapedPacketSocketHandle.hh"
33 //#include "MmapedPacketSocketHandle.ih"
34
35 // Custom includes
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/mman.h>
39 #include <linux/if_packet.h>
40 #include <net/ethernet.h>
41 #include <netinet/in.h>
42 #include <net/if.h>
43 #include <errno.h>
44
45 //#include "MmapedPacketSocketHandle.mpp"
46 #define prefix_
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48
49 prefix_ void senf::MmapedPacketSocketProtocol::init_client(SocketType type, int protocol)
50 {
51     /* Setup req */
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;
56     /* Setup socket */
57     int socktype = (type == RawSocket ? SOCK_RAW : SOCK_DGRAM);
58     if (protocol == -1)
59         protocol = ETH_P_ALL;
60     int sock = ::socket(PF_PACKET, socktype, htons(protocol));
61     if (sock < 0)
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.");
65     /* Setup mmap */
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;
75     }
76     fd( sock);
77 }
78
79 prefix_ unsigned senf::MmapedPacketSocketProtocol::available()
80     const
81 {
82
83 }
84
85 prefix_ bool senf::MmapedPacketSocketProtocol::eof()
86     const
87 {
88     return false;
89 }
90
91
92 //-/////////////////////////////////////////////////////////////////////////////////////////////////
93 #undef prefix_
94 //#include "PacketSocketHandle.mpp"
95
96 \f
97 // Local Variables:
98 // mode: c++
99 // fill-column: 100
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
105 // End: