﻿<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.ucis.nl/index.php?action=history&amp;feed=atom&amp;title=Projects%2FSoftware%2FTunTapIO%2Ftuntap.c</id>
	<title>Projects/Software/TunTapIO/tuntap.c - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.ucis.nl/index.php?action=history&amp;feed=atom&amp;title=Projects%2FSoftware%2FTunTapIO%2Ftuntap.c"/>
	<link rel="alternate" type="text/html" href="http://wiki.ucis.nl/index.php?title=Projects/Software/TunTapIO/tuntap.c&amp;action=history"/>
	<updated>2026-05-31T13:37:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.38.4</generator>
	<entry>
		<id>http://wiki.ucis.nl/index.php?title=Projects/Software/TunTapIO/tuntap.c&amp;diff=156&amp;oldid=prev</id>
		<title>Admin: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="http://wiki.ucis.nl/index.php?title=Projects/Software/TunTapIO/tuntap.c&amp;diff=156&amp;oldid=prev"/>
		<updated>2022-11-09T14:49:54Z</updated>

		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 16:49, 9 November 2022&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
	<entry>
		<id>http://wiki.ucis.nl/index.php?title=Projects/Software/TunTapIO/tuntap.c&amp;diff=155&amp;oldid=prev</id>
		<title>Admin: Nieuwe pagina aangemaakt met '&lt;pre&gt; /********************************************************************** * ttio - tun/tap to stdin/out proxy                                   * * Written by: Ivo...'</title>
		<link rel="alternate" type="text/html" href="http://wiki.ucis.nl/index.php?title=Projects/Software/TunTapIO/tuntap.c&amp;diff=155&amp;oldid=prev"/>
		<updated>2009-04-22T23:32:21Z</updated>

		<summary type="html">&lt;p&gt;Nieuwe pagina aangemaakt met &amp;#039;&amp;lt;pre&amp;gt; /********************************************************************** * ttio - tun/tap to stdin/out proxy                                   * * Written by: Ivo...&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
/**********************************************************************&lt;br /&gt;
* ttio - tun/tap to stdin/out proxy                                   *&lt;br /&gt;
* Written by: Ivo Smits &amp;lt;Ivo@UFO-Net.nl&amp;gt;                              *&lt;br /&gt;
*                                                                     *&lt;br /&gt;
* Compile using: gcc tuntap.c -o ttio                                 *&lt;br /&gt;
*                                                                     *&lt;br /&gt;
* Based on PigeonWare (http://www.blug.linux.no/rfc1149/)             *&lt;br /&gt;
*                                                                     *&lt;br /&gt;
* Many thanks to:                                                     *&lt;br /&gt;
*  - http://www.blug.linux.no/rfc1149/ (must-see!)                    *&lt;br /&gt;
*  - http://linux.about.com/od/commands/l/blcmdl2_select.htm          *&lt;br /&gt;
*                                                                     *&lt;br /&gt;
**********************************************************************/&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#ifndef HAVE_NETINET_IN_H&lt;br /&gt;
	#include &amp;lt;netinet/in.h&amp;gt;&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;sys/ioctl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;linux/if.h&amp;gt;&lt;br /&gt;
