int
main (int argc, char **argv)
{
    int		   c ;
    char           *liss_server ;
    int            timeout = 10;
    int		   verbose = 0 ;
    int		   defaultport = 4000 ;
    unsigned char *seed ;
    int 	   fd = -1 ;
    Bns 	   *bns=0 ;

    elog_init (argc, argv);
    announce(0,0) ;

    while ((c = getopt (argc, argv, "v")) != -1) {
	switch (c) {

	  case 'v':
	    verbose++ ;
	    break;

	  default:
	    usage ();
	}
    }
    if (argc - optind < 1)
	usage ();

    allot(unsigned char *, seed, SIZE) ; 
    for(;optind < argc;optind++) { 
	liss_server = argv[optind] ;
	elog_notify(0, "opening %s\n", liss_server) ; 
	fd = open_socket ( liss_server, defaultport ) ; 
	if ( fd < 0 ) { 
	    elog_complain ( 1, "Can't open liss server %s", liss_server ) ;
	} else { 
	    int out ;
	    bns = bnsnew(fd, 8192) ;
	    bnsuse_sockio(bns) ;
	    bnstimeout ( bns, timeout*1000 ) ;
	    bnsclr(bns) ;
	    bns->fd = fd ; 
	    elog_notify(0, "reading %d bytes from %s\n", SIZE, liss_server) ; 
	    if ( bnsget(bns, seed, BYTES, SIZE ) == 0 ) { 
		out = open(liss_server, O_WRONLY | O_CREAT, 0664 ) ;
		if ( out == 0 ) { 
		    elog_die (0, "Can't open %s to write", liss_server) ;
		}
		elog_notify(0, "writing %d bytes from %s\n", SIZE, liss_server) ; 
		if ( write(out, seed, SIZE) != SIZE ) { 
		   elog_complain(0, "failed to write %d bytes to %s", SIZE, liss_server) ; 
		}
		if ( close(out) != 0 ) { 
		    elog_complain(0, "failed to close %s", liss_server) ; 
		}
	    } else { 
		elog_complain(0, "Failed to read data from %s", liss_server) ; 
	    }
	    if ( bnsclose(bns) != 0 ) { 
		elog_complain(0, "failed to close bns #%d for %s", fd, liss_server) ; 
	    }
	}
    }

    return 0 ;
}
Example #2
0
void
refresh_import_thread( ImportThread *it )
{
	int	val;

	while( it->bnsin == NULL ) {

		it->so = socket( PF_INET, SOCK_STREAM, 0 );
		if( it->so < 0 ) {

			elog_complain( 1,
		 	  "'%s': Failed to open socket to %s:%d\n", 
			  it->name, it->server_ipaddress, it->server_port );

			sleep( CONNECT_FAILURE_SLEEPTIME_SEC );

			continue;
		}
	
		it->sin.sin_family = AF_INET;
		it->sin.sin_port = htons( 0 ); /* Any port */
		it->sin.sin_addr.s_addr = htonl( INADDR_ANY );

		if( bind( it->so, (struct sockaddr *) &it->sin, 
		                                   sizeof( it->sin ) ) ) {
			if( it->bindfail < NCOMPLAIN_MAX ) {

				elog_complain( 1, "'%s': Couldn't bind socket\n",
					  it->name );
			}

			if( it->bindfail++ == NCOMPLAIN_MAX ) {
					
				elog_complain( 0, 
					"'%s': Last message repeated %d "
					"times; will keep retrying "
					"every %d sec\n",
					it->name,
					NCOMPLAIN_MAX,
					CONNECT_FAILURE_SLEEPTIME_SEC );
			}

			close( it->so );
				
			sleep( CONNECT_FAILURE_SLEEPTIME_SEC );

			continue;

		} else {

			it->bindfail = 0;
		}
	
		it->sin.sin_port = htons( it->server_port );
		it->sin.sin_addr.s_addr = inet_addr( it->server_ipaddress );

		if( Verbose ) {

			elog_notify( 0, 
				  "'%s': Attempting to connect "
				  "to remote export module at %s:%d\n",
				  it->name, it->server_ipaddress, 
				  it->server_port );
		}

		if( connect( it->so, (struct sockaddr *) &it->sin, sizeof( it->sin ) ) ) {
			if( it->connectfail < NCOMPLAIN_MAX ) {

				elog_complain( 1, 
					"'%s': Failed to connect socket for %s:%d\n",
					it->name,
					it->server_ipaddress, it->server_port );
			}

			if( it->connectfail++ == NCOMPLAIN_MAX ) {
					
				elog_complain( 0, 
					"'%s': Last message repeated %d "
					"times; will keep retrying "
					"every %d sec\n",
					it->name,
					NCOMPLAIN_MAX,
					CONNECT_FAILURE_SLEEPTIME_SEC );
			}

			close( it->so );
				
			sleep( CONNECT_FAILURE_SLEEPTIME_SEC );

			continue;

		} else {

			it->connectfail = 0;

			val = 1;

			if( setsockopt( it->so, SOL_SOCKET, SO_KEEPALIVE, 
					&val, sizeof(int) ) ) {
	
				elog_die( 1, 
					"Failed to set KEEPALIVE for socket\n" );
			}

			if( Verbose ) {

				elog_notify( 0, 
					  "'%s': import thread Connected "
					  "to remote export module\n",
					  it->name );
			}
		}

		it->bnsin = bnsnew( it->so, BNS_BUFFER_SIZE ); 

		bnsuse_sockio( it->bnsin );

		bnstimeout( it->bnsin, DEFAULT_BNS_TIMEOUT );
	}

	return;
}