]> arthur.barton.de Git - netatalk.git/blob - libatalk/atp/atp_rreq.c
be11f4298b0f508413c83e254d391d235dbad8a4
[netatalk.git] / libatalk / atp / atp_rreq.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
30 #include <netinet/in.h>
31 #include <netatalk/at.h>
32 #include <netatalk/endian.h>
33 #include <atalk/atp.h>
34
35 #include "atp_internals.h"
36
37
38 /* wait for a tranasaction service request
39 */
40 int atp_rreq( ah, atpb )
41     ATP                 ah;             /* open atp handle */
42     struct atp_block    *atpb;          /* parameter block */
43 {
44     struct atpbuf       *req_buf;       /* for receiving request packet */
45     struct atphdr       req_hdr;        /* request header overlay */
46     struct sockaddr_at  faddr;          /* sender's address */
47     int                 recvlen;        /* length of received packet */
48     u_int16_t           tid;
49     int                 rc;
50     u_int8_t            func;
51
52 #ifdef EBUG
53     atp_print_bufuse( ah, "atp_rreq" );
54 #endif
55
56     while (( rc = atp_rsel( ah, atpb->atp_saddr, ATP_TREQ )) == 0 ) {
57         ;
58     }
59
60     if ( rc != ATP_TREQ ) {
61 #ifdef EBUG
62         printf( "<%d> atp_rreq: atp_rsel returns err %d\n", getpid(), rc );
63 #endif EBUG
64         return( rc );
65     }
66
67     /* allocate a buffer for receiving request
68     */
69     if (( req_buf = atp_alloc_buf()) == NULL ) {
70         return -1;
71     }
72
73     memcpy( &faddr, atpb->atp_saddr, sizeof( struct sockaddr_at ));
74     func = ATP_TREQ;
75     if (( recvlen = atp_recv_atp( ah, &faddr, &func, ATP_TIDANY,
76           req_buf->atpbuf_info.atpbuf_data, 1 )) < 0 ) {
77         atp_free_buf( req_buf );
78         return -1;
79     }
80
81     memcpy( &req_hdr, req_buf->atpbuf_info.atpbuf_data + 1, 
82             sizeof( struct atphdr ));
83     tid = ntohs( req_hdr.atphd_tid );
84
85     ah->atph_rtid = tid;
86     if (( ah->atph_rxo = req_hdr.atphd_ctrlinfo & ATP_XO ) != 0 ) {
87         ah->atph_rreltime = ATP_RELTIME *
88                 ( 1 << ( req_hdr.atphd_ctrlinfo & ATP_TRELMASK ));
89     }
90
91     memcpy( atpb->atp_saddr, &faddr, sizeof( struct sockaddr_at ));
92
93     if ( recvlen - ATP_HDRSIZE > atpb->atp_rreqdlen ) {
94         atp_free_buf( req_buf );
95         errno = EMSGSIZE;
96         return -1;
97     }
98
99     atpb->atp_rreqdlen = recvlen - ATP_HDRSIZE;
100     memcpy( atpb->atp_rreqdata, 
101             req_buf->atpbuf_info.atpbuf_data + ATP_HDRSIZE,
102             recvlen - ATP_HDRSIZE );
103     atpb->atp_bitmap = req_hdr.atphd_bitmap;
104     atp_free_buf( req_buf );
105     return( 0 );
106 }