15
mbexp
- #include <sys/types.h>
- #include <sys/socket.h>
- #if defined( LINUX )
- #else
- #include <sys/select.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #if defined( SUN ) || defined( LINUX )
- #include <unistd.h>
- #endif
- #include <errno.h>
- #include "mb.h"
- struct mbexp {
- u_int mode;
- char * peer_name;
- char * domain_name;
- char * host_name;
- char * port_name;
- int mb_socket;
- u_int mb_msg_id;
- } mbexp;
- void mbexp_unbind( ) {
- if ( mbexp.mb_socket != -1 ) {
- mb_unbind( mbexp.mb_socket );
- mbexp.mb_socket = -1;
- }
- }
- void mbexp_bind( char *host ) {
- if ( mbexp.mb_socket == -1 ) {
- int ret = mb_bind( mbexp.host_name, mbexp.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:
- mbexp.mb_socket = ret;
- break;
- }
- }
- }
- void mbexp_exit( int status ) {
- mbexp_unbind( );
- exit( status );
- }
- void mbexp_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:
- fprintf( stdout, "\nONEWAY[%i]<%i>\n", msg_id, msg_len );
- free( msg );
- break;
- case MB_MULTIWAY:
- fprintf( stdout, "\nMULTIWAY[%i]<%i>\n", msg_id, msg_len );
- free( msg );
- break;
- case MB_TWOWAY:
- fprintf( stdout, "\nTWOWAY[%i]<%i>\n", msg_id, msg_len );
- mb_reply( socket, msg_id, msg_len, msg ); /* echo */
- free( msg );
- break;
- case MB_REPLY:
- if ( msg_len > 0 ) {
- fprintf( stdout, "\nREPLY[%i]<%i>\n", msg_id, msg_len );
- free( msg );
- }
- else
- fprintf( stdout, "\nSENT[%i]\n", msg_id );
- break;
- case MB_NOBODY:
- fprintf( stdout, "\nNOBODY[%i]\n", msg_id );
- break;
- case MB_TIMEOUT:
- fprintf( stdout, "\nTIMEOUT[%i]\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", mbexp.port_name ? mbexp.port_name : MB_PORTNAME );
- mbexp_exit( 2 );
- break;
- }
- }
- void mbexp_register_as( char *app_name, int sync ) {
- int n;
- if ( mbexp.mb_socket != -1 ) {
- if ( (n = mb_register( mbexp.mb_socket, ++mbexp.mb_msg_id, app_name, sync )) < 0 )
- mbexp.mb_socket = -1;
- else {
- if ( sync )
- fprintf( stdout, "%i\n", n );
- }
- }
- }
- void mbexp_probe( char *app_name, int sync ) {
- int n;
- if ( mbexp.mb_socket != -1 ) {
- if ( (n = mb_probe( mbexp.mb_socket, ++mbexp.mb_msg_id, app_name, sync )) < 0 )
- mbexp.mb_socket = -1;
- else {
- if ( sync )
- fprintf( stdout, "%i\n", n );
- }
- }
- }
- void mbexp_send_data( char *peer_name, u_int len, u_char *msg, int mode ) {
- if ( mbexp.mb_socket != -1 )
- if ( mb_send( mbexp.mb_socket, peer_name, mode, ++mbexp.mb_msg_id, len, msg ) < 0 )
- mbexp.mb_socket = -1;
- }
- static void mbexp_send( ) {
- char text[ 4096 ];
- u_int len;
- char *nl;
- if ( !fgets( text, sizeof( text ), stdin ) )
- mbexp_exit( 0 );
- if ((nl = strchr( text, '\n' )))
- *nl = 0;
- if ( (len = strlen( text )) > 0 )
- mbexp_send_data( mbexp.peer_name, len, (u_char *)text, mbexp.mode );
- }
- static void startup( ) {
- mbexp_bind( mbexp.host_name );
- if ( mbexp.mb_socket == -1 )
- mbexp_exit( 1 );
- if ( mbexp.domain_name )
- mbexp_register_as( mbexp.domain_name, 0 );
- }
- 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 ( mbexp.mb_socket != -1 )
- FD_SET( mbexp.mb_socket, &read_fds );
- switch ( select( n_fds, &read_fds, 0, 0, 0 )) {
- case -1: /* trouble */
- if ( errno != EINTR )
- mbexp_exit( errno );
- break;
- case 0: /* time out */
- break;
- default: /* event */
- if ( FD_ISSET( 0, &read_fds ))
- mbexp_send( );
- if ( mbexp.mb_socket != -1 && FD_ISSET( mbexp.mb_socket, &read_fds ))
- mbexp_receive_data( mbexp.mb_socket );
- break;
- }
- }
- }
- int main( int argc, char **argv ) {
- extern int opterr;
- int c;
- mbexp.mode = MB_ONEWAY;
- mbexp.mb_socket = -1;
- opterr = 0;
- while ( (c = getopt( argc, argv, "m:" )) != -1 )
- switch ( c ) {
- case 'm':
- if ( strcmp( optarg, "oneway" ) == 0 )
- mbexp.mode = MB_ONEWAY;
- else if ( strcmp( optarg, "twoway" ) == 0 )
- mbexp.mode = MB_TWOWAY;
- else if ( strcmp( optarg, "multiway" ) == 0 )
- mbexp.mode = MB_MULTIWAY;
- else
- goto error;
- break;
- case '?':
- default:
- error:
- fprintf( stderr, "%s [-m oneway|twoway|multiway] peer_name [domain_name]\n", argv[ 0 ] );
- exit( 1 );
- }
- switch ( argc - optind ) {
- case 2:
- mbexp.domain_name = argv[ optind + 1 ];
- /* fall thru */
- case 1:
- mbexp.peer_name = argv[ optind ];
- break;
- default:
- goto error;
- }
- startup( );
- main_loop( );
- }
Comments