1 // Copyright 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SANDBOXED_API_SANDBOX2_EXAMPLES_NETWORK_PROXY_NETWORKPROXY_LIB_H_ 16 #define SANDBOXED_API_SANDBOX2_EXAMPLES_NETWORK_PROXY_NETWORKPROXY_LIB_H_ 17 18 #include <memory> 19 #include <thread> 20 #include <utility> 21 22 #include "absl/status/statusor.h" 23 #include "sandboxed_api/util/fileops.h" 24 25 namespace sandbox2 { 26 27 class NetworkProxyTestServer { 28 public: 29 static absl::StatusOr<std::unique_ptr<NetworkProxyTestServer>> Start( 30 bool ipv6 = true); 31 32 NetworkProxyTestServer(NetworkProxyTestServer&&) = delete; 33 NetworkProxyTestServer& operator=(NetworkProxyTestServer&&) = delete; 34 ~NetworkProxyTestServer()35 ~NetworkProxyTestServer() { Stop(); } 36 port()37 int port() { return port_; } 38 void Stop(); 39 40 private: NetworkProxyTestServer(int port,sapi::file_util::fileops::FDCloser server_socket,sapi::file_util::fileops::FDCloser event_fd)41 NetworkProxyTestServer(int port, 42 sapi::file_util::fileops::FDCloser server_socket, 43 sapi::file_util::fileops::FDCloser event_fd) 44 : port_(port), 45 server_socket_(std::move(server_socket)), 46 event_fd_(std::move(event_fd)) {} 47 void Spawn(); 48 void Run(); 49 std::thread thread_; 50 int port_; 51 sapi::file_util::fileops::FDCloser server_socket_; 52 sapi::file_util::fileops::FDCloser event_fd_; 53 }; 54 55 } // namespace sandbox2 56 57 #endif // SANDBOXED_API_SANDBOX2_EXAMPLES_NETWORK_PROXY_NETWORKPROXY_LIB_H_ 58