-
Notifications
You must be signed in to change notification settings - Fork 1
/
mread.c
245 lines (178 loc) · 4.95 KB
/
mread.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "marignotti.h"
#include <gno/kerntool.h>
#include <errno.h>
#include <sys/socket.h>
#include <memory.h>
#include <misctool.h>
#include "s16debug.h"
#pragma noroot
#pragma optimize 79
typedef struct xipheader {
byte verlen;
byte tos;
word len;
word id;
word fragoff;
byte ttl;
byte proto;
word cksum;
longword src;
longword dst;
// verlen & 0x0f is the number of 32-but words
// if > 5, extra data present and should be skipped.
// data...
} xipheader;
typedef struct xudpheader {
word source;
word dst;
word len;
word cksum;
// data...
} xudpheader;
// todo -- should have addr/addrlen for recvfrom.
static int sock_read(
Entry *e,
void *buffer,
LongWord nbytes,
LongWord *outbytes)
{
LongWord size;
LongWord lowat;
Word t;
Word terr;
rrBuff rr;
if (e->_SHUT_RD)
return 0;
if (e->_TYPE == SOCK_DGRAM)
{
// todo -- address support.
// manually parse the ip header and udp header.
//
Handle h;
xipheader *ip;
Word offset = 0;
char *cp;
h = TCPIPGetNextDatagram(e->ipid, protocolUDP, 0);
t = _toolErr;
if (t) return ENETDOWN;
if (!h) return EAGAIN;
size = GetHandleSize(h);
HLock(h);
cp = *(char **)h;
ip = (xipheader *)cp;
offset = (ip->verlen & 0x0f) << 2;
// would get the address here.
// ip->src
offset += u_data;
if (offset >= size)
{
// ???
DisposeHandle(h);
return 0;
}
size -= offset;
cp += offset;
// nbytes is the max read... any overflow is discarded.
if (size > nbytes) size = nbytes;
BlockMove(cp, buffer, nbytes);
if (Debug > 1)
{
s16_debug_dump(buffer, (Word)size);
}
DisposeHandle(h);
*outbytes = size;
return 0;
}
// todo -- address support (which will always be the dest ip...)
terr = TCPIPStatusTCP(e->ipid, &e->sr);
t = _toolErr;
if (t) terr = t;
e->terr = terr;
if (t) return ENETDOWN;
if (e->sr.srState < TCPSESTABLISHED) return ENOTCONN;
size = e->sr.srRcvQueued;
lowat = e->_RCVLOWAT;
// if the state is established, read LOWAT...nbytes.
// if the connection is closing, LOWAT is 1.
if (e->sr.srState > TCPSESTABLISHED)
{
lowat = 1;
if (!size) return 0; // eof
}
if (size < nbytes && size >= lowat)
nbytes = size;
if (size < nbytes) return EAGAIN;
// size >= nbytes.
terr = TCPIPReadTCP(e->ipid, 0, (Ref)buffer, nbytes, &rr);
t = _toolErr;
if (t) terr = t;
e->terr = terr;
// todo -- if there was a push, < nbyte will have been read.
// loop until nbytes have been read?
if (Debug > 1)
{
s16_debug_dump(buffer, (Word)rr.rrBuffCount);
}
*outbytes = rr.rrBuffCount;
return 0;
}
int mreadoob(Entry *e, void *p1, void *p2, void *p3, void *p4, void *p5)
{
// called via recv, recvfrom.
// OOB is always inline, therefore an OOB read will
// just return 0.
// todo -- just treat as regular read?
char *buffer = (char *)p1;
LongWord nbytes = *(LongWord *)p2;
xsockaddr *addr = (xsockaddr *)p3;
int addrlen = p4 ? *(int *)p4 : 0;
LongWord *outbytes = (LongWord *)p2;
*outbytes = 0;
if (Debug > 0)
{
s16_debug_printf("oob read nbytes = %ld", nbytes);
}
return 0;
}
// called through ReadGS, recv, recvfrom
int mread(Entry *e, void *p1, void *p2, void *p3, void *p4, void *p5)
{
int xerrno = 0;
LongWord timeout;
char *buffer = (char *)p1;
LongWord nbytes = *(LongWord *)p2;
xsockaddr *addr = (xsockaddr *)p3;
int addrlen = p4 ? *(int *)p4 : 0;
LongWord *outbytes = (LongWord *)p2;
*outbytes = 0;
if (Debug > 0)
{
s16_debug_printf("read nbytes = %ld", nbytes);
}
IncBusy();
xerrno = sock_read(e, buffer, nbytes, outbytes);
DecBusy();
if (xerrno != EAGAIN || e->_NONBLOCK)
{
return xerrno;
}
if (e->_RCVTIMEO)
timeout = GetTick() + e->_RCVTIMEO;
else
timeout = 0;
for(;;)
{
xerrno = queue_command(e, kCommandRead, nbytes, timeout);
if (xerrno == EINTR) return EINTR;
if (xerrno) return EIO;
if (e->command) continue; // reset to 0 if processed.
IncBusy();
xerrno = sock_read(e, buffer, nbytes, outbytes);
DecBusy();
if (xerrno != EAGAIN) return xerrno;
if (timeout && timeout <= GetTick())
return EAGAIN;
}
// should not hit...
return 0;
}