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

📄 proxy.c

📁 简单的基于SIP的会话边界控制器
💻 C
📖 第 1 页 / 共 3 页
字号:
		/* no via, should be repair, now not supported */
		printf("Can't register client: no via header!\n");
		return STS_FAILURE;
	}
	/* parameters now do not rewrite */
	
	/* 5. rewrite contact */
	/* there could be many contact headers, now just supported one contact */
	/* now not supported "exe" params */
	osip_contact_t *contact;
	osip_message_get_contact(request, 0, &contact);
	if (contact) {
		osip_list_remove(request->contacts, 0);
		if (contact->url->host) {
			strcpy(reg_hostname, contact->url->host);
			osip_free(contact->url->host);
			contact->url->host = NULL;
		}
		osip_uri_set_host(contact->url, osip_strdup(args_info.outaddr_arg));
		
		char contact_port[6];
		sprintf(contact_port, "%d", SIP_LISTENING_PORT);
		if (contact->url->port) {
			strcpy(reg_port, contact->url->port);
			osip_free(contact->url->port);
			contact->url->port = NULL;
			osip_uri_set_port(contact->url, osip_strdup(contact_port));
		}
		osip_list_add(request->contacts, contact, 0);
	}else {
		/* no contact, should be repair, now not supported */
		printf("Can't register client: no contact header!\n");
		//return STS_FAILURE;
	}

	/* 6. rewrite call id */
	char *cidold;
	char cidnew[CALLID_SIZE];
	if (request->call_id) {
		osip_call_id_to_str(request->call_id, &cidold);
		sts = sessctl_find(sip_sess_ctl, cidold, SESSNUM_SIZE);
		if (sts >= 0) {
			memcpy(cidnew, sip_sess_ctl[sts].cid_new, 
			    strlen(sip_sess_ctl[sts].cid_new));
			osip_call_id_free(request->call_id);
			request->call_id = NULL;
			osip_message_set_call_id(request, osip_strdup(cidnew));
	//		printf("%d\n",sts);
	//		printf("%s\n",cidold);
	//		printf("%s\n",cidnew);
	//		printf("rewrite call-id header successful!\n");
		} 
		else {
			char *cidrand;
			cidrand = osip_call_id_new_random();
			sprintf(cidnew, "%s@%s", cidrand, args_info.outaddr_arg);	
			osip_call_id_free(request->call_id);
			request->call_id = NULL;
			osip_message_set_call_id(request, osip_strdup(cidnew));
	//		printf("%s\n",cidold);
	//		printf("%s\n",cidnew);
	//		printf("rewrite call-id header successful!\n");
			if (strlen(reg_port) == 0)	sprintf(reg_port, "%s", SIP_LISTENING_PORT);
			if (strlen(reg_hostname) == 0)	sprintf(reg_hostname, "%s", "0.0.0.0");

			sts = sessctl_new(sip_sess_ctl, cidold, cidnew, reg_hostname, 
					reg_port, SESSNUM_SIZE);
			if (sts < 0)	return STS_FAILURE;
		}
	}else {
		/* no call id, should be repair, now not supported */
		printf("Can't register client: no call id header!\n");
		return STS_FAILURE;
	}


	/* 7. rewrite expires */
	{
		osip_header_t *expires_hdr = NULL;
		char expires[10];
		sprintf(expires, "%d", DEFAULT_EXPIRES);
		osip_message_get_expires(request, 0, &expires_hdr);
		if (expires_hdr == NULL) {
			osip_message_set_expires(request, expires);
		} else {
			if (expires_hdr->hvalue) osip_free(expires_hdr->hvalue);
			expires_hdr->hvalue = osip_strdup(expires);
		}
	}
		
	/* 8. send register to proxy */	
	sts = osip_message_to_str(request, &buffer, &buflen);
	if (sts != 0) {
		printf("ERROR:proxy_rewrite_register(): sip_message_to_str failed\n");
		return STS_FAILURE;
	}

       char tmp[2048];
       sprintf(tmp,"<cursip>%s</cursip>",buffer);
  //  printf("%s\n",tmp);
       strcat(interface_buffer,tmp);
  //  printf("%s\n",interface_buffer);
       printf("%s\n",buffer);
	
	if (inet_aton(args_info.proxy_addr_arg, &dest_addr) < 0) {
		printf("proxy_rewrite_register(): proxy ip error, can not send!\n");
		return STS_FAILURE;
	}
	
	/* there should be able to define register port, here takes SIP_LISTENING_PORT */
	sipsock_send(dest_addr, SIP_LISTENING_PORT, ticket->protocol, buffer, buflen); 
	
	osip_free(buffer);
	//osip_free(tmp);
	
	/* 9. Save register informaiton */
	recv_addr = ticket->from.sin_addr;
	recv_port = ntohs(ticket->from.sin_port);
	
	i = regctl_find(sip_reg_ctl, recv_addr, recv_port, REGNUM_SIZE);
	if (i >= 0) {
		regctl_update(sip_reg_ctl, DEFAULT_EXPIRES, reg_hostname, 
				reg_port, reg_username, i);
	} else {
		sts = regctl_new(sip_reg_ctl, DEFAULT_EXPIRES, recv_addr, 
			recv_port, reg_hostname, reg_username, reg_port, REGNUM_SIZE);
		if (sts < 0)	return STS_FAILURE;
	}
	
	return STS_SUCCESS;
}

