All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ardikars.common.net.NetworkInterface Maven / Gradle / Ivy

Go to download

Java Common is an project focused on all aspects of reusable Java components.

The newest version!
/**
 * Copyright 2017-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ardikars.common.net;

import com.ardikars.common.annotation.Immutable;
import com.ardikars.common.annotation.Incubating;
import com.ardikars.common.annotation.Mutable;

import java.net.InterfaceAddress;
import java.net.SocketException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

/**
 * This class represents an Network Interface Card.
 *
 * @author Ardika Rommy Sanjaya
 * @since 1.0.0
 */
@Incubating
@Mutable(blocking = true)
public class NetworkInterface {

    private final int index;
    private final String name;
    private final String displayName;
    private final com.ardikars.common.util.Address hardwareAddress;
    private final Collection
addresses; private final int mtu; private final boolean pointToPoint; private final boolean virtual; private final boolean loopback; private final boolean up; private final NetworkInterface parent; private final Collection childs; private NetworkInterface(final Builder builder) { this.index = builder.index; this.name = builder.name; this.displayName = builder.displayName; this.hardwareAddress = builder.hardwareAddress; this.addresses = builder.addresses; this.mtu = builder.mtu; this.pointToPoint = builder.pointToPoint; this.virtual = builder.virtual; this.loopback = builder.loopback; this.up = builder.up; this.parent = builder.parent; this.childs = builder.childs; } public int getIndex() { return index; } public String getName() { return name; } public String getDisplayName() { return displayName; } public com.ardikars.common.util.Address getHardwareAddress() { return hardwareAddress; } public Collection
getAddresses() { return addresses; } public int getMtu() { return mtu; } public boolean isPointToPoint() { return pointToPoint; } public boolean isVirtual() { return virtual; } public boolean isLoopback() { return loopback; } public boolean isUp() { return up; } public NetworkInterface getParent() { return parent; } public Collection getChilds() { return childs; } /** * Add child {@code NetworkInterface}. Example: virtual interface. * @param networkInterface {@code NetworkInterface}. */ public void addChild(NetworkInterface networkInterface) { synchronized (this) { this.childs.add(networkInterface); } } @Immutable public static class Address { private final InetAddress inetAddress; private final int maskLength; private Address(final NetworkInterface.Address.Builder builder) { this.inetAddress = builder.inetAddress; this.maskLength = builder.maskLength; } public InetAddress getInetAddress() { return inetAddress; } public int getNetworkPrefixLength() { return maskLength; } @Override public String toString() { return new StringBuilder("Address{") .append("inetAddress=").append(inetAddress) .append(", maskLength=").append(maskLength) .append('}') .toString(); } public static class Builder implements com.ardikars.common.util.Builder { private InetAddress inetAddress; private int maskLength; public Builder inetAddress(InetAddress inetAddress) { this.inetAddress = inetAddress; return this; } public Builder maskLength(int maskLength) { this.maskLength = maskLength; return this; } @Override public Address build() { return new Address(this); } @Override public Address build(Void value) { throw new UnsupportedOperationException(); } } } public static class Builder implements com.ardikars.common.util.Builder { private int index; private String name; private String displayName; private com.ardikars.common.util.Address hardwareAddress; private Collection
addresses; private int mtu; private boolean pointToPoint; private boolean virtual; private boolean loopback; private boolean up; private NetworkInterface parent; private Collection childs; public Builder index(int index) { this.index = index; return this; } public Builder name(String name) { this.name = name; return this; } public Builder displayName(String displayName) { this.displayName = displayName; return this; } public Builder hardwareAddress(MacAddress hardwareAddress) { this.hardwareAddress = hardwareAddress; return this; } public Builder addresses(Collection
addresses) { this.addresses = addresses; return this; } public Builder mtu(int mtu) { this.mtu = mtu; return this; } public Builder pointToPoint(boolean pointToPoint) { this.pointToPoint = pointToPoint; return this; } public Builder virtual(boolean virtual) { this.virtual = virtual; return this; } public Builder loopback(boolean loopback) { this.loopback = loopback; return this; } public Builder up(boolean up) { this.up = up; return this; } public Builder parent(NetworkInterface parent) { this.parent = parent; return this; } public Builder childs(Collection childs) { this.childs = childs; return this; } /** * Build {@code NetworkInterface}. * @return {@code NetworkInterface}. */ @Override public NetworkInterface build() { return new NetworkInterface(this); } /** * Build {@code NetworkInterface}. * @param value value. * @return {@code NetworkInterface}. */ @Override public NetworkInterface build(Void value) { throw new UnsupportedOperationException(); } } @Override public String toString() { return new StringBuilder("NetworkInterface{") .append("index=").append(index) .append(", name='").append(name).append('\'') .append(", displayName='").append(displayName).append('\'') .append(", hardwareAddress=").append(hardwareAddress) .append(", addresses=").append(addresses) .append(", mtu=").append(mtu) .append(", pointToPoint=").append(pointToPoint) .append(", virtual=").append(virtual) .append(", loopback=").append(loopback) .append(", up=").append(up) .append(", parent=").append(parent) .append(", childs=").append(childs) .append('}') .toString(); } /** * Get network interfaces. * @return returns network interfaces. * @throws SocketException socket exception. */ public static Collection getNetworkInterfaces() throws SocketException { Enumeration networkInterfaces = java.net.NetworkInterface.getNetworkInterfaces(); Set networkInterfaceSet = new HashSet(); while (networkInterfaces.hasMoreElements()) { java.net.NetworkInterface networkInterface = networkInterfaces.nextElement(); byte[] hardwareAddress = networkInterface.getHardwareAddress(); Set
parentAddreses = new HashSet
(); for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { if (interfaceAddress.getAddress() instanceof java.net.Inet4Address) { Address address = new Address.Builder() .inetAddress(Inet4Address.valueOf(interfaceAddress.getAddress().getAddress())) .maskLength(interfaceAddress.getNetworkPrefixLength()) .build(); parentAddreses.add(address); } else if (interfaceAddress.getAddress() instanceof java.net.Inet6Address) { Address address = new Address.Builder() .inetAddress(Inet6Address.valueOf(interfaceAddress.getAddress().getAddress())) .maskLength(interfaceAddress.getNetworkPrefixLength()) .build(); parentAddreses.add(address); } } NetworkInterface parent = new Builder() .index(networkInterface.getIndex()) .name(networkInterface.getName()) .displayName(networkInterface.getDisplayName()) .hardwareAddress(hardwareAddress == null || hardwareAddress.length != MacAddress.MAC_ADDRESS_LENGTH ? MacAddress.ZERO : MacAddress.valueOf(hardwareAddress)) .addresses(parentAddreses) .mtu(networkInterface.getMTU()) .pointToPoint(networkInterface.isPointToPoint()) .virtual(networkInterface.isVirtual()) .loopback(networkInterface.isLoopback()) .up(networkInterface.isUp()) .parent(null) .childs(null) .build(); Enumeration childs = networkInterface.getSubInterfaces(); while (childs.hasMoreElements()) { java.net.NetworkInterface childNetworkInterface = childs.nextElement(); hardwareAddress = childNetworkInterface.getHardwareAddress(); Set
childAddresses = new HashSet
(); for (InterfaceAddress interfaceAddress : childNetworkInterface.getInterfaceAddresses()) { if (interfaceAddress.getAddress() instanceof java.net.Inet4Address) { Address address = new Address.Builder() .inetAddress(Inet4Address.valueOf(interfaceAddress.getAddress().getAddress())) .maskLength(interfaceAddress.getNetworkPrefixLength()) .build(); childAddresses.add(address); } else if (interfaceAddress.getAddress() instanceof java.net.Inet6Address) { Address address = new Address.Builder() .inetAddress(Inet6Address.valueOf(interfaceAddress.getAddress().getAddress())) .maskLength(interfaceAddress.getNetworkPrefixLength()) .build(); childAddresses.add(address); } } NetworkInterface child = new Builder() .index(childNetworkInterface.getIndex()) .name(childNetworkInterface.getName()) .displayName(childNetworkInterface.getDisplayName()) .hardwareAddress(hardwareAddress == null || hardwareAddress.length != MacAddress.MAC_ADDRESS_LENGTH ? MacAddress.ZERO : MacAddress.valueOf(hardwareAddress)) .addresses(childAddresses) .mtu(childNetworkInterface.getMTU()) .pointToPoint(childNetworkInterface.isPointToPoint()) .virtual(childNetworkInterface.isVirtual()) .loopback(childNetworkInterface.isLoopback()) .up(childNetworkInterface.isUp()) .parent(parent) .childs(null) .build(); parent.addChild(child); } networkInterfaceSet.add(parent); } return networkInterfaceSet; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy