OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package crimsondb | 5 package crimsondb |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "database/sql" | 9 "database/sql" |
10 "encoding/hex" | 10 "encoding/hex" |
(...skipping 27 matching lines...) Expand all Loading... |
38 ipb := net.ParseIP(ip) | 38 ipb := net.ParseIP(ip) |
39 if ipb == nil { | 39 if ipb == nil { |
40 return "", fmt.Errorf("parsing of IP address failed: %s", ip) | 40 return "", fmt.Errorf("parsing of IP address failed: %s", ip) |
41 } | 41 } |
42 if ipb.DefaultMask() != nil { | 42 if ipb.DefaultMask() != nil { |
43 ipb = ipb.To4() | 43 ipb = ipb.To4() |
44 } | 44 } |
45 return "0x" + hex.EncodeToString(ipb), nil | 45 return "0x" + hex.EncodeToString(ipb), nil |
46 } | 46 } |
47 | 47 |
| 48 // MacAddrStringToHexString turns a mac address into a hex string. |
| 49 func MacAddrStringToHexString(macAddr string) (string, error) { |
| 50 mac, err := net.ParseMAC(macAddr) |
| 51 if err != nil { |
| 52 return "", err |
| 53 } |
| 54 return "0x" + hex.EncodeToString(mac), nil |
| 55 } |
| 56 |
| 57 // HexStringToHardwareAddr turns an hex string into a hardware address. |
| 58 func HexStringToHardwareAddr(hexMac string) (net.HardwareAddr, error) { |
| 59 // 6 bytes in hex + leading '0x' |
| 60 if len(hexMac) < 14 { |
| 61 err := fmt.Errorf("parsing of hex string failed (too short: %d c
haracters)", |
| 62 len(hexMac)) |
| 63 return net.HardwareAddr{}, err |
| 64 } |
| 65 if hexMac[:2] != "0x" { |
| 66 return net.HardwareAddr{}, fmt.Errorf("parsing of hex string fai
led: %s", hexMac) |
| 67 } |
| 68 hwAddrRaw, err := hex.DecodeString(hexMac[2:]) |
| 69 if err != nil { |
| 70 return net.HardwareAddr{}, err |
| 71 } |
| 72 hwAddr := make(net.HardwareAddr, len(hwAddrRaw)) |
| 73 for n := 0; n < len(hwAddrRaw); n++ { |
| 74 hwAddr[n] = hwAddrRaw[n] |
| 75 } |
| 76 return hwAddr, nil |
| 77 } |
| 78 |
48 // HexStringToIP converts an hex string returned by MySQL into a net.IP structur
e. | 79 // HexStringToIP converts an hex string returned by MySQL into a net.IP structur
e. |
49 func HexStringToIP(hexIP string) net.IP { | 80 func HexStringToIP(hexIP string) (net.IP, error) { |
50 // TODO(pgervais): Add decent error checking. Ex: check hexIP starts wit
h '0x'. | 81 // TODO(pgervais): Add decent error checking. Ex: check hexIP starts wit
h '0x'. |
51 » ip, _ := hex.DecodeString(hexIP[2:]) | 82 » ip, err := hex.DecodeString(hexIP[2:]) |
| 83 » if err != nil { |
| 84 » » return net.IP{}, err |
| 85 » } |
52 length := 4 | 86 length := 4 |
53 if len(ip) > 4 { | 87 if len(ip) > 4 { |
54 length = 16 | 88 length = 16 |
55 } | 89 } |
56 netIP := make(net.IP, length) | 90 netIP := make(net.IP, length) |
57 for n := 1; n <= len(ip); n++ { | 91 for n := 1; n <= len(ip); n++ { |
58 netIP[length-n] = ip[len(ip)-n] | 92 netIP[length-n] = ip[len(ip)-n] |
59 } | 93 } |
60 » return netIP | 94 » return netIP, nil |
61 } | 95 } |
62 | 96 |
63 // scanIPRanges is a low-level function to scan sql results. | 97 // scanIPRanges is a low-level function to scan sql results. |
64 // Rows must contain site, vlan, start_ip, end_ip in that order. | 98 // Rows must contain site, vlan, start_ip, end_ip in that order. |
65 func scanIPRanges(ctx context.Context, rows *sql.Rows) ([]IPRange, error) { | 99 func scanIPRanges(ctx context.Context, rows *sql.Rows) ([]IPRange, error) { |
66 var ipRanges []IPRange | 100 var ipRanges []IPRange |
67 | 101 |
68 for rows.Next() { | 102 for rows.Next() { |
69 var startIP, endIP string | 103 var startIP, endIP string |
| 104 var ip net.IP |
70 ipRange := IPRange{} | 105 ipRange := IPRange{} |
71 err := rows.Scan(&ipRange.Site, &ipRange.Vlan, &startIP, &endIP) | 106 err := rows.Scan(&ipRange.Site, &ipRange.Vlan, &startIP, &endIP) |
72 if err != nil { // Users can't trigger that. | 107 if err != nil { // Users can't trigger that. |
73 logging.Errorf(ctx, "%s", err) | 108 logging.Errorf(ctx, "%s", err) |
74 return nil, err | 109 return nil, err |
75 } | 110 } |
76 » » ipRange.StartIP = HexStringToIP(startIP).String() | 111 » » ip, err = HexStringToIP(startIP) |
77 » » ipRange.EndIP = HexStringToIP(endIP).String() | 112 » » if err != nil { |
| 113 » » » return nil, err |
| 114 » » } |
| 115 » » ipRange.StartIP = ip.String() |
| 116 |
| 117 » » ip, err = HexStringToIP(endIP) |
| 118 » » if err != nil { |
| 119 » » » return nil, err |
| 120 » » } |
| 121 » » ipRange.EndIP = ip.String() |
78 ipRanges = append(ipRanges, ipRange) | 122 ipRanges = append(ipRanges, ipRange) |
79 } | 123 } |
80 err := rows.Err() | 124 err := rows.Err() |
81 if err != nil { | 125 if err != nil { |
82 logging.Errorf(ctx, "%s", err) | 126 logging.Errorf(ctx, "%s", err) |
83 return nil, err | 127 return nil, err |
84 } | 128 } |
85 return ipRanges, nil | 129 return ipRanges, nil |
86 } | 130 } |
87 | 131 |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 // DB gets the current db handle from the context. | 283 // DB gets the current db handle from the context. |
240 func DB(ctx context.Context) *sql.DB { | 284 func DB(ctx context.Context) *sql.DB { |
241 return ctx.Value("dbHandle").(*sql.DB) | 285 return ctx.Value("dbHandle").(*sql.DB) |
242 } | 286 } |
243 | 287 |
244 // GetDBHandle returns a handle to the Cloud SQL instance used by this deploymen
t. | 288 // GetDBHandle returns a handle to the Cloud SQL instance used by this deploymen
t. |
245 func GetDBHandle() (*sql.DB, error) { | 289 func GetDBHandle() (*sql.DB, error) { |
246 // TODO(pgervais): do not hard-code the name of the database. | 290 // TODO(pgervais): do not hard-code the name of the database. |
247 return sql.Open("mysql", "root@cloudsql(crimson-staging:crimson-staging)
/crimson") | 291 return sql.Open("mysql", "root@cloudsql(crimson-staging:crimson-staging)
/crimson") |
248 } | 292 } |
OLD | NEW |