I am developing application that manage a plain text, containing ip address ranges.
User will be able to add/delete ip from ranges.
Example to delete ip:
Ip entered by user through a text box : 25.2.0.58
Ip Range container: 25.2.0.10 - 25.255.255.255
the Algorithm should determine whether there is in text file, a range that may contain the ip entered by the user. If so, remove it from container ip range.
Results, (using previous example )
25.2.0.10 - 25.2.0.57
25.2.0.59 - 25.255.255.255
As you can see, Ip entered by user it has been extracted from, and two
new ranges has been created.
Any Algorithm that can be able to do so?
Thanks in advance.
Jota69
12 7340 jkmyoung 2,057
Recognized Expert Top Contributor
This seems like a search and split algorithm. I'm not sure how you're implementing it, thus I'm not sure where the problem is.
1. How are you holding the ranges? Are they always sorted?
2. How are you searching the ranges?
1. How are you holding the ranges? Are they always sorted?
Explanation:
There are 10 lists,(lis1.txt , list2.txt, list3.txt and so on.) involved in the process.
They all contain IP ranges.
Now you imagine such an application like a firewall, that uses these lists to filter harmful ips for network, and its users.
Well, Ipfile.txt file is the sum of all these lists.
Obviously ip ranges have to order, when added to the final file. (Ipfile.txt).
Example:
List1.txt
0.0.0.1-0.0.0.255
3.0.0.0-3.0.0.255
6.1.125.0-6.255.255.255
List2.txt
2.0.0.0-2.0.0.255
3.0.0.0-3.0.0.255
5.1.125.0-6.255.255.255
List3.txt
4.0.0.125-4.0.0.255
7.0.0.0-7.0.0.155
8.1.125.0-8.15.255.255
Ipfile.txt
0.0.0.1 - 0.0.0.255 - List1.txt
2.0.0.0 - 2.0.0.255 - List2.txt
3.0.0.0 - 3.0.0.255 - List2.txt
4.0.0.125 - 4.0.0.255 - List3.txt
5.1.125.0 -6.255.255.255 - List2.txt
6.1.125.0 - 6.255.255.255 - List1.txt
7.0.0.0 - 7.0.0.155 - List3.txt
8.1.125.0 - 8.15.255.255 - List3.txt
As you can see the ranges in the final file, are properly sorted.
For you to have an idea, read this:
The user of my application, they will can search, they can also add and remove IP ranges.
To do this, only you must enter the ip you want to delete, and press a button.
When you press the button "delete", this button should execute an algorithm capable of:
1-check the file, if there is a range of values that includes user-entered ip .
2 - In case if there is, then algorithm it should remove the ip range of values. As explained above.(my firts post).
2. How are you searching the ranges?
That my friend, is the origin of the question. I need an algorithm to do it for me.
Since....
IPv4 addresses are usually represented in dot-decimal notation (four numbers, each ranging from 0 to 255, separated by dots, e.g. 208.77.188.166) . Each part represents 8 bits of the address, and is therefore called an octet. In less common cases of technical writing, IPv4 addresses may be presented in hexadecimal, octal, or binary representations . In most representations each octet is converted individually.
I get octets splitting (as you point below) ip address, entered by user. and same with each file line. (delimited file(-)). e.g.
208.77.188.166 - 208.77.188.183
208.77.188.195 - 208.77.188.206
208.77.188.210 - 208.77.188.255
Ip to excluded from range, (entered by user) : 208.77.188.236
Range container: 208.77.188.210 - 208.77.188.255
Notice that we can to merge overlapping and adjacent ranges. (I need logaritm do it so)
e.g. 208.77.188.166 - 208.77.188.255.
Note all ranges are included in only one line, saving file size. (We are talking about 500.000 lines)
But, lets come back to mean target = Find possible range container, extract user ip from, and delete it. How..?
Creating two new ranges, e.g.
Ip to excluded from range, (entered by user) : 208.77.188.236
Range container: 208.77.188.210 - 208.77.188.255
Results (I want to get, using any algoritm)
208.77.188.166 - 208.77.188.235
208.77.188.237 - 208.77.188.255
Also well be nice, if algoritm locate and delete duplicate ranges.
Firts at all, split file lines.
208.77.188.166 - 208.77.188.235, you get, IpStart and IpEnd.
IpStart = 208.77.188.166
IpEnd = 208.77.188.235 - Split(currentField, "-")
-
'MsgBox("Ip: " & currentField)
-
pos = InStrRev(currentField, "-")
-
IpEnd = Strings.Mid(currentField, pos + 1)
-
IpStart = Strings.Left(currentField, currentField.Length -IpEnd.Length -1)
Getting ip address Octets... -
sIP = IpStart
-
arrIP = Split(sIP, ".")
-
sFormattedIP = Format(CLng(arrIP(0)), "000") & "." & Format(CLng(arrIP(1)), "000") _
-
& "." & Format(CLng(arrIP(2)), "000") & "." & Format(CLng(arrIP(3)), "000")
-
IpStart = sFormattedIP
Note: same process to get user ip and IpEnd.
When you get ip address octets, you are able to compare to.
208.77.188.236
208
77
188
236
How..?
Maybe we feel most comfortable working with long values instead of ip address value.
OK, Lets converte ip address to long value.
Firts, user ip: -
user0 = CLng(CLng(arrIP2(0)) * (256 ^ 3))
-
user1 = CLng(CLng(arrIP2(1)) * (256 ^ 2))
-
user2 = CLng(CLng(arrIP2(2)) * (256 ^ 1))
-
user3 = CLng(CLng(arrIP2(3)) * (256 ^ 0))
-
user = CLng(user0 + user1 + user2 + user3)
IpStart: -
arr0 = CLng(CLng(arrIP(0)) * (256 ^ 3))
-
arr1 = CLng(CLng(arrIP(1)) * (256 ^ 2))
-
arr2 = CLng(CLng(arrIP(2)) * (256 ^ 1))
-
arr3 = CLng(CLng(arrIP(3)) * (256 ^ 0))
-
arr = CLng(arr0 + arr1 + arr2 + arr3)
IpEnd: -
arra0 = CLng(CLng(arrIP1(0)) * (256 ^ 3))
-
arra1 = CLng(CLng(arrIP1(1)) * (256 ^ 2))
-
arra2 = CLng(CLng(arrIP1(2)) * (256 ^ 1))
-
arra3 = CLng(CLng(arrIP1(3)) * (256 ^ 0))
-
arra = CLng(arra0 + arra1 + arra2 + arra3)
Well, now we are able to CompareTo..
Is user>IpStart and user<IpEnd...?? ??? -
If CULng(user) > CULng(arr) And CULng(user) < CULng(arra) Then
-
gString = String.Concat(IpStart, Dash, IpEnd)
-
MsgBox("gString: " & gString)
If so, then... -
If (MaxValue - (CLng(arrIP(3))) < MaxValue) Then
-
sFormattedIP2 = Format(CLng(arrIP2(0)), "000") & "." & Format(CLng(arrIP2(1)), "000") _
-
& "." & Format(CLng(arrIP2(2)), "000") & "." & Format(CLng(arrIP2(3) - 1), "000")
-
SearchLine = sFormattedIP2
-
LineFile = String.Concat(IpStart & " - " & SearchLine & " " & Level & " " & Description)
-
-
ElseIf (MaxValue - (CLng(arrIP(3))) = MaxValue) And _
-
(MaxValue - (CLng(arrIP(2))) < MaxValue) Then
-
sFormattedIP2 = Format(CLng(arrIP2(0)), "000") & "." & Format(CLng(arrIP2(1)), "000") _
-
& "." & Format(CLng(arrIP2(2) - 1), "000") & "." & Format(CLng(arrIP2(3) + 9), "000")
-
SearchLine = sFormattedIP2
-
LineFile = String.Concat(IpStart & " - " & SearchLine & " " & Level & " " & Description)
-
-
ElseIf (MaxValue - (CLng(arrIP(3))) = MaxValue) And _
-
(MaxValue - (CLng(arrIP(2))) = MaxValue) And (MaxValue - (CLng(arrIP(1))) < MaxValue) Then
-
-
sFormattedIP2 = Format(CLng(arrIP2(0)), "000") & "." & Format(CLng(arrIP2(1) - 1), "000") _
-
& "." & Format(CLng(arrIP2(2)), "000") & "." & Format(CLng(arrIP2(3)), "000")
-
SearchLine = sFormattedIP2
-
LineFile = String.Concat(IpStart & " - " & SearchLine & " " & Level & " " & Description)
-
End If
-
-
sFormattedIP3 = Format(CLng(arrIP2(0)), "000") & "." & Format(CLng(arrIP2(1)), "000") _
-
& "." & Format(CLng(arrIP2(2)), "000") & "." & Format(CLng(arrIP2(3) + 1), "000")
-
SearchLine = sFormattedIP3
-
LineFile1 = String.Concat(SearchLine & " - " & IpEnd)
Note: MaxValue is a Const = 255
It work ok.
So, what I'm looking for?
Although this code works, I'm not satisfied, I am sure that somewhere there is an algorithm that does the same, but in a more technical and elegant way.
Any idea??
Thanks in advance.
jkmyoung 2,057
Recognized Expert Top Contributor
What language are you using? Is it ASP, or .NET C++?
Note, when you compare, you need to check the endpoints too: - You have:
-
CULng(user) > CULng(arr) And CULng(user) < CULng(arra)
-
Should be:
-
CULng(arr) <= CULng(user) And CULng(user) <= CULng(arra)
-
Also rearranged the direction to use <= so it's easier to understand.
Determining overlapping of two ranges: -
1st range: start1 - end1
-
2nd range: start2 - end2
-
-
Pseudo: (because I'm lazy)
-
if (start1 <= start2 <= end1) || (start1 <= end2 <= end1) we overlap
Merging 2 ranges: - min = (start1 < start2 ? start1 : start2)
-
max = (end1 < end2 ? end2 : end1)
-
New range: (min --- max)
I don't understand why the author has chosen to do it that way in the last part of the code. If there is an intersection, we should still use the longs we had before! Convert it backwards.
eg. - long ip = some value
-
ip0 = ip >> 24;
-
ip1 = (ip >> 16) %256
-
ip2 = (ip >> 8) % 256
-
ip3 = ip % 256
Since you're dealing exclusively within ranges, creating the new ranges will never put you out of range.
Removing ip from range: - long ip = some value to be removed
-
long start
-
long end
-
-
// do range before ip
-
if(! (ip == start)){
-
start1 = start;
-
end1 = ip--;
-
// save this range.
-
}
-
//do range after ip
-
if (!(ip == end)){
-
start2 = ip++;
-
end2 = end;
-
//save this range too.
-
}
I use Visual basic (vs 2005) and WXP sp3.
I have no knowledge of c #. I'm trying to adapt your code to mine.
I must assume that this is correct?
1st range = Ip range entered by user, (208.77.188.236 - 208.77.188.243)
2nd range = Ip range in textline
start1 = userIpStart = 208.77.188.236
end1= userIpEnd = 208.77.188.243
start2 = arr
end2 = arra
Now, adapting your code to mine, would be as follows:
if (start1 <= start2 <= end1) || (start1 <= end2 <= end1) we overlap
- if (userIpStart <= arr <= userIpEnd) OrElse _
-
(userIpStart <= arra <= userIpEnd) then
-
-
End If
Is this correct?
Another one.. - ' do range before ip
-
If Not (ip = start) Then
-
start1 = start
-
' save this range.
-
end1 = System.Math.Max(System.Threading.Interlocked.Decrement(ip),ip + 1)
-
End If
-
-
'do range after ip
-
If Not (ip = [end]) Then
-
start2 = System.Math.Max(System.Threading.Interlocked.Increment(ip),ip - 1)
-
'save this range too.
-
end2 = [end]
-
End If
is it correct..?
jkmyoung 2,057
Recognized Expert Top Contributor
My Vb is rusty. I think that looks right, although not sure why you have [end] in brackets.
Also when I say 'Save this range, I mean: actually put the range back into your array of ranges. call a function to do it with the 2 arguments, if needed. eg
AddRange(start1 , end1)
...
...
AddRange(start2 , end2)
About brackets, You are right.
I modified the code, but forgot to change it in the post.
Now I could not find the equivalent of these operators in vb. (?) and (:) and (---)
Can you help me, with this block of code? - min = (start1 < start2 ? start1 : start2)
-
max = (end1 < end2 ? end2 : end1)
-
New range: (min --- max)
I am storing start,end,start 1,end1 values, in SortedList.
My knowledge about the language C # are zero, and your knowledge of vb is rusty, I think we makes a good team. :)
jkmyoung 2,057
Recognized Expert Top Contributor
Seriously? That's pseudocode. I'm done here.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Wahoo |
last post by:
Dear All,
What is the name of sorting algorithm used in list (STL) class? and how it
work?
Thanks!!!
Best Regards,
Wahoo
|
by: stemc © |
last post by:
Hi there,
In work, we often mail merge letters and post them to contacts. But more
and more, we've been emailing information to people instead. So far, I've
been writing a single generic email, then copying and pasting the email
addresses into the address fields. I start the email off with 'Dear all...'
But is there a way to 'email merge' this directly into outlook emails, in
the same way we do for normal printed letters? This...
|
by: Squirrel |
last post by:
Hi everyone,
I've created a mail merge Word doc. (using Office XP) , the data source is
an Access query.
Functionality I'm attempting to set up is:
User sets a boolean field to true for each person for whom a mail
merge letter is desired.
The query reads address info from the table for each record where is
true.
|
by: Andy Davis |
last post by:
I have set up a mail merge document in Word 2003 which gets its data from my
Access 2000 database. I want to set up a button on a form that:
1. runs the query to provide the dat for the merge document in Word;
2. opens the document and runs the merge process for the new data.
I have managed to write the code to perform step 1 ok, but I'm having
trouble with step 2. It opens the word document fine but does not perform
the mail merge of...
|
by: rkk |
last post by:
Hi,
I have written a generic mergesort program which is as below:
---------------------------------------------------------
mergesort.h
-----------------------
void
MergeSort(void *array,int p,int r,int elemSize,int(*Compare)(const
void *keyA,const void *keyB));
| |
by: Joerg Schoen |
last post by:
Hi folks!
Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.)
For linked lists, mergesort is the typical choice.
While I was looking for a optimized implementation of mergesort for
linked lists, I couldn't find one. I read something about Mcilroy's
"Optimistic Merge Sort" and studied some implementation, but they
were for arrays. Does anybody know if Mcilroys optimization is applicable to
truly linked lists at all?
|
by: vekka |
last post by:
Hi!
Could someone please help me to understand the use of merge algorithm(conceptually) with linked lists. What I want to do in the linked list function is:
the function gets two inputs, i.e the pointers to the two smaller lists, then the function should able to merge these two together, and then return the head-pointer to this merged list. What happens in my case, is that I "lose" data when running it . My result is like this:
Even OR...
|
by: subramanian100in |
last post by:
consider :
template<class InIt1, class InIt2, class OutIt>
OutIt merge(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2,
OutIt dest);
Can the destination container already contain some elements ? If so,
will they also be taken into account for doing the sorting and
merging ? Or, should the destination container have just enough space
to hold the result of the merge( that is, it should not contain any
|
by: subramanian100in |
last post by:
Consider the following program:
#include <cstdlib>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <iterator>
#include <utility>
|
by: slapsh0t11 |
last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment:
Here is my class file (Sorts.java):
import java.util.*;
/**
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |