2 * $Id: pap.c,v 1.14 2009-10-16 01:10:59 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"
34 /* Forward Declarations */
35 static void updatestatus(char *s, int len);
36 static int send_file(int fd, ATP atp, int lastfile);
38 static void usage(char *path)
42 if (( p = strrchr( path, '/' )) == NULL ) {
48 "Usage:\t%s [ -A address ] [ -c ] [ -d ] [ -e ] [ -E ] [ -p printer ]\n"
49 " [ -s statusfile ] [ -w ] [ -W ] [ FILES ]\n"
50 " -A address - printer Appletalk address\n"
51 " -c - take cuts (lie about wait time)\n"
52 " -d - enable debug\n"
53 " -e - send stdout to stderr\n"
54 " -E - don't wait for EOF from printer\n"
55 " -p printer - printer name\n"
56 " -s statusfile - put current printer status in statusfile\n"
57 " -w - wait for printer status = 'waiting'\n"
58 " -W - wait for printer status = 'idle'\n"
59 " FILES - send FILES to printer\n"
67 static char s[ 32 + 1 + 32 + 1 + 32 ];
71 if (( f = fopen( _PATH_PAPRC, "r" )) == NULL ) {
72 if ( errno == ENOENT ) {
75 perror( _PATH_PAPRC );
79 while ( fgets( s, sizeof( s ), f ) != NULL ) {
80 s[ strlen( s ) - 1 ] = '\0'; /* remove trailing newline */
91 static char *printer = NULL;
92 static char *status = NULL;
94 static int waitforprinter = 0;
96 static unsigned char connid, quantum, oquantum = PAP_MAXQUANTUM;
97 static struct sockaddr_at sat;
99 static char cbuf[ 8 ];
100 static struct nbpnve nn;
103 static char fbuf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
104 static struct iovec rfiov[ PAP_MAXQUANTUM ] = {
105 { fbuf[ 0 ] + 4, 0 },
106 { fbuf[ 1 ] + 4, 0 },
107 { fbuf[ 2 ] + 4, 0 },
108 { fbuf[ 3 ] + 4, 0 },
109 { fbuf[ 4 ] + 4, 0 },
110 { fbuf[ 5 ] + 4, 0 },
111 { fbuf[ 6 ] + 4, 0 },
112 { fbuf[ 7 ] + 4, 0 },
115 static struct iovec sniov[ PAP_MAXQUANTUM ] = {
126 static char nbuf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
127 static struct iovec rniov[ PAP_MAXQUANTUM ] = {
138 static struct iovec sfiov[ PAP_MAXQUANTUM ] = {
139 { nbuf[ 0 ] + 4, 0 },
140 { nbuf[ 1 ] + 4, 0 },
141 { nbuf[ 2 ] + 4, 0 },
142 { nbuf[ 3 ] + 4, 0 },
143 { nbuf[ 4 ] + 4, 0 },
144 { nbuf[ 5 ] + 4, 0 },
145 { nbuf[ 6 ] + 4, 0 },
146 { nbuf[ 7 ] + 4, 0 },
151 int main( int ac, char **av)
154 struct atp_block atpb;
155 int c, err = 0, fd, cuts = 0;
156 char *obj = NULL, *type = "LaserWriter", *zone = "*";
157 struct timeval stv, tv;
158 char rbuf[ ATP_MAXDATA ];
160 unsigned short waiting, result;
161 int connattempts = 10;
168 memset(&addr, 0, sizeof(addr));
169 while (( c = getopt( ac, av, "dWwcep:s:EA:" )) != EOF ) {
181 /* enable debugging */
190 case 'e' : /* send stdout to stderr */
207 if (!atalk_aton(optarg, &addr)) {
208 fprintf(stderr, "Bad address.\n");
220 if ( printer == NULL && (( printer = paprc()) == NULL )) {
221 fprintf( stderr, "No printer specified and ./.paprc not found.\n" );
228 if ( nbp_name( printer, &obj, &type, &zone ) < 0 ) {
229 fprintf( stderr, "%s: Bad name\n", printer );
233 fprintf( stderr, "%s: Bad name\n", printer );
237 if ( nbp_lookup( obj, type, zone, &nn, 1, &addr ) <= 0 ) {
239 perror( "nbp_lookup" );
242 fprintf( stderr, "%s:%s@%s: NBP Lookup failed\n", obj, type, zone );
247 printf( "Trying %u.%d:%d ...\n", ntohs( nn.nn_sat.sat_addr.s_net ),
248 nn.nn_sat.sat_addr.s_node, nn.nn_sat.sat_port );
251 if (( atp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
252 perror( "atp_open" );
255 if (( satp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
256 perror( "atp_open" );
260 while ( waitforidle ) {
261 char st_buf[ 1024 ]; /* XXX too big */
264 cbuf[ 1 ] = PAP_SENDSTATUS;
265 cbuf[ 2 ] = cbuf[ 3 ] = 0;
266 atpb.atp_saddr = &nn.nn_sat;
267 atpb.atp_sreqdata = cbuf;
268 atpb.atp_sreqdlen = 4; /* bytes in SendStatus request */
269 atpb.atp_sreqto = 2; /* retry timer */
270 atpb.atp_sreqtries = 5; /* retry count */
271 if ( atp_sreq( satp, &atpb, 1, 0 ) < 0 ) {
272 perror( "atp_sreq" );
276 if(debug){ printf( "SENDSTATUS >\n" ), fflush( stdout );}
278 atpb.atp_saddr = &nn.nn_sat;
279 rniov[ 0 ].iov_len = PAP_MAXDATA + 4;
280 atpb.atp_rresiov = rniov;
281 atpb.atp_rresiovcnt = 1;
282 if ( atp_rresp( satp, &atpb ) < 0 ) {
283 perror( "atp_rresp" );
287 #ifndef NONZEROSTATUS
289 * The stinking LaserWriter IINTX puts crap in this
292 if ( ((char *)rniov[ 0 ].iov_base)[ 0 ] != 0 ) {
293 fprintf( stderr, "Bad status response!\n" );
296 #endif /* NONZEROSTATUS */
298 if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_STATUS ||
299 atpb.atp_rresiovcnt != 1 ) {
300 fprintf( stderr, "Bad status response!\n" );
304 if(debug){ printf( "< STATUS\n" ), fflush( stdout );}
306 memcpy( st_buf, (char *) rniov[ 0 ].iov_base + 9,
307 ((char *)rniov[ 0 ].iov_base)[ 8 ] );
308 st_buf[ (int) ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
309 if ( strstr( st_buf, "idle" ) != NULL ) {
312 updatestatus( (char *) rniov[ 0 ].iov_base + 9,
313 ((char *)rniov[ 0 ].iov_base)[ 8 ] );
318 cbuf[ 0 ] = connid = getpid() & 0xff;
319 cbuf[ 1 ] = PAP_OPEN;
320 cbuf[ 2 ] = cbuf[ 3 ] = 0;
321 cbuf[ 4 ] = atp_sockaddr( atp )->sat_port;
322 cbuf[ 5 ] = oquantum; /* flow quantum */
323 if ( gettimeofday( &stv, NULL ) < 0 ) {
324 perror( "gettimeofday" );
331 if ( gettimeofday( &tv, NULL ) < 0 ) {
332 perror( "gettimeofday" );
335 waiting = htons( tv.tv_sec - stv.tv_sec );
337 memcpy(cbuf + 6, &waiting, sizeof( waiting ));
339 atpb.atp_saddr = &nn.nn_sat;
340 atpb.atp_sreqdata = cbuf;
341 atpb.atp_sreqdlen = 8; /* bytes in OpenConn request */
342 atpb.atp_sreqto = 2; /* retry timer */
343 atpb.atp_sreqtries = 5; /* retry count */
344 if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
345 perror( "atp_sreq" );
349 if(debug){ printf( "OPEN >\n" ), fflush( stdout );}
352 iov.iov_len = sizeof( rbuf );
353 atpb.atp_rresiov = &iov;
354 atpb.atp_rresiovcnt = 1;
355 if ( atp_rresp( atp, &atpb ) < 0 ) {
356 perror( "atp_rresp" );
357 if ( connattempts-- <= 0 ) {
358 fprintf( stderr, "Can't connect!\n" );
365 if ( iov.iov_len < 8 || (unsigned char)rbuf[ 0 ] != connid ||
366 rbuf[ 1 ] != PAP_OPENREPLY ) {
367 fprintf( stderr, "Bad response!\n" );
368 continue; /* This is weird, since TIDs must match... */
371 if(debug){ printf( "< OPENREPLY\n" ), fflush( stdout );}
374 printf( "%.*s\n", (int)iov.iov_len - 9, (char *) iov.iov_base + 9 );
376 updatestatus( (char *) iov.iov_base + 9, iov.iov_len - 9 );
378 memcpy( &result, rbuf + 6, sizeof( result ));
382 memcpy( &sat, &nn.nn_sat, sizeof( struct sockaddr_at ));
383 sat.sat_port = rbuf[ 4 ];
390 printf( "Connected to %.*s:%.*s@%.*s.\n",
391 nn.nn_objlen, nn.nn_obj,
392 nn.nn_typelen, nn.nn_type,
393 nn.nn_zonelen, nn.nn_zone );
396 if ( optind == ac ) {
397 send_file( 0, atp, 1 );
399 for (; optind < ac; optind++ ) {
400 if ( strcmp( av[ optind ], "-" ) == 0 ) {
402 } else if (( fd = open( av[ optind ], O_RDONLY )) < 0 ) {
403 perror( av[ optind ] );
406 send_file( fd, atp, ( optind == ac - 1 ) ? 1 : 0 );
417 cbuf[ 1 ] = PAP_CLOSE;
418 cbuf[ 2 ] = cbuf[ 3 ] = 0;
420 atpb.atp_saddr = &sat;
421 atpb.atp_sreqdata = cbuf;
422 atpb.atp_sreqdlen = 4; /* bytes in CloseConn request */
423 atpb.atp_sreqto = 2; /* retry timer */
424 atpb.atp_sreqtries = 5; /* retry count */
425 if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
426 perror( "atp_sreq" );
430 if(debug){ printf( "CLOSE >\n" ), fflush( stdout );}
433 iov.iov_len = sizeof( rbuf );
434 atpb.atp_rresiov = &iov;
435 atpb.atp_rresiovcnt = 1;
436 if ( atp_rresp( atp, &atpb ) < 0 ) {
437 perror( "atp_rresp" );
442 if ( iov.iov_len != 4 || rbuf[ 1 ] != PAP_CLOSEREPLY ) {
443 fprintf( stderr, "Bad response!\n" );
449 * The AGFA Viper Rip doesn't have the connection id in the close request.
451 if ((unsigned char)rbuf[ 0 ] != connid ) {
452 fprintf( stderr, "Bad connid in close!\n" );
455 #endif /* ZEROCONNID */
457 if(debug){ printf( "< CLOSEREPLY\n" ), fflush( stdout );}
460 printf( "Connection closed.\n" );
466 static unsigned char port;
467 static u_int16_t seq = 0;
469 static int send_file( int fd, ATP atp, int lastfile)
471 struct timeval stv, tv;
472 struct sockaddr_at ssat;
473 struct atp_block atpb;
475 int fiovcnt = 0, eof = 0, senteof = 0, to = 0;
477 unsigned short netseq;
479 if ( gettimeofday( &stv, NULL ) < 0 ) {
480 perror( "gettimeofday" );
488 cbuf[ 1 ] = PAP_READ;
489 if ( ++seq == 0 ) seq = 1;
490 netseq = htons( seq );
491 memcpy( cbuf + 2, &netseq, sizeof( netseq ));
492 atpb.atp_saddr = &sat;
493 atpb.atp_sreqdata = cbuf;
494 atpb.atp_sreqdlen = 4; /* bytes in SendData request */
495 atpb.atp_sreqto = 15; /* retry timer */
496 atpb.atp_sreqtries = -1; /* retry count */
497 if ( atp_sreq( atp, &atpb, oquantum, ATP_XO ) < 0 ) {
498 perror( "atp_sreq" );
502 if(debug){ printf( "READ %d >\n", seq ), fflush( stdout );}
505 if ( gettimeofday( &tv, NULL ) < 0 ) {
506 perror( "gettimeofday" );
510 if (( tv.tv_sec - stv.tv_sec ) >= 60 ) {
517 cbuf[ 1 ] = PAP_TICKLE;
518 cbuf[ 2 ] = cbuf[ 3 ] = 0;
519 atpb.atp_saddr = &sat;
520 atpb.atp_sreqdata = cbuf;
521 atpb.atp_sreqdlen = 4; /* bytes in Tickle request */
522 atpb.atp_sreqto = 0; /* retry timer */
523 atpb.atp_sreqtries = 1; /* retry count */
524 if ( atp_sreq( satp, &atpb, 0, 0 ) < 0 ) {
525 perror( "atp_sreq" );
529 if(debug){ printf( "TICKLE >\n" ), fflush( stdout );}
532 tv.tv_sec = stv.tv_sec + 60 - tv.tv_sec;
536 if ( !waitforprinter && !eof && fiovcnt == 0 ) {
539 FD_SET( atp_fileno( atp ), &fds );
541 if (( cc = select( FD_SETSIZE, &fds, NULL, NULL, &tv )) < 0 ) {
547 * A timeout has occured. Keep track of it.
551 fprintf( stderr, "Connection timed out.\n" );
560 if ( !fiovcnt && FD_ISSET( fd, &fds )) {
561 for ( i = 0; i < quantum; i++ ) {
562 rfiov[ i ].iov_len = PAP_MAXDATA;
564 if (( cc = readv( fd, rfiov, quantum )) < 0 ) {
571 fiovcnt = cc / PAP_MAXDATA + ( cc % PAP_MAXDATA > 0 );
572 for ( i = 0; cc > 0; i++ ) {
573 rfiov[ i ].iov_len = ( cc > PAP_MAXDATA ) ? PAP_MAXDATA : cc;
574 cc -= ( cc > PAP_MAXDATA ) ? PAP_MAXDATA : cc;
578 if ( FD_ISSET( atp_fileno( atp ), &fds )) {
580 ssat.sat_port = ATADDR_ANYPORT;
581 switch( atp_rsel( atp, &ssat, ATP_TRESP | ATP_TREQ )) {
583 atpb.atp_saddr = &ssat;
584 atpb.atp_rreqdata = cbuf;
585 atpb.atp_rreqdlen = sizeof( cbuf );
586 if ( atp_rreq( atp, &atpb ) < 0 ) {
587 perror( "atp_rreq" );
591 if ( (unsigned char)cbuf[ 0 ] != connid ) {
595 /* reset timeout counter for all valid requests */
598 switch ( cbuf[ 1 ] ) {
600 memcpy( cbuf + 2, &netseq, sizeof( netseq ));
601 if(debug){ printf( "< READ %d\n", ntohs( netseq )), fflush( stdout );}
604 if ( rseq != ntohs( netseq )) {
605 if(debug){ printf( "| DUP %d\n", rseq ), fflush( stdout );}
608 if ( rseq++ == 0xffff ) rseq = 1;
613 port = ssat.sat_port;
618 if(debug){ printf( "< CLOSE\n" ), fflush( stdout );}
621 * Respond to the close request, and fail.
623 sniov[ 0 ].iov_len = 4;
624 ((char *)sniov[ 0 ].iov_base)[ 0 ] = connid;
625 ((char *)sniov[ 0 ].iov_base)[ 1 ] = PAP_CLOSEREPLY;
626 ((char *)sniov[ 0 ].iov_base)[ 2 ] =
627 ((char *)sniov[ 0 ].iov_base)[ 3 ] = 0;
628 atpb.atp_sresiov = sniov;
629 atpb.atp_sresiovcnt = 1;
630 if ( atp_sresp( atp, &atpb ) < 0 ) {
631 perror( "atp_sresp" );
635 if(debug){ printf( "CLOSEREPLY >\n" ), fflush( stdout );}
637 fprintf( stderr, "Connection closed by foreign host.\n" );
642 if(debug){ printf( "< TICKLE\n" ), fflush( stdout );}
646 fprintf( stderr, "Bad PAP request!\n" );
652 /* reset timeout counter for all valid requests */
655 atpb.atp_saddr = &ssat;
656 for ( i = 0; i < oquantum; i++ ) {
657 rniov[ i ].iov_len = PAP_MAXDATA + 4;
659 atpb.atp_rresiov = rniov;
660 atpb.atp_rresiovcnt = oquantum;
661 if ( atp_rresp( atp, &atpb ) < 0 ) {
662 perror( "atp_rresp" );
668 * The HP LJIIISI w/ BridgePort LocalTalk card sends
669 * zero instead of the connid.
671 if ( ((unsigned char *)rniov[ 0 ].iov_base)[ 0 ] != connid ) {
672 fprintf( stderr, "Bad data response!\n" );
675 #endif /* ZEROCONNID */
676 if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_DATA ) {
677 fprintf( stderr, "Bad data response!\n" );
681 for ( cc = 0, i = 0; i < atpb.atp_rresiovcnt; i++ ) {
682 sfiov[ i ].iov_len = rniov[ i ].iov_len - 4;
683 cc += sfiov[ i ].iov_len;
685 if ( cc && writev( 1, sfiov, atpb.atp_rresiovcnt ) < cc ) {
691 if ( ((char *)rniov[ 0 ].iov_base)[ 2 ] ) {
693 if(debug){ printf( "< DATA (eof)\n" ), fflush( stdout );}
698 if(debug){ printf( "< DATA\n" ), fflush( stdout );}
705 cbuf[ 1 ] = PAP_READ;
706 if ( ++seq == 0 ) seq = 1;
707 netseq = htons( seq );
708 memcpy( cbuf + 2, &netseq, sizeof( netseq ));
709 atpb.atp_saddr = &sat;
710 atpb.atp_sreqdata = cbuf;
711 atpb.atp_sreqdlen = 4; /* bytes in SendData request */
712 atpb.atp_sreqto = 15; /* retry timer */
713 atpb.atp_sreqtries = -1; /* retry count */
714 if ( atp_sreq( atp, &atpb, oquantum, ATP_XO ) < 0 ) {
715 perror( "atp_sreq" );
719 if(debug){ printf( "READ %d >\n", seq ), fflush( stdout );}
725 if(debug){ printf( "| RETRANS\n" ), fflush( stdout );}
730 perror( "atp_rsel" );
736 * Send whatever is pending.
738 if ( !waitforprinter && !senteof && data && ( fiovcnt || eof )) {
739 ssat.sat_port = port;
740 atpb.atp_saddr = &ssat;
742 for ( i = 0; i < fiovcnt; i++ ) {
743 sniov[ i ].iov_len = rfiov[ i ].iov_len + 4;
744 ((char *)sniov[ i ].iov_base)[ 0 ] = connid;
745 ((char *)sniov[ i ].iov_base)[ 1 ] = PAP_DATA;
746 senteof = ((char *)sniov[ i ].iov_base)[ 2 ] = eof;
747 ((char *)sniov[ i ].iov_base)[ 3 ] = 0;
750 sniov[ 0 ].iov_len = 4;
751 ((char *)sniov[ 0 ].iov_base)[ 0 ] = connid;
752 ((char *)sniov[ 0 ].iov_base)[ 1 ] = PAP_DATA;
753 senteof = ((char *)sniov[ 0 ].iov_base)[ 2 ] = eof;
754 ((char *)sniov[ 0 ].iov_base)[ 3 ] = 0;
756 atpb.atp_sresiov = sniov;
757 atpb.atp_sresiovcnt = fiovcnt ? fiovcnt : 1;
758 if ( atp_sresp( atp, &atpb ) < 0 ) {
759 perror( "atp_sresp" );
764 if(debug){ printf( "DATA %s\n", eof ? "(eof) >" : ">" ), fflush( stdout );}
767 * The Apple LaserWriter IIf, the HP LWIIISi, and IV, don't
768 * seem to send us an EOF on large jobs. To work around
769 * this heinous protocol violation, we won't wait for their
770 * EOF before closing.
772 if ( eof && noeof && lastfile ) {
777 * If we can't send data right now, go ahead and get the
778 * status. This is cool, because we get here reliably
779 * if there is a problem.
782 cbuf[ 1 ] = PAP_SENDSTATUS;
783 cbuf[ 2 ] = cbuf[ 3 ] = 0;
784 atpb.atp_saddr = &nn.nn_sat;
785 atpb.atp_sreqdata = cbuf;
786 atpb.atp_sreqdlen = 4; /* bytes in SendStatus request */
787 atpb.atp_sreqto = 2; /* retry timer */
788 atpb.atp_sreqtries = 5; /* retry count */
789 if ( atp_sreq( satp, &atpb, 1, 0 ) < 0 ) {
790 perror( "atp_sreq" );
794 if(debug){ printf( "SENDSTATUS >\n" ), fflush( stdout );}
796 atpb.atp_saddr = &nn.nn_sat;
797 rniov[ 0 ].iov_len = PAP_MAXDATA + 4;
798 atpb.atp_rresiov = rniov;
799 atpb.atp_rresiovcnt = 1;
800 if ( atp_rresp( satp, &atpb ) < 0 ) {
801 perror( "atp_rresp" );
805 #ifndef NONZEROSTATUS
807 * The stinking LaserWriter IINTX puts crap in this
810 if ( ((char *)rniov[ 0 ].iov_base)[ 0 ] != 0 ) {
811 fprintf( stderr, "Bad status response!\n" );
814 #endif /* NONZEROSTATUS */
816 if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_STATUS ||
817 atpb.atp_rresiovcnt != 1 ) {
818 fprintf( stderr, "Bad status response!\n" );
822 if(debug){ printf( "< STATUS\n" ), fflush( stdout );}
825 if ( waitforprinter ) {
826 char st_buf[ 1024 ]; /* XXX too big */
828 memcpy( st_buf, (char *) rniov[ 0 ].iov_base + 9,
829 ((char *)rniov[ 0 ].iov_base)[ 8 ] );
830 st_buf[ (int) ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
831 if ( strstr( st_buf, "waiting" ) != NULL ) {
837 updatestatus( (char *) rniov[ 0 ].iov_base + 9,
838 ((char *)rniov[ 0 ].iov_base)[ 8 ] );
843 static void updatestatus(char *s, int len)
846 struct iovec iov[ 3 ];
849 if (( fd = open( status, O_WRONLY|O_TRUNC )) < 0 ) {
859 iov[ 0 ].iov_base = "%%[ ";
860 iov[ 0 ].iov_len = 4;
861 iov[ 1 ].iov_base = s;
862 iov[ 1 ].iov_len = len;
863 iov[ 2 ].iov_base = " ]%%\n";
864 iov[ 2 ].iov_len = 5;
866 writev( fd, iov, 3 );