dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libsip / common / sip_uri_ui.c
blob53f856bdced7c3e20ea49b0e1757f2f45d0c5e0d
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <errno.h>
32 #include "sip_parse_uri.h"
34 void
35 sip_free_parsed_uri(sip_uri_t uri)
37 _sip_uri_t *_uri;
39 if (uri == NULL)
40 return;
42 _uri = (_sip_uri_t *)uri;
43 if (_uri->sip_uri_issip) {
44 sip_param_t *param;
45 sip_param_t *param_next;
47 param = _uri->sip_uri_params;
48 while (param != NULL) {
49 param_next = param->param_next;
50 free(param);
51 param = param_next;
54 free(_uri);
58 * Parse the URI in uri_str
60 struct sip_uri *
61 sip_parse_uri(sip_str_t *uri_str, int *error)
63 struct sip_uri *parsed_uri;
65 if (error != NULL)
66 *error = 0;
68 if (uri_str == NULL) {
69 if (error != NULL)
70 *error = EINVAL;
71 return (NULL);
73 parsed_uri = calloc(1, sizeof (_sip_uri_t));
74 if (parsed_uri == NULL) {
75 if (error != NULL)
76 *error = ENOMEM;
77 return (NULL);
80 sip_uri_parse_it(parsed_uri, uri_str);
81 if (parsed_uri->sip_uri_errflags & SIP_URIERR_MEMORY) {
82 free(parsed_uri);
83 if (error != NULL)
84 *error = ENOMEM;
85 return (NULL);
87 if (parsed_uri->sip_uri_errflags != 0 && error != NULL)
88 *error = EPROTO;
89 return ((sip_uri_t)parsed_uri);
93 * Get parsed URI
95 const struct sip_uri *
96 sip_get_uri_parsed(sip_header_value_t value, int *error)
98 const struct sip_uri *ret = NULL;
100 if (error != NULL)
101 *error = 0;
102 if (value == NULL || value->sip_value_parse_uri == NULL ||
103 value->value_state == SIP_VALUE_DELETED) {
104 if (error != NULL)
105 *error = EINVAL;
106 return (NULL);
108 ret = value->sip_value_parse_uri;
109 if (ret->sip_uri_errflags != 0 && error != NULL)
110 *error = EINVAL;
111 return ((sip_uri_t)ret);
115 * Return TRUE if this is a SIP URI
117 boolean_t
118 sip_is_sipuri(const struct sip_uri *uri)
120 _sip_uri_t *_uri;
122 if (uri == NULL)
123 return (B_FALSE);
124 _uri = (_sip_uri_t *)uri;
125 if ((_uri->sip_uri_errflags & SIP_URIERR_SCHEME) == 0 &&
126 _uri->sip_uri_scheme.sip_str_len > 0 && _uri->sip_uri_issip) {
127 return (B_TRUE);
129 return (B_FALSE);
133 * Some common checks
135 static _sip_uri_t *
136 sip_check_get_param(const struct sip_uri *uri, int *error)
138 if (error != NULL)
139 *error = 0;
141 if (uri == NULL) {
142 if (error != NULL)
143 *error = EINVAL;
144 return (NULL);
146 return ((_sip_uri_t *)uri);
151 * Return the URI scheme
153 const sip_str_t *
154 sip_get_uri_scheme(const struct sip_uri *uri, int *error)
156 _sip_uri_t *_uri;
158 _uri = sip_check_get_param(uri, error);
159 if (_uri == NULL)
160 return (NULL);
162 if (((_uri->sip_uri_errflags & SIP_URIERR_SCHEME) != 0 ||
163 _uri->sip_uri_scheme.sip_str_len == 0) && error != NULL) {
164 *error = EINVAL;
166 if (_uri->sip_uri_scheme.sip_str_len > 0)
167 return (&_uri->sip_uri_scheme);
168 return (NULL);
172 * Return user name from URI
174 const sip_str_t *
175 sip_get_uri_user(const struct sip_uri *uri, int *error)
177 _sip_uri_t *_uri;
179 _uri = sip_check_get_param(uri, error);
180 if (_uri == NULL)
181 return (NULL);
183 if ((_uri->sip_uri_errflags & SIP_URIERR_USER) != 0 && error != NULL)
184 *error = EINVAL;
185 if (uri->sip_uri_user.sip_str_len > 0)
186 return (&uri->sip_uri_user);
187 return (NULL);
191 * Return password from URI
193 const sip_str_t *
194 sip_get_uri_password(const struct sip_uri *uri, int *error)
196 _sip_uri_t *_uri;
198 _uri = sip_check_get_param(uri, error);
199 if (_uri == NULL)
200 return (NULL);
202 if ((_uri->sip_uri_errflags & SIP_URIERR_PASS) != 0 && error != NULL)
203 *error = EINVAL;
204 if (_uri->sip_uri_password.sip_str_len > 0)
205 return (&_uri->sip_uri_password);
206 return (NULL);
210 * Get host from the URI
212 const sip_str_t *
213 sip_get_uri_host(const struct sip_uri *uri, int *error)
215 _sip_uri_t *_uri;
217 _uri = sip_check_get_param(uri, error);
218 if (_uri == NULL)
219 return (NULL);
221 if ((_uri->sip_uri_errflags & SIP_URIERR_HOST) != 0 && error != NULL)
222 *error = EINVAL;
223 if (_uri->sip_uri_host.sip_str_len > 0)
224 return (&_uri->sip_uri_host);
225 return (NULL);
229 * Get port from the URI
232 sip_get_uri_port(const struct sip_uri *uri, int *error)
234 _sip_uri_t *_uri;
236 _uri = sip_check_get_param(uri, error);
237 if (_uri == NULL)
238 return (0);
240 if ((_uri->sip_uri_errflags & SIP_URIERR_PORT) != 0) {
241 if (error != NULL)
242 *error = EINVAL;
243 return (0);
245 return (_uri->sip_uri_port);
248 const sip_param_t *
249 sip_get_uri_params(const struct sip_uri *uri, int *error)
251 _sip_uri_t *_uri;
253 _uri = sip_check_get_param(uri, error);
254 if (_uri == NULL)
255 return (NULL);
257 if (!_uri->sip_uri_issip) {
258 if (error != NULL)
259 *error = EINVAL;
260 return (NULL);
263 if ((_uri->sip_uri_errflags & SIP_URIERR_PARAM) != 0 && error != NULL)
264 *error = EINVAL;
265 return (_uri->sip_uri_params);
269 * Get headers from the URI
271 const sip_str_t *
272 sip_get_uri_headers(const struct sip_uri *uri, int *error)
274 _sip_uri_t *_uri;
276 _uri = sip_check_get_param(uri, error);
277 if (_uri == NULL)
278 return (NULL);
280 if (!_uri->sip_uri_issip) {
281 if (error != NULL)
282 *error = EINVAL;
283 return (NULL);
285 if ((_uri->sip_uri_errflags & SIP_URIERR_HEADER) != 0 && error != NULL)
286 *error = EINVAL;
287 if (_uri->sip_uri_headers.sip_str_len > 0)
288 return (&_uri->sip_uri_headers);
289 return (NULL);
293 * Return opaque value for an ABS URI
295 const sip_str_t *
296 sip_get_uri_opaque(const struct sip_uri *uri, int *error)
298 _sip_uri_t *_uri;
300 _uri = sip_check_get_param(uri, error);
301 if (_uri == NULL)
302 return (NULL);
304 if (_uri->sip_uri_issip) {
305 if (error != NULL)
306 *error = EINVAL;
307 return (NULL);
309 if ((_uri->sip_uri_errflags & SIP_URIERR_OPAQUE) != 0 && error != NULL)
310 *error = EINVAL;
311 if (_uri->sip_uri_opaque.sip_str_len > 0)
312 return (&_uri->sip_uri_opaque);
313 return (NULL);
317 * Return query from an absolute URI
319 const sip_str_t *
320 sip_get_uri_query(const struct sip_uri *uri, int *error)
322 _sip_uri_t *_uri;
324 _uri = sip_check_get_param(uri, error);
325 if (_uri == NULL)
326 return (NULL);
328 if (_uri->sip_uri_issip) {
329 if (error != NULL)
330 *error = EINVAL;
331 return (NULL);
333 if ((_uri->sip_uri_errflags & SIP_URIERR_QUERY) != 0 && error != NULL)
334 *error = EINVAL;
335 if (_uri->sip_uri_query.sip_str_len > 0)
336 return (&_uri->sip_uri_query);
337 return (NULL);
341 * Get path from an assolute URI
343 const sip_str_t *
344 sip_get_uri_path(const struct sip_uri *uri, int *error)
346 _sip_uri_t *_uri;
348 _uri = sip_check_get_param(uri, error);
349 if (_uri == NULL)
350 return (NULL);
352 if (_uri->sip_uri_issip) {
353 if (error != NULL)
354 *error = EINVAL;
355 return (NULL);
357 if ((_uri->sip_uri_errflags & SIP_URIERR_PATH) != 0 && error != NULL)
358 *error = EINVAL;
359 if (_uri->sip_uri_path.sip_str_len > 0)
360 return (&_uri->sip_uri_path);
361 return (NULL);
365 * Get the reg-name from absolute URI
367 const sip_str_t *
368 sip_get_uri_regname(const struct sip_uri *uri, int *error)
370 _sip_uri_t *_uri;
372 _uri = sip_check_get_param(uri, error);
373 if (_uri == NULL)
374 return (NULL);
376 if (_uri->sip_uri_issip) {
377 if (error != NULL)
378 *error = EINVAL;
379 return (NULL);
381 if ((_uri->sip_uri_errflags & SIP_URIERR_REGNAME) != 0 && error != NULL)
382 *error = EINVAL;
383 if (_uri->sip_uri_regname.sip_str_len > 0)
384 return (&_uri->sip_uri_regname);
385 return (NULL);
389 * Return TRUE if this is a teluser
391 boolean_t
392 sip_is_uri_teluser(const struct sip_uri *uri)
394 _sip_uri_t *_uri;
396 if (uri == NULL)
397 return (B_FALSE);
399 _uri = (_sip_uri_t *)uri;
400 return (_uri->sip_uri_isteluser);
404 sip_get_uri_errflags(const struct sip_uri *uri, int *error)
406 _sip_uri_t *_uri;
408 _uri = sip_check_get_param(uri, error);
409 if (_uri == NULL)
410 return (0);
411 return (_uri->sip_uri_errflags);
415 * the caller is responsible for freeing the returned string
417 char *
418 sip_uri_errflags_to_str(int errflags)
420 char *err_info = NULL;
422 if (errflags == 0)
423 return (NULL);
425 err_info = (char *)malloc(SIP_URI_BUF_SIZE);
426 if (err_info == NULL)
427 return (NULL);
429 if (errflags & SIP_URIERR_NOURI) {
430 (void) strncpy(err_info, "Error : No URI",
431 strlen("Error : No URI"));
432 err_info[strlen("Error : No URI")] = '\0';
433 return (err_info);
436 (void) strncpy(err_info, "Error(s) in", strlen("Error(s) in"));
437 err_info[strlen("Error(s) in")] = '\0';
438 if (errflags & SIP_URIERR_SCHEME)
439 (void) strncat(err_info, " SCHEME,", strlen(" SCHEME,"));
440 if (errflags & SIP_URIERR_USER)
441 (void) strncat(err_info, " USER,", strlen(" USER,"));
442 if (errflags & SIP_URIERR_PASS)
443 (void) strncat(err_info, " PASSWORD,", strlen(" PASSWORD,"));
444 if (errflags & SIP_URIERR_HOST)
445 (void) strncat(err_info, " HOST,", strlen(" HOST,"));
446 if (errflags & SIP_URIERR_PORT)
447 (void) strncat(err_info, " PORT,", strlen(" PORT,"));
448 if (errflags & SIP_URIERR_PARAM) {
449 (void) strncat(err_info, " PARAMETERS,",
450 strlen(" PARAMETERS,"));
452 if (errflags & SIP_URIERR_HEADER)
453 (void) strncat(err_info, " HEADERS,", strlen(" HEADERS,"));
454 if (errflags & SIP_URIERR_OPAQUE)
455 (void) strncat(err_info, " OPAQUE,", strlen(" OPAQUE,"));
456 if (errflags & SIP_URIERR_QUERY)
457 (void) strncat(err_info, " QUERY,", strlen(" QUERY,"));
458 if (errflags & SIP_URIERR_PATH)
459 (void) strncat(err_info, " PATH,", strlen(" PATH,"));
460 if (errflags & SIP_URIERR_REGNAME)
461 (void) strncat(err_info, " REG-NAME,", strlen(" REG-NAME,"));
462 if (strlen(err_info) == strlen("Error(s) in")) {
463 free(err_info);
464 err_info = NULL;
465 } else {
466 err_info[strlen(err_info) - 1] = '\0';
467 (void) strncat(err_info, " part(s)", strlen(" part(s)"));
469 return (err_info);