date and time created 85/01/22 13:04:49 by ralph
[unix-history] / usr / src / usr.bin / uucp / libacu / bsdtcp.c
CommitLineData
555ae7de
RC
1#ifndef lint
2static char sccsid[] = "@(#)bsdtcp.c 4.1 (Berkeley) %G%";
3#endif
4
5#include "../condevs.h"
6#ifdef BSDTCP
7#include <sys/socket.h>
8#include <netinet/in.h>
9#include <netdb.h>
10
11/*
12 * bsdtcpopn -- make a tcp connection
13 *
14 * return codes:
15 * >0 - file number - ok
16 * FAIL - failed
17 */
18
19bsdtcpopn(flds)
20register 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;
54 s = socket(hp->h_addrtype, SOCK_STREAM, 0, 0);
55 if (s < 0)
56 goto bad;
57 if (bind(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0)
58 goto bad;
59 bcopy(hp->h_addr, (char *)&hisctladdr.sin_addr, hp->h_length);
60 hisctladdr.sin_port = port;
61 if (connect(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0)
62 goto bad;
63 alarm(0);
64 CU_end = bsdtcpcls;
65 return s;
66bad:
67 alarm(0);
68 close(s);
69 DEBUG(5, "tcpopen failed: errno %d\n", errno);
70 logent(sys_errlist[errno], _FAILED);
71 return CF_DIAL;
72}
73
74/*
75 * bsdtcpcls -- close tcp connection
76 */
77bsdtcpcls(fd)
78register int fd;
79{
80 DEBUG(4, "TCP CLOSE called\n", 0);
81 if (fd > 0) {
82 close(fd);
83 DEBUG(4, "closed fd %d\n", fd);
84 }
85}
86#endif BSDTCP