From a21704afd4feba7584ce9ded7c6a4b191916be57 Mon Sep 17 00:00:00 2001 From: victor Date: Sun, 18 Oct 2009 13:28:28 +0000 Subject: [PATCH] the Address inner class git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@437 e16421f0-f15b-0410-abcd-98678b794739 --- datagram.cpp | 4 ++-- datagram.h | 39 +++++++++++++++++++++++++++++++-------- tests/dgramtest.cpp | 8 ++++---- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/datagram.cpp b/datagram.cpp index a81622d..a745d63 100644 --- a/datagram.cpp +++ b/datagram.cpp @@ -114,7 +114,7 @@ std::string sock2str (struct sockaddr_in addr) { return std::string(ipch); } - +/* std::string Datagram::to_string () const { // TODO: pretty-print P2TP std::string addrs = sock2str(addr); char hex[MAXDGRAMSZ*2]; @@ -122,6 +122,6 @@ std::string Datagram::to_string () const { // TODO: pretty-print P2TP sprintf(hex+i*2,"%02x",buf[i]); std::string hexs(hex+offset*2,(length-offset)*2); return addrs + '\t' + hexs; -} +}*/ } diff --git a/datagram.h b/datagram.h index dd2f5dd..2b81140 100644 --- a/datagram.h +++ b/datagram.h @@ -16,6 +16,7 @@ #include #include #include +#include //#include #include #include @@ -31,28 +32,50 @@ typedef int64_t tint; #define TINT_uSEC ((tint)1) #define TINT_NEVER ((tint)0x7fffffffffffffffLL) #define MAXDGRAMSZ 1400 - +#define INVALID_SOCKET -1 + + struct Datagram { - struct sockaddr_in addr; + + struct Address { + struct sockaddr_in addr; + Address() { + memset(&addr,0,sizeof(struct sockaddr_in)); + } + Address(const char* ip, uint16_t port) { + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + inet_aton(ip,&(addr.sin_addr)); + } + Address(uint16_t port) { + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + } + Address(const struct sockaddr_in& address) : addr(address) {} + operator sockaddr_in () {return addr;} + }; + + Address addr; int sock; int offset, length; - uint8_t buf[MAXDGRAMSZ]; - + uint8_t buf[MAXDGRAMSZ*2]; + static int Bind(int port); static void Close(int port); static tint Time(); static int Wait (int sockcnt, int* sockets, tint usec=0); static tint now; - Datagram (int socket, struct sockaddr_in& addr_) : addr(addr_), offset(0), + Datagram (int socket, const Address addr_) : addr(addr_), offset(0), length(0), sock(socket) {} Datagram (int socket) : offset(0), length(0), sock(socket) { - memset(&addr,0,sizeof(struct sockaddr_in)); } int space () const { return MAXDGRAMSZ-length; } int size() const { return length-offset; } std::string str() const { return std::string((char*)buf+offset,size()); } + const uint8_t* operator * () const { return buf+offset; } int Push (const uint8_t* data, int l) { // scatter-gather one day int toc = l0); ASSERT_TRUE(sock2>0); - struct sockaddr_in addr1, addr2; + /*struct sockaddr_in addr1, addr2; addr1.sin_family = AF_INET; addr1.sin_port = htons(10001); addr1.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr2.sin_family = AF_INET; addr2.sin_port = htons(10002); - addr2.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + addr2.sin_addr.s_addr = htonl(INADDR_LOOPBACK);*/ - Datagram send(sock1,addr2); + Datagram send(sock1,Datagram::Address(10002)); send.Push32(1234); send.Send(); -- 2.20.1