20 #include "ns3/mac48-address.h"
22 #include <arpa/inet.h>
28 #include <linux/if_tun.h>
30 #include <net/route.h>
31 #include <netinet/in.h>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
41 #define TAP_MAGIC 95549
50 struct in6_addr ifr6_addr;
56 SetIpv4(
const char* deviceName,
const char* ip,
const char* netmask)
59 struct sockaddr_in* sin;
61 int sock = socket(AF_INET, SOCK_DGRAM, 0);
66 memset(&ifr, 0,
sizeof(
struct ifreq));
67 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
69 sin = (
struct sockaddr_in*)&ifr.ifr_addr;
70 inet_pton(AF_INET, ip, &sin->sin_addr);
71 ifr.ifr_addr.sa_family = AF_INET;
73 ABORT_IF(ioctl(sock, SIOCSIFADDR, &ifr) == -1,
"Could not set IP address",
true);
75 LOG(
"Set device IP address to " << ip);
80 memset(&ifr, 0,
sizeof(
struct ifreq));
81 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
83 sin = (
struct sockaddr_in*)&ifr.ifr_netmask;
84 inet_pton(AF_INET, netmask, &sin->sin_addr);
85 ifr.ifr_addr.sa_family = AF_INET;
87 ABORT_IF(ioctl(sock, SIOCSIFNETMASK, &ifr) == -1,
"Could not set net mask",
true);
89 LOG(
"Set device Net Mask to " << netmask);
94 SetIpv6(
const char* deviceName,
const char* ip,
int netprefix)
97 struct sockaddr_in6 sin;
100 int sock = socket(AF_INET6, SOCK_DGRAM, 0);
101 memset(&ifr, 0,
sizeof(
struct ifreq));
102 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
104 ABORT_IF(ioctl(sock, SIOGIFINDEX, &ifr) == -1,
"Could not get interface index",
true);
106 LOG(
"Set device IP v6 address to " << ip);
108 memset(&sin, 0,
sizeof(
struct sockaddr_in6));
109 sin.sin6_family = AF_INET6;
110 inet_pton(AF_INET6, ip, (
void*)&sin.sin6_addr);
113 memcpy((
char*)&ifr6.
ifr6_addr, (
char*)&sin.sin6_addr,
sizeof(
struct in6_addr));
121 ABORT_IF(ioctl(sock, SIOCSIFADDR, &ifr6) == -1,
"Could not set IP v6 address",
true);
123 LOG(
"Set device IP v6 address to " << ip);
131 memset(&ifr, 0,
sizeof(
struct ifreq));
133 ifr.ifr_hwaddr.sa_family = 1;
135 ABORT_IF(ioctl(fd, SIOCSIFHWADDR, &ifr) == -1,
"Could not set MAC address",
true);
136 LOG(
"Set device MAC address to " <<
mac);
144 int sock = socket(AF_INET, SOCK_DGRAM, 0);
146 memset(&ifr, 0,
sizeof(
struct ifreq));
147 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
149 ABORT_IF(ioctl(sock, SIOCGIFFLAGS, &ifr) == -1,
"Could not get flags for interface",
true);
150 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
152 ABORT_IF(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1,
"Could not bring interface up",
true);
171 int fd = open(
"/dev/net/tun", O_RDWR);
172 ABORT_IF(fd == -1,
"Could not open /dev/net/tun",
true);
179 memset(&ifr, 0,
sizeof(
struct ifreq));
181 ifr.ifr_flags = (ifftap ? IFF_TAP : IFF_TUN);
184 ifr.ifr_flags |= IFF_NO_PI;
192 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
195 ABORT_IF(ioctl(fd, TUNSETIFF, (
void*)&ifr) == -1,
"Could not allocate tap device",
true);
197 LOG(
"Allocated TAP device " << deviceName);
212 SetIpv4(deviceName, ip4, netmask);
217 SetIpv6(deviceName, ip6, netprefix);
229 main(
int argc,
char* argv[])
236 char* netmask =
nullptr;
237 char* path =
nullptr;
242 while ((c = getopt(argc, argv,
"vd:i:m:n:I:P:thp:")) != -1)
262 prefix = atoi(optarg);
285 LOG(
"Provided Device Name is \"" << dev <<
"\"");
292 ABORT_IF(ip4 ==
nullptr && ip6 ==
nullptr,
"IP Address is a required argument", 0);
295 ABORT_IF(netmask ==
nullptr,
"Net mask is a required argument", 0);
296 LOG(
"Provided IP v4 Address is \"" << ip4 <<
"\"");
297 LOG(
"Provided IP v4 Net Mask is \"" << netmask <<
"\"");
301 ABORT_IF(prefix == -1,
"Prefix is a required argument", 0);
302 LOG(
"Provided IP v6 Address is \"" << ip6 <<
"\"");
303 LOG(
"Provided IP v6 Prefix is \"" << prefix <<
"\"");
312 ABORT_IF(
mac ==
nullptr,
"MAC Address is a required argument", 0);
313 LOG(
"Provided MAC Address is \"" <<
mac <<
"\"");
320 LOG(
"Provided device Mode is TAP");
324 LOG(
"Provided device Mode is TUN");
332 LOG(
"IFF_NO_PI flag set. Packet Information will be present in the traffic");
346 ABORT_IF(path ==
nullptr,
"path is a required argument", 0);
347 LOG(
"Provided path is \"" << path <<
"\"");
358 int sock =
CreateTap(dev,
mac, tap, pi, ip4, netmask, ip6, prefix);
359 ABORT_IF(sock == -1,
"main(): Unable to create tap socket", 1);
#define LOG(x)
Log to std::cout.
void CopyTo(uint8_t buffer[6]) const
#define ABORT_IF(cond, msg, printErrno)
void SendSocket(const char *path, int fd, const int magic_number)
Send the file descriptor back to the code that invoked the creation.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool gVerbose
Flag to enable / disable verbose log mode.
Struct holding IPv6 address data.
int32_t ifr6_ifindex
interface index
uint32_t ifr6_prefixlen
IPv6 prefix length.
struct in6_addr ifr6_addr
IPv6 address.
void SetIpv6(const char *deviceName, const char *ip, int netprefix)
void SetIpv4(const char *deviceName, const char *ip, const char *netmask)
void SetUp(char *deviceName)
int CreateTap(char *deviceName, const char *mac, bool ifftap, bool iffpi, const char *ip4, const char *netmask, const char *ip6, const int netprefix)
void SetMacAddress(int fd, const char *mac)