Initial Debian packaging; add src/include.mk
[debian/uanytun.git] / src / tun_helper.h
1 /*
2  *  uAnytun
3  *
4  *  uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
5  *  featured implementation uAnytun has no support for multiple connections
6  *  or synchronisation. It is a small single threaded implementation intended
7  *  to act as a client on small platforms.
8  *  The secure anycast tunneling protocol (satp) defines a protocol used
9  *  for communication between any combination of unicast and anycast
10  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
11  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
12  *  ethernet, ip, arp ...). satp directly includes cryptography and
13  *  message authentication based on the methodes used by SRTP.  It is
14  *  intended to deliver a generic, scaleable and secure solution for
15  *  tunneling and relaying of packets of any protocol.
16  *  
17  *
18  *  Copyright (C) 2007-2008 Christian Pointner <equinox@anytun.org>
19  *
20  *  This file is part of uAnytun.
21  *
22  *  uAnytun is free software: you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License version 3 as
24  *  published by the Free Software Foundation.
25  *
26  *  uAnytun is distributed in the hope that it will be useful,
27  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
28  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  *  GNU General Public License for more details.
30  *
31  *  You should have received a copy of the GNU General Public License
32  *  along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
33  */
34
35 #ifndef _TUN_HELPER_H_
36 #define _TUN_HELPER_H_
37
38 #include <string.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42
43 void tun_conf(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_addr, u_int16_t ifcfg_prefix, u_int16_t mtu)
44 {
45   if(!dev) return;
46
47   dev->mtu_ = mtu;
48   dev->type_ = TYPE_UNDEF;
49   if(dev_type) {
50     if(!strncmp(dev_type, "tun", 3))
51       dev->type_ = TYPE_TUN;
52     else if (!strncmp(dev_type, "tap", 3))
53       dev->type_ = TYPE_TAP;
54   }
55   else if(dev_name) {
56     if(!strncmp(dev_name, "tun", 3))
57       dev->type_ = TYPE_TUN;
58     else if(!strncmp(dev_name, "tap", 3))
59       dev->type_ = TYPE_TAP;
60   }
61
62   dev->net_addr_ = NULL;
63   dev->net_mask_ = NULL;
64   dev->prefix_length_ = 0;
65   if(ifcfg_addr) {
66     dev->net_addr_ = strdup(ifcfg_addr);
67     dev->prefix_length_ = ifcfg_prefix;
68
69     u_int32_t mask = 0;
70     u_int16_t i = 0;
71     for(i = 0; i < ifcfg_prefix; ++i) {
72       mask = mask >> 1;
73       mask |= 0x80000000L;
74     }
75     struct in_addr addr;
76     addr.s_addr = ntohl(mask);
77     dev->net_mask_ = strdup(inet_ntoa(addr));
78   }
79 }
80
81
82 int tun_fix_return(int ret, size_t pi_length)
83 {
84   if(ret < 0)
85     return ret;
86
87   return ((size_t)ret > pi_length ? (ret - pi_length) : 0);
88 }
89
90 const char* tun_get_type_string(tun_device_t* dev)
91 {
92   if(!dev || dev->fd_ < 0)
93     return "";
94   
95   switch(dev->type_)
96   {
97   case TYPE_UNDEF: return "undef"; break;
98   case TYPE_TUN: return "tun"; break;
99   case TYPE_TAP: return "tap"; break;
100   }
101   return "";
102 }
103
104
105
106 #endif