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 #include <stdio.h>
28 #include <stdlib.h>
29 #include <libintl.h>
30 #include <signal.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <syslog.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <netdb.h>
42 #include "netpr.h"
43 #include "netdebug.h"
44
45 #define MAX_NLPS 60 /* Max no. loops in while */
46
47 np_tcpjob_t *
create_tcp_job(np_job_t * genjob,int filesize)48 create_tcp_job(np_job_t *genjob, int filesize)
49 {
50 np_tcpjob_t *tcpjob;
51
52 if (genjob == NULL)
53 return (NULL);
54
55 tcpjob = (np_tcpjob_t *)malloc(sizeof (np_tcpjob_t));
56 ASSERT(tcpjob, MALLOC_ERR);
57 (void) memset(tcpjob, 0, sizeof (np_tcpjob_t));
58
59 tcpjob->np_port = "9100";
60 tcpjob->gen_data = genjob;
61 tcpjob->gen_data->filesize = filesize;
62
63 return (tcpjob);
64 }
65
66 int
tcp_open(char * dest,np_tcpjob_t * tcpjob,int timeout)67 tcp_open(char *dest, np_tcpjob_t *tcpjob, int timeout)
68 {
69 struct hostent *hp;
70 struct sockaddr_in6 serv_addr;
71 int s,
72 err,
73 error_num;
74 unsigned timo = 1;
75 int retry;
76 int rtnerr;
77
78 /*
79 * Get the host address and port number to connect to.
80 */
81 if (dest == NULL) {
82 return (-1);
83 }
84
85 if ((hp = (getipnodebyname(dest, AF_INET6, AI_DEFAULT,
86 &error_num))) == NULL) {
87 (void) fprintf(stderr,
88 gettext("Netpr: System call getipnodebyname fails\n"));
89 syslog(LOG_DEBUG, "System call getipnodebyname fails "
90 "getipnodebyname() returned %d", error_num);
91 return (-1);
92 }
93
94 (void) memset(&serv_addr, 0, sizeof (struct sockaddr_in6));
95 bcopy(hp->h_addr, (caddr_t)&serv_addr.sin6_addr, hp->h_length);
96 serv_addr.sin6_family = hp->h_addrtype;
97 serv_addr.sin6_port = (int)htons(atoi(tcpjob->np_port));
98
99 do {
100
101 retry = 0;
102 rtnerr = 0;
103 /*
104 * Try connecting to the printer.
105 */
106 s = socket(PF_INET6, SOCK_STREAM, 0);
107 if (s < 0) {
108 (void) fprintf(stderr,
109 gettext("Netpr: System call socket fails\n"));
110 syslog(LOG_DEBUG, "System call socket fails");
111 rtnerr = -1;
112 } else {
113 (void) signal(SIGALRM, null_sighandler);
114 (void) alarm(timeout);
115 if (connect(s, (struct sockaddr *)&serv_addr,
116 sizeof (serv_addr)) < 0) {
117 err = errno;
118 (void) alarm(0);
119 errno = err;
120
121 if (errno == ECONNREFUSED && timo <= 16) {
122 (void) sleep(timo);
123 timo *= 2;
124 retry++;
125 } else {
126 (void) fprintf(stderr,
127 gettext("Netpr: Cannot connect to printer\n"));
128 syslog(LOG_DEBUG, "Cannot connect to printer");
129 rtnerr = -1;
130 }
131 /* The connect on this socket failed; close it */
132 (void) close(s);
133 } else
134 (void) alarm(0);
135 }
136
137 } while (retry);
138
139 return ((rtnerr) ? rtnerr : s);
140 }
141
142
143 int
tcp_print(int sockfd,caddr_t pa,np_tcpjob_t * tcpjob)144 tcp_print(int sockfd, caddr_t pa, np_tcpjob_t *tcpjob)
145 {
146 char c;
147 int xfer;
148 char buf[BUFSIZ + 1];
149 int nr = 0;
150 int ctr = 0;
151 int msg_given = 0;
152 int nlps = 0;
153
154 if ((xfer = xfer_file(sockfd, pa,
155 tcpjob->gen_data->filesize, tcpjob->gen_data->timeout)) < 0) {
156 return (xfer);
157 }
158
159 if ((shutdown(sockfd, 1)) != 0) {
160 (void) fprintf(stderr,
161 gettext("Netpr: System error: possible loss of data\n"));
162 syslog(LOG_DEBUG,
163 "shutdown error; possible loss of data");
164 return (E_SYSTEM_ERROR);
165 }
166
167
168 /* read in single character ack or msg from printer */
169
170 (void) memset(buf, 0, BUFSIZ + 1);
171 while (ctr < BUFSIZ) {
172 (void) signal(SIGALRM, null_sighandler);
173 (void) alarm(2);
174 errno = 0;
175 nr = read(sockfd, &c, 1);
176 (void) alarm(0);
177 if (errno == EINTR) {
178 if (msg_given == 0) {
179 tell_lptell(ERRORMSG,
180 gettext("Printer not responding;" \
181 "Either warming up or needs attention\n"));
182 msg_given++;
183 }
184
185 /* if no ACK received, do not loop forever */
186
187 if (nlps++ >= MAX_NLPS) {
188 syslog(LOG_DEBUG, "No final ack received");
189 break;
190 }
191 } else {
192 if ((buf[ctr++] = c) == '\n' || (nr == 0))
193 break;
194 }
195 }
196 if (ctr > 1)
197 syslog(LOG_DEBUG, "Message from tcp printer on read: %s",
198 buf);
199
200 if (msg_given && (nlps < MAX_NLPS)) {
201 (void) fprintf(stderr, gettext("Printer ok\n"));
202 tell_lptell(OKMSG, "Current");
203 }
204
205 return (E_SUCCESS);
206 }
207