📄 voudpconfigd.cpp
字号:
//// Copyright 2004 Alan Post//// This file is part of voudp.//// voudp is free software; you can redistribute it and/or modify it under the// terms of the GNU General Public License as published by the Free Software// Foundation; either version 2 of the License, or (at your option) any later// version.//// voudp is distributed in the hope that it will be useful, but WITHOUT ANY// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR// A PARTICULAR PURPOSE. See the GNU General Public License for more details.//// You should have received a copy of the GNU General Public License along with// voudp; if not, write to the Free Software Foundation, Inc., 59 Temple Place,// Suite 330, Boston, MA 02111-1307 USA//#include "voudpconfigd.h"#include "voudpconfig_msg.h"#include "sender.h"#include "receiver.h"#include "constants.h"#include "die.h"#include <sys/un.h>#include <fcntl.h>#include <sys/socket.h>#include <string.h>#include <unistd.h>#include <stdio.h>#include <errno.h>void set_flags( int fd, int flags_to_set ){ int opts = fcntl( fd, F_GETFL ); if ( opts == -1 ) die( "fcntl f_GETFL" ); opts |= flags_to_set; if ( -1 == fcntl( fd, F_SETFL, opts )) die( "fcntl F_SETFL" );}void clear_flags( int fd, int flags_to_clear ){ int opts = fcntl( fd, F_GETFL ); if ( opts == -1 ) die( "fcntl f_GETFL" ); opts &= ~flags_to_clear; if ( -1 == fcntl( fd, F_SETFL, opts )) die( "fcntl F_SETFL" );}voudp_configd_t::voudp_configd_t(){ lfd = socket( PF_LOCAL, SOCK_STREAM, 0 ); set_flags( lfd, O_NONBLOCK ); if ( -1 == unlink( sock_path ) && errno != ENOENT ) die( "unlink sock" ); sockaddr_un addr; addr.sun_len = sizeof( addr ); addr.sun_family = PF_LOCAL; strlcpy( addr.sun_path, sock_path, sizeof( addr.sun_path )); if ( -1 == bind( lfd, (struct sockaddr*) &addr, sizeof( addr ))) die( "bind to local socket" ); if ( -1 == listen( lfd, 5 )) die( "listen" );}voudp_configd_t::~voudp_configd_t(){ if ( -1 == close( lfd )) die( "closing listen socket" );}intvoudp_configd_t::get_listen_fd() const { return lfd; }staticvoid do_show_all( int fd, Sender &sender ){ if ( ! send_resp_code( fd, voudpconfig_msg::OK )) return; for ( Sender::infos_t::const_iterator i = sender.infos_begin(); i != sender.infos_end(); ++i ) { if ( ! send_name( fd, (*i).first )) return; if ( ! send_info( fd, (*i).second )) return; bool sending = sender.is_recipient_active( (*i).first ); if ( ! send_bool( fd, sending )) return; }}staticvoid do_get_info( int fd, Sender &sender ){ std::string name; recipient_info_t info; if ( ! decode_name( fd, name )) return; if ( ! sender.get_recipient_info( name, info )) { send_resp_code( fd, voudpconfig_msg::NOT_FOUND ); return; } if ( ! send_resp_code( fd, voudpconfig_msg::FOUND )) return; if ( ! send_info( fd, info )) return; if ( ! send_bool( fd, sender.is_recipient_active( name ))) return;}staticvoid do_set_info( int fd, Sender &sender ){ std::string name; recipient_info_t info; if ( ! decode_name( fd, name )) return; if ( ! decode_info( fd, info )) return; sender.set_recipient_info( name, info ); send_resp_code( fd, voudpconfig_msg::OK );}staticvoid do_delete( int fd, Sender &sender ){ std::string name; if ( ! decode_name( fd, name )) return; sender.delete_recipient( name ); send_resp_code( fd, voudpconfig_msg::OK );}staticvoid do_start( int fd, Sender &sender ){ std::string name; if ( ! decode_name( fd, name )) return; voudpconfig_msg::resp_code_t code = sender.start_recipient( name ) ? voudpconfig_msg::OK : voudpconfig_msg::NOT_FOUND; send_resp_code( fd, code );}staticvoid do_stop( int fd, Sender &sender ){ std::string name; if ( ! decode_name( fd, name )) return; voudpconfig_msg::resp_code_t code = sender.stop_recipient( name ) ? voudpconfig_msg::OK : voudpconfig_msg::NOT_FOUND; send_resp_code( fd, code );}staticvoid do_print( int fd, Receiver &receiver ){ receiver.print_diagnostics(); send_resp_code( fd, voudpconfig_msg::OK );}staticvoid really_do_config_req( int fd, Sender &sender, Receiver &receiver ){ voudpconfig_msg::req_code_t code; if ( ! decode_req_code( fd, code )) return; switch( code ) { case voudpconfig_msg::SHOW_ALL: do_show_all( fd, sender ); break; case voudpconfig_msg::GET_INFO: do_get_info( fd, sender ); break; case voudpconfig_msg::SET_INFO: do_set_info( fd, sender ); break; case voudpconfig_msg::DELETE: do_delete( fd, sender ); break; case voudpconfig_msg::START: do_start( fd, sender ); break; case voudpconfig_msg::STOP: do_stop( fd, sender ); break; case voudpconfig_msg::PRINT_DIAGNOSTICS: do_print( fd, receiver ); break; }}voidvoudp_configd_t::do_config_req( Sender &sender, Receiver &receiver ){ int fd = accept( lfd, NULL, NULL ); if ( fd == -1 ) { perror( "accept" ); return; } clear_flags( fd, O_NONBLOCK ); really_do_config_req( fd, sender, receiver ); if ( -1 == close( fd )) { perror( "closing accept socket" ); return; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -