Fix DEBUG_ONLY use
[libfirm] / ir / lpp / lpp_comm.c
1 /*
2  * Copyright (C) 2005-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Protocol stuff for lpp server
23  * @author  Sebastian Hack
24  */
25 #include "config.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <errno.h>
31
32 #ifdef _WIN32
33 #define WIN32_LEAN_AND_MEAN
34 #include <windows.h>
35 #include <winsock2.h>
36 #else
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <arpa/inet.h>
41 #endif
42
43 #include "irtools.h"
44 #include "debug.h"
45
46 #include "lpp_comm.h"
47
48 struct _lpp_comm_t {
49         int fd;
50         size_t buf_size;
51         char *w_pos;
52         char *r_pos;
53         char *r_max;
54         char *w_buf;
55         char *r_buf;
56 };
57
58 #ifdef DEBUG_libfirm
59 static inline firm_dbg_module_t *get_dbg_module(void)
60 {
61         static firm_dbg_module_t *dbg = NULL;
62         if(!dbg) {
63                 dbg = firm_dbg_register("lpp.comm");
64         }
65
66         return dbg;
67 }
68 #define dbg get_dbg_module()
69 #endif
70
71 /**
72  * Try to read some bytes but block until a certain amount is read.
73  * @param fd The file descriptor.
74  * @param buf The buffer to read into.
75  * @param try_amount The amount of bytes to try to read.
76  * @param at_least block until this many bytes are read.
77  * @return The number of bytes read or -1 on error.
78  */
79 static ssize_t secure_recv(int fd, void *buf, size_t try_amount, size_t at_least)
80 {
81         ssize_t res;
82         size_t bytes_read = 0;
83         char *data = buf;
84
85         do {
86                 res = recv(fd, &data[bytes_read], try_amount - bytes_read, 0);
87                 if(res <= 0) {
88                         if(res == 0 || errno != EAGAIN)
89                                 return -1;
90                         continue;
91                 }
92
93                 bytes_read += res;
94
95         } while(bytes_read < at_least);
96
97         return bytes_read;
98 }
99
100 static ssize_t secure_send(int fd, const void *buf, size_t n)
101 {
102         ssize_t res;
103         size_t bytes_written = 0;
104         const char *data = buf;
105
106         do {
107                 res = send(fd, &data[bytes_written], n - bytes_written, 0);
108                 if(res < 0) {
109                         if(errno != EAGAIN)
110                                 return -1;
111                         continue;
112                 }
113
114                 bytes_written += res;
115
116         } while(bytes_written < n);
117
118         return n;
119 }
120
121 ssize_t lpp_flush(lpp_comm_t *comm)
122 {
123         ssize_t res = 0;
124         if(comm->w_pos - comm->w_buf > 0) {
125                 DBG((dbg, LEVEL_1, "flushing %d bytes\n", comm->w_pos - comm->w_buf));
126                 res = secure_send(comm->fd, comm->w_buf, comm->w_pos - comm->w_buf);
127                 if(res < 0)
128                         return res;
129
130                 comm->w_pos = comm->w_buf;
131         }
132         return res;
133 }
134
135 static ssize_t lpp_write(lpp_comm_t *comm, const void *buf, size_t len)
136 {
137         assert(comm->w_pos - comm->w_buf >= 0);
138
139         DBG((dbg, LEVEL_1, "write of length %d\n", len));
140         if(len > 0) {
141                 size_t free = (comm->w_buf + comm->buf_size) - comm->w_pos;
142                 size_t copy = MIN(free, len);
143                 size_t rest = len - copy;
144                 const char *pos   = buf;
145
146                 DBG((dbg, LEVEL_1, "\tfree = %d, copy = %d, rest = %d\n", free, copy, rest));
147                 if(copy > 0) {
148                         memcpy(comm->w_pos, pos, copy);
149                         comm->w_pos += copy;
150                         pos         += copy;
151                 }
152
153                 /*
154                  * Not everything in buf fits into the buffer,
155                  * so flush the buffer and write the rest.
156                  */
157                 if(rest > 0) {
158                         size_t i;
159                         size_t n_direct = rest / comm->buf_size;
160                         size_t last_rest;
161
162                         if(lpp_flush(comm) < 0)
163                                 return -1;
164
165                         for(i = 0; i < n_direct; ++i) {
166                                 if(secure_send(comm->fd, pos, comm->buf_size) < 0)
167                                         return -1;
168
169                                 pos += comm->buf_size;
170                         }
171
172                         last_rest = ((const char *) buf + len) - pos;
173
174                         if(last_rest > 0) {
175                                 assert(last_rest < comm->buf_size);
176                                 assert(comm->w_pos == comm->w_buf);
177                                 memcpy(comm->w_pos, pos, last_rest);
178                                 comm->w_pos += last_rest;
179                         }
180                 }
181         }
182
183         return len;
184 }
185
186 static ssize_t lpp_read(lpp_comm_t *comm, void *buf, size_t len)
187 {
188         DBG((dbg, LEVEL_1, "read of length %d\n", len));
189         if(len > 0) {
190                 size_t left = comm->r_max - comm->r_pos;
191                 size_t copy = MIN(left, len);
192                 size_t rest = len - copy;
193                 char *pos   = buf;
194
195                 DBG((dbg, LEVEL_1, "\tleft = %d, copy = %d, rest = %d\n", left, copy, rest));
196                 if(copy > 0) {
197                         memcpy(pos, comm->r_pos, copy);
198                         pos         += copy;
199                         comm->r_pos += copy;
200                 }
201
202                 /* We want to read more than the buffer can provide. */
203                 if(rest > 0) {
204                         size_t bs = comm->buf_size;
205                         size_t n_direct = rest / comm->buf_size;
206                         size_t i;
207                         size_t last_rest;
208
209                         /*
210                          * The buffer is now completely read, so
211                          * reset the pointers.
212                          */
213                         comm->r_pos = comm->r_buf;
214                         comm->r_max = comm->r_buf;
215
216                         for(i = 0; i < n_direct; ++i) {
217                                 if(secure_recv(comm->fd, pos, bs, bs) < 0)
218                                         return -1;
219
220                                 pos += comm->buf_size;
221                         }
222
223                         last_rest = ((const char *) buf + len) - pos;
224
225                         if(last_rest > 0) {
226                                 ssize_t bytes_read = 0;
227
228                                 assert(last_rest < comm->buf_size);
229                                 assert(comm->r_pos == comm->r_buf);
230
231                                 bytes_read = secure_recv(comm->fd, comm->r_buf, bs, last_rest);
232                                 if(bytes_read < 0)
233                                         return -1;
234
235                                 memcpy(pos, comm->r_buf, last_rest);
236                                 comm->r_pos = comm->r_buf + last_rest;
237                                 comm->r_max = comm->r_buf + bytes_read;
238                         }
239                 }
240         }
241
242         return len;
243 }
244
245 lpp_comm_t *lpp_comm_new(int fd, size_t buf_size)
246 {
247         lpp_comm_t *res = malloc(sizeof(res[0]));
248
249         res->fd       = fd;
250         res->w_buf    = malloc(buf_size);
251         res->w_pos    = res->w_buf;
252         res->r_buf    = malloc(buf_size);
253         res->r_pos    = res->r_buf;
254         res->r_max    = res->r_buf;
255         res->buf_size = buf_size;
256
257         return res;
258 }
259
260 int lpp_comm_fileno(const lpp_comm_t *comm)
261 {
262         return comm->fd;
263 }
264
265 void lpp_comm_free(lpp_comm_t *comm)
266 {
267         free(comm->w_buf);
268         free(comm->r_buf);
269         free(comm);
270 }
271
272 void lpp_print_err(const char *fmt, ...)
273 {
274         va_list args;
275
276         va_start(args, fmt);
277         vfprintf(stderr, fmt, args);
278         va_end(args);
279 }
280
281 void lpp_writel(lpp_comm_t *comm, uint32_t x)
282 {
283         x = htonl(x);
284         ERRNO_CHECK(lpp_write(comm, &x, sizeof(x)), !=, (ssize_t)sizeof(x));
285 }
286
287 void lpp_writed(lpp_comm_t *comm, double dbl)
288 {
289         ERRNO_CHECK(lpp_write(comm, &dbl, sizeof(dbl)), !=, (ssize_t)sizeof(dbl));
290 }
291
292 void lpp_writes(lpp_comm_t *comm, const char *str)
293 {
294         size_t n = strlen(str);
295         lpp_writel(comm, n);
296         ERRNO_CHECK(lpp_write(comm, str, n), !=, (ssize_t) n);
297 }
298
299 uint32_t lpp_readl(lpp_comm_t *comm)
300 {
301         uint32_t res;
302
303         ERRNO_CHECK(lpp_read(comm, &res, sizeof(res)), !=, (ssize_t)sizeof(res));
304         return ntohl(res);
305 }
306
307 int lpp_read_cmd(lpp_comm_t *comm)
308 {
309         uint32_t res = 0;
310         int retval;
311
312         for(;;) {
313                 retval = recv(comm->fd, (char *)&res, sizeof(res), 0);
314                 if(retval < 0) {
315                         if(errno != EAGAIN)
316                                 return -1;
317                 }
318
319                 else
320                         break;
321         }
322
323         return (int) ntohl(res);
324 }
325
326 double lpp_readd(lpp_comm_t *comm)
327 {
328         double res;
329         ERRNO_CHECK(lpp_read(comm, &res, sizeof(res)), !=, (ssize_t)sizeof(res));
330         return res;
331 }
332
333 char *lpp_reads(lpp_comm_t *comm)
334 {
335         size_t len = lpp_readl(comm);
336         char *res = malloc(sizeof(char) * (len + 1));
337
338         ERRNO_CHECK(lpp_read(comm, res, len), !=, (ssize_t) len);
339         res[len] = '\0';
340         return res;
341 }
342
343 char *lpp_readbuf(lpp_comm_t *comm, char *buf, size_t buflen)
344 {
345         char dummy[1024];
346         size_t i;
347         size_t n         = buflen - 1;
348         size_t len       = lpp_readl(comm);
349         size_t max_read  = n < len ? n : len;
350         size_t rest      = len - max_read;
351
352         if(buflen > 0 && buf != NULL) {
353                 ERRNO_CHECK(lpp_read(comm, buf, max_read), !=, (ssize_t) max_read);
354                 buf[max_read] = '\0';
355         }
356         else
357                 rest = len;
358
359         /* eat up data that didnt fit into the string */
360         for(i = 0, n = rest / sizeof(dummy); i < n; ++i)
361                 ERRNO_CHECK(lpp_read(comm, dummy, sizeof(dummy)), !=, (ssize_t)sizeof(dummy));
362
363         if(rest % sizeof(dummy) > 0)
364                 ERRNO_CHECK(lpp_read(comm, dummy, rest % sizeof(dummy)), !=,
365                                         (ssize_t) (rest % sizeof(dummy)) );
366
367         return buf;
368 }
369
370 int lpp_ack(lpp_comm_t *comm, char *buf, size_t buflen)
371 {
372         int res = 0;
373         int cmd = lpp_readl(comm);
374
375         switch(cmd) {
376         case LPP_CMD_OK:
377                 res = 1;
378                 break;
379         case LPP_CMD_BAD:
380                 lpp_readbuf(comm, buf, buflen);
381         default:
382                 res = 0;
383         }
384
385         return res;
386 }
387
388 void lpp_send_res(lpp_comm_t *comm, int ok, const char *fmt, ...)
389 {
390         if(!ok) {
391                 char buf[1024];
392                 va_list args;
393
394                 va_start(args, fmt);
395                 vsnprintf(buf, sizeof(buf), fmt, args);
396                 va_end(args);
397
398                 lpp_writel(comm, LPP_CMD_BAD);
399                 lpp_writes(comm, buf);
400         } else {
401                 lpp_writel(comm, LPP_CMD_OK);
402         }
403 }
404
405 void lpp_send_ack(lpp_comm_t *comm)
406 {
407         lpp_send_res(comm, 1, "");
408 }
409
410 const char *lpp_get_cmd_name(int cmd)
411 {
412         switch(cmd) {
413         case LPP_CMD_BAD:       return "BAD";
414         case LPP_CMD_OK:        return "OK";
415         case LPP_CMD_PROBLEM:   return "PROBLEM";
416         case LPP_CMD_SOLUTION:  return "SOLUTION";
417         case LPP_CMD_SOLVER:    return "SOLVER";
418         case LPP_CMD_BYE:       return "BYE";
419         case LPP_CMD_SOLVERS:   return "SOLVERS";
420         case LPP_CMD_SET_DEBUG: return "SET_DEBUG";
421         case LPP_CMD_INFO:      return "INFO";
422         case LPP_CMD_LAST:
423                 break;
424         }
425
426         return "<unknown>";
427 }