19 #include <arpa/inet.h>
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
34 #include <linux/if_tun.h>
35 #include <net/route.h>
36 #include <netinet/in.h>
40 #define TAP_MAGIC 95549
45 #define ASCII_DOT (0x2e)
46 #define ASCII_ZERO (0x30)
47 #define ASCII_a (0x41)
48 #define ASCII_z (0x5a)
49 #define ASCII_A (0x61)
50 #define ASCII_Z (0x7a)
51 #define ASCII_COLON (0x3a)
52 #define ASCII_ZERO (0x30)
60 struct in6_addr ifr6_addr;
86 while (*str != 0 && i < 6)
114 SetIpv4 (
const char *deviceName,
const char *ip,
const char *netmask)
117 struct sockaddr_in *sin;
119 int sock = socket(AF_INET, SOCK_DGRAM, 0);
124 memset(&ifr, 0,
sizeof(
struct ifreq));
125 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
127 sin = (
struct sockaddr_in*) &ifr.ifr_addr;
128 inet_pton(AF_INET, ip, &sin->sin_addr);
129 ifr.ifr_addr.sa_family = AF_INET;
131 ABORT_IF (ioctl (sock, SIOCSIFADDR, &ifr) == -1,
132 "Could not set IP address",
true);
134 LOG (
"Set device IP address to " << ip);
139 memset(&ifr, 0,
sizeof(
struct ifreq));
140 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
142 sin = (
struct sockaddr_in*) &ifr.ifr_netmask;
143 inet_pton(AF_INET, netmask, &sin->sin_addr);
144 ifr.ifr_addr.sa_family = AF_INET;
146 ABORT_IF (ioctl (sock, SIOCSIFNETMASK, &ifr) == -1,
147 "Could not set net mask",
true);
149 LOG (
"Set device Net Mask to " << netmask);
154 SetIpv6 (
const char* deviceName,
const char *ip,
int netprefix)
157 struct sockaddr_in6 sin;
160 int sock = socket(AF_INET6, SOCK_DGRAM, 0);
161 memset(&ifr, 0,
sizeof(
struct ifreq));
162 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
164 ABORT_IF (ioctl (sock, SIOGIFINDEX, &ifr) == -1,
165 "Could not get interface index",
true);
167 LOG (
"Set device IP v6 address to " << ip);
169 memset(&sin, 0,
sizeof(
struct sockaddr_in6));
170 sin.sin6_family = AF_INET6;
171 inet_pton(AF_INET6, ip, (
void *) &sin.sin6_addr);
174 memcpy((
char *) &ifr6.
ifr6_addr, (
char *) &sin.sin6_addr,
sizeof(
struct in6_addr));
182 ABORT_IF (ioctl (sock, SIOCSIFADDR, &ifr6) == -1,
183 "Could not set IP v6 address",
true);
185 LOG (
"Set device IP v6 address to " << ip);
194 memset(&ifr, 0,
sizeof(
struct ifreq));
196 ifr.ifr_hwaddr.sa_family = 1;
198 ABORT_IF (ioctl (fd, SIOCSIFHWADDR, &ifr) == -1,
"Could not set MAC address",
true);
199 LOG (
"Set device MAC address to " <<
mac);
207 int sock = socket(AF_INET, SOCK_DGRAM, 0);
209 memset(&ifr, 0,
sizeof(
struct ifreq));
210 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
212 ABORT_IF (ioctl (sock, SIOCGIFFLAGS, &ifr) == -1,
213 "Could not get flags for interface",
true);
214 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
216 ABORT_IF (ioctl (sock, SIOCSIFFLAGS, &ifr) == -1,
217 "Could not bring interface up",
true);
219 LOG (
"Device is up");
224 CreateTap (
char *deviceName,
const char *
mac,
const int ifftap,
const int iffpi,
225 const char *ip4,
const char *netmask,
const char *ip6,
const int netprefix)
231 int fd = open (
"/dev/net/tun", O_RDWR);
232 ABORT_IF (fd == -1,
"Could not open /dev/net/tun",
true);
239 memset(&ifr, 0,
sizeof(
struct ifreq));
241 ifr.ifr_flags = (ifftap ? IFF_TAP : IFF_TUN);
244 ifr.ifr_flags |= IFF_NO_PI;
252 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
256 ABORT_IF (ioctl (fd, TUNSETIFF, (
void *) &ifr) == -1,
257 "Could not allocate tap device",
true);
259 LOG (
"Allocated TAP device " << deviceName);
274 SetIpv4(deviceName, ip4, netmask);
279 SetIpv6(deviceName, ip6, netprefix);
292 main (
int argc,
char *argv[])
299 char *netmask = NULL;
305 while ((c = getopt (argc, argv,
"vd:i:m:n:I:P:thp:")) != -1)
325 prefix = atoi(optarg);
348 LOG (
"Provided Device Name is \"" << dev <<
"\"");
355 ABORT_IF (ip4 == NULL && ip6 == NULL,
"IP Address is a required argument", 0);
358 ABORT_IF (netmask == NULL,
"Net mask is a required argument", 0);
359 LOG (
"Provided IP v4 Address is \"" << ip4 <<
"\"");
360 LOG (
"Provided IP v4 Net Mask is \"" << netmask <<
"\"");
364 ABORT_IF (prefix == -1,
"Prefix is a required argument", 0);
365 LOG (
"Provided IP v6 Address is \"" << ip6 <<
"\"");
366 LOG (
"Provided IP v6 Prefix is \"" << prefix <<
"\"");
375 ABORT_IF (
mac == NULL,
"MAC Address is a required argument", 0);
376 LOG (
"Provided MAC Address is \"" <<
mac <<
"\"");
383 LOG (
"Provided device Mode is TAP");
387 LOG (
"Provided device Mode is TUN");
395 LOG (
"IFF_NO_PI flag set. Packet Information will be present in the traffic");
409 ABORT_IF (path == NULL,
"path is a required argument", 0);
410 LOG (
"Provided path is \"" << path <<
"\"");
420 LOG (
"Creating Tap");
421 int sock =
CreateTap (dev,
mac, tap, pi, ip4, netmask, ip6, prefix);
422 ABORT_IF (sock == -1,
"main(): Unable to create tap socket", 1);
#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.
int gVerbose
Flag to enable / disable verbose log mode.
static char AsciiToLowCase(char c)
Converts a char to lower case.
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 AsciiToMac48(const char *str, uint8_t addr[6])
void SetUp(char *deviceName)
void SetMacAddress(int fd, const char *mac)
int CreateTap(char *deviceName, const char *mac, const int ifftap, const int iffpi, const char *ip4, const char *netmask, const char *ip6, const int netprefix)