The Linux Channel

Videos :: Online Course - Linux TUN/TAP virtual network interfaces

Written by: Kiran Kankipati
Published: 25-Jan-2017

 

TAP Interface sample access - Linux TUN/TAP virtual network interfaces - Episode2

Download this episode my entire kernel module sample code, make file, clean script HERE. And here is the source code for a quick reference.

/* tap_access.c - tap interface demo access
 * The Linux Channel
 * Author: Kiran Kankipati
 * Updated: 25-jan-2017
 */
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <netpacket/packet.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <malloc.h>
#include <net/ethernet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <signal.h>


static int tap_open(char *dev)
{   struct ifreq ifr;
    int fd;

    if ((fd = open("/dev/net/tun", O_RDWR)) < 0) { return -1; }

    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
    if (*dev) strncpy(ifr.ifr_name, dev, IFNAMSIZ);
    if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) { goto failed; } 

    strcpy(dev, ifr.ifr_name); return fd;

failed:
    close(fd); return -1;
}


int tapfd;
void signal_handler(int sig) { close(tapfd); exit(1); }

int main()
{	unsigned char buf[2000];
	unsigned char name[20];
	int i;
	int numbytes=0;
	
	
	strcpy(name,"tapint"); tapfd=tap_open(name); 
	if(tapfd<0) { printf("port open error!\n"); return 0; }

signal(SIGHUP,signal_handler);
signal(SIGTERM,signal_handler);
signal(SIGKILL,signal_handler);
signal(SIGSTOP,signal_handler);
signal(SIGINT,signal_handler);

repeat:	
	numbytes=read(tapfd, buf, 2000);
	if(numbytes<=0) { usleep(100); goto repeat;}
	
	printf("\tTAP Data:");
	for (i=0; i<numbytes; i++) printf("%02x:", buf[i]);
	printf("\n\n");
   
	goto repeat;	

	close(tapfd);
	return 0;
}

Linux TUN/TAP virtual network interfaces - Introduction Episode1


Suggested Topics:

Linux (user-space), Systems Architecture and Networking
Ethtool - Source Code Walk
30-Sep-2017
IPUtils - Source Code Walk
21-Aug-2017
net-tools - Source Code Walk
09-Sep-2017
Oracle VM VirtualBox
27-Mar-2017
Code Snippets
16-Mar-2017
CPU Performance and Benchmarks
13-Mar-2017
Online Course - Linux TUN/TAP virtual network interfaces
25-Jan-2017
Networking and Q&A
27-Aug-2016
Online Course - Networking Protocols
04-Jul-2016
Online Course - Linux CLI Scripting
04-Jul-2016
Linux (user-space) RAW Socket Programming
27-Aug-2016
Systems Architecture
05-Jul-2016
Wireshark Packet Capture
27-Oct-2016



The Linux Channel - v5.45 :: Updated: 11-Oct-2017 :: © 2017 :: Author: Kiran Kankipati
Your IP: 54.157.81.13 :: Browser: CCBot/2.0 (http://commoncrawl.org/faq/)