mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-03-12 10:06:41 -04:00
retroshare cache optimisations
including cache file optimisations git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-cacheopt@4370 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
commit
b7cbe7045c
5
build_scripts/Debian/README
Normal file
5
build_scripts/Debian/README
Normal file
@ -0,0 +1,5 @@
|
||||
Packing building for Ubuntu 32bits and 64bits
|
||||
=============================================
|
||||
|
||||
Just type 'sudo ./make.sh', and follow the instructions from the script .
|
||||
|
81
build_scripts/Debian/make.sh
Executable file
81
build_scripts/Debian/make.sh
Executable file
@ -0,0 +1,81 @@
|
||||
#!/bin/sh
|
||||
|
||||
# @author Cyril Soler, Thomas Eimers
|
||||
|
||||
###################### PARAMETERS ####################
|
||||
version="0.5.1b"
|
||||
arch=`dpkg --print-architecture`
|
||||
packager="Thomas Eimers <retroshare@notdefine.de>"
|
||||
######################################################
|
||||
|
||||
echo attempting to get svn revision number...
|
||||
svn=`svn info | grep 'Revision:' | cut -d\ -f2`
|
||||
echo done.
|
||||
version="$version"."$svn"
|
||||
pkgname=RetroShare_"$version"_debian_"$arch".deb
|
||||
|
||||
echo Building retroshare debian package version $version for Debian $arch.
|
||||
echo Please check that:
|
||||
echo " "- you have sudo access and that root has right to write in this directory and the subdirectories.
|
||||
echo " "- you have compiled libretroshare and retroshare-gui in ../../libretroshare/src/
|
||||
echo " "and ../../retroshare-gui/src/
|
||||
echo " "- you have updated version numbers in ../../retroshare-gui/src/util/rsversion.cpp
|
||||
echo " "and ../../retroshare-gui/src/retroshare.nsi
|
||||
echo " "- version and name will be: $pkgname
|
||||
|
||||
if ! test `whoami` = "root" ; then
|
||||
echo Please run this script as root.
|
||||
echo
|
||||
exit ;
|
||||
fi
|
||||
|
||||
echo
|
||||
echo Press [ENTER] when ok, else quit with Ctrl+C.
|
||||
read tmp
|
||||
|
||||
echo
|
||||
echo Unzipping...
|
||||
tar zxvf retroshare.tgz
|
||||
echo Changing ownership...
|
||||
chown -R root.root retroshare
|
||||
|
||||
# setup version numbers and arch in DEBIAN/control
|
||||
|
||||
echo Setting up version numbers...
|
||||
cat retroshare/DEBIAN/control | sed -e s/XXXXXX/"$version"/g | sed -e s/YYYYYY/"$arch"/g | sed -e s/ZZZZZZ/"$packager"/g > retroshare/DEBIAN/control.tmp
|
||||
mv retroshare/DEBIAN/control.tmp retroshare/DEBIAN/control
|
||||
|
||||
# clean
|
||||
find retroshare -name "*~" -exec \rm -f {} \;
|
||||
|
||||
# copy executables at the right place
|
||||
if ! test -f ../../retroshare-gui/src/RetroShare; then
|
||||
echo Can not find executable ../../retroshare-gui/src/RetroShare. Please fix this.
|
||||
echo
|
||||
exit ;
|
||||
fi
|
||||
#if ! test -f ../../retroshare-nogui/src/retroshare-nogui; then
|
||||
# echo Can not find executable ../../retroshare-nogui/src/retroshare-nogui. Please fix this.
|
||||
# echo
|
||||
# exit ;
|
||||
#fi
|
||||
|
||||
echo Stripping executables...
|
||||
cp ../../retroshare-gui/src/RetroShare retroshare/usr/bin/
|
||||
strip retroshare/usr/bin/RetroShare
|
||||
#cp ../../retroshare-nogui/src/retroshare-nogui retroshare/usr/bin/
|
||||
#strip retroshare/usr/bin/retroshare-nogui
|
||||
|
||||
# compute md5 sums
|
||||
echo Computing/setting md5 sums...
|
||||
cd retroshare
|
||||
find usr -type f -exec md5sum {} \; > DEBIAN/md5sums
|
||||
cd ..
|
||||
echo Creating package $pkgname
|
||||
dpkg-deb -b retroshare $pkgname
|
||||
|
||||
# cleaning
|
||||
echo Cleaning up...
|
||||
\rm -rf retroshare
|
||||
|
||||
echo Done.
|
BIN
build_scripts/Debian/retroshare.tgz
Normal file
BIN
build_scripts/Debian/retroshare.tgz
Normal file
Binary file not shown.
66
build_scripts/OSX/OSX_RS_Compilation_Instructions.txt
Normal file
66
build_scripts/OSX/OSX_RS_Compilation_Instructions.txt
Normal file
@ -0,0 +1,66 @@
|
||||
|
||||
Mac OSX Build Instructions.
|
||||
-------------------------------------------
|
||||
|
||||
There are several complications with building Retroshare under OSX.
|
||||
1) Support Libraries must be built and installed seperately.
|
||||
2) Universal and OSX 10.5 support is a little tricky, mainly due to the support libraries.
|
||||
|
||||
Additional Libraries
|
||||
---------------------
|
||||
|
||||
* GnuPG Package, that comes with Retroshare OSX install image. (GnuPG-1.4.9.dmg)
|
||||
|
||||
GPG Development libraries
|
||||
* libassuan (I'm using 2.0.1)
|
||||
* libgpg-error (I'm using 1.9)
|
||||
* libgpgme (I'm using 1.3.0)
|
||||
|
||||
These libraries use standard UNIX installation systems: AUTOCONF/AUTOMAKE (configure, make, etc)
|
||||
Unfortunately, this makes it difficult and a little manual to compile Universal and 10.5 libraries.
|
||||
|
||||
UPNPC (for OSX and windows)
|
||||
* miniupnpc (I'm using 1.0)
|
||||
|
||||
You will also need to install
|
||||
* XCode (available on the Apple Install CDs)
|
||||
* Qt4 (from trolltech.com)
|
||||
|
||||
First Compilation...
|
||||
--------------------
|
||||
|
||||
The First challenge is to build Retroshare on your Mac. For this first compilation,
|
||||
we only build for your specific machine, and not attempt a Generic / 10.5 / Univeral build.
|
||||
|
||||
|
||||
1) Install / Compile all the packages listed above.
|
||||
be sure to use a configure command like this where applicable to only create a static library.
|
||||
./configure --enable-static=yes --enable-shared=no CFLAGS="-arch i386" CPPFLAGS="-arch i386"
|
||||
|
||||
2) Check out the Retroshare SVN.
|
||||
|
||||
3) compile libbitdht:
|
||||
cd libbitdht/src
|
||||
qmake
|
||||
|
||||
This recreates a xcodeproj file for compilation using XCode.
|
||||
Open with Xcode, and build.
|
||||
|
||||
4) compile libretroshare: same way.
|
||||
5) compile retroshare-gui: same way.
|
||||
|
||||
|
||||
Creating Retroshare OSX Distribution Packages.
|
||||
-----------------------------------------------
|
||||
TODO, once I've got feedback on First Compilation!
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
5
build_scripts/Ubuntu/README
Normal file
5
build_scripts/Ubuntu/README
Normal file
@ -0,0 +1,5 @@
|
||||
Packing building for Ubuntu 32bits and 64bits
|
||||
=============================================
|
||||
|
||||
Just type 'sudo ./make.sh', and follow the instructions from the script .
|
||||
|
79
build_scripts/Ubuntu/make.sh
Executable file
79
build_scripts/Ubuntu/make.sh
Executable file
@ -0,0 +1,79 @@
|
||||
#!/bin/sh
|
||||
|
||||
###################### PARAMETERS ####################
|
||||
version="0.4.12"
|
||||
arch=`dpkg --print-architecture`
|
||||
packager="Cyril Soler <csoler@users.sourceforge.net>"
|
||||
######################################################
|
||||
|
||||
echo attempting to get svn revision number...
|
||||
svn=`svn info | grep 'Revision:' | cut -d\ -f2`
|
||||
echo done.
|
||||
version="$version"."$svn"
|
||||
pkgname=RetroShare_"$version"_ubuntu_"$arch".deb
|
||||
|
||||
echo Building retroshare debian package version $version for Ubuntu $arch.
|
||||
echo Please check that:
|
||||
echo " "- you have sudo access and that root has right to write in this directory and the subdirectories.
|
||||
echo " "- you have compiled libretroshare and retroshare-gui in ../../libretroshare/src/
|
||||
echo " "and ../../retroshare-gui/src/
|
||||
echo " "- you have updated version numbers in ../../retroshare-gui/src/util/rsversion.cpp
|
||||
echo " "and ../../retroshare-gui/src/retroshare.nsi
|
||||
echo " "- version and name will be: $pkgname
|
||||
|
||||
if ! test `whoami` = "root" ; then
|
||||
echo Please run this script as root.
|
||||
echo
|
||||
exit ;
|
||||
fi
|
||||
|
||||
echo
|
||||
echo Press [ENTER] when ok, else quit with Ctrl+C.
|
||||
read tmp
|
||||
|
||||
echo
|
||||
echo Unzipping...
|
||||
tar zxvf retroshare.tgz
|
||||
echo Changing ownership...
|
||||
chown -R root.root retroshare
|
||||
|
||||
# setup version numbers and arch in DEBIAN/control
|
||||
|
||||
echo Setting up version numbers...
|
||||
cat retroshare/DEBIAN/control | sed -e s/XXXXXX/"$version"/g | sed -e s/YYYYYY/"$arch"/g | sed -e s/ZZZZZZ/"$packager"/g > retroshare/DEBIAN/control.tmp
|
||||
mv retroshare/DEBIAN/control.tmp retroshare/DEBIAN/control
|
||||
|
||||
# clean
|
||||
find retroshare -name "*~" -exec \rm -f {} \;
|
||||
|
||||
# copy executables at the right place
|
||||
if ! test -f ../../retroshare-gui/src/RetroShare; then
|
||||
echo Can not find executable ../../retroshare-gui/src/RetroShare. Please fix this.
|
||||
echo
|
||||
exit ;
|
||||
fi
|
||||
if ! test -f ../../retroshare-nogui/src/retroshare-nogui; then
|
||||
echo Can not find executable ../../retroshare-nogui/src/retroshare-nogui. Please fix this.
|
||||
echo
|
||||
exit ;
|
||||
fi
|
||||
|
||||
echo Stripping executables...
|
||||
cp ../../retroshare-gui/src/RetroShare retroshare/usr/bin/
|
||||
strip retroshare/usr/bin/RetroShare
|
||||
cp ../../retroshare-nogui/src/retroshare-nogui retroshare/usr/bin/
|
||||
strip retroshare/usr/bin/retroshare-nogui
|
||||
|
||||
# compute md5 sums
|
||||
echo Computing/setting md5 sums...
|
||||
cd retroshare
|
||||
find usr -type f -exec md5sum {} \; > DEBIAN/md5sums
|
||||
cd ..
|
||||
echo Creating package $pkgname
|
||||
dpkg-deb -b retroshare $pkgname
|
||||
|
||||
# cleaning
|
||||
echo Cleaning up...
|
||||
\rm -rf retroshare
|
||||
|
||||
echo Done.
|
BIN
build_scripts/Ubuntu/retroshare.tgz
Normal file
BIN
build_scripts/Ubuntu/retroshare.tgz
Normal file
Binary file not shown.
BIN
build_scripts/Ubuntu_src/BaseRetroShareDirs.tgz
Normal file
BIN
build_scripts/Ubuntu_src/BaseRetroShareDirs.tgz
Normal file
Binary file not shown.
9
build_scripts/Ubuntu_src/README.txt
Normal file
9
build_scripts/Ubuntu_src/README.txt
Normal file
@ -0,0 +1,9 @@
|
||||
This directory contains the material for buildign a debian/ubuntu source package from
|
||||
the svn repository of RetroShare. The various files here are:
|
||||
|
||||
BaseRetroShareDirs.tgz: basic directory structure. Does not contain any source file.
|
||||
makeSourcePackage.sh : main script. Produces source binary debian package (.dsc+.tgz files)
|
||||
from the base directory structure and the sources in the svn repository.
|
||||
clean.sh : compiles the source package. Produce a binary pkg for a given ubuntu distribution
|
||||
make_tgz.sh : rebuilds the basic directory structure if some config/debian files where modified
|
||||
in the retroshare-0.5 directory
|
196
build_scripts/Ubuntu_src/changelog
Normal file
196
build_scripts/Ubuntu_src/changelog
Normal file
@ -0,0 +1,196 @@
|
||||
retroshare (0.5.1-0.XXXXXX~YYYYYY) YYYYYY; urgency=low
|
||||
|
||||
* Improvements
|
||||
- Updated tests for tcponudp.
|
||||
|
||||
* Bug fixes
|
||||
- fixed bug in search with size in MB. Caused crash due to deserialization error.
|
||||
- Added Check for the existance of each Cache File before that are passed on the loader.
|
||||
This should once and for-all remove the copy file warnings
|
||||
|
||||
-- Cyril Soler <csoler@users.sourceforge.net> Tue, 28 Jun 2011 19:35:46 +0100
|
||||
|
||||
retroshare (0.5.1-0.4337~lucid) lucid; urgency=low
|
||||
|
||||
* Bug fixes
|
||||
- Fixed the cache transfer killing bug (misplaced brackets)
|
||||
- Fixed bug in pqissl wrongly casting a pointer to unsigned long, possibly impacting 64bit systems
|
||||
- Fixed bug responsible for corrupting large packets. Solved the chat bug,
|
||||
the errors in GetTlvString, the corrupted exchange of keys, location names, etc.
|
||||
|
||||
-- Cyril Soler <csoler@users.sourceforge.net> Sat, 25 Jun 2011 19:35:46 +0100
|
||||
|
||||
retroshare (0.5.1-0.4331~lucid) lucid; urgency=low
|
||||
|
||||
* Improvements
|
||||
- Started dev of plugin system. Works on linux, not yet on windows.
|
||||
- Added gui for plugins in Settings
|
||||
- Ported LinksCloud service as a plugin
|
||||
- Improvement of the unit tests, and coverage calculations.
|
||||
- Hash cache: missing root directories are not discarded anymore, but kept empty,
|
||||
unless the user really removes them explicitely.
|
||||
- Reworked settings dialog
|
||||
- Automatic add the recommended friend(s) as CC in the MessageComposer.
|
||||
- Send plain text message instead of html, when the text contains no html specifics.
|
||||
This is a huge gain in forum posts size, up to a factor of 50
|
||||
- Enabled Spanish translation
|
||||
- Moved the fill function of the ForumsDialog to a QThread.
|
||||
- Made multiple keyword search use an AND instead of an OR.
|
||||
|
||||
* Bug fixes
|
||||
- Fixed display of upload transfers
|
||||
- Fixed the Missing Cache File bug.
|
||||
- Added Old Cache Storage... so these can be retrieved.
|
||||
- Connected up the CancelCacheFile() -> FileCancel.
|
||||
- Removed "ipaddr=1" bug
|
||||
- Fixed uninitialized value in p3turtle
|
||||
- Fixed html title bug in forum message when answering from news feed
|
||||
- Fixed reaction of the checkbox "web of trust"
|
||||
- Fixed calculation of the unread messages in MessagesDialog
|
||||
- Corrected bug that would wipe out the hash cache file every other start
|
||||
- Fixed crash in ForumsDialog
|
||||
- Fixed memory leak of the fill thread of the history browser
|
||||
- Fixed refill of the threads tree after receiving a new message. Now the new message is expanded.
|
||||
|
||||
-- Cyril Soler <csoler@users.sourceforge.net> Mon, 11 Apr 2010 21:35:46 +0100
|
||||
|
||||
retroshare (0.5.1-0.4239~lucid) lucid; urgency=low
|
||||
|
||||
* Improvements
|
||||
- Optimized the functions for the forum messages.
|
||||
- added a star functionality to forums
|
||||
- placed private message editing into a separate window
|
||||
- added a tab to graphically display turtle router statistics
|
||||
- added automatic handling of turtle requests/search conjestion cases
|
||||
- patched for compilation on OpenSuze11.4
|
||||
- put links to peer and signers in certificate dialog
|
||||
- updated french translation
|
||||
- added copy/paste functionality of RS links to friends keys
|
||||
- various GUI tweaks
|
||||
- added to RetroShare Help page noew commands entry for "-r retroshare://..."
|
||||
- added history cache optimisation to subscribed/client groups (functionality is now disabled)
|
||||
- Added new RetroShare link format to write a message retroshare://message?id=...[&subject=...]
|
||||
- Added new RetroShare link format to start a search retroshare://search?keywords=...
|
||||
- Enabled on Private Chat to display for the status string smileys, when there is smiley codes used.
|
||||
|
||||
* Bug fixes
|
||||
- Channels: Fixed bug related to opening cache hist file in text mode for windows
|
||||
- Chat: Sending large messages now works, thanks to RsChatMsgItem splitting (not 100% backward compatible, but avoids crashing)
|
||||
- Chat: Removed crash due to dynamic_cast onto a deleted pointer.
|
||||
- Dbase: Removed the difference of the separator for Linux and Windows.
|
||||
- Dbase: Removed extra "/" of the filename (e.g. "E://") of the hashed file when sharing a whole drive.
|
||||
- Forums: Fixed drag and drop of files to a forum message with utf8 chars.
|
||||
- General: Fixed recommend friend.
|
||||
- Search: Fixed sorting by date in search
|
||||
|
||||
-- Cyril Soler <csoler@users.sourceforge.net> Mon, 11 Apr 2010 21:35:46 +0100
|
||||
|
||||
retroshare (0.5.1-0.4179~lucid) lucid; urgency=low
|
||||
|
||||
* Improvements
|
||||
- updated packaging for ubuntu natty, removed dependency on qt4-opengl
|
||||
- updated french/german translations
|
||||
- updated icons
|
||||
- enabled drag and drop of directories in share manager
|
||||
|
||||
* Bug fixes
|
||||
- suppred unwanted apparence of channel source id as channel source
|
||||
- improved strategy for cleaning stalled cache transfers
|
||||
- fixed leaking of sources in channel posts
|
||||
|
||||
-- Cyril Soler <csoler@users.sourceforge.net> Mon, 11 Apr 2010 21:35:46 +0100
|
||||
|
||||
retroshare (0.5.1-0.4166~lucid) lucid; urgency=low
|
||||
|
||||
* Improvements
|
||||
- made flat view for shared files less CPU demanding
|
||||
- added automated treatment of RS links from web browser
|
||||
|
||||
* Bug fixes
|
||||
- removed potential segfault when decrypting cache file
|
||||
|
||||
-- Cyril Soler <csoler@users.sourceforge.net> Mon, 11 Apr 2010 21:35:46 +0100
|
||||
|
||||
retroshare (0.5.1-0.4154~lucid) lucid; urgency=low
|
||||
|
||||
* Improvements
|
||||
- added flat view for shared files, with search, sorting and temporised update
|
||||
- enabled file preview for partially downloaded files, for non windows users
|
||||
- removed display of directories from search
|
||||
- added auto-download option to channels
|
||||
- added random bias to turtle search and tunnels at maximum depth, to prevent security issues
|
||||
- added biased rerouting of tunnels requests to statistically optimize tunnel length
|
||||
- made sign GPG button disabled by default
|
||||
- optimized lookups of/into FileEntry structures (faster, better memory coalescence)
|
||||
- added tunning for auto-check of shared directories, in config
|
||||
- updated french/german translations
|
||||
- saved the visible state of the avatar picture in private chat for the friend.
|
||||
- removed own settings for news feed
|
||||
- added tooltip for systray to display nickname and location
|
||||
- made lock file warning more explicit
|
||||
|
||||
* Bug fixes
|
||||
- channel download is now called only once for each channel post
|
||||
- fixed cleaning issue of certificates. Added a button in cert dialog for cleaning.
|
||||
- removed automatic cleaning of certs.
|
||||
- fixed bug with utf8 chatacters in %appdata% paths on windows
|
||||
- fixed crash on shutdown when the cache loading is running
|
||||
- fixed some stylesheets to use gradients instead of a background images,
|
||||
- fixed thread leak in file checking due to not calling pthread_join() on finished thread
|
||||
- showing uploads by default, instead of current transfer
|
||||
|
||||
-- Cyril Soler <csoler@users.sourceforge.net> Mon, 11 Apr 2010 21:35:46 +0100
|
||||
|
||||
retroshare (0.5.1-0.4097~lucid) lucid; urgency=low
|
||||
|
||||
* Improvements
|
||||
- attempt to make GPG errors more verbose at cert exchange time
|
||||
- translate QuickStartWizard, GenCertDialog and StartDialog into german
|
||||
- fixed tab order of the upload and download settings in QuickStartWizard
|
||||
- Added a basic description of the chat style
|
||||
- Added share incoming directory to the QuickStartWizard, while keeping
|
||||
the Download directory shared by default
|
||||
|
||||
* Bug Fixes
|
||||
- implemented a backward compatible fix for advance searching with size
|
||||
larger than 2Gb
|
||||
- implemented a better management of tunnel requests
|
||||
|
||||
|
||||
-- Cyril Soler <csoler@users.sourceforge.net> Thu, 07 Mar 2010 21:35:46 +0100
|
||||
|
||||
retroshare (0.5.1-0.4087~lucid) lucid; urgency=low
|
||||
|
||||
* Improvements
|
||||
- The working (hashing) thread FileIndexMonitor is now stopped when RetroShare is closed.
|
||||
- Removed tab focus from the toolbar and image buttons.
|
||||
- Fixed german translation.
|
||||
|
||||
* Bug Fixes:
|
||||
- Fixed a toolbar icon to display with a correct size on macosx
|
||||
- Added missing return... which was causing crash at start during directory creation.
|
||||
- Supressed memory leak due to not deleting a RsItem
|
||||
- Suppressed potential SIGSEGV as exit time
|
||||
|
||||
-- Cyril Soler <csoler@users.sourceforge.net> Thu, 07 Mar 2010 21:35:46 +0100
|
||||
|
||||
retroshare (0.5.1-0.4069~lucid) lucid; urgency=low
|
||||
|
||||
* Improvements:
|
||||
- Added a confirm to the delete history action.
|
||||
- Added the RetroShare icon to the password input window.
|
||||
- Added display of number of intermediate tunnels, and limited the number of displayed tunnel requests
|
||||
|
||||
* Bug Fixes:
|
||||
- Fixed potential SIGSEGV because of not checking returned pointer in DHT code.
|
||||
- Fixed memory leak in GUI responsible for high leaks in large networks.
|
||||
- Fixed memory leak in p3distrib
|
||||
|
||||
-- Cyril Soler <csoler@users.sourceforge.net> Thu, 28 Feb 2010 21:35:46 +0100
|
||||
|
||||
retroshare (0.5.1-0.4059~lucid) lucid; urgency=low
|
||||
|
||||
* Release 0.5.1:
|
||||
New features (DHT, Channels, Network View), Improved stability w.r.t. previous version 0.5.0g
|
||||
|
||||
-- Cyril Soler <csoler@users.sourceforge.net> Thu, 24 Feb 2010 21:35:46 +0100
|
11
build_scripts/Ubuntu_src/clean.sh
Executable file
11
build_scripts/Ubuntu_src/clean.sh
Executable file
@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
|
||||
rm ./retroshare_0.5.1-0.*_source.build
|
||||
rm ./retroshare_0.5.1-0.*_source.changes
|
||||
rm ./retroshare_0.5.1-0.*.tar.gz
|
||||
rm ./retroshare_0.5.1-0.*.diff.gz
|
||||
rm ./retroshare_0.5.1-0.*.dsc
|
||||
|
||||
rm *~
|
||||
find . -name "*~" -exec rm {} \;
|
||||
|
8
build_scripts/Ubuntu_src/cleanProFile.sh
Executable file
8
build_scripts/Ubuntu_src/cleanProFile.sh
Executable file
@ -0,0 +1,8 @@
|
||||
#!/bin/sh
|
||||
|
||||
tmpfile=/tmp/toto42314321
|
||||
|
||||
cat "$1" | grep -v "CONFIG += version_detail_bash_script" > $tmpfile
|
||||
echo "INCLUDEPATH += ../libretroshare" > $1
|
||||
cat $tmpfile >> $1
|
||||
|
27
build_scripts/Ubuntu_src/make.sh
Executable file
27
build_scripts/Ubuntu_src/make.sh
Executable file
@ -0,0 +1,27 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo This script will
|
||||
echo - remove the directory retroshare-0.5/
|
||||
echo - remove existing sources packages in the current directory
|
||||
echo - build a new source package from the svn
|
||||
echo - rebuild the source package for the karmic i386 arch.
|
||||
echo
|
||||
|
||||
echo attempting to get svn revision number...
|
||||
svn=`svn info | grep 'Revision:' | cut -d\ -f2`
|
||||
echo Revision number is $svn.
|
||||
|
||||
echo Type ^C to abort, or enter to continue
|
||||
read tmp
|
||||
|
||||
svn=4153
|
||||
rm -rf ./retroshare-0.5
|
||||
# ./makeSourcePackage.sh
|
||||
|
||||
for dist in jaunty karmic lucid maverick natty; do
|
||||
sudo PBUILDFOLDER=/var/cache/pbuilder pbuilder-dist "$dist" build retroshare_0.5.1-0."$svn"~"$dist".dsc
|
||||
cp /var/cache/pbuilder/"$dist"_result/retroshare_0.5.1-0."$svn"~"$dist"_amd64.deb .
|
||||
sudo PBUILDFOLDER=/var/cache/pbuilder pbuilder-dist "$dist" i386 build retroshare_0.5.1-0."$svn"~"$dist".dsc
|
||||
cp /var/cache/pbuilder/"$dist"-i386_result/retroshare_0.5.1-0."$svn"~"$dist"_i386.deb .
|
||||
done
|
||||
|
114
build_scripts/Ubuntu_src/makeSourcePackage.sh
Executable file
114
build_scripts/Ubuntu_src/makeSourcePackage.sh
Executable file
@ -0,0 +1,114 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo This script is going to build the debian source package for RetroShare, from the svn.
|
||||
|
||||
if test -d "RetroShare" ; then
|
||||
echo Please remove the RetroShare/ directory first.
|
||||
exit
|
||||
fi
|
||||
|
||||
svn update
|
||||
|
||||
###################### PARAMETERS ####################
|
||||
version="0.5.1"
|
||||
######################################################
|
||||
|
||||
if test "$1" = "" ; then
|
||||
echo attempting to get svn revision number...
|
||||
svn=`svn info | grep 'Revision:' | cut -d\ -f2`
|
||||
else
|
||||
echo svn number has been provided. Forcing update.
|
||||
svn="$1"
|
||||
fi
|
||||
|
||||
echo done.
|
||||
version="$version"."$svn"
|
||||
echo got version number $version.
|
||||
echo Please check that the changelog is up to date.
|
||||
echo Hit ENTER is this is this correct. Otherwise hit Ctrl+C
|
||||
read tmp
|
||||
|
||||
packages="."
|
||||
|
||||
echo SVN number is $svn
|
||||
echo version is $version
|
||||
|
||||
echo Extracting base archive...
|
||||
tar zxvf $packages/BaseRetroShareDirs.tgz 2> /dev/null
|
||||
|
||||
echo Checking out latest snapshot in libbitdht...
|
||||
cd retroshare-0.5/src/libbitdht/
|
||||
svn co -r$svn https://retroshare.svn.sourceforge.net/svnroot/retroshare/trunk/libbitdht/src . 2> /dev/null
|
||||
cd ../../..
|
||||
#
|
||||
echo Checking out latest snapshot in libretroshare...
|
||||
cd retroshare-0.5/src/libretroshare/
|
||||
svn co -r$svn https://retroshare.svn.sourceforge.net/svnroot/retroshare/trunk/libretroshare/src . 2> /dev/null
|
||||
cd ../../..
|
||||
#
|
||||
echo Checking out latest snapshot in retroshare-gui...
|
||||
cd retroshare-0.5/src/retroshare-gui/
|
||||
svn co -r$svn https://retroshare.svn.sourceforge.net/svnroot/retroshare/trunk/retroshare-gui/src . 2> /dev/null
|
||||
cd ../../..
|
||||
#
|
||||
echo Checking out latest snapshot in retroshare-nogui...
|
||||
cd retroshare-0.5/src/retroshare-nogui/
|
||||
svn co -r$svn https://retroshare.svn.sourceforge.net/svnroot/retroshare/trunk/retroshare-nogui/src . 2> /dev/null
|
||||
cd ../../..
|
||||
|
||||
echo Copying bdboot.txt file at installation place
|
||||
cp retroshare-0.5/src/libbitdht/bitdht/bdboot.txt
|
||||
echo Setting version numbers...
|
||||
|
||||
# setup version numbers
|
||||
cat retroshare-0.5/src/libretroshare/util/rsversion.h | grep -v SVN_REVISION > /tmp/toto2342
|
||||
echo \#define SVN_REVISION \"Revision: "$version" date : `date`\" >> /tmp/toto2342
|
||||
cp /tmp/toto2342 retroshare-0.5/src/libretroshare/util/rsversion.h
|
||||
|
||||
cat retroshare-0.5/src/retroshare-gui/util/rsversion.h | grep -v GUI_REVISION > /tmp/toto4463
|
||||
echo \#define GUI_REVISION \"Revision: "$version" date : `date`\" >> /tmp/toto4463
|
||||
cp /tmp/toto4463 retroshare-0.5/src/retroshare-gui/util/rsversion.h
|
||||
|
||||
# Various cleaning
|
||||
|
||||
echo Cleaning...
|
||||
find retroshare-0.5 -name ".svn" -exec rm -rf {} \; # remove all svn repositories
|
||||
|
||||
echo Preparing package
|
||||
mv retroshare-0.5/src/retroshare-gui/RetroShare.pro retroshare-0.5/src/retroshare-gui/retroshare-gui.pro
|
||||
|
||||
./cleanProFile.sh retroshare-0.5/src/libretroshare/libretroshare.pro
|
||||
./cleanProFile.sh retroshare-0.5/src/retroshare-gui/retroshare-gui.pro
|
||||
./cleanProFile.sh retroshare-0.5/src/retroshare-nogui/retroshare-nogui.pro
|
||||
|
||||
echo "DESTDIR = ../../libretroshare/src/lib/" > /tmp/toto75299
|
||||
cat retroshare-0.5/src/libretroshare/libretroshare.pro /tmp/toto75299 > /tmp/toto752992
|
||||
cp /tmp/toto752992 retroshare-0.5/src/libretroshare/libretroshare.pro
|
||||
|
||||
echo "DESTDIR = ../../libbitdht/src/lib/" > /tmp/toto75299
|
||||
cat retroshare-0.5/src/libbitdht/libbitdht.pro /tmp/toto75299 > /tmp/toto752992
|
||||
cp /tmp/toto752992 retroshare-0.5/src/libbitdht/libbitdht.pro
|
||||
|
||||
#cat retroshare-gui-ext.pro >> retroshare-0.5/src/retroshare-gui/retroshare-gui.pro
|
||||
|
||||
#echo Building orig directory...
|
||||
#mkdir retroshare-0.5.orig
|
||||
#cp -r retroshare-0.5/src retroshare-0.5.orig
|
||||
|
||||
# Call debuild to make the source debian package
|
||||
|
||||
echo Calling debuild...
|
||||
cat retroshare-0.5/debian/control | sed -e s/XXXXXX/"$version"/g > retroshare-0.5/debian/control.tmp
|
||||
mv -f retroshare-0.5/debian/control.tmp retroshare-0.5/debian/control
|
||||
|
||||
cd retroshare-0.5
|
||||
|
||||
for i in lucid karmic jaunty maverick natty; do
|
||||
echo copying changelog for $i
|
||||
cat ../changelog | sed -e s/XXXXXX/"$svn"/g | sed -e s/YYYYYY/"$i"/g > debian/changelog
|
||||
|
||||
# This is the key for "Cyril Soler <csoler@sourceforge.net>"
|
||||
debuild -S -kC737CA98
|
||||
done
|
||||
|
||||
|
28
build_scripts/Ubuntu_src/make_tgz.sh
Executable file
28
build_scripts/Ubuntu_src/make_tgz.sh
Executable file
@ -0,0 +1,28 @@
|
||||
#!/bin/sh
|
||||
|
||||
rm BaseRetroShareDirs.tgz
|
||||
|
||||
rm -rf retroshare-0.5/libbitdht/
|
||||
rm -rf retroshare-0.5/libretroshare/
|
||||
|
||||
rm -rf retroshare-0.5/src/libbitdht/*
|
||||
rm -rf retroshare-0.5/src/libbitdht/.svn/
|
||||
rm -rf retroshare-0.5/src/libretroshare/*
|
||||
rm -rf retroshare-0.5/src/libretroshare/.svn/
|
||||
rm -rf retroshare-0.5/src/retroshare-gui/*
|
||||
rm -rf retroshare-0.5/src/retroshare-gui/.svn/
|
||||
rm -rf retroshare-0.5/src/retroshare-nogui/*
|
||||
rm -rf retroshare-0.5/src/retroshare-nogui/.svn/
|
||||
|
||||
find retroshare-0.5 -name "*~" -exec \rm {} \;
|
||||
|
||||
tar zcvf BaseRetroShareDirs.tgz retroshare-0.5/
|
||||
|
||||
if ! test -f BaseRetroShareDirs.tgz; then
|
||||
echo BaseRetroShareDirs.tgz could not be created
|
||||
exit;
|
||||
fi
|
||||
|
||||
rm -rf retroshare-0.5/
|
||||
|
||||
|
2
build_scripts/Ubuntu_src/ppa_upload.sh
Normal file
2
build_scripts/Ubuntu_src/ppa_upload.sh
Normal file
@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
dput ppa:csoler-users/ppa retroshare_0.5-1_source.changes
|
32
build_scripts/Ubuntu_src/retroshare_ppastatistics.py
Executable file
32
build_scripts/Ubuntu_src/retroshare_ppastatistics.py
Executable file
@ -0,0 +1,32 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
# Requires packages
|
||||
# python-launchpad-integration
|
||||
# python-launchpadlib
|
||||
|
||||
from launchpadlib.launchpad import Launchpad
|
||||
PPAOWNER = "csoler-users" #the launchpad PPA owener. It's usually the first part of a PPA. Example: in "webupd8team/vlmc", the owener is "webupd8team".
|
||||
|
||||
distribs = ['jaunty','karmic','lucid','maverick','natty']
|
||||
archs = ['i386','amd64']
|
||||
ppas = ['retroshare','retroshare-snapshots']
|
||||
total = 0
|
||||
|
||||
|
||||
for PPA in ppas:
|
||||
for distrib in distribs:
|
||||
for arch in archs:
|
||||
desired_dist_and_arch = 'https://api.edge.launchpad.net/devel/ubuntu/' + distrib + '/' + arch
|
||||
#here, edit "maverick" and "i386" with the Ubuntu version and desired arhitecture
|
||||
|
||||
cachedir = "~/.launchpadlib/cache/"
|
||||
lp_ = Launchpad.login_anonymously('ppastats', 'edge', cachedir, version='devel')
|
||||
owner = lp_.people[PPAOWNER]
|
||||
archive = owner.getPPAByName(name=PPA)
|
||||
|
||||
for individualarchive in archive.getPublishedBinaries(status='Published',distro_arch_series=desired_dist_and_arch):
|
||||
print PPA + "\t" + arch + "\t" + individualarchive.binary_package_version + "\t" + str(individualarchive.getDownloadCount())
|
||||
total += individualarchive.getDownloadCount()
|
||||
|
||||
print "Total downloads: " + str(total)
|
||||
|
224
build_scripts/Windows/compilingForWindows.txt
Normal file
224
build_scripts/Windows/compilingForWindows.txt
Normal file
@ -0,0 +1,224 @@
|
||||
|
||||
Compling for Windows.
|
||||
-----------------------------------
|
||||
For Version V0.5.0+ we are switching to GPGme libraries.
|
||||
This documents a clean compilation of V0.5 for Windows.
|
||||
|
||||
Tested on: WinXP July 2009.
|
||||
|
||||
You will need:
|
||||
* Qt4.5 Development Libraries (from TrollTech)
|
||||
* MinGw/MinSys full installation (from sourceforge.net)
|
||||
* Cygwin (for OpenSSL compilation / SVN / Unix Tools)
|
||||
* OpenSSL standard libraries.
|
||||
* GPG Windows Install (includes gpg.exe)
|
||||
* MiniUpnp library
|
||||
* gpgme Library.
|
||||
* libgpg-error Library.
|
||||
|
||||
|
||||
This compilation is a little tricky as different environments
|
||||
are needed to compile different libraries.
|
||||
a) Cygwin: libssl.a libcrypto.a
|
||||
b) MinGW/MSYS: libgpg-error, libgpgme.a, libretroshare.a, libminiupnp.a
|
||||
c) Qt/MinGW: Retroshare.exe
|
||||
|
||||
|
||||
/***************** Installation of Development Tools **************/
|
||||
In this section we install: cygwin, mingw/msys and Qt4.5
|
||||
|
||||
1) Install Cygwin
|
||||
a) download the installer and run.
|
||||
b) be sure to select from the development packages:
|
||||
* mingw
|
||||
* svn
|
||||
* gcc/gdb
|
||||
* make
|
||||
links:
|
||||
http://www.cygwin.org/
|
||||
|
||||
2) install the MinGW/Msys package.
|
||||
a) Download MinGW-5.1.4 installer and run.
|
||||
b) Download MSYS-1.0.11 installer and run.
|
||||
|
||||
links:
|
||||
http://downloads.sourceforge.net/mingw/MinGW-5.1.4.exe
|
||||
http://downloads.sourceforge.net/mingw/MSYS-1.0.11.exe
|
||||
http://www.mingw.org/
|
||||
http://sourceforge.net/projects/mingw/files/
|
||||
|
||||
3) Install Qt4.5 LGPL Version. (Qt SDK 2009.03)
|
||||
a) Download from website and run.
|
||||
b) Select all options (including MinGW)
|
||||
|
||||
links:
|
||||
http://www.qtsoftware.com/downloads
|
||||
|
||||
/***************** Installation of Development Tools **************/
|
||||
/******************* Compiling Support Libraries ******************/
|
||||
|
||||
1) Compile OpenSSL using Cygwin.
|
||||
a) launch cygwin xterm
|
||||
b) untar openssl source package.
|
||||
c) Configure mingw
|
||||
d) make.
|
||||
|
||||
2) Compile gpg-error
|
||||
a) Download from gpg
|
||||
b) load up MinGW/MSYS Bash shell.
|
||||
c) extract code.
|
||||
d) configure
|
||||
e) make
|
||||
f) make install
|
||||
|
||||
The library will end up in /local/lib
|
||||
|
||||
3) Compile gpgme
|
||||
a) Download from gpg
|
||||
b) load up MinGW/MSYS Bash shell.
|
||||
c) extract code.
|
||||
d) configure
|
||||
e) make
|
||||
f) make install
|
||||
|
||||
The library will end up in /local/lib
|
||||
|
||||
|
||||
4) Compile MiniUPnP.
|
||||
up to release v1.3 now.
|
||||
links:
|
||||
http://miniupnp.free.fr/
|
||||
http://miniupnp.free.fr/files/
|
||||
|
||||
5) Install pthreads
|
||||
|
||||
links:
|
||||
http://sourceware.org/pthreads-win32/
|
||||
ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-8-0-release.exe
|
||||
|
||||
/******************* Compiling Support Libraries ******************/
|
||||
/*********************** Compiling Retroshare *********************/
|
||||
|
||||
1) Setup Source Code TAR (TODO: Update for windows).
|
||||
This is useful as it provides a standard structure to work against,
|
||||
we will update this with the required support libraries soon.
|
||||
|
||||
a) Open MSYS Bash shell, and change to this location.
|
||||
c:/msys/1.0/home/XXX/retroshare
|
||||
b) create a working directory under this.
|
||||
1) c:/msys/1.0/home/XXX/miniupnpc-1.3
|
||||
2) c:/msys/1.0/home/XXX/pthreads-w32-2-8-0-release
|
||||
3) c:/msys/1.0/home/XXX/zlib-1.2.3
|
||||
4) c:/msys/1.0/home/XXX/gpgme-1.1.8
|
||||
5) c:/msys/1.0/home/XXX/libgpg-error-1.7
|
||||
c) Install OpenSSL to this directory (get from http://www.slproweb.com/download/Win32OpenSSL-0_9_8k.exe)
|
||||
1) c:/msys/1.0/home/XXX/OpenSSL
|
||||
d) get the latest svn trunk and unzip to (get from http://retroshare.svn.sourceforge.net/viewvc/retroshare/trunk.tar.gz) .
|
||||
1) c:/msys/1.0/home/XXX/retroshare
|
||||
|
||||
2) Compile libretroshare
|
||||
a) change to the libretroshare directory.
|
||||
1) cd c:/msys/1.0/home/XXX/retroshare/libretroshare/src
|
||||
b) modify support directory paths to point at correct locations.
|
||||
1) open scripts/config.mk and enable OS = Win # MinGw.
|
||||
2) open scripts/config-mingw.mk
|
||||
3) disable lines (line 20-29)
|
||||
4) enable lines 34-42 and 118-119
|
||||
#### Enable this section for compiling with MSYS/MINGW compile
|
||||
SRC_ROOT=/home/linux
|
||||
|
||||
SSL_DIR=$(SRC_ROOT)/OpenSSL
|
||||
GPGME_DIR=$(SRC_ROOT)/gpgme-1.1.8
|
||||
GPG_ERROR_DIR=$(SRC_ROOT)/libgpg-error-1.7
|
||||
|
||||
ZLIB_DIR=$(SRC_ROOT)/zlib-1.2.3
|
||||
UPNPC_DIR=$(SRC_ROOT)/miniupnpc-1.0
|
||||
PTHREADS_DIR=$(SRC_ROOT)/pthreads-w32-2-8-0-release
|
||||
|
||||
### Enable this for GPGME and GPG ERROR dirs
|
||||
CFLAGS += -I$(GPGME_DIR)/src
|
||||
CFLAGS += -I$(GPG_ERROR_DIR)/src
|
||||
5) Edit your SRC_ROOT to your /home/xxxx dir
|
||||
|
||||
|
||||
c) compile libretroshare
|
||||
1) make
|
||||
|
||||
3) Copy all the required support libraries into the library directory:
|
||||
c:/msys/1.0/home/XXX/lib
|
||||
|
||||
This should include:
|
||||
|
||||
gpg-error / gpgme libraries:
|
||||
libgpg-error.a
|
||||
libgpg-error.dll.a
|
||||
libgpg-error.la
|
||||
libgpgme.dll.a
|
||||
libgpgme.la
|
||||
|
||||
openssl libraries:
|
||||
libcrypto.a
|
||||
libssl.a
|
||||
|
||||
miniupnpc / pthreads / libz
|
||||
|
||||
libminiupnpc.a
|
||||
miniupnpc.dll
|
||||
libpthreadGCE2.a
|
||||
pthreadGCE2.dll
|
||||
libz.a
|
||||
|
||||
extra library from cygwin. (renamed from libcrypt32.a)
|
||||
libcrypt32-cygwin.a
|
||||
|
||||
libretroshare
|
||||
libretroshare.a
|
||||
|
||||
|
||||
4) Compile GUI.
|
||||
|
||||
/*********************** Compiling Retroshare *********************/
|
||||
/************************ Running Retroshare **********************/
|
||||
|
||||
1) Collect Relevant DLL's with Application.
|
||||
a) make retroshare application directory.
|
||||
b) copy Retroshare.exe executable into directory.
|
||||
c) Copy required DLL's into that directory.
|
||||
These include:
|
||||
|
||||
mingw runtime:
|
||||
mingwm10.dll
|
||||
|
||||
gpgme/gpg-error:
|
||||
libgpg-error-0.dll
|
||||
libgpgme-11.dll
|
||||
|
||||
other support libraries:
|
||||
miniupnpc.dll
|
||||
pthreadGCE2.dll
|
||||
|
||||
Qt DLLs:
|
||||
QtCore4.dll,
|
||||
QtGui4.dll,...
|
||||
etc.
|
||||
|
||||
d) You should be able to run Retroshare from this directory now!
|
||||
|
||||
2) Install gpg4win package... for gpg.exe
|
||||
|
||||
3) Create an OpenGPG identity using gpa or Enigmail or other PGP tool.
|
||||
|
||||
4) You should be able to use Retroshare on your PC now.
|
||||
|
||||
/************************ Running Retroshare **********************/
|
||||
/******************************** FAQs ****************************/
|
||||
Ask and it might be answered!
|
||||
/******************************** FAQs ****************************/
|
||||
|
||||
This is obviously a work in progress -
|
||||
drBob.
|
||||
|
||||
|
||||
|
||||
|
||||
|
46
build_scripts/Windows/make_installer.sh
Executable file
46
build_scripts/Windows/make_installer.sh
Executable file
@ -0,0 +1,46 @@
|
||||
#!/bin/sh
|
||||
###
|
||||
#
|
||||
#
|
||||
#
|
||||
|
||||
QTGUI_DIR=../devel/retroshare-package-v0.4.09b/src/svn-retroshare/retroshare-gui/src
|
||||
INST_DIR=../devel/retroshare-package-v0.4.09b/src/svn-retroshare/build_scripts/Windows/
|
||||
EXEC=release/Retroshare.exe
|
||||
|
||||
BIN_DIR=./release
|
||||
NSIS_EXE="/cygdrive/c/Program\ Files/NSIS/makensis.exe"
|
||||
|
||||
cp $QTGUI_DIR/$EXEC $BIN_DIR
|
||||
echo cp $QTGUI_DIR/$EXEC $BIN_DIR
|
||||
|
||||
# copy skin files.
|
||||
cp -r $QTGUI_DIR/qss/* $BIN_DIR/qss
|
||||
echo 'cp -r $QTGUI_DIR/qss/* $BIN_DIR/qss'
|
||||
#
|
||||
cp -r $QTGUI_DIR/skin/* $BIN_DIR/skin
|
||||
echo 'cp $QTGUI_DIR/skin/* $BIN_DIR/skin'
|
||||
#
|
||||
cp -r $QTGUI_DIR/emoticons/* $BIN_DIR/emoticons/
|
||||
echo cp -r $QTGUI_DIR/emoticons/* $BIN_DIR/emoticons/
|
||||
|
||||
cp -r $QTGUI_DIR/style/* $BIN_DIR/style
|
||||
echo 'cp -r $QTGUI_DIR/style/* $BIN_DIR/style'
|
||||
|
||||
./stripSVN.sh release
|
||||
echo ./stripSVN.sh release
|
||||
|
||||
cp $INST_DIR/retroshare.nsi ./
|
||||
echo cp $QTGUI_DIR/retroshare.nsi ./
|
||||
|
||||
cp $QTGUI_DIR/license/* ./license/
|
||||
echo cp $QTGUI_DIR/license/* ./license/
|
||||
|
||||
cp $QTGUI_DIR/gui/images/splash.bmp ./gui/images/
|
||||
echo cp $QTGUI_DIR/gui/images/splash.bmp ./gui/images/
|
||||
|
||||
#cp $QTGUI_DIR/release/changelog.txt ./release/changelog.txt
|
||||
#echo cp $QTGUI_DIR/release/changelog.txt ./release/changelog.txt
|
||||
|
||||
"/cygdrive/c/Program Files/NSIS/makensis.exe" retroshare.nsi
|
||||
#$NSIS_EXE retroshare.nsi
|
390
build_scripts/Windows/retroshare.nsi
Normal file
390
build_scripts/Windows/retroshare.nsi
Normal file
@ -0,0 +1,390 @@
|
||||
; Script generated with the Venis Install Wizard & modified by defnax
|
||||
|
||||
; Define your application name
|
||||
!define APPNAME "RetroShare"
|
||||
!define VERSION "0.4.13c"
|
||||
!define APPNAMEANDVERSION "${APPNAME} ${VERSION}"
|
||||
|
||||
; Main Install settings
|
||||
Name "${APPNAMEANDVERSION}"
|
||||
InstallDir "$PROGRAMFILES\RetroShare"
|
||||
InstallDirRegKey HKLM "Software\${APPNAME}" ""
|
||||
OutFile "RetroShare_${VERSION}_setup.exe"
|
||||
BrandingText "${APPNAMEANDVERSION}"
|
||||
; Use compression
|
||||
SetCompressor LZMA
|
||||
|
||||
; Modern interface settings
|
||||
!include Sections.nsh
|
||||
!include "MUI.nsh"
|
||||
|
||||
;Interface Settings
|
||||
!define MUI_ABORTWARNING
|
||||
;!define MUI_HEADERIMAGE
|
||||
;!define MUI_HEADERIMAGE_BITMAP "retroshare.bmp" ; optional
|
||||
|
||||
# MUI defines
|
||||
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\orange-install.ico"
|
||||
!define MUI_FINISHPAGE_NOAUTOCLOSE
|
||||
!define MUI_LICENSEPAGE_RADIOBUTTONS
|
||||
!define MUI_COMPONENTSPAGE_SMALLDESC
|
||||
!define MUI_FINISHPAGE_LINK "Visit the RetroShare forum for the latest news and support"
|
||||
!define MUI_FINISHPAGE_LINK_LOCATION "http://retroshare.sourceforge.net/forum/"
|
||||
!define MUI_FINISHPAGE_RUN "$INSTDIR\RetroShare.exe"
|
||||
!define MUI_FINISHPAGE_SHOWREADME $INSTDIR\changelog.txt
|
||||
!define MUI_FINISHPAGE_SHOWREADME_TEXT changelog.txt
|
||||
!define MUI_FINISHPAGE_SHOWREADME_NOTCHECKED
|
||||
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\orange-uninstall.ico"
|
||||
!define MUI_UNFINISHPAGE_NOAUTOCLOSE
|
||||
!define MUI_LANGDLL_REGISTRY_ROOT HKLM
|
||||
!define MUI_LANGDLL_REGISTRY_KEY ${REGKEY}
|
||||
!define MUI_LANGDLL_REGISTRY_VALUENAME InstallerLanguage
|
||||
|
||||
;!define MUI_WELCOMEPAGE_TEXT "This wizard will guide you through the installation of RetroShare. \r\n\r\nIt is recommended that you close all other applications before starting Setup. This will make it possible to update relevant system files without havinf to reboot your computer. \r\n\r\nIMPORTANT: Ensure that RetroShare is NOT RUNNING before continuing (you can exit from the taskbar menu), otherwise the installer cannot update the executables, and the installation will fail. \r\n\r\nClick Next to continue. "
|
||||
|
||||
;!define MUI_WELCOMEPAGE_TEXT "This wizard will guide you through the installation of RetroShare. \r\n\r\nIMPORTANT: Ensure that RetroShare is NOT RUNNING before continuing (you can exit from the taskbar menu), otherwise the installer cannot update the executables, and the installation will fail. \r\n\r\nClick Next to continue. "
|
||||
|
||||
|
||||
; Defines the un-/installer logo of RetroShare
|
||||
!insertmacro MUI_DEFAULT MUI_WELCOMEFINISHPAGE_BITMAP "${NSISDIR}\Contrib\Graphics\Wizard\orange.bmp"
|
||||
!insertmacro MUI_DEFAULT MUI_UNWELCOMEFINISHPAGE_BITMAP "${NSISDIR}\Contrib\Graphics\Wizard\orange-uninstall.bmp"
|
||||
|
||||
; Set languages (first is default language)
|
||||
!insertmacro MUI_RESERVEFILE_LANGDLL
|
||||
ReserveFile "${NSISDIR}\Plugins\AdvSplash.dll"
|
||||
|
||||
;--------------------------------
|
||||
;Configuration
|
||||
|
||||
|
||||
;!insertmacro MUI_RESERVEFILE_SPECIALBITMAP
|
||||
|
||||
LicenseLangString myLicenseData 1033 "license\license.txt"
|
||||
LicenseLangString myLicenseData 1031 "license\license-GER.txt"
|
||||
LicenseLangString myLicenseData 1036 "license\license-FR.txt"
|
||||
LicenseLangString myLicenseData 1055 "license\license-TR.txt"
|
||||
LicenseLangString myLicenseData 2052 "license\license.txt"
|
||||
LicenseLangString myLicenseData 1045 "license\license.txt"
|
||||
|
||||
LicenseData $(myLicenseData)
|
||||
|
||||
# Installer pages
|
||||
!insertmacro MUI_PAGE_WELCOME
|
||||
!insertmacro MUI_PAGE_LICENSE "$(myLicenseData)"
|
||||
!insertmacro MUI_PAGE_COMPONENTS
|
||||
!insertmacro MUI_PAGE_DIRECTORY
|
||||
!insertmacro MUI_PAGE_INSTFILES
|
||||
!insertmacro MUI_PAGE_FINISH
|
||||
!insertmacro MUI_UNPAGE_CONFIRM
|
||||
!insertmacro MUI_UNPAGE_INSTFILES
|
||||
|
||||
# Installer languages
|
||||
!define MUI_LANGDLL_ALLLANGUAGES
|
||||
|
||||
!insertmacro MUI_LANGUAGE English
|
||||
!insertmacro MUI_LANGUAGE German
|
||||
!insertmacro MUI_LANGUAGE French
|
||||
!insertmacro MUI_LANGUAGE Turkish
|
||||
!insertmacro MUI_LANGUAGE SimpChinese
|
||||
!insertmacro MUI_LANGUAGE Polish
|
||||
|
||||
|
||||
;Component-selection page
|
||||
;Titles
|
||||
|
||||
LangString sec_main ${LANG_ENGLISH} "Program Files"
|
||||
LangString sec_data ${LANG_ENGLISH} "Program Skins"
|
||||
LangString sec_shortcuts ${LANG_ENGLISH} "Shortcuts"
|
||||
LangString sec_link ${LANG_ENGLISH} "File Association"
|
||||
LangString sec_autostart ${LANG_ENGLISH} "Auto Startup"
|
||||
LangString DESC_sec_main ${LANG_ENGLISH} "Installs the RetroShare program files."
|
||||
LangString DESC_sec_data ${LANG_ENGLISH} "Installs RetroShare Skins"
|
||||
LangString DESC_sec_shortcuts ${LANG_ENGLISH} "Create RetroShare shortcut icons."
|
||||
LangString DESC_sec_link ${LANG_ENGLISH} "Associate RetroShare with .pqi file extension"
|
||||
LangString DESC_sec_autostart ${LANG_ENGLISH} "Auto-Run and Login at Startup"
|
||||
LangString LANGUAGEID ${LANG_ENGLISH} "1033"
|
||||
|
||||
|
||||
LangString sec_main ${LANG_FRENCH} "RetroShare"
|
||||
LangString sec_data ${LANG_FRENCH} "Programme de Skins"
|
||||
LangString sec_shortcuts ${LANG_FRENCH} "Raccourcis"
|
||||
LangString sec_link ${LANG_FRENCH} "RetroShare fichiers Association"
|
||||
LangString sec_startmenu ${LANG_FRENCH} "Raccourcis du menu Démarrer"
|
||||
LangString sec_autostart ${LANG_FRENCH} "Démarrage automatique"
|
||||
LangString DESC_sec_main ${LANG_FRENCH} "Installe les fichiers du programme."
|
||||
LangString DESC_sec_data ${LANG_FRENCH} "Installe RetroShare Skins"
|
||||
LangString DESC_sec_startmenu ${LANG_FRENCH} "Crée les raccourcis du menu Démarrer"
|
||||
LangString DESC_sec_shortcuts ${LANG_FRENCH} "Crée une icône sur le bureau."
|
||||
LangString DESC_sec_link ${LANG_FRENCH} "Associate RetroShare with .pqi file extension"
|
||||
LangString DESC_sec_autostart ${LANG_FRENCH} "Run and Auto-connexion au démarrage"
|
||||
LangString LANGUAGEID ${LANG_FRENCH} "1036"
|
||||
|
||||
|
||||
LangString sec_main ${LANG_GERMAN} "Programmdateien"
|
||||
LangString sec_data ${LANG_GERMAN} "Skins f<>r das Programm"
|
||||
LangString sec_shortcuts ${LANG_GERMAN} "Shortcuts"
|
||||
LangString sec_link ${LANG_GERMAN} "Dateiverkn<EFBFBD>pfungen"
|
||||
LangString sec_autostart ${LANG_GERMAN} "Auto Startup"
|
||||
LangString DESC_sec_main ${LANG_GERMAN} "Installiert die erforderlichen Programmdateien."
|
||||
LangString DESC_sec_data ${LANG_GERMAN} "Installiert RetroShare Skins"
|
||||
LangString DESC_sec_shortcuts ${LANG_GERMAN} "Erstellt eine RetroShare Verkn<6B>pfung im Startmen<65>, Desktop oder im Schnellstarter."
|
||||
LangString DESC_sec_link ${LANG_GERMAN} "RetroShare mit .pqi Dateien verkn<6B>pfen"
|
||||
LangString DESC_sec_autostart ${LANG_GERMAN} "Beim Neustart automatisch RetroShare starten und sich anmelden"
|
||||
LangString LANGUAGEID ${LANG_GERMAN} "1031"
|
||||
|
||||
LangString sec_main ${LANG_TURKISH} "Program Dosyalar<61>"
|
||||
LangString sec_data ${LANG_TURKISH} "Program Skinleri"
|
||||
LangString sec_shortcuts ${LANG_TURKISH} "Shortcut'lar"
|
||||
LangString sec_link ${LANG_TURKISH} ".pqi Dosya Kaydet"
|
||||
LangString sec_autostart ${LANG_TURKISH} "Otomatik calistir ve baglan"
|
||||
LangString DESC_sec_main ${LANG_TURKISH} "Program dosyalar<61>n<EFBFBD> kurar."
|
||||
LangString DESC_sec_data ${LANG_TURKISH} "RetroShare Skin'leri kurar"
|
||||
LangString DESC_sec_shortcuts ${TURKISH} "Shortcut yap Start menu , Desktop veya Quicklaunchbar icin."
|
||||
LangString DESC_sec_link ${LANG_TURKISH} "RetroShare .pqi almas<61> i<>in kaydettirir"
|
||||
LangString DESC_sec_autostart ${LANG_TURKISH} "Isletim sistemi acildiginda Otomatik olarak calistir ve baglan"
|
||||
LangString LANGUAGEID ${LANG_TURKISH} "1055"
|
||||
|
||||
LangString sec_main ${LANG_SIMPCHINESE} "程序文件"
|
||||
LangString sec_data ${LANG_SIMPCHINESE} "程序皮肤"
|
||||
LangString sec_shortcuts ${LANG_SIMPCHINESE} "快捷方式"
|
||||
LangString sec_link ${LANG_SIMPCHINESE} "RetroShare文件关联"
|
||||
LangString sec_autostart ${LANG_SIMPCHINESE} "自动启动"
|
||||
LangString DESC_sec_main ${LANG_SIMPCHINESE} "安装RetroShare程序"
|
||||
LangString DESC_sec_data ${LANG_SIMPCHINESE} "安装RetroShare皮肤"
|
||||
LangString DESC_sec_shortcuts ${LANG_SIMPCHINESE} "建RetroShare快捷方式"
|
||||
LangString DESC_sec_link ${LANG_SIMPCHINESE} "关联.pqi扩展名"
|
||||
LangString DESC_sec_autostart ${LANG_SIMPCHINESE} "启动时自动运行和登录"
|
||||
LangString LANGUAGEID ${LANG_SIMPCHINESE} "2052"
|
||||
|
||||
LangString sec_main ${LANG_POLISH} "Pliki programu"
|
||||
LangString sec_data ${LANG_POLISH} "Skórki"
|
||||
LangString sec_shortcuts ${LANG_POLISH} "Skróty"
|
||||
LangString sec_link ${LANG_POLISH} "Skojarz pliki"
|
||||
LangString sec_autostart ${LANG_POLISH} "Automatyczne uruchamianie"
|
||||
LangString DESC_sec_main ${LANG_POLISH} "Instaluje pliki programu RetroShare"
|
||||
LangString DESC_sec_data ${LANG_POLISH} "Instaluje skórki programu RetroShare"
|
||||
LangString DESC_sec_shortcuts ${LANG_POLISH} "Utwórz ikony skrótów na pulpicie, w menu start oraz na pasku szybkiego uruchamiania."
|
||||
LangString DESC_sec_link ${LANG_POLISH} "Skojarz pliki o rozszerzeniu .pqi z RetroShare"
|
||||
LangString DESC_sec_autostart ${LANG_POLISH} "Uruchom i zaloguj podczas startu systemu"
|
||||
LangString LANGUAGEID ${LANG_POLISH} "1045"
|
||||
|
||||
|
||||
|
||||
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS
|
||||
|
||||
Section $(sec_main) sec_main
|
||||
|
||||
;Set Section required
|
||||
SectionIn RO
|
||||
|
||||
; Set Section properties
|
||||
SetOverwrite on
|
||||
|
||||
; Clears previous error logs
|
||||
Delete "$INSTDIR\*.log"
|
||||
|
||||
; Set Section Files and Shortcuts
|
||||
SetOutPath "$INSTDIR\"
|
||||
File /r "release\RetroShare.exe"
|
||||
File /r "D:\Qt\2009.02\mingw\bin\mingwm10.dll"
|
||||
File /r "D:\Qt\2009.02\qt\bin\QtCore4.dll"
|
||||
File /r "D:\Qt\2009.02\qt\bin\QtGui4.dll"
|
||||
File /r "D:\Qt\2009.02\qt\bin\QtNetwork4.dll"
|
||||
File /r "D:\Qt\2009.02\qt\bin\QtXml4.dll"
|
||||
File /r "D:\Qt\2009.02\qt\bin\QtScript4.dll"
|
||||
File /r "pthreadGCE2.dll"
|
||||
File /r "pthreadGC2d.dll"
|
||||
File /r "changelog.txt"
|
||||
|
||||
|
||||
|
||||
SectionEnd
|
||||
|
||||
Section $(sec_data) sec_data
|
||||
|
||||
; Set Section properties
|
||||
SetOverwrite on
|
||||
|
||||
; Set Section Files and Shortcuts
|
||||
SetOutPath "$APPDATA\RetroShare\"
|
||||
;File /r "data\*"
|
||||
|
||||
; We're not ready for external skins...
|
||||
; Set Section qss need to remove svn path
|
||||
SetOutPath "$INSTDIR\qss\"
|
||||
File /r qss\*.*
|
||||
|
||||
; Set Section skin
|
||||
; SetOutPath "$INSTDIR\skin\"
|
||||
; File /r release\skin\*.*
|
||||
|
||||
; Add emoticons
|
||||
SetOutPath "$INSTDIR\emoticons\"
|
||||
File /r emoticons\*.*
|
||||
|
||||
; Add Chat Style
|
||||
SetOutPath "$INSTDIR\style\"
|
||||
File /r style\*.*
|
||||
|
||||
SectionEnd
|
||||
|
||||
Section $(sec_link) sec_link
|
||||
; Delete any existing keys
|
||||
|
||||
|
||||
; Write the file association
|
||||
WriteRegStr HKCR .pqi "" retroshare
|
||||
WriteRegStr HKCR retroshare "" "PQI File"
|
||||
WriteRegBin HKCR retroshare EditFlags 00000100
|
||||
WriteRegStr HKCR "retroshare\shell" "" open
|
||||
WriteRegStr HKCR "retroshare\shell\open\command" "" `"$INSTDIR\RetroShare.exe" "%1"`
|
||||
|
||||
SectionEnd
|
||||
|
||||
SectionGroup $(sec_shortcuts) sec_shortcuts
|
||||
Section StartMenu SEC0001
|
||||
|
||||
SetOutPath "$INSTDIR"
|
||||
CreateDirectory "$SMPROGRAMS\${APPNAME}"
|
||||
CreateShortCut "$SMPROGRAMS\${APPNAME}\$(^UninstallLink).lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
|
||||
CreateShortCut "$SMPROGRAMS\${APPNAME}\${APPNAME}.lnk" "$INSTDIR\RetroShare.exe" "" "$INSTDIR\RetroShare.exe" 0
|
||||
|
||||
SectionEnd
|
||||
|
||||
Section Desktop SEC0002
|
||||
|
||||
|
||||
CreateShortCut "$DESKTOP\${APPNAME}.lnk" "$INSTDIR\RetroShare.exe" "" "$INSTDIR\RetroShare.exe" 0
|
||||
|
||||
SectionEnd
|
||||
|
||||
Section Quicklaunchbar SEC0003
|
||||
|
||||
|
||||
CreateShortCut "$QUICKLAUNCH\${APPNAME}.lnk" "$INSTDIR\RetroShare.exe" "" "$INSTDIR\RetroShare.exe" 0
|
||||
|
||||
SectionEnd
|
||||
SectionGroupEnd
|
||||
|
||||
;Section $(sec_autostart) sec_autostart
|
||||
|
||||
; WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Run" "RetroRun" "$INSTDIR\${APPNAME}.exe -a"
|
||||
|
||||
;SectionEnd
|
||||
|
||||
Section $(sec_autostart) sec_autostart
|
||||
|
||||
CreateShortCut "$SMSTARTUP\${APPNAME}.lnk" "$INSTDIR\RetroShare.exe" "" "$INSTDIR\RetroShare.exe" 0
|
||||
SectionEnd
|
||||
|
||||
|
||||
Section -FinishSection
|
||||
|
||||
WriteRegStr HKLM "Software\${APPNAME}" "" "$INSTDIR"
|
||||
WriteRegStr HKLM "Software\${APPNAME}" "Version" "${VERSION}"
|
||||
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "DisplayName" "${APPNAME}"
|
||||
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "UninstallString" "$INSTDIR\uninstall.exe"
|
||||
WriteUninstaller "$INSTDIR\uninstall.exe"
|
||||
|
||||
SectionEnd
|
||||
|
||||
|
||||
|
||||
;--------------------------------
|
||||
;Descriptions
|
||||
|
||||
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
|
||||
!insertmacro MUI_DESCRIPTION_TEXT ${sec_main} $(DESC_sec_main)
|
||||
!insertmacro MUI_DESCRIPTION_TEXT ${sec_data} $(DESC_sec_data)
|
||||
!insertmacro MUI_DESCRIPTION_TEXT ${sec_shortcuts} $(DESC_sec_shortcuts)
|
||||
!insertmacro MUI_DESCRIPTION_TEXT ${sec_link} $(DESC_sec_link)
|
||||
!insertmacro MUI_DESCRIPTION_TEXT ${sec_autostart} $(DESC_sec_autostart)
|
||||
!insertmacro MUI_FUNCTION_DESCRIPTION_END
|
||||
|
||||
;Uninstall section
|
||||
Section "Uninstall"
|
||||
|
||||
; Remove file association registry keys
|
||||
DeleteRegKey HKCR .pqi
|
||||
DeleteRegKey HKCR retroshare
|
||||
|
||||
; Remove program/uninstall regsitry keys
|
||||
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}"
|
||||
DeleteRegKey HKLM SOFTWARE\${APPNAME}
|
||||
|
||||
DeleteRegValue HKCU "Software\Microsoft\Windows\CurrentVersion\Run" "RetroRun"
|
||||
|
||||
; Remove files and uninstaller
|
||||
Delete $INSTDIR\RetroShare.exe
|
||||
Delete $INSTDIR\*.dll
|
||||
Delete $INSTDIR\*.dat
|
||||
Delete $INSTDIR\*.txt
|
||||
Delete $INSTDIR\*.ini
|
||||
Delete $INSTDIR\*.log
|
||||
|
||||
Delete $INSTDIR\uninstall.exe
|
||||
|
||||
; Remove the kadc.ini file.
|
||||
; Don't remove the directory, otherwise
|
||||
; we lose the XPGP keys.
|
||||
; Should make this an option though...
|
||||
Delete "$APPDATA\${APPNAME}\kadc.ini"
|
||||
Delete "$APPDATA\${APPNAME}\*.cfg"
|
||||
Delete "$APPDATA\${APPNAME}\*.conf"
|
||||
Delete "$APPDATA\${APPNAME}\*.log-save"
|
||||
Delete "$APPDATA\${APPNAME}\*.log"
|
||||
Delete "$APPDATA\${APPNAME}\*.failed"
|
||||
|
||||
RMDir /r "$APPDATA\${APPNAME}\cache"
|
||||
RMDir /r "$APPDATA\${APPNAME}\Partials"
|
||||
|
||||
|
||||
; Remove shortcuts, if any
|
||||
Delete "$SMPROGRAMS\${APPNAME}\*.*"
|
||||
|
||||
; Remove desktop shortcut
|
||||
Delete "$DESKTOP\${APPNAME}.lnk"
|
||||
|
||||
; Remove Quicklaunch shortcut
|
||||
Delete "$QUICKLAUNCH\${APPNAME}.lnk"
|
||||
|
||||
; Remove Autstart
|
||||
Delete "$SMSTARTUP\${APPNAME}.lnk"
|
||||
|
||||
; Remove directories used
|
||||
RMDir "$SMPROGRAMS\${APPNAME}"
|
||||
RMDir /r "$INSTDIR"
|
||||
RMDir /r "$INSTDIR\qss"
|
||||
RMDir /r "$INSTDIR\emoticons"
|
||||
RMDir /r "$INSTDIR\style"
|
||||
|
||||
SectionEnd
|
||||
|
||||
Function .onInit
|
||||
|
||||
InitPluginsDir
|
||||
Push $R1
|
||||
File /oname=$PLUGINSDIR\spltmp.bmp "gui\images\splash.bmp"
|
||||
advsplash::show 1200 1000 1000 -1 $PLUGINSDIR\spltmp
|
||||
Pop $R1
|
||||
Pop $R1
|
||||
!insertmacro MUI_LANGDLL_DISPLAY
|
||||
|
||||
|
||||
|
||||
FunctionEnd
|
||||
|
||||
|
||||
# Installer Language Strings
|
||||
# TODO Update the Language Strings with the appropriate translations.
|
||||
|
||||
LangString ^UninstallLink ${LANG_ENGLISH} "Uninstall"
|
||||
LangString ^UninstallLink ${LANG_GERMAN} "Deinstallieren"
|
||||
LangString ^UninstallLink ${LANG_TURKISH} "Kald<EFBFBD>r"
|
||||
LangString ^UninstallLink ${LANG_FRENCH} "Désinstaller"
|
||||
LangString ^UninstallLink ${LANG_SIMPCHINESE} "卸载"
|
||||
LangString ^UninstallLink ${LANG_POLISH} "Odinstaluj"
|
||||
|
||||
|
||||
|
||||
; eof
|
12
build_scripts/Windows/stripSVN.sh
Executable file
12
build_scripts/Windows/stripSVN.sh
Executable file
@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
|
||||
|
||||
if [ $# -le 0 ]
|
||||
then
|
||||
echo usage $0 directory
|
||||
exit
|
||||
fi
|
||||
|
||||
echo find $1 -name .svn
|
||||
rm -vrf `find $1 -name .svn`
|
||||
|
23
libbitdht/src/README.txt
Normal file
23
libbitdht/src/README.txt
Normal file
@ -0,0 +1,23 @@
|
||||
|
||||
What's in the Package
|
||||
---------------------------------------------------------------
|
||||
bitdht - base BitDHT Code.
|
||||
util - generic utils for networking and threading.
|
||||
udp - UDP interfacing code.
|
||||
lib - Where the library is created.
|
||||
tests - basic unit tests.
|
||||
example - example code of how to use libbitdht.
|
||||
libbitdht.pro - build script for Qt's qmake.
|
||||
README.txt - this file.
|
||||
|
||||
|
||||
HOWTO libbitdht.
|
||||
----------------------------------------------
|
||||
This version is build using Qt's qmake system.
|
||||
|
||||
1) Install Qt's qmake system: libqt-dev
|
||||
2) type ./qmake
|
||||
3) type ./make
|
||||
4) check out the example and tests to learn how to interface with libbitdht.
|
||||
|
||||
|
44
libbitdht/src/bitdht/Makefile
Normal file
44
libbitdht/src/bitdht/Makefile
Normal file
@ -0,0 +1,44 @@
|
||||
CPPFLAGS += -g -Wall -D BE_DEBUG -lpthread
|
||||
LDFLAGS += -g -Wall -D BE_DEBUG -lpthread
|
||||
CFLAGS += -g -Wall -D BE_DEBUG
|
||||
CC = g++
|
||||
LIB = -lpthread
|
||||
CPPLIB = -lpthread
|
||||
|
||||
EXEC = bdmsgs_test bdmetric_test bdquery_test bdspace_test bdspace_test2 bdnode_test bdnode_test2 bdstore_test
|
||||
EXEC += bdnode_multitest1 bdmidids_test
|
||||
EXEC += udpbitdht_nettest bencode_test
|
||||
EXEC += bdmgr_multitest
|
||||
#EXEC += bdudp_test
|
||||
|
||||
all: $(EXEC)
|
||||
|
||||
OBJ = bencode.o bdmsgs.o bdobj.o
|
||||
OBJ += bdpeer.o bdquery.o bdnode.o bdstore.o bdhash.o
|
||||
OBJ += bdmanager.o bdstddht.o
|
||||
|
||||
# udp base objs
|
||||
OBJ += bdthreads.o udplayer.o udpstack.o
|
||||
OBJ += udpbitdht.o
|
||||
|
||||
bdmsgs_test: $(OBJ) bdmsgs_test.o
|
||||
bdmetric_test: $(OBJ) bdmetric_test.o
|
||||
bdquery_test: $(OBJ) bdquery_test.o
|
||||
bdspace_test: $(OBJ) bdspace_test.o
|
||||
bdspace_test2: $(OBJ) bdspace_test2.o
|
||||
bdnode_test: $(OBJ) bdnode_test.o
|
||||
bdnode_test2: $(OBJ) bdnode_test2.o
|
||||
bdmidids_test: $(OBJ) bdmidids_test.o
|
||||
|
||||
bdnode_multitest1: $(OBJ) bdnode_multitest1.o
|
||||
bdmgr_multitest: $(OBJ) bdmgr_multitest.o
|
||||
|
||||
bdstore_test: $(OBJ) bdstore_test.o
|
||||
bdudp_test: $(OBJ) bdudp_test.o
|
||||
udpbitdht_nettest: $(OBJ) udpbitdht_nettest.o
|
||||
bencode_test: $(OBJ) bencode_test.o
|
||||
|
||||
clean:
|
||||
rm -f *.o core $(EXEC)
|
||||
|
||||
.PHONY: all clean
|
137
libbitdht/src/bitdht/bdaccount.cc
Normal file
137
libbitdht/src/bitdht/bdaccount.cc
Normal file
@ -0,0 +1,137 @@
|
||||
/*
|
||||
* bitdht/bdaccount.cc
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2011 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 "bitdht/bdaccount.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
#define LPF_FACTOR (0.90)
|
||||
|
||||
bdAccount::bdAccount()
|
||||
:mNoStats(BDACCOUNT_NUM_ENTRIES),
|
||||
mCountersOut(BDACCOUNT_NUM_ENTRIES), mCountersRecv(BDACCOUNT_NUM_ENTRIES),
|
||||
mLpfOut(BDACCOUNT_NUM_ENTRIES), mLpfRecv(BDACCOUNT_NUM_ENTRIES),
|
||||
mLabel(BDACCOUNT_NUM_ENTRIES)
|
||||
{
|
||||
|
||||
mLabel[BDACCOUNT_MSG_OUTOFDATEPING] = "OUTOFDATEPING ";
|
||||
mLabel[BDACCOUNT_MSG_PING] = "PING ";
|
||||
mLabel[BDACCOUNT_MSG_PONG] = "PONG ";
|
||||
mLabel[BDACCOUNT_MSG_QUERYNODE] = "QUERYNODE ";
|
||||
mLabel[BDACCOUNT_MSG_QUERYHASH] = "QUERYHASH ";
|
||||
mLabel[BDACCOUNT_MSG_REPLYFINDNODE] = "REPLYFINDNODE ";
|
||||
mLabel[BDACCOUNT_MSG_REPLYQUERYHASH] = "REPLYQUERYHASH ";
|
||||
|
||||
mLabel[BDACCOUNT_MSG_POSTHASH] = "POSTHASH ";
|
||||
mLabel[BDACCOUNT_MSG_REPLYPOSTHASH] = "REPLYPOSTHASH ";
|
||||
|
||||
mLabel[BDACCOUNT_MSG_CONNECTREQUEST] = "CONNECTREQUEST ";
|
||||
mLabel[BDACCOUNT_MSG_CONNECTREPLY] = "CONNECTREPLY ";
|
||||
mLabel[BDACCOUNT_MSG_CONNECTSTART] = "CONNECTSTART ";
|
||||
mLabel[BDACCOUNT_MSG_CONNECTACK] = "CONNECTACK ";
|
||||
|
||||
resetStats();
|
||||
}
|
||||
|
||||
|
||||
void bdAccount::incCounter(uint32_t idx, bool out)
|
||||
{
|
||||
if (idx > mNoStats-1)
|
||||
{
|
||||
std::cerr << "bdAccount::incCounter() Invalid Index";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
|
||||
if (out)
|
||||
{
|
||||
mCountersOut[idx]++;
|
||||
}
|
||||
else
|
||||
{
|
||||
mCountersRecv[idx]++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void bdAccount::doStats()
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < mNoStats; i++)
|
||||
{
|
||||
mLpfOut[i] *= (LPF_FACTOR) ;
|
||||
mLpfOut[i] += (1.0 - LPF_FACTOR) * mCountersOut[i];
|
||||
|
||||
mLpfRecv[i] *= (LPF_FACTOR) ;
|
||||
mLpfRecv[i] += (1.0 - LPF_FACTOR) * mCountersRecv[i];
|
||||
}
|
||||
resetCounters();
|
||||
}
|
||||
|
||||
void bdAccount::printStats(std::ostream &out)
|
||||
{
|
||||
int i;
|
||||
out << " Send Recv: ";
|
||||
out << std::endl;
|
||||
for(i = 0; i < mNoStats; i++)
|
||||
{
|
||||
|
||||
out << "Send" << mLabel[i] << " : " << std::setw(10) << mLpfOut[i];
|
||||
out << " ";
|
||||
out << "Recv" << mLabel[i] << " : " << std::setw(10) << mLpfRecv[i];
|
||||
out << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void bdAccount::resetCounters()
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < mNoStats; i++)
|
||||
{
|
||||
mCountersOut[i] = 0;
|
||||
mCountersRecv[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void bdAccount::resetStats()
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < mNoStats; i++)
|
||||
{
|
||||
mLpfOut[i] = 0;
|
||||
mLpfRecv[i] = 0;
|
||||
}
|
||||
resetCounters();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
79
libbitdht/src/bitdht/bdaccount.h
Normal file
79
libbitdht/src/bitdht/bdaccount.h
Normal file
@ -0,0 +1,79 @@
|
||||
#ifndef BITDHT_ACCOUNT_H
|
||||
#define BITDHT_ACCOUNT_H
|
||||
|
||||
/*
|
||||
* bitdht/bdaccount.h
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2011 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 <vector>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#define BDACCOUNT_MSG_OUTOFDATEPING 0
|
||||
#define BDACCOUNT_MSG_PING 1
|
||||
#define BDACCOUNT_MSG_PONG 2
|
||||
#define BDACCOUNT_MSG_QUERYNODE 3
|
||||
#define BDACCOUNT_MSG_QUERYHASH 4
|
||||
#define BDACCOUNT_MSG_REPLYFINDNODE 5
|
||||
#define BDACCOUNT_MSG_REPLYQUERYHASH 6
|
||||
|
||||
#define BDACCOUNT_MSG_POSTHASH 7
|
||||
#define BDACCOUNT_MSG_REPLYPOSTHASH 8
|
||||
|
||||
#define BDACCOUNT_MSG_CONNECTREQUEST 9
|
||||
#define BDACCOUNT_MSG_CONNECTREPLY 10
|
||||
#define BDACCOUNT_MSG_CONNECTSTART 11
|
||||
#define BDACCOUNT_MSG_CONNECTACK 12
|
||||
|
||||
#define BDACCOUNT_NUM_ENTRIES 13
|
||||
|
||||
class bdAccount
|
||||
{
|
||||
public:
|
||||
|
||||
bdAccount();
|
||||
|
||||
void incCounter(uint32_t idx, bool out);
|
||||
void doStats();
|
||||
void printStats(std::ostream &out);
|
||||
void resetCounters();
|
||||
void resetStats();
|
||||
|
||||
private:
|
||||
|
||||
int mNoStats;
|
||||
|
||||
std::vector<double> mCountersOut;
|
||||
std::vector<double> mCountersRecv;
|
||||
|
||||
std::vector<double> mLpfOut;
|
||||
std::vector<double> mLpfRecv;
|
||||
|
||||
std::vector<std::string> mLabel;
|
||||
// Statistics.
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // BITDHT_ACCOUNT_H
|
500
libbitdht/src/bitdht/bdboot.txt
Normal file
500
libbitdht/src/bitdht/bdboot.txt
Normal file
@ -0,0 +1,500 @@
|
||||
108.36.64.33 58209
|
||||
89.160.187.217 16932
|
||||
84.124.224.199 56885
|
||||
94.237.72.30 42945
|
||||
85.201.229.3 11452
|
||||
201.173.120.93 23428
|
||||
76.127.178.159 14380
|
||||
92.247.199.243 19710
|
||||
95.58.126.146 64477
|
||||
174.94.94.69 43503
|
||||
95.79.65.220 8087
|
||||
78.90.64.148 10231
|
||||
24.193.122.202 11511
|
||||
216.97.218.167 40124
|
||||
70.69.163.9 23456
|
||||
67.185.18.229 52572
|
||||
210.251.181.85 55531
|
||||
82.137.120.244 34738
|
||||
61.52.66.122 42160
|
||||
61.61.233.108 21637
|
||||
111.255.1.79 11783
|
||||
110.182.101.142 10347
|
||||
70.69.188.108 22559
|
||||
81.153.223.47 46074
|
||||
77.220.132.130 44444
|
||||
213.177.119.61 6881
|
||||
75.126.69.83 29903
|
||||
85.186.138.51 20478
|
||||
46.147.163.175 33427
|
||||
77.81.147.93 7937
|
||||
216.183.139.145 6681
|
||||
203.122.227.234 18471
|
||||
94.182.158.89 18819
|
||||
61.62.57.67 27226
|
||||
84.31.247.193 14144
|
||||
84.2.188.109 23962
|
||||
91.145.86.134 62952
|
||||
41.228.217.33 37400
|
||||
78.30.225.207 80
|
||||
64.53.221.48 38759
|
||||
79.131.81.227 10361
|
||||
85.56.11.182 15327
|
||||
68.101.25.86 30415
|
||||
61.227.170.158 15549
|
||||
178.44.156.94 5001
|
||||
83.46.22.193 61631
|
||||
24.178.72.100 4417
|
||||
87.254.152.23 57402
|
||||
213.22.241.214 27873
|
||||
46.73.100.190 11594
|
||||
95.78.217.2 21760
|
||||
195.245.119.20 7625
|
||||
95.136.244.101 31640
|
||||
91.146.43.182 35691
|
||||
95.134.156.22 27409
|
||||
184.82.35.166 6821
|
||||
84.50.94.139 26929
|
||||
95.133.87.134 45486
|
||||
59.9.209.241 40451
|
||||
83.226.67.223 12241
|
||||
216.37.139.190 59646
|
||||
77.97.241.193 54359
|
||||
95.134.154.114 60010
|
||||
61.103.124.160 15674
|
||||
46.64.80.102 35895
|
||||
99.118.0.136 53622
|
||||
188.235.132.82 52580
|
||||
188.173.99.140 42754
|
||||
71.195.234.69 45976
|
||||
83.172.22.81 55552
|
||||
80.221.169.92 18891
|
||||
2.82.212.216 61972
|
||||
98.77.175.116 33627
|
||||
121.174.151.70 26581
|
||||
81.102.255.173 35470
|
||||
110.159.178.96 10214
|
||||
190.16.216.71 52982
|
||||
80.162.37.81 22080
|
||||
213.124.220.198 6881
|
||||
77.248.101.169 51413
|
||||
173.49.168.99 51735
|
||||
213.5.78.19 17590
|
||||
86.88.195.220 51979
|
||||
79.114.26.233 40236
|
||||
79.130.39.45 26676
|
||||
90.199.157.147 37872
|
||||
96.48.156.192 13393
|
||||
85.226.120.222 39272
|
||||
78.63.251.197 18248
|
||||
89.221.25.71 51000
|
||||
83.226.167.119 22644
|
||||
88.120.240.30 37760
|
||||
82.225.93.93 53862
|
||||
84.48.59.74 27223
|
||||
212.117.4.117 51413
|
||||
92.148.10.30 54848
|
||||
62.216.126.134 35621
|
||||
84.250.34.176 25812
|
||||
109.165.126.178 41845
|
||||
81.23.163.247 25809
|
||||
79.50.33.239 26631
|
||||
124.191.192.176 48362
|
||||
76.114.42.202 33582
|
||||
173.55.160.70 14566
|
||||
58.196.220.183 24681
|
||||
95.28.1.159 60240
|
||||
81.170.186.164 39044
|
||||
46.7.204.107 35529
|
||||
109.73.104.152 37534
|
||||
90.200.237.165 23711
|
||||
46.70.27.140 10719
|
||||
207.161.37.213 18690
|
||||
80.117.226.21 30443
|
||||
77.38.42.20 9715
|
||||
92.239.12.100 54721
|
||||
110.174.165.63 39500
|
||||
88.88.84.245 12345
|
||||
91.121.26.60 55124
|
||||
109.239.213.19 41255
|
||||
92.47.45.110 13189
|
||||
76.216.237.156 64405
|
||||
118.168.4.98 8014
|
||||
111.91.225.146 6881
|
||||
124.89.124.129 18563
|
||||
94.8.56.115 29333
|
||||
109.207.176.90 17931
|
||||
91.217.5.1 41930
|
||||
92.112.169.230 29038
|
||||
62.162.51.133 61656
|
||||
125.27.152.162 8007
|
||||
60.216.222.200 11640
|
||||
118.81.110.97 16001
|
||||
112.202.153.252 49354
|
||||
70.84.216.90 15007
|
||||
89.40.176.80 52201
|
||||
67.184.198.91 55290
|
||||
95.52.255.243 18914
|
||||
91.83.156.215 34982
|
||||
151.51.17.237 35268
|
||||
92.99.25.17 14092
|
||||
46.164.215.90 14014
|
||||
212.52.56.16 63696
|
||||
89.178.129.27 18456
|
||||
89.88.248.180 59786
|
||||
109.96.108.144 48019
|
||||
178.206.21.232 12086
|
||||
99.37.202.87 51092
|
||||
94.248.194.63 48559
|
||||
188.232.154.148 6881
|
||||
99.73.91.225 51413
|
||||
117.200.148.179 16442
|
||||
122.120.128.224 14850
|
||||
220.248.146.129 56019
|
||||
113.141.183.16 10910
|
||||
41.218.252.74 29060
|
||||
203.218.78.175 51413
|
||||
94.176.131.60 27185
|
||||
77.122.219.96 6881
|
||||
94.27.105.217 28094
|
||||
99.62.146.221 16880
|
||||
188.83.195.167 43090
|
||||
121.214.19.48 21179
|
||||
213.87.123.205 65503
|
||||
67.82.123.5 37156
|
||||
46.70.161.55 63742
|
||||
70.82.91.50 61288
|
||||
218.91.146.70 50791
|
||||
125.252.61.71 63002
|
||||
90.213.160.205 35913
|
||||
78.235.221.72 59553
|
||||
178.121.52.96 60579
|
||||
87.247.62.167 27816
|
||||
118.172.31.159 23100
|
||||
82.17.243.247 43682
|
||||
109.173.41.131 47434
|
||||
89.254.192.115 57300
|
||||
222.5.240.97 21494
|
||||
190.25.44.167 10459
|
||||
41.99.59.57 26955
|
||||
125.230.193.210 16001
|
||||
91.137.156.131 52773
|
||||
109.92.201.235 17434
|
||||
82.141.87.41 32786
|
||||
109.75.197.236 39742
|
||||
94.236.149.79 54983
|
||||
84.202.212.85 47459
|
||||
74.210.181.212 39137
|
||||
82.233.137.114 16390
|
||||
87.55.32.94 42788
|
||||
41.233.84.104 37902
|
||||
68.84.52.55 36773
|
||||
41.230.239.93 54167
|
||||
89.123.145.26 10029
|
||||
129.95.72.236 56091
|
||||
74.138.196.114 62934
|
||||
95.37.81.137 35567
|
||||
14.194.100.227 42436
|
||||
81.164.46.76 6881
|
||||
74.102.22.47 11336
|
||||
213.114.29.161 50888
|
||||
78.90.176.124 56017
|
||||
192.11.19.2 38614
|
||||
192.11.19.2 38603
|
||||
98.228.57.239 57855
|
||||
96.23.56.6 12860
|
||||
86.151.199.161 43280
|
||||
220.230.99.102 53175
|
||||
213.57.146.235 36661
|
||||
99.244.175.108 18892
|
||||
98.232.219.193 50555
|
||||
93.86.22.133 22942
|
||||
81.198.240.88 46581
|
||||
95.153.176.117 33309
|
||||
121.2.134.222 13597
|
||||
118.209.164.73 44759
|
||||
78.57.193.144 29742
|
||||
109.105.15.232 62929
|
||||
86.101.214.205 60200
|
||||
69.138.195.57 54191
|
||||
83.212.200.132 12132
|
||||
77.77.26.194 9171
|
||||
69.196.186.81 36734
|
||||
203.160.24.156 43016
|
||||
94.247.88.154 24234
|
||||
46.118.116.49 48599
|
||||
87.93.8.216 15147
|
||||
70.92.75.203 29092
|
||||
76.22.97.29 39847
|
||||
178.213.213.100 37592
|
||||
77.96.97.40 51413
|
||||
24.189.102.125 42007
|
||||
118.169.120.177 12350
|
||||
115.135.179.213 13726
|
||||
98.251.72.191 56125
|
||||
84.74.112.151 34936
|
||||
84.51.88.213 59826
|
||||
86.106.112.169 28453
|
||||
95.135.143.126 50860
|
||||
178.140.187.211 54465
|
||||
178.121.100.43 35693
|
||||
2.217.35.228 21568
|
||||
220.211.30.194 54587
|
||||
122.145.130.70 16881
|
||||
87.207.15.170 29646
|
||||
82.239.79.88 44207
|
||||
109.187.146.145 10124
|
||||
89.135.117.177 61182
|
||||
122.169.146.174 16519
|
||||
77.78.26.66 8338
|
||||
41.104.30.0 17325
|
||||
117.11.157.108 16611
|
||||
79.116.50.118 44770
|
||||
116.74.101.72 43363
|
||||
86.126.196.48 35650
|
||||
60.241.61.19 37797
|
||||
79.113.28.247 12658
|
||||
92.113.35.173 52525
|
||||
123.110.9.189 23814
|
||||
200.104.141.151 27144
|
||||
70.94.54.203 28093
|
||||
188.26.254.79 22577
|
||||
188.254.130.80 10722
|
||||
88.118.111.251 24290
|
||||
109.173.106.39 6881
|
||||
109.252.32.107 33633
|
||||
176.14.235.3 57996
|
||||
188.226.77.216 48959
|
||||
86.72.177.79 20211
|
||||
95.84.248.77 36474
|
||||
98.226.183.179 62538
|
||||
173.176.31.176 61480
|
||||
98.110.164.233 18738
|
||||
109.169.129.229 33204
|
||||
67.189.89.157 54418
|
||||
218.28.216.174 15334
|
||||
41.132.206.240 17260
|
||||
124.234.185.101 22990
|
||||
95.247.246.235 22300
|
||||
91.66.51.19 4636
|
||||
115.187.48.222 19949
|
||||
41.218.206.184 51078
|
||||
109.205.248.138 34794
|
||||
129.2.86.70 19788
|
||||
178.59.141.236 58319
|
||||
78.100.18.29 12094
|
||||
220.100.30.198 27290
|
||||
2.133.72.50 18676
|
||||
95.34.94.2 7227
|
||||
77.123.178.198 35691
|
||||
95.29.188.15 14653
|
||||
98.80.131.126 50703
|
||||
123.193.24.234 18099
|
||||
93.155.151.134 19304
|
||||
78.29.92.93 17481
|
||||
93.87.39.186 40929
|
||||
193.110.115.71 61728
|
||||
81.27.61.29 64090
|
||||
187.69.27.142 25996
|
||||
118.172.11.101 10343
|
||||
202.176.172.192 17288
|
||||
93.178.199.55 25690
|
||||
118.169.14.92 10308
|
||||
173.14.30.134 45196
|
||||
188.17.235.68 18143
|
||||
188.17.235.68 18152
|
||||
80.251.134.66 25101
|
||||
88.178.72.2 7812
|
||||
78.27.33.114 15136
|
||||
14.136.5.8 20249
|
||||
151.203.29.130 50096
|
||||
24.10.47.12 55203
|
||||
76.187.211.94 59425
|
||||
195.67.238.121 15000
|
||||
89.82.71.87 6881
|
||||
78.150.5.162 27729
|
||||
81.155.121.101 6881
|
||||
62.122.5.70 11473
|
||||
213.196.160.129 6881
|
||||
195.91.155.91 24234
|
||||
178.150.196.145 35691
|
||||
77.20.253.136 6881
|
||||
99.192.107.151 29420
|
||||
93.182.146.26 15210
|
||||
89.132.74.190 27071
|
||||
94.192.17.72 59453
|
||||
46.107.134.155 46382
|
||||
77.66.223.100 25012
|
||||
175.137.178.223 16001
|
||||
120.147.48.118 6881
|
||||
61.73.149.123 57527
|
||||
46.9.225.86 51337
|
||||
77.77.34.49 53250
|
||||
46.118.64.164 38555
|
||||
72.91.173.234 53989
|
||||
108.0.13.192 13395
|
||||
96.234.5.159 50601
|
||||
95.143.147.128 47163
|
||||
216.228.194.176 49234
|
||||
92.107.1.68 47053
|
||||
92.52.48.200 64153
|
||||
85.157.73.46 44039
|
||||
213.22.59.123 33547
|
||||
194.44.174.33 11653
|
||||
173.179.46.189 37498
|
||||
98.196.139.105 28085
|
||||
98.213.2.57 25910
|
||||
173.192.155.242 15009
|
||||
210.51.59.72 8921
|
||||
84.108.167.190 20819
|
||||
78.46.106.133 51420
|
||||
77.223.225.140 46881
|
||||
118.233.249.25 19661
|
||||
118.223.202.132 34704
|
||||
93.74.54.75 41632
|
||||
178.41.52.249 23388
|
||||
189.33.41.183 57777
|
||||
108.11.139.67 50063
|
||||
178.130.19.250 22912
|
||||
24.6.228.21 24104
|
||||
14.32.5.68 44972
|
||||
173.63.112.3 14618
|
||||
24.47.92.66 56734
|
||||
76.107.89.84 18354
|
||||
81.236.195.180 18203
|
||||
68.52.17.145 8280
|
||||
184.59.91.226 49545
|
||||
50.18.3.51 11079
|
||||
68.13.217.94 30656
|
||||
80.64.26.155 48649
|
||||
82.233.85.22 38594
|
||||
69.84.120.59 51580
|
||||
24.131.35.42 43079
|
||||
189.122.104.141 50099
|
||||
78.96.226.99 52701
|
||||
68.80.227.54 55421
|
||||
188.49.22.126 17110
|
||||
89.215.54.73 35995
|
||||
112.173.202.157 31786
|
||||
188.246.77.253 29604
|
||||
182.21.19.42 12348
|
||||
68.148.101.110 53676
|
||||
67.184.242.108 47636
|
||||
68.102.204.198 45538
|
||||
109.148.249.57 49722
|
||||
217.43.247.199 26578
|
||||
87.15.161.70 29772
|
||||
87.255.16.8 21689
|
||||
98.82.237.179 27374
|
||||
67.184.213.33 61087
|
||||
68.209.144.92 24424
|
||||
88.198.183.157 32257
|
||||
219.84.8.213 20509
|
||||
78.63.251.219 54049
|
||||
188.143.45.245 33207
|
||||
178.207.61.146 48127
|
||||
92.37.38.199 34094
|
||||
76.255.178.99 55241
|
||||
187.113.243.20 29024
|
||||
219.126.158.155 62647
|
||||
95.172.127.31 52302
|
||||
94.66.254.203 24543
|
||||
24.179.64.210 15228
|
||||
78.139.225.244 45682
|
||||
76.234.131.193 41917
|
||||
93.91.229.98 64578
|
||||
82.244.203.153 21428
|
||||
112.201.99.184 24555
|
||||
78.62.147.218 40756
|
||||
86.186.163.106 24725
|
||||
188.168.87.217 56926
|
||||
201.79.56.34 51002
|
||||
60.240.20.17 52324
|
||||
95.42.221.189 21048
|
||||
113.147.245.39 7015
|
||||
86.219.130.100 33634
|
||||
46.229.50.135 64546
|
||||
188.115.160.24 22829
|
||||
2.125.135.95 41031
|
||||
84.212.218.243 15778
|
||||
174.96.234.186 55204
|
||||
178.79.21.107 29953
|
||||
67.188.133.52 32812
|
||||
184.155.124.198 49383
|
||||
119.152.32.140 44814
|
||||
93.7.182.144 45874
|
||||
95.7.188.130 54914
|
||||
24.69.34.86 60863
|
||||
84.208.115.8 19878
|
||||
70.52.174.137 53161
|
||||
88.168.14.141 46232
|
||||
112.118.110.73 23523
|
||||
86.131.64.203 10961
|
||||
90.16.186.222 28963
|
||||
114.25.189.2 26371
|
||||
41.201.193.31 62478
|
||||
78.243.1.54 23096
|
||||
188.40.241.165 42860
|
||||
134.106.236.73 47676
|
||||
178.126.116.177 41191
|
||||
109.184.124.201 15949
|
||||
173.32.244.91 36480
|
||||
78.160.108.42 26516
|
||||
110.4.37.208 18405
|
||||
79.35.38.183 56065
|
||||
178.140.45.11 58773
|
||||
90.23.195.31 5980
|
||||
114.42.69.220 25934
|
||||
124.209.90.197 7143
|
||||
77.122.48.90 15241
|
||||
95.211.8.11 19632
|
||||
113.253.67.125 15806
|
||||
82.233.53.72 24504
|
||||
85.222.76.230 28749
|
||||
188.247.87.63 61053
|
||||
190.205.135.114 6558
|
||||
85.73.96.172 20157
|
||||
74.248.209.2 43507
|
||||
79.165.180.49 61839
|
||||
71.166.156.253 50056
|
||||
99.255.144.165 47227
|
||||
118.168.190.64 21716
|
||||
61.206.121.120 17377
|
||||
85.187.37.123 13017
|
||||
108.28.93.29 21491
|
||||
66.91.48.116 43960
|
||||
196.205.129.245 11972
|
||||
93.12.186.53 7448
|
||||
62.147.204.92 7812
|
||||
90.205.87.163 25878
|
||||
207.81.138.15 47037
|
||||
112.202.44.3 19127
|
||||
91.17.208.53 1253
|
||||
2.221.14.86 14144
|
||||
89.131.159.217 38402
|
||||
24.0.27.234 33531
|
||||
46.55.74.45 45370
|
||||
68.224.62.176 54916
|
||||
78.58.13.203 11641
|
||||
77.182.30.93 8812
|
||||
95.182.76.102 24521
|
||||
122.167.5.167 12542
|
||||
92.134.16.12 42932
|
||||
88.187.80.252 49180
|
||||
134.106.236.73 32092
|
||||
95.90.231.148 35420
|
||||
82.232.0.124 9156
|
||||
125.26.193.75 15782
|
||||
84.123.34.33 19467
|
||||
193.95.54.218 51240
|
||||
89.28.25.110 25077
|
||||
124.6.0.183 30001
|
||||
92.50.80.105 49232
|
||||
88.189.82.239 5666
|
||||
213.245.188.141 25551
|
||||
91.66.51.19 33548
|
||||
24.1.118.110 27704
|
||||
216.137.133.89 44376
|
||||
188.126.74.13 45416
|
||||
84.208.60.162 37692
|
||||
86.193.13.31 7812
|
2907
libbitdht/src/bitdht/bdconnection.cc
Normal file
2907
libbitdht/src/bitdht/bdconnection.cc
Normal file
File diff suppressed because it is too large
Load Diff
274
libbitdht/src/bitdht/bdconnection.h
Normal file
274
libbitdht/src/bitdht/bdconnection.h
Normal file
@ -0,0 +1,274 @@
|
||||
#ifndef BITDHT_CONNECTION_H
|
||||
#define BITDHT_CONNECTION_H
|
||||
|
||||
/*
|
||||
* bitdht/bdconnection.h
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2011 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 "bitdht/bdiface.h"
|
||||
|
||||
class bdQueryManager;
|
||||
class bdNodePublisher;
|
||||
|
||||
/************************************************************************************************************
|
||||
************************************** ProxyTuple + Connection State ****************************************
|
||||
************************************************************************************************************/
|
||||
|
||||
#define BITDHT_CONNREQUEST_READY 1
|
||||
#define BITDHT_CONNREQUEST_PAUSED 2
|
||||
#define BITDHT_CONNREQUEST_INPROGRESS 3
|
||||
#define BITDHT_CONNREQUEST_EXTCONNECT 4
|
||||
#define BITDHT_CONNREQUEST_DONE 5
|
||||
|
||||
#define BITDHT_CONNREQUEST_TIMEOUT_CONNECT 30
|
||||
#define BITDHT_CONNREQUEST_TIMEOUT_INPROGRESS 30
|
||||
#define BITDHT_CONNREQUEST_MAX_AGE 60
|
||||
|
||||
|
||||
#define BITDHT_CONNECTION_WAITING_AUTH 1
|
||||
#define BITDHT_CONNECTION_WAITING_REPLY 2
|
||||
#define BITDHT_CONNECTION_WAITING_START 3
|
||||
#define BITDHT_CONNECTION_WAITING_ACK 4
|
||||
#define BITDHT_CONNECTION_COMPLETED 5
|
||||
|
||||
|
||||
#define BD_CONNECTION_START_RETRY_PERIOD 5 // Should only take a couple of seconds to get reply.
|
||||
#define BD_CONNECTION_START_MAX_RETRY 3
|
||||
#define BD_CONNECTION_MAX_TIMEOUT 30
|
||||
|
||||
|
||||
|
||||
class bdProxyTuple
|
||||
{
|
||||
public:
|
||||
bdProxyTuple() { return; }
|
||||
bdProxyTuple(bdNodeId *s, bdNodeId *p, bdNodeId *d)
|
||||
:srcId(*s), proxyId(*p), destId(*d) { return; }
|
||||
|
||||
bdNodeId srcId;
|
||||
bdNodeId proxyId;
|
||||
bdNodeId destId;
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const bdProxyTuple &t);
|
||||
int operator<(const bdProxyTuple &a, const bdProxyTuple &b);
|
||||
int operator==(const bdProxyTuple &a, const bdProxyTuple &b);
|
||||
|
||||
|
||||
class bdConnection
|
||||
{
|
||||
public:
|
||||
bdConnection();
|
||||
|
||||
/** Functions to tweak the connection status */
|
||||
|
||||
// User initialised Connection.
|
||||
int ConnectionSetup(bdId *proxyId, bdId *srcConnAddr, bdId *destConnAddr, int mode);
|
||||
int ConnectionSetupDirect(bdId *destId, bdId *srcConnAddr);
|
||||
|
||||
// Initialise a new Connection. (receiving a Connection Request)
|
||||
int ConnectionRequestDirect(bdId *id, bdId *srcConnAddr, bdId *destConnAddr);
|
||||
int ConnectionRequestProxy(bdId *id, bdId *srcConnAddr, bdNodeId *ownId, bdId *destConnAddr, int mode);
|
||||
int ConnectionRequestEnd(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode);
|
||||
|
||||
// Setup Finishing Stage, (receiving a Connection Reply).
|
||||
int upgradeProxyConnectionToFinish(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int status);
|
||||
|
||||
int AuthoriseDirectConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc);
|
||||
int AuthoriseProxyConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc);
|
||||
int AuthoriseEndConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc);
|
||||
|
||||
int CompleteConnection(bdId *id, bdId *srcConnAddr, bdId *destConnAddr);
|
||||
|
||||
int checkForDefaultConnectAddress();
|
||||
|
||||
/* Connection State, and TimeStamp of Update */
|
||||
int mState;
|
||||
time_t mLastEvent;
|
||||
|
||||
/* Addresses of Start/Proxy/End Nodes */
|
||||
bdId mSrcId;
|
||||
bdId mDestId;
|
||||
bdId mProxyId;
|
||||
|
||||
/* Where we are in the connection,
|
||||
* and what connection mode.
|
||||
*/
|
||||
int mPoint;
|
||||
int mMode;
|
||||
|
||||
/* must have ip:ports of connection ends (if proxied) */
|
||||
bdId mSrcConnAddr;
|
||||
bdId mDestConnAddr;
|
||||
|
||||
int mBandwidth;
|
||||
|
||||
/* START/ACK Finishing ****/
|
||||
time_t mLastStart; /* timer for retries */
|
||||
int mRetryCount; /* retry counter */
|
||||
|
||||
bool mSrcAck;
|
||||
bool mDestAck;
|
||||
|
||||
// Completion TS.
|
||||
time_t mCompletedTS;
|
||||
|
||||
|
||||
};
|
||||
|
||||
class bdConnectionRequest
|
||||
{
|
||||
public:
|
||||
int setupDirectConnection(struct sockaddr_in *laddr, bdNodeId *target);
|
||||
int setupProxyConnection(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode);
|
||||
|
||||
int addGoodProxy(const bdId *srcId);
|
||||
int checkGoodProxyPeer(const bdId *Id);
|
||||
|
||||
bdNodeId mTarget;
|
||||
struct sockaddr_in mLocalAddr;
|
||||
int mMode;
|
||||
|
||||
int mState;
|
||||
time_t mStateTS;
|
||||
|
||||
time_t mPauseTS;
|
||||
uint32_t mErrCode;
|
||||
|
||||
|
||||
std::list<bdId> mGoodProxies;
|
||||
std::list<bdId> mPotentialProxies;
|
||||
int mRecycled;
|
||||
|
||||
bdId mCurrentAttempt;
|
||||
std::list<bdId> mPeersTried;
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const bdConnectionRequest &req);
|
||||
std::ostream &operator<<(std::ostream &out, const bdConnection &conn);
|
||||
|
||||
|
||||
/*********
|
||||
* The Connection Management Class.
|
||||
* this encapsulates all of the functionality..
|
||||
* except for a couple of message in/outs + callback.
|
||||
*/
|
||||
|
||||
class bdConnectManager
|
||||
{
|
||||
public:
|
||||
|
||||
bdConnectManager(bdNodeId *ownid, bdSpace *space, bdQueryManager *qmgr, bdDhtFunctions *fns, bdNodePublisher *pub);
|
||||
|
||||
|
||||
/* connection functions */
|
||||
void requestConnection(bdNodeId *id, uint32_t modes);
|
||||
void allowConnection(bdNodeId *id, uint32_t modes);
|
||||
|
||||
|
||||
/* high level */
|
||||
|
||||
void shutdownConnections();
|
||||
void printConnections();
|
||||
|
||||
/* Connections: Configuration */
|
||||
void defaultConnectionOptions();
|
||||
virtual void setConnectionOptions(uint32_t allowedModes, uint32_t flags);
|
||||
|
||||
/* Connections: Initiation */
|
||||
|
||||
int requestConnection(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t start);
|
||||
int requestConnection_direct(struct sockaddr_in *laddr, bdNodeId *target);
|
||||
int requestConnection_proxy(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode);
|
||||
|
||||
int killConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode);
|
||||
|
||||
int checkExistingConnectionAttempt(bdNodeId *target);
|
||||
void addPotentialConnectionProxy(const bdId *srcId, const bdId *target);
|
||||
void updatePotentialConnectionProxy(const bdId *id, uint32_t mode);
|
||||
|
||||
int checkPeerForFlag(const bdId *id, uint32_t with_flag);
|
||||
|
||||
int tickConnections();
|
||||
void iterateConnectionRequests();
|
||||
int startConnectionAttempt(bdConnectionRequest *req);
|
||||
|
||||
// internal Callback -> normally continues to callbackConnect().
|
||||
void callbackConnectRequest(bdId *srcId, bdId *proxyId, bdId *destId,
|
||||
int mode, int point, int cbtype, int errcode);
|
||||
|
||||
/* Connections: Outgoing */
|
||||
|
||||
int startConnectionAttempt(bdId *proxyId, bdId *srcConnAddr, bdId *destConnAddr, int mode);
|
||||
void AuthConnectionOk(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc);
|
||||
void AuthConnectionNo(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc, int errcode);
|
||||
void iterateConnections();
|
||||
|
||||
|
||||
/* Connections: Utility State */
|
||||
|
||||
bdConnection *findExistingConnection(bdNodeId *srcId, bdNodeId *proxyId, bdNodeId *destId);
|
||||
bdConnection *newConnection(bdNodeId *srcId, bdNodeId *proxyId, bdNodeId *destId);
|
||||
int cleanConnection(bdNodeId *srcId, bdNodeId *proxyId, bdNodeId *destId);
|
||||
|
||||
int determinePosition(bdNodeId *sender, bdNodeId *src, bdNodeId *dest);
|
||||
int determineProxyId(bdNodeId *sender, bdNodeId *src, bdNodeId *dest, bdNodeId *proxyId);
|
||||
|
||||
bdConnection *findSimilarConnection(bdNodeId *srcId, bdNodeId *destId);
|
||||
bdConnection *findExistingConnectionBySender(bdId *sender, bdId *src, bdId *dest);
|
||||
bdConnection *newConnectionBySender(bdId *sender, bdId *src, bdId *dest);
|
||||
int cleanConnectionBySender(bdId *sender, bdId *src, bdId *dest);
|
||||
|
||||
// Overloaded Generalised Connection Callback.
|
||||
virtual void callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId,
|
||||
int mode, int point, int cbtype, int errcode);
|
||||
|
||||
/* Connections: */
|
||||
int recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode);
|
||||
int recvedConnectionReply(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int status);
|
||||
int recvedConnectionStart(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int bandwidth);
|
||||
int recvedConnectionAck(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode);
|
||||
|
||||
private:
|
||||
|
||||
std::map<bdProxyTuple, bdConnection> mConnections;
|
||||
std::map<bdNodeId, bdConnectionRequest> mConnectionRequests;
|
||||
|
||||
uint32_t mConfigAllowedModes;
|
||||
bool mConfigAutoProxy;
|
||||
|
||||
/****************************** Connection Code (in bdconnection.cc) ****************************/
|
||||
|
||||
private:
|
||||
|
||||
bdNodeId mOwnId;
|
||||
bdSpace *mNodeSpace;
|
||||
bdQueryManager *mQueryMgr;
|
||||
bdDhtFunctions *mFns;
|
||||
bdNodePublisher *mPub;
|
||||
};
|
||||
|
||||
|
||||
#endif // BITDHT_CONNECTION_H
|
175
libbitdht/src/bitdht/bdfilter.cc
Normal file
175
libbitdht/src/bitdht/bdfilter.cc
Normal file
@ -0,0 +1,175 @@
|
||||
|
||||
/*
|
||||
* bitdht/bdfilter.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 "bitdht/bdfilter.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
|
||||
/**
|
||||
* #define DEBUG_FILTER 1
|
||||
**/
|
||||
|
||||
|
||||
bdFilter::bdFilter(const bdNodeId *ownId, std::list<bdFilteredPeer> &startList,
|
||||
uint32_t filterFlags, bdDhtFunctions *fns)
|
||||
{
|
||||
/* */
|
||||
mOwnId = *ownId;
|
||||
mFns = fns;
|
||||
|
||||
time_t now = time(NULL);
|
||||
std::list<bdFilteredPeer>::iterator it;
|
||||
|
||||
for(it = startList.begin(); it != startList.end(); it++)
|
||||
{
|
||||
mFiltered.push_back(*it);
|
||||
}
|
||||
|
||||
mFilterFlags = filterFlags;
|
||||
}
|
||||
|
||||
bool bdFilter::filtered(std::list<bdFilteredPeer> &answer)
|
||||
{
|
||||
answer = mFiltered;
|
||||
return (answer.size() > 0);
|
||||
}
|
||||
|
||||
bool bdFilter::filteredIPs(std::list<struct sockaddr_in> &answer)
|
||||
{
|
||||
std::list<bdFilteredPeer>::iterator it;
|
||||
for(it = mFiltered.begin(); it != mFiltered.end(); it++)
|
||||
{
|
||||
answer.push_back(it->mAddr);
|
||||
}
|
||||
return (answer.size() > 0);
|
||||
}
|
||||
|
||||
int bdFilter::checkPeer(const bdId *id, uint32_t mode)
|
||||
{
|
||||
bool add = false;
|
||||
uint32_t flags = 0;
|
||||
if ((mFilterFlags & BITDHT_FILTER_REASON_OWNID) &&
|
||||
isOwnIdWithoutBitDhtFlags(id, mode))
|
||||
{
|
||||
add = true;
|
||||
flags |= BITDHT_FILTER_REASON_OWNID;
|
||||
}
|
||||
|
||||
if (add)
|
||||
{
|
||||
bool isNew = addPeerToFilter(id, flags);
|
||||
if (isNew)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bdFilter::addPeerToFilter(const bdId *id, uint32_t flags)
|
||||
{
|
||||
std::list<bdFilteredPeer>::iterator it;
|
||||
bool found = false;
|
||||
for(it = mFiltered.begin(); it != mFiltered.end(); it++)
|
||||
{
|
||||
if (id->addr.sin_addr.s_addr == it->mAddr.sin_addr.s_addr)
|
||||
{
|
||||
found = true;
|
||||
it->mLastSeen = time(NULL);
|
||||
it->mFilterFlags |= flags;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
bdFilteredPeer fp;
|
||||
|
||||
fp.mAddr = id->addr;
|
||||
fp.mAddr.sin_port = 0;
|
||||
fp.mFilterFlags = flags;
|
||||
fp.mFilterTS = now;
|
||||
fp.mLastSeen = now;
|
||||
|
||||
mFiltered.push_back(fp);
|
||||
|
||||
uint32_t saddr = id->addr.sin_addr.s_addr;
|
||||
mIpsBanned.insert(saddr);
|
||||
|
||||
std::cerr << "Adding New Banned Ip Address: " << inet_ntoa(id->addr.sin_addr);
|
||||
std::cerr << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* fast check if the addr is in the structure */
|
||||
int bdFilter::addrOkay(struct sockaddr_in *addr)
|
||||
{
|
||||
std::set<uint32_t>::const_iterator it = mIpsBanned.find(addr->sin_addr.s_addr);
|
||||
if (it == mIpsBanned.end())
|
||||
{
|
||||
return 1; // Address is Okay!
|
||||
}
|
||||
std::cerr << "Detected Packet From Banned Ip Address: " << inet_ntoa(addr->sin_addr);
|
||||
std::cerr << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
bool bdFilter::isOwnIdWithoutBitDhtFlags(const bdId *id, uint32_t peerFlags)
|
||||
{
|
||||
if (peerFlags & BITDHT_PEER_STATUS_RECV_PONG)
|
||||
{
|
||||
if (peerFlags & BITDHT_PEER_STATUS_DHT_ENGINE)
|
||||
{
|
||||
/* okay! */
|
||||
return false;
|
||||
}
|
||||
|
||||
/* now check distance */
|
||||
bdMetric dist;
|
||||
mFns->bdDistance(&mOwnId, &(id->id), &dist);
|
||||
int bucket = mFns->bdBucketDistance(&dist);
|
||||
|
||||
/* if they match us... kill it */
|
||||
if (bucket == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
82
libbitdht/src/bitdht/bdfilter.h
Normal file
82
libbitdht/src/bitdht/bdfilter.h
Normal file
@ -0,0 +1,82 @@
|
||||
#ifndef BITDHT_FILTER_H
|
||||
#define BITDHT_FILTER_H
|
||||
|
||||
/*
|
||||
* bitdht/bdfilter.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".
|
||||
*
|
||||
*/
|
||||
|
||||
/* This class is used to detect bad and filter them peers
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "bitdht/bdiface.h"
|
||||
#include <set>
|
||||
|
||||
/* Query result flags are in bdiface.h */
|
||||
|
||||
#define BITDHT_FILTER_REASON_OWNID 0x0001
|
||||
|
||||
class bdFilteredPeer
|
||||
{
|
||||
public:
|
||||
struct sockaddr_in mAddr;
|
||||
uint32_t mFilterFlags; /* reasons why we are filtering */
|
||||
time_t mFilterTS;
|
||||
time_t mLastSeen;
|
||||
};
|
||||
|
||||
class bdFilter
|
||||
{
|
||||
public:
|
||||
bdFilter(const bdNodeId *ownid, std::list<bdFilteredPeer> &initialFilters,
|
||||
uint32_t filterFlags, bdDhtFunctions *fns);
|
||||
|
||||
// get the answer.
|
||||
bool filtered(std::list<bdFilteredPeer> &answer);
|
||||
bool filteredIPs(std::list<struct sockaddr_in> &answer);
|
||||
|
||||
int checkPeer(const bdId *id, uint32_t peerFlags);
|
||||
|
||||
int addrOkay(struct sockaddr_in *addr);
|
||||
|
||||
private:
|
||||
|
||||
int addPeerToFilter(const bdId *id, uint32_t flags);
|
||||
bool isOwnIdWithoutBitDhtFlags(const bdId *id, uint32_t peerFlags);
|
||||
|
||||
// searching for
|
||||
bdNodeId mOwnId;
|
||||
uint32_t mFilterFlags;
|
||||
|
||||
std::list<bdFilteredPeer> mFiltered;
|
||||
bdDhtFunctions *mFns;
|
||||
|
||||
// = addr.sin_addr.s_addr (uint32_t) stored in network order.
|
||||
std::set<uint32_t> mIpsBanned;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
287
libbitdht/src/bitdht/bdhash.cc
Normal file
287
libbitdht/src/bitdht/bdhash.cc
Normal file
@ -0,0 +1,287 @@
|
||||
/*
|
||||
* bitdht/bdhash.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 "bitdht/bdhash.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
#include <iostream>
|
||||
|
||||
bdHashEntry::bdHashEntry(std::string value, std::string secret, time_t lifetime, time_t store)
|
||||
:mValue(value), mStoreTS(store), mSecret(secret), mLifetime(lifetime)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**************************** class bdHashSet ********************************/
|
||||
|
||||
bdHashSet::bdHashSet(bdNodeId *id)
|
||||
:mId(*id)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int bdHashSet::search(std::string key, uint32_t maxAge, std::list<bdHashEntry> &entries)
|
||||
{
|
||||
std::multimap<std::string, bdHashEntry>::iterator sit, eit, it;
|
||||
sit = mEntries.lower_bound(key);
|
||||
eit = mEntries.upper_bound(key);
|
||||
|
||||
time_t now = time(NULL);
|
||||
|
||||
for(it = sit; it != eit; it++)
|
||||
{
|
||||
time_t age = now - it->second.mStoreTS;
|
||||
if (age < (int32_t) maxAge)
|
||||
{
|
||||
entries.push_back(it->second);
|
||||
}
|
||||
}
|
||||
return (0 < entries.size());
|
||||
}
|
||||
|
||||
/***
|
||||
* With modification.
|
||||
* If there is no secret -> it cannot be deleted (must timeout), but can be extended.
|
||||
* If there is a secret -> must include it to modify.
|
||||
*
|
||||
* Therefore if identical entry without secret comes along - what do I do?
|
||||
* -> create duplicate?
|
||||
*/
|
||||
|
||||
int bdHashSet::modify(std::string key, bdHashEntry *entry, uint32_t modFlags)
|
||||
{
|
||||
std::multimap<std::string, bdHashEntry>::iterator sit, eit, it;
|
||||
sit = mEntries.lower_bound(key);
|
||||
eit = mEntries.upper_bound(key);
|
||||
|
||||
time_t now = time(NULL);
|
||||
|
||||
bool updated = false;
|
||||
for(it = sit; it != eit; it++)
|
||||
{
|
||||
/* check it all */
|
||||
if (it->second.mValue == entry->mValue)
|
||||
{
|
||||
bool noSecret = (it->second.mSecret == "");
|
||||
bool sameSecret = (it->second.mSecret == entry->mSecret);
|
||||
bool update = false;
|
||||
|
||||
if (noSecret && sameSecret)
|
||||
{
|
||||
/* only allowed to increase lifetime */
|
||||
if (modFlags == BITDHT_HASH_ENTRY_ADD)
|
||||
{
|
||||
time_t existKillTime = it->second.mLifetime + it->second.mStoreTS;
|
||||
time_t newKillTime = entry->mLifetime + now;
|
||||
if (newKillTime > existKillTime)
|
||||
{
|
||||
update = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (sameSecret)
|
||||
{
|
||||
if (modFlags == BITDHT_HASH_ENTRY_ADD)
|
||||
{
|
||||
update = true;
|
||||
}
|
||||
else if (modFlags == BITDHT_HASH_ENTRY_DELETE)
|
||||
{
|
||||
/* do it here */
|
||||
mEntries.erase(it);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (update)
|
||||
{
|
||||
it->second.mStoreTS = now;
|
||||
it->second.mLifetime = entry->mLifetime;
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((!updated) && (modFlags == BITDHT_HASH_ENTRY_ADD))
|
||||
{
|
||||
/* create a new entry */
|
||||
bdHashEntry newEntry(entry->mValue, entry->mSecret, entry->mLifetime, now);
|
||||
mEntries.insert(std::pair<std::string, bdHashEntry>(key, newEntry));
|
||||
updated = true;
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
int bdHashSet::printHashSet(std::ostream &out)
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
std::multimap<std::string, bdHashEntry>::iterator it;
|
||||
out << "Hash: ";
|
||||
bdStdPrintNodeId(out, &mId); // Allowing "Std" as we dont need dht functions everywhere.
|
||||
out << std::endl;
|
||||
|
||||
for(it = mEntries.begin(); it != mEntries.end();it++)
|
||||
{
|
||||
time_t age = now - it->second.mStoreTS;
|
||||
out << "\tK:" << bdStdConvertToPrintable(it->first);
|
||||
out << " V:" << bdStdConvertToPrintable(it->second.mValue);
|
||||
out << " A:" << age << " L:" << it->second.mLifetime;
|
||||
out << " S:" << bdStdConvertToPrintable(it->second.mSecret);
|
||||
out << std::endl;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int bdHashSet::cleanupHashSet(uint32_t maxAge)
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
|
||||
/* this is nasty... but don't know how to effectively remove from multimaps
|
||||
* * Must do full repeat for each removal.
|
||||
*/
|
||||
|
||||
std::multimap<std::string, bdHashEntry>::iterator it;
|
||||
for(it = mEntries.begin(); it != mEntries.end();)
|
||||
{
|
||||
time_t age = now - it->second.mStoreTS;
|
||||
if ((age > (int32_t) maxAge) || (age > it->second.mLifetime))
|
||||
{
|
||||
mEntries.erase(it);
|
||||
it = mEntries.begin();
|
||||
}
|
||||
else
|
||||
{
|
||||
it++;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/******************************* class bdHashSpace ***************************/
|
||||
|
||||
bdHashSpace::bdHashSpace()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* accessors */
|
||||
int bdHashSpace::search(bdNodeId *id, std::string key, uint32_t maxAge, std::list<bdHashEntry> &entries)
|
||||
{
|
||||
std::map<bdNodeId, bdHashSet>::iterator it;
|
||||
it = mHashTable.find(*id);
|
||||
if (it == mHashTable.end())
|
||||
{
|
||||
/* no entry */
|
||||
return 1;
|
||||
}
|
||||
|
||||
return it->second.search(key, maxAge, entries);
|
||||
}
|
||||
|
||||
int bdHashSpace::modify(bdNodeId *id, std::string key, bdHashEntry *entry, uint32_t modFlags)
|
||||
{
|
||||
std::map<bdNodeId, bdHashSet>::iterator it;
|
||||
it = mHashTable.find(*id);
|
||||
if (it == mHashTable.end())
|
||||
{
|
||||
if (modFlags == BITDHT_HASH_ENTRY_DELETE)
|
||||
{
|
||||
/* done already */
|
||||
return 1;
|
||||
}
|
||||
|
||||
//mHashTable[*id] = bdHashSet(id);
|
||||
mHashTable.insert(std::pair<bdNodeId, bdHashSet>(*id, bdHashSet(id)));
|
||||
it = mHashTable.find(*id);
|
||||
}
|
||||
|
||||
return it->second.modify(key, entry, modFlags);
|
||||
}
|
||||
|
||||
int bdHashSpace::printHashSpace(std::ostream &out)
|
||||
{
|
||||
std::map<bdNodeId, bdHashSet>::iterator it;
|
||||
out << "bdHashSpace::printHashSpace()" << std::endl;
|
||||
out << "--------------------------------------------" << std::endl;
|
||||
|
||||
for(it = mHashTable.begin(); it != mHashTable.end(); it++)
|
||||
{
|
||||
it->second.printHashSet(out);
|
||||
}
|
||||
out << "--------------------------------------------" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int bdHashSpace::cleanHashSpace(bdNodeId *min, bdNodeId *max, time_t maxAge)
|
||||
{
|
||||
std::map<bdNodeId, bdHashSet>::iterator it;
|
||||
std::list<bdNodeId> eraseList;
|
||||
std::list<bdNodeId>::iterator eit;
|
||||
|
||||
for(it = mHashTable.begin(); it != mHashTable.end(); it++)
|
||||
{
|
||||
if ((it->first < *min) ||
|
||||
(*max < it->first))
|
||||
{
|
||||
/* schedule for erasure */
|
||||
eraseList.push_back(it->first);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* clean up Hash Set */
|
||||
it->second.cleanupHashSet(maxAge);
|
||||
}
|
||||
}
|
||||
|
||||
/* cleanup */
|
||||
while(eraseList.size() > 0)
|
||||
{
|
||||
bdNodeId &eId = eraseList.front();
|
||||
it = mHashTable.find(eId);
|
||||
if (it != mHashTable.end())
|
||||
{
|
||||
mHashTable.erase(it);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int bdHashSpace::clear()
|
||||
{
|
||||
mHashTable.clear();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
88
libbitdht/src/bitdht/bdhash.h
Normal file
88
libbitdht/src/bitdht/bdhash.h
Normal file
@ -0,0 +1,88 @@
|
||||
#ifndef BITDHT_HASH_SPACE_H
|
||||
#define BITDHT_HASH_SPACE_H
|
||||
|
||||
/*
|
||||
* bitdht/bdhash.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 "bitdht/bdpeer.h"
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#define BITDHT_HASH_ENTRY_ADD 1
|
||||
#define BITDHT_HASH_ENTRY_DELETE 2
|
||||
|
||||
class bdHashEntry
|
||||
{
|
||||
public:
|
||||
|
||||
bdHashEntry(std::string value, std::string secret, time_t lifetime, time_t store);
|
||||
std::string mValue;
|
||||
time_t mStoreTS;
|
||||
|
||||
/* These are nice features that OpenDHT had */
|
||||
std::string mSecret;
|
||||
time_t mLifetime;
|
||||
};
|
||||
|
||||
|
||||
class bdHashSet
|
||||
{
|
||||
public:
|
||||
|
||||
bdHashSet(bdNodeId *id);
|
||||
|
||||
int search(std::string key, uint32_t maxAge, std::list<bdHashEntry> &entries);
|
||||
int modify(std::string key, bdHashEntry *entry, uint32_t modFlags);
|
||||
int printHashSet(std::ostream &out);
|
||||
int cleanupHashSet(uint32_t maxAge);
|
||||
|
||||
bdNodeId mId;
|
||||
std::multimap<std::string, bdHashEntry> mEntries;
|
||||
};
|
||||
|
||||
class bdHashSpace
|
||||
{
|
||||
public:
|
||||
|
||||
bdHashSpace();
|
||||
|
||||
/* accessors */
|
||||
int search(bdNodeId *id, std::string key, uint32_t maxAge, std::list<bdHashEntry> &entries);
|
||||
int modify(bdNodeId *id, std::string key, bdHashEntry *entry, uint32_t modFlags);
|
||||
|
||||
int printHashSpace(std::ostream&);
|
||||
int cleanHashSpace(bdNodeId *min, bdNodeId *max, time_t maxAge);
|
||||
int clear();
|
||||
|
||||
private:
|
||||
|
||||
std::map<bdNodeId, bdHashSet> mHashTable;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
211
libbitdht/src/bitdht/bdhistory.cc
Normal file
211
libbitdht/src/bitdht/bdhistory.cc
Normal file
@ -0,0 +1,211 @@
|
||||
|
||||
|
||||
#include "bitdht/bdhistory.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
|
||||
#define MIN_RESEND_PERIOD 60
|
||||
|
||||
void bdMsgHistoryList::addMsg(time_t ts, uint32_t msgType, bool incoming)
|
||||
|
||||
{
|
||||
uint32_t msg = msgType | (incoming ? MSG_DIRECTION_INCOMING : MSG_DIRECTION_OUTGOING);
|
||||
msgHistory.insert(std::make_pair(ts, msg));
|
||||
}
|
||||
|
||||
int bdMsgHistoryList::msgCount(time_t start_ts, time_t end_ts)
|
||||
{
|
||||
std::multimap<time_t, uint32_t>::iterator sit, eit, it;
|
||||
sit = msgHistory.lower_bound(start_ts);
|
||||
eit = msgHistory.upper_bound(end_ts);
|
||||
int count = 0;
|
||||
for (it = sit; it != eit; it++, count++) ; // empty loop.
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
void bdMsgHistoryList::msgClear()
|
||||
{
|
||||
msgHistory.clear();
|
||||
}
|
||||
|
||||
|
||||
void bdMsgHistoryList::printHistory(std::ostream &out, int mode, time_t start_ts, time_t end_ts)
|
||||
{
|
||||
//out << "AGE: MSGS => incoming, <= outgoing" << std::endl;
|
||||
std::multimap<time_t, uint32_t>::iterator sit, eit, it;
|
||||
sit = msgHistory.lower_bound(start_ts);
|
||||
eit = msgHistory.upper_bound(end_ts);
|
||||
time_t curr_ts = 0;
|
||||
time_t old_ts = 0;
|
||||
bool time_changed = false;
|
||||
bool first_line = true;
|
||||
|
||||
for(it = sit; it != eit; it++)
|
||||
{
|
||||
time_changed = false;
|
||||
if (curr_ts != it->first)
|
||||
{
|
||||
old_ts = curr_ts;
|
||||
curr_ts = it->first;
|
||||
time_changed = true;
|
||||
}
|
||||
switch(mode)
|
||||
{
|
||||
default:
|
||||
{
|
||||
/* print one line per ts */
|
||||
if (time_changed)
|
||||
{
|
||||
if (!first_line)
|
||||
{
|
||||
/* finish existing line */
|
||||
out << " " << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
first_line = false;
|
||||
}
|
||||
out << "\tTS: " << time(NULL) - curr_ts << " ";
|
||||
}
|
||||
|
||||
if (MSG_DIRECTION_INCOMING & it->second)
|
||||
{
|
||||
out << " =>" << it->second - MSG_DIRECTION_INCOMING;
|
||||
out << " ";
|
||||
}
|
||||
else
|
||||
{
|
||||
out << " ";
|
||||
out << it->second - MSG_DIRECTION_OUTGOING << "=> ";
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
} // end of switch.
|
||||
}
|
||||
|
||||
/* finish up last line */
|
||||
if (!first_line)
|
||||
{
|
||||
out << " " << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool bdMsgHistoryList::canSend()
|
||||
{
|
||||
std::cerr << "bdMsgHistoryList::canSend()";
|
||||
|
||||
std::multimap<time_t, uint32_t>::reverse_iterator rit;
|
||||
|
||||
rit = msgHistory.rbegin();
|
||||
if (rit != msgHistory.rend())
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
if (now - rit->first > MIN_RESEND_PERIOD)
|
||||
{
|
||||
std::cerr << " OVER RESEND_PERIOD... true";
|
||||
std::cerr << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (msgHistory.size() % 2 == 0)
|
||||
{
|
||||
std::cerr << " SIZE: " << msgHistory.size() << " % 2 = 0 ... true";
|
||||
std::cerr << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::cerr << " false";
|
||||
std::cerr << std::endl;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool bdMsgHistoryList::validPeer()
|
||||
{
|
||||
std::cerr << "bdMsgHistoryList::validPeer()";
|
||||
|
||||
std::multimap<time_t, uint32_t>::iterator it;
|
||||
|
||||
for(it = msgHistory.begin(); it != msgHistory.end(); it++)
|
||||
{
|
||||
if (MSG_DIRECTION_INCOMING & it->second)
|
||||
{
|
||||
std::cerr << " Incoming Msg... so validPeer";
|
||||
std::cerr << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << " false";
|
||||
std::cerr << std::endl;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void bdHistory::addMsg(const bdId *id, bdToken * /*transId*/, uint32_t msgType, bool incoming)
|
||||
{
|
||||
std::map<bdId, bdMsgHistoryList>::iterator it;
|
||||
bdMsgHistoryList &histRef = mHistory[*id]; /* will instaniate empty */
|
||||
histRef.addMsg(time(NULL), msgType, incoming);
|
||||
}
|
||||
|
||||
void bdHistory::printMsgs()
|
||||
{
|
||||
/* print and clear msgs */
|
||||
std::ostream &out = std::cerr;
|
||||
|
||||
std::map<bdId, bdMsgHistoryList> ::iterator it;
|
||||
for(it = mHistory.begin(); it != mHistory.end(); it++)
|
||||
{
|
||||
if (it->second.msgCount(0, time(NULL))) // all msg count.
|
||||
{
|
||||
/* print header */
|
||||
out << "Msgs for ";
|
||||
bdStdPrintId(out, &(it->first));
|
||||
out << std::endl;
|
||||
|
||||
it->second.printHistory(out, 0, 0, time(NULL));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void bdHistory::clearHistory()
|
||||
{
|
||||
mHistory.clear();
|
||||
}
|
||||
|
||||
bool bdHistory::canSend(const bdId *id)
|
||||
{
|
||||
|
||||
std::map<bdId, bdMsgHistoryList> ::iterator it;
|
||||
it = mHistory.find(*id);
|
||||
if (it != mHistory.end())
|
||||
{
|
||||
return (it->second.canSend());
|
||||
}
|
||||
|
||||
/* if not found - then can send */
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool bdHistory::validPeer(const bdId *id)
|
||||
{
|
||||
std::map<bdId, bdMsgHistoryList> ::iterator it;
|
||||
it = mHistory.find(*id);
|
||||
if (it != mHistory.end())
|
||||
{
|
||||
return (it->second.validPeer());
|
||||
}
|
||||
|
||||
/* if not found - then can send */
|
||||
return false;
|
||||
}
|
||||
|
||||
|
50
libbitdht/src/bitdht/bdhistory.h
Normal file
50
libbitdht/src/bitdht/bdhistory.h
Normal file
@ -0,0 +1,50 @@
|
||||
#ifndef BITDHT_HISTORY_H
|
||||
#define BITDHT_HISTORY_H
|
||||
|
||||
#include "bitdht/bdpeer.h"
|
||||
#include "bitdht/bdobj.h"
|
||||
#include <map>
|
||||
|
||||
#define MSG_TYPE_DIRECTION_MASK 0x000f0000
|
||||
|
||||
#define MSG_DIRECTION_INCOMING 0x00010000
|
||||
#define MSG_DIRECTION_OUTGOING 0x00020000
|
||||
|
||||
/**** DEBUGGING HISTORY ****/
|
||||
|
||||
class bdMsgHistoryList
|
||||
{
|
||||
public:
|
||||
|
||||
void addMsg(time_t ts, uint32_t msgType, bool incoming);
|
||||
int msgCount(time_t start_ts, time_t end_ts);
|
||||
void msgClear();
|
||||
void printHistory(std::ostream &out, int mode, time_t start_ts, time_t end_ts);
|
||||
|
||||
bool canSend();
|
||||
bool validPeer();
|
||||
|
||||
std::multimap<time_t, uint32_t> msgHistory;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class bdHistory
|
||||
{
|
||||
public:
|
||||
void addMsg(const bdId *id, bdToken *transId, uint32_t msgType, bool incoming);
|
||||
void printMsgs();
|
||||
void clearHistory();
|
||||
|
||||
bool canSend(const bdId *id);
|
||||
bool validPeer(const bdId *id);
|
||||
|
||||
/* recent history */
|
||||
//std::list<bdId> lastMsgs;
|
||||
std::map<bdId, bdMsgHistoryList> mHistory;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
355
libbitdht/src/bitdht/bdiface.h
Normal file
355
libbitdht/src/bitdht/bdiface.h
Normal file
@ -0,0 +1,355 @@
|
||||
#ifndef BIT_DHT_INTERFACE_H
|
||||
#define BIT_DHT_INTERFACE_H
|
||||
|
||||
/*
|
||||
* bitdht/bdiface.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 <list>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "util/bdnet.h"
|
||||
|
||||
/*
|
||||
* Functions and Classes required for Interfacing with the BitDht.
|
||||
* This should be the sole header file required to talk to Dht.
|
||||
* ... though setting it up will require including udpbitdht.h as well.
|
||||
*
|
||||
*/
|
||||
|
||||
#define BITDHT_KEY_LEN 20
|
||||
#define BITDHT_KEY_INTLEN 5
|
||||
#define BITDHT_KEY_BITLEN 160
|
||||
|
||||
|
||||
|
||||
|
||||
#define BITDHT_MAX_PKTSIZE 1024
|
||||
|
||||
#define BITDHT_TTL 64
|
||||
|
||||
#define BITDHT_SEARCH_ONE_SHOT 1
|
||||
#define BITDHT_SEARCH_REPEATING 2
|
||||
|
||||
class bdNodeId
|
||||
{
|
||||
public:
|
||||
unsigned char data[BITDHT_KEY_LEN];
|
||||
};
|
||||
|
||||
class bdMetric: public bdNodeId {};
|
||||
|
||||
class bdId
|
||||
{
|
||||
public:
|
||||
|
||||
bdId();
|
||||
bdId(bdNodeId in_id, struct sockaddr_in in_addr);
|
||||
|
||||
struct sockaddr_in addr;
|
||||
bdNodeId id;
|
||||
};
|
||||
|
||||
#define BITDHT_LIKELY_SAME_NO 0x00000000
|
||||
#define BITDHT_LIKELY_SAME_YES 0x00000001
|
||||
#define BITDHT_LIKELY_SAME_PORT_CHANGED 0x00000002
|
||||
#define BITDHT_LIKELY_SAME_LOC_CHANGED 0x00000004
|
||||
#define BITDHT_LIKELY_SAME_IDENTICAL 0x00000008
|
||||
|
||||
|
||||
class bdDhtFunctions
|
||||
{
|
||||
public:
|
||||
|
||||
// bdDhtFunctions();
|
||||
/* setup variables */
|
||||
virtual uint16_t bdNumBuckets() = 0;
|
||||
virtual uint16_t bdNodesPerBucket() = 0; /* used for query + bdspace */
|
||||
virtual uint16_t bdBucketBitSize() = 0;
|
||||
|
||||
virtual int bdDistance(const bdNodeId *n1, const bdNodeId *n2, bdMetric *metric) = 0;
|
||||
virtual int bdBucketDistance(const bdNodeId *n1, const bdNodeId *n2) = 0;
|
||||
virtual int bdBucketDistance(const bdMetric *metric) = 0;
|
||||
|
||||
virtual bool bdSimilarId(const bdId *id1, const bdId *id2) = 0;
|
||||
virtual bool bdUpdateSimilarId(bdId *dest, const bdId *src) = 0;
|
||||
|
||||
virtual void bdRandomMidId(const bdNodeId *target, const bdNodeId *other, bdNodeId *mid) = 0;
|
||||
|
||||
virtual void bdPrintId(std::ostream &out, const bdId *a) = 0;
|
||||
virtual void bdPrintNodeId(std::ostream &out, const bdNodeId *a) = 0;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
/* NODE OPTIONS */
|
||||
#define BITDHT_OPTIONS_MAINTAIN_UNSTABLE_PORT 0x00000001
|
||||
|
||||
|
||||
|
||||
/* peer flags
|
||||
* order is important!
|
||||
* higher bits = more priority.
|
||||
* BITDHT_PEER_STATUS_RECVPING
|
||||
* BITDHT_PEER_STATUS_RECVPONG
|
||||
* BITDHT_PEER_STATUS_RECVNODES
|
||||
* BITDHT_PEER_STATUS_RECVHASHES
|
||||
* BITDHT_PEER_STATUS_DHT_ENGINE (dbXXxx)
|
||||
* BITDHT_PEER_STATUS_DHT_APPL (XXRSxx)
|
||||
* BITDHT_PEER_STATUS_DHT_VERSION (XXxx50)
|
||||
*
|
||||
*/
|
||||
|
||||
#define BITDHT_PEER_STATUS_MASK_RECVD 0x000000ff
|
||||
#define BITDHT_PEER_STATUS_MASK_DHT 0x0000ff00
|
||||
#define BITDHT_PEER_STATUS_MASK_KNOWN 0x00ff0000
|
||||
|
||||
#define BITDHT_PEER_STATUS_RECV_PING 0x00000001
|
||||
#define BITDHT_PEER_STATUS_RECV_PONG 0x00000002
|
||||
#define BITDHT_PEER_STATUS_RECV_NODES 0x00000004
|
||||
#define BITDHT_PEER_STATUS_RECV_HASHES 0x00000008
|
||||
#define BITDHT_PEER_STATUS_RECV_CONNECT_MSG 0x00000010
|
||||
|
||||
#define BITDHT_PEER_STATUS_DHT_ENGINE 0x00000100
|
||||
#define BITDHT_PEER_STATUS_DHT_ENGINE_VERSION 0x00000200
|
||||
#define BITDHT_PEER_STATUS_DHT_APPL 0x00000400
|
||||
#define BITDHT_PEER_STATUS_DHT_APPL_VERSION 0x00000800
|
||||
|
||||
#define BITDHT_PEER_STATUS_DHT_WHITELIST 0x00010000
|
||||
#define BITDHT_PEER_STATUS_DHT_FOF 0x00020000
|
||||
#define BITDHT_PEER_STATUS_DHT_FRIEND 0x00040000
|
||||
|
||||
|
||||
// EXTRA FLAGS are our internal thoughts about the peer.
|
||||
#define BITDHT_PEER_EXFLAG_MASK_BASIC 0x000000ff
|
||||
#define BITDHT_PEER_EXFLAG_UNSTABLE 0x00000001 // Port changes.
|
||||
#define BITDHT_PEER_EXFLAG_ATTACHED 0x00000002 // We will ping in heavily. (if unstable)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define BITDHT_CONNECT_MODE_DIRECT 0x00000001
|
||||
#define BITDHT_CONNECT_MODE_PROXY 0x00000002
|
||||
#define BITDHT_CONNECT_MODE_RELAY 0x00000004
|
||||
|
||||
#define BITDHT_CONNECT_OPTION_AUTOPROXY 0x00000001
|
||||
|
||||
// STATUS CODES. == 0 is okay, != 0 is error.
|
||||
#define BITDHT_CONNECT_ANSWER_OKAY 0x00000000
|
||||
#define BITDHT_CONNECT_ERROR_NONE (BITDHT_CONNECT_ANSWER_OKAY)
|
||||
|
||||
#define BITDHT_CONNECT_ERROR_MASK_TYPE 0x0000ffff
|
||||
#define BITDHT_CONNECT_ERROR_MASK_SOURCE 0x00ff0000
|
||||
#define BITDHT_CONNECT_ERROR_MASK_CRMOVE 0xff000000
|
||||
|
||||
#define BITDHT_CONNECT_ERROR_SOURCE_START 0x00010000
|
||||
#define BITDHT_CONNECT_ERROR_SOURCE_MID 0x00020000
|
||||
#define BITDHT_CONNECT_ERROR_SOURCE_END 0x00040000
|
||||
#define BITDHT_CONNECT_ERROR_SOURCE_OTHER 0x00080000
|
||||
|
||||
#define BITDHT_CONNECT_ERROR_CRMOVE_FATAL 0x01000000
|
||||
#define BITDHT_CONNECT_ERROR_CRMOVE_NOMOREIDS 0x02000000
|
||||
#define BITDHT_CONNECT_ERROR_CRMOVE_NEXTID 0x04000000
|
||||
#define BITDHT_CONNECT_ERROR_CRMOVE_PAUSED 0x08000000
|
||||
|
||||
// ERROR CODES.
|
||||
#define BITDHT_CONNECT_ERROR_GENERIC 0x00000001
|
||||
#define BITDHT_CONNECT_ERROR_PROTOCOL 0x00000002
|
||||
#define BITDHT_CONNECT_ERROR_TIMEOUT 0x00000003
|
||||
#define BITDHT_CONNECT_ERROR_TEMPUNAVAIL 0x00000004 // Haven't got ext address yet.
|
||||
#define BITDHT_CONNECT_ERROR_NOADDRESS 0x00000005 // Can't find the peer in tables.
|
||||
#define BITDHT_CONNECT_ERROR_UNREACHABLE 0x00000006 // Symmetric NAT
|
||||
|
||||
#define BITDHT_CONNECT_ERROR_UNSUPPORTED 0x00000007
|
||||
#define BITDHT_CONNECT_ERROR_OVERLOADED 0x00000008
|
||||
#define BITDHT_CONNECT_ERROR_AUTH_DENIED 0x00000009
|
||||
#define BITDHT_CONNECT_ERROR_DUPLICATE 0x0000000a
|
||||
|
||||
// These are slightly special ones used for CB_REQUEST
|
||||
#define BITDHT_CONNECT_ERROR_TOOMANYRETRY 0x0000000b
|
||||
#define BITDHT_CONNECT_ERROR_OUTOFPROXY 0x0000000c
|
||||
#define BITDHT_CONNECT_ERROR_USER 0x0000000d
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Definitions of bdSpace Peer and Bucket are publically available,
|
||||
* so we can expose the bucket entries for the gui.
|
||||
*/
|
||||
|
||||
class bdPeer
|
||||
{
|
||||
public:
|
||||
bdPeer():mPeerFlags(0), mLastSendTime(0), mLastRecvTime(0), mFoundTime(0), mExtraFlags(0) { return; }
|
||||
|
||||
bdId mPeerId;
|
||||
uint32_t mPeerFlags;
|
||||
time_t mLastSendTime;
|
||||
time_t mLastRecvTime;
|
||||
time_t mFoundTime; /* time stamp that peer was found */
|
||||
|
||||
uint32_t mExtraFlags;
|
||||
};
|
||||
|
||||
class bdBucket
|
||||
{
|
||||
public:
|
||||
|
||||
bdBucket();
|
||||
/* list so we can queue properly */
|
||||
std::list<bdPeer> entries;
|
||||
};
|
||||
|
||||
class bdQueryStatus
|
||||
{
|
||||
public:
|
||||
uint32_t mStatus;
|
||||
uint32_t mQFlags;
|
||||
std::list<bdId> mResults;
|
||||
};
|
||||
|
||||
class bdQuerySummary
|
||||
{
|
||||
public:
|
||||
|
||||
bdNodeId mId;
|
||||
bdMetric mLimit;
|
||||
uint32_t mState;
|
||||
time_t mQueryTS;
|
||||
uint32_t mQueryFlags;
|
||||
int32_t mSearchTime;
|
||||
|
||||
int32_t mQueryIdlePeerRetryPeriod; // seconds between retries.
|
||||
|
||||
// closest peers
|
||||
std::multimap<bdMetric, bdPeer> mClosest;
|
||||
std::multimap<bdMetric, bdPeer> mPotentialPeers;
|
||||
std::list<bdPeer> mProxiesUnknown;
|
||||
std::list<bdPeer> mProxiesFlagged;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
/* Status options */
|
||||
#define BITDHT_QUERY_READY 1
|
||||
#define BITDHT_QUERY_QUERYING 2
|
||||
#define BITDHT_QUERY_FAILURE 3
|
||||
#define BITDHT_QUERY_FOUND_CLOSEST 4
|
||||
#define BITDHT_QUERY_PEER_UNREACHABLE 5
|
||||
#define BITDHT_QUERY_SUCCESS 6
|
||||
|
||||
/* Query Flags */
|
||||
#define BITDHT_QFLAGS_NONE 0x0000
|
||||
#define BITDHT_QFLAGS_DISGUISE 0x0001
|
||||
#define BITDHT_QFLAGS_DO_IDLE 0x0002
|
||||
#define BITDHT_QFLAGS_INTERNAL 0x0004 // means it runs through startup.
|
||||
#define BITDHT_QFLAGS_QUICK 0x0008 // ONE Request per peer.
|
||||
#define BITDHT_QFLAGS_UPDATES 0x0010 // Do regular updates.
|
||||
|
||||
/* Connect Callback Flags */
|
||||
#define BITDHT_CONNECT_CB_AUTH 1
|
||||
#define BITDHT_CONNECT_CB_PENDING 2
|
||||
#define BITDHT_CONNECT_CB_START 3
|
||||
#define BITDHT_CONNECT_CB_PROXY 4
|
||||
#define BITDHT_CONNECT_CB_FAILED 5
|
||||
#define BITDHT_CONNECT_CB_REQUEST 6
|
||||
|
||||
#define BD_PROXY_CONNECTION_UNKNOWN_POINT 0
|
||||
#define BD_PROXY_CONNECTION_START_POINT 1
|
||||
#define BD_PROXY_CONNECTION_MID_POINT 2
|
||||
#define BD_PROXY_CONNECTION_END_POINT 3
|
||||
|
||||
class BitDhtCallback
|
||||
{
|
||||
public:
|
||||
// ~BitDhtCallback();
|
||||
|
||||
// dummy cos not needed for standard dht behaviour;
|
||||
virtual int dhtNodeCallback(const bdId * /*id*/, uint32_t /*peerflags*/) { return 0; }
|
||||
|
||||
// must be implemented.
|
||||
virtual int dhtPeerCallback(const bdId *id, uint32_t status) = 0;
|
||||
virtual int dhtValueCallback(const bdNodeId *id, std::string key, uint32_t status) = 0;
|
||||
|
||||
// connection callback. Not required for basic behaviour, but forced for initial development.
|
||||
virtual int dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId,
|
||||
uint32_t mode, uint32_t point, uint32_t cbtype, uint32_t errcode) = 0; /* { return 0; } */
|
||||
|
||||
};
|
||||
|
||||
|
||||
class BitDhtInterface
|
||||
{
|
||||
public:
|
||||
|
||||
/***** Request Lookup (DHT Peer & Keyword) *****/
|
||||
virtual void addFindNode(bdNodeId *id, uint32_t mode) = 0;
|
||||
virtual void removeFindNode(bdNodeId *id) = 0;
|
||||
virtual void findDhtValue(bdNodeId *id, std::string key, uint32_t mode) = 0;
|
||||
|
||||
/***** Connections Requests *****/
|
||||
virtual void ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t start) = 0;
|
||||
virtual void ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t answer) = 0;
|
||||
virtual void ConnectionOptions(uint32_t allowedModes, uint32_t flags) = 0;
|
||||
|
||||
|
||||
/***** Add / Remove Callback Clients *****/
|
||||
virtual void addCallback(BitDhtCallback *cb) = 0;
|
||||
virtual void removeCallback(BitDhtCallback *cb) = 0;
|
||||
|
||||
/***** Get Results Details *****/
|
||||
virtual int getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &from) = 0;
|
||||
virtual int getDhtValue(const bdNodeId *id, std::string key, std::string &value) = 0;
|
||||
virtual int getDhtBucket(const int idx, bdBucket &bucket) = 0;
|
||||
|
||||
virtual int getDhtQueries(std::map<bdNodeId, bdQueryStatus> &queries) = 0;
|
||||
virtual int getDhtQueryStatus(const bdNodeId *id, bdQuerySummary &query) = 0;
|
||||
|
||||
/* stats and Dht state */
|
||||
virtual int startDht() = 0;
|
||||
virtual int stopDht() = 0;
|
||||
virtual int stateDht() = 0; /* STOPPED, STARTING, ACTIVE, FAILED */
|
||||
virtual uint32_t statsNetworkSize() = 0;
|
||||
virtual uint32_t statsBDVersionSize() = 0; /* same version as us! */
|
||||
};
|
||||
|
||||
|
||||
// general helper functions for decoding error messages.
|
||||
std::string decodeConnectionError(uint32_t errcode);
|
||||
std::string decodeConnectionErrorCRMove(uint32_t errcode);
|
||||
std::string decodeConnectionErrorSource(uint32_t errcode);
|
||||
std::string decodeConnectionErrorType(uint32_t errcode);
|
||||
|
||||
|
||||
#endif
|
||||
|
1290
libbitdht/src/bitdht/bdmanager.cc
Normal file
1290
libbitdht/src/bitdht/bdmanager.cc
Normal file
File diff suppressed because it is too large
Load Diff
195
libbitdht/src/bitdht/bdmanager.h
Normal file
195
libbitdht/src/bitdht/bdmanager.h
Normal file
@ -0,0 +1,195 @@
|
||||
#ifndef BITDHT_MANAGER_H
|
||||
#define BITDHT_MANAGER_H
|
||||
|
||||
/*
|
||||
* bitdht/bdmanager.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".
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*******
|
||||
* Node Manager.
|
||||
******/
|
||||
|
||||
/******************************************
|
||||
* 1) Maintains a list of ids to search for.
|
||||
* 2) Sets up initial search for own node.
|
||||
* 3) Checks on status of queries.
|
||||
* 4) Callback on successful searches.
|
||||
*
|
||||
* This is pretty specific to RS requirements.
|
||||
****/
|
||||
|
||||
#define BITDHT_PS_MASK_ACTIONS (0x000000ff)
|
||||
#define BITDHT_PS_MASK_STATE (0x0000ff00)
|
||||
|
||||
#define BITDHT_PS_ACTION_SEARCHING (0x00000001)
|
||||
#define BITDHT_PS_ACTION_WAITING (0x00000002)
|
||||
#define BITDHT_PS_ACTION_PINGING (0x00000004)
|
||||
|
||||
#define BITDHT_PS_STATE_UNKNOWN (0x00000100)
|
||||
#define BITDHT_PS_STATE_OFFLINE (0x00000200)
|
||||
#define BITDHT_PS_STATE_ONLINE (0x00000400)
|
||||
#define BITDHT_PS_STATE_CONNECTED (0x00000800)
|
||||
|
||||
#include "bitdht/bdiface.h"
|
||||
#include "bitdht/bdnode.h"
|
||||
#include "util/bdbloom.h"
|
||||
|
||||
|
||||
|
||||
class bdQueryPeer
|
||||
{
|
||||
public:
|
||||
bdId mId;
|
||||
uint32_t mStatus;
|
||||
uint32_t mQFlags;
|
||||
time_t mLastQuery;
|
||||
time_t mLastFound;
|
||||
struct sockaddr_in mDhtAddr;
|
||||
};
|
||||
|
||||
|
||||
#define BITDHT_MGR_STATE_OFF 0
|
||||
#define BITDHT_MGR_STATE_STARTUP 1
|
||||
#define BITDHT_MGR_STATE_FINDSELF 2
|
||||
#define BITDHT_MGR_STATE_ACTIVE 3
|
||||
#define BITDHT_MGR_STATE_REFRESH 4
|
||||
#define BITDHT_MGR_STATE_QUIET 5
|
||||
#define BITDHT_MGR_STATE_FAILED 6
|
||||
|
||||
#define MAX_STARTUP_TIME 10
|
||||
#define MAX_REFRESH_TIME 10
|
||||
|
||||
#define BITDHT_MGR_QUERY_FAILURE 1
|
||||
#define BITDHT_MGR_QUERY_PEER_OFFLINE 2
|
||||
#define BITDHT_MGR_QUERY_PEER_UNREACHABLE 3
|
||||
#define BITDHT_MGR_QUERY_PEER_ONLINE 4
|
||||
|
||||
|
||||
/*** NB: Nothing in here is protected by mutexes
|
||||
* must be done at a higher level!
|
||||
***/
|
||||
|
||||
class bdNodeManager: public bdNode, public BitDhtInterface
|
||||
{
|
||||
public:
|
||||
bdNodeManager(bdNodeId *id, std::string dhtVersion, std::string bootfile, bdDhtFunctions *fns);
|
||||
|
||||
|
||||
void iteration();
|
||||
|
||||
/***** 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(const bdNodeId *id, struct sockaddr_in &from);
|
||||
virtual int getDhtValue(const bdNodeId *id, std::string key, std::string &value);
|
||||
virtual int getDhtBucket(const int idx, bdBucket &bucket);
|
||||
|
||||
virtual int getDhtQueries(std::map<bdNodeId, bdQueryStatus> &queries);
|
||||
virtual int getDhtQueryStatus(const bdNodeId *id, bdQuerySummary &query);
|
||||
|
||||
/***** Connection Interface ****/
|
||||
virtual void ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t start);
|
||||
virtual void ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId,
|
||||
uint32_t mode, uint32_t loc, uint32_t answer);
|
||||
virtual void ConnectionOptions(uint32_t allowedModes, uint32_t flags);
|
||||
|
||||
|
||||
/* stats and Dht state */
|
||||
virtual int startDht();
|
||||
virtual int stopDht();
|
||||
virtual int stateDht(); /* STOPPED, STARTING, ACTIVE, FAILED */
|
||||
virtual uint32_t statsNetworkSize();
|
||||
virtual uint32_t statsBDVersionSize(); /* same version as us! */
|
||||
/******************* Internals *************************/
|
||||
|
||||
// Overloaded from bdnode for external node callback.
|
||||
virtual void addPeer(const bdId *id, uint32_t peerflags);
|
||||
// Overloaded from bdnode for external node callback.
|
||||
virtual void callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId,
|
||||
int mode, int point, int cbtype, int errcode);
|
||||
|
||||
int isBitDhtPacket(char *data, int size, struct sockaddr_in &from);
|
||||
private:
|
||||
|
||||
|
||||
void doNodeCallback(const bdId *id, uint32_t peerflags);
|
||||
void doPeerCallback(const bdId *id, uint32_t status);
|
||||
void doValueCallback(const bdNodeId *id, std::string key, uint32_t status);
|
||||
|
||||
int status();
|
||||
int checkStatus();
|
||||
int checkPingStatus();
|
||||
int SearchOutOfDate();
|
||||
void startQueries();
|
||||
|
||||
int QueryRandomLocalNet();
|
||||
void SearchForLocalNet();
|
||||
|
||||
std::map<bdNodeId, bdQueryPeer> mActivePeers;
|
||||
std::list<BitDhtCallback *> mCallbacks;
|
||||
|
||||
uint32_t mMode;
|
||||
time_t mModeTS;
|
||||
|
||||
time_t mStartTS;
|
||||
time_t mSearchTS;
|
||||
bool mSearchingDone;
|
||||
|
||||
bdDhtFunctions *mFns;
|
||||
|
||||
uint32_t mNetworkSize;
|
||||
uint32_t mBdNetworkSize;
|
||||
|
||||
bdBloom mBloomFilter;
|
||||
|
||||
/* future node functions */
|
||||
//addPeerPing(foundId);
|
||||
//clearPing(it->first);
|
||||
//PingStatus(it->first);
|
||||
};
|
||||
|
||||
class bdDebugCallback: public BitDhtCallback
|
||||
{
|
||||
public:
|
||||
~bdDebugCallback();
|
||||
virtual int dhtPeerCallback(const bdId *id, uint32_t status);
|
||||
virtual int dhtValueCallback(const bdNodeId *id, std::string key, uint32_t status);
|
||||
virtual int dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId,
|
||||
uint32_t mode, uint32_t point, uint32_t cbtype, uint32_t errcode);
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
1034
libbitdht/src/bitdht/bdmsgs.cc
Normal file
1034
libbitdht/src/bitdht/bdmsgs.cc
Normal file
File diff suppressed because it is too large
Load Diff
130
libbitdht/src/bitdht/bdmsgs.h
Normal file
130
libbitdht/src/bitdht/bdmsgs.h
Normal file
@ -0,0 +1,130 @@
|
||||
#ifndef BITDHT_MSGS_H
|
||||
#define BITDHT_MSGS_H
|
||||
|
||||
/*
|
||||
* bitdht/bdmsgs.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 <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include <list>
|
||||
#include "bitdht/bencode.h"
|
||||
#include "bitdht/bdobj.h"
|
||||
#include "bitdht/bdpeer.h"
|
||||
|
||||
#define BITDHT_MSG_TYPE_UNKNOWN 0
|
||||
#define BITDHT_MSG_TYPE_PING 1
|
||||
#define BITDHT_MSG_TYPE_PONG 2
|
||||
#define BITDHT_MSG_TYPE_FIND_NODE 3
|
||||
#define BITDHT_MSG_TYPE_REPLY_NODE 4
|
||||
#define BITDHT_MSG_TYPE_GET_HASH 5
|
||||
#define BITDHT_MSG_TYPE_REPLY_HASH 6
|
||||
#define BITDHT_MSG_TYPE_REPLY_NEAR 7
|
||||
#define BITDHT_MSG_TYPE_POST_HASH 8
|
||||
#define BITDHT_MSG_TYPE_REPLY_POST 9
|
||||
|
||||
|
||||
|
||||
// THESE ARE EXTENSIONS
|
||||
#define BITDHT_MSG_TYPE_CONNECT 20
|
||||
|
||||
// CONNECTIONS.
|
||||
#define BITDHT_MSG_TYPE_CONNECT_REQUEST 101
|
||||
#define BITDHT_MSG_TYPE_CONNECT_REPLY 102
|
||||
#define BITDHT_MSG_TYPE_CONNECT_START 103
|
||||
#define BITDHT_MSG_TYPE_CONNECT_ACK 104
|
||||
|
||||
// FANCY HASHES.
|
||||
|
||||
|
||||
#define BITDHT_COMPACTNODEID_LEN 26
|
||||
#define BITDHT_COMPACTPEERID_LEN 6
|
||||
|
||||
#define BE_Y_UNKNOWN 0
|
||||
#define BE_Y_R 1
|
||||
#define BE_Y_Q 2
|
||||
|
||||
|
||||
/****** Known BD Version Strings ******/
|
||||
|
||||
#define BITDHT_VID_RS1 1
|
||||
#define BITDHT_VID_UT 2
|
||||
|
||||
|
||||
int bitdht_create_ping_msg(bdToken *tid, bdNodeId *id, char *msg, int avail);
|
||||
int bitdht_response_ping_msg(bdToken *tid, bdNodeId *id, bdToken *vid, char *msg, int avail);
|
||||
int bitdht_find_node_msg(bdToken *tid, bdNodeId *id, bdNodeId *target, char *msg, int avail);
|
||||
int bitdht_resp_node_msg(bdToken *tid, bdNodeId *id, std::list<bdId> &nodes,
|
||||
char *msg, int avail);
|
||||
int bitdht_get_peers_msg(bdToken *tid, bdNodeId *id, bdNodeId *info_hash,
|
||||
char *msg, int avail);
|
||||
int bitdht_peers_reply_hash_msg(bdToken *tid, bdNodeId *id,
|
||||
bdToken *token, std::list<std::string> &values,
|
||||
char *msg, int avail);
|
||||
int bitdht_peers_reply_closest_msg(bdToken *tid, bdNodeId *id,
|
||||
bdToken *token, std::list<bdId> &nodes,
|
||||
char *msg, int avail);
|
||||
int bitdht_announce_peers_msg(bdToken *tid, bdNodeId *id, bdNodeId *info_hash,
|
||||
uint32_t port, bdToken *token, char *msg, int avail);
|
||||
int bitdht_reply_announce_msg(bdToken *tid, bdNodeId *id,
|
||||
char *msg, int avail);
|
||||
|
||||
|
||||
// Extensions.
|
||||
int bitdht_connect_genmsg(bdToken *tid, bdNodeId *id, int msgtype, bdId *src, bdId *dest, int mode, int status, char *msg, int avail);
|
||||
|
||||
|
||||
//int response_peers_message()
|
||||
//int response_closestnodes_message()
|
||||
|
||||
be_node *beMsgGetDictNode(be_node *node, const char *key);
|
||||
int beMsgMatchString(be_node *n, const char *str, int len);
|
||||
uint32_t beMsgGetY(be_node *n);
|
||||
uint32_t beMsgType(be_node *n);
|
||||
|
||||
|
||||
uint32_t convertBdVersionToVID(bdVersion *version);
|
||||
|
||||
be_node *makeCompactPeerIds(std::list<std::string> &values);
|
||||
be_node *makeCompactNodeIdString(std::list<bdId> &nodes);
|
||||
|
||||
int beMsgGetToken(be_node *n, bdToken &token);
|
||||
int beMsgGetNodeId(be_node *n, bdNodeId &nodeId);
|
||||
int beMsgGetBdId(be_node *n, bdId &id);
|
||||
int beMsgGetListBdIds(be_node *n, std::list<bdId> &nodes);
|
||||
|
||||
int beMsgGetListStrings(be_node *n, std::list<std::string> &values);
|
||||
int beMsgGetUInt32(be_node *n, uint32_t *port);
|
||||
|
||||
/* Low Level conversion functions */
|
||||
int decodeCompactPeerId(struct sockaddr_in *addr, char *enc, int len);
|
||||
std::string encodeCompactPeerId(struct sockaddr_in *addr);
|
||||
|
||||
int decodeCompactNodeId(bdId *id, char *enc, int len);
|
||||
std::string encodeCompactNodeId(bdId *id);
|
||||
|
||||
|
||||
|
||||
#endif
|
2064
libbitdht/src/bitdht/bdnode.cc
Normal file
2064
libbitdht/src/bitdht/bdnode.cc
Normal file
File diff suppressed because it is too large
Load Diff
260
libbitdht/src/bitdht/bdnode.h
Normal file
260
libbitdht/src/bitdht/bdnode.h
Normal file
@ -0,0 +1,260 @@
|
||||
#ifndef BITDHT_NODE_H
|
||||
#define BITDHT_NODE_H
|
||||
|
||||
/*
|
||||
* bitdht/bdnode.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 "bitdht/bdpeer.h"
|
||||
#include "bitdht/bdquery.h"
|
||||
#include "bitdht/bdstore.h"
|
||||
#include "bitdht/bdobj.h"
|
||||
#include "bitdht/bdhash.h"
|
||||
#include "bitdht/bdhistory.h"
|
||||
|
||||
#include "bitdht/bdconnection.h"
|
||||
#include "bitdht/bdaccount.h"
|
||||
|
||||
class bdFilter;
|
||||
|
||||
|
||||
#define BD_QUERY_NEIGHBOURS 1
|
||||
#define BD_QUERY_HASH 2
|
||||
|
||||
/**********************************
|
||||
* Running a node....
|
||||
*
|
||||
* run().
|
||||
* loops through and checks out of date peers.
|
||||
* handles searches.
|
||||
* prints out dht Table.
|
||||
*
|
||||
|
||||
The node handles the i/o traffic from peers.
|
||||
It
|
||||
|
||||
|
||||
|
||||
ping, return
|
||||
peers, return
|
||||
hash store, return
|
||||
hash get, return
|
||||
|
||||
|
||||
|
||||
respond queue.
|
||||
|
||||
query queue.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
input -> call into recvFunction()
|
||||
output -> call back to Udp().
|
||||
|
||||
|
||||
|
||||
|
||||
*********/
|
||||
|
||||
class bdNodeNetMsg
|
||||
{
|
||||
|
||||
public:
|
||||
bdNodeNetMsg(char *data, int size, struct sockaddr_in *addr);
|
||||
~bdNodeNetMsg();
|
||||
|
||||
void print(std::ostream &out);
|
||||
|
||||
char *data;
|
||||
int mSize;
|
||||
struct sockaddr_in addr;
|
||||
|
||||
};
|
||||
|
||||
class bdNodePublisher
|
||||
{
|
||||
public:
|
||||
/* simplified outgoing msg functions (for the managers) */
|
||||
virtual void send_ping(bdId *id) = 0; /* message out */
|
||||
virtual void send_query(bdId *id, bdNodeId *targetNodeId) = 0; /* message out */
|
||||
virtual void send_connect_msg(bdId *id, int msgtype,
|
||||
bdId *srcAddr, bdId *destAddr, int mode, int status) = 0;
|
||||
|
||||
// internal Callback -> normally continues to callbackConnect().
|
||||
virtual void callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId,
|
||||
int mode, int point, int cbtype, int errcode) = 0;
|
||||
|
||||
};
|
||||
|
||||
|
||||
class bdNode: public bdNodePublisher
|
||||
{
|
||||
public:
|
||||
|
||||
bdNode(bdNodeId *id, std::string dhtVersion, std::string bootfile,
|
||||
bdDhtFunctions *fns);
|
||||
|
||||
void init(); /* sets up the self referential classes (mQueryMgr & mConnMgr) */
|
||||
|
||||
void setNodeOptions(uint32_t optFlags);
|
||||
|
||||
/* startup / shutdown node */
|
||||
void restartNode();
|
||||
void shutdownNode();
|
||||
|
||||
void getOwnId(bdNodeId *id);
|
||||
|
||||
// virtual so manager can do callback.
|
||||
// peer flags defined in bdiface.h
|
||||
virtual void addPeer(const bdId *id, uint32_t peerflags);
|
||||
|
||||
void printState();
|
||||
void checkPotentialPeer(bdId *id, bdId *src);
|
||||
void addPotentialPeer(bdId *id, bdId *src);
|
||||
|
||||
void iterationOff();
|
||||
void iteration();
|
||||
void processRemoteQuery();
|
||||
void updateStore();
|
||||
|
||||
/* simplified outgoing msg functions (for the managers) */
|
||||
virtual void send_ping(bdId *id); /* message out */
|
||||
virtual void send_query(bdId *id, bdNodeId *targetNodeId); /* message out */
|
||||
virtual void send_connect_msg(bdId *id, int msgtype,
|
||||
bdId *srcAddr, bdId *destAddr, int mode, int status);
|
||||
|
||||
// This is implemented in bdManager.
|
||||
// virtual void callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId,
|
||||
// int mode, int point, int cbtype, int errcode);
|
||||
|
||||
/* interaction with outside world (Accessed by controller to deliver us msgs) */
|
||||
int outgoingMsg(struct sockaddr_in *addr, char *msg, int *len);
|
||||
void incomingMsg(struct sockaddr_in *addr, char *msg, int len);
|
||||
|
||||
|
||||
// Below is internal Management of incoming / outgoing messages.
|
||||
private:
|
||||
|
||||
/* internal interaction with network */
|
||||
void sendPkt(char *msg, int len, struct sockaddr_in addr);
|
||||
void recvPkt(char *msg, int len, struct sockaddr_in addr);
|
||||
|
||||
|
||||
/* output functions (send msg) */
|
||||
void msgout_ping(bdId *id, bdToken *transId);
|
||||
void msgout_pong(bdId *id, bdToken *transId);
|
||||
void msgout_find_node(bdId *id, bdToken *transId, bdNodeId *query);
|
||||
void msgout_reply_find_node(bdId *id, bdToken *transId,
|
||||
std::list<bdId> &peers);
|
||||
void msgout_get_hash(bdId *id, bdToken *transId, bdNodeId *info_hash);
|
||||
void msgout_reply_hash(bdId *id, bdToken *transId,
|
||||
bdToken *token, std::list<std::string> &values);
|
||||
void msgout_reply_nearest(bdId *id, bdToken *transId,
|
||||
bdToken *token, std::list<bdId> &peers);
|
||||
|
||||
void msgout_post_hash(bdId *id, bdToken *transId, bdNodeId *info_hash,
|
||||
uint32_t port, bdToken *token);
|
||||
void msgout_reply_post(bdId *id, bdToken *transId);
|
||||
|
||||
|
||||
/* input functions (once mesg is parsed) */
|
||||
void msgin_ping(bdId *id, bdToken *token);
|
||||
void msgin_pong(bdId *id, bdToken *transId, bdToken *versionId);
|
||||
|
||||
void msgin_find_node(bdId *id, bdToken *transId, bdNodeId *query);
|
||||
void msgin_reply_find_node(bdId *id, bdToken *transId,
|
||||
std::list<bdId> &entries);
|
||||
|
||||
void msgin_get_hash(bdId *id, bdToken *transId, bdNodeId *nodeid);
|
||||
void msgin_reply_hash(bdId *id, bdToken *transId,
|
||||
bdToken *token, std::list<std::string> &values);
|
||||
void msgin_reply_nearest(bdId *id, bdToken *transId,
|
||||
bdToken *token, std::list<bdId> &nodes);
|
||||
|
||||
void msgin_post_hash(bdId *id, bdToken *transId,
|
||||
bdNodeId *info_hash, uint32_t port, bdToken *token);
|
||||
void msgin_reply_post(bdId *id, bdToken *transId);
|
||||
|
||||
void msgout_connect_genmsg(bdId *id, bdToken *transId, int msgtype,
|
||||
bdId *srcAddr, bdId *destAddr, int mode, int status);
|
||||
void msgin_connect_genmsg(bdId *id, bdToken *transId, int msgtype,
|
||||
bdId *srcAddr, bdId *destAddr, int mode, int status);
|
||||
|
||||
|
||||
|
||||
/* token handling */
|
||||
void genNewToken(bdToken *token);
|
||||
int queueQuery(bdId *id, bdNodeId *query, bdToken *transId, uint32_t query_type);
|
||||
|
||||
/* transId handling */
|
||||
void genNewTransId(bdToken *token);
|
||||
void registerOutgoingMsg(bdId *id, bdToken *transId, uint32_t msgType);
|
||||
uint32_t checkIncomingMsg(bdId *id, bdToken *transId, uint32_t msgType);
|
||||
void cleanupTransIdRegister();
|
||||
|
||||
|
||||
void doStats();
|
||||
|
||||
/********** Variables **********/
|
||||
private:
|
||||
|
||||
/**** Some Variables are Protected to allow inherited classes to use *****/
|
||||
protected:
|
||||
|
||||
bdSpace mNodeSpace;
|
||||
|
||||
bdQueryManager *mQueryMgr;
|
||||
bdConnectManager *mConnMgr;
|
||||
bdFilter *mFilterPeers;
|
||||
|
||||
bdNodeId mOwnId;
|
||||
bdId mLikelyOwnId; // Try to workout own id address.
|
||||
std::string mDhtVersion;
|
||||
|
||||
bdAccount mAccount;
|
||||
bdStore mStore;
|
||||
|
||||
bdDhtFunctions *mFns;
|
||||
bdHashSpace mHashSpace;
|
||||
|
||||
private:
|
||||
|
||||
uint32_t mNodeOptionFlags;
|
||||
|
||||
bdHistory mHistory; /* for understanding the DHT */
|
||||
|
||||
std::list<bdRemoteQuery> mRemoteQueries;
|
||||
|
||||
std::list<bdId> mPotentialPeers;
|
||||
|
||||
std::list<bdNodeNetMsg *> mOutgoingMsgs;
|
||||
std::list<bdNodeNetMsg *> mIncomingMsgs;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // BITDHT_NODE_H
|
52
libbitdht/src/bitdht/bdobj.cc
Normal file
52
libbitdht/src/bitdht/bdobj.cc
Normal file
@ -0,0 +1,52 @@
|
||||
|
||||
/*
|
||||
* bitdht/bdobj.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 "bitdht/bdobj.h"
|
||||
|
||||
|
||||
void bdPrintTransId(std::ostream &out, bdToken *transId)
|
||||
{
|
||||
//out << transId->data;
|
||||
bdPrintToken(out, transId);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void bdPrintToken(std::ostream &out, bdToken *token)
|
||||
{
|
||||
for(unsigned int i = 0; i < token->len; i++)
|
||||
{
|
||||
out << std::hex << (uint32_t) token->data[i];
|
||||
}
|
||||
out << std::dec;
|
||||
}
|
||||
|
||||
void bdPrintCompactPeerId(std::ostream &out, std::string /*cpi*/ )
|
||||
{
|
||||
out << "DummyCompactPeerId";
|
||||
}
|
||||
|
||||
|
64
libbitdht/src/bitdht/bdobj.h
Normal file
64
libbitdht/src/bitdht/bdobj.h
Normal file
@ -0,0 +1,64 @@
|
||||
#ifndef BITDHT_OBJECTS_H
|
||||
#define BITDHT_OBJECTS_H
|
||||
|
||||
/*
|
||||
* bitdht/bdobj.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".
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#define BITDHT_TOKEN_MAX_LEN 20
|
||||
|
||||
#include <iostream>
|
||||
#include <inttypes.h>
|
||||
|
||||
class bdToken
|
||||
{
|
||||
public:
|
||||
bdToken() :len(0) { return; }
|
||||
uint32_t len;
|
||||
unsigned char data[BITDHT_TOKEN_MAX_LEN];
|
||||
};
|
||||
|
||||
class bdCompactIds
|
||||
{
|
||||
public:
|
||||
bdCompactIds() :len(0) { return; }
|
||||
uint32_t len;
|
||||
unsigned char data[BITDHT_TOKEN_MAX_LEN];
|
||||
};
|
||||
|
||||
class bdVersion
|
||||
{
|
||||
public:
|
||||
bdVersion() :len(0) { return; }
|
||||
uint32_t len;
|
||||
unsigned char data[BITDHT_TOKEN_MAX_LEN];
|
||||
};
|
||||
|
||||
void bdPrintTransId(std::ostream &out, bdToken *transId);
|
||||
void bdPrintToken(std::ostream &out, bdToken *transId);
|
||||
void bdPrintCompactPeerId(std::ostream &out, std::string cpi);
|
||||
|
||||
#endif
|
||||
|
1262
libbitdht/src/bitdht/bdpeer.cc
Normal file
1262
libbitdht/src/bitdht/bdpeer.cc
Normal file
File diff suppressed because it is too large
Load Diff
209
libbitdht/src/bitdht/bdpeer.h
Normal file
209
libbitdht/src/bitdht/bdpeer.h
Normal file
@ -0,0 +1,209 @@
|
||||
#ifndef BITDHT_PEER_H
|
||||
#define BITDHT_PEER_H
|
||||
|
||||
/*
|
||||
* bitdht/bdpeer.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 "bitdht/bdiface.h"
|
||||
#include <time.h>
|
||||
|
||||
/*******
|
||||
* These type of parameters are now DHT Function dependent
|
||||
*
|
||||
#define BITDHT_BUCKET_SIZE 20
|
||||
#define BITDHT_BUCKET_SIZE_BITS 5
|
||||
|
||||
#define BITDHT_N_BUCKETS BITDHT_KEY_BITLEN
|
||||
*
|
||||
*
|
||||
***/
|
||||
|
||||
/***
|
||||
* DEFINED in bdiface.h
|
||||
* #define BITDHT_KEY_LEN 20
|
||||
* #define BITDHT_KEY_INTLEN 5
|
||||
* #define BITDHT_KEY_BITLEN 160
|
||||
***/
|
||||
|
||||
#define BITDHT_ULLONG_BITS 64
|
||||
|
||||
#define BITDHT_MAX_RESPONSE_PERIOD (15)
|
||||
#define BITDHT_MAX_SEND_PERIOD 300 // 5 minutes.
|
||||
#define BITDHT_MAX_RECV_PERIOD (BITDHT_MAX_SEND_PERIOD + BITDHT_MAX_RESPONSE_PERIOD) // didn't respond to a ping.
|
||||
|
||||
// Properly out of date.
|
||||
#define BITDHT_DISCARD_PERIOD (2 * BITDHT_MAX_SEND_PERIOD + BITDHT_MAX_RESPONSE_PERIOD) // didn't respond to two pings.
|
||||
|
||||
// Must have a FLAG by this time. (Make it really quick - so we through away the rubbish).
|
||||
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
/****
|
||||
* DEFINED in bdiface.h
|
||||
*
|
||||
* class bdNodeId
|
||||
* {
|
||||
* public:
|
||||
* unsigned char data[BITDHT_KEY_LEN];
|
||||
* };
|
||||
****/
|
||||
|
||||
|
||||
/****
|
||||
* DEFINED in bdiface.h
|
||||
*
|
||||
|
||||
class bdMetric: public bdNodeId {};
|
||||
|
||||
class bdId
|
||||
{
|
||||
public:
|
||||
|
||||
bdId();
|
||||
bdId(bdNodeId in_id, struct sockaddr_in in_addr);
|
||||
|
||||
struct sockaddr_in addr;
|
||||
bdNodeId id;
|
||||
};
|
||||
*
|
||||
*********/
|
||||
|
||||
|
||||
|
||||
//void bdRandomNodeId(bdNodeId *id);
|
||||
|
||||
// Only Functions that are common for all Dhts.
|
||||
// zero, basic comparisons..
|
||||
void bdZeroNodeId(bdNodeId *id);
|
||||
|
||||
//void bdRandomId(bdId *id);
|
||||
//int bdDistance(const bdNodeId *a, const bdNodeId *b, bdMetric *r);
|
||||
//int bdBucketDistance(const bdMetric *m);
|
||||
//int bdBucketDistance(const bdNodeId *a, const bdNodeId *b);
|
||||
//int operator<(const bdMetric &a, const bdMetric &b);
|
||||
|
||||
//int operator<(const struct sockaddr_in &a, const struct sockaddr_in &b);
|
||||
|
||||
int operator<(const bdNodeId &a, const bdNodeId &b);
|
||||
int operator<(const bdId &a, const bdId &b);
|
||||
int operator==(const bdNodeId &a, const bdNodeId &b);
|
||||
int operator==(const bdId &a, const bdId &b);
|
||||
|
||||
//void bdRandomMidId(const bdNodeId *target, const bdNodeId *other, bdNodeId *mid);
|
||||
|
||||
//void bdPrintId(std::ostream &out, const bdId *a);
|
||||
//void bdPrintNodeId(std::ostream &out, const bdNodeId *a);
|
||||
|
||||
//std::string bdConvertToPrintable(std::string input);
|
||||
|
||||
/****
|
||||
* DEFINED in bdiface.h
|
||||
*
|
||||
|
||||
class bdPeer
|
||||
{
|
||||
public:
|
||||
|
||||
bdId mPeerId;
|
||||
uint32_t mPeerFlags;
|
||||
time_t mLastSendTime;
|
||||
time_t mLastRecvTime;
|
||||
time_t mFoundTime; // time stamp that peer was found
|
||||
};
|
||||
|
||||
|
||||
class bdBucket
|
||||
{
|
||||
public:
|
||||
|
||||
bdBucket();
|
||||
|
||||
// list so we can queue properly
|
||||
std::list<bdPeer> entries;
|
||||
};
|
||||
*
|
||||
*
|
||||
*****/
|
||||
|
||||
class bdSpace
|
||||
{
|
||||
public:
|
||||
|
||||
bdSpace(bdNodeId *ownId, bdDhtFunctions *fns);
|
||||
|
||||
int clear();
|
||||
|
||||
int setAttachedFlag(uint32_t withflags, int count);
|
||||
|
||||
/* accessors */
|
||||
int find_nearest_nodes(const bdNodeId *id, int number,
|
||||
std::multimap<bdMetric, bdId> &nearest);
|
||||
|
||||
int find_nearest_nodes_with_flags(const bdNodeId *id, int number,
|
||||
std::list<bdId> excluding,
|
||||
std::multimap<bdMetric, bdId> &nearest, uint32_t with_flag);
|
||||
|
||||
int find_node(const bdNodeId *id, int number,
|
||||
std::list<bdId> &matchIds, uint32_t with_flag);
|
||||
int find_exactnode(const bdId *id, bdPeer &peer);
|
||||
|
||||
// switched to more efficient single sweep.
|
||||
//int out_of_date_peer(bdId &id); // side-effect updates, send flag on peer.
|
||||
int scanOutOfDatePeers(std::list<bdId> &peerIds);
|
||||
int updateAttachedPeers();
|
||||
|
||||
int add_peer(const bdId *id, uint32_t mode);
|
||||
int printDHT();
|
||||
int getDhtBucket(const int idx, bdBucket &bucket);
|
||||
|
||||
uint32_t calcNetworkSize();
|
||||
uint32_t calcNetworkSizeWithFlag(uint32_t withFlag);
|
||||
uint32_t calcSpaceSize();
|
||||
uint32_t calcSpaceSizeWithFlag(uint32_t withFlag);
|
||||
|
||||
/* special function to enable DHT localisation (i.e find peers from own network) */
|
||||
bool findRandomPeerWithFlag(bdId &id, uint32_t withFlag);
|
||||
|
||||
/* to add later */
|
||||
int updateOwnId(bdNodeId *newOwnId);
|
||||
|
||||
private:
|
||||
|
||||
std::vector<bdBucket> buckets;
|
||||
bdNodeId mOwnId;
|
||||
bdDhtFunctions *mFns;
|
||||
|
||||
uint32_t mAttachedFlags;
|
||||
uint32_t mAttachedCount;
|
||||
time_t mAttachTS;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
1021
libbitdht/src/bitdht/bdquery.cc
Normal file
1021
libbitdht/src/bitdht/bdquery.cc
Normal file
File diff suppressed because it is too large
Load Diff
128
libbitdht/src/bitdht/bdquery.h
Normal file
128
libbitdht/src/bitdht/bdquery.h
Normal file
@ -0,0 +1,128 @@
|
||||
#ifndef BITDHT_QUERY_H
|
||||
#define BITDHT_QUERY_H
|
||||
|
||||
/*
|
||||
* bitdht/bdquery.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 "bitdht/bdiface.h"
|
||||
#include "bitdht/bdpeer.h"
|
||||
#include "bitdht/bdobj.h"
|
||||
|
||||
/* Query result flags are in bdiface.h */
|
||||
|
||||
#define BITDHT_MIN_QUERY_AGE 10
|
||||
#define BITDHT_MAX_QUERY_AGE 1800 /* 30 minutes */
|
||||
|
||||
class bdQuery
|
||||
{
|
||||
public:
|
||||
bdQuery(const bdNodeId *id, std::list<bdId> &startList, uint32_t queryFlags,
|
||||
bdDhtFunctions *fns);
|
||||
|
||||
// get the answer.
|
||||
bool result(std::list<bdId> &answer);
|
||||
bool proxies(std::list<bdId> &answer);
|
||||
bool potentialProxies(std::list<bdId> &answer);
|
||||
|
||||
// returning results get passed to all queries.
|
||||
//void addNode(const bdId *id, int mode);
|
||||
int nextQuery(bdId &id, bdNodeId &targetId);
|
||||
int addPeer(const bdId *id, uint32_t mode);
|
||||
int addPotentialPeer(const bdId *id, const bdId *src, uint32_t srcmode);
|
||||
int printQuery();
|
||||
|
||||
// searching for
|
||||
bdNodeId mId;
|
||||
bdMetric mLimit;
|
||||
uint32_t mState;
|
||||
time_t mQueryTS;
|
||||
uint32_t mQueryFlags;
|
||||
int32_t mSearchTime;
|
||||
|
||||
int32_t mQueryIdlePeerRetryPeriod; // seconds between retries.
|
||||
|
||||
//private:
|
||||
|
||||
// Closest Handling Fns.
|
||||
int addClosestPeer(const bdId *id, uint32_t mode);
|
||||
|
||||
// Potential Handling Fns.
|
||||
int worthyPotentialPeer(const bdId *id);
|
||||
int updatePotentialPeer(const bdId *id, uint32_t mode, uint32_t addType);
|
||||
int trimPotentialPeers_FixedLength();
|
||||
int trimPotentialPeers_toClosest();
|
||||
|
||||
// Proxy Handling Fns.
|
||||
int addProxy(const bdId *id, const bdId *src, uint32_t srcmode);
|
||||
int updateProxy(const bdId *id, uint32_t mode);
|
||||
int updateProxyList(const bdId *id, uint32_t mode, std::list<bdPeer> &searchProxyList);
|
||||
int trimProxies();
|
||||
|
||||
|
||||
// closest peers.
|
||||
std::multimap<bdMetric, bdPeer> mClosest;
|
||||
std::multimap<bdMetric, bdPeer> mPotentialPeers;
|
||||
|
||||
uint32_t mRequiredPeerFlags;
|
||||
std::list<bdPeer> mProxiesUnknown;
|
||||
std::list<bdPeer> mProxiesFlagged;
|
||||
|
||||
int mClosestListSize;
|
||||
bdDhtFunctions *mFns;
|
||||
};
|
||||
|
||||
#if 0
|
||||
|
||||
class bdQueryStatus
|
||||
{
|
||||
public:
|
||||
uint32_t mStatus;
|
||||
uint32_t mQFlags;
|
||||
std::list<bdId> mResults;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* this is just a container class.
|
||||
* we locally seach for this, once then discard.
|
||||
*/
|
||||
class bdRemoteQuery
|
||||
{
|
||||
public:
|
||||
bdRemoteQuery(bdId *id, bdNodeId *query, bdToken *transId, uint32_t query_type);
|
||||
|
||||
bdId mId;
|
||||
bdNodeId mQuery;
|
||||
bdToken mTransId;
|
||||
uint32_t mQueryType;
|
||||
|
||||
time_t mQueryTS;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
380
libbitdht/src/bitdht/bdquerymgr.cc
Normal file
380
libbitdht/src/bitdht/bdquerymgr.cc
Normal file
@ -0,0 +1,380 @@
|
||||
/*
|
||||
* bitdht/bdnode.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 "bitdht/bdquerymgr.h"
|
||||
#include "bitdht/bdnode.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
#define BITDHT_QUERY_START_PEERS 10
|
||||
#define BITDHT_QUERY_NEIGHBOUR_PEERS 8
|
||||
#define BITDHT_MAX_REMOTE_QUERY_AGE 10
|
||||
|
||||
/****
|
||||
* #define DEBUG_NODE_MULTIPEER 1
|
||||
* #define DEBUG_NODE_MSGS 1
|
||||
* #define DEBUG_NODE_ACTIONS 1
|
||||
|
||||
* #define DEBUG_NODE_MSGIN 1
|
||||
* #define DEBUG_NODE_MSGOUT 1
|
||||
***/
|
||||
|
||||
//#define DEBUG_NODE_MSGS 1
|
||||
|
||||
|
||||
bdQueryManager::bdQueryManager(bdSpace *space, bdDhtFunctions *fns, bdNodePublisher *pub)
|
||||
:mNodeSpace(space), mFns(fns), mPub(pub)
|
||||
{
|
||||
}
|
||||
|
||||
/***** Startup / Shutdown ******/
|
||||
void bdQueryManager::shutdownQueries()
|
||||
{
|
||||
/* clear the queries */
|
||||
std::list<bdQuery *>::iterator it;
|
||||
for(it = mLocalQueries.begin(); it != mLocalQueries.end();it++)
|
||||
{
|
||||
delete (*it);
|
||||
}
|
||||
|
||||
mLocalQueries.clear();
|
||||
}
|
||||
|
||||
|
||||
void bdQueryManager::printQueries()
|
||||
{
|
||||
std::cerr << "bdQueryManager::printQueries()";
|
||||
std::cerr << std::endl;
|
||||
|
||||
int i = 0;
|
||||
std::list<bdQuery *>::iterator it;
|
||||
for(it = mLocalQueries.begin(); it != mLocalQueries.end(); it++, i++)
|
||||
{
|
||||
fprintf(stderr, "Query #%d:\n", i);
|
||||
(*it)->printQuery();
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int bdQueryManager::iterateQueries(int maxQueries)
|
||||
{
|
||||
#ifdef DEBUG_NODE_MULTIPEER
|
||||
std::cerr << "bdQueryManager::iterateQueries() of Peer: ";
|
||||
mFns->bdPrintNodeId(std::cerr, &mOwnId);
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
/* allow each query to send up to one query... until maxMsgs has been reached */
|
||||
int numQueries = mLocalQueries.size();
|
||||
int sentQueries = 0;
|
||||
int i = 0;
|
||||
|
||||
bdId id;
|
||||
bdNodeId targetNodeId;
|
||||
|
||||
while((i < numQueries) && (sentQueries < maxQueries))
|
||||
{
|
||||
bdQuery *query = mLocalQueries.front();
|
||||
mLocalQueries.pop_front();
|
||||
mLocalQueries.push_back(query);
|
||||
|
||||
/* go through the possible queries */
|
||||
if (query->nextQuery(id, targetNodeId))
|
||||
{
|
||||
#ifdef DEBUG_NODE_MSGS
|
||||
std::cerr << "bdQueryManager::iteration() send_query(";
|
||||
mFns->bdPrintId(std::cerr, &id);
|
||||
std::cerr << ",";
|
||||
mFns->bdPrintNodeId(std::cerr, &targetNodeId);
|
||||
std::cerr << ")";
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
mPub->send_query(&id, &targetNodeId);
|
||||
sentQueries++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_NODE_ACTIONS
|
||||
std::cerr << "bdQueryManager::iteration() maxMsgs: " << maxMsgs << " sentPings: " << sentPings;
|
||||
std::cerr << " / " << allowedPings;
|
||||
std::cerr << " sentQueries: " << sentQueries;
|
||||
std::cerr << " / " << numQueries;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
//printQueries();
|
||||
|
||||
return sentQueries;
|
||||
}
|
||||
|
||||
|
||||
bool bdQueryManager::checkPotentialPeer(bdId *id, bdId *src)
|
||||
{
|
||||
bool isWorthyPeer = false;
|
||||
/* also push to queries */
|
||||
std::list<bdQuery *>::iterator it;
|
||||
for(it = mLocalQueries.begin(); it != mLocalQueries.end(); it++)
|
||||
{
|
||||
if ((*it)->addPotentialPeer(id, src, 0))
|
||||
{
|
||||
isWorthyPeer = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isWorthyPeer)
|
||||
{
|
||||
isWorthyPeer = checkWorthyPeerSources(src);
|
||||
}
|
||||
|
||||
return isWorthyPeer;
|
||||
}
|
||||
|
||||
|
||||
void bdQueryManager::addPeer(const bdId *id, uint32_t peerflags)
|
||||
{
|
||||
|
||||
#ifdef DEBUG_NODE_ACTIONS
|
||||
fprintf(stderr, "bdQueryManager::addPeer(");
|
||||
mFns->bdPrintId(std::cerr, id);
|
||||
fprintf(stderr, ")\n");
|
||||
#endif
|
||||
|
||||
/* iterate through queries */
|
||||
std::list<bdQuery *>::iterator it;
|
||||
for(it = mLocalQueries.begin(); it != mLocalQueries.end(); it++)
|
||||
{
|
||||
(*it)->addPeer(id, peerflags);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/************************************ Query Details *************************/
|
||||
void bdQueryManager::addQuery(const bdNodeId *id, uint32_t qflags)
|
||||
{
|
||||
|
||||
std::list<bdId> startList;
|
||||
std::multimap<bdMetric, bdId> nearest;
|
||||
std::multimap<bdMetric, bdId>::iterator it;
|
||||
|
||||
mNodeSpace->find_nearest_nodes(id, BITDHT_QUERY_START_PEERS, nearest);
|
||||
|
||||
fprintf(stderr, "bdQueryManager::addQuery(");
|
||||
mFns->bdPrintNodeId(std::cerr, id);
|
||||
fprintf(stderr, ")\n");
|
||||
|
||||
for(it = nearest.begin(); it != nearest.end(); it++)
|
||||
{
|
||||
startList.push_back(it->second);
|
||||
}
|
||||
|
||||
bdQuery *query = new bdQuery(id, startList, qflags, mFns);
|
||||
mLocalQueries.push_back(query);
|
||||
}
|
||||
|
||||
|
||||
void bdQueryManager::clearQuery(const bdNodeId *rmId)
|
||||
{
|
||||
std::list<bdQuery *>::iterator it;
|
||||
for(it = mLocalQueries.begin(); it != mLocalQueries.end();)
|
||||
{
|
||||
if ((*it)->mId == *rmId)
|
||||
{
|
||||
bdQuery *query = (*it);
|
||||
it = mLocalQueries.erase(it);
|
||||
delete query;
|
||||
}
|
||||
else
|
||||
{
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void bdQueryManager::QueryStatus(std::map<bdNodeId, bdQueryStatus> &statusMap)
|
||||
{
|
||||
std::list<bdQuery *>::iterator it;
|
||||
for(it = mLocalQueries.begin(); it != mLocalQueries.end(); it++)
|
||||
{
|
||||
bdQueryStatus status;
|
||||
status.mStatus = (*it)->mState;
|
||||
status.mQFlags = (*it)->mQueryFlags;
|
||||
(*it)->result(status.mResults);
|
||||
statusMap[(*it)->mId] = status;
|
||||
}
|
||||
}
|
||||
|
||||
int bdQueryManager::QuerySummary(const bdNodeId *id, bdQuerySummary &query)
|
||||
{
|
||||
std::list<bdQuery *>::iterator it;
|
||||
for(it = mLocalQueries.begin(); it != mLocalQueries.end(); it++)
|
||||
{
|
||||
if ((*it)->mId == *id)
|
||||
{
|
||||
query.mId = (*it)->mId;
|
||||
query.mLimit = (*it)->mLimit;
|
||||
query.mState = (*it)->mState;
|
||||
query.mQueryTS = (*it)->mQueryTS;
|
||||
query.mQueryFlags = (*it)->mQueryFlags;
|
||||
query.mSearchTime = (*it)->mSearchTime;
|
||||
query.mClosest = (*it)->mClosest;
|
||||
query.mPotentialPeers = (*it)->mPotentialPeers;
|
||||
query.mProxiesUnknown = (*it)->mProxiesUnknown;
|
||||
query.mProxiesFlagged = (*it)->mProxiesFlagged;
|
||||
query.mQueryIdlePeerRetryPeriod = (*it)->mQueryIdlePeerRetryPeriod;
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Extract Results from Peer Queries */
|
||||
|
||||
#define BDQRYMGR_RESULTS 1
|
||||
#define BDQRYMGR_PROXIES 2
|
||||
#define BDQRYMGR_POTPROXIES 3
|
||||
|
||||
int bdQueryManager::getResults(bdNodeId *target, std::list<bdId> &answer, int querytype)
|
||||
{
|
||||
|
||||
/* grab any peers from any existing query */
|
||||
int results = 0;
|
||||
std::list<bdQuery *>::iterator qit;
|
||||
for(qit = mLocalQueries.begin(); qit != mLocalQueries.end(); qit++)
|
||||
{
|
||||
if (!((*qit)->mId == (*target)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_NODE_CONNECTION
|
||||
std::cerr << "bdQueryManager::getResults() Found Matching Query";
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
switch(querytype)
|
||||
{
|
||||
default:
|
||||
case BDQRYMGR_RESULTS:
|
||||
results = (*qit)->result(answer);
|
||||
break;
|
||||
case BDQRYMGR_PROXIES:
|
||||
results = (*qit)->proxies(answer);
|
||||
break;
|
||||
case BDQRYMGR_POTPROXIES:
|
||||
results = (*qit)->potentialProxies(answer);
|
||||
break;
|
||||
}
|
||||
/* will only be one matching query.. so end loop */
|
||||
return results;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int bdQueryManager::result(bdNodeId *target, std::list<bdId> &answer)
|
||||
{
|
||||
return getResults(target, answer, BDQRYMGR_RESULTS);
|
||||
}
|
||||
|
||||
int bdQueryManager::proxies(bdNodeId *target, std::list<bdId> &answer)
|
||||
{
|
||||
return getResults(target, answer, BDQRYMGR_PROXIES);
|
||||
}
|
||||
|
||||
int bdQueryManager::potentialProxies(bdNodeId *target, std::list<bdId> &answer)
|
||||
{
|
||||
return getResults(target, answer, BDQRYMGR_POTPROXIES);
|
||||
}
|
||||
|
||||
|
||||
/************ WORTHY PEERS **********/
|
||||
|
||||
#define MAX_WORTHY_PEER_AGE 15
|
||||
|
||||
void bdQueryManager::addWorthyPeerSource(bdId *src)
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
|
||||
bdPeer peer;
|
||||
peer.mPeerId = *src;
|
||||
peer.mFoundTime = now;
|
||||
|
||||
#ifdef DEBUG_NODE_ACTIONS
|
||||
std::cerr << "bdQueryManager::addWorthyPeerSource(";
|
||||
mFns->bdPrintId(std::cerr, src);
|
||||
std::cerr << ")" << std::endl;
|
||||
#endif
|
||||
|
||||
mWorthyPeerSources.push_back(peer);
|
||||
}
|
||||
|
||||
bool bdQueryManager::checkWorthyPeerSources(bdId *src)
|
||||
{
|
||||
if (!src)
|
||||
return false;
|
||||
|
||||
time_t now = time(NULL);
|
||||
std::list<bdPeer>::iterator it;
|
||||
for(it = mWorthyPeerSources.begin(); it != mWorthyPeerSources.end(); )
|
||||
{
|
||||
if (now - it->mFoundTime > MAX_WORTHY_PEER_AGE)
|
||||
{
|
||||
#ifdef DEBUG_NODE_ACTIONS
|
||||
std::cerr << "bdQueryManager::checkWorthyPeerSource() Discard old Source: ";
|
||||
mFns->bdPrintId(std::cerr, &(it->mPeerId));
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
it = mWorthyPeerSources.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (it->mPeerId == *src)
|
||||
{
|
||||
#ifdef DEBUG_NODE_ACTIONS
|
||||
std::cerr << "bdQueryManager::checkWorthyPeerSource(";
|
||||
mFns->bdPrintId(std::cerr, src);
|
||||
std::cerr << ") = true" << std::endl;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
it++;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
78
libbitdht/src/bitdht/bdquerymgr.h
Normal file
78
libbitdht/src/bitdht/bdquerymgr.h
Normal file
@ -0,0 +1,78 @@
|
||||
#ifndef BITDHT_QUERY_MANAGER_H
|
||||
#define BITDHT_QUERY_MANAGER_H
|
||||
|
||||
/*
|
||||
* bitdht/bdquerymgr.h
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2011 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 "bitdht/bdquery.h"
|
||||
class bdNodePublisher;
|
||||
|
||||
|
||||
class bdQueryManager
|
||||
{
|
||||
public:
|
||||
|
||||
bdQueryManager(bdSpace *space, bdDhtFunctions *fns, bdNodePublisher *pub);
|
||||
|
||||
void shutdownQueries();
|
||||
void printQueries();
|
||||
|
||||
int iterateQueries(int maxqueries);
|
||||
|
||||
bool checkPotentialPeer(bdId *id, bdId *src);
|
||||
void addPeer(const bdId *id, uint32_t peerflags);
|
||||
|
||||
void addQuery(const bdNodeId *id, uint32_t qflags);
|
||||
void clearQuery(const bdNodeId *id);
|
||||
void QueryStatus(std::map<bdNodeId, bdQueryStatus> &statusMap);
|
||||
int QuerySummary(const bdNodeId *id, bdQuerySummary &query);
|
||||
|
||||
int result(bdNodeId *target, std::list<bdId> &answer);
|
||||
int proxies(bdNodeId *target, std::list<bdId> &answer);
|
||||
int potentialProxies(bdNodeId *target, std::list<bdId> &answer);
|
||||
|
||||
// extra "Worthy Peers" we will want to ping.
|
||||
void addWorthyPeerSource(bdId *src);
|
||||
bool checkWorthyPeerSources(bdId *src);
|
||||
|
||||
private:
|
||||
|
||||
int getResults(bdNodeId *target, std::list<bdId> &answer, int querytype);
|
||||
|
||||
/* NB: No Mutex Protection... Single threaded, Mutex at higher level!
|
||||
*/
|
||||
|
||||
bdSpace *mNodeSpace;
|
||||
bdDhtFunctions *mFns;
|
||||
bdNodePublisher *mPub;
|
||||
|
||||
std::list<bdQuery *> mLocalQueries;
|
||||
std::list<bdPeer> mWorthyPeerSources;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // BITDHT_QUERY_MANAGER_H
|
313
libbitdht/src/bitdht/bdstddht.cc
Normal file
313
libbitdht/src/bitdht/bdstddht.cc
Normal file
@ -0,0 +1,313 @@
|
||||
/*
|
||||
* bitdht/bdstddht.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 "bitdht/bdstddht.h"
|
||||
#include "bitdht/bdpeer.h"
|
||||
#include "util/bdrandom.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
/**
|
||||
* #define BITDHT_DEBUG 1
|
||||
**/
|
||||
|
||||
void bdStdRandomId(bdId *id)
|
||||
{
|
||||
bdStdRandomNodeId(&(id->id));
|
||||
id->addr.sin_addr.s_addr = bdRandom::random_u32();
|
||||
id->addr.sin_port = (bdRandom::random_u32() % USHRT_MAX);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void bdStdRandomNodeId(bdNodeId *id)
|
||||
{
|
||||
uint32_t *a_data = (uint32_t *) id->data;
|
||||
for(int i = 0; i < BITDHT_KEY_INTLEN; i++)
|
||||
{
|
||||
a_data[i] = bdRandom::random_u32();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void bdStdZeroNodeId(bdNodeId *id)
|
||||
{
|
||||
uint32_t *a_data = (uint32_t *) id->data;
|
||||
for(int i = 0; i < BITDHT_KEY_INTLEN; i++)
|
||||
{
|
||||
a_data[i] = 0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Ignore differences in port....
|
||||
// must be careful which one we accept after this.
|
||||
// can could end-up with the wrong port.
|
||||
// However this only matters with firewalled peers anyway.
|
||||
// So not too serious.
|
||||
bool bdStdSimilarId(const bdId *n1, const bdId *n2)
|
||||
{
|
||||
if (n1->id == n2->id)
|
||||
{
|
||||
if (n1->addr.sin_addr.s_addr == n2->addr.sin_addr.s_addr)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool bdStdUpdateSimilarId(bdId *dest, const bdId *src)
|
||||
{
|
||||
/* only difference that's currently allowed */
|
||||
if (dest->addr.sin_port == src->addr.sin_port)
|
||||
{
|
||||
/* no update required */
|
||||
return false;
|
||||
}
|
||||
|
||||
dest->addr.sin_port = src->addr.sin_port;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/* fills in bdNodeId r, with XOR of a and b */
|
||||
int bdStdDistance(const bdNodeId *a, const bdNodeId *b, bdMetric *r)
|
||||
{
|
||||
uint8_t *a_data = (uint8_t *) a->data;
|
||||
uint8_t *b_data = (uint8_t *) b->data;
|
||||
uint8_t *ans = (uint8_t *) r->data;
|
||||
for(int i = 0; i < BITDHT_KEY_LEN; i++)
|
||||
{
|
||||
*(ans++) = *(a_data++) ^ *(b_data++);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void bdStdRandomMidId(const bdNodeId *target, const bdNodeId *other, bdNodeId *midId)
|
||||
{
|
||||
bdMetric dist;
|
||||
|
||||
/* get distance between a & c */
|
||||
bdStdDistance(target, other, &dist);
|
||||
|
||||
/* generate Random Id */
|
||||
bdStdRandomNodeId(midId);
|
||||
|
||||
/* zero bits of Random Id until under 1/2 of distance
|
||||
* done in bytes for ease... matches one extra byte than distance = 0
|
||||
* -> hence wierd order of operations
|
||||
*/
|
||||
//bool done = false;
|
||||
for(int i = 0; i < BITDHT_KEY_LEN; i++)
|
||||
{
|
||||
midId->data[i] = target->data[i];
|
||||
|
||||
if (dist.data[i] != 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int bdStdLoadNodeId(bdNodeId *id, std::string input)
|
||||
{
|
||||
uint8_t *a_data = (uint8_t *) id->data;
|
||||
int reqlen = BITDHT_KEY_LEN * 2;
|
||||
if (input.size() < reqlen)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
for(int i = 0; i < BITDHT_KEY_LEN; i++)
|
||||
{
|
||||
char ch1 = input[2 * i];
|
||||
char ch2 = input[2 * i + 1];
|
||||
uint8_t value1 = 0;
|
||||
uint8_t value2 = 0;
|
||||
|
||||
/* do char1 */
|
||||
if (ch1 >= '0' && ch1 <= '9')
|
||||
value1 = (ch1 - '0');
|
||||
else if (ch1 >= 'A' && ch1 <= 'F')
|
||||
value1 = (ch1 - 'A' + 10);
|
||||
else if (ch1 >= 'a' && ch1 <= 'f')
|
||||
value1 = (ch1 - 'a' + 10);
|
||||
|
||||
/* do char2 */
|
||||
if (ch2 >= '0' && ch2 <= '9')
|
||||
value2 = (ch2 - '0');
|
||||
else if (ch2 >= 'A' && ch2 <= 'F')
|
||||
value2 = (ch2 - 'A' + 10);
|
||||
else if (ch2 >= 'a' && ch2 <= 'f')
|
||||
value2 = (ch2 - 'a' + 10);
|
||||
|
||||
a_data[i] = (value1 << 4) + value2;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string bdStdConvertToPrintable(std::string input)
|
||||
{
|
||||
std::ostringstream out;
|
||||
for(uint32_t i = 0; i < input.length(); i++)
|
||||
{
|
||||
/* sensible chars */
|
||||
if ((input[i] > 31) && (input[i] < 127))
|
||||
{
|
||||
out << input[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
out << "[0x" << std::hex << (uint32_t) input[i] << "]";
|
||||
out << std::dec;
|
||||
}
|
||||
}
|
||||
return out.str();
|
||||
}
|
||||
|
||||
void bdStdPrintNodeId(std::ostream &out, const bdNodeId *a)
|
||||
{
|
||||
for(int i = 0; i < BITDHT_KEY_LEN; i++)
|
||||
{
|
||||
out << std::setw(2) << std::setfill('0') << std::hex << (uint32_t) (a->data)[i];
|
||||
}
|
||||
out << std::dec;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void bdStdPrintId(std::ostream &out, const bdId *a)
|
||||
{
|
||||
bdStdPrintNodeId(out, &(a->id));
|
||||
out << " ip:" << inet_ntoa(a->addr.sin_addr);
|
||||
out << ":" << ntohs(a->addr.sin_port);
|
||||
return;
|
||||
}
|
||||
|
||||
/* returns 0-160 depending on bucket */
|
||||
int bdStdBucketDistance(const bdNodeId *a, const bdNodeId *b)
|
||||
{
|
||||
bdMetric m;
|
||||
bdStdDistance(a, b, &m);
|
||||
return bdStdBucketDistance(&m);
|
||||
}
|
||||
|
||||
/* returns 0-160 depending on bucket */
|
||||
int bdStdBucketDistance(const bdMetric *m)
|
||||
{
|
||||
for(int i = 0; i < BITDHT_KEY_BITLEN; i++)
|
||||
{
|
||||
int bit = BITDHT_KEY_BITLEN - i - 1;
|
||||
int byte = i / 8;
|
||||
int bbit = 7 - (i % 8);
|
||||
unsigned char comp = (1 << bbit);
|
||||
|
||||
#ifdef BITDHT_DEBUG
|
||||
fprintf(stderr, "bdStdBucketDistance: bit:%d byte:%d bbit:%d comp:%x, data:%x\n", bit, byte, bbit, comp, m->data[byte]);
|
||||
#endif
|
||||
|
||||
if (comp & m->data[byte])
|
||||
{
|
||||
return bit;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
bdStdDht::bdStdDht()
|
||||
{
|
||||
return;
|
||||
}
|
||||
/* setup variables */
|
||||
uint16_t bdStdDht::bdNumBuckets()
|
||||
{
|
||||
|
||||
return BITDHT_STANDARD_N_BUCKETS;
|
||||
}
|
||||
|
||||
uint16_t bdStdDht::bdNodesPerBucket() /* used for query + bdspace */
|
||||
{
|
||||
return BITDHT_STANDARD_BUCKET_SIZE;
|
||||
}
|
||||
uint16_t bdStdDht::bdBucketBitSize()
|
||||
{
|
||||
return BITDHT_STANDARD_BUCKET_SIZE_BITS;
|
||||
}
|
||||
|
||||
int bdStdDht::bdDistance(const bdNodeId *n1, const bdNodeId *n2, class bdMetric *metric)
|
||||
{
|
||||
return bdStdDistance(n1, n2, metric);
|
||||
}
|
||||
|
||||
int bdStdDht::bdBucketDistance(const bdNodeId *n1, const bdNodeId *n2)
|
||||
{
|
||||
return bdStdBucketDistance(n1, n2);
|
||||
}
|
||||
|
||||
int bdStdDht::bdBucketDistance(const bdMetric *metric)
|
||||
{
|
||||
return bdStdBucketDistance(metric);
|
||||
}
|
||||
|
||||
|
||||
bool bdStdDht::bdSimilarId(const bdId *id1, const bdId *id2)
|
||||
{
|
||||
return bdStdSimilarId(id1, id2);
|
||||
}
|
||||
|
||||
|
||||
bool bdStdDht::bdUpdateSimilarId(bdId *dest, const bdId *src)
|
||||
{
|
||||
return bdStdUpdateSimilarId(dest, src);
|
||||
}
|
||||
|
||||
|
||||
void bdStdDht::bdRandomMidId(const bdNodeId *target, const bdNodeId *other, bdNodeId *mid)
|
||||
{
|
||||
return bdStdRandomMidId(target, other, mid);
|
||||
}
|
||||
|
||||
|
||||
void bdStdDht::bdPrintId(std::ostream &out, const bdId *a)
|
||||
{
|
||||
return bdStdPrintId(out, a);
|
||||
}
|
||||
|
||||
void bdStdDht::bdPrintNodeId(std::ostream &out, const bdNodeId *a)
|
||||
{
|
||||
return bdStdPrintNodeId(out, a);
|
||||
}
|
||||
|
||||
|
||||
|
91
libbitdht/src/bitdht/bdstddht.h
Normal file
91
libbitdht/src/bitdht/bdstddht.h
Normal file
@ -0,0 +1,91 @@
|
||||
#ifndef BITDHT_STANDARD_DHT_H
|
||||
#define BITDHT_STANDARD_DHT_H
|
||||
|
||||
/*
|
||||
* bitdht/bdstddht.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 "bitdht/bdiface.h"
|
||||
|
||||
#define BITDHT_STANDARD_BUCKET_SIZE 10 // 20 too many per query?
|
||||
#define BITDHT_STANDARD_BUCKET_SIZE_BITS 5
|
||||
|
||||
#define BITDHT_STANDARD_N_BUCKETS BITDHT_KEY_BITLEN
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
|
||||
|
||||
|
||||
void bdStdRandomNodeId(bdNodeId *id);
|
||||
void bdStdZeroNodeId(bdNodeId *id);
|
||||
|
||||
void bdStdRandomId(bdId *id);
|
||||
int bdStdDistance(const bdNodeId *a, const bdNodeId *b, bdMetric *r);
|
||||
int bdStdBucketDistance(const bdMetric *m);
|
||||
int bdStdBucketDistance(const bdNodeId *a, const bdNodeId *b);
|
||||
|
||||
void bdStdRandomMidId(const bdNodeId *target, const bdNodeId *other, bdNodeId *mid);
|
||||
|
||||
int bdStdLoadNodeId(bdNodeId *id, std::string input);
|
||||
|
||||
void bdStdPrintId(std::ostream &out, const bdId *a);
|
||||
void bdStdPrintNodeId(std::ostream &out, const bdNodeId *a);
|
||||
|
||||
std::string bdStdConvertToPrintable(std::string input);
|
||||
|
||||
//uint32_t bdStdSimilarNode(const bdId*, const bdId*);
|
||||
|
||||
|
||||
class bdStdDht: public bdDhtFunctions
|
||||
{
|
||||
public:
|
||||
|
||||
bdStdDht();
|
||||
/* setup variables */
|
||||
virtual uint16_t bdNumBuckets();
|
||||
virtual uint16_t bdNodesPerBucket(); /* used for query + bdspace */
|
||||
virtual uint16_t bdBucketBitSize();
|
||||
|
||||
virtual int bdDistance(const bdNodeId *n1, const bdNodeId *n2, bdMetric *metric);
|
||||
virtual int bdBucketDistance(const bdNodeId *n1, const bdNodeId *n2);
|
||||
virtual int bdBucketDistance(const bdMetric *metric);
|
||||
|
||||
virtual bool bdSimilarId(const bdId *id1, const bdId *id2);
|
||||
virtual bool bdUpdateSimilarId(bdId *dest, const bdId *src); /* returns true if update was necessary */
|
||||
|
||||
virtual void bdRandomMidId(const bdNodeId *target, const bdNodeId *other, bdNodeId *mid);
|
||||
|
||||
virtual void bdPrintId(std::ostream &out, const bdId *a);
|
||||
virtual void bdPrintNodeId(std::ostream &out, const bdNodeId *a);
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
253
libbitdht/src/bitdht/bdstore.cc
Normal file
253
libbitdht/src/bitdht/bdstore.cc
Normal file
@ -0,0 +1,253 @@
|
||||
/*
|
||||
* bitdht/bdstore.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 "bitdht/bdstore.h"
|
||||
#include "util/bdnet.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
//#define DEBUG_STORE 1
|
||||
|
||||
bdStore::bdStore(std::string file, bdDhtFunctions *fns)
|
||||
:mFns(fns)
|
||||
{
|
||||
#ifdef DEBUG_STORE
|
||||
std::cerr << "bdStore::bdStore(" << file << ")";
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
/* read data from file */
|
||||
mStoreFile = file;
|
||||
|
||||
reloadFromStore();
|
||||
}
|
||||
|
||||
int bdStore::clear()
|
||||
{
|
||||
mIndex = 0;
|
||||
store.clear();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int bdStore::reloadFromStore()
|
||||
{
|
||||
clear();
|
||||
|
||||
FILE *fd = fopen(mStoreFile.c_str(), "r");
|
||||
if (!fd)
|
||||
{
|
||||
fprintf(stderr, "Failed to Open File: %s ... No Peers\n", mStoreFile.c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
char line[10240];
|
||||
char addr_str[10240];
|
||||
struct sockaddr_in addr;
|
||||
addr.sin_family = PF_INET;
|
||||
unsigned short port;
|
||||
|
||||
while(line == fgets(line, 10240, fd))
|
||||
{
|
||||
if (2 == sscanf(line, "%s %hd", addr_str, &port))
|
||||
{
|
||||
if (bdnet_inet_aton(addr_str, &(addr.sin_addr)))
|
||||
{
|
||||
addr.sin_port = htons(port);
|
||||
bdPeer peer;
|
||||
//bdZeroNodeId(&(peer.mPeerId.id));
|
||||
peer.mPeerId.addr = addr;
|
||||
peer.mLastSendTime = 0;
|
||||
peer.mLastRecvTime = 0;
|
||||
store.push_back(peer);
|
||||
#ifdef DEBUG_STORE
|
||||
fprintf(stderr, "Read: %s %d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fd);
|
||||
|
||||
#ifdef DEBUG_STORE
|
||||
fprintf(stderr, "Read %ld Peers\n", (long) store.size());
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
// This is a very ugly function!
|
||||
int bdStore::getPeer(bdPeer *peer)
|
||||
{
|
||||
#ifdef DEBUG_STORE
|
||||
fprintf(stderr, "bdStore::getPeer() %ld Peers left\n", (long) store.size());
|
||||
#endif
|
||||
|
||||
std::list<bdPeer>::iterator it;
|
||||
int i = 0;
|
||||
for(it = store.begin(); (it != store.end()) && (i < mIndex); it++, i++) ; /* empty loop */
|
||||
if (it != store.end())
|
||||
{
|
||||
*peer = *it;
|
||||
mIndex++;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bdStore::filterIpList(const std::list<struct sockaddr_in> &filteredIPs)
|
||||
{
|
||||
// Nasty O(n^2) iteration over 500 entries!!!.
|
||||
// hope its not used to often.
|
||||
|
||||
std::list<struct sockaddr_in>::const_iterator it;
|
||||
for(it = filteredIPs.begin(); it != filteredIPs.end(); it++)
|
||||
{
|
||||
std::list<bdPeer>::iterator sit;
|
||||
for(sit = store.begin(); sit != store.end();)
|
||||
{
|
||||
if (it->sin_addr.s_addr == sit->mPeerId.addr.sin_addr.s_addr)
|
||||
{
|
||||
std::cerr << "bdStore::filterIpList() Found Bad entry in Store. Erasing!";
|
||||
std::cerr << std::endl;
|
||||
|
||||
sit = store.erase(sit);
|
||||
}
|
||||
else
|
||||
{
|
||||
sit++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define MAX_ENTRIES 500
|
||||
|
||||
/* maintain a sorted list */
|
||||
void bdStore::addStore(bdPeer *peer)
|
||||
{
|
||||
#ifdef DEBUG_STORE
|
||||
std::cerr << "bdStore::addStore() ";
|
||||
mFns->bdPrintId(std::cerr, &(peer->mPeerId));
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
/* remove old entry */
|
||||
bool removed = false;
|
||||
|
||||
std::list<bdPeer>::iterator it;
|
||||
for(it = store.begin(); it != store.end(); )
|
||||
{
|
||||
if ((it->mPeerId.addr.sin_addr.s_addr == peer->mPeerId.addr.sin_addr.s_addr) &&
|
||||
(it->mPeerId.addr.sin_port == peer->mPeerId.addr.sin_port))
|
||||
{
|
||||
removed = true;
|
||||
#ifdef DEBUG_STORE
|
||||
std::cerr << "bdStore::addStore() Removed Existing Entry: ";
|
||||
mFns->bdPrintId(std::cerr, &(it->mPeerId));
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
it = store.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
it++;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef DEBUG_STORE
|
||||
std::cerr << "bdStore::addStore() Push_back";
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
store.push_back(*peer);
|
||||
|
||||
while(store.size() > MAX_ENTRIES)
|
||||
{
|
||||
#ifdef DEBUG_STORE
|
||||
std::cerr << "bdStore::addStore() pop_front()";
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
store.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void bdStore::writeStore(std::string file)
|
||||
{
|
||||
/* write out store */
|
||||
#ifdef DEBUG_STORE
|
||||
fprintf(stderr, "bdStore::writeStore(%s) = %d entries\n", file.c_str(), store.size());
|
||||
#endif
|
||||
|
||||
if (store.size() < 0.9 * MAX_ENTRIES)
|
||||
{
|
||||
/* don't save yet! */
|
||||
#ifdef DEBUG_STORE
|
||||
fprintf(stderr, "bdStore::writeStore() Delaying until more entries\n");
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
FILE *fd = fopen(file.c_str(), "w");
|
||||
|
||||
if (!fd)
|
||||
{
|
||||
#ifdef DEBUG_STORE
|
||||
#endif
|
||||
fprintf(stderr, "bdStore::writeStore() FAILED to Open File\n");
|
||||
return;
|
||||
}
|
||||
|
||||
std::list<bdPeer>::iterator it;
|
||||
for(it = store.begin(); it != store.end(); it++)
|
||||
{
|
||||
fprintf(fd, "%s %d\n", inet_ntoa(it->mPeerId.addr.sin_addr), ntohs(it->mPeerId.addr.sin_port));
|
||||
#ifdef DEBUG_STORE
|
||||
fprintf(stderr, "Storing Peer Address: %s %d\n", inet_ntoa(it->mPeerId.addr.sin_addr), ntohs(it->mPeerId.addr.sin_port));
|
||||
#endif
|
||||
|
||||
}
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
void bdStore::writeStore()
|
||||
{
|
||||
#if 0
|
||||
if (mStoreFile == "")
|
||||
{
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
return writeStore(mStoreFile);
|
||||
}
|
||||
|
||||
|
||||
|
57
libbitdht/src/bitdht/bdstore.h
Normal file
57
libbitdht/src/bitdht/bdstore.h
Normal file
@ -0,0 +1,57 @@
|
||||
#ifndef BITDHT_STORE_H
|
||||
#define BITDHT_STORE_H
|
||||
|
||||
/*
|
||||
* bitdht/bdstore.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 <string>
|
||||
#include "bitdht/bdiface.h"
|
||||
#include "bitdht/bdpeer.h"
|
||||
|
||||
class bdStore
|
||||
{
|
||||
public:
|
||||
|
||||
bdStore(std::string file, bdDhtFunctions *fns);
|
||||
|
||||
int reloadFromStore(); /* for restarts */
|
||||
int filterIpList(const std::list<struct sockaddr_in> &filteredIPs);
|
||||
int clear();
|
||||
|
||||
int getPeer(bdPeer *peer);
|
||||
void addStore(bdPeer *peer);
|
||||
void writeStore(std::string file);
|
||||
void writeStore();
|
||||
|
||||
private:
|
||||
std::string mStoreFile;
|
||||
std::list<bdPeer> store;
|
||||
int mIndex;
|
||||
bdDhtFunctions *mFns;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
602
libbitdht/src/bitdht/bencode.c
Normal file
602
libbitdht/src/bitdht/bencode.c
Normal file
@ -0,0 +1,602 @@
|
||||
/*
|
||||
* C implementation of a bencode decoder.
|
||||
* This is the format defined by BitTorrent:
|
||||
* http://wiki.theory.org/BitTorrentSpecification#bencoding
|
||||
*
|
||||
* The only external requirements are a few [standard] function calls and
|
||||
* the long long type. Any sane system should provide all of these things.
|
||||
*
|
||||
* See the bencode.h header file for usage information.
|
||||
*
|
||||
* This is released into the public domain.
|
||||
* Written by Mike Frysinger <vapier@gmail.com>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This implementation isn't optimized at all as I wrote it to support
|
||||
* a bogus system. I have no real interest in this format. Feel free
|
||||
* to send me patches (so long as you don't copyright them and you release
|
||||
* your changes into the public domain as well).
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> /* malloc() realloc() free() strtoll() */
|
||||
#include <string.h> /* memset() */
|
||||
|
||||
#include "bitdht/bencode.h"
|
||||
|
||||
/***
|
||||
* #define BE_DEBUG_DECODE 1
|
||||
* #define BE_DEBUG 1 // controlled from Makefile too.
|
||||
***/
|
||||
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
#include <stdio.h> /* debug */
|
||||
#endif
|
||||
|
||||
static be_node *be_alloc(be_type type)
|
||||
{
|
||||
be_node *ret = (be_node *) malloc(sizeof(*ret));
|
||||
if (ret) {
|
||||
memset(ret, 0x00, sizeof(*ret));
|
||||
ret->type = type;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static long long _be_decode_int(const char **data, long long *data_len)
|
||||
{
|
||||
char *endp;
|
||||
long long ret = strtoll(*data, &endp, 10);
|
||||
*data_len -= (endp - *data);
|
||||
*data = endp;
|
||||
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode_int(pnt: %p, rem: %lld) = %lld\n", *data, *data_len, ret);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
long long be_str_len(be_node *node)
|
||||
{
|
||||
long long ret = 0;
|
||||
if (node->val.s)
|
||||
memcpy(&ret, node->val.s - sizeof(ret), sizeof(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static char *_be_decode_str(const char **data, long long *data_len)
|
||||
{
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode_str(pnt: %p, rem: %lld)\n", *data, *data_len);
|
||||
#endif
|
||||
long long sllen = _be_decode_int(data, data_len);
|
||||
long slen = sllen;
|
||||
unsigned long len;
|
||||
char *ret = NULL;
|
||||
|
||||
/* slen is signed, so negative values get rejected */
|
||||
if (sllen < 0)
|
||||
{
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode_str() reject bad length\n");
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* reject attempts to allocate large values that overflow the
|
||||
* size_t type which is used with malloc()
|
||||
*/
|
||||
if (sizeof(long long) != sizeof(long))
|
||||
if (sllen != slen)
|
||||
{
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode_str() reject large_values\n");
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* make sure we have enough data left */
|
||||
if (sllen > *data_len - 1)
|
||||
{
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode_str() reject large_values\n");
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* switch from signed to unsigned so we don't overflow below */
|
||||
len = slen;
|
||||
|
||||
if (**data == ':') {
|
||||
char *_ret = (char *) malloc(sizeof(sllen) + len + 1);
|
||||
memcpy(_ret, &sllen, sizeof(sllen));
|
||||
ret = _ret + sizeof(sllen);
|
||||
memcpy(ret, *data + 1, len);
|
||||
ret[len] = '\0';
|
||||
*data += len + 1;
|
||||
*data_len -= len + 1;
|
||||
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode_str() read %ld bytes\n", len+1);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode_str() reject missing :\n");
|
||||
#endif
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static be_node *_be_decode(const char **data, long long *data_len)
|
||||
{
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode(pnt: %p, rem: %lld)\n", *data, *data_len);
|
||||
#endif
|
||||
be_node *ret = NULL;
|
||||
|
||||
if (!*data_len)
|
||||
{
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode() reject invalid datalen\n");
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
switch (**data) {
|
||||
/* lists */
|
||||
case 'l': {
|
||||
unsigned int i = 0;
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode() found list\n");
|
||||
#endif
|
||||
|
||||
ret = be_alloc(BE_LIST);
|
||||
|
||||
--(*data_len);
|
||||
++(*data);
|
||||
while (**data != 'e') {
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode() list get item (%d)\n", i);
|
||||
#endif
|
||||
ret->val.l = (be_node **) realloc(ret->val.l, (i + 2) * sizeof(*ret->val.l));
|
||||
ret->val.l[i] = _be_decode(data, data_len);
|
||||
if (ret->val.l[i] == NULL)
|
||||
{
|
||||
/* failed decode - kill decode */
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode() failed list decode - kill\n");
|
||||
#endif
|
||||
be_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
--(*data_len);
|
||||
++(*data);
|
||||
|
||||
/* empty list case. */
|
||||
if (i == 0)
|
||||
{
|
||||
ret->val.l = (be_node **) realloc(ret->val.l, 1 * sizeof(*ret->val.l));
|
||||
}
|
||||
|
||||
ret->val.l[i] = NULL;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* dictionaries */
|
||||
case 'd': {
|
||||
unsigned int i = 0;
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode() found dictionary\n");
|
||||
#endif
|
||||
|
||||
ret = be_alloc(BE_DICT);
|
||||
|
||||
--(*data_len);
|
||||
++(*data);
|
||||
while (**data != 'e') {
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode() dictionary get key (%d)\n", i);
|
||||
#endif
|
||||
ret->val.d = (be_dict *) realloc(ret->val.d, (i + 2) * sizeof(*ret->val.d));
|
||||
ret->val.d[i].key = _be_decode_str(data, data_len);
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode() dictionary get val\n");
|
||||
#endif
|
||||
ret->val.d[i].val = _be_decode(data, data_len);
|
||||
|
||||
if ((ret->val.d[i].key == NULL) || (ret->val.d[i].val == NULL))
|
||||
{
|
||||
/* failed decode - kill decode */
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode() failed dict decode - kill\n");
|
||||
#endif
|
||||
be_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
--(*data_len);
|
||||
++(*data);
|
||||
|
||||
/* empty dictionary case. */
|
||||
if (i == 0)
|
||||
{
|
||||
ret->val.d = (be_dict *) realloc(ret->val.d, 1 * sizeof(*ret->val.d));
|
||||
}
|
||||
|
||||
|
||||
ret->val.d[i].val = NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* integers */
|
||||
case 'i': {
|
||||
ret = be_alloc(BE_INT);
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode() found int\n");
|
||||
#endif
|
||||
|
||||
--(*data_len);
|
||||
++(*data);
|
||||
ret->val.i = _be_decode_int(data, data_len);
|
||||
if (**data != 'e')
|
||||
{
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode() reject data != e - kill\n");
|
||||
#endif
|
||||
be_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
--(*data_len);
|
||||
++(*data);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* byte strings */
|
||||
case '0'...'9': {
|
||||
ret = be_alloc(BE_STR);
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode() found string\n");
|
||||
#endif
|
||||
|
||||
ret->val.s = _be_decode_str(data, data_len);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* invalid */
|
||||
default:
|
||||
#ifdef BE_DEBUG_DECODE
|
||||
fprintf(stderr, "bencode::_be_decode() found invalid - kill\n");
|
||||
#endif
|
||||
return NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
be_node *be_decoden(const char *data, long long len)
|
||||
{
|
||||
return _be_decode(&data, &len);
|
||||
}
|
||||
|
||||
be_node *be_decode(const char *data)
|
||||
{
|
||||
return be_decoden(data, strlen(data));
|
||||
}
|
||||
|
||||
static inline void _be_free_str(char *str)
|
||||
{
|
||||
if (str)
|
||||
free(str - sizeof(long long));
|
||||
}
|
||||
void be_free(be_node *node)
|
||||
{
|
||||
switch (node->type) {
|
||||
case BE_STR:
|
||||
_be_free_str(node->val.s);
|
||||
break;
|
||||
|
||||
case BE_INT:
|
||||
break;
|
||||
|
||||
case BE_LIST: {
|
||||
unsigned int i;
|
||||
for (i = 0; node->val.l[i]; ++i)
|
||||
be_free(node->val.l[i]);
|
||||
free(node->val.l);
|
||||
break;
|
||||
}
|
||||
|
||||
case BE_DICT: {
|
||||
unsigned int i;
|
||||
for (i = 0; node->val.d[i].val; ++i) {
|
||||
_be_free_str(node->val.d[i].key);
|
||||
be_free(node->val.d[i].val);
|
||||
}
|
||||
free(node->val.d);
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(node);
|
||||
}
|
||||
|
||||
#ifdef BE_DEBUG
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static void _be_dump_indent(ssize_t indent)
|
||||
{
|
||||
while (indent-- > 0)
|
||||
printf(" ");
|
||||
}
|
||||
static void _be_dump(be_node *node, ssize_t indent)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
_be_dump_indent(indent);
|
||||
indent = abs(indent);
|
||||
|
||||
switch (node->type) {
|
||||
case BE_STR:
|
||||
be_dump_str(node);
|
||||
//printf("str = %s (len = %lli)\n", node->val.s, be_str_len(node));
|
||||
break;
|
||||
|
||||
case BE_INT:
|
||||
printf("int = %lli\n", node->val.i);
|
||||
break;
|
||||
|
||||
case BE_LIST:
|
||||
puts("list [");
|
||||
|
||||
for (i = 0; node->val.l[i]; ++i)
|
||||
_be_dump(node->val.l[i], indent + 1);
|
||||
|
||||
_be_dump_indent(indent);
|
||||
puts("]");
|
||||
break;
|
||||
|
||||
case BE_DICT:
|
||||
puts("dict {");
|
||||
|
||||
for (i = 0; node->val.d[i].val; ++i) {
|
||||
_be_dump_indent(indent + 1);
|
||||
printf("%s => ", node->val.d[i].key);
|
||||
_be_dump(node->val.d[i].val, -(indent + 1));
|
||||
}
|
||||
|
||||
_be_dump_indent(indent);
|
||||
puts("}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
void be_dump(be_node *node)
|
||||
{
|
||||
_be_dump(node, 0);
|
||||
}
|
||||
|
||||
void be_dump_str(be_node *node)
|
||||
{
|
||||
if (node->type != BE_STR)
|
||||
{
|
||||
printf("be_dump_str(): error not a string\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int len = be_str_len(node);
|
||||
int i = 0;
|
||||
printf("str[%d] = ", len);
|
||||
for(i = 0; i < len; i++)
|
||||
{
|
||||
/* sensible chars */
|
||||
if ((node->val.s[i] > 31) && (node->val.s[i] < 127))
|
||||
{
|
||||
printf("%c", node->val.s[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[%d]", node->val.s[i]);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
/******************** New Functions added by drBob *************
|
||||
* Output bencode
|
||||
*
|
||||
*/
|
||||
|
||||
int be_encode(be_node *node, char *str, int len)
|
||||
{
|
||||
size_t i;
|
||||
int loc = 0;
|
||||
|
||||
switch (node->type) {
|
||||
case BE_STR:
|
||||
snprintf(str, len, "%lli:", be_str_len(node));
|
||||
loc += strlen(&(str[loc]));
|
||||
|
||||
memcpy(&(str[loc]), node->val.s, be_str_len(node));
|
||||
loc += be_str_len(node);
|
||||
break;
|
||||
|
||||
case BE_INT:
|
||||
snprintf(str, len, "i%llie", node->val.i);
|
||||
loc += strlen(&(str[loc]));
|
||||
break;
|
||||
|
||||
case BE_LIST:
|
||||
|
||||
snprintf(str, len, "l");
|
||||
loc += 1;
|
||||
|
||||
for (i = 0; node->val.l[i]; ++i)
|
||||
{
|
||||
loc += be_encode(node->val.l[i], &(str[loc]), len-loc);
|
||||
}
|
||||
|
||||
snprintf(&(str[loc]), len - loc, "e");
|
||||
loc += 1;
|
||||
|
||||
break;
|
||||
|
||||
case BE_DICT:
|
||||
snprintf(str, len, "d");
|
||||
loc += 1;
|
||||
|
||||
for (i = 0; node->val.d[i].val; ++i) {
|
||||
|
||||
/* assumption that key must be ascii! */
|
||||
snprintf(&(str[loc]), len-loc, "%i:%s",
|
||||
(int) strlen(node->val.d[i].key),
|
||||
node->val.d[i].key);
|
||||
loc += strlen(&(str[loc]));
|
||||
loc += be_encode(node->val.d[i].val, &(str[loc]), len-loc);
|
||||
}
|
||||
|
||||
snprintf(&(str[loc]), len - loc, "e");
|
||||
loc += 1;
|
||||
|
||||
break;
|
||||
}
|
||||
return loc;
|
||||
}
|
||||
|
||||
/* hackish way to create nodes! */
|
||||
be_node *be_create_dict()
|
||||
{
|
||||
be_node *n = be_decode("de");
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
be_node *be_create_list()
|
||||
{
|
||||
be_node *n = be_decode("le");
|
||||
return n;
|
||||
}
|
||||
|
||||
be_node *be_create_str(const char *str)
|
||||
{
|
||||
|
||||
/* must */
|
||||
be_node *n = be_alloc(BE_STR);
|
||||
int len = strlen(str);
|
||||
long long int sllen = len;
|
||||
char *_ret = (char *) malloc(sizeof(sllen) + len + 1);
|
||||
char *ret = NULL;
|
||||
|
||||
memcpy(_ret, &sllen, sizeof(sllen));
|
||||
ret = _ret + sizeof(sllen);
|
||||
memcpy(ret, str, len);
|
||||
ret[len] = '\0';
|
||||
|
||||
n->val.s = ret;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
be_node *be_create_str_wlen(const char *str, int len) /* not including \0 */
|
||||
{
|
||||
|
||||
/* must */
|
||||
be_node *n = be_alloc(BE_STR);
|
||||
long long int sllen = len;
|
||||
char *_ret = (char *) malloc(sizeof(sllen) + len + 1);
|
||||
char *ret = NULL;
|
||||
|
||||
memcpy(_ret, &sllen, sizeof(sllen));
|
||||
ret = _ret + sizeof(sllen);
|
||||
memcpy(ret, str, len);
|
||||
ret[len] = '\0';
|
||||
|
||||
n->val.s = ret;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
be_node *be_create_int(long long int num)
|
||||
{
|
||||
/* must */
|
||||
be_node *n = be_alloc(BE_INT);
|
||||
n->val.i = num;
|
||||
return n;
|
||||
}
|
||||
|
||||
int be_add_keypair(be_node *dict, const char *str, be_node *node)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
/* only if dict type */
|
||||
if (dict->type != BE_DICT)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// get to end of dict.
|
||||
for(i = 0; dict->val.d[i].val; i++);
|
||||
|
||||
//fprintf(stderr, "be_add_keypair() i = %d\n",i);
|
||||
|
||||
/* realloc space */
|
||||
dict->val.d = (be_dict *) realloc(dict->val.d, (i + 2) * sizeof(*dict->val.d));
|
||||
|
||||
/* stupid key storage system */
|
||||
int len = strlen(str);
|
||||
long long int sllen = len;
|
||||
char *_ret = (char *) malloc(sizeof(sllen) + len + 1);
|
||||
char *ret = NULL;
|
||||
|
||||
//fprintf(stderr, "be_add_keypair() key len = %d\n",len);
|
||||
|
||||
memcpy(_ret, &sllen, sizeof(sllen));
|
||||
ret = _ret + sizeof(sllen);
|
||||
memcpy(ret, str, len);
|
||||
ret[len] = '\0';
|
||||
|
||||
dict->val.d[i].key = ret;
|
||||
dict->val.d[i].val = node;
|
||||
i++;
|
||||
dict->val.d[i].val = NULL;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int be_add_list(be_node *list, be_node *node)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
/* only if dict type */
|
||||
if (list->type != BE_LIST)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// get to end of dict.
|
||||
for(i = 0; list->val.l[i]; i++);
|
||||
|
||||
/* realloc space */
|
||||
list->val.l = (be_node **) realloc(list->val.l, (i + 2) * sizeof(*list->val.l));
|
||||
list->val.l[i] = node;
|
||||
++i;
|
||||
list->val.l[i] = NULL;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
82
libbitdht/src/bitdht/bencode.h
Normal file
82
libbitdht/src/bitdht/bencode.h
Normal file
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* C implementation of a bencode decoder.
|
||||
* This is the format defined by BitTorrent:
|
||||
* http://wiki.theory.org/BitTorrentSpecification#bencoding
|
||||
*
|
||||
* The only external requirements are a few [standard] function calls and
|
||||
* the long long type. Any sane system should provide all of these things.
|
||||
*
|
||||
* This is released into the public domain.
|
||||
* Written by Mike Frysinger <vapier@gmail.com>.
|
||||
*/
|
||||
|
||||
/* USAGE:
|
||||
* - pass the string full of the bencoded data to be_decode()
|
||||
* - parse the resulting tree however you like
|
||||
* - call be_free() on the tree to release resources
|
||||
*/
|
||||
|
||||
#ifndef _BENCODE_H
|
||||
#define _BENCODE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
BE_STR,
|
||||
BE_INT,
|
||||
BE_LIST,
|
||||
BE_DICT,
|
||||
} be_type;
|
||||
|
||||
struct be_dict;
|
||||
struct be_node;
|
||||
|
||||
/*
|
||||
* XXX: the "val" field of be_dict and be_node can be confusing ...
|
||||
*/
|
||||
|
||||
typedef struct be_dict {
|
||||
char *key;
|
||||
struct be_node *val;
|
||||
} be_dict;
|
||||
|
||||
typedef struct be_node {
|
||||
be_type type;
|
||||
union {
|
||||
char *s;
|
||||
long long i;
|
||||
struct be_node **l;
|
||||
struct be_dict *d;
|
||||
} val;
|
||||
} be_node;
|
||||
|
||||
extern long long be_str_len(be_node *node);
|
||||
// This function uses strlen, so is unreliable.
|
||||
//extern be_node *be_decode(const char *bencode);
|
||||
extern be_node *be_decoden(const char *bencode, long long bencode_len);
|
||||
extern void be_free(be_node *node);
|
||||
extern void be_dump(be_node *node);
|
||||
extern void be_dump_str(be_node *node);
|
||||
|
||||
// New Functions for the other half of the work - encoding */
|
||||
|
||||
extern int be_encode(be_node *node, char *str, int len);
|
||||
|
||||
|
||||
// Creating the data structure.
|
||||
extern int be_add_list(be_node *list, be_node *node);
|
||||
extern int be_add_keypair(be_node *dict, const char *str, be_node *node);
|
||||
extern be_node *be_create_int(long long int num);
|
||||
extern be_node *be_create_list();
|
||||
extern be_node *be_create_str(const char *str);
|
||||
extern be_node *be_create_str_wlen(const char *str, int len); /* not including \0 */
|
||||
extern be_node *be_create_dict();
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
16
libbitdht/src/example/Makefile
Normal file
16
libbitdht/src/example/Makefile
Normal file
@ -0,0 +1,16 @@
|
||||
|
||||
CXXFLAGS = -Wall -g -I..
|
||||
#CXXFLAGS += -arch i386 # OSX
|
||||
|
||||
LIBS = -L../lib -lbitdht
|
||||
|
||||
|
||||
|
||||
EXEC : bdexample
|
||||
|
||||
EGOBJ = bdhandler.o bdexample.o
|
||||
|
||||
bdexample: $(EGOBJ)
|
||||
$(CXX) $(CXXFLAGS) -o bdexample $(EGOBJ) $(LIBS)
|
||||
|
||||
|
500
libbitdht/src/example/bdboot.txt
Normal file
500
libbitdht/src/example/bdboot.txt
Normal file
@ -0,0 +1,500 @@
|
||||
64.121.80.63 45642
|
||||
84.82.225.139 52002
|
||||
200.104.218.244 24447
|
||||
116.81.147.106 10014
|
||||
76.238.231.136 37770
|
||||
68.148.109.134 32815
|
||||
78.43.60.243 18973
|
||||
62.33.209.243 20600
|
||||
84.40.115.5 49669
|
||||
93.11.108.82 45078
|
||||
90.157.139.222 28487
|
||||
79.83.184.114 40139
|
||||
109.104.187.16 30000
|
||||
87.101.162.223 21364
|
||||
99.251.104.111 60382
|
||||
130.63.133.216 47451
|
||||
64.216.32.37 6881
|
||||
75.137.149.242 19860
|
||||
208.102.25.182 50675
|
||||
75.72.144.15 54485
|
||||
68.148.20.202 22025
|
||||
69.148.177.153 26961
|
||||
121.222.5.240 51932
|
||||
88.161.156.9 48251
|
||||
88.200.104.216 27784
|
||||
109.160.52.2 36630
|
||||
74.12.101.58 61864
|
||||
174.71.75.121 48749
|
||||
186.66.255.5 37580
|
||||
24.116.239.194 59147
|
||||
95.65.178.164 23715
|
||||
220.70.175.83 43726
|
||||
182.169.110.245 64684
|
||||
119.247.164.154 22132
|
||||
77.202.227.150 15621
|
||||
79.129.217.171 17248
|
||||
98.117.197.195 53478
|
||||
71.72.3.201 40665
|
||||
72.88.206.217 57170
|
||||
95.29.8.61 59559
|
||||
142.163.161.23 60957
|
||||
70.27.27.105 23047
|
||||
90.42.104.194 10595
|
||||
114.36.7.29 26051
|
||||
78.26.164.132 59437
|
||||
70.126.197.41 14207
|
||||
187.14.3.249 8753
|
||||
62.166.34.78 62133
|
||||
82.45.213.218 24740
|
||||
213.123.71.145 56050
|
||||
93.12.188.2 22070
|
||||
203.184.0.161 10295
|
||||
69.156.209.139 19165
|
||||
83.81.119.121 34439
|
||||
109.208.211.98 36278
|
||||
84.3.208.199 50001
|
||||
85.87.98.187 53911
|
||||
93.127.107.157 35691
|
||||
188.195.145.172 21827
|
||||
184.56.151.190 11951
|
||||
98.168.190.247 61279
|
||||
83.237.221.242 16613
|
||||
75.30.79.175 57107
|
||||
90.24.61.26 57262
|
||||
125.27.132.48 10018
|
||||
94.98.122.161 62266
|
||||
81.25.50.209 44962
|
||||
94.24.137.191 35691
|
||||
74.119.191.181 62498
|
||||
46.33.237.37 21135
|
||||
183.83.30.78 15822
|
||||
109.75.207.23 14085
|
||||
92.147.163.123 36706
|
||||
110.3.132.18 10149
|
||||
90.38.163.86 61080
|
||||
77.50.87.114 11399
|
||||
76.106.47.230 6881
|
||||
24.47.46.185 59822
|
||||
118.150.145.87 16553
|
||||
98.121.178.252 31353
|
||||
95.62.64.40 12174
|
||||
85.91.17.78 8644
|
||||
98.148.37.251 55774
|
||||
122.117.82.108 16293
|
||||
200.26.143.117 14013
|
||||
187.37.80.22 25095
|
||||
95.19.224.225 25446
|
||||
79.132.4.131 36094
|
||||
109.86.136.248 44281
|
||||
85.54.187.59 28890
|
||||
99.225.92.6 38080
|
||||
178.40.89.75 10018
|
||||
189.237.151.135 17643
|
||||
189.114.233.119 12308
|
||||
93.13.211.51 59788
|
||||
80.56.69.253 25685
|
||||
83.131.224.235 62622
|
||||
201.229.9.69 28159
|
||||
201.223.65.4 43804
|
||||
77.167.196.115 64788
|
||||
71.72.158.196 37961
|
||||
86.132.129.242 23071
|
||||
93.103.167.242 35298
|
||||
212.37.169.128 31631
|
||||
74.105.104.186 23922
|
||||
173.176.243.27 31393
|
||||
86.13.110.77 47020
|
||||
76.181.24.116 35553
|
||||
94.158.107.234 32516
|
||||
99.242.143.80 19034
|
||||
201.209.54.201 26726
|
||||
109.87.52.11 20186
|
||||
98.121.161.117 54068
|
||||
75.69.6.91 6881
|
||||
98.223.44.215 35998
|
||||
123.193.220.215 54157
|
||||
95.130.12.23 61546
|
||||
84.215.125.229 51413
|
||||
77.203.179.13 23145
|
||||
217.172.179.78 18692
|
||||
85.170.52.161 12310
|
||||
98.244.158.38 40311
|
||||
71.201.200.183 41435
|
||||
99.111.202.61 10341
|
||||
84.252.27.201 17253
|
||||
210.6.173.193 6887
|
||||
74.67.142.125 17874
|
||||
94.23.212.70 57174
|
||||
77.99.18.29 63852
|
||||
91.100.81.198 49999
|
||||
82.74.117.94 25000
|
||||
78.83.127.162 53345
|
||||
95.84.255.201 17122
|
||||
85.228.78.9 47500
|
||||
217.77.60.189 58847
|
||||
67.174.74.95 35000
|
||||
95.105.198.233 80
|
||||
85.238.105.154 40000
|
||||
70.114.173.8 26375
|
||||
71.100.182.81 21724
|
||||
24.9.187.250 29985
|
||||
72.204.16.239 50200
|
||||
78.94.104.125 63580
|
||||
67.83.132.220 51515
|
||||
96.52.145.135 12607
|
||||
85.187.24.45 14111
|
||||
91.148.128.36 24464
|
||||
24.186.53.139 61726
|
||||
109.110.3.135 57756
|
||||
78.159.108.124 63982
|
||||
82.244.72.113 10620
|
||||
24.16.216.224 45546
|
||||
24.60.118.120 60005
|
||||
89.135.149.47 52055
|
||||
88.191.117.198 46881
|
||||
79.108.19.172 52750
|
||||
84.24.37.138 15678
|
||||
213.93.83.121 51413
|
||||
87.50.207.134 60361
|
||||
77.120.182.144 30585
|
||||
80.109.42.12 29168
|
||||
188.230.38.94 32525
|
||||
109.86.246.11 11400
|
||||
93.72.108.237 39272
|
||||
109.160.201.32 27495
|
||||
90.214.93.249 25238
|
||||
2.106.159.182 28860
|
||||
24.49.43.222 26917
|
||||
67.215.242.139 6881
|
||||
83.83.52.92 10000
|
||||
89.161.85.250 27553
|
||||
76.20.230.30 58335
|
||||
60.62.38.145 22101
|
||||
62.148.159.55 52603
|
||||
69.123.52.81 64701
|
||||
67.215.242.138 6881
|
||||
99.225.194.114 45586
|
||||
79.124.27.117 26472
|
||||
86.10.209.139 12001
|
||||
95.181.30.110 37075
|
||||
65.5.225.206 6881
|
||||
66.110.140.217 6881
|
||||
99.112.96.188 35979
|
||||
87.120.113.236 18301
|
||||
67.162.118.20 9820
|
||||
173.51.87.191 37950
|
||||
58.69.218.34 38072
|
||||
87.120.78.253 25118
|
||||
69.137.192.214 6881
|
||||
81.57.50.221 11095
|
||||
89.212.93.62 32590
|
||||
93.152.176.62 6881
|
||||
96.250.206.111 20965
|
||||
93.72.132.238 47860
|
||||
96.20.161.32 44893
|
||||
24.235.41.231 11181
|
||||
88.80.49.238 35857
|
||||
89.215.103.145 16242
|
||||
142.162.65.171 30537
|
||||
178.162.184.203 6821
|
||||
93.182.0.249 32217
|
||||
217.208.59.201 46227
|
||||
79.132.182.136 30129
|
||||
77.70.96.3 11949
|
||||
174.59.144.37 5895
|
||||
24.206.168.152 22102
|
||||
24.64.233.94 17962
|
||||
109.168.162.23 32195
|
||||
71.53.119.62 23108
|
||||
60.241.156.191 24064
|
||||
18.242.7.242 65435
|
||||
216.113.97.150 29801
|
||||
89.133.173.35 37855
|
||||
78.82.124.105 20047
|
||||
78.128.57.15 22034
|
||||
99.54.50.211 64189
|
||||
174.118.36.27 60010
|
||||
189.120.102.167 63495
|
||||
187.1.109.123 20546
|
||||
123.193.205.49 22248
|
||||
87.48.15.175 52767
|
||||
195.145.124.15 33639
|
||||
189.34.175.133 1142
|
||||
188.16.249.8 10428
|
||||
78.96.153.123 10943
|
||||
209.188.19.242 61204
|
||||
94.208.99.166 13733
|
||||
77.70.32.188 9459
|
||||
69.134.71.56 10713
|
||||
74.213.69.68 17477
|
||||
175.137.140.31 18987
|
||||
219.79.191.108 35982
|
||||
86.129.7.137 21672
|
||||
78.225.25.249 10323
|
||||
216.195.251.137 41238
|
||||
201.6.19.10 64067
|
||||
76.101.164.51 20673
|
||||
196.221.130.175 18290
|
||||
80.192.63.126 59768
|
||||
80.177.109.9 34474
|
||||
77.243.211.17 40330
|
||||
99.179.5.68 55337
|
||||
206.63.116.47 47092
|
||||
2.83.53.144 14926
|
||||
188.25.174.202 26218
|
||||
86.28.149.67 39274
|
||||
92.133.136.242 41243
|
||||
95.28.8.196 39167
|
||||
213.231.136.129 13219
|
||||
98.237.145.195 46637
|
||||
58.165.71.127 25006
|
||||
111.252.45.153 25943
|
||||
89.29.15.90 56736
|
||||
86.171.44.134 57592
|
||||
94.59.26.121 36725
|
||||
81.191.88.223 54799
|
||||
75.81.195.23 22056
|
||||
81.109.38.132 31233
|
||||
109.121.220.16 10301
|
||||
212.92.246.213 22544
|
||||
95.158.10.130 13941
|
||||
112.118.166.151 45684
|
||||
202.154.59.140 43442
|
||||
93.181.215.55 46157
|
||||
76.217.106.151 62033
|
||||
61.227.125.97 13874
|
||||
78.58.201.174 28778
|
||||
81.231.229.41 19130
|
||||
186.10.23.230 37769
|
||||
83.254.77.75 17232
|
||||
84.110.59.219 62065
|
||||
81.98.22.127 47800
|
||||
85.17.5.132 51413
|
||||
83.55.120.23 42521
|
||||
195.18.28.147 35691
|
||||
61.202.78.247 22221
|
||||
93.80.153.76 48878
|
||||
95.29.196.251 61629
|
||||
94.75.148.194 24796
|
||||
109.194.167.19 47193
|
||||
207.216.26.166 36202
|
||||
76.186.158.107 56213
|
||||
79.118.112.122 26181
|
||||
93.115.48.2 16132
|
||||
84.32.80.26 6881
|
||||
72.198.44.72 31231
|
||||
70.162.57.125 48027
|
||||
216.162.201.192 17384
|
||||
77.49.95.176 45619
|
||||
77.47.185.248 15245
|
||||
80.185.186.95 10318
|
||||
82.200.130.249 58389
|
||||
67.162.140.62 29974
|
||||
118.168.172.177 27161
|
||||
114.51.38.178 23793
|
||||
125.175.146.71 17014
|
||||
80.66.213.182 64219
|
||||
77.50.234.5 25866
|
||||
78.90.218.100 49844
|
||||
46.118.135.250 58667
|
||||
92.80.58.143 14569
|
||||
99.253.186.136 43003
|
||||
71.60.40.75 41590
|
||||
151.11.145.100 18163
|
||||
123.237.49.221 17321
|
||||
201.130.198.7 56378
|
||||
64.219.119.229 53546
|
||||
121.118.219.211 6880
|
||||
114.32.171.238 6881
|
||||
110.33.112.22 7336
|
||||
85.58.8.57 59138
|
||||
77.70.70.8 19445
|
||||
178.140.72.96 33178
|
||||
78.166.69.33 40531
|
||||
211.24.149.162 39719
|
||||
62.16.162.73 50001
|
||||
95.84.244.177 33076
|
||||
46.118.71.243 57944
|
||||
91.139.194.92 24902
|
||||
77.248.205.101 58849
|
||||
83.152.236.85 46613
|
||||
41.236.189.5 11493
|
||||
94.26.63.5 29973
|
||||
189.100.145.169 45441
|
||||
84.202.146.45 32122
|
||||
183.179.108.42 15833
|
||||
78.247.112.51 48465
|
||||
95.102.42.122 6881
|
||||
89.185.199.69 49879
|
||||
74.129.254.238 21830
|
||||
99.181.161.222 27636
|
||||
96.235.14.144 40928
|
||||
189.71.180.71 18172
|
||||
96.54.129.2 26085
|
||||
222.141.53.49 16006
|
||||
121.184.217.130 21563
|
||||
84.3.111.252 51111
|
||||
79.103.174.69 40190
|
||||
89.139.35.26 45625
|
||||
62.215.144.191 33078
|
||||
75.27.19.0 42707
|
||||
83.192.127.87 10785
|
||||
99.174.215.236 51128
|
||||
78.36.180.6 33016
|
||||
92.101.222.111 42441
|
||||
85.91.17.59 14091
|
||||
24.14.120.77 11682
|
||||
188.26.55.161 13650
|
||||
77.37.133.138 63572
|
||||
117.199.5.229 28835
|
||||
66.231.126.16 53509
|
||||
151.68.147.67 38146
|
||||
86.28.168.196 52549
|
||||
95.208.206.96 24833
|
||||
99.36.177.82 57416
|
||||
82.19.43.141 50274
|
||||
88.243.226.34 31952
|
||||
89.117.4.177 9026
|
||||
92.62.81.2 11369
|
||||
124.103.179.186 15356
|
||||
62.182.72.252 56899
|
||||
195.174.137.55 10152
|
||||
220.77.252.33 64170
|
||||
84.28.164.197 38804
|
||||
92.247.230.190 56889
|
||||
77.255.188.154 50182
|
||||
188.52.90.206 62502
|
||||
58.99.68.183 16001
|
||||
81.166.131.74 49856
|
||||
84.72.160.67 21783
|
||||
61.58.164.40 61726
|
||||
121.3.76.229 24859
|
||||
119.188.12.17 16003
|
||||
109.194.148.235 35691
|
||||
89.152.22.150 54895
|
||||
79.169.181.30 15368
|
||||
71.229.7.56 63792
|
||||
85.152.185.205 37976
|
||||
72.179.54.146 62040
|
||||
188.168.19.251 33178
|
||||
62.42.136.67 14957
|
||||
95.104.224.245 55165
|
||||
68.148.68.46 61984
|
||||
122.3.9.39 62546
|
||||
121.222.50.109 33067
|
||||
212.7.27.71 59005
|
||||
41.233.123.170 57369
|
||||
85.69.170.120 44386
|
||||
83.209.26.106 57404
|
||||
79.114.213.111 41823
|
||||
72.139.120.43 64593
|
||||
220.235.102.85 40329
|
||||
189.114.35.172 9884
|
||||
213.89.205.22 51413
|
||||
206.45.53.201 6881
|
||||
95.93.140.179 58457
|
||||
86.212.236.62 41939
|
||||
64.120.217.48 6821
|
||||
24.192.141.196 43444
|
||||
98.111.205.18 38107
|
||||
72.70.248.25 50824
|
||||
93.80.95.102 6882
|
||||
78.161.215.48 1024
|
||||
92.46.27.57 32582
|
||||
109.185.14.134 10020
|
||||
76.10.184.99 48754
|
||||
59.129.24.10 12345
|
||||
79.167.72.16 24971
|
||||
76.69.58.83 33258
|
||||
212.75.11.116 18431
|
||||
91.111.73.116 60521
|
||||
188.177.32.124 25573
|
||||
70.119.124.175 32302
|
||||
93.141.74.98 25058
|
||||
24.113.236.144 28024
|
||||
76.22.33.148 47355
|
||||
84.38.176.176 59787
|
||||
90.3.48.40 52585
|
||||
89.72.89.197 19610
|
||||
204.191.41.127 23927
|
||||
221.217.45.24 47709
|
||||
74.137.200.28 6112
|
||||
84.1.255.253 39168
|
||||
90.230.207.166 64697
|
||||
109.195.179.25 49034
|
||||
213.7.173.150 27444
|
||||
113.190.35.133 26298
|
||||
219.71.14.70 20000
|
||||
188.26.238.55 12082
|
||||
188.126.90.189 42521
|
||||
92.152.222.207 60086
|
||||
94.41.126.183 35691
|
||||
92.124.18.175 7691
|
||||
121.84.53.235 36421
|
||||
114.181.232.144 14859
|
||||
87.126.166.37 23145
|
||||
79.136.38.51 61193
|
||||
79.136.29.76 64123
|
||||
91.203.188.89 31780
|
||||
173.70.56.200 34107
|
||||
69.112.65.152 26394
|
||||
122.168.6.130 36327
|
||||
124.24.226.162 8640
|
||||
178.164.171.162 38624
|
||||
92.241.245.68 47765
|
||||
99.99.66.212 23188
|
||||
211.38.161.183 41794
|
||||
95.153.168.28 8033
|
||||
114.77.55.185 14890
|
||||
114.41.140.117 11675
|
||||
66.41.250.122 26560
|
||||
66.131.96.98 9090
|
||||
95.42.148.127 21982
|
||||
217.172.183.163 6881
|
||||
78.56.249.219 11699
|
||||
82.209.126.232 49180
|
||||
74.129.176.196 18957
|
||||
119.246.185.40 25295
|
||||
87.207.245.56 14132
|
||||
86.205.109.156 9739
|
||||
92.239.247.56 39166
|
||||
24.9.173.223 61423
|
||||
80.54.16.78 14221
|
||||
173.58.23.148 56221
|
||||
174.93.222.175 58944
|
||||
83.188.251.214 37857
|
||||
96.242.15.74 31666
|
||||
71.56.126.209 50316
|
||||
178.46.224.6 61397
|
||||
203.121.129.35 48913
|
||||
78.107.255.16 6884
|
||||
178.30.150.171 49763
|
||||
97.118.36.234 28186
|
||||
58.179.125.192 62734
|
||||
92.28.78.203 14274
|
||||
80.219.210.106 56365
|
||||
89.38.197.172 27077
|
||||
201.226.156.70 16668
|
||||
183.191.171.160 4041
|
||||
95.233.192.118 52630
|
||||
94.253.81.217 52858
|
||||
189.98.226.168 54960
|
||||
93.103.210.87 35228
|
||||
109.86.255.184 6881
|
||||
200.120.232.126 42476
|
||||
80.56.123.123 15541
|
||||
81.234.102.107 18764
|
||||
89.253.109.172 31844
|
||||
122.166.49.234 16842
|
||||
188.27.102.215 33333
|
||||
212.233.236.87 1041
|
||||
79.165.176.206 39053
|
||||
109.171.12.85 51441
|
||||
178.94.111.98 59615
|
||||
94.112.39.248 7289
|
||||
125.134.85.142 21707
|
||||
46.33.245.141 45822
|
||||
217.23.176.216 58252
|
||||
154.5.172.67 23127
|
||||
126.12.49.142 23120
|
42
libbitdht/src/example/bdexample.cc
Normal file
42
libbitdht/src/example/bdexample.cc
Normal file
@ -0,0 +1,42 @@
|
||||
|
||||
|
||||
#include "bitdht/bdiface.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
#include "bdhandler.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
/* startup dht : with a random id! */
|
||||
bdNodeId ownId;
|
||||
bdStdRandomNodeId(&ownId);
|
||||
|
||||
uint16_t port = 6775;
|
||||
std::string appId = "exId";
|
||||
std::string bootstrapfile = "bdboot.txt";
|
||||
|
||||
BitDhtHandler dht(&ownId, port, appId, bootstrapfile);
|
||||
|
||||
/* run your program */
|
||||
while(1)
|
||||
{
|
||||
bdNodeId searchId;
|
||||
bdStdRandomNodeId(&searchId);
|
||||
|
||||
dht.FindNode(&searchId);
|
||||
|
||||
sleep(180);
|
||||
|
||||
dht.DropNode(&searchId);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
251
libbitdht/src/example/bdhandler.cc
Normal file
251
libbitdht/src/example/bdhandler.cc
Normal file
@ -0,0 +1,251 @@
|
||||
/*
|
||||
* libretroshare/src/dht: bdhandler.h
|
||||
*
|
||||
* BitDht example interface
|
||||
*
|
||||
* Copyright 2009-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 2 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/udpstack.h>
|
||||
#include <udp/udpbitdht.h>
|
||||
#include <bitdht/bdstddht.h>
|
||||
|
||||
#include "bdhandler.h"
|
||||
|
||||
/* This is a conversion callback class
|
||||
*/
|
||||
|
||||
class BdCallback: public BitDhtCallback
|
||||
{
|
||||
public:
|
||||
|
||||
BdCallback(BitDhtHandler *parent)
|
||||
:mParent(parent) { return; }
|
||||
|
||||
virtual int dhtNodeCallback(const bdId *id, uint32_t peerflags)
|
||||
{
|
||||
return mParent->NodeCallback(id, peerflags);
|
||||
}
|
||||
|
||||
virtual int dhtPeerCallback(const bdNodeId *id, uint32_t status)
|
||||
{
|
||||
return mParent->PeerCallback(id, status);
|
||||
}
|
||||
|
||||
virtual int dhtValueCallback(const bdNodeId *id, std::string key, uint32_t status)
|
||||
{
|
||||
return mParent->ValueCallback(id, key, status);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
BitDhtHandler *mParent;
|
||||
};
|
||||
|
||||
|
||||
BitDhtHandler::BitDhtHandler(bdNodeId *ownId, uint16_t port, std::string appId, std::string bootstrapfile)
|
||||
{
|
||||
std::cerr << "BitDhtHandler::BitDhtHandler()" << std::endl;
|
||||
std::cerr << "Using Id: ";
|
||||
bdStdPrintNodeId(std::cerr, ownId);
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "Using Bootstrap File: " << bootstrapfile;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "Converting OwnId to bdNodeId....";
|
||||
std::cerr << std::endl;
|
||||
|
||||
/* standard dht behaviour */
|
||||
bdDhtFunctions *stdfns = new bdStdDht();
|
||||
|
||||
std::cerr << "BitDhtHandler() startup ... creating UdpBitDht";
|
||||
std::cerr << std::endl;
|
||||
|
||||
/* create dht */
|
||||
struct sockaddr_in local;
|
||||
memset(&local, 0, sizeof(local));
|
||||
local.sin_family = AF_INET;
|
||||
local.sin_addr.s_addr = 0;
|
||||
local.sin_port = htons(port);
|
||||
|
||||
mStack = new UdpStack(local);
|
||||
|
||||
mUdpBitDht = new UdpBitDht(mStack, ownId, appId, bootstrapfile, stdfns);
|
||||
mStack->addReceiver(mUdpBitDht);
|
||||
|
||||
/* setup callback to here */
|
||||
BdCallback *bdcb = new BdCallback(this);
|
||||
mUdpBitDht->addCallback(bdcb);
|
||||
|
||||
std::cerr << "BitDhtHandler() starting threads and dht";
|
||||
std::cerr << std::endl;
|
||||
|
||||
mUdpBitDht->start(); /* starts up the bitdht thread */
|
||||
|
||||
/* switch on the dht too */
|
||||
mUdpBitDht->startDht();
|
||||
}
|
||||
|
||||
/* pqiNetAssist - external interface functions */
|
||||
void BitDhtHandler::enable(bool on)
|
||||
{
|
||||
std::cerr << "p3BitDht::enable(" << on << ")";
|
||||
std::cerr << std::endl;
|
||||
if (on)
|
||||
{
|
||||
mUdpBitDht->startDht();
|
||||
}
|
||||
else
|
||||
{
|
||||
mUdpBitDht->stopDht();
|
||||
}
|
||||
}
|
||||
|
||||
void BitDhtHandler::shutdown() /* blocking call */
|
||||
{
|
||||
mUdpBitDht->stopDht();
|
||||
}
|
||||
|
||||
|
||||
void BitDhtHandler::restart()
|
||||
{
|
||||
mUdpBitDht->stopDht();
|
||||
mUdpBitDht->startDht();
|
||||
}
|
||||
|
||||
bool BitDhtHandler::getEnabled()
|
||||
{
|
||||
return (mUdpBitDht->stateDht() != 0);
|
||||
}
|
||||
|
||||
bool BitDhtHandler::getActive()
|
||||
{
|
||||
return (mUdpBitDht->stateDht() >= BITDHT_MGR_STATE_ACTIVE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* pqiNetAssistConnect - external interface functions */
|
||||
/* add / remove peers */
|
||||
bool BitDhtHandler::FindNode(bdNodeId *peerId)
|
||||
{
|
||||
std::cerr << "BitDhtHandler::FindNode(";
|
||||
bdStdPrintNodeId(std::cerr, peerId);
|
||||
std::cerr << ")" << std::endl;
|
||||
|
||||
/* add in peer */
|
||||
mUdpBitDht->addFindNode(peerId, BITDHT_QFLAGS_DO_IDLE);
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
bool BitDhtHandler::DropNode(bdNodeId *peerId)
|
||||
{
|
||||
std::cerr << "BitDhtHandler::DropNode(";
|
||||
bdStdPrintNodeId(std::cerr, peerId);
|
||||
std::cerr << ")" << std::endl;
|
||||
std::cerr << std::endl;
|
||||
|
||||
/* remove in peer */
|
||||
mUdpBitDht->removeFindNode(peerId);
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/********************** Callback Functions **************************/
|
||||
|
||||
int BitDhtHandler::NodeCallback(const bdId *id, uint32_t peerflags)
|
||||
{
|
||||
#ifdef DEBUG_BITDHT
|
||||
std::cerr << "BitDhtHandler::NodeCallback()";
|
||||
bdStdPrintNodeId(std::cerr, &(id->id));
|
||||
std::cerr << " flags: " << peerflags;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BitDhtHandler::PeerCallback(const bdNodeId *id, uint32_t status)
|
||||
{
|
||||
std::cerr << "BitDhtHandler::PeerCallback() NodeId: ";
|
||||
bdStdPrintNodeId(std::cerr, id);
|
||||
std::cerr << std::endl;
|
||||
|
||||
bool connect = false;
|
||||
switch(status)
|
||||
{
|
||||
case BITDHT_MGR_QUERY_FAILURE:
|
||||
/* do nothing */
|
||||
std::cerr << "BitDhtHandler::PeerCallback() QUERY FAILURE ... do nothin ";
|
||||
std::cerr << std::endl;
|
||||
|
||||
break;
|
||||
|
||||
case BITDHT_MGR_QUERY_PEER_OFFLINE:
|
||||
/* do nothing */
|
||||
|
||||
std::cerr << "BitDhtHandler::PeerCallback() QUERY PEER OFFLINE ... do nothin ";
|
||||
std::cerr << std::endl;
|
||||
|
||||
break;
|
||||
|
||||
case BITDHT_MGR_QUERY_PEER_UNREACHABLE:
|
||||
/* do nothing */
|
||||
|
||||
std::cerr << "BitDhtHandler::PeerCallback() QUERY PEER UNREACHABLE ... flag? / do nothin ";
|
||||
std::cerr << std::endl;
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case BITDHT_MGR_QUERY_PEER_ONLINE:
|
||||
/* do something */
|
||||
|
||||
std::cerr << "BitDhtHandler::PeerCallback() QUERY PEER ONLINE ... try udp connection";
|
||||
std::cerr << std::endl;
|
||||
|
||||
connect = true;
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int BitDhtHandler::ValueCallback(const bdNodeId *id, std::string key, uint32_t status)
|
||||
{
|
||||
std::cerr << "BitDhtHandler::ValueCallback() NOOP for NOW";
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "BitDhtHandler::ValueCallback()";
|
||||
bdStdPrintNodeId(std::cerr, id);
|
||||
std::cerr << " key: " << key;
|
||||
std::cerr << " status: " << status;
|
||||
std::cerr << std::endl;
|
||||
|
||||
/* ignore for now */
|
||||
return 0;
|
||||
}
|
||||
|
71
libbitdht/src/example/bdhandler.h
Normal file
71
libbitdht/src/example/bdhandler.h
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* libbitdht/src/example/bdhandler.h
|
||||
*
|
||||
* BitDht interface example
|
||||
*
|
||||
* Copyright 2009-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 2 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".
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef BITDHT_HANDLER_H
|
||||
#define BITDHT_HANDLER_H
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include "udp/udpstack.h"
|
||||
#include "udp/udpbitdht.h"
|
||||
#include "bitdht/bdiface.h"
|
||||
|
||||
/*** This class can be overloaded to use the XXXXCallback() Functions *****/
|
||||
class BitDhtIntCallback;
|
||||
|
||||
class BitDhtHandler
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
BitDhtHandler(bdNodeId *ownId, uint16_t port, std::string appId, std::string bootstrapfile);
|
||||
|
||||
void enable(bool on);
|
||||
void shutdown(); /* blocking call */
|
||||
void restart();
|
||||
|
||||
bool getEnabled();
|
||||
bool getActive();
|
||||
|
||||
bool FindNode(bdNodeId *peerId);
|
||||
bool DropNode(bdNodeId *peerId);
|
||||
|
||||
virtual int NodeCallback(const bdId *id, uint32_t peerflags);
|
||||
virtual int PeerCallback(const bdNodeId *id, uint32_t status);
|
||||
virtual int ValueCallback(const bdNodeId *id, std::string key, uint32_t status);
|
||||
|
||||
private:
|
||||
|
||||
/* real DHT classes */
|
||||
UdpStack *mStack;
|
||||
UdpBitDht *mUdpBitDht;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif /* BITDHT_HANDLER_H */
|
||||
|
137
libbitdht/src/libbitdht.pro
Normal file
137
libbitdht/src/libbitdht.pro
Normal file
@ -0,0 +1,137 @@
|
||||
TEMPLATE = lib
|
||||
CONFIG += staticlib
|
||||
CONFIG -= qt
|
||||
TARGET = bitdht
|
||||
QMAKE_CXXFLAGS *= -Wall -DBE_DEBUG
|
||||
|
||||
profiling {
|
||||
QMAKE_CXXFLAGS -= -fomit-frame-pointer
|
||||
QMAKE_CXXFLAGS *= -pg -g -fno-omit-frame-pointer
|
||||
}
|
||||
|
||||
release {
|
||||
# not much here yet.
|
||||
}
|
||||
|
||||
#CONFIG += debug
|
||||
debug {
|
||||
QMAKE_CXXFLAGS -= -O2 -fomit-frame-pointer
|
||||
QMAKE_CXXFLAGS *= -g -fno-omit-frame-pointer
|
||||
}
|
||||
|
||||
################################# Linux ##########################################
|
||||
linux-* {
|
||||
DESTDIR = lib
|
||||
QMAKE_CC = g++
|
||||
}
|
||||
|
||||
linux-g++ {
|
||||
OBJECTS_DIR = temp/linux-g++/obj
|
||||
}
|
||||
|
||||
linux-g++-64 {
|
||||
OBJECTS_DIR = temp/linux-g++-64/obj
|
||||
}
|
||||
|
||||
#################### Cross compilation for windows under Linux ####################
|
||||
|
||||
win32-x-g++ {
|
||||
OBJECTS_DIR = temp/win32xgcc/obj
|
||||
DESTDIR = lib.win32xgcc
|
||||
# These have been replaced by _WIN32 && __MINGW32__
|
||||
# DEFINES *= WINDOWS_SYS WIN32 WIN_CROSS_UBUNTU
|
||||
QMAKE_CXXFLAGS *= -Wmissing-include-dirs
|
||||
QMAKE_CC = i586-mingw32msvc-g++
|
||||
QMAKE_LIB = i586-mingw32msvc-ar
|
||||
QMAKE_AR = i586-mingw32msvc-ar
|
||||
DEFINES *= STATICLIB WIN32
|
||||
|
||||
INCLUDEPATH *= /usr/i586-mingw32msvc/include ${HOME}/.wine/drive_c/pthreads/include/
|
||||
}
|
||||
################################# Windows ##########################################
|
||||
|
||||
win32 {
|
||||
QMAKE_CC = g++
|
||||
OBJECTS_DIR = temp/obj
|
||||
MOC_DIR = temp/moc
|
||||
DEFINES *= STATICLIB
|
||||
# These have been replaced by _WIN32 && __MINGW32__
|
||||
#DEFINES *= WINDOWS_SYS WIN32 STATICLIB MINGW
|
||||
DESTDIR = lib
|
||||
|
||||
# Switch off optimization for release version
|
||||
QMAKE_CXXFLAGS_RELEASE -= -O2
|
||||
QMAKE_CXXFLAGS_RELEASE += -O0
|
||||
QMAKE_CFLAGS_RELEASE -= -O2
|
||||
QMAKE_CFLAGS_RELEASE += -O0
|
||||
|
||||
# Switch on optimization for debug version
|
||||
#QMAKE_CXXFLAGS_DEBUG += -O2
|
||||
#QMAKE_CFLAGS_DEBUG += -O2
|
||||
}
|
||||
|
||||
################################# MacOSX ##########################################
|
||||
|
||||
mac {
|
||||
QMAKE_CC = g++
|
||||
OBJECTS_DIR = temp/obj
|
||||
MOC_DIR = temp/moc
|
||||
DESTDIR = lib
|
||||
}
|
||||
|
||||
################################### COMMON stuff ##################################
|
||||
################################### COMMON stuff ##################################
|
||||
|
||||
#DEPENDPATH += . \
|
||||
INCLUDEPATH += . \
|
||||
|
||||
HEADERS += \
|
||||
bitdht/bdiface.h \
|
||||
bitdht/bencode.h \
|
||||
bitdht/bdobj.h \
|
||||
bitdht/bdmsgs.h \
|
||||
bitdht/bdpeer.h \
|
||||
bitdht/bdquery.h \
|
||||
bitdht/bdhash.h \
|
||||
bitdht/bdstore.h \
|
||||
bitdht/bdnode.h \
|
||||
bitdht/bdmanager.h \
|
||||
bitdht/bdstddht.h \
|
||||
bitdht/bdhistory.h \
|
||||
util/bdnet.h \
|
||||
util/bdthreads.h \
|
||||
util/bdrandom.h \
|
||||
udp/udplayer.h \
|
||||
udp/udpstack.h \
|
||||
udp/udpbitdht.h \
|
||||
bitdht/bdconnection.h \
|
||||
bitdht/bdfilter.h \
|
||||
bitdht/bdaccount.h \
|
||||
bitdht/bdquerymgr.h \
|
||||
util/bdbloom.h \
|
||||
|
||||
SOURCES += \
|
||||
bitdht/bencode.c \
|
||||
bitdht/bdobj.cc \
|
||||
bitdht/bdmsgs.cc \
|
||||
bitdht/bdpeer.cc \
|
||||
bitdht/bdquery.cc \
|
||||
bitdht/bdhash.cc \
|
||||
bitdht/bdstore.cc \
|
||||
bitdht/bdnode.cc \
|
||||
bitdht/bdmanager.cc \
|
||||
bitdht/bdstddht.cc \
|
||||
bitdht/bdhistory.cc \
|
||||
util/bdnet.cc \
|
||||
util/bdthreads.cc \
|
||||
util/bdrandom.cc \
|
||||
udp/udplayer.cc \
|
||||
udp/udpstack.cc \
|
||||
udp/udpbitdht.cc \
|
||||
bitdht/bdconnection.cc \
|
||||
bitdht/bdfilter.cc \
|
||||
bitdht/bdaccount.cc \
|
||||
bitdht/bdquerymgr.cc \
|
||||
util/bdbloom.cc \
|
||||
|
||||
|
13
libbitdht/src/mingw32make.bat
Normal file
13
libbitdht/src/mingw32make.bat
Normal file
@ -0,0 +1,13 @@
|
||||
set QTDIR=d:\qt\2010.01
|
||||
set MINGW=%QTDIR%\mingw
|
||||
|
||||
set PATH=%QTDIR%\qt\bin;%QTDIR%\bin;%MINGW%\bin;%PATH%
|
||||
|
||||
mingw32-make clean
|
||||
|
||||
qmake libbitdht.pro
|
||||
|
||||
mingw32-make
|
||||
|
||||
pause
|
||||
|
91
libbitdht/src/tests/Makefile
Normal file
91
libbitdht/src/tests/Makefile
Normal file
@ -0,0 +1,91 @@
|
||||
|
||||
LIB_TOP_DIR = ..
|
||||
TEST_TOP_DIR = $(LIB_TOP_DIR)/tests
|
||||
|
||||
##### Define any flags that are needed for this section #######
|
||||
###############################################################
|
||||
|
||||
###############################################################
|
||||
include $(TEST_TOP_DIR)/scripts/config.mk
|
||||
###############################################################
|
||||
|
||||
# Generic Test Harnesses.
|
||||
TESTOBJ = bdmetric_test.o bdmsgs_test.o bdnode_test.o bdspace_test.o
|
||||
TESTOBJ += bdmgr_multitest.o bdquery_test.o bdstore_test.o
|
||||
TESTOBJ += bdmidids_test.o bdspace_test2.o udpbitdht_nettest.o
|
||||
TESTOBJ += bdbloom_test.o bdbloom_makefilter.o
|
||||
#TESTOBJ += bencode_test.o bdudp_test.o
|
||||
#TESTOBJ = bdnode_test.o bdnode_multitest1.o bdnode_test2.o
|
||||
|
||||
TESTS = bdmetric_test bdmsgs_test bdspace_test
|
||||
TESTS += bdmgr_multitest bdquery_test bdstore_test
|
||||
TESTS += bdmidids_test bdspace_test2 udpbitdht_nettest
|
||||
TESTS += bdbloom_test
|
||||
#TESTS += bencode_test bdudp_test
|
||||
#Tests to Fix.
|
||||
#TESTS = bdnode_test bdnode_multitest1 bdnode_test2
|
||||
|
||||
MANUAL_TESTS =
|
||||
|
||||
all: tests $(MANUAL_TESTS)
|
||||
|
||||
bdmsgs_test: bdmsgs_test.o
|
||||
$(CC) $(CFLAGS) -o bdmsgs_test bdmsgs_test.o $(LIBS)
|
||||
|
||||
bdmetric_test: bdmetric_test.o
|
||||
$(CC) $(CFLAGS) -o bdmetric_test bdmetric_test.o $(LIBS)
|
||||
|
||||
bdquery_test: bdquery_test.o
|
||||
$(CC) $(CFLAGS) -o bdquery_test bdquery_test.o $(LIBS)
|
||||
|
||||
bdspace_test: bdspace_test.o
|
||||
$(CC) $(CFLAGS) -o bdspace_test bdspace_test.o $(LIBS)
|
||||
|
||||
bdspace_test2: bdspace_test2.o
|
||||
$(CC) $(CFLAGS) -o bdspace_test2 bdspace_test2.o $(LIBS)
|
||||
|
||||
bdnode_test: bdnode_test.o
|
||||
$(CC) $(CFLAGS) -o bdnode_test bdnode_test.o $(LIBS)
|
||||
|
||||
bdnode_test2: bdnode_test2.o
|
||||
$(CC) $(CFLAGS) -o bdnode_test2 bdnode_test2.o $(LIBS)
|
||||
|
||||
bdmidids_test: bdmidids_test.o
|
||||
$(CC) $(CFLAGS) -o bdmidids_test bdmidids_test.o $(LIBS)
|
||||
|
||||
|
||||
bdnode_multitest1: bdnode_multitest1.o
|
||||
$(CC) $(CFLAGS) -o bdnode_multitest1 bdnode_multitest1.o $(LIBS)
|
||||
|
||||
bdmgr_multitest: bdmgr_multitest.o
|
||||
$(CC) $(CFLAGS) -o bdmgr_multitest bdmgr_multitest.o $(LIBS)
|
||||
|
||||
|
||||
bdstore_test: bdstore_test.o
|
||||
$(CC) $(CFLAGS) -o bdstore_test bdstore_test.o $(LIBS)
|
||||
|
||||
bdudp_test: bdudp_test.o
|
||||
$(CC) $(CFLAGS) -o bdudp_test bdudp_test.o $(LIBS)
|
||||
|
||||
udpbitdht_nettest: udpbitdht_nettest.o
|
||||
$(CC) $(CFLAGS) -o udpbitdht_nettest udpbitdht_nettest.o $(LIBS)
|
||||
|
||||
bencode_test: bencode_test.o
|
||||
$(CC) $(CFLAGS) -o bencode_test bencode_test.o $(LIBS)
|
||||
|
||||
bdbloom_test: bdbloom_test.o
|
||||
$(CC) $(CFLAGS) -o bdbloom_test bdbloom_test.o $(LIBS)
|
||||
|
||||
bdbloom_makefilter: bdbloom_makefilter.o
|
||||
$(CC) $(CFLAGS) -o bdbloom_makefilter bdbloom_makefilter.o $(LIBS)
|
||||
|
||||
|
||||
clobber: remove_extra_files
|
||||
|
||||
remove_extra_files:
|
||||
-$(RM) $(MANUAL_TESTS)
|
||||
|
||||
###############################################################
|
||||
include $(TEST_TOP_DIR)/scripts/rules.mk
|
||||
###############################################################
|
||||
|
91
libbitdht/src/tests/bdbloom_makefilter.cc
Normal file
91
libbitdht/src/tests/bdbloom_makefilter.cc
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* bitdht/bdbloom_test.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 "util/bdbloom.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#define N_TESTS 100
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
/* read from the file */
|
||||
if (argc < 2)
|
||||
{
|
||||
std::cerr << "Missing Hash File";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
|
||||
FILE *fd = fopen(argv[1], "r");
|
||||
if (!fd)
|
||||
{
|
||||
std::cerr << "Failed to Open File: " << argv[1];
|
||||
std::cerr << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
char line[1000];
|
||||
|
||||
bdBloom filter;
|
||||
|
||||
int nHashes = 0;
|
||||
while(fgets(line, 1000-1, fd))
|
||||
{
|
||||
std::string hash = line;
|
||||
std::cerr << "Read Hash: " << hash;
|
||||
std::cerr << std::endl;
|
||||
|
||||
filter.add(hash);
|
||||
nHashes++;
|
||||
}
|
||||
fclose(fd);
|
||||
|
||||
filter.printFilter(std::cerr);
|
||||
|
||||
int bits = filter.countBits();
|
||||
int filterBits = filter.filterBits();
|
||||
std::cerr << "Filter Bits Set: " << bits;
|
||||
std::cerr << std::endl;
|
||||
|
||||
double possible = ((double) bits) * bits;
|
||||
double max = ((double) filterBits) * filterBits;
|
||||
std::cerr << "Therefore Possible Finds: " << possible << "/" << max << " = %" << 100 * possible / max;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "With Insertions: " << nHashes;
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << std::endl;
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "Filter String: " << filter.getFilter() << std::endl;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << std::endl;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
165
libbitdht/src/tests/bdbloom_test.cc
Normal file
165
libbitdht/src/tests/bdbloom_test.cc
Normal file
@ -0,0 +1,165 @@
|
||||
/*
|
||||
* bitdht/bdbloom_test.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 "util/bdbloom.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#define N_TESTS 100
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
/* Do this multiple times */
|
||||
int i;
|
||||
|
||||
|
||||
bdBloom filter;
|
||||
|
||||
std::list<bdNodeId> testIds;
|
||||
std::list<bdNodeId>::iterator it;
|
||||
|
||||
for(i = 0; i < N_TESTS; i++)
|
||||
{
|
||||
bdNodeId targetId;
|
||||
bdStdRandomNodeId(&targetId);
|
||||
testIds.push_back(targetId);
|
||||
}
|
||||
|
||||
std::cerr << "Test bdBloom Filter...." << std::endl;
|
||||
for(it = testIds.begin(); it != testIds.end(); it++)
|
||||
{
|
||||
bdNodeId targetId = *it;
|
||||
|
||||
std::cerr << "-------------------------------------------------" << std::endl;
|
||||
std::cerr << "Inserting : ";
|
||||
std::ostringstream str;
|
||||
bdStdPrintNodeId(str, &targetId);
|
||||
std::cerr << str.str();
|
||||
std::cerr << std::endl;
|
||||
|
||||
filter.add(str.str());
|
||||
|
||||
filter.printFilter(std::cerr);
|
||||
}
|
||||
|
||||
std::string fs1 = filter.getFilter();
|
||||
/* now extract, and reinsert filter */
|
||||
bdBloom filter2;
|
||||
filter2.setFilterBits(fs1);
|
||||
|
||||
std::string fs2 = filter2.getFilter();
|
||||
filter2.printFilter(std::cerr);
|
||||
|
||||
if (fs1 == fs2)
|
||||
{
|
||||
std::cerr << "SUCCESS: Filter Correctly Transferred!";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "FAILURE: Filter Not Transferred!";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
|
||||
|
||||
for(it = testIds.begin(); it != testIds.end(); it++)
|
||||
{
|
||||
bdNodeId targetId = *it;
|
||||
|
||||
std::cerr << "-------------------------------------------------" << std::endl;
|
||||
std::cerr << "Testing : ";
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::ostringstream str;
|
||||
bdStdPrintNodeId(str, &targetId);
|
||||
std::cerr << str.str() << std::endl;
|
||||
|
||||
if (filter2.test(str.str()))
|
||||
{
|
||||
std::cerr << "SUCCESS: Filter Found Entry";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "FAILURE: Filter Didn't Found Entry";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int bits = filter.countBits();
|
||||
int filterBits = filter.filterBits();
|
||||
std::cerr << "Filter Bits Set: " << bits;
|
||||
std::cerr << std::endl;
|
||||
|
||||
double possible = ((double) bits) * bits;
|
||||
double max = ((double) filterBits) * filterBits;
|
||||
std::cerr << "Therefore Possible Finds: " << possible << "/" << max << " = %" << 100 * possible / max;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "With Insertions: " << N_TESTS;
|
||||
std::cerr << std::endl;
|
||||
|
||||
#define FINAL_TESTS (1000000)
|
||||
int found = 0;
|
||||
int didnt = 0;
|
||||
for(i = 0; i < FINAL_TESTS; i++)
|
||||
{
|
||||
if ((i != 0) && (i % 100000 == 0))
|
||||
{
|
||||
std::cerr << "Run " << i << " Checks" << std::endl;
|
||||
}
|
||||
|
||||
bdNodeId targetId;
|
||||
bdStdRandomNodeId(&targetId);
|
||||
std::ostringstream str;
|
||||
bdStdPrintNodeId(str, &targetId);
|
||||
|
||||
if (filter2.test(str.str()))
|
||||
{
|
||||
found++;
|
||||
}
|
||||
else
|
||||
{
|
||||
didnt++;
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << "Final Stats: " << FINAL_TESTS << " random checks done";
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "\t" << found << " " << 100.0 * ((double) found) / FINAL_TESTS << "% found";
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "\t" << didnt << " " << 100.0 * ((double) didnt) / FINAL_TESTS << "% didnt";
|
||||
std::cerr << std::endl;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
500
libbitdht/src/tests/bdboot.txt
Normal file
500
libbitdht/src/tests/bdboot.txt
Normal file
@ -0,0 +1,500 @@
|
||||
93.113.253.235 10351
|
||||
109.195.26.63 35691
|
||||
78.84.189.238 16105
|
||||
93.149.194.159 10006
|
||||
142.165.155.66 54333
|
||||
87.2.252.141 32523
|
||||
86.28.119.162 65393
|
||||
75.141.203.120 9269
|
||||
61.219.92.157 26309
|
||||
113.197.131.184 39390
|
||||
61.202.78.247 22221
|
||||
99.192.87.181 63645
|
||||
186.14.24.166 53495
|
||||
90.191.162.101 56943
|
||||
75.9.79.58 49173
|
||||
189.79.164.180 51916
|
||||
188.2.188.200 15904
|
||||
216.137.107.5 42096
|
||||
119.82.197.117 14165
|
||||
93.142.184.229 30804
|
||||
81.230.155.227 49912
|
||||
78.3.22.204 12176
|
||||
70.53.218.176 48079
|
||||
99.138.101.252 45682
|
||||
109.96.227.190 17059
|
||||
71.72.158.196 37961
|
||||
92.80.210.120 10000
|
||||
91.146.132.224 12566
|
||||
81.182.99.86 58659
|
||||
83.217.46.30 59184
|
||||
118.173.96.14 15000
|
||||
41.107.83.159 10000
|
||||
80.189.136.230 51692
|
||||
212.142.83.32 48265
|
||||
194.220.84.244 22464
|
||||
79.219.206.130 57803
|
||||
94.179.29.137 9371
|
||||
86.132.129.242 23071
|
||||
92.101.129.144 59039
|
||||
113.77.164.93 31872
|
||||
114.149.135.219 7128
|
||||
178.90.5.139 14054
|
||||
85.67.83.55 18671
|
||||
77.121.45.122 26731
|
||||
84.202.51.66 52591
|
||||
80.9.93.215 16504
|
||||
83.82.128.46 62372
|
||||
178.167.21.234 6881
|
||||
82.243.15.108 64162
|
||||
89.241.183.88 11887
|
||||
187.1.109.123 20546
|
||||
76.104.157.104 13313
|
||||
121.190.21.7 23585
|
||||
111.184.60.209 8473
|
||||
175.137.141.17 4841
|
||||
114.47.124.155 16001
|
||||
190.9.80.109 60921
|
||||
78.53.157.19 61597
|
||||
82.22.73.207 39714
|
||||
94.227.124.236 51413
|
||||
93.86.121.17 23491
|
||||
78.8.127.204 49158
|
||||
173.21.246.1 28175
|
||||
74.105.104.186 23922
|
||||
93.106.78.26 62241
|
||||
95.52.220.221 46873
|
||||
46.41.99.164 41403
|
||||
90.3.189.132 27532
|
||||
95.24.191.34 37686
|
||||
94.158.206.37 47733
|
||||
24.192.141.196 43444
|
||||
88.114.60.226 14701
|
||||
190.233.28.216 10035
|
||||
187.59.104.184 38406
|
||||
88.241.54.177 6881
|
||||
75.169.41.44 23921
|
||||
212.231.219.123 30749
|
||||
95.111.59.146 31360
|
||||
117.20.185.249 34787
|
||||
184.15.43.192 25872
|
||||
41.236.143.104 18254
|
||||
212.37.169.128 31631
|
||||
93.103.167.242 35298
|
||||
95.208.34.13 28134
|
||||
81.184.29.241 60338
|
||||
173.176.243.27 31393
|
||||
200.30.215.247 53196
|
||||
95.27.138.236 32373
|
||||
78.181.51.1 56672
|
||||
69.113.118.17 43710
|
||||
118.15.159.47 14924
|
||||
219.78.37.4 17323
|
||||
79.179.35.221 8080
|
||||
41.233.232.153 28027
|
||||
2.40.81.5 39425
|
||||
76.181.24.116 35553
|
||||
183.37.193.84 21819
|
||||
188.2.108.62 62838
|
||||
188.2.182.88 12877
|
||||
93.150.26.240 19076
|
||||
78.60.165.96 46785
|
||||
99.242.143.80 19034
|
||||
190.166.108.145 57967
|
||||
90.15.142.10 20380
|
||||
212.139.216.168 28287
|
||||
79.173.18.85 16234
|
||||
109.122.6.119 35077
|
||||
95.70.76.28 39132
|
||||
178.206.42.99 35691
|
||||
189.110.158.105 52423
|
||||
112.195.15.4 1083
|
||||
219.79.127.226 20579
|
||||
194.102.155.85 30332
|
||||
94.158.107.234 32516
|
||||
144.85.198.132 55745
|
||||
220.132.140.86 16001
|
||||
92.14.78.83 15294
|
||||
92.147.31.42 14099
|
||||
86.13.110.77 47020
|
||||
95.8.170.249 34481
|
||||
178.166.138.75 33347
|
||||
117.195.196.154 53480
|
||||
189.159.45.177 27778
|
||||
99.228.167.174 18537
|
||||
60.34.32.239 64398
|
||||
71.103.193.176 50913
|
||||
213.156.101.150 47078
|
||||
95.72.233.199 13299
|
||||
70.70.161.4 37542
|
||||
89.252.11.169 39924
|
||||
94.21.150.86 20004
|
||||
90.206.166.236 30055
|
||||
92.45.198.23 56969
|
||||
71.190.128.27 8181
|
||||
201.209.54.201 26726
|
||||
201.254.175.141 1052
|
||||
86.22.154.1 34153
|
||||
202.212.230.225 62582
|
||||
217.210.126.55 6881
|
||||
92.242.118.12 59741
|
||||
195.18.28.147 35691
|
||||
77.105.61.152 10003
|
||||
90.157.174.67 64061
|
||||
109.184.242.18 19268
|
||||
97.95.66.89 37027
|
||||
109.212.41.127 46365
|
||||
204.182.12.51 56654
|
||||
186.205.50.126 54325
|
||||
119.247.252.118 13277
|
||||
118.20.246.94 7113
|
||||
116.3.57.205 19699
|
||||
189.101.77.13 52448
|
||||
89.178.80.137 40431
|
||||
75.69.6.91 6881
|
||||
98.121.161.117 54068
|
||||
69.153.56.74 13496
|
||||
78.62.118.16 47252
|
||||
79.46.255.126 26751
|
||||
122.169.170.101 10028
|
||||
123.193.220.215 54157
|
||||
218.218.128.104 49963
|
||||
219.106.122.171 55959
|
||||
112.210.235.218 27157
|
||||
90.60.172.96 6881
|
||||
92.17.64.55 10008
|
||||
98.223.44.215 35998
|
||||
190.28.95.226 41411
|
||||
186.206.12.110 9037
|
||||
90.218.16.149 40972
|
||||
109.87.52.11 20186
|
||||
77.101.143.17 37570
|
||||
82.34.77.180 41818
|
||||
94.41.21.214 36939
|
||||
85.127.34.173 10085
|
||||
83.4.7.35 19276
|
||||
91.200.254.202 12737
|
||||
87.121.66.246 12420
|
||||
81.198.26.183 39568
|
||||
92.132.93.148 23722
|
||||
91.77.94.227 14372
|
||||
206.45.53.201 6881
|
||||
74.95.222.53 60733
|
||||
72.19.62.187 21338
|
||||
71.201.200.183 41435
|
||||
24.98.172.105 61382
|
||||
78.92.152.60 12365
|
||||
93.87.252.82 21564
|
||||
61.228.64.67 38444
|
||||
219.79.74.97 43357
|
||||
118.173.119.134 10993
|
||||
123.254.183.208 36587
|
||||
99.111.202.61 10341
|
||||
94.194.87.1 31169
|
||||
93.23.174.188 39415
|
||||
78.70.152.38 30356
|
||||
94.251.17.152 35691
|
||||
67.175.189.15 16703
|
||||
109.242.224.50 45107
|
||||
86.196.29.237 28767
|
||||
84.252.27.201 17253
|
||||
70.162.57.125 48027
|
||||
210.6.173.193 6886
|
||||
210.6.173.193 6887
|
||||
110.132.207.153 29959
|
||||
94.25.171.229 40703
|
||||
178.53.139.9 13328
|
||||
85.170.52.161 12310
|
||||
85.17.5.132 51413
|
||||
95.130.12.23 61546
|
||||
87.106.18.80 21060
|
||||
85.69.170.120 44386
|
||||
77.203.179.13 23145
|
||||
86.125.150.25 59847
|
||||
82.130.251.32 54834
|
||||
83.139.173.200 56953
|
||||
87.11.51.210 8500
|
||||
85.70.116.131 19860
|
||||
98.244.158.38 40311
|
||||
98.143.96.103 21711
|
||||
84.215.125.229 51413
|
||||
217.172.179.78 18692
|
||||
74.67.142.125 17874
|
||||
98.195.197.177 32346
|
||||
207.216.26.166 36202
|
||||
76.29.22.154 37778
|
||||
114.220.155.188 7576
|
||||
109.8.31.209 16726
|
||||
71.100.182.81 21724
|
||||
58.85.55.238 14725
|
||||
67.174.74.95 35000
|
||||
99.194.168.253 29358
|
||||
85.228.78.9 47500
|
||||
71.187.90.32 50260
|
||||
216.113.97.150 29801
|
||||
85.49.133.208 27989
|
||||
94.208.99.166 13733
|
||||
78.83.127.162 53345
|
||||
91.121.174.148 55690
|
||||
94.3.214.214 56319
|
||||
94.23.212.70 57174
|
||||
85.217.136.179 47053
|
||||
78.94.104.125 63580
|
||||
95.105.198.233 80
|
||||
95.84.255.201 17122
|
||||
91.100.81.198 49999
|
||||
78.90.128.10 52518
|
||||
95.26.156.134 19486
|
||||
82.232.104.187 16807
|
||||
79.101.213.250 13121
|
||||
75.118.101.142 30141
|
||||
93.89.209.227 19280
|
||||
82.7.214.190 21876
|
||||
217.168.214.39 19766
|
||||
70.114.173.8 26375
|
||||
72.204.16.239 50200
|
||||
24.9.187.250 29985
|
||||
76.16.32.242 16278
|
||||
74.79.89.24 11697
|
||||
200.158.63.125 39637
|
||||
82.74.116.4 17223
|
||||
96.52.145.135 12607
|
||||
77.99.18.29 63852
|
||||
217.77.60.189 58847
|
||||
82.74.117.94 25000
|
||||
85.238.105.154 40000
|
||||
67.83.132.220 51515
|
||||
82.237.186.158 6881
|
||||
188.187.172.48 63699
|
||||
90.192.99.87 19903
|
||||
74.162.128.111 65432
|
||||
109.184.8.183 64619
|
||||
77.198.210.34 58320
|
||||
89.90.170.230 23442
|
||||
97.100.136.205 25942
|
||||
98.119.32.149 13434
|
||||
68.51.90.191 16878
|
||||
142.167.52.20 42904
|
||||
118.5.144.222 45043
|
||||
98.149.14.52 26659
|
||||
79.108.19.172 52750
|
||||
79.115.9.172 63866
|
||||
79.168.88.72 12883
|
||||
91.148.128.36 24464
|
||||
85.187.24.45 14111
|
||||
82.240.100.118 19906
|
||||
89.135.149.47 52055
|
||||
71.185.134.201 50771
|
||||
77.78.178.156 60852
|
||||
24.186.53.139 61726
|
||||
74.140.96.199 19420
|
||||
109.110.3.135 57756
|
||||
24.16.216.224 45546
|
||||
85.132.109.108 21838
|
||||
118.101.115.37 10395
|
||||
24.102.8.251 34774
|
||||
85.247.82.191 6882
|
||||
59.113.66.184 8473
|
||||
220.61.29.35 32776
|
||||
82.244.72.113 10620
|
||||
124.144.11.76 26144
|
||||
59.100.97.236 421
|
||||
115.166.24.156 21109
|
||||
188.36.71.13 45750
|
||||
95.37.35.103 6881
|
||||
78.159.108.124 63982
|
||||
81.166.43.192 48429
|
||||
85.222.55.113 51409
|
||||
62.80.183.225 26033
|
||||
84.85.23.152 56471
|
||||
86.165.244.171 6881
|
||||
90.157.165.36 1046
|
||||
84.61.147.98 6889
|
||||
81.154.201.146 45682
|
||||
24.60.118.120 60005
|
||||
24.61.141.82 60919
|
||||
2.106.159.182 28860
|
||||
78.57.176.166 33061
|
||||
87.1.182.97 48031
|
||||
84.3.249.76 11353
|
||||
87.120.64.46 25428
|
||||
88.181.152.48 44330
|
||||
109.160.201.32 27495
|
||||
188.230.38.94 32525
|
||||
90.214.93.249 25238
|
||||
84.249.175.45 34904
|
||||
114.45.181.114 61860
|
||||
69.176.179.196 53579
|
||||
220.210.178.105 37492
|
||||
213.93.83.121 51413
|
||||
88.191.117.198 46881
|
||||
212.233.241.25 20164
|
||||
96.38.226.139 47850
|
||||
77.120.182.144 30585
|
||||
82.228.54.32 32492
|
||||
92.138.66.102 17105
|
||||
95.158.179.229 13684
|
||||
77.70.20.90 17610
|
||||
87.203.114.107 39136
|
||||
85.113.48.19 6881
|
||||
174.51.65.149 46912
|
||||
67.215.242.139 6881
|
||||
99.111.240.83 54548
|
||||
24.54.1.238 60738
|
||||
65.13.127.70 56958
|
||||
83.83.52.92 10000
|
||||
60.62.38.145 22101
|
||||
61.22.93.23 35048
|
||||
58.152.160.161 51954
|
||||
121.7.94.125 23766
|
||||
89.161.85.250 27553
|
||||
84.24.37.138 15678
|
||||
80.109.42.12 29168
|
||||
87.50.207.134 60361
|
||||
93.72.108.237 39272
|
||||
91.117.209.44 17063
|
||||
109.86.246.11 11400
|
||||
24.49.43.222 26917
|
||||
64.121.80.63 45642
|
||||
76.20.230.30 58335
|
||||
86.10.209.139 12001
|
||||
67.215.242.138 6881
|
||||
87.120.78.253 25118
|
||||
84.82.225.139 52002
|
||||
200.104.218.244 24447
|
||||
116.81.147.106 10014
|
||||
76.238.231.136 37770
|
||||
68.148.109.134 32815
|
||||
78.43.60.243 18973
|
||||
62.33.209.243 20600
|
||||
84.40.115.5 49669
|
||||
93.11.108.82 45078
|
||||
90.157.139.222 28487
|
||||
79.124.27.117 26472
|
||||
99.225.194.114 45586
|
||||
79.83.184.114 40139
|
||||
109.104.187.16 30000
|
||||
87.101.162.223 21364
|
||||
99.251.104.111 60382
|
||||
130.63.133.216 47451
|
||||
66.110.140.217 6881
|
||||
87.120.113.236 18301
|
||||
64.216.32.37 6881
|
||||
75.137.149.242 19860
|
||||
69.137.192.214 6881
|
||||
208.102.25.182 50675
|
||||
173.51.87.191 37950
|
||||
75.72.144.15 54485
|
||||
68.148.20.202 22025
|
||||
99.112.96.188 35979
|
||||
67.162.118.20 9820
|
||||
69.148.177.153 26961
|
||||
58.69.218.34 38072
|
||||
121.222.5.240 51932
|
||||
88.161.156.9 48251
|
||||
88.200.104.216 27784
|
||||
62.148.159.55 52603
|
||||
109.160.52.2 36630
|
||||
69.123.52.81 64701
|
||||
95.181.30.110 37075
|
||||
65.5.225.206 6881
|
||||
74.12.101.58 61864
|
||||
174.71.75.121 48749
|
||||
186.66.255.5 37580
|
||||
24.116.239.194 59147
|
||||
95.65.178.164 23715
|
||||
220.70.175.83 43726
|
||||
182.169.110.245 64684
|
||||
122.166.49.234 16842
|
||||
119.247.164.154 22132
|
||||
77.202.227.150 15621
|
||||
79.129.217.171 17248
|
||||
88.80.49.238 35857
|
||||
96.250.206.111 20965
|
||||
98.117.197.195 53478
|
||||
81.57.50.221 11095
|
||||
71.72.3.201 40665
|
||||
72.88.206.217 57170
|
||||
95.29.8.61 59559
|
||||
24.235.41.231 11181
|
||||
142.163.161.23 60957
|
||||
70.27.27.105 23047
|
||||
90.42.104.194 10595
|
||||
114.36.7.29 26051
|
||||
78.26.164.132 59437
|
||||
70.126.197.41 14207
|
||||
142.162.65.171 30537
|
||||
187.14.3.249 8753
|
||||
62.166.34.78 62133
|
||||
82.45.213.218 24740
|
||||
213.123.71.145 56050
|
||||
93.12.188.2 22070
|
||||
203.184.0.161 10295
|
||||
93.152.176.62 6881
|
||||
69.156.209.139 19165
|
||||
96.20.161.32 44893
|
||||
83.81.119.121 34439
|
||||
109.208.211.98 36278
|
||||
154.5.172.67 23127
|
||||
93.72.132.238 47860
|
||||
89.215.103.145 16242
|
||||
89.212.93.62 32590
|
||||
84.3.208.199 50001
|
||||
85.87.98.187 53911
|
||||
93.127.107.157 35691
|
||||
188.195.145.172 21827
|
||||
184.56.151.190 11951
|
||||
98.168.190.247 61279
|
||||
83.237.221.242 16613
|
||||
75.30.79.175 57107
|
||||
90.24.61.26 57262
|
||||
125.27.132.48 10018
|
||||
94.98.122.161 62266
|
||||
178.162.184.203 6821
|
||||
93.182.0.249 32217
|
||||
81.25.50.209 44962
|
||||
94.24.137.191 35691
|
||||
78.82.124.105 20047
|
||||
71.53.119.62 23108
|
||||
74.119.191.181 62498
|
||||
46.33.237.37 21135
|
||||
183.83.30.78 15822
|
||||
109.75.207.23 14085
|
||||
92.147.163.123 36706
|
||||
110.3.132.18 10149
|
||||
90.38.163.86 61080
|
||||
89.133.173.35 37855
|
||||
77.50.87.114 11399
|
||||
76.106.47.230 6881
|
||||
24.206.168.152 22102
|
||||
24.47.46.185 59822
|
||||
60.241.156.191 24064
|
||||
118.150.145.87 16553
|
||||
98.121.178.252 31353
|
||||
24.64.233.94 17962
|
||||
95.62.64.40 12174
|
||||
85.91.17.78 8644
|
||||
98.148.37.251 55774
|
||||
122.117.82.108 16293
|
||||
200.26.143.117 14013
|
||||
187.37.80.22 25095
|
||||
95.19.224.225 25446
|
||||
79.132.4.131 36094
|
||||
217.208.59.201 46227
|
||||
109.86.136.248 44281
|
||||
85.54.187.59 28890
|
||||
99.225.92.6 38080
|
||||
178.40.89.75 10018
|
||||
79.132.182.136 30129
|
||||
174.59.144.37 5895
|
||||
189.237.151.135 17643
|
||||
189.114.233.119 12308
|
||||
109.168.162.23 32195
|
||||
93.13.211.51 59788
|
||||
80.56.69.253 25685
|
||||
77.70.96.3 11949
|
||||
18.242.7.242 65435
|
||||
83.131.224.235 62622
|
||||
201.229.9.69 28159
|
||||
201.223.65.4 43804
|
||||
77.167.196.115 64788
|
264
libbitdht/src/tests/bdmetric_test.cc
Normal file
264
libbitdht/src/tests/bdmetric_test.cc
Normal file
@ -0,0 +1,264 @@
|
||||
/*
|
||||
* bitdht/bdmetric_test.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 "bitdht/bdpeer.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
#include "utest.h"
|
||||
|
||||
bool test_metric_explicit();
|
||||
bool test_metric_random();
|
||||
|
||||
INITTEST();
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
std::cerr << "libbitdht: " << argv[0] << std::endl;
|
||||
|
||||
test_metric_explicit();
|
||||
|
||||
FINALREPORT("libbitdht: Metric Tests");
|
||||
return TESTRESULT();
|
||||
}
|
||||
|
||||
bool test_metric_explicit()
|
||||
{
|
||||
std::cerr << "test_metric_explicit:" << std::endl;
|
||||
|
||||
#define NUM_IDS 6
|
||||
|
||||
/* create some ids */
|
||||
bdId id[NUM_IDS];
|
||||
int i, j;
|
||||
|
||||
/* create a set of known ids ... and
|
||||
* check the metrics are what we expect.
|
||||
*/
|
||||
|
||||
for(i = 0; i < NUM_IDS; i++)
|
||||
{
|
||||
bdZeroNodeId(&(id[i].id));
|
||||
}
|
||||
|
||||
/* test the zero node works */
|
||||
for(i = 0; i < NUM_IDS; i++)
|
||||
{
|
||||
for(j = 0; j < BITDHT_KEY_LEN; j++)
|
||||
{
|
||||
CHECK(id[i].id.data[j] == 0);
|
||||
}
|
||||
}
|
||||
|
||||
for(i = 0; i < NUM_IDS; i++)
|
||||
{
|
||||
for(j = i; j < NUM_IDS; j++)
|
||||
{
|
||||
id[j].id.data[BITDHT_KEY_LEN - i - 1] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
for(i = 0; i < NUM_IDS; i++)
|
||||
{
|
||||
fprintf(stderr, "id[%d]:", i+1);
|
||||
bdStdPrintId(std::cerr,&(id[i]));
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
/* now do the sums */
|
||||
bdMetric met;
|
||||
bdMetric met2;
|
||||
int bdist = 0;
|
||||
|
||||
for(i = 0; i < 6; i++)
|
||||
{
|
||||
for(j = i+1; j < 6; j++)
|
||||
{
|
||||
bdStdDistance(&(id[i].id), &(id[j].id), &met);
|
||||
|
||||
fprintf(stderr, "%d^%d:", i, j);
|
||||
bdStdPrintNodeId(std::cerr,&met);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
bdist = bdStdBucketDistance(&met);
|
||||
fprintf(stderr, " bucket: %d\n", bdist);
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
int c1 = met < met2;
|
||||
int c2 = met2 < met;
|
||||
|
||||
fprintf(stderr, "1^2<1^3? : %d 1^3<1^2?: %d\n", c1, c2);
|
||||
#endif
|
||||
|
||||
|
||||
REPORT("Test Byte Manipulation");
|
||||
//FAILED("Couldn't Bind to socket");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool test_metric_random()
|
||||
{
|
||||
std::cerr << "test_metric_random:" << std::endl;
|
||||
|
||||
/* create some ids */
|
||||
bdId id1;
|
||||
bdId id2;
|
||||
bdId id3;
|
||||
bdId id4;
|
||||
bdId id5;
|
||||
bdId id6;
|
||||
|
||||
bdStdRandomId(&id1);
|
||||
bdStdRandomId(&id2);
|
||||
bdStdRandomId(&id3);
|
||||
bdStdRandomId(&id4);
|
||||
bdStdRandomId(&id5);
|
||||
bdStdRandomId(&id6);
|
||||
|
||||
fprintf(stderr, "id1:");
|
||||
bdStdPrintId(std::cerr,&id1);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
fprintf(stderr, "id2:");
|
||||
bdStdPrintId(std::cerr,&id2);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
fprintf(stderr, "id3:");
|
||||
bdStdPrintId(std::cerr,&id3);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
fprintf(stderr, "id4:");
|
||||
bdStdPrintId(std::cerr,&id4);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
fprintf(stderr, "id5:");
|
||||
bdStdPrintId(std::cerr,&id5);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
fprintf(stderr, "id6:");
|
||||
bdStdPrintId(std::cerr,&id6);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
/* now do the sums */
|
||||
bdMetric met;
|
||||
bdMetric met2;
|
||||
int bdist = 0;
|
||||
bdStdDistance(&(id1.id), &(id2.id), &met);
|
||||
|
||||
fprintf(stderr, "1^2:");
|
||||
bdStdPrintNodeId(std::cerr,&met);
|
||||
fprintf(stderr, "\n");
|
||||
bdist = bdStdBucketDistance(&met);
|
||||
fprintf(stderr, " bucket: %d\n", bdist);
|
||||
|
||||
bdStdDistance(&(id1.id), &(id3.id), &met2);
|
||||
bdist = bdStdBucketDistance(&met2);
|
||||
|
||||
fprintf(stderr, "1^3:");
|
||||
bdStdPrintNodeId(std::cerr,&met2);
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, " bucket: %d\n", bdist);
|
||||
|
||||
int c1 = met < met2;
|
||||
int c2 = met2 < met;
|
||||
|
||||
fprintf(stderr, "1^2<1^3? : %d 1^3<1^2?: %d\n", c1, c2);
|
||||
|
||||
|
||||
bdStdDistance(&(id1.id), &(id4.id), &met2);
|
||||
bdist = bdStdBucketDistance(&met2);
|
||||
|
||||
fprintf(stderr, "1^4:");
|
||||
bdStdPrintNodeId(std::cerr,&met2);
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, " bucket: %d\n", bdist);
|
||||
|
||||
c1 = met < met2;
|
||||
c2 = met2 < met;
|
||||
|
||||
fprintf(stderr, "1^2<1^4? : %d 1^4<1^2?: %d\n", c1, c2);
|
||||
|
||||
bdStdDistance(&(id1.id), &(id5.id), &met);
|
||||
bdist = bdStdBucketDistance(&met);
|
||||
|
||||
fprintf(stderr, "1^5:");
|
||||
bdStdPrintNodeId(std::cerr,&met);
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, " bucket: %d\n", bdist);
|
||||
|
||||
bdStdDistance(&(id1.id), &(id6.id), &met);
|
||||
bdist = bdStdBucketDistance(&met);
|
||||
|
||||
fprintf(stderr, "1^6:");
|
||||
bdStdPrintNodeId(std::cerr,&met);
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, " bucket: %d\n", bdist);
|
||||
|
||||
bdStdDistance(&(id2.id), &(id3.id), &met);
|
||||
bdist = bdStdBucketDistance(&met);
|
||||
|
||||
fprintf(stderr, "2^3:");
|
||||
bdStdPrintNodeId(std::cerr,&met);
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, " bucket: %d\n", bdist);
|
||||
|
||||
|
||||
fprintf(stderr, "id1:");
|
||||
bdStdPrintId(std::cerr,&id1);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
fprintf(stderr, "id2:");
|
||||
bdStdPrintId(std::cerr,&id2);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
fprintf(stderr, "id3:");
|
||||
bdStdPrintId(std::cerr,&id3);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
fprintf(stderr, "id4:");
|
||||
bdStdPrintId(std::cerr,&id4);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
fprintf(stderr, "id5:");
|
||||
bdStdPrintId(std::cerr,&id5);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
fprintf(stderr, "id6:");
|
||||
bdStdPrintId(std::cerr,&id6);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
186
libbitdht/src/tests/bdmgr_multitest.cc
Normal file
186
libbitdht/src/tests/bdmgr_multitest.cc
Normal file
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* bitdht/bdmgr_multitest.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 "bitdht/bdmanager.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
#include "udp/udplayer.h"
|
||||
#include "util/bdrandom.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/**********************************************************************************
|
||||
* tests of multi bdnodes all connected together.
|
||||
* in these cases, the networking step is shortcut and the ip addresses ignored.
|
||||
* instead the port number is used as an index to peers.
|
||||
*
|
||||
* test1()
|
||||
* Small cross seeding, and static list of peers.
|
||||
* Set it going - and see what happens.
|
||||
*/
|
||||
|
||||
std::map<bdId, bdNodeManager *> nodes;
|
||||
std::map<struct sockaddr_in, bdId> addrIdx;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
time_t sim_time = 600;
|
||||
time_t starttime = time(NULL);
|
||||
int n_nodes = 1000;
|
||||
std::map<bdId, bdNodeManager *>::iterator it;
|
||||
std::map<bdId, bdNodeManager *>::iterator nit;
|
||||
std::map<struct sockaddr_in, bdId>::iterator ait;
|
||||
|
||||
int i, j;
|
||||
|
||||
bdDhtFunctions *fns = new bdStdDht();
|
||||
|
||||
std::cerr << "bdmgr_multitest() Setting up Nodes" << std::endl;
|
||||
/* setup nodes */
|
||||
for(i = 0; i < n_nodes; i++)
|
||||
{
|
||||
bdId id;
|
||||
|
||||
bdStdRandomId(&id);
|
||||
|
||||
//id.addr.sin_port = htons(i);
|
||||
//((uint32_t *) (id.id.data))[0] = i * 16 * 16; /* force this so the sort order is maintained! */
|
||||
std::cerr << "bdmgr_multitest() Id: ";
|
||||
fns->bdPrintId(std::cerr, &id);
|
||||
std::cerr << std::endl;
|
||||
|
||||
bdNodeManager *mgr = new bdNodeManager(&(id.id), "bdTEST", "", fns);
|
||||
|
||||
/* Store in nodes */
|
||||
nodes[id] = mgr;
|
||||
/* Store in indices */
|
||||
addrIdx[id.addr] = id;
|
||||
|
||||
}
|
||||
|
||||
std::cerr << "bdmgr_multitest() Cross Seeding" << std::endl;
|
||||
/* do a little cross seeding */
|
||||
for(nit = nodes.begin(); nit != nodes.end(); nit++)
|
||||
{
|
||||
for(j = 0; j < 2; j++)
|
||||
{
|
||||
int peeridx = bdRandom::random_u32() % n_nodes;
|
||||
for(i = 0, it = nodes.begin();
|
||||
(i < peeridx) && (it != nodes.end()); i++, it++)
|
||||
{
|
||||
/* empty */
|
||||
}
|
||||
if (it != nodes.end())
|
||||
{
|
||||
nit->second->addPotentialPeer((bdId *) &(it->first), NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ready to run */
|
||||
|
||||
std::cerr << "bdmgr_multitest() Simulation Time....." << std::endl;
|
||||
i = 0;
|
||||
while(time(NULL) < starttime + sim_time)
|
||||
{
|
||||
i++;
|
||||
std::cerr << "bdmgr_multitest() Iteration: " << i << std::endl;
|
||||
|
||||
for(it = nodes.begin(), j = 0; it != nodes.end(); it++, j++)
|
||||
{
|
||||
/* extract messages to go -> and deliver */
|
||||
#define MAX_MSG_SIZE 10240
|
||||
struct sockaddr_in addr;
|
||||
char data[MAX_MSG_SIZE];
|
||||
int len = MAX_MSG_SIZE;
|
||||
|
||||
while(it->second->outgoingMsg(&addr, data, &len))
|
||||
{
|
||||
std::cerr << "bdmgr_multitest() Msg from Peer: " << j;
|
||||
|
||||
/* find the peer */
|
||||
ait = addrIdx.find(addr);
|
||||
nit = nodes.end();
|
||||
if (ait != addrIdx.end())
|
||||
{
|
||||
nit = nodes.find(ait->second);
|
||||
std::cerr << " For: ";
|
||||
fns->bdPrintId(std::cerr, &(nit->first));
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << " For Unknown Destination";
|
||||
std::cerr << std::endl;
|
||||
|
||||
}
|
||||
|
||||
if (nit != nodes.end())
|
||||
{
|
||||
/* set from address */
|
||||
nit->second->incomingMsg( (sockaddr_in *) &(it->first.addr), data, len);
|
||||
}
|
||||
/* reset message size */
|
||||
len = MAX_MSG_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
for(it = nodes.begin(), j = 0; it != nodes.end(); it++, j++)
|
||||
{
|
||||
/* tick */
|
||||
std::cerr << "bdmgr_multitest() Ticking peer: " << j << std::endl;
|
||||
it->second->iteration();
|
||||
}
|
||||
|
||||
|
||||
/* have a rest */
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
std::cerr << "bdmgr_multitest() Displying States"<< std::endl;
|
||||
for(it = nodes.begin(), j = 0; it != nodes.end(); it++, j++)
|
||||
{
|
||||
/* tick */
|
||||
std::cerr << "bdmgr_multitest() Peer State: " << j << std::endl;
|
||||
it->second->printState();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
96
libbitdht/src/tests/bdmidids_test.cc
Normal file
96
libbitdht/src/tests/bdmidids_test.cc
Normal file
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* bitdht/bdmidids_test.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 "bitdht/bdpeer.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
/* Do this multiple times */
|
||||
int i, j;
|
||||
|
||||
std::cerr << "Test Mid Peer Intersection....." << std::endl;
|
||||
for(i = 0; i < 10; i++)
|
||||
{
|
||||
bdNodeId targetId;
|
||||
bdNodeId peerId;
|
||||
|
||||
bdStdRandomNodeId(&targetId);
|
||||
bdStdRandomNodeId(&peerId);
|
||||
|
||||
std::cerr << "-------------------------------------------------" << std::endl;
|
||||
|
||||
for(j = 0; j < 10; j++)
|
||||
{
|
||||
|
||||
bdNodeId midId;
|
||||
|
||||
bdStdRandomMidId(&targetId, &peerId, &midId);
|
||||
|
||||
bdMetric TPmetric;
|
||||
bdMetric TMmetric;
|
||||
bdMetric PMmetric;
|
||||
|
||||
bdStdDistance(&targetId, &peerId, &TPmetric);
|
||||
bdStdDistance(&targetId, &midId, &TMmetric);
|
||||
bdStdDistance(&peerId, &midId, &PMmetric);
|
||||
|
||||
int TPdist = bdStdBucketDistance(&TPmetric);
|
||||
int TMdist = bdStdBucketDistance(&TMmetric);
|
||||
int PMdist = bdStdBucketDistance(&PMmetric);
|
||||
|
||||
std::cerr << "Target: ";
|
||||
bdStdPrintNodeId(std::cerr,&targetId);
|
||||
std::cerr << " Peer: ";
|
||||
bdStdPrintNodeId(std::cerr,&peerId);
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "\tTarget ^ Peer: ";
|
||||
bdStdPrintNodeId(std::cerr,&TPmetric);
|
||||
std::cerr << " Bucket: " << TPdist;
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "\tTarget ^ Mid: ";
|
||||
bdStdPrintNodeId(std::cerr,&TMmetric);
|
||||
std::cerr << " Bucket: " << TMdist;
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "\tPeer ^ Mid: ";
|
||||
bdStdPrintNodeId(std::cerr,&PMmetric);
|
||||
std::cerr << " Bucket: " << PMdist;
|
||||
std::cerr << std::endl;
|
||||
|
||||
/* now save mid to peer... and repeat */
|
||||
peerId = midId;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
102
libbitdht/src/tests/bdmsgs_test.cc
Normal file
102
libbitdht/src/tests/bdmsgs_test.cc
Normal file
@ -0,0 +1,102 @@
|
||||
|
||||
/*
|
||||
* bitdht/bdmsgs_test.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 "bitdht/bdmsgs.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
#include <string.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)
|
||||
{
|
||||
/***** create messages *****/
|
||||
char msg[MAX_MESSAGE_LEN];
|
||||
int avail = MAX_MESSAGE_LEN -1;
|
||||
|
||||
bdToken tid;
|
||||
bdToken vid;
|
||||
bdToken token;
|
||||
|
||||
|
||||
bdNodeId ownId;
|
||||
bdNodeId peerId;
|
||||
bdNodeId target;
|
||||
bdNodeId info_hash;
|
||||
|
||||
bdStdRandomNodeId(&ownId);
|
||||
bdStdRandomNodeId(&peerId);
|
||||
bdStdRandomNodeId(&target);
|
||||
bdStdRandomNodeId(&info_hash);
|
||||
|
||||
std::list<bdId> nodes;
|
||||
std::list<std::string> values;
|
||||
|
||||
/* setup tokens */
|
||||
strncpy((char*)tid.data, "tid", 4);
|
||||
strncpy((char*)vid.data, "RS50", 5);
|
||||
strncpy((char*)token.data, "ToKEn", 6);
|
||||
|
||||
tid.len = 3;
|
||||
vid.len = 4;
|
||||
token.len = 5;
|
||||
|
||||
/* setup lists */
|
||||
for(int i = 0; i < 8; i++)
|
||||
{
|
||||
bdId rndId;
|
||||
bdStdRandomId(&rndId);
|
||||
|
||||
nodes.push_back(rndId);
|
||||
values.push_back("values");
|
||||
}
|
||||
|
||||
uint32_t port = 1234;
|
||||
|
||||
bitdht_create_ping_msg(&tid, &ownId, msg, avail);
|
||||
bitdht_response_ping_msg(&tid, &ownId, &vid, msg, avail);
|
||||
|
||||
bitdht_find_node_msg(&tid, &ownId, &target, msg, avail);
|
||||
bitdht_resp_node_msg(&tid, &ownId, nodes, msg, avail);
|
||||
|
||||
bitdht_get_peers_msg(&tid, &ownId, &info_hash, msg, avail);
|
||||
bitdht_peers_reply_hash_msg(&tid, &ownId, &token, values, msg, avail);
|
||||
bitdht_peers_reply_closest_msg(&tid, &ownId, &token, nodes, msg, avail);
|
||||
|
||||
bitdht_announce_peers_msg(&tid, &ownId, &info_hash, port, &token, msg, avail);
|
||||
bitdht_reply_announce_msg(&tid, &ownId, msg, avail);
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
185
libbitdht/src/tests/bdnode_multitest1.cc
Normal file
185
libbitdht/src/tests/bdnode_multitest1.cc
Normal file
@ -0,0 +1,185 @@
|
||||
/*
|
||||
* bitdht/bdnode_multitest1.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 "bitdht/bdnode.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
#include "util/bdrandom.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/**********************************************************************************
|
||||
* tests of multi bdnodes all connected together.
|
||||
* in these cases, the networking step is shortcut and the ip addresses ignored.
|
||||
* instead the port number is used as an index to peers.
|
||||
*
|
||||
* test1()
|
||||
* Small cross seeding, and static list of peers.
|
||||
* Set it going - and see what happens.
|
||||
*/
|
||||
|
||||
std::map<bdId, bdNode *> nodes;
|
||||
std::map<uint16_t, bdId> portIdx;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
time_t sim_time = 60;
|
||||
time_t starttime = time(NULL);
|
||||
int n_nodes = 10;
|
||||
std::map<bdId, bdNode *>::iterator it;
|
||||
std::map<bdId, bdNode *>::iterator nit;
|
||||
std::map<uint16_t, bdId>::iterator pit;
|
||||
|
||||
int i, j;
|
||||
|
||||
bdDhtFunctions *fns = new bdStdDht();
|
||||
|
||||
std::cerr << "bdnode_multitest1() Setting up Nodes" << std::endl;
|
||||
/* setup nodes */
|
||||
for(i = 0; i < n_nodes; i++)
|
||||
{
|
||||
bdId id;
|
||||
|
||||
bdStdRandomId(&id);
|
||||
|
||||
id.addr.sin_port = htons(i);
|
||||
((uint32_t *) (id.id.data))[0] = i * 16 * 16; /* force this so the sort order is maintained! */
|
||||
std::cerr << "bdnode_multitest1() Id: ";
|
||||
fns->bdPrintId(std::cerr, &id);
|
||||
std::cerr << std::endl;
|
||||
|
||||
bdNode *node = new bdNode(&(id.id), "bdTEST", "", fns);
|
||||
|
||||
/* Store in nodes */
|
||||
nodes[id] = node;
|
||||
/* Store in indices */
|
||||
portIdx[i] = id;
|
||||
|
||||
}
|
||||
|
||||
std::cerr << "bdnode_multitest1() Cross Seeding" << std::endl;
|
||||
/* do a little cross seeding */
|
||||
for(i = 0; i < n_nodes; i++)
|
||||
{
|
||||
bdId nid = portIdx[i];
|
||||
bdNode *node = nodes[nid];
|
||||
|
||||
for(j = 0; j < 5; j++)
|
||||
{
|
||||
int peeridx = bdRandom::random_u32() % n_nodes;
|
||||
|
||||
bdId pid = portIdx[peeridx];
|
||||
node->addPotentialPeer(&pid, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
/* ready to run */
|
||||
|
||||
std::cerr << "bdnode_multitest1() Simulation Time....." << std::endl;
|
||||
i = 0;
|
||||
while(time(NULL) < starttime + sim_time)
|
||||
{
|
||||
i++;
|
||||
std::cerr << "bdnode_multitest1() Iteration: " << i << std::endl;
|
||||
|
||||
for(it = nodes.begin(), j = 0; it != nodes.end(); it++, j++)
|
||||
{
|
||||
/* extract messages to go -> and deliver */
|
||||
#define MAX_MSG_SIZE 10240
|
||||
struct sockaddr_in addr;
|
||||
char data[MAX_MSG_SIZE];
|
||||
int len = MAX_MSG_SIZE;
|
||||
|
||||
while(it->second->outgoingMsg(&addr, data, &len))
|
||||
{
|
||||
std::cerr << "bdnode_multitest1() Msg from Peer: " << j;
|
||||
|
||||
/* find the peer */
|
||||
int peeridx = htons(addr.sin_port);
|
||||
pit = portIdx.find(peeridx);
|
||||
nit = nodes.end();
|
||||
if (pit != portIdx.end())
|
||||
{
|
||||
nit = nodes.find(pit->second);
|
||||
std::cerr << " For: ";
|
||||
fns->bdPrintId(std::cerr, &(nit->first));
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << " For Unknown Destination";
|
||||
std::cerr << std::endl;
|
||||
|
||||
}
|
||||
|
||||
if (nit != nodes.end())
|
||||
{
|
||||
/* set from address */
|
||||
nit->second->incomingMsg( (sockaddr_in *) &(it->first.addr), data, len);
|
||||
}
|
||||
/* reset message size */
|
||||
len = MAX_MSG_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
for(it = nodes.begin(), j = 0; it != nodes.end(); it++, j++)
|
||||
{
|
||||
/* tick */
|
||||
std::cerr << "bdnode_multitest1() Ticking peer: " << j << std::endl;
|
||||
it->second->iteration();
|
||||
}
|
||||
|
||||
if (i % 5 == 0)
|
||||
{
|
||||
std::cerr << "bdnode_multitest1() Displying States"<< std::endl;
|
||||
for(it = nodes.begin(), j = 0; it != nodes.end(); it++, j++)
|
||||
{
|
||||
/* tick */
|
||||
std::cerr << "bdnode_multitest1() Peer State: " << j << std::endl;
|
||||
it->second->printState();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* have a rest */
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
90
libbitdht/src/tests/bdnode_test.cc
Normal file
90
libbitdht/src/tests/bdnode_test.cc
Normal file
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* bitdht/bdnode_test.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 "bitdht/bdpeer.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
#include "bitdht/bdquery.h"
|
||||
#include "bitdht/bdnode.h"
|
||||
|
||||
#define N_PEERS_TO_ADD_INIT 10
|
||||
#define N_PEERS_TO_ADD 11
|
||||
#define N_PEERS_TO_START 10
|
||||
#define N_PEERS_TO_PRINT 1
|
||||
#define N_QUERIES 2
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
/* create some ids */
|
||||
bdDhtFunctions *fns = new bdStdDht();
|
||||
|
||||
bdNodeId ownId;
|
||||
bdStdRandomNodeId(&ownId);
|
||||
|
||||
bdNode node(&ownId, "bdTEST","./dht.log", fns);
|
||||
|
||||
int i = 0;
|
||||
for (i = 0; i < N_PEERS_TO_ADD_INIT; i++)
|
||||
{
|
||||
bdId tmpId;
|
||||
bdStdRandomId(&tmpId);
|
||||
|
||||
node.addPeer(&tmpId, 0);
|
||||
}
|
||||
|
||||
node.printState();
|
||||
|
||||
#if 0
|
||||
for(i = 0; i < N_QUERIES; i++)
|
||||
{
|
||||
/* create a query */
|
||||
bdNodeId queryId;
|
||||
bdStdRandomNodeId(&queryId);
|
||||
|
||||
node.addQuery(&queryId, 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
node.printState();
|
||||
|
||||
for (i = 0; i < N_PEERS_TO_ADD; i++)
|
||||
{
|
||||
bdId tmpId;
|
||||
bdStdRandomId(&tmpId);
|
||||
|
||||
node.addPeer(&tmpId, 0);
|
||||
|
||||
if (i % N_PEERS_TO_PRINT == 0)
|
||||
{
|
||||
node.printState();
|
||||
node.iteration();
|
||||
sleep(5);
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
56
libbitdht/src/tests/bdnode_test2.cc
Normal file
56
libbitdht/src/tests/bdnode_test2.cc
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* bitdht/bdnode_test2.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 "bitdht/bdpeer.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
#include "bitdht/bdquery.h"
|
||||
#include "bitdht/bdnode.h"
|
||||
|
||||
#define N_PEERS_TO_ADD_INIT 10
|
||||
#define N_PEERS_TO_ADD 11
|
||||
#define N_PEERS_TO_START 10
|
||||
#define N_PEERS_TO_PRINT 1
|
||||
#define N_QUERIES 2
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
/* create some ids */
|
||||
bdNodeId ownId;
|
||||
bdStdRandomNodeId(&ownId);
|
||||
bdDhtFunctions *fns = new bdStdDht();
|
||||
|
||||
bdNode node(&ownId, "bdTEST", "./dht.log", fns);
|
||||
|
||||
while(1)
|
||||
{
|
||||
node.iteration();
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
83
libbitdht/src/tests/bdquery_test.cc
Normal file
83
libbitdht/src/tests/bdquery_test.cc
Normal file
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* bitdht/bdquery_test.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 "bitdht/bdpeer.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
#include "bitdht/bdquery.h"
|
||||
|
||||
#define N_PEERS_TO_ADD 10000
|
||||
#define N_PEERS_TO_PRINT 1000
|
||||
#define N_PEERS_TO_START 10
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
/* create some ids */
|
||||
bdNodeId ownId;
|
||||
bdStdRandomNodeId(&ownId);
|
||||
|
||||
bdDhtFunctions *fns = new bdStdDht();
|
||||
|
||||
bdSpace space(&ownId, fns);
|
||||
int i = 0;
|
||||
for (i = 0; i < N_PEERS_TO_ADD; i++)
|
||||
{
|
||||
bdId tmpId;
|
||||
bdStdRandomId(&tmpId);
|
||||
|
||||
space.add_peer(&tmpId, 0);
|
||||
}
|
||||
|
||||
space.printDHT();
|
||||
|
||||
/* create a query */
|
||||
bdId queryId;
|
||||
bdStdRandomId(&queryId);
|
||||
|
||||
|
||||
|
||||
std::list<bdId> startList;
|
||||
std::multimap<bdMetric, bdId> nearest;
|
||||
std::multimap<bdMetric, bdId>::iterator it;
|
||||
|
||||
space.find_nearest_nodes(&(queryId.id), N_PEERS_TO_START, nearest);
|
||||
|
||||
for(it = nearest.begin(); it != nearest.end(); it++)
|
||||
{
|
||||
startList.push_back(it->second);
|
||||
}
|
||||
|
||||
bdQuery query(&(queryId.id), startList, BITDHT_QFLAGS_DISGUISE, fns);
|
||||
|
||||
/* */
|
||||
|
||||
query.printQuery();
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
60
libbitdht/src/tests/bdspace_test.cc
Normal file
60
libbitdht/src/tests/bdspace_test.cc
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* bitdht/bdspace_test.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 "bitdht/bdpeer.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
|
||||
#define N_PEERS_TO_ADD 10000
|
||||
#define N_PEERS_TO_PRINT 1000
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
/* create some ids */
|
||||
bdNodeId ownId;
|
||||
bdStdRandomNodeId(&ownId);
|
||||
bdDhtFunctions *fns = new bdStdDht();
|
||||
|
||||
bdSpace space(&ownId, fns);
|
||||
int i = 0;
|
||||
for (i = 0; i < N_PEERS_TO_ADD; i++)
|
||||
{
|
||||
bdId tmpId;
|
||||
bdStdRandomId(&tmpId);
|
||||
|
||||
space.add_peer(&tmpId, 0);
|
||||
|
||||
if (i % N_PEERS_TO_PRINT == 0)
|
||||
{
|
||||
space.printDHT();
|
||||
}
|
||||
}
|
||||
space.printDHT();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
67
libbitdht/src/tests/bdspace_test2.cc
Normal file
67
libbitdht/src/tests/bdspace_test2.cc
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* bitdht/bdspace_test2.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 "bitdht/bdpeer.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
|
||||
#define N_PEERS_TO_ADD 10000
|
||||
#define N_PEERS_TO_TEST 100
|
||||
#define N_PEERS_TO_FIND 10
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
/* create some ids */
|
||||
bdNodeId ownId;
|
||||
bdStdRandomNodeId(&ownId);
|
||||
bdDhtFunctions *fns = new bdStdDht();
|
||||
|
||||
bdSpace space(&ownId, fns);
|
||||
int i = 0;
|
||||
for (i = 0; i < N_PEERS_TO_ADD; i++)
|
||||
{
|
||||
bdId tmpId;
|
||||
bdStdRandomId(&tmpId);
|
||||
space.add_peer(&tmpId, 0);
|
||||
}
|
||||
|
||||
space.printDHT();
|
||||
|
||||
|
||||
/* now generate random id's and test closeness */
|
||||
for(i = 0; i < N_PEERS_TO_TEST; i++)
|
||||
{
|
||||
bdId tmpId;
|
||||
bdStdRandomId(&tmpId);
|
||||
std::multimap<bdMetric, bdId> list2;
|
||||
|
||||
space.find_nearest_nodes(&(tmpId.id), N_PEERS_TO_FIND, list2);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
47
libbitdht/src/tests/bdstore_test.cc
Normal file
47
libbitdht/src/tests/bdstore_test.cc
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* bitdht/bdstore_test.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 "bitdht/bdstore.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
/* load store */
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf(stderr, "Missing Store File\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
bdDhtFunctions *fns = new bdStdDht();
|
||||
bdStore store(argv[1], fns);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
66
libbitdht/src/tests/bdudp_test.cc
Normal file
66
libbitdht/src/tests/bdudp_test.cc
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* bitdht/bdudp_test.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 "bitdht/bdpeer.h"
|
||||
#include "bitdht/bdquery.h"
|
||||
#include "udp/udpbitdht.h"
|
||||
|
||||
#define N_PEERS_TO_ADD 10000
|
||||
#define N_PEERS_TO_PRINT 1000
|
||||
#define N_PEERS_TO_START 10
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
/* create some ids */
|
||||
bdId ownId;
|
||||
bdRandomId(&ownId);
|
||||
|
||||
|
||||
struct sockaddr_in local;
|
||||
local.sin_addr.s_addr = 0;
|
||||
local.sin_port = htons(7812);
|
||||
std::string bootstrapfile = "dht.log";
|
||||
|
||||
bdId bid;
|
||||
|
||||
bid.addr = local;
|
||||
bid.id = ownId.id;
|
||||
|
||||
UdpBitDht ubd(local, 0, &bid, bootstrapfile);
|
||||
|
||||
|
||||
|
||||
while(1)
|
||||
{
|
||||
ubd.tick();
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
182
libbitdht/src/tests/bencode_test.cc
Normal file
182
libbitdht/src/tests/bencode_test.cc
Normal file
@ -0,0 +1,182 @@
|
||||
/*
|
||||
* bitdht/bencode_test.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 "bitdht/bencode.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
char msg[100];
|
||||
|
||||
msg[0] = 'd';
|
||||
msg[1] = '1';
|
||||
msg[2] = ':';
|
||||
msg[3] = 't';
|
||||
msg[4] = 'L';
|
||||
msg[5] = 'b';
|
||||
msg[6] = 'U';
|
||||
msg[7] = 0xd9;
|
||||
msg[8] = 0xfa;
|
||||
msg[9] = 0xff;
|
||||
msg[10] = 0xff;
|
||||
msg[11] = 0xff;
|
||||
msg[12] = 0xff;
|
||||
msg[13] = '\n';
|
||||
msg[14] = 'H';
|
||||
msg[15] = '#';
|
||||
msg[16] = '#';
|
||||
msg[17] = '#';
|
||||
msg[18] = '#';
|
||||
msg[19] = '#';
|
||||
msg[20] = '#';
|
||||
msg[21] = '#';
|
||||
|
||||
be_node *n = be_decoden(msg, 16);
|
||||
|
||||
if (n)
|
||||
{
|
||||
be_dump(n);
|
||||
be_free(n);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "didn't crash!\n");
|
||||
}
|
||||
|
||||
msg[0] = 'd';
|
||||
msg[1] = '1';
|
||||
msg[2] = ':';
|
||||
msg[3] = 'a';
|
||||
msg[4] = 'L';
|
||||
msg[5] = 0x8d;
|
||||
msg[6] = 0xd6;
|
||||
msg[7] = '\r';
|
||||
msg[8] = 0x9d;
|
||||
msg[9] = ';';
|
||||
msg[10] = 0xff;
|
||||
msg[11] = 0xff;
|
||||
msg[12] = 0xff;
|
||||
msg[13] = 0xff;
|
||||
msg[14] = 'H';
|
||||
msg[15] = '#';
|
||||
msg[16] = '#';
|
||||
msg[17] = '#';
|
||||
msg[18] = '#';
|
||||
msg[19] = '#';
|
||||
msg[20] = '#';
|
||||
msg[21] = '#';
|
||||
|
||||
n = be_decoden(msg, 14);
|
||||
|
||||
if (n)
|
||||
{
|
||||
be_dump(n);
|
||||
be_free(n);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "didn't crash!\n");
|
||||
}
|
||||
|
||||
msg[0] = 'd';
|
||||
msg[1] = '1';
|
||||
msg[2] = ':';
|
||||
msg[3] = 't';
|
||||
msg[4] = '4';
|
||||
msg[5] = ':';
|
||||
msg[6] = 'a';
|
||||
msg[7] = 'b';
|
||||
msg[8] = 'c';
|
||||
msg[9] = 'd';
|
||||
msg[10] = '1';
|
||||
msg[11] = ':';
|
||||
msg[12] = 'y';
|
||||
msg[13] = '1';
|
||||
msg[14] = ':';
|
||||
msg[15] = 'r';
|
||||
msg[16] = '1';
|
||||
msg[17] = ':';
|
||||
msg[18] = 'r';
|
||||
msg[19] = 'd';
|
||||
msg[20] = '2';
|
||||
msg[21] = ':';
|
||||
msg[22] = 'i';
|
||||
msg[23] = 'd';
|
||||
msg[24] = '2';
|
||||
msg[25] = '0';
|
||||
msg[26] = ':';
|
||||
msg[27] = '1';
|
||||
msg[28] = '2';
|
||||
msg[29] = '3';
|
||||
msg[30] = '4';
|
||||
msg[31] = '5';
|
||||
msg[32] = '6';
|
||||
msg[33] = '7';
|
||||
msg[34] = '8';
|
||||
msg[35] = '9';
|
||||
msg[36] = '0';
|
||||
msg[37] = 'a';
|
||||
msg[38] = 'b';
|
||||
msg[39] = 'c';
|
||||
msg[40] = 'd';
|
||||
msg[41] = 'e';
|
||||
msg[42] = 'f';
|
||||
msg[43] = 'g';
|
||||
msg[44] = 'h';
|
||||
msg[45] = 'i';
|
||||
msg[46] = '.';
|
||||
msg[47] = '5';
|
||||
msg[48] = ':';
|
||||
msg[49] = 'n';
|
||||
msg[50] = 'o';
|
||||
msg[51] = 'd';
|
||||
msg[52] = 'e';
|
||||
msg[53] = 's';
|
||||
msg[54] = '2';
|
||||
msg[55] = '0';
|
||||
msg[56] = '8';
|
||||
msg[57] = ':';
|
||||
msg[58] = '\0';
|
||||
msg[59] = '\0';
|
||||
msg[60] = '\0';
|
||||
|
||||
n = be_decoden(msg, 58);
|
||||
|
||||
if (n)
|
||||
{
|
||||
be_dump(n);
|
||||
be_free(n);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "didn't crash!\n");
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
19
libbitdht/src/tests/scripts/checks.mk
Normal file
19
libbitdht/src/tests/scripts/checks.mk
Normal file
@ -0,0 +1,19 @@
|
||||
#Basic checks
|
||||
|
||||
ifndef TEST_TOP_DIR
|
||||
dummy:
|
||||
echo "TEST_TOP_DIR is not defined in your makefile"
|
||||
endif
|
||||
|
||||
ifneq ($(OS),Linux)
|
||||
ifneq ($(OS),MacOSX)
|
||||
ifndef PTHREADS_DIR
|
||||
dummy:
|
||||
echo "you must define PTHREADS_DIR before you can compile"
|
||||
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
|
||||
|
118
libbitdht/src/tests/scripts/config-cygwin.mk
Normal file
118
libbitdht/src/tests/scripts/config-cygwin.mk
Normal file
@ -0,0 +1,118 @@
|
||||
ifneq ($(OS),Cygwin)
|
||||
dummy:
|
||||
echo "ERROR Cygwin configuration file included, but (OS != Cygwin)
|
||||
|
||||
endif
|
||||
|
||||
############ LINUX CONFIGURATION ########################
|
||||
|
||||
# flags for components....
|
||||
PQI_USE_XPGP = 1
|
||||
#PQI_USE_PROXY = 1
|
||||
#PQI_USE_CHANNELS = 1
|
||||
#USE_FILELOOK = 1
|
||||
|
||||
###########################################################################
|
||||
|
||||
#### DrBobs Versions.... Please Don't Delete.
|
||||
### Comment out if needed.
|
||||
ALT_SRC_ROOT=/cygdrive/c/home/rmfern/prog/MinGW
|
||||
SRC_ROOT=../../../..
|
||||
|
||||
PTHREADS_DIR=$(ALT_SRC_ROOT)/pthreads/pthreads.2
|
||||
|
||||
###################
|
||||
|
||||
#ALT_SRC_ROOT=/cygdrive/c/RetroShareBuild/src
|
||||
#SRC_ROOT=/cygdrive/c/RetroShareBuild/src
|
||||
|
||||
#PTHREADS_DIR=$(ALT_SRC_ROOT)/pthreads-w32-2-8-0-release
|
||||
|
||||
###################
|
||||
|
||||
ZLIB_DIR=$(ALT_SRC_ROOT)/zlib-1.2.3
|
||||
|
||||
SSL_DIR=$(SRC_ROOT)/openssl-0.9.7g-xpgp-0.1c
|
||||
UPNPC_DIR=$(SRC_ROOT)/miniupnpc-1.0
|
||||
|
||||
include $(RS_TOP_DIR)/scripts/checks.mk
|
||||
|
||||
############ ENFORCE DIRECTORY NAMING ########################
|
||||
|
||||
CC = g++
|
||||
RM = /bin/rm
|
||||
RANLIB = ranlib
|
||||
LIBDIR = $(RS_TOP_DIR)/lib
|
||||
LIBRS = $(LIBDIR)/libretroshare.a
|
||||
|
||||
# Unix: Linux/Cygwin
|
||||
INCLUDE = -I $(RS_TOP_DIR)
|
||||
|
||||
ifdef PQI_DEBUG
|
||||
CFLAGS = -Wall -g $(INCLUDE)
|
||||
else
|
||||
CFLAGS = -Wall -O2 $(INCLUDE)
|
||||
endif
|
||||
|
||||
ifdef PQI_USE_XPGP
|
||||
INCLUDE += -I $(SSL_DIR)/include
|
||||
endif
|
||||
|
||||
ifdef PQI_USE_XPGP
|
||||
CFLAGS += -DPQI_USE_XPGP
|
||||
endif
|
||||
|
||||
ifdef PQI_USE_PROXY
|
||||
CFLAGS += -DPQI_USE_PROXY
|
||||
endif
|
||||
|
||||
ifdef PQI_USE_CHANNELS
|
||||
CFLAGS += -DPQI_USE_CHANNELS
|
||||
endif
|
||||
|
||||
ifdef USE_FILELOOK
|
||||
CFLAGS += -DUSE_FILELOOK
|
||||
endif
|
||||
|
||||
|
||||
RSCFLAGS = -Wall -g $(INCLUDE)
|
||||
|
||||
#########################################################################
|
||||
# OS Compile Options
|
||||
#########################################################################
|
||||
|
||||
# For the SSL BIO compilation. (Copied from OpenSSL compilation flags)
|
||||
BIOCC = gcc
|
||||
|
||||
# Cygwin - ?same? as Linux flags
|
||||
BIOCFLAGS = -I $(SSL_DIR)/include -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -DOPENSSL_NO_KRB5 -DL_ENDIAN -DTERMIO -O3 -fomit-frame-pointer -m486 -Wall -DSHA1_ASM -DMD5_ASM -DRMD160_ASM
|
||||
|
||||
#########################################################################
|
||||
# OS specific Linking.
|
||||
#########################################################################
|
||||
|
||||
# for static pthread libs....
|
||||
WININC += -DPTW32_STATIC_LIB
|
||||
WININC += -mno-cygwin -mwindows -fno-exceptions
|
||||
WININC += -DWINDOWS_SYS
|
||||
|
||||
WINLIB = -lws2_32 -luuid -lole32 -liphlpapi
|
||||
WINLIB += -lcrypt32 -lwinmm
|
||||
|
||||
CFLAGS += -I$(PTHREADS_DIR) $(WININC)
|
||||
CFLAGS += -I$(ZLIB_DIR)
|
||||
|
||||
LIBS = -L$(LIBDIR) -lretroshare
|
||||
ifdef PQI_USE_XPGP
|
||||
LIBS += -L$(SSL_DIR)
|
||||
endif
|
||||
|
||||
LIBS += -lssl -lcrypto
|
||||
LIBS += -L$(UPNPC_DIR) -lminiupnpc
|
||||
LIBS += -L$(ZLIB_DIR) -lz
|
||||
LIBS += -L$(PTHREADS_DIR) -lpthreadGC2d
|
||||
LIBS += $(WINLIB)
|
||||
|
||||
RSCFLAGS += $(WININC)
|
||||
|
||||
|
41
libbitdht/src/tests/scripts/config-linux.mk
Normal file
41
libbitdht/src/tests/scripts/config-linux.mk
Normal file
@ -0,0 +1,41 @@
|
||||
|
||||
ifneq ($(OS),Linux)
|
||||
dummy:
|
||||
echo "ERROR Linux configuration file included, but (OS != Linux)
|
||||
|
||||
endif
|
||||
|
||||
############ LINUX CONFIGURATION ########################
|
||||
|
||||
include $(TEST_TOP_DIR)/scripts/checks.mk
|
||||
|
||||
############ ENFORCE DIRECTORY NAMING ########################
|
||||
|
||||
CC = g++
|
||||
RM = /bin/rm
|
||||
RANLIB = ranlib
|
||||
LIBDIR = $(LIB_TOP_DIR)/lib
|
||||
LIBRS = $(LIBDIR)/libretroshare.a
|
||||
|
||||
# Unix: Linux/Cygwin
|
||||
INCLUDE = -I $(LIB_TOP_DIR)
|
||||
CFLAGS = -Wall -g $(INCLUDE)
|
||||
CFLAGS += ${DEFINES} -D BE_DEBUG
|
||||
|
||||
#########################################################################
|
||||
# OS Compile Options
|
||||
#########################################################################
|
||||
|
||||
#########################################################################
|
||||
# OS specific Linking.
|
||||
#########################################################################
|
||||
|
||||
LIBS = -L$(LIBDIR) -lbitdht
|
||||
LIBS += -lpthread
|
||||
#LIBS += $(XLIB) -ldl -lz
|
||||
#LIBS += -lupnp
|
||||
#LIBS += -lgpgme
|
||||
#
|
||||
#RSLIBS = $(LIBS)
|
||||
|
||||
|
81
libbitdht/src/tests/scripts/config-macosx.mk
Normal file
81
libbitdht/src/tests/scripts/config-macosx.mk
Normal file
@ -0,0 +1,81 @@
|
||||
|
||||
ifneq ($(OS),MacOSX)
|
||||
dummy:
|
||||
echo "ERROR MacOSX configuration file included, but (OS != MacOSX)
|
||||
|
||||
endif
|
||||
|
||||
############ MACOSX CONFIGURATION ########################
|
||||
|
||||
|
||||
# FLAGS to decide if we want i386 Build or ppc Build
|
||||
#
|
||||
#
|
||||
|
||||
# MAC_I386_BUILD = 1
|
||||
# MAC_PPC_BUILD = 1
|
||||
|
||||
MAC_I386_BUILD = 1
|
||||
#MAC_PPC_BUILD = 1
|
||||
|
||||
ifndef MAC_I386_BUILD
|
||||
MAC_PPC_BUILD = 1
|
||||
endif
|
||||
|
||||
include $(TEST_TOP_DIR)/scripts/checks.mk
|
||||
|
||||
############ ENFORCE DIRECTORY NAMING ########################
|
||||
|
||||
CC = g++
|
||||
RM = /bin/rm
|
||||
|
||||
RANLIB = ranlib
|
||||
|
||||
# Dummy ranlib -> can't do it until afterwards with universal binaries.
|
||||
# RANLIB = ls -l
|
||||
|
||||
LIBDIR = $(LIB_TOP_DIR)/lib
|
||||
LIBRS = $(LIBDIR)/libretroshare.a
|
||||
|
||||
OPT_DIR = /opt/local
|
||||
OPT_INCLUDE = $(OPT_DIR)/include
|
||||
OPT_LIBS = $(OPT_DIR)/lib
|
||||
|
||||
INCLUDE = -I $(LIB_TOP_DIR)
|
||||
#-I $(OPT_INCLUDE)
|
||||
#CFLAGS = -Wall -O3
|
||||
CFLAGS = -Wall -g
|
||||
|
||||
# Flags for architecture builds.
|
||||
ifdef MAC_I386_BUILD
|
||||
CFLAGS += -arch i386
|
||||
endif
|
||||
|
||||
ifdef MAC_PPC_BUILD
|
||||
CFLAGS += -arch ppc
|
||||
endif
|
||||
|
||||
CFLAGS += $(INCLUDE)
|
||||
|
||||
# This Line is for Universal BUILD for 10.4 + 10.5
|
||||
# (but unlikely to work unless Qt Libraries are build properly)
|
||||
#CFLAGS += -isysroot /Developer/SDKs/MacOSX10.5.sdk
|
||||
|
||||
#########################################################################
|
||||
# OS Compile Options
|
||||
#########################################################################
|
||||
|
||||
#########################################################################
|
||||
# OS specific Linking.
|
||||
#########################################################################
|
||||
|
||||
#LIBS = -Wl,-search_paths_first
|
||||
|
||||
# for Univeral BUILD
|
||||
# LIBS += -arch ppc -arch i386
|
||||
# LIBS += -Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386
|
||||
|
||||
#LIBS += -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk
|
||||
LIBS += -L$(LIBDIR) -lbitdht
|
||||
|
||||
|
138
libbitdht/src/tests/scripts/config-mingw.mk
Normal file
138
libbitdht/src/tests/scripts/config-mingw.mk
Normal file
@ -0,0 +1,138 @@
|
||||
#ifneq ($(OS),"Win ")
|
||||
#dummy:
|
||||
# echo "ERROR OS = $(OS)"
|
||||
# echo "ERROR MinGW configuration file included, but (OS != Win)
|
||||
#
|
||||
#endif
|
||||
|
||||
############ LINUX CONFIGURATION ########################
|
||||
|
||||
# flags for components....
|
||||
#PQI_USE_XPGP = 1
|
||||
#PQI_USE_PROXY = 1
|
||||
#PQI_USE_CHANNELS = 1
|
||||
#USE_FILELOOK = 1
|
||||
|
||||
###########################################################################
|
||||
|
||||
#### DrBobs Versions.... Please Don't Delete.
|
||||
### Comment out if needed.
|
||||
SRC_ROOT_PKG=/home/Mark/prog/retroshare/package/rs-win-v0.5.0/src
|
||||
SRC_ROOT_GPG=/local
|
||||
|
||||
#ALT_SRC_ROOT=/cygdrive/c/home/rmfern/prog/MinGW
|
||||
#SRC_ROOT=../../../..
|
||||
|
||||
PTHREADS_DIR=$(SRC_ROOT_PKG)/pthreads-w32-2-8-0/Pre-built.2
|
||||
ZLIB_DIR=$(SRC_ROOT_PKG)/zlib-1.2.3
|
||||
SSL_DIR=$(SRC_ROOT_PKG)/openssl-tmp
|
||||
UPNPC_DIR=$(SRC_ROOT_PKG)/miniupnpc-1.3
|
||||
|
||||
###########################################################################
|
||||
|
||||
#### Enable this section for compiling with MSYS/MINGW compile
|
||||
#SRC_ROOT=/home/linux
|
||||
|
||||
#SSL_DIR=$(SRC_ROOT)/OpenSSL
|
||||
#GPGME_DIR=$(SRC_ROOT)/gpgme-1.1.8
|
||||
#GPG_ERROR_DIR=$(SRC_ROOT)/libgpg-error-1.7
|
||||
|
||||
#ZLIB_DIR=$(SRC_ROOT)/zlib-1.2.3
|
||||
#UPNPC_DIR=$(SRC_ROOT)/miniupnpc-1.0
|
||||
#PTHREADS_DIR=$(SRC_ROOT)/pthreads-w32-2-8-0-release
|
||||
|
||||
include $(RS_TOP_DIR)/scripts/checks.mk
|
||||
|
||||
############ ENFORCE DIRECTORY NAMING #######################################
|
||||
|
||||
CC = g++
|
||||
RM = /bin/rm
|
||||
RANLIB = ranlib
|
||||
LIBDIR = $(RS_TOP_DIR)/lib
|
||||
LIBRS = $(LIBDIR)/libretroshare.a
|
||||
|
||||
# Unix: Linux/Cygwin
|
||||
INCLUDE = -I $(RS_TOP_DIR)
|
||||
|
||||
ifdef PQI_DEBUG
|
||||
CFLAGS = -Wall -g $(INCLUDE)
|
||||
else
|
||||
CFLAGS = -Wall -O2 $(INCLUDE)
|
||||
endif
|
||||
|
||||
# These aren't used anymore.... really.
|
||||
ifdef PQI_USE_XPGP
|
||||
CFLAGS += -DPQI_USE_XPGP
|
||||
endif
|
||||
|
||||
ifdef PQI_USE_PROXY
|
||||
CFLAGS += -DPQI_USE_PROXY
|
||||
endif
|
||||
|
||||
ifdef PQI_USE_CHANNELS
|
||||
CFLAGS += -DPQI_USE_CHANNELS
|
||||
endif
|
||||
|
||||
ifdef USE_FILELOOK
|
||||
CFLAGS += -DUSE_FILELOOK
|
||||
endif
|
||||
|
||||
|
||||
# SSL / pthreads / Zlib
|
||||
# included by default for Windows compilation.
|
||||
INCLUDE += -I $(SSL_DIR)/include
|
||||
INCLUDE += -I$(PTHREADS_DIR)
|
||||
INCLUDE += -I$(ZLIB_DIR)
|
||||
|
||||
|
||||
#########################################################################
|
||||
# OS Compile Options
|
||||
#########################################################################
|
||||
|
||||
# For the SSL BIO compilation. (Copied from OpenSSL compilation flags)
|
||||
BIOCC = gcc
|
||||
|
||||
# Cygwin - ?same? as Linux flags
|
||||
BIOCFLAGS = -I $(SSL_DIR)/include -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -DOPENSSL_NO_KRB5 -DL_ENDIAN -DTERMIO -O3 -fomit-frame-pointer -m486 -Wall -DSHA1_ASM -DMD5_ASM -DRMD160_ASM
|
||||
BIOCFLAGS += -DWINDOWS_SYS
|
||||
|
||||
#########################################################################
|
||||
# OS specific Linking.
|
||||
#########################################################################
|
||||
|
||||
# for static pthread libs....
|
||||
#WININC += -DPTW32_STATIC_LIB
|
||||
#WININC += -mno-cygwin -mwindows -fno-exceptions
|
||||
|
||||
WININC += -DWINDOWS_SYS
|
||||
|
||||
WINLIB = -lws2_32 -luuid -lole32 -liphlpapi
|
||||
WINLIB += -lcrypt32 -lwinmm
|
||||
|
||||
CFLAGS += -I$(SSL_DIR)/include
|
||||
CFLAGS += -I$(PTHREADS_DIR)/include
|
||||
CFLAGS += -I$(ZLIB_DIR)
|
||||
CFLAGS += -I$(SRC_ROOT_GPG)/include
|
||||
|
||||
### Enable this for GPGME and GPG ERROR dirs
|
||||
#CFLAGS += -I$(GPGME_DIR)/src
|
||||
#CFLAGS += -I$(GPG_ERROR_DIR)/src
|
||||
|
||||
CFLAGS += $(WININC)
|
||||
|
||||
|
||||
|
||||
LIBS = -L$(LIBDIR) -lretroshare
|
||||
|
||||
LIBS += -L$(SSL_DIR)
|
||||
|
||||
LIBS += -lssl -lcrypto
|
||||
LIBS += -L$(UPNPC_DIR) -lminiupnpc
|
||||
LIBS += -L$(ZLIB_DIR) -lz
|
||||
LIBS += -L$(PTHREADS_DIR) -lpthreadGC2d
|
||||
LIBS += $(WINLIB)
|
||||
|
||||
#RSCFLAGS = -Wall -g $(INCLUDE)
|
||||
#RSCFLAGS += $(WININC)
|
||||
|
||||
|
27
libbitdht/src/tests/scripts/config.mk
Normal file
27
libbitdht/src/tests/scripts/config.mk
Normal file
@ -0,0 +1,27 @@
|
||||
|
||||
# determine which operating system
|
||||
#
|
||||
###########################################################################
|
||||
#Define OS.
|
||||
#
|
||||
OS = Linux
|
||||
#OS = MacOSX
|
||||
#OS = Cygwin
|
||||
#OS = Win # MinGw.
|
||||
###########################################################################
|
||||
|
||||
ifeq ($(OS),Linux)
|
||||
include $(TEST_TOP_DIR)/scripts/config-linux.mk
|
||||
else
|
||||
ifeq ($(OS),MacOSX)
|
||||
include $(TEST_TOP_DIR)/scripts/config-macosx.mk
|
||||
else
|
||||
ifeq ($(OS),Cygwin)
|
||||
include $(TEST_TOP_DIR)/scripts/config-cygwin.mk
|
||||
else
|
||||
include $(TEST_TOP_DIR)/scripts/config-mingw.mk
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
###########################################################################
|
25
libbitdht/src/tests/scripts/regress.mk
Normal file
25
libbitdht/src/tests/scripts/regress.mk
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
testoutputfiles = $(foreach tt,$(1),$(tt).tstout)
|
||||
|
||||
%.tstout : %.sh
|
||||
-sh ./$< > $@ 2>&1
|
||||
|
||||
%.tstout : %
|
||||
-./$< > $@ 2>&1
|
||||
|
||||
TESTOUT = $(call testoutputfiles,$(TESTS))
|
||||
|
||||
.phony : tests regress retest clobber
|
||||
|
||||
tests: $(TESTS)
|
||||
|
||||
regress: $(TESTOUT)
|
||||
@-echo "--------------- SUCCESS (count):"
|
||||
@-grep -c SUCCESS $(TESTOUT)
|
||||
@-echo "--------------- FAILURE REPORTS:"
|
||||
@-grep FAILURE $(TESTOUT) || echo no failures
|
||||
@-echo "--------------- end"
|
||||
|
||||
retest:
|
||||
-/bin/rm $(TESTOUT)
|
||||
|
19
libbitdht/src/tests/scripts/rules.mk
Normal file
19
libbitdht/src/tests/scripts/rules.mk
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
# defines required / used.
|
||||
#
|
||||
# CFLAGS
|
||||
#
|
||||
#
|
||||
|
||||
.cc.o:
|
||||
$(CC) $(CFLAGS) -c $<
|
||||
|
||||
clean:
|
||||
-/bin/rm $(EXECOBJ) $(TESTOBJ)
|
||||
|
||||
clobber: clean retest
|
||||
-/bin/rm $(EXEC) $(TESTS)
|
||||
|
||||
|
||||
include $(TEST_TOP_DIR)/scripts/regress.mk
|
||||
|
278
libbitdht/src/tests/udpbitdht_nettest.cc
Normal file
278
libbitdht/src/tests/udpbitdht_nettest.cc
Normal file
@ -0,0 +1,278 @@
|
||||
/*
|
||||
* 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 "udp/udpbitdht.h"
|
||||
#include "udp/udpstack.h"
|
||||
#include "bitdht/bdstddht.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*******************************************************************
|
||||
* DHT test program.
|
||||
*
|
||||
*
|
||||
* Create a couple of each type.
|
||||
*/
|
||||
|
||||
#define MAX_MESSAGE_LEN 10240
|
||||
|
||||
int args(char *name)
|
||||
{
|
||||
std::cerr << "Usage: " << name;
|
||||
std::cerr << " -p <port> ";
|
||||
std::cerr << " -b </path/to/bootfile> ";
|
||||
std::cerr << " -u <uid> ";
|
||||
std::cerr << " -q <num_queries>";
|
||||
std::cerr << " -r (do dht restarts) ";
|
||||
std::cerr << " -j (do join test) ";
|
||||
std::cerr << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define DEF_PORT 7500
|
||||
|
||||
#define MIN_DEF_PORT 1001
|
||||
#define MAX_DEF_PORT 16000
|
||||
|
||||
#define DEF_BOOTFILE "bdboot.txt"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int c;
|
||||
int port = DEF_PORT;
|
||||
std::string bootfile = DEF_BOOTFILE;
|
||||
std::string uid;
|
||||
bool setUid = false;
|
||||
bool doRandomQueries = false;
|
||||
bool doRestart = false;
|
||||
bool doThreadJoin = false;
|
||||
int noQueries = 0;
|
||||
|
||||
while((c = getopt(argc, argv,"rjp:b:u:q:")) != -1)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case 'r':
|
||||
doRestart = true;
|
||||
break;
|
||||
case 'j':
|
||||
doThreadJoin = true;
|
||||
break;
|
||||
case 'p':
|
||||
{
|
||||
int tmp_port = atoi(optarg);
|
||||
if ((tmp_port > MIN_DEF_PORT) && (tmp_port < MAX_DEF_PORT))
|
||||
{
|
||||
port = tmp_port;
|
||||
std::cerr << "Port: " << port;
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Invalid Port";
|
||||
std::cerr << std::endl;
|
||||
args(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
case 'b':
|
||||
{
|
||||
bootfile = optarg;
|
||||
std::cerr << "Bootfile: " << bootfile;
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
break;
|
||||
case 'u':
|
||||
{
|
||||
setUid = true;
|
||||
uid = optarg;
|
||||
std::cerr << "UID: " << uid;
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
break;
|
||||
case 'q':
|
||||
{
|
||||
doRandomQueries = true;
|
||||
noQueries = atoi(optarg);
|
||||
std::cerr << "Doing Random Queries";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
{
|
||||
args(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bdDhtFunctions *fns = new bdStdDht();
|
||||
|
||||
bdNodeId id;
|
||||
|
||||
/* start off with a random id! */
|
||||
bdStdRandomNodeId(&id);
|
||||
|
||||
if (setUid)
|
||||
{
|
||||
int len = uid.size();
|
||||
if (len > 20)
|
||||
{
|
||||
len = 20;
|
||||
}
|
||||
|
||||
for(int i = 0; i < len; i++)
|
||||
{
|
||||
id.data[i] = uid[i];
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << "Using NodeId: ";
|
||||
fns->bdPrintNodeId(std::cerr, &id);
|
||||
std::cerr << std::endl;
|
||||
|
||||
/* setup the udp port */
|
||||
struct sockaddr_in local;
|
||||
memset(&local, 0, sizeof(local));
|
||||
local.sin_family = AF_INET;
|
||||
local.sin_addr.s_addr = 0;
|
||||
local.sin_port = htons(port);
|
||||
UdpStack *udpstack = new UdpStack(local);
|
||||
|
||||
/* create bitdht component */
|
||||
std::string dhtVersion = "dbTEST";
|
||||
UdpBitDht *bitdht = new UdpBitDht(udpstack, &id, dhtVersion, bootfile, 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;
|
||||
int running = 1;
|
||||
|
||||
std::cerr << "Starting Dht: ";
|
||||
std::cerr << std::endl;
|
||||
bitdht->startDht();
|
||||
|
||||
|
||||
if (doRandomQueries)
|
||||
{
|
||||
for(int i = 0; i < noQueries; i++)
|
||||
{
|
||||
bdNodeId rndId;
|
||||
bdStdRandomNodeId(&rndId);
|
||||
|
||||
std::cerr << "BitDht Launching Random Search: ";
|
||||
bdStdPrintNodeId(std::cerr, &rndId);
|
||||
std::cerr << std::endl;
|
||||
|
||||
bitdht->addFindNode(&rndId, mode);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
while(1)
|
||||
{
|
||||
sleep(60);
|
||||
|
||||
std::cerr << "BitDht State: ";
|
||||
std::cerr << bitdht->stateDht();
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "Dht Network Size: ";
|
||||
std::cerr << bitdht->statsNetworkSize();
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "BitDht Network Size: ";
|
||||
std::cerr << bitdht->statsBDVersionSize();
|
||||
std::cerr << std::endl;
|
||||
|
||||
if (++count == 2)
|
||||
{
|
||||
/* switch to one-shot searchs */
|
||||
mode = 0;
|
||||
}
|
||||
|
||||
if (doThreadJoin)
|
||||
{
|
||||
/* change address */
|
||||
if (count % 2 == 0)
|
||||
{
|
||||
|
||||
std::cerr << "Resetting UdpStack: ";
|
||||
std::cerr << std::endl;
|
||||
|
||||
udpstack->resetAddress(local);
|
||||
}
|
||||
}
|
||||
if (doRestart)
|
||||
{
|
||||
if (count % 2 == 1)
|
||||
{
|
||||
if (running)
|
||||
{
|
||||
|
||||
std::cerr << "Stopping Dht: ";
|
||||
std::cerr << std::endl;
|
||||
|
||||
bitdht->stopDht();
|
||||
running = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Starting Dht: ";
|
||||
std::cerr << std::endl;
|
||||
|
||||
bitdht->startDht();
|
||||
running = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
23
libbitdht/src/tests/utest.h
Normal file
23
libbitdht/src/tests/utest.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef _UNIT_TEST_MACROS_H__
|
||||
#define _UNIT_TEST_MACROS_H__
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define TFAILURE( s ) printf( "FAILURE: " __FILE__ ":%-4d %s\n", __LINE__, s )
|
||||
#define TSUCCESS( s ) printf( "SUCCESS: " __FILE__ ":%-4d %s\n", __LINE__, s )
|
||||
|
||||
/* careful with this line (no protection) */
|
||||
#define INITTEST() int ok = 1; int gok = 1;
|
||||
|
||||
/* declare the variables */
|
||||
extern int ok;
|
||||
extern int gok;
|
||||
|
||||
#define CHECK( b ) do { if ( ! (b) ) { ok = 0; TFAILURE( #b ); } } while(0)
|
||||
#define FAILED( s ) do { ok = 0; TFAILURE( s ); } while(0)
|
||||
#define REPORT( s ) do { if ( ! (ok) ) { ok = 0; TFAILURE( s ); } else { TSUCCESS( s );} gok &= ok; ok = 1; } while(0)
|
||||
#define REPORT2( b, s ) do { if ( ! (b) ) { ok = 0; TFAILURE( s ); } else { TSUCCESS( s );} gok &= ok; ok = 1; } while(0)
|
||||
#define FINALREPORT( s ) do { gok &= ok; ok = 1; if ( ! (gok) ) { TFAILURE( s ); } else { TSUCCESS( s );} } while(0)
|
||||
#define TESTRESULT() (!gok)
|
||||
|
||||
#endif
|
306
libbitdht/src/udp/udpbitdht.cc
Normal file
306
libbitdht/src/udp/udpbitdht.cc
Normal file
@ -0,0 +1,306 @@
|
||||
/*
|
||||
* 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 "util/bdnet.h"
|
||||
|
||||
/*
|
||||
* #define DEBUG_UDP_BITDHT 1
|
||||
*
|
||||
* #define BITDHT_VERSION_ANONYMOUS 1
|
||||
*/
|
||||
|
||||
//#define DEBUG_UDP_BITDHT 1
|
||||
|
||||
#define BITDHT_VERSION_IDENTIFER 1
|
||||
|
||||
// Original RS 0.5.0/0.5.1 version, is un-numbered.
|
||||
#define BITDHT_VERSION "00" // First Release of BitDHT with Connections (Proxy Support + Dht Stun)
|
||||
//#define BITDHT_VERSION "01" // Potentially... Connections + Full DHT implementation. (TBD)
|
||||
/*************************************/
|
||||
|
||||
UdpBitDht::UdpBitDht(UdpPublisher *pub, bdNodeId *id, std::string appVersion, std::string bootstrapfile, bdDhtFunctions *fns)
|
||||
:UdpSubReceiver(pub), dhtMtx(true), mFns(fns)
|
||||
{
|
||||
std::string usedVersion;
|
||||
|
||||
#ifdef BITDHT_VERSION_IDENTIFER
|
||||
usedVersion = "BD";
|
||||
usedVersion += BITDHT_VERSION;
|
||||
#endif
|
||||
usedVersion += appVersion;
|
||||
|
||||
#ifdef BITDHT_VERSION_ANONYMOUS
|
||||
usedVersion = ""; /* blank it */
|
||||
#endif
|
||||
|
||||
/* setup nodeManager */
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
mBitDhtManager = new bdNodeManager(id, usedVersion, 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);
|
||||
}
|
||||
|
||||
void UdpBitDht::ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t start)
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
mBitDhtManager->ConnectionRequest(laddr, target, mode, start);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void UdpBitDht::ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t answer)
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
mBitDhtManager->ConnectionAuth(srcId, proxyId, destId, mode, loc, answer);
|
||||
}
|
||||
|
||||
void UdpBitDht::ConnectionOptions(uint32_t allowedModes, uint32_t flags)
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
mBitDhtManager->ConnectionOptions(allowedModes, flags);
|
||||
}
|
||||
|
||||
|
||||
int UdpBitDht::getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &from)
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
return mBitDhtManager->getDhtPeerAddress(id, from);
|
||||
}
|
||||
|
||||
int UdpBitDht::getDhtValue(const bdNodeId *id, std::string key, std::string &value)
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
return mBitDhtManager->getDhtValue(id, key, value);
|
||||
}
|
||||
|
||||
int UdpBitDht::getDhtBucket(const int idx, bdBucket &bucket)
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
return mBitDhtManager->getDhtBucket(idx, bucket);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int UdpBitDht::getDhtQueries(std::map<bdNodeId, bdQueryStatus> &queries)
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
return mBitDhtManager->getDhtQueries(queries);
|
||||
}
|
||||
|
||||
int UdpBitDht::getDhtQueryStatus(const bdNodeId *id, bdQuerySummary &query)
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
return mBitDhtManager->getDhtQueryStatus(id, query);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* stats and Dht state */
|
||||
int UdpBitDht:: startDht()
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
return mBitDhtManager->startDht();
|
||||
}
|
||||
|
||||
int UdpBitDht:: stopDht()
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
return mBitDhtManager->stopDht();
|
||||
}
|
||||
|
||||
int UdpBitDht::stateDht()
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
return mBitDhtManager->stateDht();
|
||||
}
|
||||
|
||||
uint32_t UdpBitDht::statsNetworkSize()
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
return mBitDhtManager->statsNetworkSize();
|
||||
}
|
||||
|
||||
uint32_t UdpBitDht::statsBDVersionSize()
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
return mBitDhtManager->statsBDVersionSize();
|
||||
}
|
||||
|
||||
/******************* 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 ***/
|
||||
#define MAX_MSG_PER_TICK 100
|
||||
#define TICK_PAUSE_USEC 20000 /* 20ms secs .. max messages = 50 x 100 = 5000 */
|
||||
|
||||
void UdpBitDht::run()
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
while(tick())
|
||||
{
|
||||
usleep(TICK_PAUSE_USEC);
|
||||
}
|
||||
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
mBitDhtManager->iteration();
|
||||
}
|
||||
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;
|
||||
|
||||
while((i < MAX_MSG_PER_TICK) && (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!
|
||||
}
|
||||
|
||||
if (i == MAX_MSG_PER_TICK)
|
||||
{
|
||||
return 1; /* keep on ticking */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
112
libbitdht/src/udp/udpbitdht.h
Normal file
112
libbitdht/src/udp/udpbitdht.h
Normal file
@ -0,0 +1,112 @@
|
||||
#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);
|
||||
|
||||
/***** Connections Requests *****/
|
||||
virtual void ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t start);
|
||||
virtual void ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t answer);
|
||||
virtual void ConnectionOptions(uint32_t allowedModes, uint32_t flags);
|
||||
|
||||
/***** Get Results Details *****/
|
||||
virtual int getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &from);
|
||||
virtual int getDhtValue(const bdNodeId *id, std::string key, std::string &value);
|
||||
virtual int getDhtBucket(const int idx, bdBucket &bucket);
|
||||
|
||||
virtual int getDhtQueries(std::map<bdNodeId, bdQueryStatus> &queries);
|
||||
virtual int getDhtQueryStatus(const bdNodeId *id, bdQuerySummary &query);
|
||||
|
||||
/* stats and Dht state */
|
||||
virtual int startDht();
|
||||
virtual int stopDht();
|
||||
virtual int stateDht();
|
||||
virtual uint32_t statsNetworkSize();
|
||||
virtual uint32_t statsBDVersionSize();
|
||||
|
||||
/******************* 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
|
668
libbitdht/src/udp/udplayer.cc
Normal file
668
libbitdht/src/udp/udplayer.cc
Normal file
@ -0,0 +1,668 @@
|
||||
/*
|
||||
* udp/udplayer.cc
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2004-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/udplayer.h"
|
||||
#include "util/bdrandom.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/***
|
||||
* #define UDP_ENABLE_BROADCAST 1
|
||||
* #define UDP_LOOPBACK_TESTING 1
|
||||
* #define DEBUG_UDP_LAYER 1
|
||||
***/
|
||||
|
||||
//#define DEBUG_UDP_LAYER 1
|
||||
|
||||
static const int UDP_DEF_TTL = 64;
|
||||
|
||||
/* NB: This #define makes the listener open 0.0.0.0:X port instead
|
||||
* of a specific port - this helps library communicate on systems
|
||||
* with multiple interfaces or unique network setups.
|
||||
*
|
||||
* - It should always be used!
|
||||
*
|
||||
* #define OPEN_UNIVERSAL_PORT 1
|
||||
*
|
||||
*/
|
||||
|
||||
#define OPEN_UNIVERSAL_PORT 1
|
||||
|
||||
|
||||
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)
|
||||
std::ostream &operator<<(std::ostream &out, 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)
|
||||
{
|
||||
#ifdef DEBUG_UDP_LAYER
|
||||
std::cerr << "UdpLayer::reset()" << std::endl;
|
||||
#endif
|
||||
|
||||
/* stop the old thread */
|
||||
{
|
||||
bdStackMutex stack(sockMtx); /********** LOCK MUTEX *********/
|
||||
#ifdef DEBUG_UDP_LAYER
|
||||
std::cerr << "UdpLayer::reset() setting stopThread flag" << std::endl;
|
||||
#endif
|
||||
stopThread = true;
|
||||
}
|
||||
#ifdef DEBUG_UDP_LAYER
|
||||
std::cerr << "UdpLayer::reset() joining" << std::endl;
|
||||
#endif
|
||||
|
||||
join();
|
||||
|
||||
#ifdef DEBUG_UDP_LAYER
|
||||
std::cerr << "UdpLayer::reset() closing socket" << std::endl;
|
||||
#endif
|
||||
closeSocket();
|
||||
|
||||
#ifdef DEBUG_UDP_LAYER
|
||||
std::cerr << "UdpLayer::reset() resetting variables" << std::endl;
|
||||
#endif
|
||||
laddr = local;
|
||||
errorState = 0;
|
||||
ttl = UDP_DEF_TTL;
|
||||
|
||||
#ifdef DEBUG_UDP_LAYER
|
||||
std::cerr << "UdpLayer::reset() opening socket" << std::endl;
|
||||
#endif
|
||||
openSocket();
|
||||
|
||||
return 1 ;
|
||||
}
|
||||
|
||||
|
||||
int UdpLayer::closeSocket()
|
||||
{
|
||||
/* close socket if open */
|
||||
sockMtx.lock(); /********** LOCK MUTEX *********/
|
||||
|
||||
if (sockfd > 0)
|
||||
{
|
||||
bdnet_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)
|
||||
{
|
||||
fd_set rset;
|
||||
for(;;)
|
||||
{
|
||||
/* check if we need to stop */
|
||||
bool toStop = false;
|
||||
{
|
||||
bdStackMutex stack(sockMtx); /********** LOCK MUTEX *********/
|
||||
toStop = stopThread;
|
||||
}
|
||||
|
||||
if (toStop)
|
||||
{
|
||||
#ifdef DEBUG_UDP_LAYER
|
||||
std::cerr << "UdpLayer::recv_loop() stopping thread" << std::endl;
|
||||
#endif
|
||||
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)
|
||||
{
|
||||
#ifdef DEBUG_UDP_LAYER
|
||||
std::cerr << "UdpLayer::recv_loop() Error: " << bdnet_errno() << std::endl;
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
free(inbuf) ;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int UdpLayer::sendPkt(const void *data, int size, const 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 = bdnet_socket(PF_INET, SOCK_DGRAM, 0);
|
||||
#ifdef DEBUG_UDP_LAYER
|
||||
std::cerr << "UpdStreamer::openSocket()" << std::endl;
|
||||
#endif
|
||||
/* bind to address */
|
||||
|
||||
|
||||
#ifdef UDP_LOOPBACK_TESTING
|
||||
bdnet_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 != bdnet_bind(sockfd, (struct sockaddr *) (&tmpaddr), sizeof(tmpaddr)))
|
||||
#else
|
||||
if (0 != bdnet_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: " << bdnet_errno() << std::endl;
|
||||
#endif
|
||||
errorState = EADDRINUSE;
|
||||
//exit(1);
|
||||
|
||||
sockMtx.unlock(); /******** UNLOCK MUTEX *********/
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (-1 == bdnet_fcntl(sockfd, F_SETFL, O_NONBLOCK))
|
||||
{
|
||||
#ifdef DEBUG_UDP_LAYER
|
||||
std::cerr << "Failed to Make Non-Blocking" << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef UDP_ENABLE_BROADCAST
|
||||
/* 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
|
||||
}
|
||||
#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 = bdnet_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 = bdnet_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, const 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 *********/
|
||||
|
||||
bdnet_sendto(sockfd, data, size, 0,
|
||||
(struct sockaddr *) &(toaddr),
|
||||
sizeof(toaddr));
|
||||
|
||||
sockMtx.unlock(); /******** UNLOCK MUTEX *********/
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**************************** LossyUdpLayer - for Testing **************/
|
||||
|
||||
|
||||
LossyUdpLayer::LossyUdpLayer(UdpReceiver *udpr,
|
||||
struct sockaddr_in &local, double frac)
|
||||
:UdpLayer(udpr, local), lossFraction(frac)
|
||||
{
|
||||
return;
|
||||
}
|
||||
LossyUdpLayer::~LossyUdpLayer() { return; }
|
||||
|
||||
int LossyUdpLayer::receiveUdpPacket(void *data, int *size, struct sockaddr_in &from)
|
||||
{
|
||||
if (0 < UdpLayer::receiveUdpPacket(data, size, from))
|
||||
{
|
||||
float prob = bdRandom::random_f32();
|
||||
if (prob < lossFraction)
|
||||
{
|
||||
/* discard */
|
||||
std::cerr << "LossyUdpLayer::receiveUdpPacket() Dropping packet!";
|
||||
std::cerr << std::endl;
|
||||
std::cerr << printPkt(data, *size);
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "LossyUdpLayer::receiveUdpPacket() Packet Dropped!";
|
||||
std::cerr << std::endl;
|
||||
|
||||
size = 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return *size;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int LossyUdpLayer::sendUdpPacket(const void *data, int size, struct sockaddr_in &to)
|
||||
{
|
||||
double prob = (1.0 * (rand() / (RAND_MAX + 1.0)));
|
||||
|
||||
if (prob < lossFraction)
|
||||
{
|
||||
/* discard */
|
||||
|
||||
std::cerr << "LossyUdpLayer::sendUdpPacket() Dropping packet!";
|
||||
std::cerr << std::endl;
|
||||
std::cerr << printPkt((void *) data, size);
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "LossyUdpLayer::sendUdpPacket() Packet Dropped!";
|
||||
std::cerr << std::endl;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
// otherwise read normally;
|
||||
return UdpLayer::sendUdpPacket(data, size, to);
|
||||
}
|
||||
|
||||
/**************************** LossyUdpLayer - for Testing **************/
|
||||
|
||||
PortRange::PortRange() :lport(0), uport(0) { return; }
|
||||
PortRange::PortRange(uint16_t lp, uint16_t up) :lport(lp), uport(up) { return; }
|
||||
|
||||
bool PortRange::inRange(uint16_t port)
|
||||
{
|
||||
if (port < lport)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (port > uport)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
RestrictedUdpLayer::RestrictedUdpLayer(UdpReceiver *udpr,
|
||||
struct sockaddr_in &local)
|
||||
:UdpLayer(udpr, local)
|
||||
{
|
||||
return;
|
||||
}
|
||||
RestrictedUdpLayer::~RestrictedUdpLayer() { return; }
|
||||
|
||||
void RestrictedUdpLayer::addRestrictedPortRange(int lp, int up)
|
||||
{
|
||||
PortRange pr(lp, up);
|
||||
mLostPorts.push_back(pr);
|
||||
}
|
||||
|
||||
int RestrictedUdpLayer::receiveUdpPacket(void *data, int *size, struct sockaddr_in &from)
|
||||
{
|
||||
if (0 < UdpLayer::receiveUdpPacket(data, size, from))
|
||||
{
|
||||
/* check the port against list */
|
||||
uint16_t inPort = ntohs(from.sin_port);
|
||||
|
||||
std::list<PortRange>::iterator it;
|
||||
for(it = mLostPorts.begin(); it != mLostPorts.end(); it++)
|
||||
{
|
||||
if (it->inRange(inPort))
|
||||
{
|
||||
#ifdef DEBUG_UDP_LAYER
|
||||
std::cerr << "RestrictedUdpLayer::receiveUdpPacket() Dropping packet";
|
||||
std::cerr << ", Port(" << inPort << ") in restricted range!";
|
||||
std::cerr << std::endl;
|
||||
//std::cerr << printPkt(data, *size);
|
||||
//std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
size = 0;
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
/* acceptable port */
|
||||
return *size;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int RestrictedUdpLayer::sendUdpPacket(const void *data, int size, struct sockaddr_in &to)
|
||||
{
|
||||
/* check the port against list */
|
||||
uint16_t outPort = ntohs(to.sin_port);
|
||||
|
||||
std::list<PortRange>::iterator it;
|
||||
for(it = mLostPorts.begin(); it != mLostPorts.end(); it++)
|
||||
{
|
||||
if (it->inRange(outPort))
|
||||
{
|
||||
/* drop */
|
||||
#ifdef DEBUG_UDP_LAYER
|
||||
std::cerr << "RestrictedUdpLayer::sendUdpPacket() Dropping packet";
|
||||
std::cerr << ", Port(" << outPort << ") in restricted range!";
|
||||
std::cerr << std::endl;
|
||||
//std::cerr << printPkt(data, *size);
|
||||
//std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// otherwise read normally;
|
||||
return UdpLayer::sendUdpPacket(data, size, to);
|
||||
}
|
||||
|
168
libbitdht/src/udp/udplayer.h
Normal file
168
libbitdht/src/udp/udplayer.h
Normal file
@ -0,0 +1,168 @@
|
||||
#ifndef BITDHT_UDP_LAYER_H
|
||||
#define BITDHT_UDP_LAYER_H
|
||||
|
||||
/*
|
||||
* udp/udplayer.h
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2004-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 "util/bdthreads.h"
|
||||
#include "util/bdnet.h"
|
||||
|
||||
#include <iosfwd>
|
||||
#include <list>
|
||||
#include <deque>
|
||||
|
||||
/* careful - duplicate definitions */
|
||||
//std::ostream &operator<<(std::ostream &out, const struct sockaddr_in &addr);
|
||||
std::ostream &operator<<(std::ostream &out, 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, const 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, const 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, const 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;
|
||||
};
|
||||
|
||||
|
||||
/* For Testing - drops packets */
|
||||
class LossyUdpLayer: public UdpLayer
|
||||
{
|
||||
public:
|
||||
LossyUdpLayer(UdpReceiver *udpr, struct sockaddr_in &local, double frac);
|
||||
virtual ~LossyUdpLayer();
|
||||
|
||||
protected:
|
||||
|
||||
virtual int receiveUdpPacket(void *data, int *size, struct sockaddr_in &from);
|
||||
virtual int sendUdpPacket(const void *data, int size, struct sockaddr_in &to);
|
||||
|
||||
double lossFraction;
|
||||
};
|
||||
|
||||
class PortRange
|
||||
{
|
||||
public:
|
||||
PortRange();
|
||||
PortRange(uint16_t lp, uint16_t up);
|
||||
|
||||
bool inRange(uint16_t port);
|
||||
|
||||
uint16_t lport;
|
||||
uint16_t uport;
|
||||
};
|
||||
|
||||
|
||||
/* For Testing - drops packets */
|
||||
class RestrictedUdpLayer: public UdpLayer
|
||||
{
|
||||
public:
|
||||
RestrictedUdpLayer(UdpReceiver *udpr, struct sockaddr_in &local);
|
||||
virtual ~RestrictedUdpLayer();
|
||||
|
||||
void addRestrictedPortRange(int lp, int up);
|
||||
|
||||
protected:
|
||||
|
||||
virtual int receiveUdpPacket(void *data, int *size, struct sockaddr_in &from);
|
||||
virtual int sendUdpPacket(const void *data, int size, struct sockaddr_in &to);
|
||||
|
||||
std::list<PortRange> mLostPorts;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
242
libbitdht/src/udp/udpstack.cc
Normal file
242
libbitdht/src/udp/udpstack.cc
Normal file
@ -0,0 +1,242 @@
|
||||
/*
|
||||
* udp/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 "udp/udpstack.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <string.h>
|
||||
#include <stdlib.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;
|
||||
}
|
||||
|
||||
UdpStack::UdpStack(int testmode, struct sockaddr_in &local)
|
||||
:udpLayer(NULL), laddr(local)
|
||||
{
|
||||
std::cerr << "UdpStack::UdpStack() Evoked in TestMode" << std::endl;
|
||||
if (testmode == UDP_TEST_LOSSY_LAYER)
|
||||
{
|
||||
std::cerr << "UdpStack::UdpStack() Installing LossyUdpLayer" << std::endl;
|
||||
udpLayer = new LossyUdpLayer(this, laddr, UDP_TEST_LOSSY_FRAC);
|
||||
}
|
||||
else if (testmode == UDP_TEST_RESTRICTED_LAYER)
|
||||
{
|
||||
std::cerr << "UdpStack::UdpStack() Installing RestrictedUdpLayer" << std::endl;
|
||||
udpLayer = new RestrictedUdpLayer(this, laddr);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "UdpStack::UdpStack() Installing Standard UdpLayer" << std::endl;
|
||||
// standard layer
|
||||
openSocket();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
UdpLayer *UdpStack::getUdpLayer() /* for testing only */
|
||||
{
|
||||
return udpLayer;
|
||||
}
|
||||
|
||||
bool UdpStack::resetAddress(struct sockaddr_in &local)
|
||||
{
|
||||
std::cerr << "UdpStack::resetAddress(" << local << ")";
|
||||
std::cerr << std::endl;
|
||||
|
||||
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, const 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, const 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);
|
||||
}
|
||||
|
||||
|
117
libbitdht/src/udp/udpstack.h
Normal file
117
libbitdht/src/udp/udpstack.h
Normal file
@ -0,0 +1,117 @@
|
||||
#ifndef BITDHT_UDP_STACK_RECEIVER_H
|
||||
#define BITDHT_UDP_STACK_RECEIVER_H
|
||||
|
||||
/*
|
||||
* udp/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 "util/bdthreads.h"
|
||||
#include "util/bdnet.h"
|
||||
|
||||
#include <iosfwd>
|
||||
#include <list>
|
||||
#include <deque>
|
||||
|
||||
#include <iosfwd>
|
||||
#include <map>
|
||||
|
||||
#include "udp/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, const 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;
|
||||
};
|
||||
|
||||
|
||||
#define UDP_TEST_LOSSY_LAYER 1
|
||||
#define UDP_TEST_RESTRICTED_LAYER 2
|
||||
|
||||
#define UDP_TEST_LOSSY_FRAC (0.10)
|
||||
|
||||
class UdpStack: public UdpReceiver, public UdpPublisher
|
||||
{
|
||||
public:
|
||||
|
||||
UdpStack(struct sockaddr_in &local);
|
||||
UdpStack(int testmode, struct sockaddr_in &local);
|
||||
virtual ~UdpStack() { return; }
|
||||
|
||||
UdpLayer *getUdpLayer(); /* for testing only */
|
||||
|
||||
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, const 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
|
383
libbitdht/src/util/bdbloom.cc
Normal file
383
libbitdht/src/util/bdbloom.cc
Normal file
@ -0,0 +1,383 @@
|
||||
/*
|
||||
* bitdht/bdbloom.cc
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2011 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 "util/bdbloom.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <malloc.h>
|
||||
|
||||
/* Bloom Filter implementation */
|
||||
|
||||
|
||||
bloomFilter::bloomFilter(int m, int k)
|
||||
{
|
||||
mBits.resize(m);
|
||||
mHashFns.resize(k);
|
||||
|
||||
mFilterBits = m;
|
||||
mNoHashs = k;
|
||||
mNoElements = 0;
|
||||
|
||||
int i;
|
||||
for(i = 0; i < m; i++)
|
||||
{
|
||||
mBits[i] = 0;
|
||||
}
|
||||
|
||||
for(i = 0; i < k; i++)
|
||||
{
|
||||
mHashFns[i] = NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
uint8_t convertCharToUint8(char ch1, char ch2)
|
||||
{
|
||||
uint8_t value1 = 0;
|
||||
uint8_t value2 = 0;
|
||||
|
||||
/* do char1 */
|
||||
if (ch1 >= '0' && ch1 <= '9')
|
||||
value1 = (ch1 - '0');
|
||||
else if (ch1 >= 'A' && ch1 <= 'F')
|
||||
value1 = (ch1 - 'A' + 10);
|
||||
else if (ch1 >= 'a' && ch1 <= 'f')
|
||||
value1 = (ch1 - 'a' + 10);
|
||||
|
||||
/* do char2 */
|
||||
if (ch2 >= '0' && ch2 <= '9')
|
||||
value2 = (ch2 - '0');
|
||||
else if (ch2 >= 'A' && ch2 <= 'F')
|
||||
value2 = (ch2 - 'A' + 10);
|
||||
else if (ch2 >= 'a' && ch2 <= 'f')
|
||||
value2 = (ch2 - 'a' + 10);
|
||||
|
||||
uint8_t output = (value1 << 4) + value2;
|
||||
return output;
|
||||
}
|
||||
|
||||
#define BITS_PER_BYTE (8)
|
||||
|
||||
int bloomFilter::setFilterBits(const std::string &hex)
|
||||
{
|
||||
int bytes = (mFilterBits / BITS_PER_BYTE);
|
||||
if (mFilterBits % BITS_PER_BYTE)
|
||||
{
|
||||
bytes++;
|
||||
}
|
||||
|
||||
if (hex.size() < bytes * 2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// convert to binary array.
|
||||
uint8_t *tmparray = (uint8_t *) malloc(bytes);
|
||||
int i = 0;
|
||||
|
||||
for(i = 0; i < bytes; i++)
|
||||
{
|
||||
tmparray[i] = convertCharToUint8(hex[2 * i], hex[2 * i + 1]);
|
||||
}
|
||||
|
||||
|
||||
for(i = 0; i < mFilterBits; i++)
|
||||
{
|
||||
int byte = i / BITS_PER_BYTE;
|
||||
int bit = i % BITS_PER_BYTE;
|
||||
uint8_t value = (tmparray[byte] & (1 << bit));
|
||||
|
||||
if (value)
|
||||
{
|
||||
mBits[i] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
mBits[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
free(tmparray);
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string bloomFilter::getFilter()
|
||||
{
|
||||
/* extract filter as a hex string */
|
||||
std::string output;
|
||||
int bytes = (mFilterBits / BITS_PER_BYTE);
|
||||
if (mFilterBits % BITS_PER_BYTE)
|
||||
{
|
||||
bytes++;
|
||||
}
|
||||
|
||||
// convert to binary array.
|
||||
uint8_t *tmparray = (uint8_t *) malloc(bytes);
|
||||
int i,j;
|
||||
|
||||
for(i = 0; i < bytes; i++)
|
||||
{
|
||||
tmparray[i] = 0;
|
||||
for(j = 0; j < BITS_PER_BYTE; j++)
|
||||
{
|
||||
int bit = i * BITS_PER_BYTE + j;
|
||||
if (mBits[bit])
|
||||
{
|
||||
tmparray[i] |= (1 << j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::ostringstream out;
|
||||
for(int i = 0; i < bytes; i++)
|
||||
{
|
||||
out << std::setw(2) << std::setfill('0') << std::hex << (uint32_t) (tmparray)[i];
|
||||
}
|
||||
|
||||
free(tmparray);
|
||||
|
||||
return out.str();
|
||||
}
|
||||
|
||||
void bloomFilter::setBit(int bit)
|
||||
{
|
||||
mBits[bit] = 1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool bloomFilter::isBitSet(int bit)
|
||||
{
|
||||
return (mBits[bit] == 1);
|
||||
}
|
||||
|
||||
uint32_t bloomFilter::filterBits()
|
||||
{
|
||||
return mFilterBits;
|
||||
}
|
||||
|
||||
uint32_t bloomFilter::countBits()
|
||||
{
|
||||
int count = 0;
|
||||
int i;
|
||||
for(i = 0; i < mFilterBits; i++)
|
||||
{
|
||||
if (mBits[i])
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
void bloomFilter::printFilter(std::ostream &out)
|
||||
{
|
||||
out << "bloomFilter: m = " << mFilterBits;
|
||||
out << " k = " << mNoHashs;
|
||||
out << " n = " << mNoElements;
|
||||
out << std::endl;
|
||||
|
||||
out << "BITS: ";
|
||||
int i;
|
||||
for(i = 0; i < mFilterBits; i++)
|
||||
{
|
||||
if ((i > 0) && (i % 32 == 0))
|
||||
{
|
||||
out << std::endl;
|
||||
out << "BITS: ";
|
||||
}
|
||||
if (mBits[i])
|
||||
{
|
||||
out << "1";
|
||||
}
|
||||
else
|
||||
{
|
||||
out << "0";
|
||||
}
|
||||
}
|
||||
out << std::endl;
|
||||
out << "STR: " << getFilter();
|
||||
out << std::endl;
|
||||
}
|
||||
|
||||
void bloomFilter::setHashFunction(int idx, uint32_t (*hashfn)(const std::string &))
|
||||
{
|
||||
mHashFns[idx] = hashfn;
|
||||
}
|
||||
|
||||
void bloomFilter::add(const std::string &hex)
|
||||
{
|
||||
uint32_t (*hashfn)(const std::string &);
|
||||
int i;
|
||||
for(i = 0; i < mNoHashs; i++)
|
||||
{
|
||||
hashfn = mHashFns[i];
|
||||
|
||||
int bit = hashfn(hex);
|
||||
|
||||
setBit(bit);
|
||||
}
|
||||
|
||||
mNoElements++;
|
||||
|
||||
}
|
||||
|
||||
bool bloomFilter::test(const std::string &hex)
|
||||
{
|
||||
uint32_t (*hashfn)(const std::string &);
|
||||
int i;
|
||||
for(i = 0; i < mNoHashs; i++)
|
||||
{
|
||||
hashfn = mHashFns[i];
|
||||
|
||||
int bit = hashfn(hex);
|
||||
|
||||
if (!isBitSet(bit))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t getFirst10BitsAsNumber(const std::string &input)
|
||||
{
|
||||
if (input.size() < 8)
|
||||
{
|
||||
std::cerr << "getFirst10BitsAsNumber() ERROR Size too small!";
|
||||
std::cerr << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t data[4];
|
||||
|
||||
data[0] = convertCharToUint8(input[0], input[1]);
|
||||
data[1] = convertCharToUint8(input[2], input[3]);
|
||||
data[2] = convertCharToUint8(input[4], input[5]);
|
||||
data[3] = convertCharToUint8(input[6], input[7]);
|
||||
|
||||
uint32_t val = ((data[0] & 0xff) << 2) + ((data[1] & 0xc0) >> 6);
|
||||
|
||||
#ifdef DEBUG_BLOOM
|
||||
std::cerr << "getFirst10BitsAsNumber() input: " << input;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "getFirst10BitsAsNumber() ";
|
||||
std::cerr << " data[0]: " << std::hex << (uint32_t) data[0];
|
||||
std::cerr << " data[1]: " << (uint32_t) data[1];
|
||||
std::cerr << " data[2]: " << (uint32_t) data[2];
|
||||
std::cerr << " data[3]: " << (uint32_t) data[3];
|
||||
std::cerr << " val: " << std::dec << (uint32_t) val;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
uint32_t getSecond10BitsAsNumber(const std::string &input)
|
||||
{
|
||||
if (input.size() < 8)
|
||||
{
|
||||
std::cerr << "getSecond10BitsAsNumber() ERROR Size too small!";
|
||||
std::cerr << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t data[4];
|
||||
|
||||
data[0] = convertCharToUint8(input[0], input[1]);
|
||||
data[1] = convertCharToUint8(input[2], input[3]);
|
||||
data[2] = convertCharToUint8(input[4], input[5]);
|
||||
data[3] = convertCharToUint8(input[6], input[7]);
|
||||
|
||||
uint32_t val = ((data[1] & 0x3f) << 4) + ((data[2] & 0xf0) >> 4);
|
||||
|
||||
#ifdef DEBUG_BLOOM
|
||||
std::cerr << "getSecond10BitsAsNumber() input: " << input;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "getSecond10BitsAsNumber() ";
|
||||
std::cerr << " data[0]: " << std::hex << (uint32_t) data[0];
|
||||
std::cerr << " data[1]: " << (uint32_t) data[1];
|
||||
std::cerr << " data[2]: " << (uint32_t) data[2];
|
||||
std::cerr << " data[3]: " << (uint32_t) data[3];
|
||||
std::cerr << " val: " << std::dec << (uint32_t) val;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
uint32_t getMid10BitsAsNumber(const std::string &input)
|
||||
{
|
||||
if (input.size() < 8)
|
||||
{
|
||||
std::cerr << "getMid10BitsAsNumber() ERROR Size too small!";
|
||||
std::cerr << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t data[4];
|
||||
|
||||
data[0] = convertCharToUint8(input[0], input[1]);
|
||||
data[1] = convertCharToUint8(input[2], input[3]);
|
||||
data[2] = convertCharToUint8(input[4], input[5]);
|
||||
data[3] = convertCharToUint8(input[6], input[7]);
|
||||
|
||||
uint32_t val = ((data[0] & 0x07) << 7) + ((data[1] & 0x7f) >> 1);
|
||||
|
||||
#ifdef DEBUG_BLOOM
|
||||
std::cerr << "getMid10BitsAsNumber() input: " << input;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "getMid10BitsAsNumber() ";
|
||||
std::cerr << " data[0]: " << std::hex << (uint32_t) data[0];
|
||||
std::cerr << " data[1]: " << (uint32_t) data[1];
|
||||
std::cerr << " data[2]: " << (uint32_t) data[2];
|
||||
std::cerr << " data[3]: " << (uint32_t) data[3];
|
||||
std::cerr << " val: " << std::dec << (uint32_t) val;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
#define BDFILTER_M 1024
|
||||
#define BDFILTER_K 3
|
||||
|
||||
bdBloom::bdBloom()
|
||||
:bloomFilter(BDFILTER_M, BDFILTER_K)
|
||||
{
|
||||
/* set the fns. */
|
||||
setHashFunction(0, getFirst10BitsAsNumber);
|
||||
setHashFunction(1, getSecond10BitsAsNumber);
|
||||
setHashFunction(2, getMid10BitsAsNumber);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
79
libbitdht/src/util/bdbloom.h
Normal file
79
libbitdht/src/util/bdbloom.h
Normal file
@ -0,0 +1,79 @@
|
||||
#ifndef BITDHT_BLOOM_H
|
||||
#define BITDHT_BLOOM_H
|
||||
|
||||
/*
|
||||
* bitdht/bdbloom.h
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2011 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 <vector>
|
||||
#include <string>
|
||||
|
||||
#include <iostream>
|
||||
#include <inttypes.h>
|
||||
|
||||
|
||||
class bloomFilter
|
||||
{
|
||||
public:
|
||||
|
||||
bloomFilter(int m, int k);
|
||||
|
||||
int setFilterBits(const std::string &hex);
|
||||
std::string getFilter();
|
||||
|
||||
void printFilter(std::ostream &out);
|
||||
|
||||
bool test(const std::string &hex); // takes first m bits.
|
||||
void add(const std::string &hex);
|
||||
uint32_t countBits();
|
||||
uint32_t filterBits();
|
||||
|
||||
protected:
|
||||
void setHashFunction(int idx, uint32_t (*hashfn)(const std::string &));
|
||||
|
||||
private:
|
||||
void setBit(int bit);
|
||||
bool isBitSet(int bit);
|
||||
|
||||
std::vector<uint8_t> mBits;
|
||||
std::vector<uint32_t (*)(const std::string &)> mHashFns;
|
||||
|
||||
uint32_t mFilterBits;
|
||||
uint32_t mNoHashs;
|
||||
uint32_t mNoElements;
|
||||
};
|
||||
|
||||
|
||||
/* our specific implementation */
|
||||
class bdBloom: public bloomFilter
|
||||
{
|
||||
public:
|
||||
|
||||
bdBloom();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
359
libbitdht/src/util/bdnet.cc
Normal file
359
libbitdht/src/util/bdnet.cc
Normal file
@ -0,0 +1,359 @@
|
||||
|
||||
/*
|
||||
* util/bdnet.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 "bdnet.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
#if defined(_WIN32) || defined(__MINGW32__)
|
||||
|
||||
|
||||
/* error handling */
|
||||
int bdnet_int_errno;
|
||||
|
||||
int bdnet_errno()
|
||||
{
|
||||
return bdnet_int_errno;
|
||||
}
|
||||
|
||||
int bdnet_init()
|
||||
{
|
||||
std::cerr << "bdnet_init()" << std::endl;
|
||||
bdnet_int_errno = 0;
|
||||
|
||||
// Windows Networking Init.
|
||||
WORD wVerReq = MAKEWORD(2,2);
|
||||
WSADATA wsaData;
|
||||
|
||||
if (0 != WSAStartup(wVerReq, &wsaData))
|
||||
{
|
||||
std::cerr << "Failed to Startup Windows Networking";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Started Windows Networking";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* check if we can modify the TTL on a UDP packet */
|
||||
int bdnet_checkTTL(int fd)
|
||||
{
|
||||
std::cerr << "bdnet_checkTTL()" << std::endl;
|
||||
int optlen = 4;
|
||||
char optval[optlen];
|
||||
|
||||
int ret = getsockopt(fd, IPPROTO_IP, IP_TTL, optval, &optlen);
|
||||
//int ret = getsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, optval, &optlen);
|
||||
if (ret == SOCKET_ERROR)
|
||||
{
|
||||
ret = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
std::cerr << "bdnet_checkTTL() Failed!";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "bdnet_checkTTL() :";
|
||||
std::cerr << (int) optval[0] << ":";
|
||||
std::cerr << (int) optval[1] << ":";
|
||||
std::cerr << (int) optval[2] << ":";
|
||||
std::cerr << (int) optval[3] << ": RET: ";
|
||||
std::cerr << ret << ":";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bdnet_close(int fd)
|
||||
{
|
||||
std::cerr << "bdnet_close()" << std::endl;
|
||||
return closesocket(fd);
|
||||
}
|
||||
|
||||
int bdnet_socket(int domain, int type, int protocol)
|
||||
{
|
||||
int osock = socket(domain, type, protocol);
|
||||
std::cerr << "bdnet_socket()" << std::endl;
|
||||
|
||||
if ((unsigned) osock == INVALID_SOCKET)
|
||||
{
|
||||
// Invalidate socket Unix style.
|
||||
osock = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
}
|
||||
bdnet_checkTTL(osock);
|
||||
return osock;
|
||||
}
|
||||
|
||||
int bdnet_bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen)
|
||||
{
|
||||
std::cerr << "bdnet_bind()" << std::endl;
|
||||
int ret = bind(sockfd,my_addr,addrlen);
|
||||
if (ret != 0)
|
||||
{
|
||||
/* store unix-style error
|
||||
*/
|
||||
|
||||
ret = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bdnet_fcntl(int fd, int cmd, long arg)
|
||||
{
|
||||
int ret;
|
||||
|
||||
unsigned long int on = 1;
|
||||
std::cerr << "bdnet_fcntl()" << std::endl;
|
||||
|
||||
/* can only do NONBLOCK at the moment */
|
||||
if ((cmd != F_SETFL) || (arg != O_NONBLOCK))
|
||||
{
|
||||
std::cerr << "bdnet_fcntl() limited to fcntl(fd, F_SETFL, O_NONBLOCK)";
|
||||
std::cerr << std::endl;
|
||||
bdnet_int_errno = EOPNOTSUPP;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ioctlsocket(fd, FIONBIO, &on);
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
/* store unix-style error
|
||||
*/
|
||||
|
||||
ret = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bdnet_setsockopt(int s, int level, int optname,
|
||||
const void *optval, socklen_t optlen)
|
||||
{
|
||||
std::cerr << "bdnet_setsockopt() val:" << *((int *) optval) << std::endl;
|
||||
std::cerr << "bdnet_setsockopt() len:" << optlen << std::endl;
|
||||
if ((level != IPPROTO_IP) || (optname != IP_TTL))
|
||||
{
|
||||
std::cerr << "bdnet_setsockopt() limited to ";
|
||||
std::cerr << "setsockopt(fd, IPPROTO_IP, IP_TTL, ....)";
|
||||
std::cerr << std::endl;
|
||||
bdnet_int_errno = EOPNOTSUPP;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = setsockopt(s, level, optname, (const char *) optval, optlen);
|
||||
//int ret = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (const char *) optval, optlen);
|
||||
if (ret == SOCKET_ERROR)
|
||||
{
|
||||
ret = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
}
|
||||
bdnet_checkTTL(s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t bdnet_recvfrom(int s, void *buf, size_t len, int flags,
|
||||
struct sockaddr *from, socklen_t *fromlen)
|
||||
{
|
||||
std::cerr << "bdnet_recvfrom()" << std::endl;
|
||||
int ret = recvfrom(s, (char *) buf, len, flags, from, fromlen);
|
||||
if (ret == SOCKET_ERROR)
|
||||
{
|
||||
ret = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t bdnet_sendto(int s, const void *buf, size_t len, int flags,
|
||||
const struct sockaddr *to, socklen_t tolen)
|
||||
{
|
||||
std::cerr << "bdnet_sendto()" << std::endl;
|
||||
int ret = sendto(s, (const char *) buf, len, flags, to, tolen);
|
||||
if (ret == SOCKET_ERROR)
|
||||
{
|
||||
ret = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bdnet_w2u_errno(int err)
|
||||
{
|
||||
/* switch */
|
||||
std::cerr << "tou_net_w2u_errno(" << err << ")" << std::endl;
|
||||
switch(err)
|
||||
{
|
||||
case WSAEINPROGRESS:
|
||||
return EINPROGRESS;
|
||||
break;
|
||||
case WSAEWOULDBLOCK:
|
||||
return EINPROGRESS;
|
||||
break;
|
||||
case WSAENETUNREACH:
|
||||
return ENETUNREACH;
|
||||
break;
|
||||
case WSAETIMEDOUT:
|
||||
return ETIMEDOUT;
|
||||
break;
|
||||
case WSAEHOSTDOWN:
|
||||
return EHOSTDOWN;
|
||||
break;
|
||||
case WSAECONNREFUSED:
|
||||
return ECONNREFUSED;
|
||||
break;
|
||||
case WSAEADDRINUSE:
|
||||
return EADDRINUSE;
|
||||
break;
|
||||
case WSAEUSERS:
|
||||
return EUSERS;
|
||||
break;
|
||||
/* This one is returned for UDP recvfrom, when nothing there
|
||||
* but not a real error... translate into EINPROGRESS
|
||||
*/
|
||||
case WSAECONNRESET:
|
||||
std::cerr << "tou_net_w2u_errno(" << err << ")";
|
||||
std::cerr << " = WSAECONNRESET ---> EINPROGRESS";
|
||||
std::cerr << std::endl;
|
||||
return EINPROGRESS;
|
||||
break;
|
||||
/***
|
||||
*
|
||||
case WSAECONNRESET:
|
||||
return ECONNRESET;
|
||||
break;
|
||||
*
|
||||
***/
|
||||
|
||||
case WSANOTINITIALISED:
|
||||
std::cerr << "tou_net_w2u_errno(" << err << ") WSANOTINITIALISED. Fix Your Code!";
|
||||
std::cerr << std::endl;
|
||||
break;
|
||||
default:
|
||||
std::cerr << "tou_net_w2u_errno(" << err << ") Unknown";
|
||||
std::cerr << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
return ECONNREFUSED; /* sensible default? */
|
||||
}
|
||||
|
||||
int bdnet_inet_aton(const char *name, struct in_addr *addr)
|
||||
{
|
||||
return (((*addr).s_addr = inet_addr(name)) != INADDR_NONE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int sleep(unsigned int sec)
|
||||
{
|
||||
Sleep(sec * 1000);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int usleep(unsigned int usec)
|
||||
{
|
||||
Sleep(usec / 1000);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
#else // UNIX
|
||||
|
||||
/* Unix Version is easy -> just call the unix fn
|
||||
*/
|
||||
|
||||
#include <unistd.h> /* for close definition */
|
||||
|
||||
/* the universal interface */
|
||||
int bdnet_init() { return 0; }
|
||||
int bdnet_errno() { return errno; }
|
||||
|
||||
|
||||
/* check if we can modify the TTL on a UDP packet */
|
||||
int bdnet_checkTTL(int /*fd*/) { return 1;}
|
||||
|
||||
|
||||
int bdnet_inet_aton(const char *name, struct in_addr *addr)
|
||||
{
|
||||
return inet_aton(name, addr);
|
||||
}
|
||||
|
||||
int bdnet_close(int fd) { return close(fd); }
|
||||
|
||||
int bdnet_socket(int domain, int type, int protocol)
|
||||
{
|
||||
return socket(domain, type, protocol);
|
||||
}
|
||||
|
||||
int bdnet_bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen)
|
||||
{
|
||||
return bind(sockfd,my_addr,addrlen);
|
||||
}
|
||||
|
||||
int bdnet_fcntl(int fd, int cmd, long arg)
|
||||
{
|
||||
return fcntl(fd, cmd, arg);
|
||||
}
|
||||
|
||||
int bdnet_setsockopt(int s, int level, int optname,
|
||||
const void *optval, socklen_t optlen)
|
||||
{
|
||||
return setsockopt(s, level, optname, optval, optlen);
|
||||
}
|
||||
|
||||
ssize_t bdnet_recvfrom(int s, void *buf, size_t len, int flags,
|
||||
struct sockaddr *from, socklen_t *fromlen)
|
||||
{
|
||||
return recvfrom(s, buf, len, flags, from, fromlen);
|
||||
}
|
||||
|
||||
ssize_t bdnet_sendto(int s, const void *buf, size_t len, int flags,
|
||||
const struct sockaddr *to, socklen_t tolen)
|
||||
{
|
||||
return sendto(s, buf, len, flags, to, tolen);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
|
||||
|
||||
void bdsockaddr_clear(struct sockaddr_in *addr)
|
||||
{
|
||||
memset(addr, 0, sizeof(*addr));
|
||||
}
|
||||
|
||||
|
171
libbitdht/src/util/bdnet.h
Normal file
171
libbitdht/src/util/bdnet.h
Normal file
@ -0,0 +1,171 @@
|
||||
#ifndef BITDHT_UNIVERSAL_NETWORK_HEADER
|
||||
#define BITDHT_UNIVERSAL_NETWORK_HEADER
|
||||
/*
|
||||
*
|
||||
* util/bdnet.h
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2004-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 <inttypes.h>
|
||||
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
#if defined(_WIN32) || defined(__MINGW32__)
|
||||
|
||||
#include <windows.h>
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
#include <stdio.h> /* for ssize_t */
|
||||
typedef uint32_t in_addr_t;
|
||||
|
||||
#else // UNIX
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#endif
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
|
||||
|
||||
|
||||
/* C Interface */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*******
|
||||
* This defines a (unix-like) universal networking layer
|
||||
* that should function on both windows and unix. (C - interface)
|
||||
*
|
||||
* This is of course only a subset of the full interface.
|
||||
* functions required are:
|
||||
*
|
||||
* int bdnet_close(int fd);
|
||||
* int bdnet_socket(int domain, int type, int protocol);
|
||||
* int bdnet_bind(int sockfd, const struct sockaddr *my_addr,
|
||||
* socklen_t addrlen);
|
||||
* int bdnet_fcntl(int fd, int cmd, long arg);
|
||||
* int bdnet_setsockopt(int s, int level, int optname,
|
||||
* const void *optval, socklen_t optlen);
|
||||
* ssize_t bdnet_recvfrom(int s, void *buf, size_t len, int flags,
|
||||
* struct sockaddr *from, socklen_t *fromlen);
|
||||
* ssize_t bdnet_sendto(int s, const void *buf, size_t len, int flags,
|
||||
* const struct sockaddr *to, socklen_t tolen);
|
||||
*
|
||||
* There are some non-standard ones as well:
|
||||
* int bdnet_errno(); for internal networking errors
|
||||
* int bdnet_init(); required for windows
|
||||
* int bdnet_checkTTL(); a check if we can modify the ttl
|
||||
*/
|
||||
|
||||
|
||||
/* the universal interface */
|
||||
int bdnet_errno(); /* for internal networking errors */
|
||||
int bdnet_init(); /* required for windows */
|
||||
int bdnet_close(int fd);
|
||||
int bdnet_socket(int domain, int type, int protocol);
|
||||
int bdnet_bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen);
|
||||
int bdnet_fcntl(int fd, int cmd, long arg);
|
||||
int bdnet_setsockopt(int s, int level, int optname,
|
||||
const void *optval, socklen_t optlen);
|
||||
ssize_t bdnet_recvfrom(int s, void *buf, size_t len, int flags,
|
||||
struct sockaddr *from, socklen_t *fromlen);
|
||||
ssize_t bdnet_sendto(int s, const void *buf, size_t len, int flags,
|
||||
const struct sockaddr *to, socklen_t tolen);
|
||||
|
||||
/* address filling */
|
||||
int bdnet_inet_aton(const char *name, struct in_addr *addr);
|
||||
/* check if we can modify the TTL on a UDP packet */
|
||||
int bdnet_checkTTL(int fd);
|
||||
|
||||
void bdsockaddr_clear(struct sockaddr_in *addr);
|
||||
|
||||
/* Extra stuff to declare for windows error handling (mimics unix errno)
|
||||
*/
|
||||
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
#if defined(_WIN32) || defined(__MINGW32__)
|
||||
|
||||
// Some Network functions that are missing from windows.
|
||||
//in_addr_t inet_netof(struct in_addr addr);
|
||||
//in_addr_t inet_network(char *inet_name);
|
||||
//int inet_aton(const char *name, struct in_addr *addr);
|
||||
|
||||
|
||||
// definitions for fcntl (NON_BLOCK) (random?)
|
||||
#define F_SETFL 0x1010
|
||||
#define O_NONBLOCK 0x0100
|
||||
|
||||
// definitions for setsockopt (TTL) (random?)
|
||||
//#define IPPROTO_IP 0x0011
|
||||
//#define IP_TTL 0x0110
|
||||
|
||||
/* define the Unix Error Codes that we use...
|
||||
* NB. we should make the same, but not necessary
|
||||
*/
|
||||
|
||||
#define EAGAIN 11
|
||||
#define EWOULDBLOCK EAGAIN
|
||||
|
||||
#define EUSERS 87
|
||||
#define ENOTSOCK 88
|
||||
|
||||
#define EOPNOTSUPP 95
|
||||
|
||||
#define EADDRINUSE 98
|
||||
#define EADDRNOTAVAIL 99
|
||||
#define ENETDOWN 100
|
||||
#define ENETUNREACH 101
|
||||
|
||||
#define ECONNRESET 104
|
||||
|
||||
#define ETIMEDOUT 10060 // value from pthread.h
|
||||
#define ECONNREFUSED 111
|
||||
#define EHOSTDOWN 112
|
||||
#define EHOSTUNREACH 113
|
||||
#define EALREADY 114
|
||||
#define EINPROGRESS 115
|
||||
|
||||
int bdnet_w2u_errno(int error);
|
||||
|
||||
/* also put the sleep commands in here (where else to go)
|
||||
* ms uses millisecs.
|
||||
* void Sleep(int ms);
|
||||
*/
|
||||
int sleep(unsigned int sec);
|
||||
int usleep(unsigned int usec);
|
||||
|
||||
#endif // END of WINDOWS defines.
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* C Interface */
|
||||
#endif
|
||||
|
||||
#endif /* BITDHT_UNIVERSAL_NETWORK_HEADER */
|
93
libbitdht/src/util/bdrandom.cc
Normal file
93
libbitdht/src/util/bdrandom.cc
Normal file
@ -0,0 +1,93 @@
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include "util/bdrandom.h"
|
||||
|
||||
uint32_t bdRandom::index = 0 ;
|
||||
std::vector<uint32_t> bdRandom::MT(bdRandom::N,0u) ;
|
||||
bdMutex bdRandom::rndMtx ;
|
||||
|
||||
#if defined(_WIN32) || defined(__MINGW32__)
|
||||
static bool auto_seed = bdRandom::seed( (time(NULL) + ((uint32_t) pthread_self().p)*0x1293fe)^0x18e34a12 ) ;
|
||||
#else
|
||||
#ifdef __APPLE__
|
||||
static bool auto_seed = bdRandom::seed( (time(NULL) + pthread_mach_thread_np(pthread_self())*0x1293fe + (getpid()^0x113ef76b))^0x18e34a12 ) ;
|
||||
#else
|
||||
static bool auto_seed = bdRandom::seed( (time(NULL) + pthread_self()*0x1293fe + (getpid()^0x113ef76b))^0x18e34a12 ) ;
|
||||
#endif
|
||||
#endif
|
||||
bool bdRandom::seed(uint32_t s)
|
||||
{
|
||||
bdStackMutex mtx(rndMtx) ;
|
||||
|
||||
MT.resize(N,0) ; // because MT might not be already resized
|
||||
|
||||
uint32_t j ;
|
||||
MT[0]= s & 0xffffffffUL;
|
||||
for (j=1; j<N; j++)
|
||||
MT[j] = (1812433253UL * (MT[j-1] ^ (MT[j-1] >> 30)) + j) & 0xffffffffUL ;
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
void bdRandom::locked_next_state()
|
||||
{
|
||||
for(uint32_t i=0;i<N;++i)
|
||||
{
|
||||
uint32_t y = ((MT[i]) & UMASK) | ((MT[(i+1)%(int)N]) & LMASK) ;
|
||||
|
||||
MT[i] = MT[(i + M) % (int)N] ^ (y >> 1) ;
|
||||
|
||||
if((y & 1) == 1)
|
||||
MT[i] = MT[i] ^ 0x9908b0df ;
|
||||
}
|
||||
index = 0 ;
|
||||
}
|
||||
|
||||
uint32_t bdRandom::random_u32()
|
||||
{
|
||||
uint32_t y;
|
||||
|
||||
{
|
||||
bdStackMutex mtx(rndMtx) ;
|
||||
|
||||
y = MT[index++] ;
|
||||
|
||||
if(index == N)
|
||||
locked_next_state();
|
||||
}
|
||||
|
||||
// Tempering
|
||||
y ^= (y >> 11);
|
||||
y ^= (y << 7 ) & 0x9d2c5680UL;
|
||||
y ^= (y << 15) & 0xefc60000UL;
|
||||
y ^= (y >> 18);
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
uint64_t bdRandom::random_u64()
|
||||
{
|
||||
return ((uint64_t)random_u32() << 32ul) + random_u32() ;
|
||||
}
|
||||
|
||||
float bdRandom::random_f32()
|
||||
{
|
||||
return random_u32() / (float)(~(uint32_t)0) ;
|
||||
}
|
||||
|
||||
double bdRandom::random_f64()
|
||||
{
|
||||
return random_u64() / (double)(~(uint64_t)0) ;
|
||||
}
|
||||
|
||||
std::string bdRandom::random_alphaNumericString(uint32_t len)
|
||||
{
|
||||
std::string s = "" ;
|
||||
|
||||
for(uint32_t i=0;i<len;++i)
|
||||
s += (char)( (random_u32()%94) + 33) ;
|
||||
|
||||
return s ;
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user