aboutsummaryrefslogtreecommitdiff
path: root/plugin/rewrite/wire.go
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/rewrite/wire.go')
-rw-r--r--plugin/rewrite/wire.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/plugin/rewrite/wire.go b/plugin/rewrite/wire.go
new file mode 100644
index 000000000..116b7dcc1
--- /dev/null
+++ b/plugin/rewrite/wire.go
@@ -0,0 +1,37 @@
+package rewrite
+
+import (
+ "encoding/binary"
+ "fmt"
+ "net"
+ "strconv"
+)
+
+// ipToWire writes IP address to wire/binary format, 4 or 16 bytes depends on IPV4 or IPV6.
+func ipToWire(family int, ipAddr string) ([]byte, error) {
+
+ switch family {
+ case 1:
+ return net.ParseIP(ipAddr).To4(), nil
+ case 2:
+ return net.ParseIP(ipAddr).To16(), nil
+ }
+ return nil, fmt.Errorf("invalid IP address family (i.e. version) %d", family)
+}
+
+// uint16ToWire writes unit16 to wire/binary format
+func uint16ToWire(data uint16) []byte {
+ buf := make([]byte, 2)
+ binary.BigEndian.PutUint16(buf, uint16(data))
+ return buf
+}
+
+// portToWire writes port to wire/binary format, 2 bytes
+func portToWire(portStr string) ([]byte, error) {
+
+ port, err := strconv.ParseUint(portStr, 10, 16)
+ if err != nil {
+ return nil, err
+ }
+ return uint16ToWire(uint16(port)), nil
+}