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