forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfanos_shared.c
215 lines (183 loc) · 5.18 KB
/
fanos_shared.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
#include "cpp/fanos_shared.h"
#include <assert.h>
#include <errno.h>
#include <stdarg.h> // va_list, etc.
#include <stdio.h> // vfprintf
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define SIZEOF_FDS (sizeof(int) * FANOS_NUM_FDS)
const char* kErrTooLarge = "Message too large";
const char* kErrSolSocket = "Expected cmsg_level SOL_SOCKET";
const char* kErrScmRights = "Expected cmsg_type SCM_RIGHTS";
const char* kErrUnexpectedEof = "Unexpected EOF";
const char* kErrMissingLength = "Expected netstring length";
const char* kErrMissingColon = "Expected : after netstring length";
const char* kErrMissingComma = "Expected ,";
void fanos_send(int sock_fd, char* blob, int blob_len, const int* fds,
struct FanosError* err) {
char buf[10];
// snprintf() doesn't write more than 10 bytes, INCLUDING \0
// It the number of bytes it would have written, EXCLUDING \0
unsigned int full_length = snprintf(buf, 10, "%d:", blob_len);
if (full_length > sizeof(buf)) {
err->value_err = kErrTooLarge;
return;
}
if (write(sock_fd, buf, full_length) < 0) { // send '3:'
err->err_code = errno;
return;
}
// Example code adapted from 'man CMSG_LEN' on my machine. (Is this
// portable?)
//
// The APUE code only sends a single FD and doesn't use CMSG_SPACE.
// Set up the blob data
struct iovec iov = {0};
iov.iov_base = blob;
iov.iov_len = blob_len;
struct msghdr msg = {0};
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
// This stack buffer has to live until the sendmsg() call!
union {
/* ancillary data buffer, wrapped in a union in order to ensure
it is suitably aligned */
char buf[CMSG_SPACE(SIZEOF_FDS)];
struct cmsghdr align;
} u;
// Optionally set up file descriptor data
if (fds[0] != -1) {
// If one FD is passed, all should be passed
assert(fds[1] != -1);
assert(fds[2] != -1);
msg.msg_control = u.buf;
msg.msg_controllen = sizeof u.buf;
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(SIZEOF_FDS);
int* fd_msg = (int*)CMSG_DATA(cmsg);
memcpy(fd_msg, fds, SIZEOF_FDS);
}
int num_bytes = sendmsg(sock_fd, &msg, 0);
if (num_bytes < 0) {
err->err_code = errno;
return;
}
buf[0] = ',';
if (write(sock_fd, buf, 1) < 0) {
err->err_code = errno;
return;
}
}
static int recv_fds_once(int sock_fd, int num_bytes, char* buf, int* buf_len,
int* fd_out, struct FanosError* err) {
// Where to put data
struct iovec iov = {0};
iov.iov_base = buf;
iov.iov_len = num_bytes; // number of bytes REQUESTED
struct msghdr msg = {0};
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
union {
char control[CMSG_SPACE(SIZEOF_FDS)];
struct cmsghdr align;
} u;
msg.msg_control = u.control;
msg.msg_controllen = sizeof u.control;
size_t bytes_read = recvmsg(sock_fd, &msg, 0);
if (bytes_read < 0) {
err->err_code = errno;
return -1;
}
*buf_len = bytes_read;
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg && cmsg->cmsg_len == CMSG_LEN(SIZEOF_FDS)) {
if (cmsg->cmsg_level != SOL_SOCKET) {
err->value_err = kErrSolSocket;
return -1;
}
if (cmsg->cmsg_type != SCM_RIGHTS) {
err->value_err = kErrScmRights;
return -1;
}
int* fd_list = (int*)CMSG_DATA(cmsg);
// Append the descriptors received
fd_out[0] = fd_list[0];
fd_out[1] = fd_list[1];
fd_out[2] = fd_list[2];
}
return 0;
}
void fanos_recv(int sock_fd, int* fd_out, struct FanosResult* result_out,
struct FanosError* err) {
// Receive with netstring encoding
char buf[10]; // up to 9 digits, then :
char* p = buf;
int n;
for (int i = 0; i < 10; ++i) {
n = read(sock_fd, p, 1);
if (n < 0) {
err->err_code = errno;
return;
}
if (n != 1) {
if (i == 0) {
// read() returned 0 bytes, which means we got EOF at a message
// boundary. This is part of the protocol and the caller should handle
// it.
result_out->len = FANOS_EOF;
return;
} else {
err->value_err = kErrUnexpectedEof;
return;
}
}
if ('0' <= *p && *p <= '9') {
; // added to the buffer
} else {
break;
}
p++;
}
if (p == buf) {
err->value_err = kErrMissingLength;
return;
}
if (*p != ':') {
err->value_err = kErrMissingColon;
return;
}
*p = '\0'; // change : to NUL terminator
int expected_bytes = atoi(buf);
char* data_buf = (char*)malloc(expected_bytes + 1);
data_buf[expected_bytes] = '\0';
n = 0;
while (n < expected_bytes) {
int bytes_read;
if (recv_fds_once(sock_fd, expected_bytes - n, data_buf + n, &bytes_read,
fd_out, err) < 0) {
return;
}
n += bytes_read;
break;
}
assert(n == expected_bytes);
n = read(sock_fd, buf, 1);
if (n < 0) {
err->err_code = errno;
return;
}
if (n != 1) {
err->value_err = kErrUnexpectedEof;
return;
}
if (buf[0] != ',') {
err->value_err = kErrMissingComma;
return;
}
result_out->data = data_buf;
result_out->len = expected_bytes;
}