When we use VMware virtualization software, we often find that they can all Virtualize a nic, which seems amazing. In fact, in Linux, there are two kinds of virtual backup, point-to-Point device in Tun. Tap indicates an Ethernet device. It is used as a virtual NIC Driver. Program Data receiving and sending does not directly deal with the real network card, but is transferred through the user State. In Linux, there are multiple ways to achieve interaction between core and user-state data: General socket can be used to create special sockets and data interaction through sockets; you can use the proc file system to create files for Data Interaction. You can also use the device file method to access the device file and call the corresponding routine of the device driver, the device driver itself is an interface between the core State and the user State. The tun/TAP driver uses the device file to realize data interaction between the user State and the core state.# Include <unistd. h> <br/> # include <stdio. h> <br/> # include <curses. h> <br/> # include <string. h> <br/> # include <assert. h> <br/> # include <sys/types. h> <br/> # include <sys/socket. h> <br/> # include <netinet/in. h> <br/> # include <signal. h> <br/> # include <unistd. h> <br/> # include <Linux/if_tun.h> <br/> # include <netinet/in. h> <br/> # include <sys/IOCTL. h> <br/> # include <sys/time. h> <br/> # include <Linux/if. h> <br/> # Include <netinet/in. h> <br/> # include <ARPA/inet. h> <br/> # include <errno. h> <br/> # include <fcntl. h> <br/> int tun_creat (char * Dev, int flags) <br/>{< br/> struct ifreq IFR; <br/> int FD, err; <br/> assert (Dev! = NULL); <br/> If (FD = open ("/dev/NET/TUN", o_rdwr) <0) // you can replace it to tap to create tap device. <br/> return FD; <br/> memset (& IFR, 0, sizeof (IFR); <br/> IFR. ifr_flags | = flags; <br/> If (* Dev! = '/0') <br/> strncpy (IFR. ifr_name, Dev, ifnamsiz); <br/> If (ERR = IOCTL (FD, tunsetiff, (void *) & IFR) <0) <br/>{< br/> close (FD); <br/> return err; <br/>}< br/> strcpy (Dev, IFR. ifr_name); <br/> return FD; <br/>}</P> <p> int main () <br/>{< br/> int Tun, RET; <br/> char tun_name [ifnamsiz]; <br/> unsigned char Buf [4096]; <br/> tun_name [0] = '/0 '; <br/> Tun = tun_creat (tun_name, iff_tap | iff_no_pi); // if you need to configure the Tun device, change "iff_tap" to "iff_tun" <br/> If (Tun <0) <br/>{< br/> perror ("tun_create "); <br/> return 1; <br/>}< br/> printf ("tun name is % s/n", tun_name); <br/> while (1) {<br/> unsigned char IP [4]; </P> <p> ret = read (Tun, Buf, sizeof (BUF )); <br/> If (Ret <0) <br/> break; <br/> memcpy (IP, & Buf [12], 4 ); <br/> memcpy (& Buf [12], & Buf [16], 4); <br/> memcpy (& Buf [16], IP, 4 ); <br/> Buf [20] = 0; <br/> * (unsigned short *) & Buf [22]) + = 8; <br/> printf ("read % d Bytes/N", RET); <br/> ret = write (Tun, Buf, RET ); <br/> printf ("write % d Bytes/N", RET); <br/>}< br/> return 0; <br/>}< br/>
Enable another terminal
Route Configuration:
Ifconfig devname 10.0.0.1 up; // 10.0.0.1 indicates the IP address of the virtual network card, and up indicates activating the network card.
Route add-net 10.0.0.2 netmask implements 255.255.255 Dev devname
Ping 10.0.0.2
Start testing