19 #include <arpa/inet.h> 28 #include <sys/ioctl.h> 29 #include <sys/types.h> 30 #include <sys/socket.h> 33 #include <linux/if_tun.h> 34 #include <net/route.h> 35 #include <netinet/in.h> 39 #define TAP_MAGIC 95549 44 #define ASCII_DOT (0x2e) 45 #define ASCII_ZERO (0x30) 46 #define ASCII_a (0x41) 47 #define ASCII_z (0x5a) 48 #define ASCII_A (0x61) 49 #define ASCII_Z (0x7a) 50 #define ASCII_COLON (0x3a) 51 #define ASCII_ZERO (0x30) 59 struct in6_addr ifr6_addr;
85 while (*str != 0 && i < 6)
113 SetIpv4 (
const char *deviceName,
const char *ip,
const char *netmask)
116 struct sockaddr_in *sin;
118 int sock = socket(AF_INET, SOCK_DGRAM, 0);
123 memset(&ifr, 0,
sizeof(
struct ifreq));
124 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
126 sin = (
struct sockaddr_in*) &ifr.ifr_addr;
127 inet_pton(AF_INET, ip, &sin->sin_addr);
128 ifr.ifr_addr.sa_family = AF_INET;
130 ABORT_IF (ioctl (sock, SIOCSIFADDR, &ifr) == -1,
131 "Could not set IP address",
true);
133 LOG (
"Set device IP address to " << ip);
138 memset(&ifr, 0,
sizeof(
struct ifreq));
139 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
141 sin = (
struct sockaddr_in*) &ifr.ifr_netmask;
142 inet_pton(AF_INET, netmask, &sin->sin_addr);
143 ifr.ifr_addr.sa_family = AF_INET;
145 ABORT_IF (ioctl (sock, SIOCSIFNETMASK, &ifr) == -1,
146 "Could not set net mask",
true);
148 LOG (
"Set device Net Mask to " << netmask);
153 SetIpv6 (
const char* deviceName,
const char *ip,
int netprefix)
156 struct sockaddr_in6 sin;
159 int sock = socket(AF_INET6, SOCK_DGRAM, 0);
160 memset(&ifr, 0,
sizeof(
struct ifreq));
161 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
163 ABORT_IF (ioctl (sock, SIOGIFINDEX, &ifr) == -1,
164 "Could not get interface index",
true);
166 LOG (
"Set device IP v6 address to " << ip);
168 memset(&sin, 0,
sizeof(
struct sockaddr_in6));
169 sin.sin6_family = AF_INET6;
170 inet_pton(AF_INET6, ip, (
void *) &sin.sin6_addr);
173 memcpy((
char *) &ifr6.
ifr6_addr, (
char *) &sin.sin6_addr,
sizeof(
struct in6_addr));
181 ABORT_IF (ioctl (sock, SIOCSIFADDR, &ifr6) == -1,
182 "Could not set IP v6 address",
true);
184 LOG (
"Set device IP v6 address to " << ip);
193 memset(&ifr, 0,
sizeof(
struct ifreq));
195 ifr.ifr_hwaddr.sa_family = 1;
197 ABORT_IF (ioctl (fd, SIOCSIFHWADDR, &ifr) == -1,
"Could not set MAC address",
true);
198 LOG (
"Set device MAC address to " <<
mac);
206 int sock = socket(AF_INET, SOCK_DGRAM, 0);
208 memset(&ifr, 0,
sizeof(
struct ifreq));
209 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
211 ABORT_IF (ioctl (sock, SIOCGIFFLAGS, &ifr) == -1,
212 "Could not get flags for interface",
true);
213 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
215 ABORT_IF (ioctl (sock, SIOCSIFFLAGS, &ifr) == -1,
216 "Could not bring interface up",
true);
218 LOG (
"Device is up");
223 CreateTap (
char *deviceName,
const char *
mac,
const int ifftap,
const int iffpi,
224 const char *ip4,
const char *netmask,
const char *ip6,
const int netprefix)
230 int fd = open (
"/dev/net/tun", O_RDWR);
231 ABORT_IF (fd == -1,
"Could not open /dev/net/tun",
true);
238 memset(&ifr, 0,
sizeof(
struct ifreq));
240 ifr.ifr_flags = (ifftap ? IFF_TAP : IFF_TUN);
243 ifr.ifr_flags |= IFF_NO_PI;
251 strncpy(ifr.ifr_name, deviceName, IFNAMSIZ - 1);
255 ABORT_IF (ioctl (fd, TUNSETIFF, (
void *) &ifr) == -1,
256 "Could not allocate tap device",
true);
258 LOG (
"Allocated TAP device " << deviceName);
273 SetIpv4(deviceName, ip4, netmask);
278 SetIpv6(deviceName, ip6, netprefix);
291 main (
int argc,
char *argv[])
298 char *netmask = NULL;
304 while ((c = getopt (argc, argv,
"vd:i:m:n:I:P:thp:")) != -1)
324 prefix = atoi(optarg);
347 LOG (
"Provided Device Name is \"" << dev <<
"\"");
354 ABORT_IF (ip4 == NULL && ip6 == NULL,
"IP Address is a required argument", 0);
357 ABORT_IF (netmask == NULL,
"Net mask is a required argument", 0);
358 LOG (
"Provided IP v4 Address is \"" << ip4 <<
"\"");
359 LOG (
"Provided IP v4 Net Mask is \"" << netmask <<
"\"");
363 ABORT_IF (prefix == -1,
"Prefix is a required argument", 0);
364 LOG (
"Provided IP v6 Address is \"" << ip6 <<
"\"");
365 LOG (
"Provided IP v6 Prefix is \"" << prefix <<
"\"");
374 ABORT_IF (
mac == NULL,
"MAC Address is a required argument", 0);
375 LOG (
"Provided MAC Address is \"" <<
mac <<
"\"");
382 LOG (
"Provided device Mode is TAP");
386 LOG (
"Provided device Mode is TUN");
394 LOG (
"IFF_NO_PI flag set. Packet Information will be present in the traffic");
408 ABORT_IF (path == NULL,
"path is a required argument", 0);
409 LOG (
"Provided path is \"" << path <<
"\"");
419 LOG (
"Creating Tap");
420 int sock =
CreateTap (dev,
mac, tap, pi, ip4, netmask, ip6, prefix);
421 ABORT_IF (sock == -1,
"main(): Unable to create tap socket", 1);
void SendSocket(const char *path, int fd, const int magic_number)
Send the file descriptor back to the code that invoked the creation.
void SetIpv4(const char *deviceName, const char *ip, const char *netmask)
uint32_t ifr6_prefixlen
IPv6 prefix length.
#define ABORT_IF(cond, msg, printErrno)
struct in6_addr ifr6_addr
IPv6 address.
static char AsciiToLowCase(char c)
Converts a char to lower case.
void SetUp(char *deviceName)
int gVerbose
Flag to enable / disable verbose log mode.
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)
int32_t ifr6_ifindex
interface index
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Struct holding IPv6 address data.
void SetIpv6(const char *deviceName, const char *ip, int netprefix)
void SetMacAddress(int fd, const char *mac)
void AsciiToMac48(const char *str, uint8_t addr[6])