rem: Debug(("Single %d, curport %d", Single, curport))
add: debug_d("Single %d, curport %d\n", Single, curport);
context: int main(int argc, char *argv[]){  register int x;  register char *cp;  netcat_host *gp;  netcat_host *whereto = NULL;  netcat_host *wherefrom = NULL;  IA *ouraddr = NULL;  IA *themaddr = NULL;  USHORT o_lport = 0;  USHORT ourport = 0;  USHORT loport = 0;		/* for scanning stuff */  USHORT hiport = 0;  USHORT curport = 0;  char *randports = NULL;  int c;#ifdef HAVE_BIND/* can *you* say "cc -yaddayadda netcat.c -lresolv -l44bsd" on SunLOSs? */  res_init();#endif/* I was in this barbershop quartet in Skokie IL ... *//* round up the usual suspects, i.e. malloc up all the stuff we need */  lclend = (SAI *) Hmalloc(sizeof(SA));  remend = (SAI *) Hmalloc(sizeof(SA));  bigbuf_in = Hmalloc(BIGSIZ);  bigbuf_net = Hmalloc(BIGSIZ);  ding1 = (fd_set *) Hmalloc(sizeof(fd_set));  ding2 = (fd_set *) Hmalloc(sizeof(fd_set));  errno = 0;  gatesptr = 4;  h_errno = 0;/* catch a signal or two for cleanup */  signal(SIGINT, catch);  signal(SIGQUIT, catch);  signal(SIGTERM, catch);/* and suppress others... */#ifdef SIGURG  signal(SIGURG, SIG_IGN);#endif#ifdef SIGPIPE  signal(SIGPIPE, SIG_IGN);	/* important! */#endif  /* if no args given at all, get them from stdin */  if (argc == 1)    netcat_commandline(&argc, &argv);  while (TRUE) {    int option_index = 0;    static const struct option long_options[] = {	{ "gateway",	required_argument,	NULL, 'g' },	{ "pointer",	required_argument,	NULL, 'G' },	{ "help",	no_argument,		NULL, 'h' },	{ "interval",	required_argument,	NULL, 'i' },	{ "listen",	no_argument,		NULL, 'l' },	{ "dont-resolve", no_argument,		NULL, 'n' },	{ "output",	required_argument,	NULL, 'o' },	{ "local-port",	required_argument,	NULL, 'p' },	{ "randomize",	no_argument,		NULL, 'r' },	{ "telnet",	no_argument,		NULL, 't' },	{ "udp",		no_argument,		NULL, 'u' },	{ "verbose",	no_argument,		NULL, 'v' },	{ "version",	no_argument,		NULL, 'V' },	{ "hexdump",	no_argument,		NULL, 'x' },	{ "zero",	no_argument,		NULL, 'z' },	{ 0, 0, 0, 0 }    };    c = getopt_long(argc, argv, "g:G:hi:lno:p:rtuvxz", long_options, &option_index);    if (c == -1)      break;    switch (c) {#ifdef GAPING_SECURITY_HOLE    case 'e':			/* prog to exec */      pr00gie = optarg;      break;#endif    case 'G':			/* srcrt gateways pointer val */      x = atoi(optarg);      if ((x) && (x == (x & 0x1c)))	/* mask off bits of fukt values */	gatesptr = x;      else	bail("invalid hop pointer %d, must be multiple of 4 <= 28", x);      break;    case 'g':			/* srcroute hop[s] */      if (gatesidx > 8)	bail("too many -g hops");      if (gates == NULL)	/* eat this, Billy-boy */	gates = (netcat_host **) Hmalloc(sizeof(netcat_host *) * 10);      gp = netcat_resolvehost(optarg);      if (gp)	gates[gatesidx] = gp;      gatesidx++;      break;    case 'h':      netcat_printhelp(argv[0]);      exit(EXIT_SUCCESS);    case 'i':			/* line-interval time */      o_interval = atoi(optarg) & 0xffff;      if (!o_interval)	bail("invalid interval time %s", optarg);      break;    case 'l':			/* listen mode */      opt_listen = TRUE;      break;    case 'n':			/* numeric-only, no DNS lookups */      opt_numeric++;      break;    case 'o':			/* hexdump log */      opt_outputfile = strdup(optarg);      opt_hexdump = TRUE;	/* implied */      break;    case 'p':			/* local source port */      netcat_getport(&portpoop, optarg, 0);      o_lport = portpoop.num;      if (o_lport == 0)	bail("invalid local port %s", optarg);      break;    case 'r':			/* randomize various things */      opt_random = TRUE;      break;    case 's':			/* local source address *//* do a full lookup [since everything else goes through the same mill],   unless -n was previously specified.  In fact, careful placement of -n can   be useful, so we'll still pass opt_numeric here instead of forcing numeric.  */      wherefrom = netcat_resolvehost(optarg);      ouraddr = &wherefrom->iaddrs[0];      break;    case 't':			/* do telnet fakeout */      opt_telnet++;      break;    case 'u':			/* use UDP */      opt_udpmode = TRUE;      break;    case 'v':			/* verbose */      opt_verbose++;      break;    case 'V':			/* display version and exit */      netcat_printversion();      exit(EXIT_SUCCESS);    case 'w':			/* wait time */      o_wait = atoi(optarg);      if (o_wait <= 0)	bail("invalid wait-time %s", optarg);      timer1 = (struct timeval *) Hmalloc(sizeof(struct timeval));      timer2 = (struct timeval *) Hmalloc(sizeof(struct timeval));      timer1->tv_sec = o_wait;	/* we need two.  see readwrite()... */      break;    case 'x':			/* hexdump traffic */      opt_hexdump = TRUE;      break;    case 'z':			/* little or no data xfer */      opt_zero++;      break;    default:      fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);      exit(EXIT_FAILURE);    }				/* switch c */  }				/* while getopt *//* other misc initialization */  Debug(("fd_set size %d", sizeof(*ding1)))	/* how big *is* it? */    FD_SET(0, ding1);		/* stdin *is* initially open */  if (opt_random) {    SRAND(time(0));    randports = Hmalloc(65536);	/* big flag array for ports */  }#ifdef GAPING_SECURITY_HOLE  if (pr00gie) {    close(0);			/* won't need stdin */    opt_hexdump = FALSE;		/* -o with -e is meaningless! */    output_fd = NULL;  }#endif /* G_S_H */  if (opt_outputfile) {    output_fd = fopen(opt_outputfile, "w");    if (!output_fd) {      perror("Failed to open output file: ");      exit(EXIT_FAILURE);    }  }  else    output_fd = stdout;/* optind is now index of first non -x arg */  Debug(("after go: x now %c, optarg %x optind %d", x, (int)optarg, optind))/* Debug (("optind up to %d at host-arg %s", optind, argv[optind])) *//* gonna only use first addr of host-list, like our IQ was normal; if you wanna   get fancy with addresses, look up the list yourself and plug 'em in for now.   unless we finally implement -a, that is. */    if (argv[optind])    whereto = netcat_resolvehost(argv[optind]);  if (whereto && whereto->iaddrs)    themaddr = &whereto->iaddrs[0];  if (themaddr)    optind++;			/* skip past valid host lookup */  errno = 0;  h_errno = 0;/* Handle listen mode here, and exit afterward.  Only does one connect;   this is arguably the right thing to do.  A "persistent listen-and-fork"   mode a la inetd has been thought about, but not implemented.  A tiny   wrapper script can handle such things... */  if (opt_listen) {    curport = 0;		/* rem port *can* be zero here... */    if (argv[optind]) {		/* any rem-port-arg? */      netcat_getport(&portpoop, argv[optind], 0);      curport = portpoop.num;      if (curport == 0)		/* if given, demand correctness */	bail("invalid port %s", argv[optind]);    }				/* if port-arg */    netfd = dolisten(themaddr, curport, ouraddr, o_lport);/* dolisten does its own connect reporting, so we don't holler anything here */    if (netfd > 0) {#ifdef GAPING_SECURITY_HOLE      if (pr00gie)		/* -e given? */	doexec(netfd);#endif /* GAPING_SECURITY_HOLE */      x = readwrite(netfd);	/* it even works with UDP! */      if (opt_verbose > 1)	/* normally we don't care */	holler(wrote_txt, wrote_net, wrote_out);      exit(x);			/* "pack out yer trash" */    }    else			/* if no netfd */      bail("no connection");  }				/* opt_listen *//* fall thru to outbound connects.  Now we're more picky about args... */  if (!themaddr)    bail("no destination");  if (argv[optind] == NULL)    bail("no port[s] to connect to");  if (argv[optind + 1])		/* look ahead: any more port args given? */    Single = 0;			/* multi-mode, case A */  ourport = o_lport;		/* which can be 0 *//* everything from here down is treated as as ports and/or ranges thereof, so   it's all enclosed in this big ol' argv-parsin' loop.  Any randomization is   done within each given *range*, but in separate chunks per each succeeding   argument, so we can control the pattern somewhat. */  while (argv[optind]) {    hiport = loport = 0;    cp = strchr(argv[optind], '-');	/* nn-mm range? */    if (cp) {      *cp = '\0';      cp++;      netcat_getport(&portpoop, cp, 0);      hiport = portpoop.num;      if (hiport == 0)	bail("invalid port %s", cp);    }				/* if found a dash */    netcat_getport(&portpoop, argv[optind], 0);    loport = portpoop.num;    if (loport == 0)      bail("invalid port %s", argv[optind]);    if (hiport > loport) {	/* was it genuinely a range? */      Single = 0;		/* multi-mode, case B */      curport = hiport;		/* start high by default */      if (opt_random) {		/* maybe populate the random array */	loadports(randports, loport, hiport);	curport = nextport(randports);      }    }    else			/* not a range, including args like "25-25" */      curport = loport;    Debug(("Single %d, curport %d", Single, curport))/* Now start connecting to these things.  curport is already preloaded. */      while (loport <= curport) {      if ((!o_lport) && (opt_random)) {	/* -p overrides random local-port */	ourport = (RAND() & 0xffff);	/* random local-bind -- well above */	if (ourport < 8192)	/* resv and any likely listeners??? */	  ourport += 8192;	/* if it *still* conflicts, use -s. */      }      netcat_getport(&portpoop, NULL, curport);      curport = portpoop.num;      netfd = doconnect(themaddr, curport, ouraddr, ourport);      Debug(("netfd %d from port %d to port %d", netfd, ourport,	     curport)) if (netfd > 0)	if (opt_zero && opt_udpmode)	/* if UDP scanning... */	  netfd = udptest(netfd, themaddr);      if (netfd > 0) {		/* Yow, are we OPEN YET?! */	x = 0;			/* pre-exit status */	holler("%s [%s] %d (%s) open",	       whereto->name, whereto->addrs[0], curport, portpoop.name);#ifdef GAPING_SECURITY_HOLE	if (pr00gie)		/* exec is valid for outbound, too */	  doexec(netfd);#endif /* GAPING_SECURITY_HOLE */	if (!opt_zero)	  x = readwrite(netfd);	/* go shovel shit */      }      else {			/* no netfd... */	x = 1;			/* preload exit status for later *//* if we're scanning at a "one -v" verbosity level, don't print refusals.   Give it another -v if you want to see everything. */	if ((Single || (opt_verbose > 1)) || (errno != ECONNREFUSED))	  holler("%s [%s] %d (%s)",		 whereto->name, whereto->addrs[0], curport, portpoop.name);      }				/* if netfd */      close(netfd);		/* just in case we didn't already */      if (o_interval)	sleep(o_interval);	/* if -i, delay between ports too */      if (opt_random)	curport = nextport(randports);      else	curport--;		/* just decrement... */    }				/* while curport within current range */    optind++;  }				/* while remaining port-args -- end of big argv-ports loop */  errno = 0;  if (opt_verbose > 1)		/* normally we don't care */    holler(wrote_txt, wrote_net, wrote_out);  if (Single)    exit(x);			/* give us status on one connection */  exit(0);			/* otherwise, we're just done */}				/* main */