-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
132 lines (118 loc) · 3.1 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"encoding/json"
"net"
"net/http"
"strconv"
)
type HTTPServer struct {
AF AddressFamily
RIB *RIB
}
func (s *HTTPServer) handleRIB(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
rib := s.RIB.Entries()
type aux struct {
Prefix string `json:"prefix"`
ASPath []uint16 `json:"as_path"`
NextHop string `json:"next_hop"`
}
res := make([]aux, len(rib))
for i, e := range rib {
res[i] = aux{
Prefix: e.Prefix.String(),
ASPath: e.ASPath.Segments,
NextHop: net.IP(e.NextHop).String(),
}
if e.NextHop == nil {
res[i].NextHop = ""
}
}
b, err := json.MarshalIndent(res, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
w.Write(b)
}
func (s *HTTPServer) handleNetworkAdd(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
var body struct {
Prefix string `json:"prefix"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, "bad request body", http.StatusBadRequest)
return
}
if body.Prefix == "" {
http.Error(w, "prefix is not specified", http.StatusBadRequest)
return
}
_, prefix, err := net.ParseCIDR(body.Prefix)
if err != nil {
http.Error(w, "bad prefix value", http.StatusBadRequest)
return
}
e := s.RIB.Find(prefix)
if e != nil {
http.Error(w, "network already exists in RIB", http.StatusBadRequest)
return
}
if err := s.RIB.Update(&RIBEntry{
AF: s.AF,
Prefix: prefix,
Origin: OriginAttributeIGP,
ASPath: ASPath{Sequence: true, Segments: []uint16{}},
NextHop: nil,
Source: nil,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusAccepted)
}
func (s *HTTPServer) handleNetworkDelete(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
prefixStr := r.URL.Query().Get("prefix")
if prefixStr == "" {
http.Error(w, "prefix is not specified", http.StatusBadRequest)
return
}
_, prefix, err := net.ParseCIDR(prefixStr)
if err != nil {
http.Error(w, "bad prefix value", http.StatusBadRequest)
return
}
e := s.RIB.Find(prefix)
if e == nil {
http.Error(w, "not found in RIB", http.StatusNotFound)
return
}
if e.Source != nil {
http.Error(w, "the entry is not managed by us", http.StatusForbidden)
return
}
if err := s.RIB.Remove(e); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusAccepted)
}
func (s *HTTPServer) ListenAndServe(addr string) error {
mux := http.NewServeMux()
mux.HandleFunc("/rib", s.handleRIB)
mux.HandleFunc("/network/add", s.handleNetworkAdd)
mux.HandleFunc("/network/delete", s.handleNetworkDelete)
return http.ListenAndServe(addr, mux)
}