Example #1
0
        private PingReply SendPingCore(IPAddress address, byte[] buffer, int timeout, PingOptions?options)
        {
            PingReply reply = RawSocketPermissions.CanUseRawSockets(address.AddressFamily) ?
                              SendIcmpEchoRequestOverRawSocket(address, buffer, timeout, options) :
                              SendWithPingUtility(address, buffer, timeout, options);

            return(reply);
        }
Example #2
0
        private Socket GetRawSocket(SocketConfig socketConfig)
        {
            IPEndPoint    ep         = (IPEndPoint)socketConfig.EndPoint;
            AddressFamily addrFamily = ep.Address.AddressFamily;
            SocketType    socketType = RawSocketPermissions.CanUseRawSockets(addrFamily) ?
                                       SocketType.Raw :
                                       SocketType.Dgram; // macOS/iOS has ability to send ICMP echo without RAW

            Socket socket = new Socket(addrFamily, socketType, socketConfig.ProtocolType);

            socket.ReceiveTimeout = socketConfig.Timeout;
            socket.SendTimeout    = socketConfig.Timeout;
            if (addrFamily == AddressFamily.InterNetworkV6 && OperatingSystem.IsMacOS())
            {
                socket.DualMode = false;
            }

            if (socketConfig.Options != null)
            {
                if (socketConfig.Options.Ttl > 0)
                {
                    socket.Ttl = (short)socketConfig.Options.Ttl;
                }

                if (addrFamily == AddressFamily.InterNetwork)
                {
                    if (SendIpHeader)
                    {
                        // some platforms like OSX don't support DontFragment so we construct IP header instead.
                        socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1);
                    }
                    else
                    {
                        socket.DontFragment = socketConfig.Options.DontFragment;
                    }
                }
            }

#pragma warning disable 618
            // Disable warning about obsolete property. We could use GetAddressBytes but that allocates.
            // IPv4 multicast address starts with 1110 bits so mask rest and test if we get correct value e.g. 0xe0.
            if (NeedsConnect && !ep.Address.IsIPv6Multicast && !(addrFamily == AddressFamily.InterNetwork && (ep.Address.Address & 0xf0) == 0xe0))
            {
                // If it is not multicast, use Connect to scope responses only to the target address.
                socket.Connect(socketConfig.EndPoint);
            }
#pragma warning restore 618

            return(socket);
        }
Example #3
0
 private async Task <PingReply> SendPingAsyncCore(IPAddress address, byte[] buffer, int timeout, PingOptions options)
 {
     try
     {
         Task <PingReply> t = RawSocketPermissions.CanUseRawSockets() ?
                              SendIcmpEchoRequestOverRawSocket(address, buffer, timeout, options) :
                              SendWithPingUtility(address, buffer, timeout, options);
         return(await t.ConfigureAwait(false));
     }
     finally
     {
         Finish();
     }
 }
Example #4
0
        private async Task <PingReply> SendPingAsyncCore(IPAddress address, byte[] buffer, int timeout, PingOptions?options)
        {
            Task <PingReply> t = RawSocketPermissions.CanUseRawSockets(address.AddressFamily) ?
                                 SendIcmpEchoRequestOverRawSocketAsync(address, buffer, timeout, options) :
                                 SendWithPingUtilityAsync(address, buffer, timeout, options);

            PingReply reply = await t.ConfigureAwait(false);

            if (_canceled)
            {
                throw new OperationCanceledException();
            }

            return(reply);
        }
Example #5
0
 public static bool CanUseRawSockets()
 {
     return(RawSocketPermissions.CanUseRawSockets());
 }
 public static bool CanUseRawSockets(AddressFamily addressFamily)
 {
     return(RawSocketPermissions.CanUseRawSockets(addressFamily));
 }