3

mbping

  1. #include "mbping.h"
  2. #include "mbping_kbd.h"
  3.  
  4. #include "mb_api.h"
  5.  
  6. #include "mbdef.h"
  7.  
  8. #include "ctk/dump.h"
  9.  
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12.  
  13. #if defined( LINUX ) || defined(HPUX)
  14. #include <sys/time.h>
  15. #include <unistd.h>
  16. #else
  17. #include <sys/select.h>
  18. #endif
  19.  
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #include <netdb.h>
  24.  
  25. #include <errno.h>
  26.  
  27. #ifndef MIN
  28. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  29. #endif
  30. #ifndef MAX
  31. #define MAX(x, y) ((x) < (y) ? (y) : (x))
  32. #endif
  33.  
  34. struct mbping mbping;
  35.  
  36. void mbping_unbind( ) {
  37.     if ( mbping.mb_socket != -1 ) {
  38.         mb_unbind( mbping.mb_socket );
  39.         mbping.mb_socket = -1;
  40.     }
  41. }
  42.  
  43. void mbping_bind( char *host ) {
  44.     int ret;
  45.  
  46.     mbping_unbind( );
  47.  
  48.     ret = mb_bind( host, mbping.port_name );
  49.     switch ( ret ) {
  50.     case -3:
  51.         fprintf( stderr, "Host not found\n" );
  52.         break;
  53.     case -2:
  54.         fprintf( stderr, "Service not found\n" );
  55.         break;
  56.     case -1:
  57.         fprintf( stderr, "%s\n", strerror( errno ) );
  58.         break;
  59.     default:
  60.         mbping.mb_socket = ret;
  61.         break;
  62.     }
  63. }
  64.  
  65. void mbping_exit( int status ) {
  66.     mbping_unbind( );
  67.     exit( status );
  68. }
  69.  
  70. void mbping_receive_data( int socket ) {
  71.     int msg_id, msg_len;
  72.     u_char *msg;
  73.  
  74.     int msg_type = mb_receive( socket, &msg_id, &msg_len, &msg );
  75.  
  76.     switch( msg_type ) {
  77.     case MB_ONEWAY:
  78.     case MB_MULTIWAY:
  79.         fprintf( stdout, "\n[%i]<%i>\n", msg_id, msg_len );
  80.         dump( msg, MIN( msg_len, mbping.dump_size ), stdout );
  81.         free( msg );
  82.         break;
  83.     case MB_TWOWAY:
  84.         mb_reply( socket, msg_id, msg_len, msg );
  85.         fprintf( stdout, "\n[%i]<%i>\n", msg_id, msg_len );
  86.         dump( msg, MIN( msg_len, mbping.dump_size ), stdout );
  87.         free( msg );
  88.         break;
  89.     case MB_REPLY:
  90.         if ( msg_len > 0 ) {
  91.             fprintf( stdout, "\n[%i]<%i>\n", msg_id, msg_len );
  92.             dump( msg, MIN( msg_len, mbping.dump_size ), stdout );
  93.             free( msg );
  94.         }
  95.         else
  96.             fprintf( stdout, "\n[%i]SENT\n", msg_id );
  97.         break;
  98.     case MB_NOBODY:
  99.         fprintf( stdout, "\n[%i]NOBODY\n", msg_id );
  100.         break;
  101.     case MB_TIMEOUT:
  102.         fprintf( stdout, "\n[%i]TIMEOUT\n", msg_id );
  103.         break;
  104.     case MB_REGISTER:
  105.         fprintf( stdout, "\n%s\n", msg );
  106.         break;
  107.     case MB_PROBE:
  108.         fprintf( stdout, "\n%s\n", msg );
  109.         break;
  110.     default:
  111.         fprintf( stderr, "\n%s?\n", mbping.port_name );
  112.         mbping.mb_socket = -1;
  113.         break;
  114.     }
  115. }
  116.  
  117. void mbping_register_as( char *app_name, int sync ) {
  118.     int n;
  119.  
  120.     if ( mbping.mb_socket != -1 ) {
  121.         if ( (n = mb_register( mbping.mb_socket, ++mbping.mb_msg_id, app_name, sync )) < 0 )
  122.             mbping.mb_socket = -1;
  123.         else {
  124.             if ( sync )
  125.                 fprintf( stdout, "%i\n", n );
  126.         }
  127.     }
  128.     else
  129.         fprintf( stderr, "Not connected\n" );
  130. }
  131.  
  132. void mbping_probe( char *app_name, int sync ) {
  133.     int n;
  134.  
  135.     if ( mbping.mb_socket != -1 ) {
  136.         if ( (n = mb_probe( mbping.mb_socket, ++mbping.mb_msg_id, app_name, sync )) < 0 )
  137.             mbping.mb_socket = -1;
  138.         else {
  139.             if ( sync )
  140.                 fprintf( stdout, "%i\n", n );
  141.         }
  142.     }
  143.     else
  144.         fprintf( stderr, "Not connected\n" );
  145. }
  146.  
  147. void mbping_send_data( char *peer_name, u_int len, u_char *msg, int mode ) {
  148.     if ( mbping.mb_socket != -1 ) {
  149.         if ( mb_send( mbping.mb_socket, peer_name, mode, ++mbping.mb_msg_id, len, msg ) < 0 )
  150.             mbping.mb_socket = -1;
  151.     }
  152.     else
  153.         fprintf( stderr, "Not connected\n" );
  154. }
  155.  
  156. static void startup( ) {
  157.     mbping.kbd_shell = shell_new( );
  158.  
  159.     shell_addcmd( mbping.kbd_shell, "bind",     "bind [host]",                  kbd_bind);
  160.     shell_addcmd( mbping.kbd_shell, "register", "register app_name [sync]",     kbd_register_as);
  161.     shell_addcmd( mbping.kbd_shell, "probe",    "probe app_name [sync]",        kbd_probe);
  162.     shell_addcmd( mbping.kbd_shell, "1",        "1 app_name msg",               kbd_oneway);
  163.     shell_addcmd( mbping.kbd_shell, "2",        "2 app_name msg",               kbd_twoway);
  164.     shell_addcmd( mbping.kbd_shell, "send",     "send app_name count char",     kbd_send);
  165.     shell_addcmd( mbping.kbd_shell, "msend",    "msend app_name count char",    kbd_multiway);
  166.     shell_addcmd( mbping.kbd_shell, "dumpsize", "dumpsize [n_octets]",          kbd_dumpsize);
  167.     shell_addcmd( mbping.kbd_shell, "pagesize", "pagesize [n_octets]",          kbd_pagesize);
  168.     shell_addcmd( mbping.kbd_shell, "sleep",    "sleep nsecs",              kbd_sleep);
  169.     shell_addcmd( mbping.kbd_shell, "unbind",   "unbind",                   kbd_unbind);
  170.     shell_addcmd( mbping.kbd_shell, "exit",     "exit | quit",              kbd_exit);
  171.     shell_addcmd( mbping.kbd_shell, "quit",     "quit | exit",              kbd_exit); 
  172.  
  173.     fprintf( stdout, "MagicBus Ping %s(%s)\n", MB_VERSION, MB_REVISION);
  174.     fprintf( stdout, "(C)2010 frasq.org.\n" );
  175.     fprintf( stdout, "%s", mbping.kbd_prompt );
  176.  
  177.     fflush( stdout );
  178. }
  179.  
  180. static void main_loop( ) {
  181.     fd_set read_fds;
  182.     int n_fds = FD_SETSIZE;
  183.  
  184.     for ( ;; ) {    /* forever */
  185.         FD_ZERO( &read_fds );
  186.  
  187.         FD_SET( 0, &read_fds );
  188.  
  189.         if ( mbping.mb_socket != -1 )
  190.             FD_SET( mbping.mb_socket, &read_fds );
  191.  
  192.         switch ( select( n_fds, &read_fds, 0, 0, 0 )) {
  193.         case -1:    /* trouble */
  194.             if ( errno != EINTR )
  195.                 mbping_exit( errno );
  196.             break;
  197.         case 0: /* time out */
  198.             break;
  199.         default:    /* event */
  200.             if ( FD_ISSET( 0, &read_fds ))
  201.                 shell_input( mbping.kbd_shell, mbping.kbd_prompt );     /* from operator */
  202.             if ( mbping.mb_socket != -1 && FD_ISSET( mbping.mb_socket, &read_fds ))
  203.                 mbping_receive_data( mbping.mb_socket );
  204.             break;
  205.         }
  206.     }
  207. }
  208.  
  209. int main( int argc, char **argv ) {
  210.     extern int opterr;
  211.  
  212.     int c;
  213.  
  214.     mbping.kbd_prompt = MBPING_PROMPT;
  215.     mbping.port_name = MB_INET;
  216.     mbping.mb_socket = -1;
  217.     mbping.dump_size = 32;
  218.  
  219.     opterr = 0;
  220.  
  221.     while ( (c = getopt( argc, argv, "P:d:" )) != -1 )
  222.         switch ( c ) {
  223.         case 'P':
  224.             mbping.kbd_prompt = optarg;
  225.             break;
  226.         case 'd': {
  227.             int dump_size = atoi( optarg );
  228.             if ( dump_size >= 16 )
  229.                 mbping.dump_size = dump_size;
  230.             break;
  231.         }
  232.         case '?':
  233.         default:
  234.         error:
  235.             fprintf( stderr, "%s [-P prompt] [-d dump_size] [port_name]\n", argv[ 0 ] );
  236.             exit( 1 );
  237.         }
  238.  
  239.     switch ( argc - optind ) {
  240.     case 0:
  241.         break;
  242.     case 1:
  243.         mbping.port_name = argv[ optind ];
  244.         break;
  245.     default:
  246.         goto error;
  247.     }
  248.  
  249.     startup( );
  250.     main_loop( );
  251. }
  1. #ifndef _MBPING_H
  2. #define _MBPING_H
  3.  
  4. #include <sys/types.h>
  5.  
  6. #include "ctk/shell.h"
  7.  
  8. #define MBPING_PROMPT       "ping> "
  9.  
  10. struct mbping {
  11.     shell   kbd_shell;
  12.     char *  kbd_prompt;
  13.     char *  port_name;
  14.     int     mb_socket;
  15.     u_int   mb_msg_id;
  16.     int     dump_size;
  17. };
  18.  
  19. extern struct mbping mbping;
  20.  
  21. #ifdef __STDC__
  22. extern void mbping_unbind( );
  23. extern void mbping_bind( char *host );
  24. extern void mbping_exit( int status );
  25. extern void mbping_receive_data( int socket );
  26. extern void mbping_register_as( char *app_name, int sync );
  27. extern void mbping_probe( char *app_name, int sync );
  28. extern void mbping_send_data( char *peer_name, u_int len, u_char *msg, int mode );
  29. #else
  30. extern void mbping_unbind();
  31. extern void mbping_bind();
  32. extern void mbping_exit();
  33. extern void mbping_receive_data();
  34. extern void mbping_register_as();
  35. extern void mbping_probe();
  36. extern void mbping_send_data();
  37. #endif
  38.  
  39. #endif

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].