/*
 * PROXY_REWRITE_REGISTER_RESPONSE
 *
 * rewrites the response  message for REGISTER
 * 
 * RETURNS
 *	STS_SUCCESS on success
 *    STS_FAILURE on failure
 */
int proxy_rewrite_register_response(sip_ticket_t *ticket) {
	int sts,i,j;
	struct in_addr dest_addr;
	int dest_port;
	char *buffer;
	size_t buflen;
	
	osip_message_t *response;

       if (ticket == NULL) {
		printf("ERROR: proxy_rewrite_register_response: response with NULL ticket\n");
		return STS_FAILURE;
	}
	response = ticket->sipmsg;
	char* callid;
	osip_call_id_to_str(response->call_id,&callid);
	/* find matching register session */
	i=sessctl_find(sip_sess_ctl, callid, SESSNUM_SIZE);
	if(i>=0) {
		/* rewrite sip message */
		/* rewrite call_id */
		osip_call_id_free(response->call_id);
		response->call_id = NULL;			
		osip_message_set_call_id(response, osip_strdup(sip_sess_ctl[i].cid_old));
		/* rewrite from */
		{
			osip_from_t *from;
			from = osip_message_get_from(response);		
		
			if (from->url->host) {
				osip_free(from->url->host);
				from->url->host = NULL;
			}
			osip_uri_set_host(from->url, osip_strdup(args_info.inaddr_arg));
		
			if (from->url->port) {
				osip_free(from->url->port);
				from->url->port = NULL;
				char new_port[6];
				sprintf(new_port, "%d", SIP_LISTENING_PORT);
				osip_uri_set_port(from->url, osip_strdup(new_port));
			}
		}
		/* rewrite to */
		{
			osip_to_t *to;
			to = osip_message_get_to(response);
		
			if (to->url->host) {
				osip_free(to->url->host);
				to->url->host = NULL;
			}
			osip_uri_set_host(to->url, osip_strdup(args_info.inaddr_arg));
		
			if (to->url->port) {
				osip_free(to->url->port);
				to->url->port = NULL;
				char nport[6];
				sprintf(nport, "%d", SIP_LISTENING_PORT);
				osip_uri_set_port(to->url, osip_strdup(nport));
			}
		}

		/* rewrite via */
		osip_via_t *via;
		osip_message_get_via(response, 0, &via);		
		osip_list_remove(response->vias, 0);
		if (via->host) {
			osip_free(via->host);
			via->host = NULL;
		}	
		via_set_host(via, osip_strdup(sip_sess_ctl[i].from_host));
		if (via->port) {
			osip_free(via->port);
			via->port = NULL;
			via_set_port(via, osip_strdup(sip_sess_ctl[i].from_port));
		}
		osip_list_add(response->vias, via, 0);

		/* rewrite contact */
		osip_contact_t *contact;
		osip_message_get_contact(response, 0, &contact);
		if(contact) {
			osip_list_remove(response->contacts, 0);
			if (contact->url->host) {
				osip_free(contact->url->host);
				contact->url->host = NULL;
			}
			osip_uri_set_host(contact->url, osip_strdup(sip_sess_ctl[i].from_host));
			if (contact->url->port) {
				osip_free(contact->url->port);
				contact->url->port = NULL;
				osip_uri_set_port(contact->url, osip_strdup(sip_sess_ctl[i].from_port));
			}
			osip_list_add(response->contacts, contact, 0);
		}
		else
			printf("There is no contact header\n");

		/* send response */	
		sts = osip_message_to_str(response, &buffer, &buflen);
		if (sts != 0) {
			printf("ERROR:proxy_rewrite_register(): sip_message_to_str failed\n");
			return STS_FAILURE;
		}

        	printf("%s\n",buffer);
         	char tmp[2048];
       	sprintf(tmp,"<cursip>%s</cursip>",buffer);
       	strcat(interface_buffer,tmp);

       	j = regctl_findaddr(sip_reg_ctl, response->from->url->username, &dest_addr, 
				&dest_port, REGNUM_SIZE);
       	if (j < 0) {
			printf("ERROR:proxy_response(): can not get ip by username!\n");
			return STS_FAILURE;
		}

		sipsock_send(dest_addr, dest_port, ticket->protocol, buffer, buflen); 
	
		osip_free(buffer);

		/* delete info from sip_sess_t */
		sts = sessctl_free(sip_sess_ctl,i);
	}
	else {
		//osip_free(response);
		printf("The register response is no matching dialog,discard it\n");
		return STS_FAILURE;
	}
	return STS_SUCCESS;
}

