2 * Copyright (c) 1990,1994 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
12 #include <sys/types.h>
13 #include <sys/syslog.h>
16 #include <netatalk/endian.h>
17 #include <netatalk/at.h>
18 #include <atalk/atp.h>
19 #include <atalk/pap.h>
25 int ps(struct papfile *infile, struct papfile *outfile, struct sockaddr_at *sat);
27 extern unsigned char connid, quantum, oquantum;
29 char buf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
30 struct iovec niov[ PAP_MAXQUANTUM ] = {
40 struct iovec iov[ PAP_MAXQUANTUM ] = {
52 * Accept files until the client closes the connection.
53 * Read lines of a file, until the client sends eof, after
54 * which we'll send eof also.
56 int session( atp, sat )
58 struct sockaddr_at *sat;
61 struct atp_block atpb;
62 struct sockaddr_at ssat;
63 struct papfile infile, outfile;
66 int i, cc, timeout = 0, readpending = 0;
67 u_int16_t seq = 0, rseq = 1, netseq;
70 infile.pf_state = PF_BOT;
71 infile.pf_bufsize = 0;
72 infile.pf_datalen = 0;
76 outfile.pf_state = PF_BOT;
77 outfile.pf_bufsize = 0;
78 outfile.pf_datalen = 0;
87 if (++seq == 0) seq = 1;
88 netseq = htons( seq );
89 bcopy( &netseq, &cbuf[ 2 ], sizeof( netseq ));
91 atpb.atp_sreqdata = cbuf;
92 atpb.atp_sreqdlen = 4; /* bytes in SendData request */
93 atpb.atp_sreqto = 5; /* retry timer */
94 atpb.atp_sreqtries = -1; /* infinite retries */
95 if ( atp_sreq( atp, &atpb, oquantum, ATP_XO )) {
96 syslog( LOG_ERR, "atp_sreq: %m" );
102 * Time between tickles.
108 * If we don't get anything for a while, time out.
111 FD_SET( atp_fileno( atp ), &fds );
113 if (( cc = select( FD_SETSIZE, &fds, 0, 0, &tv )) < 0 ) {
114 syslog( LOG_ERR, "select: %m" );
118 if ( timeout++ > 2 ) {
119 syslog( LOG_ERR, "connection timed out" );
128 cbuf[ 1 ] = PAP_TICKLE;
129 cbuf[ 2 ] = cbuf[ 3 ] = 0;
130 atpb.atp_saddr = sat;
131 atpb.atp_sreqdata = cbuf;
132 atpb.atp_sreqdlen = 4; /* bytes in Tickle request */
133 atpb.atp_sreqto = 0; /* best effort */
134 atpb.atp_sreqtries = 1; /* try once */
135 if ( atp_sreq( atp, &atpb, 0, 0 )) {
136 syslog( LOG_ERR, "atp_sreq: %m" );
144 memset( &ssat, 0, sizeof( struct sockaddr_at ));
145 switch( atp_rsel( atp, &ssat, ATP_TRESP | ATP_TREQ )) {
147 atpb.atp_saddr = &ssat;
148 atpb.atp_rreqdata = cbuf;
149 atpb.atp_rreqdlen = sizeof( cbuf );
150 if ( atp_rreq( atp, &atpb ) < 0 ) {
151 syslog( LOG_ERR, "atp_rreq: %m" );
155 if ( (unsigned char)cbuf[ 0 ] != connid ) {
156 syslog( LOG_ERR, "Bad ATP request!" );
160 switch( cbuf[ 1 ] ) {
163 * Other side is ready for some data.
165 bcopy( &cbuf[ 2 ], &netseq, sizeof( netseq ));
167 if ( rseq != ntohs( netseq )) {
170 if ( rseq++ == 0xffff ) rseq = 1;
173 readport = ssat.sat_port;
178 * Respond to the close request.
179 * If we're in the middle of a file, clean up.
181 if (( infile.pf_state & PF_BOT ) ||
182 ( infile.pf_datalen == 0 &&
183 ( infile.pf_state & PF_EOF ))) {
189 niov[ 0 ].iov_len = 4;
190 ((char *)niov[ 0 ].iov_base)[ 0 ] = connid;
191 ((char *)niov[ 0 ].iov_base)[ 1 ] = PAP_CLOSEREPLY;
192 ((char *)niov[ 0 ].iov_base)[ 2 ] =
193 ((char *)niov[ 0 ].iov_base)[ 3 ] = 0;
194 atpb.atp_sresiov = niov;
195 atpb.atp_sresiovcnt = 1;
196 if ( atp_sresp( atp, &atpb ) < 0 ) {
197 syslog( LOG_ERR, "atp_sresp: %m" );
206 syslog( LOG_ERR, "Bad PAP request!" );
212 atpb.atp_saddr = &ssat;
213 for ( i = 0; i < oquantum; i++ ) {
214 niov[ i ].iov_len = PAP_MAXDATA + 4;
216 atpb.atp_rresiov = niov;
217 atpb.atp_rresiovcnt = oquantum;
218 if ( atp_rresp( atp, &atpb ) < 0 ) {
219 syslog( LOG_ERR, "atp_rresp: %m" );
224 if ( ((unsigned char *)niov[ 0 ].iov_base)[ 0 ] != connid ||
225 ((char *)niov[ 0 ].iov_base)[ 1 ] != PAP_DATA ) {
226 syslog( LOG_ERR, "Bad data response!" );
230 for ( i = 0; i < atpb.atp_rresiovcnt; i++ ) {
232 niov[ i ].iov_base + 4, niov[ i ].iov_len - 4 );
233 if (( infile.pf_state & PF_EOF ) == 0 &&
234 ((char *)niov[ 0 ].iov_base)[ 2 ] ) {
235 infile.pf_state |= PF_EOF;
240 if ( ps( &infile, &outfile, sat ) < 0 ) {
241 syslog( LOG_ERR, "parse: bad return" );
242 return( -1 ); /* really? close? */
249 cbuf[ 1 ] = PAP_READ;
250 if ( ++seq == 0 ) seq = 1;
251 netseq = htons( seq );
252 memcpy( &cbuf[ 2 ], &netseq, sizeof( netseq ));
253 atpb.atp_saddr = sat;
254 atpb.atp_sreqdata = cbuf;
255 atpb.atp_sreqdlen = 4; /* bytes in SendData request */
256 atpb.atp_sreqto = 5; /* retry timer */
257 atpb.atp_sreqtries = -1; /* infinite retries */
258 if ( atp_sreq( atp, &atpb, oquantum, ATP_XO )) {
259 syslog( LOG_ERR, "atp_sreq: %m" );
268 syslog( LOG_ERR, "atp_rsel: %m" );
272 /* send any data that we have */
274 ( outfile.pf_datalen || ( outfile.pf_state & PF_EOF ))) {
275 for ( i = 0; i < quantum; i++ ) {
276 ((char *)niov[ i ].iov_base)[ 0 ] = connid;
277 ((char *)niov[ i ].iov_base)[ 1 ] = PAP_DATA;
278 ((char *)niov[ i ].iov_base)[ 2 ] =
279 ((char *)niov[ i ].iov_base)[ 3 ] = 0;
281 if ( outfile.pf_datalen > PAP_MAXDATA ) {
284 cc = outfile.pf_datalen;
285 if ( outfile.pf_state & PF_EOF ) {
286 ((char *)niov[ 0 ].iov_base)[ 2 ] = 1; /* eof */
287 outfile.pf_state = PF_BOT;
288 infile.pf_state = PF_BOT;
292 niov[ i ].iov_len = 4 + cc;
293 memcpy( niov[ i ].iov_base + 4, outfile.pf_data, cc );
294 CONSUME( &outfile, cc );
295 if ( outfile.pf_datalen == 0 ) {
300 ssat.sat_port = readport;
301 atpb.atp_saddr = &ssat;
302 atpb.atp_sresiov = niov;
303 atpb.atp_sresiovcnt = i; /* reported by stevebn@pc1.eos.co.uk */
304 if ( atp_sresp( atp, &atpb ) < 0 ) {
305 syslog( LOG_ERR, "atp_sresp: %m" );