#ifndef LIBRARIES_NACL_IO_SOCKET_SOCKET_NODE_H_
#define LIBRARIES_NACL_IO_SOCKET_SOCKET_NODE_H_
#include "nacl_io/ossocket.h"
#ifdef PROVIDES_SOCKET_API
#include <sys/fcntl.h>
#include <ppapi/c/pp_errors.h>
#include <ppapi/c/pp_resource.h>
#include <ppapi/c/ppb_net_address.h>
#include "nacl_io/filesystem.h"
#include "nacl_io/node.h"
#include "nacl_io/pepper_interface.h"
#include "nacl_io/stream/stream_node.h"
namespace nacl_io {
static const size_t MAX_SOCK_TRANSFER = 65536;
class StreamFs;
class SocketNode;
typedef sdk_util::ScopedRef<SocketNode> ScopedSocketNode;
class SocketNode : public StreamNode {
public:
explicit SocketNode(Filesystem* filesystem);
SocketNode(Filesystem* filesystem, PP_Resource socket);
protected:
virtual void Destroy();
public:
virtual Error Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes);
virtual Error Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes);
virtual Error GetSockOpt(int lvl, int optname, void* optval, socklen_t* len);
virtual Error SetSockOpt(int lvl,
int optname,
const void* optval,
socklen_t len);
virtual Error MMap(void* addr,
size_t length,
int prot,
int flags,
size_t offset,
void** out_addr);
virtual Error Accept(const HandleAttr& attr,
PP_Resource* new_sock,
struct sockaddr* addr,
socklen_t* len);
virtual Error Bind(const struct sockaddr* addr, socklen_t len);
virtual Error Connect(const HandleAttr& attr,
const struct sockaddr* addr,
socklen_t len);
virtual Error Listen(int backlog);
virtual Error Recv(const HandleAttr& attr,
void* buf,
size_t len,
int flags,
int* out_len);
virtual Error RecvFrom(const HandleAttr& attr,
void* buf,
size_t len,
int flags,
struct sockaddr* src_addr,
socklen_t* addrlen,
int* out_len);
virtual Error Send(const HandleAttr& attr,
const void* buf,
size_t len,
int flags,
int* out_len);
virtual Error SendTo(const HandleAttr& attr,
const void* buf,
size_t len,
int flags,
const struct sockaddr* dest_addr,
socklen_t addrlen,
int* out_len);
virtual Error Shutdown(int how);
virtual Error GetPeerName(struct sockaddr* addr, socklen_t* len);
virtual Error GetSockName(struct sockaddr* addr, socklen_t* len);
PP_Resource socket_resource() { return socket_resource_; }
PP_Resource remote_addr() { return remote_addr_; }
virtual void SetError_Locked(int pp_error_num);
protected:
bool IsBound() { return local_addr_ != 0; }
bool IsConnected() { return remote_addr_ != 0; }
Error SendHelper(const HandleAttr& attr,
const void* buf,
size_t len,
int flags,
PP_Resource addr,
int* out_len);
Error RecvHelper(const HandleAttr& attr,
void* buf,
size_t len,
int flags,
PP_Resource* addr,
int* out_len);
virtual Error Recv_Locked(void* buffer,
size_t len,
PP_Resource* out_addr,
int* out_len) = 0;
virtual Error Send_Locked(const void* buffer,
size_t len,
PP_Resource addr,
int* out_len) = 0;
NetAddressInterface* NetInterface();
TCPSocketInterface* TCPInterface();
UDPSocketInterface* UDPInterface();
PP_Resource SockAddrToResource(const struct sockaddr* addr, socklen_t len);
socklen_t ResourceToSockAddr(PP_Resource addr,
socklen_t len,
struct sockaddr* out_addr);
bool IsEquivalentAddress(PP_Resource addr1, PP_Resource addr2);
protected:
PP_Resource socket_resource_;
PP_Resource local_addr_;
PP_Resource remote_addr_;
uint32_t socket_flags_;
int last_errno_;
bool keep_alive_;
struct linger linger_;
friend class KernelProxy;
friend class StreamFs;
};
}
#endif
#endif