2 * $Id: dsi_stream.c,v 1.4.2.2 2002-03-12 16:16:55 srittau 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 */
24 #endif /* HAVE_UNISTD_H */
27 #include <sys/types.h>
30 #endif /* USE_WRITEV */
33 #include <atalk/dsi.h>
34 #include <netatalk/endian.h>
36 #define min(a,b) ((a) < (b) ? (a) : (b))
39 /* write raw data. return actual bytes read. checks against EINTR
40 * aren't necessary if all of the signals have SA_RESTART
42 size_t dsi_stream_write(DSI *dsi, void *data, const size_t length)
48 while (written < length) {
49 if (((len = write(dsi->socket, (u_int8_t *) data + written,
50 length - written)) == -1 && errno == EINTR) ||
55 syslog(LOG_ERR, "dsi_stream_write: %s", strerror(errno));
62 dsi->write_count += written;
66 /* read raw data. return actual bytes read. this will wait until
67 * it gets length bytes */
68 size_t dsi_stream_read(DSI *dsi, void *data, const size_t length)
74 while (stored < length) {
75 len = read(dsi->socket, (u_int8_t *) data + stored, length - stored);
76 if (len == -1 && errno == EINTR)
81 else { /* eof or error */
82 syslog(LOG_ERR, "dsi_stream_read(%d): %s", len, strerror(errno));
87 dsi->read_count += stored;
92 /* write data. 0 on failure. this assumes that dsi_len will never
93 * cause an overflow in the data buffer. */
94 int dsi_stream_send(DSI *dsi, void *buf, size_t length)
96 char block[DSI_BLOCKSIZ];
102 #endif /* USE_WRITEV */
104 block[0] = dsi->header.dsi_flags;
105 block[1] = dsi->header.dsi_command;
106 memcpy(block + 2, &dsi->header.dsi_requestID,
107 sizeof(dsi->header.dsi_requestID));
108 memcpy(block + 4, &dsi->header.dsi_code, sizeof(dsi->header.dsi_code));
109 memcpy(block + 8, &dsi->header.dsi_len, sizeof(dsi->header.dsi_len));
110 memcpy(block + 12, &dsi->header.dsi_reserved,
111 sizeof(dsi->header.dsi_reserved));
114 sigprocmask(SIG_BLOCK, &dsi->sigblockset, &oldset);
116 if (!length) { /* just write the header */
117 length = (dsi_stream_write(dsi, block, sizeof(block)) == sizeof(block));
118 sigprocmask(SIG_SETMASK, &oldset, NULL);
119 return length; /* really 0 on failure, 1 on success */
123 iov[0].iov_base = block;
124 iov[0].iov_len = sizeof(block);
125 iov[1].iov_base = buf;
126 iov[1].iov_len = length;
128 towrite = sizeof(block) + length;
129 dsi->write_count += towrite;
130 while (towrite > 0) {
131 if (((len = writev(dsi->socket, iov, 2)) == -1 && errno == EINTR) ||
135 if (len == towrite) /* wrote everything out */
137 else if (len < 0) { /* error */
138 syslog(LOG_ERR, "dsi_stream_send: %s", strerror(errno));
139 sigprocmask(SIG_SETMASK, &oldset, NULL);
144 if (towrite > length) { /* skip part of header */
145 iov[0].iov_base = (char *) iov[0].iov_base + len;
146 iov[0].iov_len -= len;
147 } else { /* skip to data */
148 if (iov[0].iov_len) {
149 len -= iov[0].iov_len;
152 iov[1].iov_base = (char *) iov[1].iov_base + len;
153 iov[1].iov_len -= len;
157 #else /* USE_WRITEV */
158 /* write the header then data */
159 if ((dsi_stream_write(dsi, block, sizeof(block)) != sizeof(block)) ||
160 (dsi_stream_write(dsi, buf, length) != length)) {
161 sigprocmask(SIG_SETMASK, &oldset, NULL);
164 #endif /* USE_WRITEV */
166 sigprocmask(SIG_SETMASK, &oldset, NULL);
171 /* read data. function on success. 0 on failure. data length gets
172 * stored in length variable. this should really use size_t's, but
173 * that would require changes elsewhere. */
174 int dsi_stream_receive(DSI *dsi, void *buf, const int ilength,
177 char block[DSI_BLOCKSIZ];
179 /* read in the header */
180 if (dsi_stream_read(dsi, block, sizeof(block)) != sizeof(block))
183 dsi->header.dsi_flags = block[0];
184 dsi->header.dsi_command = block[1];
185 /* FIXME, not the right place,
186 but we get a server disconnect without reason in the log
189 LOG(log_error, logtype_default, "dsi_stream_receive: invalid packet, fatal");
193 memcpy(&dsi->header.dsi_requestID, block + 2,
194 sizeof(dsi->header.dsi_requestID));
195 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
196 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
197 memcpy(&dsi->header.dsi_reserved, block + 12,
198 sizeof(dsi->header.dsi_reserved));
199 dsi->clientID = ntohs(dsi->header.dsi_requestID);
201 /* make sure we don't over-write our buffers. */
202 *rlength = min(ntohl(dsi->header.dsi_len), ilength);
203 if (dsi_stream_read(dsi, buf, *rlength) != *rlength)