Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #534 from abligh/select-ports-on-cli
Browse files Browse the repository at this point in the history
Add options to set ports on weaver command line
  • Loading branch information
rade committed Apr 10, 2015
2 parents f3d2011 + 12d88ea commit bd8cb02
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 40 deletions.
28 changes: 15 additions & 13 deletions router/connection_maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ const (
)

type ConnectionMaker struct {
ourself *LocalPeer
peers *Peers
targets map[string]*Target
cmdLineAddress map[string]struct{}
actionChan chan<- ConnectionMakerAction
ourself *LocalPeer
peers *Peers
normalisePeerAddr func(string) string
targets map[string]*Target
cmdLineAddress map[string]struct{}
actionChan chan<- ConnectionMakerAction
}

// Information about an address where we may find a peer
Expand All @@ -31,12 +32,13 @@ type Target struct {

type ConnectionMakerAction func() bool

func NewConnectionMaker(ourself *LocalPeer, peers *Peers) *ConnectionMaker {
func NewConnectionMaker(ourself *LocalPeer, peers *Peers, normalisePeerAddr func(string) string) *ConnectionMaker {
return &ConnectionMaker{
ourself: ourself,
peers: peers,
cmdLineAddress: make(map[string]struct{}),
targets: make(map[string]*Target)}
ourself: ourself,
peers: peers,
normalisePeerAddr: normalisePeerAddr,
cmdLineAddress: make(map[string]struct{}),
targets: make(map[string]*Target)}
}

func (cm *ConnectionMaker) Start() {
Expand All @@ -47,7 +49,7 @@ func (cm *ConnectionMaker) Start() {

func (cm *ConnectionMaker) InitiateConnection(address string) {
cm.actionChan <- func() bool {
cm.cmdLineAddress[NormalisePeerAddr(address)] = void
cm.cmdLineAddress[cm.normalisePeerAddr(address)] = void
if target, found := cm.targets[address]; found {
target.tryAfter, target.tryInterval = tryImmediately()
}
Expand All @@ -57,7 +59,7 @@ func (cm *ConnectionMaker) InitiateConnection(address string) {

func (cm *ConnectionMaker) ForgetConnection(address string) {
cm.actionChan <- func() bool {
delete(cm.cmdLineAddress, NormalisePeerAddr(address))
delete(cm.cmdLineAddress, cm.normalisePeerAddr(address))
return false
}
}
Expand Down Expand Up @@ -150,7 +152,7 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration {
addTarget(address)
}
if host, _, err := net.SplitHostPort(address); err == nil {
addTarget(NormalisePeerAddr(host))
addTarget(cm.normalisePeerAddr(host))
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion router/gossip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type mockChannelConnection struct {
// Construct a "passive" Router, i.e. without any goroutines, except
// for Routes and GossipSenders.
func NewTestRouter(name PeerName) *Router {
router := NewRouter(nil, name, "", nil, 10, 1024, nil)
router := NewRouter(nil, name, "", nil, 10, 1024, nil, Port)
// need to create a dummy channel otherwise tests hang on nil
// channels when the Router invoked ConnectionMaker.Refresh
router.ConnectionMaker.actionChan = make(chan ConnectionMakerAction, ChannelSize)
Expand Down
2 changes: 1 addition & 1 deletion router/local_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (peer *LocalPeer) CreateConnection(peerAddr string, acceptNewPeer bool) err
return err
}
// We're dialing the remote so that means connections will come from random ports
addrStr := NormalisePeerAddr(peerAddr)
addrStr := peer.Router.NormalisePeerAddr(peerAddr)
tcpAddr, tcpErr := net.ResolveTCPAddr("tcp4", addrStr)
udpAddr, udpErr := net.ResolveUDPAddr("udp4", addrStr)
if tcpErr != nil || udpErr != nil {
Expand Down
24 changes: 18 additions & 6 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Router struct {
ConnLimit int
BufSz int
LogFrame func(string, []byte, *layers.Ethernet)
Port int
}

type PacketSource interface {
Expand All @@ -46,14 +47,15 @@ type PacketSourceSink interface {
PacketSink
}

func NewRouter(iface *net.Interface, name PeerName, nickName string, password []byte, connLimit int, bufSz int, logFrame func(string, []byte, *layers.Ethernet)) *Router {
func NewRouter(iface *net.Interface, name PeerName, nickName string, password []byte, connLimit int, bufSz int, logFrame func(string, []byte, *layers.Ethernet), port int) *Router {
router := &Router{
Iface: iface,
GossipChannels: make(map[uint32]*GossipChannel),
Password: password,
ConnLimit: connLimit,
BufSz: bufSz,
LogFrame: logFrame}
LogFrame: logFrame,
Port: port}
onMacExpiry := func(mac net.HardwareAddr, peer *Peer) {
log.Println("Expired MAC", mac, "at", peer.FullName())
}
Expand All @@ -66,7 +68,7 @@ func NewRouter(iface *net.Interface, name PeerName, nickName string, password []
router.Peers = NewPeers(router.Ourself.Peer, onPeerGC)
router.Peers.FetchWithDefault(router.Ourself.Peer)
router.Routes = NewRoutes(router.Ourself.Peer, router.Peers)
router.ConnectionMaker = NewConnectionMaker(router.Ourself, router.Peers)
router.ConnectionMaker = NewConnectionMaker(router.Ourself, router.Peers, router.NormalisePeerAddr)
router.TopologyGossip = router.NewGossip("topology", router)
return router
}
Expand All @@ -86,8 +88,8 @@ func (router *Router) Start() {
router.Macs.Start()
router.Routes.Start()
router.ConnectionMaker.Start()
router.UDPListener = router.listenUDP(Port, po)
router.listenTCP(Port)
router.UDPListener = router.listenUDP(router.Port, po)
router.listenTCP(router.Port)
if pio != nil {
router.sniff(pio)
}
Expand Down Expand Up @@ -198,7 +200,7 @@ func (router *Router) listenTCP(localPort int) {

func (router *Router) acceptTCP(tcpConn *net.TCPConn) {
// someone else is dialing us, so our udp sender is the conn
// on Port and we wait for them to send us something on UDP to
// on router.Port and we wait for them to send us something on UDP to
// start.
remoteAddrStr := tcpConn.RemoteAddr().String()
log.Printf("->[%s] connection accepted\n", remoteAddrStr)
Expand Down Expand Up @@ -416,3 +418,13 @@ func (router *Router) applyTopologyUpdate(update []byte) (PeerNameSet, PeerNameS
}
return origUpdate, newUpdate, nil
}

// given an address like '1.2.3.4:567', return the address if it has a port,
// otherwise return the address with the default port number for the router
func (router *Router) NormalisePeerAddr(peerAddr string) string {
_, _, err := net.SplitHostPort(peerAddr)
if err == nil {
return peerAddr
}
return fmt.Sprintf("%s:%d", peerAddr, router.Port)
}
2 changes: 1 addition & 1 deletion router/udp_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewRawUDPSender(conn *LocalConnection) (*RawUDPSender, error) {
if err != nil {
return nil, err
}
udpHeader := &layers.UDP{SrcPort: layers.UDPPort(Port)}
udpHeader := &layers.UDP{SrcPort: layers.UDPPort(conn.Router.Port)}
ipBuf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
Expand Down
10 changes: 0 additions & 10 deletions router/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,3 @@ func (lop ListOfPeers) Swap(i, j int) {
func (lop ListOfPeers) Less(i, j int) bool {
return lop[i].Name < lop[j].Name
}

// given an address like '1.2.3.4:567', return the address if it has a port,
// otherwise return the address with weave's standard port number
func NormalisePeerAddr(peerAddr string) string {
_, _, err := net.SplitHostPort(peerAddr)
if err == nil {
return peerAddr
}
return fmt.Sprintf("%s:%d", peerAddr, Port)
}
21 changes: 13 additions & 8 deletions weaver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func main() {
peers []string
connLimit int
bufSz int
port int
httpAddr string
)

flag.BoolVar(&justVersion, "version", false, "print version and exit")
Expand All @@ -58,6 +60,8 @@ func main() {
flag.StringVar(&prof, "profile", "", "enable profiling and write profiles to given path")
flag.IntVar(&connLimit, "connlimit", 30, "connection limit (defaults to 30, set to 0 for unlimited)")
flag.IntVar(&bufSz, "bufsz", 8, "capture buffer size in MB (defaults to 8MB)")
flag.IntVar(&port, "port", weave.Port, fmt.Sprintf("router port (defaults to %d)", weave.Port))
flag.StringVar(&httpAddr, "httpaddr", fmt.Sprintf(":%d", weave.HTTPPort), fmt.Sprintf("address to bind HTTP interface to (defaults to :%d, set to \"\" to disable)", weave.HTTPPort))
flag.Parse()
peers = flag.Args()

Expand Down Expand Up @@ -121,11 +125,13 @@ func main() {
defer profile.Start(&p).Stop()
}

router := weave.NewRouter(iface, ourName, nickName, pwSlice, connLimit, bufSz*1024*1024, logFrameFunc(debug))
router := weave.NewRouter(iface, ourName, nickName, pwSlice, connLimit, bufSz*1024*1024, logFrameFunc(debug), port)
log.Println("Our name is", router.Ourself.FullName())
router.Start()
initiateConnections(router, peers)
go handleHTTP(router)
if httpAddr != "" {
go handleHTTP(router, httpAddr)
}
handleSignals(router)
}

Expand All @@ -145,15 +151,15 @@ func logFrameFunc(debug bool) func(string, []byte, *layers.Ethernet) {

func initiateConnections(router *weave.Router, peers []string) {
for _, peer := range peers {
if addr, err := net.ResolveTCPAddr("tcp4", weave.NormalisePeerAddr(peer)); err == nil {
if addr, err := net.ResolveTCPAddr("tcp4", router.NormalisePeerAddr(peer)); err == nil {
router.ConnectionMaker.InitiateConnection(addr.String())
} else {
log.Fatal(err)
}
}
}

func handleHTTP(router *weave.Router) {
func handleHTTP(router *weave.Router, httpAddr string) {
encryption := "off"
if router.UsingPassword() {
encryption = "on"
Expand All @@ -174,7 +180,7 @@ func handleHTTP(router *weave.Router) {

muxRouter.Methods("POST").Path("/connect").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
peer := r.FormValue("peer")
if addr, err := net.ResolveTCPAddr("tcp4", weave.NormalisePeerAddr(peer)); err == nil {
if addr, err := net.ResolveTCPAddr("tcp4", router.NormalisePeerAddr(peer)); err == nil {
router.ConnectionMaker.InitiateConnection(addr.String())
} else {
http.Error(w, fmt.Sprint("invalid peer address: ", err), http.StatusBadRequest)
Expand All @@ -183,7 +189,7 @@ func handleHTTP(router *weave.Router) {

muxRouter.Methods("POST").Path("/forget").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
peer := r.FormValue("peer")
if addr, err := net.ResolveTCPAddr("tcp4", weave.NormalisePeerAddr(peer)); err == nil {
if addr, err := net.ResolveTCPAddr("tcp4", router.NormalisePeerAddr(peer)); err == nil {
router.ConnectionMaker.ForgetConnection(addr.String())
} else {
http.Error(w, fmt.Sprint("invalid peer address: ", err), http.StatusBadRequest)
Expand All @@ -192,8 +198,7 @@ func handleHTTP(router *weave.Router) {

http.Handle("/", muxRouter)

address := fmt.Sprintf(":%d", weave.HTTPPort)
err := http.ListenAndServe(address, nil)
err := http.ListenAndServe(httpAddr, nil)
if err != nil {
log.Fatal("Unable to create http listener: ", err)
}
Expand Down

0 comments on commit bd8cb02

Please sign in to comment.