4 * @author Sebastian Hack
6 * Protocol stuff for lpp server
8 * Copyright (C) 2005 Universitaet Karlsruhe
9 * Released under the GPL
18 #define WIN32_LEAN_AND_MEAN
22 #define vsnprintf _vsnprintf
26 #include <sys/types.h>
27 #include <sys/socket.h>
36 /* undef to disable buffering socket i/o */
37 #define ENABLE_BUFFERING
39 /* undef to disable debugging */
40 #undef ENABLE_DEBUGGING
52 static firm_dbg_module_t *dbg = NULL;
55 * Try to read some bytes but block until a certain amount is read.
56 * @param fd The file descriptor.
57 * @param buf The buffer to read into.
58 * @param try The amount of bytes to try to read.
59 * @param at_least block until this many bytes are read.
60 * @return The number of bytes read or -1 on error.
62 static ssize_t secure_recv(int fd, void *buf, size_t try, size_t at_least)
65 size_t bytes_read = 0;
69 res = recv(fd, &data[bytes_read], try - bytes_read, 0);
71 if(res == 0 || errno != EAGAIN)
78 } while(bytes_read < at_least);
83 static ssize_t secure_send(int fd, const void *buf, size_t n)
86 size_t bytes_written = 0;
87 const char *data = buf;
90 res = send(fd, &data[bytes_written], n - bytes_written, 0);
99 } while(bytes_written < n);
104 #ifdef ENABLE_BUFFERING
105 ssize_t lpp_flush(lpp_comm_t *comm)
108 if(comm->w_pos - comm->w_buf > 0) {
109 DBG((dbg, LEVEL_1, "flushing %d bytes\n", comm->w_pos - comm->w_buf));
110 res = secure_send(comm->fd, comm->w_buf, comm->w_pos - comm->w_buf);
114 comm->w_pos = comm->w_buf;
119 static ssize_t lpp_write(lpp_comm_t *comm, const void *buf, size_t len)
121 assert(comm->w_pos - comm->w_buf >= 0);
123 DBG((dbg, LEVEL_1, "write of length %d\n", len));
125 size_t free = (comm->w_buf + comm->buf_size) - comm->w_pos;
126 size_t copy = MIN(free, len);
127 size_t rest = len - copy;
128 const char *pos = buf;
130 DBG((dbg, LEVEL_1, "\tfree = %d, copy = %d, rest = %d\n", free, copy, rest));
132 memcpy(comm->w_pos, pos, copy);
138 * Not everything in buf fits into the buffer,
139 * so flush the buffer and write the rest.
143 size_t n_direct = rest / comm->buf_size;
146 if(lpp_flush(comm) < 0)
149 for(i = 0; i < n_direct; ++i) {
150 if(secure_send(comm->fd, pos, comm->buf_size) < 0)
153 pos += comm->buf_size;
156 last_rest = ((const char *) buf + len) - pos;
159 assert(last_rest < comm->buf_size);
160 assert(comm->w_pos == comm->w_buf);
161 memcpy(comm->w_pos, pos, last_rest);
162 comm->w_pos += last_rest;
170 static ssize_t lpp_read(lpp_comm_t *comm, void *buf, size_t len)
172 DBG((dbg, LEVEL_1, "read of length %d\n", len));
174 size_t left = comm->r_max - comm->r_pos;
175 size_t copy = MIN(left, len);
176 size_t rest = len - copy;
179 DBG((dbg, LEVEL_1, "\tleft = %d, copy = %d, rest = %d\n", left, copy, rest));
181 memcpy(pos, comm->r_pos, copy);
186 /* We want to read more than the buffer can provide. */
188 size_t bs = comm->buf_size;
189 size_t n_direct = rest / comm->buf_size;
194 * The buffer is now completely read, so
195 * reset the pointers.
197 comm->r_pos = comm->r_buf;
198 comm->r_max = comm->r_buf;
200 for(i = 0; i < n_direct; ++i) {
201 if(secure_recv(comm->fd, pos, bs, bs) < 0)
204 pos += comm->buf_size;
207 last_rest = ((const char *) buf + len) - pos;
210 ssize_t bytes_read = 0;
212 assert(last_rest < comm->buf_size);
213 assert(comm->r_pos == comm->r_buf);
215 bytes_read = secure_recv(comm->fd, comm->r_buf, bs, last_rest);
219 memcpy(pos, comm->r_buf, last_rest);
220 comm->r_pos = comm->r_buf + last_rest;
221 comm->r_max = comm->r_buf + bytes_read;
229 #else /* ENABLE_BUFFERING */
230 ssize_t lpp_flush(lpp_comm_t *comm)
235 static ssize_t lpp_write(lpp_comm_t *comm, const void *buf, size_t len)
237 return secure_send(comm->fd, buf, len);
240 static ssize_t lpp_read(lpp_comm_t *comm, void *buf, size_t len)
242 return secure_recv(comm->fd, buf, len, len);
246 lpp_comm_t *lpp_comm_new(int fd, size_t buf_size)
248 lpp_comm_t *res = malloc(sizeof(res[0]));
251 res->w_buf = malloc(buf_size);
252 res->w_pos = res->w_buf;
253 res->r_buf = malloc(buf_size);
254 res->r_pos = res->r_buf;
255 res->r_max = res->r_buf;
256 res->buf_size = buf_size;
261 int lpp_comm_fileno(const lpp_comm_t *comm)
266 void lpp_comm_free(lpp_comm_t *comm)
273 void lpp_print_err(const char *fmt, ...)
278 vfprintf(stderr, fmt, args);
282 void lpp_writel(lpp_comm_t *comm, uint32_t x)
285 ERRNO_CHECK(lpp_write(comm, &x, sizeof(x)), !=, sizeof(x));
288 void lpp_writed(lpp_comm_t *comm, double dbl)
290 ERRNO_CHECK(lpp_write(comm, &dbl, sizeof(dbl)), !=, sizeof(dbl));
293 void lpp_writes(lpp_comm_t *comm, const char *str)
295 size_t n = strlen(str);
297 ERRNO_CHECK(lpp_write(comm, str, n), !=, (ssize_t) n);
300 uint32_t lpp_readl(lpp_comm_t *comm)
304 ERRNO_CHECK(lpp_read(comm, &res, sizeof(res)), !=, sizeof(res));
308 int lpp_read_cmd(lpp_comm_t *comm)
314 retval = recv(comm->fd, (char *)&res, sizeof(res), 0);
324 return (int) ntohl(res);
327 double lpp_readd(lpp_comm_t *comm)
330 ERRNO_CHECK(lpp_read(comm, &res, sizeof(res)), !=, sizeof(res));
334 char *lpp_reads(lpp_comm_t *comm)
336 size_t len = lpp_readl(comm);
337 char *res = malloc(sizeof(char) * (len + 1));
339 ERRNO_CHECK(lpp_read(comm, res, len), !=, (ssize_t) len);
344 char *lpp_readbuf(lpp_comm_t *comm, char *buf, size_t buflen)
348 size_t n = buflen - 1;
349 size_t len = lpp_readl(comm);
350 size_t max_read = n < len ? n : len;
351 size_t rest = len - max_read;
353 if(buflen > 0 && buf != NULL) {
354 ERRNO_CHECK(lpp_read(comm, buf, max_read), !=, (ssize_t) max_read);
355 buf[max_read] = '\0';
360 /* eat up data that didnt fit into the string */
361 for(i = 0, n = rest / sizeof(dummy); i < n; ++i)
362 ERRNO_CHECK(lpp_read(comm, dummy, sizeof(dummy)), !=, sizeof(dummy));
364 if(rest % sizeof(dummy) > 0)
365 ERRNO_CHECK(lpp_read(comm, dummy, rest % sizeof(dummy)), !=,
366 (ssize_t) (rest % sizeof(dummy)) );
371 int lpp_ack(lpp_comm_t *comm, char *buf, size_t buflen)
374 int cmd = lpp_readl(comm);
381 lpp_readbuf(comm, buf, buflen);
389 void lpp_send_res(lpp_comm_t *comm, int ok, const char *fmt, ...)
396 vsnprintf(buf, sizeof(buf), fmt, args);
399 lpp_writel(comm, LPP_CMD_BAD);
400 lpp_writes(comm, buf);
404 lpp_writel(comm, LPP_CMD_OK);
407 void lpp_send_ack(lpp_comm_t *comm)
409 lpp_send_res(comm, 1, "");
412 const char *lpp_get_cmd_name(int cmd)
415 #define LPP_CMD(x) case LPP_CMD_ ## x: return #x;
416 #include "lpp_cmd.def"