Linux Tun/tap

Source: Internet
Author: User

Description

TUN/TAP provides packet reception and transmission for user space programs.

It can be seen as a simple point-to-point or Ethernet device, which,

Instead of receiving packets from physical media, receives them from

User space program and instead of sending packets via physical media

Writes them to the user space program.

/* demo.c */#include  <fcntl.h>   #include  <string.h>  # include <stdio.h>   #include  <stdlib.h>  #include  <sys/ioctl.h>   #include  <sys/types.h> #include  <sys/socket.h> #include  <linux/if.h># Include <linux/if_tun.h>int tun_open (Char *devname) {    struct  ifreq ifr;    int fd, err;    if  (  (fd =  open ("/dev/net/tun",  o_rdwr)  == -1 )  {         perror ("Open /dev/net/tun");        return -1;     }    memset (&ifr, 0, sizeof (IFR));     ifr.ifr_flags = iff_tun;    strncpy (ifr.ifr_name, devname,  IFNAMSIZ);           if  (  (Err = ioctl (fd, tunsetiff,  (void  *) (&NBSP;&AMP;IFR))  == -1 )  {        perror (" Ioctl tunsetiff ");         close (FD);     &NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;-1;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;RETURN&NBSP;FD;} Int main (INT&NBSP;ARGC,&NBSP;CHAR&NBSP;**ARGV) {    int i, fd, nbytes;     char buf[2048];    fd = tun_open ("Tun0")  ;     printf ("device tun0 opened\n");     while (1)  {         nbytes = read (fd, buf, sizeof (BUF));         printf ("read %d bytes from tun0\n",  nbytes);        &nbsP;for ( i = 0; i < nbytes; i++)              printf ("%02x ",  buf[i]);           printf ("\ n");     }    return 0;}


Terminal1:

[[email protected] sf_share]$ make Demo

CC Demo.c-o Demo

[Email protected] sf_share]$./demo

Device Tun0 opened


Terminal2:

[Email protected] sf_share]$ ifconfig tun0 172.16.0.0/16 up

[[email protected] sf_share]$ Ping 172.16.66.88


Termina 1 OutPut:

Read bytes from Tun0

In the XX, XX, the 7e ffffff87 Ffffffac, the FFFFFFAC of the FFFFFFDC 0c, and the FF FFFFB2, Ffffffe2 ffffffb2, XX, XX, 22, 21, 1a 1b 1c 1d 1e 1f (20), 2 3 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37

Read bytes from Tun0

xx xx (XX)--------7e ffffff87 Ffffffac, FFFFFFAC, 2b FFFFFFDC 0c FFFFB3, ffffffbb ffffffb1, XX, XX, 22, 21, 1a 1b 1c 1d 1e 1f (20), 2 3 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37

Read bytes from Tun0

xx xx (XX)--------7e ffffff87 Ffffffac, FFFFFFAC, 5f ffffffdb 0c FFFFB4, ffffff86 ffffffb1, XX, XX, 22, 21, 1a 1b 1c 1d 1e 1f (20), 2 3 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37


Linux Tun/tap

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.