Skip to content
Snippets Groups Projects
Commit 222afca8 authored by Abose Ukhun's avatar Abose Ukhun
Browse files

Upload New File

parent 4865cccd
No related merge requests found
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using Microsoft.Extensions.Configuration;
namespace IPAddressProcessor
{
class test_task_IP_address
{
static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true)
.AddCommandLine(args)
.AddEnvironmentVariables()
.Build();
string fileLog = configuration["file-log"];
string fileOutput = configuration["file-output"];
string addressStart = configuration["address-start"];
string addressMask = configuration["address-mask"];
string timeStart = configuration["time-start"];
string timeEnd = configuration["time-end"];
if (string.IsNullOrEmpty(fileLog) || string.IsNullOrEmpty(fileOutput) ||
string.IsNullOrEmpty(timeStart) || string.IsNullOrEmpty(timeEnd))
{
Console.WriteLine("Required parameters are missing.");
return;
}
DateTime startTime = DateTime.ParseExact(timeStart, "dd.MM.yyyy", null);
DateTime endTime = DateTime.ParseExact(timeEnd, "dd.MM.yyyy", null);
IPAddress startAddress = null;
IPAddress endAddress = null;
if (!string.IsNullOrEmpty(addressStart))
{
if (!IPAddress.TryParse(addressStart, out startAddress))
{
Console.WriteLine("Invalid start address.");
return;
}
if (!string.IsNullOrEmpty(addressMask))
{
if (!IPAddress.TryParse(addressMask, out IPAddress mask))
{
Console.WriteLine("Invalid address mask.");
return;
}
endAddress = CalculateEndAddress(startAddress, mask);
}
}
var ipCounts = new Dictionary<string, int>();
try
{
using (var reader = new StreamReader(fileLog))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(':');
if (parts.Length == 2)
{
string ipAddress = parts[0].Trim();
DateTime accessTime = DateTime.ParseExact(parts[1].Trim(), "yyyy-MM-dd HH:mm:ss", null);
if (accessTime >= startTime && accessTime <= endTime)
{
if (startAddress != null)
{
IPAddress currentAddress;
if (IPAddress.TryParse(ipAddress, out currentAddress))
{
byte[] currentBytes = currentAddress.GetAddressBytes();
byte[] startBytes = startAddress.GetAddressBytes();
byte[] endBytes = endAddress?.GetAddressBytes(); // Handle null for endAddress
if (ByteArrayCompare(currentBytes, startBytes) >= 0 &&
(endBytes == null || ByteArrayCompare(currentBytes, endBytes) <= 0))
{
if (ipCounts.ContainsKey(ipAddress))
ipCounts[ipAddress]++;
else
ipCounts[ipAddress] = 1;
}
}
}
else
{
if (ipCounts.ContainsKey(ipAddress))
ipCounts[ipAddress]++;
else
ipCounts[ipAddress] = 1;
}
}
}
}
}
using (var writer = new StreamWriter(fileOutput))
{
foreach (var entry in ipCounts.OrderByDescending(x => x.Value))
{
writer.WriteLine($"{entry.Key}: {entry.Value}");
}
}
Console.WriteLine("Processing completed successfully.");
}
catch (IOException ex)
{
Console.WriteLine($"An error occurred while accessing the files: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
}
}
static IPAddress CalculateEndAddress(IPAddress startAddress, IPAddress mask)
{
byte[] startBytes = startAddress.GetAddressBytes();
byte[] maskBytes = mask.GetAddressBytes();
byte[] endBytes = new byte[4];
for (int i = 0; i < 4; i++)
{
endBytes[i] = (byte)(startBytes[i] | ~maskBytes[i]);
}
return new IPAddress(endBytes);
}
// Helper method for comparing byte arrays
static int ByteArrayCompare(byte[] a1, byte[] a2)
{
int length = Math.Min(a1.Length, a2.Length);
for (int i = 0; i < length; i++)
{
int comparison = a1[i].CompareTo(a2[i]);
if (comparison != 0)
{
return comparison;
}
}
return a1.Length.CompareTo(a2.Length);
}
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment