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