Giter Club home page Giter Club logo

networkinterfaceinfo's Introduction

NetworkInterfaceInfo

GitHub code size in bytes GitHub build results

Basically a Swift abstraction over getifaddrs; a way to enumerate all the network interfaces on the current host, with their core information such as associated addresses & netmasks.

This is intentionally very lightweight, and designed to be very efficient even for cases where you're looking for a small subset of interfaces. e.g. to find all active IPv4 addresses (excluding loopback networks):

import NetworkInterfaceInfo

try NetworkInterface.all
    .filter { $0.up             // Only active network interfaces…
              && !$0.loopback } // Ignoring loopback interfaces…
    .compactMap(\.address)      // That have addresses and…
    .filter(\.isIPv4)           // Use IPv4.

On macOS only, interface metrics are also available - both configuration values such as MTU and line speed, as well as counters such as bytes & packets sent & received.

See the documentation for more details.

Change monitoring

There is also a second module which allows you to monitor for changes to network interfaces, as shown in the example below.

Note: this additional module is currently only implemented for platforms that have Apple's Network package available (i.e. Apple's platforms, not Linux or Windows).

import NetworkInterfaceInfo
import NetworkInterfaceChangeMonitoring

for try await change in NetworkInterface.changes() {
    switch change.nature {
    case .added:
        print("New network interface: \(change.interface)")
    case .modified(let modificationNature):
        if modificationNature.contains(.address) {
            print("Address changed to \(change.interface.address).")
        }
    case .removed:
        // etc
    }
}

Note: you still need to explicitly import NetworkInterfaceInfo in order to access the changes method and otherwise use NetworkInterface et al.

This monitoring functionality is in a separate module so that you don't pay the cost of it if you don't need it.

Important: monitoring for network interface changes relies on Apple's NWPathMonitor class. Generally that notices changes virtually immediately, but sometimes it is delayed - up to minute(s) later. This might be worked around in a future version of this library, but for now at least be aware of that annoying uncertainty and consider taking steps to work around it (e.g. polling NetworkInterface.all instead, if you need a clear latency upper bound).

networkinterfaceinfo's People

Contributors

nbrachet avatar wadetregaskis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

nbrachet

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.