~ubuntu-branches/ubuntu/saucy/resiprocate/saucy-proposed

« back to all changes in this revision

Viewing changes to repro/monkeys/GeoProximityTargetSorter.hxx

  • Committer: Package Import Robot
  • Author(s): Daniel Pocock
  • Date: 2012-05-17 19:29:59 UTC
  • Revision ID: package-import@ubuntu.com-20120517192959-vv00m77isztdy64q
Tags: upstream-1.8.2
ImportĀ upstreamĀ versionĀ 1.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef GEO_PROXIMITY_TARGET_SORTER_HXX
 
2
#define GEO_PROXIMITY_TARGET_SORTER_HXX 1
 
3
 
 
4
#ifndef RESIP_FIXED_POINT
 
5
 
 
6
#ifdef WIN32
 
7
#include <pcreposix.h>
 
8
#else
 
9
#include <regex.h>
 
10
#endif
 
11
 
 
12
#include "repro/Processor.hxx"
 
13
#include "repro/ResponseContext.hxx"
 
14
#include "repro/ProxyConfig.hxx"
 
15
 
 
16
#include "rutil/Data.hxx"
 
17
 
 
18
namespace resip
 
19
{
 
20
   class SipMessage;
 
21
}
 
22
 
 
23
namespace repro
 
24
{
 
25
 
 
26
class RequestContext;
 
27
 
 
28
/*
 
29
  If enabled, then this baboon can post-process the target list.  
 
30
  This includes targets from the StaticRoute monkey and/or targets
 
31
  from the LocationServer monkey.  Requests that meet the filter 
 
32
  criteria will have their Target list, flatened (serialized) and
 
33
  ordered based on the proximity of the target to the client sending
 
34
  the request.  Proximity is determined by looking for a 
 
35
  x-repro-geolocation="<latitude>,<longitude>" parameter on the Contact
 
36
  header of a received request, or the Contact headers of Registration
 
37
  requests.  If this parameter is not found, then this processor will
 
38
  attempt to determine the public IP address closest to the client or
 
39
  target and use the MaxMind Geo IP library to lookup the geo location.
 
40
 
 
41
  There are several requirements for using this Processor/Baboon:
 
42
  1.  RESIP_FIXED_POINT preprocessor define is required to allow floating
 
43
      point operations required for distance calculations.
 
44
  2.  USE_MAXMIND_GEOIP preprocessor define is required to allow linking
 
45
      with the MaxMind Geo IP library for looking up lat/long information
 
46
      for an IP address.
 
47
  3.  A copy of the GeoIP City database is required for looking up lat/long
 
48
      information for an IP address.  A free version of the databases can 
 
49
      be downloaded from here:
 
50
      http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
 
51
      and here (v6):
 
52
      http://geolite.maxmind.com/download/geoip/database/GeoLiteCityv6-beta/
 
53
      For a more accurate database, please see the details here:
 
54
      http://www.maxmind.com/app/city
 
55
      The location of these files are specifed in the following setting2:
 
56
      GeoProximityIPv4CityDatabaseFile and GeoProximityIPv6CityDatabaseFile
 
57
  4.  A request URI filter must be defined in the repro configuration.  This
 
58
      filter uses a PCRE compliant regular expression to attempt
 
59
      to match against the request URI of inbound requests.  Any requests
 
60
      matching this expression, will have its Targets sorted as described
 
61
      above.
 
62
      ie: GeoProximityRequestUriFilter = ^sip:mediaserver.*@mydomain.com$
 
63
 
 
64
   Some additional settings are:
 
65
   GeoProximityDefaultDistance - The distance (in Kilometers) to use for 
 
66
      proximity sorting, when the Geo Location of a target cannot be 
 
67
      determined. 
 
68
 
 
69
   LoadBalanceEqualDistantTargets - If enabled, then targets that are 
 
70
      determined to be of equal distance from the client, will be placed in 
 
71
      a random order.
 
72
*/
 
73
 
 
74
 
 
75
class GeoProximityTargetSorter : public Processor
 
76
{
 
77
   public:
 
78
 
 
79
      static resip::KeyValueStore::Key mGeoTargetSortingDoneKey;
 
80
 
 
81
      GeoProximityTargetSorter(ProxyConfig& config);
 
82
      virtual ~GeoProximityTargetSorter();
 
83
      
 
84
      virtual processor_action_t process(RequestContext &);
 
85
         
 
86
   protected:
 
87
      void getClientGeoLocation(const resip::SipMessage& request, double& latitude, double& longitude);
 
88
      void getTargetGeoLocation(const Target& target, double& latitude, double& longitude);
 
89
      double getTargetDistance(const Target& target, double clientLatitude, double clientLongitude);
 
90
      void parseGeoLocationParameter(const resip::Data& parameter, double& latitude, double& longitude);
 
91
      double calculateDistance(double latitude1, double longitude1, double latitude2, double longitude2);
 
92
      bool geoIPLookup(const resip::Tuple& address, double& latitude, double& longitude);
 
93
 
 
94
      resip::Data mRUriRegularExpressionData;
 
95
      regex_t* mRUriRegularExpression;
 
96
 
 
97
      unsigned long mDefaultDistance;
 
98
      bool mLoadBalanceEqualDistantTargets;
 
99
      void* mGeoIPv4;
 
100
      void* mGeoIPv6;
 
101
};
 
102
 
 
103
}
 
104
 
 
105
#endif // ndef RESIP_FIXED_POINT
 
106
 
 
107
#endif
 
108
 
 
109
/* ====================================================================
 
110
 * The Vovida Software License, Version 1.0 
 
111
 * 
 
112
 * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
 
113
 * 
 
114
 * Redistribution and use in source and binary forms, with or without
 
115
 * modification, are permitted provided that the following conditions
 
116
 * are met:
 
117
 * 
 
118
 * 1. Redistributions of source code must retain the above copyright
 
119
 *    notice, this list of conditions and the following disclaimer.
 
120
 * 
 
121
 * 2. Redistributions in binary form must reproduce the above copyright
 
122
 *    notice, this list of conditions and the following disclaimer in
 
123
 *    the documentation and/or other materials provided with the
 
124
 *    distribution.
 
125
 * 
 
126
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
 
127
 *    and "Vovida Open Communication Application Library (VOCAL)" must
 
128
 *    not be used to endorse or promote products derived from this
 
129
 *    software without prior written permission. For written
 
130
 *    permission, please contact vocal@vovida.org.
 
131
 *
 
132
 * 4. Products derived from this software may not be called "VOCAL", nor
 
133
 *    may "VOCAL" appear in their name, without prior written
 
134
 *    permission of Vovida Networks, Inc.
 
135
 * 
 
136
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 
137
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
138
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
 
139
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
 
140
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
 
141
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
 
142
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
143
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
144
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
145
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
146
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 
147
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 
148
 * DAMAGE.
 
149
 * 
 
150
 * ====================================================================
 
151
 */