]> arthur.barton.de Git - netatalk.git/blob - libatalk/atp/atp_sreq.c
implemented config.h
[netatalk.git] / libatalk / atp / atp_sreq.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 <sys/time.h>
32 #include <errno.h>
33 #include <sys/uio.h>
34 #include <signal.h>
35
36 #include <netinet/in.h>
37 #include <netatalk/at.h>
38 #include <netatalk/endian.h>
39
40 #include <atalk/netddp.h>
41 #include <atalk/atp.h>
42 #include <atalk/util.h>
43
44 #include "atp_internals.h"
45
46 int
47 atp_sreq( ah, atpb, respcount, flags )
48     ATP                 ah;             /* open atp handle */
49     struct atp_block    *atpb;          /* parameter block */
50     int                 respcount;      /* buffers available for response */
51     u_int8_t            flags;          /* ATP_XO, ATP_TREL?? */
52 {
53     struct atpbuf       *req_buf;
54     int                 i;
55
56 #ifdef EBUG
57     atp_print_bufuse( ah, "atp_sreq" );
58 #endif
59
60     /* check parameters
61     */
62     if ( atpb->atp_sreqdlen < 4 || atpb->atp_sreqdlen > ATP_MAXDATA
63             || ( respcount < 0 ) || ( respcount > 8 )
64             || ( atpb->atp_sreqto < 0 ) || (( atpb->atp_sreqtries < 1 )
65             && ( atpb->atp_sreqtries != ATP_TRIES_INFINITE ))) {
66         errno = EINVAL;
67         return -1;
68     }
69     /* clean up any packet fragments left from last request
70     */
71     for ( i = 0; i < 8; ++i ) {
72         if ( ah->atph_resppkt[ i ] != NULL ) {
73             atp_free_buf( ah->atph_resppkt[ i ] );
74             ah->atph_resppkt[ i ] = NULL;
75         }
76     }
77
78     /* generate bitmap, tid and ctrlinfo
79     */
80     atpb->atp_bitmap = ( 1 << respcount ) - 1;
81
82     /* allocate a new buffer and build request packet
83     */
84     if (( req_buf = atp_alloc_buf()) == NULL ) {
85         return( -1 );
86     }
87     atp_build_req_packet( req_buf, ah->atph_tid++, flags | ATP_TREQ, atpb );
88     memcpy( &req_buf->atpbuf_addr, atpb->atp_saddr,
89             sizeof( struct sockaddr_at ));
90
91     /* send the initial request
92     */
93 #ifdef EBUG
94     printf( "\n<%d> atp_sreq: sending a %d byte packet ", getpid(),
95             req_buf->atpbuf_dlen );
96     atp_print_addr( " to", atpb->atp_saddr );
97     putchar( '\n' );
98     bprint( req_buf->atpbuf_info.atpbuf_data, req_buf->atpbuf_dlen );
99 #endif
100
101     gettimeofday( &ah->atph_reqtv, (struct timezone *)0 );
102 #ifdef DROPPACKETS
103 if (( random() % 3 ) != 2 ) {
104 #endif
105     if ( netddp_sendto( ah->atph_socket, req_buf->atpbuf_info.atpbuf_data,
106             req_buf->atpbuf_dlen, 0, (struct sockaddr *) atpb->atp_saddr,
107             sizeof( struct sockaddr_at )) != req_buf->atpbuf_dlen ) {
108         atp_free_buf( req_buf );
109         return( -1 );
110     }
111 #ifdef DROPPACKETS
112 } else printf( "<%d> atp_sreq: dropped request\n", getpid() );
113 #endif
114
115     if ( atpb->atp_sreqto != 0 ) {
116         if ( ah->atph_reqpkt != NULL ) {
117             atp_free_buf( ah->atph_reqpkt );
118         }
119         ah->atph_reqto = atpb->atp_sreqto;
120         if ( atpb->atp_sreqtries == ATP_TRIES_INFINITE ) {
121             ah->atph_reqtries = ATP_TRIES_INFINITE;
122         } else {
123             /* we already sent one */
124             ah->atph_reqtries = atpb->atp_sreqtries - 1;
125         }
126         ah->atph_reqpkt = req_buf;
127         ah->atph_rbitmap = ( 1 << respcount ) - 1;
128         ah->atph_rrespcount = respcount;
129     } else {
130         atp_free_buf( req_buf );
131         ah->atph_rrespcount = 0;
132     }
133
134     return( 0 );
135 }