title: How to check if interface is up
body: <p>Title pretty much says it all. If I run <code>ifconfig</code>, I get this:</p>
  
  <pre><code>eth0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500
      inet -snip-  netmask 255.255.255.0  broadcast -snip-
      ...
  </code></pre>
  
  <p>Using this, I can know if it's up or not (<code>&lt;UP,...</code>), but I want to be able to do this in C (or C++, if there is a simpler solution there) without relying on parsing external processes.</p>
  
  <hr>
  
  <p>Here is what I've got so far (doesn't work):</p>
  
  <pre><code>bool is_interface_online(std::string interface) {
      struct ifreq ifr;
      int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
      memset(&amp;ifr, 0, sizeof(ifr));
      strcpy(ifr.ifr_name, interface.c_str());
      if (ioctl(sock, SIOCGIFFLAGS, &amp;ifr) &lt; 0) {
              perror("SIOCGIFFLAGS");
      }
      close(sock);
      return !!(ifr.ifr_flags | IFF_UP);
  }
  </code></pre>
  
  <p>Can anyone point me in the correct direction for this?</p>