xref: /csrg-svn/usr.bin/uucp/libacu/bsdtcp.c (revision 46875)
117770Sralph #ifndef lint
2*46875Sbostic static char sccsid[] = "@(#)bsdtcp.c	4.5 (Berkeley) 03/02/91";
317770Sralph #endif
417770Sralph 
5*46875Sbostic #include "condevs.h"
623695Sbloom #include <netdb.h>
717770Sralph #include <sys/socket.h>
817770Sralph #include <netinet/in.h>
917770Sralph 
1017770Sralph /*
1117770Sralph  *	bsdtcpopn -- make a tcp connection
1217770Sralph  *
1317770Sralph  *	return codes:
1417770Sralph  *		>0 - file number - ok
1517770Sralph  *		FAIL - failed
1617770Sralph  */
1717770Sralph 
1817770Sralph bsdtcpopn(flds)
1917770Sralph register char *flds[];
2017770Sralph {
2117770Sralph 	struct servent *sp;
2217770Sralph 	struct hostent *hp;
2317770Sralph 	struct	sockaddr_in hisctladdr;
2429405Sbloom 	int s = -1, port;
2517770Sralph 	extern int errno;
2617770Sralph 	extern char *sys_errlist[];
2717770Sralph 
2817770Sralph 	sp = getservbyname(flds[F_CLASS], "tcp");
2917770Sralph 	if (sp == NULL) {
3017770Sralph 		port = htons(atoi(flds[F_CLASS]));
3117770Sralph 		if (port == 0) {
3217770Sralph 			logent(_FAILED, "UNKNOWN PORT NUMBER");
3317770Sralph 			return CF_SYSTEM;
3417770Sralph 		}
3517770Sralph 	} else
3617770Sralph 		port = sp->s_port;
3717770Sralph 	DEBUG(4, "bsdtcpopn host %s, ", flds[F_PHONE]);
3817770Sralph 	DEBUG(4, "port %d\n", ntohs(port));
3917770Sralph 	if (setjmp(Sjbuf)) {
4029405Sbloom 		bsdtcpcls(s);
4117770Sralph 		logent("tcpopen", "TIMEOUT");
4217770Sralph 		return CF_DIAL;
4317770Sralph 	}
4417770Sralph 
4517770Sralph 	bzero((char *)&hisctladdr, sizeof (hisctladdr));
4617770Sralph 	hp = gethostbyname(flds[F_PHONE]);
4717770Sralph 	if (hp == NULL) {
4817770Sralph 		logent("tcpopen","UNKNOWN HOST");
4917770Sralph 		return CF_DIAL;
5017770Sralph 	}
5117770Sralph 	signal(SIGALRM, alarmtr);
5233584Srick 	alarm(MAXMSGTIME*2);
5317770Sralph 	hisctladdr.sin_family = hp->h_addrtype;
5423695Sbloom #ifdef BSD2_9
5523695Sbloom 	s = socket(SOCK_STREAM, 0, &hisctladdr, 0);
5623695Sbloom #else BSD4_2
5723695Sbloom 	s = socket(hp->h_addrtype, SOCK_STREAM, 0);
5823695Sbloom #endif BSD4_2
5917770Sralph 	if (s < 0)
6017770Sralph 		goto bad;
6123695Sbloom #ifndef BSD2_9
62*46875Sbostic 	if (bind(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0)
6317770Sralph 		goto bad;
6423695Sbloom #endif BSD2_9
6517770Sralph 	bcopy(hp->h_addr, (char *)&hisctladdr.sin_addr, hp->h_length);
6617770Sralph 	hisctladdr.sin_port = port;
6723695Sbloom #ifdef BSD2_9
6823695Sbloom 	if (connect(s, (char *)&hisctladdr) < 0)
6923695Sbloom #else BSD4_2
70*46875Sbostic 	if (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0)
7123695Sbloom #endif BSD4_2
7217770Sralph 		goto bad;
7317770Sralph 	alarm(0);
7417770Sralph 	CU_end = bsdtcpcls;
7517770Sralph 	return s;
7617770Sralph bad:
7717770Sralph 	alarm(0);
7829405Sbloom 	bsdtcpcls(s);
7917770Sralph 	DEBUG(5, "tcpopen failed: errno %d\n", errno);
8017770Sralph 	logent(sys_errlist[errno], _FAILED);
8117770Sralph 	return CF_DIAL;
8217770Sralph }
8317770Sralph 
8417770Sralph /*
8517770Sralph  * bsdtcpcls -- close tcp connection
8617770Sralph  */
8717770Sralph bsdtcpcls(fd)
8817770Sralph register int fd;
8917770Sralph {
9017770Sralph 	DEBUG(4, "TCP CLOSE called\n", 0);
9117770Sralph 	if (fd > 0) {
9217770Sralph 		close(fd);
9317770Sralph 		DEBUG(4, "closed fd %d\n", fd);
9417770Sralph 	}
9517770Sralph }
96