Skip to content

Instantly share code, notes, and snippets.

@AndreaCrotti
Created October 24, 2010 17:17
Show Gist options
  • Select an option

  • Save AndreaCrotti/643690 to your computer and use it in GitHub Desktop.

Select an option

Save AndreaCrotti/643690 to your computer and use it in GitHub Desktop.
/***************************************************************************
* Copyright (C) 2007 by Jahn Bertsch *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef TUNDEVICE_H_
#define TUNDEVICE_H_
/**
* \file
* Creates a tun device to be able to grab data from the upper layer which is
* destined for the network card.
*/
//FIXME: check if all of these are needed
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h> // required by if.h
#include <sys/types.h>
#include <string.h>
#include <linux/if.h> // IFNAMSIZ
#include <linux/if_tun.h>
#include <unistd.h>
#include <net/if_arp.h>
#include <net/route.h>
#include <errno.h>
// for packet display
#include <net/ethernet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <iostream>
#include "defines.h"
/**
* Represents a tun decive.
*/
class TunDevice
{
public:
TunDevice(const char *dev, const char *ip);
~TunDevice();
void tunRead(char *buffer, int bufferSize, ssize_t *bytesRead);
ssize_t tunWrite(const void *buf, size_t len);
int getFd();
void getIp(in_addr *ip);
void setCatchAllDefaultGateway();
void restoreDefaultGateway();
private:
int fd;
in_addr ip;
char deviceName[IFNAMSIZ];
struct rtentry defaultGateway;
void createDevice(const char *dev);
};
#endif /*TUNDEVICE_H_*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment