2 * Copyright (c) 1990,1994 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
11 #include <sys/types.h>
12 #include <sys/syslog.h>
15 #include <netatalk/endian.h>
16 #include <netatalk/at.h>
17 #include <atalk/atp.h>
18 #include <atalk/pap.h>
22 extern unsigned char connid, quantum, oquantum;
24 char buf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
25 struct iovec niov[ PAP_MAXQUANTUM ] = {
35 struct iovec iov[ PAP_MAXQUANTUM ] = {
47 * Accept files until the client closes the connection.
48 * Read lines of a file, until the client sends eof, after
49 * which we'll send eof also.
53 struct sockaddr_at *sat;
56 struct atp_block atpb;
57 struct sockaddr_at ssat;
58 struct papfile infile, outfile;
61 int i, cc, timeout = 0, readpending = 0;
62 u_int16_t seq = 0, rseq = 1, netseq;
65 infile.pf_state = PF_BOT;
66 infile.pf_bufsize = 0;
67 infile.pf_datalen = 0;
71 outfile.pf_state = PF_BOT;
72 outfile.pf_bufsize = 0;
73 outfile.pf_datalen = 0;
82 if (++seq == 0) seq = 1;
83 netseq = htons( seq );
84 bcopy( &netseq, &cbuf[ 2 ], sizeof( netseq ));
86 atpb.atp_sreqdata = cbuf;
87 atpb.atp_sreqdlen = 4; /* bytes in SendData request */
88 atpb.atp_sreqto = 5; /* retry timer */
89 atpb.atp_sreqtries = -1; /* infinite retries */
90 if ( atp_sreq( atp, &atpb, oquantum, ATP_XO )) {
91 syslog( LOG_ERR, "atp_sreq: %m" );
97 * Time between tickles.
103 * If we don't get anything for a while, time out.
106 FD_SET( atp_fileno( atp ), &fds );
108 if (( cc = select( FD_SETSIZE, &fds, 0, 0, &tv )) < 0 ) {
109 syslog( LOG_ERR, "select: %m" );
113 if ( timeout++ > 2 ) {
114 syslog( LOG_ERR, "connection timed out" );
123 cbuf[ 1 ] = PAP_TICKLE;
124 cbuf[ 2 ] = cbuf[ 3 ] = 0;
125 atpb.atp_saddr = sat;
126 atpb.atp_sreqdata = cbuf;
127 atpb.atp_sreqdlen = 4; /* bytes in Tickle request */
128 atpb.atp_sreqto = 0; /* best effort */
129 atpb.atp_sreqtries = 1; /* try once */
130 if ( atp_sreq( atp, &atpb, 0, 0 )) {
131 syslog( LOG_ERR, "atp_sreq: %m" );
139 bzero( &ssat, sizeof( struct sockaddr_at ));
140 switch( atp_rsel( atp, &ssat, ATP_TRESP | ATP_TREQ )) {
142 atpb.atp_saddr = &ssat;
143 atpb.atp_rreqdata = cbuf;
144 atpb.atp_rreqdlen = sizeof( cbuf );
145 if ( atp_rreq( atp, &atpb ) < 0 ) {
146 syslog( LOG_ERR, "atp_rreq: %m" );
150 if ( (unsigned char)cbuf[ 0 ] != connid ) {
151 syslog( LOG_ERR, "Bad ATP request!" );
155 switch( cbuf[ 1 ] ) {
158 * Other side is ready for some data.
160 bcopy( &cbuf[ 2 ], &netseq, sizeof( netseq ));
162 if ( rseq != ntohs( netseq )) {
165 if ( rseq++ == 0xffff ) rseq = 1;
168 readport = ssat.sat_port;
173 * Respond to the close request.
174 * If we're in the middle of a file, clean up.
176 if (( infile.pf_state & PF_BOT ) ||
177 ( infile.pf_datalen == 0 &&
178 ( infile.pf_state & PF_EOF ))) {
184 niov[ 0 ].iov_len = 4;
185 ((char *)niov[ 0 ].iov_base)[ 0 ] = connid;
186 ((char *)niov[ 0 ].iov_base)[ 1 ] = PAP_CLOSEREPLY;
187 ((char *)niov[ 0 ].iov_base)[ 2 ] =
188 ((char *)niov[ 0 ].iov_base)[ 3 ] = 0;
189 atpb.atp_sresiov = niov;
190 atpb.atp_sresiovcnt = 1;
191 if ( atp_sresp( atp, &atpb ) < 0 ) {
192 syslog( LOG_ERR, "atp_sresp: %m" );
201 syslog( LOG_ERR, "Bad PAP request!" );
207 atpb.atp_saddr = &ssat;
208 for ( i = 0; i < oquantum; i++ ) {
209 niov[ i ].iov_len = PAP_MAXDATA + 4;
211 atpb.atp_rresiov = niov;
212 atpb.atp_rresiovcnt = oquantum;
213 if ( atp_rresp( atp, &atpb ) < 0 ) {
214 syslog( LOG_ERR, "atp_rresp: %m" );
219 if ( ((unsigned char *)niov[ 0 ].iov_base)[ 0 ] != connid ||
220 ((char *)niov[ 0 ].iov_base)[ 1 ] != PAP_DATA ) {
221 syslog( LOG_ERR, "Bad data response!" );
225 for ( i = 0; i < atpb.atp_rresiovcnt; i++ ) {
227 niov[ i ].iov_base + 4, niov[ i ].iov_len - 4 );
228 if (( infile.pf_state & PF_EOF ) == 0 &&
229 ((char *)niov[ 0 ].iov_base)[ 2 ] ) {
230 infile.pf_state |= PF_EOF;
235 if ( ps( &infile, &outfile, sat ) < 0 ) {
236 syslog( LOG_ERR, "parse: bad return" );
237 exit( 1 ); /* really? close? */
244 cbuf[ 1 ] = PAP_READ;
245 if ( ++seq == 0 ) seq = 1;
246 netseq = htons( seq );
247 bcopy( &netseq, &cbuf[ 2 ], sizeof( netseq ));
248 atpb.atp_saddr = sat;
249 atpb.atp_sreqdata = cbuf;
250 atpb.atp_sreqdlen = 4; /* bytes in SendData request */
251 atpb.atp_sreqto = 5; /* retry timer */
252 atpb.atp_sreqtries = -1; /* infinite retries */
253 if ( atp_sreq( atp, &atpb, oquantum, ATP_XO )) {
254 syslog( LOG_ERR, "atp_sreq: %m" );
263 syslog( LOG_ERR, "atp_rsel: %m" );
267 /* send any data that we have */
269 ( outfile.pf_datalen || ( outfile.pf_state & PF_EOF ))) {
270 for ( i = 0; i < quantum; i++ ) {
271 ((char *)niov[ i ].iov_base)[ 0 ] = connid;
272 ((char *)niov[ i ].iov_base)[ 1 ] = PAP_DATA;
273 ((char *)niov[ i ].iov_base)[ 2 ] =
274 ((char *)niov[ i ].iov_base)[ 3 ] = 0;
276 if ( outfile.pf_datalen > PAP_MAXDATA ) {
279 cc = outfile.pf_datalen;
280 if ( outfile.pf_state & PF_EOF ) {
281 ((char *)niov[ 0 ].iov_base)[ 2 ] = 1; /* eof */
282 outfile.pf_state = PF_BOT;
283 infile.pf_state = PF_BOT;
287 niov[ i ].iov_len = 4 + cc;
288 bcopy( outfile.pf_data, niov[ i ].iov_base + 4, cc );
289 CONSUME( &outfile, cc );
290 if ( outfile.pf_datalen == 0 ) {
295 ssat.sat_port = readport;
296 atpb.atp_saddr = &ssat;
297 atpb.atp_sresiov = niov;
298 atpb.atp_sresiovcnt = i; /* reported by stevebn@pc1.eos.co.uk */
299 if ( atp_sresp( atp, &atpb ) < 0 ) {
300 syslog( LOG_ERR, "atp_sresp: %m" );