📄 sip_ui.c
字号:
{ int *c; if (error != NULL) *error = 0; if (value == NULL || value->value_state == SIP_VALUE_DELETED) { if (error != NULL) *error = EINVAL; return (-1); } c = (int *)sip_get_warninfo(value, W_CODE, error); if (c == NULL) return (-1); return (*c);}/* get warning agent */const sip_str_t *sip_get_warning_agent(sip_header_value_t value, int *error){ sip_str_t *r; if (value == NULL || value->value_state == SIP_VALUE_DELETED) { if (error != NULL) *error = EINVAL; return (NULL); } r = (sip_str_t *)sip_get_warninfo(value, W_AGENT, error); return (r);}/* get warning text */const sip_str_t *sip_get_warning_text(sip_header_value_t value, int *error){ sip_str_t *r; if (value == NULL || value->value_state == SIP_VALUE_DELETED) { if (error != NULL) *error = EINVAL; return (NULL); } r = (sip_str_t *)sip_get_warninfo(value, W_TEXT, error); return (r);}/* get authorization scheme */const sip_str_t *sip_get_author_scheme(sip_msg_t sip_msg, int *error){ sip_str_t *r; r = sip_get_val_from_msg(sip_msg, SIP_AUTHOR, SIP_AUTH_VAL, B_FALSE, B_FALSE, error); return (r);}/* get authentication parameter */static const sip_str_t *sip_get_auth_param(sip_msg_t msg, char *hdr_name, char *pname, int *error){ const _sip_header_t *header; sip_hdr_value_t *value; sip_param_t *param; if (error != NULL) *error = 0; if (msg == NULL || pname == NULL || hdr_name == NULL) { if (error != NULL) *error = EINVAL; return (NULL); } header = sip_get_header(msg, hdr_name, NULL, error); if (header == NULL) { if (error != NULL) *error = EINVAL; return (NULL); } value = (sip_hdr_value_t *)sip_get_header_value(header, error); if (value == NULL) { if (error != NULL) *error = EPROTO; return (NULL); } param = sip_get_param_from_list(value->auth_param, pname); if (param != NULL) return (¶m->param_value); return (NULL);}/* get authentication parameter */const sip_str_t *sip_get_author_param(sip_msg_t sip_msg, char *name, int *error){ const sip_str_t *r; r = sip_get_auth_param(sip_msg, SIP_AUTHOR, name, error); return (r);}/* get authentication info */const sip_str_t *sip_get_authen_info(sip_header_value_t value, int *error){ sip_str_t *r; sip_hdr_value_t *val = (sip_hdr_value_t *)value; if (error != NULL) *error = 0; if (value == NULL || value->value_state == SIP_VALUE_DELETED) { if (error != NULL) *error = EINVAL; return (NULL); } r = sip_get_val_from_hdr(val, SIP_STR_VAL, B_FALSE, error); return (r);}/* get proxy-authentication scheme */const sip_str_t *sip_get_proxy_authen_scheme(sip_msg_t msg, int *error){ sip_str_t *r; r = sip_get_val_from_msg(msg, SIP_PROXY_AUTHEN, SIP_AUTH_VAL, B_FALSE, B_FALSE, error); return (r);}/* get proxy authentication parameter */const sip_str_t *sip_get_proxy_authen_param(sip_msg_t sip_msg, char *name, int *error){ const sip_str_t *r; r = sip_get_auth_param(sip_msg, SIP_PROXY_AUTHEN, name, error); return (r);}/* get proxy-authorization scheme */const sip_str_t *sip_get_proxy_author_scheme(sip_msg_t msg, int *error){ sip_str_t *r; r = sip_get_val_from_msg(msg, SIP_PROXY_AUTHOR, SIP_AUTH_VAL, B_FALSE, B_FALSE, error); return (r);}/* get proxy-authorization parameter */const sip_str_t *sip_get_proxy_author_param(sip_msg_t sip_msg, char *name, int *error){ const sip_str_t *r; r = sip_get_auth_param(sip_msg, SIP_PROXY_AUTHOR, name, error); return (r);}/* get proxy-require */const sip_str_t *sip_get_proxy_require(sip_header_value_t value, int *error){ sip_str_t *r; sip_hdr_value_t *val = (sip_hdr_value_t *)value; if (error != NULL) *error = 0; if (value == NULL || value->value_state == SIP_VALUE_DELETED) { if (error != NULL) *error = EINVAL; return (NULL); } r = sip_get_val_from_hdr(val, SIP_STR_VAL, B_FALSE, error); return (r);}/* get www-authentication scheme */const sip_str_t *sip_get_www_authen_scheme(sip_msg_t msg, int *error){ sip_str_t *r; r = sip_get_val_from_msg(msg, SIP_WWW_AUTHEN, SIP_AUTH_VAL, B_FALSE, B_FALSE, error); return (r);}/* get www-authentication parameter */const sip_str_t *sip_get_www_authen_param(sip_msg_t sip_msg, char *name, int *error){ const sip_str_t *r; r = sip_get_auth_param(sip_msg, SIP_WWW_AUTHEN, name, error); return (r);}/* copy sip_header with param, if any, to sip_msg */intsip_copy_header(sip_msg_t sip_msg, sip_header_t sip_header, char *param){ _sip_msg_t *_sip_msg; _sip_header_t *_sip_header; int ret; if (sip_msg == NULL || sip_header == NULL) return (EINVAL); _sip_msg = (_sip_msg_t *)sip_msg; _sip_header = (_sip_header_t *)sip_header; (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex); if (!sip_ok_to_modify_message(_sip_msg)) { (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (EPERM); } if (_sip_header->sip_header_state == SIP_HEADER_DELETED) { (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (EINVAL); } ret = _sip_copy_header(_sip_msg, _sip_header, param, B_TRUE); (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (ret);}/* copy the header specified by header_name, with param, if any */intsip_copy_header_by_name(sip_msg_t old_msg, sip_msg_t new_msg, char *header_name, char *param){ int ret; _sip_msg_t *_old_msg = (_sip_msg_t *)old_msg; _sip_msg_t *_new_msg = (_sip_msg_t *)new_msg; if (_old_msg == NULL || _new_msg == NULL || header_name == NULL) return (EINVAL); (void) pthread_mutex_lock(&_new_msg->sip_msg_mutex); if (!sip_ok_to_modify_message(_new_msg)) { (void) pthread_mutex_unlock(&_new_msg->sip_msg_mutex); return (EPERM); } (void) pthread_mutex_unlock(&_new_msg->sip_msg_mutex); (void) pthread_mutex_lock(&_old_msg->sip_msg_mutex); ret = _sip_find_and_copy_header(_old_msg, _new_msg, header_name, param); (void) pthread_mutex_unlock(&_old_msg->sip_msg_mutex); return (ret);}/* add the given header to sip_message */intsip_add_header(sip_msg_t sip_msg, char *header_string){ int header_size; _sip_header_t *new_header; _sip_msg_t *_sip_msg; if (sip_msg == NULL || header_string == NULL) return (EINVAL); _sip_msg = (_sip_msg_t *)sip_msg; (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex); if (!sip_ok_to_modify_message(_sip_msg)) { (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (EPERM); } header_size = strlen(header_string) + strlen(SIP_CRLF); new_header = sip_new_header(header_size); if (new_header == NULL) { (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (ENOMEM); } (void) snprintf(new_header->sip_hdr_start, header_size + 1, "%s%s", header_string, SIP_CRLF); _sip_add_header(_sip_msg, new_header, B_TRUE, B_FALSE, NULL); (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (0);}/* * add the given param to the sip_header. create a new header with the param * and mark the old header as deleted. */sip_header_tsip_add_param(sip_header_t sip_header, char *param, int *error){ _sip_header_t *_sip_header; _sip_header_t *new_header; int hdrlen; _sip_msg_t *_sip_msg; int param_len; char *tmp_ptr; if (error != NULL) *error = 0; if (param == NULL || sip_header == NULL) { if (error != NULL) *error = EINVAL; return (NULL); } _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)) { if (error != NULL) *error = EPERM; (void) pthread_mutex_unlock( &_sip_header->sip_hdr_sipmsg->sip_msg_mutex); return (NULL); } if (_sip_header->sip_header_state == SIP_HEADER_DELETED) { if (error != NULL) *error = EINVAL; (void) pthread_mutex_unlock( &_sip_header->sip_hdr_sipmsg->sip_msg_mutex); return (NULL); } param_len = SIP_SPACE + sizeof (char) + SIP_SPACE + strlen(param); hdrlen = _sip_header->sip_hdr_end - _sip_header->sip_hdr_start; new_header = sip_new_header(hdrlen + param_len); if (new_header == NULL) { if (error != NULL) *error = ENOMEM; (void) pthread_mutex_unlock( &_sip_header->sip_hdr_sipmsg->sip_msg_mutex); return (NULL); } (void) memcpy(new_header->sip_hdr_start, _sip_header->sip_hdr_start, hdrlen); new_header->sip_hdr_end = new_header->sip_hdr_start + hdrlen; hdrlen = param_len + 1; /* Find CRLF */ tmp_ptr = new_header->sip_hdr_end; while (*tmp_ptr-- != '\n') { hdrlen++; if (tmp_ptr == new_header->sip_hdr_start) { sip_free_header(new_header); if (error != NULL) *error = EINVAL; (void) pthread_mutex_unlock( &_sip_header->sip_hdr_sipmsg->sip_msg_mutex); return (NULL); } } (void) snprintf(tmp_ptr, hdrlen + 1, " %c %s%s", SIP_SEMI, param, SIP_CRLF); new_header->sip_hdr_end += param_len; new_header->sip_header_functions = _sip_header->sip_header_functions; _sip_msg = _sip_header->sip_hdr_sipmsg; _sip_add_header(_sip_msg, new_header, B_TRUE, B_FALSE, NULL); (void) pthread_mutex_unlock(&new_header->sip_hdr_sipmsg->sip_msg_mutex); (void) sip_delete_header(sip_header); return ((sip_header_t)new_header);}/* get the branch id from the topmost VIA header */char *sip_get_branchid(sip_msg_t sip_msg, int *error){ _sip_header_t *header; sip_parsed_header_t *parsed_header; sip_hdr_value_t *via_value; const sip_str_t *param_value; char *bid; _sip_msg_t *_sip_msg; 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); header = sip_search_for_header(_sip_msg, SIP_VIA, NULL); if (header == NULL) { if (error != NULL) *error = EINVAL; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (NULL); } if (sip_parse_via_header(header, &parsed_header) != 0) { if (error != NULL) *error = EPROTO; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (NULL); } if (parsed_header == NULL) { if (error != NULL) *error = EPROTO; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (NULL); } via_value = (sip_hdr_value_t *)parsed_header->value; if (via_value == NULL || via_value->sip_value_state == SIP_VALUE_BAD) { if (error != NULL) *error = EPROTO; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (NULL); } param_value = sip_get_param_value((sip_header_value_t)via_value, "branch", error); if (param_value == NULL) { if (error != NULL) *error = EINVAL; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (NULL); } bid = (char *)malloc(param_value->sip_str_len + 1); if (bid == NULL) { if (error != NULL) *error = ENOMEM; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (NULL); } (void) strncpy(bid, param_value->sip_str_ptr, param_value->sip_str_len); bid[param_value->sip_str_len] = '\0'; (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (bid);}/* * adds branchid to the topmost VIA header, if a branchid already exists, * returns error. */intsip_add_branchid_to_via(sip_msg_t sip_msg, char *branchid){ int err = 0; char *param; int plen; sip_header_t via_hdr; _sip_msg_t *_sip_msg; if (sip_msg == NULL) return (EINVAL); /* If there is already a branchid param, error? */ if (sip_get_branchid(sip_msg, NULL) != NULL) return (EINVAL); _sip_msg = (_sip_msg_t *)sip_msg; (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex); via_hdr = (sip_header_t)sip_search_for_header(_sip_msg, SIP_VIA, NULL); (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); if (via_hdr == NULL) return (EINVAL); plen = strlen(branchid) + strlen("branch=") + 1; param = malloc(plen); if (param == NULL) return (ENOMEM); (void) snprintf(param, plen, "branch=%s", branchid); (void) sip_add_param(via_hdr, param, &err); free(param); return (err);}/* returns the number of VIA headers in the SIP message */intsip_get_num_via(sip_msg_t sip_msg, int *error){ _sip_msg_t *_sip_msg; sip_header_t hdr; int via_cnt = 0; if (error != NULL) *error = 0; if (sip_msg == NULL) { if (error != NULL) *error = EINVAL; return (via_cnt); } _sip_msg = (_sip_msg_t *)sip_msg; (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex); hdr = (sip_header_t)sip_search_for_header(_sip_msg, SIP_VIA, NULL); while (hdr != NULL) { via_cnt++; hdr = (sip_header_t)sip_search_for_header(_sip_msg, SIP_VIA, hdr); } (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); return (via_cnt);}/* Get Request URI */const struct sip_uri *sip_get_request_uri(sip_msg_t sip_msg, int *error){ _sip_msg_t *_sip_msg; sip_message_type_t *sip_msg_info; const struct sip_uri *ret = NULL; 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_msg_info = _sip_msg->sip_msg_req_res; if (sip_msg_info != NULL && sip_msg_info->is_request) { ret = sip_msg_info->sip_req_parse_uri; } else { if (error != NULL) *error = EINVAL; } (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex); if (ret != NULL) { if (ret->sip_uri_scheme.sip_str_len == 0 || ret->sip_uri_scheme.sip_str_ptr == NULL) { ret = NULL; if (error != NULL) *error = EINVAL; } else if (ret->sip_uri_errflags != 0 && error != NULL) { *error = EINVAL; } } return ((sip_uri_t)ret);}/* * The following two fns initialize and destroy the private library * data in sip_conn_object_t. The assumption is that the 1st member * of sip_conn_object_t is reserved for library use. The private data * is used only for byte-stream protocols such as TCP to accumulate * a complete SIP message, based on the CONTENT-LENGTH value, before * processing it. */intsip_init_conn_object(sip_conn_object_t obj){ void **obj_val; sip_conn_obj_pvt_t *pvt_data; if (obj == NULL) return (EINVAL); pvt_data = malloc(sizeof (sip_conn_obj_pvt_t)); if (pvt_data == NULL) return (ENOMEM); pvt_data->sip_conn_obj_cache = NULL; pvt_data->sip_conn_obj_reass = malloc(sizeof (sip_reass_entry_t)); if (pvt_data->sip_conn_obj_reass == NULL) { free(pvt_data); return (ENOMEM); } bzero(pvt_data->sip_conn_obj_reass, sizeof (sip_reass_entry_t)); (void) pthread_mutex_init(&pvt_data->sip_conn_obj_reass_lock, NULL); (void) pthread_mutex_init(&pvt_data->sip_conn_obj_cache_lock, NULL); sip_refhold_conn(obj); obj_val = (void *)obj; *obj_val = (void *)pvt_data; return (0);}/* Clear private date, if any */voidsip_clear_stale_data(sip_conn_object_t obj){ void **obj_val; sip_conn_obj_pvt_t *pvt_data; sip_reass_entry_t *reass; if (obj == NULL) return; obj_val = (void *)obj; pvt_data = (sip_conn_obj_pvt_t *)*obj_val; (void) pthread_mutex_lock(&pvt_data->sip_conn_obj_reass_lock); reass = pvt_data->sip_conn_obj_reass; if (reass->sip_reass_msg != NULL) { assert(reass->sip_reass_msglen > 0); free(reass->sip_reass_msg); reass->sip_reass_msglen = 0; } assert(reass->sip_reass_msglen == 0); (void) pthread_mutex_unlock(&pvt_data->sip_conn_obj_reass_lock);}/* * Walk through all the transactions, remove if this obj has been cached * by any. */voidsip_conn_destroyed(sip_conn_object_t obj){ void **obj_val; sip_conn_obj_pvt_t *pvt_data; if (obj == NULL) return; obj_val = (void *)obj; pvt_data = (sip_conn_obj_pvt_t *)*obj_val; sip_clear_stale_data(obj); free(pvt_data->sip_conn_obj_reass); pvt_data->sip_conn_obj_reass = NULL; (void) pthread_mutex_destroy(&pvt_data->sip_conn_obj_reass_lock); sip_del_conn_obj_cache(obj, NULL); (void) pthread_mutex_destroy(&pvt_data->sip_conn_obj_cache_lock); free(pvt_data); *obj_val = NULL; sip_refrele_conn(obj);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -