4 * @author Sebastian Hack
6 * Protocol stuff for lpp server
8 * Copyright (C) 2005 Universitaet Karlsruhe
9 * Released under the GPL
17 #define WIN32_LEAN_AND_MEAN
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <arpa/inet.h>
34 /* undef to disable debugging */
35 #undef ENABLE_DEBUGGING
47 static inline firm_dbg_module_t *get_dbg_module(void)
49 static firm_dbg_module_t *dbg = NULL;
51 dbg = firm_dbg_register("lpp.comm");
57 #define dbg get_dbg_module()
60 * Try to read some bytes but block until a certain amount is read.
61 * @param fd The file descriptor.
62 * @param buf The buffer to read into.
63 * @param try_amount The amount of bytes to try to read.
64 * @param at_least block until this many bytes are read.
65 * @return The number of bytes read or -1 on error.
67 static ssize_t secure_recv(int fd, void *buf, size_t try_amount, size_t at_least)
70 size_t bytes_read = 0;
74 res = recv(fd, &data[bytes_read], try_amount - bytes_read, 0);
76 if(res == 0 || errno != EAGAIN)
83 } while(bytes_read < at_least);
88 static ssize_t secure_send(int fd, const void *buf, size_t n)
91 size_t bytes_written = 0;
92 const char *data = buf;
95 res = send(fd, &data[bytes_written], n - bytes_written, 0);
102 bytes_written += res;
104 } while(bytes_written < n);
109 ssize_t lpp_flush(lpp_comm_t *comm)
112 if(comm->w_pos - comm->w_buf > 0) {
113 DBG((dbg, LEVEL_1, "flushing %d bytes\n", comm->w_pos - comm->w_buf));
114 res = secure_send(comm->fd, comm->w_buf, comm->w_pos - comm->w_buf);
118 comm->w_pos = comm->w_buf;
123 static ssize_t lpp_write(lpp_comm_t *comm, const void *buf, size_t len)
125 assert(comm->w_pos - comm->w_buf >= 0);
127 DBG((dbg, LEVEL_1, "write of length %d\n", len));
129 size_t free = (comm->w_buf + comm->buf_size) - comm->w_pos;
130 size_t copy = MIN(free, len);
131 size_t rest = len - copy;
132 const char *pos = buf;
134 DBG((dbg, LEVEL_1, "\tfree = %d, copy = %d, rest = %d\n", free, copy, rest));
136 memcpy(comm->w_pos, pos, copy);
142 * Not everything in buf fits into the buffer,
143 * so flush the buffer and write the rest.
147 size_t n_direct = rest / comm->buf_size;
150 if(lpp_flush(comm) < 0)
153 for(i = 0; i < n_direct; ++i) {
154 if(secure_send(comm->fd, pos, comm->buf_size) < 0)
157 pos += comm->buf_size;
160 last_rest = ((const char *) buf + len) - pos;
163 assert(last_rest < comm->buf_size);
164 assert(comm->w_pos == comm->w_buf);
165 memcpy(comm->w_pos, pos, last_rest);
166 comm->w_pos += last_rest;
174 static ssize_t lpp_read(lpp_comm_t *comm, void *buf, size_t len)
176 DBG((dbg, LEVEL_1, "read of length %d\n", len));
178 size_t left = comm->r_max - comm->r_pos;
179 size_t copy = MIN(left, len);
180 size_t rest = len - copy;
183 DBG((dbg, LEVEL_1, "\tleft = %d, copy = %d, rest = %d\n", left, copy, rest));
185 memcpy(pos, comm->r_pos, copy);
190 /* We want to read more than the buffer can provide. */
192 size_t bs = comm->buf_size;
193 size_t n_direct = rest / comm->buf_size;
198 * The buffer is now completely read, so
199 * reset the pointers.
201 comm->r_pos = comm->r_buf;
202 comm->r_max = comm->r_buf;
204 for(i = 0; i < n_direct; ++i) {
205 if(secure_recv(comm->fd, pos, bs, bs) < 0)
208 pos += comm->buf_size;
211 last_rest = ((const char *) buf + len) - pos;
214 ssize_t bytes_read = 0;
216 assert(last_rest < comm->buf_size);
217 assert(comm->r_pos == comm->r_buf);
219 bytes_read = secure_recv(comm->fd, comm->r_buf, bs, last_rest);
223 memcpy(pos, comm->r_buf, last_rest);
224 comm->r_pos = comm->r_buf + last_rest;
225 comm->r_max = comm->r_buf + bytes_read;
233 lpp_comm_t *lpp_comm_new(int fd, size_t buf_size)
235 lpp_comm_t *res = malloc(sizeof(res[0]));
238 res->w_buf = malloc(buf_size);
239 res->w_pos = res->w_buf;
240 res->r_buf = malloc(buf_size);
241 res->r_pos = res->r_buf;
242 res->r_max = res->r_buf;
243 res->buf_size = buf_size;
248 int lpp_comm_fileno(const lpp_comm_t *comm)
253 void lpp_comm_free(lpp_comm_t *comm)
260 void lpp_print_err(const char *fmt, ...)
265 vfprintf(stderr, fmt, args);
269 void lpp_writel(lpp_comm_t *comm, uint32_t x)
272 ERRNO_CHECK(lpp_write(comm, &x, sizeof(x)), !=, (ssize_t)sizeof(x));
275 void lpp_writed(lpp_comm_t *comm, double dbl)
277 ERRNO_CHECK(lpp_write(comm, &dbl, sizeof(dbl)), !=, (ssize_t)sizeof(dbl));
280 void lpp_writes(lpp_comm_t *comm, const char *str)
282 size_t n = strlen(str);
284 ERRNO_CHECK(lpp_write(comm, str, n), !=, (ssize_t) n);
287 uint32_t lpp_readl(lpp_comm_t *comm)
291 ERRNO_CHECK(lpp_read(comm, &res, sizeof(res)), !=, (ssize_t)sizeof(res));
295 int lpp_read_cmd(lpp_comm_t *comm)
301 retval = recv(comm->fd, (char *)&res, sizeof(res), 0);
311 return (int) ntohl(res);
314 double lpp_readd(lpp_comm_t *comm)
317 ERRNO_CHECK(lpp_read(comm, &res, sizeof(res)), !=, (ssize_t)sizeof(res));
321 char *lpp_reads(lpp_comm_t *comm)
323 size_t len = lpp_readl(comm);
324 char *res = malloc(sizeof(char) * (len + 1));
326 ERRNO_CHECK(lpp_read(comm, res, len), !=, (ssize_t) len);
331 char *lpp_readbuf(lpp_comm_t *comm, char *buf, size_t buflen)
335 size_t n = buflen - 1;
336 size_t len = lpp_readl(comm);
337 size_t max_read = n < len ? n : len;
338 size_t rest = len - max_read;
340 if(buflen > 0 && buf != NULL) {
341 ERRNO_CHECK(lpp_read(comm, buf, max_read), !=, (ssize_t) max_read);
342 buf[max_read] = '\0';
347 /* eat up data that didnt fit into the string */
348 for(i = 0, n = rest / sizeof(dummy); i < n; ++i)
349 ERRNO_CHECK(lpp_read(comm, dummy, sizeof(dummy)), !=, (ssize_t)sizeof(dummy));
351 if(rest % sizeof(dummy) > 0)
352 ERRNO_CHECK(lpp_read(comm, dummy, rest % sizeof(dummy)), !=,
353 (ssize_t) (rest % sizeof(dummy)) );
358 int lpp_ack(lpp_comm_t *comm, char *buf, size_t buflen)
361 int cmd = lpp_readl(comm);
368 lpp_readbuf(comm, buf, buflen);
376 void lpp_send_res(lpp_comm_t *comm, int ok, const char *fmt, ...)
383 vsnprintf(buf, sizeof(buf), fmt, args);
386 lpp_writel(comm, LPP_CMD_BAD);
387 lpp_writes(comm, buf);
389 lpp_writel(comm, LPP_CMD_OK);
393 void lpp_send_ack(lpp_comm_t *comm)
395 lpp_send_res(comm, 1, "");
398 const char *lpp_get_cmd_name(int cmd)
401 case LPP_CMD_BAD: return "BAD";
402 case LPP_CMD_OK: return "OK";
403 case LPP_CMD_PROBLEM: return "PROBLEM";
404 case LPP_CMD_SOLUTION: return "SOLUTION";
405 case LPP_CMD_SOLVER: return "SOLVER";
406 case LPP_CMD_BYE: return "BYE";
407 case LPP_CMD_SOLVERS: return "SOLVERS";
408 case LPP_CMD_SET_DEBUG: return "SET_DEBUG";
409 case LPP_CMD_INFO: return "INFO";