summaryrefslogtreecommitdiff
path: root/src/win32/tunDevice.cpp
blob: 559d52027a0c2ff439b98caf68573d12d10bd7c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 *  anytun
 *
 *  The secure anycast tunneling protocol (satp) defines a protocol used
 *  for communication between any combination of unicast and anycast
 *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
 *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
 *  ethernet, ip, arp ...). satp directly includes cryptography and
 *  message authentication based on the methodes used by SRTP.  It is
 *  intended to deliver a generic, scaleable and secure solution for
 *  tunneling and relaying of packets of any protocol.
 *
 *
 *  Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, 
 *                          Christian Pointner <satp@wirdorange.org>
 *
 *  This file is part of Anytun.
 *
 *  Anytun is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3 as
 *  published by the Free Software Foundation.
 *
 *  Anytun 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 anytun.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>
#include <sstream>

#include "../tunDevice.h"
#include "../threadUtils.hpp"
#include "../log.h"

#include "common.h"
#include <windows.h>
#include <winioctl.h>

TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifcfg_lp, std::string ifcfg_rnmp) : conf_(dev_name, dev_type, ifcfg_lp, ifcfg_rnmp, 1400)
{
  handle_ = INVALID_HANDLE_VALUE;

  HKEY key, key2;
  LONG err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &key);
  if(err) {
    std::stringstream msg;
    msg << "Unable to read registry: " << LogErrno(err);
    throw std::runtime_error(msg.str());
  }

  bool found = false;
  DWORD len;
  char adapterid[1024];
  char adaptername[1024];
  for(int i=0; ; ++i) {
    len = sizeof(adapterid);
		if(RegEnumKeyEx(key, i, adapterid, &len, 0, 0, 0, NULL))
			break;
    
    std::stringstream regpath;
    regpath << NETWORK_CONNECTIONS_KEY << "\\" << adapterid << "\\Connection";
    err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath.str().c_str(), 0, KEY_READ, &key2);
    if(err) {
//      cLog.msg(Log::PRIO_ERR) << "Error RegOpenKeyEx: " << LogErrno(err);
      continue;
    }
    len = sizeof(adaptername);
    err = RegQueryValueEx(key2, "Name", 0, 0, (LPBYTE)adaptername, &len);
		RegCloseKey(key2);
    if(err) {
//			cLog.msg(Log::PRIO_ERR) << "Error RegQueryValueEx: " << LogErrno(err);
      continue;
    }
//    cLog.msg(Log::PRIO_DEBUG) << "adapter[" << i << "]: " << adapterid << " " << adaptername;
    if(!strncmp(adaptername, "anytun", len)) {
      found = true;
      break;
    }
  }
  RegCloseKey(key);
  
  if(!found)
    throw std::runtime_error("can't find any suitable device");

  std::stringstream tapname;
	tapname << USERMODEDEVICEDIR << adapterid << TAPSUFFIX;
  handle_ = CreateFile(tapname.str().c_str(), GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
  if(handle_ == INVALID_HANDLE_VALUE) {
    std::stringstream msg;
    msg << "Unable to open device: " << adapterid << " (" << adaptername << "): " << LogErrno(GetLastError());
    throw std::runtime_error(msg.str());
	}

  conf_.type_ = TYPE_TAP;
  actual_name_ = adapterid;

  int status = true;
  if(!DeviceIoControl(handle_, TAP_IOCTL_SET_MEDIA_STATUS, &status, sizeof(status), &status, sizeof(status), &len, NULL)) {
    std::stringstream msg;
    msg << "Unable to set device media status: " << LogErrno(GetLastError());
    throw std::runtime_error(msg.str());
	}

  if(ifcfg_lp != "" && ifcfg_rnmp != "")
    do_ifconfig();
}

TunDevice::~TunDevice()
{
  CloseHandle(handle_);
}

int TunDevice::fix_return(int ret, size_t pi_length)
{
// nothing to be done here
	return 0;
}

int TunDevice::read(u_int8_t* buf, u_int32_t len)
{
  DWORD lenout;
  OVERLAPPED overlapped;
  overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  overlapped.Offset = 0;
	overlapped.OffsetHigh = 0;
//  ResetEvent(overlapped.hEvent);
  
  if(!ReadFile(handle_, buf, len, &lenout, &overlapped)) {
    DWORD err = GetLastError();
    if(err == ERROR_IO_PENDING) {
      WaitForSingleObject(overlapped.hEvent, INFINITE);
      if(!GetOverlappedResult(handle_, &overlapped, &lenout, FALSE)) {
        cLog.msg(Log::PRIO_ERR) << "Error while trying to get overlapped result: " << LogErrno(GetLastError());
        return -1;
      }
    }
    else {
      cLog.msg(Log::PRIO_ERR) << "Error while reading from device: " << LogErrno(GetLastError());
      return -1;
    }
  }
  return lenout;
}

int TunDevice::write(u_int8_t* buf, u_int32_t len)
{
  DWORD lenout;
  OVERLAPPED overlapped;
  overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  overlapped.Offset = 0;
	overlapped.OffsetHigh = 0;
//  ResetEvent(overlapped.hEvent);

	if(!WriteFile(handle_, buf, len, &lenout, &overlapped)) {
    DWORD err = GetLastError();
    if(err == ERROR_IO_PENDING) {
      WaitForSingleObject(overlapped.hEvent, INFINITE);
      if(!GetOverlappedResult(handle_, &overlapped, &lenout, FALSE)) {
        cLog.msg(Log::PRIO_ERR) << "Error while trying to get overlapped result: " << LogErrno(GetLastError());
        return -1;
      }
    }
    else {
      cLog.msg(Log::PRIO_ERR) << "Error while writing to device: " << LogErrno(GetLastError());
      return -1;
    }
  }
  return lenout;	
}

void TunDevice::init_post()
{
// nothing to be done here
}

void TunDevice::do_ifconfig()
{

}