From 0b1126babf3b9620f3a9464cc39eca6e5d494e0c Mon Sep 17 00:00:00 2001 From: thunder2 Date: Sun, 18 Mar 2012 01:33:25 +0000 Subject: [PATCH] Added thread safe function for inet_ntoa in libbitdht. Used the existing thread safe function for inet_ntoa in libretroshare. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5033 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libbitdht/src/bitdht/bdfilter.cc | 2 +- libbitdht/src/bitdht/bdmanager.cc | 2 +- libbitdht/src/bitdht/bdnode.cc | 2 +- libbitdht/src/bitdht/bdstddht.cc | 2 +- libbitdht/src/bitdht/bdstore.cc | 2 +- libbitdht/src/udp/udplayer.cc | 2 +- libbitdht/src/util/bdnet.cc | 12 ++++++++++++ libbitdht/src/util/bdnet.h | 3 +++ libretroshare/src/dht/p3bitdht_interface.cc | 4 ++-- libretroshare/src/util/rsnet.cc | 2 +- 10 files changed, 24 insertions(+), 9 deletions(-) diff --git a/libbitdht/src/bitdht/bdfilter.cc b/libbitdht/src/bitdht/bdfilter.cc index 163693ba3..0efd0875f 100644 --- a/libbitdht/src/bitdht/bdfilter.cc +++ b/libbitdht/src/bitdht/bdfilter.cc @@ -126,7 +126,7 @@ int bdFilter::addPeerToFilter(const bdId *id, uint32_t flags) 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 << "Adding New Banned Ip Address: " << bdnet_inet_ntoa(id->addr.sin_addr); std::cerr << std::endl; return true; diff --git a/libbitdht/src/bitdht/bdmanager.cc b/libbitdht/src/bitdht/bdmanager.cc index 55030d179..d03039de3 100644 --- a/libbitdht/src/bitdht/bdmanager.cc +++ b/libbitdht/src/bitdht/bdmanager.cc @@ -1031,7 +1031,7 @@ int bdNodeManager::getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &fro from = pit->second.mDhtAddr; std::cerr << "bdNodeManager::getDhtPeerAddress() Found Peer Address:"; - std::cerr << inet_ntoa(from.sin_addr) << ":" << htons(from.sin_port); + std::cerr << bdnet_inet_ntoa(from.sin_addr) << ":" << htons(from.sin_port); std::cerr << std::endl; return 1; diff --git a/libbitdht/src/bitdht/bdnode.cc b/libbitdht/src/bitdht/bdnode.cc index 758cb89ed..66e918f54 100644 --- a/libbitdht/src/bitdht/bdnode.cc +++ b/libbitdht/src/bitdht/bdnode.cc @@ -2234,7 +2234,7 @@ bdNodeNetMsg::bdNodeNetMsg(char *msg, int len, struct sockaddr_in *in_addr) void bdNodeNetMsg::print(std::ostream &out) { out << "bdNodeNetMsg::print(" << mSize << ") to " - << inet_ntoa(addr.sin_addr) << ":" << htons(addr.sin_port); + << bdnet_inet_ntoa(addr.sin_addr) << ":" << htons(addr.sin_port); out << std::endl; } diff --git a/libbitdht/src/bitdht/bdstddht.cc b/libbitdht/src/bitdht/bdstddht.cc index 6a2d5abec..7ccc2b060 100644 --- a/libbitdht/src/bitdht/bdstddht.cc +++ b/libbitdht/src/bitdht/bdstddht.cc @@ -209,7 +209,7 @@ void bdStdPrintNodeId(std::ostream &out, const bdNodeId *a) void bdStdPrintId(std::ostream &out, const bdId *a) { bdStdPrintNodeId(out, &(a->id)); - out << " ip:" << inet_ntoa(a->addr.sin_addr); + out << " ip:" << bdnet_inet_ntoa(a->addr.sin_addr); out << ":" << ntohs(a->addr.sin_port); return; } diff --git a/libbitdht/src/bitdht/bdstore.cc b/libbitdht/src/bitdht/bdstore.cc index bf51b16f4..d201234e9 100644 --- a/libbitdht/src/bitdht/bdstore.cc +++ b/libbitdht/src/bitdht/bdstore.cc @@ -227,7 +227,7 @@ void bdStore::writeStore(std::string file) std::list::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)); + fprintf(fd, "%s %d\n", bdnet_inet_ntoa(it->mPeerId.addr.sin_addr).c_str(), 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 diff --git a/libbitdht/src/udp/udplayer.cc b/libbitdht/src/udp/udplayer.cc index c618a8a9b..7dd521e31 100644 --- a/libbitdht/src/udp/udplayer.cc +++ b/libbitdht/src/udp/udplayer.cc @@ -83,7 +83,7 @@ class udpPacket //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 << "[" << bdnet_inet_ntoa(addr.sin_addr) << ":"; out << htons(addr.sin_port) << "]"; return out; } diff --git a/libbitdht/src/util/bdnet.cc b/libbitdht/src/util/bdnet.cc index 887230a2c..5f137e091 100644 --- a/libbitdht/src/util/bdnet.cc +++ b/libbitdht/src/util/bdnet.cc @@ -27,6 +27,7 @@ #include "bdnet.h" #include +#include #include #include @@ -356,4 +357,15 @@ void bdsockaddr_clear(struct sockaddr_in *addr) memset(addr, 0, sizeof(*addr)); } +/* thread-safe version of inet_ntoa */ +std::string bdnet_inet_ntoa(struct in_addr in) +{ + std::ostringstream str; + uint8_t *bytes = (uint8_t *) &(in.s_addr); + str << (int) bytes[0] << "."; + str << (int) bytes[1] << "."; + str << (int) bytes[2] << "."; + str << (int) bytes[3]; + return str.str(); +} diff --git a/libbitdht/src/util/bdnet.h b/libbitdht/src/util/bdnet.h index 2fc5f4a50..6acba2922 100644 --- a/libbitdht/src/util/bdnet.h +++ b/libbitdht/src/util/bdnet.h @@ -27,6 +27,7 @@ */ #include +#include /********************************** WINDOWS/UNIX SPECIFIC PART ******************/ #if defined(_WIN32) || defined(__MINGW32__) @@ -104,6 +105,8 @@ int bdnet_inet_aton(const char *name, struct in_addr *addr); int bdnet_checkTTL(int fd); void bdsockaddr_clear(struct sockaddr_in *addr); +/* thread-safe version of inet_ntoa */ +std::string bdnet_inet_ntoa(struct in_addr in); /* Extra stuff to declare for windows error handling (mimics unix errno) */ diff --git a/libretroshare/src/dht/p3bitdht_interface.cc b/libretroshare/src/dht/p3bitdht_interface.cc index 8753768ff..ff17e96ae 100644 --- a/libretroshare/src/dht/p3bitdht_interface.cc +++ b/libretroshare/src/dht/p3bitdht_interface.cc @@ -156,7 +156,7 @@ std::string p3BitDht::getUdpAddressString() if (mDhtStunner->externalAddr(extAddr, extStable)) { - out << " DhtExtAddr: " << inet_ntoa(extAddr.sin_addr); + out << " DhtExtAddr: " << rs_inet_ntoa(extAddr.sin_addr); out << ":" << ntohs(extAddr.sin_port); if (extStable) @@ -174,7 +174,7 @@ std::string p3BitDht::getUdpAddressString() } if (mProxyStunner->externalAddr(extAddr, extStable)) { - out << " ProxyExtAddr: " << inet_ntoa(extAddr.sin_addr); + out << " ProxyExtAddr: " << rs_inet_ntoa(extAddr.sin_addr); out << ":" << ntohs(extAddr.sin_port); if (extStable) diff --git a/libretroshare/src/util/rsnet.cc b/libretroshare/src/util/rsnet.cc index 3ef5254e0..edea8febd 100644 --- a/libretroshare/src/util/rsnet.cc +++ b/libretroshare/src/util/rsnet.cc @@ -132,7 +132,7 @@ bool isExternalNet(const struct in_addr *addr) std::ostream &operator<<(std::ostream &out, const struct sockaddr_in &addr) { - out << "[" << inet_ntoa(addr.sin_addr) << ":"; + out << "[" << rs_inet_ntoa(addr.sin_addr) << ":"; out << htons(addr.sin_port) << "]"; return out; }