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

📄 sip_utils.c

📁 简单的基于SIP的会话边界控制器
💻 C
📖 第 1 页 / 共 2 页
字号:
       osip_message_get_contact(mymsg, 0, &contact);
	if (contact == NULL) {
		printf("ERROR:sip_rewrite_contact: no contact header!\n");
		return STS_FAILURE;
	}
	osip_list_remove(mymsg->contacts, 0);
	if (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[5];
	sprintf(contact_port, "%d", SIP_LISTENING_PORT);
	if (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(mymsg->contacts, contact, 0);

	return STS_SUCCESS;
}

/*
 * SIP_REWRITE_FROM
 *
 * rewrite the From header
 *
 * RETURNS
 *	STS_SUCCESS on success
 *	STS_FAILURE on error
 */
int sip_rewrite_from(osip_message_t *mymsg, int type, int idx) {
	/* Outgoing Request and Incoming Response need to rewrite from 
	 * rewrite host, port if presented 
	 * 10.0.0.5 <--> 192.168.0.5
	 */
	int sts;
	osip_from_t *from;
	from = osip_message_get_from(mymsg);

	if((idx<0) || (idx >= REGNUM_SIZE) || (idx >=  SESSNUM_SIZE)) {
		printf("idx is not in range!");
		return STS_FAILURE;
	}
	
	if (type == RESTYP_INCOMING) {
		/* rewrite host */
		if (from->url->host) {
			osip_free(from->url->host);
			from->url->host = NULL;
		}
		osip_uri_set_host(from->url, osip_strdup(sip_sess_ctl[idx].from_host));
		
		/* rewrite port */
		if (from->url->port) {
			osip_free(from->url->port);
			from->url->port = NULL;
			osip_uri_set_port(from->url, osip_strdup(sip_sess_ctl[idx].from_port));
		}
		
	} else if (type == REQTYP_OUTGOING) {
		/* rewrite host */
		if (from->url->host) {
			osip_free(from->url->host);
			from->url->host = NULL;
		}
		osip_uri_set_host(from->url, osip_strdup(args_info.outaddr_arg));
		
		/* rewrite port */
		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));
		}
	}

	return STS_SUCCESS;
}

/*
 * SIP_REWRITE_TO
 *
 * rewrite the To header
 *
 * RETURNS
 *	STS_SUCCESS on success
 *	STS_FAILURE on error
 */
int sip_rewrite_to(osip_message_t *mymsg, int type, int idx) {
	/* Incoming Request, Outgoing Response need to rewrite to header
	 * rewrite host, port if presented 
	 * 172.16.0.5 <--> 192.168.0.88
	 */
	osip_to_t *to;
	to = osip_message_get_to(mymsg);

	if((idx<0) || (idx >= REGNUM_SIZE) || (idx >=  SESSNUM_SIZE)) {
		printf("idx is not in range!");
		return STS_FAILURE;
	}
	if (type == REQTYP_INCOMING) {
		if(MSG_IS_INVITE(mymsg)) {
			if (to->url->host) {
				osip_free(to->url->host);
				to->url->host = NULL;
			}
			osip_uri_set_host(to->url, osip_strdup(sip_reg_ctl[idx].reg_host));
		
			if (to->url->port) {
				osip_free(to->url->port);
				to->url->port = NULL;
                            osip_uri_set_port(to->url, osip_strdup(sip_reg_ctl[idx].reg_port));	
			}
		}
		else{		
			if (to->url->host) {
				osip_free(to->url->host);
				to->url->host = NULL;
			}
			osip_uri_set_host(to->url, osip_strdup(sip_sess_ctl[idx].from_host));
		
			if (to->url->port) {
				osip_free(to->url->port);
				to->url->port = NULL;
                            osip_uri_set_port(to->url, osip_strdup(sip_sess_ctl[idx].from_port));	
			}
		}
	}
	else if (type == RESTYP_OUTGOING) {
		/* rewrite host */
		if (to->url->host) {
			osip_free(to->url->host);
			to->url->host = NULL;
		}
		osip_uri_set_host(to->url, osip_strdup(args_info.outaddr_arg));		
		/* rewrite port */
		if (to->url->port) {
			osip_free(to->url->port);
			to->url->port = NULL;
			char new_port[6];
			sprintf(new_port, "%d", SIP_LISTENING_PORT);
			osip_uri_set_port(to->url, osip_strdup(new_port));
		}
	}
	return STS_SUCCESS;
}

