mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-29 16:47:25 -04:00
Massive commit - changing from sockaddr_in => sockaddr_storage.
In preparation for making RS support IPv6. NB: This breaks the build of retroshare-gui, as the sockaddr_storage_xxx fns are only defined as prototypes for now. All the aux libraries like udp / stun / tcponudp / dht have still to be converted. These changes will probably break various things and need to be tested thoroughly. git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.6-initdev@6735 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
fd071161bf
commit
6290d8fed9
66 changed files with 1182 additions and 1046 deletions
|
@ -1,6 +1,8 @@
|
|||
#include "dnsresolver.h"
|
||||
|
||||
#include "pqi/pqinetwork.h"
|
||||
#include "util/rsnet.h"
|
||||
|
||||
|
||||
#ifndef WIN32
|
||||
#include <netdb.h>
|
||||
|
@ -64,13 +66,19 @@ void *solveDNSEntries(void *p)
|
|||
{
|
||||
RsStackMutex mut(dnsr->_rdnsMtx) ;
|
||||
|
||||
DNSResolver::AddrInfo &info = (*dnsr->_addr_map)[next_call];
|
||||
|
||||
if(pHost)
|
||||
{
|
||||
(*dnsr->_addr_map)[next_call].state = DNSResolver::DNS_HAVE ;
|
||||
(*dnsr->_addr_map)[next_call].addr.s_addr = *(unsigned long*) (pHost->h_addr);
|
||||
info.state = DNSResolver::DNS_HAVE ;
|
||||
// IPv4 for the moment.
|
||||
struct sockaddr_in *addrv4p = (struct sockaddr_in *) &(info.addr);
|
||||
addrv4p->sin_family = AF_INET;
|
||||
addrv4p->sin_addr.s_addr = *(unsigned long*) (pHost->h_addr);
|
||||
addrv4p->sin_port = htons(0);
|
||||
}
|
||||
else
|
||||
(*dnsr->_addr_map)[next_call].state = DNSResolver::DNS_LOOKUP_ERROR ;
|
||||
info.state = DNSResolver::DNS_LOOKUP_ERROR ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -103,9 +111,9 @@ void DNSResolver::reset()
|
|||
_addr_map->clear();
|
||||
}
|
||||
|
||||
bool DNSResolver::getIPAddressFromString(const std::string& server_name,struct in_addr& addr)
|
||||
bool DNSResolver::getIPAddressFromString(const std::string& server_name,struct sockaddr_storage &addr)
|
||||
{
|
||||
addr.s_addr = 0 ;
|
||||
sockaddr_storage_clear(addr);
|
||||
bool running = false;
|
||||
{
|
||||
RsStackMutex mut(_rdnsMtx) ;
|
||||
|
|
|
@ -18,7 +18,7 @@ class DNSResolver
|
|||
DNSResolver() ;
|
||||
~DNSResolver() ;
|
||||
|
||||
bool getIPAddressFromString(const std::string& server_name,struct in_addr& addr) ;
|
||||
bool getIPAddressFromString(const std::string& server_name,struct sockaddr_storage &addr) ;
|
||||
|
||||
void start_request() ;
|
||||
void reset() ;
|
||||
|
@ -30,7 +30,7 @@ class DNSResolver
|
|||
{
|
||||
uint32_t state ; // state: Looked-up, not found, have
|
||||
time_t last_lookup_time ; // last lookup time
|
||||
struct in_addr addr ;
|
||||
struct sockaddr_storage addr ;
|
||||
};
|
||||
friend void *solveDNSEntries(void *p) ;
|
||||
|
||||
|
|
|
@ -150,11 +150,11 @@ void* doExtAddrSearch(void *p)
|
|||
// thread safe copy results.
|
||||
//
|
||||
{
|
||||
RsStackMutex mtx(af->_addrMtx) ;
|
||||
RsStackMutex mtx(af->mAddrMtx) ;
|
||||
|
||||
*(af->_found) = false ;
|
||||
*(af->mFoundTS) = time(NULL) ;
|
||||
*(af->_searching) = false ;
|
||||
af->mFound = false ;
|
||||
af->mFoundTS = time(NULL) ;
|
||||
af->mSearching = false ;
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
return NULL ;
|
||||
|
@ -163,24 +163,25 @@ void* doExtAddrSearch(void *p)
|
|||
sort(res.begin(),res.end()) ; // eliminates outliers.
|
||||
|
||||
|
||||
if(!inet_aton(res[res.size()/2].c_str(),af->_addr))
|
||||
|
||||
if(!sockaddr_storage_ipv4_aton(af->mAddr, res[res.size()/2].c_str()))
|
||||
{
|
||||
std::cerr << "ExtAddrFinder: Could not convert " << res[res.size()/2] << " into an address." << std::endl ;
|
||||
{
|
||||
RsStackMutex mtx(af->_addrMtx) ;
|
||||
*(af->_found) = false ;
|
||||
*(af->mFoundTS) = time(NULL) ;
|
||||
*(af->_searching) = false ;
|
||||
RsStackMutex mtx(af->mAddrMtx) ;
|
||||
af->mFound = false ;
|
||||
af->mFoundTS = time(NULL) ;
|
||||
af->mSearching = false ;
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
{
|
||||
RsStackMutex mtx(af->_addrMtx) ;
|
||||
*(af->_found) = true ;
|
||||
*(af->mFoundTS) = time(NULL) ;
|
||||
*(af->_searching) = false ;
|
||||
RsStackMutex mtx(af->mAddrMtx) ;
|
||||
af->mFound = true ;
|
||||
af->mFoundTS = time(NULL) ;
|
||||
af->mSearching = false ;
|
||||
}
|
||||
|
||||
pthread_exit(NULL);
|
||||
|
@ -196,44 +197,44 @@ void ExtAddrFinder::start_request()
|
|||
pthread_detach(tid); /* so memory is reclaimed in linux */
|
||||
}
|
||||
|
||||
bool ExtAddrFinder::hasValidIP(struct in_addr *addr)
|
||||
bool ExtAddrFinder::hasValidIP(struct sockaddr_storage &addr)
|
||||
{
|
||||
#ifdef EXTADDRSEARCH_DEBUG
|
||||
std::cerr << "ExtAddrFinder: Getting ip." << std::endl ;
|
||||
#endif
|
||||
|
||||
{
|
||||
RsStackMutex mut(_addrMtx) ;
|
||||
if(*_found)
|
||||
RsStackMutex mut(mAddrMtx) ;
|
||||
if(mFound)
|
||||
{
|
||||
#ifdef EXTADDRSEARCH_DEBUG
|
||||
std::cerr << "ExtAddrFinder: Has stored ip: responding with this ip." << std::endl ;
|
||||
#endif
|
||||
*addr = *_addr;
|
||||
addr = mAddr;
|
||||
}
|
||||
}
|
||||
time_t delta;
|
||||
{
|
||||
RsStackMutex mut(_addrMtx) ;
|
||||
RsStackMutex mut(mAddrMtx) ;
|
||||
//timeout the current ip
|
||||
delta = time(NULL) - *mFoundTS;
|
||||
delta = time(NULL) - mFoundTS;
|
||||
}
|
||||
if((uint32_t)delta > MAX_IP_STORE) {//launch a research
|
||||
if( _addrMtx.trylock())
|
||||
if( mAddrMtx.trylock())
|
||||
{
|
||||
if(!*_searching)
|
||||
if(!mSearching)
|
||||
{
|
||||
#ifdef EXTADDRSEARCH_DEBUG
|
||||
std::cerr << "ExtAddrFinder: No stored ip: Initiating new search." << std::endl ;
|
||||
#endif
|
||||
*_searching = true ;
|
||||
mSearching = true ;
|
||||
start_request() ;
|
||||
}
|
||||
#ifdef EXTADDRSEARCH_DEBUG
|
||||
else
|
||||
std::cerr << "ExtAddrFinder: Already searching." << std::endl ;
|
||||
#endif
|
||||
_addrMtx.unlock();
|
||||
mAddrMtx.unlock();
|
||||
}
|
||||
#ifdef EXTADDRSEARCH_DEBUG
|
||||
else
|
||||
|
@ -241,24 +242,17 @@ bool ExtAddrFinder::hasValidIP(struct in_addr *addr)
|
|||
#endif
|
||||
}
|
||||
|
||||
RsStackMutex mut(_addrMtx) ;
|
||||
return *_found ;
|
||||
RsStackMutex mut(mAddrMtx) ;
|
||||
return mFound ;
|
||||
}
|
||||
|
||||
void ExtAddrFinder::reset()
|
||||
{
|
||||
// while(*_searching)
|
||||
//#ifdef WIN32
|
||||
// Sleep(1000) ;
|
||||
//#else
|
||||
// sleep(1) ;
|
||||
//#endif
|
||||
RsStackMutex mut(mAddrMtx) ;
|
||||
|
||||
RsStackMutex mut(_addrMtx) ;
|
||||
|
||||
*_found = false ;
|
||||
*_searching = false ;
|
||||
*mFoundTS = time(NULL) - MAX_IP_STORE;
|
||||
mFound = false ;
|
||||
mSearching = false ;
|
||||
mFoundTS = time(NULL) - MAX_IP_STORE;
|
||||
}
|
||||
|
||||
ExtAddrFinder::~ExtAddrFinder()
|
||||
|
@ -266,37 +260,20 @@ ExtAddrFinder::~ExtAddrFinder()
|
|||
#ifdef EXTADDRSEARCH_DEBUG
|
||||
std::cerr << "ExtAddrFinder: Deleting ExtAddrFinder." << std::endl ;
|
||||
#endif
|
||||
// while(*_searching)
|
||||
//#ifdef WIN32
|
||||
// Sleep(1000) ;
|
||||
//#else
|
||||
// sleep(1) ;
|
||||
//#endif
|
||||
|
||||
RsStackMutex mut(_addrMtx) ;
|
||||
|
||||
delete _found ;
|
||||
delete _searching ;
|
||||
free (_addr) ;
|
||||
}
|
||||
|
||||
ExtAddrFinder::ExtAddrFinder() : _addrMtx("ExtAddrFinder")
|
||||
ExtAddrFinder::ExtAddrFinder() : mAddrMtx("ExtAddrFinder")
|
||||
{
|
||||
#ifdef EXTADDRSEARCH_DEBUG
|
||||
std::cerr << "ExtAddrFinder: Creating new ExtAddrFinder." << std::endl ;
|
||||
#endif
|
||||
RsStackMutex mut(_addrMtx) ;
|
||||
RsStackMutex mut(mAddrMtx) ;
|
||||
|
||||
_found = new bool ;
|
||||
*_found = false ;
|
||||
|
||||
_searching = new bool ;
|
||||
*_searching = false ;
|
||||
|
||||
mFoundTS = new time_t;
|
||||
*mFoundTS = time(NULL) - MAX_IP_STORE;
|
||||
|
||||
_addr = (in_addr*)malloc(sizeof(in_addr)) ;
|
||||
mFound = false;
|
||||
mSearching = false;
|
||||
mFoundTS = time(NULL) - MAX_IP_STORE;
|
||||
sockaddr_storage_clear(mAddr);
|
||||
|
||||
_ip_servers.push_back(std::string( "checkip.dyndns.org" )) ;
|
||||
_ip_servers.push_back(std::string( "www.myip.dk" )) ;
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <list>
|
||||
#include <string>
|
||||
#include "util/rsthreads.h"
|
||||
#include "util/rsnet.h"
|
||||
|
||||
struct sockaddr ;
|
||||
|
||||
|
@ -12,7 +13,7 @@ class ExtAddrFinder
|
|||
ExtAddrFinder() ;
|
||||
~ExtAddrFinder() ;
|
||||
|
||||
bool hasValidIP(struct in_addr *addr) ;
|
||||
bool hasValidIP(struct sockaddr_storage &addr) ;
|
||||
void getIPServersList(std::list<std::string>& ip_servers) { ip_servers = _ip_servers ; }
|
||||
|
||||
void start_request() ;
|
||||
|
@ -22,10 +23,10 @@ class ExtAddrFinder
|
|||
private:
|
||||
friend void* doExtAddrSearch(void *p) ;
|
||||
|
||||
time_t *mFoundTS;
|
||||
RsMutex _addrMtx ;
|
||||
struct in_addr *_addr ;
|
||||
bool *_found ;
|
||||
bool *_searching ;
|
||||
RsMutex mAddrMtx ;
|
||||
time_t mFoundTS;
|
||||
struct sockaddr_storage mAddr;
|
||||
bool mFound ;
|
||||
bool mSearching ;
|
||||
std::list<std::string> _ip_servers ;
|
||||
};
|
||||
|
|
|
@ -75,4 +75,46 @@ std::ostream& operator<<(std::ostream& o,const struct sockaddr_in&) ;
|
|||
std::string rs_inet_ntoa(struct in_addr in);
|
||||
|
||||
|
||||
/***************************/
|
||||
// sockaddr_storage fns.
|
||||
|
||||
void sockaddr_storage_clear(struct sockaddr_storage &addr);
|
||||
|
||||
// mods.
|
||||
bool sockaddr_storage_zeroip(struct sockaddr_storage &addr);
|
||||
bool sockaddr_storage_copyip(struct sockaddr_storage &dst, const struct sockaddr_storage &src);
|
||||
uint16_t sockaddr_storage_port(const struct sockaddr_storage &addr);
|
||||
bool sockaddr_storage_setport(struct sockaddr_storage &addr, uint16_t port);
|
||||
|
||||
bool sockaddr_storage_ipv4_aton(struct sockaddr_storage &addr, const char *name);
|
||||
bool sockaddr_storage_ipv4_setport(struct sockaddr_storage &addr, const uint16_t port);
|
||||
|
||||
// comparisons.
|
||||
bool operator<(const struct sockaddr_storage &a, const struct sockaddr_storage &b);
|
||||
|
||||
bool sockaddr_storage_same(const struct sockaddr_storage &addr, const struct sockaddr_storage &addr2);
|
||||
bool sockaddr_storage_samefamily(const struct sockaddr_storage &addr, const struct sockaddr_storage &addr2);
|
||||
bool sockaddr_storage_sameip(const struct sockaddr_storage &addr, const struct sockaddr_storage &addr2);
|
||||
bool sockaddr_storage_samenet(const struct sockaddr_storage &addr, const struct sockaddr_storage &addr2);
|
||||
bool sockaddr_storage_samesubnet(const struct sockaddr_storage &addr, const struct sockaddr_storage &addr2);
|
||||
|
||||
// string,
|
||||
std::string sockaddr_storage_tostring(const struct sockaddr_storage &addr);
|
||||
std::string sockaddr_storage_familytostring(const struct sockaddr_storage &addr);
|
||||
std::string sockaddr_storage_iptostring(const struct sockaddr_storage &addr);
|
||||
std::string sockaddr_storage_porttostring(const struct sockaddr_storage &addr);
|
||||
|
||||
// output
|
||||
//void sockaddr_storage_output(const struct sockaddr_storage &addr, std::ostream &out);
|
||||
//void sockaddr_storage_ipoutput(const struct sockaddr_storage &addr, std::ostream &out);
|
||||
|
||||
// net checks.
|
||||
bool sockaddr_storage_isnull(const struct sockaddr_storage &addr);
|
||||
bool sockaddr_storage_isValidNet(const struct sockaddr_storage &addr);
|
||||
bool sockaddr_storage_isLoopbackNet(const struct sockaddr_storage &addr);
|
||||
bool sockaddr_storage_isPrivateNet(const struct sockaddr_storage &addr);
|
||||
bool sockaddr_storage_isExternalNet(const struct sockaddr_storage &addr);
|
||||
|
||||
|
||||
|
||||
#endif /* RS_UNIVERSAL_NETWORK_HEADER */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue