2017-09-17 01:26:19 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-04-20 06:56:06 +00:00
|
|
|
"encoding/json"
|
2019-01-17 12:26:16 +00:00
|
|
|
"fmt"
|
2017-09-17 01:26:19 +00:00
|
|
|
"net"
|
2017-12-05 22:17:49 +00:00
|
|
|
"strings"
|
2017-09-17 01:26:19 +00:00
|
|
|
"time"
|
|
|
|
|
2019-01-17 12:26:16 +00:00
|
|
|
"github.com/bdlm/log"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2017-09-17 01:26:19 +00:00
|
|
|
"github.com/FreifunkBremen/yanic/respond"
|
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
)
|
|
|
|
|
2017-12-05 22:17:49 +00:00
|
|
|
var (
|
|
|
|
wait int
|
|
|
|
port int
|
|
|
|
ipAddress string
|
|
|
|
)
|
2017-09-17 01:26:19 +00:00
|
|
|
|
|
|
|
// queryCmd represents the query command
|
|
|
|
var queryCmd = &cobra.Command{
|
2017-12-05 22:17:49 +00:00
|
|
|
Use: "query <interfaces> <destination>",
|
2017-09-17 01:26:19 +00:00
|
|
|
Short: "Sends a query on the interface to the destination and waits for a response",
|
2017-12-05 22:17:49 +00:00
|
|
|
Example: `yanic query "eth0,wlan0" "fe80::eade:27ff:dead:beef"`,
|
2017-09-17 01:26:19 +00:00
|
|
|
Args: cobra.ExactArgs(2),
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2017-12-05 22:17:49 +00:00
|
|
|
ifaces := strings.Split(args[0], ",")
|
2017-10-01 09:13:26 +00:00
|
|
|
dstAddress := net.ParseIP(args[1])
|
2017-09-17 01:26:19 +00:00
|
|
|
|
2019-01-17 12:26:16 +00:00
|
|
|
log.WithFields(map[string]interface{}{
|
|
|
|
"address": dstAddress,
|
|
|
|
"ifaces": ifaces,
|
|
|
|
}).Info("sending request")
|
2017-12-05 22:17:49 +00:00
|
|
|
|
|
|
|
var ifacesConfigs []respond.InterfaceConfig
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
ifaceConfig := respond.InterfaceConfig{
|
|
|
|
InterfaceName: iface,
|
|
|
|
Port: port,
|
|
|
|
IPAddress: ipAddress,
|
|
|
|
}
|
|
|
|
ifacesConfigs = append(ifacesConfigs, ifaceConfig)
|
|
|
|
}
|
2017-09-17 01:26:19 +00:00
|
|
|
|
2018-01-07 20:00:56 +00:00
|
|
|
nodes := runtime.NewNodes(&runtime.NodesConfig{})
|
2017-09-17 01:26:19 +00:00
|
|
|
|
2018-01-17 19:20:35 +00:00
|
|
|
sitesDomains := make(map[string][]string)
|
2017-12-05 22:17:49 +00:00
|
|
|
collector := respond.NewCollector(nil, nodes, sitesDomains, ifacesConfigs)
|
2017-09-17 01:26:19 +00:00
|
|
|
defer collector.Close()
|
2017-10-01 09:13:26 +00:00
|
|
|
collector.SendPacket(dstAddress)
|
2017-09-17 01:26:19 +00:00
|
|
|
|
|
|
|
time.Sleep(time.Second * time.Duration(wait))
|
|
|
|
|
|
|
|
for id, data := range nodes.List {
|
2017-12-05 22:17:49 +00:00
|
|
|
jq, err := json.Marshal(data)
|
2018-04-20 06:56:06 +00:00
|
|
|
if err != nil {
|
2019-01-17 12:26:16 +00:00
|
|
|
fmt.Printf("%s: %+v", id, data)
|
2018-04-20 06:56:06 +00:00
|
|
|
} else {
|
2017-12-05 22:17:49 +00:00
|
|
|
jqNeighbours, err := json.Marshal(data.Neighbours)
|
|
|
|
if err != nil {
|
2019-01-17 12:26:16 +00:00
|
|
|
fmt.Printf("%s: %s neighbours: %+v", id, string(jq), data.Neighbours)
|
2017-12-05 22:17:49 +00:00
|
|
|
} else {
|
2019-01-17 12:26:16 +00:00
|
|
|
fmt.Printf("%s: %s neighbours: %s", id, string(jq), string(jqNeighbours))
|
2017-12-05 22:17:49 +00:00
|
|
|
}
|
2018-04-20 06:56:06 +00:00
|
|
|
}
|
2017-09-17 01:26:19 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(queryCmd)
|
|
|
|
queryCmd.Flags().IntVar(&wait, "wait", 1, "Seconds to wait for a response")
|
2017-12-05 22:17:49 +00:00
|
|
|
queryCmd.Flags().IntVar(&port, "port", 0, "define a port to listen (if not set or set to 0 the kernel will use a random free port at its own)")
|
|
|
|
queryCmd.Flags().StringVar(&ipAddress, "ip", "", "ip address which is used for sending (optional - without definition used the link-local address)")
|
2017-09-17 01:26:19 +00:00
|
|
|
}
|