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

📄 extcmd_funcs.c

📁 用来作为linux中SIP SERVER,完成VOIP网络电话中服务器的功能
💻 C
📖 第 1 页 / 共 2 页
字号:
	/* get to len */	len = get_int_n2h( p, 4);	p = p + 4;	if ( p+len>end) {		LOG(L_ERR,"ERROR:extcmd:send_sip_req: TO size to big (%d)\n",len);		push_reply_to_client(400,"extcmd: invalid TO header",25,client_fd);		goto error;	}	/* get to body */	to.s = p;	to.len = len;	p += len;	DBG("DEBUG:extcmd:send_sip_req: to=%d<%.*s>\n",to.len,to.len,to.s);	/* get headers len */	len = get_int_n2h( p, 4);	p = p + 4;	if ( p+len>end) {		LOG(L_ERR,"ERROR:extcmd:send_sip_req: HDRS size to big (%d)\n",len);		push_reply_to_client(400,"extcmd: invalid HEADERS field",29,client_fd);		goto error;	}	/* get headers body */	hdrs.s = p;	hdrs.len = len;	p += len;	DBG("DEBUG:extcmd:send_sip_req: hdrs=%d<%.*s>\n",hdrs.len,hdrs.len,hdrs.s);	/* get body len */	len = get_int_n2h( p, 4);	p = p + 4;	if ( p+len>end) {		LOG(L_ERR,"ERROR:extcmd:send_sip_req: BODY size to big (%d)\n",len);		push_reply_to_client(400,"extcmd: invalid BODY field",26,client_fd);		goto error;	}	/* get body's body */	body.s = p;	body.len = len;	DBG("DEBUG:extcmd:send_sip_req: body=%d<%.*s>\n",body.len,body.len,body.s);	/* allocate the param in shm */	if( !(pcbp = (int*)shm_malloc(sizeof(int*))) ) {		LOG(L_ERR,"ERROR:extcmd:send_sip_req: no more shm mem free!!\n");		push_reply_to_client(500,"extcmd: no shm memory free",26,client_fd);		goto error;	}	*pcbp = client_fd;	/* send the message */	ret = tmb.t_request(  msg_type,   /* request type */			0,                      /* Request-URI */			&to,                      /* To */			&from,                    /* From */			&hdrs,                    /* Additional headers including CRLF */			&body,                    /* Message body */			tuac_callback,           /* Callback function */			(void*)pcbp               /* Callback parameter */		);	if (ret<=0) {		err_ret=err2reason_phrase(ret, &sip_error, err_buf,				sizeof(err_buf), "EXTCMD" ) ;		if (err_ret > 0 )		{			push_reply_to_client( sip_error, err_buf, err_ret, client_fd);		} else {			push_reply_to_client(500,"EXTCMD unknown error\n",20,client_fd);		}	}	return 1;error:	return -1;}inline int forward_reply_to_client(int pipe_fd){	anchor_t anchor;	anchor.cmd.s = 0;	anchor.cmd.len = 0;	/* get cmd from pipe */	if (read(pipe_fd,&anchor,sizeof(anchor))!=sizeof(anchor) ) {		LOG(L_ERR,"ERROR:extcmd:send_cmd_from_pipe_to_client: cannot read from"			" pipe: %s \n", strerror(errno));		goto error;	}	/* send the reply */	if (write( anchor.fd, anchor.cmd.s, anchor.cmd.len)!=anchor.cmd.len ) {		LOG(L_ERR,"ERROR:extcmd:send_cmd_from_pipe_to_client: cannot write to "			"socket: %s \n", strerror(errno));		goto error;	}	/* free the mem */	shm_free( anchor.cmd.s );	return 1;error:	if (anchor.cmd.s)		shm_free( anchor.cmd.s );	return -1;}static int forward_request_to_all_clients(int pipe_fd){	anchor_t anchor;	client_t *client;	int fd;	int i;	anchor.cmd.s = 0;	anchor.cmd.len = 0;	/* get request cmd from pipe */	if (read(pipe_fd,&anchor,sizeof(anchor))!=sizeof(anchor) ) {		LOG(L_ERR,"ERROR:extcmd:send_cmd_from_pipe_to_client: cannot read from"			" pipe: %s \n", strerror(errno));		goto error;	}	/* send the reply to all clients */	for(i=0; i<get_nr_clients(); i++) {		client = get_client( i );		fd = client->fd;		if (write( fd, anchor.cmd.s, anchor.cmd.len)!=anchor.cmd.len ) {			LOG(L_ERR,"ERROR:extcmd:forward_request_to_all_client: cannot "				"write to socket: %s \n", strerror(errno));			continue;		}	}	/* free the mem */	shm_free( anchor.cmd.s );	return 1;error:	if (anchor.cmd.s)		shm_free( anchor.cmd.s );	return -1;}inline int read_n( int fd, int n, char *b){	int l;	int c;	l=0;	c=0;	while( l<n && (c=read(fd,b+l,n))>0 )		l += c;	if (c<0)		return c;	return l;}static int get_cmd( int fd, str *cmd){	static char buffer[BUFFER_SIZE];	int cmd_type;	int cmd_len;	int  len;	/* get command header */	len = read_n( fd, HEADER_SIZE , buffer);	if (len<HEADER_SIZE) {		LOG(L_ERR,"ERROR:extcmd:get_cmd: cannot read command's header: %s \n",			len<0?strerror(len):"incomplete string");		return NO_CMD;	}	/* decode command header */	cmd_type = get_int_n2h( buffer, TYPE_LEN);	cmd_len = get_int_n2h( buffer+TYPE_LEN, LEN_LEN);	DBG("DEBUG:extcmd:get_cmd: type=%d, len=%d \n",cmd_type, cmd_len);	/* read the command body */	len = read_n( fd, cmd_len , buffer);	if (len<cmd_len) {		LOG(L_ERR,"ERROR:extcmd:get_cmd: cannot read command's body: %s \n",			len<0?strerror(len):"incomplete string");		return NO_CMD;	}	DBG("DEBUG:extcmd:get_cmd: command = [%.*s]\n",cmd_len,buffer);	/* return the command body */	cmd->s = buffer;	cmd->len = cmd_len;	return cmd_type;}#if 0static int send_void_command( int sock_fd ){	char buf[HEADER_SIZE];	/* add command type */	put_int_h2n( VOID_CMD, buf, 1);	/* add the cmd len */	put_int_h2n( 0, buf+1, 4);	/* send the reply */	if (write( sock_fd, buf, HEADER_SIZE)!=HEADER_SIZE ) {		LOG(L_ERR,"ERROR:extcmd:send_void_commnad: cannot write to "			"socket: %s \n", strerror(errno));		return -1;	}	return 1;}#endifinline void FD_SET_AND_MAX( fd_set *fdset, int fd, int *max_fd){	FD_SET( fd, fdset );	if (fd>*max_fd)		*max_fd = fd;}inline void FD_CLR_AND_MAX( fd_set *fdset, int fd, int *max_fd){	int i;	int new_max = 0;	FD_CLR( fd, fdset );	if (fd==*max_fd) {		for(i=0;i<=*max_fd;i++)			if ( FD_ISSET(i,fdset) && i>new_max )				new_max = i;		*max_fd = new_max;	}}void extcmd_server_process( int server_sock ){	str message_req = { "MESSAGE", 7};	//str invite_req = { "INVITE", 6};	fd_set read_set;	fd_set wait_set;	client_t *client;	union sockaddr_union sau;	int sau_len;	str cmd;	int cmd_type;	int max_fd;	int fd = -1;	int index;	/* set SIG_PIPE to be ignored by this process */	if (signal( SIGPIPE, SIG_IGN)==SIG_ERR) {		LOG(L_ERR,"ERROR:extcmd_server_process: cannot set SIGPIPE to be "			"ignored! \n");		goto error;	}	/* set the socket for listening */	if ( listen( server_sock, 10)==-1 ) {		LOG(L_ERR,"ERROR:extcmd_server_process: cannot listen to the server"			" socket: %s \n", strerror(errno) );		goto error;	}	sau_len = sizeof( union sockaddr_union );	/* prepare fd set for select */	FD_ZERO( &wait_set );	max_fd = 0;	FD_SET_AND_MAX( &wait_set, server_sock, &max_fd);	FD_SET_AND_MAX( &wait_set, req_pipe[0], &max_fd);	FD_SET_AND_MAX( &wait_set, rpl_pipe[0], &max_fd);	while(1) {		/* what fd should we listen to? */		read_set = wait_set;		if (clients_is_full())			FD_CLR_AND_MAX( &read_set, server_sock, &max_fd);		/* wait for read something */		DBG("DEBUG:extcmd_server_process: waiting to read!\n");		if (select( max_fd+1, &read_set, 0, 0, 0)==-1) {			LOG(L_ERR,"ERROR:extcmd_server_process: select failed : %s\n",				strerror(errno) );			sleep(1);			continue;		}		/* let's see what we read */		/* maybe is a connect request !?*/		if ( FD_ISSET(server_sock, &read_set) ) {			fd=accept(server_sock,(struct sockaddr*)&sau,(socklen_t*)&sau_len);			if (fd==-1) {				LOG(L_ERR,"ERROR: extcmd_server_process: accept failed : %s\n",					strerror(errno) );			} else {				DBG("DEBUG:extcmd_server_process: connection accepted \n");				add_client(fd);				/* add this socket to wait_sock to read from it */				FD_SET_AND_MAX( &wait_set, fd, &max_fd);			}		}		/* maybe is a from req_pipe ?! */		if ( FD_ISSET(req_pipe[0], &read_set) ) {			forward_request_to_all_clients( req_pipe[0] );		}		/* maybe is a SIP reply from rpl_pipe ?! */		if ( FD_ISSET(rpl_pipe[0], &read_set) ) {			forward_reply_to_client( rpl_pipe[0] );		}		/* maybe is from a client -> look for it's fd */		for( fd=-1,index=0 ; index<get_nr_clients() ; index++ ) {			client = get_client(index);			if ( !client || !FD_ISSET(client->fd,&read_set) )				continue;			/* we can read something from this client */			fd = client->fd;			/* get the command from the client */			cmd_type = get_cmd(fd,&cmd);			switch (cmd_type) {				/* NO commnad - nothing was read - probably disconnection */				case NO_CMD :				/* BYE command - used by the client to end the connection */				case BYE_CMD:					DBG("DEBUG:extcmd_server_process: BYE received->close\n");					/* remove client's fd from wait_sock set */					FD_CLR_AND_MAX( &wait_set, fd, &max_fd);					/* remove client */					del_client(index);					index--;					/* close the connection */					close(fd);					break;				/* MSG commnad - client wants to send a SIP message request */				case MSG_CMD:					DBG("DEBUG:extcmd_server_process: MSG received\n");					/* send the message */					send_sip_req( &message_req, &cmd, fd);					break;				/* unknown commnad was received - ignore it */				default:					LOG(L_ERR,"ERROR:extcmd_server_process: unknown command "					"[%d] received from client (fd=%d)\n",cmd_type,fd);			} /* end switch */		} /* end for */	}error:	return;}

⌨️ 快捷键说明

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