2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.silvercrestwifisocket.internal.utils;
15 import java.net.InetAddress;
16 import java.net.InterfaceAddress;
17 import java.net.NetworkInterface;
18 import java.net.SocketException;
19 import java.util.ArrayList;
20 import java.util.Enumeration;
21 import java.util.Iterator;
22 import java.util.List;
25 * Utilitary static class to perform some network routines.
27 * @author Jaime Vaz - Initial contribution
30 public final class NetworkUtils {
32 private NetworkUtils() {
33 // Avoid instantiation.
37 * Gets all the broadcast address's from the machine.
39 * @return list with all the broadcast address's
41 public static List<InetAddress> getAllBroadcastAddresses() {
42 List<InetAddress> listOfBroadcasts = new ArrayList<>();
43 Enumeration<NetworkInterface> list;
45 list = NetworkInterface.getNetworkInterfaces();
47 while (list.hasMoreElements()) {
48 NetworkInterface iface = list.nextElement();
52 if (!iface.isLoopback() && iface.isUp()) {
53 Iterator<InterfaceAddress> it = iface.getInterfaceAddresses().iterator();
54 while (it.hasNext()) {
55 InterfaceAddress address = it.next();
56 if (address == null) {
59 InetAddress broadcast = address.getBroadcast();
60 if (broadcast != null) {
61 listOfBroadcasts.add(broadcast);
66 } catch (SocketException ex) {
67 return new ArrayList<>();
69 return listOfBroadcasts;