Ipv6 to number conversion in java - java

I need to convert IpV6 address to long and vice-versa. I could not find any util or way to do this conversion. I am able to convert from IpV4 to long. But stuck in the above issue.So could you please suggest a suitable approach.
Please Help.....

You can't. At least not without losing information.
An IPv6 address contains 128-bits of information, while a long in Java only holds 64-bits of information, so any possible conversion scheme would lose information.
If you need to store an IPv6 address in Java, you should use the java.net.Inet6Address class.

Related

Implementing binary protocol Java

I am trying to implement a binary protocol in a Java Android application. The variables in this protocol are unsigned and are either uint32, uint16 or uint8.
I am having trouble with sending integer values. For example, when I try to send a short with a value of 1, the server (written in C++) receives a value of 256.
After searching a bit, I saw some posts talking about endianess and all but it doesn't really give me an answer.
How can I manage to get the bits stored in my variables in Java aligned in the same fashion as the one in C++.
Thanks
C++ does not define the order in which bytes in multibyte integers are stored. You should pick one standard and make sure that everyone uses it.
The standard API in Java has many classes that use big-endian byte order, so you might as well use that as the standard. To receive these correctly in C++ you can use the ntohl and ntohs functions for the conversion, for example.
I solved it! It was a problem of endianess. I solved it using the order() method of ByteBuffer.

Datatype to store range of IP addresses in Java

What data type is recommended to store range of IP addresses?
For eg,
"IP1":["12.21.31.0/24"],
"IP2":["13.96.210.122/28","12.33.116.17/21"]
The data is in JSON format. I have a java object that is parsing this JSON information. I am just not sure what data type to go ahead with in the Java class. Please suggest.
The InetAddress class would be the ideal type store IP addresses. It is a lot more than you've asked for here. But would be useful for getting IP from name/address or the other way round.
Since you are using CIDR masks not supported by INetAddress you'll need other types
Code sample using Apache SubnetUtils
String subnet = "12.21.31.0/24";
SubnetUtils utils = new SubnetUtils(subnet);
utils.getInfo().isInRange(address);
Code sample using CIDRUtils
CIDRUtils cidrUtils = new CIDRUtils("10.21.31.0/24");
String networkAddress = cidrUtils.getNetworkAddress();
String broadcastAddress = cidrUtils.getBroadcastAddress();
IP addresses can be represented as 32-bit integers (128-bit for IPv6). So, you could simply treat ranges as ranges of integers. You could then consider a TreeSet, a bit mask, or some other data structure, depending on how you query. Alternately, perhaps a RangeSet of either integers or a IP address wrapper class you write that implements Comparable would be a good solution.

Decimal formatted IPV6 ip address or extract IPV4 from IPV6

