Getting IP address list between two IP addresses - java

I am writing a program that gets From IP address and To IP address from the user and displays the list of IP addresses between them. For example, if the user gives 10.0.0.1 and 10.0.0.5 then I will display the five IP addresses between these two. The current solution that is coming in my mind are:
To have a list of all IP addresses and then look for the resultant IP address list
Use a nested loop
What solution should I adopt between these (or suggest a better solution)? For the first solution, what is the link for IP address table/list?
What is the solution in terms of JavaScript or Java?

First split the IP addresses with .. From the first IP address, start increasing the fourth part up to 255 and then add 1 to the third part and set the fourth one to 1. Until you reach the to IP address.
IP address bytes -> bits -> Int32
From: 10.0.10.10 -> 00001010 00000000 00001010 00001010 -> 167774730
To: 10.1.45.1 -> 00001010 00000001 00101101 00000001 -> 167849217
Start count from From to To and just check the unwanted bytes which is 11111111 and 00000000.
That's all.

The "dot"-writing is for humans. For computers, it is one 4-byte-number. So parse it to a number. Then you will get all addresses in the range by simply increasing a number until the bound is reached and format them back for output.

I was experimenting in an updated jsFiddle, and finally I came to the solution below. The following code should work for all IP addresses. You have to provide a start and end IP address in hex (since it is easy, I did not write code for it).
var startIp = 0x0A000001,
endIp = 0x0A000F05;
var temp, list = [],str;
for(var i=startIp ; i <= endIp ; i++){
temp = (i).toString(16);
str ='';
if(temp.length == 7){
temp = "0"+temp;
}
for(var k=temp.length-1; k >= 0 ; k-=2){
str = parseInt(temp[k-1] + "" + temp[k], 16) +"." + str ;
}
document.write(temp + " " + str+ "<br>");
list.push(str.substring(0, str.length-1));
}
?

If you know enough to get the addresses (from the network or from the file system or user input), you can test the address itself with subtraction and get the number of IP addresses right there.
This is simplified, but you will get it if you know about addresses: 000044-000002 = 000042.

Related

How to compare two CSV file?

