mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-03 14:45:12 -04:00
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:
parent
9b72977bba
commit
c415bb6158
56 changed files with 11344 additions and 0 deletions
215
libbitdht/src/udp/udpstack.cc
Normal file
215
libbitdht/src/udp/udpstack.cc
Normal 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);
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue