5
mb_api.c
- #include "mb_api.h"
- #include "mb_sio.h"
- #include "mbdef.h"
- #include <sys/types.h>
- #include <sys/socket.h>
- #if defined( LINUX ) || defined(HPUX)
- #else
- #include <sys/select.h>
- #endif
- #include <sys/param.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #if defined( SUN ) || defined( LINUX )
- #include <netinet/in.h>
- #endif
- #include <netdb.h>
- #include <errno.h>
- #if !defined( LINUX )
- extern char *sys_errlist[];
- #endif
- #ifndef MIN
- #define MIN(x, y) ((x) < (y) ? (x) : (y))
- #endif
- #ifndef MAX
- #define MAX(x, y) ((x) < (y) ? (y) : (x))
- #endif
- static int pagesize = MB_PAGESIZE;
- int mb_getpagesize( ) {
- return pagesize;
- }
- int mb_setpagesize( int size ) {
- if ( size > 0 )
- pagesize = size;
- return pagesize;
- }
- int mb_send( int socket, char *peer, int msg_type, int msg_id, int msg_len, u_char *msg ) {
- u_char str[ 128 ];
- int cnt;
- int n, w; /* write status */
- n = sprintf( (char *)str, "%c%c%s%c%u%c%u%c", MB_STAMP, msg_type, peer, 0, msg_id, 0, msg_len, 0 );
- if ( s_write( socket, str, n ) < 0 )
- return -1;
- if ( msg_len > 0 ) { /* write msg */
- cnt = 0;
- do {
- n = MIN( msg_len, pagesize );
- w = s_write( socket, msg + cnt, n );
- if ( w < 0 )
- return -1;
- cnt += w;
- }
- while ( msg_len -= w );
- }
- return 0;
- }
- int mb_reply( int socket, int msg_id, int msg_len, u_char *msg ) {
- u_char str[ 128 ];
- int n, w;
- n = sprintf( (char *)str, "%c%c%u%c%u%c", MB_STAMP, MB_REPLY, msg_id, 0, msg_len, 0 );
- w = s_write( socket, str, n );
- if ( w < 0 )
- return -1;
- if ( msg_len > 0 ) { /* write msg */
- int cnt = 0;
- n = MIN( msg_len, pagesize );
- do {
- w = s_write( socket, msg + cnt, n );
- if ( w < 0 )
- return -1;
- cnt += w;
- }
- while ( msg_len -= w );
- }
- return 0;
- }
- int mb_reply_nobody( int socket, int msg_id ) {
- u_char str[ 128 ];
- int n;
- n = sprintf( (char *)str, "%c%c%u%c", MB_STAMP, MB_NOBODY, msg_id, 0 );
- return s_write( socket, str, n );
- }
- int mb_reply_timeout( int socket, int msg_id ) {
- u_char str[ 128 ];
- int n;
- n = sprintf( (char *)str, "%c%c%u%c", MB_STAMP, MB_TIMEOUT, msg_id, 0 );
- return s_write( socket, str, n );
- }
- int mb_register( int socket, int msg_id, char *app_name, int sync ) {
- u_char str[ 128 ];
- int n;
- n = sprintf( (char *)str, "%c%c%u%c%s%c", MB_STAMP, MB_REGISTER, msg_id, 0, app_name, 0 );
- if ( s_write( socket, str, n ) == -1 )
- return -1;
- if ( sync ) {
- int msg_id, msg_len;
- u_char *msg;
- if ( mb_receive( socket, &msg_id, &msg_len, &msg ) == -1 )
- return -1;
- n = atoi( (const char *)msg );
- free( msg );
- return n;
- }
- return 0;
- }
- int mb_probe( int socket, int msg_id, char *app_name, int sync ) {
- u_char str[ 128 ];
- int n;
- n = sprintf( (char *)str, "%c%c%u%c%s%c", MB_STAMP, MB_PROBE, msg_id, 0, app_name, 0 );
- if ( s_write( socket, str, n ) == -1 )
- return -1;
- if ( sync ) {
- int msg_id, msg_len;
- u_char *msg;
- if ( mb_receive( socket, &msg_id, &msg_len, &msg ) == -1 )
- return -1;
- n = atoi( (const char *)msg );
- free( msg );
- return n;
- }
- return 0;
- }
- int mb_receive( int socket, int *msg_id, int *msg_len, u_char **msg ) {
- int msg_type;
- int c, r;
- #if 0
- fd_set read_fds;
- int n_fds = FD_SETSIZE;
- struct timeval time_out;
- FD_ZERO( &read_fds );
- FD_SET( socket, &read_fds );
- time_out.tv_sec = 0L;
- time_out.tv_usec = 0L;
- /* peek socket */
- if ( select( n_fds, &read_fds, 0, 0, &time_out ) == 0 )
- return 0;
- #endif
- if ( (c = s_getc( socket )) == MB_STAMP ) {
- msg_type = s_getc( socket );
- switch ( msg_type ) {
- case MB_ONEWAY:
- case MB_TWOWAY:
- case MB_MULTIWAY:
- case MB_REPLY:
- case MB_NOBODY:
- case MB_TIMEOUT:
- case MB_REGISTER:
- case MB_PROBE:
- if ( (*msg_id = s_getuint( socket )) < 0 )
- return -1;
- if ( msg_type == MB_NOBODY || msg_type == MB_TIMEOUT )
- return msg_type;
- if ( (*msg_len = s_getuint( socket )) < 0 )
- return -1;
- if ( *msg_len > 0 ) {
- int cnt = 0;
- int len = *msg_len;
- *msg = (u_char *)malloc( len );
- if ( !*msg ) /* just cautious */
- break;
- do {
- r = s_read( socket, *msg + cnt, MIN( len, pagesize ));
- if ( r < 0 ) {
- free( *msg );
- return -1;
- }
- cnt += r;
- }
- while ( len -= r );
- }
- else
- *msg = 0;
- return msg_type;
- default:
- /* error */
- break;
- }
- }
- /* protocol error */
- close( socket );
- return -1;
- }
- static int bind_unix( char *name ) {
- int sd;
- struct sockaddr sd_address;
- char pathname[ MAXPATHLEN ];
- if ( (sd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1 )
- return -1;
- sd_address.sa_family = AF_UNIX;
- sprintf( pathname, "%s/%s", MB_UNIX, name == 0 ? MB_INET : name );
- (void)strcpy( sd_address.sa_data, pathname );
- if ( connect( sd, &sd_address, sizeof( struct sockaddr )) == -1 ) {
- close( sd );
- return -1;
- }
- return sd;
- }
- static int bind_inet( char *host, char *port_name ) {
- int sd;
- struct hostent *hp;
- struct sockaddr_in sd_address;
- struct servent *serv;
- if ( (hp = gethostbyname( host )) == 0 )
- return -3;
- endhostent();
- if ( (serv = getservbyname( port_name == 0 ? MB_INET : port_name, "tcp" )) == 0 )
- return -2;
- endservent();
- if ( (sd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1 )
- return -1;
- sd_address.sin_family = AF_INET;
- sd_address.sin_addr.s_addr = *((u_long *)hp->h_addr);
- sd_address.sin_port = serv->s_port;
- if ( connect( sd, (struct sockaddr *)&sd_address, sizeof( struct sockaddr_in )) == -1 ) {
- close( sd );
- return -1;
- }
- return sd;
- }
- int mb_bind( char *host, char *port_name ) {
- return host ? bind_inet( host, port_name ) : bind_unix( port_name );
- }
- int mb_unbind( int socket ) {
- return close( socket );
- }
Comments