Skip to content

Commit

Permalink
Udp (#34)
Browse files Browse the repository at this point in the history
* add udp

* version bump
  • Loading branch information
wybiral authored Apr 29, 2019
1 parent 769ba1f commit 159f14e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
5 changes: 4 additions & 1 deletion hookah.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// Version of hookah API.
const Version = "2.1.0"
const Version = "2.2.0"

// API is an instance of the Hookah API.
type API struct {
Expand Down Expand Up @@ -88,6 +88,9 @@ func (a *API) registerProtocols() {
a.RegisterProtocol("tcp-listen", "tcp-listen://address", protocols.TCPListen)
a.RegisterProtocol("tls", "tls://address?cert=path&insecure=false", protocols.TLS)
a.RegisterProtocol("tls-listen", "tls-listen://address?cert=path&key=path", protocols.TLSListen)
a.RegisterProtocol("udp", "udp://address", protocols.UDP)
a.RegisterProtocol("udp-listen", "udp-listen://address", protocols.UDPListen)
a.RegisterProtocol("udp-multicast", "udp-multicast://address", protocols.UDPMulticast)
a.RegisterProtocol("unix", "unix://path/to/sock", protocols.Unix)
a.RegisterProtocol("unix-listen", "unix-listen://path/to/sock", protocols.UnixListen)
a.RegisterProtocol("ws", "ws://address", protocols.WS)
Expand Down
16 changes: 16 additions & 0 deletions internal/protocols/udp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package protocols

import (
"net"

"github.com/wybiral/hookah/pkg/node"
)

// UDP creates a UDP client node
func UDP(addr string) (*node.Node, error) {
rwc, err := net.Dial("udp", addr)
if err != nil {
return nil, err
}
return node.New(rwc), nil
}
61 changes: 61 additions & 0 deletions internal/protocols/udplisten.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package protocols

import (
"net"
"net/url"
"strings"

"github.com/wybiral/hookah/pkg/node"
)

// UDPListen creates a UDP listener Node
func UDPListen(addr string) (*node.Node, error) {
a, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
c, err := net.ListenUDP("udp", a)
if err != nil {
return nil, err
}
n := node.New(c)
n.W = nil
return n, nil
}

// UDPMulticast creates a UDP multicast listener Node
func UDPMulticast(arg string) (*node.Node, error) {
var err error
var opts url.Values
// Parse options
addrOpts := strings.SplitN(arg, "?", 2)
addr := addrOpts[0]
if len(addrOpts) == 2 {
op, err := url.ParseQuery(addrOpts[1])
if err != nil {
return nil, err
}
opts = op
}
iface := opts.Get("iface")
var ifi *net.Interface
ifi = nil
if len(iface) > 0 {
// If interface is supplied, look it up
ifi, err = net.InterfaceByName(iface)
if err != nil {
return nil, err
}
}
a, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
c, err := net.ListenMulticastUDP("udp", ifi, a)
if err != nil {
return nil, err
}
n := node.New(c)
n.W = nil
return n, nil
}

0 comments on commit 159f14e

Please sign in to comment.