| // Copyright 2015 The Chromium OS 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 "thermald/network_interface.h" |
| |
| #include <net/if.h> |
| #include <sys/ioctl.h> |
| #include <sys/socket.h> |
| #include <sys/types.h> |
| |
| #include <string> |
| |
| #include "base/files/file_path.h" |
| #include "base/files/file_util.h" |
| #include "base/files/scoped_file.h" |
| |
| using std::string; |
| |
| namespace thermald { |
| |
| bool NetworkInterface::IsUp() { |
| base::ScopedFD sockfd(socket(AF_INET, SOCK_DGRAM, 0)); |
| if (sockfd.get() < 0) |
| return false; |
| |
| struct ifreq ifr; |
| memset(&ifr, 0, sizeof ifr); |
| strncpy(ifr.ifr_name, name_.c_str(), IFNAMSIZ); |
| ifr.ifr_flags |= IFF_UP; |
| if (ioctl(sockfd.get(), SIOCGIFFLAGS, &ifr)) { |
| LOG(ERROR) << "[" << name_ << "] Failed to read interface flags: " |
| << strerror(errno); |
| return false; |
| } |
| |
| return ifr.ifr_flags & IFF_UP; |
| } |
| |
| bool NetworkInterface::Exists(const string &name) { |
| return base::DirectoryExists(base::FilePath("/sys/class/net/" + name)); |
| } |
| |
| } // namespace thermald |