Changeset 2010

Show
Ignore:
Timestamp:
02/14/11 10:58:10 (2 years ago)
Author:
ahu
Message:

phase out sockAddrToString function (ComboAddress? has a better one)
teach ComboAddress? to accept 1.2.3.4:53 as well as [::]:53

Location:
trunk/pdns/pdns
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/pdns/pdns/dnsproxy.cc

    r1549 r2010  
    140140      if(i->second.created) 
    141141        L<<Logger::Warning<<"Recursive query for remote "<< 
    142           sockAddrToString((struct sockaddr_in *)&i->second.remote)<<" with internal id "<<n<< 
     142          i->second.remote.toStringWithPort()<<" with internal id "<<n<< 
    143143          " was not answered by backend within timeout, reusing id"<<endl; 
    144144       
  • trunk/pdns/pdns/iputils.hh

    r2009 r2010  
    109109  } 
    110110 
     111  // 'port' sets a default value in case 'str' does not set a port 
    111112  explicit ComboAddress(const string& str, uint16_t port=0) 
    112113  { 
    113114    memset(&sin6, 0, sizeof(sin6)); 
    114115    sin4.sin_family = AF_INET; 
    115      
    116     if(!IpToU32(str, (uint32_t*)&sin4.sin_addr.s_addr)) { 
     116    sin4.sin_port = 0; 
     117    if(makeIPv4sockaddr(str, &sin4)) { 
    117118      sin6.sin6_family = AF_INET6; 
    118119      if(makeIPv6sockaddr(str, &sin6) < 0) 
     
    120121       
    121122    } 
    122     sin4.sin_port=htons(port); 
     123    if(!sin4.sin_port) // 'str' overrides port! 
     124      sin4.sin_port=htons(port); 
    123125  } 
    124126 
  • trunk/pdns/pdns/misc.cc

    r1978 r2010  
    482482 
    483483 
    484 const string sockAddrToString(struct sockaddr_in *remote)  
    485 {     
    486   if(remote->sin_family == AF_INET) { 
    487     struct sockaddr_in sip; 
    488     memcpy(&sip,(struct sockaddr_in*)remote,sizeof(sip)); 
    489     return inet_ntoa(sip.sin_addr); 
    490   } 
    491   else { 
    492     char tmp[128]; 
    493      
    494     if(!Utility::inet_ntop(AF_INET6, ( const char * ) &((struct sockaddr_in6 *)remote)->sin6_addr, tmp, sizeof(tmp))) 
    495       return "IPv6 untranslateable"; 
    496  
    497     return tmp; 
    498   } 
    499 } 
    500  
    501484string makeHexDump(const string& str) 
    502485{ 
     
    511494  return ret; 
    512495} 
    513  
    514  
    515496 
    516497// shuffle, maintaining some semblance of order 
     
    666647int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret) 
    667648{ 
     649  if(addr.empty()) 
     650    return -1; 
     651  string ourAddr(addr); 
     652  int port = -1; 
     653  if(addr[0]=='[') { // [::]:53 style address 
     654    string::size_type pos = addr.find(']'); 
     655    if(pos == string::npos || pos + 2 > addr.size() || addr[pos+1]!=':') 
     656      return -1; 
     657    ourAddr.assign(addr.c_str() + 1, pos-1); 
     658    port = atoi(addr.c_str()+pos+2);   
     659  } 
     660   
    668661  struct addrinfo* res; 
    669662  struct addrinfo hints; 
     
    674667   
    675668  int error; 
    676   if((error=getaddrinfo(addr.c_str(), 0, &hints, &res))) { 
     669  if((error=getaddrinfo(ourAddr.c_str(), 0, &hints, &res))) { // this is correct 
    677670    /* 
    678671    cerr<<"Error translating IPv6 address '"<<addr<<"': "; 
     
    686679   
    687680  memcpy(ret, res->ai_addr, res->ai_addrlen); 
    688    
     681  if(port >= 0) 
     682    ret->sin6_port = htons(port); 
    689683  freeaddrinfo(res); 
    690684  return 0; 
    691685} 
     686 
     687int makeIPv4sockaddr(const string &str, struct sockaddr_in* ret) 
     688{ 
     689  if(str.empty()) { 
     690    return -1; 
     691  } 
     692  struct in_addr inp; 
     693   
     694  string::size_type pos = str.find(':'); 
     695  if(pos == string::npos) { // no port specified, not touching the port 
     696    if(Utility::inet_aton(str.c_str(), &inp)) { 
     697      ret->sin_addr.s_addr=inp.s_addr; 
     698      return 0; 
     699    } 
     700    return -1; 
     701  } 
     702  if(!*(str.c_str() + pos + 1)) // trailing : 
     703    return -1;  
     704     
     705  char *eptr = (char*)str.c_str() + str.size(); 
     706  int port = strtol(str.c_str() + pos + 1, &eptr, 10); 
     707  if(*eptr) 
     708    return -1; 
     709   
     710  ret->sin_port = htons(port); 
     711  if(Utility::inet_aton(str.substr(0, pos).c_str(), &inp)) { 
     712    ret->sin_addr.s_addr=inp.s_addr; 
     713    return 0; 
     714  } 
     715  return -1; 
     716} 
     717 
    692718 
    693719//! read a line of text from a FILE* to a std::string, returns false on 'no data' 
  • trunk/pdns/pdns/misc.hh

    r1976 r2010  
    216216  struct timeval d_set; 
    217217}; 
    218 const string sockAddrToString(struct sockaddr_in *remote); 
     218 
    219219int sendData(const char *buffer, int replen, int outsock); 
    220220 
     
    458458std::string dotConcat(const std::string& a, const std::string &b); 
    459459int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret); 
     460int makeIPv4sockaddr(const string &str, struct sockaddr_in* ret); 
    460461bool stringfgets(FILE* fp, std::string& line); 
    461462 
  • trunk/pdns/pdns/pdns_recursor.cc

    r1997 r2010  
    13851385      g_stats.serverParseError++;  
    13861386      if(g_logCommonErrors) 
    1387         L<<Logger::Error<<"Unable to parse packet from remote UDP server "<< sockAddrToString((struct sockaddr_in*) &fromaddr) << 
     1387        L<<Logger::Error<<"Unable to parse packet from remote UDP server "<< fromaddr.toString() << 
    13881388          ": packet smalller than DNS header"<<endl; 
    13891389    } 
     
    14181418      catch(std::exception& e) { 
    14191419        g_stats.serverParseError++; // won't be fed to lwres.cc, so we have to increment 
    1420         L<<Logger::Warning<<"Error in packet from "<<sockAddrToString((struct sockaddr_in*) &fromaddr) << ": "<<e.what() << endl; 
     1420        L<<Logger::Warning<<"Error in packet from "<< fromaddr.toStringWithPort() << ": "<<e.what() << endl; 
    14211421        return; 
    14221422      } 
     
    14581458  } 
    14591459  else 
    1460     L<<Logger::Warning<<"Ignoring question on outgoing socket from "<< sockAddrToString((struct sockaddr_in*) &fromaddr)  <<endl; 
     1460    L<<Logger::Warning<<"Ignoring question on outgoing socket from "<< fromaddr.toStringWithPort()  <<endl; 
    14611461} 
    14621462