xref: /csrg-svn/usr.bin/uucp/libacu/bsdtcp.c (revision 62384)
148651Sbostic /*-
2*62384Sbostic  * Copyright (c) 1985, 1993
3*62384Sbostic  *	The Regents of the University of California.  All rights reserved.
448651Sbostic  *
548651Sbostic  * %sccs.include.proprietary.c%
648651Sbostic  */
748651Sbostic 
817770Sralph #ifndef lint
9*62384Sbostic static char sccsid[] = "@(#)bsdtcp.c	8.1 (Berkeley) 06/06/93";
1048651Sbostic #endif /* not lint */
1117770Sralph 
1246875Sbostic #include "condevs.h"
1323695Sbloom #include <netdb.h>
1417770Sralph #include <sys/socket.h>
1517770Sralph #include <netinet/in.h>
1617770Sralph 
1717770Sralph /*
1817770Sralph  *	bsdtcpopn -- make a tcp connection
1917770Sralph  *
2017770Sralph  *	return codes:
2117770Sralph  *		>0 - file number - ok
2217770Sralph  *		FAIL - failed
2317770Sralph  */
2417770Sralph 
bsdtcpopn(flds)2517770Sralph bsdtcpopn(flds)
2617770Sralph register char *flds[];
2717770Sralph {
2817770Sralph 	struct servent *sp;
2917770Sralph 	struct hostent *hp;
3017770Sralph 	struct	sockaddr_in hisctladdr;
3129405Sbloom 	int s = -1, port;
3217770Sralph 	extern int errno;
3360114Storek 	extern const char *const sys_errlist[];
3417770Sralph 
3517770Sralph 	sp = getservbyname(flds[F_CLASS], "tcp");
3617770Sralph 	if (sp == NULL) {
3717770Sralph 		port = htons(atoi(flds[F_CLASS]));
3817770Sralph 		if (port == 0) {
3917770Sralph 			logent(_FAILED, "UNKNOWN PORT NUMBER");
4017770Sralph 			return CF_SYSTEM;
4117770Sralph 		}
4217770Sralph 	} else
4317770Sralph 		port = sp->s_port;
4417770Sralph 	DEBUG(4, "bsdtcpopn host %s, ", flds[F_PHONE]);
4517770Sralph 	DEBUG(4, "port %d\n", ntohs(port));
4617770Sralph 	if (setjmp(Sjbuf)) {
4729405Sbloom 		bsdtcpcls(s);
4817770Sralph 		logent("tcpopen", "TIMEOUT");
4917770Sralph 		return CF_DIAL;
5017770Sralph 	}
5117770Sralph 
5217770Sralph 	bzero((char *)&hisctladdr, sizeof (hisctladdr));
5317770Sralph 	hp = gethostbyname(flds[F_PHONE]);
5417770Sralph 	if (hp == NULL) {
5517770Sralph 		logent("tcpopen","UNKNOWN HOST");
5617770Sralph 		return CF_DIAL;
5717770Sralph 	}
5817770Sralph 	signal(SIGALRM, alarmtr);
5933584Srick 	alarm(MAXMSGTIME*2);
6017770Sralph 	hisctladdr.sin_family = hp->h_addrtype;
6123695Sbloom #ifdef BSD2_9
6223695Sbloom 	s = socket(SOCK_STREAM, 0, &hisctladdr, 0);
6323695Sbloom #else BSD4_2
6423695Sbloom 	s = socket(hp->h_addrtype, SOCK_STREAM, 0);
6523695Sbloom #endif BSD4_2
6617770Sralph 	if (s < 0)
6717770Sralph 		goto bad;
6823695Sbloom #ifndef BSD2_9
6946875Sbostic 	if (bind(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0)
7017770Sralph 		goto bad;
7123695Sbloom #endif BSD2_9
7217770Sralph 	bcopy(hp->h_addr, (char *)&hisctladdr.sin_addr, hp->h_length);
7317770Sralph 	hisctladdr.sin_port = port;
7423695Sbloom #ifdef BSD2_9
7523695Sbloom 	if (connect(s, (char *)&hisctladdr) < 0)
7623695Sbloom #else BSD4_2
7746875Sbostic 	if (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0)
7823695Sbloom #endif BSD4_2
7917770Sralph 		goto bad;
8017770Sralph 	alarm(0);
8117770Sralph 	CU_end = bsdtcpcls;
8217770Sralph 	return s;
8317770Sralph bad:
8417770Sralph 	alarm(0);
8529405Sbloom 	bsdtcpcls(s);
8617770Sralph 	DEBUG(5, "tcpopen failed: errno %d\n", errno);
8717770Sralph 	logent(sys_errlist[errno], _FAILED);
8817770Sralph 	return CF_DIAL;
8917770Sralph }
9017770Sralph 
9117770Sralph /*
9217770Sralph  * bsdtcpcls -- close tcp connection
9317770Sralph  */
bsdtcpcls(fd)9417770Sralph bsdtcpcls(fd)
9517770Sralph register int fd;
9617770Sralph {
9717770Sralph 	DEBUG(4, "TCP CLOSE called\n", 0);
9817770Sralph 	if (fd > 0) {
9917770Sralph 		close(fd);
10017770Sralph 		DEBUG(4, "closed fd %d\n", fd);
10117770Sralph 	}
10217770Sralph }
103