无法解决NACL上卷曲导致的主机错误

时间:2015-04-10 10:01:38

标签: google-chrome curl google-nativeclient

我是nacl的新手。在我的应用程序中,我使用原生卷曲。我通过nacl端口检查了curl。即时通讯"无法解析主机名"错误。

任何帮助都会得到满足。

以下是示例代码:

hello_tutorial.cc:

// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include "nacl_io/nacl_io.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var.h"
#include "ppapi/utility/completion_callback_factory.h"
#include "ppapi/utility/threading/simple_thread.h"
#include "curl/curl.h"
namespace {

// The expected string sent by the browser.
const char* const kHelloString = "hello";
// The string sent back to the browser upon receipt of a message
// containing "hello".
const char* const kReplyString = "hello from NaCl";

}  // namespace

class HelloTutorialInstance : public pp::Instance {
 public:
  explicit HelloTutorialInstance(PP_Instance instance)
      : pp::Instance(instance),
        callback_factory_(this),
        file_thread_(this){}
  virtual ~HelloTutorialInstance() {}
    int download_progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) {
        fprintf(stderr, "### Progress : %f : %f : %f", dlnow/dltotal, dlnow, dltotal);

        return 0;
    }

    int download_callback(char *buffer, size_t size, size_t nitems, void *outstream) {
        static int downloadSize = 0;
        downloadSize += (size * nitems);
        fprintf(stderr, "SIZE : %d", downloadSize);
        return nitems * size;
    }

  void curlTest(int32_t /* result */)
    {
        CURLM *multiCurlHandle = NULL;
        CURL *curlHandle = NULL;
        int res = curl_global_init(CURL_GLOBAL_ALL);
        if (res == CURLE_OK) {
            multiCurlHandle = curl_multi_init();
            curlHandle = curl_easy_init();
            curl_easy_setopt(curlHandle, CURLOPT_URL, "www.google.com");
            curl_easy_setopt(curlHandle, CURLOPT_VERBOSE, 1);
            curl_easy_setopt(curlHandle, CURLOPT_NOPROGRESS, 0);
            curl_easy_setopt(curlHandle, CURLOPT_PROGRESSFUNCTION ,&HelloTutorialInstance::download_progress_callback);
            curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, &HelloTutorialInstance::download_callback);

            res = curl_easy_perform(curlHandle);


            //curl_multi_add_handle(multiCurlHandle, curlHandle);
            //curl_multi_perform(multiCurlHandle, &handle_count);
            if (CURLE_OK != res) {
                fprintf(stderr, "curl_easy_perform failed !!\nReason : %s", curl_easy_strerror((CURLcode)res));
            }
        }    }

  virtual void HandleMessage(const pp::Var& var_message) {
    // Ignore the message if it is not a string.
    if (!var_message.is_string())
      return;

    // Get the string message and compare it to "hello".
    std::string message = var_message.AsString();
    if (message == kHelloString) {
      // If it matches, send our response back to JavaScript.
      pp::Var var_reply(kReplyString);
      nacl_io_init_ppapi(pp::Instance::pp_instance(),pp::Module::Get()->get_browser_interface());
      file_thread_.Start();
      file_thread_.message_loop().PostWork(
                    callback_factory_.NewCallback(&HelloTutorialInstance::curlTest));
      PostMessage(var_reply);
    }
  }
private:
    pp::CompletionCallbackFactory<HelloTutorialInstance> callback_factory_;
    pp::SimpleThread file_thread_;
};

class HelloTutorialModule : public pp::Module {
 public:
  HelloTutorialModule() : pp::Module() {}
  virtual ~HelloTutorialModule() {}

  virtual pp::Instance* CreateInstance(PP_Instance instance) {
    return new HelloTutorialInstance(instance);
  }
};

namespace pp {

Module* CreateModule() {
  return new HelloTutorialModule();
}

}  // namespace pp

makefile:

# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# GNU Makefile based on shared rules provided by the Native Client SDK.
# See README.Makefiles for more details.

VALID_TOOLCHAINS := pnacl newlib glibc

NACL_SDK_ROOT ?= $(abspath $(CURDIR)/../..)

TARGET = part2


include $(NACL_SDK_ROOT)/tools/common.mk

CHROME_ARGS += --allow-nacl-socket-api=localhost

LIBS = ppapi_cpp ppapi pthread nacl_io curl ssl crypto stdc++ z glibc-compat

CFLAGS = -Wall
SOURCES = hello_tutorial.cc

# Build rules generated by macros from common.mk:

$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))

# The PNaCl workflow uses both an unstripped and finalized/stripped binary.
# On NaCl, only produce a stripped binary for Release configs (not Debug).
ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG))))
$(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS)))
$(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped))
else
$(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS)))
endif

