1 // Copyright 2021 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_DNS_PUBLIC_SCOPED_RES_STATE_H_ 6 #define NET_DNS_PUBLIC_SCOPED_RES_STATE_H_ 7 8 #include <resolv.h> 9 10 #include "build/build_config.h" 11 #include "net/base/net_export.h" 12 #include "third_party/abseil-cpp/absl/types/optional.h" 13 14 namespace net { 15 16 // Helper class to open, read and close a __res_state. 17 class NET_EXPORT ScopedResState { 18 public: 19 // This constructor will call memset and res_init/res_ninit a __res_state, and 20 // store the result in `res_init_result_`. 21 ScopedResState(); 22 23 // Calls res_ndestroy or res_nclose if the platform uses `res_`. 24 virtual ~ScopedResState(); 25 26 // Returns true iff a __res_state was initialized successfully. 27 // Other methods in this class shouldn't be called if it returns false. 28 bool IsValid() const; 29 30 // Access the __res_state used by this class to compute other values. 31 virtual const struct __res_state& state() const; 32 33 private: 34 #if !BUILDFLAG(IS_OPENBSD) && !BUILDFLAG(IS_FUCHSIA) 35 struct __res_state res_; 36 #endif // !BUILDFLAG(IS_OPENBSD) && !BUILDFLAG(IS_FUCHSIA) 37 38 int res_init_result_ = -1; 39 }; 40 41 } // namespace net 42 43 #endif // NET_DNS_PUBLIC_SCOPED_RES_STATE_H_ 44