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 <optional> 11 12 #include "build/build_config.h" 13 #include "net/base/net_export.h" 14 15 namespace net { 16 17 // Helper class to open, read and close a __res_state. 18 class NET_EXPORT ScopedResState { 19 public: 20 // This constructor will call memset and res_init/res_ninit a __res_state, and 21 // store the result in `res_init_result_`. 22 ScopedResState(); 23 24 // Calls res_ndestroy or res_nclose if the platform uses `res_`. 25 virtual ~ScopedResState(); 26 27 // Returns true iff a __res_state was initialized successfully. 28 // Other methods in this class shouldn't be called if it returns false. 29 bool IsValid() const; 30 31 // Access the __res_state used by this class to compute other values. 32 virtual const struct __res_state& state() const; 33 34 private: 35 #if !BUILDFLAG(IS_OPENBSD) && !BUILDFLAG(IS_FUCHSIA) 36 struct __res_state res_; 37 #endif // !BUILDFLAG(IS_OPENBSD) && !BUILDFLAG(IS_FUCHSIA) 38 39 int res_init_result_ = -1; 40 }; 41 42 } // namespace net 43 44 #endif // NET_DNS_PUBLIC_SCOPED_RES_STATE_H_ 45