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.
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
38 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
47 #define MAX_NLPS 60 /* Max no. loops in while */
50 create_tcp_job(np_job_t
*genjob
, int filesize
)
57 tcpjob
= (np_tcpjob_t
*)malloc(sizeof (np_tcpjob_t
));
58 ASSERT(tcpjob
, MALLOC_ERR
);
59 (void) memset(tcpjob
, 0, sizeof (np_tcpjob_t
));
61 tcpjob
->np_port
= "9100";
62 tcpjob
->gen_data
= genjob
;
63 tcpjob
->gen_data
->filesize
= filesize
;
69 tcp_open(char *dest
, np_tcpjob_t
*tcpjob
, int timeout
)
72 struct sockaddr_in6 serv_addr
;
81 * Get the host address and port number to connect to.
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
);
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
));
106 * Try connecting to the printer.
108 s
= socket(PF_INET6
, SOCK_STREAM
, 0);
110 (void) fprintf(stderr
,
111 gettext("Netpr: System call socket fails\n"));
112 syslog(LOG_DEBUG
, "System call socket fails");
115 (void) signal(SIGALRM
, null_sighandler
);
116 (void) alarm(timeout
);
117 if (connect(s
, (struct sockaddr
*)&serv_addr
,
118 sizeof (serv_addr
)) < 0) {
123 if (errno
== ECONNREFUSED
&& timo
<= 16) {
128 (void) fprintf(stderr
,
129 gettext("Netpr: Cannot connect to printer\n"));
130 syslog(LOG_DEBUG
, "Cannot connect to printer");
133 /* The connect on this socket failed; close it */
141 return ((rtnerr
) ? rtnerr
: s
);
146 tcp_print(int sockfd
, caddr_t pa
, np_tcpjob_t
*tcpjob
)
150 char buf
[BUFSIZ
+ 1];
156 if ((xfer
= xfer_file(sockfd
, pa
,
157 tcpjob
->gen_data
->filesize
, tcpjob
->gen_data
->timeout
)) < 0) {
161 if ((shutdown(sockfd
, 1)) != 0) {
162 (void) fprintf(stderr
,
163 gettext("Netpr: System error: possible loss of data\n"));
165 "shutdown error; possible loss of data");
166 return (E_SYSTEM_ERROR
);
170 /* read in single character ack or msg from printer */
172 (void) memset(buf
, 0, BUFSIZ
+ 1);
173 while (ctr
< BUFSIZ
) {
174 (void) signal(SIGALRM
, null_sighandler
);
177 nr
= read(sockfd
, &c
, 1);
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"));
187 /* if no ACK received, do not loop forever */
189 if (nlps
++ >= MAX_NLPS
) {
190 syslog(LOG_DEBUG
, "No final ack received");
194 if ((buf
[ctr
++] = c
) == '\n' || (nr
== 0))
199 syslog(LOG_DEBUG
, "Message from tcp printer on read: %s",
202 if (msg_given
&& (nlps
< MAX_NLPS
)) {
203 (void) fprintf(stderr
, gettext("Printer ok\n"));
204 tell_lptell(OKMSG
, "Current");