From fc704485589e2b1841d587e26f74794ac59c00d4 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 18 Jun 2007 01:37:58 +0000 Subject: readded not silly refernce removed really silly refernce some tests with poll() --- anytun.cpp | 74 +++++++++++++++++++++++++++++++++++++---------------------- buffer.cpp | 4 ++-- buffer.h | 4 ++-- tunDevice.cpp | 12 ++++++---- tunDevice.h | 2 +- 5 files changed, 59 insertions(+), 37 deletions(-) diff --git a/anytun.cpp b/anytun.cpp index 75d311a..966c3a8 100644 --- a/anytun.cpp +++ b/anytun.cpp @@ -29,6 +29,7 @@ */ #include +#include #include "datatypes.h" @@ -41,42 +42,59 @@ int main(int argc, char* argv[]) { std::cout << "anytun - secure anycast tunneling protocol" << std::endl; - u_int8_t test[100]; - for(int i=0;i<100;++i) - test[i] = i; +// u_int8_t test[100]; +// for(int i=0;i<100;++i) +// test[i] = i; - Buffer a(test, 100); - Buffer b(a); +// Buffer a(test, 100); +// Buffer b(a); - Buffer c; - c = b; - c.resize(500); +// Buffer c; +// c = b; +// c.resize(500); - for(unsigned int i=0;igetActualName() << "'" << std::endl; -// std::cout << "dev type is '" << dev->getType() << "'" << std::endl; + TunDevice* dev; + dev = new TunDevice("tun", "192.168.200.1", "192.168.201.1"); + std::cout << "dev created (opened)" << std::endl; + std::cout << "dev opened - actual name is '" << dev->getActualName() << "'" << std::endl; + std::cout << "dev type is '" << dev->getType() << "'" << std::endl; -// sleep(10); + sleep(10); -// Buffer inBuf(2000); + Buffer inBuf(2000); -// int32_t len = 0; -// do -// { -// len = dev->read(inBuf); -// std::cout << "read " << len << " bytes" << std::endl; -// } -// while(len >= 0); - -// delete dev; -// std::cout << "dev destroyed" << std::endl; + while(1) + { + short revents = dev->read(inBuf); + if(revents & POLLIN) + std::cout << "POLLIN,"; + else if(revents & POLLRDNORM) + std::cout << "POLLRDNORM,"; + else if(revents & POLLRDBAND) + std::cout << "POLLRDBAND,"; + else if(revents & POLLPRI) + std::cout << "POLLPRI,"; + else if(revents & POLLOUT) + std::cout << "POLLOUT,"; + else if(revents & POLLWRNORM) + std::cout << "POLLWRNORM,"; + else if(revents & POLLWRBAND) + std::cout << "POLLWRBAND,"; + else if(revents & POLLERR) + std::cout << "POLLERR,"; + else if(revents & POLLHUP) + std::cout << "POLLHUP,"; + else if(revents & POLLNVAL) + std::cout << "POLLNVAL,"; + std::cout << std::endl; + } + + delete dev; + std::cout << "dev destroyed" << std::endl; // dev = new TunDevice("tap", "192.168.202.1", "255.255.255.0"); // std::cout << "dev created (opened)" << std::endl; diff --git a/buffer.cpp b/buffer.cpp index 342373a..3f6fe7c 100644 --- a/buffer.cpp +++ b/buffer.cpp @@ -115,7 +115,7 @@ u_int8_t* Buffer::getBuf() return buf_; } -u_int8_t Buffer::operator[](u_int32_t index) +u_int8_t& Buffer::operator[](u_int32_t index) { if(index >= length_) throw std::out_of_range("buffer::operator[]"); @@ -123,7 +123,7 @@ u_int8_t Buffer::operator[](u_int32_t index) return buf_[index]; } -u_int8_t const& Buffer::operator[](u_int32_t index) const +u_int8_t Buffer::operator[](u_int32_t index) const { if(index >= length_) throw std::out_of_range("buffer::operator[] const"); diff --git a/buffer.h b/buffer.h index 6219939..9856f97 100644 --- a/buffer.h +++ b/buffer.h @@ -46,8 +46,8 @@ public: u_int32_t resize(u_int32_t new_length); u_int32_t getLength() const; u_int8_t* getBuf(); - u_int8_t operator[](u_int32_t index); - u_int8_t const& operator[](u_int32_t index) const; + u_int8_t& operator[](u_int32_t index); + u_int8_t operator[](u_int32_t index) const; protected: operator u_int8_t*(); // just for write/read tun diff --git a/tunDevice.cpp b/tunDevice.cpp index bfac2a4..0f17fb7 100644 --- a/tunDevice.cpp +++ b/tunDevice.cpp @@ -29,6 +29,7 @@ */ #include +#include extern "C" { #include "openvpn/config.h" @@ -105,14 +106,17 @@ TunDevice::~TunDevice() close_tun(dev_); } -int TunDevice::read(Buffer& buf) +short TunDevice::read(Buffer& buf) { if(!dev_) return -1; - perf_push (PERF_READ_IN_TUN); - - return read_tun(dev_, buf, buf.getLength()); + struct pollfd pfd[1]; + pfd[0].fd = tun_event_handle(dev_); +// pfd[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI | POLLOUT | POLLWRNORM | POLLWRBAND | POLLERR | POLLHUP | POLLNVAL; + pfd[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI; + poll(pfd, 1, -1); + return pfd[0].revents;//read_tun(dev_, buf, buf.getLength()); } int TunDevice::write(Buffer& buf) diff --git a/tunDevice.h b/tunDevice.h index 245f7e8..6fdcc2d 100644 --- a/tunDevice.h +++ b/tunDevice.h @@ -43,7 +43,7 @@ public: void close(); bool isOpen(); - int read(Buffer& buf); + short read(Buffer& buf); int write(Buffer& buf); char* getActualName(); -- cgit v1.2.3