1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef LIBRARIES_NACL_IO_SOCKET_SOCKET_NODE_H_
6 #define LIBRARIES_NACL_IO_SOCKET_SOCKET_NODE_H_
8 #include "nacl_io/ossocket.h"
9 #ifdef PROVIDES_SOCKET_API
11 #include <sys/fcntl.h>
12 #include <ppapi/c/pp_errors.h>
13 #include <ppapi/c/pp_resource.h>
14 #include <ppapi/c/ppb_net_address.h>
16 #include "nacl_io/filesystem.h"
17 #include "nacl_io/node.h"
18 #include "nacl_io/pepper_interface.h"
19 #include "nacl_io/stream/stream_node.h"
23 /* Only allow single maximum transfers of 64K or less. Socket users
24 * should be looping on Send/Recv size. */
25 static const size_t MAX_SOCK_TRANSFER
= 65536;
29 typedef sdk_util::ScopedRef
<SocketNode
> ScopedSocketNode
;
31 class SocketNode
: public StreamNode
{
33 explicit SocketNode(Filesystem
* filesystem
);
34 SocketNode(Filesystem
* filesystem
, PP_Resource socket
);
37 virtual void Destroy();
40 // Normal read/write operations on a file (recv/send).
41 virtual Error
Read(const HandleAttr
& attr
,
45 virtual Error
Write(const HandleAttr
& attr
,
50 virtual Error
GetSockOpt(int lvl
, int optname
, void* optval
, socklen_t
* len
);
51 virtual Error
SetSockOpt(int lvl
,
56 // Unsupported Functions
57 virtual Error
MMap(void* addr
,
65 virtual Error
Accept(const HandleAttr
& attr
,
66 PP_Resource
* new_sock
,
67 struct sockaddr
* addr
,
69 virtual Error
Bind(const struct sockaddr
* addr
, socklen_t len
);
70 virtual Error
Connect(const HandleAttr
& attr
,
71 const struct sockaddr
* addr
,
73 virtual Error
Listen(int backlog
);
74 virtual Error
Recv(const HandleAttr
& attr
,
79 virtual Error
RecvFrom(const HandleAttr
& attr
,
83 struct sockaddr
* src_addr
,
86 virtual Error
Send(const HandleAttr
& attr
,
91 virtual Error
SendTo(const HandleAttr
& attr
,
95 const struct sockaddr
* dest_addr
,
99 virtual Error
Shutdown(int how
);
101 virtual Error
GetPeerName(struct sockaddr
* addr
, socklen_t
* len
);
102 virtual Error
GetSockName(struct sockaddr
* addr
, socklen_t
* len
);
104 PP_Resource
socket_resource() { return socket_resource_
; }
105 PP_Resource
remote_addr() { return remote_addr_
; }
107 // Updates socket's state, recording last error.
108 virtual void SetError_Locked(int pp_error_num
);
111 bool IsBound() { return local_addr_
!= 0; }
112 bool IsConnected() { return remote_addr_
!= 0; }
114 // Wraps common error checks, timeouts, work pump for send.
115 Error
SendHelper(const HandleAttr
& attr
,
122 // Wraps common error checks, timeouts, work pump for recv.
123 Error
RecvHelper(const HandleAttr
& attr
,
130 // Per socket type send and recv
131 virtual Error
Recv_Locked(void* buffer
,
133 PP_Resource
* out_addr
,
136 virtual Error
Send_Locked(const void* buffer
,
141 NetAddressInterface
* NetInterface();
142 TCPSocketInterface
* TCPInterface();
143 UDPSocketInterface
* UDPInterface();
145 PP_Resource
SockAddrToResource(const struct sockaddr
* addr
, socklen_t len
);
147 PP_Resource
SockAddrInToResource(const sockaddr_in
* sin
, socklen_t len
);
149 PP_Resource
SockAddrIn6ToResource(const sockaddr_in6
* sin
, socklen_t len
);
151 socklen_t
ResourceToSockAddr(PP_Resource addr
,
153 struct sockaddr
* out_addr
);
155 bool IsEquivalentAddress(PP_Resource addr1
, PP_Resource addr2
);
157 virtual Error
SetSockOptSocket(int opname
, const void* optval
, socklen_t len
);
159 virtual Error
SetSockOptTCP(int optname
, const void* optval
, socklen_t len
);
161 virtual Error
SetSockOptIP(int optname
, const void* optval
, socklen_t len
);
163 virtual Error
SetSockOptIPV6(int optname
, const void* optval
, socklen_t len
);
166 PP_Resource socket_resource_
;
167 PP_Resource local_addr_
;
168 PP_Resource remote_addr_
;
169 uint32_t socket_flags_
;
172 struct linger linger_
;
174 friend class KernelProxy
;
175 friend class StreamFs
;
178 } // namespace nacl_io
180 #endif // PROVIDES_SOCKET_API
181 #endif // LIBRARIES_NACL_IO_SOCKET_SOCKET_NODE_H_