diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..63f6e02 --- /dev/null +++ b/LICENSE @@ -0,0 +1,14 @@ +BSD-3-Clause License + +Copyright (c) 2018, Tony Blyler +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ce6a34c --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# Discovery +[![Documentation](https://godoc.org/gitlab.com/tblyler/discovery?status.svg)](https://godoc.org/gitlab.com/tblyler/discovery) +`discovery` is a pure [Go](https://golang.org) library to provide simple abstractions to discover other hosts on networks. + +## Documentation +The [`discovery` documentation for source code is available on GoDoc](https://godoc.org/gitlab.com/tblyler/discovery) or you can run: + +```bash +godoc gitlab.com/tblyler/discovery +``` + +## Testing +Most tests do not require an active network. However, for the ones that do, they only utilize the loopback device and bind to an automatically assigned port via the `0` port. + +```bash +go test -cover gitlab.com/tblyler/discovery +``` + +## Examples +Usable examples are [located here](./examples) or run: + +```bash +go get gitlab.com/tblyler/discovery/examples/... +``` diff --git a/examples/local/broadcast/broadcast b/examples/local/broadcast/broadcast new file mode 100755 index 0000000..068bab7 Binary files /dev/null and b/examples/local/broadcast/broadcast differ diff --git a/examples/local/broadcast/main.go b/examples/local/broadcast/main.go new file mode 100644 index 0000000..aaf6282 --- /dev/null +++ b/examples/local/broadcast/main.go @@ -0,0 +1,112 @@ +package main + +import ( + "context" + "fmt" + "net" + "os" + "os/signal" + "sync" + "time" + + "gitlab.com/tblyler/discovery/local" +) + +func main() { + b, err := local.NewBroadcast( + &net.UDPAddr{ + IP: net.IPv4zero, + Port: 50000, + }, + nil, + ) + + if err != nil { + fmt.Fprintln(os.Stderr, "Failed to create new broadcast:", err) + os.Exit(1) + } + + msgChan := make(chan []byte, 5) + errChan := make(chan error, 2) + + ctx, cancel := context.WithCancel(context.Background()) + + wg := sync.WaitGroup{} + + wg.Add(1) + go func() { + defer wg.Done() + errChan <- b.Listen(ctx, msgChan) + }() + + wg.Add(1) + go func() { + defer wg.Done() + + for { + select { + case <-ctx.Done(): + return + + case msg := <-msgChan: + fmt.Println("Got message:", string(msg)) + } + } + }() + + wg.Add(1) + go func() { + defer wg.Done() + ticker := time.NewTicker(time.Second * time.Duration(5)) + defer ticker.Stop() + + hostname, _ := os.Hostname() + msg := []byte(hostname) + + interfaces, _ := net.InterfaceAddrs() + for _, inter := range interfaces { + msg = append(msg, []byte("\n"+inter.String())...) + } + + msg = append(msg, []byte("\n")...) + + for { + select { + case <-ctx.Done(): + return + + case tTime := <-ticker.C: + fmt.Println("Sending message at", tTime) + + err := b.Send(msg) + if err != nil { + errChan <- err + return + } + } + } + }() + + wg.Add(1) + go func() { + defer wg.Done() + defer cancel() + + sigChan := make(chan os.Signal) + signal.Notify(sigChan, os.Interrupt) + + for { + select { + case err := <-errChan: + fmt.Println("Got error:", err) + return + + case <-sigChan: + fmt.Println("Got signal to quit, quitting") + return + } + } + }() + + wg.Wait() +} diff --git a/local/broadcast.go b/local/broadcast.go index b0bd623..1892072 100644 --- a/local/broadcast.go +++ b/local/broadcast.go @@ -1,4 +1,4 @@ -package broadcast +package local import ( "context" @@ -34,7 +34,7 @@ var ( ErrBroadcastSendBadSize = fmt.Errorf("broadcast message size must be > 0B and <= %dB", MaxUDPPacketSize) ) -// Broadcast implements Seeker with local network broadcasting with IPv4 broadcasting +// Broadcast implements Seeker with local network broadcasting via IPv4 broadcasting type Broadcast struct { listenAddr *net.UDPAddr bcastIPs []net.IP @@ -66,7 +66,10 @@ func NewBroadcast(listenAddr *net.UDPAddr, bcastIPs []net.IP) (b *Broadcast, err return } -// Listen for incoming Send requests from other Broadcast implementations +// Listen for incoming Send requests from other Broadcast implementations. +// It is very likely that if this instance is listening on the same broadcast IP as one that +// it is also sending from, that it will receive what it is sent. It is up to the user to +// decide whether or not they care about receiving messages they are also sending. func (b *Broadcast) Listen(ctx context.Context, msgChan chan<- []byte) error { b.lconnLock.Lock() defer b.lconnLock.Unlock() diff --git a/local/broadcast_test.go b/local/broadcast_test.go index 9882a77..b57e600 100644 --- a/local/broadcast_test.go +++ b/local/broadcast_test.go @@ -1,4 +1,4 @@ -package broadcast +package local import ( "context" diff --git a/local/local.go b/local/local.go new file mode 100644 index 0000000..b535d2b --- /dev/null +++ b/local/local.go @@ -0,0 +1,3 @@ +// Package local provides Seeker implementations that are designed to only work on +// LAN interfaces. +package local diff --git a/seeker.go b/seeker.go index 245a01d..32615e1 100644 --- a/seeker.go +++ b/seeker.go @@ -1,3 +1,4 @@ +// Package discovery provides discovery interfaces to find other users of discovery on networks. package discovery import "context"