-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmauncher-ipc.c
216 lines (174 loc) · 4.01 KB
/
mauncher-ipc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "mauncher-ipc.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "sysutil.h"
#define checklen(buf, len, l) do { \
len -= l; \
if (len < 0) { \
fprintf(stderr, "Received malformed message.\n"); \
free(buf); \
return -1; \
} \
} while (0)
int daemon_message_read(int fd, struct daemon_message *msg) {
uint8_t total_len_buf[8];
ssize_t n = read(fd, total_len_buf, sizeof(total_len_buf));
if (n < 0) {
perror("read");
return -1;
} else if (n < sizeof(total_len_buf)) {
fprintf(stderr, "Short read: %zu/%zu\n", n, sizeof(total_len_buf));
return -1;
}
ssize_t len = (ssize_t)read_uint64(total_len_buf);
uint8_t *buf = malloc(len);
uint8_t *b = buf;
if (buf == NULL) {
perror("malloc");
return -1;
}
n = read(fd, buf, len);
if (n < 0) {
perror("read");
free(buf);
return FALSE;
} else if (n < len) {
fprintf(stderr, "Short read: %zu/%zu\n", n, len);
free(buf);
return -1;
}
// uint64 payload length
checklen(buf, len, 8);
uint64_t payload_len = read_uint64(b);
b += 8;
// payload
checklen(buf, len, payload_len);
char *payload = (char *)b;
b += payload_len;
// uint32 prompt length
checklen(buf, len, 4);
uint32_t prompt_len = read_uint32(b);
b += 4;
// prompt
checklen(buf, len, prompt_len);
char *prompt = (char *)b;
b += prompt_len;
// flags
checklen(buf, len, 1);
uint8_t flags = b[0];
b += 1;
msg->payload = strndup(payload, payload_len);
msg->opts.prompt = strndup(prompt, prompt_len);
msg->opts.insensitive = flags & (1 << 0);
free(buf);
return 0;
}
int daemon_reply_read(int fd, struct daemon_reply *reply) {
uint8_t total_len_buf[8];
ssize_t n = read(fd, total_len_buf, sizeof(total_len_buf));
if (n < 0) {
perror("read");
return -1;
} else if (n == 0) {
fprintf(stderr, "Short read: %zu/%zu\n", n, sizeof(total_len_buf));
return -1;
}
ssize_t len = (ssize_t)read_uint64(total_len_buf);
uint8_t *buf = malloc(len);
uint8_t *b = buf;
if (buf == NULL) {
perror("malloc");
return -1;
}
n = read(fd, buf, len);
if (n < 0) {
perror("read");
free(buf);
return FALSE;
} else if (n < len) {
fprintf(stderr, "Short read: %zu/%zu\n", n, len);
free(buf);
return -1;
}
// uint64 reply length
checklen(buf, len, 8);
uint64_t reply_len = read_uint64(b);
b += 8;
// reply
checklen(buf, len, reply_len);
char *reply_str = (char *)b;
b += reply_len;
// status
int status = (int)read_uint32(b);
reply->reply = strndup(reply_str, reply_len);
reply->status = status;
free(buf);
return 0;
}
#undef checklen
int daemon_message_write(int fd, struct daemon_message *msg) {
uint64_t payload_len = msg->payload == NULL ? 0 : strlen(msg->payload);
uint32_t prompt_len = msg->opts.prompt == NULL ? 0 : (uint32_t)strlen(msg->opts.prompt);
uint64_t len = 8 + payload_len + 4 + prompt_len + 1;
uint8_t *buf = malloc(len);
uint8_t *b = buf;
if (buf == NULL) {
perror("malloc");
return -1;
}
uint8_t len_buf[8];
write_uint64(len_buf, len);
if (write(fd, len_buf, 8) < 0) {
perror("write");
free(buf);
return -1;
}
write_uint64(b, payload_len);
b += 8;
memcpy(b, msg->payload, payload_len);
b += payload_len;
write_uint32(b, prompt_len);
b += 4;
memcpy(b, msg->opts.prompt, prompt_len);
b += prompt_len;
b[0] = !!msg->opts.insensitive << 0;
if (write(fd, buf, len) < 0) {
perror("write");
free(buf);
return -1;
}
free(buf);
return 0;
}
int daemon_reply_write(int fd, struct daemon_reply *reply) {
uint64_t reply_len = reply->reply == NULL ? 0 : strlen(reply->reply);
uint64_t len = 8 + reply_len + 4;
uint8_t *buf = malloc(len);
uint8_t *b = buf;
if (buf == NULL) {
perror("malloc");
return -1;
}
uint8_t len_buf[8];
write_uint64(len_buf, len);
if (write(fd, len_buf, 8) < 0) {
perror("write");
free(buf);
return -1;
}
write_uint64(b, reply_len);
b += 8;
memcpy(b, reply->reply, reply_len);
b += reply_len;
write_uint32(b, (uint32_t)reply->status);
b += 4;
if (write(fd, buf, len) < 0) {
perror("write");
free(buf);
return -1;
}
free(buf);
return 0;
}