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