control_tester.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // InjectUDPPacket will inject a packet into the udp side of nebula
  52. func (c *Control) InjectUDPPacket(p *UdpPacket) {
  53. c.f.outside.Send(p)
  54. }
  55. // InjectTunUDPPacket puts a udp packet on the tun interface. Using UDP here because it's a simpler protocol
  56. func (c *Control) InjectTunUDPPacket(toIp net.IP, toPort uint16, fromPort uint16, data []byte) {
  57. ip := layers.IPv4{
  58. Version: 4,
  59. TTL: 64,
  60. Protocol: layers.IPProtocolUDP,
  61. SrcIP: c.f.inside.CidrNet().IP,
  62. DstIP: toIp,
  63. }
  64. udp := layers.UDP{
  65. SrcPort: layers.UDPPort(fromPort),
  66. DstPort: layers.UDPPort(toPort),
  67. }
  68. udp.SetNetworkLayerForChecksum(&ip)
  69. buffer := gopacket.NewSerializeBuffer()
  70. opt := gopacket.SerializeOptions{
  71. ComputeChecksums: true,
  72. FixLengths: true,
  73. }
  74. err := gopacket.SerializeLayers(buffer, opt, &ip, &udp, gopacket.Payload(data))
  75. if err != nil {
  76. panic(err)
  77. }
  78. c.f.inside.(*Tun).Send(buffer.Bytes())
  79. }