?? voudpconfig.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 "voudpconfig_msg.h"#include "sender.h"#include "die.h"#include <string.h>#include <sys/socket.h>#include <sys/un.h>#include <unistd.h>#include <err.h>#include <stdio.h>staticvoid usage( int status ){ FILE *out = ( status == EXIT_SUCCESS ) ? stdout : stderr; fprintf( out,"Usage: voudpconfig -h Print this message to stdout\n"" voudpconfig -a Show all recipients\n"" voudpconfig diagnostics Print diagnostic info to the log\n"" voudpconfig <name> Show info on the recipient <name>\n"" voudpconfig <name> {start|stop}\n"" Start or stop sending to <name>\n"" voudpconfig <name> delete\n"" Delete recipient <name> from the db\n"" voudpconfig <name> [max_packet_len l] [max_buf_ms n] [host h] [port p]\n"" Set info on <name>\n" ); exit( status );}staticvoid boom() { exit( EXIT_FAILURE ); }staticint voudpd_socket(){ int sock = socket( PF_LOCAL, SOCK_STREAM, 0 ); if ( sock == -1 ) die( "socket" ); 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 == connect( sock, (struct sockaddr*) &addr, sizeof( addr ))) die( "connect" ); return sock;}staticvoid my_close( int fd ){ if ( -1 == close( fd )) die( "close fd" );}staticvoid print_record( const std::string &name, const recipient_info_t &info, const bool &sending ){ printf( "%s:\n" " host/port: %s:%d\n" " maximum packet bytes: %lu\n" " maximum packet ms: %lu\n" " sending: %s\n", name.c_str(), info.host.c_str(), info.port, (unsigned long) info.max_packet_len, (unsigned long) info.max_buf_ms, sending ? "yes" : "no" );}staticbool get_info( const std::string &name, recipient_info_t &info, bool &sending ){ int fd = voudpd_socket(); if ( ! send_req_code( fd, voudpconfig_msg::GET_INFO )) boom(); if ( ! send_name( fd, name )) boom(); voudpconfig_msg::resp_code_t resp; if ( ! decode_resp_code( fd, resp )) boom(); if ( resp != voudpconfig_msg::FOUND ) { my_close( fd ); return false; } if ( ! decode_info( fd, info )) boom(); if ( ! decode_bool( fd, sending )) boom(); my_close( fd ); return true;}staticvoid set_info( const std::string &name, const recipient_info_t &info ){ int fd = voudpd_socket(); if ( ! send_req_code( fd, voudpconfig_msg::SET_INFO )) boom(); if ( ! send_name( fd, name )) boom(); if ( ! send_info( fd, info )) boom(); voudpconfig_msg::resp_code_t resp; if ( ! decode_resp_code( fd, resp )) boom(); if ( resp != voudpconfig_msg::OK ) errx( EXIT_FAILURE, "Couldn't set information" ); my_close( fd );}int main( int argc, char **argv ){ if ( argc == 2 && 0 == strcmp( argv[1], "-h" )) usage( EXIT_SUCCESS ); if ( argc == 2 && 0 == strcmp( argv[1], "-a" )) { int fd = voudpd_socket(); if ( ! send_req_code( fd, voudpconfig_msg::SHOW_ALL )) boom(); voudpconfig_msg::resp_code_t resp; if ( ! decode_resp_code( fd, resp )) boom(); if ( resp != voudpconfig_msg::OK ) errx( EXIT_FAILURE, "Couldn't fetch records" ); std::string name; recipient_info_t info; bool sending; while ( decode_name( fd, name ) && decode_info( fd, info ) && decode_bool( fd, sending )) print_record( name, info, sending ); my_close( fd ); exit( EXIT_SUCCESS ); } if ( argc == 2 && 0 == strcmp( argv[1], "diagnostics" )) { int fd = voudpd_socket(); if ( ! send_req_code( fd, voudpconfig_msg::PRINT_DIAGNOSTICS )) boom(); voudpconfig_msg::resp_code_t resp; if ( ! decode_resp_code( fd, resp )) boom(); if ( resp != voudpconfig_msg::OK ) errx( EXIT_FAILURE, "Couldn't print diagnostic info" ); my_close( fd ); exit( EXIT_SUCCESS ); } if ( argc == 2 ) { recipient_info_t info; bool sending; if ( ! get_info( argv[1], info, sending )) errx( EXIT_FAILURE, "No such user %s", argv[1]); print_record( argv[1], info, sending ); exit( EXIT_SUCCESS ); } if ( argc == 3 ) { voudpconfig_msg::req_code_t code; if ( ! strcmp( argv[ 2 ], "start" )) code = voudpconfig_msg::START; else if ( ! strcmp( argv[ 2 ], "stop" )) code = voudpconfig_msg::STOP; else if ( ! strcmp( argv[ 2 ], "delete" )) code = voudpconfig_msg::DELETE; else usage( EXIT_FAILURE ); std::string name = argv[ 1 ]; int fd = voudpd_socket(); if ( ! send_req_code( fd, code )) boom(); if ( ! send_name( fd, name )) boom(); voudpconfig_msg::resp_code_t resp; if ( ! decode_resp_code( fd, resp )) boom(); if ( resp != voudpconfig_msg::OK ) errx( EXIT_FAILURE, "No such user %s", name.c_str()); my_close( fd ); exit( EXIT_SUCCESS ); } if ( argc > 2 && ( argc % 2 == 0 )) { // // I want ocaml option types here. Sigh. // Of course, there *is* a boost.org C++ version of ocaml options.... // bool has_mpl = false; bool has_mbm = false; bool has_host = false; bool has_port = false; size_t max_packet_len; size_t max_buf_ms; std::string host; int port; std::string name = argv[ 1 ]; for ( int i = 2; i < argc; i += 2 ) { if ( ! strcmp( "max_packet_len", argv[ i ])) { has_mpl = true; max_packet_len = (size_t) atol( argv[ i + 1 ]); continue; } if ( ! strcmp( "max_buf_ms", argv[ i ])) { has_mbm = true; max_buf_ms = (size_t) atol( argv[ i + 1 ]); continue; } if ( ! strcmp( "host", argv[ i ])) { has_host = true; host = argv[ i + 1 ]; continue; } if ( ! strcmp( "port", argv[ i ])) { has_port = true; port = atoi( argv[ i + 1 ]); continue; } } recipient_info_t info; bool dummy; if ( ! get_info( name, info, dummy )) { if ( ! ( has_host && has_port && has_mpl && has_mbm )) { errx( EXIT_FAILURE, "New recipient must have all fields specified" ); } } if ( has_host ) info.host = host; if ( has_port ) info.port = port; if ( has_mpl ) info.max_packet_len = max_packet_len; if ( has_mbm ) info.max_buf_ms = max_buf_ms; set_info( name, info ); exit( EXIT_SUCCESS ); } usage( EXIT_FAILURE ); assert( NULL == "not reachable" ); return 0;}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -