This was done anyway, but only deep in the functions, just do this everywhere; allows for shorter code and request.Request allows for caching as well. Cleanups, make it more Go like. * remove unneeded switches * remove testdir (why was this there??) * simplify the logic * remove unneeded variables * put short functions on a single line * fix documentation. * spin off wire funcs in wire.go, make them functions. Signed-off-by: Miek Gieben <miek@miek.nl>
37 lines
841 B
Go
37 lines
841 B
Go
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
|
|
}
|