sip-uri.h 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
        belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program 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.

    This program 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, see <http://www.gnu.org/licenses/>.
*/

jehan's avatar
jehan committed
19 20
#ifndef BELLE_SIP_URI_H_
#define BELLE_SIP_URI_H_
21

22
#include "belle-sip/defs.h"
23
#include "belle-sip/list.h"
Ghislain MARY's avatar
Ghislain MARY committed
24
#include "belle-sip/utils.h"
Simon Morlat's avatar
Simon Morlat committed
25
#include "belle-sip/types.h"
26

27
BELLE_SIP_BEGIN_DECLS
Ghislain MARY's avatar
Ghislain MARY committed
28

29 30 31
/**
 *
 */
32
BELLESIP_EXPORT belle_sip_uri_t* belle_sip_uri_new(void);
jehan's avatar
jehan committed
33

34 35 36 37
/**
 *
 */
BELLESIP_EXPORT belle_sip_uri_t* belle_sip_uri_parse (const char* uri) ;
jehan's avatar
jehan committed
38 39 40 41 42 43

/**
 *
 */
BELLESIP_EXPORT belle_sip_uri_t* belle_sip_fast_uri_parse (const char* uri) ;

jehan's avatar
jehan committed
44 45 46 47
/**
 * same as belle_sip_uri_parse but with much less checks
 */
BELLESIP_EXPORT belle_sip_uri_t* belle_sip_fast_uri_parse (const char* uri) ;
48 49 50 51
/**
 *
 */
BELLESIP_EXPORT belle_sip_uri_t* belle_sip_uri_create (const char* username,const char* host) ;
52

53

54 55 56 57 58
/**
 *	Returns the value of the named header, or null if it is not set.
 *
 */
BELLESIP_EXPORT const char*	belle_sip_uri_get_header(const belle_sip_uri_t* uri,const char* name);
jehan's avatar
jehan committed
59 60 61 62 63 64 65 66 67


/**
 *	remove all headers
 *
 */
BELLESIP_EXPORT void belle_sip_uri_headers_clean(belle_sip_uri_t* uri);


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
/**
 * Returns an Iterator over the const char*names of all headers present in this SipURI.
 *
 */
BELLESIP_EXPORT const belle_sip_list_t*	belle_sip_uri_get_header_names(const belle_sip_uri_t* uri) ;
/**
 * 	          Returns the host part of this SipURI.
 *
 */
BELLESIP_EXPORT const char*	belle_sip_uri_get_host(const belle_sip_uri_t* uri) ;
/**
 * 	          Returns the value of the maddr parameter, or null if this is not set.
 *
 */
BELLESIP_EXPORT const char*	belle_sip_uri_get_maddr_param(const belle_sip_uri_t* uri) ;
/**
 *	          Returns the value of the method parameter, or null if this is not set.
 *
 */
BELLESIP_EXPORT const char*	belle_sip_uri_get_method_param(const belle_sip_uri_t* uri) ;
/**
 *	          Returns the port part of this SipURI.
 *
 */
BELLESIP_EXPORT int	belle_sip_uri_get_port(const belle_sip_uri_t* uri) ;
/**
 * Returns the port of the uri, if not specified in the uri returns the well known port according to the transport.
**/
BELLESIP_EXPORT int belle_sip_uri_get_listening_port(const belle_sip_uri_t *uri);
/**
 * 	          Returns the value of the "transport" parameter, or null if this is not set.
 *
 */
BELLESIP_EXPORT const char*	belle_sip_uri_get_transport_param(const belle_sip_uri_t* uri) ;
/**
 * 	          Returns the value of the "ttl" parameter, or -1 if this is not set.
 *
 */
BELLESIP_EXPORT int	belle_sip_uri_get_ttl_param(const belle_sip_uri_t* uri) ;
/**
 * 	          Returns the user part of this SipURI.
 *
 */
BELLESIP_EXPORT const char*	belle_sip_uri_get_user(const belle_sip_uri_t* uri) ;
/**
 * 	          Returns the value of the userParam, or null if this is not set.
 *
 */
BELLESIP_EXPORT const char*	belle_sip_uri_get_user_param(const belle_sip_uri_t* uri) ;
/**
 * 	          Gets user password of SipURI, or null if it is not set.
 *
 */
BELLESIP_EXPORT const char*	belle_sip_uri_get_user_password(const belle_sip_uri_t* uri) ;
/**
 *	          Returns whether the the lr parameter is set.
 *
 */
BELLESIP_EXPORT unsigned int	belle_sip_uri_has_lr_param(const belle_sip_uri_t* uri) ;
/**
 *
 * 	          Returns true if this SipURI is secure i.e. if this SipURI represents a sips URI.
 *
 */
BELLESIP_EXPORT unsigned int	belle_sip_uri_is_secure(const belle_sip_uri_t* uri) ;
133

134
/**
135
 * 	          Sets the value of the specified header fields to be included in a request constructed from the URI.
136 137
 *
 */
138 139
BELLESIP_EXPORT void	belle_sip_uri_set_header(belle_sip_uri_t* uri,const char*name, const char*value) ;

140
/**
141 142 143 144 145 146
 * Removes specified header from uri.
**/
BELLESIP_EXPORT void belle_sip_uri_remove_header(belle_sip_uri_t *uri, const char *name);

/**
 * 	          Removes the port part of this SipURI.
147 148
 *
 */
149
BELLESIP_EXPORT void	belle_sip_uri_remove_port(belle_sip_uri_t* uri) ;
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
/**
 * 	          Set the host part of this SipURI to the newly supplied host parameter.
 *
 */
