⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sip_msg.c

📁 VoIP use SIP protocol interface
💻 C
📖 第 1 页 / 共 5 页
字号:
 */intsip_add_author(sip_msg_t sip_msg, char *scheme, char *param){	return (sip_add_str_to_msg(sip_msg, SIP_AUTHOR, scheme, param, SIP_SP));}/* * Authentication-Info  =  "Authentication-Info" HCOLON ainfo *				*(COMMA ainfo) * ainfo                =  nextnonce / message-qop *				/ response-auth / cnonce *				/ nonce-count * nextnonce            =  "nextnonce" EQUAL nonce-value * response-auth        =  "rspauth" EQUAL response-digest * response-digest      =  LDQUOT *LHEX RDQUOT * */intsip_add_authen_info(sip_msg_t sip_msg, char *ainfo){	return (sip_add_str_to_msg(sip_msg, SIP_AUTHEN_INFO, ainfo, NULL,	    (char)NULL));}/* * Proxy-Authenticate  =  "Proxy-Authenticate" HCOLON challenge * challenge           =  ("Digest" LWS digest-cln *(COMMA digest-cln)) *				/ other-challenge * other-challenge     =  auth-scheme LWS auth-param * 				*(COMMA auth-param) * digest-cln          =  realm / domain / nonce *				/ opaque / stale / algorithm *				/ qop-options / auth-param * realm               =  "realm" EQUAL realm-value * realm-value         =  quoted-string * domain              =  "domain" EQUAL LDQUOT URI *				*( 1*SP URI ) RDQUOT * URI                 =  absoluteURI / abs-path * nonce               =  "nonce" EQUAL nonce-value * nonce-value         =  quoted-string * opaque              =  "opaque" EQUAL quoted-string * stale               =  "stale" EQUAL ( "true" / "false" ) * algorithm           =  "algorithm" EQUAL ( "MD5" / "MD5-sess" *			/ token ) * qop-options         =  "qop" EQUAL LDQUOT qop-value *			*("," qop-value) RDQUOT * qop-value           =  "auth" / "auth-int" / token * */intsip_add_proxy_authen(sip_msg_t sip_msg, char *pascheme, char *paparam){	return (sip_add_str_to_msg(sip_msg, SIP_PROXY_AUTHEN, pascheme, paparam,	    SIP_SP));}/* * Proxy-Authorization  =  "Proxy-Authorization" HCOLON credentials * */intsip_add_proxy_author(sip_msg_t sip_msg, char *paschem, char *paparam){	return (sip_add_str_to_msg(sip_msg, SIP_PROXY_AUTHOR, paschem, paparam,	    SIP_SP));}/* * Proxy-Require  =  "Proxy-Require" HCOLON option-tag *			*(COMMA option-tag) * option-tag     =  token * */intsip_add_proxy_require(sip_msg_t sip_msg, char *opt){	return (sip_add_str_to_msg(sip_msg, SIP_PROXY_REQ, opt, NULL,	    (char)NULL));}/* * WWW-Authenticate  =  "WWW-Authenticate" HCOLON challenge * extension-header  =  header-name HCOLON header-value * header-name       =  token * header-value      =  *(TEXT-UTF8char / UTF8-CONT / LWS) * message-body  =  *OCTET * */intsip_add_www_authen(sip_msg_t sip_msg, char *wascheme, char *waparam){	return (sip_add_str_to_msg(sip_msg, SIP_WWW_AUTHEN, wascheme, waparam,	    SIP_SP));}/* * Call-ID  =  ( "Call-ID" / "i" ) HCOLON callid * */intsip_add_callid(sip_msg_t sip_msg, char *callid){	int		ret;	boolean_t	allocd = B_FALSE;	if (sip_msg == NULL || (callid != NULL && callid[0] == '\0'))		return (EINVAL);	if (callid == NULL) {		callid = (char *)sip_guid();		if (callid == NULL)			return (ENOMEM);		allocd = B_TRUE;	}	ret = sip_add_str_to_msg(sip_msg, SIP_CALL_ID, callid, NULL,	    (char)NULL);	if (allocd)		free(callid);	return (ret);}/* * CSeq  =  "CSeq" HCOLON 1*DIGIT LWS Method * */intsip_add_cseq(sip_msg_t sip_msg, sip_method_t method, uint32_t cseq){	int	r;	if (sip_msg == NULL || (int)cseq < 0 || method == 0 ||	    method >= MAX_SIP_METHODS) {		return (EINVAL);	}	r = sip_add_intstr_to_msg(sip_msg, SIP_CSEQ, cseq,	    sip_methods[method].name, NULL);	return (r);}/* * Via =  ( "Via" / "v" ) HCOLON via-parm *(COMMA via-parm) * via-parm          =  sent-protocol LWS sent-by *( SEMI via-params ) * via-params        =  via-ttl / via-maddr *                      / via-received / via-branch *                      / via-extension * via-ttl           =  "ttl" EQUAL ttl * via-maddr         =  "maddr" EQUAL host * via-received      =  "received" EQUAL (IPv4address / IPv6address) * via-branch        =  "branch" EQUAL token * via-extension     =  generic-param * sent-protocol     =  protocol-name SLASH protocol-version *                      SLASH transport * protocol-name     =  "SIP" / token * protocol-version  =  token * transport         =  "UDP" / "TCP" / "TLS" / "SCTP" *                      / other-transport * sent-by           =  host [ COLON port ] * ttl               =  1*3DIGIT ; 0 to 255 * */_sip_header_t *sip_create_via_hdr(char *sent_protocol_transport, char *sent_by_host,    int sent_by_port, char *via_params){	_sip_header_t	*new_header;	int		header_size;	int		count;	header_size = strlen(SIP_VIA) + SIP_SPACE + sizeof (char) + SIP_SPACE +	    strlen(SIP_VERSION) + sizeof (char) +	    strlen(sent_protocol_transport) + SIP_SPACE + strlen(sent_by_host) +	    strlen(SIP_CRLF);	if (sent_by_port > 0) {		header_size += SIP_SPACE + sizeof (char) + SIP_SPACE +		    sip_num_of_bytes(sent_by_port);	}	if (via_params != NULL)		header_size += SIP_SPACE + sizeof (char) + strlen(via_params);	new_header = sip_new_header(header_size);	if (new_header->sip_hdr_start == NULL)		return (NULL);	count = snprintf(new_header->sip_hdr_current, header_size + 1,	    "%s %c %s/%s %s",	    SIP_VIA, SIP_HCOLON, SIP_VERSION, sent_protocol_transport,	    sent_by_host);	new_header->sip_hdr_current += count;	header_size -= count;	if (sent_by_port > 0) {		count = snprintf(new_header->sip_hdr_current, header_size + 1,		    " %c %d", SIP_HCOLON, sent_by_port);		new_header->sip_hdr_current += count;		header_size -= count;	}	if (via_params != NULL) {		count = snprintf(new_header->sip_hdr_current, header_size + 1,		    " %c%s", SIP_SEMI, via_params);		new_header->sip_hdr_current += count;		header_size -= count;	}	(void) snprintf(new_header->sip_hdr_current, header_size + 1,	    "%s", SIP_CRLF);	return (new_header);}/* * There can be multiple via headers we always append the header. * We expect the via params to be a semi-colon separated list of parameters. * We will add a semi-clone, before adding the list to the header. */intsip_add_via(sip_msg_t sip_msg, char *sent_protocol_transport,    char *sent_by_host, int sent_by_port, char *via_params){	_sip_header_t	*new_header;	_sip_msg_t	*_sip_msg;	if (sip_msg == NULL || sent_protocol_transport == NULL ||	    sent_by_host == NULL || sent_by_port < 0) {		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 (ENOTSUP);	}	new_header = sip_create_via_hdr(sent_protocol_transport, sent_by_host,	    sent_by_port, via_params);	if (new_header == NULL) {		(void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);		return (ENOMEM);	}	_sip_add_header(_sip_msg, new_header, B_TRUE, B_FALSE, NULL);	(void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);	return (0);}/* * Max-Forwards  =  "Max-Forwards" HCOLON 1*DIGIT * */intsip_add_maxforward(sip_msg_t sip_msg, uint_t maxforward){	if (sip_msg == NULL || (int)maxforward < 0)		return (EINVAL);	return (sip_add_int_to_msg(sip_msg, SIP_MAX_FORWARDS, maxforward,	    NULL));}/* Add content (message body) to sip_msg */intsip_add_content(sip_msg_t sip_msg, char *content){	size_t		len;	sip_content_t	**loc;	sip_content_t	*msg_content;	_sip_msg_t	*_sip_msg;	if (sip_msg == NULL || content == NULL || strlen(content) == 0)		return (EINVAL);	len = strlen(content);	_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 (ENOTSUP);	}	msg_content = calloc(1, sizeof (sip_content_t));	if (msg_content == NULL) {		(void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);		return (ENOMEM);	}	msg_content->sip_content_start = malloc(strlen(content) + 1);	if (msg_content->sip_content_start == NULL) {		(void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);		free(msg_content);		return (ENOMEM);	}	(void) strncpy(msg_content->sip_content_start, content,	    strlen(content));	msg_content->sip_content_start[strlen(content)] = '\0';	msg_content->sip_content_current = msg_content->sip_content_start;	msg_content->sip_content_end = msg_content->sip_content_start +	    strlen(msg_content->sip_content_start);	msg_content->sip_content_allocated = B_TRUE;	loc = &_sip_msg->sip_msg_content;	while (*loc != NULL)		loc = &((*loc)->sip_content_next);	*loc = msg_content;	_sip_msg->sip_msg_content_len += len;	_sip_msg->sip_msg_len += len;	(void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);	return (0);}/* * Content-Type     =  ( "Content-Type" / "c" ) HCOLON media-type * media-type       =  m-type SLASH m-subtype *(SEMI m-parameter) * m-type           =  discrete-type / composite-type * discrete-type    =  "text" / "image" / "audio" / "video" *			/ "application" / extension-token * composite-type   =  "message" / "multipart" / extension-token * extension-token  =  ietf-token / x-token * ietf-token       =  token * x-token          =  "x-" token * m-subtype        =  extension-token / iana-token * iana-token       =  token * m-parameter      =  m-attribute EQUAL m-value * m-attribute      =  token * m-value          =  token / quoted-string * */intsip_add_content_type(sip_msg_t sip_msg, char *type, char *subtype){	if (sip_msg == NULL || type == NULL || subtype == NULL)		return (EINVAL);	return (sip_add_2strs_to_msg(sip_msg, SIP_CONTENT_TYPE, type, B_FALSE,	    subtype, NULL, SIP_SLASH));}/* * Content-Length  =  ( "Content-Length" / "l" ) HCOLON 1*DIGIT * */intsip_add_content_length(_sip_msg_t *_sip_msg, int length){	_sip_header_t	*new_header;	int 		header_size;	if (_sip_msg == NULL || length < 0)		return (EINVAL);	(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 (ENOTSUP);	}	header_size = strlen(SIP_CONTENT_LENGTH) + SIP_SPACE + sizeof (char) +	    SIP_SPACE + sip_num_of_bytes(length) + strlen(SIP_CRLF) +	    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 %c %u%s%s", SIP_CONTENT_LENGTH, SIP_HCOLON, length,	    SIP_CRLF, 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);}/* * Generic function to add Contact, From,  To, Route or Record-Route header */static intsip_add_name_aspec(sip_msg_t sip_msg, char *display_name, char *uri,    char *tags, boolean_t add_aquot, char *header_name, char *params){	char		*t = uri;	boolean_t	qalloc = B_FALSE;	boolean_t	palloc = B_FALSE;	int		r;	if (sip_msg == NULL || uri == NULL || header_name == NULL)		return (EINVAL);	if (display_name != NULL && !add_aquot)		return (EINVAL);	if (add_aquot) {		t = sip_add_aquot_to_str(uri, &qalloc);		if (t == NULL)			return (ENOMEM);	}	if (tags != NULL) {		int	plen;		if (params != NULL)			return (EINVAL);		plen = strlen(SIP_TAG) + strlen(tags) + 1;		params = malloc(plen);		if (params == NULL)			return (ENOMEM);		(void) snprintf(params, plen, "%s%s", SIP_TAG, tags);		params[plen - 1] = '\0';		palloc = B_TRUE;	}	if (display_name == NULL) {		r = sip_add_2strs_to_msg(sip_msg, header_name, " ", B_FALSE,		    t, params, SIP_SP);	} else {		r = sip_add_2strs_to_msg(sip_msg, header_name, display_name,		    B_TRUE, t, params, SIP_SP);	}	if (qalloc)		free(t);	if (palloc)		free(params);	return (r);}/* * Contact = ("Contact" / "m" ) HCOLON *		( STAR / (contact-param *(COMMA contact-param))) * contact-param  =  (name-addr / addr-spec) *(SEMI contact-params) * name-addr      =  [ display-name ] LAQUOT addr-spec RAQUOT * addr-spec      =  SIP-URI / SIPS-URI / absoluteURI * display-name   =  *(token LWS)/ quoted-string * contact-params     =  c-p-q / c-p-expires *                     / contact-extension */intsip_add_contact(sip_msg_t sip_msg, char *display_name, char *contact_uri,    boolean_t add_aquot, char *contact_params){	return (sip_add_name_aspec(sip_msg, display_name, contact_uri, NULL,	    add_aquot, SIP_CONTACT, contact_params));}/* * From =  ( "From" / "f" ) HCOLON from-spec * from-spec = ( name-addr / addr-spec ) *	*( SEMI from-param ) * from-param  =  tag-param / generic-param * tag-param   =  "tag" EQUAL token * * Since there can be more than one tags, fromtags is a semi colon separated * list of tags. */intsip_add_from(sip_msg_t sip_msg, char *display_name, char *from_uri,    char *fromtags, boolean_t add_aquot, char *from_params){	return (sip_add_name_aspec(sip_msg, display_name, from_uri, fromtags,	    add_aquot, SIP_FROM, from_params));}/* * To =  ( "To" / "t" ) HCOLON ( name-addr *	/ addr-spec ) *( SEMI to-param ) * to-param  =  tag-param / generic-param * */intsip_add_to(sip_msg_t sip_msg, char *display_name, char *to_uri,    char *totags, boolean_t add_aquot, char *to_params){	return (sip_add_name_aspec(sip_msg, display_name, to_uri, totags,	    add_aquot, SIP_TO, to_params));}/* * Route        =  "Route" HCOLON route-param *(COMMA route-param) * route-param  =  name-addr *( SEMI rr-param ) * */intsip_add_route(sip_msg_t sip_msg, char *display_name, char *uri,    char *route_params){	return (sip_add_name_aspec(sip_msg, display_name, uri, NULL, B_TRUE,	    SIP_ROUTE, route_params));}/* * Record-Route  =  "Record-Route" HCOLON rec-route *(COMMA rec-route) * rec-route     =  name-addr *( SEMI rr-param ) * rr-param      =  generic-param * */intsip_add_record_route(sip_msg_t sip_msg, char *display_name, char *uri,    char *route_params){	return (sip_add_name_aspec(sip_msg, display_name, uri, NULL, B_TRUE,	    SIP_RECORD_ROUTE, route_params));}/* * PAssertedID = "P-Asserted-Identity" HCOLON PAssertedID-value *			*(COMMA PAssertedID-value) * PAssertedID-value = name-addr / addr-spec * */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -