Repository: jcmturner/gokrb5 (go)

Issue:
types.GetHostAddress doesn't produce correct addresses
The client machine has the following addresses: `172.17.0.1, 192.168.3.225, 172.17.8.1`. The service is running locally.

This is a trace of a service trying to check host adresses contained in a ticket:

```
trace: validate APREQ: client address: 127.0.0.1:45860
trace: validate APREQ: client address: {2 [49 50 55 46 48 46 48 46 49]}
trace: validate APREQ: ticket client addresses: [{2 [172 17 0 1]} {2 [192 168 3 225]} {2 [172 17 8 1]}]
```

We do find the client IPs in the ticket, encoded as 4 bytes. `types.GetHostAddres()` encodes the client address (`127.0.0.1`) in a weird way though.`0x49 0x50 0x55 0x46 0x48 0x46 0x48 0x46 0x49` is indeed the string `"127.0.0.1"` where each character is encoded separately (and the hex value end up as base 10!).

For `127.0.0.1` I would expect `types.GetHostAddres()` to return `{2 [127 0 0 1]}`. That's what the [spec](https://tools.ietf.org/html/rfc1510#section-8.1) seems to be saying.

Pull request description:
fix GetHostAddress values
resolves #232 

Patch:
diff --git a/types/HostAddress.go b/types/HostAddress.go
index 3ce45a3..ae8e23a 100644
--- a/types/HostAddress.go
+++ b/types/HostAddress.go
@@ -53,21 +53,19 @@ func GetHostAddress(s string) (HostAddress, error) {
 		return h, fmt.Errorf("invalid format of client address: %v", err)
 	}
 	ip := net.ParseIP(cAddr)
-	hb, err := ip.MarshalText()
-	if err != nil {
-		return h, fmt.Errorf("could not marshal client's address into bytes: %v", err)
-	}
 	var ht int32
 	if ip.To4() != nil {
 		ht = addrtype.IPv4
+		ip = ip.To4()
 	} else if ip.To16() != nil {
 		ht = addrtype.IPv6
+		ip = ip.To16()
 	} else {
 		return h, fmt.Errorf("could not determine client's address types: %v", err)
 	}
 	h = HostAddress{
 		AddrType: ht,
-		Address:  hb,
+		Address:  ip,
 	}
 	return h, nil
 }


Test patch:
diff --git a/types/HostAddress_test.go b/types/HostAddress_test.go
new file mode 100644
index 0000000..0437ce1
--- /dev/null
+++ b/types/HostAddress_test.go
@@ -0,0 +1,29 @@
+package types
+
+import (
+	"encoding/hex"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	"gopkg.in/jcmturner/gokrb5.v6/iana/addrtype"
+)
+
+func TestGetHostAddress(t *testing.T) {
+	tests := []struct {
+		str    string
+		ipType int32
+		hex    string
+	}{
+		{"192.168.1.100", addrtype.IPv4, "c0a80164"},
+		{"127.0.0.1", addrtype.IPv4, "7f000001"},
+		{"[fe80::1cf3:b43b:df29:d43e]", addrtype.IPv6, "fe800000000000001cf3b43bdf29d43e"},
+	}
+	for _, test := range tests {
+		h, err := GetHostAddress(test.str + ":1234")
+		if err != nil {
+			t.Errorf("error getting host for %s: %v", test.str, err)
+		}
+		assert.Equal(t, test.ipType, h.AddrType, "wrong address type for %s", test.str)
+		assert.Equal(t, test.hex, hex.EncodeToString(h.Address), "wrong address bytes for %s", test.str)
+	}
+}