2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // NaCl Handle Transfer Protocol
35 // The NaCl sel_ldr uses the NaCl Handle Transfer Protocol to send/receive
36 // messages between NaCl modules. This module provides the utility functions
37 // to communicate with a NaCl module from the trusted code.
39 #ifndef NATIVE_CLIENT_INTERMODULE_COMM_NACL_HTP_H_
40 #define NATIVE_CLIENT_INTERMODULE_COMM_NACL_HTP_H_
44 #ifdef __native_client__
47 #include "native_client/intermodule_comm/nacl_imc.h"
49 #ifdef __native_client__
53 typedef Handle HtpHandle
;
54 typedef MessageHeader HtpHeader
;
56 // Creates a host I/O descriptor from the handle.
57 inline HtpHandle
CreateIoDesc(Handle handle
) {
61 // Creates a shared memory descriptor from the handle.
62 inline HtpHandle
CreateShmDesc(Handle handle
, off_t length
) {
66 // Creates an IMC memory descriptor from the handle.
67 inline HtpHandle
CreateImcDesc(Handle handle
) {
71 // Reads up to count bytes from the handle into buffer.
72 inline int Read(HtpHandle handle
, void* buffer
, size_t count
) {
73 return read(handle
, buffer
, count
);
76 // Writes up to count bytes to the handle from buffer.
77 inline int Write(HtpHandle handle
, const void* buffer
, size_t count
) {
78 return write(handle
, buffer
, count
);
81 const HtpHandle kInvalidHtpHandle
= kInvalidHandle
;
85 #else // __native_client__
91 // NaCl resource descriptor type compatible with sel_ldr.
92 typedef struct NaClDesc
* HtpHandle
;
94 // Message header used by SendDatagram() and ReceiveDatagram().
95 // Note the member layout is compatible with NaClImcTypedMsgHdr{}.
97 IOVec
* iov
; // scatter/gather array
98 size_t iov_length
; // number of elements in iov
99 HtpHandle
* handles
; // array of handles to be transferred
100 size_t handle_count
; // number of handles in handles
104 // Sends a message on a socket. Except for the fact that HtpHeader is used,
105 // the runtime behavior of this function is the same as SendDatagram() for
107 // On success, SendDatagram() returns the number of bytes sent.
108 int SendDatagram(HtpHandle socket
, const HtpHeader
* message
, int flags
);
110 // Receives a message from a socket. Except for the fact that HtpHeader is
111 // used, the runtime behavior of this function is the same as ReceiveDatagram()
112 // for MessageHeader.
113 // On success, ReceiveDatagram() returns the number of bytes received.
114 int ReceiveDatagram(HtpHandle socket
, HtpHeader
* message
, int flags
);
116 // Closes a sel_ldr compatible NaCl descriptor.
117 int Close(HtpHandle handle
);
119 // Maps the specified memory object into the process address space.
120 // Map() returns a pointer to the mapped area, or kMapFailed upon error.
121 // For prot, the bitwise OR of the kProt* bits must be specified.
122 // For flags, either kMapShared or kMapPrivate must be specified. If kMapFixed
123 // is also set, Map() tries to map the memory object at the address specified by
125 void* Map(void* start
, size_t length
, int prot
, int flags
,
126 HtpHandle memory
, off_t offset
);
128 // Creates a host I/O descriptor from the handle.
129 HtpHandle
CreateIoDesc(Handle handle
);
131 // Creates a shared memory descriptor from the handle.
132 HtpHandle
CreateShmDesc(Handle handle
, off_t length
);
134 // Creates an IMC memory descriptor from the handle.
135 HtpHandle
CreateImcDesc(Handle handle
);
137 // Reads up to count bytes from the handle into buffer.
138 int Read(HtpHandle handle
, void* buffer
, size_t count
);
140 // Writes up to count bytes to the handle from buffer.
141 int Write(HtpHandle handle
, const void* buffer
, size_t count
);
143 const HtpHandle kInvalidHtpHandle
= NULL
;
147 #endif // __native_client__
149 #endif // NATIVE_CLIENT_INTERMODULE_COMM_NACL_HTP_H_