forked from eurogiciel-oss/Tizen_Crosswalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nacl_desc_conn_cap.c
271 lines (233 loc) · 7.87 KB
/
nacl_desc_conn_cap.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright (c) 2012 The Native Client Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
* NaCl Service Runtime. I/O Descriptor / Handle abstraction.
* Connection capabilities.
*/
#include <stdlib.h>
#include <string.h>
#include "native_client/src/include/portability.h"
#include "native_client/src/include/nacl_macros.h"
#include "native_client/src/shared/imc/nacl_imc_c.h"
#include "native_client/src/shared/platform/nacl_log.h"
#include "native_client/src/trusted/desc/nacl_desc_base.h"
#include "native_client/src/trusted/desc/nacl_desc_conn_cap.h"
#include "native_client/src/trusted/desc/nacl_desc_imc.h"
#include "native_client/src/trusted/desc/nacl_desc_imc_bound_desc.h"
#include "native_client/src/trusted/service_runtime/nacl_config.h"
#include "native_client/src/trusted/service_runtime/include/sys/errno.h"
#include "native_client/src/trusted/service_runtime/include/sys/stat.h"
static struct NaClDescVtbl const kNaClDescConnCapVtbl; /* fwd */
/*
* NaClDescConnCapSubclassCtor takes a memory region of at least the
* size of a NaClDescConnCap in which construction of
* NaClDescConnCap's superclass has already occurred, and construct
* the remaining members (newly introduced by NaClDescConnCap). This
* is needed by the *Internalize function.
*/
static int NaClDescConnCapSubclassCtor(struct NaClDescConnCap *self,
struct NaClSocketAddress const *nsap) {
struct NaClDesc *basep = (struct NaClDesc *) self;
self->cap = *nsap;
basep->base.vtbl = (struct NaClRefCountVtbl const *) &kNaClDescConnCapVtbl;
return 1;
}
int NaClDescConnCapCtor(struct NaClDescConnCap *self,
struct NaClSocketAddress const *nsap) {
struct NaClDesc *basep = (struct NaClDesc *) self;
int rv;
basep->base.vtbl = (struct NaClRefCountVtbl const *) NULL;
if (!NaClDescCtor(basep)) {
return 0;
}
rv = NaClDescConnCapSubclassCtor(self, nsap);
if (!rv) {
/* NaClDescConnCap construction failed, still a NaClDesc object */
(*NACL_VTBL(NaClRefCount, basep)->Dtor)((struct NaClRefCount *) basep);
/* not-an-object; caller frees */
}
return rv;
}
static void NaClDescConnCapDtor(struct NaClRefCount *vself) {
vself->vtbl = (struct NaClRefCountVtbl const *) &kNaClDescVtbl;
(*vself->vtbl->Dtor)(vself);
return;
}
static int NaClDescConnCapFstat(struct NaClDesc *vself,
struct nacl_abi_stat *statbuf) {
UNREFERENCED_PARAMETER(vself);
memset(statbuf, 0, sizeof *statbuf);
statbuf->nacl_abi_st_mode = NACL_ABI_S_IFSOCKADDR | NACL_ABI_S_IRWXU;
return 0;
}
static int NaClDescConnCapExternalizeSize(struct NaClDesc *vself,
size_t *nbytes,
size_t *nhandles) {
int rv;
UNREFERENCED_PARAMETER(vself);
rv = NaClDescExternalizeSize(vself, nbytes, nhandles);
if (0 != rv) {
return rv;
}
*nbytes += NACL_PATH_MAX;
return 0;
}
static int NaClDescConnCapExternalize(struct NaClDesc *vself,
struct NaClDescXferState *xfer) {
struct NaClDescConnCap *self;
int rv;
rv = NaClDescExternalize(vself, xfer);
if (0 != rv) {
return rv;
}
self = (struct NaClDescConnCap *) vself;
memcpy(xfer->next_byte, self->cap.path, NACL_PATH_MAX);
xfer->next_byte += NACL_PATH_MAX;
return 0;
}
int NaClDescConnCapConnectAddr(struct NaClDesc *vself,
struct NaClDesc **out_desc) {
/*
* See NaClDescImcBoundDescAcceptConn code in
* nacl_desc_imc_bound_desc.c
*/
struct NaClDescConnCap *self;
int retval;
NaClHandle nh[2];
size_t ix;
struct NaClMessageHeader conn_msg;
struct NaClDescImcDesc *peer;
NaClLog(3, "Entered NaClDescConnCapConnectAddr\n");
self = (struct NaClDescConnCap *) vself;
peer = NULL;
for (ix = 0; ix < NACL_ARRAY_SIZE(nh); ++ix) {
nh[ix] = NACL_INVALID_HANDLE;
}
NaClLog(4, " socket address %.*s\n", NACL_PATH_MAX, self->cap.path);
if (NULL == (peer = malloc(sizeof *peer))) {
retval = -NACL_ABI_ENOMEM;
goto cleanup;
}
if (0 != NaClSocketPair(nh)) {
retval = -NACL_ABI_EMFILE;
goto cleanup;
}
conn_msg.iov_length = 0;
conn_msg.iov = NULL;
conn_msg.handles = &nh[0];
conn_msg.handle_count = 1; /* send nh[0], keep nh[1] */
conn_msg.flags = 0;
NaClLog(4, " sending connection message\n");
if (-1 == NaClSendDatagramTo(&conn_msg, 0, &self->cap)) {
NaClLog(LOG_ERROR, ("NaClDescConnCapConnectAddr:"
" initial connect message could not be sent.\n"));
retval = -NACL_ABI_EIO;
goto cleanup;
}
(void) NaClClose(nh[0]);
nh[0] = NACL_INVALID_HANDLE;
NaClLog(4, " creating NaClDescImcDesc for local end of socketpair\n");
if (!NaClDescImcDescCtor(peer, nh[1])) {
retval = -NACL_ABI_EMFILE; /* TODO(bsy): is this the right errno? */
goto cleanup;
}
nh[1] = NACL_INVALID_HANDLE;
*out_desc = (struct NaClDesc *) peer;
retval = 0;
cleanup:
if (retval < 0) {
NaClLog(4, " error return; cleaning up\n");
if (NACL_INVALID_HANDLE != nh[0])
(void) NaClClose(nh[0]);
if (NACL_INVALID_HANDLE != nh[1])
(void) NaClClose(nh[1]);
/* peer is not constructed, so we need only to free the memory */
free(peer);
}
return retval;
}
static int NaClDescConnCapAcceptConn(struct NaClDesc *vself,
struct NaClDesc **out_desc) {
UNREFERENCED_PARAMETER(vself);
UNREFERENCED_PARAMETER(out_desc);
NaClLog(LOG_ERROR, "NaClDescConnCapAcceptConn: not IMC\n");
return -NACL_ABI_EINVAL;
}
static struct NaClDescVtbl const kNaClDescConnCapVtbl = {
{
NaClDescConnCapDtor,
},
NaClDescMapNotImplemented,
NACL_DESC_UNMAP_NOT_IMPLEMENTED
NaClDescReadNotImplemented,
NaClDescWriteNotImplemented,
NaClDescSeekNotImplemented,
NaClDescPReadNotImplemented,
NaClDescPWriteNotImplemented,
NaClDescIoctlNotImplemented,
NaClDescConnCapFstat,
NaClDescGetdentsNotImplemented,
NaClDescConnCapExternalizeSize,
NaClDescConnCapExternalize,
NaClDescLockNotImplemented,
NaClDescTryLockNotImplemented,
NaClDescUnlockNotImplemented,
NaClDescWaitNotImplemented,
NaClDescTimedWaitAbsNotImplemented,
NaClDescSignalNotImplemented,
NaClDescBroadcastNotImplemented,
NaClDescSendMsgNotImplemented,
NaClDescRecvMsgNotImplemented,
NaClDescLowLevelSendMsgNotImplemented,
NaClDescLowLevelRecvMsgNotImplemented,
NaClDescConnCapConnectAddr,
NaClDescConnCapAcceptConn,
NaClDescPostNotImplemented,
NaClDescSemWaitNotImplemented,
NaClDescGetValueNotImplemented,
NaClDescSetMetadata,
NaClDescGetMetadata,
NaClDescSetFlags,
NaClDescGetFlags,
NACL_DESC_CONN_CAP,
};
int NaClDescConnCapInternalize(struct NaClDesc **out_desc,
struct NaClDescXferState *xfer,
struct NaClDescQuotaInterface *quota_interface) {
int rv;
struct NaClSocketAddress nsa;
struct NaClDescConnCap *ndccp;
UNREFERENCED_PARAMETER(quota_interface);
rv = -NACL_ABI_EIO; /* catch-all */
ndccp = malloc(sizeof *ndccp);
if (NULL == ndccp) {
rv = -NACL_ABI_ENOMEM;
goto cleanup;
}
if (!NaClDescInternalizeCtor((struct NaClDesc *) ndccp, xfer)) {
free(ndccp);
ndccp = NULL;
rv = -NACL_ABI_ENOMEM;
goto cleanup;
}
if (xfer->next_byte + NACL_PATH_MAX > xfer->byte_buffer_end) {
rv = -NACL_ABI_EIO;
goto cleanup;
}
memcpy(nsa.path, xfer->next_byte, NACL_PATH_MAX);
if (!NaClDescConnCapSubclassCtor(ndccp, &nsa)) {
rv = -NACL_ABI_EIO;
goto cleanup;
}
*out_desc = (struct NaClDesc *) ndccp;
rv = 0;
xfer->next_byte += NACL_PATH_MAX;
cleanup:
if (rv < 0) {
NaClDescSafeUnref((struct NaClDesc *) ndccp);
}
return rv;
}