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::instance_ = NULL; | |
Evgeniy Stepanov
2012/05/25 12:05:57
unused
vissi
2012/05/25 13:07:09
No, it's used by GetSocketSubSystem() referenced i
Evgeniy Stepanov
2012/05/25 13:19:51
Pass pp::Instance through TCPSocket constructor.
vissi
2012/05/25 14:32:06
They use the same condition variable. Should they?
vissi
2012/05/25 14:54:08
Per offline discussion, we decided that it would b
| |
20 | |
21 SocketSubSystem::SocketSubSystem(pp::Instance* instance) | |
22 : pp_instance_(instance) | |
23 , first_unused_addr_(kFirstAddr) { | |
24 AddHostAddress("localhost", 0x7F000001); | |
25 instance_ = this; | |
26 } | |
27 | |
28 void SocketSubSystem::AddHostAddress(const char* name, unsigned long addr) { | |
29 addr = htonl(addr); | |
30 hosts_[name] = addr; | |
31 addrs_[addr] = name; | |
32 } | |
33 | |
34 unsigned long SocketSubSystem::gethostbyname(const char* name) { | |
35 Mutex::Lock lock(mutex_); | |
36 HostMap::iterator it = hosts_.find(name); | |
37 if (it != hosts_.end()) | |
38 return it->second; | |
39 | |
40 int addr = htonl(first_unused_addr_++); | |
41 hosts_[name] = addr; | |
42 addrs_[addr] = name; | |
43 return addr; | |
44 } | |
45 | |
46 int SocketSubSystem::bind(FileStream** stream, unsigned long addr, | |
47 unsigned short port) { | |
48 Mutex::Lock lock(mutex_); | |
49 | |
50 std::string host; | |
51 AddressMap::iterator it = addrs_.find(addr); | |
52 if (it != addrs_.end()) { | |
53 host = it->second; | |
54 } else { | |
55 in_addr iaddr; | |
56 iaddr.s_addr = addr; | |
57 host = inet_ntoa(iaddr); | |
58 } | |
59 | |
60 *stream = new TCPServerSocket(0, host.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 std::string host; | |
69 AddressMap::iterator it = addrs_.find(addr); | |
Evgeniy Stepanov
2012/05/25 12:05:57
factor this piece out
vissi
2012/05/25 13:07:09
Why?
Evgeniy Stepanov
2012/05/25 13:19:51
I mean, make it a function. It's repeated twice in
vissi
2012/05/25 14:32:06
Done.
| |
70 if (it != addrs_.end()) { | |
71 host = it->second; | |
72 } else { | |
73 in_addr iaddr; | |
74 iaddr.s_addr = addr; | |
75 host = inet_ntoa(iaddr); | |
76 } | |
77 | |
78 FileStream* new_stream = NULL; | |
79 TCPSocket* socket = new TCPSocket(O_RDWR); | |
80 if (!socket->connect(host.c_str(), port)) { | |
81 errno = ECONNREFUSED; | |
82 socket->release(); | |
83 return -1; | |
84 } | |
85 new_stream = socket; | |
86 | |
87 *stream = new_stream; | |
88 return 0; | |
89 } | |
90 | |
91 SocketSubSystem::~SocketSubSystem() { | |
92 } | |
93 | |
94 int SocketSubSystem::shutdown(FileStream* stream, int how) { | |
95 Mutex::Lock lock(mutex_); | |
96 if (stream && stream != kBadFileStream) { | |
97 // Actually shutdown should be something more complicated by for now | |
98 // it works. Method close can be called multiple time. | |
99 stream->close(); | |
100 return 0; | |
101 } else { | |
102 errno = EBADF; | |
103 return -1; | |
104 } | |
105 } | |
106 | |
107 int SocketSubSystem::close(FileStream* stream) { | |
108 Mutex::Lock lock(mutex_); | |
109 | |
110 if (stream && stream != kBadFileStream) { | |
Evgeniy Stepanov
2012/05/25 12:05:57
could we get rid of this constant?
vissi
2012/05/25 13:07:09
yup.
| |
111 stream->close(); | |
112 stream->release(); | |
113 } | |
114 return 0; | |
115 } | |
116 | |
117 int SocketSubSystem::read(FileStream* stream, char* buf, size_t count, | |
118 size_t* nread) { | |
119 Mutex::Lock lock(mutex_); | |
120 if (stream && stream != kBadFileStream) | |
Evgeniy Stepanov
2012/05/25 12:05:57
move this check to KernelProxy, assume that stream
vissi
2012/05/25 13:07:09
Haven't moved this yet. We check if FileHandle is
| |
121 return stream->read(buf, count, nread); | |
122 else | |
123 return EBADF; | |
124 } | |
125 | |
126 int SocketSubSystem::write(FileStream* stream, const char* buf, size_t count, | |
127 size_t* nwrote) { | |
128 Mutex::Lock lock(mutex_); | |
129 if (stream && stream != kBadFileStream) | |
130 return stream->write(buf, count, nwrote); | |
131 else | |
132 return EBADF; | |
133 } | |
134 | |
135 int SocketSubSystem::seek(FileStream* stream, nacl_abi_off_t offset, int whence, | |
136 nacl_abi_off_t* new_offset) { | |
137 Mutex::Lock lock(mutex_); | |
138 if (stream && stream != kBadFileStream) | |
139 return stream->seek(offset, whence, new_offset); | |
140 else | |
141 return EBADF; | |
142 } | |
143 | |
144 int SocketSubSystem::fstat(FileStream* stream, nacl_abi_stat* out) { | |
145 Mutex::Lock lock(mutex_); | |
146 if (stream && stream != kBadFileStream) | |
147 return stream->fstat(out); | |
148 else | |
149 return EBADF; | |
150 } | |
151 | |
152 int SocketSubSystem::fcntl(FileStream* stream, int cmd, va_list ap) { | |
153 Mutex::Lock lock(mutex_); | |
154 if (stream && stream != kBadFileStream) { | |
155 return stream->fcntl(cmd, ap); | |
156 } else { | |
157 errno = EBADF; | |
158 return -1; | |
159 } | |
160 } | |
161 | |
162 int SocketSubSystem::ioctl(FileStream* stream, int request, va_list ap) { | |
163 Mutex::Lock lock(mutex_); | |
164 if (stream && stream != kBadFileStream) { | |
165 return stream->ioctl(request, ap); | |
166 } else { | |
167 errno = EBADF; | |
168 return -1; | |
169 } | |
170 } | |
171 | |
172 int SocketSubSystem::listen(FileStream* stream, int backlog) { | |
173 Mutex::Lock lock(mutex_); | |
174 if (stream && stream != kBadFileStream) { | |
175 if (static_cast<TCPServerSocket*>(stream)->listen(backlog)) { | |
176 return 0; | |
177 } else { | |
178 errno = EACCES; | |
179 return -1; | |
180 } | |
181 } else { | |
182 errno = EBADF; | |
183 return -1; | |
184 } | |
185 } | |
186 | |
187 FileStream* SocketSubSystem::accept(FileStream* stream, struct sockaddr *addr, | |
188 socklen_t* addrlen) { | |
189 Mutex::Lock lock(mutex_); | |
190 if (stream && stream != kBadFileStream) { | |
191 PP_Resource resource = static_cast<TCPServerSocket*>(stream)->accept(); | |
192 if (resource) { | |
193 TCPSocket* socket = new TCPSocket(O_RDWR); | |
194 if (socket->accept(resource)) { | |
195 return socket; | |
196 } else { | |
197 socket->release(); | |
198 } | |
199 } | |
200 errno = EINVAL; | |
201 return 0; | |
202 } else { | |
203 errno = EBADF; | |
204 return 0; | |
205 } | |
206 } | |
207 | |
OLD | NEW |