|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/base64" |
| 7 | + "encoding/json" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "log" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/urfave/cli/v2" |
| 16 | + "github.com/usnistgov/ndn-dpdk/core/jsonhelper" |
| 17 | + "github.com/usnistgov/ndn-dpdk/ndn" |
| 18 | + "github.com/usnistgov/ndn-dpdk/ndn/endpoint" |
| 19 | + "github.com/usnistgov/ndn-dpdk/ndn/keychain" |
| 20 | + "github.com/usnistgov/ndn-dpdk/ndn/mgmt/nfdmgmt" |
| 21 | + "github.com/usnistgov/ndn-dpdk/ndn/tlv" |
| 22 | +) |
| 23 | + |
| 24 | +type nfdReg struct { |
| 25 | + Client *nfdmgmt.Client |
| 26 | + ServedCerts []ndn.Data |
| 27 | + DefaultOrigin int |
| 28 | + DefaultCost int |
| 29 | + Commands []nfdmgmt.ControlCommand |
| 30 | + CommandsDesc []string |
| 31 | + Interval time.Duration |
| 32 | + Repeat time.Duration |
| 33 | +} |
| 34 | + |
| 35 | +func (cmd *nfdReg) readBase64File(filename string) (wire []byte, e error) { |
| 36 | + b64, e := os.ReadFile(filename) |
| 37 | + if e != nil { |
| 38 | + return nil, e |
| 39 | + } |
| 40 | + |
| 41 | + wire = make([]byte, base64.StdEncoding.DecodedLen(len(b64))) |
| 42 | + n, e := base64.StdEncoding.Decode(wire, b64) |
| 43 | + if e != nil { |
| 44 | + return nil, e |
| 45 | + } |
| 46 | + return wire[:n], nil |
| 47 | +} |
| 48 | + |
| 49 | +func (cmd *nfdReg) SetSigner(safeBagFile, safeBagPassphrase string) error { |
| 50 | + if safeBagFile == "" { |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + wire, e := cmd.readBase64File(safeBagFile) |
| 55 | + if e != nil { |
| 56 | + return e |
| 57 | + } |
| 58 | + |
| 59 | + pvt, cert, e := keychain.ImportSafeBag(wire, []byte(safeBagPassphrase)) |
| 60 | + if e != nil { |
| 61 | + return e |
| 62 | + } |
| 63 | + |
| 64 | + cmd.Client.Signer = pvt.WithKeyLocator(cert.Name()) |
| 65 | + cmd.ServedCerts = append(cmd.ServedCerts, cert.Data()) |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +func (cmd *nfdReg) AddCert(certFile string) error { |
| 70 | + wire, e := cmd.readBase64File(certFile) |
| 71 | + if e != nil { |
| 72 | + return e |
| 73 | + } |
| 74 | + |
| 75 | + var pkt ndn.Packet |
| 76 | + if e := tlv.Decode(wire, &pkt); e != nil { |
| 77 | + return e |
| 78 | + } |
| 79 | + |
| 80 | + if pkt.Data == nil { |
| 81 | + return errors.New("not a Data") |
| 82 | + } |
| 83 | + |
| 84 | + cmd.ServedCerts = append(cmd.ServedCerts, *pkt.Data) |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func (cmd *nfdReg) AddRegisterCommand(p string) (e error) { |
| 89 | + var c nfdmgmt.RibRegisterCommand |
| 90 | + if strings.HasPrefix(p, "/") { |
| 91 | + e = jsonhelper.Roundtrip(map[string]any{ |
| 92 | + "name": p, |
| 93 | + "origin": cmd.DefaultOrigin, |
| 94 | + "cost": cmd.DefaultCost, |
| 95 | + "noInherit": true, |
| 96 | + "capture": true, |
| 97 | + }, &c) |
| 98 | + } else { |
| 99 | + e = json.Unmarshal([]byte(p), &c) |
| 100 | + } |
| 101 | + if e != nil { |
| 102 | + return e |
| 103 | + } |
| 104 | + |
| 105 | + cmd.addCommand(c) |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func (cmd *nfdReg) AddUnregisterCommand(p string) (e error) { |
| 110 | + var c nfdmgmt.RibUnregisterCommand |
| 111 | + if strings.HasPrefix(p, "/") { |
| 112 | + e = jsonhelper.Roundtrip(map[string]any{ |
| 113 | + "name": p, |
| 114 | + "origin": cmd.DefaultOrigin, |
| 115 | + "cost": cmd.DefaultCost, |
| 116 | + }, &c) |
| 117 | + } else { |
| 118 | + e = json.Unmarshal([]byte(p), &c) |
| 119 | + } |
| 120 | + if e != nil { |
| 121 | + return e |
| 122 | + } |
| 123 | + |
| 124 | + cmd.addCommand(c) |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func (cmd *nfdReg) addCommand(c nfdmgmt.ControlCommand) { |
| 129 | + cmd.Commands = append(cmd.Commands, c) |
| 130 | + |
| 131 | + var b bytes.Buffer |
| 132 | + fmt.Fprintf(&b, "%T ", c) |
| 133 | + json.NewEncoder(&b).Encode(c) |
| 134 | + cmd.CommandsDesc = append(cmd.CommandsDesc, string(bytes.TrimRight(b.Bytes(), "\n"))) |
| 135 | +} |
| 136 | + |
| 137 | +func (cmd *nfdReg) StartServeCerts(ctx context.Context) { |
| 138 | + for _, cert := range cmd.ServedCerts { |
| 139 | + cert := cert |
| 140 | + endpoint.Produce(ctx, endpoint.ProducerOptions{ |
| 141 | + Prefix: keychain.ToKeyName(cert.Name), |
| 142 | + Handler: func(ctx context.Context, interest ndn.Interest) (ndn.Data, error) { return cert, nil }, |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func (cmd *nfdReg) SendCommands(ctx context.Context) { |
| 148 | + for i, command := range cmd.Commands { |
| 149 | + cr, e := cmd.Client.Invoke(ctx, command) |
| 150 | + if e != nil { |
| 151 | + log.Printf("%v %s", e, cmd.CommandsDesc[i]) |
| 152 | + } else { |
| 153 | + log.Printf("%d %s", cr.StatusCode, cmd.CommandsDesc[i]) |
| 154 | + } |
| 155 | + time.Sleep(cmd.Interval) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func init() { |
| 160 | + var commandPrefixURI, safeBagFile, safeBagPassphrase string |
| 161 | + var serveCertSlice, registerSlice, unregisterSlice cli.StringSlice |
| 162 | + var cmd nfdReg |
| 163 | + |
| 164 | + defineCommand(&cli.Command{ |
| 165 | + Name: "nfdreg", |
| 166 | + Usage: "Register a prefix on NFD uplink.", |
| 167 | + Flags: []cli.Flag{ |
| 168 | + &cli.StringFlag{ |
| 169 | + Name: "command", |
| 170 | + Usage: "NFD command prefix.", |
| 171 | + Value: nfdmgmt.PrefixLocalhop.String(), |
| 172 | + Destination: &commandPrefixURI, |
| 173 | + }, |
| 174 | + &cli.StringFlag{ |
| 175 | + Name: "signer", |
| 176 | + Usage: "Signer key SafeBag file.", |
| 177 | + Destination: &safeBagFile, |
| 178 | + }, |
| 179 | + &cli.StringFlag{ |
| 180 | + Name: "signer-pass", |
| 181 | + Usage: "Signer key SafeBag passphrase.", |
| 182 | + Destination: &safeBagPassphrase, |
| 183 | + }, |
| 184 | + &cli.StringSliceFlag{ |
| 185 | + Name: "serve-cert", |
| 186 | + Usage: "Serve certificate(s).", |
| 187 | + Destination: &serveCertSlice, |
| 188 | + }, |
| 189 | + &cli.IntFlag{ |
| 190 | + Name: "origin", |
| 191 | + Usage: "Route origin, for prefix(es) not specified as JSON.", |
| 192 | + Value: nfdmgmt.RouteOriginClient, |
| 193 | + Destination: &cmd.DefaultOrigin, |
| 194 | + }, |
| 195 | + &cli.IntFlag{ |
| 196 | + Name: "cost", |
| 197 | + Usage: "Route cost, for prefix(es) not specified as JSON.", |
| 198 | + Value: 0, |
| 199 | + Destination: &cmd.DefaultCost, |
| 200 | + }, |
| 201 | + &cli.StringSliceFlag{ |
| 202 | + Name: "register", |
| 203 | + Usage: "Register prefix(es).", |
| 204 | + Destination: ®isterSlice, |
| 205 | + }, |
| 206 | + &cli.StringSliceFlag{ |
| 207 | + Name: "unregister", |
| 208 | + Usage: "Unregister prefix(es).", |
| 209 | + Destination: &unregisterSlice, |
| 210 | + }, |
| 211 | + &cli.DurationFlag{ |
| 212 | + Name: "interval", |
| 213 | + Usage: "Interval between commands.", |
| 214 | + Value: 1 * time.Second, |
| 215 | + Destination: &cmd.Interval, |
| 216 | + }, |
| 217 | + &cli.DurationFlag{ |
| 218 | + Name: "repeat", |
| 219 | + Usage: "Repeat process after this duration. If zero, run once and exit.", |
| 220 | + Destination: &cmd.Repeat, |
| 221 | + }, |
| 222 | + }, |
| 223 | + Before: func(c *cli.Context) error { |
| 224 | + client, e := nfdmgmt.New() |
| 225 | + if e != nil { |
| 226 | + return e |
| 227 | + } |
| 228 | + cmd.Client = client |
| 229 | + cmd.Client.Prefix = ndn.ParseName(commandPrefixURI) |
| 230 | + |
| 231 | + if e := cmd.SetSigner(safeBagFile, safeBagPassphrase); e != nil { |
| 232 | + return fmt.Errorf("import signer SafeBag: %w", e) |
| 233 | + } |
| 234 | + for i, certFile := range serveCertSlice.Value() { |
| 235 | + if e := cmd.AddCert(certFile); e != nil { |
| 236 | + return fmt.Errorf("add cert %d: %w", i, e) |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + for i, p := range unregisterSlice.Value() { |
| 241 | + if e := cmd.AddUnregisterCommand(p); e != nil { |
| 242 | + return fmt.Errorf("unregister command %d: %w", i, e) |
| 243 | + } |
| 244 | + } |
| 245 | + for i, p := range registerSlice.Value() { |
| 246 | + if e := cmd.AddRegisterCommand(p); e != nil { |
| 247 | + return fmt.Errorf("register command %d: %w", i, e) |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + return openUplink(c) |
| 252 | + }, |
| 253 | + Action: func(c *cli.Context) error { |
| 254 | + cmd.StartServeCerts(c.Context) |
| 255 | + for { |
| 256 | + cmd.SendCommands(c.Context) |
| 257 | + if cmd.Repeat <= 0 { |
| 258 | + return nil |
| 259 | + } |
| 260 | + select { |
| 261 | + case <-c.Context.Done(): |
| 262 | + return c.Context.Err() |
| 263 | + case <-time.After(cmd.Repeat): |
| 264 | + } |
| 265 | + } |
| 266 | + }, |
| 267 | + }) |
| 268 | +} |
0 commit comments