blob: 6f67d5abbd4b6a3488bff339883610b4a4025e80 [file] [log] [blame]
// <copyright file="PortUtilities.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC 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
//
// 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.
// </copyright>
using System.Net;
using System.Net.Sockets;
namespace OpenQA.Selenium.Internal;
/// <summary>
/// Encapsulates methods for working with ports.
/// </summary>
public static class PortUtilities
{
/// <summary>
/// Finds a random, free port to be listened on.
/// </summary>
/// <remarks>
/// Prefers IPv4, but falls back to IPv6 if necessary.
/// </remarks>
/// <returns>A random, free port to be listened on.</returns>
public static int FindFreePort()
{
try
{
using var ipV4socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ipV4socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
return ((IPEndPoint)ipV4socket.LocalEndPoint!).Port;
}
catch (SocketException)
{
using var ipV6socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
ipV6socket.Bind(new IPEndPoint(IPAddress.IPv6Loopback, 0));
return ((IPEndPoint)ipV6socket.LocalEndPoint!).Port;
}
}
}