OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium OS 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. |
| 4 |
| 5 #include <arpa/inet.h> |
| 6 #include <irt.h> |
| 7 #include <netinet/in.h> |
| 8 #include <signal.h> |
| 9 #include <string.h> |
| 10 #include <sys/socket.h> |
| 11 |
| 12 #include "net/SocketSubSystem.h" |
| 13 #include "net/TcpServerSocket.h" |
| 14 #include "net/TcpSocket.h" |
| 15 #include "ppapi/cpp/file_ref.h" |
| 16 #include "util/DebugPrint.h" |
| 17 |
| 18 |
| 19 SocketSubSystem::SocketSubSystem(pp::Instance* instance) |
| 20 : pp_instance_(instance) |
| 21 , first_unused_addr_(kFirstAddr) { |
| 22 AddHostAddress("localhost", 0x7F000001); |
| 23 } |
| 24 |
| 25 void SocketSubSystem::AddHostAddress(const char* name, unsigned long addr) { |
| 26 addr = htonl(addr); |
| 27 hosts_[name] = addr; |
| 28 addrs_[addr] = name; |
| 29 } |
| 30 |
| 31 unsigned long SocketSubSystem::gethostbyname(const char* name) { |
| 32 Mutex::Lock lock(mutex_); |
| 33 HostMap::iterator it = hosts_.find(name); |
| 34 if (it != hosts_.end()) |
| 35 return it->second; |
| 36 |
| 37 int addr = htonl(first_unused_addr_++); |
| 38 hosts_[name] = addr; |
| 39 addrs_[addr] = name; |
| 40 return addr; |
| 41 } |
| 42 |
| 43 std::string SocketSubSystem::GetHostByAddr(unsigned long addr) { |
| 44 Mutex::Lock lock(mutex_); |
| 45 std::string host; |
| 46 AddressMap::iterator it = addrs_.find(addr); |
| 47 if (it != addrs_.end()) { |
| 48 host = it->second; |
| 49 } else { |
| 50 in_addr iaddr; |
| 51 iaddr.s_addr = addr; |
| 52 host = inet_ntoa(iaddr); |
| 53 } |
| 54 return host; |
| 55 } |
| 56 |
| 57 int SocketSubSystem::bind(FileStream** stream, unsigned long addr, |
| 58 unsigned short port) { |
| 59 *stream = new TCPServerSocket(this, 0, GetHostByAddr(addr).c_str(), port); |
| 60 return 0; |
| 61 } |
| 62 |
| 63 int SocketSubSystem::connect(FileStream** stream, unsigned long addr, |
| 64 unsigned short port) { |
| 65 FileStream* new_stream = NULL; |
| 66 TCPSocket* socket = new TCPSocket(this, O_RDWR); |
| 67 if (!socket->connect(GetHostByAddr(addr).c_str(), port)) { |
| 68 errno = ECONNREFUSED; |
| 69 socket->release(); |
| 70 return -1; |
| 71 } |
| 72 new_stream = socket; |
| 73 |
| 74 *stream = new_stream; |
| 75 return 0; |
| 76 } |
| 77 |
| 78 SocketSubSystem::~SocketSubSystem() { |
| 79 } |
| 80 |
| 81 int SocketSubSystem::shutdown(FileStream* stream, int how) { |
| 82 if (stream) { |
| 83 // Actually shutdown should be something more complicated by for now |
| 84 // it works. Method close can be called multiple time. |
| 85 stream->close(); |
| 86 return 0; |
| 87 } else { |
| 88 errno = EBADF; |
| 89 return -1; |
| 90 } |
| 91 } |
| 92 |
| 93 int SocketSubSystem::close(FileStream* stream) { |
| 94 if (stream) { |
| 95 stream->close(); |
| 96 stream->release(); |
| 97 } |
| 98 return 0; |
| 99 } |
| 100 |
| 101 int SocketSubSystem::read(FileStream* stream, char* buf, size_t count, |
| 102 size_t* nread) { |
| 103 if (stream) |
| 104 return stream->read(buf, count, nread); |
| 105 else |
| 106 return EBADF; |
| 107 } |
| 108 |
| 109 int SocketSubSystem::write(FileStream* stream, const char* buf, size_t count, |
| 110 size_t* nwrote) { |
| 111 if (stream) |
| 112 return stream->write(buf, count, nwrote); |
| 113 else |
| 114 return EBADF; |
| 115 } |
| 116 |
| 117 int SocketSubSystem::seek(FileStream* stream, nacl_abi_off_t offset, int whence, |
| 118 nacl_abi_off_t* new_offset) { |
| 119 if (stream) |
| 120 return stream->seek(offset, whence, new_offset); |
| 121 else |
| 122 return EBADF; |
| 123 } |
| 124 |
| 125 int SocketSubSystem::fstat(FileStream* stream, nacl_abi_stat* out) { |
| 126 if (stream) |
| 127 return stream->fstat(out); |
| 128 else |
| 129 return EBADF; |
| 130 } |
| 131 |
| 132 int SocketSubSystem::fcntl(FileStream* stream, int cmd, va_list ap) { |
| 133 if (stream) { |
| 134 return stream->fcntl(cmd, ap); |
| 135 } else { |
| 136 errno = EBADF; |
| 137 return -1; |
| 138 } |
| 139 } |
| 140 |
| 141 int SocketSubSystem::ioctl(FileStream* stream, int request, va_list ap) { |
| 142 if (stream) { |
| 143 return stream->ioctl(request, ap); |
| 144 } else { |
| 145 errno = EBADF; |
| 146 return -1; |
| 147 } |
| 148 } |
| 149 |
| 150 int SocketSubSystem::listen(FileStream* stream, int backlog) { |
| 151 if (stream) { |
| 152 if (static_cast<TCPServerSocket*>(stream)->listen(backlog)) { |
| 153 return 0; |
| 154 } else { |
| 155 errno = EACCES; |
| 156 return -1; |
| 157 } |
| 158 } else { |
| 159 errno = EBADF; |
| 160 return -1; |
| 161 } |
| 162 } |
| 163 |
| 164 FileStream* SocketSubSystem::accept(FileStream* stream, struct sockaddr *addr, |
| 165 socklen_t* addrlen) { |
| 166 if (stream) { |
| 167 PP_Resource resource = static_cast<TCPServerSocket*>(stream)->accept(); |
| 168 if (resource) { |
| 169 TCPSocket* socket = new TCPSocket(this, O_RDWR); |
| 170 if (socket->acceptFrom(resource)) { |
| 171 return socket; |
| 172 } else { |
| 173 socket->release(); |
| 174 } |
| 175 } |
| 176 errno = EINVAL; |
| 177 return 0; |
| 178 } else { |
| 179 errno = EBADF; |
| 180 return 0; |
| 181 } |
| 182 } |
| 183 |
OLD | NEW |