I have two CSV file 1st one IP Geo-location CSV file and second one have date time and IP address how to compare both file and make new CSV file have date time IP address and IP location(country name).How to make it in java?
1st File
18102015 11:35:59 93.178.13.37:2065
18102015 11:36:00 93.178.13.37:2078
18102015 11:36:21 93.178.13.37:7251
18102015 11:37:05 222.35.153.160:502
18102015 11:37:05 222.35.153.160:5050
2nd File
93.178.0.0 93.178.63.255 1571946496 1571962879 SA Saudi Arabia
93.178.64.0 93.178.127.255 1571962880 1571979263 RU Russian Federation
222.16.0.0 222.95.255.255 3725590528 3730833407 CN China
I want Result:
18102015 11:35:59 93.178.13.37 2065 SA Saudi Arabia
18102015 11:36:00 93.178.13.37 2078 SA Saudi Arabia
18102015 11:36:21 93.178.13.37 7251 SA Saudi Arabia
18102015 11:37:05 222.35.153.160 5029 CN China
18102015 11:37:05 222.35.153.160 5050 CN China
Hints:
Think of a dotted IP address as 4 octets of a 32 bit number. For example, the IP address 93.178.63.255 is really (93*(2^24)) + (178*(2^16)) + (63*(2^8)) + 255. Do the math, and you come up with a number that represents the IP address.
The second file has IP ranges (low to high). If you convert the low and high IP addresses of the range, you'll come up with two numbers. Therefore, if you're trying to detect if a certain IP address (from file #1) is within a range (in file #2), it's simple. if IP >= lowIP && IP <= highIP it's in the range.
Further advice: parse file #2 and create objects for each of them. The object will have most notably the starting IP address, the ending IP address, and the country. Then you can parse file #1, search within your collection of objects for a match, and then output what you need to.

how to search for the ip addresses in cidr notation

I need to search for the entries in the mysql database with following ipaddress “192.168.0.1/20 ”
Note: ip addresses stored are varchars (ex. 192.168.0.3,192.168.0.4)
Address: 192.168.0.1 11000000.10101000.0000 0000.00000001
Netmask: 255.255.240.0 = 20 11111111.11111111.1111 0000.00000000
Wildcard: 0.0.15.255 00000000.00000000.0000 1111.11111111
=>
Network: 192.168.0.0/20 11000000.10101000.0000 0000.00000000
HostMin: 192.168.0.1 11000000.10101000.0000 0000.00000001
HostMax: 192.168.15.254 11000000.10101000.0000 1111.11111110
Broadcast: 192.168.15.255 11000000.10101000.0000 1111.11111111
Hosts/Net: 4094 Class C, Private Internet
my solution is to find to the network address from given cidr notation (replace 0 with "*") and search for 192.168.*.* entries in database as they are stored in strings. It works but I am not sure whether this is correct
can any point out the problem or more optimal solution than this?
The following works if you have single IP address per VARCHAR field.
Correct way to match IP addresses against their network using netmask is through INET_ATON()/INET_NTOA().
There are several steps, though. First use the following to convert your length to subnetmask:
SET #l=20;
SELECT INET_NTOA(0xffffffff >> (32-#l) << (32-#l));
That's just to convey the idea, to make this useful you could do the following:
SELECT 0xffffffff >> (32-#l) << (32-#l) INTO #mask;
For the actual matching of the address against your network address you can use INET_ functions with bitwise operators, for example:
SET #l=20;
SET #nw='192.168.0.0';
SELECT 0xffffffff >> (32-#l) << (32-#l) INTO #mask;
SELECT * FROM yourtable
WHERE (INET_ATON(addrfield) & #mask) = INET_ATON(#nw);
Of course you may roll this into a single statement like:
SELECT * FROM yourtable
WHERE (INET_ATON(addrfield) & (0xffffffff >> (32-#l) << (32-#l))) = INET_ATON('192.168.0.0');

Memcached cross client compatability issue between telnet and Java (com.danga)

I am new to memcached and getting to know an old friend, Java - am running on win x64 with java 1.7. Also running the couchbase memcache server on my local win 64 machine via a setup file couchbase-server-enterprise_2.2.0_x86_64. Everything generally works fine until I noticed a strange behavior when doing string comparisons with a key set in the telnet session and checking this key in java.
From the telnet session
set s1 1 0 4
abcd
STORED
set s2 32 0 4
abcd
STORED
From my main java test class:
...
System.out.println("Get s1 from Cache:" +mcca.get("s1"));
System.out.println("Get s1 from Cache:" +mcca.get("s1",1));
System.out.println("Get s1 from Cache:" +mcca.get("s1",32));
System.out.println("Get s1 from Cache:" +mcca.get("s1",77, true));
System.out.println("Get s2 a from Cache:" +mcca.get("s2"));
System.out.println("Get s2 b from Cache:" +mcca.get("s2",1));
System.out.println("Get s2 c from Cache:" +mcca.get("s2",32));
System.out.println("Get s2 c from Cache:" +mcca.get("s2",77, true));
Outputs
Get s1 from Cache:97
Get s1 from Cache:97
Get s1 from Cache:97
com.danga.MemCached.MemCachedClient Mon Dec 30 11:50:06 EST 2013 - ++++ retrieving object and stuffing into a string.
Get s1 from Cache:abcd
Get s2 a from Cache:abcd
Get s2 b from Cache:abcd
Get s2 c from Cache:abcd
com.danga.MemCached.MemCachedClient Mon Dec 30 11:50:06 EST 2013 - ++++ retrieving object and stuffing into a string.
Get s2 c from Cache:abcd
I was looking here: http://www.geelou.com/javadocs/java_memcached-release_2.0.1/com/danga/MemCached/MemCachedClient.html but I didn't see any explanation about the hashCode and whether it corresponds to the same flag/metadata parameter in the memcached server.
I think my question roughly boils down to: can the com.danga get command hashCode parameter value be changed from 32 so that I can get the full string when I set the s1 key with metadata/flag 1 as shown above without having to specify the asString flag or mcca.setPrimitiveAsString(true)?
And related, why does
System.out.println("Get s2 a from Cache:" +mcca.get("s2"));
print what seems to be the correct value of abcd whereas neither of:
System.out.println("Get s1 from Cache:" +mcca.get("s1"));
System.out.println("Get s1 from Cache:" +mcca.get("s1",1));
print what seems to be the correct value of abcd?
As stated in this answer Memcached getting null for String set with python and then get from Java I can work around my issue using
mcca.setPrimitiveAsString(true);
mcca.setSanitizeKeys(false);
pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);
but still I do not understand why the difference appears, and if/how I can modify the call to get parameters to fix it
Notes:
The reason for setting the flag/metadata parameter to 32 is that in another little java test I ran
System.out.println("set 1 status:" + mcc.set("1", "Modified"));
//which outputs
com.danga.MemCached.MemCachedClient Fri Dec 27 00:12:51 EST 2013 - ++++ memcache cmd (result code): set 1 32 0 8
(STORED)
This seems to indicate that the com.danga library is using a flag/metadata value of 32.
I think my question boils down to From memcache telnet session
set s1 1 0 4
abcd
set s2 32 0 4
abcd
From java Why does
mcca.get("s1")// only gives the first ascii character code (97)
mcca.get("s2")// but gives the entire string. What is so special about the second memcache command using the hash of 32?
I think the problem is that your "flag" parameter is telling your library that the data is stored differently. I'm not sure why you're specifying "1" and "32" in the telnet SET commands, but you might want to test it with a different flag value specified.
The "1" and "32" you're specifying are often used by client libraries to specify the format the data is in. They are not hash values.
I think you mixed the parameters.
Telnet:
set key metaData expiryTime lengthInBytes
set s1 1 0 4
Java:
https://github.com/gwhalin/Memcached-Java-Client
mc.get(key, hash); // <- retrieves key by hash
mc.keyExists(key);
mc.get(key, null, true);
https://github.com/gwhalin/Memcached-Java-Client/blob/master/src/com/meetup/memcached/MemcachedClient.java#L1237

Search hierarchical text in Oracle database

Table = BLOCK (Has composite unique index both the columns)
IP_ADDRESS CIDR_SIZE
========= ==========
10.10 16
15.0 16
67.7 16
18.0 8
Requirements:
Sub block is not allowed. For e.g. 67.7.1 and 24 is not allowed as this is child of 67.7. In other words, if there is any IP address in the database that matches beginning portion of new IP, then it should fail. Is it possible for me to do it using a Oracle SQL query?
I was thinking of doing it by...
Select all records into the memory.
Convert each IP into its binary bits
10.10 = 00001010.00001010
15.0 = 00001111.00000000
67.7 = 01000011.00000111
18.0 = 00010010.00000000
Convert new IP into binary bit. 67.7.1 = 01000011.00000111.00000001
Check to see if new IP binary bits start with existing IP binary bits.
If true, then the new record exists in the database.
For example, new binary bit 01000011.00000111.00000001 does start with existing ip (67.7) binary bits 01000011.00000111. Rest of records don't match.
I am looking to see if there a Oracle query that can do this for me, that is return the matching IP addresses from the database. I checked out Oracle's Text API, but didn't find anything just yet.
Is there a reason you can't use the INSTR function?
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions068.htm#i77598
I'd do something like a NOT EXISTS clause that checks for INSTR(b_outer.IP_ADDRESS,b_inner.IP_ADDRESS) <> 1
*edit: thinking about this you'd probably need to check to see if the result is 1 (meaning the potential IP address matches starting at the first character of an existing IP address) as opposed to a general substring search as I originally had it.
Yes you can do it in SQL by converting IP's to numbers and then ensureing this is not a record with a smaller cidr size that gives the same ipnum when using its cidr size.
WITH ipv AS
( SELECT IP.*
, NVL(REGEXP_SUBSTR( ip, '\d+', 1, 1 ),0) * 256 * 256 * 256 -- octet1
+ NVL(REGEXP_SUBSTR( ip, '\d+', 1, 2 ),0) * 256 * 256 -- octet2
+ NVL(REGEXP_SUBSTR( ip, '\d+', 1, 3 ),0) * 256 -- octet3
+ NVL(REGEXP_SUBSTR( ip, '\d+', 1, 4 ),0) AS ipnum -- octet4
, 32-bits AS ignorebits
FROM ips IP
)
SELECT IP1.ip, IP1.bits
FROM ipv IP1
WHERE NOT EXISTS
( SELECT 1
FROM ipv IP2
WHERE IP2.bits < IP1.bits
AND TRUNC( IP2.ipnum / POWER( 2, IP2.ignorebits ) )
= TRUNC( IP1.ipnum / POWER( 2, IP2.ignorebits ) )
)
Note: My example uses the table equivalent to yours:
SQL> desc ips
Name Null? Type
----------------------------------------- -------- ----------------------------
IP NOT NULL VARCHAR2(16)
BITS NOT NULL NUMBER

Extract the results of a GWT service

Messy, complicated question, but here goes. I'm working on an integration project with Google Checkout, and there is a Google Checkout GWT service that returns the currency conversion rates used by the Checkout web interface to convert USD into local currencies. This endpoint is hosted at https://market.android.com/publish/gwt/, and staring at Firebug I see this going to the server:
7|0|6|https://market.android.com/publish/gwt/|FCCA4108CB89BFC2FEC78BA7363D4AF6|com.google.wireless.android.vending.developer.
shared.MerchantService|getCurrencyExchangeRates|com.google.common.money.CurrencyCode/112449834|java.util.ArrayList/4159755760
|1|2|3|4|2|5|6|5|235|6|13|5|18|5|81|5|53|5|72|5|102|5|121|5|177|5|175|5|205|5|204|5|55|5|86|-1|
and this being returned
//OK[235,3,'D0JA',2,86,3,'CXXg',2,55,3,'DW2A',2,204,3,'X9NA',2,205,3,'EuvA',2,175,3,'VIig',2,177,3,'E2Dw',2,121,3,'E4ziA',2,1
02,3,'do$Q',2,72,3,'T82w',2,53,3,'Ds0Q',2,81,3,'Cq5g',2,18,3,'Dlfg',2,13,1,["com.google.common.collect.RegularImmutableList/4
40499227","com.google.common.money.SimpleMoney/627983206","com.google.common.money.CurrencyCode/112449834"],0,7]
Forgive the odd formatting: can't quite get the code block to format right.
Wandering the web for hours on end I was able to determine that the RegularImmutableList class is in the Guava libraries (at http://code.google.com/p/guava-libraries/). What I'm looking for is:
I can't find the com.google.common.money.SimpleMoney or com.google.common.money.CurrencyCode classes anywhere: anyone seen them?
The GWT wire format appears to be an odd JSON string. I see various references to Google Groups messages talking about descriptions of the wire format, but can't find the underlying messages or any coherent reference that would let me reverse this: anyone have a handle on a handy reference? If I can at least understand WHAT the encoding is I might be able to get away without the class files from question 1 above.
I started wandering through the Android Market api library at http://code.google.com/p/android-market-api/, figuring they have to have done SOME of the Android Market communication integration, and they appear to have done so using protobufs. Is there any decent reference for the GWT/protobufs communication bits?
The underlying reason for this craziness is that I need to be able to take regular exchange rate values from Google Checkout so when I'm importing sales transactions in foreign currencies I can do the conversion at the prevailing rate at the time of the transaction. The current Checkout reporting formats do NOT provide this, so most folks end up using alternative sources of exchange rates that don't match what Google uses. It is clearly a shortcoming on the part of Google Checkout's integration interface, but if we got started on shortcomings of Google Checkout's interface we'd be here all week. My intention is to poll the Checkout interface for newly fulfilled orders and then request the appropriate exchange rate table so I can figure out in near real-time what the incoming payments are. I've got the polling bit down pat but can't quite get past the exchange rate bit.
While trying to create a script to bulk upload in-app products for my application (CSV upload constantly failed with obscure error messages), I have managed to understand the GWT AJAX protocol.
It's actually pretty simple, except it requires you to know structure of all used classes. Or guess it, as is the case with internal classes used by Google. :)
I'll use examples from the question to explain the protocol in detail.
Request format
7|0|6|https://market.android.com/publish/gwt/|FCCA4108CB89BFC2FEC78BA7363D4AF6|com.google.wireless.android.vending.developer.shared.MerchantService|getCurrencyExchangeRates|com.google.common.money.CurrencyCode/112449834|java.util.ArrayList/4159755760|1|2|3|4|2|5|6|5|235|6|13|5|18|5|81|5|53|5|72|5|102|5|121|5|177|5|175|5|205|5|204|5|55|5|86|-1|
The request is pipe-delimited list of tokens with the following meaning:
7 - protocol version
0 - flags. 1 is FLAG_ELIDE_TYPE_NAMES, 2 is FLAG_RPC_TOKEN_INCLUDED
6 - string token count
6 string tokens:
https://market.android.com/publish/gwt/
FCCA4108CB89BFC2FEC78BA7363D4AF6
com.google.wireless.android.vending.developer.shared.MerchantService
getCurrencyExchangeRates
com.google.common.money.CurrencyCode/112449834
java.util.ArrayList/4159755760
The actual encoded request, which references strings from the list above using 1-based indices:
1 - https://market.android.com/publish/gwt/ - base URL
2 - FCCA4108CB89BFC2FEC78BA7363D4AF6 - some hash, which is references as serializationPolicyStrongName in GWT sources.
3 - com.google.wireless.android.vending.developer.shared.MerchantService - service name
4 - getCurrencyExchangeRates - method name
2 - parameter count. Parameter types follow:
5 - com.google.common.money.CurrencyCode/112449834
6 - java.util.ArrayList/4159755760
Serialized parameters. Each object is represented either by its classname and list of serialized fields or by negative integer back-reference to previously encountered object. In our case we have two objects:
5 - com.google.common.money.CurrencyCode/112449834, which only has one integer field: 235
6 - java.util.ArrayList/4159755760, which has one integer length field 13, followed by 13 serialized list items. Note that 12 of them are CurrencyCode objects serialized just as the above one, and the last one is a backreference (-1) to the very first object we encountered while (de-)serializing this request, i.e. CurrencyCode(235)
Response format
//OK[235,3,'D0JA',2,86,3,'CXXg',2,55,3,'DW2A',2,204,3,'X9NA',2,205,3,'EuvA',2,175,3,'VIig',2,177,3,'E2Dw',2,121,3,'E4ziA',2,102,3,'do$Q',2,72,3,'T82w',2,53,3,'Ds0Q',2,81,3,'Cq5g',2,18,3,'Dlfg',2,13,1,["com.google.common.collect.RegularImmutableList/440499227","com.google.common.money.SimpleMoney/627983206","com.google.common.money.CurrencyCode/112449834"],0,7]
The response is very similar in format to the request except it's JS-formatted array (though not JSON, as it uses invalid single quotes), and it's in reverse order.
The field meaning is as follows:
7 - protocol version
0 - flags, same as for request
Array of string tokens:
com.google.common.collect.RegularImmutableList/440499227
com.google.common.money.SimpleMoney/627983206
com.google.common.money.CurrencyCode/112449834
And then goes one serialized object of type 1 - com.google.common.collect.RegularImmutableList/440499227 with one integer length field 13, followed by 13 serialized objects of class 2 - com.google.common.money.SimpleMoney/627983206. Each SimpleMoney object has two fields, for example:
'Dlfg' - long integer field encoded as base64 number. This particular one is 940000
3, 18 - CurrencyCode object with integer field 18
What you are looking at is GWT-RPC serialization format. Unfortunatelly it is not publicly documented. Fortunatelly GWT is open-source so you could look at the source to see how it is produced.
Note: This format might change between GWT versions (I known it did in 2.2). This is most likelly also a reason why Google does not document it - if they did they'd need to keep it backward compatible.
Class names that you see are Java classes that Google Checkout uses internally. When GWT is compiled to JS the names get mangled so you don't see them any more.
As noted this is GWT-RPC.
What you are trying to do is reverse-engineer Google internal APIs. I wouldn't do that because, a. It might change without notice, breaking your app and, b. I'm sure Goog wouldn't like it and it probably violates the service agreement (have you read it?).
I have some code made in VB that may be useful for you to realize how to parse GWT Serialized strings. "Datos" contains the string you received.
aAux = Split(Datos, ",[")
aAux(1) = Replace(aAux(1), "],0,7]", "")
aAux(0) = Replace(aAux(0), "//OK[", "")
aAux(0) = Replace(aAux(0), "'", "")
aDescripcion = Split(aAux(1), """,""")
aValor = Split(aAux(0), ",")
InvertirArray aValor
For X = 0 To UBound(aValor)
If Not IsNumeric(aValor(X)) Then
Exit For
End If
If adescripcion(Int(aValor(X))-1) = "gov.senasa.embalajemadera.shared.domain.Pais/3238585366" Then
For Y = X + 1 To UBound(aValor)
If Int(aValor(Y)) = "" Then '- Do what you want
end if
If adescripcion(Int(aValor(Y))) = "java.lang.Integer/3438268394" Then
'- Do what you want
Next Y
End If
Next X
Of course you have to adapt it to your needs and you will have to play a little bit with the arrays...
InvertirArray:
Public Sub InvertirArray(ByRef Arr() As String)
'- el array va tiene que empezar en 0
Dim X As Long
Dim Hasta As Long
Dim Tmp As String
If UBound(Arr) Mod 2 = 0 Then
'- Es impar
Hasta = UBound(Arr) + 1
Else
Hasta = UBound(Arr)
End If
For X = LBound(Arr) To UBound(Arr) \ 2
Tmp = Arr(X)
Arr(X) = Arr(UBound(Arr) - X)
Arr(UBound(Arr) - X) = Tmp
Next X
end sub
And of course you need to decode and encode Long Numbers and dates. So:
Public Function EncodeDateGwt(Numero As Double, Optional isDate As Boolean = False) As String
Dim s As String
Dim a As Double
Dim i As Integer
Dim u As Integer
Dim Base As String
Numero = IIf(isDate, Numero * 1000, Numero)
Base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$"
Do While Val(Numero) <> 0
a = Numero
i = 0
Do While a >= 64
i = i + 1
a = a / 64
Loop
If i <> u - 1 And u <> 0 Then EncodeDateGwt = EncodeDateGwt & String(u - i - 1, Left(Base, 1))
a = Int(a)
EncodeDateGwt = EncodeDateGwt + Mid(Base, a + 1, 1)
Numero = Numero - a * (64 ^ i)
u = i
Loop
EncodeDateGwt = EncodeDateGwt & String(i, Left(Base, 1))
End Function
Public Function DecodeDateGwt(Texto As String, Optional isDate As Boolean = False) As Long
Dim Base As String
Dim a As Integer
Base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$"
For a = 1 To Len(Texto)
DecodeDateGwt = DecodeDateGwt + (InStr(Base, Mid(Texto, a, 1)) - 1) * (Len(Base) ^ ((Len(Texto) - (a))))
Next
DecodeDateGwt = IIf(isDate, DecodeDateGwt / 1000, DecodeDateGwt)
'devuelve timestamp
End Function
If what you need to encode/decode is a date, then you need to do this before:
Call encodegwtdate(date2unix("20/02/2016"),true)
Public Function Date2Unix(ByVal vDate As Date) As Long
Date2Unix = DateDiff("s", Unix1970, vDate)
End Function
Public Function Unix2Date(vUnixDate As Long) As Date
Unix2Date = DateAdd("s", vUnixDate, Unix1970)
End Function
Hope you solve it. By the way, does anyone knows what negative numbers means?????

Categories

Resources