2 * $Id: pap.c,v 1.7 2001-07-31 19:49:02 srittau 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>
17 #include <atalk/atp.h>
18 #include <atalk/pap.h>
19 #include <atalk/nbp.h>
20 #include <atalk/util.h>
23 #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 /* if there is a less hacky way to do this, please do it... */
47 #define DEBUG(x,y) (x,y)
57 if (( p = strrchr( path, '/' )) == NULL ) {
63 "Usage:\t%s [ -A address ] [ -p printername ] [ -s statusfile ] [ file ] ...\n", p );
70 static char s[ 32 + 1 + 32 + 1 + 32 ];
74 if (( f = fopen( _PATH_PAPRC, "r" )) == NULL ) {
75 if ( errno == ENOENT ) {
78 perror( _PATH_PAPRC );
82 while ( fgets( s, sizeof( s ), f ) != NULL ) {
83 s[ strlen( s ) - 1 ] = '\0'; /* remove trailing newline */
97 int waitforprinter = 0;
99 unsigned char connid, quantum, oquantum = PAP_MAXQUANTUM;
100 struct sockaddr_at sat;
106 char fbuf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
107 struct iovec rfiov[ PAP_MAXQUANTUM ] = {
108 { fbuf[ 0 ] + 4, 0 },
109 { fbuf[ 1 ] + 4, 0 },
110 { fbuf[ 2 ] + 4, 0 },
111 { fbuf[ 3 ] + 4, 0 },
112 { fbuf[ 4 ] + 4, 0 },
113 { fbuf[ 5 ] + 4, 0 },
114 { fbuf[ 6 ] + 4, 0 },
115 { fbuf[ 7 ] + 4, 0 },
117 struct iovec sniov[ PAP_MAXQUANTUM ] = {
128 char nbuf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
129 struct iovec rniov[ PAP_MAXQUANTUM ] = {
139 struct iovec sfiov[ PAP_MAXQUANTUM ] = {
140 { nbuf[ 0 ] + 4, 0 },
141 { nbuf[ 1 ] + 4, 0 },
142 { nbuf[ 2 ] + 4, 0 },
143 { nbuf[ 3 ] + 4, 0 },
144 { nbuf[ 4 ] + 4, 0 },
145 { nbuf[ 5 ] + 4, 0 },
146 { nbuf[ 6 ] + 4, 0 },
147 { nbuf[ 7 ] + 4, 0 },
155 struct atp_block atpb;
156 int c, err = 0, fd, cuts = 0;
157 char *obj = NULL, *type = "LaserWriter", *zone = "*";
158 struct timeval stv, tv;
159 char rbuf[ ATP_MAXDATA ];
161 unsigned short waiting, result;
162 int connattempts = 10;
169 memset(&addr, 0, sizeof(addr));
170 while (( c = getopt( ac, av, "Wwcep:s:EA:" )) != EOF ) {
186 case 'e' : /* send stdout to stderr */
203 if (!atalk_aton(optarg, &addr)) {
204 fprintf(stderr, "Bad address.\n");
216 if ( printer == NULL && (( printer = paprc()) == NULL )) {
217 fprintf( stderr, "No printer specified and ./.paprc not found.\n" );
224 if ( nbp_name( printer, &obj, &type, &zone ) < 0 ) {
225 fprintf( stderr, "%s: Bad name\n", printer );
229 fprintf( stderr, "%s: Bad name\n", printer );
233 if ( nbp_lookup( obj, type, zone, &nn, 1, &addr ) <= 0 ) {
235 perror( "nbp_lookup" );
238 fprintf( stderr, "%s:%s@%s: NBP Lookup failed\n", obj, type, zone );
243 printf( "Trying %u.%d:%d ...\n", ntohs( nn.nn_sat.sat_addr.s_net ),
244 nn.nn_sat.sat_addr.s_node, nn.nn_sat.sat_port );
247 if (( atp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
248 perror( "atp_open" );
251 if (( satp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
252 perror( "atp_open" );
256 while ( waitforidle ) {
257 char st_buf[ 1024 ]; /* XXX too big */
260 cbuf[ 1 ] = PAP_SENDSTATUS;
261 cbuf[ 2 ] = cbuf[ 3 ] = 0;
262 atpb.atp_saddr = &nn.nn_sat;
263 atpb.atp_sreqdata = cbuf;
264 atpb.atp_sreqdlen = 4; /* bytes in SendStatus request */
265 atpb.atp_sreqto = 2; /* retry timer */
266 atpb.atp_sreqtries = 5; /* retry count */
267 if ( atp_sreq( satp, &atpb, 1, 0 ) < 0 ) {
268 perror( "atp_sreq" );
272 DEBUG( printf( "SENDSTATUS >\n" ), fflush( stdout ));
274 atpb.atp_saddr = &nn.nn_sat;
275 rniov[ 0 ].iov_len = PAP_MAXDATA + 4;
276 atpb.atp_rresiov = rniov;
277 atpb.atp_rresiovcnt = 1;
278 if ( atp_rresp( satp, &atpb ) < 0 ) {
279 perror( "atp_rresp" );
283 #ifndef NONZEROSTATUS
285 * The stinking LaserWriter IINTX puts crap in this
288 if ( ((char *)rniov[ 0 ].iov_base)[ 0 ] != 0 ) {
289 fprintf( stderr, "Bad status response!\n" );
292 #endif /* NONZEROSTATUS */
294 if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_STATUS ||
295 atpb.atp_rresiovcnt != 1 ) {
296 fprintf( stderr, "Bad status response!\n" );
300 DEBUG( printf( "< STATUS\n" ), fflush( stdout ));
302 memcpy( st_buf, (char *) rniov[ 0 ].iov_base + 9,
303 ((char *)rniov[ 0 ].iov_base)[ 8 ] );
304 st_buf[ (int) ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
305 if ( strstr( st_buf, "idle" ) != NULL ) {
308 updatestatus( (char *) rniov[ 0 ].iov_base + 9,
309 ((char *)rniov[ 0 ].iov_base)[ 8 ] );
314 cbuf[ 0 ] = connid = getpid() & 0xff;
315 cbuf[ 1 ] = PAP_OPEN;
316 cbuf[ 2 ] = cbuf[ 3 ] = 0;
317 cbuf[ 4 ] = atp_sockaddr( atp )->sat_port;
318 cbuf[ 5 ] = oquantum; /* flow quantum */
319 if ( gettimeofday( &stv, 0 ) < 0 ) {
320 perror( "gettimeofday" );
327 if ( gettimeofday( &tv, 0 ) < 0 ) {
328 perror( "gettimeofday" );
331 waiting = htons( tv.tv_sec - stv.tv_sec );
333 memcpy(cbuf + 6, &waiting, sizeof( waiting ));
335 atpb.atp_saddr = &nn.nn_sat;
336 atpb.atp_sreqdata = cbuf;
337 atpb.atp_sreqdlen = 8; /* bytes in OpenConn request */
338 atpb.atp_sreqto = 2; /* retry timer */
339 atpb.atp_sreqtries = 5; /* retry count */
340 if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
341 perror( "atp_sreq" );
345 DEBUG( printf( "OPEN >\n" ), fflush( stdout ));
348 iov.iov_len = sizeof( rbuf );
349 atpb.atp_rresiov = &iov;
350 atpb.atp_rresiovcnt = 1;
351 if ( atp_rresp( atp, &atpb ) < 0 ) {
352 perror( "atp_rresp" );
353 if ( connattempts-- <= 0 ) {
354 fprintf( stderr, "Can't connect!\n" );
361 if ( iov.iov_len < 8 || (unsigned char)rbuf[ 0 ] != connid ||
362 rbuf[ 1 ] != PAP_OPENREPLY ) {
363 fprintf( stderr, "Bad response!\n" );
364 continue; /* This is weird, since TIDs must match... */
367 DEBUG( printf( "< OPENREPLY\n" ), fflush( stdout ));
370 printf( "%.*s\n", (int)iov.iov_len - 9, (char *) iov.iov_base + 9 );
372 updatestatus( (char *) iov.iov_base + 9, iov.iov_len - 9 );
374 memcpy( &result, rbuf + 6, sizeof( result ));
378 memcpy( &sat, &nn.nn_sat, sizeof( struct sockaddr_at ));
379 sat.sat_port = rbuf[ 4 ];
386 printf( "Connected to %.*s:%.*s@%.*s.\n",
387 nn.nn_objlen, nn.nn_obj,
388 nn.nn_typelen, nn.nn_type,
389 nn.nn_zonelen, nn.nn_zone );
392 if ( optind == ac ) {
393 send_file( 0, atp, 1 );
395 for (; optind < ac; optind++ ) {
396 if ( strcmp( av[ optind ], "-" ) == 0 ) {
398 } else if (( fd = open( av[ optind ], O_RDONLY )) < 0 ) {
399 perror( av[ optind ] );
402 send_file( fd, atp, ( optind == ac - 1 ) ? 1 : 0 );
413 cbuf[ 1 ] = PAP_CLOSE;
414 cbuf[ 2 ] = cbuf[ 3 ] = 0;
416 atpb.atp_saddr = &sat;
417 atpb.atp_sreqdata = cbuf;
418 atpb.atp_sreqdlen = 4; /* bytes in CloseConn request */
419 atpb.atp_sreqto = 2; /* retry timer */
420 atpb.atp_sreqtries = 5; /* retry count */
421 if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
422 perror( "atp_sreq" );
426 DEBUG( printf( "CLOSE >\n" ), fflush( stdout ));
429 iov.iov_len = sizeof( rbuf );
430 atpb.atp_rresiov = &iov;
431 atpb.atp_rresiovcnt = 1;
432 if ( atp_rresp( atp, &atpb ) < 0 ) {
433 perror( "atp_rresp" );
438 if ( iov.iov_len != 4 || rbuf[ 1 ] != PAP_CLOSEREPLY ) {
439 fprintf( stderr, "Bad response!\n" );
445 * The AGFA Viper Rip doesn't have the connection id in the close request.
447 if ((unsigned char)rbuf[ 0 ] != connid ) {
448 fprintf( stderr, "Bad connid in close!\n" );
451 #endif /* ZEROCONNID */
453 DEBUG( printf( "< CLOSEREPLY\n" ), fflush( stdout ));
456 printf( "Connection closed.\n" );
463 u_int16_t seq = 0, rseq = 1;
465 int send_file( fd, atp, 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 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 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 DEBUG( printf( "< READ %d\n", ntohs( netseq )), fflush( stdout ));
603 if ( rseq != ntohs( netseq )) {
604 DEBUG( printf( "| DUP %d\n", rseq ), fflush( stdout ));
607 if ( rseq++ == 0xffff ) rseq = 1;
612 port = ssat.sat_port;
617 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 DEBUG( printf( "CLOSEREPLY >\n" ), fflush( stdout ));
636 fprintf( stderr, "Connection closed by foreign host.\n" );
641 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 DEBUG( printf( "< DATA (eof)\n" ), fflush( stdout ));
697 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 DEBUG( printf( "READ %d >\n", seq ), fflush( stdout ));
724 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 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 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 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( s, len )
847 struct iovec iov[ 2 ];
853 if (( fd = open( status, O_WRONLY|O_TRUNC )) < 0 ) {
858 iov[ 0 ].iov_base = s;
859 iov[ 0 ].iov_len = len;
860 iov[ 1 ].iov_base = "\n";
861 iov[ 1 ].iov_len = 1;
862 writev( fd, iov, 2 );