xref: /onnv-gate/usr/src/cmd/lp/model/netpr/tcp_misc.c (revision 4062:c84209043eff)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*4062Sjacobs  * Common Development and Distribution License (the "License").
6*4062Sjacobs  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
23*4062Sjacobs  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*4062Sjacobs  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <libintl.h>
320Sstevel@tonic-gate #include <signal.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <strings.h>
360Sstevel@tonic-gate #include <syslog.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <fcntl.h>
400Sstevel@tonic-gate #include <sys/socket.h>
410Sstevel@tonic-gate #include <netinet/in.h>
420Sstevel@tonic-gate #include <arpa/inet.h>
430Sstevel@tonic-gate #include <netdb.h>
440Sstevel@tonic-gate #include "netpr.h"
450Sstevel@tonic-gate #include "netdebug.h"
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define	MAX_NLPS	60	/* Max no. loops in while */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate np_tcpjob_t *
create_tcp_job(np_job_t * genjob,int filesize)500Sstevel@tonic-gate create_tcp_job(np_job_t *genjob, int filesize)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	np_tcpjob_t *tcpjob;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	if (genjob == NULL)
550Sstevel@tonic-gate 		return (NULL);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	tcpjob = (np_tcpjob_t *)malloc(sizeof (np_tcpjob_t));
580Sstevel@tonic-gate 	ASSERT(tcpjob, MALLOC_ERR);
590Sstevel@tonic-gate 	(void) memset(tcpjob, 0, sizeof (np_tcpjob_t));
600Sstevel@tonic-gate 
61*4062Sjacobs 	tcpjob->np_port = "9100";
620Sstevel@tonic-gate 	tcpjob->gen_data = genjob;
630Sstevel@tonic-gate 	tcpjob->gen_data->filesize = filesize;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	return (tcpjob);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate 
680Sstevel@tonic-gate int
tcp_open(char * dest,np_tcpjob_t * tcpjob,int timeout)690Sstevel@tonic-gate tcp_open(char *dest, np_tcpjob_t *tcpjob, int timeout)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	struct hostent *hp;
720Sstevel@tonic-gate 	struct sockaddr_in6 serv_addr;
730Sstevel@tonic-gate 	int	s,
740Sstevel@tonic-gate 		err,
750Sstevel@tonic-gate 		error_num;
760Sstevel@tonic-gate 	unsigned timo = 1;
770Sstevel@tonic-gate 	int retry;
780Sstevel@tonic-gate 	int rtnerr;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	/*
810Sstevel@tonic-gate 	 * Get the host address and port number to connect to.
820Sstevel@tonic-gate 	 */
830Sstevel@tonic-gate 	if (dest == NULL) {
840Sstevel@tonic-gate 		return (-1);
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	if ((hp = (getipnodebyname(dest, AF_INET6, AI_DEFAULT,
880Sstevel@tonic-gate 		    &error_num))) == NULL) {
890Sstevel@tonic-gate 		(void) fprintf(stderr,
900Sstevel@tonic-gate 		gettext("Netpr: System call getipnodebyname fails\n"));
910Sstevel@tonic-gate 		syslog(LOG_DEBUG, "System call getipnodebyname fails "
920Sstevel@tonic-gate 		    "getipnodebyname() returned %d", error_num);
930Sstevel@tonic-gate 		return (-1);
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	(void) memset(&serv_addr, 0, sizeof (struct sockaddr_in6));
970Sstevel@tonic-gate 	bcopy(hp->h_addr, (caddr_t)&serv_addr.sin6_addr, hp->h_length);
980Sstevel@tonic-gate 	serv_addr.sin6_family = hp->h_addrtype;
990Sstevel@tonic-gate 	serv_addr.sin6_port = (int)htons(atoi(tcpjob->np_port));
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	do {
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	retry = 0;
1040Sstevel@tonic-gate 	rtnerr = 0;
1050Sstevel@tonic-gate 	/*
1060Sstevel@tonic-gate 	 * Try connecting to the printer.
1070Sstevel@tonic-gate 	 */
1080Sstevel@tonic-gate 	s = socket(PF_INET6, SOCK_STREAM, 0);
1090Sstevel@tonic-gate 	if (s < 0) {
1100Sstevel@tonic-gate 		(void) fprintf(stderr,
1110Sstevel@tonic-gate 		    gettext("Netpr: System call socket fails\n"));
1120Sstevel@tonic-gate 		syslog(LOG_DEBUG, "System call socket fails");
1130Sstevel@tonic-gate 		rtnerr = -1;
1140Sstevel@tonic-gate 	} else {
1150Sstevel@tonic-gate 		(void) signal(SIGALRM, null_sighandler);
1160Sstevel@tonic-gate 		(void) alarm(timeout);
1170Sstevel@tonic-gate 		if (connect(s, (struct sockaddr *)&serv_addr,
1180Sstevel@tonic-gate 					sizeof (serv_addr)) < 0) {
1190Sstevel@tonic-gate 			err = errno;
1200Sstevel@tonic-gate 			(void) alarm(0);
1210Sstevel@tonic-gate 			errno = err;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 			if (errno == ECONNREFUSED && timo <= 16) {
1240Sstevel@tonic-gate 				(void) sleep(timo);
1250Sstevel@tonic-gate 				timo *= 2;
1260Sstevel@tonic-gate 				retry++;
1270Sstevel@tonic-gate 			} else {
1280Sstevel@tonic-gate 			(void) fprintf(stderr,
1290Sstevel@tonic-gate 				gettext("Netpr: Cannot connect to printer\n"));
1300Sstevel@tonic-gate 				syslog(LOG_DEBUG, "Cannot connect to printer");
1310Sstevel@tonic-gate 				rtnerr = -1;
1320Sstevel@tonic-gate 			}
1330Sstevel@tonic-gate 			/* The connect on this socket failed; close it */
1340Sstevel@tonic-gate 			(void) close(s);
1350Sstevel@tonic-gate 		} else
1360Sstevel@tonic-gate 			(void) alarm(0);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	} while (retry);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	return ((rtnerr) ? rtnerr : s);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate int
tcp_print(int sockfd,caddr_t pa,np_tcpjob_t * tcpjob)1460Sstevel@tonic-gate tcp_print(int sockfd, caddr_t pa, np_tcpjob_t *tcpjob)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	char c;
1490Sstevel@tonic-gate 	int xfer;
1500Sstevel@tonic-gate 	char buf[BUFSIZ + 1];
1510Sstevel@tonic-gate 	int nr = 0;
1520Sstevel@tonic-gate 	int ctr = 0;
1530Sstevel@tonic-gate 	int msg_given = 0;
1540Sstevel@tonic-gate 	int nlps = 0;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if ((xfer = xfer_file(sockfd, pa,
1570Sstevel@tonic-gate 		tcpjob->gen_data->filesize, tcpjob->gen_data->timeout)) < 0) {
1580Sstevel@tonic-gate 			return (xfer);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if ((shutdown(sockfd, 1)) != 0) {
1620Sstevel@tonic-gate 		(void) fprintf(stderr,
1630Sstevel@tonic-gate 		gettext("Netpr: System error: possible loss of data\n"));
1640Sstevel@tonic-gate 			syslog(LOG_DEBUG,
1650Sstevel@tonic-gate 				"shutdown error; possible loss of data");
1660Sstevel@tonic-gate 		return (E_SYSTEM_ERROR);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/* read in single character ack or msg from printer */
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	(void) memset(buf, 0, BUFSIZ + 1);
1730Sstevel@tonic-gate 	while (ctr < BUFSIZ) {
1740Sstevel@tonic-gate 		(void) signal(SIGALRM, null_sighandler);
1750Sstevel@tonic-gate 		(void) alarm(2);
1760Sstevel@tonic-gate 		errno = 0;
1770Sstevel@tonic-gate 		nr = read(sockfd, &c, 1);
1780Sstevel@tonic-gate 		(void) alarm(0);
1790Sstevel@tonic-gate 		if (errno == EINTR) {
1800Sstevel@tonic-gate 			if (msg_given == 0) {
1810Sstevel@tonic-gate 				tell_lptell(ERRORMSG,
1820Sstevel@tonic-gate 				gettext("Printer not responding;" \
1830Sstevel@tonic-gate 				"Either warming up or needs attention\n"));
1840Sstevel@tonic-gate 				msg_given++;
1850Sstevel@tonic-gate 			}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 			/* if no ACK received, do not loop forever */
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 			if (nlps++ >= MAX_NLPS) {
1900Sstevel@tonic-gate 				syslog(LOG_DEBUG, "No final ack received");
1910Sstevel@tonic-gate 				break;
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 		} else {
1940Sstevel@tonic-gate 			if ((buf[ctr++] = c) == '\n' || (nr == 0))
1950Sstevel@tonic-gate 				break;
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	if (ctr > 1)
1990Sstevel@tonic-gate 		syslog(LOG_DEBUG, "Message from tcp printer on read: %s",
2000Sstevel@tonic-gate 			buf);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	if (msg_given && (nlps < MAX_NLPS)) {
2030Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Printer ok\n"));
2040Sstevel@tonic-gate 		tell_lptell(OKMSG, "Current");
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	return (E_SUCCESS);
2080Sstevel@tonic-gate }
209