2 * $Id: pap.c,v 1.11 2009-10-13 22:55:36 didg Exp $
4 * Copyright (c) 1990,1994 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
12 #include <sys/types.h>
15 #include <netatalk/endian.h>
16 #include <netatalk/at.h>
18 #include <atalk/atp.h>
19 #include <atalk/pap.h>
20 #include <atalk/nbp.h>
21 #include <atalk/util.h>
24 #endif /* HAVE_FCNTL_H */
32 #define _PATH_PAPRC ".paprc"
33 char *nbpfailure = "AppleTalk printer offline";
35 /* Forward Declarations */
36 void updatestatus(char *s, int len);
37 int send_file(int fd, ATP atp, int lastfile);
39 void usage(char *path)
43 if (( p = strrchr( path, '/' )) == NULL ) {
49 "Usage:\t%s [ -A address ] [ -c ] [ -d ] [ -e ] [ -E ] [ -p printer ]\n"
50 " [ -s statusfile ] [ -w ] [ -W ] [ FILES ]\n"
51 " -A address - printer Appletalk address\n"
52 " -c - take cuts (lie about wait time)\n"
53 " -d - enable debug\n"
54 " -e - send stdout to stderr\n"
55 " -E - don't wait for EOF from printer\n"
56 " -p printer - printer name\n"
57 " -s statusfile - put current printer status in statusfile\n"
58 " -w - wait for printer status = 'waiting'\n"
59 " -W - wait for printer status = 'idle'\n"
60 " FILES - send FILES to printer\n"
68 static char s[ 32 + 1 + 32 + 1 + 32 ];
72 if (( f = fopen( _PATH_PAPRC, "r" )) == NULL ) {
73 if ( errno == ENOENT ) {
76 perror( _PATH_PAPRC );
80 while ( fgets( s, sizeof( s ), f ) != NULL ) {
81 s[ strlen( s ) - 1 ] = '\0'; /* remove trailing newline */
95 int waitforprinter = 0;
97 unsigned char connid, quantum, oquantum = PAP_MAXQUANTUM;
98 struct sockaddr_at sat;
104 char fbuf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
105 struct iovec rfiov[ PAP_MAXQUANTUM ] = {
106 { fbuf[ 0 ] + 4, 0 },
107 { fbuf[ 1 ] + 4, 0 },
108 { fbuf[ 2 ] + 4, 0 },
109 { fbuf[ 3 ] + 4, 0 },
110 { fbuf[ 4 ] + 4, 0 },
111 { fbuf[ 5 ] + 4, 0 },
112 { fbuf[ 6 ] + 4, 0 },
113 { fbuf[ 7 ] + 4, 0 },
115 struct iovec sniov[ PAP_MAXQUANTUM ] = {
126 char nbuf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
127 struct iovec rniov[ PAP_MAXQUANTUM ] = {
137 struct iovec sfiov[ PAP_MAXQUANTUM ] = {
138 { nbuf[ 0 ] + 4, 0 },
139 { nbuf[ 1 ] + 4, 0 },
140 { nbuf[ 2 ] + 4, 0 },
141 { nbuf[ 3 ] + 4, 0 },
142 { nbuf[ 4 ] + 4, 0 },
143 { nbuf[ 5 ] + 4, 0 },
144 { nbuf[ 6 ] + 4, 0 },
145 { nbuf[ 7 ] + 4, 0 },
150 int main( int ac, char **av)
153 struct atp_block atpb;
154 int c, err = 0, fd, cuts = 0;
155 char *obj = NULL, *type = "LaserWriter", *zone = "*";
156 struct timeval stv, tv;
157 char rbuf[ ATP_MAXDATA ];
159 unsigned short waiting, result;
160 int connattempts = 10;
167 memset(&addr, 0, sizeof(addr));
168 while (( c = getopt( ac, av, "dWwcep:s:EA:" )) != EOF ) {
180 /* enable debugging */
189 case 'e' : /* send stdout to stderr */
206 if (!atalk_aton(optarg, &addr)) {
207 fprintf(stderr, "Bad address.\n");
219 if ( printer == NULL && (( printer = paprc()) == NULL )) {
220 fprintf( stderr, "No printer specified and ./.paprc not found.\n" );
227 if ( nbp_name( printer, &obj, &type, &zone ) < 0 ) {
228 fprintf( stderr, "%s: Bad name\n", printer );
232 fprintf( stderr, "%s: Bad name\n", printer );
236 if ( nbp_lookup( obj, type, zone, &nn, 1, &addr ) <= 0 ) {
238 perror( "nbp_lookup" );
241 fprintf( stderr, "%s:%s@%s: NBP Lookup failed\n", obj, type, zone );
246 printf( "Trying %u.%d:%d ...\n", ntohs( nn.nn_sat.sat_addr.s_net ),
247 nn.nn_sat.sat_addr.s_node, nn.nn_sat.sat_port );
250 if (( atp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
251 perror( "atp_open" );
254 if (( satp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
255 perror( "atp_open" );
259 while ( waitforidle ) {
260 char st_buf[ 1024 ]; /* XXX too big */
263 cbuf[ 1 ] = PAP_SENDSTATUS;
264 cbuf[ 2 ] = cbuf[ 3 ] = 0;
265 atpb.atp_saddr = &nn.nn_sat;
266 atpb.atp_sreqdata = cbuf;
267 atpb.atp_sreqdlen = 4; /* bytes in SendStatus request */
268 atpb.atp_sreqto = 2; /* retry timer */
269 atpb.atp_sreqtries = 5; /* retry count */
270 if ( atp_sreq( satp, &atpb, 1, 0 ) < 0 ) {
271 perror( "atp_sreq" );
275 if(debug){ printf( "SENDSTATUS >\n" ), fflush( stdout );}
277 atpb.atp_saddr = &nn.nn_sat;
278 rniov[ 0 ].iov_len = PAP_MAXDATA + 4;
279 atpb.atp_rresiov = rniov;
280 atpb.atp_rresiovcnt = 1;
281 if ( atp_rresp( satp, &atpb ) < 0 ) {
282 perror( "atp_rresp" );
286 #ifndef NONZEROSTATUS
288 * The stinking LaserWriter IINTX puts crap in this
291 if ( ((char *)rniov[ 0 ].iov_base)[ 0 ] != 0 ) {
292 fprintf( stderr, "Bad status response!\n" );
295 #endif /* NONZEROSTATUS */
297 if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_STATUS ||
298 atpb.atp_rresiovcnt != 1 ) {
299 fprintf( stderr, "Bad status response!\n" );
303 if(debug){ printf( "< STATUS\n" ), fflush( stdout );}
305 memcpy( st_buf, (char *) rniov[ 0 ].iov_base + 9,
306 ((char *)rniov[ 0 ].iov_base)[ 8 ] );
307 st_buf[ (int) ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
308 if ( strstr( st_buf, "idle" ) != NULL ) {
311 updatestatus( (char *) rniov[ 0 ].iov_base + 9,
312 ((char *)rniov[ 0 ].iov_base)[ 8 ] );
317 cbuf[ 0 ] = connid = getpid() & 0xff;
318 cbuf[ 1 ] = PAP_OPEN;
319 cbuf[ 2 ] = cbuf[ 3 ] = 0;
320 cbuf[ 4 ] = atp_sockaddr( atp )->sat_port;
321 cbuf[ 5 ] = oquantum; /* flow quantum */
322 if ( gettimeofday( &stv, 0 ) < 0 ) {
323 perror( "gettimeofday" );
330 if ( gettimeofday( &tv, 0 ) < 0 ) {
331 perror( "gettimeofday" );
334 waiting = htons( tv.tv_sec - stv.tv_sec );
336 memcpy(cbuf + 6, &waiting, sizeof( waiting ));
338 atpb.atp_saddr = &nn.nn_sat;
339 atpb.atp_sreqdata = cbuf;
340 atpb.atp_sreqdlen = 8; /* bytes in OpenConn request */
341 atpb.atp_sreqto = 2; /* retry timer */
342 atpb.atp_sreqtries = 5; /* retry count */
343 if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
344 perror( "atp_sreq" );
348 if(debug){ printf( "OPEN >\n" ), fflush( stdout );}
351 iov.iov_len = sizeof( rbuf );
352 atpb.atp_rresiov = &iov;
353 atpb.atp_rresiovcnt = 1;
354 if ( atp_rresp( atp, &atpb ) < 0 ) {
355 perror( "atp_rresp" );
356 if ( connattempts-- <= 0 ) {
357 fprintf( stderr, "Can't connect!\n" );
364 if ( iov.iov_len < 8 || (unsigned char)rbuf[ 0 ] != connid ||
365 rbuf[ 1 ] != PAP_OPENREPLY ) {
366 fprintf( stderr, "Bad response!\n" );
367 continue; /* This is weird, since TIDs must match... */
370 if(debug){ printf( "< OPENREPLY\n" ), fflush( stdout );}
373 printf( "%.*s\n", (int)iov.iov_len - 9, (char *) iov.iov_base + 9 );
375 updatestatus( (char *) iov.iov_base + 9, iov.iov_len - 9 );
377 memcpy( &result, rbuf + 6, sizeof( result ));
381 memcpy( &sat, &nn.nn_sat, sizeof( struct sockaddr_at ));
382 sat.sat_port = rbuf[ 4 ];
389 printf( "Connected to %.*s:%.*s@%.*s.\n",
390 nn.nn_objlen, nn.nn_obj,
391 nn.nn_typelen, nn.nn_type,
392 nn.nn_zonelen, nn.nn_zone );
395 if ( optind == ac ) {
396 send_file( 0, atp, 1 );
398 for (; optind < ac; optind++ ) {
399 if ( strcmp( av[ optind ], "-" ) == 0 ) {
401 } else if (( fd = open( av[ optind ], O_RDONLY )) < 0 ) {
402 perror( av[ optind ] );
405 send_file( fd, atp, ( optind == ac - 1 ) ? 1 : 0 );
416 cbuf[ 1 ] = PAP_CLOSE;
417 cbuf[ 2 ] = cbuf[ 3 ] = 0;
419 atpb.atp_saddr = &sat;
420 atpb.atp_sreqdata = cbuf;
421 atpb.atp_sreqdlen = 4; /* bytes in CloseConn request */
422 atpb.atp_sreqto = 2; /* retry timer */
423 atpb.atp_sreqtries = 5; /* retry count */
424 if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
425 perror( "atp_sreq" );
429 if(debug){ printf( "CLOSE >\n" ), fflush( stdout );}
432 iov.iov_len = sizeof( rbuf );
433 atpb.atp_rresiov = &iov;
434 atpb.atp_rresiovcnt = 1;
435 if ( atp_rresp( atp, &atpb ) < 0 ) {
436 perror( "atp_rresp" );
441 if ( iov.iov_len != 4 || rbuf[ 1 ] != PAP_CLOSEREPLY ) {
442 fprintf( stderr, "Bad response!\n" );
448 * The AGFA Viper Rip doesn't have the connection id in the close request.
450 if ((unsigned char)rbuf[ 0 ] != connid ) {
451 fprintf( stderr, "Bad connid in close!\n" );
454 #endif /* ZEROCONNID */
456 if(debug){ printf( "< CLOSEREPLY\n" ), fflush( stdout );}
459 printf( "Connection closed.\n" );
466 u_int16_t seq = 0, rseq = 1;
468 int send_file( int fd, ATP atp, int lastfile)
470 struct timeval stv, tv;
471 struct sockaddr_at ssat;
472 struct atp_block atpb;
474 int fiovcnt = 0, eof = 0, senteof = 0, to = 0;
476 unsigned short netseq;
478 if ( gettimeofday( &stv, 0 ) < 0 ) {
479 perror( "gettimeofday" );
487 cbuf[ 1 ] = PAP_READ;
488 if ( ++seq == 0 ) seq = 1;
489 netseq = htons( seq );
490 memcpy( cbuf + 2, &netseq, sizeof( netseq ));
491 atpb.atp_saddr = &sat;
492 atpb.atp_sreqdata = cbuf;
493 atpb.atp_sreqdlen = 4; /* bytes in SendData request */
494 atpb.atp_sreqto = 15; /* retry timer */
495 atpb.atp_sreqtries = -1; /* retry count */
496 if ( atp_sreq( atp, &atpb, oquantum, ATP_XO ) < 0 ) {
497 perror( "atp_sreq" );
501 if(debug){ printf( "READ %d >\n", seq ), fflush( stdout );}
504 if ( gettimeofday( &tv, 0 ) < 0 ) {
505 perror( "gettimeofday" );
509 if (( tv.tv_sec - stv.tv_sec ) >= 60 ) {
516 cbuf[ 1 ] = PAP_TICKLE;
517 cbuf[ 2 ] = cbuf[ 3 ] = 0;
518 atpb.atp_saddr = &sat;
519 atpb.atp_sreqdata = cbuf;
520 atpb.atp_sreqdlen = 4; /* bytes in Tickle request */
521 atpb.atp_sreqto = 0; /* retry timer */
522 atpb.atp_sreqtries = 1; /* retry count */
523 if ( atp_sreq( satp, &atpb, 0, 0 ) < 0 ) {
524 perror( "atp_sreq" );
528 if(debug){ printf( "TICKLE >\n" ), fflush( stdout );}
531 tv.tv_sec = stv.tv_sec + 60 - tv.tv_sec;
535 if ( !waitforprinter && !eof && fiovcnt == 0 ) {
538 FD_SET( atp_fileno( atp ), &fds );
540 if (( cc = select( FD_SETSIZE, &fds, 0, 0, &tv )) < 0 ) {
546 * A timeout has occured. Keep track of it.
550 fprintf( stderr, "Connection timed out.\n" );
559 if ( !fiovcnt && FD_ISSET( fd, &fds )) {
560 for ( i = 0; i < quantum; i++ ) {
561 rfiov[ i ].iov_len = PAP_MAXDATA;
563 if (( cc = readv( fd, rfiov, quantum )) < 0 ) {
570 fiovcnt = cc / PAP_MAXDATA + ( cc % PAP_MAXDATA > 0 );
571 for ( i = 0; cc > 0; i++ ) {
572 rfiov[ i ].iov_len = ( cc > PAP_MAXDATA ) ? PAP_MAXDATA : cc;
573 cc -= ( cc > PAP_MAXDATA ) ? PAP_MAXDATA : cc;
577 if ( FD_ISSET( atp_fileno( atp ), &fds )) {
579 ssat.sat_port = ATADDR_ANYPORT;
580 switch( atp_rsel( atp, &ssat, ATP_TRESP | ATP_TREQ )) {
582 atpb.atp_saddr = &ssat;
583 atpb.atp_rreqdata = cbuf;
584 atpb.atp_rreqdlen = sizeof( cbuf );
585 if ( atp_rreq( atp, &atpb ) < 0 ) {
586 perror( "atp_rreq" );
590 if ( (unsigned char)cbuf[ 0 ] != connid ) {
594 /* reset timeout counter for all valid requests */
597 switch ( cbuf[ 1 ] ) {
599 memcpy( cbuf + 2, &netseq, sizeof( netseq ));
600 if(debug){ printf( "< READ %d\n", ntohs( netseq )), fflush( stdout );}
603 if ( rseq != ntohs( netseq )) {
604 if(debug){ printf( "| DUP %d\n", rseq ), fflush( stdout );}
607 if ( rseq++ == 0xffff ) rseq = 1;
612 port = ssat.sat_port;
617 if(debug){ printf( "< CLOSE\n" ), fflush( stdout );}
620 * Respond to the close request, and fail.
622 sniov[ 0 ].iov_len = 4;
623 ((char *)sniov[ 0 ].iov_base)[ 0 ] = connid;
624 ((char *)sniov[ 0 ].iov_base)[ 1 ] = PAP_CLOSEREPLY;
625 ((char *)sniov[ 0 ].iov_base)[ 2 ] =
626 ((char *)sniov[ 0 ].iov_base)[ 3 ] = 0;
627 atpb.atp_sresiov = sniov;
628 atpb.atp_sresiovcnt = 1;
629 if ( atp_sresp( atp, &atpb ) < 0 ) {
630 perror( "atp_sresp" );
634 if(debug){ printf( "CLOSEREPLY >\n" ), fflush( stdout );}
636 fprintf( stderr, "Connection closed by foreign host.\n" );
641 if(debug){ printf( "< TICKLE\n" ), fflush( stdout );}
645 fprintf( stderr, "Bad PAP request!\n" );
651 /* reset timeout counter for all valid requests */
654 atpb.atp_saddr = &ssat;
655 for ( i = 0; i < oquantum; i++ ) {
656 rniov[ i ].iov_len = PAP_MAXDATA + 4;
658 atpb.atp_rresiov = rniov;
659 atpb.atp_rresiovcnt = oquantum;
660 if ( atp_rresp( atp, &atpb ) < 0 ) {
661 perror( "atp_rresp" );
667 * The HP LJIIISI w/ BridgePort LocalTalk card sends
668 * zero instead of the connid.
670 if ( ((unsigned char *)rniov[ 0 ].iov_base)[ 0 ] != connid ) {
671 fprintf( stderr, "Bad data response!\n" );
674 #endif /* ZEROCONNID */
675 if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_DATA ) {
676 fprintf( stderr, "Bad data response!\n" );
680 for ( cc = 0, i = 0; i < atpb.atp_rresiovcnt; i++ ) {
681 sfiov[ i ].iov_len = rniov[ i ].iov_len - 4;
682 cc += sfiov[ i ].iov_len;
684 if ( cc && writev( 1, sfiov, atpb.atp_rresiovcnt ) < cc ) {
690 if ( ((char *)rniov[ 0 ].iov_base)[ 2 ] ) {
692 if(debug){ printf( "< DATA (eof)\n" ), fflush( stdout );}
697 if(debug){ printf( "< DATA\n" ), fflush( stdout );}
704 cbuf[ 1 ] = PAP_READ;
705 if ( ++seq == 0 ) seq = 1;
706 netseq = htons( seq );
707 memcpy( cbuf + 2, &netseq, sizeof( netseq ));
708 atpb.atp_saddr = &sat;
709 atpb.atp_sreqdata = cbuf;
710 atpb.atp_sreqdlen = 4; /* bytes in SendData request */
711 atpb.atp_sreqto = 15; /* retry timer */
712 atpb.atp_sreqtries = -1; /* retry count */
713 if ( atp_sreq( atp, &atpb, oquantum, ATP_XO ) < 0 ) {
714 perror( "atp_sreq" );
718 if(debug){ printf( "READ %d >\n", seq ), fflush( stdout );}
724 if(debug){ printf( "| RETRANS\n" ), fflush( stdout );}
729 perror( "atp_rsel" );
735 * Send whatever is pending.
737 if ( !waitforprinter && !senteof && data && ( fiovcnt || eof )) {
738 ssat.sat_port = port;
739 atpb.atp_saddr = &ssat;
741 for ( i = 0; i < fiovcnt; i++ ) {
742 sniov[ i ].iov_len = rfiov[ i ].iov_len + 4;
743 ((char *)sniov[ i ].iov_base)[ 0 ] = connid;
744 ((char *)sniov[ i ].iov_base)[ 1 ] = PAP_DATA;
745 senteof = ((char *)sniov[ i ].iov_base)[ 2 ] = eof;
746 ((char *)sniov[ i ].iov_base)[ 3 ] = 0;
749 sniov[ 0 ].iov_len = 4;
750 ((char *)sniov[ 0 ].iov_base)[ 0 ] = connid;
751 ((char *)sniov[ 0 ].iov_base)[ 1 ] = PAP_DATA;
752 senteof = ((char *)sniov[ 0 ].iov_base)[ 2 ] = eof;
753 ((char *)sniov[ 0 ].iov_base)[ 3 ] = 0;
755 atpb.atp_sresiov = sniov;
756 atpb.atp_sresiovcnt = fiovcnt ? fiovcnt : 1;
757 if ( atp_sresp( atp, &atpb ) < 0 ) {
758 perror( "atp_sresp" );
763 if(debug){ printf( "DATA %s\n", eof ? "(eof) >" : ">" ), fflush( stdout );}
766 * The Apple LaserWriter IIf, the HP LWIIISi, and IV, don't
767 * seem to send us an EOF on large jobs. To work around
768 * this heinous protocol violation, we won't wait for their
769 * EOF before closing.
771 if ( eof && noeof && lastfile ) {
776 * If we can't send data right now, go ahead and get the
777 * status. This is cool, because we get here reliably
778 * if there is a problem.
781 cbuf[ 1 ] = PAP_SENDSTATUS;
782 cbuf[ 2 ] = cbuf[ 3 ] = 0;
783 atpb.atp_saddr = &nn.nn_sat;
784 atpb.atp_sreqdata = cbuf;
785 atpb.atp_sreqdlen = 4; /* bytes in SendStatus request */
786 atpb.atp_sreqto = 2; /* retry timer */
787 atpb.atp_sreqtries = 5; /* retry count */
788 if ( atp_sreq( satp, &atpb, 1, 0 ) < 0 ) {
789 perror( "atp_sreq" );
793 if(debug){ printf( "SENDSTATUS >\n" ), fflush( stdout );}
795 atpb.atp_saddr = &nn.nn_sat;
796 rniov[ 0 ].iov_len = PAP_MAXDATA + 4;
797 atpb.atp_rresiov = rniov;
798 atpb.atp_rresiovcnt = 1;
799 if ( atp_rresp( satp, &atpb ) < 0 ) {
800 perror( "atp_rresp" );
804 #ifndef NONZEROSTATUS
806 * The stinking LaserWriter IINTX puts crap in this
809 if ( ((char *)rniov[ 0 ].iov_base)[ 0 ] != 0 ) {
810 fprintf( stderr, "Bad status response!\n" );
813 #endif /* NONZEROSTATUS */
815 if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_STATUS ||
816 atpb.atp_rresiovcnt != 1 ) {
817 fprintf( stderr, "Bad status response!\n" );
821 if(debug){ printf( "< STATUS\n" ), fflush( stdout );}
824 if ( waitforprinter ) {
825 char st_buf[ 1024 ]; /* XXX too big */
827 memcpy( st_buf, (char *) rniov[ 0 ].iov_base + 9,
828 ((char *)rniov[ 0 ].iov_base)[ 8 ] );
829 st_buf[ (int) ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
830 if ( strstr( st_buf, "waiting" ) != NULL ) {
836 updatestatus( (char *) rniov[ 0 ].iov_base + 9,
837 ((char *)rniov[ 0 ].iov_base)[ 8 ] );
842 void updatestatus(char *s, int len)
845 struct iovec iov[ 3 ];
848 if (( fd = open( status, O_WRONLY|O_TRUNC )) < 0 ) {
858 iov[ 0 ].iov_base = "%%[ ";
859 iov[ 0 ].iov_len = 4;
860 iov[ 1 ].iov_base = s;
861 iov[ 1 ].iov_len = len;
862 iov[ 2 ].iov_base = " ]%%\n";
863 iov[ 2 ].iov_len = 5;
865 writev( fd, iov, 3 );