且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何确定用户IP地址是否在可访问范围内?

更新时间:2022-05-03 05:32:08

至少,您需要将前N位与已知私有或已知未使用的网络块进行比较。最低限度是 RFC 1918私人网络:

At minimum, you need to be comparing the top N bits against known-private or known-unused network blocks. And the minimum of that is the RFC 1918 private networks:

10/8
172.16/12
192.168/16

(这种表示法意味着如果你屏蔽前8位并获得10,它就在第一个私有块中.12位== 172.16是第二位,等等)

(This notation means that if you mask off the top 8 bits and get "10", it's in the first private block. 12 bits == 172.16 is the second, etc.)

你可以检测到更多的块。

There are many more blocks you could detect.

最简单的例子是127/8,这些都是环回地址。您可能知道127.0.0.1,但事实上所有127.xyz地址都指的是您机器上的环回接口。

The easiest example is 127/8, which are all loopback addresses. You probably know about 127.0.0.1, but in fact all 127.x.y.z addresses refer to the loopback interface on your machine.

一个更加模糊的例子是5/8,其中已分配,但从未在广泛的互联网上使用,因此它被 Hamachi 所占用。但是,如果您的程序可以在可以访问Hamachi网络的计算机上运行,​​您可能需要小心测试。

A more obscure example is 5/8, which was assigned but never used on the wide Internet, so it's been appropriated by Hamachi. You would want to be careful about testing for this, though, if there's a chance that your program could be run on a machine with access to the Hamachi network.

另一组您可能希望忽略的地址是各种多播范围。你应该只向那些人发送UDP数据包,而不是TCP连接。

Another set of addresses you'll probably want to ignore are the various multicast ranges. You should only ever be sending UDP packets to those, never TCP connections.

如果你想要变得非常积极,你可以及时了解最新发布的作业。 IANA ,忽略尚未分配的块中的IP。但是,您希望经常更新该列表。

If you want to get really aggressive, you could keep up to date on the latest assignments published by IANA, ignoring IPs from blocks that haven't been assigned yet. You'd want to keep that list frequently updated, though.