forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqp.c
More file actions
385 lines (330 loc) · 7.07 KB
/
Copy pathqp.c
File metadata and controls
385 lines (330 loc) · 7.07 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
inet/qp.c
Query parameters
Created: June 1995 by Philip Homburg <philip@f-mnx.phicoh.com>
*/
#include "inet.h"
#include "generic/assert.h"
#include <sys/svrctl.h>
#include "queryparam.h"
#include "generic/buf.h"
#include "generic/clock.h"
#include "generic/event.h"
#include "generic/type.h"
#include "generic/sr.h"
#include "generic/tcp_int.h"
#include "generic/udp_int.h"
#include "mq.h"
#include "qp.h"
#include "sr_int.h"
THIS_FILE
#define MAX_REQ 1024 /* Maximum size of a request */
#define QP_FD_NR 4
typedef struct qp_fd
{
int qf_flags;
int qf_srfd;
get_userdata_t qf_get_userdata;
put_userdata_t qf_put_userdata;
acc_t *qf_req_pkt;
} qp_fd_t;
#define QFF_EMPTY 0
#define QFF_INUSE 1
static qp_fd_t qp_fd_table[QP_FD_NR];
static struct export_param_list inet_ex_list[]=
{
QP_VARIABLE(sr_fd_table),
QP_VARIABLE(ip_dev),
QP_VARIABLE(tcp_fd_table),
QP_VARIABLE(tcp_conn_table),
QP_VARIABLE(tcp_cancel_f),
QP_VECTOR(udp_port_table, udp_port_table, ip_conf_nr),
QP_VARIABLE(udp_fd_table),
QP_END()
};
static struct export_params inet_ex_params= { inet_ex_list, NULL };
static struct queryvars {
/* Input */
acc_t *param;
/* Output */
qp_fd_t *qp_fd;
off_t fd_offset;
size_t rd_bytes_left;
off_t outbuf_off;
char outbuf[256];
int r; /* result */
} *qvars;
static int qp_open ARGS(( int port, int srfd,
get_userdata_t get_userdata, put_userdata_t put_userdata,
put_pkt_t put_pkt, select_res_t select_res ));
static void qp_close ARGS(( int fd ));
static int qp_read ARGS(( int fd, size_t count ));
static int qp_write ARGS(( int fd, size_t count ));
static int qp_ioctl ARGS(( int fd, ioreq_t req ));
static int qp_cancel ARGS(( int fd, int which_operation ));
static int qp_select ARGS(( int fd, unsigned operations ));
static qp_fd_t *get_qp_fd ARGS(( int fd ));
static int do_query ARGS(( qp_fd_t *qp_fd, acc_t *pkt, int count ));
static int qp_getc ARGS(( void ));
static void qp_putc ARGS(( struct queryvars *qv, int c ));
static void qp_buffree ARGS(( int priority ));
#ifdef BUF_CONSISTENCY_CHECK
static void qp_bufcheck ARGS(( void ));
#endif
void qp_init()
{
int i;
qp_export(&inet_ex_params);
for (i= 0; i<QP_FD_NR; i++)
qp_fd_table[i].qf_flags= QFF_EMPTY;
#ifndef BUF_CONSISTENCY_CHECK
bf_logon(qp_buffree);
#else
bf_logon(qp_buffree, qp_bufcheck);
#endif
sr_add_minor(IPSTAT_MINOR, 0, qp_open, qp_close, qp_read, qp_write,
qp_ioctl, qp_cancel, qp_select);
}
static int qp_open(port, srfd, get_userdata, put_userdata, put_pkt,
select_res)
int port;
int srfd;
get_userdata_t get_userdata;
put_userdata_t put_userdata;
put_pkt_t put_pkt;
select_res_t select_res;
{
int i;
qp_fd_t *qp_fd;
for (i= 0; i< QP_FD_NR; i++)
{
if (!(qp_fd_table[i].qf_flags & QFF_INUSE))
break;
}
if (i >= QP_FD_NR)
return EAGAIN;
qp_fd= &qp_fd_table[i];
qp_fd->qf_flags= QFF_INUSE;
qp_fd->qf_srfd= srfd;
qp_fd->qf_get_userdata= get_userdata;
qp_fd->qf_put_userdata= put_userdata;
qp_fd->qf_req_pkt= NULL;
return i;
}
static void qp_close(fd)
int fd;
{
qp_fd_t *qp_fd;
qp_fd= get_qp_fd(fd);
qp_fd->qf_flags= QFF_EMPTY;
if (qp_fd->qf_req_pkt)
{
bf_afree(qp_fd->qf_req_pkt);
qp_fd->qf_req_pkt= NULL;
}
}
static int qp_read(fd, count)
int fd;
size_t count;
{
int r;
acc_t *pkt;
qp_fd_t *qp_fd;
qp_fd= get_qp_fd(fd);
pkt= qp_fd->qf_req_pkt;
qp_fd->qf_req_pkt= NULL;
if (!pkt)
{
/* Nothing to do */
qp_fd->qf_put_userdata(qp_fd->qf_srfd, EIO, 0,
FALSE /* !for_ioctl*/);
return OK;
}
r= do_query(qp_fd, pkt, count);
qp_fd->qf_put_userdata(qp_fd->qf_srfd, r, 0,
FALSE /* !for_ioctl*/);
return OK;
}
static int qp_write(fd, count)
int fd;
size_t count;
{
acc_t *pkt;
qp_fd_t *qp_fd;
qp_fd= get_qp_fd(fd);
if (count > MAX_REQ)
{
qp_fd->qf_get_userdata(qp_fd->qf_srfd, ENOMEM, 0,
FALSE /* !for_ioctl*/);
return OK;
}
pkt= qp_fd->qf_get_userdata(qp_fd->qf_srfd, 0, count,
FALSE /* !for_ioctl*/);
if (!pkt)
{
qp_fd->qf_get_userdata(qp_fd->qf_srfd, EFAULT, 0,
FALSE /* !for_ioctl*/);
return OK;
}
if (qp_fd->qf_req_pkt)
{
bf_afree(qp_fd->qf_req_pkt);
qp_fd->qf_req_pkt= NULL;
}
qp_fd->qf_req_pkt= pkt;
qp_fd->qf_get_userdata(qp_fd->qf_srfd, count, 0,
FALSE /* !for_ioctl*/);
return OK;
}
static int qp_ioctl(fd, req)
int fd;
ioreq_t req;
{
qp_fd_t *qp_fd;
qp_fd= get_qp_fd(fd);
qp_fd->qf_get_userdata(qp_fd->qf_srfd, ENOTTY, 0,
TRUE /* for_ioctl*/);
return OK;
}
static int qp_cancel(fd, which_operation)
int fd;
int which_operation;
{
ip_panic(( "qp_cancel: should not be here, no blocking calls" ));
return OK;
}
static int qp_select(fd, operations)
int fd;
unsigned operations;
{
unsigned resops;
resops= 0;
if (operations & SR_SELECT_READ)
resops |= SR_SELECT_READ;
if (operations & SR_SELECT_WRITE)
resops |= SR_SELECT_WRITE;
return resops;
}
static qp_fd_t *get_qp_fd(fd)
int fd;
{
qp_fd_t *qp_fd;
assert(fd >= 0 && fd < QP_FD_NR);
qp_fd= &qp_fd_table[fd];
assert(qp_fd->qf_flags & QFF_INUSE);
return qp_fd;
}
static int do_query(qp_fd, pkt, count)
qp_fd_t *qp_fd;
acc_t *pkt;
int count;
{
struct queryvars qv;
void *addr;
size_t n, size;
int byte;
int more;
static char hex[]= "0123456789ABCDEF";
qvars= &qv;
qv.param= pkt; pkt= NULL;
qv.qp_fd= qp_fd;
qv.fd_offset= 0;
qv.outbuf_off= 0;
qv.rd_bytes_left= count;
qv.r= 0;
do {
more= queryparam(qp_getc, &addr, &size);
for (n= 0; n < size; n++) {
byte= ((u8_t *) addr)[n];
qp_putc(&qv, hex[byte >> 4]);
qp_putc(&qv, hex[byte & 0x0F]);
}
qp_putc(&qv, more ? ',' : 0);
if (qv.r)
break;
} while (more);
if (qv.param)
{
assert(0);
bf_afree(qv.param);
qv.param= NULL;
}
if (qv.r)
return qv.r;
return qv.fd_offset;
}
static int qp_getc()
{
/* Return one character of the names to search for. */
acc_t *pkt;
struct queryvars *qv= qvars;
u8_t c;
pkt= qv->param;
qv->param= NULL;
if (pkt == NULL)
return 0;
assert(bf_bufsize(pkt) > 0);
c= ptr2acc_data(pkt)[0];
if (bf_bufsize(pkt) > 1)
qv->param= bf_delhead(pkt, 1);
else
{
bf_afree(pkt);
qv->param= NULL;
}
return c;
}
static void qp_putc(qv, c)
struct queryvars *qv;
int c;
{
/* Send one character back to the user. */
acc_t *pkt;
qp_fd_t *qp_fd;
size_t bytes_left;
off_t off;
bytes_left= qv->rd_bytes_left;
if (qv->r || bytes_left == 0)
return;
off= qv->outbuf_off;
assert(off < sizeof(qv->outbuf));
qv->outbuf[off]= c;
off++;
bytes_left--;
qv->rd_bytes_left= bytes_left;
if (c != '\0' && off < sizeof(qv->outbuf) && bytes_left != 0)
{
qv->outbuf_off= off;
return;
}
pkt= bf_memreq(off);
assert(pkt->acc_next == NULL);
memcpy(ptr2acc_data(pkt), qv->outbuf, off);
qp_fd= qv->qp_fd;
qv->r= qp_fd->qf_put_userdata(qp_fd->qf_srfd, qv->fd_offset,
pkt, FALSE /* !for_ioctl*/ );
qv->fd_offset += off;
qv->outbuf_off= 0;
}
static void qp_buffree (priority)
int priority;
{
/* For the moment, we are not going to free anything */
}
#ifdef BUF_CONSISTENCY_CHECK
static void qp_bufcheck()
{
int i;
qp_fd_t *qp_fd;
for (i= 0, qp_fd= qp_fd_table; i<QP_FD_NR; i++, qp_fd++)
{
if (!(qp_fd->qf_flags & QFF_INUSE))
continue;
if (qp_fd->qf_req_pkt)
bf_check_acc(qp_fd->qf_req_pkt);
}
}
#endif
/*
* $PchId: qp.c,v 1.7 2005/06/28 14:25:25 philip Exp $
*/