117770Sralph #ifndef lint 2*29405Sbloom static char sccsid[] = "@(#)bsdtcp.c 4.3 (Berkeley) 06/07/86"; 317770Sralph #endif 417770Sralph 517770Sralph #include "../condevs.h" 617770Sralph #ifdef BSDTCP 723695Sbloom #include <netdb.h> 817770Sralph #include <sys/socket.h> 917770Sralph #include <netinet/in.h> 1017770Sralph 1117770Sralph /* 1217770Sralph * bsdtcpopn -- make a tcp connection 1317770Sralph * 1417770Sralph * return codes: 1517770Sralph * >0 - file number - ok 1617770Sralph * FAIL - failed 1717770Sralph */ 1817770Sralph 1917770Sralph bsdtcpopn(flds) 2017770Sralph register char *flds[]; 2117770Sralph { 2217770Sralph struct servent *sp; 2317770Sralph struct hostent *hp; 2417770Sralph struct sockaddr_in hisctladdr; 25*29405Sbloom int s = -1, port; 2617770Sralph extern int errno; 2717770Sralph extern char *sys_errlist[]; 2817770Sralph 2917770Sralph sp = getservbyname(flds[F_CLASS], "tcp"); 3017770Sralph if (sp == NULL) { 3117770Sralph port = htons(atoi(flds[F_CLASS])); 3217770Sralph if (port == 0) { 3317770Sralph logent(_FAILED, "UNKNOWN PORT NUMBER"); 3417770Sralph return CF_SYSTEM; 3517770Sralph } 3617770Sralph } else 3717770Sralph port = sp->s_port; 3817770Sralph DEBUG(4, "bsdtcpopn host %s, ", flds[F_PHONE]); 3917770Sralph DEBUG(4, "port %d\n", ntohs(port)); 4017770Sralph if (setjmp(Sjbuf)) { 41*29405Sbloom bsdtcpcls(s); 4217770Sralph logent("tcpopen", "TIMEOUT"); 4317770Sralph return CF_DIAL; 4417770Sralph } 4517770Sralph 4617770Sralph bzero((char *)&hisctladdr, sizeof (hisctladdr)); 4717770Sralph hp = gethostbyname(flds[F_PHONE]); 4817770Sralph if (hp == NULL) { 4917770Sralph logent("tcpopen","UNKNOWN HOST"); 5017770Sralph return CF_DIAL; 5117770Sralph } 5217770Sralph signal(SIGALRM, alarmtr); 5317770Sralph alarm(30); 5417770Sralph hisctladdr.sin_family = hp->h_addrtype; 5523695Sbloom #ifdef BSD2_9 5623695Sbloom s = socket(SOCK_STREAM, 0, &hisctladdr, 0); 5723695Sbloom #else BSD4_2 5823695Sbloom s = socket(hp->h_addrtype, SOCK_STREAM, 0); 5923695Sbloom #endif BSD4_2 6017770Sralph if (s < 0) 6117770Sralph goto bad; 6223695Sbloom #ifndef BSD2_9 6317770Sralph if (bind(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0) 6417770Sralph goto bad; 6523695Sbloom #endif BSD2_9 6617770Sralph bcopy(hp->h_addr, (char *)&hisctladdr.sin_addr, hp->h_length); 6717770Sralph hisctladdr.sin_port = port; 6823695Sbloom #ifdef BSD2_9 6923695Sbloom if (connect(s, (char *)&hisctladdr) < 0) 7023695Sbloom #else BSD4_2 7117770Sralph if (connect(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0) 7223695Sbloom #endif BSD4_2 7317770Sralph goto bad; 7417770Sralph alarm(0); 7517770Sralph CU_end = bsdtcpcls; 7617770Sralph return s; 7717770Sralph bad: 7817770Sralph alarm(0); 79*29405Sbloom bsdtcpcls(s); 8017770Sralph DEBUG(5, "tcpopen failed: errno %d\n", errno); 8117770Sralph logent(sys_errlist[errno], _FAILED); 8217770Sralph return CF_DIAL; 8317770Sralph } 8417770Sralph 8517770Sralph /* 8617770Sralph * bsdtcpcls -- close tcp connection 8717770Sralph */ 8817770Sralph bsdtcpcls(fd) 8917770Sralph register int fd; 9017770Sralph { 9117770Sralph DEBUG(4, "TCP CLOSE called\n", 0); 9217770Sralph if (fd > 0) { 9317770Sralph close(fd); 9417770Sralph DEBUG(4, "closed fd %d\n", fd); 9517770Sralph } 9617770Sralph } 9717770Sralph #endif BSDTCP 98