]> git.sur5r.net Git - i3/i3/blob - libi3/ipc_recv_message.c
Merge branch 'msg' into next
[i3/i3] / libi3 / ipc_recv_message.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2011 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <string.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <stdint.h>
15 #include <unistd.h>
16
17 #include <i3/ipc.h>
18
19 #include "libi3.h"
20
21 /*
22  * Reads a message from the given socket file descriptor and stores its length
23  * (reply_length) as well as a pointer to its contents (reply).
24  *
25  * Returns -1 when read() fails, errno will remain.
26  * Returns -2 when the IPC protocol is violated (invalid magic, unexpected
27  * message type, EOF instead of a message). Additionally, the error will be
28  * printed to stderr.
29  * Returns 0 on success.
30  *
31  */
32 int ipc_recv_message(int sockfd, uint32_t message_type,
33                      uint32_t *reply_length, uint8_t **reply) {
34     /* Read the message header first */
35     uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
36     char msg[to_read];
37     char *walk = msg;
38
39     uint32_t read_bytes = 0;
40     while (read_bytes < to_read) {
41         int n = read(sockfd, msg + read_bytes, to_read);
42         if (n == -1)
43             return -1;
44         if (n == 0) {
45             fprintf(stderr, "IPC: received EOF instead of reply\n");
46             return -2;
47         }
48
49         read_bytes += n;
50         to_read -= n;
51     }
52
53     if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
54         fprintf(stderr, "IPC: invalid magic in reply\n");
55         return -2;
56     }
57
58     walk += strlen(I3_IPC_MAGIC);
59     *reply_length = *((uint32_t*)walk);
60     walk += sizeof(uint32_t);
61     if (*((uint32_t*)walk) != message_type) {
62         fprintf(stderr, "IPC: unexpected reply type (got %d, expected %d)\n", *((uint32_t*)walk), message_type);
63         return -2;
64     }
65     walk += sizeof(uint32_t);
66
67     *reply = smalloc(*reply_length);
68
69     to_read = *reply_length;
70     read_bytes = 0;
71     while (read_bytes < to_read) {
72         int n = read(sockfd, *reply + read_bytes, to_read);
73         if (n == -1)
74             return -1;
75
76         read_bytes += n;
77         to_read -= n;
78     }
79
80     return 0;
81 }