简体   繁体   English

如何使用cidr获取客户的IP地址

[英]How do I get client's ip address with cidr

I tried getting Public IP address from HttpServletRequest object using 我尝试使用以下方法从HttpServletRequest对象获取公共IP地址

httpServletRequest.getRemoteAddress();

which returns simple public ip address eg 123.21.21.12 but what I need is 132.21.21.12/8 . 它返回简单的公共IP地址,例如123.21.21.12,但我需要的是132.21.21.12/8

I have checked this How to get client's IP address using javascript only? 我已经检查过此方法如何仅使用javascript获取客户端的IP地址? but all of these provide simple ip addresses not with CIDR bits. 但是所有这些都提供不带有CIDR位的简单IP地址。

Thank you. 谢谢。

As others stated in the comments to your question, there is no way to extract subnet mask information just from the IP address alone, and there is no reliable way to get this information from the server. 正如其他人在对您的问题的评论中所指出的那样,无法仅从IP地址中提取子网掩码信息,也没有从服务器获取此信息的可靠方法。 Firstly, you are communicating with a single IP address and not the whole subnet, so the correct CIDR is, as others said, /32. 首先,您使用的是单个IP​​地址而不是整个子网,因此,正如其他人所说,正确的CIDR是/ 32。 Secondly, network mask is network layer information and the way IP routing works doesn't require any other information to be provided other than a destination IP address. 其次,网络掩码是网络层信息,并且IP路由的工作方式不需要提供目标IP地址以外的任何其他信息。

It is important to know what exactly are trying to accomplish and are using this information for. 重要的是要知道到底要完成什么并将这些信息用于什么。 Do you need this just to fulfill some format requirements or you really need the subnet mask. 您是否仅需要满足某些格式要求,还是确实需要子网掩码? Anyway, if we're talking about ordinary web client/server I could see two different scenarios: 无论如何,如果我们谈论的是普通的Web客户端/服务器,我会看到两种不同的情况:

1) You need exact information about client network configuration 1)您需要有关客户端网络配置的确切信息

Even if you would be able to programmatically obtain such data, I'm not sure how useful it would be. 即使您能够以编程方式获取此类数据,我也不知道它会有用吗。 Because of the shortage of IPv4 addresses and the way ISPs allocate addresses to customers most of the clients are behind their home router which does NAT or in some cases behind carrier grade NAT. 由于IPv4地址的短缺以及ISP将地址分配给客户的方式,大多数客户端位于其进行NAT的家庭路由器之后,或者在某些情况下位于运营商级NAT之后。 Having bunch of clients that all have the same 192.168.1.2/24 address would be a bit pointless. 拥有一堆都具有相同192.168.1.2/24地址的客户端将是毫无意义的。

2) You need the public information (used for global routing) to group customers or something related to that. 2)您需要公共信息(用于全局路由)来对客户或与此相关的事物进行分组。

This means getting the public IP address (the one you get with httpServletRequest.getRemoteAddress(); ). 这意味着获取公共IP地址(使用httpServletRequest.getRemoteAddress();获得的IP地址)。 There's no way to obtain the exact subnet with 100% accuracy but you can get the route object from a RIR (Regional Internet Registry) database that delegated the scope that IP belongs to. 无法以100%的准确度获取确切的子网,但是您可以从委托IP所属范围的RIR(区域Internet注册表)数据库中获取路由对象。 LIRs (organizations that got the address block) have the obligation to update this database with various information and one of those is creating these route objects. LIR(具有地址块的组织)有义务使用各种信息来更新该数据库,其中之一就是创建这些路由对象。 For that you can use Whois protocol which is really simple and easily implemented. 为此,您可以使用真正简单且易于实现的Whois协议 Basically you just need to open TCP connection to a port 43, send a command that contain the IP address you need the info on and parse the output. 基本上,您只需要打开与端口43的TCP连接,发送包含所需信息的IP地址的命令并解析输出即可。

For your example (123.21.21.12) the route object would look like: 对于您的示例(123.21.21.12),路由对象如下所示:

~ # whois -T route 123.21.21.12
% [whois.apnic.net]
% Whois data copyright terms    http://www.apnic.net/db/dbcopyright.html

% Information related to '123.21.16.0/20AS45899'

route:          123.21.16.0/20
descr:          VietNam Post and Telecom Corporation (VNPT)
descr:          VNPT-AS-AP
country:        VN
origin:         AS45899
remarks:        mailto: noc@vnn.vn
notify:         hm-changed@vnnic.net.vn
mnt-by:         MAINT-VN-VNPT
changed:        hm-changed@vnnic.net.vn 20100810
source:         APNIC

% This query was served by the APNIC Whois Service version 1.69.1-APNICv1r0 (UNDEFINED)

We can see that that IP address belongs to a AS45899 (VNPT) and that the route object is 123.21.16.0/20, which gives you the CIDR you wanted. 我们可以看到该IP地址属于AS45899(VNPT),并且路由对象为123.21.16.0/20,这为您提供了所需的CIDR。

And if we query that IP address from a router that has a full BGP table: 如果我们从具有完整BGP表的路由器查询该IP地址,请执行以下操作:

# show bgp ipv4 unicast 123.21.21.12          
BGP routing table entry for 123.21.16.0/20, version 71369881
Paths: (3 available, best #1, table default)

we do see that indeed the Whois database is updated and that IP belongs to 123.21.16.0/20 route. 我们确实看到Whois数据库确实已更新,并且IP属于123.21.16.0/20路由。

Keep in mind that this /20 could be aggregated route, but that's the best you can get. 请记住,这个/ 20可能是汇总路由,但这是您可以获得的最佳路由。 That's the routing information that's globally available and used by routers around the world to route traffic. 那是路由信息,它是全球可用的,并且被世界各地的路由器用来路由流量。 How that /20 prefix is used inside the ISP network is up to their internal organization and policies and you can't check that. ISP网络内部如何使用/ 20前缀取决于其内部组织和策略,您无法检查。 You also shouldn't get routes smaller than /24 this way. 您也不应以此方式获得小于/ 24的路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM