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

📄 rsvp_main.c

📁 radius协议源码÷The Radius Stack will connect to a Radius Server. This stack implementation is built upo
💻 C
📖 第 1 页 / 共 3 页
字号:
	if (kill(recp->api_pid, 0) == -1)		if (errno == ESRCH)			return(-1);	/* Also make sure we still have record for it	 */	if (recp->api_p_packet.pkt_data == NULL &&	    recp->api_r_packet.pkt_data == NULL)		return(-1);	if ((recp->api_p_packet.pkt_data)) {		rc = rsvp_pkt_process(&recp->api_p_packet, NULL, api_num);		if (rc < 0) {  /* Internal error */			log(LOG_ERR, 0, "\nParse error %d in API pkt\n", rc);			hexf(stdout, (char *)recp->api_p_packet.pkt_data, 					recp->api_p_packet.pkt_len);		}	}	if ((recp->api_r_packet.pkt_data)) {		rc = rsvp_pkt_process(&recp->api_r_packet, NULL, api_num);		if (rc < 0) {  /* Internal error */			log(LOG_ERR, 0, "\nParse error %d in API pkt\n", rc);			hexf(stdout, (char *)recp->api_r_packet.pkt_data, 					recp->api_r_packet.pkt_len);		}	}	return (0);}/* * reset_log() resets a log, if it is too long. It renames it to another name * and opens a new log. This way there is a limit on the size of the logs, * and at most 2 logs exists at any given time. * * The date and time are written in the header of each logfile. */voidreset_log(int init) {	char            file[256],prev[256];	char            header[81];	time_t          clock;	if (!init) {		fprintf(logfp, ".\n.\n= END OF LOG, ... start a new log =\n");		(void) fclose(logfp);	}	strcpy(file,RSVP_LOG);	strcat(file,rsvp_log_suffix);	strcpy(prev,RSVP_LOG_PREV);	strcat(prev,rsvp_log_suffix);	(void) unlink(prev);	(void) rename(file, prev);	if ((logfp = fopen(file, "w")) == NULL) {		fprintf(stderr, "RSVP: can\'t open log file %s\n", file);		sprintf(rsvp_log_suffix,".%d",(int) getpid());		strcat(file,rsvp_log_suffix);		if ((logfp = fopen(file, "w")) == NULL) {			fprintf(stderr, "RSVP: can\'t open log file %s\n",file);			exit(1);		}	}	setbuf(logfp, (char *) NULL);	sprintf(header, "%s\n", vers_comp_date);	log(LOG_ALWAYS, 0, header);	clock = time((time_t *) NULL);        log(LOG_ALWAYS, 0, "Log level:%d  Debug mask:%d  Start time: %s", 			l_debug, m_debug, ctime(&clock));}/* *  api_cmd() takes a file descriptor and a command message from the API *	executes the command in the message. * *	DON'T FORGET to set the response code, 'old' */intapi_cmd(int fd, rapi_cmd_t *cmd) {	int old;	if (cmd->rapi_cmd_type == RAPI_CMD_DEBUG_MASK) {		old = m_debug;		if (cmd->rapi_filler != (u_short)-1) {			m_debug = cmd->rapi_filler;			log(LOG_INFO, 0, "Debug Mask changed from %d to %d\n",				old, m_debug);		} else			log(LOG_INFO, 0, "Debug Mask unchanged at %d\n", old);	} else if (cmd->rapi_cmd_type == RAPI_CMD_DEBUG_LEVEL) {		old = l_debug;		if (cmd->rapi_filler != (u_short)-1) {			l_debug = cmd->rapi_filler;			log(LOG_INFO, 0, "Debug Level changed from %d to %d\n",				old, l_debug);		} else			log(LOG_INFO, 0, "Debug Level unchanged at %d\n", old);	} else if (cmd->rapi_cmd_type == RAPI_CMD_FILTER_ON) {		old = debug_filter;		if (cmd->rapi_filler != (u_short)-1) {			debug_filter = 1;			log(LOG_INFO, 0, "Debug filter now 1, was %d\n", old);		} else			log(LOG_INFO, 0, "Debug filter unchanged at %d\n", old);	} else if (cmd->rapi_cmd_type == RAPI_CMD_FILTER_OFF) {		old = debug_filter;		if (cmd->rapi_filler != (u_short)-1) {			debug_filter = 0;			debug_filter_num = 0;			log(LOG_INFO, 0, "Debug filter now 0, was %d\n", old);		} else			log(LOG_INFO, 0, "Debug filter unchanged at %d\n", old);	} else if (cmd->rapi_cmd_type == RAPI_CMD_ADD_FILTER) {		if (debug_filter != 1) {			log(LOG_INFO, 0, "debug_filter should be 1\n");			old = -1;		} else {			int i;			struct in_addr in;			net_addr addr;			in.s_addr = cmd->rapi_data[0];			NET_SET_ADDR_IPv4(&addr,in);			for (i = 0; i < debug_filter_num; i++)				if (net_addr_equal(&addr,&debug_filters[i]))					break;			if (i == debug_filter_num) {				if (debug_filter_num >= MAX_DEBUG_FILTER) {					log(LOG_ERR, 0, "Too many filters\n");					old = -1;				} else {					debug_filters[debug_filter_num++] = addr;					old = debug_filter_num;				}			}		}	} else if (cmd->rapi_cmd_type == RAPI_CMD_DEL_FILTER) {		if (debug_filter != 1) {			log(LOG_INFO, 0, "debug_filter should be 1\n");			old = -1;		} else {			int i;			struct in_addr in;			net_addr addr;			in.s_addr = cmd->rapi_data[0];			for (i = 0; i < debug_filter_num; i++)				if (net_addr_equal(&addr,&debug_filters[i]))					break;			if (i == debug_filter_num) {				log(LOG_ERR, 0, "No filter to delete\n");				old = -1;			} else {				old = --debug_filter_num;				debug_filters[i] = debug_filters[old];			}		}	}/****  Don't tell...	 Ack the application with the old debug value	if (write(fd, (char *) &old, sizeof(old)) == -1)		log(LOG_ERR, errno, "write() error\n");****/	FD_CLR(fd, &fds);	(void) close(fd);	return (0);}		/* * test_alive(): Check whether daemon is already running, return its pid; *	else create file containing my pid and return 0. */inttest_alive(){	int pid;	FILE *fp;	struct sockaddr_in addr;	NET_SOCKADDR_UDP_IPv4(&addr,inaddr_any,RSVP_LOCK_PORT);	lockfd = socket(AF_INET,SOCK_DGRAM,PF_UNSPEC);	if (FAILED(lockfd))		return(-1);	if (!FAILED(bind(lockfd,(struct sockaddr *) &addr,sizeof(addr))))		return(0);	close(lockfd);	if ((fp = fopen(RSVP_PID_FILE, "r")) == NULL)		return(-1);	if (fscanf(fp, "%u", &pid) != 1)		return(-1);	(void) fclose(fp);	return (pid);}intsave_pid() {	FILE *fp;	int pid = getpid();	if (unlink(RSVP_PID_FILE) == -1) {		if (errno != ENOENT)			perror("unlink");	}	if ((fp = fopen(RSVP_PID_FILE, "w")) == NULL) {		perror("fopen");		return (-1);	}	fprintf(fp, "%u\n", pid);	(void) fclose(fp);	return (0);}net_addr *get_if(int index){	net_addr *addr;	net_if *inf;	inf = &GET_IF(index);	switch(NET_GET_TYPE(inf)) {		case NET_IF_PHY:			addr = &NET_GET_IF_PHY_ADDR(inf);			break;		case NET_IF_VIF:			addr = &NET_GET_IF_VIF_ADDR(inf);			break;		default:			return(NULL);	}	return(addr);}/*	Map IP address into local interface number, or -1 */intmap_if_addr(net_addr *addr)	{	int	i;	net_addr *addr2;	net_if *inf;	addr2 = net_addr_ip(addr);	for (i = 0; i < if_num; i++) {		if (IsNumAPI(i))			continue;		if (IF_DOWN(i))			continue;		inf = &GET_IF(i);		if (NET_GET_TYPE(inf) != NET_IF_PHY)			continue;		if (net_addr_equal(addr2,&NET_GET_IF_PHY_ADDR(inf)))			return(i);	}	return(-1);}/*	Map kernel if number into local interface number, or -1 */intmap_if_id(int id)	{	int	i;	net_if *inf;	for (i = 0; i < if_num; i++) {		if (IsNumAPI(i))			continue;		if (IF_DOWN(i))			continue;		inf = &GET_IF(i);		if (NET_GET_TYPE(inf) != NET_IF_PHY)			continue;		if (id == NET_GET_IF_PHY_ID(inf))			return(i);	}	return(-1);}/*	Map kernel vif number into local interface number, or -1 */intmap_if_vif(int vif)	{	int	i;	net_if *inf;	for (i = 0; i < if_num; i++) {		if (IsNumAPI(i))			continue;		if (IF_DOWN(i))			continue;		inf = &GET_IF(i);		if (NET_GET_TYPE(inf) != NET_IF_VIF)			continue;		if (vif == NET_GET_IF_VIF_ID(inf))			return(i);	}	return(-1);}voidprint_ifs()	{	int i;	char  flgs[20];        log(LOG_ALWAYS, 0, "Physical, Virtual, and API interfaces are:\n");	for (i=0; i < if_num; i++) {		flgs[0] = '\0';		if (IF_DOWN(i))			strcat(flgs, "DOWN ");		else if (if_vec[i].if_up)			strcat(flgs, "TCup ");		else			strcat(flgs, "NoIS ");		if (if_vec[i].if_flags & IF_FLAG_UseUDP)			strcat(flgs, "UDP ");		if (if_vec[i].if_flags & IF_FLAG_Intgrty)			strcat(flgs, "Intgry ");		if (if_vec[i].if_flags & IF_FLAG_Police)			strcat(flgs, "Police ");			if (if_vec[i].if_flags & IF_FLAG_IFF_MC)			strcat(flgs, "M");	 		if (if_vec[i].if_flags & IF_FLAG_Disable)			strcpy(flgs, "RSVP Disabled!");        	log(LOG_ALWAYS, 0, "%2d %-12.12s %38.38s/%-3d %s\n", i,			if_vec[i].if_name, net_if_print(&GET_IF(i)),			if_vec[i].prefix, flgs);	}}/*	Read and parse a configuration file. */voidread_config_file(char *fname)        {        FILE            *fd;	char		 cfig_line[80], word[64];	char		*cp;        fd = fopen(fname, "r");        if (!fd) {                if (errno != ENOENT)                        log(LOG_ERR, 0, "Cannot open config file\n");                return;        }        while (fgets(cfig_line, sizeof(cfig_line), fd)) {		cp = cfig_line;		if (*cp == '#' || !next_word(&cp, word))			continue;		cfig_do(&cp, word);		/*	Loop to parse keywords in line		 */		while (*cp != '\n' && *cp != '#'&& *cp != '\0') {		    next_word(&cp, word);		    cfig_do(&cp, word);		}		if (*cp == '\0') {		    log(LOG_ERR, 0, "Config file line 2 long: %s\n",cfig_line);			exit(1);		}	}	log(LOG_INFO, 0, "Used configuration file \n", fname);}			/* This does not do as much syntax-checking as one might like... */				    		voidcfig_do(char **cpp, char *word)	{	Cfig_element	*cep, *tcep;	char		parm1[64], parm2[64];	for (cep = Cfig_Table; cep->cfig_keywd; cep++)		if (!pfxcmp(word, cep->cfig_keywd))			break;	if (!cep->cfig_keywd) {		log(LOG_ERR, 0, "Unknown config verb: %s\n", word);		exit(1);	}	for (tcep = cep+1; tcep->cfig_keywd; tcep++)		if (!pfxcmp(word, tcep->cfig_keywd)) {			log(LOG_ERR, 0, "Ambiguous keyword: %s\n", word);			exit(1);		}	if (cep->cfig_flags & (CFF_HASPARM|CFF_HASPARM2)) {		if (!next_word(cpp, parm1)) {			log(LOG_ERR, 0, "Missing parm for %s\n", word);			exit(1);		}	}	if (cep->cfig_flags & CFF_HASPARM2) {		if (!next_word(cpp, parm2)) {			log(LOG_ERR, 0, "Missing parm for %s\n", word);			exit(1);		}	}	cfig_action(cep->cfig_actno, parm1, parm2);}voidcfig_action(int act, char *parm1, char *parm2)	{	int i;	KEY_ASSOC *kap;	switch (act) {		case CfAct_iface:		for (i = 0; i < if_num; i++) {			if (!strcmp(parm1, if_vec[i].if_name))				break;		}		if (i >= if_num) {			log(LOG_ERR, 0, "Unknown interface %s\n", parm1);			exit(1);		}		Cfig_if = i;		break;	case CfAct_police:		if_vec[Cfig_if].if_flags |= IF_FLAG_Police;		break;		case CfAct_udp:		if_vec[Cfig_if].if_flags |= IF_FLAG_UseUDP;		break;		case CfAct_udpttl:		if_vec[Cfig_if].if_flags |= IF_FLAG_UseUDP;		if_vec[Cfig_if].if_udpttl = atoi(parm1);		break;			case CfAct_intgr:		/* integrity  -- require INTEGRITY object in message		 *		received through this interface		 */		if_vec[Cfig_if].if_flags |= IF_FLAG_Intgrty;		break;	case CfAct_disable:		if_vec[Cfig_if].if_flags |= IF_FLAG_Disable;		log(LOG_WARNING, 0, "disable config parm not yet supported\n");		break;	case CfAct_sndttl:		/* Set default TTL for Resv messages, etc. */		log(LOG_WARNING, 0, "sndttl config parm not yet supported\n");		break;	case CfAct_sendkey:		/* sendkey <hex: keyid> <hex: key>  --		 *		keyid and key for sending message to this		 *		interface.		 */		kap = load_key(parm1, parm2);		if (!kap)			exit(1);		kap->kas_if = Cfig_if;		break;	case CfAct_recvkey:		/* recvkey <hex: keyid> <hex: key>  --		 *		keyid and key for receiving message		 */		kap = load_key(parm1, parm2);		if (!kap)			exit(1);		kap->kas_if = -1;		break;		case CfAct_refresh:		log(LOG_WARNING, 0, "refresh config parm not yet supported\n");		if_vec[Cfig_if].if_Rdefault = atoi(parm1);		break;	default:		assert(0);	}}#ifndef RTAP/* * Skip leading blanks, then copy next word (delimited by blank or zero, but * no longer than 63 bytes) into buffer b, set scan pointer to following  * non-blank (or end of string), and return 1.  If there is no non-blank text, * set scan ptr to point to 0 byte and return 0. */int next_word(char **cpp, char *b){	char           *tp;	int		L;	*cpp += strspn(*cpp, " \t");	if (**cpp == '\0' || **cpp == '\n' || **cpp == '#')		return(0);	tp = strpbrk(*cpp, " \t\n#");	L = MIN((tp)?(tp-*cpp):strlen(*cpp), 63);	strncpy(b, *cpp, L);	*(b + L) = '\0';	*cpp += L;	*cpp += strspn(*cpp, " \t");	return (1);}/* * Prefix string comparison: Return 0 if s1 string is prefix of s2 string, 1 * otherwise. */int pfxcmp(s1, s2)	register char  *s1, *s2;{	while (*s1)		if (*s1++ != *s2++)			return (1);	return (0);}#endif#ifndef SECURITYKEY_ASSOC *load_key(char *keyidstr, char *keystr)	{	log(LOG_ERR, 0, "Keys unsupported\n");	return(NULL);}#endifvoidntoh_rapi_cmd(rapi_cmd_t *cmd) {}

⌨️ 快捷键说明

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