-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathatp_input.c
125 lines (116 loc) · 3.84 KB
/
atp_input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Add an AT packet to ATPs input queue
*
* Original work (c) 1997 Stefan Bethke. All rights reserved.
* Modified work (c) 2015 Jason King. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <string.h>
#undef s_net
#include <netatalk/at.h>
#include <atalk/ddp.h>
#include <atalk/atp.h>
#include <stdlib.h>
#include "atp_internals.h"
#ifdef EBUG
#include <stdio.h>
#endif
extern int at_addr_eq (struct sockaddr_at *paddr, struct sockaddr_at *saddr);
extern int atp_free_buf (struct atpbuf *);
extern struct atpbuf *atp_alloc_buf (void);
/*#define EBUG
*/
int atp_input(ATP ah, struct sockaddr_at *faddr, char *rbuf, int recvlen) {
struct atpbuf *pq, *cq;
struct atphdr ahdr;
u_short rfunc;
u_short rtid;
int i;
struct atpbuf *inbuf;
bcopy( rbuf + 1, (char *)&ahdr, sizeof( struct atphdr ));
if ( recvlen >= ATP_HDRSIZE && *rbuf == DDPTYPE_ATP) {
/* this is a valid ATP packet -- check for a match */
rfunc = ahdr.atphd_ctrlinfo & ATP_FUNCMASK;
rtid = ahdr.atphd_tid;
#ifdef EBUG
printf( "<%d> got tid=%hu func=", getpid(), ntohs( rtid ));
print_func( rfunc );
print_addr( " from", faddr );
putchar( '\n' );
bprint( rbuf, recvlen );
#endif
if ( rfunc == ATP_TREL ) {
/* remove response from sent list */
for ( pq = NULL, cq = ah->atph_sent; cq != NULL;
pq = cq, cq = cq->atpbuf_next ) {
if ( at_addr_eq( faddr, &cq->atpbuf_addr ) &&
cq->atpbuf_info.atpbuf_xo.atpxo_tid == ntohs( rtid ))
break;
}
if ( cq != NULL ) {
#ifdef EBUG
printf( "<%d> releasing transaction %hu\n", getpid(), ntohs( rtid ));
#endif
if ( pq == NULL ) {
ah->atph_sent = cq->atpbuf_next;
} else {
pq->atpbuf_next = cq->atpbuf_next;
}
for ( i = 0; i < 8; ++i ) {
if ( cq->atpbuf_info.atpbuf_xo.atpxo_packet[ i ]
!= NULL ) {
atp_free_buf ( cq->atpbuf_info.atpbuf_xo.atpxo_packet[ i ] );
}
}
atp_free_buf( cq );
}
} else {
/* add packet to incoming queue */
#ifdef EBUG
printf( "<%d> queuing incoming...\n", getpid() );
#endif
if (( inbuf = atp_alloc_buf()) == NULL ) {
#ifdef EBUG
printf( "<%d> can't alloc buffer\n", getpid() );
#endif
return -1;
}
bcopy( (char *)faddr, (char *)&inbuf->atpbuf_addr,
sizeof( struct sockaddr_at ));
inbuf->atpbuf_next = ah->atph_queue;
ah->atph_queue = inbuf;
inbuf->atpbuf_dlen = recvlen;
bcopy( (char *)rbuf,
(char *)inbuf->atpbuf_info.atpbuf_data, recvlen );
}
return 0;
}
return -1; /* invalid packet */
}