1*17770Sralph #ifndef lint 2*17770Sralph static char sccsid[] = "@(#)bsdtcp.c 4.1 (Berkeley) 01/22/85"; 3*17770Sralph #endif 4*17770Sralph 5*17770Sralph #include "../condevs.h" 6*17770Sralph #ifdef BSDTCP 7*17770Sralph #include <sys/socket.h> 8*17770Sralph #include <netinet/in.h> 9*17770Sralph #include <netdb.h> 10*17770Sralph 11*17770Sralph /* 12*17770Sralph * bsdtcpopn -- make a tcp connection 13*17770Sralph * 14*17770Sralph * return codes: 15*17770Sralph * >0 - file number - ok 16*17770Sralph * FAIL - failed 17*17770Sralph */ 18*17770Sralph 19*17770Sralph bsdtcpopn(flds) 20*17770Sralph register char *flds[]; 21*17770Sralph { 22*17770Sralph struct servent *sp; 23*17770Sralph struct hostent *hp; 24*17770Sralph struct sockaddr_in hisctladdr; 25*17770Sralph int s, port; 26*17770Sralph extern int errno; 27*17770Sralph extern char *sys_errlist[]; 28*17770Sralph 29*17770Sralph sp = getservbyname(flds[F_CLASS], "tcp"); 30*17770Sralph if (sp == NULL) { 31*17770Sralph port = htons(atoi(flds[F_CLASS])); 32*17770Sralph if (port == 0) { 33*17770Sralph logent(_FAILED, "UNKNOWN PORT NUMBER"); 34*17770Sralph return CF_SYSTEM; 35*17770Sralph } 36*17770Sralph } else 37*17770Sralph port = sp->s_port; 38*17770Sralph DEBUG(4, "bsdtcpopn host %s, ", flds[F_PHONE]); 39*17770Sralph DEBUG(4, "port %d\n", ntohs(port)); 40*17770Sralph if (setjmp(Sjbuf)) { 41*17770Sralph logent("tcpopen", "TIMEOUT"); 42*17770Sralph return CF_DIAL; 43*17770Sralph } 44*17770Sralph 45*17770Sralph bzero((char *)&hisctladdr, sizeof (hisctladdr)); 46*17770Sralph hp = gethostbyname(flds[F_PHONE]); 47*17770Sralph if (hp == NULL) { 48*17770Sralph logent("tcpopen","UNKNOWN HOST"); 49*17770Sralph return CF_DIAL; 50*17770Sralph } 51*17770Sralph signal(SIGALRM, alarmtr); 52*17770Sralph alarm(30); 53*17770Sralph hisctladdr.sin_family = hp->h_addrtype; 54*17770Sralph s = socket(hp->h_addrtype, SOCK_STREAM, 0, 0); 55*17770Sralph if (s < 0) 56*17770Sralph goto bad; 57*17770Sralph if (bind(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0) 58*17770Sralph goto bad; 59*17770Sralph bcopy(hp->h_addr, (char *)&hisctladdr.sin_addr, hp->h_length); 60*17770Sralph hisctladdr.sin_port = port; 61*17770Sralph if (connect(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0) 62*17770Sralph goto bad; 63*17770Sralph alarm(0); 64*17770Sralph CU_end = bsdtcpcls; 65*17770Sralph return s; 66*17770Sralph bad: 67*17770Sralph alarm(0); 68*17770Sralph close(s); 69*17770Sralph DEBUG(5, "tcpopen failed: errno %d\n", errno); 70*17770Sralph logent(sys_errlist[errno], _FAILED); 71*17770Sralph return CF_DIAL; 72*17770Sralph } 73*17770Sralph 74*17770Sralph /* 75*17770Sralph * bsdtcpcls -- close tcp connection 76*17770Sralph */ 77*17770Sralph bsdtcpcls(fd) 78*17770Sralph register int fd; 79*17770Sralph { 80*17770Sralph DEBUG(4, "TCP CLOSE called\n", 0); 81*17770Sralph if (fd > 0) { 82*17770Sralph close(fd); 83*17770Sralph DEBUG(4, "closed fd %d\n", fd); 84*17770Sralph } 85*17770Sralph } 86*17770Sralph #endif BSDTCP 87