-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add udp * version bump
- Loading branch information
Showing
3 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |