本文介绍了本地主机上的双堆栈ipv6/ipv4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ipv4服务器,该服务器仅接受通过localhost(使用INADDR_LOOPBACK)的连接.我想将此服务器转换为双栈ipv6/ipv4.但是,使用in6addr_loopback仅接受与::1的连接.

I have an ipv4 server that only accepts connections over localhost (using INADDR_LOOPBACK). I'd like to convert this server to be dual-stack ipv6/ipv4. However, using in6addr_loopback only accepts connections to ::1.

我发现我可以使用in6addr_any同时接受ipv4和ipv6连接,但是因为这也允许从任何对我的特定情况没有用的地方进行连接.

I've found that I can accept ipv4 and ipv6 connections simultaneously using in6addr_any, but as this also allows connections from anywhere it's not useful for my particular case.

是否可以同时绑定到ipv6本地主机(::1)和ipv4本地主机(127.0.0.1)?

Is it possible to bind to ipv6 localhost (::1) and ipv4 localhost (127.0.0.1) simultaneously?

推荐答案

AFAIK,这是不可能的.

AFAIK, that is not possible.

您需要做的是创建两个套接字,一个套接字绑定到INADDR_LOOPBACK,另一个绑定到in6addr_loopback.然后,您可以使用所选的多路复用器(轮询,选择,epoll等)同时等待它们.

What you will need to do is create two sockets, one bound to INADDR_LOOPBACK and one bound to in6addr_loopback. You can then wait on both of them simultaneously using your multiplexer of choice (poll, select, epoll, etc0).

更新

只是想出了一些您可能要考虑的变通办法.在这两种解决方法中,您都绑定到in6addr_any.

Just came up with some work-arounds that you may want to consider. In both of these work-arounds, you bind to in6addr_any.

  1. 建立连接后,检查远程地址,如果它不是127.0.0.1或:: 1,则将其关闭.尽管在IP地址上进行连接的行为并不理想(建立连接/立即关闭而不是拒绝连接),但可以完全在您的应用程序中完成操作是一件很高兴的事情.
  2. 调整操作系统IP堆栈的设置,以拒绝来自非环回IP的端口连接.在具有 iptables 的Linux上,这绝对是可行的.虽然行为较为理想,但需要在应用程序外部进行配置.
  1. When a connection is established, check the remote address and if it's not 127.0.0.1 or ::1, close it. While the behavior of connecting on an IP address is not ideal (connections are established/immediately closed instead of being refused), the nice thing it that this can be done purely in your application.
  2. Adjust the settings of your OS's IP stack to refuse connections to your port from non loopback IP's. This is definitely doable on Linux with iptables. While the behavior is more ideal, it requires configuration external to your application.

这篇关于本地主机上的双堆栈ipv6/ipv4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:55
查看更多