2018-12-15 01:28:11 +01:00
|
|
|
/*
|
|
|
|
* IXDNSLookup.h
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*
|
2019-02-21 03:59:07 +01:00
|
|
|
* Resolve a hostname+port to a struct addrinfo obtained with getaddrinfo
|
2018-12-15 01:28:11 +01:00
|
|
|
* Does this in a background thread so that it can be cancelled, since
|
|
|
|
* getaddrinfo is a blocking call, and we don't want to block the main thread on Mobile.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "IXCancellationRequest.h"
|
|
|
|
#include <atomic>
|
2023-02-25 22:50:35 +01:00
|
|
|
#include <cstdint>
|
2019-08-13 19:59:18 +02:00
|
|
|
#include <memory>
|
2019-06-27 04:12:48 +02:00
|
|
|
#include <mutex>
|
2018-12-15 02:49:42 +01:00
|
|
|
#include <set>
|
2019-05-30 17:46:50 +02:00
|
|
|
#include <string>
|
2018-12-15 01:28:11 +01:00
|
|
|
|
|
|
|
struct addrinfo;
|
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
namespace ix
|
2018-12-15 01:28:11 +01:00
|
|
|
{
|
2019-06-19 09:43:59 +02:00
|
|
|
class DNSLookup : public std::enable_shared_from_this<DNSLookup>
|
2019-05-30 17:46:50 +02:00
|
|
|
{
|
2018-12-15 01:28:11 +01:00
|
|
|
public:
|
2022-12-23 02:13:51 +01:00
|
|
|
using AddrInfoPtr = std::shared_ptr<addrinfo>;
|
2019-05-30 17:46:50 +02:00
|
|
|
DNSLookup(const std::string& hostname, int port, int64_t wait = DNSLookup::kDefaultWait);
|
2019-06-19 09:43:59 +02:00
|
|
|
~DNSLookup() = default;
|
2018-12-15 01:28:11 +01:00
|
|
|
|
2022-12-23 02:13:51 +01:00
|
|
|
AddrInfoPtr resolve(std::string& errMsg,
|
2018-12-15 01:28:11 +01:00
|
|
|
const CancellationRequest& isCancellationRequested,
|
2019-07-01 08:26:14 +02:00
|
|
|
bool cancellable = true);
|
2018-12-15 01:28:11 +01:00
|
|
|
|
|
|
|
private:
|
2022-12-23 02:13:51 +01:00
|
|
|
AddrInfoPtr resolveCancellable(std::string& errMsg,
|
2019-07-01 08:26:14 +02:00
|
|
|
const CancellationRequest& isCancellationRequested);
|
2022-12-23 02:13:51 +01:00
|
|
|
AddrInfoPtr resolveUnCancellable(std::string& errMsg,
|
2019-07-01 08:26:14 +02:00
|
|
|
const CancellationRequest& isCancellationRequested);
|
2018-12-15 01:28:11 +01:00
|
|
|
|
2022-12-23 02:13:51 +01:00
|
|
|
AddrInfoPtr getAddrInfo(const std::string& hostname,
|
2018-12-15 01:28:11 +01:00
|
|
|
int port,
|
|
|
|
std::string& errMsg);
|
|
|
|
|
2019-06-27 01:25:07 +02:00
|
|
|
void run(std::weak_ptr<DNSLookup> self, std::string hostname, int port); // thread runner
|
2019-04-30 04:29:27 +02:00
|
|
|
|
|
|
|
void setErrMsg(const std::string& errMsg);
|
|
|
|
const std::string& getErrMsg();
|
|
|
|
|
2022-12-23 02:13:51 +01:00
|
|
|
void setRes(AddrInfoPtr addr);
|
|
|
|
AddrInfoPtr getRes();
|
2018-12-15 01:28:11 +01:00
|
|
|
|
|
|
|
std::string _hostname;
|
|
|
|
int _port;
|
2018-12-23 23:14:38 +01:00
|
|
|
int64_t _wait;
|
2022-04-12 17:55:43 +02:00
|
|
|
const static int64_t kDefaultWait;
|
2019-04-30 04:29:27 +02:00
|
|
|
|
2022-12-23 02:13:51 +01:00
|
|
|
AddrInfoPtr _res;
|
2019-04-30 04:29:27 +02:00
|
|
|
std::mutex _resMutex;
|
|
|
|
|
|
|
|
std::string _errMsg;
|
|
|
|
std::mutex _errMsgMutex;
|
2018-12-15 01:28:11 +01:00
|
|
|
|
|
|
|
std::atomic<bool> _done;
|
|
|
|
};
|
2019-05-30 17:46:50 +02:00
|
|
|
} // namespace ix
|