source: trunk/athena/bin/lpr/tcp_conn.c @ 5247

Revision 5247, 2.4 KB checked in by epeisach, 33 years ago (diff)
Removed extraneous argument to connect
Line 
1#ifndef lint
2static char *sccsid = "@(#)tcp_conn.c   4.1     ULTRIX  9/13/88";
3#endif
4
5#include "lp.h"
6
7/*
8 *              T C P _ c o n n
9 *
10 * Create a TCP connection to the remote printer server.
11 *
12 * Inputs:              char *rhost;
13 * Outputs:             none
14 * Returns:             FD open on network printer
15 *
16 * The string "rhost" is expected to be something like "decwrl", or
17 * "decwrl.dec.com", or "decwrl.dec.com/servicename". If the / is present,
18 * then the string following it is the name of the service, to be looked
19 * up in /etc/services. If no name is provided (the / is absent), then
20 * the service "printserver" is assumed.
21 *
22 */
23tcp_conn(rhost)
24char *rhost;
25{
26    struct hostent *hp;
27    struct servent *sp;
28    struct sockaddr_in sin;
29    int s, retries, aval;
30    int err;
31    char hostString[512];
32    char serviceName[512];
33
34    extern int sys_nerr;
35    extern char *sys_errlist[];
36
37    /*
38     * Get the host address and port number to connect to.
39     */
40    strcpy(serviceName,"printserver");
41    sscanf(rhost,"%[^/]/%[^/]", hostString, serviceName);
42    bzero((char *)&sin, sizeof(sin));
43
44    aval = inet_addr(hostString);
45    if (aval != -1) {
46        sin.sin_addr.s_addr = aval;
47    } else {
48        hp = gethostbyname(hostString);
49        if (hp == (struct hostent *) NULL)
50            fatal("unknown host %s", hostString);
51        sin.sin_addr.s_addr = *(int *) hp->h_addr;
52    }
53    sp = getservbyname(serviceName, "tcp");
54    if (sp == (struct servent *) NULL)
55        fatal("Service '%s/tcp' not defined. Fix /etc/services.",serviceName);
56    sin.sin_family = AF_INET;
57    sin.sin_port = sp->s_port;
58
59    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
60        if ((errno > 0) && (errno <= sys_nerr))
61            fatal("Can't get socket: %s",sys_errlist[errno]);
62        else    fatal("Can't get socket, errno=%d",errno);
63    }
64    retries = 0;
65    while (1) {
66        if (connect(s, (caddr_t)&sin, sizeof(sin)) < 0) {
67            err = errno;
68            close(s);
69            retries = (retries > 5) ? 6 : retries+1;
70            status("Trying to connect to %s",hostString);
71            if (err == ECONNREFUSED) sleep(1<<retries);
72        } else
73            return(s);
74    }
75}
76
77/*VARARGS1*/
78static
79status(msg, a1, a2, a3)
80        char *msg;
81{
82        register int fd;
83        char buf[BUFSIZ];
84
85        umask(0);
86        fd = open(ST, O_WRONLY|O_CREAT, 0664);
87        if (fd < 0 || flock(fd, LOCK_EX) < 0)
88                fatal("cannot create status file");
89        ftruncate(fd, 0);
90        sprintf(buf, msg, a1, a2, a3);
91        strcat(buf, "\n");
92        (void) write(fd, buf, strlen(buf));
93        (void) close(fd);
94}
95
Note: See TracBrowser for help on using the repository browser.