Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Unified Diff: go/src/infra/crimson/server/crimsondb/crimsondb.go

Issue 2105213002: Created MacAddrStringToHexString and reverse (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@crimson-add-host
Patch Set: More tests to get coverage up Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | go/src/infra/crimson/server/crimsondb/crimsondb_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/src/infra/crimson/server/crimsondb/crimsondb.go
diff --git a/go/src/infra/crimson/server/crimsondb/crimsondb.go b/go/src/infra/crimson/server/crimsondb/crimsondb.go
index 250d165db739fbd12be27cb4c40a0cbf4d75c0c3..752e6bcfa22c4c7aae9f398ae6224769702b6239 100644
--- a/go/src/infra/crimson/server/crimsondb/crimsondb.go
+++ b/go/src/infra/crimson/server/crimsondb/crimsondb.go
@@ -45,10 +45,44 @@ func IPStringToHexString(ip string) (string, error) {
return "0x" + hex.EncodeToString(ipb), nil
}
+// MacAddrStringToHexString turns a mac address into a hex string.
+func MacAddrStringToHexString(macAddr string) (string, error) {
+ mac, err := net.ParseMAC(macAddr)
+ if err != nil {
+ return "", err
+ }
+ return "0x" + hex.EncodeToString(mac), nil
+}
+
+// HexStringToHardwareAddr turns an hex string into a hardware address.
+func HexStringToHardwareAddr(hexMac string) (net.HardwareAddr, error) {
+ // 6 bytes in hex + leading '0x'
+ if len(hexMac) < 14 {
+ err := fmt.Errorf("parsing of hex string failed (too short: %d characters)",
+ len(hexMac))
+ return net.HardwareAddr{}, err
+ }
+ if hexMac[:2] != "0x" {
+ return net.HardwareAddr{}, fmt.Errorf("parsing of hex string failed: %s", hexMac)
+ }
+ hwAddrRaw, err := hex.DecodeString(hexMac[2:])
+ if err != nil {
+ return net.HardwareAddr{}, err
+ }
+ hwAddr := make(net.HardwareAddr, len(hwAddrRaw))
+ for n := 0; n < len(hwAddrRaw); n++ {
+ hwAddr[n] = hwAddrRaw[n]
+ }
+ return hwAddr, nil
+}
+
// HexStringToIP converts an hex string returned by MySQL into a net.IP structure.
-func HexStringToIP(hexIP string) net.IP {
+func HexStringToIP(hexIP string) (net.IP, error) {
// TODO(pgervais): Add decent error checking. Ex: check hexIP starts with '0x'.
- ip, _ := hex.DecodeString(hexIP[2:])
+ ip, err := hex.DecodeString(hexIP[2:])
+ if err != nil {
+ return net.IP{}, err
+ }
length := 4
if len(ip) > 4 {
length = 16
@@ -57,7 +91,7 @@ func HexStringToIP(hexIP string) net.IP {
for n := 1; n <= len(ip); n++ {
netIP[length-n] = ip[len(ip)-n]
}
- return netIP
+ return netIP, nil
}
// scanIPRanges is a low-level function to scan sql results.
@@ -67,14 +101,24 @@ func scanIPRanges(ctx context.Context, rows *sql.Rows) ([]IPRange, error) {
for rows.Next() {
var startIP, endIP string
+ var ip net.IP
ipRange := IPRange{}
err := rows.Scan(&ipRange.Site, &ipRange.Vlan, &startIP, &endIP)
if err != nil { // Users can't trigger that.
logging.Errorf(ctx, "%s", err)
return nil, err
}
- ipRange.StartIP = HexStringToIP(startIP).String()
- ipRange.EndIP = HexStringToIP(endIP).String()
+ ip, err = HexStringToIP(startIP)
+ if err != nil {
+ return nil, err
+ }
+ ipRange.StartIP = ip.String()
+
+ ip, err = HexStringToIP(endIP)
+ if err != nil {
+ return nil, err
+ }
+ ipRange.EndIP = ip.String()
ipRanges = append(ipRanges, ipRange)
}
err := rows.Err()
« no previous file with comments | « no previous file | go/src/infra/crimson/server/crimsondb/crimsondb_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698