errx(1, "cannot use -p and -l");
errx(1, "cannot use -z and -l");
ssize_t	atomicio __P((ssize_t (*)(), int, void *, size_t));void	atelnet __P((int, unsigned char *, unsigned int));void	build_ports __P((char *));void	help __P((void));int	local_listen __P((char *, char *, struct addrinfo));void	readwrite __P((int));int	remote_connect __P((char *, char *, struct addrinfo));int	udptest __P((int));void	usage __P((int));intmain(argc, argv)	int argc;	char *argv[];{	int ch, s, ret;	char *host, *uport, *endp;	struct addrinfo hints;	struct servent *sv;	socklen_t len;	struct sockaddr *cliaddr;	ret = 1;	s = 0;	host = NULL;	uport = NULL;	endp = NULL;	sv = NULL;	while ((ch = getopt(argc, argv, "46hi:klnp:rs:tuvw:z")) != -1) {		switch (ch) {		case '4':			family = AF_INET;			break;		case '6':			family = AF_INET6;			break;		case 'h':			help();			break;		case 'i':			iflag = (int)strtoul(optarg, &endp, 10);			if (iflag < 0 || *endp != '\0')				errx(1, "interval cannot be negative");			break;		case 'k':			kflag = 1;			break;		case 'l':			lflag = 1;			break;		case 'n':			nflag = 1;			break;		case 'p':			pflag = optarg;			break;		case 'r':			rflag = 1;			break;		case 's':			sflag = optarg;			break;		case 't':			tflag = 1;			break;		case 'u':			uflag = 1;			break;		case 'v':			vflag = 1;			break;		case 'w': 			timeout = (int)strtoul(optarg, &endp, 10);			if (timeout < 0 || *endp != '\0')				errx(1, "timeout cannot be negative");			break;		case 'z':			zflag = 1;			break;		default:			usage(1);		}	}	argc -= optind;	argv += optind;	/* Cruft to make sure options are clean, and used properly. */	if (argv[0] && !argv[1]) {		if  (!lflag)			usage(1);		uport = argv[0];		host = NULL;	} else if (argv[0] && argv[1]) {		host = argv[0];		uport = argv[1];	} else		usage(1);	if (lflag && sflag)		errx(1, "cannot use -s and -l");	if (lflag && pflag)		errx(1, "cannot use -p and -l");	if (lflag && zflag)		errx(1, "cannot use -p and -l");	if (!lflag && kflag)		errx(1, "must use -k with -l");	/* Initialize addrinfo structure */	memset(&hints, 0, sizeof(struct addrinfo));	hints.ai_family = family;	hints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;	hints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;	if (nflag)		hints.ai_flags |= AI_NUMERICHOST;	if (lflag) {		int connfd;		ret = 0;		/* Allow only one connection at a time, but stay alive */		for (;;) {			if ((s = local_listen(host, uport, hints)) < 0)				err(1, NULL);			/*			 * For UDP, we will use recvfrom() initially			 * to wait for a caller, then use the regular			 * functions to talk to the caller.			 */			if (uflag) {				int rv;				char buf[1024];				struct sockaddr_storage z;				len = sizeof(z);				rv = recvfrom(s, buf, sizeof(buf), MSG_PEEK,					(struct sockaddr *)&z, &len);				if (rv < 0)					errx(1, "%s", strerror(errno));				rv = connect(s, (struct sockaddr *)&z,					len);				if (rv < 0)					errx(1, "%s", strerror(errno));				connfd = s;			} else {				connfd = accept(s, (struct sockaddr *)&cliaddr,									&len);			}			readwrite(connfd);			close(connfd);			close(s);			if (!kflag)				break;		}	} else {		int i = 0;		/* construct the portlist[] array */		build_ports(uport);		/* Cycle through portlist, connecting to each port */		for (i = 0; portlist[i] != NULL; i++) {						if (s)				close(s);				if ((s = remote_connect(host, portlist[i], hints)) < 0)				continue;			ret = 0;			if (vflag || zflag) {				/* For UDP, make sure we are connected */				if (uflag) {					if ((udptest(s)) == -1) {						ret = 1;						continue;					}				}				/* Don't lookup port if -n */				if (nflag)					sv = NULL;				else {					sv = getservbyport(						ntohs(atoi(portlist[i])),						uflag ? "udp" : "tcp");				}								printf("Connection to %s %s port [%s/%s] succeeded!\n",					host, portlist[i], uflag ? "udp" : "tcp",					sv ? sv->s_name : "*");			}			if (!zflag)				readwrite(s);		}	}	if (s)		close(s);	exit(ret);}