Addition of libbitdht.

============================================================

This is intended to be a completely independent library from RS, 
(hosted at sf.net/projects/bitdht) hence is being commited at the top level.

As initial further development / testing will be driven by RS integration
it is being added to the RS repository. Equally important is ensuring
that RS can compile without requiring aux libraries.

Once libbitdht is further developed, this section of the repository
is expected to be removed... But that will not be for a while, I expect.

drbob.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3276 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2010-07-10 11:48:24 +00:00
parent 9b72977bba
commit c415bb6158
56 changed files with 11344 additions and 0 deletions

View file

@ -0,0 +1,188 @@
/*
* bitdht/udpbitdht.cc
*
* BitDHT: An Flexible DHT library.
*
* Copyright 2010 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 3 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "bitdht@lunamutt.com".
*
*/
#include "udp/udpbitdht.h"
#include "bitdht/bdpeer.h"
#include "bitdht/bdstore.h"
#include "bitdht/bdmsgs.h"
#include "bitdht/bencode.h"
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*
* #define DEBUG_UDP_BITDHT 1
*/
//#define DEBUG_UDP_BITDHT 1
/*************************************/
UdpBitDht::UdpBitDht(UdpPublisher *pub, bdNodeId *id, std::string dhtVersion, std::string bootstrapfile, bdDhtFunctions *fns)
:UdpSubReceiver(pub), mFns(fns)
{
/* setup nodeManager */
mBitDhtManager = new bdNodeManager(id, dhtVersion, bootstrapfile, fns);
}
UdpBitDht::~UdpBitDht()
{
return;
}
/*********** External Interface to the World ************/
/***** Functions to Call down to bdNodeManager ****/
/* Request DHT Peer Lookup */
/* Request Keyword Lookup */
void UdpBitDht::addFindNode(bdNodeId *id, uint32_t mode)
{
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
mBitDhtManager->addFindNode(id, mode);
}
void UdpBitDht::removeFindNode(bdNodeId *id)
{
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
mBitDhtManager->removeFindNode(id);
}
void UdpBitDht::findDhtValue(bdNodeId *id, std::string key, uint32_t mode)
{
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
mBitDhtManager->findDhtValue(id, key, mode);
}
/***** Add / Remove Callback Clients *****/
void UdpBitDht::addCallback(BitDhtCallback *cb)
{
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
mBitDhtManager->addCallback(cb);
}
void UdpBitDht::removeCallback(BitDhtCallback *cb)
{
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
mBitDhtManager->removeCallback(cb);
}
int UdpBitDht::getDhtPeerAddress(bdNodeId *id, struct sockaddr_in &from)
{
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
return mBitDhtManager->getDhtPeerAddress(id, from);
}
int UdpBitDht::getDhtValue(bdNodeId *id, std::string key, std::string &value)
{
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
return mBitDhtManager->getDhtValue(id, key, value);
}
/******************* Internals *************************/
/***** Iteration / Loop Management *****/
/*** Overloaded from UdpSubReceiver ***/
int UdpBitDht::recvPkt(void *data, int size, struct sockaddr_in &from)
{
/* pass onto bitdht */
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
/* check packet suitability */
if (mBitDhtManager->isBitDhtPacket((char *) data, size, from))
{
mBitDhtManager->incomingMsg(&from, (char *) data, size);
return 1;
}
return 0;
}
int UdpBitDht::status(std::ostream &out)
{
out << "UdpBitDht::status()" << std::endl;
return 1;
}
/*** Overloaded from iThread ***/
void UdpBitDht::run()
{
while(1)
{
tick();
sleep(1);
}
}
int UdpBitDht::tick()
{
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
/* pass on messages from the node */
int i = 0;
char data[BITDHT_MAX_PKTSIZE];
struct sockaddr_in toAddr;
int size = BITDHT_MAX_PKTSIZE;
/* accept up to 10 msgs / tick() */
while((i < 10) && (mBitDhtManager->outgoingMsg(&toAddr, data, &size)))
{
#ifdef DEBUG_UDP_BITDHT
std::cerr << "UdpBitDht::tick() outgoing msg(" << size << ") to " << toAddr;
std::cerr << std::endl;
#endif
sendPkt(data, size, toAddr, BITDHT_TTL);
// iterate
i++;
size = BITDHT_MAX_PKTSIZE; // reset msg size!
}
mBitDhtManager->iteration();
return 1;
}

View file

