/*
 * Copyright 2016 The Netty Project
 *
 * The Netty Project licenses this file to you 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:
 *
 *   https://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 io.netty.util.internal;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.channels.DatagramChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Collections;
import java.util.Enumeration;

/**
 * Provides socket operations with privileges enabled.
 */
public final class SocketUtils {

    private static final Enumeration<Object> EMPTY = Collections.enumeration(Collections.emptyList());

    private SocketUtils() {
    }

    @SuppressWarnings("unchecked")
    private static <T> Enumeration<T> empty() {
        return (Enumeration<T>) EMPTY;
    }

    public static void connect(final Socket socket, final SocketAddress remoteAddress, final int timeout)
            throws IOException {
        socket.connect(remoteAddress, timeout);
    }

    public static void bind(final Socket socket, final SocketAddress bindpoint) throws IOException {
        socket.bind(bindpoint);
    }

    public static boolean connect(final SocketChannel socketChannel, final SocketAddress remoteAddress)
            throws IOException {
        return socketChannel.connect(remoteAddress);
    }

    @SuppressJava6Requirement(reason = "Usage guarded by java version check")
    public static void bind(final SocketChannel socketChannel, final SocketAddress address) throws IOException {
        socketChannel.bind(address);
    }

    public static SocketChannel accept(final ServerSocketChannel serverSocketChannel) throws IOException {
        return serverSocketChannel.accept();
    }

    @SuppressJava6Requirement(reason = "Usage guarded by java version check")
    public static void bind(final DatagramChannel networkChannel, final SocketAddress address) throws IOException {
        networkChannel.bind(address);
    }

    public static SocketAddress localSocketAddress(final ServerSocket socket) {
        return socket.getLocalSocketAddress();
    }

    public static InetAddress addressByName(final String hostname) throws UnknownHostException {
        return InetAddress.getByName(hostname);
    }

    public static InetAddress[] allAddressesByName(final String hostname) throws UnknownHostException {
        return InetAddress.getAllByName(hostname);
    }

    public static InetSocketAddress socketAddress(final String hostname, final int port) {
        return new InetSocketAddress(hostname, port);
    }

    public static Enumeration<InetAddress> addressesFromNetworkInterface(final NetworkInterface intf) {
        Enumeration<InetAddress> addresses = intf.getInetAddresses();
        // Android seems to sometimes return null even if this is not a valid return value by the api docs.
        // Just return an empty Enumeration in this case.
        // See https://github.com/netty/netty/issues/10045
        if (addresses == null) {
            return empty();
        }
        return addresses;
    }

    @SuppressJava6Requirement(reason = "Usage guarded by java version check")
    public static InetAddress loopbackAddress() {
        if (PlatformDependent.javaVersion() >= 7) {
            return InetAddress.getLoopbackAddress();
        }
        try {
            return InetAddress.getByName(null);
        } catch (UnknownHostException e) {
            throw new IllegalStateException(e);
        }
    }

    public static byte[] hardwareAddressFromNetworkInterface(final NetworkInterface intf) throws SocketException {
        return intf.getHardwareAddress();
    }
}
