]> arthur.barton.de Git - netatalk.git/blob - libatalk/atp/atp_sresp.c
Initial revision
[netatalk.git] / libatalk / atp / atp_sresp.c
1 /*
2  * Copyright (c) 1990,1991 Regents of The University of Michigan.
3  * All Rights Reserved.
4  *
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.
14  *
15  *      Research Systems Unix Group
16  *      The University of Michigan
17  *      c/o Mike Clark
18  *      535 W. William Street
19  *      Ann Arbor, Michigan
20  *      +1-313-763-0525
21  *      netatalk@itd.umich.edu
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <errno.h>
28 #include <sys/uio.h>
29 #include <sys/time.h>
30
31 #include <netatalk/at.h>
32 #include <netatalk/endian.h>
33
34 #include <atalk/netddp.h>
35 #include <atalk/atp.h>
36 #include <atalk/util.h>
37
38 #include "atp_internals.h"
39
40 /* send a transaction response
41 */
42 int atp_sresp( ah, atpb )
43     ATP                 ah;             /* open atp handle */
44     struct atp_block    *atpb;          /* parameter block */
45 {
46     int                 i;
47     u_int8_t            ctrlinfo;
48     struct atpbuf       *resp_buf;
49     struct atpbuf       *save_buf;
50
51 #ifdef EBUG
52     atp_print_bufuse( ah, "atp_sresp" );
53 #endif
54
55     /* check parameters
56     */
57     for ( i = atpb->atp_sresiovcnt - 1; i >= 0; --i ) {
58         if ( atpb->atp_sresiov[ i ].iov_len > ATP_MAXDATA ) 
59             break;
60     }
61     if ( i >= 0 || atpb->atp_sresiovcnt < 1 || atpb->atp_sresiovcnt > 8 ) {
62         errno = EINVAL;
63         return -1;
64     }
65
66     /* allocate a new buffer for reponse packet construction
67     */
68     if (( resp_buf = atp_alloc_buf()) == NULL ) {
69         return -1;
70     }
71
72     /* send all the response packets
73      * also need to attach list to ah for dup. detection (if XO)
74     */
75 #ifdef EBUG
76     printf( "<%d> preparing to send %s response tid=%hu ", getpid(),
77             ah->atph_rxo ? "XO" : "", ah->atph_rtid );
78     atp_print_addr( " to", atpb->atp_saddr );
79     putchar( '\n' );
80 #endif
81     if ( ah->atph_rxo ) {
82         if (( save_buf = atp_alloc_buf()) == NULL ) {
83             return -1;
84         }
85         for ( i = 0; i < 8;
86                 save_buf->atpbuf_info.atpbuf_xo.atpxo_packet[ i++ ] = NULL );
87     }
88     for ( i = 0; i < atpb->atp_sresiovcnt; ++i ) {
89         ctrlinfo = ATP_TRESP;
90 #ifdef STS_RESPONSES
91         ctrlinfo |= ATP_STS;
92 #endif STS_RESPONSES
93         if ( i == atpb->atp_sresiovcnt-1 ) {
94             ctrlinfo |= ATP_EOM;
95         }
96         atp_build_resp_packet( resp_buf, ah->atph_rtid, ctrlinfo, atpb, i );
97
98         if ( ah->atph_rxo ) {
99             save_buf->atpbuf_info.atpbuf_xo.atpxo_packet[i] = resp_buf;
100         }
101 #ifdef DROPPACKETS
102 if (( random() % 3 ) != 2 ) {
103 #endif
104 #ifdef EBUG
105 printf( "<%d> sending packet tid=%hu serial no.=%d\n", getpid(),
106   ah->atph_rtid, i );
107 bprint( resp_buf->atpbuf_info.atpbuf_data, resp_buf->atpbuf_dlen );
108 #endif
109         if ( netddp_sendto( ah->atph_socket, resp_buf->atpbuf_info.atpbuf_data,
110           resp_buf->atpbuf_dlen, 0, (struct sockaddr *) atpb->atp_saddr,
111           sizeof( struct sockaddr_at )) != resp_buf->atpbuf_dlen ) {
112             if ( ah->atph_rxo ) {
113                 for ( ; i >= 0; --i ) {
114                     atp_free_buf( save_buf->atpbuf_info.atpbuf_xo.atpxo_packet[ i ] );
115                 }
116                 atp_free_buf( save_buf );
117             }
118             return -1;
119         }
120 #ifdef DROPPACKETS
121 } else printf( "<%d> atp_sresp: dropped serial no. %d\n", getpid(),  i );
122 #endif
123         /* allocate a buffer for next packet (if XO mode)
124         */
125         if ( ah->atph_rxo && ( resp_buf = atp_alloc_buf()) == NULL ) {
126             return -1;
127         }
128     }
129     atp_free_buf( resp_buf );
130     if ( ah->atph_rxo ) {
131         /* record timestamp, tid, release time, and destination address
132         */
133         gettimeofday( &save_buf->atpbuf_info.atpbuf_xo.atpxo_tv,
134                 (struct timezone *) 0 );
135         save_buf->atpbuf_info.atpbuf_xo.atpxo_tid = ah->atph_rtid;
136         save_buf->atpbuf_info.atpbuf_xo.atpxo_reltime = ah->atph_rreltime;
137         memcpy( &save_buf->atpbuf_addr, atpb->atp_saddr, 
138                 sizeof( struct sockaddr_at ));
139
140         /* add to list of packets we have sent
141         */
142         save_buf->atpbuf_next = ah->atph_sent;
143         ah->atph_sent = save_buf;
144 #ifdef EBUG
145 printf( "<%d> saved XO response\n", getpid());
146 #endif
147     }
148     return 0;
149 }