Linux implementation of virtual network card Tap/tun Example __linux

Source: Internet
Author: User
Tags assert

http://blog.csdn.net/xuxinyl/article/details/6454119

We are using VMware virtualization software often found that they can be virtual out of a network card, seemingly very magical technology, in fact, under Linux is very simple, there are two virtual devices, Tun point-to-point equipment, tap means Ethernet devices, as a virtual network card driver, tun/ Tap driver data receive and send not directly with the real network card, but through the user state to transfer. Under Linux, to achieve nuclear mentality and user-state data interaction, there are a variety of ways: the general socket to create special sockets, the use of sockets to achieve data interaction, the proc file system to create files to interact with the data, but also the way the device files can be used, Access to device files will invoke the device driver corresponding routines, the device driver itself is the core mentality and user state of an interface, TUN/TAP drive is the use of device files to achieve user state and nuclear mentality of data interaction. #include <unistd.h> #include <stdio.h> #include <curses.h> #include <string.h> #include < assert.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include < signal.h> #include <unistd.h> #include <linux/if_tun.h> #include <netinet/in.h> #include <sys/ ioctl.h> #include <sys/time.h> #include <linux/if.h> #include <netinet/in.h> #include <arpa/ inet.h> #include <errno.h> #include <fcntl.h> int tun_creat (char *dev,int flags) {struct ifreq IFR; int FD , err; Assert (Dev!= NULL); if (FD = open ("/dev/net/tun", O_RDWR) <0)//you can replace it to tapTo create tap device. return FD; memset (&ifr,0,sizeof (IFR)); Ifr.ifr_flags|=flags; if (*dev!= '/0 ') 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; int main () {int tun,ret; char tun_name[ifnamsiz]; unsigned char buf[4096]; tun_name[0]= '/0 '; tun = tun_creat (tun_name,i ff_tap| IFF_NO_PI)//If a TUN device is required, change "Iff_tap" to "Iff_tun" if (tun<0) {perror ("tun_create"); return 1;} printf ("TUN name is%s/ n ", Tun_name); while (1) {unsigned char ip[4]; ret = Read (tun, buf, sizeof (BUF)); if (Ret < 0) break; memcpy (IP, &buf[12], 4); me mcpy (&buf[12], &buf[16], 4); memcpy (&buf[16], IP, 4); BUF[20] = 0; * ((unsigned short*) &buf[22]) + = 8; printf ("Read%d bytes/n", ret); ret = Write (tun, buf, ret); printf ("Write%d bytes/n", ret); return 0; }

Open a different terminal

Routing configuration:

Ifconfig devname 10.0.0.1 up; 10.0.0.1 is the IP address of this virtual network card, up is to activate the network card

Route add-net 10.0.0.2 netmask 255.255.255.255 Dev devname

Ping 10.0.0.2

Start testing

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.