@ -0,0 +1,96 @@
#ifndef UDP_BIT_DHT_CLASS_H
#define UDP_BIT_DHT_CLASS_H
/*
* bitdht/udpbitdht.h
*
* BitDHT: An Flexible DHT library.
*
* Copyright 2010 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 3 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "bitdht@lunamutt.com".
*
*/
#include <iosfwd>
#include <map>
#include <string>
#include "udp/udpstack.h"
#include "bitdht/bdiface.h"
#include "bitdht/bdmanager.h"
/*
* This implements a UdpSubReceiver class to allow the DHT to talk to the network.
* The parser is very strict - and will try to not pick up anyone else's messages.
*
* Mutexes are implemented at this level protecting the whole of the DHT code.
* This class is also a thread - enabling it to do callback etc.
*/
// class BitDhtCallback defined in bdiface.h
class UdpBitDht: public UdpSubReceiver, public bdThread, public BitDhtInterface
{
public:
UdpBitDht(UdpPublisher *pub, bdNodeId *id, std::string dhtVersion, std::string bootstrapfile, bdDhtFunctions *fns);
virtual ~UdpBitDht();
/*********** External Interface to the World (BitDhtInterface) ************/
/***** Functions to Call down to bdNodeManager ****/
/* Request DHT Peer Lookup */
/* Request Keyword Lookup */
virtual void addFindNode(bdNodeId *id, uint32_t mode);
virtual void removeFindNode(bdNodeId *id);
virtual void findDhtValue(bdNodeId *id, std::string key, uint32_t mode);
/***** Add / Remove Callback Clients *****/
virtual void addCallback(BitDhtCallback *cb);
virtual void removeCallback(BitDhtCallback *cb);
/***** Get Results Details *****/
virtual int getDhtPeerAddress(bdNodeId *id, struct sockaddr_in &from);
virtual int getDhtValue(bdNodeId *id, std::string key, std::string &value);
/******************* Internals *************************/
/***** Iteration / Loop Management *****/
/*** Overloaded from UdpSubReceiver ***/
virtual int recvPkt(void *data, int size, struct sockaddr_in &from);
virtual int status(std::ostream &out);
/*** Overloaded from iThread ***/
virtual void run();
/**** do whats to be done ***/
int tick();
private:
bdMutex dhtMtx; /* for all class data (below) */
bdNodeManager *mBitDhtManager;
bdDhtFunctions *mFns;
};
#endif

View file

@ -0,0 +1,124 @@
/*
* bitdht/udpbitdht_nettest.cc
*
* BitDHT: An Flexible DHT library.
*
* Copyright 2010 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 3 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "bitdht@lunamutt.com".
*
*/
#include "udpbitdht.h"
#include "udpstack.h"
#include "bdstddht.h"
#include <string.h>
#include <stdlib.h>
/*******************************************************************
* Test of bencode message creation functions in bdmsgs.cc
*
* Create a couple of each type.
*/
#define MAX_MESSAGE_LEN 10240
int main(int argc, char **argv)
{
/* setup id, and bootstrap file */
if (argc < 2)
{
std::cerr << " Missing port number " << std::endl;
exit(1);
}
int delta = 0;
if (argc < 3)
{
std::cerr << "No Id Delta" << std::endl;
}
else
{
delta = atoi(argv[2]);
}
int port = atoi(argv[1]);
std::cerr << "Using Port: " << port << std::endl;
std::cerr << "Using Delta: " << delta << std::endl;
bdDhtFunctions *fns = new bdStdDht();
bdNodeId id;
memcpy(((char *) id.data), "1234567890abcdefghi", 20);
uint32_t *deltaptr = (uint32_t *) (id.data);
(*deltaptr) += htonl(delta);
std::cerr << "Using NodeId: ";
fns->bdPrintNodeId(std::cerr, &id);
std::cerr << std::endl;
std::string bootstrapfile = "bdboot.txt";
/* setup the udp port */
struct sockaddr_in local;
local.sin_port = htons(port);
UdpStack *udpstack = new UdpStack(local);
/* create bitdht component */
std::string dhtVersion = "dbTEST";
UdpBitDht *bitdht = new UdpBitDht(udpstack, &id, dhtVersion, bootstrapfile, fns);
/* add in the stack */
udpstack->addReceiver(bitdht);
/* register callback display */
bdDebugCallback *cb = new bdDebugCallback();
bitdht->addCallback(cb);
/* startup threads */
//udpstack->start();
bitdht->start();
/* do a couple of random continuous searchs. */
uint32_t mode = BITDHT_QFLAGS_DO_IDLE;
int count = 0;
while(1)
{
sleep(120);
if (++count == 2)
{
/* switch to one-shot searchs */
mode = 0;
}
bdNodeId rndId;
bdStdRandomNodeId(&rndId);
bitdht->addFindNode(&rndId, mode);
}
return 1;
}

