]> arthur.barton.de Git - netatalk.git/blob - libatalk/atp/atp_sreq.c
Removed duplicate bprint() function (see util/bprint.c).
[netatalk.git] / libatalk / atp / atp_sreq.c
1 /*
2  * $Id: atp_sreq.c,v 1.3 2001-06-29 14:14:46 rufustfirefly Exp $
3  *
4  * Copyright (c) 1990,1991 Regents of The University of Michigan.
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and
8  * its documentation for any purpose and without fee is hereby granted,
9  * provided that the above copyright notice appears in all copies and
10  * that both that copyright notice and this permission notice appear
11  * in supporting documentation, and that the name of The University
12  * of Michigan not be used in advertising or publicity pertaining to
13  * distribution of the software without specific, written prior
14  * permission. This software is supplied as is without expressed or
15  * implied warranties of any kind.
16  *
17  *      Research Systems Unix Group
18  *      The University of Michigan
19  *      c/o Mike Clark
20  *      535 W. William Street
21  *      Ann Arbor, Michigan
22  *      +1-313-763-0525
23  *      netatalk@itd.umich.edu
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif /* HAVE_CONFIG_H */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <errno.h>
35 #include <sys/uio.h>
36 #include <signal.h>
37
38 #include <netinet/in.h>
39 #include <netatalk/at.h>
40 #include <netatalk/endian.h>
41
42 #include <atalk/netddp.h>
43 #include <atalk/atp.h>
44 #include <atalk/util.h>
45
46 #include "atp_internals.h"
47
48 int
49 atp_sreq( ah, atpb, respcount, flags )
50     ATP                 ah;             /* open atp handle */
51     struct atp_block    *atpb;          /* parameter block */
52     int                 respcount;      /* buffers available for response */
53     u_int8_t            flags;          /* ATP_XO, ATP_TREL?? */
54 {
55     struct atpbuf       *req_buf;
56     int                 i;
57
58 #ifdef EBUG
59     atp_print_bufuse( ah, "atp_sreq" );
60 #endif /* EBUG */
61
62     /* check parameters
63     */
64     if ( atpb->atp_sreqdlen < 4 || atpb->atp_sreqdlen > ATP_MAXDATA
65             || ( respcount < 0 ) || ( respcount > 8 )
66             || ( atpb->atp_sreqto < 0 ) || (( atpb->atp_sreqtries < 1 )
67             && ( atpb->atp_sreqtries != ATP_TRIES_INFINITE ))) {
68         errno = EINVAL;
69         return -1;
70     }
71     /* clean up any packet fragments left from last request
72     */
73     for ( i = 0; i < 8; ++i ) {
74         if ( ah->atph_resppkt[ i ] != NULL ) {
75             atp_free_buf( ah->atph_resppkt[ i ] );
76             ah->atph_resppkt[ i ] = NULL;
77         }
78     }
79
80     /* generate bitmap, tid and ctrlinfo
81     */
82     atpb->atp_bitmap = ( 1 << respcount ) - 1;
83
84     /* allocate a new buffer and build request packet
85     */
86     if (( req_buf = atp_alloc_buf()) == NULL ) {
87         return( -1 );
88     }
89     atp_build_req_packet( req_buf, ah->atph_tid++, flags | ATP_TREQ, atpb );
90     memcpy( &req_buf->atpbuf_addr, atpb->atp_saddr,
91             sizeof( struct sockaddr_at ));
92
93     /* send the initial request
94     */
95 #ifdef EBUG
96     printf( "\n<%d> atp_sreq: sending a %d byte packet ", getpid(),
97             req_buf->atpbuf_dlen );
98     atp_print_addr( " to", atpb->atp_saddr );
99     putchar( '\n' );
100     bprint( req_buf->atpbuf_info.atpbuf_data, req_buf->atpbuf_dlen );
101 #endif /* EBUG */
102
103     gettimeofday( &ah->atph_reqtv, (struct timezone *)0 );
104 #ifdef DROPPACKETS
105 if (( random() % 3 ) != 2 ) {
106 #endif /* DROPPACKETS */
107     if ( netddp_sendto( ah->atph_socket, req_buf->atpbuf_info.atpbuf_data,
108             req_buf->atpbuf_dlen, 0, (struct sockaddr *) atpb->atp_saddr,
109             sizeof( struct sockaddr_at )) != req_buf->atpbuf_dlen ) {
110         atp_free_buf( req_buf );
111         return( -1 );
112     }
113 #ifdef DROPPACKETS
114 } else printf( "<%d> atp_sreq: dropped request\n", getpid() );
115 #endif /* DROPPACKETS */
116
117     if ( atpb->atp_sreqto != 0 ) {
118         if ( ah->atph_reqpkt != NULL ) {
119             atp_free_buf( ah->atph_reqpkt );
120         }
121         ah->atph_reqto = atpb->atp_sreqto;
122         if ( atpb->atp_sreqtries == ATP_TRIES_INFINITE ) {
123             ah->atph_reqtries = ATP_TRIES_INFINITE;
124         } else {
125             /* we already sent one */
126             ah->atph_reqtries = atpb->atp_sreqtries - 1;
127         }
128         ah->atph_reqpkt = req_buf;
129         ah->atph_rbitmap = ( 1 << respcount ) - 1;
130         ah->atph_rrespcount = respcount;
131     } else {
132         atp_free_buf( req_buf );
133         ah->atph_rrespcount = 0;
134     }
135
136     return( 0 );
137 }