xref: /titanic_52/usr/src/cmd/lp/model/netpr/tcp_misc.c (revision 2983dda76a6d296fdb560c88114fe41caad1b84f)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <libintl.h>
32 #include <signal.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <syslog.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <fcntl.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include <netdb.h>
44 #include "netpr.h"
45 #include "netdebug.h"
46 
47 #define	MAX_NLPS	60	/* Max no. loops in while */
48 
49 np_tcpjob_t *
50 create_tcp_job(np_job_t *genjob, int filesize)
51 {
52 	np_tcpjob_t *tcpjob;
53 
54 	if (genjob == NULL)
55 		return (NULL);
56 
57 	tcpjob = (np_tcpjob_t *)malloc(sizeof (np_tcpjob_t));
58 	ASSERT(tcpjob, MALLOC_ERR);
59 	(void) memset(tcpjob, 0, sizeof (np_tcpjob_t));
60 
61 	tcpjob->np_port = "9100";
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