1: #ifndef lint
2: static char sccsid[] = "@(#)bsdtcp.c 4.3 (Berkeley) 6/7/86";
3: #endif
4:
5: #include "../condevs.h"
6: #ifdef BSDTCP
7: #include <netdb.h>
8: #include <sys/socket.h>
9: #include <netinet/in.h>
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 = -1, 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: bsdtcpcls(s);
42: logent("tcpopen", "TIMEOUT");
43: return CF_DIAL;
44: }
45:
46: bzero((char *)&hisctladdr, sizeof (hisctladdr));
47: hp = gethostbyname(flds[F_PHONE]);
48: if (hp == NULL) {
49: logent("tcpopen","UNKNOWN HOST");
50: return CF_DIAL;
51: }
52: signal(SIGALRM, alarmtr);
53: alarm(30);
54: hisctladdr.sin_family = hp->h_addrtype;
55: #ifdef BSD2_9
56: s = socket(SOCK_STREAM, 0, &hisctladdr, 0);
57: #else BSD4_2
58: s = socket(hp->h_addrtype, SOCK_STREAM, 0);
59: #endif BSD4_2
60: if (s < 0)
61: goto bad;
62: #ifndef BSD2_9
63: if (bind(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0)
64: goto bad;
65: #endif BSD2_9
66: bcopy(hp->h_addr, (char *)&hisctladdr.sin_addr, hp->h_length);
67: hisctladdr.sin_port = port;
68: #ifdef BSD2_9
69: if (connect(s, (char *)&hisctladdr) < 0)
70: #else BSD4_2
71: if (connect(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0)
72: #endif BSD4_2
73: goto bad;
74: alarm(0);
75: CU_end = bsdtcpcls;
76: return s;
77: bad:
78: alarm(0);
79: bsdtcpcls(s);
80: DEBUG(5, "tcpopen failed: errno %d\n", errno);
81: logent(sys_errlist[errno], _FAILED);
82: return CF_DIAL;
83: }
84:
85: /*
86: * bsdtcpcls -- close tcp connection
87: */
88: bsdtcpcls(fd)
89: register int fd;
90: {
91: DEBUG(4, "TCP CLOSE called\n", 0);
92: if (fd > 0) {
93: close(fd);
94: DEBUG(4, "closed fd %d\n", fd);
95: }
96: }
97: #endif BSDTCP
Defined functions
Defined variables
sccsid
defined in line
2;
never used