control_tester.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // +build e2e_testing
  2. package nebula
  3. import (
  4. "net"
  5. "github.com/google/gopacket"
  6. "github.com/google/gopacket/layers"
  7. )
  8. // WaitForTypeByIndex will pipe all messages from this control device into the pipeTo control device
  9. // returning after a message matching the criteria has been piped
  10. func (c *Control) WaitForType(msgType NebulaMessageType, subType NebulaMessageSubType, pipeTo *Control) {
  11. h := &Header{}
  12. for {
  13. p := c.f.outside.Get(true)
  14. if err := h.Parse(p.Data); err != nil {
  15. panic(err)
  16. }
  17. pipeTo.InjectUDPPacket(p)
  18. if h.Type == msgType && h.Subtype == subType {
  19. return
  20. }
  21. }
  22. }
  23. // WaitForTypeByIndex is similar to WaitForType except it adds an index check
  24. // Useful if you have many nodes communicating and want to wait to find a specific nodes packet
  25. func (c *Control) WaitForTypeByIndex(toIndex uint32, msgType NebulaMessageType, subType NebulaMessageSubType, pipeTo *Control) {
  26. h := &Header{}
  27. for {
  28. p := c.f.outside.Get(true)
  29. if err := h.Parse(p.Data); err != nil {
  30. panic(err)
  31. }
  32. pipeTo.InjectUDPPacket(p)
  33. if h.RemoteIndex == toIndex && h.Type == msgType && h.Subtype == subType {
  34. return
  35. }
  36. }
  37. }
  38. // InjectLightHouseAddr will push toAddr into the local lighthouse cache for the vpnIp
  39. // This is necessary if you did not configure static hosts or are not running a lighthouse
  40. func (c *Control) InjectLightHouseAddr(vpnIp net.IP, toAddr *net.UDPAddr) {
  41. c.f.lightHouse.Lock()
  42. remoteList := c.f.lightHouse.unlockedGetRemoteList(ip2int(vpnIp))
  43. remoteList.Lock()
  44. defer remoteList.Unlock()
  45. c.f.lightHouse.Unlock()
  46. iVpnIp := ip2int(vpnIp)
  47. if v4 := toAddr.IP.To4(); v4 != nil {
  48. remoteList.unlockedPrependV4(iVpnIp, NewIp4AndPort(v4, uint32(toAddr.Port)))
  49. } else {
  50. remoteList.unlockedPrependV6(iVpnIp, NewIp6AndPort(toAddr.IP, uint32(toAddr.Port)))
  51. }
  52. }
  53. // GetFromTun will pull a packet off the tun side of nebula
  54. func (c *Control) GetFromTun(block bool) []byte {
  55. return c.f.inside.(*Tun).Get(block)
  56. }
  57. // GetFromUDP will pull a udp packet off the udp side of nebula
  58. func (c *Control) GetFromUDP(block bool) *UdpPacket {
  59. return c.f.outside.Get(block)
  60. }
  61. func (c *Control) GetUDPTxChan() <-chan *UdpPacket {
  62. return c.f.outside.txPackets
  63. }
  64. func (c *Control) GetTunTxChan() <-chan []byte {
  65. return c.f.inside.(*Tun).txPackets
  66. }
  67. // InjectUDPPacket will inject a packet into the udp side of nebula
  68. func (c *Control) InjectUDPPacket(p *UdpPacket) {
  69. c.f.outside.Send(p)
  70. }
  71. // InjectTunUDPPacket puts a udp packet on the tun interface. Using UDP here because it's a simpler protocol
  72. func (c *Control) InjectTunUDPPacket(toIp net.IP, toPort uint16, fromPort uint16, data []byte) {
  73. ip := layers.IPv4{
  74. Version: 4,
  75. TTL: 64,
  76. Protocol: layers.IPProtocolUDP,
  77. SrcIP: c.f.inside.CidrNet().IP,
  78. DstIP: toIp,
  79. }
  80. udp := layers.UDP{
  81. SrcPort: layers.UDPPort(fromPort),
  82. DstPort: layers.UDPPort(toPort),
  83. }
  84. err := udp.SetNetworkLayerForChecksum(&ip)
  85. if err != nil {
  86. panic(err)
  87. }
  88. buffer := gopacket.NewSerializeBuffer()
  89. opt := gopacket.SerializeOptions{
  90. ComputeChecksums: true,
  91. FixLengths: true,
  92. }
  93. err = gopacket.SerializeLayers(buffer, opt, &ip, &udp, gopacket.Payload(data))
  94. if err != nil {
  95. panic(err)
  96. }
  97. c.f.inside.(*Tun).Send(buffer.Bytes())
  98. }
  99. func (c *Control) GetUDPAddr() string {
  100. return c.f.outside.addr.String()
  101. }
  102. func (c *Control) KillPendingTunnel(vpnIp net.IP) bool {
  103. hostinfo, ok := c.f.handshakeManager.pendingHostMap.Hosts[ip2int(vpnIp)]
  104. if !ok {
  105. return false
  106. }
  107. c.f.handshakeManager.pendingHostMap.DeleteHostInfo(hostinfo)
  108. return true
  109. }