Creating TUNTAP network devices under Linux

Source: Internet
Author: User

Target: Build bridge Virtual Bridge, ETH virtual network card, TUNTAP interface device under Linux. Send messages via ping, using the TUNTAP user application for sending and receiving messages.

Environment: The test environment is VMware, UBUNTU14.

1. Preparatory work.

Install Brctl, Tunctl:

The input brctl and TUNCTL instructions will show the corresponding instructions, enter the appropriate command. Due to the low version of Ubuntu, the apt-get update times is wrong, the same error can be referred to the following link to resolve.

Http://webres.wang/404-not-found-error-apt-get-update-ubuntu/?utm_source=tuicool&utm_medium=referral

Set IP:

The method I use is to ping the virtual network card from VMware by the host, and the corresponding mode needs to be set. Set the host-only mode in VMware and set the Vmnet1 IP in the host to 192.168.0.2, the subnet mask is 255.255.255.0. After that we will set bridge's IP in Linux to 192.168.0.10 as the IP address of the virtual machine.


2. Write the TUNTAP code.

The TAP device is created here instead of the Tun device. Reads and writes the tap through read and write, and prints out the resulting message.

#include <linux/if_tun.h> #include <stdio.h> #include <unistd.h> #include <sys/socket.h># Include <sys/types.h> #include <fcntl.h> #include <sys/ioctl.h> #include <linux/if.h> #include <error.h> #include <string.h>//memset#include <arpa/inet.h>//ntohsint tap_create (char *dev, int        flags); int main (int argc, char *argv[]) {int tap,ret,i,ret_all=0,time=0,pkts=0;        char *tap_name = NULL; unsigned char buf[1024];if (ARGC < 2) {printf ("Please input tap ' s name\n"); return-1;}        if (argc = = 2) {tap_name = argv[1];} Tap = Tap_create (Tap_name, Iff_tap |        IFF_NO_PI);                if (Tap < 0) {perror ("tap_create");        return 1;        } printf ("Tap name is%s\n", tap_name);                while (1) {ret = read (tap, buf, sizeof (BUF)); if (Ret < 0) {printf ("Delay secs\n"); sleep (); continue;} pkts++;ret_all+=ret;printf ("Read all%d bytes%d pkts\n", Ret_all, Pkts), for (i=0;i<ret;i++) {if (i%16==0 && i!=0) printf ("\ n");p rintf ("%02x", Buf[i]);}                                printf ("\ n");                ret = write (tap, buf, ret); printf ("Write%d bytes\n", ret); for (i=0;i<ret;i++) {if (i%16==0 && i!=0) printf ("\ n");p rintf ("%02x", Buf[i]) ;}        printf ("\ n");  } return 0;}     int Tap_create (char *dev, int flags) {struct ifreq IFR;     int FD, err; if (FD = open ("/dev/net/tun", o_rdwr|     O_nonblock)) < 0) return FD;     memset (&AMP;IFR, 0, sizeof (IFR));     Ifr.ifr_flags |= flags;     if (*dev! = ') strncpy (ifr.ifr_name, Dev, ifnamsiz);         if (err = IOCTL (FD, Tunsetiff, (void *) &AMP;IFR)) < 0) {close (FD);     return err;     } strcpy (Dev, ifr.ifr_name); return FD; }


3. Build the Network

The network we build is a virtual bridge br0, connecting Eth0 and tap100. Enter the following code in the shell to establish br0 and eth0:

Brctl ADDBR br0brctl addif br0 eth0ifconfig br0 192.168.0.10 netmask 255.255.255.0 up

Then execute the tap.c file we wrote in another shell terminal, with the following code:

GCC tap.c-o taosudo./tap tap100

program execution, and then write the code in the first shell:

sudo ip link set dev tap100 up  //The code makes the tap device up, I found in the test that if no tap device will not receive data sudo brctl addif br0 tap100

Then start cmd in the host by code:

Ping 192.168.0.11-t


4. Test results



5. Another

The MAC address of the TAP device needs to be set later in the test, followed by the following code:

Use case: Tap_setmac ("tap100", "08:00:11:22:33:44") signed char Tap_setmac (const unsigned char *interface_name, const        unsigned char *str_macaddr) {int ret;        int sock_fd;            struct Ifreq IFR;            unsigned int mac2bit[6];      if (interface_name = = NULL | | str_macaddr = = NULL) {return-1; }//Extract Mac Format sscanf ((char *) str_macaddr, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned int *) &mac2bit[0], (unsi gned int *) &mac2bit[1], (unsigned int *) &mac2bit[2], (unsigned int *) &mac2bit[3], (unsigned int *) &AMP;MAC2BI            T[4], (unsigned int *) &mac2bit[5]);        SOCK_FD = socket (pf_inet, SOCK_DGRAM, 0);        if (SOCK_FD < 0) {return-2;        } sprintf (Ifr.ifr_ifrn.ifrn_name, "%s", interface_name);        ifr.ifr_ifru.ifru_hwaddr.sa_family = 1;      Ifr.ifr_ifru.ifru_hwaddr.sa_data[0] = mac2bit[0]; IFR.IFR_IFRU.IFRU_HWADDR.SA_DATA[1] = mac2bit[1];     IFR.IFR_IFRU.IFRU_HWADDR.SA_DATA[2] = mac2bit[2];      IFR.IFR_IFRU.IFRU_HWADDR.SA_DATA[3] = mac2bit[3];      IFR.IFR_IFRU.IFRU_HWADDR.SA_DATA[4] = mac2bit[4];            IFR.IFR_IFRU.IFRU_HWADDR.SA_DATA[5] = mac2bit[5];        RET = IOCTL (SOCK_FD, SIOCSIFHWADDR, &AMP;IFR);        if (ret! = 0) {return-4;             } close (SOCK_FD);    return 0;   }


Creating TUNTAP network devices under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.