Skip to content

Commit 8d8977d

Browse files
authored
Udp (#10)
* add udp protocols * add udp multicast * udp examples
1 parent 542d6fe commit 8d8977d

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

examples_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ func ExampleNewInput_tcpListen() {
4747
r, err = hookah.NewInput("tcp-listen://localhost:8080")
4848
}
4949

50+
// Create UDP listen input.
51+
func ExampleNewInput_udpListen() {
52+
r, err = hookah.NewInput("udp-listen://localhost:8080")
53+
}
54+
55+
// Create UDP multicast listen input.
56+
// Here the network interface is set to eth0 but can be ommitted to use the
57+
// default.
58+
func ExampleNewInput_udpMulticast() {
59+
r, err = hookah.NewInput("udp-multicast://eth0,localhost:8080")
60+
}
61+
5062
// Create Unix client input.
5163
func ExampleNewInput_unixClient() {
5264
r, err = hookah.NewInput("unix://path/to/sock")
@@ -109,6 +121,11 @@ func ExampleNewOutput_tcpListen() {
109121
w, err = hookah.NewOutput("tcp-listen://localhost:8080")
110122
}
111123

124+
// Create UDP client output.
125+
func ExampleNewOutput_udpClient() {
126+
w, err = hookah.NewOutput("udp://localhost:8080")
127+
}
128+
112129
// Create Unix client output.
113130
func ExampleNewOutput_unixClient() {
114131
w, err = hookah.NewOutput("unix://path/to/sock")

pkg/input/input.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ func New(opts string) (io.ReadCloser, error) {
6363
return nil, errors.New("tcp-listen: no address supplied")
6464
}
6565
return tcpListen(arg)
66+
case "udp-listen", "udp-server":
67+
if arg == "" {
68+
return nil, errors.New("udp-listen: no address supplied")
69+
}
70+
return udpListen(arg)
71+
case "udp-multicast":
72+
if arg == "" {
73+
return nil, errors.New("udp-multicast: no address supplied")
74+
}
75+
return udpMulticast(arg)
6676
case "unix":
6777
if arg == "" {
6878
return nil, errors.New("unix: no address supplied")

pkg/input/udplisten.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package input
2+
3+
import (
4+
"io"
5+
"net"
6+
)
7+
8+
// Create a UDP listener and return as ReadCloser
9+
func udpListen(addr string) (io.ReadCloser, error) {
10+
a, err := net.ResolveUDPAddr("udp", addr)
11+
if err != nil {
12+
return nil, err
13+
}
14+
return net.ListenUDP("udp", a)
15+
}

pkg/input/udpmulticast.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package input
2+
3+
import (
4+
"io"
5+
"net"
6+
"strings"
7+
)
8+
9+
// Create a UDP multicast listener and return as ReadCloser
10+
func udpMulticast(arg string) (io.ReadCloser, error) {
11+
var addr string
12+
var err error
13+
var iface *net.Interface
14+
parts := strings.SplitN(arg, ",", 2)
15+
if len(parts) == 1 {
16+
addr = parts[0]
17+
} else {
18+
addr = parts[1]
19+
// If interface is supplied, look it up
20+
iface, err = net.InterfaceByName(parts[0])
21+
if err != nil {
22+
return nil, err
23+
}
24+
}
25+
a, err := net.ResolveUDPAddr("udp", addr)
26+
if err != nil {
27+
return nil, err
28+
}
29+
return net.ListenMulticastUDP("udp", iface, a)
30+
}

pkg/output/output.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ func New(opts string) (io.WriteCloser, error) {
6565
return nil, errors.New("tcp-listen: no address supplied")
6666
}
6767
return tcpListen(arg)
68+
case "udp":
69+
if arg == "" {
70+
return nil, errors.New("udp: no address supplied")
71+
}
72+
return udpClient(arg)
6873
case "unix":
6974
if arg == "" {
7075
return nil, errors.New("unix: no address supplied")

pkg/output/udpclient.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package output
2+
3+
import (
4+
"io"
5+
"net"
6+
)
7+
8+
// Create a UDP client and return as WriteCloser
9+
func udpClient(addr string) (io.WriteCloser, error) {
10+
return net.Dial("udp", addr)
11+
}

0 commit comments

Comments
 (0)