From 85db02c599647665633a11c830d514c96bd9aa5b Mon Sep 17 00:00:00 2001 From: Bert Hubert Date: Mon, 14 Feb 2011 09:58:10 +0000 Subject: [PATCH] phase out sockAddrToString function (ComboAddress has a better one) teach ComboAddress to accept 1.2.3.4:53 as well as [::]:53 git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@2010 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- pdns/dnsproxy.cc | 2 +- pdns/iputils.hh | 8 +++-- pdns/misc.cc | 68 ++++++++++++++++++++++++++++++------------- pdns/misc.hh | 3 +- pdns/pdns_recursor.cc | 6 ++-- 5 files changed, 58 insertions(+), 29 deletions(-) diff --git a/pdns/dnsproxy.cc b/pdns/dnsproxy.cc index eec5dd2ee..9b77666ba 100644 --- a/pdns/dnsproxy.cc +++ b/pdns/dnsproxy.cc @@ -139,7 +139,7 @@ int DNSProxy::getID_locked() else if(i->second.createdsecond.created) L<second.remote)<<" with internal id "<second.remote.toStringWithPort()<<" with internal id "<sin_family == AF_INET) { - struct sockaddr_in sip; - memcpy(&sip,(struct sockaddr_in*)remote,sizeof(sip)); - return inet_ntoa(sip.sin_addr); - } - else { - char tmp[128]; - - if(!Utility::inet_ntop(AF_INET6, ( const char * ) &((struct sockaddr_in6 *)remote)->sin6_addr, tmp, sizeof(tmp))) - return "IPv6 untranslateable"; - - return tmp; - } -} - string makeHexDump(const string& str) { char tmp[5]; @@ -511,8 +494,6 @@ string makeHexDump(const string& str) return ret; } - - // shuffle, maintaining some semblance of order void shuffle(vector& rrs) { @@ -665,6 +646,18 @@ string dotConcat(const std::string& a, const std::string &b) int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret) { + if(addr.empty()) + return -1; + string ourAddr(addr); + int port = -1; + if(addr[0]=='[') { // [::]:53 style address + string::size_type pos = addr.find(']'); + if(pos == string::npos || pos + 2 > addr.size() || addr[pos+1]!=':') + return -1; + ourAddr.assign(addr.c_str() + 1, pos-1); + port = atoi(addr.c_str()+pos+2); + } + struct addrinfo* res; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); @@ -673,7 +666,7 @@ int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret) hints.ai_flags = AI_NUMERICHOST; int error; - if((error=getaddrinfo(addr.c_str(), 0, &hints, &res))) { + if((error=getaddrinfo(ourAddr.c_str(), 0, &hints, &res))) { // this is correct /* cerr<<"Error translating IPv6 address '"<ai_addr, res->ai_addrlen); - + if(port >= 0) + ret->sin6_port = htons(port); freeaddrinfo(res); return 0; } +int makeIPv4sockaddr(const string &str, struct sockaddr_in* ret) +{ + if(str.empty()) { + return -1; + } + struct in_addr inp; + + string::size_type pos = str.find(':'); + if(pos == string::npos) { // no port specified, not touching the port + if(Utility::inet_aton(str.c_str(), &inp)) { + ret->sin_addr.s_addr=inp.s_addr; + return 0; + } + return -1; + } + if(!*(str.c_str() + pos + 1)) // trailing : + return -1; + + char *eptr = (char*)str.c_str() + str.size(); + int port = strtol(str.c_str() + pos + 1, &eptr, 10); + if(*eptr) + return -1; + + ret->sin_port = htons(port); + if(Utility::inet_aton(str.substr(0, pos).c_str(), &inp)) { + ret->sin_addr.s_addr=inp.s_addr; + return 0; + } + return -1; +} + + //! read a line of text from a FILE* to a std::string, returns false on 'no data' bool stringfgets(FILE* fp, std::string& line) { diff --git a/pdns/misc.hh b/pdns/misc.hh index ebcfa4430..c366976cb 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -215,7 +215,7 @@ public: private: struct timeval d_set; }; -const string sockAddrToString(struct sockaddr_in *remote); + int sendData(const char *buffer, int replen, int outsock); inline void DTime::set() @@ -457,6 +457,7 @@ string makeRelative(const std::string& fqdn, const std::string& zone); string labelReverse(const std::string& qname); std::string dotConcat(const std::string& a, const std::string &b); int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret); +int makeIPv4sockaddr(const string &str, struct sockaddr_in* ret); bool stringfgets(FILE* fp, std::string& line); template diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 141f48613..c2a15f619 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -1384,7 +1384,7 @@ void handleUDPServerResponse(int fd, FDMultiplexer::funcparam_t& var) else { g_stats.serverParseError++; if(g_logCommonErrors) - L<