1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/*
* $Id$
*
* utilities
*
*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of ser, a free SIP server.
*
* ser is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* For a license to use the ser software under conditions
* other than those described here, or to purchase support for this
* software, please contact iptel.org by e-mail at the following addresses:
* info@iptel.org
*
* ser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* -------
* 2003-02-13 added proto to uri2proxy (andrei)
* 2003-04-09 uri2sock moved from uac.c (janakj)
* 2003-04-14 added get_proto to determine protocol from uri unless
* specified explicitly (jiri)
* 2003-07-07 get_proto takes now two protos as arguments (andrei)
* tls/sips support for get_proto & uri2proxy (andrei)
* 2006-04-13 added uri2dst(), simplified uri2sock() (andrei)
* 2006-08-11 dns failover support: uri2dst uses the dns cache and tries to
* get the first ip for which there is a send sock. (andrei)
*/
#ifndef _TM_UT_H
#define _TM_UT_H
#include "../../proxy.h"
#include "../../str.h"
#include "../../parser/parse_uri.h"
#include "../../dprint.h"
#include "../../ut.h"
#include "../../ip_addr.h"
#include "../../error.h"
#include "../../forward.h"
#include "../../mem/mem.h"
#include "../../parser/msg_parser.h"
#include "../../resolve.h"
#ifdef USE_DNS_FAILOVER
#include "../../dns_cache.h"
#include "../../cfg_core.h" /* cfg_get(core, core_cfg, use_dns_failover) */
#endif
/*! Which header fields should be skipped */
#define tm_skip_hf(_hf) \
(((_hf)->type == HDR_FROM_T) || \
((_hf)->type == HDR_TO_T) || \
((_hf)->type == HDR_CALLID_T) || \
((_hf)->type == HDR_CSEQ_T))
/* a forced_proto takes precedence if != PROTO_NONE */
inline static enum sip_protos get_proto(enum sip_protos force_proto,
enum sip_protos proto)
{
/* calculate transport protocol */
switch(force_proto) {
case PROTO_NONE: /* no protocol has been forced -- look at proto */
switch(proto) {
case PROTO_NONE: /* leave it to dns */
return PROTO_NONE;
case PROTO_UDP:/* transport specified explicitly */
#ifdef USE_TCP
case PROTO_TCP:
case PROTO_WS:
#endif
#ifdef USE_TLS
case PROTO_TLS:
#endif
#ifdef USE_SCTP
case PROTO_SCTP:
#endif
return proto;
case PROTO_WSS: /* should never see ;transport=wss */
default:
LOG(L_ERR, "ERROR: get_proto: unsupported transport:"
" %d\n", proto );
return PROTO_NONE;
}
case PROTO_UDP: /* some protocol has been forced -- take it */
#ifdef USE_TCP
case PROTO_TCP:
case PROTO_WS:
#endif
#ifdef USE_TLS
case PROTO_TLS:
case PROTO_WSS:
#endif
#ifdef USE_SCTP
case PROTO_SCTP:
#endif
return force_proto;
default:
LOG(L_ERR, "ERROR: get_proto: unsupported forced protocol: "
"%d\n", force_proto);
return PROTO_NONE;
}
}
/*
* Convert a URI into a proxy structure
*/
inline static struct proxy_l *uri2proxy( str *uri, int proto )
{
struct sip_uri parsed_uri;
struct proxy_l *p;
enum sip_protos uri_proto;
if (parse_uri(uri->s, uri->len, &parsed_uri) < 0) {
LOG(L_ERR, "ERROR: uri2proxy: bad_uri: %.*s\n",
uri->len, uri->s );
return 0;
}
if (parsed_uri.type==SIPS_URI_T){
if (parsed_uri.proto==PROTO_UDP) {
LOG(L_ERR, "ERROR: uri2proxy: bad transport for sips uri: %d\n",
parsed_uri.proto);
return 0;
}else if (parsed_uri.proto != PROTO_WS)
uri_proto=PROTO_TLS;
else
uri_proto=PROTO_WS;
}else
uri_proto=parsed_uri.proto;
#ifdef HONOR_MADDR
if (parsed_uri.maddr_val.s && parsed_uri.maddr_val.len) {
p = mk_proxy(&parsed_uri.maddr_val,
parsed_uri.port_no,
get_proto(proto, uri_proto));
if (p == 0) {
LOG(L_ERR, "ERROR: uri2proxy: bad maddr param in URI <%.*s>\n",
uri->len, ZSW(uri->s));
return 0;
}
} else
#endif
p = mk_proxy(&parsed_uri.host,
parsed_uri.port_no,
get_proto(proto, uri_proto));
if (p == 0) {
LOG(L_ERR, "ERROR: uri2proxy: bad host name in URI <%.*s>\n",
uri->len, ZSW(uri->s));
return 0;
}
return p;
}
/*
* parse uri and return send related information
* params: uri - uri in string form
* host - filled with the uri host part
* port - filled with the uri port
* proto - if != PROTO_NONE, this protocol will be forced over the
* uri_proto, otherwise the uri proto will be used
* (value/return)
* comp - compression (if used)
* returns 0 on success, < 0 on error
*/
inline static int get_uri_send_info(str* uri, str* host, unsigned short* port,
char* proto, short* comp)
{
struct sip_uri parsed_uri;
enum sip_protos uri_proto;
if (parse_uri(uri->s, uri->len, &parsed_uri) < 0) {
LOG(L_ERR, "ERROR: get_uri_send_info: bad_uri: %.*s\n",
uri->len, uri->s );
return -1;
}
if (parsed_uri.type==SIPS_URI_T){
if (parsed_uri.proto==PROTO_UDP) {
LOG(L_ERR, "ERROR: get_uri_send_info: bad transport for"
" sips uri: %d\n", parsed_uri.proto);
return -1;
}else if (parsed_uri.proto != PROTO_WS)
uri_proto=PROTO_TLS;
else
uri_proto=PROTO_WS;
}else
uri_proto=parsed_uri.proto;
*proto= get_proto(*proto, uri_proto);
#ifdef USE_COMP
*comp=parsed_uri.comp;
#endif
#ifdef HONOR_MADDR
if (parsed_uri.maddr_val.s && parsed_uri.maddr_val.len) {
*host=parsed_uri.maddr_val;
DBG("maddr dst: %.*s:%d\n", parsed_uri.maddr_val.len,
parsed_uri.maddr_val.s, parsed_uri.port_no);
} else
#endif
*host=parsed_uri.host;
*port=parsed_uri.port_no;
return 0;
}
/*
* Convert a URI into a dest_info structure.
* Same as uri2dst, but uses directly force_send_socket instead of msg.
* If the uri host resolves to multiple ips and dns_h!=0 the first ip for
* which a send socket is found will be used. If no send_socket are found,
* the first ip is selected.
*
* params: dns_h - pointer to a valid dns_srv_handle structure (intialized!) or
* null. If null or use_dns_failover==0 normal dns lookup will
* be performed (no failover).
* dst - will be filled
* force_send_sock - if 0 dst->send_sock will be set to the default
* (see get_send_socket2())
* sflags - send flags
* uri - uri in str form
* proto - if != PROTO_NONE, this protocol will be forced over the
* uri_proto, otherwise the uri proto will be used if set or
* the proto obtained from the dns lookup
* returns 0 on error, dst on success
*/
#ifdef USE_DNS_FAILOVER
inline static struct dest_info *uri2dst2(struct dns_srv_handle* dns_h,
struct dest_info* dst,
struct socket_info *force_send_socket,
snd_flags_t sflags,
str *uri, int proto )
#else
inline static struct dest_info *uri2dst2(struct dest_info* dst,
struct socket_info *force_send_socket,
snd_flags_t sflags,
str *uri, int proto )
#endif
{
struct sip_uri parsed_uri;
enum sip_protos uri_proto;
str* host;
#ifdef USE_DNS_FAILOVER
int ip_found;
union sockaddr_union to;
int err;
#endif
if (parse_uri(uri->s, uri->len, &parsed_uri) < 0) {
LOG(L_ERR, "ERROR: uri2dst: bad_uri: %.*s\n",
uri->len, uri->s );
return 0;
}
if (parsed_uri.type==SIPS_URI_T){
if (parsed_uri.proto==PROTO_UDP) {
LOG(L_ERR, "ERROR: uri2dst: bad transport for sips uri: %d\n",
parsed_uri.proto);
return 0;
}else if (parsed_uri.proto!=PROTO_WS)
uri_proto=PROTO_TLS;
else
uri_proto=PROTO_WS;
}else
uri_proto=parsed_uri.proto;
init_dest_info(dst);
dst->proto= get_proto(proto, uri_proto);
#ifdef USE_COMP
dst->comp=parsed_uri.comp;
#endif
dst->send_flags=sflags;
#ifdef HONOR_MADDR
if (parsed_uri.maddr_val.s && parsed_uri.maddr_val.len) {
host=&parsed_uri.maddr_val;
DBG("maddr dst: %.*s:%d\n", parsed_uri.maddr_val.len,
parsed_uri.maddr_val.s, parsed_uri.port_no);
} else
#endif
host=&parsed_uri.host;
#ifdef USE_DNS_FAILOVER
if (cfg_get(core, core_cfg, use_dns_failover) && dns_h){
ip_found=0;
do{
/* try all the ips until we find a good send socket */
err=dns_sip_resolve2su(dns_h, &to, host,
parsed_uri.port_no, &dst->proto, dns_flags);
if (err!=0){
if (ip_found==0){
if (err!=-E_DNS_EOR)
LOG(L_ERR, "ERROR: uri2dst: failed to resolve \"%.*s\" :"
"%s (%d)\n", host->len, ZSW(host->s),
dns_strerror(err), err);
return 0; /* error, no ip found */
}
break;
}
if (ip_found==0){
dst->to=to;
ip_found=1;
}
dst->send_sock = get_send_socket2(force_send_socket, &to,
dst->proto, 0);
if (dst->send_sock){
dst->to=to;
return dst; /* found a good one */
}
}while(dns_srv_handle_next(dns_h, err));
ERR("no corresponding socket for \"%.*s\" af %d\n", host->len,
ZSW(host->s), dst->to.s.sa_family);
/* try to continue */
return dst;
}
#endif
if (sip_hostport2su(&dst->to, host, parsed_uri.port_no, &dst->proto)!=0){
ERR("failed to resolve \"%.*s\"\n", host->len, ZSW(host->s));
return 0;
}
dst->send_sock = get_send_socket2(force_send_socket, &dst->to,
dst->proto, 0);
if (dst->send_sock==0) {
ERR("no corresponding socket found for \"%.*s\" af %d (%s:%s)\n",
host->len, ZSW(host->s), dst->to.s.sa_family,
proto2a(dst->proto), su2a(&dst->to, sizeof(dst->to)));
/* ser_error = E_NO_SOCKET;*/
/* try to continue */
}
return dst;
}
/*
* Convert a URI into a dest_info structure
* If the uri host resolves to multiple ips and dns_h!=0 the first ip for
* which a send socket is found will be used. If no send_socket are found,
* the first ip is selected.
*
* params: dns_h - pointer to a valid dns_srv_handle structure (intialized!) or
* null. If null or use_dns_failover==0 normal dns lookup will
* be performed (no failover).
* dst - will be filled
* msg - sip message used to set dst->send_sock and dst->send_flags,
* if 0 dst->send_sock will be set to the default w/o using
* msg->force_send_socket (see get_send_socket()) and the
* send_flags will be set to 0.
* uri - uri in str form
* proto - if != PROTO_NONE, this protocol will be forced over the
* uri_proto, otherwise the uri proto will be used if set or
* the proto obtained from the dns lookup
* returns 0 on error, dst on success
*/
#ifdef USE_DNS_FAILOVER
inline static struct dest_info *uri2dst(struct dns_srv_handle* dns_h,
struct dest_info* dst,
struct sip_msg *msg, str *uri,
int proto )
{
snd_flags_t sflags;
if (msg)
return uri2dst2(dns_h, dst, msg->force_send_socket,
msg->fwd_send_flags, uri, proto);
SND_FLAGS_INIT(&sflags);
return uri2dst2(dns_h, dst, 0, sflags, uri, proto);
}
#else
inline static struct dest_info *uri2dst(struct dest_info* dst,
struct sip_msg *msg, str *uri,
int proto )
{
snd_flags_t sflags;
if (msg)
return uri2dst2(dst, msg->force_send_socket, msg->fwd_send_flags,
uri, proto);
SND_FLAGS_INIT(&sflags);
return uri2dst2(dst, 0, sflags, uri, proto);
}
#endif /* USE_DNS_FAILOVER */
#endif /* _TM_UT_H */
|