dongyuan1970 2017-03-30 18: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 04: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.

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

报告相同问题?

悬赏问题

  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
  • ¥15 我如果只想表示节点的结构信息,使用GCN方法不进行训练可以吗
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)