2 * $Id: dsi_stream.c,v 1.11.6.5.2.2 2005-09-27 10:40:41 didg Exp $
4 * Copyright (c) 1998 Adrian Sun (asun@zoology.washington.edu)
5 * All rights reserved. See COPYRIGHT.
7 * this file provides the following functions:
8 * dsi_stream_write: just write a bunch of bytes.
9 * dsi_stream_read: just read a bunch of bytes.
10 * dsi_stream_send: send a DSI header + data.
11 * dsi_stream_receive: read a DSI header + data.
16 #endif /* HAVE_CONFIG_H */
29 #include <sys/types.h>
30 #include <sys/socket.h>
36 #include <atalk/logger.h>
38 #include <atalk/dsi.h>
39 #include <netatalk/endian.h>
41 #define min(a,b) ((a) < (b) ? (a) : (b))
44 #define MSG_MORE 0x8000
48 #define MSG_DONTWAIT 0x40
51 /* -------------------------
52 * we don't use a circular buffer.
54 void dsi_buffer(DSI *dsi)
56 fd_set readfds, writefds;
62 FD_SET( dsi->socket, &readfds);
63 FD_SET( dsi->socket, &writefds);
64 maxfd = dsi->socket +1;
66 FD_SET( dsi->socket, &readfds);
67 FD_SET( dsi->socket, &writefds);
68 if (select( maxfd, &readfds, &writefds, NULL, NULL) <= 0)
71 if ( !FD_ISSET(dsi->socket, &readfds)) {
72 /* nothing waiting in the read queue */
76 /* XXX config options */
77 dsi->maxsize = 6 * dsi->server_quantum;
79 dsi->maxsize = 6 * DSI_SERVQUANT_DEF;
80 dsi->buffer = malloc(dsi->maxsize);
82 /* fall back to blocking IO */
86 dsi->start = dsi->buffer;
87 dsi->eof = dsi->buffer;
88 dsi->end = dsi->buffer + dsi->maxsize;
90 len = dsi->end - dsi->eof;
93 /* ouch, our buffer is full !
94 * fall back to blocking IO
95 * could block and disconnect but it's better than a cpu hog
101 len = read(dsi->socket, dsi->eof, len);
105 if ( FD_ISSET(dsi->socket, &writefds)) {
111 /* ------------------------------
112 * write raw data. return actual bytes read. checks against EINTR
113 * aren't necessary if all of the signals have SA_RESTART
115 size_t dsi_stream_write(DSI *dsi, void *data, const size_t length, int mode _U_)
120 /* FIXME sometime it's slower */
121 unsigned int flags = (mode)?MSG_MORE:0;
123 unsigned int flags = 0;
126 /* XXX there's no MSG_DONTWAIT in recv ?? so we have to play with ioctl
128 if (dsi->noblocking) {
129 flags |= MSG_DONTWAIT;
134 while (written < length) {
135 if ((-1 == (len = send(dsi->socket, (u_int8_t *) data + written,
136 length - written, flags)) && errno == EINTR) ||
141 if (dsi->noblocking && errno == EAGAIN) {
142 /* non blocking mode but will block
143 * read data in input queue.
149 LOG(log_error, logtype_default, "dsi_stream_write: %s", strerror(errno));
158 dsi->write_count += written;
162 /* ---------------------------------
164 static ssize_t buf_read(DSI *dsi, u_int8_t *buf, size_t count)
173 nbe = dsi->eof - dsi->start;
176 nbe = min((size_t)nbe, count);
177 memcpy(buf, dsi->start, nbe);
180 if (dsi->eof == dsi->start)
181 dsi->start = dsi->eof = dsi->buffer;
192 ret = read(dsi->socket, buf, count);
199 /* ---------------------------------------
200 * read raw data. return actual bytes read. this will wait until
201 * it gets length bytes
203 size_t dsi_stream_read(DSI *dsi, void *data, const size_t length)
209 while (stored < length) {
210 len = buf_read(dsi, (u_int8_t *) data + stored, length - stored);
211 if (len == -1 && errno == EINTR)
215 else { /* eof or error */
216 /* don't log EOF error if it's just after connect (OSX 10.3 probe) */
217 if (len || stored || dsi->read_count) {
218 LOG(log_error, logtype_default, "dsi_stream_read(%d): %s", len, (len < 0)?strerror(errno):"unexpected EOF");
224 dsi->read_count += stored;
228 /* ---------------------------------------
230 void dsi_sleep(DSI *dsi, const int state)
235 /* ---------------------------------------
237 static void block_sig(DSI *dsi)
239 if (!dsi->sigblocked) sigprocmask(SIG_BLOCK, &dsi->sigblockset, &dsi->oldset);
242 /* ---------------------------------------
244 static void unblock_sig(DSI *dsi)
246 if (!dsi->sigblocked) sigprocmask(SIG_SETMASK, &dsi->oldset, NULL);
249 /* ---------------------------------------
250 * write data. 0 on failure. this assumes that dsi_len will never
251 * cause an overflow in the data buffer.
253 int dsi_stream_send(DSI *dsi, void *buf, size_t length)
255 char block[DSI_BLOCKSIZ];
260 #endif /* USE_WRITEV */
262 block[0] = dsi->header.dsi_flags;
263 block[1] = dsi->header.dsi_command;
264 memcpy(block + 2, &dsi->header.dsi_requestID,
265 sizeof(dsi->header.dsi_requestID));
266 memcpy(block + 4, &dsi->header.dsi_code, sizeof(dsi->header.dsi_code));
267 memcpy(block + 8, &dsi->header.dsi_len, sizeof(dsi->header.dsi_len));
268 memcpy(block + 12, &dsi->header.dsi_reserved,
269 sizeof(dsi->header.dsi_reserved));
271 if (!length) { /* just write the header */
272 length = (dsi_stream_write(dsi, block, sizeof(block), 0) == sizeof(block));
273 return length; /* really 0 on failure, 1 on success */
279 iov[0].iov_base = block;
280 iov[0].iov_len = sizeof(block);
281 iov[1].iov_base = buf;
282 iov[1].iov_len = length;
284 towrite = sizeof(block) + length;
285 dsi->write_count += towrite;
286 while (towrite > 0) {
287 if (((len = writev(dsi->socket, iov, 2)) == -1 && errno == EINTR) ||
291 if (len == towrite) /* wrote everything out */
293 else if (len < 0) { /* error */
294 LOG(log_error, logtype_default, "dsi_stream_send: %s", strerror(errno));
300 if (towrite > length) { /* skip part of header */
301 iov[0].iov_base = (char *) iov[0].iov_base + len;
302 iov[0].iov_len -= len;
303 } else { /* skip to data */
304 if (iov[0].iov_len) {
305 len -= iov[0].iov_len;
308 iov[1].iov_base = (char *) iov[1].iov_base + len;
309 iov[1].iov_len -= len;
313 #else /* USE_WRITEV */
314 /* write the header then data */
315 if ((dsi_stream_write(dsi, block, sizeof(block), 1) != sizeof(block)) ||
316 (dsi_stream_write(dsi, buf, length, 0) != length)) {
320 #endif /* USE_WRITEV */
327 /* ---------------------------------------
328 * read data. function on success. 0 on failure. data length gets
329 * stored in length variable. this should really use size_t's, but
330 * that would require changes elsewhere. */
331 int dsi_stream_receive(DSI *dsi, void *buf, const size_t ilength,
334 char block[DSI_BLOCKSIZ];
336 /* read in the header */
337 if (dsi_stream_read(dsi, block, sizeof(block)) != sizeof(block))
340 dsi->header.dsi_flags = block[0];
341 dsi->header.dsi_command = block[1];
342 /* FIXME, not the right place,
343 but we get a server disconnect without reason in the log
346 LOG(log_error, logtype_default, "dsi_stream_receive: invalid packet, fatal");
350 memcpy(&dsi->header.dsi_requestID, block + 2,
351 sizeof(dsi->header.dsi_requestID));
352 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
353 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
354 memcpy(&dsi->header.dsi_reserved, block + 12,
355 sizeof(dsi->header.dsi_reserved));
356 dsi->clientID = ntohs(dsi->header.dsi_requestID);
358 /* make sure we don't over-write our buffers. */
359 *rlength = min(ntohl(dsi->header.dsi_len), ilength);
360 if (dsi_stream_read(dsi, buf, *rlength) != *rlength)