title: How to capture internet connection of an Android app without using Wireshark
body: <p>I'm developing an Android app. The app includes both Java code and native code, and uses some 3rd Java libraries (<code>.jar</code> files). Now I want to capture all the hosts/domains that the app connects to.</p>
  
  <p>I can use tools like Wireshark but this requires complex setup and filters in order to avoid interference from other apps.</p>
  
  <p>Another method I'm considering is hooking native functions like <code>gethostbyname</code></p>
  
  <pre><code>void* my_handler = NULL;
  struct hostent* (*my_gethostbyname)(const char*) = NULL;
  
  void init_hook() {
      my_handler = dlopen("libc.so", RTLD_LAZY);
      my_gethostbyname = (struct hostent* (*)(const char*)) dlsym(my_handler, "gethostbyname");
  }
  
  struct hostent* gethostbyname(const char* name) {
      __android_log_print(ANDROID_LOG_INFO, "HOOK", "name = %s", name);
      return my_gethostbyname(name);
  }
  </code></pre>
  
  <p>This code works, however, my questions are:</p>
  
  <ul>
  <li>Is the function <code>gethostbyname</code> the correct one that needs to be hooked?</li>
  <li>Does this hooking work for capturing the connections initiated from Java code and the 3rd <code>.jar</code> libraries?</li>
  <li>What is your suggestion?</li>
  </ul>