diff --git a/httpu/httpu.go b/httpu/httpu.go index 862c3de..f52dad6 100644 --- a/httpu/httpu.go +++ b/httpu/httpu.go @@ -3,6 +3,7 @@ package httpu import ( "bufio" "bytes" + "errors" "fmt" "log" "net" @@ -28,6 +29,20 @@ func NewHTTPUClient() (*HTTPUClient, error) { return &HTTPUClient{conn: conn}, nil } +// NewHTTPUClientAddr creates a new HTTPUClient which will broadcast packets +// from the specified address, opening up a new UDP socket for the purpose +func NewHTTPUClientAddr(addr string) (*HTTPUClient, error) { + ip := net.ParseIP(addr) + if ip == nil { + return nil, errors.New("Invalid listening address") + } + conn, err := net.ListenPacket("udp", ip.String()+":0") + if err != nil { + return nil, err + } + return &HTTPUClient{conn: conn}, nil +} + // Close shuts down the client. The client will no longer be useful following // this. func (httpu *HTTPUClient) Close() error {