BELLESIP_EXPORT void	belle_sip_uri_set_host(belle_sip_uri_t* uri,const char*host) ;
/**
 * 	          Sets the value of the lr parameter of this SipURI.
 *
 */
BELLESIP_EXPORT void	belle_sip_uri_set_lr_param(belle_sip_uri_t* uri,unsigned int param) ;
/**
 *          Sets the value of the maddr parameter of this SipURI.
 *
 */
BELLESIP_EXPORT void	belle_sip_uri_set_maddr_param(belle_sip_uri_t* uri,const char*mAddr) ;
/**
 *	          Sets the value of the method parameter.
 *
 */
BELLESIP_EXPORT void	belle_sip_uri_set_method_param(belle_sip_uri_t* uri,const char*method) ;
/**
 * 	          Set the port part of this SipURI to the newly supplied port parameter.
 *
 */
BELLESIP_EXPORT void	belle_sip_uri_set_port(belle_sip_uri_t* uri, int port) ;
/**
 * 	          Sets the scheme of this URI to sip or sips depending on whether the argument is true or false.
 *
 */
BELLESIP_EXPORT void	belle_sip_uri_set_secure(belle_sip_uri_t* uri,unsigned int secure) ;
/**
 * 	          Sets the value of the "transport" parameter.
 *
 */
BELLESIP_EXPORT void	belle_sip_uri_set_transport_param(belle_sip_uri_t* uri,const char*transport) ;
/**
 *  	          Sets the value of the ttl parameter.
 *
 */
BELLESIP_EXPORT void	belle_sip_uri_set_ttl_param(belle_sip_uri_t* uri,int ttl) ;
/**
 *  	          Sets the user of SipURI.
 *
 */
BELLESIP_EXPORT void	belle_sip_uri_set_user(belle_sip_uri_t* uri,const char*user) ;
/**
 * 	          Sets the value of the user parameter.
 *
 */
BELLESIP_EXPORT void	belle_sip_uri_set_user_param(belle_sip_uri_t* uri,const char*userParam) ;
/**
 * 	          Sets the user password associated with the user of SipURI.
 *
 */
BELLESIP_EXPORT void	belle_sip_uri_set_user_password(belle_sip_uri_t* uri,const char*userPassword) ;

/**
 * 	          This method returns the URI as a string.
 *
 */
210
BELLESIP_EXPORT char*	belle_sip_uri_to_string(const belle_sip_uri_t* uri) ;
211

212
belle_sip_error_code belle_sip_uri_marshal(const belle_sip_uri_t* uri, char* buff, size_t buff_size, size_t *offset);
213

214 215 216 217 218 219 220 221 222
#define BELLE_SIP_URI(obj) BELLE_SIP_CAST(obj,belle_sip_uri_t)

/**define URI equality as using comparison rules from RFC3261 section 19.1.4
 * @param belle_sip_uri_t* uri_a
 * @param belle_sip_uri_t* uri_a
 * @return 0 if not matched.
 *
 * */
BELLESIP_EXPORT int belle_sip_uri_equals(const belle_sip_uri_t* uri_a,const belle_sip_uri_t* uri_b);
jehan's avatar
jehan committed
223 224 225 226 227 228 229 230
/**
 * returns 0 if uri does follows components requirement for being a request uri
 * */
BELLESIP_EXPORT int belle_sip_uri_check_components_from_request_uri(const belle_sip_uri_t* uri);
/**
 * returns 0 if uri does follows components requirement for a given method/header
 */
BELLESIP_EXPORT int belle_sip_uri_check_components_from_context(const belle_sip_uri_t* uri,const char* method,const char* header_name);
231
BELLE_SIP_END_DECLS
232 233 234
#ifndef BELLE_SIP_USE_STL
#define BELLE_SIP_USE_STL 1
#endif
235

236
#if __cplusplus >= 201103L && BELLE_SIP_USE_STL
237
#include <ostream>
238 239
inline   std::ostream&
operator<<( std::ostream& __os, const belle_sip_uri_t* uri)
240 241 242 243 244 245
{
	char* uri_as_string = belle_sip_uri_to_string(uri);
	__os << uri_as_string;
	belle_sip_free(uri_as_string);
	return __os;
}
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
namespace std {
	template <> struct hash<const belle_sip_uri_t*> {
		size_t operator()(const belle_sip_uri_t *x ) const {
			hash<string> H;
			size_t h=0;
			if (belle_sip_uri_get_user(x))
				h = H(belle_sip_uri_get_user(x));
			if (belle_sip_uri_get_host(x))
				h ^=H(belle_sip_uri_get_host(x));
			if (belle_sip_uri_get_port(x)>0) {
				std::hash<int> H2;
				h ^=H2(belle_sip_uri_get_port(x));
			}
			if (belle_sip_uri_get_transport_param(x)) {
				h ^=H(belle_sip_uri_get_transport_param(x));
			}
			if (belle_sip_uri_is_secure(x))
				h+=1;
	
			return h;
266
		}
267 268
	};
}
269 270 271 272 273 274 275 276 277 278 279 280 281 282

#include <functional>

namespace bellesip {

struct UriComparator : public std::binary_function<belle_sip_uri_t*, belle_sip_uri_t*, bool> {
	bool operator()(const belle_sip_uri_t* lhs, const belle_sip_uri_t* rhs) const {
		return belle_sip_uri_equals(lhs,rhs);
	}
};
}



283
#endif
Ghislain MARY's avatar
Ghislain MARY committed
284

jehan's avatar
jehan committed
285
#endif  /*BELLE_SIP_URI_H_*/
286