View file

@ -0,0 +1,497 @@
/*
* bitdht/udplayer.cc
*
* BitDHT: An Flexible DHT library.
*
* Copyright 2010 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 3 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "bitdht@lunamutt.com".
*
*/
#include "udplayer.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
/*
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
*/
/***
* #define DEBUG_UDP_LAYER 1
***/
#define OPEN_UNIVERSAL_PORT 1
static const int UDP_DEF_TTL = 64;
class udpPacket
{
public:
udpPacket(struct sockaddr_in *addr, void *dta, int dlen)
:raddr(*addr), len(dlen)
{
data = malloc(len);
memcpy(data, dta, len);
}
~udpPacket()
{
if (data)
{
free(data);
data = NULL;
len = 0;
}
}
struct sockaddr_in raddr;
void *data;
int len;
};
std::ostream &operator<<(std::ostream &out, const struct sockaddr_in &addr)
{
out << "[" << inet_ntoa(addr.sin_addr) << ":";
out << htons(addr.sin_port) << "]";
return out;
}
bool operator==(const struct sockaddr_in &addr, const struct sockaddr_in &addr2)
{
if (addr.sin_family != addr2.sin_family)
return false;
if (addr.sin_addr.s_addr != addr2.sin_addr.s_addr)
return false;
if (addr.sin_port != addr2.sin_port)
return false;
return true;
}
bool operator<(const struct sockaddr_in &addr, const struct sockaddr_in &addr2)
{
if (addr.sin_family != addr2.sin_family)
return (addr.sin_family < addr2.sin_family);
if (addr.sin_addr.s_addr != addr2.sin_addr.s_addr)
return (addr.sin_addr.s_addr < addr2.sin_addr.s_addr);
if (addr.sin_port != addr2.sin_port)
return (addr.sin_port < addr2.sin_port);
return false;
}
std::string printPkt(void *d, int size)
{
std::ostringstream out;
out << "Packet:" << "**********************";
for(int i = 0; i < size; i++)
{
if (i % 16 == 0)
out << std::endl;
out << std::hex << std::setw(2) << (unsigned int) ((unsigned char *) d)[i] << " ";
}
out << std::endl << "**********************";
out << std::endl;
return out.str();
}
std::string printPktOffset(unsigned int offset, void *d, unsigned int size)
{
std::ostringstream out;
out << "Packet:" << "**********************";
out << std::endl;
out << "Offset: " << std::hex << offset << " -> " << offset + size;
out << std::endl;
out << "Packet:" << "**********************";
unsigned int j = offset % 16;
if (j != 0)
{
out << std::endl;
out << std::hex << std::setw(6) << (unsigned int) offset - j;
out << ": ";
for(unsigned int i = 0; i < j; i++)
{
out << "xx ";
}
}
for(unsigned int i = offset; i < offset + size; i++)
{
if (i % 16 == 0)
{
out << std::endl;
out << std::hex << std::setw(6) << (unsigned int) i;
out << ": ";
}
out << std::hex << std::setw(2) << (unsigned int) ((unsigned char *) d)[i-offset] << " ";
}
out << std::endl << "**********************";
out << std::endl;
return out.str();
}
UdpLayer::UdpLayer(UdpReceiver *udpr, struct sockaddr_in &local)
:recv(udpr), laddr(local), errorState(0), ttl(UDP_DEF_TTL)
{
openSocket();
return;
}
int UdpLayer::status(std::ostream &out)
{
out << "UdpLayer::status()" << std::endl;
out << "localaddr: " << laddr << std::endl;
out << "sockfd: " << sockfd << std::endl;
out << std::endl;
return 1;
}
int UdpLayer::reset(struct sockaddr_in &local)
{
std::cerr << "UdpLayer::reset()" << std::endl;
/* stop the old thread */
{
bdStackMutex stack(sockMtx); /********** LOCK MUTEX *********/
std::cerr << "UdpLayer::reset() setting stopThread flag" << std::endl;
stopThread = true;
}
std::cerr << "UdpLayer::reset() joining" << std::endl;
join();
std::cerr << "UdpLayer::reset() closing socket" << std::endl;
closeSocket();
std::cerr << "UdpLayer::reset() resetting variables" << std::endl;
laddr = local;
errorState = 0;
ttl = UDP_DEF_TTL;
std::cerr << "UdpLayer::reset() opening socket" << std::endl;
openSocket();
return 1;
}
int UdpLayer::closeSocket()
{
/* close socket if open */
sockMtx.lock(); /********** LOCK MUTEX *********/
if (sockfd > 0)
{
close(sockfd);
}
sockMtx.unlock(); /******** UNLOCK MUTEX *********/
return 1;
}
void UdpLayer::run()
{
return recv_loop();
}
/* higher level interface */
void UdpLayer::recv_loop()
{
int maxsize = 16000;
void *inbuf = malloc(maxsize);
int status;
struct timeval timeout;
while(1)
{
/* select on the socket TODO */
fd_set rset;
for(;;)
{
/* check if we need to stop */
bool toStop = false;
{
bdStackMutex stack(sockMtx); /********** LOCK MUTEX *********/
toStop = stopThread;
}
if (toStop)
{
std::cerr << "UdpLayer::recv_loop() stopping thread" << std::endl;
stop();
}
FD_ZERO(&rset);
FD_SET(sockfd, &rset);
timeout.tv_sec = 0;
timeout.tv_usec = 500000; /* 500 ms timeout */
status = select(sockfd+1, &rset, NULL, NULL, &timeout);
if (status > 0)
{
break; /* data available, go read it */
}
else if (status < 0)
{
std::cerr << "UdpLayer::recv_loop() ";
std::cerr << "Error: " << errno << std::endl;
}
};
int nsize = maxsize;
struct sockaddr_in from;
if (0 < receiveUdpPacket(inbuf, &nsize, from))
{
#ifdef DEBUG_UDP_LAYER
std::cerr << "UdpLayer::readPkt() from : " << from << std::endl;
std::cerr << printPkt(inbuf, nsize);
#endif
// send to reciever.
recv -> recvPkt(inbuf, nsize, from);
}
else
{
#ifdef DEBUG_UDP_LAYER
std::cerr << "UdpLayer::readPkt() not ready" << from;
std::cerr << std::endl;
#endif
}
}
return;
}
int UdpLayer::sendPkt(const void *data, int size, sockaddr_in &to, int ttl)
{
/* if ttl is different -> set it */
if (ttl != getTTL())
{
setTTL(ttl);
}
/* and send! */
#ifdef DEBUG_UDP_LAYER
std::cerr << "UdpLayer::sendPkt() to: " << to << std::endl;
std::cerr << printPkt((void *) data, size);
#endif
sendUdpPacket(data, size, to);
return size;
}
/* setup connections */
int UdpLayer::openSocket()
{
sockMtx.lock(); /********** LOCK MUTEX *********/
/* make a socket */
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
#ifdef DEBUG_UDP_LAYER
std::cerr << "UpdStreamer::openSocket()" << std::endl;
#endif
/* bind to address */
#ifdef UDP_LOOPBACK_TESTING
inet_aton("127.0.0.1", &(laddr.sin_addr));
#endif
#ifdef OPEN_UNIVERSAL_PORT
struct sockaddr_in tmpaddr = laddr;
tmpaddr.sin_addr.s_addr = 0;
if (0 != bind(sockfd, (struct sockaddr *) (&tmpaddr), sizeof(tmpaddr)))
#else
if (0 != bind(sockfd, (struct sockaddr *) (&laddr), sizeof(laddr)))
#endif
{
#ifdef DEBUG_UDP_LAYER
std::cerr << "Socket Failed to Bind to : " << laddr << std::endl;
std::cerr << "Error: " << errno << std::endl;
#endif
errorState = EADDRINUSE;
//exit(1);
sockMtx.unlock(); /******** UNLOCK MUTEX *********/
return -1;
}
if (-1 == fcntl(sockfd, F_SETFL, O_NONBLOCK))
{
#ifdef DEBUG_UDP_LAYER
std::cerr << "Failed to Make Non-Blocking" << std::endl;
#endif
}
/* Setup socket for broadcast. */
int val = 1;
if (-1 == setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(int)))
{
#ifdef DEBUG_UDP_LAYER
std::cerr << "Failed to Make Socket Broadcast" << std::endl;
#endif
}
errorState = 0;
#ifdef DEBUG_UDP_LAYER
std::cerr << "Socket Bound to : " << laddr << std::endl;
#endif
sockMtx.unlock(); /******** UNLOCK MUTEX *********/
#ifdef DEBUG_UDP_LAYER
std::cerr << "Setting TTL to " << UDP_DEF_TTL << std::endl;
#endif
setTTL(UDP_DEF_TTL);
// start up our thread.
{
bdStackMutex stack(sockMtx); /********** LOCK MUTEX *********/
stopThread = false;
}
start();
return 1;
}
int UdpLayer::setTTL(int t)
{
sockMtx.lock(); /********** LOCK MUTEX *********/
int err = setsockopt(sockfd, IPPROTO_IP, IP_TTL, &t, sizeof(int));
ttl = t;
sockMtx.unlock(); /******** UNLOCK MUTEX *********/
#ifdef DEBUG_UDP_LAYER
std::cerr << "UdpLayer::setTTL(" << t << ") returned: " << err;
std::cerr << std::endl;
#endif
return err;
}
int UdpLayer::getTTL()
{
sockMtx.lock(); /********** LOCK MUTEX *********/
int t = ttl;
sockMtx.unlock(); /******** UNLOCK MUTEX *********/
return t;
}
/* monitoring / updates */
int UdpLayer::okay()
{
sockMtx.lock(); /********** LOCK MUTEX *********/
bool nonFatalError = ((errorState == 0) ||
(errorState == EAGAIN) ||
(errorState == EINPROGRESS));
sockMtx.unlock(); /******** UNLOCK MUTEX *********/
#ifdef DEBUG_UDP_LAYER
if (!nonFatalError)
{
std::cerr << "UdpLayer::NOT okay(): Error: " << errorState << std::endl;
}
#endif
return nonFatalError;
}
int UdpLayer::tick()
{
#ifdef DEBUG_UDP_LAYER
std::cerr << "UdpLayer::tick()" << std::endl;
#endif
return 1;
}
/******************* Internals *************************************/
int UdpLayer::receiveUdpPacket(void *data, int *size, struct sockaddr_in &from)
{
struct sockaddr_in fromaddr;
socklen_t fromsize = sizeof(fromaddr);
int insize = *size;
sockMtx.lock(); /********** LOCK MUTEX *********/
insize = recvfrom(sockfd,data,insize,0,
(struct sockaddr*)&fromaddr,&fromsize);
sockMtx.unlock(); /******** UNLOCK MUTEX *********/
if (0 < insize)
{
#ifdef DEBUG_UDP_LAYER
std::cerr << "receiveUdpPacket() from: " << fromaddr;
std::cerr << " Size: " << insize;
std::cerr << std::endl;
#endif
*size = insize;
from = fromaddr;
return insize;
}
return -1;
}
int UdpLayer::sendUdpPacket(const void *data, int size, struct sockaddr_in &to)
{
/* send out */
#ifdef DEBUG_UDP_LAYER
std::cerr << "UdpLayer::sendUdpPacket(): size: " << size;
std::cerr << " To: " << to << std::endl;
#endif
struct sockaddr_in toaddr = to;
sockMtx.lock(); /********** LOCK MUTEX *********/
sendto(sockfd, data, size, 0,
(struct sockaddr *) &(toaddr),
sizeof(toaddr));
sockMtx.unlock(); /******** UNLOCK MUTEX *********/
return 1;
}

