
/*---------------------------------------------------------------------------*/
/* dns.h                                                                     */
/* copyright (c) innovaphone 2015                                            */
/*                                                                           */
/*---------------------------------------------------------------------------*/

class DnsExpiryTimer : public ITimer {
public:
    DnsExpiryTimer(IIoMux * ioMux, UTimerSubclass<DnsExpiryTimer> * owner) : ITimer(ioMux, owner) {};
};

class Dns : public IDns, public UIoExec, public UIoContext, public UTimerSubclass<DnsExpiryTimer> {
protected:
    IIoMux * const iomux;
    unsigned responseCounter;

    void TimerOnTimeout(DnsExpiryTimer * timer);
    inline void UserDnsGetHostByNameResult(UDns * user, IInstanceLog * const log, const char * addr, bool isIPv6);
    inline void UserDnsQueryResult(UDns * user, IInstanceLog * const log, const byte * answer, unsigned length);

public:
    Dns(IIoMux * iomux);
    virtual ~Dns();
    void GetHostByName(const char * hostName, class UDns * const user, IInstanceLog * const log, dword timeout, dword flags, word servicePort, const char * serviceName) override;
    void UnsuitableAddress(const char * hostName, class IInstanceLog * const log, const char * addr, word servicePort, const char * serviceName) override;
    void Query(const char * hostName, class UDns * const user, class IInstanceLog * const log, dns_query_type_sdk queryType) override;
    void IoContextNotify(bool indRX, bool indTX);
    void IoExec(void * execContext) override;
    void UpdateNextTimeout(dword t);

    class DnsThread * thread;
    dword nextTimeout;
    DnsExpiryTimer expiryTimer;
    int pipefd[2];
    bool pipeCreated;
};
