1

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.

4

1 回答 1

0

网络掩码只是一个位掩码。基本上,如果address & netmask != 0地址在由网络掩码表示的子网中。您必须处理的实现细节是字节而不是位,并且字节数取决于您使用的是 IPv4 还是 IPv6。但这基本上是微不足道的。

于 2012-08-21T10:00:48.777 回答