How to get local IP

96 views
Skip to first unread message

Kael

unread,
Apr 18, 2016, 4:01:39 AM4/18/16
to Native-Client-Discuss
Hi,all
My environment is win7 64.
SDK version pepper_49.
TOOCHAIN=glibc NACL_ARCH=x86_64
I run chrome with the --enable-nacl --no-sandbox --allow-nacl-socket-api=host flags. 

I tried some ways to get local IP but failed.
Question 1:
Dose nacl support gethostname ?
I tried this to get local IP, and i got this warning during compiling:
warning:gethostname isnotimplemented and will always fail.

Question 2:
I tried ioctl to get local IP:

int sockfd;
struct ifconf ifcnfig;
char buffer[4000];
ifcnfig.ifc_len = sizeof(buffer);
ifcnfig.ifc_buf = buffer;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
  fprintf(stderr, "socket failed, fd=%d.\r\n", sockfd);
}
if (ioctl(sockfd, SIOCGIFCONF, &ifcnfig) < 0)
{
  fprintf(stderr, "ioctl:%s'n", strerror(errno));
  close(sockfd);
  return;
}

When i ran it in chrome, i got this error:
ioctl:Invalid argument
How can i solve this problem?

Question 3:
Can i get local IP through another way?

Thanks


Ben Smith

unread,
Apr 18, 2016, 5:23:25 PM4/18/16
to Native-Client-Discuss
Hi Kael,

I don't think this is exposed via nacl_io. But you can get a list of network adapters using the NetworkList API. Take a look at the network monitor example in the SDK, at examples/api/network_monitor.

-Ben

Kael

unread,
Apr 22, 2016, 3:53:14 AM4/22/16
to Native-Client-Discuss
Hi Ben,
I get the list of network adapters successfully in main thread using NetworkList API.
But I failed to get it in a second thread:
Attempted to use a required callback, but there is no attached message loop on which to run the callback.

Then, I tried to UpdateNetworkList with a Blocking CompletionCallBackWithOutput in a background thread.
I got this err : UpdateNetworkLis failed: -4
Can i UpdateNetworkList with a Blocking CallBack?
Can you help me to find out the invalid argument?

Looking forward to your reply.

@code
Class XXXInstance : public pp::Instance()
{
  public:
    explicit XXXInstance(PP_Instance instance)
      : pp::instance(instance)
      , m_callBack(m_storageType)
      , m_networkMonitor(this){}
    void update();
  private:
     pp::internal::CallbackOutputTraits<pp::NetworkList>::StorageType *m_storageType;
     pp::CompletionCallBackWithOutput<pp::NetworkList> m_callBack;
     pp::NetworkMonitor m_networkMonitor;
 };

void update()
{
  int32_t result = m_networkMonitor.UpdateNetworkList(m_callBack);
  if (result != PP_OK_COMPLETIONPENDING)
  {
    std::ostringstream status;
    status<< "UpdateNetworkLis failed: "<< result;
    PostMessage(status.str());
  }
}
@endcode


Thanks




在 2016年4月19日星期二 UTC+8上午5:23:25,Ben Smith写道:

Bill Budge

unread,
Apr 23, 2016, 10:19:29 AM4/23/16
to Native-Client-Discuss
I'm not sure why you're getting a PP_ERROR_BADARGUMENT but you must have a message loop to run asynchronous callbacks on your own thread.

This link explains how to create the message loop:

Reply all
Reply to author
Forward
0 new messages