diff --git a/PeerNetQt/src/Notes.txt b/PeerNetQt/src/Notes.txt new file mode 100644 index 000000000..d07b569d0 --- /dev/null +++ b/PeerNetQt/src/Notes.txt @@ -0,0 +1,9 @@ + +PeerNetQt. + +requires: + 1) libbitdht. + 2) tou. + 3) udp stack. + 4) forwarding. + diff --git a/PeerNetQt/src/PeerNetQt.pro b/PeerNetQt/src/PeerNetQt.pro new file mode 100644 index 000000000..6811c78e6 --- /dev/null +++ b/PeerNetQt/src/PeerNetQt.pro @@ -0,0 +1,72 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2011-06-06T19:21:58 +# +#------------------------------------------------- + +TARGET = PeerNetQt +TEMPLATE = app + +RCC_DIR = temp/qrc +UI_DIR = temp/ui +MOC_DIR = temp/moc + +CONFIG += bitdht librs +#CONFIG += qt gui uic qrc resources uitools idle bitdht # framecatcher# blogs +#QT += network xml script + +SOURCES += main.cpp\ + mainwindow.cpp \ + peernet.cc + +HEADERS += mainwindow.h \ + peernet.h + +FORMS += mainwindow.ui + + +librs { + LIBRS_DIR = ../../libretroshare/src/ + INCLUDEPATH += $${LIBRS_DIR} +} + + +bitdht { + BITDHT_DIR = ../../libbitdht/src + INCLUDEPATH += . $${BITDHT_DIR} + + # The next line if for compliance with debian packages. Keep it! + INCLUDEPATH += ../libbitdht + DEFINES *= RS_USE_BITDHT + + LIBS += ../../libbitdht/src/lib/libbitdht.a + PRE_TARGETDEPS *= ../../libbitdht/src/lib/libbitdht.a +} + + + +macx { + # ENABLE THIS OPTION FOR Univeral Binary BUILD. + # CONFIG += ppc x86 + + #CONFIG += version_detail_bash_script + LIBS += ../../libretroshare/src/lib/libretroshare.a + + LIBS += -lssl -lcrypto -lz -lgpgme -lgpg-error -lassuan + LIBS += ../../../miniupnpc-1.0/libminiupnpc.a + LIBS += -framework CoreFoundation + LIBS += -framework Security + + INCLUDEPATH += . + #DEFINES* = MAC_IDLE # for idle feature + CONFIG -= uitools + +} + +############################## Common stuff ###################################### + +# On Linux systems that alredy have libssl and libcrypto it is advisable +# to rename the patched version of SSL to something like libsslxpgp.a and libcryptoxpg.a + +# ########################################### + diff --git a/PeerNetQt/src/bdboot.txt b/PeerNetQt/src/bdboot.txt new file mode 100644 index 000000000..7aa234b5d --- /dev/null +++ b/PeerNetQt/src/bdboot.txt @@ -0,0 +1,500 @@ +212.139.213.150 11032 +212.139.213.150 10990 +85.152.219.110 16300 +151.64.206.15 26860 +84.121.123.155 18218 +193.109.161.129 6881 +64.121.111.102 29081 +95.178.41.37 46715 +99.70.101.170 50784 +119.203.237.201 46148 +114.172.79.251 7922 +24.35.134.70 49031 +110.201.109.110 16001 +188.115.181.175 63651 +85.152.219.110 16466 +86.101.126.209 45168 +87.246.21.240 10255 +79.163.101.143 36097 +194.247.17.241 22817 +123.113.36.18 16001 +188.55.82.33 63068 +85.152.219.110 16471 +212.139.213.150 11167 +84.41.37.179 55364 +178.72.75.183 53646 +212.139.213.150 10920 +195.93.138.193 35691 +184.57.201.40 49252 +217.175.171.48 51413 +85.152.219.110 16277 +212.139.213.150 10925 +85.152.219.110 16317 +121.93.98.221 23082 +212.139.213.150 10899 +212.139.213.150 11065 +81.183.201.252 25094 +85.152.219.110 16327 +85.152.219.110 16478 +85.210.60.130 48512 +212.139.213.150 10945 +85.152.219.110 16386 +212.139.213.150 10869 +178.149.19.10 55995 +87.19.184.107 37322 +85.152.219.110 16382 +119.152.12.221 53113 +85.152.219.110 16394 +2.49.78.158 60971 +77.243.112.131 33702 +212.139.213.150 10885 +212.139.213.150 10905 +95.30.54.87 36323 +93.79.180.105 29600 +212.139.213.150 11128 +94.228.121.123 59898 +212.139.213.150 11078 +88.204.117.27 35691 +78.176.90.204 34412 +195.66.100.3 39983 +81.231.142.85 51709 +85.172.249.94 26350 +212.139.213.150 10999 +212.139.213.150 10857 +212.139.213.150 11008 +212.139.213.150 10929 +89.221.115.221 42474 +212.139.213.150 10898 +212.139.213.150 11211 +92.84.140.193 25245 +69.245.13.37 52765 +98.193.26.149 58998 +76.14.166.121 10553 +121.215.135.167 11589 +117.206.235.234 25206 +87.235.214.29 42817 +109.173.5.179 16516 +178.140.91.128 56909 +72.175.61.150 40290 +111.241.48.84 54660 +95.182.88.133 42313 +93.183.149.239 39456 +79.173.248.84 1043 +212.139.213.150 11202 +212.139.213.150 10937 +212.139.213.150 11035 +212.139.213.150 11212 +119.105.202.44 11184 +94.178.16.95 16741 +79.178.121.200 62324 +78.120.178.227 20444 +118.36.226.47 45090 +212.139.213.150 10856 +85.152.219.110 16504 +85.152.219.110 16503 +212.139.213.150 10852 +85.152.219.110 16402 +212.139.213.150 11105 +85.152.219.110 16506 +85.152.219.110 16509 +85.232.219.81 18084 +88.164.58.144 55857 +82.73.234.131 37018 +89.38.11.135 51816 +178.67.37.134 42202 +85.152.219.110 16511 +212.139.213.150 11054 +2.60.177.71 51413 +94.209.60.137 37161 +212.139.213.150 11069 +122.163.55.49 33709 +110.35.14.46 16394 +41.240.141.226 58157 +218.102.151.97 22792 +112.120.35.208 14845 +112.134.123.210 46374 +212.67.133.227 22110 +85.152.219.110 16408 +58.0.103.222 13868 +1.36.170.135 49888 +118.160.44.124 15529 +61.219.11.135 12967 +219.77.101.32 10168 +123.231.18.70 11935 +94.23.1.199 45846 +62.248.204.118 51000 +99.28.131.130 46291 +76.121.8.79 16599 +94.193.177.45 13254 +91.95.224.125 33937 +122.161.167.221 22840 +68.149.78.143 18261 +94.174.132.84 10493 +182.89.64.219 4041 +95.55.22.43 15346 +84.55.96.240 37839 +67.82.53.124 5931 +84.0.215.220 37787 +81.225.143.195 1256 +190.189.39.56 27211 +175.142.195.240 18988 +59.177.2.234 57683 +188.220.197.130 51413 +123.236.87.167 21823 +118.5.196.178 14373 +203.253.73.158 29843 +115.59.105.0 1041 +66.191.26.174 49995 +89.147.94.239 38144 +24.201.212.220 40489 +184.59.221.228 22428 +114.46.102.183 16881 +88.134.99.73 27052 +94.250.26.27 22979 +122.178.113.80 58012 +88.178.72.2 7812 +98.215.148.74 60357 +110.164.35.39 44112 +178.152.0.100 10888 +212.139.213.150 10972 +72.200.149.14 15712 +82.244.138.223 14284 +82.244.203.153 21428 +82.232.0.124 9156 +178.178.240.33 24988 +212.139.213.150 10943 +212.139.213.150 11087 +212.139.213.150 11086 +92.134.151.100 23522 +160.114.58.149 37689 +80.232.240.131 51413 +91.144.87.105 57933 +75.119.230.89 1723 +95.135.73.84 41175 +118.0.112.234 26755 +187.15.6.101 58525 +175.145.223.193 36964 +94.194.224.35 40927 +195.138.72.56 35691 +216.106.197.206 10476 +188.16.99.201 48712 +61.196.49.130 53264 +123.155.206.188 11935 +122.120.37.152 43097 +212.139.213.150 11004 +173.63.78.77 17310 +89.165.209.72 27145 +2.60.88.158 61218 +212.139.213.150 10913 +71.225.229.119 16709 +213.37.47.70 20727 +98.244.167.120 23246 +173.34.90.46 8222 +2.104.156.207 26387 +98.110.89.33 55302 +80.80.153.149 17610 +72.213.204.222 33471 +189.101.32.212 51413 +118.9.53.170 21739 +85.227.192.135 40053 +186.23.58.157 21301 +212.139.213.150 10930 +173.56.22.140 51413 +67.41.137.173 48302 +212.139.213.150 10895 +114.38.21.70 11904 +70.81.118.246 33106 +72.38.3.58 28012 +78.29.157.202 52896 +118.165.101.114 61471 +84.73.253.37 43354 +99.99.184.86 55431 +89.214.191.127 10467 +122.106.10.173 59931 +72.27.71.232 11746 +82.137.212.38 20621 +60.0.124.214 11935 +95.171.26.4 52382 +78.20.54.114 63681 +80.92.105.91 11255 +212.139.213.150 11146 +212.139.213.150 11113 +212.139.213.150 11124 +212.139.213.150 10963 +46.109.115.238 47440 +2.4.167.74 61574 +71.58.129.230 41859 +84.244.4.8 37691 +213.110.132.160 13564 +92.244.116.227 34939 +212.200.207.47 27440 +24.13.126.175 44715 +76.89.148.206 28183 +78.237.125.203 10595 +62.255.191.244 60457 +97.118.93.163 16283 +94.69.47.150 17350 +99.22.214.100 36244 +213.107.94.57 55377 +212.139.213.150 11143 +78.31.227.221 53171 +98.113.12.175 41060 +109.132.186.182 37836 +2.95.54.76 26871 +70.120.233.87 60139 +119.194.58.175 6881 +202.64.42.71 56584 +212.139.213.150 11084 +78.190.140.113 23526 +194.144.16.142 26935 +212.139.213.150 10896 +212.139.213.150 11085 +92.158.0.211 44981 +212.139.213.150 11068 +212.139.213.150 10843 +212.139.213.150 10900 +145.236.20.190 52156 +212.139.213.150 11028 +112.201.75.180 41616 +212.139.213.150 10894 +212.139.213.150 10846 +212.139.213.150 11197 +212.139.213.150 10868 +212.139.213.150 11081 +88.187.80.252 49180 +212.139.213.150 11175 +212.139.213.150 11226 +81.13.186.186 27652 +212.139.213.150 11110 +212.139.213.150 11201 +212.139.213.150 11213 +212.139.213.150 11101 +212.139.213.150 11017 +212.139.213.150 10936 +212.139.213.150 10891 +89.252.227.41 1751 +212.139.213.150 10870 +212.139.213.150 11064 +212.139.213.150 10837 +212.139.213.150 10941 +212.139.213.150 10841 +77.40.134.117 37692 +213.245.18.98 54416 +212.139.213.150 11122 +212.139.213.150 11132 +212.139.213.150 11127 +212.139.213.150 10838 +212.139.213.150 11005 +212.139.213.150 11092 +212.139.213.150 11013 +87.58.253.222 6881 +212.139.213.150 11219 +212.139.213.150 11224 +93.183.159.227 15234 +109.8.252.86 26646 +24.118.215.76 7000 +192.117.97.129 21181 +119.240.201.43 33331 +203.142.185.144 27235 +119.109.89.98 16001 +212.139.213.150 10983 +81.82.209.160 45974 +75.84.23.196 48501 +76.19.133.240 60301 +212.139.213.150 11152 +69.47.128.239 60159 +109.86.99.249 11362 +77.38.174.41 58764 +109.195.211.3 40416 +95.57.36.113 27439 +114.164.15.235 6882 +109.203.147.188 31679 +212.139.213.150 10956 +85.152.219.110 16373 +85.152.219.110 16375 +85.152.219.110 16374 +212.139.213.150 10923 +212.139.213.150 10909 +212.139.213.150 11002 +85.152.219.110 16378 +212.139.213.150 11188 +212.139.213.150 10848 +85.152.219.110 16383 +151.66.162.65 23067 +85.152.219.110 16387 +212.139.213.150 11193 +85.152.219.110 16389 +212.139.213.150 10977 +85.152.219.110 16392 +212.139.213.150 11038 +212.139.213.150 11147 +189.60.244.233 15861 +85.152.219.110 16397 +212.139.213.150 10971 +212.139.213.150 10893 +85.152.219.110 16404 +85.152.219.110 16418 +212.139.213.150 10984 +212.139.213.150 10962 +212.139.213.150 10839 +85.152.219.110 16429 +212.139.213.150 10874 +85.152.219.110 16437 +212.139.213.150 10955 +89.169.104.162 51800 +212.139.213.150 11136 +94.113.169.223 20959 +85.152.219.110 16444 +212.139.213.150 10917 +212.139.213.150 11063 +85.152.219.110 16449 +212.139.213.150 11050 +123.228.132.214 59802 +212.139.213.150 10912 +212.139.213.150 10966 +85.152.219.110 16452 +212.139.213.150 10987 +212.139.213.150 10924 +212.139.213.150 10847 +85.152.219.110 16459 +89.178.249.16 46584 +46.32.50.107 6881 +95.168.230.154 1210 +113.146.92.42 51413 +212.139.213.150 10866 +77.123.47.213 35741 +111.233.229.16 14221 +89.201.255.71 21940 +99.238.211.18 58061 +112.201.14.0 10301 +85.152.219.110 16461 +212.139.213.150 11042 +94.21.183.178 29943 +212.139.213.150 10978 +212.139.213.150 11102 +84.60.60.147 49860 +79.136.239.71 16688 +212.139.213.150 11121 +88.77.94.222 49860 +24.1.118.110 27704 +93.145.89.34 53568 +85.152.219.110 16464 +178.73.200.28 45416 +112.221.10.75 11811 +212.139.213.150 11010 +142.161.67.218 50221 +98.250.169.93 30165 +88.170.61.126 7465 +76.28.161.135 64289 +113.11.4.87 39907 +27.186.73.219 9371 +212.139.213.150 10994 +85.152.219.110 16297 +212.139.213.150 11022 +93.116.164.129 32793 +212.139.213.150 10833 +95.102.83.196 22931 +85.152.219.110 16465 +212.139.213.150 11060 +82.224.136.52 7812 +212.139.213.150 11107 +61.18.118.141 20822 +85.152.219.110 16299 +69.76.140.34 16157 +212.139.213.150 10985 +212.139.213.150 11046 +86.58.76.161 44032 +87.151.74.145 53760 +80.94.245.157 43332 +117.196.105.164 15076 +212.139.213.150 10948 +173.74.237.177 59850 +95.25.98.209 40615 +87.110.2.78 38633 +79.119.105.174 58851 +85.152.219.110 16302 +89.73.211.60 60335 +212.139.213.150 10836 +177.16.80.162 40000 +74.232.225.202 50908 +212.139.213.150 11067 +94.137.247.253 52053 +95.73.252.250 13214 +85.152.219.110 16467 +212.139.213.150 11062 +212.139.213.150 11214 +196.205.236.76 23958 +212.139.213.150 11048 +85.152.219.110 16291 +212.139.213.150 10961 +212.139.213.150 10897 +85.152.219.110 16309 +212.139.213.150 11218 +212.139.213.150 10944 +85.152.219.110 16311 +212.139.213.150 11015 +212.139.213.150 11000 +178.162.242.146 37592 +212.139.213.150 11161 +85.152.219.110 16319 +212.139.213.150 11189 +212.139.213.150 11033 +94.190.83.102 33102 +212.139.213.150 11044 +212.139.213.150 11144 +85.152.219.110 16322 +212.139.213.150 10991 +212.139.213.150 10975 +85.152.219.110 16324 +212.139.213.150 11049 +85.152.219.110 16295 +212.139.213.150 11134 +212.139.213.150 11183 +85.152.219.110 16284 +212.139.213.150 11135 +85.152.219.110 16339 +212.139.213.150 11168 +85.152.219.110 16343 +212.139.213.150 11115 +212.139.213.150 11047 +212.139.213.150 10884 +212.139.213.150 11210 +85.152.219.110 16469 +212.139.213.150 11108 +213.119.89.134 37949 +212.139.213.150 10919 +212.139.213.150 11198 +212.139.213.150 11220 +88.168.14.141 46232 +212.139.213.150 11205 +212.139.213.150 11114 +212.139.213.150 10952 +212.139.213.150 11095 +212.139.213.150 11207 +212.139.213.150 11082 +212.139.213.150 10851 +212.139.213.150 11160 +212.139.213.150 11109 +212.139.213.150 11184 +212.139.213.150 10926 +212.139.213.150 10844 +212.139.213.150 10916 +118.11.81.144 55695 +212.139.213.150 11153 +212.139.213.150 10875 +212.139.213.150 10871 +212.139.213.150 11204 +212.139.213.150 10865 +212.139.213.150 11014 +212.139.213.150 11229 +212.139.213.150 11179 +212.139.213.150 11001 +212.139.213.150 11029 +212.139.213.150 10853 +212.139.213.150 10927 +212.139.213.150 11140 +212.139.213.150 11129 +212.139.213.150 11099 +212.139.213.150 10969 +212.139.213.150 10921 +212.139.213.150 11185 diff --git a/PeerNetQt/src/main.cpp b/PeerNetQt/src/main.cpp new file mode 100644 index 000000000..e8566b332 --- /dev/null +++ b/PeerNetQt/src/main.cpp @@ -0,0 +1,24 @@ +#include +#include "mainwindow.h" +#include "peernet.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + + #ifdef __APPLE__ + PeerNet pnet("", "../..", 0); // This is because of the apple APP structure. + #else + PeerNet pnet("", ".", 0); + #endif + + + MainWindow w; + w.show(); + + w.setPeerNet(&pnet); + + + return a.exec(); +} diff --git a/PeerNetQt/src/mainwindow.cpp b/PeerNetQt/src/mainwindow.cpp new file mode 100644 index 000000000..bc3ae6c30 --- /dev/null +++ b/PeerNetQt/src/mainwindow.cpp @@ -0,0 +1,263 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include + +#include +#include +#include + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + // tick for gui update. + QTimer *timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(update())); + timer->start(1000); + + // connect add Peer button. + connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addPeer())); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::changeEvent(QEvent *e) +{ + QMainWindow::changeEvent(e); + switch (e->type()) { + case QEvent::LanguageChange: + ui->retranslateUi(this); + break; + default: + break; + } +} + +void MainWindow::setPeerNet(PeerNet *pnet) +{ + mPeerNet = pnet; +} + + +void MainWindow::update() +{ + //std::cerr << "MainWindow::update()" << std::endl; + updateNetStatus(); + updateDhtPeers(); + updateNetPeers(); + updateChat(); +} + + +void MainWindow::updateNetStatus() +{ + + + +} + +#define DTW_COL_BUCKET 0 +#define DTW_COL_IPADDR 1 +#define DTW_COL_PEERID 2 +#define DTW_COL_FLAGS 3 +#define DTW_COL_FOUND 4 +#define DTW_COL_SEND 5 +#define DTW_COL_RECV 6 + +void MainWindow::updateDhtPeers() +{ + + /* Hackish display of all Dht peers, should be split into buckets (as children) */ + + bdNodeId ownId; + mPeerNet->getOwnId(&ownId); + + QString status = QString::fromStdString(mPeerNet->getDhtStatusString()); + ui->dhtLabel->setText(status); + + std::list allpeers; + std::list::iterator it; + int i; + for(i = 0; i < 160; i++) + { + bdBucket peers; + mPeerNet->get_dht_peers(i, peers); + + for(it = peers.entries.begin(); it != peers.entries.end(); it++) + { + allpeers.push_back(*it); + } + } + QTreeWidget *dhtTreeWidget = ui->dhtTreeWidget; + + dhtTreeWidget->clear(); + +#if 0 + /* clear old entries */ + int itemCount = dhtTreeWidget->topLevelItemCount(); + for (int nIndex = 0; nIndex < itemCount;) + { + QTreeWidgetItem *tmp_item = dhtTreeWidget->topLevelItem(nIndex); + std::string tmpid = tmp_item->data(DTW_COL_PEERID, Qt::DisplayRole).toString().toStdString(); + if (peerIds.end() == std::find(peerIds.begin(), peerIds.end(), tmpid)) + { + peerTreeWidget->removeItemWidget(tmp_item, 0); + /* remove it! */ + itemCount--; + } + else + { + nIndex++; + } + } +#endif + + time_t now = time(NULL); + for(it = allpeers.begin(); it != allpeers.end(); it++) + { + /* find the entry */ + QTreeWidgetItem *dht_item = NULL; + + /* insert */ + dht_item = new QTreeWidgetItem(); + + int dist = bdStdBucketDistance(&ownId, &(it->mPeerId.id)); + std::ostringstream buckstr; + buckstr << dist; + + std::ostringstream ipstr; + ipstr << inet_ntoa(it->mPeerId.addr.sin_addr); + ipstr << ":" << ntohs(it->mPeerId.addr.sin_port); + + std::ostringstream idstr; + bdStdPrintNodeId(idstr, &(it->mPeerId.id)); + + std::ostringstream flagsstr; + flagsstr << "0x" << std::hex << std::setfill('0') << it->mPeerFlags; + + std::ostringstream foundstr; + foundstr << now - it->mFoundTime << " secs ago"; + + std::ostringstream lastsendstr; + lastsendstr << now - it->mLastSendTime << " secs ago"; + + std::ostringstream lastrecvstr; + lastrecvstr << now - it->mLastRecvTime << " secs ago"; + + dht_item -> setData(DTW_COL_BUCKET, Qt::DisplayRole, QString::fromStdString(buckstr.str())); + dht_item -> setData(DTW_COL_IPADDR, Qt::DisplayRole, QString::fromStdString(ipstr.str())); + dht_item -> setData(DTW_COL_PEERID, Qt::DisplayRole, QString::fromStdString(idstr.str())); + dht_item -> setData(DTW_COL_FLAGS, Qt::DisplayRole, QString::fromStdString(flagsstr.str())); + + dht_item -> setData(DTW_COL_FOUND, Qt::DisplayRole, QString::fromStdString(foundstr.str())); + dht_item -> setData(DTW_COL_SEND, Qt::DisplayRole, QString::fromStdString(lastsendstr.str())); + dht_item -> setData(DTW_COL_RECV, Qt::DisplayRole, QString::fromStdString(lastrecvstr.str())); + + dhtTreeWidget->addTopLevelItem(dht_item); + } + +} + + +void MainWindow::updateNetPeers() +{ + QString status = QString::fromStdString(mPeerNet->getPeerStatusString()); + QString oldstatus = ui->peerLine->text(); + if (oldstatus != status) + { + ui->peerLine->setText(status); + } + + + QTreeWidget *peerTreeWidget = ui->peerTreeWidget; + + std::list peerIds; + std::list::iterator it; + mPeerNet->get_net_peers(peerIds); + +#define PTW_COL_PEERID 0 +#define PTW_COL_STATUS 1 +#define PTW_COL_UPDATETS 2 + + /* clear old entries */ + int itemCount = peerTreeWidget->topLevelItemCount(); + for (int nIndex = 0; nIndex < itemCount;) + { + QTreeWidgetItem *tmp_item = peerTreeWidget->topLevelItem(nIndex); + std::string tmpid = tmp_item->data(PTW_COL_PEERID, Qt::DisplayRole).toString().toStdString(); + if (peerIds.end() == std::find(peerIds.begin(), peerIds.end(), tmpid)) + { + peerTreeWidget->removeItemWidget(tmp_item, 0); + /* remove it! */ + itemCount--; + } + else + { + nIndex++; + } + } + + for(it = peerIds.begin(); it != peerIds.end(); it++) + { + /* find the entry */ + QTreeWidgetItem *peer_item = NULL; + QString qpeerid = QString::fromStdString(*it); + int itemCount = peerTreeWidget->topLevelItemCount(); + for (int nIndex = 0; nIndex < itemCount; nIndex++) + { + QTreeWidgetItem *tmp_item = peerTreeWidget->topLevelItem(nIndex); + if (tmp_item->data(PTW_COL_PEERID, Qt::DisplayRole).toString() == qpeerid) + { + peer_item = tmp_item; + break; + } + } + + if (!peer_item) + { + /* insert */ + peer_item = new QTreeWidgetItem(); + peerTreeWidget->addTopLevelItem(peer_item); + } + + /* update the data */ + PeerStatus status; + mPeerNet->get_peer_status(*it, status); + + std::ostringstream updatestr; + time_t now = time(NULL); + updatestr << now - status.mUpdateTS << " secs ago"; + + peer_item -> setData(PTW_COL_PEERID, Qt::DisplayRole, QString::fromStdString(*it)); + peer_item -> setData(PTW_COL_STATUS, Qt::DisplayRole, QString::fromStdString(status.mStatusMsg)); + peer_item -> setData(PTW_COL_UPDATETS, Qt::DisplayRole, QString::fromStdString(updatestr.str())); + } +} + + +void MainWindow::addPeer() +{ + std::string id = ui->peerLineEdit->text().toStdString(); + mPeerNet->add_peer(id); +} + + +void MainWindow::sendChat() +{ + + +} + + +void MainWindow::updateChat() +{ + + + +} + diff --git a/PeerNetQt/src/mainwindow.h b/PeerNetQt/src/mainwindow.h new file mode 100644 index 000000000..2659cbcf1 --- /dev/null +++ b/PeerNetQt/src/mainwindow.h @@ -0,0 +1,37 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include "peernet.h" + +namespace Ui { + class MainWindow; +} + +class MainWindow : public QMainWindow { + Q_OBJECT +public: + MainWindow(QWidget *parent = 0); + ~MainWindow(); + + void setPeerNet(PeerNet *pnet); + + void updateNetStatus(); + void updateDhtPeers(); + void updateNetPeers(); + void sendChat(); + void updateChat(); + +public slots: + void update(); + void addPeer(); + +protected: + void changeEvent(QEvent *e); + +private: + Ui::MainWindow *ui; + PeerNet *mPeerNet; +}; + +#endif // MAINWINDOW_H diff --git a/PeerNetQt/src/mainwindow.ui b/PeerNetQt/src/mainwindow.ui new file mode 100644 index 000000000..d53f12685 --- /dev/null +++ b/PeerNetQt/src/mainwindow.ui @@ -0,0 +1,174 @@ + + + MainWindow + + + true + + + + 0 + 0 + 644 + 590 + + + + MainWindow + + + + + + + + + Add Peer + + + + + + + + + + + + Qt::Vertical + + + + + + + true + + + Peer Details + + + true + + + + + + + + PeerId + + + + + Status + + + + + Last Update + + + + + + + + + + + + + + Enter to send Chat: + + + + + + + + + + + + + + + + + + + DHT Details: + + + + + + + + Bucket + + + + + Ip:Port + + + + + Key + + + + + StatusFlags + + + + + Found + + + + + Last Send + + + + + Last Recv + + + + + + + + + + + + + + 0 + 0 + 644 + 22 + + + + + + TopToolBarArea + + + false + + + + + + + + diff --git a/PeerNetQt/src/peernet.cc b/PeerNetQt/src/peernet.cc new file mode 100644 index 000000000..7ea706c1a --- /dev/null +++ b/PeerNetQt/src/peernet.cc @@ -0,0 +1,512 @@ + +#include "peernet.h" +#include +#include + + +#include "bitdht/bdstddht.h" +#include "tcponudp/tou.h" +#include "util/rsnet.h" + + +PeerNet::PeerNet(std::string id, std::string configpath, uint16_t port) +{ + std::string dhtVersion = "RS52"; // should come from elsewhere! + + std::cerr << "PeerNet::PeerNet()" << std::endl; + std::cerr << "Using Id: " << id; + std::cerr << std::endl; + std::cerr << "Using Config Path: " << configpath; + std::cerr << std::endl; + std::cerr << "Converting OwnId to bdNodeId...."; + std::cerr << std::endl; + + std::cerr << "Loading Configuration"; + std::cerr << std::endl; + + mConfigFile = configpath + "/peerconfig.txt"; + mPeersFile = configpath + "/peerlist.txt"; + std::string bootstrapfile = configpath + "/bdboot.txt"; + + srand(time(NULL)); + + mPort = 10240 + (rand() % 10240); + if (!loadConfig(mConfigFile)) + { + std::cerr << "Failed to loadConfig, Creating random Id/Port"; + std::cerr << std::endl; + + /* */ + bdStdRandomNodeId(&mOwnId); + } + + if (!bdStdLoadNodeId(&mOwnId, id)) + { + std::cerr << "Failed to load Id from FnParameters"; + std::cerr << std::endl; + } + + if (port > 1024) + { + mPort = port; + } + + + std::cerr << "Input Id: " << id; + std::cerr << std::endl; + + std::cerr << "Own NodeId: "; + bdStdPrintNodeId(std::cerr, &mOwnId); + std::cerr << std::endl; + + /* standard dht behaviour */ + bdDhtFunctions *stdfns = new bdStdDht(); + + std::cerr << "PeerNet() startup ... creating UdpStack"; + std::cerr << std::endl; + + + struct sockaddr_in tmpladdr; + sockaddr_clear(&tmpladdr); + tmpladdr.sin_port = htons(mPort); + mUdpStack = new UdpStack(tmpladdr); + + std::cerr << "PeerNet() startup ... creating UdpBitDht"; + std::cerr << std::endl; + + /* create dht */ + mUdpBitDht = new UdpBitDht(mUdpStack, &mOwnId, dhtVersion, bootstrapfile, stdfns); + mUdpStack->addReceiver(mUdpBitDht); + + /* setup callback to here */ + mUdpBitDht->addCallback(this); + + /* setup TOU part */ + tou_init(mUdpStack); + + /* startup the Udp stuff! */ + mUdpBitDht->start(); + mUdpBitDht->startDht(); + + // handle configuration. + loadPeers(mPeersFile); + storeConfig(mConfigFile); + +} + +int PeerNet::getOwnId(bdNodeId *id) +{ + *id = mOwnId; + return 1; +} + +#define PN_PEER_STATE_UNKNOWN 0 +#define PN_PEER_STATE_SEARCHING 1 +#define PN_PEER_STATE_FAILURE 2 +#define PN_PEER_STATE_OFFLINE 3 +#define PN_PEER_STATE_UNREACHABLE 4 +#define PN_PEER_STATE_ONLINE 5 + +int PeerNet::add_peer(std::string id) +{ + bdNodeId tmpId; + + if (!bdStdLoadNodeId(&tmpId, id)) + { + std::cerr << "PeerNet::add_peer() Failed to load own Id"; + std::cerr << std::endl; + return 0; + } + + std::ostringstream str; + bdStdPrintNodeId(str, &tmpId); + std::string filteredId = str.str(); + + std::cerr << "PeerNet::add_peer()"; + std::cerr << std::endl; + + std::cerr << "Input Id: " << id; + std::cerr << std::endl; + std::cerr << "Filtered Id: " << id; + std::cerr << std::endl; + std::cerr << "Final NodeId: "; + bdStdPrintNodeId(std::cerr, &tmpId); + std::cerr << std::endl; + + if (mPeers.end() == mPeers.find(filteredId)) + { + std::cerr << "Adding New Peer: " << filteredId << std::endl; + mPeers[filteredId] = PeerStatus(); + std::map::iterator it = mPeers.find(filteredId); + + mUdpBitDht->addFindNode(&tmpId, BITDHT_QFLAGS_DO_IDLE); + + it->second.mId = filteredId; + it->second.mStatusMsg = "Just Added"; + it->second.mState = PN_PEER_STATE_SEARCHING; + it->second.mUpdateTS = time(NULL); + + storePeers(mPeersFile); + + return 1; + } + + std::cerr << "Peer Already Exists, ignoring: " << filteredId << std::endl; + + return 0; +} + +int PeerNet::remove_peer(std::string id) +{ + bdNodeId tmpId; + + if (!bdStdLoadNodeId(&tmpId, id)) + { + std::cerr << "PeerNet::remove_peer() Failed to load own Id"; + std::cerr << std::endl; + return 0; + } + + std::ostringstream str; + bdStdPrintNodeId(str, &tmpId); + std::string filteredId = str.str(); + + std::map::iterator it = mPeers.find(filteredId); + + if (mPeers.end() != it) + { + mUdpBitDht->removeFindNode(&tmpId); + mPeers.erase(it); + return 1; + } + return 0; +} + +int PeerNet::get_dht_peers(int lvl, bdBucket &peers) +{ + /* must be able to access deep into bitdht, which isn't possible + * at the moment! ... add this functionality now! + */ + + return mUdpBitDht->getDhtBucket(lvl, peers); +} + + +std::string PeerNet::getDhtStatusString() +{ + std::ostringstream out; + int state = mUdpBitDht->stateDht(); + + switch(state) + { + default: + out << "Unknown State: " << state << " "; + break; + case BITDHT_MGR_STATE_OFF: + out << "BitDHT OFF "; + break; + case BITDHT_MGR_STATE_STARTUP: + out << "BitDHT Startup "; + break; + case BITDHT_MGR_STATE_FINDSELF: + out << "BitDHT FindSelf "; + break; + case BITDHT_MGR_STATE_ACTIVE: + out << "BitDHT Active "; + break; + case BITDHT_MGR_STATE_REFRESH: + out << "BitDHT Refresh "; + break; + case BITDHT_MGR_STATE_QUIET: + out << "BitDHT Quiet "; + break; + case BITDHT_MGR_STATE_FAILED: + out << "BitDHT Failed "; + break; + } + + + out << " BitNetSize: " << mUdpBitDht->statsNetworkSize(); + out << " LocalNetSize: " << mUdpBitDht->statsBDVersionSize(); + + return out.str(); +} + + +std::string PeerNet::getPeerStatusString() +{ + std::ostringstream out; + + out << "OwnId: "; + bdStdPrintNodeId(out, &mOwnId); + + out << " LocalPort: " << mPort; + + return out.str(); +} + + + + + +int PeerNet::get_net_peers(std::list &peerIds) +{ + std::map::iterator it; + for(it = mPeers.begin(); it != mPeers.end(); it++) + { + peerIds.push_back(it->first); + } + return 1; +} + +int PeerNet::get_peer_status(std::string id, PeerStatus &status) +{ + std::map::iterator it = mPeers.find(id); + if (it != mPeers.end()) + { + status = it->second; + return 1; + } + return 0; +} + + /* remember peers */ +int PeerNet::storePeers(std::string filepath) +{ + std::cerr << "PeerNet::storePeers(" << filepath << ")"; + std::cerr << std::endl; + + FILE *fd = fopen(filepath.c_str(), "w"); + if (!fd) + { + std::cerr << "Failed to StorePeers, Issue Opening file: " << filepath; + std::cerr << std::endl; + return 0; + } + + std::map::iterator it; + for(it = mPeers.begin(); it != mPeers.end(); it++) + { + fprintf(fd, "%s\n", (it->first).c_str()); + } + fclose(fd); + return 1; +} + +int PeerNet::loadPeers(std::string filepath) +{ + FILE *fd = fopen(filepath.c_str(), "r"); + if (!fd) + { + std::cerr << "Failed to loadPeers, Issue Opening file: " << filepath; + std::cerr << std::endl; + return 0; + } + + char line[1024]; + bool firstline = true; + + std::list peerIds; + while(1 == fscanf(fd, "%[^\n]\n", line)) + { + std::cerr << "Read Peer: " << line; + std::cerr << std::endl; + + std::string id(line); + peerIds.push_back(id); + } + fclose(fd); + + std::list::iterator it; + for(it = peerIds.begin(); it != peerIds.end(); it++) + { + add_peer(*it); + } + return 1; +} + + + /* remember peers */ +int PeerNet::storeConfig(std::string filepath) +{ + FILE *fd = fopen(filepath.c_str(), "w"); + if (!fd) + { + std::cerr << "Failed to StorePeers, Issue Opening file: " << filepath; + std::cerr << std::endl; + return 0; + } + + /* store own hash */ + std::ostringstream ownidstr; + bdStdPrintNodeId(ownidstr, &(mOwnId)); + fprintf(fd, "%s\n", ownidstr.str().c_str()); + + fclose(fd); + return 1; +} + +int PeerNet::loadConfig(std::string filepath) +{ + FILE *fd = fopen(filepath.c_str(), "r"); + if (!fd) + { + std::cerr << "Failed to loadConfig, Issue Opening file: " << filepath; + std::cerr << std::endl; + return 0; + } + + char line[1024]; + bool firstline = true; + + if (1 == fscanf(fd, "%[^\n]\n", line)) + { + std::string id(line); + std::cerr << "Read OwnId: " << line; + std::cerr << std::endl; + + if (!bdStdLoadNodeId(&mOwnId, id)) + { + std::cerr << "Failed to load own Id"; + std::cerr << std::endl; + } + } + fclose(fd); + return 1; +} + + + + + + + /**** dht Callback ****/ +int PeerNet::dhtNodeCallback(const bdId *id, uint32_t peerflags) +{ + std::ostringstream str; + bdStdPrintNodeId(str, &(id->id)); + std::string strId = str.str(); + + std::map::iterator it = mPeers.find(strId); + if (it != mPeers.end()) + { + std::cerr << "PeerNet::dhtNodeCallback() From KNOWN PEER: "; + bdStdPrintId(std::cerr, id); + std::cerr << " Flags: " << peerflags; + std::cerr << std::endl; + } + return 1; +} + +int PeerNet::dhtPeerCallback(const bdId *id, uint32_t status) +{ + std::ostringstream str; + bdStdPrintNodeId(str, &(id->id)); + std::string strId = str.str(); + + //std::cerr << "PeerNet::dhtPeerCallback()"; + //std::cerr << std::endl; + + std::map::iterator it = mPeers.find(strId); + if (it != mPeers.end()) + { + switch(status) + { + default: + { + it->second.mStatusMsg = "Unknown Peer State"; + it->second.mState = PN_PEER_STATE_UNKNOWN; + } + break; + + case BITDHT_MGR_QUERY_FAILURE: + { + it->second.mStatusMsg = "Search Failed (is DHT working?)"; + it->second.mState = PN_PEER_STATE_FAILURE; + } + break; + case BITDHT_MGR_QUERY_PEER_OFFLINE: + { + it->second.mStatusMsg = "Peer Offline"; + it->second.mState = PN_PEER_STATE_OFFLINE; + } + break; + case BITDHT_MGR_QUERY_PEER_UNREACHABLE: + { + it->second.mStatusMsg = "Peer Unreachable"; + it->second.mState = PN_PEER_STATE_UNREACHABLE; + } + break; + case BITDHT_MGR_QUERY_PEER_ONLINE: + { + it->second.mStatusMsg = "Peer Online"; + it->second.mState = PN_PEER_STATE_ONLINE; + } + break; + } + time_t now = time(NULL); + it->second.mUpdateTS = now; + return 1; + } + else + { + std::cerr << "PeerNet::dhtPeerCallback() Unknown Peer: "; + bdStdPrintId(std::cerr, id); + std::cerr << " status: " << status; + std::cerr << std::endl; + } + + return 1; +} + + +int PeerNet::dhtValueCallback(const bdNodeId *id, std::string key, uint32_t status) +{ + std::cerr << "PeerNet::dhtValueCallback()"; + std::cerr << std::endl; + + return 1; +} + +int PeerNet::dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId, + uint32_t mode, uint32_t point, uint32_t cbtype) +{ + std::cerr << "PeerNet::dhtConnectCallback()"; + std::cerr << std::endl; + + switch(cbtype) + { + case BITDHT_CONNECT_CB_AUTH: + { + + + } + break; + case BITDHT_CONNECT_CB_PENDING: + { + + } + break; + case BITDHT_CONNECT_CB_START: + { + + } + break; + case BITDHT_CONNECT_CB_PROXY: + { + + } + break; + default: + case BITDHT_CONNECT_CB_FAILED: + { + + } + break; + } + return 1; +} + + + + diff --git a/PeerNetQt/src/peernet.h b/PeerNetQt/src/peernet.h new file mode 100644 index 000000000..5ccf5fe17 --- /dev/null +++ b/PeerNetQt/src/peernet.h @@ -0,0 +1,90 @@ +#ifndef PEER_NET_INTERFACE_H +#define PEER_NET_INTERFACE_H + +/* top-level p2p overlay network interface */ + +#include +#include + +#include "bitdht/bdiface.h" +#include "udp/udpstack.h" +#include "udp/udpbitdht.h" + +#include "bitdht/bdstddht.h" + +class DhtPeer +{ + public: + std::string id; +}; + +class PeerStatus +{ + public: + std::string mId; + std::string mStatusMsg; + uint32_t mState; + time_t mUpdateTS; +}; + + +class PeerNet: public BitDhtCallback +{ + public: + PeerNet(std::string id, std::string bootstrapfile, uint16_t port); + /* GUI interface */ + + int getOwnId(bdNodeId *id); + + int add_peer(std::string id); + int remove_peer(std::string id); + + + std::string getPeerStatusString(); + std::string getDhtStatusString(); + int get_dht_peers(int lvl, bdBucket &peers); + //int get_dht_peers(int lvl, std::list &peers); + int get_net_peers(std::list &peerIds); + int get_peer_status(std::string peerId, PeerStatus &status); + + /* remember peers */ + int storePeers(std::string filepath); + int loadPeers(std::string filepath); + + int storeConfig(std::string filepath); + int loadConfig(std::string filepath); + /* under the hood */ + + + + /**** dht Callback ****/ + // dummy cos not needed for standard dht behaviour; +virtual int dhtNodeCallback(const bdId *id, uint32_t peerflags); + + // must be implemented. +virtual int dhtPeerCallback(const bdId *id, uint32_t status); +virtual int dhtValueCallback(const bdNodeId *id, std::string key, uint32_t status); + + // 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); + + + private: + + UdpStack *mUdpStack; + UdpBitDht *mUdpBitDht; + + std::map mPeers; + + bdNodeId mOwnId; + + std::string mPeersFile; + std::string mConfigFile; + + uint16_t mPort; +}; + + + +#endif