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 std::string host; |
| 45 AddressMap::iterator it = addrs_.find(addr); |
| 46 if (it != addrs_.end()) { |
| 47 host = it->second; |
| 48 } else { |
| 49 in_addr iaddr; |
| 50 iaddr.s_addr = addr; |
| 51 host = inet_ntoa(iaddr); |
| 52 } |
| 53 return host; |
| 54 } |
| 55 |
| 56 int SocketSubSystem::bind(FileStream** stream, unsigned long addr, |
| 57 unsigned short port) { |
| 58 Mutex::Lock lock(mutex_); |
| 59 |
| 60 *stream = new TCPServerSocket(this, 0, GetHostByAddr(addr).c_str(), port); |
| 61 return 0; |
| 62 } |
| 63 |
| 64 int SocketSubSystem::connect(FileStream** stream, unsigned long addr, |
| 65 unsigned short port) { |
| 66 Mutex::Lock lock(mutex_); |
| 67 |
| 68 FileStream* new_stream = NULL; |
| 69 TCPSocket* socket = new TCPSocket(this, O_RDWR); |
| 70 if (!socket->connect(GetHostByAddr(addr).c_str(), port)) { |
| 71 errno = ECONNREFUSED; |
| 72 socket->release(); |
| 73 return -1; |
| 74 } |
| 75 new_stream = socket; |
| 76 |
| 77 *stream = new_stream; |
| 78 return 0; |
| 79 } |
| 80 |
| 81 SocketSubSystem::~SocketSubSystem() { |
| 82 } |
| 83 |
| 84 int SocketSubSystem::shutdown(FileStream* stream, int how) { |
| 85 Mutex::Lock lock(mutex_); |
| 86 if (stream) { |
| 87 // Actually shutdown should be something more complicated by for now |
| 88 // it works. Method close can be called multiple time. |
| 89 stream->close(); |
| 90 return 0; |
| 91 } else { |
| 92 errno = EBADF; |
| 93 return -1; |
| 94 } |
| 95 } |
| 96 |
| 97 int SocketSubSystem::close(FileStream* stream) { |
| 98 Mutex::Lock lock(mutex_); |
| 99 |
| 100 if (stream) { |
| 101 stream->close(); |
| 102 stream->release(); |
| 103 } |
| 104 return 0; |
| 105 } |
| 106 |
| 107 int SocketSubSystem::read(FileStream* stream, char* buf, size_t count, |
| 108 size_t* nread) { |
| 109 Mutex::Lock lock(mutex_); |
| 110 if (stream) |
| 111 return stream->read(buf, count, nread); |
| 112 else |
| 113 return EBADF; |
| 114 } |
| 115 |
| 116 int SocketSubSystem::write(FileStream* stream, const char* buf, size_t count, |
| 117 size_t* nwrote) { |
| 118 Mutex::Lock lock(mutex_); |
| 119 if (stream) |
| 120 return stream->write(buf, count, nwrote); |
| 121 else |
| 122 return EBADF; |
| 123 } |
| 124 |
| 125 int SocketSubSystem::seek(FileStream* stream, nacl_abi_off_t offset, int whence, |
| 126 nacl_abi_off_t* new_offset) { |
| 127 Mutex::Lock lock(mutex_); |
| 128 if (stream) |
| 129 return stream->seek(offset, whence, new_offset); |
| 130 else |
| 131 return EBADF; |
| 132 } |
| 133 |
| 134 int SocketSubSystem::fstat(FileStream* stream, nacl_abi_stat* out) { |
| 135 Mutex::Lock lock(mutex_); |
| 136 if (stream) |
| 137 return stream->fstat(out); |
| 138 else |
| 139 return EBADF; |
| 140 } |
| 141 |
| 142 int SocketSubSystem::fcntl(FileStream* stream, int cmd, va_list ap) { |
| 143 Mutex::Lock lock(mutex_); |
| 144 if (stream) { |
| 145 return stream->fcntl(cmd, ap); |
| 146 } else { |
| 147 errno = EBADF; |
| 148 return -1; |
| 149 } |
| 150 } |
| 151 |
| 152 int SocketSubSystem::ioctl(FileStream* stream, int request, va_list ap) { |
| 153 Mutex::Lock lock(mutex_); |
| 154 if (stream) { |
| 155 return stream->ioctl(request, ap); |
| 156 } else { |
| 157 errno = EBADF; |
| 158 return -1; |
| 159 } |
| 160 } |
| 161 |
| 162 int SocketSubSystem::listen(FileStream* stream, int backlog) { |
| 163 Mutex::Lock lock(mutex_); |
| 164 if (stream) { |
| 165 if (static_cast<TCPServerSocket*>(stream)->listen(backlog)) { |
| 166 return 0; |
| 167 } else { |
| 168 errno = EACCES; |
| 169 return -1; |
| 170 } |
| 171 } else { |
| 172 errno = EBADF; |
| 173 return -1; |
| 174 } |
| 175 } |
| 176 |
| 177 FileStream* SocketSubSystem::accept(FileStream* stream, struct sockaddr *addr, |
| 178 socklen_t* addrlen) { |
| 179 Mutex::Lock lock(mutex_); |
| 180 if (stream) { |
| 181 PP_Resource resource = static_cast<TCPServerSocket*>(stream)->accept(); |
| 182 if (resource) { |
| 183 TCPSocket* socket = new TCPSocket(this, O_RDWR); |
| 184 if (socket->acceptFrom(resource)) { |
| 185 return socket; |
| 186 } else { |
| 187 socket->release(); |
| 188 } |
| 189 } |
| 190 errno = EINVAL; |
| 191 return 0; |
| 192 } else { |
| 193 errno = EBADF; |
| 194 return 0; |
| 195 } |
| 196 } |
| 197 |
OLD | NEW |