#include &amp;lt;linux/if_tun.h&amp;gt;&lt;br /&gt;
#include &amp;lt;linux/if_ether.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt; &lt;br /&gt;
&lt;br /&gt;
const char* TunTapDev = &amp;quot;/dev/net/tun&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// The main program, this is where all the magic happens&lt;br /&gt;
int main(int argc, char** argv) {&lt;br /&gt;
	int DumpStats = 1;&lt;br /&gt;
	int IncludePLen = 1;&lt;br /&gt;
&lt;br /&gt;
	//Parse command line arguments&lt;br /&gt;
	if (argc &amp;gt; 1) {&lt;br /&gt;
		if (strcmp(argv[1], &amp;quot;-h&amp;quot;) == 0) {&lt;br /&gt;
			fprintf(stderr, &amp;quot;ttio - tun/tap to stdio proxy\n&amp;quot;);&lt;br /&gt;
			fprintf(stderr, &amp;quot;Usage: %s INTF [-tap|-tun] [CLEN] [-pi]\n&amp;quot;, argv[0]);&lt;br /&gt;
			fprintf(stderr, &amp;quot;  INTF:  the name of the network interface\n&amp;quot;);&lt;br /&gt;
			fprintf(stderr, &amp;quot;  -tap:  tap style ethernet tunnel (default)\n&amp;quot;);&lt;br /&gt;
			fprintf(stderr, &amp;quot;  -tun:  point-to-point IP tunnel\n&amp;quot;);&lt;br /&gt;
			fprintf(stderr, &amp;quot;  CLEN:  capture size (should be the same as the mtu, default: %d)\n&amp;quot;, ETH_FRAME_LEN);&lt;br /&gt;
			fprintf(stderr, &amp;quot;  -pi:   include packet information\n&amp;quot;);&lt;br /&gt;
			fprintf(stderr, &amp;quot;Note that the arguments should be in exactly THIS order.\n&amp;quot;);&lt;br /&gt;
			fprintf(stderr, &amp;quot;Report bugs to &amp;lt;Ivo@UFO-Net.nl&amp;gt;\n&amp;quot;);&lt;br /&gt;
			exit(0);&lt;br /&gt;
		}&lt;br /&gt;
	} else {&lt;br /&gt;
		//Application won't run without arguments&lt;br /&gt;
		fprintf(stderr, &amp;quot;Try: %s -h\n&amp;quot;, argv[0]);&lt;br /&gt;
		exit(1);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	struct ifreq ifr;&lt;br /&gt;
	//Clear the ifreq structure&lt;br /&gt;
	memset(&amp;amp;ifr, 0, sizeof(ifr));&lt;br /&gt;
&lt;br /&gt;
	char dev[IFNAMSIZ] = &amp;quot;tap1&amp;quot;;	//Interface name&lt;br /&gt;
	//Copy the interface name from the commandline&lt;br /&gt;
	strcpy(dev, argv[1]);&lt;br /&gt;
	strcpy(ifr.ifr_name, dev);&lt;br /&gt;
&lt;br /&gt;
	//Initial capture length&lt;br /&gt;
	int CaptureLen = ETH_FRAME_LEN;&lt;br /&gt;
&lt;br /&gt;
	{&lt;br /&gt;
		//Default to TAP interface, change only if --tun option is detected&lt;br /&gt;
		short ifrflags = IFF_TAP;&lt;br /&gt;
		if (argc &amp;gt; 2 &amp;amp;&amp;amp; strcmp(argv[2], &amp;quot;-tun&amp;quot;) == 0) {&lt;br /&gt;
			ifrflags = IFF_TUN;&lt;br /&gt;
			CaptureLen = ETH_DATA_LEN;&lt;br /&gt;
		}&lt;br /&gt;
		//Different capture length?&lt;br /&gt;
		if (argc &amp;gt; 3) CaptureLen = atoi(argv[3]);&lt;br /&gt;
		if (CaptureLen &amp;lt; 1) {&lt;br /&gt;
			fprintf(stderr, &amp;quot;Capture length %d invalid!\n&amp;quot;, CaptureLen);&lt;br /&gt;
			exit(1);&lt;br /&gt;
		}&lt;br /&gt;
		fprintf(stderr, &amp;quot;Max packet length: %d\n&amp;quot;, CaptureLen);&lt;br /&gt;
		//Include packet info in output?&lt;br /&gt;
		if (!(argc &amp;gt; 4 &amp;amp;&amp;amp; strcmp(argv[4], &amp;quot;-pi&amp;quot;) == 0)) ifrflags = ifrflags | IFF_NO_PI;&lt;br /&gt;
		//Set ifr flags&lt;br /&gt;
		ifr.ifr_flags = ifrflags;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	#warning &amp;quot;Make sure that dbuf can hold all the captured data&amp;quot;&lt;br /&gt;
	register int Ret; //Return value&lt;br /&gt;
	int TunFD; //Tun/tap stream&lt;br /&gt;
&lt;br /&gt;
	//Try to open the tun/tap device file, exit on failure&lt;br /&gt;
	fprintf(stderr, &amp;quot;Opening %s\n&amp;quot;, TunTapDev);&lt;br /&gt;
	if ((TunFD = open(TunTapDev, O_RDWR)) &amp;lt; 0) {&lt;br /&gt;
		fprintf(stderr, &amp;quot;Failed to open %s: %d\n&amp;quot;, TunTapDev, TunFD);&lt;br /&gt;
		exit(2);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	//Request the interface and set its flags&lt;br /&gt;
	fprintf(stderr, &amp;quot;Requesting device: %s\n&amp;quot;, dev);&lt;br /&gt;
	if (ioctl(TunFD, TUNSETIFF, (void *)&amp;amp;ifr) &amp;lt; 0 ) {&lt;br /&gt;
		fprintf(stderr, &amp;quot;ioctl for device name failed!\n&amp;quot;);&lt;br /&gt;
		close(TunFD);&lt;br /&gt;
		exit(3);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	//Just to make sure..?&lt;br /&gt;
	#warning &amp;quot;Do we really need to read the interface name back from the ifr structure?&amp;quot;&lt;br /&gt;
	strcpy(dev, ifr.ifr_name);&lt;br /&gt;
	fprintf(stderr, &amp;quot;Tunnel interface: %s\n&amp;quot;, dev);&lt;br /&gt;
&lt;br /&gt;
	#warning &amp;quot;Set the interfaces mtu to CaptureLength (-2 if Packet Info flag)&amp;quot;&lt;br /&gt;
	//Run the setup script&lt;br /&gt;
	{&lt;br /&gt;
		char syscmd[256];&lt;br /&gt;
		sprintf(syscmd, &amp;quot;./setup.sh %s&amp;quot;, dev);&lt;br /&gt;
		fprintf(stderr, &amp;quot;Execute: %s\n&amp;quot;, syscmd);&lt;br /&gt;
		system(syscmd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	fprintf(stderr, &amp;quot;Proxy ready for action!\n&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
	fd_set fdsRead, fdsWrite; //FileDescriptor sets for select()&lt;br /&gt;
	unsigned char RBuf[CaptureLen], WBuf[CaptureLen]; //Data buffers&lt;br /&gt;
	int RBufLen = 0, WBufLen = 0; //Packet length&lt;br /&gt;
	int PLen;&lt;br /&gt;
	&lt;br /&gt;
	//Start an infinite loop&lt;br /&gt;
	while (1) {&lt;br /&gt;
		if (WBufLen &amp;lt; 0 || RBufLen &amp;lt; 0) {&lt;br /&gt;
			fprintf(stderr, &amp;quot;WBufLen &amp;lt; 0 or RBufLen &amp;lt; 0 :|\n&amp;quot;);&lt;br /&gt;
			exit(2);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		FD_ZERO(&amp;amp;fdsRead);			//Clear FD set&lt;br /&gt;
		if (RBufLen == 0) FD_SET(TunFD, &amp;amp;fdsRead);	//Add tunnel device file&lt;br /&gt;
		if (WBufLen == 0) FD_SET(0, &amp;amp;fdsRead);		//Add stdin&lt;br /&gt;
&lt;br /&gt;
		FD_ZERO(&amp;amp;fdsWrite);			//Clear FD set&lt;br /&gt;
		if (WBufLen &amp;gt; 0) FD_SET(TunFD, &amp;amp;fdsWrite);	//Add tunnel device file&lt;br /&gt;
		if (RBufLen &amp;gt; 0) FD_SET(1, &amp;amp;fdsWrite);		//Add stdout&lt;br /&gt;
&lt;br /&gt;
		if (select(TunFD + 1, &amp;amp;fdsRead, &amp;amp;fdsWrite, NULL, NULL) &amp;lt; 0) {&lt;br /&gt;
			if (errno == EAGAIN || errno == EINTR) {&lt;br /&gt;
				continue; //Retry select&lt;br /&gt;
			} else {&lt;br /&gt;
				fprintf(stderr, &amp;quot;An unhandled error occured in select: %d&amp;quot;, errno);&lt;br /&gt;
				exit(5);&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		//Read from tun/tap device file&lt;br /&gt;
		if (FD_ISSET(TunFD, &amp;amp;fdsRead) &amp;amp;&amp;amp; RBufLen == 0) {&lt;br /&gt;
			RBufLen = read(TunFD, RBuf, CaptureLen);&lt;br /&gt;
			if (RBufLen == 0) {&lt;br /&gt;
				fprintf(stderr, &amp;quot;End of file on %s\n&amp;quot;, TunTapDev);&lt;br /&gt;
				exit(0);&lt;br /&gt;
			} else if (RBufLen &amp;lt; 0) {&lt;br /&gt;
				fprintf(stderr, &amp;quot;Some error occured while reading from %s: %d\n&amp;quot;, TunTapDev, RBufLen);&lt;br /&gt;
				exit(4);&lt;br /&gt;
			}&lt;br /&gt;
			if (DumpStats != 0) fprintf(stderr, &amp;quot;R: %d\n&amp;quot;, RBufLen);&lt;br /&gt;
		}&lt;br /&gt;
		//Write to tun/tap device file&lt;br /&gt;
		if (FD_ISSET(TunFD, &amp;amp;fdsWrite) &amp;amp;&amp;amp; WBufLen &amp;gt; 0) {&lt;br /&gt;
			write(TunFD, WBuf, WBufLen);&lt;br /&gt;
			if (DumpStats != 0) fprintf(stderr, &amp;quot;W: %d\n&amp;quot;, WBufLen);&lt;br /&gt;
			WBufLen = 0;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		//Read from stdin (FD 0)&lt;br /&gt;
		if (FD_ISSET(0, &amp;amp;fdsRead) &amp;amp;&amp;amp; WBufLen == 0) {&lt;br /&gt;
			if (IncludePLen == 1) {&lt;br /&gt;
				WBufLen = read(0, &amp;amp;PLen, 4);&lt;br /&gt;
			}&lt;br /&gt;
			if (IncludePLen == 0 || PLen &amp;gt; CaptureLen) {&lt;br /&gt;
				PLen = CaptureLen;&lt;br /&gt;
			}&lt;br /&gt;
			if (IncludePLen == 0 || WBufLen &amp;gt; 0) {&lt;br /&gt;
				WBufLen = read(0, WBuf, PLen);&lt;br /&gt;
			}&lt;br /&gt;
			if (WBufLen == 0) {&lt;br /&gt;
				fprintf(stderr, &amp;quot;End of file on stdin\n&amp;quot;);&lt;br /&gt;
				exit(0);&lt;br /&gt;
			} else if (WBufLen &amp;lt; 0) {&lt;br /&gt;
				fprintf(stderr, &amp;quot;Some error occured while reading stdin: %d\n&amp;quot;, WBufLen);&lt;br /&gt;
				exit(4);&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		//Write to stdout (FD 1)&lt;br /&gt;
		if (FD_ISSET(1, &amp;amp;fdsWrite) &amp;amp;&amp;amp; RBufLen &amp;gt; 0) {&lt;br /&gt;
			if (IncludePLen == 1) write(1, &amp;amp;RBufLen, 4);&lt;br /&gt;
			write(1, RBuf, RBufLen);&lt;br /&gt;
			RBufLen = 0;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>