/*
 * PROXY_REQUEST
 *
 * RETURNS
 *	STS_SUCCESS on success
 *	STS_FAILURE on error
 */
int proxy_request(sip_ticket_t *ticket) {
	int sts, i, j;
	int type;
	struct in_addr destaddr;
	int destport;
	char *buffer;
	size_t buflen;
	osip_message_t *request;
	osip_uri_t *req_uri;
	char* callid;

	if (ticket==NULL) {
		printf("ERROR: proxy_request: called with NULL ticket!\n");
		return STS_FAILURE;
	}
	request = ticket->sipmsg;
	
	/* get request direction */
	sts = sip_find_direction(ticket);
	if(sts) {
		printf("Can't handle this message,we can't find matching dialog!\n"); 
              return STS_FAILURE;
	}
	type = ticket->direction;

	/* Save request uri for destination determine */
	req_uri = osip_message_get_uri(request);
	/* According call_id  we check whether this request  belong to a existed dialog */
	osip_call_id_to_str(request->call_id,&callid);
      	 i = sessctl_find(sip_sess_ctl,callid,SESSNUM_SIZE);
	switch (type) {
		/*
		 * from an external host to the internal host
		 */
		case REQTYP_INCOMING:
			 /* include BYE,ACK ...message */
      			if(i >= 0) {
       			/* rewrite request uri by sip_sessctl_t */
				sip_rewrite_requri(request, REQTYP_INCOMING, i);
       			
				/* rewrite to */
				sip_rewrite_to(request,REQTYP_INCOMING,i);

				/* add via */
				sip_add_myvia(ticket);

				/*add record-route */
				sip_add_recordroute(ticket);
				
				//modified 2007.11.02
				if (MSG_IS_BYE(request) && invite_direct == DIR_INCOMING) {
					rtp_stop_fwd(osip_message_get_call_id(request), DIR_INCOMING);
					rtp_stop_fwd(osip_message_get_call_id(request), DIR_OUTGOING);
				}

				if (MSG_IS_BYE(request) && invite_direct == DIR_OUTGOING) {
					rtp_stop_fwd(osip_message_get_call_id(request), DIR_INCOMING);
					rtp_stop_fwd(osip_message_get_call_id(request), DIR_OUTGOING);
					/* rewrite call-id */
					osip_call_id_free(request->call_id);
					request->call_id = NULL;
					osip_message_set_call_id(request, osip_strdup(sip_sess_ctl[i].cid_old));
				}	
       		}
      			 else {
       			if(MSG_IS_INVITE(request)) {

					invite_direct = DIR_INCOMING; //add 2007.11.02
       				
					j = regctl_finduser(sip_reg_ctl, request->req_uri->username, REGNUM_SIZE);
					if(j < 0) {
						printf("The callee is not a user registered in nsrsbc!\n");
						osip_free(request);
						return STS_FAILURE;
					}
					/* rewrtie request_uri */
					sts = sip_rewrite_requri(request, REQTYP_INCOMING, j);
					/* rewrite to */
					sip_rewrite_to(request,REQTYP_INCOMING,j);
					/* add Record-Route */
					sip_add_recordroute(ticket);
					/* add via */
					sip_add_myvia(ticket);
					/* add sip_sess_ctl */
					sts = sessctl_new(sip_sess_ctl, callid, NULL,  sip_reg_ctl[j].reg_host,
						sip_reg_ctl[j].reg_port, SESSNUM_SIZE);
					if (sts < 0)	return STS_FAILURE;
                     		/* rewrite sdp */
					sts = sip_rewrite_sdp(ticket, DIR_INCOMING);
       			}
       			else {
       				printf("Can't handle this message!\n");
					//osip_free(request);
               			return STS_FAILURE;
       			}
       		}
      			            
			/* max-forward decrement by one */               
			/*{
   				osip_header_t *max_forwards;
  				 int forwards_count = DEFAULT_MAXFWD;
   				char mfwd[8];

   				osip_message_get_max_forwards(request, 0, &max_forwards);
   				if (max_forwards == NULL) {
      					sprintf(mfwd, "%i", forwards_count);
      					osip_message_set_max_forwards(request, mfwd);
   				}
   				else {
      					if (max_forwards->hvalue) {

⌨️ 快捷键说明

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