17
mbping
- #include "mbping.h"
- #include "mbping_kbd.h"
- #include "mb_api.h"
- #include "mbdef.h"
- #include "ctk/dump.h"
- #include <sys/types.h>
- #include <sys/socket.h>
- #if defined( LINUX ) || defined(HPUX)
- #include <sys/time.h>
- #include <unistd.h>
- #else
- #include <sys/select.h>
- #endif
- #include <stdlib.h>
- #include <string.h>
- #include <netdb.h>
- #include <errno.h>
- #ifndef MIN
- #define MIN(x, y) ((x) < (y) ? (x) : (y))
- #endif
- #ifndef MAX
- #define MAX(x, y) ((x) < (y) ? (y) : (x))
- #endif
- struct mbping mbping;
- void mbping_unbind( ) {
- if ( mbping.mb_socket != -1 ) {
- mb_unbind( mbping.mb_socket );
- mbping.mb_socket = -1;
- }
- }
- void mbping_bind( char *host ) {
- int ret;
- mbping_unbind( );
- ret = mb_bind( host, mbping.port_name );
- switch ( ret ) {
- case -3:
- fprintf( stderr, "Host not found\n" );
- break;
- case -2:
- fprintf( stderr, "Service not found\n" );
- break;
- case -1:
- fprintf( stderr, "%s\n", strerror( errno ) );
- break;
- default:
- mbping.mb_socket = ret;
- break;
- }
- }
- void mbping_exit( int status ) {
- mbping_unbind( );
- exit( status );
- }
- void mbping_receive_data( int socket ) {
- int msg_id, msg_len;
- u_char *msg;
- int msg_type = mb_receive( socket, &msg_id, &msg_len, &msg );
- switch( msg_type ) {
- case MB_ONEWAY:
- case MB_MULTIWAY:
- fprintf( stdout, "\n[%i]<%i>\n", msg_id, msg_len );
- dump( msg, MIN( msg_len, mbping.dump_size ), stdout );
- free( msg );
- break;
- case MB_TWOWAY:
- mb_reply( socket, msg_id, msg_len, msg );
- fprintf( stdout, "\n[%i]<%i>\n", msg_id, msg_len );
- dump( msg, MIN( msg_len, mbping.dump_size ), stdout );
- free( msg );
- break;
- case MB_REPLY:
- if ( msg_len > 0 ) {
- fprintf( stdout, "\n[%i]<%i>\n", msg_id, msg_len );
- dump( msg, MIN( msg_len, mbping.dump_size ), stdout );
- free( msg );
- }
- else
- fprintf( stdout, "\n[%i]SENT\n", msg_id );
- break;
- case MB_NOBODY:
- fprintf( stdout, "\n[%i]NOBODY\n", msg_id );
- break;
- case MB_TIMEOUT:
- fprintf( stdout, "\n[%i]TIMEOUT\n", msg_id );
- break;
- case MB_REGISTER:
- fprintf( stdout, "\n%s\n", msg );
- break;
- case MB_PROBE:
- fprintf( stdout, "\n%s\n", msg );
- break;
- default:
- fprintf( stderr, "\n%s?\n", mbping.port_name );
- mbping.mb_socket = -1;
- break;
- }
- }
- void mbping_register_as( char *app_name, int sync ) {
- int n;
- if ( mbping.mb_socket != -1 ) {
- if ( (n = mb_register( mbping.mb_socket, ++mbping.mb_msg_id, app_name, sync )) < 0 )
- mbping.mb_socket = -1;
- else {
- if ( sync )
- fprintf( stdout, "%i\n", n );
- }
- }
- else
- fprintf( stderr, "Not connected\n" );
- }
- void mbping_probe( char *app_name, int sync ) {
- int n;
- if ( mbping.mb_socket != -1 ) {
- if ( (n = mb_probe( mbping.mb_socket, ++mbping.mb_msg_id, app_name, sync )) < 0 )
- mbping.mb_socket = -1;
- else {
- if ( sync )
- fprintf( stdout, "%i\n", n );
- }
- }
- else
- fprintf( stderr, "Not connected\n" );
- }
- void mbping_send_data( char *peer_name, u_int len, u_char *msg, int mode ) {
- if ( mbping.mb_socket != -1 ) {
- if ( mb_send( mbping.mb_socket, peer_name, mode, ++mbping.mb_msg_id, len, msg ) < 0 )
- mbping.mb_socket = -1;
- }
- else
- fprintf( stderr, "Not connected\n" );
- }
- static void startup( ) {
- mbping.kbd_shell = shell_new( );
- shell_addcmd( mbping.kbd_shell, "bind", "bind [host]", kbd_bind);
- shell_addcmd( mbping.kbd_shell, "register", "register app_name [sync]", kbd_register_as);
- shell_addcmd( mbping.kbd_shell, "probe", "probe app_name [sync]", kbd_probe);
- shell_addcmd( mbping.kbd_shell, "1", "1 app_name msg", kbd_oneway);
- shell_addcmd( mbping.kbd_shell, "2", "2 app_name msg", kbd_twoway);
- shell_addcmd( mbping.kbd_shell, "send", "send app_name count char", kbd_send);
- shell_addcmd( mbping.kbd_shell, "msend", "msend app_name count char", kbd_multiway);
- shell_addcmd( mbping.kbd_shell, "dumpsize", "dumpsize [n_octets]", kbd_dumpsize);
- shell_addcmd( mbping.kbd_shell, "pagesize", "pagesize [n_octets]", kbd_pagesize);
- shell_addcmd( mbping.kbd_shell, "sleep", "sleep nsecs", kbd_sleep);
- shell_addcmd( mbping.kbd_shell, "unbind", "unbind", kbd_unbind);
- shell_addcmd( mbping.kbd_shell, "exit", "exit | quit", kbd_exit);
- shell_addcmd( mbping.kbd_shell, "quit", "quit | exit", kbd_exit);
- fprintf( stdout, "MagicBus Ping %s(%s)\n", MB_VERSION, MB_REVISION);
- fprintf( stdout, "(C)2010 frasq.org.\n" );
- fprintf( stdout, "%s", mbping.kbd_prompt );
- fflush( stdout );
- }
- static void main_loop( ) {
- fd_set read_fds;
- int n_fds = FD_SETSIZE;
- for ( ;; ) { /* forever */
- FD_ZERO( &read_fds );
- FD_SET( 0, &read_fds );
- if ( mbping.mb_socket != -1 )
- FD_SET( mbping.mb_socket, &read_fds );
- switch ( select( n_fds, &read_fds, 0, 0, 0 )) {
- case -1: /* trouble */
- if ( errno != EINTR )
- mbping_exit( errno );
- break;
- case 0: /* time out */
- break;
- default: /* event */
- if ( FD_ISSET( 0, &read_fds ))
- shell_input( mbping.kbd_shell, mbping.kbd_prompt ); /* from operator */
- if ( mbping.mb_socket != -1 && FD_ISSET( mbping.mb_socket, &read_fds ))
- mbping_receive_data( mbping.mb_socket );
- break;
- }
- }
- }
- int main( int argc, char **argv ) {
- extern int opterr;
- int c;
- mbping.kbd_prompt = MBPING_PROMPT;
- mbping.port_name = MB_INET;
- mbping.mb_socket = -1;
- mbping.dump_size = 32;
- opterr = 0;
- while ( (c = getopt( argc, argv, "P:d:" )) != -1 )
- switch ( c ) {
- case 'P':
- mbping.kbd_prompt = optarg;
- break;
- case 'd': {
- int dump_size = atoi( optarg );
- if ( dump_size >= 16 )
- mbping.dump_size = dump_size;
- break;
- }
- case '?':
- default:
- error:
- fprintf( stderr, "%s [-P prompt] [-d dump_size] [port_name]\n", argv[ 0 ] );
- exit( 1 );
- }
- switch ( argc - optind ) {
- case 0:
- break;
- case 1:
- mbping.port_name = argv[ optind ];
- break;
- default:
- goto error;
- }
- startup( );
- main_loop( );
- }
- #ifndef _MBPING_H
- #define _MBPING_H
- #include <sys/types.h>
- #include "ctk/shell.h"
- #define MBPING_PROMPT "ping> "
- struct mbping {
- shell kbd_shell;
- char * kbd_prompt;
- char * port_name;
- int mb_socket;
- u_int mb_msg_id;
- int dump_size;
- };
- extern struct mbping mbping;
- #ifdef __STDC__
- extern void mbping_unbind( );
- extern void mbping_bind( char *host );
- extern void mbping_exit( int status );
- extern void mbping_receive_data( int socket );
- extern void mbping_register_as( char *app_name, int sync );
- extern void mbping_probe( char *app_name, int sync );
- extern void mbping_send_data( char *peer_name, u_int len, u_char *msg, int mode );
- #else
- extern void mbping_unbind();
- extern void mbping_bind();
- extern void mbping_exit();
- extern void mbping_receive_data();
- extern void mbping_register_as();
- extern void mbping_probe();
- extern void mbping_send_data();
- #endif
- #endif
Commentaires