xref: /titanic_41/usr/src/cmd/lp/model/netpr/tcp_misc.c (revision 6185db853e024a486ff8837e6784dd290d866112)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright (c) 1996-2001 by Sun Microsystems, Inc.
25  * All rights reserved.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <libintl.h>
33 #include <signal.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <syslog.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <netdb.h>
45 #include "netpr.h"
46 #include "netdebug.h"
47 
48 #define	MAX_NLPS	60	/* Max no. loops in while */
49 
50 np_tcpjob_t *
51 create_tcp_job(np_job_t *genjob, int filesize)
52 {
53 	np_tcpjob_t *tcpjob;
54 
55 	if (genjob == NULL)
56 		return (NULL);
57 
58 	tcpjob = (np_tcpjob_t *)malloc(sizeof (np_tcpjob_t));
59 	ASSERT(tcpjob, MALLOC_ERR);
60 	(void) memset(tcpjob, 0, sizeof (np_tcpjob_t));
61 
62 	tcpjob->gen_data = genjob;
63 	tcpjob->gen_data->filesize = filesize;
64 
65 	return (tcpjob);
66 }
67 
68 int
69 tcp_open(char *dest, np_tcpjob_t *tcpjob, int timeout)
70 {
71 	struct hostent *hp;
72 	struct sockaddr_in6 serv_addr;
73 	int	s,
74 		err,
75 		error_num;
76 	unsigned timo = 1;
77 	int retry;
78 	int rtnerr;
79 
80 	/*
81 	 * Get the host address and port number to connect to.
82 	 */
83 	if (dest == NULL) {
84 		return (-1);
85 	}
86 
87 	if ((hp = (getipnodebyname(dest, AF_INET6, AI_DEFAULT,
88 		    &error_num))) == NULL) {
89 		(void) fprintf(stderr,
90 		gettext("Netpr: System call getipnodebyname fails\n"));
91 		syslog(LOG_DEBUG, "System call getipnodebyname fails "
92 		    "getipnodebyname() returned %d", error_num);
93 		return (-1);
94 	}
95 
96 	(void) memset(&serv_addr, 0, sizeof (struct sockaddr_in6));
97 	bcopy(hp->h_addr, (caddr_t)&serv_addr.sin6_addr, hp->h_length);
98 	serv_addr.sin6_family = hp->h_addrtype;
99 	serv_addr.sin6_port = (int)htons(atoi(tcpjob->np_port));
100 
101 	do {
102 
103 	retry = 0;
104 	rtnerr = 0;
105 	/*
106 	 * Try connecting to the printer.
107 	 */
108 	s = socket(PF_INET6, SOCK_STREAM, 0);
109 	if (s < 0) {
110 		(void) fprintf(stderr,
111 		    gettext("Netpr: System call socket fails\n"));
112 		syslog(LOG_DEBUG, "System call socket fails");
113 		rtnerr = -1;
114 	} else {
115 		(void) signal(SIGALRM, null_sighandler);
116 		(void) alarm(timeout);
117 		if (connect(s, (struct sockaddr *)&serv_addr,
118 					sizeof (serv_addr)) < 0) {
119 			err = errno;
120 			(void) alarm(0);
121 			errno = err;
122 
123 			if (errno == ECONNREFUSED && timo <= 16) {
124 				(void) sleep(timo);
125 				timo *= 2;
126 				retry++;
127 			} else {
128 			(void) fprintf(stderr,
129 				gettext("Netpr: Cannot connect to printer\n"));
130 				syslog(LOG_DEBUG, "Cannot connect to printer");
131 				rtnerr = -1;
132 			}
133 			/* The connect on this socket failed; close it */
134 			(void) close(s);
135 		} else
136 			(void) alarm(0);
137 	}
138 
139 	} while (retry);
140 
141 	return ((rtnerr) ? rtnerr : s);
142 }
143 
144 
145 int
146 tcp_print(int sockfd, caddr_t pa, np_tcpjob_t *tcpjob)
147 {
148 	char c;
149 	int xfer;
150 	char buf[BUFSIZ + 1];
151 	int nr = 0;
152 	int ctr = 0;
153 	int msg_given = 0;
154 	int nlps = 0;
155 
156 	if ((xfer = xfer_file(sockfd, pa,
157 		tcpjob->gen_data->filesize, tcpjob->gen_data->timeout)) < 0) {
158 			return (xfer);
159 	}
160 
161 	if ((shutdown(sockfd, 1)) != 0) {
162 		(void) fprintf(stderr,
163 		gettext("Netpr: System error: possible loss of data\n"));
164 			syslog(LOG_DEBUG,
165 				"shutdown error; possible loss of data");
166 		return (E_SYSTEM_ERROR);
167 	}
168 
169 
170 	/* read in single character ack or msg from printer */
171 
172 	(void) memset(buf, 0, BUFSIZ + 1);
173 	while (ctr < BUFSIZ) {
174 		(void) signal(SIGALRM, null_sighandler);
175 		(void) alarm(2);
176 		errno = 0;
177 		nr = read(sockfd, &c, 1);
178 		(void) alarm(0);
179 		if (errno == EINTR) {
180 			if (msg_given == 0) {
181 				tell_lptell(ERRORMSG,
182 				gettext("Printer not responding;" \
183 				"Either warming up or needs attention\n"));
184 				msg_given++;
185 			}
186 
187 			/* if no ACK received, do not loop forever */
188 
189 			if (nlps++ >= MAX_NLPS) {
190 				syslog(LOG_DEBUG, "No final ack received");
191 				break;
192 			}
193 		} else {
194 			if ((buf[ctr++] = c) == '\n' || (nr == 0))
195 				break;
196 		}
197 	}
198 	if (ctr > 1)
199 		syslog(LOG_DEBUG, "Message from tcp printer on read: %s",
200 			buf);
201 
202 	if (msg_given && (nlps < MAX_NLPS)) {
203 		(void) fprintf(stderr, gettext("Printer ok\n"));
204 		tell_lptell(OKMSG, "Current");
205 	}
206 
207 	return (E_SUCCESS);
208 }
209