unstack - fix ipcvecs
[minix.git] / servers / inet / qp.c
blobdf09c856b23b5c4240089c2487ba59d51f43c67e
1 /*
2 inet/qp.c
4 Query parameters
6 Created: June 1995 by Philip Homburg <philip@f-mnx.phicoh.com>
7 */
9 #include "inet.h"
10 #include "generic/assert.h"
12 #include <sys/svrctl.h>
13 #include "queryparam.h"
15 #include "generic/buf.h"
16 #include "generic/clock.h"
17 #include "generic/event.h"
18 #include "generic/type.h"
19 #include "generic/sr.h"
21 #include "generic/tcp_int.h"
22 #include "generic/udp_int.h"
23 #include "mq.h"
24 #include "qp.h"
25 #include "sr_int.h"
27 THIS_FILE
29 #define MAX_REQ 1024 /* Maximum size of a request */
31 #define QP_FD_NR 4
33 typedef struct qp_fd
35 int qf_flags;
36 int qf_srfd;
37 get_userdata_t qf_get_userdata;
38 put_userdata_t qf_put_userdata;
39 acc_t *qf_req_pkt;
40 } qp_fd_t;
42 #define QFF_EMPTY 0
43 #define QFF_INUSE 1
45 static qp_fd_t qp_fd_table[QP_FD_NR];
47 static struct export_param_list inet_ex_list[]=
49 QP_VARIABLE(sr_fd_table),
50 QP_VARIABLE(ip_dev),
51 QP_VARIABLE(tcp_fd_table),
52 QP_VARIABLE(tcp_conn_table),
53 QP_VARIABLE(tcp_cancel_f),
54 QP_VECTOR(udp_port_table, udp_port_table, ip_conf_nr),
55 QP_VARIABLE(udp_fd_table),
56 QP_END()
59 static struct export_params inet_ex_params= { inet_ex_list, NULL };
61 static struct queryvars {
62 /* Input */
63 acc_t *param;
65 /* Output */
66 qp_fd_t *qp_fd;
67 off_t fd_offset;
68 size_t rd_bytes_left;
69 off_t outbuf_off;
70 char outbuf[256];
72 int r; /* result */
73 } *qvars;
76 static int qp_open ARGS(( int port, int srfd,
77 get_userdata_t get_userdata, put_userdata_t put_userdata,
78 put_pkt_t put_pkt, select_res_t select_res ));
79 static void qp_close ARGS(( int fd ));
80 static int qp_read ARGS(( int fd, size_t count ));
81 static int qp_write ARGS(( int fd, size_t count ));
82 static int qp_ioctl ARGS(( int fd, ioreq_t req ));
83 static int qp_cancel ARGS(( int fd, int which_operation ));
84 static int qp_select ARGS(( int fd, unsigned operations ));
85 static qp_fd_t *get_qp_fd ARGS(( int fd ));
86 static int do_query ARGS(( qp_fd_t *qp_fd, acc_t *pkt, int count ));
87 static int qp_getc ARGS(( void ));
88 static void qp_putc ARGS(( struct queryvars *qv, int c ));
89 static void qp_buffree ARGS(( int priority ));
90 #ifdef BUF_CONSISTENCY_CHECK
91 static void qp_bufcheck ARGS(( void ));
92 #endif
94 void qp_init()
96 int i;
98 qp_export(&inet_ex_params);
100 for (i= 0; i<QP_FD_NR; i++)
101 qp_fd_table[i].qf_flags= QFF_EMPTY;
103 #ifndef BUF_CONSISTENCY_CHECK
104 bf_logon(qp_buffree);
105 #else
106 bf_logon(qp_buffree, qp_bufcheck);
107 #endif
109 sr_add_minor(IPSTAT_MINOR, 0, qp_open, qp_close, qp_read, qp_write,
110 qp_ioctl, qp_cancel, qp_select);
113 static int qp_open(port, srfd, get_userdata, put_userdata, put_pkt,
114 select_res)
115 int port;
116 int srfd;
117 get_userdata_t get_userdata;
118 put_userdata_t put_userdata;
119 put_pkt_t put_pkt;
120 select_res_t select_res;
122 int i;
123 qp_fd_t *qp_fd;
125 for (i= 0; i< QP_FD_NR; i++)
127 if (!(qp_fd_table[i].qf_flags & QFF_INUSE))
128 break;
130 if (i >= QP_FD_NR)
131 return EAGAIN;
132 qp_fd= &qp_fd_table[i];
133 qp_fd->qf_flags= QFF_INUSE;
134 qp_fd->qf_srfd= srfd;
135 qp_fd->qf_get_userdata= get_userdata;
136 qp_fd->qf_put_userdata= put_userdata;
137 qp_fd->qf_req_pkt= NULL;
139 return i;
142 static void qp_close(fd)
143 int fd;
145 qp_fd_t *qp_fd;
147 qp_fd= get_qp_fd(fd);
148 qp_fd->qf_flags= QFF_EMPTY;
149 if (qp_fd->qf_req_pkt)
151 bf_afree(qp_fd->qf_req_pkt);
152 qp_fd->qf_req_pkt= NULL;
156 static int qp_read(fd, count)
157 int fd;
158 size_t count;
160 int r;
161 acc_t *pkt;
162 qp_fd_t *qp_fd;
164 qp_fd= get_qp_fd(fd);
165 pkt= qp_fd->qf_req_pkt;
166 qp_fd->qf_req_pkt= NULL;
167 if (!pkt)
169 /* Nothing to do */
170 qp_fd->qf_put_userdata(qp_fd->qf_srfd, EIO, 0,
171 FALSE /* !for_ioctl*/);
172 return OK;
174 r= do_query(qp_fd, pkt, count);
175 qp_fd->qf_put_userdata(qp_fd->qf_srfd, r, 0,
176 FALSE /* !for_ioctl*/);
177 return OK;
180 static int qp_write(fd, count)
181 int fd;
182 size_t count;
184 acc_t *pkt;
185 qp_fd_t *qp_fd;
187 qp_fd= get_qp_fd(fd);
188 if (count > MAX_REQ)
190 qp_fd->qf_get_userdata(qp_fd->qf_srfd, ENOMEM, 0,
191 FALSE /* !for_ioctl*/);
192 return OK;
194 pkt= qp_fd->qf_get_userdata(qp_fd->qf_srfd, 0, count,
195 FALSE /* !for_ioctl*/);
196 if (!pkt)
198 qp_fd->qf_get_userdata(qp_fd->qf_srfd, EFAULT, 0,
199 FALSE /* !for_ioctl*/);
200 return OK;
202 if (qp_fd->qf_req_pkt)
204 bf_afree(qp_fd->qf_req_pkt);
205 qp_fd->qf_req_pkt= NULL;
207 qp_fd->qf_req_pkt= pkt;
208 qp_fd->qf_get_userdata(qp_fd->qf_srfd, count, 0,
209 FALSE /* !for_ioctl*/);
210 return OK;
213 static int qp_ioctl(fd, req)
214 int fd;
215 ioreq_t req;
217 qp_fd_t *qp_fd;
219 qp_fd= get_qp_fd(fd);
220 qp_fd->qf_get_userdata(qp_fd->qf_srfd, ENOTTY, 0,
221 TRUE /* for_ioctl*/);
222 return OK;
225 static int qp_cancel(fd, which_operation)
226 int fd;
227 int which_operation;
229 ip_panic(( "qp_cancel: should not be here, no blocking calls" ));
230 return OK;
233 static int qp_select(fd, operations)
234 int fd;
235 unsigned operations;
237 unsigned resops;
239 resops= 0;
240 if (operations & SR_SELECT_READ)
241 resops |= SR_SELECT_READ;
242 if (operations & SR_SELECT_WRITE)
243 resops |= SR_SELECT_WRITE;
244 return resops;
247 static qp_fd_t *get_qp_fd(fd)
248 int fd;
250 qp_fd_t *qp_fd;
252 assert(fd >= 0 && fd < QP_FD_NR);
253 qp_fd= &qp_fd_table[fd];
254 assert(qp_fd->qf_flags & QFF_INUSE);
255 return qp_fd;
258 static int do_query(qp_fd, pkt, count)
259 qp_fd_t *qp_fd;
260 acc_t *pkt;
261 int count;
263 struct queryvars qv;
264 void *addr;
265 size_t n, size;
266 int byte;
267 int more;
268 static char hex[]= "0123456789ABCDEF";
270 qvars= &qv;
271 qv.param= pkt; pkt= NULL;
272 qv.qp_fd= qp_fd;
273 qv.fd_offset= 0;
274 qv.outbuf_off= 0;
275 qv.rd_bytes_left= count;
276 qv.r= 0;
278 do {
279 more= queryparam(qp_getc, &addr, &size);
280 for (n= 0; n < size; n++) {
281 byte= ((u8_t *) addr)[n];
282 qp_putc(&qv, hex[byte >> 4]);
283 qp_putc(&qv, hex[byte & 0x0F]);
285 qp_putc(&qv, more ? ',' : 0);
286 if (qv.r)
287 break;
288 } while (more);
289 if (qv.param)
291 assert(0);
292 bf_afree(qv.param);
293 qv.param= NULL;
295 if (qv.r)
296 return qv.r;
297 return qv.fd_offset;
300 static int qp_getc()
302 /* Return one character of the names to search for. */
303 acc_t *pkt;
304 struct queryvars *qv= qvars;
305 u8_t c;
307 pkt= qv->param;
308 qv->param= NULL;
309 if (pkt == NULL)
310 return 0;
312 assert(bf_bufsize(pkt) > 0);
313 c= ptr2acc_data(pkt)[0];
314 if (bf_bufsize(pkt) > 1)
315 qv->param= bf_delhead(pkt, 1);
316 else
318 bf_afree(pkt);
319 qv->param= NULL;
322 return c;
325 static void qp_putc(qv, c)
326 struct queryvars *qv;
327 int c;
329 /* Send one character back to the user. */
330 acc_t *pkt;
331 qp_fd_t *qp_fd;
332 size_t bytes_left;
333 off_t off;
335 bytes_left= qv->rd_bytes_left;
336 if (qv->r || bytes_left == 0)
337 return;
339 off= qv->outbuf_off;
340 assert(off < sizeof(qv->outbuf));
341 qv->outbuf[off]= c;
342 off++;
343 bytes_left--;
344 qv->rd_bytes_left= bytes_left;
345 if (c != '\0' && off < sizeof(qv->outbuf) && bytes_left != 0)
347 qv->outbuf_off= off;
348 return;
351 pkt= bf_memreq(off);
352 assert(pkt->acc_next == NULL);
353 memcpy(ptr2acc_data(pkt), qv->outbuf, off);
354 qp_fd= qv->qp_fd;
355 qv->r= qp_fd->qf_put_userdata(qp_fd->qf_srfd, qv->fd_offset,
356 pkt, FALSE /* !for_ioctl*/ );
357 qv->fd_offset += off;
358 qv->outbuf_off= 0;
361 static void qp_buffree (priority)
362 int priority;
364 /* For the moment, we are not going to free anything */
367 #ifdef BUF_CONSISTENCY_CHECK
368 static void qp_bufcheck()
370 int i;
371 qp_fd_t *qp_fd;
373 for (i= 0, qp_fd= qp_fd_table; i<QP_FD_NR; i++, qp_fd++)
375 if (!(qp_fd->qf_flags & QFF_INUSE))
376 continue;
377 if (qp_fd->qf_req_pkt)
378 bf_check_acc(qp_fd->qf_req_pkt);
381 #endif
384 * $PchId: qp.c,v 1.7 2005/06/28 14:25:25 philip Exp $