117770Sralph #ifndef lint 2*23695Sbloom static char sccsid[] = "@(#)bsdtcp.c 4.2 (Berkeley) 06/23/85"; 317770Sralph #endif 417770Sralph 517770Sralph #include "../condevs.h" 617770Sralph #ifdef BSDTCP 7*23695Sbloom #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; 2517770Sralph int s, 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)) { 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); 5217770Sralph alarm(30); 5317770Sralph hisctladdr.sin_family = hp->h_addrtype; 54*23695Sbloom #ifdef BSD2_9 55*23695Sbloom s = socket(SOCK_STREAM, 0, &hisctladdr, 0); 56*23695Sbloom #else BSD4_2 57*23695Sbloom s = socket(hp->h_addrtype, SOCK_STREAM, 0); 58*23695Sbloom #endif BSD4_2 5917770Sralph if (s < 0) 6017770Sralph goto bad; 61*23695Sbloom #ifndef BSD2_9 6217770Sralph if (bind(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0) 6317770Sralph goto bad; 64*23695Sbloom #endif BSD2_9 6517770Sralph bcopy(hp->h_addr, (char *)&hisctladdr.sin_addr, hp->h_length); 6617770Sralph hisctladdr.sin_port = port; 67*23695Sbloom #ifdef BSD2_9 68*23695Sbloom if (connect(s, (char *)&hisctladdr) < 0) 69*23695Sbloom #else BSD4_2 7017770Sralph if (connect(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0) 71*23695Sbloom #endif BSD4_2 7217770Sralph goto bad; 7317770Sralph alarm(0); 7417770Sralph CU_end = bsdtcpcls; 7517770Sralph return s; 7617770Sralph bad: 7717770Sralph alarm(0); 7817770Sralph close(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 } 9617770Sralph #endif BSDTCP 97