View file

@ -0,0 +1,128 @@
#ifndef BITDHT_UDP_LAYER_H
#define BITDHT_UDP_LAYER_H
/*
* bitdht/udplayer.h
*
* BitDHT: An Flexible DHT library.
*
* Copyright 2010 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 3 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "bitdht@lunamutt.com".
*
*/
#include <netinet/in.h>
#include "util/bdthreads.h"
#include <iosfwd>
#include <list>
#include <deque>
/****
* #define UDP_LOOPBACK_TESTING 1
***/
std::ostream &operator<<(std::ostream &out, const struct sockaddr_in &addr);
bool operator==(const struct sockaddr_in &addr, const struct sockaddr_in &addr2);
bool operator<(const struct sockaddr_in &addr, const struct sockaddr_in &addr2);
std::string printPkt(void *d, int size);
std::string printPktOffset(unsigned int offset, void *d, unsigned int size);
/* UdpLayer ..... is the bottom layer which
* just sends and receives Udp packets.
*/
class UdpReceiver
{
public:
virtual ~UdpReceiver() {}
virtual int recvPkt(void *data, int size, struct sockaddr_in &from) = 0;
virtual int status(std::ostream &out) = 0;
};
class UdpPublisher
{
public:
virtual ~UdpPublisher() {}
virtual int sendPkt(const void *data, int size, struct sockaddr_in &to, int ttl) = 0;
};
class UdpLayer: public bdThread
{
public:
UdpLayer(UdpReceiver *recv, struct sockaddr_in &local);
virtual ~UdpLayer() { return; }
int reset(struct sockaddr_in &local); /* calls join, close, openSocket */
int status(std::ostream &out);
/* setup connections */
int closeSocket();
int openSocket();
/* RsThread functions */
virtual void run(); /* called once the thread is started */
void recv_loop(); /* uses callback to UdpReceiver */
/* Higher Level Interface */
//int readPkt(void *data, int *size, struct sockaddr_in &from);
int sendPkt(const void *data, int size, struct sockaddr_in &to, int ttl);
/* monitoring / updates */
int okay();
int tick();
/* data */
/* internals */
protected:
virtual int receiveUdpPacket(void *data, int *size, struct sockaddr_in &from);
virtual int sendUdpPacket(const void *data, int size, struct sockaddr_in &to);
int setTTL(int t);
int getTTL();
/* low level */
private:
UdpReceiver *recv;
struct sockaddr_in laddr; /* local addr */
int errorState;
int sockfd;
int ttl;
bool stopThread;
bdMutex sockMtx;
};
#endif

