<p>I'm trying to setup some http listeners and such to listen on IPs that are not explicitly bound to an interface but Go does not seem to support it. Specifically it's for IPv6 addresses. So if I was assigned <code>aaaa:bbbb:cccc:dddd::/64</code> I would like to be able to just specify any IP in that range without having to add it via command ( <code>ip -6 addr add</code>) or on the interface itself ( <code>/etc/sysconfig/network-scripts/ifcfg-ETH</code>)</p>
<p>I have added the route using <code>ip -6 route add local aaaa:bbbb:cccc:dddd::/64 dev lo</code> and they are accessible via <code>ping6</code>. When using go to setup an http server on an address in that range it throws back a <code>could not bind</code> error.</p>
<p>I have found some hacky (and probably error prone) ways to potentially do this but maybe I'm missing something? Is there perhaps some setting or arguments I can pass in somewhere to get this done rather than deal with syscalls and modifying the socket directly or worse and modify the core Go networking code?</p>
<hr/>**评论:**<br/><br/>nemith: <pre><p>This really isn't a Go issue. It more of a kernel issue which is why, like you said, twiddling with syscalls and modifying the socket is required. You need to tell the kernel what to send into the socket.</p>
<p>A alternative may just be using a redirection rule in IPtable/nftables to redirect all traffic to a certain port on any IP address to your regular Go socket.</p></pre>shamunrr: <pre><blockquote>
<p>This really isn't a Go issue. It more of a kernel issue which is why </p>
</blockquote>
<p>Go is what sets up the sockets and listeners so it has to be setup through Go and before the server starts.</p>
<blockquote>
<p>A alternative may just be using a redirection rule in IPtable/nftables to redirect all traffic to a certain port on any IP address to your regular Go socket. </p>
</blockquote>
<p>That is actually a possible solution I hadn't thought of, granted probably not as performant as a direct Go implementation. Thanks!</p></pre>tv64738: <pre><p>Leave host empty and it'll listen on all local IPs. The rest is not a Go question.</p></pre>shamunrr: <pre><p>This is a Go question, and your answer does not fit my issue. If I leave it as <code>::</code> it will listen on all assigned addresses. That is not what I want. It needs to listen on an unassigned address.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传