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

📄 authorize.c

📁 用来作为linux中SIP SERVER,完成VOIP网络电话中服务器的功能
💻 C
📖 第 1 页 / 共 2 页
字号:
	}	if ( (req=AAAInMessage(AA_REQUEST, AAA_APP_NASREQ))==NULL)		return -1;	if(hdr && hdr->parsed)		cred = &(((auth_body_t*)hdr->parsed)->digest);	else		cred = NULL;				method = *p_method;	if(!cred)	{		/* Username AVP */		user_name.len = uri.user.len + uri.host.len;		if(user_name.len>0)		{			user_name.len += 2;			user_name.s = (char*)ad_malloc(user_name.len*sizeof(char));			memset(user_name.s, 0, user_name.len);			memcpy(user_name.s, uri.user.s, uri.user.len);			if(uri.user.len>0)			{				memcpy(user_name.s+uri.user.len, "@", 1);				memcpy(user_name.s+uri.user.len+1, uri.host.s, uri.host.len);			}			else				memcpy(user_name.s, uri.host.s, uri.host.len);		}		if( (avp=AAACreateAVP(AVP_User_Name, 0, 0, user_name.s, 							user_name.len, AVP_FREE_DATA)) == 0)		{			LOG(L_ERR,M_NAME":diameter_authorize(): no more free memory!\n");			if(user_name.len>0)				pkg_free(user_name.s);			goto error;		}		if( AAAAddAVPToMessage(req, avp, 0)!= AAA_ERR_SUCCESS)		{			LOG(L_ERR, M_NAME":diameter_authorize(): avp not added \n");			goto error1;		}	}	else /* it is a SIP message with credentials */	{		/* Add Username AVP */		if (cred->username.domain.len>0) 		{			if( (avp=AAACreateAVP(AVP_User_Name, 0, 0, cred->username.whole.s,							cred->username.whole.len, AVP_DUPLICATE_DATA)) == 0)			{				LOG(L_ERR, M_NAME":diameter_authorize(): no more free "					"memory!\n");				goto error;			}			if( AAAAddAVPToMessage(req, avp, 0)!= AAA_ERR_SUCCESS)			{				LOG(L_ERR, M_NAME":diameter_authorize(): avp not added \n");				goto error1;			}		}		else 		{			user_name.len = cred->username.user.len + cred->realm.len;			if(user_name.len>0)			{				user_name.s = ad_malloc(user_name.len);				if (!user_name.s) 				{					LOG(L_ERR, M_NAME":diameter_authorize(): no more free "						"memory\n");					goto error;				}				memcpy(user_name.s, cred->username.whole.s, 									cred->username.whole.len);				if(cred->username.whole.len>0)				{					user_name.s[cred->username.whole.len] = '@';					memcpy(user_name.s + cred->username.whole.len + 1, 							cred->realm.s, cred->realm.len);				}				else					memcpy(user_name.s,	cred->realm.s, cred->realm.len);			}			if( (avp=AAACreateAVP(AVP_User_Name, 0, 0, user_name.s,								user_name.len, AVP_FREE_DATA)) == 0)			{				LOG(L_ERR, M_NAME":diameter_authorize(): no more free "					"memory!\n");				if(user_name.len>0)					pkg_free(user_name.s);				goto error;			}			if( AAAAddAVPToMessage(req, avp, 0)!= AAA_ERR_SUCCESS)			{				LOG(L_ERR, M_NAME":diameter_authorize(): avp not added \n");				goto error1;			}		}	}	/* SIP_MSGID AVP */	DBG("******* m_id=%d\n", m_id);	tmp = m_id;	if( (avp=AAACreateAVP(AVP_SIP_MSGID, 0, 0, (char*)(&tmp), 				sizeof(m_id), AVP_DUPLICATE_DATA)) == 0)	{		LOG(L_ERR, M_NAME":diameter_authorize(): no more free memory!\n");		goto error;	}	if( AAAAddAVPToMessage(req, avp, 0)!= AAA_ERR_SUCCESS)	{		LOG(L_ERR, M_NAME":diameter_authorize(): avp not added \n");		goto error1;	}			/* SIP Service AVP */	if( (avp=AAACreateAVP(AVP_Service_Type, 0, 0, SIP_AUTHENTICATION, 				SERVICE_LEN, AVP_DUPLICATE_DATA)) == 0)	{		LOG(L_ERR, M_NAME":diameter_authorize(): no more free memory!\n");		goto error;	}	if( AAAAddAVPToMessage(req, avp, 0)!= AAA_ERR_SUCCESS)	{		LOG(L_ERR, M_NAME":diameter_authorize(): avp not added \n");		goto error1;	}			/* Destination-Realm AVP */	if( (avp=AAACreateAVP(AVP_Destination_Realm, 0, 0, uri.host.s,						uri.host.len, AVP_DUPLICATE_DATA)) == 0)	{		LOG(L_ERR, M_NAME":diameter_authorize(): no more free memory!\n");		goto error;	}#ifdef DEBUG		DBG("Destination Realm: %.*s\n", uri.host.len, uri.host.s);	#endif	if( AAAAddAVPToMessage(req, avp, 0)!= AAA_ERR_SUCCESS)	{		LOG(L_ERR, M_NAME":diameter_authorize(): avp not added \n");		goto error1;	}		/* Resource AVP */	user_name.len = ruri.user.len + ruri.host.len + ruri.port.len + 2;	user_name.s = (char*)ad_malloc(user_name.len*sizeof(char));	memset(user_name.s, 0, user_name.len);	memcpy(user_name.s, ruri.user.s, ruri.user.len);	name_flag= 0;	if(ruri.user.s)	{				name_flag = 1;		memcpy(user_name.s+ruri.user.len, "@", 1);	}		memcpy(user_name.s+ruri.user.len+name_flag, ruri.host.s, ruri.host.len);	port_flag=0;	if(ruri.port.s)	{		port_flag = 1;			memcpy(user_name.s+ruri.user.len+ruri.host.len+1, ":", 1);	}		memcpy(user_name.s+ruri.user.len+ruri.host.len+name_flag+port_flag, 					ruri.port.s, ruri.port.len);#ifdef DEBUG	DBG(M_NAME": AVP_Resource=%.*s\n", user_name.len, user_name.s);#endif	if( (avp=AAACreateAVP(AVP_Resource, 0, 0, user_name.s,						user_name.len, AVP_FREE_DATA)) == 0)	{		LOG(L_ERR, M_NAME":diameter_authorize(): no more free memory!\n");		if(user_name.s)			pkg_free(user_name.s);		goto error;	}	if( AAAAddAVPToMessage(req, avp, 0)!= AAA_ERR_SUCCESS)	{		LOG(L_ERR, M_NAME":diameter_authorize(): avp not added \n");		goto error1;	}	if(cred) /* it is a SIP message with credentials */	{		/* Response AVP */		if( (avp=AAACreateAVP(AVP_Response, 0, 0, hdr->body.s,						hdr->body.len, AVP_DUPLICATE_DATA)) == 0)		{			LOG(L_ERR, M_NAME":diameter_authorize(): no more free memory!\n");			goto error;		}				position = AAAGetLastAVP(&(req->avpList));		if( AAAAddAVPToMessage(req, avp, position)!= AAA_ERR_SUCCESS)						{			LOG(L_ERR, M_NAME":diameter_authorize(): avp not added \n");			goto error1;		}		/* Method AVP */		if( (avp=AAACreateAVP(AVP_Method, 0, 0, p_method->s,						p_method->len, AVP_DUPLICATE_DATA)) == 0)		{			LOG(L_ERR, M_NAME":diameter_authorize(): no more free memory!\n");			goto error;		}				position = AAAGetLastAVP(&(req->avpList));		if( AAAAddAVPToMessage(req, avp, position)!= AAA_ERR_SUCCESS)						{			LOG(L_ERR, M_NAME":diameter_authorize(): avp not added \n");			goto error1;		}		}			#ifdef DEBUG	AAAPrintMessage(req);#endif	/* build a AAA message buffer */	if(AAABuildMsgBuffer(req) != AAA_ERR_SUCCESS)	{		LOG(L_ERR, M_NAME":diameter_authorize(): message buffer not created\n");		goto error;	}		if(sockfd==AAA_NO_CONNECTION)	{		sockfd = init_mytcp(diameter_client_host, diameter_client_port);		if(sockfd==AAA_NO_CONNECTION)		{			LOG(L_ERR, M_NAME":diameter_authorize(): failed to reconnect"								" to Diameter client\n");			goto error;		}	}	/* send the message to the DIAMETER CLIENT */	switch( tcp_send_recv(sockfd, req->buf.s, req->buf.len, rb, m_id) )	{		case AAA_ERROR: /* a transmission error occurred */			LOG(L_ERR, M_NAME":diameter_authorize(): message sending to the" 						" DIAMETER backend authorization server failed\n");			goto error;			case AAA_CONN_CLOSED:			LOG(L_NOTICE, M_NAME":diameter_authorize(): connection to Diameter"					" client closed.It will be reopened by the next request\n");			close(sockfd);			sockfd = AAA_NO_CONNECTION;			goto error;		case AAA_TIMEOUT:			LOG(L_NOTICE,M_NAME":diameter_authorize(): no response received\n");			close(sockfd);			sockfd = AAA_NO_CONNECTION;			goto error;	}	AAAFreeMessage(&req);	return 1;error1:	AAAFreeAVP(&avp);error:	AAAFreeMessage(&req);	return -1;}/* give the appropriate response to the SER client */int srv_response(struct sip_msg* msg, rd_buf_t * rb, int hftype){	int auth_hf_len=0, ret=0;	char* auth_hf;	switch(rb->ret_code)	{		case AAA_AUTHORIZED:			return 1;					case AAA_NOT_AUTHORIZED:			send_resp(msg, 403, "Forbidden", NULL, 0);				return -1;		case AAA_SRVERR:			send_resp(msg, 500, "Internal Server Error", NULL, 0);				return -1;						case AAA_CHALENGE:		 	if(hftype==HDR_AUTHORIZATION) /* SIP server */			{				auth_hf_len = WWW_AUTH_CHALLENGE_LEN+rb->chall_len;				auth_hf = (char*)ad_malloc(auth_hf_len*(sizeof(char)));				memset(auth_hf, 0, auth_hf_len);				memcpy(auth_hf,WWW_AUTH_CHALLENGE, WWW_AUTH_CHALLENGE_LEN);				memcpy(auth_hf+WWW_AUTH_CHALLENGE_LEN, rb->chall,					rb->chall_len);						ret = send_resp(msg, 401, MESSAGE_401, auth_hf, auth_hf_len);			}			else	/* Proxy Server */			{				auth_hf_len = PROXY_AUTH_CHALLENGE_LEN+rb->chall_len;				auth_hf = (char*)ad_malloc(auth_hf_len*(sizeof(char)));				memset(auth_hf, 0, auth_hf_len);				memcpy(auth_hf, PROXY_AUTH_CHALLENGE, PROXY_AUTH_CHALLENGE_LEN);				memcpy(auth_hf + PROXY_AUTH_CHALLENGE_LEN, rb->chall, 						rb->chall_len);				ret = send_resp(msg, 407, MESSAGE_407, auth_hf, auth_hf_len);			}			if (auth_hf) pkg_free(auth_hf);				if (ret == -1) 			{				LOG(L_ERR, M_NAME":srv_response():Error while sending challenge "					"to the client of SER\n");				return -1;			}			return -1;	}		// never reach this 	return -1;		}/* * Create a response with given code and reason phrase * Optionally add new headers specified in _hdr */int send_resp(struct sip_msg* m, int code, char* reason,					char* hdr, int hdr_len){	/* Add new headers if there are any */	if ((hdr) && (hdr_len)) {		if (add_lump_rpl( m, hdr, hdr_len, LUMP_RPL_HDR)==0) {			LOG(L_ERR,"ERROR:auth_diameter:send_resp: unable to append hdr\n");			return -1;		}	}	return sl_reply(m, (char*)(long)code, reason);}

⌨️ 快捷键说明

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