]> granicus.if.org Git - pdns/commitdiff
phase out sockAddrToString function (ComboAddress has a better one)
authorBert Hubert <bert.hubert@netherlabs.nl>
Mon, 14 Feb 2011 09:58:10 +0000 (09:58 +0000)
committerBert Hubert <bert.hubert@netherlabs.nl>
Mon, 14 Feb 2011 09:58:10 +0000 (09:58 +0000)
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
pdns/iputils.hh
pdns/misc.cc
pdns/misc.hh
pdns/pdns_recursor.cc

index eec5dd2ee46f818d7e33c93b272cd68cda3b2c53..9b77666bad97d319b577680a27a7e29f1437f530 100644 (file)
@@ -139,7 +139,7 @@ int DNSProxy::getID_locked()
     else if(i->second.created<time(0)-60) {
       if(i->second.created)
         L<<Logger::Warning<<"Recursive query for remote "<<
-          sockAddrToString((struct sockaddr_in *)&i->second.remote)<<" with internal id "<<n<<
+          i->second.remote.toStringWithPort()<<" with internal id "<<n<<
           " was not answered by backend within timeout, reusing id"<<endl;
       
       return n;
index b7e5e5f445273085916453847bdfb542c0a8f028..3af1dacda2e21c302acccea1d3eb451aa7688b44 100644 (file)
@@ -108,18 +108,20 @@ union ComboAddress {
     sin4.sin_port=0;
   }
 
+  // 'port' sets a default value in case 'str' does not set a port
   explicit ComboAddress(const string& str, uint16_t port=0)
   {
     memset(&sin6, 0, sizeof(sin6));
     sin4.sin_family = AF_INET;
-    
-    if(!IpToU32(str, (uint32_t*)&sin4.sin_addr.s_addr)) {
+    sin4.sin_port = 0;
+    if(makeIPv4sockaddr(str, &sin4)) {
       sin6.sin6_family = AF_INET6;
       if(makeIPv6sockaddr(str, &sin6) < 0)
         throw AhuException("Unable to convert presentation address '"+ str +"'"); 
       
     }
-    sin4.sin_port=htons(port);
+    if(!sin4.sin_port) // 'str' overrides port!
+      sin4.sin_port=htons(port);
   }
 
   bool isMappedIPv4()  const
index 8b811fc9e43a2047052d7441b148e069d7650518..b39e6e7d1d8f0b227d89be9d0e2bebfe333b9041 100644 (file)
@@ -481,23 +481,6 @@ string U32ToIP(uint32_t val)
 }
 
 
-const string sockAddrToString(struct sockaddr_in *remote) 
-{    
-  if(remote->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<DNSResourceRecord>& 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 '"<<addr<<"': ";
     if(error==EAI_SYSTEM)
@@ -685,11 +678,44 @@ int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret)
   }
   
   memcpy(ret, res->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)
 {
index ebcfa443058e2a581a611e1cad4918fa49f87c8a..c366976cb6992da5e9a7588c4844199aa5b87f12 100644 (file)
@@ -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<typename Index>
index 141f48613469e1c6ebfcb6a25e34d6103c94de5d..c2a15f61906429b775e43481bf7617f08874c075 100644 (file)
@@ -1384,7 +1384,7 @@ void handleUDPServerResponse(int fd, FDMultiplexer::funcparam_t& var)
     else {
       g_stats.serverParseError++; 
       if(g_logCommonErrors)
-        L<<Logger::Error<<"Unable to parse packet from remote UDP server "<< sockAddrToString((struct sockaddr_in*) &fromaddr) <<
+        L<<Logger::Error<<"Unable to parse packet from remote UDP server "<< fromaddr.toString() <<
           ": packet smalller than DNS header"<<endl;
     }
 
@@ -1417,7 +1417,7 @@ void handleUDPServerResponse(int fd, FDMultiplexer::funcparam_t& var)
       }
       catch(std::exception& e) {
         g_stats.serverParseError++; // won't be fed to lwres.cc, so we have to increment
-        L<<Logger::Warning<<"Error in packet from "<<sockAddrToString((struct sockaddr_in*) &fromaddr) << ": "<<e.what() << endl;
+        L<<Logger::Warning<<"Error in packet from "<< fromaddr.toStringWithPort() << ": "<<e.what() << endl;
         return;
       }
     }
@@ -1457,7 +1457,7 @@ void handleUDPServerResponse(int fd, FDMultiplexer::funcparam_t& var)
     }
   }
   else
-    L<<Logger::Warning<<"Ignoring question on outgoing socket from "<< sockAddrToString((struct sockaddr_in*) &fromaddr)  <<endl;
+    L<<Logger::Warning<<"Ignoring question on outgoing socket from "<< fromaddr.toStringWithPort()  <<endl;
 }
 
 FDMultiplexer* getMultiplexer()