EMANE  1.2.1
tuntap.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2016 - Adjacent Link LLC, Bridgewater, New
3  * Jersey
4  * Copyright (c) 2008 - DRS CenGen, LLC, Columbia, Maryland
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of DRS CenGen, LLC nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef TUNTAP_HEADER_
36 #define TUNTAP_HEADER_
37 
38 #include "emane/inetaddr.h"
39 #include <string>
40 
42 #include "emane/utils/netutils.h"
43 
44 
45 #include <linux/if_tun.h>
46 
47 #undef ETH_ALEN
48 #undef ETH_P_ARP
49 #undef ETH_P_IPV6
50 
51 #define NETWORK_DEVICE_PATH "/dev/net/tun"
52 
53 // local sockaddr
55 {
56  short sin_family; // e.g. AF_INET, AF_INET6
57  unsigned short sin_port; // e.g. htons(3490)
58  struct in_addr sin_addr; // see struct in_addr, below
59  char sin_zero[8]; // zero this if you want to
60 } __attribute__((__may_alias__));
61 
62 
63 
64 namespace EMANE
65 {
66  namespace Transports
67  {
68  namespace Virtual
69  {
70  class TunTap
71  {
72  private:
73 
74  PlatformServiceProvider * pPlatformService_;
75  int tunHandle_;
76  std::string tunName_;
77  std::string tunPath_;
78  std::string tunGuid_;
79  int tunIndex_;
80  INETAddr tunAddr_;
81  INETAddr tunMask_;
82 
83 
84  int set_flags(int, int);
85  int get_flags();
86 
87  public:
88  TunTap(PlatformServiceProvider * pPlatformService);
89 
90  ~TunTap();
91 
100  int open(const char *, const char *);
101 
107  int close();
108 
117  int activate(bool);
118 
126  int deactivate();
127 
133  int get_handle();
134 
143  int readv(struct iovec*, size_t);
144 
153  int writev(const struct iovec*, size_t);
154 
166  int set_addr(const INETAddr &, const INETAddr &);
167 
175  int set_ethaddr(const Utils::EtherAddr &);
176 
184  int set_ethaddr(NEMId);
185 
191  INETAddr & get_addr();
192 
198  INETAddr & get_mask();
199  };
200  }
201  }
202 }
203 
204 #endif // TUNTAP_HEADER_
struct in_addr sin_addr
Definition: tuntap.h:58
The PlatformServiceProvider interface provides access to emulator services.
unsigned short sin_port
Definition: tuntap.h:57
struct sockaddr_in_t __attribute__((__may_alias__))
std::uint16_t NEMId
Definition: types.h:52
Definition of the ethernet frame address as an array of 6 bytes or set of 3 words.
Definition: netutils.h:422
short sin_family
Definition: tuntap.h:56
Definition: agent.h:43
char sin_zero[8]
Definition: tuntap.h:59