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