4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include "sip_parse_uri.h"
36 sip_free_parsed_uri(sip_uri_t uri
)
43 _uri
= (_sip_uri_t
*)uri
;
44 if (_uri
->sip_uri_issip
) {
46 sip_param_t
*param_next
;
48 param
= _uri
->sip_uri_params
;
49 while (param
!= NULL
) {
50 param_next
= param
->param_next
;
59 * Parse the URI in uri_str
62 sip_parse_uri(sip_str_t
*uri_str
, int *error
)
64 struct sip_uri
*parsed_uri
;
69 if (uri_str
== NULL
) {
74 parsed_uri
= calloc(1, sizeof (_sip_uri_t
));
75 if (parsed_uri
== NULL
) {
81 sip_uri_parse_it(parsed_uri
, uri_str
);
82 if (parsed_uri
->sip_uri_errflags
& SIP_URIERR_MEMORY
) {
88 if (parsed_uri
->sip_uri_errflags
!= 0 && error
!= NULL
)
90 return ((sip_uri_t
)parsed_uri
);
96 const struct sip_uri
*
97 sip_get_uri_parsed(sip_header_value_t value
, int *error
)
99 const struct sip_uri
*ret
= NULL
;
103 if (value
== NULL
|| value
->sip_value_parse_uri
== NULL
||
104 value
->value_state
== SIP_VALUE_DELETED
) {
109 ret
= value
->sip_value_parse_uri
;
110 if (ret
->sip_uri_errflags
!= 0 && error
!= NULL
)
112 return ((sip_uri_t
)ret
);
116 * Return TRUE if this is a SIP URI
119 sip_is_sipuri(const struct sip_uri
*uri
)
125 _uri
= (_sip_uri_t
*)uri
;
126 if ((_uri
->sip_uri_errflags
& SIP_URIERR_SCHEME
) == 0 &&
127 _uri
->sip_uri_scheme
.sip_str_len
> 0 && _uri
->sip_uri_issip
) {
137 sip_check_get_param(const struct sip_uri
*uri
, int *error
)
147 return ((_sip_uri_t
*)uri
);
152 * Return the URI scheme
155 sip_get_uri_scheme(const struct sip_uri
*uri
, int *error
)
159 _uri
= sip_check_get_param(uri
, error
);
163 if (((_uri
->sip_uri_errflags
& SIP_URIERR_SCHEME
) != 0 ||
164 _uri
->sip_uri_scheme
.sip_str_len
== 0) && error
!= NULL
) {
167 if (_uri
->sip_uri_scheme
.sip_str_len
> 0)
168 return (&_uri
->sip_uri_scheme
);
173 * Return user name from URI
176 sip_get_uri_user(const struct sip_uri
*uri
, int *error
)
180 _uri
= sip_check_get_param(uri
, error
);
184 if ((_uri
->sip_uri_errflags
& SIP_URIERR_USER
) != 0 && error
!= NULL
)
186 if (uri
->sip_uri_user
.sip_str_len
> 0)
187 return (&uri
->sip_uri_user
);
192 * Return password from URI
195 sip_get_uri_password(const struct sip_uri
*uri
, int *error
)
199 _uri
= sip_check_get_param(uri
, error
);
203 if ((_uri
->sip_uri_errflags
& SIP_URIERR_PASS
) != 0 && error
!= NULL
)
205 if (_uri
->sip_uri_password
.sip_str_len
> 0)
206 return (&_uri
->sip_uri_password
);
211 * Get host from the URI
214 sip_get_uri_host(const struct sip_uri
*uri
, int *error
)
218 _uri
= sip_check_get_param(uri
, error
);
222 if ((_uri
->sip_uri_errflags
& SIP_URIERR_HOST
) != 0 && error
!= NULL
)
224 if (_uri
->sip_uri_host
.sip_str_len
> 0)
225 return (&_uri
->sip_uri_host
);
230 * Get port from the URI
233 sip_get_uri_port(const struct sip_uri
*uri
, int *error
)
237 _uri
= sip_check_get_param(uri
, error
);
241 if ((_uri
->sip_uri_errflags
& SIP_URIERR_PORT
) != 0) {
246 return (_uri
->sip_uri_port
);
250 sip_get_uri_params(const struct sip_uri
*uri
, int *error
)
254 _uri
= sip_check_get_param(uri
, error
);
258 if (!_uri
->sip_uri_issip
) {
264 if ((_uri
->sip_uri_errflags
& SIP_URIERR_PARAM
) != 0 && error
!= NULL
)
266 return (_uri
->sip_uri_params
);
270 * Get headers from the URI
273 sip_get_uri_headers(const struct sip_uri
*uri
, int *error
)
277 _uri
= sip_check_get_param(uri
, error
);
281 if (!_uri
->sip_uri_issip
) {
286 if ((_uri
->sip_uri_errflags
& SIP_URIERR_HEADER
) != 0 && error
!= NULL
)
288 if (_uri
->sip_uri_headers
.sip_str_len
> 0)
289 return (&_uri
->sip_uri_headers
);
294 * Return opaque value for an ABS URI
297 sip_get_uri_opaque(const struct sip_uri
*uri
, int *error
)
301 _uri
= sip_check_get_param(uri
, error
);
305 if (_uri
->sip_uri_issip
) {
310 if ((_uri
->sip_uri_errflags
& SIP_URIERR_OPAQUE
) != 0 && error
!= NULL
)
312 if (_uri
->sip_uri_opaque
.sip_str_len
> 0)
313 return (&_uri
->sip_uri_opaque
);
318 * Return query from an absolute URI
321 sip_get_uri_query(const struct sip_uri
*uri
, int *error
)
325 _uri
= sip_check_get_param(uri
, error
);
329 if (_uri
->sip_uri_issip
) {
334 if ((_uri
->sip_uri_errflags
& SIP_URIERR_QUERY
) != 0 && error
!= NULL
)
336 if (_uri
->sip_uri_query
.sip_str_len
> 0)
337 return (&_uri
->sip_uri_query
);
342 * Get path from an assolute URI
345 sip_get_uri_path(const struct sip_uri
*uri
, int *error
)
349 _uri
= sip_check_get_param(uri
, error
);
353 if (_uri
->sip_uri_issip
) {
358 if ((_uri
->sip_uri_errflags
& SIP_URIERR_PATH
) != 0 && error
!= NULL
)
360 if (_uri
->sip_uri_path
.sip_str_len
> 0)
361 return (&_uri
->sip_uri_path
);
366 * Get the reg-name from absolute URI
369 sip_get_uri_regname(const struct sip_uri
*uri
, int *error
)
373 _uri
= sip_check_get_param(uri
, error
);
377 if (_uri
->sip_uri_issip
) {
382 if ((_uri
->sip_uri_errflags
& SIP_URIERR_REGNAME
) != 0 && error
!= NULL
)
384 if (_uri
->sip_uri_regname
.sip_str_len
> 0)
385 return (&_uri
->sip_uri_regname
);
390 * Return TRUE if this is a teluser
393 sip_is_uri_teluser(const struct sip_uri
*uri
)
400 _uri
= (_sip_uri_t
*)uri
;
401 return (_uri
->sip_uri_isteluser
);
405 sip_get_uri_errflags(const struct sip_uri
*uri
, int *error
)
409 _uri
= sip_check_get_param(uri
, error
);
412 return (_uri
->sip_uri_errflags
);
416 * the caller is responsible for freeing the returned string
419 sip_uri_errflags_to_str(int errflags
)
421 char *err_info
= NULL
;
426 err_info
= (char *)malloc(SIP_URI_BUF_SIZE
);
427 if (err_info
== NULL
)
430 if (errflags
& SIP_URIERR_NOURI
) {
431 (void) strncpy(err_info
, "Error : No URI",
432 strlen("Error : No URI"));
433 err_info
[strlen("Error : No URI")] = '\0';
437 (void) strncpy(err_info
, "Error(s) in", strlen("Error(s) in"));
438 err_info
[strlen("Error(s) in")] = '\0';
439 if (errflags
& SIP_URIERR_SCHEME
)
440 (void) strncat(err_info
, " SCHEME,", strlen(" SCHEME,"));
441 if (errflags
& SIP_URIERR_USER
)
442 (void) strncat(err_info
, " USER,", strlen(" USER,"));
443 if (errflags
& SIP_URIERR_PASS
)
444 (void) strncat(err_info
, " PASSWORD,", strlen(" PASSWORD,"));
445 if (errflags
& SIP_URIERR_HOST
)
446 (void) strncat(err_info
, " HOST,", strlen(" HOST,"));
447 if (errflags
& SIP_URIERR_PORT
)
448 (void) strncat(err_info
, " PORT,", strlen(" PORT,"));
449 if (errflags
& SIP_URIERR_PARAM
) {
450 (void) strncat(err_info
, " PARAMETERS,",
451 strlen(" PARAMETERS,"));
453 if (errflags
& SIP_URIERR_HEADER
)
454 (void) strncat(err_info
, " HEADERS,", strlen(" HEADERS,"));
455 if (errflags
& SIP_URIERR_OPAQUE
)
456 (void) strncat(err_info
, " OPAQUE,", strlen(" OPAQUE,"));
457 if (errflags
& SIP_URIERR_QUERY
)
458 (void) strncat(err_info
, " QUERY,", strlen(" QUERY,"));
459 if (errflags
& SIP_URIERR_PATH
)
460 (void) strncat(err_info
, " PATH,", strlen(" PATH,"));
461 if (errflags
& SIP_URIERR_REGNAME
)
462 (void) strncat(err_info
, " REG-NAME,", strlen(" REG-NAME,"));
463 if (strlen(err_info
) == strlen("Error(s) in")) {
467 err_info
[strlen(err_info
) - 1] = '\0';
468 (void) strncat(err_info
, " part(s)", strlen(" part(s)"));