modules/tm/ut.h

Go to the documentation of this file.
00001 /*
00002  * $Id: ut.h 5299 2008-12-04 18:12:33Z henningw $
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History:
00023  * -------
00024  *  2003-02-13  added proto to uri2proxy (andrei)
00025  *  2003-04-09  uri2sock moved from uac.c (janakj)
00026  *  2003-04-14  added get_proto to determine protocol from uri unless
00027  *              specified explicitly (jiri)
00028  *  2003-07-07  get_proto takes now two protos as arguments (andrei)
00029  *              tls/sips support for get_proto & uri2proxy (andrei)
00030  */
00031 
00032 /*! \file
00033  * \brief TM :: Utilities
00034  *
00035  * \ingroup tm
00036  * - Module: \ref tm
00037  */
00038 
00039 
00040 
00041 #ifndef _TM_UT_H
00042 #define _TM_UT_H
00043 
00044 
00045 #include "../../proxy.h"
00046 #include "../../str.h"
00047 #include "../../parser/parse_uri.h"
00048 #include "../../dprint.h"
00049 #include "../../ut.h"
00050 #include "../../ip_addr.h"
00051 #include "../../error.h"
00052 #include "../../forward.h"
00053 #include "../../mem/mem.h"
00054 #include "../../parser/msg_parser.h"
00055 
00056 /*!
00057  * \brief Protocol choosing helper function.
00058  *
00059  * Choose a protocol depeding on the value of the input variables
00060  * The forced_proto takes precedence if != PROTO_NONE
00061  * \param force_proto forced protocol
00062  * \param proto protocol
00063  * \return choosen protocol
00064  */
00065 inline static enum sip_protos get_proto(enum sip_protos force_proto,
00066                               enum sip_protos proto)
00067 {
00068    /* calculate transport protocol */
00069    switch(force_proto) {
00070       case PROTO_NONE: /* no protocol has been forced -- look at proto */
00071          switch(proto) {
00072             case PROTO_NONE:
00073                return PROTO_NONE;
00074             case PROTO_UDP:/* transport specified explicitly */
00075 #ifdef USE_TCP
00076             case PROTO_TCP:
00077 #endif
00078 #ifdef USE_TLS
00079             case PROTO_TLS:
00080 #endif
00081 #ifdef USE_SCTP
00082             case PROTO_SCTP:
00083 #endif
00084                return proto;
00085             default:
00086                LM_ERR("unsupported transport: %d\n", proto );
00087                return PROTO_NONE;
00088          }
00089       case PROTO_UDP: /* some protocol has been forced -- take it */
00090 #ifdef USE_TCP
00091       case PROTO_TCP:
00092 #endif
00093 #ifdef USE_TLS
00094       case PROTO_TLS:
00095 #endif
00096 #ifdef USE_SCTP
00097       case PROTO_SCTP:
00098 #endif
00099          return force_proto;
00100       default:
00101          LM_ERR("unsupported forced protocol: %d\n", force_proto);
00102          return PROTO_NONE;
00103    }
00104 }
00105 
00106 
00107 
00108 /*!
00109  * \brief Convert a URI into a proxy structure
00110  *
00111  * Convert a URI into a proxy structure. Parse URI, choose a protocol
00112  * and return a new proxy structure, allocated in pkg_memory.
00113  * \param uri input URI
00114  * \param forced_proto force a protocol
00115  * \return proxy structure on success, zero otherwise
00116  */
00117 inline static struct proxy_l *uri2proxy( str *uri, int forced_proto )
00118 {
00119    struct sip_uri parsed_uri;
00120    struct proxy_l *p;
00121 
00122    if (parse_uri(uri->s, uri->len, &parsed_uri) < 0) {
00123       LM_ERR("bad_uri: %.*s\n", uri->len, uri->s );
00124       return 0;
00125    }
00126    
00127    if (parsed_uri.type==SIPS_URI_T &&
00128    ((parsed_uri.proto!=PROTO_TLS) && (parsed_uri.proto!=PROTO_NONE)) ) {
00129       LM_ERR("bad transport for sips uri: %d\n", parsed_uri.proto);
00130       return 0;
00131    }
00132    p = mk_proxy( &parsed_uri.host, parsed_uri.port_no,
00133       get_proto(forced_proto, parsed_uri.proto),
00134       (parsed_uri.type==SIPS_URI_T)?1:0 );
00135    if (p == 0) {
00136       LM_ERR("bad host name in URI <%.*s>\n", uri->len, ZSW(uri->s));
00137       return 0;
00138    }
00139    
00140    return p;
00141 }
00142 
00143 /*!
00144  * \brief Convert a URI into socket address.
00145  *
00146  * Convert a URI into a socket address. Create a temporary proxy.
00147  * \param uri input URI
00148  * \param to_su target structure
00149  * \param proto protocol
00150  * \return choosen protocol
00151  */
00152 static inline int uri2su(str *uri, union sockaddr_union *to_su, int proto)
00153 {
00154    struct proxy_l *proxy;
00155 
00156    proxy = uri2proxy(uri, proto);
00157    if (!proxy) {
00158       ser_error = E_BAD_ADDRESS;
00159       LM_ERR("failed create a dst proxy\n");
00160       return -1;
00161    }
00162 
00163    hostent2su(to_su, &proxy->host, proxy->addr_idx, 
00164       (proxy->port) ? proxy->port : SIP_PORT);
00165    proto = proxy->proto;
00166 
00167    free_proxy(proxy);
00168    pkg_free(proxy);
00169    return proto;
00170 }
00171 
00172 
00173 
00174 /*!
00175  * \brief Convert a URI into a socket_info
00176  *
00177  * Convert a URI into a socket_info for sending.
00178  * \param msg SIP message
00179  * \param uri input URI
00180  * \param to_su socket address
00181  * \param proto protocol
00182  * \return send socket
00183  */
00184 static inline struct socket_info *uri2sock(struct sip_msg* msg, str *uri,
00185                            union sockaddr_union *to_su, int proto)
00186 {
00187    struct socket_info* send_sock;
00188 
00189    if ( (proto=uri2su(uri, to_su, proto))==-1 )
00190       return 0;
00191 
00192    send_sock = get_send_socket(msg, to_su, proto);
00193    if (!send_sock) {
00194       LM_ERR("no corresponding socket for af %d\n", to_su->s.sa_family);
00195       ser_error = E_NO_SOCKET;
00196    }
00197 
00198    return send_sock;
00199 }
00200 
00201 
00202 #endif /* _TM_UT_H */

Generated on Wed May 23 20:00:32 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6