2017-02-01 7 views
0

静的ライブラリ(どこでも使える)を使って、高速なDNSリゾルバをC++で構築しようとしています。これまでのところ、私のバージョンではgethostbynameが動作しないサーバーがありましたが、これは動作しません。私はA non-recoverable name server error occurred (NO_RECOVERY)です。また、コンパイルするとき、我々はこの警告を得ている:C++の静的DNSリゾルバ

Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

#include <stdio.h> 
#include <cstring> 

// for dom 
#include <netdb.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 


int main(int argc, char *argv[]) 
{ 
    char* host = "google.com"; 
    int debugLevel = 5; 
    char* ip_of_domain; 
    struct hostent *he; 

    he = gethostbyname(host); 

    if (he == NULL) 
    { 
     switch(h_errno) 
     { 
      case HOST_NOT_FOUND: 
       if(debugLevel >= 3) puts("The host was not found."); 
       break; 
      case NO_ADDRESS: 
       if(debugLevel >= 3) puts("The name is valid but it has no address."); 
       break; 
      case NO_RECOVERY: 
       if(debugLevel >= 3) puts("A non-recoverable name server error occurred."); 
       break; 
      case TRY_AGAIN: 
       if(debugLevel >= 3) puts("The name server is temporarily unavailable."); 
       break; 
     } 
    } 
    else 
    { 
     ip_of_domain = strdup(inet_ntoa(*((struct in_addr *) he->h_addr_list[0]))); 
    } 
    if(debugLevel >= 4) printf("IP=%s\n",ip_of_domain); 
} 

プログラムは互換性のすべての場所、32および64ビットでなければなりません。

ありがとうございました。

+6

これはDNSリゾルバではありません。これは、OSのDNSリゾルバを使用する小さなユーティリティアプリケーションです。 OSのビルトインツールを一般的なcatch-allプログラムでラップしようとするのではなく、使用させる。警告はかなり説明されているようです。 「一部のサーバーで」とはどういう意味ですか? –

+0

「静的ライブラリ」と「どこでも実行」は、特定のシステムコールインターフェイスにバインドするため、相互に排他的です。システムコールインターフェイスは標準化されていません。多少の変数があっても、少なくとも動的なライブラリ呼び出しレベルのインタフェースは標準です。 –

+0

このサーバー上の@LightnessRacesinOrbit '2.6.18-371.8.1.el5#1 SMP Thu Apr 24 18:23:07 EDT 2014 i686 i686 i386 GNU/Linux 'このLinux Linuxドッカーテストでは、 4.4.0-59-ジェネリック#80-Ubuntu SMP Fri Jan 6 17:47:47 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux'それはしていません...そして、この部分は 'OSのDNSリゾルバを使います。私は別のライブラリ/関数を 'gethostbyname'の横に使う必要があると言います。それはそうだと確信しています... – DancoD

答えて

0

c-aresライブラリ(ここではhttps://c-ares.haxx.se/)を使用してください。これは、OSに依存しない真のリゾルバです。

+0

答えていただきありがとうございます。どのように私はこれをインストールするのですか? './configure make install'スタイル?申し訳ありませんが、質問がトピックから少し外れている場合は、前に外部ライブラリとして 'curl'を使用しました。 – DancoD

+0

正確に。このプロジェクトには、競合するライブラリの概要もあります(https://c-ares.haxx.se/otherlibs.htmlを参照)。 –