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__
46 #include <nacl/nacl_imc.h>
48 #include "native_client/intermodule_comm/nacl_imc.h"
49 #endif // __native_client__
51 #ifdef __native_client__
55 typedef Handle HtpHandle
;
56 typedef MessageHeader HtpHeader
;
58 // Creates a host I/O descriptor from the handle.
59 inline HtpHandle
CreateIoDesc(Handle handle
) {
63 // Creates a shared memory descriptor from the handle.
64 inline HtpHandle
CreateShmDesc(Handle handle
, off_t length
) {
68 // Creates an IMC memory descriptor from the handle.
69 inline HtpHandle
CreateImcDesc(Handle handle
) {
73 // Reads up to count bytes from the handle into buffer.
74 inline int Read(HtpHandle handle
, void* buffer
, size_t count
) {
75 return read(handle
, buffer
, count
);
78 // Writes up to count bytes to the handle from buffer.
79 inline int Write(HtpHandle handle
, const void* buffer
, size_t count
) {
80 return write(handle
, buffer
, count
);
83 const HtpHandle kInvalidHtpHandle
= kInvalidHandle
;
87 #else // __native_client__
93 // NaCl resource descriptor type compatible with sel_ldr.
94 typedef struct NaClDesc
* HtpHandle
;
96 // Message header used by SendDatagram() and ReceiveDatagram().
97 // Note the member layout is compatible with NaClImcTypedMsgHdr{}.
99 IOVec
* iov
; // scatter/gather array
100 size_t iov_length
; // number of elements in iov
101 HtpHandle
* handles
; // array of handles to be transferred
102 size_t handle_count
; // number of handles in handles
106 // Sends a message on a socket. Except for the fact that HtpHeader is used,
107 // the runtime behavior of this function is the same as SendDatagram() for
109 // On success, SendDatagram() returns the number of bytes sent.
110 int SendDatagram(HtpHandle socket
, const HtpHeader
* message
, int flags
);
112 // Receives a message from a socket. Except for the fact that HtpHeader is
113 // used, the runtime behavior of this function is the same as ReceiveDatagram()
114 // for MessageHeader.
115 // On success, ReceiveDatagram() returns the number of bytes received.
116 int ReceiveDatagram(HtpHandle socket
, HtpHeader
* message
, int flags
);
118 // Closes a sel_ldr compatible NaCl descriptor.
119 int Close(HtpHandle handle
);
121 // Maps the specified memory object into the process address space.
122 // Map() returns a pointer to the mapped area, or kMapFailed upon error.
123 // For prot, the bitwise OR of the kProt* bits must be specified.
124 // For flags, either kMapShared or kMapPrivate must be specified. If kMapFixed
125 // is also set, Map() tries to map the memory object at the address specified by
127 void* Map(void* start
, size_t length
, int prot
, int flags
,
128 HtpHandle memory
, off_t offset
);
130 // Creates a host I/O descriptor from the handle.
131 HtpHandle
CreateIoDesc(Handle handle
);
133 // Creates a shared memory descriptor from the handle.
134 HtpHandle
CreateShmDesc(Handle handle
, off_t length
);
136 // Creates an IMC memory descriptor from the handle.
137 HtpHandle
CreateImcDesc(Handle handle
);
139 // Reads up to count bytes from the handle into buffer.
140 int Read(HtpHandle handle
, void* buffer
, size_t count
);
142 // Writes up to count bytes to the handle from buffer.
143 int Write(HtpHandle handle
, const void* buffer
, size_t count
);
145 const HtpHandle kInvalidHtpHandle
= NULL
;
149 #endif // __native_client__
151 #endif // NATIVE_CLIENT_INTERMODULE_COMM_NACL_HTP_H_