]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_stream.c
support for function 0x7a
[netatalk.git] / libatalk / dsi / dsi_stream.c
1 /*
2  * $Id: dsi_stream.c,v 1.11 2003-03-12 15:07:06 didg Exp $
3  *
4  * Copyright (c) 1998 Adrian Sun (asun@zoology.washington.edu)
5  * All rights reserved. See COPYRIGHT.
6  *
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.
12  */
13
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif /* HAVE_CONFIG_H */
17
18 #define USE_WRITEV
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif /* HAVE_UNISTD_H */
25 #include <string.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #ifdef USE_WRITEV
29 #include <sys/uio.h>
30 #endif /* USE_WRITEV */
31 #include <atalk/logger.h>
32
33 #include <atalk/dsi.h>
34 #include <netatalk/endian.h>
35
36 #define min(a,b)  ((a) < (b) ? (a) : (b))
37
38
39 /* write raw data. return actual bytes read. checks against EINTR
40  * aren't necessary if all of the signals have SA_RESTART
41  * specified. */
42 size_t dsi_stream_write(DSI *dsi, void *data, const size_t length)
43 {
44   size_t written;
45   ssize_t len;
46
47   written = 0;
48   while (written < length) {
49     if ((-1 == (len = write(dsi->socket, (u_int8_t *) data + written,
50                       length - written)) && errno == EINTR) ||
51         !len)
52       continue;
53
54     if (len < 0) {
55       LOG(log_error, logtype_default, "dsi_stream_write: %s", strerror(errno));
56       break;
57     }
58     
59     written += len;
60   }
61
62   dsi->write_count += written;
63   return written;
64 }
65
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)
69 {
70   size_t stored;
71   ssize_t len;
72   
73   stored = 0;
74   while (stored < length) {
75     len = read(dsi->socket, (u_int8_t *) data + stored, length - stored);
76     if (len == -1 && errno == EINTR)
77       continue;
78     else if (len > 0)
79       stored += len;
80     else { /* eof or error */
81       LOG(log_error, logtype_default, "dsi_stream_read(%d): %s", len, (len < 0)?strerror(errno):"unexpected EOF");
82       break;
83     }
84   }
85
86   dsi->read_count += stored;
87   return stored;
88 }
89
90 void dsi_sleep(DSI *dsi, const int state)
91 {
92     dsi->asleep = state;
93 }
94
95 /* write data. 0 on failure. this assumes that dsi_len will never
96  * cause an overflow in the data buffer. */
97 int dsi_stream_send(DSI *dsi, void *buf, size_t length)
98 {
99   char block[DSI_BLOCKSIZ];
100   sigset_t oldset;
101 #ifdef USE_WRITEV
102   struct iovec iov[2];
103   size_t towrite;
104   ssize_t len;
105 #endif /* USE_WRITEV */
106
107   block[0] = dsi->header.dsi_flags;
108   block[1] = dsi->header.dsi_command;
109   memcpy(block + 2, &dsi->header.dsi_requestID, 
110          sizeof(dsi->header.dsi_requestID));
111   memcpy(block + 4, &dsi->header.dsi_code, sizeof(dsi->header.dsi_code));
112   memcpy(block + 8, &dsi->header.dsi_len, sizeof(dsi->header.dsi_len));
113   memcpy(block + 12, &dsi->header.dsi_reserved,
114          sizeof(dsi->header.dsi_reserved));
115
116   /* block signals */
117   sigprocmask(SIG_BLOCK, &dsi->sigblockset, &oldset);
118
119   if (!length) { /* just write the header */
120     length = (dsi_stream_write(dsi, block, sizeof(block)) == sizeof(block));
121     sigprocmask(SIG_SETMASK, &oldset, NULL);
122     return length; /* really 0 on failure, 1 on success */
123   }
124   
125 #ifdef USE_WRITEV
126   iov[0].iov_base = block;
127   iov[0].iov_len = sizeof(block);
128   iov[1].iov_base = buf;
129   iov[1].iov_len = length;
130   
131   towrite = sizeof(block) + length;
132   dsi->write_count += towrite;
133   while (towrite > 0) {
134     if (((len = writev(dsi->socket, iov, 2)) == -1 && errno == EINTR) || 
135         !len)
136       continue;
137     
138     if (len == towrite) /* wrote everything out */
139       break;
140     else if (len < 0) { /* error */
141       LOG(log_error, logtype_default, "dsi_stream_send: %s", strerror(errno));
142       sigprocmask(SIG_SETMASK, &oldset, NULL);
143       return 0;
144     }
145     
146     towrite -= len;
147     if (towrite > length) { /* skip part of header */
148       iov[0].iov_base = (char *) iov[0].iov_base + len;
149       iov[0].iov_len -= len;
150     } else { /* skip to data */
151       if (iov[0].iov_len) {
152         len -= iov[0].iov_len;
153         iov[0].iov_len = 0;
154       }
155       iov[1].iov_base = (char *) iov[1].iov_base + len;
156       iov[1].iov_len -= len;
157     }
158   }
159   
160 #else /* USE_WRITEV */
161   /* write the header then data */
162   if ((dsi_stream_write(dsi, block, sizeof(block)) != sizeof(block)) ||
163       (dsi_stream_write(dsi, buf, length) != length)) {
164     sigprocmask(SIG_SETMASK, &oldset, NULL);
165     return 0;
166   }
167 #endif /* USE_WRITEV */
168
169   sigprocmask(SIG_SETMASK, &oldset, NULL);
170   return 1;
171 }
172
173
174 /* read data. function on success. 0 on failure. data length gets
175  * stored in length variable. this should really use size_t's, but
176  * that would require changes elsewhere. */
177 int dsi_stream_receive(DSI *dsi, void *buf, const size_t ilength,
178                        size_t *rlength)
179 {
180   char block[DSI_BLOCKSIZ];
181
182   /* read in the header */
183   if (dsi_stream_read(dsi, block, sizeof(block)) != sizeof(block)) 
184     return 0;
185
186   dsi->header.dsi_flags = block[0];
187   dsi->header.dsi_command = block[1];
188   /* FIXME, not the right place, 
189      but we get a server disconnect without reason in the log
190   */
191   if (!block[1]) {
192       LOG(log_error, logtype_default, "dsi_stream_receive: invalid packet, fatal");
193       return 0;
194   }
195
196   memcpy(&dsi->header.dsi_requestID, block + 2, 
197          sizeof(dsi->header.dsi_requestID));
198   memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
199   memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
200   memcpy(&dsi->header.dsi_reserved, block + 12,
201          sizeof(dsi->header.dsi_reserved));
202   dsi->clientID = ntohs(dsi->header.dsi_requestID);
203   
204   /* make sure we don't over-write our buffers. */
205   *rlength = min(ntohl(dsi->header.dsi_len), ilength);
206   if (dsi_stream_read(dsi, buf, *rlength) != *rlength) 
207     return 0;
208
209   return block[1];
210 }