address.c 8.21 KB
Newer Older
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
/*
linphone
Copyright (C) 2009  Simon MORLAT (simon.morlat@linphone.org)

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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "linphonecore.h"
#include "lpconfig.h"
#include "private.h"

/**
 * @addtogroup linphone_address
 * @{
**/

/**
 * Constructs a LinphoneAddress object by parsing the user supplied address,
 * given as a string.
**/
33
LinphoneAddress * linphone_address_new(const char *addr){
34
	SalAddress *saddr=sal_address_new(addr);
35 36
	if (saddr==NULL)
		ms_error("Cannot create LinphoneAddress, bad uri [%s]",addr);
37
	return saddr;
38 39 40 41 42
}

/**
 * Clones a LinphoneAddress object.
**/
43 44
LinphoneAddress * linphone_address_clone(const LinphoneAddress *addr){
	return sal_address_clone(addr);
45 46
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60
/**
 * Increment reference count of LinphoneAddress object.
**/
LinphoneAddress * linphone_address_ref(LinphoneAddress *addr){
	return sal_address_ref(addr);
}

/**
 * Decrement reference count of LinphoneAddress object. When dropped to zero, memory is freed.
**/
void linphone_address_unref(LinphoneAddress *addr){
	sal_address_unref(addr);
}

61 62 63 64
/**
 * Returns the address scheme, normally "sip".
**/
const char *linphone_address_get_scheme(const LinphoneAddress *u){
65
	return sal_address_get_scheme(u);
66 67 68 69 70 71
}

/**
 * Returns the display name.
**/
const char *linphone_address_get_display_name(const LinphoneAddress* u){
72
	return sal_address_get_display_name(u);
73 74 75 76 77 78
}

/**
 * Returns the username.
**/
const char *linphone_address_get_username(const LinphoneAddress *u){
79
	return sal_address_get_username(u);
80 81 82 83 84 85
}

/**
 * Returns the domain name.
**/
const char *linphone_address_get_domain(const LinphoneAddress *u){
86
	return sal_address_get_domain(u);
87 88 89 90 91
}

/**
 * Sets the display name.
**/
92
int linphone_address_set_display_name(LinphoneAddress *u, const char *display_name){
93
	sal_address_set_display_name(u,display_name);
94
	return 0;
95 96 97 98 99
}

/**
 * Sets the username.
**/
100
int linphone_address_set_username(LinphoneAddress *uri, const char *username){
101
	sal_address_set_username(uri,username);
102
	return 0;
103 104 105 106 107
}

/**
 * Sets the domain.
**/
108 109 110 111 112 113 114 115 116 117 118 119
int linphone_address_set_domain(LinphoneAddress *uri, const char *host){
	if (host) {
		char *identity = ms_strdup_printf("sip:%s", host);
		LinphoneAddress* test = linphone_address_new(identity);
		ms_free(identity);
		if (test) {
			sal_address_set_domain(uri,host);
			linphone_address_destroy(test);
			return 0;
		}
	}
	return -1;
120 121 122 123 124 125
}


/**
 * Sets the port number.
**/
126
int linphone_address_set_port(LinphoneAddress *uri, int port){
127
	sal_address_set_port(uri,port);
128
	return 0;
129 130 131 132 133
}

/**
 * Set a transport.
**/
134
int linphone_address_set_transport(LinphoneAddress *uri, LinphoneTransportType tp){
135
	sal_address_set_transport(uri,(SalTransport)tp);
136
	return 0;
137 138 139 140 141 142 143
}

/**
 * Get the transport.
**/
LinphoneTransportType linphone_address_get_transport(const LinphoneAddress *uri){
	return (LinphoneTransportType)sal_address_get_transport(uri);
144 145
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159
/**
 * Set the value of the method parameter
**/
void linphone_address_set_method_param(LinphoneAddress *addr, const char *method) {
	sal_address_set_method_param(addr, method);
}

/**
 * Get the value of the method parameter
**/
const char *linphone_address_get_method_param(const LinphoneAddress *addr) {
	return sal_address_get_method_param(addr);
}

160 161 162 163
/**
 * Removes address's tags and uri headers so that it is displayable to the user.
**/
void linphone_address_clean(LinphoneAddress *uri){
164
	sal_address_clean(uri);
165 166 167 168 169 170 171
}

/**
 * Returns the address as a string.
 * The returned char * must be freed by the application. Use ms_free().
**/
char *linphone_address_as_string(const LinphoneAddress *u){
172
	return sal_address_as_string(u);
173 174 175 176 177 178 179
}

/**
 * Returns the SIP uri only as a string, that is display name is removed.
 * The returned char * must be freed by the application. Use ms_free().
**/
char *linphone_address_as_string_uri_only(const LinphoneAddress *u){
180
	return sal_address_as_string_uri_only(u);
181 182
}

183 184
/**
 * Returns true if address refers to a secure location (sips)
185
 * @deprecated use linphone_address_get_secure()
186 187 188 189 190
**/
bool_t linphone_address_is_secure(const LinphoneAddress *uri){
	return sal_address_is_secure(uri);
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
/**
 * Returns true if address refers to a secure location (sips)
**/
bool_t linphone_address_get_secure(const LinphoneAddress *uri){
	return sal_address_is_secure(uri);
}

/**
 * Make the address refer to a secure location (sips scheme)
 * @param enabled TRUE if address is requested to be secure.
**/
void linphone_address_set_secure(LinphoneAddress *addr, bool_t enabled){
	sal_address_set_secure(addr, enabled);
}

206 207 208 209 210 211 212
/**
 * returns true if address is a routable sip address
 */
bool_t linphone_address_is_sip(const LinphoneAddress *uri){
	return sal_address_is_sip(uri);
}

Simon Morlat's avatar
Simon Morlat committed
213 214 215 216 217 218 219 220
static bool_t strings_equals(const char *s1, const char *s2){
	if (s1==NULL && s2==NULL) return TRUE;
	if (s1!=NULL && s2!=NULL && strcmp(s1,s2)==0) return TRUE;
	return FALSE;
}

/**
 * Compare two LinphoneAddress ignoring tags and headers, basically just domain, username, and port.
221 222 223 224
 * @param[in] a1 LinphoneAddress object
 * @param[in] a2 LinphoneAddress object
 * @return Boolean value telling if the LinphoneAddress objects are equal.
 * @see linphone_address_equal()
Simon Morlat's avatar
Simon Morlat committed
225
**/
226
bool_t linphone_address_weak_equal(const LinphoneAddress *a1, const LinphoneAddress *a2){
Simon Morlat's avatar
Simon Morlat committed
227 228 229 230 231
	const char *u1,*u2;
	const char *h1,*h2;
	int p1,p2;
	u1=linphone_address_get_username(a1);
	u2=linphone_address_get_username(a2);
232 233
	p1=linphone_address_get_port(a1);
	p2=linphone_address_get_port(a2);
Simon Morlat's avatar
Simon Morlat committed
234 235 236 237 238
	h1=linphone_address_get_domain(a1);
	h2=linphone_address_get_domain(a2);
	return strings_equals(u1,u2) && strings_equals(h1,h2) && p1==p2;
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/**
 * Compare two LinphoneAddress taking the tags and headers into account.
 * @param[in] a1 LinphoneAddress object
 * @param[in] a2 LinphoneAddress object
 * @return Boolean value telling if the LinphoneAddress objects are equal.
 * @see linphone_address_weak_equal()
 */
bool_t linphone_address_equal(const LinphoneAddress *a1, const LinphoneAddress *a2) {
	char *s1;
	char *s2;
	bool_t res;
	if ((a1 == NULL) && (a2 == NULL)) return TRUE;
	if ((a1 == NULL) || (a2 == NULL)) return FALSE;
	s1 = linphone_address_as_string(a1);
	s2 = linphone_address_as_string(a2);
	res = (strcmp(s1, s2) == 0) ? TRUE : FALSE;
	ms_free(s1);
	ms_free(s2);
	return res;
}

260
/**
261
 * Destroys a LinphoneAddress object (actually calls linphone_address_unref()).
262
 * @deprecated Use linphone_address_unref() instead
263 264
**/
void linphone_address_destroy(LinphoneAddress *u){
265
	sal_address_unref(u);
266 267
}

268 269 270
/**
 * Get port number as an integer value.
 */
Simon Morlat's avatar
Simon Morlat committed
271

272 273 274
/**
 * Get port number, 0 if not present.
 */
275
int linphone_address_get_port(const LinphoneAddress *u) {
jehan's avatar
jehan committed
276 277
	return sal_address_get_port(u);
}
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
/**
 * Set the password encoded in the address.
 * It is used for basic authentication (not recommended).
 * @param addr the LinphoneAddress
 * @param passwd the password to set.
**/
void linphone_address_set_password(LinphoneAddress *addr, const char *passwd){
	sal_address_set_password(addr,passwd);
}

/**
 * Get the password encoded in the address.
 * It is used for basic authentication (not recommended).
 * @param addr the address
 * @return the password, if any, NULL otherwise.
**/
const char *linphone_address_get_password(const LinphoneAddress *addr){
	return sal_address_get_password(addr);
}

/**
 * Set a header into the address.
 * Headers appear in the URI with '?', such as <sip:test@linphone.org?SomeHeader=SomeValue>.
 * @param addr the address
 * @param header_name the header name
 * @param header_value the header value
**/
void linphone_address_set_header(LinphoneAddress *addr, const char *header_name, const char *header_value){
	sal_address_set_header(addr,header_name,header_value);
}

310 311 312 313
LinphoneAddress * linphone_core_create_address(LinphoneCore *lc, const char *address) {
	return linphone_address_new(address);
}

314
/** @} */