Пример #1
0
        public async Task <IReadOnlyList <DhcpPacket> > Discover(DhcpDiscoveryParameters parameters, CancellationToken ct)
        {
            var results       = new List <DhcpPacket>();
            var transactionId = GenerateTransactionId();

            void ReceptionCallback(object sender, DhcpPacketEventArgs args)
            {
                HandleDhcpResponseReceived(results, args, transactionId);
            }

            _dhcpListener.PacketReceived += ReceptionCallback;
            _dhcpListener.StartIfNotRunning();

            using (var udpClient = new UdpClient())
            {
                var broadcastPacket      = CreateBroadcastPacket(parameters, transactionId);
                var broadcastPacketBytes = _serializer.Serialize(broadcastPacket);

                _logger.LogDebug("Sending broadcast DHCP Packet for transaction {0}", transactionId);

                await udpClient
                .SendAsync(broadcastPacketBytes, broadcastPacketBytes.Length, BroadcastEndpoint)
                .WithCancellation(ct)
                .ConfigureAwait(false);
            }

            await Task.Delay(parameters.Timeout, ct).ConfigureAwait(false);

            _dhcpListener.PacketReceived -= ReceptionCallback;

            if (results.Count == 0)
            {
                throw new TimeoutException($"No DHCP response recieved for transaction {transactionId}.");
            }

            return(results);
        }
Пример #2
0
        private DhcpPacket CreateBroadcastPacket(DhcpDiscoveryParameters parameters, uint transactionId)
        {
            // TODO: Use builder

            throw new NotImplementedException();
        }