/*
 * SIP_REWRITE_CALLID
 *
 * rewrite the Call_ID header
 *
 * RETURNS
 *	STS_SUCCESS on success
 *	STS_FAILURE on error
 */
int sip_rewrite_callid(osip_message_t *mymsg, char *ncid, int direction, int idx) {
	
	osip_call_id_t* cid;
	cid = osip_message_get_call_id(mymsg);
	char cidnew[CALLID_SIZE];  

	if (cid->host) {
		if (direction == DIR_OUTGOING) {
			sprintf(cidnew, "%s@%s", ncid, args_info.outaddr_arg);
		} else {
			sprintf(cidnew, "%s@%s", ncid, sip_reg_ctl[idx].reg_host);
		}
	} else {
		sprintf(cidnew, "%s", ncid);
	}
	
	osip_call_id_free(cid);
	cid = NULL;
	osip_message_set_call_id(mymsg, osip_strdup(cidnew));
	
	return STS_SUCCESS;
}

/*
 * SIP_ADD_RECORDROUTE
 *
 * Add a Record-route header
 * 
 * RETURNS
 *	STS_SUCCESS on success
 *	STS_FAILURE on error
 */
int sip_add_recordroute(sip_ticket_t *ticket) {
	/* Incoming Response and Incoming Request need to add recordrout
	 * Record-Route: <sip:172.16.0.5;lr>
	 */
	osip_message_t *mymsg = ticket->sipmsg;
	int sts;
	osip_record_route_t *r_route;
	osip_uri_t *uri_of_proxy;
	int position;
	
	sts = osip_record_route_init(&r_route);
	if (sts == 0) {
		sts = osip_uri_init(&uri_of_proxy);
		if (sts == 0) {
			char tmp[8];

			/* host name and IP */
			osip_uri_set_host(uri_of_proxy, osip_strdup(args_info.inaddr_arg));
			osip_uri_set_username(uri_of_proxy, osip_strdup("nsrsbc"));

			/* port number */
			sprintf(tmp, "%d", SIP_LISTENING_PORT);
			osip_uri_set_port(uri_of_proxy, osip_strdup(tmp));

			/* 'lr' parameter */
			osip_uri_uparam_add(uri_of_proxy, osip_strdup("lr"), NULL);

			osip_record_route_set_url(r_route, uri_of_proxy);

			position=0;
			/* if it is a response, add in to the end of the list
			 * (reverse order as in request!) 
			 */
			if ((ticket->direction==RESTYP_INCOMING) || 
				(ticket->direction==RESTYP_OUTGOING)) {
					position = -1;
			}

			/* insert into record-route list*/
			osip_list_add(mymsg->record_routes, r_route, position);
			
		} else {
			osip_record_route_free(r_route);
		} 
	}
	
	return STS_SUCCESS;
}

/*
 * SIP_DEL_ROUTE
 *
 * Delete a Route header
 * 
 * RETURNS
 *	STS_SUCCESS on success
 *	STS_FAILURE on error
 */
int sip_del_route(sip_ticket_t *ticket) {
	int sts;
	osip_message_t *mymsg = ticket->sipmsg;
	osip_route_t *route = NULL;
	
	if (mymsg->routes && !osip_list_eol(mymsg->routes, 0)) {
		route = (osip_route_t *) osip_list_get(mymsg->routes, 0);
		if (route==NULL || route->url==NULL || route->url->host==NULL) {
			printf("sip_del_route: got broken Route header!\n");
			return STS_FAILURE;
		}
		
		if (!strcmp(route->url->host, args_info.inaddr_arg)) {
			osip_list_remove(mymsg->routes, 0);
			osip_route_free(route);
		} else {
			printf("sip_del_route: there is no my Route header!\n");
		}	
	}
	
	return STS_SUCCESS;
}

/*
 * SIP_PURGE_RECORDROUTE
 *
 * Purge Record-Route headers pointing to myself.
 * 
 * RETURNS
 *	STS_SUCCESS on success
 *	STS_FAILURE on error
 */
