dongyuan1970 2017-03-31 02:33
浏览 361
已采纳

侦听所有IP数据包,包括ICMP,TCP和UDP?

Specially in golang, there are interfaces like net.ListenIP and net.ListenTCP.

I'm wondering that, if process p1 ListenIP(192.168.1.1) and process p2 ListenTCP(192.168.1.1:80), will all packets dest to 192.168.1.1 (no matter TCP or UDP) are intercepted by p1, and p2 will never accept a TCP connection?

  • 写回答

1条回答 默认 最新

  • dongmaxi6763 2017-04-02 12:54
    关注

    Here is the source code of net.ListenIP():

    // ListenIP listens for incoming IP packets addressed to the local
    // address laddr.  The returned connection's ReadFrom and WriteTo
    // methods can be used to receive and send IP packets with per-packet
    // addressing.
    func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error) {
        net, proto, err := parseNetwork(netProto)
        if err != nil {
            return nil, &OpError{Op: "listen", Net: netProto, Source: nil, Addr: laddr.opAddr(), Err: err}
        }   
        switch net {
        case "ip", "ip4", "ip6":
        default:
            return nil, &OpError{Op: "listen", Net: netProto, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(netProto)}
        }   
        fd, err := internetSocket(net, laddr, nil, noDeadline, syscall.SOCK_RAW, proto, "listen", noCancel)
        if err != nil {
            return nil, &OpError{Op: "listen", Net: netProto, Source: nil, Addr: laddr.opAddr(), Err: err}
        }   
        return newIPConn(fd), nil 
    }
    

    it creates a raw socket, on linux, an IPPROTO_RAW socket is send only. TCP and UDP packets are never delivered to raw sockets, they are always handled by the kernel protocol stack. Copies of ICMP packets are delivered to a matching raw socket. All other packets destined for protocols that are not processed by a kernel subsystem are delivered to raw sockets.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog