reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
GenericIPAddress.hxx
Go to the documentation of this file.
1#if !defined(RESIP_GENERIC_IP_ADDRESS_HXX)
2#define RESIP_GENERIC_IP_ADDRESS_HXX
3
4#ifndef WIN32
5#include <netinet/in.h>
6#else
7#include <winsock2.h>
8#include <Ws2tcpip.h>
9#endif
10
11#include "rutil/Socket.hxx"
12#include "rutil/compat.hxx"
13
14
15namespace resip
16{
23{
24 public:
26 {
27 }
28
29 GenericIPAddress(const sockaddr& addr) : address(addr)
30 {
31#ifdef IPPROTO_IPV6
32 if (addr.sa_family == AF_INET6)
33 {
34 v6Address = reinterpret_cast<const sockaddr_in6&>(addr);
35 }
36 else
37#endif
38 {
39 v4Address = reinterpret_cast<const sockaddr_in&>(addr);
40 }
41 }
42
43 GenericIPAddress(const sockaddr_in& v4) : v4Address(v4)
44 {
45 }
46
47#ifdef IPPROTO_IPV6
48 GenericIPAddress(const sockaddr_in6& v6) : v6Address(v6)
49 {
50 }
51#endif
52
53 size_t length() const
54 {
55 if (address.sa_family == AF_INET) // v4
56 {
57 return sizeof(sockaddr_in);
58 }
59#ifdef IPPROTO_IPV6
60 else if (address.sa_family == AF_INET6) // v6
61 {
62 return sizeof(sockaddr_in6);
63 }
64#endif
65 resip_assert(0);
66 return 0;
67 }
68
69 bool isVersion4() const
70 {
71 return address.sa_family == AF_INET;
72 }
73
74 bool isVersion6() const
75 {
76#ifdef IPPROTO_IPV6
77 if (address.sa_family == AF_INET6) return true;
78#endif
79 return false;
80 }
81
82 union
83 {
84 sockaddr address;
85 sockaddr_in v4Address;
86#ifdef IPPROTO_IPV6
87 sockaddr_in6 v6Address;
88#endif
89 char pad[28]; // this make union same size if v6 is in or out
90 };
91
92 bool operator==(const GenericIPAddress& addr) const
93 {
94 if (address.sa_family == addr.address.sa_family)
95 {
96 if (address.sa_family == AF_INET) // v4
97 {
98 return (v4Address.sin_port == addr.v4Address.sin_port &&
99 memcmp(&v4Address.sin_addr, &addr.v4Address.sin_addr, sizeof(in_addr)) == 0);
100 }
101 else // v6
102 {
103#ifdef IPPROTO_IPV6
104 return (v6Address.sin6_port == addr.v6Address.sin6_port &&
105 memcmp(&v6Address.sin6_addr, &addr.v6Address.sin6_addr, sizeof(in6_addr)) == 0);
106#else
107 resip_assert(0);
108 return false;
109#endif
110 }
111 }
112 return false;
113 }
114
115 bool operator<(const GenericIPAddress& addr) const
116 {
117
118 if (address.sa_family == AF_INET && addr.address.sa_family == AF_INET)
119 {
120 int c=memcmp(&v4Address.sin_addr,
121 &addr.v4Address.sin_addr,
122 sizeof(in_addr));
123
124 if (c < 0)
125 {
126 return true;
127 }
128 else if (c > 0)
129 {
130 return false;
131 }
132 else if (v4Address.sin_port < addr.v4Address.sin_port)
133 {
134 return true;
135 }
136 else
137 {
138 return false;
139 }
140 }
141#ifdef IPPROTO_IPV6
142 else if (address.sa_family == AF_INET6 &&
143 addr.address.sa_family == AF_INET6)
144 {
145 int c = memcmp(&v6Address.sin6_addr,
146 &addr.v6Address.sin6_addr,
147 sizeof(in6_addr));
148 if (c < 0)
149 {
150 return true;
151 }
152 else if (c > 0)
153 {
154 return false;
155 }
156 else if (v6Address.sin6_port < addr.v6Address.sin6_port)
157 {
158 return true;
159 }
160 else
161 {
162 return false;
163 }
164 }
165 else if (address.sa_family == AF_INET6 &&
166 addr.address.sa_family == AF_INET)
167 {
168 return true;
169 }
170 else if (address.sa_family == AF_INET &&
171 addr.address.sa_family == AF_INET6)
172 {
173 return false;
174 }
175#endif
176 else
177 {
178 //assert(0);
179 return false;
180 }
181 }
182
184
185};
186
188operator<<(EncodeStream& ostrm, const GenericIPAddress& addr);
189
190}
191
192
193#endif
194
195/* ====================================================================
196 * The Vovida Software License, Version 1.0
197 *
198 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
199 *
200 * Redistribution and use in source and binary forms, with or without
201 * modification, are permitted provided that the following conditions
202 * are met:
203 *
204 * 1. Redistributions of source code must retain the above copyright
205 * notice, this list of conditions and the following disclaimer.
206 *
207 * 2. Redistributions in binary form must reproduce the above copyright
208 * notice, this list of conditions and the following disclaimer in
209 * the documentation and/or other materials provided with the
210 * distribution.
211 *
212 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
213 * and "Vovida Open Communication Application Library (VOCAL)" must
214 * not be used to endorse or promote products derived from this
215 * software without prior written permission. For written
216 * permission, please contact vocal@vovida.org.
217 *
218 * 4. Products derived from this software may not be called "VOCAL", nor
219 * may "VOCAL" appear in their name, without prior written
220 * permission of Vovida Networks, Inc.
221 *
222 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
223 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
224 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
225 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
226 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
227 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
228 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
229 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
230 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
231 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
233 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
234 * DAMAGE.
235 *
236 * ====================================================================
237 *
238 * This software consists of voluntary contributions made by Vovida
239 * Networks, Inc. and many individuals on behalf of Vovida Networks,
240 * Inc. For more information on Vovida Networks, Inc., please see
241 * <http://www.vovida.org/>.
242 *
243 */
#define resip_assert(x)
This file needs to work for both C and C++ code.
Handles cross-platform sockets compatibility.
This file is used to handle compatibility fixes/tweaks so reSIProcate can function on multiple platfo...
dcm! – add UnusedChecking(_enum) below;
EncodeStream & operator<<(EncodeStream &strm, const BaseException &e)
#define EncodeStream
Represents an IP-address and port (V4 or V6).
friend EncodeStream & operator<<(EncodeStream &strm, const GenericIPAddress &addr)
bool operator<(const GenericIPAddress &addr) const
bool operator==(const GenericIPAddress &addr) const
GenericIPAddress(const sockaddr_in &v4)
GenericIPAddress(const sockaddr &addr)