From 4b5f8537becf96d57fd7fc8e38aa6f58076163cc Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Mon, 1 Jun 2020 11:20:58 +0300 Subject: [PATCH] + /dhcp/reset: clear leases --- AGHTechDoc.md | 11 +++++++++- dhcpd/dhcp_http.go | 48 +++++++++++++++++++++++++++++++++----------- openapi/openapi.yaml | 10 +++++++++ 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/AGHTechDoc.md b/AGHTechDoc.md index 002b6232..afe6002c 100644 --- a/AGHTechDoc.md +++ b/AGHTechDoc.md @@ -646,16 +646,25 @@ Response: ### API: Reset DHCP configuration Clear all DHCP leases and configuration settings. -DHCP server will be stopped if it's currently running. Request: POST /control/dhcp/reset + { + "what": "all" | "leases" + } + Response: 200 OK +`what`: +* all: + Clear all DHCP leases and configuration settings. + DHCP server will be stopped if it's currently running. +* leases: + Clear all DHCP leases ## TLS diff --git a/dhcpd/dhcp_http.go b/dhcpd/dhcp_http.go index a6cfca26..4a98a3a1 100644 --- a/dhcpd/dhcp_http.go +++ b/dhcpd/dhcp_http.go @@ -415,26 +415,50 @@ func (s *Server) handleDHCPRemoveStaticLease(w http.ResponseWriter, r *http.Requ } } +type resetJSON struct { + What string `json:"what"` +} + func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) { - err := s.Stop() + req := resetJSON{} + err := json.NewDecoder(r.Body).Decode(&req) if err != nil { - log.Error("DHCP: Stop: %s", err) + httpError(r, w, http.StatusBadRequest, "json.Decode: %s", err) + return } + switch req.What { + case "all": + err = s.Stop() + if err != nil { + log.Error("DHCP: Stop: %s", err) + } + + oldconf := s.conf + s.conf = ServerConfig{} + s.conf.WorkDir = oldconf.WorkDir + s.conf.HTTPRegister = oldconf.HTTPRegister + s.conf.ConfigModified = oldconf.ConfigModified + s.conf.DBFilePath = oldconf.DBFilePath + + s.conf.ConfigModified() + + case "leases": + // + + default: + httpError(r, w, http.StatusBadRequest, "unsupported 'what' value") + return + } + + s.srv4.ResetLeases(nil) + s.srv6.ResetLeases(nil) err = os.Remove(s.conf.DBFilePath) if err != nil && !os.IsNotExist(err) { log.Error("DHCP: os.Remove: %s: %s", s.conf.DBFilePath, err) + } else if err == nil { + log.Debug("DHCP: Deleted leases file") } - - oldconf := s.conf - s.conf = ServerConfig{} - - s.conf.WorkDir = oldconf.WorkDir - s.conf.HTTPRegister = oldconf.HTTPRegister - s.conf.ConfigModified = oldconf.ConfigModified - s.conf.DBFilePath = oldconf.DBFilePath - - s.conf.ConfigModified() } func (s *Server) registerHandlers() { diff --git a/openapi/openapi.yaml b/openapi/openapi.yaml index a47394d3..473d48d8 100644 --- a/openapi/openapi.yaml +++ b/openapi/openapi.yaml @@ -397,6 +397,11 @@ paths: - dhcp operationId: dhcpReset summary: Reset DHCP configuration + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/DhcpResetRequest" responses: "200": description: OK @@ -1366,6 +1371,11 @@ components: type: string description: Set if static=no example: "" + DhcpResetRequest: + type: object + properties: + what: + type: string DnsAnswer: type: object description: DNS answer section