summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPablo Mazzini <pmazzini@gmail.com>2018-10-15 18:03:30 +0100
committerinsomniac <insomniacslk@users.noreply.github.com>2018-10-15 18:03:30 +0100
commitf1e31f7484231a7c51b57dba85abd174f043cc1e (patch)
tree16202b92256c50f9df255e9e76030dca674d089c
parentd17d7ed73609154a9c72e1194a4ad4842447dde8 (diff)
Added String methods for types (#175)
-rw-r--r--dhcpv4/dhcpv4.go6
-rw-r--r--dhcpv4/option_archtype.go3
-rw-r--r--dhcpv6/dhcpv6message.go2
-rw-r--r--dhcpv6/dhcpv6relay.go8
-rw-r--r--dhcpv6/duid.go17
-rw-r--r--dhcpv6/option_archtype.go3
-rw-r--r--dhcpv6/option_statuscode.go2
-rw-r--r--dhcpv6/types.go11
-rw-r--r--iana/archtype.go4
-rw-r--r--iana/hwtypes.go8
-rw-r--r--iana/statuscodes.go4
11 files changed, 26 insertions, 42 deletions
diff --git a/dhcpv4/dhcpv4.go b/dhcpv4/dhcpv4.go
index eebc1b0..51621e9 100644
--- a/dhcpv4/dhcpv4.go
+++ b/dhcpv4/dhcpv4.go
@@ -327,11 +327,7 @@ func (d *DHCPv4) HwType() iana.HwTypeType {
// HwTypeToString returns the mnemonic name for the hardware type, e.g.
// "Ethernet". If the type is unknown, it returns "Unknown".
func (d *DHCPv4) HwTypeToString() string {
- hwtype, ok := iana.HwTypeToString[d.hwType]
- if !ok {
- hwtype = "Invalid"
- }
- return hwtype
+ return d.hwType.String()
}
// SetHwType returns the hardware type as defined by IANA.
diff --git a/dhcpv4/option_archtype.go b/dhcpv4/option_archtype.go
index 8eafc55..92a3769 100644
--- a/dhcpv4/option_archtype.go
+++ b/dhcpv4/option_archtype.go
@@ -42,8 +42,7 @@ func (o *OptClientArchType) Length() int {
func (o *OptClientArchType) String() string {
var archTypes string
for idx, at := range o.ArchTypes {
- name := iana.ArchTypeToString(at)
- archTypes += name
+ archTypes += at.String()
if idx < len(o.ArchTypes)-1 {
archTypes += ", "
}
diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go
index cb17624..26be5f1 100644
--- a/dhcpv6/dhcpv6message.go
+++ b/dhcpv6/dhcpv6message.go
@@ -252,7 +252,7 @@ func (d *DHCPv6Message) SetMessage(messageType MessageType) {
}
func (d *DHCPv6Message) MessageTypeToString() string {
- return MessageTypeToString(d.messageType)
+ return d.messageType.String()
}
func (d *DHCPv6Message) TransactionID() uint32 {
diff --git a/dhcpv6/dhcpv6relay.go b/dhcpv6/dhcpv6relay.go
index 4eabefe..220fd3d 100644
--- a/dhcpv6/dhcpv6relay.go
+++ b/dhcpv6/dhcpv6relay.go
@@ -3,7 +3,6 @@ package dhcpv6
import (
"errors"
"fmt"
- "log"
"net"
)
@@ -22,7 +21,7 @@ func (r *DHCPv6Relay) Type() MessageType {
}
func (r *DHCPv6Relay) MessageTypeToString() string {
- return MessageTypeToString(r.messageType)
+ return r.messageType.String()
}
func (r *DHCPv6Relay) String() string {
@@ -63,11 +62,6 @@ func (r *DHCPv6Relay) ToBytes() []byte {
return ret
}
-func (r *DHCPv6Relay) MessageType() MessageType {
- log.Printf("Warning: DHCPv6Relay.MessageType() is deprecated and will be removed, use DHCPv6Relay.Type() instead")
- return r.messageType
-}
-
func (r *DHCPv6Relay) SetMessageType(messageType MessageType) {
// not enforcing if message type is not a RELAY_FORW or a RELAY_REPL message
r.messageType = messageType
diff --git a/dhcpv6/duid.go b/dhcpv6/duid.go
index 36da9cd..8530827 100644
--- a/dhcpv6/duid.go
+++ b/dhcpv6/duid.go
@@ -25,6 +25,13 @@ var DuidTypeToString = map[DuidType]string{
DUID_UUID: "DUID-UUID",
}
+func (d DuidType) String() string {
+ if dtype, ok := DuidTypeToString[d]; ok {
+ return dtype
+ }
+ return "Unknown"
+}
+
type Duid struct {
Type DuidType
HwType iana.HwTypeType // for DUID-LLT and DUID-LL. Ignored otherwise. RFC 826
@@ -79,14 +86,6 @@ func (d *Duid) ToBytes() []byte {
}
func (d *Duid) String() string {
- dtype := DuidTypeToString[d.Type]
- if dtype == "" {
- dtype = "Unknown"
- }
- hwtype := iana.HwTypeToString[d.HwType]
- if hwtype == "" {
- hwtype = "Unknown"
- }
var hwaddr string
if d.HwType == iana.HwTypeEthernet {
for _, b := range d.LinkLayerAddr {
@@ -96,7 +95,7 @@ func (d *Duid) String() string {
hwaddr = hwaddr[:len(hwaddr)-1]
}
}
- return fmt.Sprintf("DUID{type=%v hwtype=%v hwaddr=%v}", dtype, hwtype, hwaddr)
+ return fmt.Sprintf("DUID{type=%v hwtype=%v hwaddr=%v}", d.Type.String(), d.HwType.String(), hwaddr)
}
func DuidFromBytes(data []byte) (*Duid, error) {
diff --git a/dhcpv6/option_archtype.go b/dhcpv6/option_archtype.go
index 231eddd..636d10f 100644
--- a/dhcpv6/option_archtype.go
+++ b/dhcpv6/option_archtype.go
@@ -39,8 +39,7 @@ func (op *OptClientArchType) Length() int {
func (op *OptClientArchType) String() string {
atStrings := make([]string, 0)
for _, at := range op.ArchTypes {
- name := iana.ArchTypeToString(at)
- atStrings = append(atStrings, name)
+ atStrings = append(atStrings, at.String())
}
return fmt.Sprintf("OptClientArchType{archtype=%v}", strings.Join(atStrings, ", "))
}
diff --git a/dhcpv6/option_statuscode.go b/dhcpv6/option_statuscode.go
index 11f1cbf..74d200c 100644
--- a/dhcpv6/option_statuscode.go
+++ b/dhcpv6/option_statuscode.go
@@ -38,7 +38,7 @@ func (op *OptStatusCode) Length() int {
func (op *OptStatusCode) String() string {
return fmt.Sprintf("OptStatusCode{code=%s (%d), message=%v}",
- iana.StatusCodeToString(op.StatusCode), op.StatusCode,
+ op.StatusCode.String(), op.StatusCode,
string(op.StatusMessage))
}
diff --git a/dhcpv6/types.go b/dhcpv6/types.go
index 9c9f84f..4629cf6 100644
--- a/dhcpv6/types.go
+++ b/dhcpv6/types.go
@@ -1,9 +1,5 @@
package dhcpv6
-import (
- "log"
-)
-
// from http://www.networksorcery.com/enp/protocol/dhcpv6.htm
// MessageType represents the kind of DHCPv6 message.
@@ -39,13 +35,6 @@ func (m MessageType) String() string {
return "Unknown"
}
-// MessageTypeToString converts a MessageType to a human-readable string
-// representation.
-func MessageTypeToString(t MessageType) string {
- log.Printf("Warning: MessageTypeToString is deprecated and will be removed, use MessageType.String() instead")
- return t.String()
-}
-
// MessageTypeToStringMap contains the mapping of MessageTypes to human-readable
// strings.
var MessageTypeToStringMap = map[MessageType]string{
diff --git a/iana/archtype.go b/iana/archtype.go
index 6b2cc9e..ca21490 100644
--- a/iana/archtype.go
+++ b/iana/archtype.go
@@ -32,8 +32,8 @@ var ArchTypeToStringMap = map[ArchType]string{
}
-// ArchTypeToString returns a mnemonic name for a given architecture type
-func ArchTypeToString(a ArchType) string {
+// String returns a mnemonic name for a given architecture type
+func (a ArchType) String() string {
if at := ArchTypeToStringMap[a]; at != "" {
return at
}
diff --git a/iana/hwtypes.go b/iana/hwtypes.go
index 7467a78..075cded 100644
--- a/iana/hwtypes.go
+++ b/iana/hwtypes.go
@@ -78,3 +78,11 @@ var HwTypeToString = map[HwTypeType]string{
HwTypeWiegandInterface: "Wiegand Interface",
HwTypePureIP: "Pure IP",
}
+
+func (h HwTypeType) String() string {
+ hwtype := HwTypeToString[h]
+ if hwtype == "" {
+ hwtype = "Invalid"
+ }
+ return hwtype
+}
diff --git a/iana/statuscodes.go b/iana/statuscodes.go
index d3ca410..f61f99e 100644
--- a/iana/statuscodes.go
+++ b/iana/statuscodes.go
@@ -38,8 +38,8 @@ const (
StatusExcessiveTimeSkew StatusCode = 22
)
-// StatusCodeToString returns a mnemonic name for a given status code
-func StatusCodeToString(s StatusCode) string {
+// String returns a mnemonic name for a given status code
+func (s StatusCode) String() string {
if sc := StatusCodeToStringMap[s]; sc != "" {
return sc
}