2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
5 * Permission to use, copy, modify, and distribute this software and
6 * its documentation for any purpose and without fee is hereby granted,
7 * provided that the above copyright notice appears in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the name of The University
10 * of Michigan not be used in advertising or publicity pertaining to
11 * distribution of the software without specific, written prior
12 * permission. This software is supplied as is without expressed or
13 * implied warranties of any kind.
15 * Research Systems Unix Group
16 * The University of Michigan
18 * 535 W. William Street
21 * netatalk@itd.umich.edu
25 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <netinet/in.h>
31 #include <netatalk/at.h>
32 #include <netatalk/endian.h>
34 #include <atalk/netddp.h>
35 #include <atalk/ddp.h>
36 #include <atalk/atp.h>
37 #include <atalk/util.h>
39 #include "atp_internals.h"
41 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
43 #define SOCKLEN_T unsigned int
49 static void print_func( ctrlinfo )
52 switch ( ctrlinfo & ATP_FUNCMASK ) {
66 printf( "%x", ctrlinfo & ATP_FUNCMASK );
70 static void dump_packet( buf, len )
76 for ( i = 0; i < len; ++i ) {
77 printf( "%x-%c ", buf[i], buf[i] );
82 void atp_print_addr( s, saddr )
84 struct sockaddr_at *saddr;
87 saddr->sat_family == AF_APPLETALK ? printf( "at." ) :
88 printf( "%d.", saddr->sat_family );
89 saddr->sat_addr.s_net == ATADDR_ANYNET ? printf( "*." ) :
90 printf( "%d.", ntohs( saddr->sat_addr.s_net ));
91 saddr->sat_addr.s_node == ATADDR_ANYNODE ? printf( "*." ) :
92 printf( "%d.", saddr->sat_addr.s_node );
93 saddr->sat_port == ATADDR_ANYPORT ? printf( "*" ) :
94 printf( "%d", saddr->sat_port );
99 void atp_build_req_packet( pktbuf, tid, ctrl, atpb )
100 struct atpbuf *pktbuf;
103 struct atp_block *atpb;
107 /* fill in the packet fields
109 hdr.atphd_ctrlinfo = ctrl;
110 hdr.atphd_bitmap = atpb->atp_bitmap;
111 hdr.atphd_tid = htons( tid );
112 *(pktbuf->atpbuf_info.atpbuf_data) = DDPTYPE_ATP;
113 memcpy(pktbuf->atpbuf_info.atpbuf_data + 1, &hdr, sizeof( struct atphdr ));
114 memcpy(pktbuf->atpbuf_info.atpbuf_data + ATP_HDRSIZE,
115 atpb->atp_sreqdata, atpb->atp_sreqdlen );
119 pktbuf->atpbuf_dlen = ATP_HDRSIZE + atpb->atp_sreqdlen;
122 void atp_build_resp_packet( pktbuf, tid, ctrl, atpb, seqnum )
123 struct atpbuf *pktbuf;
126 struct atp_block *atpb;
131 /* fill in the packet fields */
132 *(pktbuf->atpbuf_info.atpbuf_data) = DDPTYPE_ATP;
133 hdr.atphd_ctrlinfo = ctrl;
134 hdr.atphd_bitmap = seqnum;
135 hdr.atphd_tid = htons( tid );
136 memcpy(pktbuf->atpbuf_info.atpbuf_data + 1, &hdr,
137 sizeof( struct atphdr ));
138 memcpy(pktbuf->atpbuf_info.atpbuf_data + ATP_HDRSIZE,
139 atpb->atp_sresiov[ seqnum ].iov_base,
140 atpb->atp_sresiov[ seqnum ].iov_len );
144 pktbuf->atpbuf_dlen = ATP_HDRSIZE + atpb->atp_sresiov[ seqnum ].iov_len;
149 atp_recv_atp( ah, fromaddr, func, tid, rbuf, wait )
151 struct sockaddr_at *fromaddr;
158 Receive a packet from address fromaddr of the correct function type
159 and with the correct tid. fromaddr = AT_ANY... and function == ATP_TYPEANY
160 and tid == ATP_TIDANY can be used to wildcard match.
162 recv_atp returns the length of the packet received (or -1 if error)
163 The function code for the packet received is returned in *func (ATP_TREQ or
166 struct atpbuf *pq, *cq;
173 struct sockaddr_at faddr;
175 struct atpbuf *inbuf;
179 /* first check the queue
182 atp_print_bufuse( ah, "recv_atp checking queue" );
184 for ( pq = NULL, cq = ah->atph_queue; cq != NULL;
185 pq = cq, cq = cq->atpbuf_next ) {
186 memcpy(&ahdr, cq->atpbuf_info.atpbuf_data + 1,
187 sizeof( struct atphdr ));
188 rfunc = ahdr.atphd_ctrlinfo & ATP_FUNCMASK;
190 printf( "<%d> checking", getpid());
191 printf( " tid=%hu func=", ntohs( ahdr.atphd_tid ));
193 atp_print_addr( " from", &cq->atpbuf_addr );
196 if ((( tid & ahdr.atphd_tid ) == ahdr.atphd_tid ) &&
197 (( *func & rfunc ) == rfunc )
198 && at_addr_eq( fromaddr, &cq->atpbuf_addr )) {
203 /* we found one in the queue -- copy to rbuf
205 dlen = cq->atpbuf_dlen;
207 memcpy( fromaddr, &cq->atpbuf_addr, sizeof( struct sockaddr_at ));
208 memcpy( rbuf, cq->atpbuf_info.atpbuf_data, cq->atpbuf_dlen );
210 /* remove packet from queue and free buffer
213 ah->atph_queue = NULL;
215 pq->atpbuf_next = cq->atpbuf_next;
221 /* we need to get it the net -- call on ddp to receive a packet
224 printf( "<%d>", getpid());
225 atp_print_addr( " waiting on address", &ah->atph_saddr );
226 printf( "\nfor tid=%hu func=", ntohs( tid ));
228 atp_print_addr( " from", fromaddr );
236 faddrlen = sizeof( struct sockaddr_at );
237 memset( &faddr, 0, sizeof( struct sockaddr_at ));
239 if (( recvlen = netddp_recvfrom( ah->atph_socket, rbuf,
241 (struct sockaddr *) &faddr,
245 memcpy( &ahdr, rbuf + 1, sizeof( struct atphdr ));
246 if ( recvlen >= ATP_HDRSIZE && *rbuf == DDPTYPE_ATP) {
247 /* this is a valid ATP packet -- check for a match */
248 rfunc = ahdr.atphd_ctrlinfo & ATP_FUNCMASK;
249 rtid = ahdr.atphd_tid;
251 printf( "<%d> got tid=%hu func=", getpid(), ntohs( rtid ));
253 atp_print_addr( " from", &faddr );
255 bprint( rbuf, recvlen );
257 if ( rfunc == ATP_TREL ) {
258 /* remove response from sent list */
259 for ( pq = NULL, cq = ah->atph_sent; cq != NULL;
260 pq = cq, cq = cq->atpbuf_next ) {
261 if ( at_addr_eq( &faddr, &cq->atpbuf_addr ) &&
262 cq->atpbuf_info.atpbuf_xo.atpxo_tid == ntohs( rtid ))
267 printf( "<%d> releasing transaction %hu\n", getpid(), ntohs( rtid ));
270 ah->atph_sent = cq->atpbuf_next;
272 pq->atpbuf_next = cq->atpbuf_next;
274 for ( i = 0; i < 8; ++i ) {
275 if ( cq->atpbuf_info.atpbuf_xo.atpxo_packet[ i ]
277 atp_free_buf ( cq->atpbuf_info.atpbuf_xo.atpxo_packet[ i ] );
283 } else if ((( tid & rtid ) == rtid ) &&
284 (( *func & rfunc ) == rfunc ) &&
285 at_addr_eq( fromaddr, &faddr )) { /* got what we wanted */
288 memcpy( fromaddr, &faddr, sizeof( struct sockaddr_at ));
291 /* add packet to incoming queue */
293 printf( "<%d> queuing incoming...\n", getpid() );
295 if (( inbuf = atp_alloc_buf()) == NULL ) {
298 memcpy( &inbuf->atpbuf_addr, &faddr,
299 sizeof( struct sockaddr_at ));
300 inbuf->atpbuf_next = ah->atph_queue;
301 inbuf->atpbuf_dlen = recvlen;
302 memcpy( inbuf->atpbuf_info.atpbuf_data, rbuf, recvlen );
305 if ( !wait && dlen < 0 ) {
309 } while ( dlen < 0 );
315 int at_addr_eq( paddr, saddr )
316 struct sockaddr_at *paddr; /* primary address */
317 struct sockaddr_at *saddr; /* secondary address */
319 /* compare two atalk addresses -- only check the non-zero fields
320 of paddr against saddr.
321 return zero if not equal, non-zero if equal
323 return (( paddr->sat_port == ATADDR_ANYPORT || paddr->sat_port == saddr->sat_port )
324 && ( paddr->sat_addr.s_net == ATADDR_ANYNET ||
325 paddr->sat_addr.s_net == saddr->sat_addr.s_net )
326 && ( paddr->sat_addr.s_node == ATADDR_ANYNODE ||
327 paddr->sat_addr.s_node == saddr->sat_addr.s_node ));