Plan 9 from Bell Labs’s /usr/web/sources/contrib/fgb/cmd/ci5.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ip.h>
#include <ndb.h>

char *host;
char *net;
Biobuf bio;
int secs;

char*
getport(int i)
{
	static char port[10], *p;

	sprint(port, "%d", i);
	if((p=csgetvalue("/net", "port", port, net, nil)) == nil)
		return port;
	strncpy(port, p, sizeof(port)-1);
	port[sizeof(port)-1] = 0;
	free(p);
	return port;
}

void
ci5(char *port)
{
	int fd;

	
	alarm(1000*secs);
	if((fd=dial(netmkaddr(host, net, port), 0, 0, 0)) > 0){
		Bprint(&bio, "%s!%s!%s\n", net, host, port); Bflush(&bio);
		close(fd);
	}
	alarm(0);
}

void
main(int argc, char *argv[])
{
	Waitmsg *w;
	int port[2], i;
	char *p;

	port[0] = 1;
	port[1] = 512;
	secs = 10;
	ARGBEGIN{
	case 'r':
		for(i=0; i<2; i++){
			p = ARGF();
			if(p == nil)
				goto Usage;
			port[i] = atoi(p);
			if(port[i] <= 0)
				goto Usage;
		}
		if(port[0] >= port[1]){
			fprint(2, "ci5: invalid range: %d-%d\n", port[0], port[1]);
			exits("range");
		}
		break;
	case 's':
		p = ARGF();
		if(p == nil)
			goto Usage;
		secs = atoi(p);
		if(secs <= 0)
			goto Usage;
		break;
	default:
		goto Usage;
	}ARGEND

	if(argc != 1){
    Usage:
		fprint(2, "Usage: ci5 [-r start end] [-s secs] net!host\n");
		exits("usage");
	}

	Binit(&bio, 1, OWRITE);
	net = "tcp";
	host = argv[0];
	if((p = strchr(host, '!'))){
		*p++ = '\0';
		net = host;
		host = p;
	}

	for(i=port[0]; i<port[1]+1; i++){		
		switch(fork()){
		case -1:
			sysfatal("can't fork: %r");
		case 0:
			ci5(getport(i));
			_exits(nil);
		default:
			w = wait();
			free(w);
		}
	}
	exits(nil);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.