Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / libraries / nacl_io / socket / socket_node.h
blobf6f15418c7e674887f10c449e7a57182fd6b7123
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"
21 namespace nacl_io {
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;
27 class StreamFs;
28 class SocketNode;
29 typedef sdk_util::ScopedRef<SocketNode> ScopedSocketNode;
31 class SocketNode : public StreamNode {
32 public:
33 explicit SocketNode(Filesystem* filesystem);
34 SocketNode(Filesystem* filesystem, PP_Resource socket);
36 protected:
37 virtual void Destroy();
39 public:
40 // Normal read/write operations on a file (recv/send).
41 virtual Error Read(const HandleAttr& attr,
42 void* buf,
43 size_t count,
44 int* out_bytes);
45 virtual Error Write(const HandleAttr& attr,
46 const void* buf,
47 size_t count,
48 int* out_bytes);
50 virtual Error GetSockOpt(int lvl, int optname, void* optval, socklen_t* len);
51 virtual Error SetSockOpt(int lvl,
52 int optname,
53 const void* optval,
54 socklen_t len);
56 // Unsupported Functions
57 virtual Error MMap(void* addr,
58 size_t length,
59 int prot,
60 int flags,
61 size_t offset,
62 void** out_addr);
64 // Socket Functions.
65 virtual Error Accept(const HandleAttr& attr,
66 PP_Resource* new_sock,
67 struct sockaddr* addr,
68 socklen_t* len);
69 virtual Error Bind(const struct sockaddr* addr, socklen_t len);
70 virtual Error Connect(const HandleAttr& attr,
71 const struct sockaddr* addr,
72 socklen_t len);
73 virtual Error Listen(int backlog);
74 virtual Error Recv(const HandleAttr& attr,
75 void* buf,
76 size_t len,
77 int flags,
78 int* out_len);
79 virtual Error RecvFrom(const HandleAttr& attr,
80 void* buf,
81 size_t len,
82 int flags,
83 struct sockaddr* src_addr,
84 socklen_t* addrlen,
85 int* out_len);
86 virtual Error Send(const HandleAttr& attr,
87 const void* buf,
88 size_t len,
89 int flags,
90 int* out_len);
91 virtual Error SendTo(const HandleAttr& attr,
92 const void* buf,
93 size_t len,
94 int flags,
95 const struct sockaddr* dest_addr,
96 socklen_t addrlen,
97 int* out_len);
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);
110 protected:
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,
116 const void* buf,
117 size_t len,
118 int flags,
119 PP_Resource addr,
120 int* out_len);
122 // Wraps common error checks, timeouts, work pump for recv.
123 Error RecvHelper(const HandleAttr& attr,
124 void* buf,
125 size_t len,
126 int flags,
127 PP_Resource* addr,
128 int* out_len);
130 // Per socket type send and recv
131 virtual Error Recv_Locked(void* buffer,
132 size_t len,
133 PP_Resource* out_addr,
134 int* out_len) = 0;
136 virtual Error Send_Locked(const void* buffer,
137 size_t len,
138 PP_Resource addr,
139 int* out_len) = 0;
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,
152 socklen_t len,
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);
165 protected:
166 PP_Resource socket_resource_;
167 PP_Resource local_addr_;
168 PP_Resource remote_addr_;
169 uint32_t socket_flags_;
170 int last_errno_;
171 bool keep_alive_;
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_