int sip_purge_recordroute(sip_ticket_t *ticket) {
	osip_message_t *mymsg = ticket->sipmsg;
	osip_record_route_t *r_route = NULL;
	int last, i, sts;

	if (mymsg->record_routes) {
		last = osip_list_size(mymsg->record_routes) - 1;
		/* I MUST NOT purge any alien (non-mine) Record-Route headers,
		* only the ones I've put in myself! 
		*/
		if (last >= 0) {
			for (i = last; i >= 0; i--) {
				r_route = (osip_record_route_t *) osip_list_get(mymsg->record_routes, i);
				/* skip empty entries */
				if (r_route == NULL) continue;
				if (r_route->url == NULL) continue;
				if (r_route->url->host == NULL) continue;
				
				/* check if my own route header? */
				if (!strcmp(r_route->url->host, args_info.inaddr_arg)) {
					osip_list_remove(mymsg->record_routes, i);
					osip_record_route_free(r_route);
				}
			}
		}
	}

	return STS_SUCCESS;
}

/*
 * SIP_REWRITE_REQURI
 *
 * rewrites the incoming Request URI and REGISTER URI
 * 
 * RETURNS
 *	STS_SUCCESS on success
 */
int sip_rewrite_requri(osip_message_t *mymsg, int type, int idx) {
	int sts;
	osip_uri_t *url;
	url = osip_message_get_uri(mymsg);
	
	if (type == REQTYP_INCOMING) {
		/* Now support INVITE, BYE, ACK, all need to rewrite request uri */
		if ((idx >= REGNUM_SIZE) || (idx < 0))	return STS_FAILURE;		
		if(MSG_IS_INVITE(mymsg)) {
			if (url->host) {
				osip_free(url->host);
				url->host = NULL;
			}
			osip_uri_set_host(url, osip_strdup(sip_reg_ctl[idx].reg_host));
		
			if (url->port) {
				osip_free(url->port);
				url->port = NULL;
                osip_uri_set_port(url, osip_strdup(sip_reg_ctl[idx].reg_port));	
			}
		}
		else{		
			if (url->host) {
				osip_free(url->host);
				url->host = NULL;
			}
			osip_uri_set_host(url, osip_strdup(sip_sess_ctl[idx].from_host));
		
			if (url->port) {
				osip_free(url->port);
				url->port = NULL;
                            osip_uri_set_port(url, osip_strdup(sip_sess_ctl[idx].from_port));	
			}
		}			
	} else if (type == REQTYP_OUTGOING) {
		/* Only REGISTER message need to rewrite request uri, 
		 * other outgoing request need not.
		 */
		/* set host */
		if (url->host) {
			osip_free(url->host);
			url->host = NULL;
		}
		osip_uri_set_host(url, osip_strdup(args_info.proxy_addr_arg));
		
		/* set port if it present */
		if (url->port) {
			osip_free(url->port);
			url->port = NULL;
			char new_port[6];
			sprintf(new_port, "%d", SIP_LISTENING_PORT);
			osip_uri_set_port(url, osip_strdup(new_port));
		}
	}
	
	return STS_SUCCESS;	
}

/*
 * compares two Call-IDs
 * (by now, only hostname and number are compared)
 *
 * RETURNS
 *	STS_SUCCESS if equal
 *	STS_FAILURE if non equal or error
 */
int compare_callid(osip_call_id_t *cid1, osip_call_id_t *cid2) {

   if ((cid1==0) || (cid2==0))    return STS_FAILURE;

   /*
    * Check number part: if present must be equal, 
    * if not present, must be not present in both cids
    */
   if (cid1->number && cid2->number) {
      /* have both numbers */
      if (strcmp(cid1->number, cid2->number) != 0) goto mismatch;
   } else {
      /* at least one number missing, make sure that both are empty */
      if ( (cid1->number && (cid1->number[0]!='\0')) ||
           (cid2->number && (cid2->number[0]!='\0'))) {
         goto mismatch;
      }
   }

   /*
    * Check host part: if present must be equal, 
    * if not present, must be not present in both cids
    */
   if (cid1->host && cid2->host) {
      /* have both hosts */
      if (strcmp(cid1->host, cid2->host) != 0) goto mismatch;
   } else {
      /* at least one host missing, make sure that both are empty */
      if ( (cid1->host && (cid1->host[0]!='\0')) ||
           (cid2->host && (cid2->host[0]!='\0'))) {
         goto mismatch;
      }
   }

   //printf("comparing callid - matched: "  "%s@%s <-> %s@%s\n",
  //        cid1->number, cid1->host, cid2->number, cid2->host);
   return STS_SUCCESS;

mismatch:
   printf("comparing callid - mismatch: " "%s@%s <-> %s@%s\n",
          cid1->number, cid1->host, cid2->number, cid2->host);
   return STS_FAILURE;
}   

⌨️ 快捷键说明

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