zoukankan      html  css  js  c++  java
  • 以太坊系列之五: p2p的nat模块--以太坊源码学习

    p2p的nat模块

    该模块相对比较简单,因为nat的真正实现并不在此模块,主要是使用了第三方的nat-upnp和nat-pmp来实现真正的穿透(端口映射).

    对外公布的接口

    // An implementation of nat.Interface can map local ports to ports
    // accessible from the Internet.
    type Interface interface {
    	// These methods manage a mapping between a port on the local
    	// machine to a port that can be connected to from the internet.
    	//
    	// protocol is "UDP" or "TCP". Some implementations allow setting
    	// a display name for the mapping. The mapping may be removed by
    	// the gateway when its lifetime ends.
    	AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) error
    	DeleteMapping(protocol string, extport, intport int) error
    
    	// This method should return the external (Internet-facing)
    	// address of the gateway device.
    	ExternalIP() (net.IP, error)
    
    	// Should return name of the method. This is used for logging.
    	String() string
    }
    
    主要有三个关键函数一个是添加映射,一个是删除映射,另一个是获取外部IP.使用起来非常直观,我们这里看一个例子,nat.go中的Map函数中,
    

    m.AddMapping(protocol, extport, intport, name, mapTimeout);

    我看ethereum使用的时候,extport和intport都一样.
    
    Interface总共有四个实现分别是upnp,pmp,extIP和startautodisc,从名字可以看出这四个实现都没有暴露给调用者,也就是内部使用,其中extIP是在本机IP就是公网IP的情况下使用,也就是无需端口映射的时候使用.upnp和pmp是对两种端口映射协议的使用封装,而startautodisc则是对这两者的再次封装统一.
    
    那么nat模块怎么使用呢,用起来其实很简单.下面是示例代码,不完整.
    
       //获取nat实例,
       var m nat.Interface=nat.Any()
       //然后就可以使用AddMapping等操作了,
    
       //nat还提供了更方便的函数Map来保持端口映射
       //用法如下 来自server.go
       nat.Map(srv.NAT, srv.quit, "tcp", laddr.Port, laddr.Port, "ethereum p2p")
    
  • 相关阅读:
    动画组(花瓣)以曲线落下
    关键帧动画(2)花瓣
    关键帧动画(2)心脏的跳动
    基础动画(落叶)
    核心动画3
    手表(锚点)
    利用高德地图完成用户地图选址,包括搜索位置和标签固定居中
    控件利用Masonry添加约束之后 不能立刻获取到该控件的尺寸
    添加textView的时候注意在导航控制器下的文字内容位置偏移
    当xib没有与对应的控制器连线的时候导致的崩溃
  • 原文地址:https://www.cnblogs.com/baizx/p/6957772.html
Copyright © 2011-2022 走看看