2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Protocol stuff for lpp server
9 * @author Sebastian Hack
19 #define WIN32_LEAN_AND_MEAN
23 typedef SSIZE_T ssize_t;
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
48 static inline firm_dbg_module_t *get_dbg_module(void)
50 static firm_dbg_module_t *dbg = NULL;
52 dbg = firm_dbg_register("lpp.comm");
57 #define dbg get_dbg_module()
61 * Try to read some bytes but block until a certain amount is read.
62 * @param fd The file descriptor.
63 * @param buf The buffer to read into.
64 * @param try_amount The amount of bytes to try to read.
65 * @param at_least block until this many bytes are read.
66 * @return The number of bytes read or -1 on error.
68 static ssize_t secure_recv(int fd, void *buf, size_t try_amount, size_t at_least)
71 size_t bytes_read = 0;
72 char *data = (char*)buf;
75 res = recv(fd, &data[bytes_read], try_amount - bytes_read, 0);
77 if(res == 0 || errno != EAGAIN)
84 } while(bytes_read < at_least);
89 static ssize_t secure_send(int fd, const void *buf, size_t n)
92 size_t bytes_written = 0;
93 const char *data = (const char*)buf;
96 res = send(fd, &data[bytes_written], n - bytes_written, 0);
103 bytes_written += res;
105 } while(bytes_written < n);
110 static ssize_t lpp_flush_(lpp_comm_t *comm)
113 if(comm->w_pos - comm->w_buf > 0) {
114 DBG((dbg, LEVEL_1, "flushing %d bytes\n", comm->w_pos - comm->w_buf));
115 res = secure_send(comm->fd, comm->w_buf, comm->w_pos - comm->w_buf);
119 comm->w_pos = comm->w_buf;
124 void lpp_flush(lpp_comm_t *comm)
129 static ssize_t lpp_write(lpp_comm_t *comm, const void *buf, size_t len)
131 assert(comm->w_pos - comm->w_buf >= 0);
133 DBG((dbg, LEVEL_1, "write of length %d\n", len));
135 size_t free = (comm->w_buf + comm->buf_size) - comm->w_pos;
136 size_t copy = MIN(free, len);
137 size_t rest = len - copy;
138 const char *pos = (const char*)buf;
140 DBG((dbg, LEVEL_1, "\tfree = %d, copy = %d, rest = %d\n", free, copy, rest));
142 memcpy(comm->w_pos, pos, copy);
148 * Not everything in buf fits into the buffer,
149 * so flush the buffer and write the rest.
153 size_t n_direct = rest / comm->buf_size;
156 if(lpp_flush_(comm) < 0)
159 for(i = 0; i < n_direct; ++i) {
160 if(secure_send(comm->fd, pos, comm->buf_size) < 0)
163 pos += comm->buf_size;
166 last_rest = ((const char *) buf + len) - pos;
169 assert(last_rest < comm->buf_size);
170 assert(comm->w_pos == comm->w_buf);
171 memcpy(comm->w_pos, pos, last_rest);
172 comm->w_pos += last_rest;
180 static ssize_t lpp_read(lpp_comm_t *comm, void *buf, size_t len)
182 DBG((dbg, LEVEL_1, "read of length %d\n", len));
184 size_t left = comm->r_max - comm->r_pos;
185 size_t copy = MIN(left, len);
186 size_t rest = len - copy;
187 char *pos = (char*)buf;
189 DBG((dbg, LEVEL_1, "\tleft = %d, copy = %d, rest = %d\n", left, copy, rest));
191 memcpy(pos, comm->r_pos, copy);
196 /* We want to read more than the buffer can provide. */
198 size_t bs = comm->buf_size;
199 size_t n_direct = rest / comm->buf_size;
204 * The buffer is now completely read, so
205 * reset the pointers.
207 comm->r_pos = comm->r_buf;
208 comm->r_max = comm->r_buf;
210 for(i = 0; i < n_direct; ++i) {
211 if(secure_recv(comm->fd, pos, bs, bs) < 0)
214 pos += comm->buf_size;
217 last_rest = ((const char *) buf + len) - pos;
220 ssize_t bytes_read = 0;
222 assert(last_rest < comm->buf_size);
223 assert(comm->r_pos == comm->r_buf);
225 bytes_read = secure_recv(comm->fd, comm->r_buf, bs, last_rest);
229 memcpy(pos, comm->r_buf, last_rest);
230 comm->r_pos = comm->r_buf + last_rest;
231 comm->r_max = comm->r_buf + bytes_read;
239 lpp_comm_t *lpp_comm_new(int fd, size_t buf_size)
241 lpp_comm_t *res = XMALLOCZ(lpp_comm_t);
244 res->w_buf = XMALLOCN(char, buf_size);
245 res->w_pos = res->w_buf;
246 res->r_buf = XMALLOCN(char, buf_size);
247 res->r_pos = res->r_buf;
248 res->r_max = res->r_buf;
249 res->buf_size = buf_size;
254 int lpp_comm_fileno(const lpp_comm_t *comm)
259 void lpp_comm_free(lpp_comm_t *comm)
266 void lpp_print_err(const char *fmt, ...)
271 vfprintf(stderr, fmt, args);
275 void lpp_writel(lpp_comm_t *comm, uint32_t x)
278 ERRNO_CHECK(lpp_write(comm, &x, sizeof(x)), !=, (ssize_t)sizeof(x));
281 void lpp_writed(lpp_comm_t *comm, double dbl)
283 ERRNO_CHECK(lpp_write(comm, &dbl, sizeof(dbl)), !=, (ssize_t)sizeof(dbl));
286 void lpp_writes(lpp_comm_t *comm, const char *str)
288 size_t n = strlen(str);
290 ERRNO_CHECK(lpp_write(comm, str, n), !=, (ssize_t) n);
293 uint32_t lpp_readl(lpp_comm_t *comm)
297 ERRNO_CHECK(lpp_read(comm, &res, sizeof(res)), !=, (ssize_t)sizeof(res));
301 int lpp_read_cmd(lpp_comm_t *comm)
307 retval = recv(comm->fd, (char *)&res, sizeof(res), 0);
317 return (int) ntohl(res);
320 double lpp_readd(lpp_comm_t *comm)
323 ERRNO_CHECK(lpp_read(comm, &res, sizeof(res)), !=, (ssize_t)sizeof(res));
327 char *lpp_reads(lpp_comm_t *comm)
329 size_t len = lpp_readl(comm);
330 char *res = XMALLOCN(char, len+1);
332 ERRNO_CHECK(lpp_read(comm, res, len), !=, (ssize_t) len);
337 char *lpp_readbuf(lpp_comm_t *comm, char *buf, size_t buflen)
341 size_t n = buflen - 1;
342 size_t len = lpp_readl(comm);
343 size_t max_read = n < len ? n : len;
344 size_t rest = len - max_read;
346 if(buflen > 0 && buf != NULL) {
347 ERRNO_CHECK(lpp_read(comm, buf, max_read), !=, (ssize_t) max_read);
348 buf[max_read] = '\0';
353 /* eat up data that didnt fit into the string */
354 for(i = 0, n = rest / sizeof(dummy); i < n; ++i)
355 ERRNO_CHECK(lpp_read(comm, dummy, sizeof(dummy)), !=, (ssize_t)sizeof(dummy));
357 if(rest % sizeof(dummy) > 0)
358 ERRNO_CHECK(lpp_read(comm, dummy, rest % sizeof(dummy)), !=,
359 (ssize_t) (rest % sizeof(dummy)) );
364 int lpp_ack(lpp_comm_t *comm, char *buf, size_t buflen)
367 int cmd = lpp_readl(comm);
374 lpp_readbuf(comm, buf, buflen);
382 void lpp_send_res(lpp_comm_t *comm, int ok, const char *fmt, ...)
389 vsnprintf(buf, sizeof(buf), fmt, args);
392 lpp_writel(comm, LPP_CMD_BAD);
393 lpp_writes(comm, buf);
395 lpp_writel(comm, LPP_CMD_OK);
399 void lpp_send_ack(lpp_comm_t *comm)
401 lpp_send_res(comm, 1, "");
404 const char *lpp_get_cmd_name(int cmd)
407 case LPP_CMD_BAD: return "BAD";
408 case LPP_CMD_OK: return "OK";
409 case LPP_CMD_PROBLEM: return "PROBLEM";
410 case LPP_CMD_SOLUTION: return "SOLUTION";
411 case LPP_CMD_SOLVER: return "SOLVER";
412 case LPP_CMD_BYE: return "BYE";
413 case LPP_CMD_SOLVERS: return "SOLVERS";
414 case LPP_CMD_SET_DEBUG: return "SET_DEBUG";
415 case LPP_CMD_INFO: return "INFO";