Skip to content

Commit 159f14e

Browse files
authored
Udp (#34)
* add udp * version bump
1 parent 769ba1f commit 159f14e

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

hookah.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
// Version of hookah API.
15-
const Version = "2.1.0"
15+
const Version = "2.2.0"
1616

1717
// API is an instance of the Hookah API.
1818
type API struct {
@@ -88,6 +88,9 @@ func (a *API) registerProtocols() {
8888
a.RegisterProtocol("tcp-listen", "tcp-listen://address", protocols.TCPListen)
8989
a.RegisterProtocol("tls", "tls://address?cert=path&insecure=false", protocols.TLS)
9090
a.RegisterProtocol("tls-listen", "tls-listen://address?cert=path&key=path", protocols.TLSListen)
91+
a.RegisterProtocol("udp", "udp://address", protocols.UDP)
92+
a.RegisterProtocol("udp-listen", "udp-listen://address", protocols.UDPListen)
93+
a.RegisterProtocol("udp-multicast", "udp-multicast://address", protocols.UDPMulticast)
9194
a.RegisterProtocol("unix", "unix://path/to/sock", protocols.Unix)
9295
a.RegisterProtocol("unix-listen", "unix-listen://path/to/sock", protocols.UnixListen)
9396
a.RegisterProtocol("ws", "ws://address", protocols.WS)

internal/protocols/udp.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package protocols
2+
3+
import (
4+
"net"
5+
6+
"github.com/wybiral/hookah/pkg/node"
7+
)
8+
9+
// UDP creates a UDP client node
10+
func UDP(addr string) (*node.Node, error) {
11+
rwc, err := net.Dial("udp", addr)
12+
if err != nil {
13+
return nil, err
14+
}
15+
return node.New(rwc), nil
16+
}

internal/protocols/udplisten.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package protocols
2+
3+
import (
4+
"net"
5+
"net/url"
6+
"strings"
7+
8+
"github.com/wybiral/hookah/pkg/node"
9+
)
10+
11+
// UDPListen creates a UDP listener Node
12+
func UDPListen(addr string) (*node.Node, error) {
13+
a, err := net.ResolveUDPAddr("udp", addr)
14+
if err != nil {
15+
return nil, err
16+
}
17+
c, err := net.ListenUDP("udp", a)
18+
if err != nil {
19+
return nil, err
20+
}
21+
n := node.New(c)
22+
n.W = nil
23+
return n, nil
24+
}
25+
26+
// UDPMulticast creates a UDP multicast listener Node
27+
func UDPMulticast(arg string) (*node.Node, error) {
28+
var err error
29+
var opts url.Values
30+
// Parse options
31+
addrOpts := strings.SplitN(arg, "?", 2)
32+
addr := addrOpts[0]
33+
if len(addrOpts) == 2 {
34+
op, err := url.ParseQuery(addrOpts[1])
35+
if err != nil {
36+
return nil, err
37+
}
38+
opts = op
39+
}
40+
iface := opts.Get("iface")
41+
var ifi *net.Interface
42+
ifi = nil
43+
if len(iface) > 0 {
44+
// If interface is supplied, look it up
45+
ifi, err = net.InterfaceByName(iface)
46+
if err != nil {
47+
return nil, err
48+
}
49+
}
50+
a, err := net.ResolveUDPAddr("udp", addr)
51+
if err != nil {
52+
return nil, err
53+
}
54+
c, err := net.ListenMulticastUDP("udp", ifi, a)
55+
if err != nil {
56+
return nil, err
57+
}
58+
n := node.New(c)
59+
n.W = nil
60+
return n, nil
61+
}

0 commit comments

Comments
 (0)