server6.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. //go:build integration
  5. // +build integration
  6. package main
  7. import (
  8. "fmt"
  9. "log"
  10. "net"
  11. "runtime"
  12. "github.com/insomniacslk/dhcp/dhcpv6"
  13. "github.com/insomniacslk/dhcp/dhcpv6/client6"
  14. "github.com/insomniacslk/dhcp/iana"
  15. "github.com/vishvananda/netns"
  16. "github.com/coredhcp/coredhcp/config"
  17. "github.com/coredhcp/coredhcp/plugins"
  18. "github.com/coredhcp/coredhcp/server"
  19. // Plugins
  20. "github.com/coredhcp/coredhcp/plugins/file"
  21. "github.com/coredhcp/coredhcp/plugins/serverid"
  22. )
  23. var serverConfig = config.Config{
  24. Server6: &config.ServerConfig{
  25. Addresses: []net.UDPAddr{
  26. {
  27. IP: net.ParseIP("ff02::1:2"),
  28. Port: dhcpv6.DefaultServerPort,
  29. Zone: "cdhcp_srv",
  30. },
  31. },
  32. Plugins: []config.PluginConfig{
  33. {Name: "server_id", Args: []string{"LL", "11:22:33:44:55:66"}},
  34. {Name: "file", Args: []string{"./leases-dhcpv6-test.txt"}},
  35. },
  36. },
  37. }
  38. // This function *must* be run in its own routine
  39. // For now this assumes ns are created outside.
  40. // TODO: dynamically create NS and interfaces directly in the test program
  41. func runServer(readyCh chan<- struct{}, nsName string, desiredPlugins []*plugins.Plugin) {
  42. runtime.LockOSThread()
  43. defer runtime.UnlockOSThread()
  44. ns, err := netns.GetFromName(nsName)
  45. if err != nil {
  46. log.Panicf("Netns `%s` not set up: %v", nsName, err)
  47. }
  48. if err := netns.Set(ns); err != nil {
  49. log.Panicf("Failed to switch to netns `%s`: %v", nsName, err)
  50. }
  51. // register plugins
  52. for _, pl := range desiredPlugins {
  53. if err := plugins.RegisterPlugin(pl); err != nil {
  54. log.Panicf("Failed to register plugin `%s`: %v", pl.Name, err)
  55. }
  56. }
  57. // start DHCP server
  58. srv, err := server.Start(&serverConfig)
  59. if err != nil {
  60. log.Panicf("Server could not start: %v", err)
  61. }
  62. readyCh <- struct{}{}
  63. if err := srv.Wait(); err != nil {
  64. log.Panicf("Server errored during run: %v", err)
  65. }
  66. }
  67. // runInNs will execute the provided cmd in the namespace nsName.
  68. // It returns the error status of the cmd. Errors in NS management will panic
  69. func runClient6(nsName, iface string, modifiers ...dhcpv6.Modifier) error {
  70. runtime.LockOSThread()
  71. defer runtime.UnlockOSThread()
  72. backupNS, err := netns.Get()
  73. if err != nil {
  74. panic("Could not save handle to original NS")
  75. }
  76. ns, err := netns.GetFromName(nsName)
  77. if err != nil {
  78. panic("netns not set up")
  79. }
  80. if err := netns.Set(ns); err != nil {
  81. panic(fmt.Sprintf("Couldn't switch to test NS: %v", err))
  82. }
  83. client := client6.NewClient()
  84. _, cErr := client.Exchange(iface, modifiers...)
  85. if netns.Set(backupNS) != nil {
  86. panic("couldn't switch back to original NS")
  87. }
  88. return cErr
  89. }
  90. // Create a server and run a DORA exchange with it
  91. func main() {
  92. readyCh := make(chan struct{}, 1)
  93. go runServer(readyCh,
  94. "coredhcp-direct-upper",
  95. []*plugins.Plugin{
  96. &serverid.Plugin, &file.Plugin,
  97. },
  98. )
  99. // wait for server to be ready before sending DHCP request
  100. <-readyCh
  101. mac, err := net.ParseMAC("de:ad:be:ef:00:00")
  102. if err != nil {
  103. panic(err)
  104. }
  105. err = runClient6(
  106. "coredhcp-direct-lower", "cdhcp_cli",
  107. dhcpv6.WithClientID(&dhcpv6.DUIDLL{
  108. HWType: iana.HWTypeEthernet,
  109. LinkLayerAddr: mac,
  110. }),
  111. )
  112. if err != nil {
  113. panic(err)
  114. }
  115. }