stew

a monorepo of some sort
git clone git://git.nsmpr.xyz/stew.git
Log | Files | Refs

poke.c (1068B)


      1 /*
      2 	simple port checking utility
      3 */
      4 
      5 #include <u.h>
      6 #include <libc.h>
      7 
      8 ulong timeout = 3000;
      9 int timedout;
     10 char *tstr, **addr;
     11 
     12 int timeoutf(void *, char *);
     13 
     14 void
     15 usage(void) {
     16 	fprint(2, "usage: %s [-t timeout] addr1 [addr2 addr3 ...]\n", argv0);
     17 	exits("usage");
     18 };
     19 
     20 void
     21 poke(char *addr) {
     22 	int d;
     23 	timedout = 0;
     24 	alarm(timeout);
     25 	d = dial(addr, nil, nil, nil);
     26 	if(d > 0){
     27 		close(d);
     28 		print("%s open\n", addr);
     29 	} else if (timedout == 0) print("%s closed %r\n", addr);
     30 	else print("%s timeout\n", addr);
     31 }
     32 
     33 void
     34 main(int argc, char **argv){
     35 	ARGBEGIN{
     36 	case 't':
     37 		tstr = EARGF(usage());
     38 		if(tstr != nil) timeout = strtoul(tstr, nil, 10);
     39 		break;
     40 	default:
     41 		usage();
     42 	}ARGEND
     43 	if(argc == 0) usage();
     44 	atnotify(timeoutf, 1);
     45 	addr = malloc(sizeof(char *) * argc);
     46 	int i;
     47 	for(i = 0; i < argc; i++){
     48 		addr[i] = netmkaddr(argv[i], "tcp", "echo");
     49 		if(addr[i] == nil) sysfatal("failed at %s, %r", argv[i]);
     50 	}
     51 	for(i = 0; i < argc; i++) poke(addr[i]);
     52 }
     53 
     54 int
     55 timeoutf(void *, char *note){
     56 	if(strcmp(note, "alarm") != 0) return 0;
     57 	timedout = 1;
     58 	return 1;
     59 }