View file

@ -0,0 +1,215 @@
/*
* bitdht/udpstack.cc
*
* BitDHT: An Flexible DHT library.
*
* Copyright 2010 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 3 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "bitdht@lunamutt.com".
*
*/
#include "udpstack.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
#include <algorithm>
/***
* #define DEBUG_UDP_RECV 1
***/
//#define DEBUG_UDP_RECV 1
UdpStack::UdpStack(struct sockaddr_in &local)
:udpLayer(NULL), laddr(local)
{
openSocket();
return;
}
bool UdpStack::resetAddress(struct sockaddr_in &local)
{
return udpLayer->reset(local);
}
/* higher level interface */
int UdpStack::recvPkt(void *data, int size, struct sockaddr_in &from)
{
/* print packet information */
#ifdef DEBUG_UDP_RECV
std::cerr << "UdpStack::recvPkt(" << size << ") from: " << from;
std::cerr << std::endl;
#endif
bdStackMutex stack(stackMtx); /********** LOCK MUTEX *********/
std::list<UdpReceiver *>::iterator it;
for(it = mReceivers.begin(); it != mReceivers.end(); it++)
{
// See if they want the packet.
if ((*it)->recvPkt(data, size, from))
{
#ifdef DEBUG_UDP_RECV
std::cerr << "UdpStack::recvPkt(" << size << ") from: " << from;
std::cerr << std::endl;
#endif
break;
}
}
return 1;
}
int UdpStack::sendPkt(const void *data, int size, struct sockaddr_in &to, int ttl)
{
/* print packet information */
#ifdef DEBUG_UDP_RECV
std::cerr << "UdpStack::sendPkt(" << size << ") ttl: " << ttl;
std::cerr << " to: " << to;
std::cerr << std::endl;
#endif
/* send to udpLayer */
return udpLayer->sendPkt(data, size, to, ttl);
}
int UdpStack::status(std::ostream &out)
{
{
bdStackMutex stack(stackMtx); /********** LOCK MUTEX *********/
out << "UdpStack::status()" << std::endl;
out << "localaddr: " << laddr << std::endl;
out << "UdpStack::SubReceivers:" << std::endl;
std::list<UdpReceiver *>::iterator it;
int i = 0;
for(it = mReceivers.begin(); it != mReceivers.end(); it++, i++)
{
out << "\tReceiver " << i << " --------------------" << std::endl;
(*it)->status(out);
}
out << "--------------------" << std::endl;
out << std::endl;
}
udpLayer->status(out);
return 1;
}
/* setup connections */
int UdpStack::openSocket()
{
udpLayer = new UdpLayer(this, laddr);
return 1;
}
/* monitoring / updates */
int UdpStack::okay()
{
return udpLayer->okay();
}
int UdpStack::close()
{
/* TODO */
return 1;
}
/* add a TCPonUDP stream */
int UdpStack::addReceiver(UdpReceiver *recv)
{
bdStackMutex stack(stackMtx); /********** LOCK MUTEX *********/
/* check for duplicate */
std::list<UdpReceiver *>::iterator it;
it = std::find(mReceivers.begin(), mReceivers.end(), recv);
if (it == mReceivers.end())
{
mReceivers.push_back(recv);
return 1;
}
/* otherwise its already there! */
#ifdef DEBUG_UDP_RECV
std::cerr << "UdpStack::addReceiver() Recv already exists!" << std::endl;
std::cerr << "UdpStack::addReceiver() ERROR" << std::endl;
#endif
return 0;
}
int UdpStack::removeReceiver(UdpReceiver *recv)
{
bdStackMutex stack(stackMtx); /********** LOCK MUTEX *********/
/* check for duplicate */
std::list<UdpReceiver *>::iterator it;
it = std::find(mReceivers.begin(), mReceivers.end(), recv);
if (it != mReceivers.end())
{
mReceivers.erase(it);
return 1;
}
/* otherwise its not there! */
#ifdef DEBUG_UDP_RECV
std::cerr << "UdpStack::removeReceiver() Recv dont exist!" << std::endl;
std::cerr << "UdpStack::removeReceiver() ERROR" << std::endl;
#endif
return 0;
}
/*****************************************************************************************/
UdpSubReceiver::UdpSubReceiver(UdpPublisher *pub)
:mPublisher(pub)
{
return;
}
int UdpSubReceiver::sendPkt(const void *data, int size, struct sockaddr_in &to, int ttl)
{
/* print packet information */
#ifdef DEBUG_UDP_RECV
std::cerr << "UdpSubReceiver::sendPkt(" << size << ") ttl: " << ttl;
std::cerr << " to: " << to;
std::cerr << std::endl;
#endif
/* send to udpLayer */
return mPublisher->sendPkt(data, size, to, ttl);
}

