Commit | Line | Data |
---|---|---|
555ae7de | 1 | #ifndef lint |
de465ec0 | 2 | static char sccsid[] = "@(#)bsdtcp.c 4.2 (Berkeley) %G%"; |
555ae7de RC |
3 | #endif |
4 | ||
5 | #include "../condevs.h" | |
6 | #ifdef BSDTCP | |
de465ec0 | 7 | #include <netdb.h> |
555ae7de RC |
8 | #include <sys/socket.h> |
9 | #include <netinet/in.h> | |
555ae7de RC |
10 | |
11 | /* | |
12 | * bsdtcpopn -- make a tcp connection | |
13 | * | |
14 | * return codes: | |
15 | * >0 - file number - ok | |
16 | * FAIL - failed | |
17 | */ | |
18 | ||
19 | bsdtcpopn(flds) | |
20 | register char *flds[]; | |
21 | { | |
22 | struct servent *sp; | |
23 | struct hostent *hp; | |
24 | struct sockaddr_in hisctladdr; | |
25 | int s, port; | |
26 | extern int errno; | |
27 | extern char *sys_errlist[]; | |
28 | ||
29 | sp = getservbyname(flds[F_CLASS], "tcp"); | |
30 | if (sp == NULL) { | |
31 | port = htons(atoi(flds[F_CLASS])); | |
32 | if (port == 0) { | |
33 | logent(_FAILED, "UNKNOWN PORT NUMBER"); | |
34 | return CF_SYSTEM; | |
35 | } | |
36 | } else | |
37 | port = sp->s_port; | |
38 | DEBUG(4, "bsdtcpopn host %s, ", flds[F_PHONE]); | |
39 | DEBUG(4, "port %d\n", ntohs(port)); | |
40 | if (setjmp(Sjbuf)) { | |
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(30); | |
53 | hisctladdr.sin_family = hp->h_addrtype; | |
de465ec0 JB |
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 | |
555ae7de RC |
59 | if (s < 0) |
60 | goto bad; | |
de465ec0 | 61 | #ifndef BSD2_9 |
555ae7de RC |
62 | if (bind(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0) |
63 | goto bad; | |
de465ec0 | 64 | #endif BSD2_9 |
555ae7de RC |
65 | bcopy(hp->h_addr, (char *)&hisctladdr.sin_addr, hp->h_length); |
66 | hisctladdr.sin_port = port; | |
de465ec0 JB |
67 | #ifdef BSD2_9 |
68 | if (connect(s, (char *)&hisctladdr) < 0) | |
69 | #else BSD4_2 | |
555ae7de | 70 | if (connect(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0) |
de465ec0 | 71 | #endif BSD4_2 |
555ae7de RC |
72 | goto bad; |
73 | alarm(0); | |
74 | CU_end = bsdtcpcls; | |
75 | return s; | |
76 | bad: | |
77 | alarm(0); | |
78 | close(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 | #endif BSDTCP |