From 7044822e1f3397447b3e90bc831793c6058961a3 Mon Sep 17 00:00:00 2001 From: drbob Date: Fri, 21 Mar 2008 02:02:58 +0000 Subject: [PATCH] Addition of several utility functions: (1) xpgp_id to extract the name/id from a certificate. (2) dht_bootstrap to check the status of the bootstrap peers. Various bits of code needed to be rearranged to make these utilities possible. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@394 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/dht/Makefile | 9 +- libretroshare/src/dht/dht_bootstrap.cc | 535 +++++++++++++++++++ libretroshare/src/pqi/Makefile | 7 +- libretroshare/src/pqi/authxpgp.cc | 9 +- libretroshare/src/pqi/authxpgp.h | 5 +- libretroshare/src/pqi/p3dhtmgr.cc | 2 +- libretroshare/src/pqi/xpgp_id.cc | 44 ++ libretroshare/src/rsserver/p3face-startup.cc | 10 +- libretroshare/src/serialiser/Makefile | 3 +- libretroshare/src/services/p3photoservice.cc | 27 +- libretroshare/src/tcponudp/udpsorter.cc | 20 +- libretroshare/src/tcponudp/udpsorter.h | 11 +- 12 files changed, 651 insertions(+), 31 deletions(-) create mode 100644 libretroshare/src/dht/dht_bootstrap.cc create mode 100644 libretroshare/src/pqi/xpgp_id.cc diff --git a/libretroshare/src/dht/Makefile b/libretroshare/src/dht/Makefile index 09d825e1d..070edb2f8 100644 --- a/libretroshare/src/dht/Makefile +++ b/libretroshare/src/dht/Makefile @@ -9,9 +9,11 @@ include $(RS_TOP_DIR)/scripts/config.mk RSOBJ = b64.o opendhtstr.o opendht.o opendhtmgr.o -TESTOBJ = odhtstr_test.o odhtpost_test.o odhtmgr_test.o +TESTOBJ = odhtstr_test.o odhtpost_test.o odhtmgr_test.o \ + dht_bootstrap.o -TESTS = odhtstr_test odhtpost_test odhtmgr_test +TESTS = odhtstr_test odhtpost_test odhtmgr_test \ + dht_bootstrap all: librs tests @@ -28,6 +30,9 @@ odhtstr_test: $(OBJ) odhtstr_test.o odhtmgr_test: $(OBJ) odhtmgr_test.o $(CC) $(CFLAGS) -o odhtmgr_test $(OBJ) odhtmgr_test.o $(LIBS) +dht_bootstrap: dht_bootstrap.o + $(CC) $(CFLAGS) -o dht_bootstrap dht_bootstrap.o $(LIBS) + # Extra Rule... .c.o: $(CC) $(CFLAGS) -c $< diff --git a/libretroshare/src/dht/dht_bootstrap.cc b/libretroshare/src/dht/dht_bootstrap.cc new file mode 100644 index 000000000..a82ff54fc --- /dev/null +++ b/libretroshare/src/dht/dht_bootstrap.cc @@ -0,0 +1,535 @@ +/* + * libretroshare/src/dht: odhtmgr_test.cc + * + * Interface with OpenDHT for RetroShare. + * + * Copyright 2007-2008 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 "retroshare@lunamutt.com". + * + */ + + + +/***** Test for the new DHT system *****/ + +#include "pqi/p3dhtmgr.h" +#include "pqi/p3connmgr.h" +#include "pqi/pqimonitor.h" +#include "dht/opendhtmgr.h" + +#include "util/rsnet.h" +#include "util/rsthreads.h" +#include "util/rsprint.h" + +#include "tcponudp/tou_net.h" +#include "tcponudp/udpsorter.h" + +#include +#include +#include +#include + +#define BOOTSTRAP_DEBUG 1 + +void usage(char *name) +{ + std::cerr << "USAGE: " << name << " -o OwnId [ -p PeerId1 [ -p PeerId2 [ ... ] ] ] "; + std::cerr << std::endl; + exit(1); +} + +void loadBootStrapIds(std::list &peerIds); +bool stunPeer(struct sockaddr_in toaddr, struct sockaddr_in &ansaddr); + +class pqiConnectCbStun; + +class dhtStunData +{ + public: + pqiConnectCbStun *stunCb; + std::string id; + struct sockaddr_in toaddr; + struct sockaddr_in ansaddr; +}; + +extern "C" void* doStunPeer(void* p); + + + +class StunDetails +{ + public: + StunDetails() +{ + lastStatus = 0; + lastStunResult = 0; + stunAttempts = 0; + stunResults = 0; +} + + std::string id; + + /* peerStatus details */ + struct sockaddr_in laddr, raddr; + uint32_t type, mode, source; + + /* stun response */ + uint32_t stunAttempts; + uint32_t stunResults; + struct sockaddr_in stunaddr; + + /* timestamps */ + time_t lastStatus; + time_t lastStunResult; + +}; + +class pqiConnectCbStun: public pqiConnectCb +{ + public: + pqiConnectCbStun() +{ + return; +} + +virtual ~pqiConnectCbStun() +{ + return; +} + +void addPeer(std::string id) +{ + RsStackMutex stack(peerMtx); /**** LOCK MUTEX ***/ + std::map::iterator it; + it = peerMap.find(id); + if (it == peerMap.end()) + { + StunDetails sd; + sd.id = id; + peerMap[id] = sd; + } +} + +virtual void peerStatus(std::string id, + struct sockaddr_in laddr, struct sockaddr_in raddr, + uint32_t type, uint32_t mode, uint32_t source) +{ + + { + RsStackMutex stack(peerMtx); /**** LOCK MUTEX ***/ + + std::map::iterator it; + it = peerMap.find(id); + if (it == peerMap.end()) + { + std::cerr << "peerStatus() for unknown Peer id: " << id; + std::cerr << std::endl; + return; + } + it->second.laddr = laddr; + it->second.raddr = raddr; + it->second.type = type; + it->second.mode = mode; + it->second.source= source; + + it->second.lastStatus = time(NULL); + + it->second.stunAttempts++; /* as we are about to try! */ + } + + printPeerStatus(); + stunPeer(id, raddr); +} + +void printPeerStatus() +{ + RsStackMutex stack(peerMtx); /**** LOCK MUTEX ***/ + + time_t t = time(NULL); + std::string timestr = ctime(&t); + std::cerr << "BootstrapStatus: " << timestr; + std::cerr << "BootstrapStatus: " << peerMap.size() << " Peers"; + std::cerr << std::endl; + std::cerr << "BootstrapStatus: ID ---------- DHT ENTRY ---"; + std::cerr << " EXT PORT -- STUN OK -- %AVAIL -- LAST DHT TS"; + std::cerr << std::endl; + + std::map::iterator it; + + for(it = peerMap.begin(); it != peerMap.end(); it++) + { + std::cerr << it->first; + + bool dhtActive = (time(NULL) - it->second.lastStatus < 1900); + bool stunActive = (time(NULL) - it->second.lastStunResult < 1900); + bool extPort = it->second.type & RS_NET_CONN_TCP_EXTERNAL; + float percentAvailable = it->second.stunResults * 100.0 / (it->second.stunAttempts + 0.0001); + + if (dhtActive) + { + std::cerr << " Yes --->"; + } + else + { + std::cerr << " No "; + } + + if (extPort) + { + std::cerr << " Yes --->"; + } + else + { + std::cerr << " No "; + } + + if (stunActive) + { + std::cerr << " Yes --->"; + } + else + { + std::cerr << " No "; + } + + std::cerr << " " << std::setw(4) << percentAvailable; + std::cerr << " "; + + if (it->second.lastStatus == 0) + { + std::cerr << " NEVER "; + } + else + { + std::cerr << " " << time(NULL) - it->second.lastStatus; + std::cerr << " secs ago "; + } + std::cerr << std::endl; + } +} + + + + +void stunPeer(std::string id, struct sockaddr_in peeraddr) +{ + std::cerr << "Should Stun Peer: " << id; + std::cerr << std::endl; + + /* launch a publishThread */ + pthread_t tid; + + dhtStunData *pub = new dhtStunData; + pub->stunCb = this; + pub->id = id; + pub->toaddr = peeraddr; + + void *data = (void *) pub; + pthread_create(&tid, 0, &doStunPeer, data); + + return; + +} + + +virtual void peerConnectRequest(std::string id, + struct sockaddr_in raddr, uint32_t source) +{ + return; +} + + +virtual void stunStatus(std::string id, struct sockaddr_in raddr, uint32_t type, uint32_t flags) +{ + return; +} + +virtual void stunSuccess(std::string id, struct sockaddr_in toaddr, struct sockaddr_in ansaddr) +{ + { + RsStackMutex stack(peerMtx); /**** LOCK MUTEX ***/ + + std::map::iterator it; + it = peerMap.find(id); + if (it == peerMap.end()) + { + std::cerr << "stunSuccess() for unknown Peer id: " << id; + std::cerr << std::endl; + return; + } + std::cerr << "stunSuccess() for id: " << id; + std::cerr << std::endl; + + it->second.lastStunResult = time(NULL); + it->second.stunResults++; + } + + printPeerStatus(); +} + + private: + + RsMutex peerMtx; + std::map peerMap; +}; + + + +extern "C" void* doStunPeer(void* p) +{ + dhtStunData *data = (dhtStunData *) p; + if ((!data) || (!data->stunCb)) + { + pthread_exit(NULL); + } + + /* stun it! */ + if (stunPeer(data->toaddr, data->ansaddr)) + { + data->stunCb->stunSuccess(data->id, data->toaddr, data->ansaddr); + } + + delete data; + + pthread_exit(NULL); + + return NULL; +} + + + + +int main(int argc, char **argv) +{ + int c; + bool setOwnId = false; + std::string ownId; + std::list peerIds; + + while(-1 != (c = getopt(argc, argv, "o:p:"))) + { + switch (c) + { + case 'o': + ownId = optarg; + setOwnId = true; + break; + case 'p': + peerIds.push_back(std::string(optarg)); + break; + default: + usage(argv[0]); + break; + } + } +/******************************** WINDOWS/UNIX SPECIFIC PART ******************/ +#ifndef WINDOWS_SYS +/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ +#else +/* for static PThreads under windows... we need to init the library... + */ + #ifdef PTW32_STATIC_LIB + pthread_win32_process_attach_np(); + #endif + + // 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; + } + +#endif + + + if (!setOwnId) + { + std::cerr << "Missing OwnId: Setting dummy Id"; + std::cerr << std::endl; + + setOwnId = true; + ownId = "dummyOwnId"; + } + + if (peerIds.size() < 1) + { + std::cerr << "No PeerIds, loading bootstrap Ids"; + std::cerr << std::endl; + loadBootStrapIds(peerIds); + } + + pqiConnectCbStun cbStun; + OpenDHTMgr dhtTester(ownId, &cbStun, "."); + + /* startup dht */ + std::cerr << "Starting up DhtTester()" << std::endl; + dhtTester.start(); + + /* wait for a little before switching on */ +/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ +#ifndef WINDOWS_SYS + sleep(1); +#else + Sleep(1000); +#endif +/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ + + + std::cerr << "Switching on DhtTester()" << std::endl; + dhtTester.setDhtOn(true); + + std::cerr << "Adding a List of Peers" << std::endl; + std::list::iterator it; + for(it = peerIds.begin(); it != peerIds.end(); it++) + { + cbStun.addPeer(*it); + dhtTester.findPeer(*it); + } + + + /* wait loop */ + while(1) + { + cbStun.printPeerStatus(); + std::cerr << "Main waiting..." << std::endl; +/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ +#ifndef WINDOWS_SYS + sleep(30); +#else + Sleep(30000); +#endif +/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ + } +}; + + +void loadBootStrapIds(std::list &peerIds) +{ + std::string id; + + // Two Defaults for The Initial Release. + id = "7ad672ea4d4af8560d5230aff3c88b59"; + peerIds.push_back(id); + + id = "8ad7c08e7778e0289de04843bf57a6ae"; + peerIds.push_back(id); + + // Donated by public. + id = "8523688347027884059506005618ae74"; /* tm */ + peerIds.push_back(id); + + id = "1bd15b320269fa1561ceb1162fd042f0"; /* cp */ + peerIds.push_back(id); + + id = "2cf2361f2afcd6d871159714bbbfc502"; /* cc */ + peerIds.push_back(id); + + id = "128646cdf761970376a62c52c372c931"; /* rf */ + peerIds.push_back(id); + + id = "86d5d94474a4b8ac4386686eff31aeb9"; /* bn */ + peerIds.push_back(id); + + return; +} + + +bool stunPeer(struct sockaddr_in toaddr, struct sockaddr_in &ansaddr) +{ +#ifdef BOOTSTRAP_DEBUG + std::cerr << "stunPeer: " << toaddr << std::endl; +#endif + /* open a socket */ + int sockfd = tounet_socket(PF_INET, SOCK_DGRAM, 0); + if (-1 == tounet_fcntl(sockfd, F_SETFL, O_NONBLOCK)) + { +#ifdef BOOTSTRAP_DEBUG + std::cerr << "Failed to Make Non-Blocking" << std::endl; +#endif + } + + /* create a stun packet */ + char stunpkt[100]; + int maxlen = 100; + int len = maxlen; + + UdpStun_generate_stun_pkt((void *) stunpkt, &len); + +#ifdef BOOTSTRAP_DEBUG + std::cerr << "stunPeer() Send packet length: " << len << std::endl; +#endif + + /* send stun packet */ + tounet_sendto(sockfd, stunpkt, len, 0, + (struct sockaddr *) &(toaddr), + sizeof(toaddr)); + + /* wait */ +/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ +#ifndef WINDOWS_SYS + sleep(2); +#else + Sleep(2000); +#endif +/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ + + /* check for response */ + struct sockaddr_in fromaddr; + socklen_t fromsize = sizeof(fromaddr); + int insize = maxlen; + + insize = tounet_recvfrom(sockfd,stunpkt,insize,0, + (struct sockaddr*)&fromaddr,&fromsize); + + tounet_close(sockfd); + + if (0 >= insize) + { +#ifdef BOOTSTRAP_DEBUG + std::cerr << "No Stun response from: " << toaddr; + std::cerr << std::endl; +#endif + return false; + } + + if (UdpStun_response(stunpkt, insize, ansaddr)) + { +#ifdef BOOTSTRAP_DEBUG + std::cerr << "received Stun Reply from : " << fromaddr; + std::cerr << std::endl; + std::cerr << "External Address is: " << ansaddr; + std::cerr << std::endl; +#endif + return true; + } + +#ifdef BOOTSTRAP_DEBUG + std::cerr << "received Data (not Stun Reply) from : " << fromaddr; + std::cerr << std::endl; +#endif + return false; +} + diff --git a/libretroshare/src/pqi/Makefile b/libretroshare/src/pqi/Makefile index 1a7fcea3e..37e0db158 100644 --- a/libretroshare/src/pqi/Makefile +++ b/libretroshare/src/pqi/Makefile @@ -34,15 +34,18 @@ RSOBJ = $(BASE_OBJ) $(LOOP_OBJ) \ $(GRP_OBJ) \ $(OTHER_OBJ) -TESTOBJ = net_test.o dht_test.o +TESTOBJ = xpgp_id.o net_test.o dht_test.o #conn_test.o -TESTS = net_test dht_test +TESTS = xpgp_id net_test dht_test #conn_test all: librs tests +xpgp_id: xpgp_id.o + $(CC) $(CFLAGS) -o xpgp_id xpgp_id.o $(LIBS) + dht_test: dht_test.o $(CC) $(CFLAGS) -o dht_test dht_test.o $(LIBS) diff --git a/libretroshare/src/pqi/authxpgp.cc b/libretroshare/src/pqi/authxpgp.cc index ca2c3b896..6a5c4e7da 100644 --- a/libretroshare/src/pqi/authxpgp.cc +++ b/libretroshare/src/pqi/authxpgp.cc @@ -1186,7 +1186,7 @@ bool AuthXPGP::ProcessXPGP(XPGP *xpgp, std::string &id) } -bool AuthXPGP::getXPGPid(XPGP *xpgp, std::string &xpgpid) +bool getXPGPid(XPGP *xpgp, std::string &xpgpid) { #ifdef AUTHXPGP_DEBUG std::cerr << "AuthXPGP::getXPGPid()"; @@ -1320,7 +1320,7 @@ int pem_passwd_cb(char *buf, int size, int rwflag, void *password) // Not dependent on sslroot. load, and detroys the XPGP memory. -int LoadCheckXPGPandGetName(const char *cert_file, std::string &userName) +int LoadCheckXPGPandGetName(const char *cert_file, std::string &userName, std::string &userId) { /* This function loads the XPGP certificate from the file, * and checks the certificate @@ -1352,6 +1352,11 @@ int LoadCheckXPGPandGetName(const char *cert_file, std::string &userName) userName = getX509CNString(xpgp->subject->subject); } + if (!getXPGPid(xpgp, userId)) + { + valid = false; + } + // clean up. XPGP_free(xpgp); diff --git a/libretroshare/src/pqi/authxpgp.h b/libretroshare/src/pqi/authxpgp.h index e7b18af25..66b0188e8 100644 --- a/libretroshare/src/pqi/authxpgp.h +++ b/libretroshare/src/pqi/authxpgp.h @@ -144,7 +144,6 @@ bool loadCertificates(bool &oldFormat, std::map &key /* Helper Functions */ -bool getXPGPid(XPGP *xpgp, std::string &xpgpid); bool ProcessXPGP(XPGP *xpgp, std::string &id); XPGP * loadXPGPFromPEM(std::string pem); @@ -191,7 +190,9 @@ std::list getXPGPsigners(XPGP *cert); std::string getXPGPInfo(XPGP *cert); std::string getXPGPAuthCode(XPGP *xpgp); -int LoadCheckXPGPandGetName(const char *cert_file, std::string &userName); +int LoadCheckXPGPandGetName(const char *cert_file, + std::string &userName, std::string &userId); +bool getXPGPid(XPGP *xpgp, std::string &xpgpid); #endif // MRK_SSL_XPGP_CERT_HEADER diff --git a/libretroshare/src/pqi/p3dhtmgr.cc b/libretroshare/src/pqi/p3dhtmgr.cc index 029e7751e..5f6ec24bd 100644 --- a/libretroshare/src/pqi/p3dhtmgr.cc +++ b/libretroshare/src/pqi/p3dhtmgr.cc @@ -31,8 +31,8 @@ #include "util/rsprint.h" -#define DHT_DEBUG 1 /***** + * #define DHT_DEBUG 1 * #define P3DHTMGR_USE_LOCAL_UDP_CONN 1 // For Testing only ****/ diff --git a/libretroshare/src/pqi/xpgp_id.cc b/libretroshare/src/pqi/xpgp_id.cc new file mode 100644 index 000000000..c122d401f --- /dev/null +++ b/libretroshare/src/pqi/xpgp_id.cc @@ -0,0 +1,44 @@ + + +/***** Extract XPGP Id *****/ + +#include "pqi/authxpgp.h" + +#include +#include + +int main(int argc, char **argv) +{ + if (argc < 2) + { + std::cerr << "Usage: " << argv[0] << " "; + std::cerr << std::endl; + exit(1); + } + + std::string userName, userId; + + if (LoadCheckXPGPandGetName(argv[1], userName, userId)) + { + std::cerr << "Cert Ok: name: " << userName; + std::cerr << std::endl; + std::cerr << "id = \"" << userId << "\""; + std::cerr << std::endl; + } + else + { + std::cerr << "Cert Check Failed"; + std::cerr << std::endl; + } +} + + + + + + + + + + + diff --git a/libretroshare/src/rsserver/p3face-startup.cc b/libretroshare/src/rsserver/p3face-startup.cc index df869b415..08dd4fdd2 100644 --- a/libretroshare/src/rsserver/p3face-startup.cc +++ b/libretroshare/src/rsserver/p3face-startup.cc @@ -370,10 +370,12 @@ int InitRetroShare(int argcIgnored, char **argvIgnored, RsInit *config) /******************************** WINDOWS/UNIX SPECIFIC PART ******************/ std::string userName; + std::string userId; bool existingUser = false; - if (LoadCheckXPGPandGetName(config->load_cert.c_str(), userName)) + if (LoadCheckXPGPandGetName(config->load_cert.c_str(), userName, userId)) { std::cerr << "Existing Name: " << userName << std::endl; + std::cerr << "Existing Id: " << userId << std::endl; existingUser = true; } else @@ -773,16 +775,18 @@ int LoadCertificates(RsInit *config, bool autoLoginNT) bool ValidateCertificate(RsInit *config, std::string &userName) { std::string fname = config->load_cert; + std::string userId; if (fname != "") { - return LoadCheckXPGPandGetName(fname.c_str(), userName); + return LoadCheckXPGPandGetName(fname.c_str(), userName, userId); } return false; } bool ValidateTrustedUser(RsInit *config, std::string fname, std::string &userName) { - bool valid = LoadCheckXPGPandGetName(fname.c_str(), userName); + std::string userId; + bool valid = LoadCheckXPGPandGetName(fname.c_str(), userName, userId); if (valid) { config -> load_trustedpeer = true; diff --git a/libretroshare/src/serialiser/Makefile b/libretroshare/src/serialiser/Makefile index ca775ede9..db02efa08 100644 --- a/libretroshare/src/serialiser/Makefile +++ b/libretroshare/src/serialiser/Makefile @@ -22,7 +22,8 @@ TESTOBJ += rsserial_test.o TESTS = tlvbase_test tlvbase_test2 tlvfileitem_test TESTS += tlvitems_test tlvstack_test tlvconfig_test -TESTS += rsserial_test +TESTS += +#rsserial_test #rsbaseitem_test diff --git a/libretroshare/src/services/p3photoservice.cc b/libretroshare/src/services/p3photoservice.cc index 121014a2a..81d5e8627 100644 --- a/libretroshare/src/services/p3photoservice.cc +++ b/libretroshare/src/services/p3photoservice.cc @@ -774,12 +774,27 @@ std::string generateRandomShowId() std::ostringstream out; out << std::hex; - /* 4 bytes per random number: 4 x 4 = 16 bytes */ - for(int i = 0; i < 4; i++) - { - uint32_t rint = random(); - out << rint; - } +/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ +#ifndef WINDOWS_SYS + /* 4 bytes per random number: 4 x 4 = 16 bytes */ + for(int i = 0; i < 4; i++) + { + out << std::setw(8) << std::setfill('0'); + uint32_t rint = random(); + out << rint; + } +#else + srand(time(NULL)); + /* 2 bytes per random number: 8 x 2 = 16 bytes */ + for(int i = 0; i < 8; i++) + { + out << std::setw(4) << std::setfill('0'); + uint16_t rint = rand(); /* only gives 16 bits */ + out << rint; + } +#endif +/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ + return out.str(); } diff --git a/libretroshare/src/tcponudp/udpsorter.cc b/libretroshare/src/tcponudp/udpsorter.cc index d2745b523..26b070a4c 100644 --- a/libretroshare/src/tcponudp/udpsorter.cc +++ b/libretroshare/src/tcponudp/udpsorter.cc @@ -69,7 +69,7 @@ void UdpSorter::recvPkt(void *data, int size, struct sockaddr_in &from) it = streams.find(from); /* check for STUN packet */ - if (isStunPacket(data, size)) + if (UdpStun_isStunPacket(data, size)) { std::cerr << "UdpSorter::recvPkt() is Stun Packet"; std::cerr << std::endl; @@ -230,7 +230,7 @@ bool UdpSorter::locked_handleStunPkt(void *data, int size, struct sockaddr_in &f /* generate a response */ int len; - void *pkt = generate_stun_reply(&from, &len); + void *pkt = UdpStun_generate_stun_reply(&from, &len); if (!pkt) return false; @@ -252,7 +252,7 @@ bool UdpSorter::locked_handleStunPkt(void *data, int size, struct sockaddr_in &f #endif /* got response */ struct sockaddr_in eAddr; - bool good = response(data, size, eAddr); + bool good = UdpStun_response(data, size, eAddr); if (good) { #ifdef DEBUG_UDP_SORTER @@ -310,7 +310,7 @@ int UdpSorter::doStun(struct sockaddr_in stun_addr) #define MAX_STUN_SIZE 64 char stundata[MAX_STUN_SIZE]; int tmplen = MAX_STUN_SIZE; - bool done = generate_stun_pkt(stundata, &tmplen); + bool done = UdpStun_generate_stun_pkt(stundata, &tmplen); if (!done) { #ifdef DEBUG_UDP_SORTER @@ -343,7 +343,11 @@ int UdpSorter::doStun(struct sockaddr_in stun_addr) return 1; } -bool UdpSorter::response(void *stun_pkt, int size, struct sockaddr_in &addr) +/******************************* STUN Handling ********************************/ +/***** These next functions are generic and not dependent on class variables **/ +/******************************* STUN Handling ********************************/ + +bool UdpStun_response(void *stun_pkt, int size, struct sockaddr_in &addr) { /* check what type it is */ if (size < 28) @@ -376,7 +380,7 @@ bool UdpSorter::response(void *stun_pkt, int size, struct sockaddr_in &addr) } -bool UdpSorter::generate_stun_pkt(void *stun_pkt, int *len) +bool UdpStun_generate_stun_pkt(void *stun_pkt, int *len) { if (*len < 20) { @@ -396,7 +400,7 @@ bool UdpSorter::generate_stun_pkt(void *stun_pkt, int *len) } -void *UdpSorter::generate_stun_reply(struct sockaddr_in *stun_addr, int *len) +void *UdpStun_generate_stun_reply(struct sockaddr_in *stun_addr, int *len) { /* just the header */ void *stun_pkt = malloc(28); @@ -422,7 +426,7 @@ void *UdpSorter::generate_stun_reply(struct sockaddr_in *stun_addr, int *len) return stun_pkt; } -bool UdpSorter::isStunPacket(void *data, int size) +bool UdpStun_isStunPacket(void *data, int size) { #ifdef DEBUG_UDP_SORTER std::cerr << "UdpSorter::isStunPacket() ?"; diff --git a/libretroshare/src/tcponudp/udpsorter.h b/libretroshare/src/tcponudp/udpsorter.h index 30cc6ead8..43e6c86c7 100644 --- a/libretroshare/src/tcponudp/udpsorter.h +++ b/libretroshare/src/tcponudp/udpsorter.h @@ -99,14 +99,10 @@ int status(std::ostream &out); private: /* STUN handling */ -bool isStunPacket(void *data, int size); bool locked_handleStunPkt(void *data, int size, struct sockaddr_in &from); int doStun(struct sockaddr_in stun_addr); -bool response(void *stun_pkt, int size, struct sockaddr_in &addr); -void *generate_stun_reply(struct sockaddr_in *stun_addr, int *len); -bool generate_stun_pkt(void *stun_pkt, int *len); /* stun keepAlive */ bool locked_printStunList(); @@ -137,4 +133,11 @@ bool storeStunPeer(const struct sockaddr_in &remote, const char *peerid); }; + /* generic stun functions */ + +bool UdpStun_isStunPacket(void *data, int size); +bool UdpStun_response(void *stun_pkt, int size, struct sockaddr_in &addr); +void *UdpStun_generate_stun_reply(struct sockaddr_in *stun_addr, int *len); +bool UdpStun_generate_stun_pkt(void *stun_pkt, int *len); + #endif