📄 sip_ui.c
字号:
/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END *//* * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */#pragma ident "@(#)sip_ui.c 1.51 06/08/16 SMI"#include "sip_msg.h"#include "sip_xaction.h"#include "sip_miscdefs.h"#define SIP_BUF_SIZE 128/* * Find the header named header, consecutive calls with old_header * passed in will return next header of the same type. * If no name is passed the first header is returned. consectutive calls * with no name but an old header will return the next header. */const struct sip_header *sip_get_header(sip_msg_t sip_msg, char *header_name, sip_header_t old_header, int *error){ _sip_msg_t *_sip_msg; const struct sip_header *sip_hdr; if (error != NULL) *error = 0; if (sip_msg == NULL) { if (error != NULL) *error = EINVAL; return (NULL); } _sip_msg = (_sip_msg_t *)sip_msg; (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex); sip_hdr = (sip_header_t)sip_search_for_header((_sip_msg_t *)sip_msg, header_name, (_sip_header_t *)old_header); (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); if (sip_hdr == NULL && error != NULL) *error = EINVAL; return (sip_hdr);}/* * Return the request line as a string. Caller releases the returned string. */char *sip_reqline_to_str(sip_msg_t sip_msg, int *error){ char *reqstr; if (error != NULL) *error = 0; if (sip_msg == NULL || !sip_msg_is_request(sip_msg, error)) { if (error != NULL) *error = EINVAL; return (NULL); } reqstr = _sip_startline_to_str((_sip_msg_t *)sip_msg, error); return (reqstr);}/* * Return the response line as a string. Caller releases the returned string. */char *sip_respline_to_str(sip_msg_t sip_msg, int *error){ char *respstr; if (error != NULL) *error = 0; if (sip_msg == NULL || sip_msg_is_request(sip_msg, error)) { if (error != NULL) *error = EINVAL; return (NULL); } respstr = _sip_startline_to_str((_sip_msg_t *)sip_msg, error); return (respstr);}/* * return the first value of the header */const struct sip_value *sip_get_header_value(const struct sip_header *sip_header, int *error){ _sip_header_t *_sip_header; sip_parsed_header_t *sip_parsed_header; int ret = 0; const struct sip_value *value; if (error != NULL) *error = 0; if (sip_header == NULL) { if (error != NULL) *error = EINVAL; return (NULL); } _sip_header = (_sip_header_t *)sip_header; if (_sip_header->sip_hdr_sipmsg != NULL) { (void) pthread_mutex_lock( &_sip_header->sip_hdr_sipmsg->sip_msg_mutex); } if (_sip_header->sip_header_state == SIP_HEADER_DELETED) { if (_sip_header->sip_hdr_sipmsg != NULL) { (void) pthread_mutex_unlock( &_sip_header->sip_hdr_sipmsg->sip_msg_mutex); } if (error != NULL) *error = EINVAL; return (NULL); } ret = _sip_header->sip_header_functions->header_parse_func( _sip_header, &sip_parsed_header); if (_sip_header->sip_hdr_sipmsg != NULL) { (void) pthread_mutex_unlock (&_sip_header->sip_hdr_sipmsg->sip_msg_mutex); } if (error != NULL) *error = ret; if (ret != 0) return (NULL); value = (sip_header_value_t)sip_parsed_header->value; while (value != NULL && value->value_state == SIP_VALUE_DELETED) value = value->next; if (value != NULL && value->value_state == SIP_VALUE_BAD && error != NULL) { *error = EPROTO; } return ((sip_header_value_t)value);}/* * Return the next value of the header. */const struct sip_value *sip_get_next_value(sip_header_value_t old_value, int *error){ const struct sip_value *value; if (error != NULL) *error = 0; if (old_value == NULL || old_value->next == NULL) { if (error != NULL) *error = EINVAL; return (NULL); } /* * We never free the deleted values so no need to hold a lock. */ value = (sip_header_value_t)old_value->next; while (value != NULL && value->value_state == SIP_VALUE_DELETED) value = value->next; if (value != NULL && value->value_state == SIP_VALUE_BAD && error != NULL) { *error = EPROTO; } return ((sip_header_value_t)value);}/* * Given a SIP message, delete the header "header_name". */intsip_delete_header_by_name(sip_msg_t msg, char *header_name){ _sip_msg_t *_msg = (_sip_msg_t *)msg; sip_header_t sip_hdr; _sip_header_t *_sip_hdr; if (_msg == NULL || header_name == NULL) return (EINVAL); (void) pthread_mutex_lock(&_msg->sip_msg_mutex); if (!sip_ok_to_modify_message(_msg)) { (void) pthread_mutex_unlock(&_msg->sip_msg_mutex); return (EPERM); } sip_hdr = (sip_header_t)sip_search_for_header(_msg, header_name, NULL); if (sip_hdr == NULL) { (void) pthread_mutex_unlock(&_msg->sip_msg_mutex); return (EINVAL); } /* * Avoid duplication by calling sip_delete_header(), at the expense * of some redundant work. */ _sip_hdr = (_sip_header_t *)sip_hdr; _sip_hdr->sip_header_state = SIP_HEADER_DELETED; _sip_hdr->sip_hdr_sipmsg->sip_msg_len -= _sip_hdr->sip_hdr_end - _sip_hdr->sip_hdr_start; assert(_sip_hdr->sip_hdr_sipmsg->sip_msg_len >= 0); (void) pthread_mutex_unlock(&_msg->sip_msg_mutex); return (0);}/* * Mark the header as deleted. */intsip_delete_header(sip_header_t sip_header){ _sip_header_t *_sip_header; if (sip_header == NULL) return (EINVAL); _sip_header = (_sip_header_t *)sip_header; (void) pthread_mutex_lock(&_sip_header->sip_hdr_sipmsg->sip_msg_mutex); if (!sip_ok_to_modify_message(_sip_header->sip_hdr_sipmsg)) { (void) pthread_mutex_unlock (&_sip_header->sip_hdr_sipmsg->sip_msg_mutex); return (EPERM); } if (_sip_header->sip_header_state == SIP_HEADER_DELETED) { (void) pthread_mutex_unlock( &_sip_header->sip_hdr_sipmsg->sip_msg_mutex); return (EINVAL); } _sip_header->sip_header_state = SIP_HEADER_DELETED; _sip_header->sip_hdr_sipmsg->sip_msg_len -= _sip_header->sip_hdr_end - _sip_header->sip_hdr_start; assert(_sip_header->sip_hdr_sipmsg->sip_msg_len >= 0); (void) pthread_mutex_unlock (&_sip_header->sip_hdr_sipmsg->sip_msg_mutex); return (0);}/* * Mark the value as deleted. */intsip_delete_value(sip_header_t sip_header, sip_header_value_t sip_header_value){ _sip_header_t *_sip_header; sip_value_t *_sip_header_value; if (sip_header == NULL || sip_header_value == NULL) return (EINVAL); _sip_header = (_sip_header_t *)sip_header; (void) pthread_mutex_lock(&_sip_header->sip_hdr_sipmsg->sip_msg_mutex); if (!sip_ok_to_modify_message(_sip_header->sip_hdr_sipmsg)) { (void) pthread_mutex_unlock(&_sip_header-> sip_hdr_sipmsg->sip_msg_mutex); return (EPERM); } if (_sip_header->sip_header_state == SIP_HEADER_DELETED) { (void) pthread_mutex_unlock( &_sip_header->sip_hdr_sipmsg->sip_msg_mutex); return (EINVAL); } _sip_header_value = (sip_value_t *)sip_header_value; if (_sip_header_value->value_state == SIP_VALUE_DELETED) { (void) pthread_mutex_unlock( &_sip_header->sip_hdr_sipmsg->sip_msg_mutex); return (EINVAL); } _sip_header->sip_header_state = SIP_HEADER_DELETED_VAL; _sip_header_value->value_state = SIP_VALUE_DELETED; (void) pthread_mutex_unlock (&_sip_header->sip_hdr_sipmsg->sip_msg_mutex); return (0);}/* * Given a param list, check if a param name exists. */boolean_tsip_is_param_present(const sip_param_t *param_list, char *param_name, int param_len){ const sip_param_t *param = param_list; while (param != NULL) { if (param->param_name.sip_str_len == param_len && strncasecmp(param->param_name.sip_str_ptr, param_name, param_len) == 0) { return (B_TRUE); } param = param->param_next; } return (B_FALSE);}/* * Given a value header return the value of the named param. */const sip_str_t *sip_get_param_value(sip_header_value_t header_value, char *param_name, int *error){ sip_value_t *_sip_header_value; sip_param_t *sip_param; if (error != NULL) *error = 0; if (header_value == NULL || param_name == NULL) { if (error != NULL) *error = EINVAL; return (NULL); } _sip_header_value = (sip_value_t *)header_value; if (_sip_header_value->value_state == SIP_VALUE_DELETED) { if (error != NULL) *error = EINVAL; return (NULL); } if (_sip_header_value->param_list == NULL) { if (error != NULL) *error = EINVAL; return (NULL); } sip_param = sip_get_param_from_list(_sip_header_value->param_list, param_name); if (sip_param != NULL) return (&sip_param->param_value); return (NULL);}/* * Return the list of params in the header */const sip_param_t *sip_get_params(sip_header_value_t header_value, int *error){ sip_value_t *sip_header_value; if (error != NULL) *error = 0; if (header_value == NULL) { if (error != NULL) *error = EINVAL; return (NULL); } sip_header_value = (sip_value_t *)header_value; if (sip_header_value->value_state == SIP_VALUE_DELETED) { if (error != NULL) *error = EINVAL; return (NULL); } return (sip_header_value->param_list);}/* Return true if this is a SIP request */boolean_tsip_msg_is_request(sip_msg_t sip_msg, int *error){ _sip_msg_t *_sip_msg; sip_message_type_t *sip_msg_info; boolean_t ret; if (error != NULL) *error = 0; if (sip_msg == NULL) { if (error != NULL) *error = EINVAL; return (B_FALSE); } _sip_msg = (_sip_msg_t *)sip_msg; (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex); if (_sip_msg->sip_msg_req_res == NULL) { (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); if (error != NULL) *error = EINVAL; return (B_FALSE); } sip_msg_info = _sip_msg->sip_msg_req_res; ret = sip_msg_info->is_request; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (ret);}/* Return true if this is a SIP response */boolean_tsip_msg_is_response(sip_msg_t sip_msg, int *error){ boolean_t is_resp; _sip_msg_t *_sip_msg; sip_message_type_t *sip_msg_info; if (error != NULL) *error = 0; if (sip_msg == NULL) { if (error != NULL) *error = EINVAL; return (B_FALSE); } _sip_msg = (_sip_msg_t *)sip_msg; (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex); if (_sip_msg->sip_msg_req_res == NULL) { (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); if (error != NULL) *error = EINVAL; return (B_FALSE); } sip_msg_info = _sip_msg->sip_msg_req_res; is_resp = !sip_msg_info->is_request; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (is_resp);}/* Return the method in the request line */sip_method_tsip_get_request_method(sip_msg_t sip_msg, int *error){ _sip_msg_t *_sip_msg; sip_message_type_t *sip_msg_info; sip_method_t ret = -1; if (error != NULL) *error = 0; if (sip_msg == NULL) { if (error != NULL) *error = EINVAL; return (ret); } _sip_msg = (_sip_msg_t *)sip_msg; (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex); sip_msg_info = _sip_msg->sip_msg_req_res; if (_sip_msg->sip_msg_req_res == NULL) { (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); if (error != NULL) *error = EINVAL; return (ret); } if (sip_msg_info->is_request) ret = sip_msg_info->sip_req_method; else if (error != NULL) *error = EINVAL; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (ret);}/* Return the URI from the request line */const sip_str_t *sip_get_request_uri_str(sip_msg_t sip_msg, int *error){ _sip_msg_t *_sip_msg; sip_message_type_t *sip_msg_info; sip_str_t *ret = NULL; struct sip_uri *parsed_uri; if (error != NULL) *error = 0; if (sip_msg == NULL) { if (error != NULL) *error = EINVAL; return (NULL); } _sip_msg = (_sip_msg_t *)sip_msg; (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex); if (_sip_msg->sip_msg_req_res == NULL) { (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); if (error != NULL) *error = EINVAL; return (NULL); } sip_msg_info = _sip_msg->sip_msg_req_res; if (sip_msg_info->is_request) ret = &sip_msg_info->sip_req_uri; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); /* * If the error is required, check the validity of the URI via * sip_uri_parse(). */ if (error != NULL) { parsed_uri = sip_parse_uri(ret, error); if (parsed_uri != NULL) free(parsed_uri); } return (ret);}/* Return the response code */intsip_get_response_code(sip_msg_t sip_msg, int *error){ _sip_msg_t *_sip_msg; sip_message_type_t *sip_msg_info; int ret = -1; if (error != NULL) *error = 0; if (sip_msg == NULL) { if (error != NULL) *error = EINVAL; return (ret); } _sip_msg = (_sip_msg_t *)sip_msg; (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex); if (_sip_msg->sip_msg_req_res == NULL) { (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); if (error != NULL) *error = EINVAL; return (ret); } sip_msg_info = _sip_msg->sip_msg_req_res; if (!sip_msg_info->is_request) ret = sip_msg_info->sip_resp_code; else if (error != NULL) *error = EINVAL; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (ret);}/* Get the response phrase */const sip_str_t *sip_get_response_phrase(sip_msg_t sip_msg, int *error){ _sip_msg_t *_sip_msg; sip_message_type_t *sip_msg_info; sip_str_t *ret = NULL; if (error != NULL) *error = 0; if (sip_msg == NULL) { if (error != NULL) *error = EINVAL; return (ret); } _sip_msg = (_sip_msg_t *)sip_msg; (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex); if (_sip_msg->sip_msg_req_res == NULL) { (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); if (error != NULL) *error = EINVAL; return (ret); } sip_msg_info = _sip_msg->sip_msg_req_res; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); if (!sip_msg_info->is_request) { if (sip_msg_info->sip_resp_phrase_len == 0) ret = NULL; else ret = &sip_msg_info->sip_resp_phrase; } else if (error != NULL) { *error = EINVAL; } return (ret);}/* Get the SIP version string */const sip_str_t *sip_get_sip_version(sip_msg_t sip_msg, int *error){ _sip_msg_t *_sip_msg; sip_message_type_t *sip_msg_info; sip_str_t *ret = NULL; if (error != NULL) *error = 0; if (sip_msg == NULL) { if (error != NULL) *error = EINVAL; return (ret); } _sip_msg = (_sip_msg_t *)sip_msg; (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex); if (_sip_msg->sip_msg_req_res == NULL) { (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); if (error != NULL) *error = EINVAL; return (ret); } sip_msg_info = _sip_msg->sip_msg_req_res; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); ret = &sip_msg_info->sip_proto_version.version; return (ret);}/* Return the length of the SIP message */intsip_get_msg_len(sip_msg_t sip_msg, int *error){ _sip_msg_t *_sip_msg; if (error != NULL) *error = 0; if (sip_msg == NULL) { if (error != NULL) *error = EINVAL; return (-1); } _sip_msg = (_sip_msg_t *)sip_msg; return (_sip_msg->sip_msg_len);}/* Get the URI from the value */const sip_str_t *sip_get_cftruri_from_val(sip_header_value_t value, int *error){ sip_hdr_value_t *cftrvalue; if (error != NULL) *error = 0; if (value == NULL || value->value_state == SIP_VALUE_DELETED) { if (error != NULL) *error = EINVAL; return (NULL); } cftrvalue = (sip_hdr_value_t *)value;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -