reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
RRVip.cxx
Go to the documentation of this file.
1#if defined(HAVE_CONFIG_H)
2#include "config.h"
3#endif
4
5#include "AresCompat.hxx"
6
7#include <map>
8#include <list>
9#include <vector>
10
11#ifndef WIN32
12#include <sys/types.h>
13#include <sys/socket.h>
14#include <arpa/inet.h>
15#ifndef __CYGWIN__
16# include <netinet/in.h>
17# include <arpa/nameser.h>
18# include <resolv.h>
19#endif
20#include <netdb.h>
21#include <netinet/in.h>
22#else
23#include <Winsock2.h>
24#include <svcguid.h>
25#ifdef USE_IPV6
26#include <ws2tcpip.h>
27#endif
28#endif
29
30#include "rutil/Log.hxx"
31#include "rutil/Logger.hxx"
38#include "rutil/dns/RRVip.hxx"
40
41#define RESIPROCATE_SUBSYSTEM resip::Subsystem::DNS
42
43using namespace resip;
44using namespace std;
45
47{
48 mFactories[T_A] = new HostTransformFactory;
49 mFactories[T_AAAA] = new HostTransformFactory;
50 mFactories[T_NAPTR] = new NaptrTransformFactroy;
51 mFactories[T_SRV] = new SrvTransformFactory;
52}
53
55{
56 for (map<MapKey, Transform*>::iterator it = mTransforms.begin(); it != mTransforms.end(); ++it)
57 {
58 delete (*it).second;
59 }
60
61 for (TransformFactoryMap::iterator it = mFactories.begin(); it != mFactories.end(); ++it)
62 {
63 delete (*it).second;
64 }
65}
66
67void RRVip::vip(const Data& target,
68 int rrType,
69 const Data& vip)
70{
71 RRVip::MapKey key(target, rrType);
72 TransformMap::iterator it = mTransforms.find(key);
73 if (it != mTransforms.end())
74 {
75 it->second->updateVip(vip);
76 }
77 else
78 {
79 TransformFactoryMap::iterator it = mFactories.find(rrType);
80 resip_assert(it != mFactories.end());
81 Transform* transform = it->second->createTransform(vip);
82 mTransforms.insert(TransformMap::value_type(key, transform));
83 }
84}
85
86void RRVip::removeVip(const Data& target,
87 int rrType)
88{
89 RRVip::MapKey key(target, rrType);
90 TransformMap::iterator it = mTransforms.find(key);
91 if (it != mTransforms.end())
92 {
93 Data vip = it->second->vip();
94 delete (*it).second;
95 mTransforms.erase(it);
96 DebugLog(<< "removed vip " << target << "(" << rrType << "): " << vip);
97 }
98}
99
100void RRVip::transform(const Data& target,
101 int rrType,
102 std::vector<DnsResourceRecord*>& src)
103{
104 RRVip::MapKey key(target, rrType);
105 TransformMap::iterator it = mTransforms.find(key);
106 if (it != mTransforms.end())
107 {
108 bool invalidVip = false;
109 it->second->transform(src, invalidVip);
110 if (invalidVip)
111 {
112 removeVip(target, rrType);
113 }
114 }
115}
116
117RRVip::Transform::Transform(const Data& vip)
118 : mVip(vip)
119{
120}
121
122RRVip::Transform::~Transform()
123{
124}
125
126void RRVip::Transform::updateVip(const Data& vip)
127{
128 DebugLog(<< "updating an existing vip: " << mVip << " with " << vip);
129 mVip = vip;
130}
131
132void RRVip::Transform::transform(RRVector& src,
133 bool& invalidVip)
134{
135 invalidVip = true;
136 RRVector::iterator it;
137 for (it = src.begin(); it != src.end(); ++it)
138 {
139 if ((*it)->isSameValue(mVip))
140 {
141 invalidVip = false;
142 break;
143 }
144 }
145 if(!invalidVip)
146 {
147 DebugLog( << "tranforming records");
148 if (src.begin() != it)
149 {
150 DnsResourceRecord* vip = *it;
151 src.erase(it);
152 src.insert(src.begin(), vip);
153 }
154 }
155}
156
157RRVip::NaptrTransform::NaptrTransform(const Data& vip)
158 : Transform(vip)
159{
160 DebugLog(<< "Creating a new Napter transform for " << vip);
161}
162
163void RRVip::NaptrTransform::transform(RRVector& src,
164 bool& invalidVip)
165{
166 invalidVip = true;
167 RRVector::iterator vip;
168 for (RRVector::iterator it = src.begin(); it != src.end(); ++it)
169 {
170 if ((*it)->isSameValue(mVip))
171 {
172 DebugLog(<< "naptr vip record " << mVip << "found");
173 invalidVip = false;
174 vip = it;
175 break;
176 }
177 }
178 if(!invalidVip)
179 {
180 DebugLog(<< "Transforming Naptr records");
181 int min = dynamic_cast<DnsNaptrRecord*>(*(src.begin()))->order();
182 for (RRVector::iterator it = src.begin(); it != src.end(); ++it)
183 {
184 int order = ((dynamic_cast<DnsNaptrRecord*>(*it))->order())++;
185 if (order < min) min = order;
186 }
187 dynamic_cast<DnsNaptrRecord*>((*vip))->order() = min;
188 }
189}
190
191RRVip::SrvTransform::SrvTransform(const Data& vip)
192 : Transform(vip)
193{
194 DebugLog(<< "Creating a new SRV transform for" << vip);
195}
196
197void RRVip::SrvTransform::transform(RRVector& src,
198 bool& invalidVip)
199{
200 invalidVip = true;
201 RRVector::iterator vip;
202 for (RRVector::iterator it = src.begin(); it != src.end(); ++it)
203 {
204 if ((*it)->isSameValue(mVip))
205 {
206 invalidVip = false;
207 vip = it;
208 break;
209 }
210 }
211 if(!invalidVip)
212 {
213 DebugLog(<< "Transforming SRV records");
214 int min = dynamic_cast<DnsSrvRecord*>(*(src.begin()))->priority();
215 for (RRVector::iterator it = src.begin(); it != src.end(); ++it)
216 {
217 int priority = ((dynamic_cast<DnsSrvRecord*>(*it))->priority())++;
218 if (priority < min) min = priority;
219 }
220 dynamic_cast<DnsSrvRecord*>((*vip))->priority() = min;
221 }
222}
223
224RRVip::MapKey::MapKey()
225 : mRRType(0) // .kw. what is reasonable default?
226{
227}
228
229RRVip::MapKey::MapKey(const Data& target, int rrType)
230 : mTarget(target),
231 mRRType(rrType)
232{
233}
234
235bool RRVip::MapKey::operator<(const MapKey& rhs) const
236{
237 if (mRRType < rhs.mRRType)
238 {
239 return true;
240 }
241 else if (mRRType > rhs.mRRType)
242 {
243 return false;
244 }
245 else
246 {
247 return mTarget < rhs.mTarget;
248 }
249}
250
251/* ====================================================================
252 * The Vovida Software License, Version 1.0
253 *
254 * Copyright (c) 2000-2005 Vovida Networks, Inc. All rights reserved.
255 *
256 * Redistribution and use in source and binary forms, with or without
257 * modification, are permitted provided that the following conditions
258 * are met:
259 *
260 * 1. Redistributions of source code must retain the above copyright
261 * notice, this list of conditions and the following disclaimer.
262 *
263 * 2. Redistributions in binary form must reproduce the above copyright
264 * notice, this list of conditions and the following disclaimer in
265 * the documentation and/or other materials provided with the
266 * distribution.
267 *
268 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
269 * and "Vovida Open Communication Application Library (VOCAL)" must
270 * not be used to endorse or promote products derived from this
271 * software without prior written permission. For written
272 * permission, please contact vocal@vovida.org.
273 *
274 * 4. Products derived from this software may not be called "VOCAL", nor
275 * may "VOCAL" appear in their name, without prior written
276 * permission of Vovida Networks, Inc.
277 *
278 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
279 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
280 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
281 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
282 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
283 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
284 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
285 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
286 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
287 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
288 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
289 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
290 * DAMAGE.
291 *
292 * ====================================================================
293 *
294 * This software consists of voluntary contributions made by Vovida
295 * Networks, Inc. and many individuals on behalf of Vovida Networks,
296 * Inc. For more information on Vovida Networks, Inc., please see
297 * <http://www.vovida.org/>.
298 *
299 */
300
#define DebugLog(args_)
Definition Logger.hxx:128
#define resip_assert(x)
This file needs to work for both C and C++ code.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
void removeVip(const Data &target, int rrType)
Definition RRVip.cxx:86
void vip(const Data &target, int rrType, const Data &vip)
Definition RRVip.cxx:67
void transform(const Data &target, int rrType, std::vector< DnsResourceRecord * > &)
Definition RRVip.cxx:100
#define T_NAPTR
Definition compat.hxx:140
#define T_A
Definition compat.hxx:152
#define T_AAAA
Definition compat.hxx:148
#define T_SRV
Definition compat.hxx:144
dcm! – add UnusedChecking(_enum) below;