reSIProcate/rutil  9694
RRVip.cxx
Go to the documentation of this file.
00001 #if defined(HAVE_CONFIG_H)
00002 #include "config.h"
00003 #endif
00004 
00005 #include "AresCompat.hxx"
00006 
00007 #include <map>
00008 #include <list>
00009 #include <vector>
00010 
00011 #ifndef WIN32
00012 #include <sys/types.h>
00013 #include <sys/socket.h>
00014 #include <arpa/inet.h>
00015 #ifndef __CYGWIN__
00016 #  include <netinet/in.h>
00017 #  include <arpa/nameser.h>
00018 #  include <resolv.h>
00019 #endif
00020 #include <netdb.h>
00021 #include <netinet/in.h>
00022 #else
00023 #include <Winsock2.h>
00024 #include <svcguid.h>
00025 #ifdef USE_IPV6
00026 #include <ws2tcpip.h>
00027 #endif
00028 #endif
00029 
00030 #include "rutil/Log.hxx"
00031 #include "rutil/Logger.hxx"
00032 #include "rutil/BaseException.hxx"
00033 #include "rutil/dns/DnsResourceRecord.hxx"
00034 #include "rutil/dns/DnsAAAARecord.hxx"
00035 #include "rutil/dns/DnsHostRecord.hxx"
00036 #include "rutil/dns/DnsNaptrRecord.hxx"
00037 #include "rutil/dns/DnsSrvRecord.hxx"
00038 #include "rutil/dns/RRVip.hxx"
00039 #include "rutil/WinLeakCheck.hxx"
00040 
00041 #define RESIPROCATE_SUBSYSTEM resip::Subsystem::DNS
00042 
00043 using namespace resip;
00044 using namespace std;
00045 
00046 RRVip::RRVip()
00047 {
00048    mFactories[T_A] = new HostTransformFactory;
00049    mFactories[T_AAAA] = new HostTransformFactory;
00050    mFactories[T_NAPTR] = new NaptrTransformFactroy;
00051    mFactories[T_SRV] = new SrvTransformFactory;
00052 }
00053 
00054 RRVip::~RRVip()
00055 {
00056    for (map<MapKey, Transform*>::iterator it = mTransforms.begin(); it != mTransforms.end(); ++it)
00057    {
00058       delete (*it).second;
00059    }
00060 
00061    for (TransformFactoryMap::iterator it = mFactories.begin(); it != mFactories.end(); ++it)
00062    {
00063       delete (*it).second;
00064    }
00065 }
00066 
00067 void RRVip::vip(const Data& target,
00068                 int rrType,
00069                 const Data& vip)
00070 {
00071    RRVip::MapKey key(target, rrType);
00072    TransformMap::iterator it = mTransforms.find(key);
00073    if (it != mTransforms.end())
00074    {
00075       it->second->updateVip(vip);
00076    }
00077    else
00078    {
00079       TransformFactoryMap::iterator it = mFactories.find(rrType);
00080       assert(it != mFactories.end());
00081       Transform* transform = it->second->createTransform(vip);
00082       mTransforms.insert(TransformMap::value_type(key, transform));
00083    }
00084 }
00085 
00086 void RRVip::removeVip(const Data& target,
00087                       int rrType)
00088 {
00089    RRVip::MapKey key(target, rrType);
00090    TransformMap::iterator it = mTransforms.find(key);
00091    if (it != mTransforms.end())
00092    {
00093       Data vip = it->second->vip();
00094       delete (*it).second;
00095       mTransforms.erase(it);
00096       DebugLog(<< "removed vip " << target << "(" << rrType << "): " << vip);
00097    }
00098 }
00099 
00100 void RRVip::transform(const Data& target,
00101                       int rrType,
00102                       std::vector<DnsResourceRecord*>& src)
00103 {
00104    RRVip::MapKey key(target, rrType);
00105    TransformMap::iterator it = mTransforms.find(key);
00106    if (it != mTransforms.end())
00107    {
00108       bool invalidVip = false;
00109       it->second->transform(src, invalidVip);
00110       if (invalidVip) 
00111       {
00112          removeVip(target, rrType);
00113       }
00114    }
00115 }
00116 
00117 RRVip::Transform::Transform(const Data& vip) 
00118    : mVip(vip)
00119 {
00120 }
00121 
00122 RRVip::Transform::~Transform()
00123 {
00124 }
00125 
00126 void RRVip::Transform::updateVip(const Data& vip)
00127 {
00128    DebugLog(<< "updating an existing vip: " << mVip << " with " << vip);
00129    mVip = vip;
00130 }
00131 
00132 void RRVip::Transform::transform(RRVector& src,
00133                                  bool& invalidVip)
00134 {
00135    invalidVip = true;
00136    RRVector::iterator it;
00137    for (it = src.begin(); it != src.end(); ++it)
00138    {
00139       if ((*it)->isSameValue(mVip))
00140       {
00141          invalidVip = false;
00142          break;
00143       }
00144    }
00145    if(!invalidVip)
00146    {
00147       DebugLog( << "tranforming records");
00148       if (src.begin() != it)
00149       {
00150          DnsResourceRecord* vip = *it;
00151          src.erase(it);
00152          src.insert(src.begin(), vip);
00153       }
00154    }
00155 }
00156 
00157 RRVip::NaptrTransform::NaptrTransform(const Data& vip)
00158    : Transform(vip)
00159 {
00160    DebugLog(<< "Creating a new Napter transform for " << vip);
00161 }
00162 
00163 void RRVip::NaptrTransform::transform(RRVector& src,
00164                                       bool& invalidVip)
00165 {
00166    invalidVip = true;
00167    RRVector::iterator vip;
00168    for (RRVector::iterator it = src.begin(); it != src.end(); ++it)
00169    {
00170       if ((*it)->isSameValue(mVip))
00171       {
00172          DebugLog(<< "naptr vip record " << mVip << "found");
00173          invalidVip = false;
00174          vip = it;
00175          break;
00176       }
00177    }
00178    if(!invalidVip)
00179    {
00180       DebugLog(<< "Transforming Naptr records");
00181       int min = dynamic_cast<DnsNaptrRecord*>(*(src.begin()))->order();
00182       for (RRVector::iterator it = src.begin(); it != src.end(); ++it)
00183       {
00184          int order = ((dynamic_cast<DnsNaptrRecord*>(*it))->order())++;
00185          if (order < min) min = order;
00186       }
00187       dynamic_cast<DnsNaptrRecord*>((*vip))->order() = min;
00188    }
00189 }
00190 
00191 RRVip::SrvTransform::SrvTransform(const Data& vip)
00192    : Transform(vip)
00193 {
00194    DebugLog(<< "Creating a new SRV transform for" << vip);
00195 }
00196 
00197 void RRVip::SrvTransform::transform(RRVector& src,
00198                                     bool& invalidVip)
00199 {
00200    invalidVip = true;
00201    RRVector::iterator vip;
00202    for (RRVector::iterator it = src.begin(); it != src.end(); ++it)
00203    {
00204       if ((*it)->isSameValue(mVip))
00205       {
00206          invalidVip = false;
00207          vip = it;
00208          break;
00209       }
00210    }
00211    if(!invalidVip)
00212    {
00213       DebugLog(<< "Transforming SRV records");
00214       int min = dynamic_cast<DnsSrvRecord*>(*(src.begin()))->priority();
00215       for (RRVector::iterator it = src.begin(); it != src.end(); ++it)
00216       {
00217          int priority = ((dynamic_cast<DnsSrvRecord*>(*it))->priority())++;
00218          if (priority < min) min = priority;
00219       }
00220       dynamic_cast<DnsSrvRecord*>((*vip))->priority() = min;
00221    }
00222 }
00223 
00224 RRVip::MapKey::MapKey() 
00225   : mRRType(0)          // .kw. what is reasonable default?
00226 {
00227 }
00228 
00229 RRVip::MapKey::MapKey(const Data& target, int rrType)
00230    : mTarget(target), 
00231      mRRType(rrType)
00232 {
00233 }
00234 
00235 bool RRVip::MapKey::operator<(const MapKey& rhs) const
00236 {
00237    if (mRRType < rhs.mRRType)
00238    {
00239       return true;
00240    }
00241    else if (mRRType > rhs.mRRType)
00242    {
00243       return false;
00244    }
00245    else
00246    {
00247       return mTarget < rhs.mTarget;
00248    }
00249 }
00250 
00251 /* ====================================================================
00252  * The Vovida Software License, Version 1.0 
00253  * 
00254  * Copyright (c) 2000-2005 Vovida Networks, Inc.  All rights reserved.
00255  * 
00256  * Redistribution and use in source and binary forms, with or without
00257  * modification, are permitted provided that the following conditions
00258  * are met:
00259  * 
00260  * 1. Redistributions of source code must retain the above copyright
00261  *    notice, this list of conditions and the following disclaimer.
00262  * 
00263  * 2. Redistributions in binary form must reproduce the above copyright
00264  *    notice, this list of conditions and the following disclaimer in
00265  *    the documentation and/or other materials provided with the
00266  *    distribution.
00267  * 
00268  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00269  *    and "Vovida Open Communication Application Library (VOCAL)" must
00270  *    not be used to endorse or promote products derived from this
00271  *    software without prior written permission. For written
00272  *    permission, please contact vocal@vovida.org.
00273  *
00274  * 4. Products derived from this software may not be called "VOCAL", nor
00275  *    may "VOCAL" appear in their name, without prior written
00276  *    permission of Vovida Networks, Inc.
00277  * 
00278  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00279  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00280  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00281  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00282  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00283  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00284  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00285  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00286  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00287  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00288  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00289  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00290  * DAMAGE.
00291  * 
00292  * ====================================================================
00293  * 
00294  * This software consists of voluntary contributions made by Vovida
00295  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00296  * Inc.  For more information on Vovida Networks, Inc., please see
00297  * <http://www.vovida.org/>.
00298  *
00299  */
00300