diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | anytun.cpp | 70 | ||||
-rw-r--r-- | authAlgo.cpp | 2 | ||||
-rw-r--r-- | authAlgo.h | 4 | ||||
-rw-r--r-- | buffer.cpp | 21 | ||||
-rw-r--r-- | buffer.h | 8 | ||||
-rw-r--r-- | cypher.cpp | 15 | ||||
-rw-r--r-- | cypher.h | 2 | ||||
-rw-r--r-- | tunDevice.cpp | 2 |
9 files changed, 81 insertions, 46 deletions
@@ -48,9 +48,6 @@ buffer.o: buffer.cpp buffer.h cypher.o: cypher.cpp cypher.h buffer.h $(C++) $(CCFLAGS) $< -c -cypher.o: cypher.cpp cypher.h buffer.h - $(C++) $(CCFLAGS) $< -c - authAlgo.o: authAlgo.cpp authAlgo.h buffer.h $(C++) $(CCFLAGS) $< -c @@ -55,37 +55,47 @@ int main(int argc, char* argv[]) for(unsigned int i=0;i<c.getLength();++i) c[i] = i; - 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); - delete dev; - std::cout << "dev destroyed" << 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); - - dev = new TunDevice("tap", "192.168.202.1", "255.255.255.0"); - 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); - delete dev; - std::cout << "dev destroyed" << std::endl; - - sleep(10); - - dev = new TunDevice("tun17", "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); - delete dev; - std::cout << "dev destroyed" << std::endl; - - NullAuthAlgo au; +// 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; + +// dev = new TunDevice("tap", "192.168.202.1", "255.255.255.0"); +// 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); +// delete dev; +// std::cout << "dev destroyed" << std::endl; + +// sleep(10); + +// dev = new TunDevice("tun17", "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); +// delete dev; +// std::cout << "dev destroyed" << std::endl; + return 0; } diff --git a/authAlgo.cpp b/authAlgo.cpp index 90fc4a2..57c9ee6 100644 --- a/authAlgo.cpp +++ b/authAlgo.cpp @@ -30,7 +30,7 @@ #include "authAlgo.h" -auth_tag_t NullAuthAlgo::calc(Buffer& buf) +auth_tag_t NullAuthAlgo::calc(const Buffer& buf) { return 0; } @@ -40,13 +40,13 @@ public: AuthAlgo() {}; virtual ~AuthAlgo() {}; - virtual auth_tag_t calc(Buffer& buf) = 0; + virtual auth_tag_t calc(const Buffer& buf) = 0; }; class NullAuthAlgo : AuthAlgo { public: - auth_tag_t calc(Buffer& buf); + auth_tag_t calc(const Buffer& buf); }; #endif @@ -28,10 +28,10 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include <stdexcept> #include <string> #include "datatypes.h" - #include "buffer.h" Buffer::Buffer() : buf_(0), length_(0) @@ -115,7 +115,24 @@ u_int8_t* Buffer::getBuf() return buf_; } -Buffer::operator u_int8_t*( ) +u_int8_t& Buffer::operator[](u_int32_t index) +{ + if(index >= length_) + throw std::out_of_range("buffer::operator[]"); + + return buf_[index]; +} + +u_int8_t const& Buffer::operator[](u_int32_t index) const +{ + if(index >= length_) + throw std::out_of_range("buffer::operator[] const"); + + return buf_[index]; +} + +Buffer::operator u_int8_t*() // just for write/read tun { return buf_; } + @@ -31,6 +31,8 @@ #ifndef _BUFFER_H_ #define _BUFFER_H_ +class TunDevice; + class Buffer { public: @@ -44,13 +46,15 @@ public: u_int32_t resize(u_int32_t new_length); u_int32_t getLength() const; u_int8_t* getBuf(); - operator u_int8_t*( ); + u_int8_t& operator[](u_int32_t index); + u_int8_t const& operator[](u_int32_t index) const; protected: + operator u_int8_t*(); // just for write/read tun + friend class TunDevice; u_int8_t *buf_; u_int32_t length_; }; - #endif @@ -28,6 +28,8 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include <stdexcept> + #include "datatypes.h" #include "cypher.h" @@ -35,16 +37,19 @@ void Cypher::cypher(Buffer& buf) { Buffer stream = getBitStream(buf.getLength()); - calc(buf, stream, buf.getLength()); + exor(buf, stream); } -void Cypher::calc(u_int8_t* buf, u_int8_t* bit_stream, u_int32_t length) +void Cypher::exor(Buffer& buf, const Buffer& bit_stream) { - for(u_int32_t i; i<length; ++i) - buf[i] ^= bit_stream[i]; + try + { + for(u_int32_t i; i<buf.getLength(); ++i) + buf[i] ^= bit_stream[i]; + } + catch(std::out_of_range& o) {} } - Buffer NullCypher::getBitStream(u_int32_t length) { Buffer buf(length); @@ -42,7 +42,7 @@ public: void cypher(Buffer& buf); protected: - void calc(u_int8_t* buf, u_int8_t* bit_stream, u_int32_t length); + void exor(Buffer& buf, const Buffer& bit_stream); virtual Buffer getBitStream(u_int32_t length) = 0; }; diff --git a/tunDevice.cpp b/tunDevice.cpp index 04fcb9a..bfac2a4 100644 --- a/tunDevice.cpp +++ b/tunDevice.cpp @@ -110,6 +110,8 @@ int TunDevice::read(Buffer& buf) if(!dev_) return -1; + perf_push (PERF_READ_IN_TUN); + return read_tun(dev_, buf, buf.getLength()); } |