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