control_tester.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.AddRemote(ip2int(vpnIp), &udpAddr{IP: toAddr.IP, Port: uint16(toAddr.Port)}, false)
  42. }
  43. // GetFromTun will pull a packet off the tun side of nebula
  44. func (c *Control) GetFromTun(block bool) []byte {
  45. return c.f.inside.(*Tun).Get(block)
  46. }
  47. // GetFromUDP will pull a udp packet off the udp side of nebula
  48. func (c *Control) GetFromUDP(block bool) *UdpPacket {
  49. return c.f.outside.Get(block)
  50. }
  51. func (c *Control) GetUDPTxChan() <-chan *UdpPacket {
  52. return c.f.outside.txPackets
  53. }
  54. func (c *Control) GetTunTxChan() <-chan []byte {
  55. return c.f.inside.(*Tun).txPackets
  56. }
  57. // InjectUDPPacket will inject a packet into the udp side of nebula
  58. func (c *Control) InjectUDPPacket(p *UdpPacket) {
  59. c.f.outside.Send(p)
  60. }
  61. // InjectTunUDPPacket puts a udp packet on the tun interface. Using UDP here because it's a simpler protocol
  62. func (c *Control) InjectTunUDPPacket(toIp net.IP, toPort uint16, fromPort uint16, data []byte) {
  63. ip := layers.IPv4{
  64. Version: 4,
  65. TTL: 64,
  66. Protocol: layers.IPProtocolUDP,
  67. SrcIP: c.f.inside.CidrNet().IP,
  68. DstIP: toIp,
  69. }
  70. udp := layers.UDP{
  71. SrcPort: layers.UDPPort(fromPort),
  72. DstPort: layers.UDPPort(toPort),
  73. }
  74. udp.SetNetworkLayerForChecksum(&ip)
  75. buffer := gopacket.NewSerializeBuffer()
  76. opt := gopacket.SerializeOptions{
  77. ComputeChecksums: true,
  78. FixLengths: true,
  79. }
  80. err := gopacket.SerializeLayers(buffer, opt, &ip, &udp, gopacket.Payload(data))
  81. if err != nil {
  82. panic(err)
  83. }
  84. c.f.inside.(*Tun).Send(buffer.Bytes())
  85. }
  86. func (c *Control) GetUDPAddr() string {
  87. return c.f.outside.addr.String()
  88. }