Is there any JAVA or LINUX way to extract decimal formatted IPV6 ip address.
Or
Is there any JAVA or LINUX way to covert IPV6 ip to IPV4 format.
Thanks.
Dnyanesh.
Is there any JAVA or LINUX way to extract decimal formatted IPV6 ip address.
No. IPv6 addresses are always presented in hexadecimal.
Or Is there any JAVA or LINUX way to covert IPV6 ip to IPV4 format.
No. They're completely different address types.
Well, IPv6 addresses are just 16 bytes (and a netmask), in the same way IPv4 addresses are 4 bytes. So, printing them in decimal is certainly possible. The question is, though, why you'd want to do that, given that everybody writes these addresses in hex.
While there is a specific address range within IPv6 for embedding IPv4 addresses (and you're free to create new ones in your own infrastructure), this is obviously not generically possible.
If your real question is "how do I talk to an IPv6 host if I only have IPv4", then the answer is more complicated and involves tunneling IPv6 traffic within IPv4.
http://www.networkworld.com/news/2010/050610-ipv6-tunnel-basics.html has a comprehensive overview about the options available. The TLDR summary: you probably should use Teredo for ad-hoc access to IPv6 (Linux client: http://www.remlab.net/miredo/). If you want to connect a server and can't get IPv6 service from your provider, the best answer used to be 6to4 (in my experience), but these days it's more useful to tell your provider to get their act together. Or change providers.
Depends on what you mean by extract.
The IPAddress Java library will do some or all of what you ask. The javadoc is available at the link. Disclaimer: I am the project manager.
The library parses IPv4 and IPv6. It allows you to produce various strings of different formats, and you could call the toNormalizedString(IPStringOptions) method to produce an IPv6 string with a decimal format, even though IPv6 is generally hexadecimal.
The library allows you to extract an IPv4 address from an IPv6 address using IPAddress.getEmbeddedIPv4Address(), as shown:
String str = "1::1";//ipv6 address
IPAddressString addrString = new IPAddressString(str);
try {
IPAddress addr = addrString.toAddress();//parse the ipv6 address
IPAddress addr2 = addr.getEmbeddedIPv4Address()//extract the ipv4 address 0.0.0.1 from the terminating bytes
...
} catch(AddressStringException e) {
//e.getMessage provides validation issue
}

Match public IP addresses with user-supplied netmasks

Recently I was given a task at my company where I have to create a function like this:
boolean addrMatch(String IP, String netMask);
This has nothing to do with routing. We have a network service that will use this function. The IP parameter varies upon all requests, the netMask parameter is user-supplied. The function must tell that an actual IP address matches with the supplied netmask or not. This is something like the user tells our system to only serve requests on the public internet to a specific subset of IP addresses, not all of them.
My networking related knowledge is far from complete, so I did a deep search on the topic, but I didn't get very far.
What I know (or been told): all the two parameteres are valid IP addresses or netmasks in xxx.xxx.xxx.xxx notation. I have to do a bitwise AND on them (obviously after converting them into BitSet or at least byte[] array). But I guess this is not the complete algorithm.
So my question: what is the correct algorithm for matching an IP address with a netmask?
ps.: I'm working in Java, but I need the generic method.
A netmask is just a bitmask. Basically if address & netmask != 0 the address is in the subnet represented by the netmask. The implementation details you have to cope with are bytes instead of bits, and varying numbers of bytes depending on whether you have IPv4 or IPv6. But it's basically trivial.

Java / TCP Traffic classes

Well I notice in Java and presumably other languages there is a Socket option similar to
setTrafficClass(int tc)
I know the application I am using has a traffic class of 24, however despite googling I cannot find a list of what these classes correspond to, nor a list of valid ones.
Please enlighten me.
md_5
According to the specification for Socket.setTrafficClass, we see:
For Internet Protocol v4 the value consists of an integer, the least significant 8 bits of which represent the value of the TOS octet in IP packets sent by the socket. RFC 1349 defines the TOS values as follows:
IPTOS_LOWCOST (0x02)
IPTOS_RELIABILITY (0x04)
IPTOS_THROUGHPUT (0x08)
IPTOS_LOWDELAY (0x10)
The last low order bit is always ignored as this corresponds to the MBZ (must be zero) bit.
24 is 0x18 i.e. 0x10 | 0x08, which corresponds to IPTOS_THROUGHPUT and IPTOS_LOWDELAY being set.
As you can see, the TOS only serves as a hint; it requests high-throughput, low-delay routing... which may or may not be serviced!
You can read more on types of service in RFC 1349 and the relevant Wikipedia article here.
The Javadocs have some details. Essentially, you're setting the TOS (type of service) header of your packet. The routing network may choose to use that as a suggestion on how to process the packet (or it might ignore it completely). A lot of networks don't actually do anything meaningful with this field, so I wouldn't rely on it's behaviour.
Traffic class is ultimately a matter between you and your nearest router. The field has been through several mutations. It also varies between IPv4 and IPv6. The first definition for IPv4 was given in RFC 791-5; this was revised in RFC 1349, and redefined completely in RFC 2474 as 'Differentiated Services'. The whole business may well have been revised again since I researched it for my book in 2003 or so. For IPv6 see RFC 2460. The stuff in the Javadoc about the IPTOS_* values refers to RFC 1349, and was already several years out of date when it was written.

Categories

Resources