View file

@ -0,0 +1,110 @@
#ifndef BITDHT_UDP_STACK_RECEIVER_H
#define BITDHT_UDP_STACK_RECEIVER_H
/*
* bitdht/udpstack.h
*
* BitDHT: An Flexible DHT library.
*
* Copyright 2010 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 3 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "bitdht@lunamutt.com".
*
*/
#include <netinet/in.h>
#include "util/bdthreads.h"
#include <iosfwd>
#include <list>
#include <deque>
#include <iosfwd>
#include <map>
#include "udplayer.h"
/* UdpStackReceiver is a Generic Receiver of info from a UdpLayer class.
* it provides a UdpReceiver class, and accepts a stack of UdpReceivers,
* which will be iterated through (in-order) until someone accepts the packet.
*
* It is important to order these Receivers correctly!
*
* This infact becomes the holder of the UdpLayer, and all controls
* go through the StackReceiver.
*/
class UdpSubReceiver: public UdpReceiver
{
public:
UdpSubReceiver(UdpPublisher *pub);
/* calls mPublisher->sendPkt */
virtual int sendPkt(const void *data, int size, struct sockaddr_in &to, int ttl);
/* callback for recved data (overloaded from UdpReceiver) */
//virtual int recvPkt(void *data, int size, struct sockaddr_in &from) = 0;
UdpPublisher *mPublisher;
};
class UdpStack: public UdpReceiver, public UdpPublisher
{
public:
UdpStack(struct sockaddr_in &local);
virtual ~UdpStack() { return; }
bool resetAddress(struct sockaddr_in &local);
/* add in a receiver */
int addReceiver(UdpReceiver *recv);
int removeReceiver(UdpReceiver *recv);
/* Packet IO */
/* pass-through send packets */
virtual int sendPkt(const void *data, int size, struct sockaddr_in &to, int ttl);
/* callback for recved data (overloaded from UdpReceiver) */
virtual int recvPkt(void *data, int size, struct sockaddr_in &from);
int status(std::ostream &out);
/* setup connections */
int openSocket();
/* monitoring / updates */
int okay();
// int tick();
int close();
private:
UdpLayer *udpLayer;
bdMutex stackMtx; /* for all class data (below) */
struct sockaddr_in laddr; /* local addr */
std::list<UdpReceiver *> mReceivers;
};
#endif