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