search.c

Demonstrates usage of the EIBnetmux server search functions.

It produces a list of all active EIBnetmux servers.

This version does NOT rely on the GNU Pth threading library.

/*
 * search - get list of eibnetmux servers
 * 
 * eibnetmux - eibnet/ip multiplexer
 * Copyright (C) 2006-2009 Urs Zurbuchen <going_nuts@users.sourceforge.net>
 * 
 * This program 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 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
 
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <getopt.h>
#include <arpa/inet.h>

#include <eibnetmux/enmx_lib.h>
#include <mylib.h>

#define  FALSE          0
#define  TRUE           1


/*
 * Global variables
 */
ENMX_HANDLE     sock_con = 0;
unsigned char   conn_state = 0;


/*
 * local function declarations
 */
static void Usage( char *progname );


static void Usage( char *progname )
{
    fprintf( stderr, "Usage: %s [-t <seconds>]\n", basename( progname ));
}


int main( int argc, char **argv )
{
    sENMX_Server            *list;
    sENMX_Server            *entry;
    int                     enmx_version;
    int                     c;
    int                     seconds;

    opterr = 0;
    seconds = 3;
    while( ( c = getopt( argc, argv, "t:" )) != -1 ) {
        switch( c ) {
            case 't':
                seconds = atoi( optarg );
                break;
            default:
                fprintf( stderr, "Invalid option: %c\n", c );
                Usage( argv[0] );
                exit( -1 );
        }
    }
    if( optind != argc ) {
        Usage( argv[0] );
        exit( -1 );
    }
    
    // get list of servers
    enmx_version = enmx_init();
    list = enmx_getservers( seconds );
    
    if( list == NULL ) {
        printf( "No servers found.\n" );
    } else {
        printf( "Servers found:\n" );
        for( entry = list; entry != NULL; entry = entry->next ) {
            printf( "%c %s (%s:%d) %s\n", (entry->eibnetmux == 1) ? '*' : ' ', entry->hostname, ip_addr( entry->ip ), ntohs( entry->port ), entry->version );
        }
    }
    
    enmx_releaseservers( list );
    enmx_close( sock_con );
    
    return( 0 );
}