$(eval $(call NMF_RULE,$(TARGET),))

在终端执行:

part2$ NACL_SDK_ROOT=~/nacl_sdk/pepper_40 NACL_ARCH=x86_64 TOOLCHAIN=newlib make clean run
~/nacl_sdk/pepper_40/tools/common.mk:495: Using chrome at: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
  CXX  newlib/Release/hello_tutorial_x86_64.o
  LINK newlib/Release/part2_unstripped_x86_64.nexe
  VALIDATE newlib/Release/part2_unstripped_x86_64.nexe
  STRIP newlib/Release/part2_x86_64.nexe
  CREATE_NMF newlib/Release/part2.nmf
python ~/nacl_sdk/pepper_40/tools/run.py -C ~/nacl_sdk/pepper_40/getting_started/part2 -P "index.html?tc=newlib&config=Release" \
         -- /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
        --enable-nacl --enable-pnacl --no-first-run --user-data-dir=~/nacl_sdk/pepper_40/getting_started/part2/user-data-dir --allow-nacl-socket-api=localhost \
        --register-pepper-plugins="~/nacl_sdk/pepper_40/getting_started/part2/mac/Debug/part2.so;application/x-ppapi-debug~nacl_sdk/pepper_40/getting_started/part2/mac/Release/part2.so;application/x-ppapi-release"
Serving ~/nacl_sdk/pepper_40/getting_started/part2 on http://localhost:5103/...
Running: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --enable-nacl --enable-pnacl --no-first-run --user-data-dir=~/nacl_sdk/pepper_40/getting_started/part2/user-data-dir --allow-nacl-socket-api=localhost --register-pepper-plugins=~/nacl_sdk/pepper_40/getting_started/part2/mac/Debug/part2.so;application/x-ppapi-debug,~/nacl_sdk/pepper_40/getting_started/part2/mac/Release/part2.so;application/x-ppapi-release http://localhost:5103/index.html?tc=newlib&config=Release...



Output :

127.0.0.1 - - [10/Apr/2015 15:05:26] "GET /index.html?tc=newlib&config=Release HTTP/1.1" 200 -
127.0.0.1 - - [10/Apr/2015 15:05:26] "GET /common.js HTTP/1.1" 200 -
127.0.0.1 - - [10/Apr/2015 15:05:26] "GET /example.js HTTP/1.1" 200 -
127.0.0.1 - - [10/Apr/2015 15:05:26] "GET /newlib/Release/part2.nmf HTTP/1.1" 200 -
127.0.0.1 - - [10/Apr/2015 15:05:26] "GET /newlib/Release/part2_x86_64.nexe HTTP/1.1" 200 -
127.0.0.1 - - [10/Apr/2015 15:05:26] code 404, message File not found
127.0.0.1 - - [10/Apr/2015 15:05:26] "GET /favicon.ico HTTP/1.1" 404 -
[43852,1925374720:15:05:26.885906] Native Client module will be loaded at base address 0x000052a200000000
* Rebuilt URL to: www.google.com/
* timeout on name lookup is not supported
* Hostname was NOT found in DNS cache
* Curl_ipv4_resolve_r failed for www.google.com
* Couldn't resolve host 'www.google.com'
* Closing connection 0
curl_easy_perform failed !!
Reason : Couldn't resolve host name

2 个答案:

答案 0 :(得分:0)

我设法使示例正常工作(至少解析主机名) 通过改变LIBS行Makefile。我删除了glibc-compat 并将nacl_io移动到该行的末尾:

LIBS = ppapi_cpp ppapi pthread curl ssl crypto z nacl_io 

需要的原因是glibc-compat包含很多 存根/虚拟/非工作函数,而nacl_io包含工作 implemenations。

- 感谢Sam(来自原生Google群组)指出此

答案 1 :(得分:0)

我正在尝试使用pepper_49构建curl但是在构建

时仍然存在一些问题

生成文件:

LIBS = ppapi_cpp ppapi pthread curl ssl crypto z nacl_io

使用TOOLCHAIN=pnacl make clean run构建

/Users/fu/nacl_sdk/pepper_49/tools/common.mk:500: Using chrome at: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
CXX  pnacl/Release/hello_tutorial.o
LINK pnacl/Release/curltest_unstripped.bc
/Users/fu/nacl_sdk/pepper_49/toolchain/mac_pnacl/le32-nacl/usr/lib/libcurl.a: error: undefined reference to 'alarm'
make: *** [pnacl/Release/curltest_unstripped.bc] Error 1

我将Makefile更改为

后解决了
LIBS = ppapi_cpp ppapi pthread curl ssl crypto z glibc-compat nacl_io

并且示例正常。