lpp: add firm copyright header, some cleanups
[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 static inline firm_dbg_module_t *get_dbg_module(void)
59 {
60         static firm_dbg_module_t *dbg = NULL;
61         if(!dbg) {
62                 dbg = firm_dbg_register("lpp.comm");
63         }
64
65         return dbg;
66 }
67
68 #define dbg get_dbg_module()
69
70 /**
71  * Try to read some bytes but block until a certain amount is read.
72  * @param fd The file descriptor.
73  * @param buf The buffer to read into.
74  * @param try_amount The amount of bytes to try to read.
75  * @param at_least block until this many bytes are read.
76  * @return The number of bytes read or -1 on error.
77  */
78 static ssize_t secure_recv(int fd, void *buf, size_t try_amount, size_t at_least)
79 {
80         ssize_t res;
81         size_t bytes_read = 0;
82         char *data = buf;
83
84         do {
85                 res = recv(fd, &data[bytes_read], try_amount - bytes_read, 0);
86                 if(res <= 0) {
87                         if(res == 0 || errno != EAGAIN)
88                                 return -1;
89                         continue;
90                 }
91
92                 bytes_read += res;
93
94         } while(bytes_read < at_least);
95
96         return bytes_read;
97 }
98
99 static ssize_t secure_send(int fd, const void *buf, size_t n)
100 {
101         ssize_t res;
102         size_t bytes_written = 0;
103         const char *data = buf;
104
105         do {
106                 res = send(fd, &data[bytes_written], n - bytes_written, 0);
107                 if(res < 0) {
108                         if(errno != EAGAIN)
109                                 return -1;
110                         continue;
111                 }
112
113                 bytes_written += res;
114
115         } while(bytes_written < n);
116
117         return n;
118 }
119
120 ssize_t lpp_flush(lpp_comm_t *comm)
121 {
122         ssize_t res = 0;
123         if(comm->w_pos - comm->w_buf > 0) {
124                 DBG((dbg, LEVEL_1, "flushing %d bytes\n", comm->w_pos - comm->w_buf));
125                 res = secure_send(comm->fd, comm->w_buf, comm->w_pos - comm->w_buf);
126                 if(res < 0)
127                         return res;
128
129                 comm->w_pos = comm->w_buf;
130         }
131         return res;
132 }
133
134 static ssize_t lpp_write(lpp_comm_t *comm, const void *buf, size_t len)
135 {
136         assert(comm->w_pos - comm->w_buf >= 0);
137
138         DBG((dbg, LEVEL_1, "write of length %d\n", len));
139         if(len > 0) {
140                 size_t free = (comm->w_buf + comm->buf_size) - comm->w_pos;
141                 size_t copy = MIN(free, len);
142                 size_t rest = len - copy;
143                 const char *pos   = buf;
144
145                 DBG((dbg, LEVEL_1, "\tfree = %d, copy = %d, rest = %d\n", free, copy, rest));
146                 if(copy > 0) {
147                         memcpy(comm->w_pos, pos, copy);
148                         comm->w_pos += copy;
149                         pos         += copy;
150                 }
151
152                 /*
153                  * Not everything in buf fits into the buffer,
154                  * so flush the buffer and write the rest.
155                  */
156                 if(rest > 0) {
157                         size_t i;
158                         size_t n_direct = rest / comm->buf_size;
159                         size_t last_rest;
160
161                         if(lpp_flush(comm) < 0)
162                                 return -1;
163
164                         for(i = 0; i < n_direct; ++i) {
165                                 if(secure_send(comm->fd, pos, comm->buf_size) < 0)
166                                         return -1;
167
168                                 pos += comm->buf_size;
169                         }
170
171                         last_rest = ((const char *) buf + len) - pos;
172
173                         if(last_rest > 0) {
174                                 assert(last_rest < comm->buf_size);
175                                 assert(comm->w_pos == comm->w_buf);
176                                 memcpy(comm->w_pos, pos, last_rest);
177                                 comm->w_pos += last_rest;
178                         }
179                 }
180         }
181
182         return len;
183 }
184
185 static ssize_t lpp_read(lpp_comm_t *comm, void *buf, size_t len)
186 {
187         DBG((dbg, LEVEL_1, "read of length %d\n", len));
188         if(len > 0) {
189                 size_t left = comm->r_max - comm->r_pos;
190                 size_t copy = MIN(left, len);
191                 size_t rest = len - copy;
192                 char *pos   = buf;
193
194                 DBG((dbg, LEVEL_1, "\tleft = %d, copy = %d, rest = %d\n", left, copy, rest));
195                 if(copy > 0) {
196                         memcpy(pos, comm->r_pos, copy);
197                         pos         += copy;
198                         comm->r_pos += copy;
199                 }
200
201                 /* We want to read more than the buffer can provide. */
202                 if(rest > 0) {
203                         size_t bs = comm->buf_size;
204                         size_t n_direct = rest / comm->buf_size;
205                         size_t i;
206                         size_t last_rest;
207
208                         /*
209                          * The buffer is now completely read, so
210                          * reset the pointers.
211                          */
212                         comm->r_pos = comm->r_buf;
213                         comm->r_max = comm->r_buf;
214
215                         for(i = 0; i < n_direct; ++i) {
216                                 if(secure_recv(comm->fd, pos, bs, bs) < 0)
217                                         return -1;
218
219                                 pos += comm->buf_size;
220                         }
221
222                         last_rest = ((const char *) buf + len) - pos;
223
224                         if(last_rest > 0) {
225                                 ssize_t bytes_read = 0;
226
227                                 assert(last_rest < comm->buf_size);
228                                 assert(comm->r_pos == comm->r_buf);
229
230                                 bytes_read = secure_recv(comm->fd, comm->r_buf, bs, last_rest);
231                                 if(bytes_read < 0)
232                                         return -1;
233
234                                 memcpy(pos, comm->r_buf, last_rest);
235                                 comm->r_pos = comm->r_buf + last_rest;
236                                 comm->r_max = comm->r_buf + bytes_read;
237                         }
238                 }
239         }
240
241         return len;
242 }
243
244 lpp_comm_t *lpp_comm_new(int fd, size_t buf_size)
245 {
246         lpp_comm_t *res = malloc(sizeof(res[0]));
247
248         res->fd       = fd;
249         res->w_buf    = malloc(buf_size);
250         res->w_pos    = res->w_buf;
251         res->r_buf    = malloc(buf_size);
252         res->r_pos    = res->r_buf;
253         res->r_max    = res->r_buf;
254         res->buf_size = buf_size;
255
256         return res;
257 }
258
259 int lpp_comm_fileno(const lpp_comm_t *comm)
260 {
261         return comm->fd;
262 }
263
264 void lpp_comm_free(lpp_comm_t *comm)
265 {
266         free(comm->w_buf);
267         free(comm->r_buf);
268         free(comm);
269 }
270
271 void lpp_print_err(const char *fmt, ...)
272 {
273         va_list args;
274
275         va_start(args, fmt);
276         vfprintf(stderr, fmt, args);
277         va_end(args);
278 }
279
280 void lpp_writel(lpp_comm_t *comm, uint32_t x)
281 {
282         x = htonl(x);
283         ERRNO_CHECK(lpp_write(comm, &x, sizeof(x)), !=, (ssize_t)sizeof(x));
284 }
285
286 void lpp_writed(lpp_comm_t *comm, double dbl)
287 {
288         ERRNO_CHECK(lpp_write(comm, &dbl, sizeof(dbl)), !=, (ssize_t)sizeof(dbl));
289 }
290
291 void lpp_writes(lpp_comm_t *comm, const char *str)
292 {
293         size_t n = strlen(str);
294         lpp_writel(comm, n);
295         ERRNO_CHECK(lpp_write(comm, str, n), !=, (ssize_t) n);
296 }
297
298 uint32_t lpp_readl(lpp_comm_t *comm)
299 {
300         uint32_t res;
301
302         ERRNO_CHECK(lpp_read(comm, &res, sizeof(res)), !=, (ssize_t)sizeof(res));
303         return ntohl(res);
304 }
305
306 int lpp_read_cmd(lpp_comm_t *comm)
307 {
308         uint32_t res = 0;
309         int retval;
310
311         for(;;) {
312                 retval = recv(comm->fd, (char *)&res, sizeof(res), 0);
313                 if(retval < 0) {
314                         if(errno != EAGAIN)
315                                 return -1;
316                 }
317
318                 else
319                         break;
320         }
321
322         return (int) ntohl(res);
323 }
324
325 double lpp_readd(lpp_comm_t *comm)
326 {
327         double res;
328         ERRNO_CHECK(lpp_read(comm, &res, sizeof(res)), !=, (ssize_t)sizeof(res));
329         return res;
330 }
331
332 char *lpp_reads(lpp_comm_t *comm)
333 {
334         size_t len = lpp_readl(comm);
335         char *res = malloc(sizeof(char) * (len + 1));
336
337         ERRNO_CHECK(lpp_read(comm, res, len), !=, (ssize_t) len);
338         res[len] = '\0';
339         return res;
340 }
341
342 char *lpp_readbuf(lpp_comm_t *comm, char *buf, size_t buflen)
343 {
344         char dummy[1024];
345         size_t i;
346         size_t n         = buflen - 1;
347         size_t len       = lpp_readl(comm);
348         size_t max_read  = n < len ? n : len;
349         size_t rest      = len - max_read;
350
351         if(buflen > 0 && buf != NULL) {
352                 ERRNO_CHECK(lpp_read(comm, buf, max_read), !=, (ssize_t) max_read);
353                 buf[max_read] = '\0';
354         }
355         else
356                 rest = len;
357
358         /* eat up data that didnt fit into the string */
359         for(i = 0, n = rest / sizeof(dummy); i < n; ++i)
360                 ERRNO_CHECK(lpp_read(comm, dummy, sizeof(dummy)), !=, (ssize_t)sizeof(dummy));
361
362         if(rest % sizeof(dummy) > 0)
363                 ERRNO_CHECK(lpp_read(comm, dummy, rest % sizeof(dummy)), !=,
364                                         (ssize_t) (rest % sizeof(dummy)) );
365
366         return buf;
367 }
368
369 int lpp_ack(lpp_comm_t *comm, char *buf, size_t buflen)
370 {
371         int res = 0;
372         int cmd = lpp_readl(comm);
373
374         switch(cmd) {
375         case LPP_CMD_OK:
376                 res = 1;
377                 break;
378         case LPP_CMD_BAD:
379                 lpp_readbuf(comm, buf, buflen);
380         default:
381                 res = 0;
382         }
383
384         return res;
385 }
386
387 void lpp_send_res(lpp_comm_t *comm, int ok, const char *fmt, ...)
388 {
389         if(!ok) {
390                 char buf[1024];
391                 va_list args;
392
393                 va_start(args, fmt);
394                 vsnprintf(buf, sizeof(buf), fmt, args);
395                 va_end(args);
396
397                 lpp_writel(comm, LPP_CMD_BAD);
398                 lpp_writes(comm, buf);
399         } else {
400                 lpp_writel(comm, LPP_CMD_OK);
401         }
402 }
403
404 void lpp_send_ack(lpp_comm_t *comm)
405 {
406         lpp_send_res(comm, 1, "");
407 }
408
409 const char *lpp_get_cmd_name(int cmd)
410 {
411         switch(cmd) {
412         case LPP_CMD_BAD:       return "BAD";
413         case LPP_CMD_OK:        return "OK";
414         case LPP_CMD_PROBLEM:   return "PROBLEM";
415         case LPP_CMD_SOLUTION:  return "SOLUTION";
416         case LPP_CMD_SOLVER:    return "SOLVER";
417         case LPP_CMD_BYE:       return "BYE";
418         case LPP_CMD_SOLVERS:   return "SOLVERS";
419         case LPP_CMD_SET_DEBUG: return "SET_DEBUG";
420         case LPP_CMD_INFO:      return "INFO";
421         case LPP_CMD_LAST:
422                 break;
423         }
424
425         return "<unknown>";
426 }