]> git.basschouten.com Git - openhab-addons.git/blob
dd64b1175e632efe662f9f7f279acd754012f392
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.silvercrestwifisocket.internal.utils;
14
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;
23
24 /**
25  * Utilitary static class to perform some network routines.
26  *
27  * @author Jaime Vaz - Initial contribution
28  *
29  */
30 public final class NetworkUtils {
31
32     private NetworkUtils() {
33         // Avoid instantiation.
34     }
35
36     /**
37      * Gets all the broadcast address's from the machine.
38      *
39      * @return list with all the broadcast address's
40      */
41     public static List<InetAddress> getAllBroadcastAddresses() {
42         List<InetAddress> listOfBroadcasts = new ArrayList<>();
43         Enumeration<NetworkInterface> list;
44         try {
45             list = NetworkInterface.getNetworkInterfaces();
46
47             while (list.hasMoreElements()) {
48                 NetworkInterface iface = list.nextElement();
49                 if (iface == null) {
50                     continue;
51                 }
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) {
57                             continue;
58                         }
59                         InetAddress broadcast = address.getBroadcast();
60                         if (broadcast != null) {
61                             listOfBroadcasts.add(broadcast);
62                         }
63                     }
64                 }
65             }
66         } catch (SocketException ex) {
67             return new ArrayList<>();
68         }
69         return listOfBroadcasts;
70     }
71 }