File: driver_parser.go

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (2571 lines) | stat: -rw-r--r-- 71,896 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
package common

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"log"
	"math"
	"net"
	"os"
	"reflect"
	"regexp"
	"sort"
	"strconv"
	"strings"
	"time"
)

/** low-level parsing */
// strip the comments and extraneous newlines from a byte channel
func uncomment(in <-chan byte) chan byte {
	out := make(chan byte)

	go func(in <-chan byte, out chan byte) {
		var endofline bool

		for {
			by, ok := <-in
			if !ok {
				break
			}

			// If we find a comment, then everything until the end of line
			// needs to be culled. We keep track of that using the `endofline`
			// flag.
			if by == '#' {
				endofline = true

			} else if by == '\n' && endofline {
				endofline = false
			}

			// If we're not in the processing of culling bytes, then write what
			// we've read into our output chan.
			if !endofline {
				out <- by
			}
		}
		close(out)
	}(in, out)
	return out
}

// convert a byte channel into a channel of pseudo-tokens
func tokenizeDhcpConfig(in chan byte) chan string {
	var state string
	var quote bool

	out := make(chan string)
	go func(out chan string) {
		for {
			by, ok := <-in
			if !ok {
				break
			}

			// If we're in a quote, then we continue until we're not in a quote
			// before we start looing for tokens
			if quote {
				if by == '"' {
					out <- state + string(by)
					state, quote = "", false
					continue
				}
				state += string(by)
				continue
			}

			switch by {
			case '"':
				// Otherwise we're outside any quotes and can process bytes normaly
				quote = true
				state += string(by)
				continue

			case '\r':
				fallthrough
			case '\n':
				fallthrough
			case '\t':
				fallthrough
			case ' ':
				// Whitespace is a separator, so we check to see if there's any state.
				// If so, then write our state prior to resetting.

				if len(state) == 0 {
					continue
				}
				out <- state
				state = ""

			case '{':
				fallthrough
			case '}':
				fallthrough
			case ';':
				// If we encounter a brace or a semicolon, then we need to emit our
				// state and then the byte because it can be part of the token.

				if len(state) > 0 {
					out <- state
				}
				out <- string(by)
				state = ""

			default:
				// Just a byte which needs to be aggregated into our state
				state += string(by)
			}
		}

		// If we still have any data left, then make sure to emit that
		if len(state) > 0 {
			out <- state
		}

		// Close our channel since we're responsible for it.
		close(out)
	}(out)
	return out
}

/** mid-level parsing */
type tkParameter struct {
	name    string
	operand []string
}

func (e *tkParameter) String() string {
	var values []string
	for _, val := range e.operand {
		values = append(values, val)
	}
	return fmt.Sprintf("%s [%s]", e.name, strings.Join(values, ","))
}

type tkGroup struct {
	parent *tkGroup
	id     tkParameter

	groups []*tkGroup
	params []tkParameter
}

func (e *tkGroup) String() string {
	var id []string

	id = append(id, e.id.name)
	for _, val := range e.id.operand {
		id = append(id, val)
	}

	var config []string
	for _, val := range e.params {
		config = append(config, val.String())
	}
	return fmt.Sprintf("%s {\n%s\n}", strings.Join(id, " "), strings.Join(config, "\n"))
}

// convert a channel of pseudo-tokens into an tkParameter struct
func parseTokenParameter(in chan string) tkParameter {
	var result tkParameter
	for {
		token, ok := <-in
		if !ok {
			break
		}

		// If there's no name for this parameter yet, then the first token
		// is our name. Snag it into our struct, and grab the next one.
		if result.name == "" {
			result.name = token
			continue
		}

		// If encounter any braces or line-terminators, then we're done parsing.
		// Anything else we find are just operands we need to keep track of.
		if strings.ContainsAny("{};", token) {
			break
		} else {
			result.operand = append(result.operand, token)
		}
	}
	return result
}

// convert a channel of pseudo-tokens into an tkGroup tree */
func parseDhcpConfig(in chan string) (tkGroup, error) {
	var tokens []string
	var result tkGroup

	// This utility function takes a list of tokens and line-terminates them
	// before sending them to parseTokenParameter().
	toParameter := func(tokens []string) tkParameter {
		out := make(chan string)
		go func(out chan string) {
			for _, v := range tokens {
				out <- v
			}
			out <- ";"
			close(out)
		}(out)
		return parseTokenParameter(out)
	}

	// Start building our tree using result as our root node
	node := &result
	for {
		tk, ok := <-in
		if !ok {
			break
		}

		switch tk {
		case "{":
			// If our next token is an opening brace, then we need to collect our
			// current aggregated tokens to parse, push our current node onto the
			// tree, and then pivot into it. Then we can reset our tokens for the child.

			grp := &tkGroup{parent: node}
			grp.id = toParameter(tokens)

			node.groups = append(node.groups, grp)
			node = grp

			tokens = []string{}

		case "}":
			// Otherwise if it's a closing brace, then we need to pop back up to
			// the parent node and resume parsing. If we have any tokens, then
			// that was because they were unterminated. Raise an error in that case.

			if node.parent == nil {
				return tkGroup{}, fmt.Errorf("Refusing to close the global declaration")
			}
			if len(tokens) > 0 {
				return tkGroup{}, fmt.Errorf("List of tokens was left unterminated : %v", tokens)
			}
			node = node.parent

			tokens = []string{}

		case ";":
			// If we encounter a line-terminator, then the list of tokens we've been
			// aggregating are ready to be parsed. Afterwards, we can write them
			// to our current tree node.

			arg := toParameter(tokens)
			node.params = append(node.params, arg)
			tokens = []string{}

		default:
			// Anything else requires us to aggregate our token into our list, and
			// try grabbing the next one.

			tokens = append(tokens, tk)
		}
	}
	return result, nil
}

func tokenizeNetworkMapConfig(in chan byte) chan string {
	var state string
	var quote bool
	var lastnewline bool

	// This logic is very similar to tokenizeDhcpConfig except she needs to handle
	// braces, and we don't. This is the only major difference from us.

	out := make(chan string)
	go func(out chan string) {
		for {
			by, ok := <-in
			if !ok {
				break
			}

			// If we're currently inide a quote, then we need to continue until
			// we encounter the closing quote. We'll keep collecting our state
			// in the meantime.
			if quote {
				if by == '"' {
					out <- state + string(by)
					state, quote = "", false
					continue
				}
				state += string(by)
				continue
			}

			switch by {
			case '"':
				// If we encounter a quote, then we need to transition into our
				// quote-parsing state that keeps collecting data until the closing
				// quote is encountered.

				quote = true
				state += string(by)
				continue

			case '\r':
				fallthrough
			case '\t':
				fallthrough
			case ' ':
				// Whitespace is considered a separator, so if we encounter this
				// then we can write our current state, and then reset.

				if len(state) == 0 {
					continue
				}
				out <- state
				state = ""

			case '\n':
				// Newlines are a somewhat special case because they separate each
				// attribute/line-item, and they can repeat. We need to preserve
				// this token, so we write our current state, then the newline.
				// We also maintain a flag so that we can consolidate multiple
				// newlines together.

				if lastnewline {
					continue
				}
				if len(state) > 0 {
					out <- state
				}
				out <- string(by)
				state = ""
				lastnewline = true
				continue

			case '.':
				fallthrough
			case '=':
				// These characters separate attributes or tokens from one another,
				// so they result in writing the state, the character, and then reset.

				if len(state) > 0 {
					out <- state
				}
				out <- string(by)
				state = ""

			default:
				// Any byte we couldn't parse just gets aggregated for the next pass.
				state += string(by)
			}

			// If we made it here, then we can guarantee that the we didn't just
			// process a newline. Clear this flag for the next one we find.
			lastnewline = false
		}

		// If there's anything left in our state, then the last line was just not
		// newline-terminated. This is a common occurrence, so write our current
		// state before we finish.
		if len(state) > 0 {
			out <- state
		}
		close(out)
	}(out)
	return out
}

func parseNetworkMapConfig(in chan string) (NetworkMap, error) {
	var state []string
	unsorted := make(map[string]map[string]string)

	// A network map has the following syntax "network.attribute = value". This
	// closure is responsible for using the "network" as a key into the `unsorted`
	// mapping, and then assigning the "value" into it keyed by the "attribute".
	addResult := func(network string, attribute string, value string) error {
		_, ok := unsorted[network]
		if !ok {
			unsorted[network] = make(map[string]string)
		}

		val, err := strconv.Unquote(value)
		if err != nil {
			return err
		}

		current := unsorted[network]
		current[attribute] = val
		return nil
	}

	// Loop through all of our tokens making sure to update our unsorted map.
	for {
		tk, ok := <-in
		if !ok {
			// If our token channel is closed, then check to see if we've
			// collected 3 items in our state. If so, then we can add this
			// final attribute/value before we leave.
			if len(state) == 3 {
				err := addResult(state[0], state[1], state[2])
				if err != nil {
					return nil, err
				}
			}
			break
		}

		// This switch makes sure we encounter these tokens in the correct order.
		switch tk {
		case ".":
			if len(state) != 1 {
				return nil, fmt.Errorf("Missing network index")
			}

		case "=":
			if len(state) != 2 {
				return nil, fmt.Errorf("Assignment to empty attribute")
			}

		case "\n":
			if len(state) == 0 {
				continue
			}
			if len(state) != 3 {
				return nil, fmt.Errorf("Invalid attribute assignment : %v", state)
			}
			err := addResult(state[0], state[1], state[2])
			if err != nil {
				return nil, err
			}
			state = make([]string, 0)

		default:
			state = append(state, tk)
		}
	}

	// Go through our unsorted map, and collect all of the keys for "network".
	result := make([]map[string]string, 0)
	var keys []string
	for k := range unsorted {
		keys = append(keys, k)
	}

	// This way we can sort them.
	sort.Strings(keys)

	// And then collect all of them into a list to return to the caller.
	for _, k := range keys {
		result = append(result, unsorted[k])
	}

	return result, nil
}

/** higher-level parsing */
/// parameters
type pParameter interface {
	repr() string
}

type pParameterInclude struct {
	filename string
}

func (e pParameterInclude) repr() string { return fmt.Sprintf("include-file:filename=%s", e.filename) }

type pParameterOption struct {
	name  string
	value string
}

func (e pParameterOption) repr() string { return fmt.Sprintf("option:%s=%s", e.name, e.value) }

// allow some-kind-of-something
type pParameterGrant struct {
	verb      string // allow,deny,ignore
	attribute string
}

func (e pParameterGrant) repr() string { return fmt.Sprintf("grant:%s,%s", e.verb, e.attribute) }

type pParameterAddress4 []string

func (e pParameterAddress4) repr() string {
	return fmt.Sprintf("fixed-address4:%s", strings.Join(e, ","))
}

type pParameterAddress6 []string

func (e pParameterAddress6) repr() string {
	return fmt.Sprintf("fixed-address6:%s", strings.Join(e, ","))
}

// hardware address 00:00:00:00:00:00
type pParameterHardware struct {
	class   string
	address []byte
}

func (e pParameterHardware) repr() string {
	res := make([]string, 0)

	for _, v := range e.address {
		res = append(res, fmt.Sprintf("%02x", v))
	}

	return fmt.Sprintf("hardware-address:%s[%s]", e.class, strings.Join(res, ":"))
}

type pParameterBoolean struct {
	parameter string
	truancy   bool
}

func (e pParameterBoolean) repr() string { return fmt.Sprintf("boolean:%s=%v", e.parameter, e.truancy) }

type pParameterClientMatch struct {
	name string
	data string
}

func (e pParameterClientMatch) repr() string {
	return fmt.Sprintf("match-client:%s=%s", e.name, e.data)
}

// range 127.0.0.1 127.0.0.255
type pParameterRange4 struct {
	min net.IP
	max net.IP
}

func (e pParameterRange4) repr() string {
	return fmt.Sprintf("range4:%s-%s", e.min.String(), e.max.String())
}

type pParameterRange6 struct {
	min net.IP
	max net.IP
}

func (e pParameterRange6) repr() string {
	return fmt.Sprintf("range6:%s-%s", e.min.String(), e.max.String())
}

type pParameterPrefix6 struct {
	min  net.IP
	max  net.IP
	bits int
}

func (e pParameterPrefix6) repr() string {
	return fmt.Sprintf("prefix6:/%d:%s-%s", e.bits, e.min.String(), e.max.String())
}

// some-kind-of-parameter 1024
type pParameterOther struct {
	parameter string
	value     string
}

func (e pParameterOther) repr() string { return fmt.Sprintf("parameter:%s=%s", e.parameter, e.value) }

type pParameterExpression struct {
	parameter  string
	expression string
}

func (e pParameterExpression) repr() string {
	return fmt.Sprintf("parameter-expression:%s=\"%s\"", e.parameter, e.expression)
}

type pDeclarationIdentifier interface {
	repr() string
}

type pDeclaration struct {
	id           pDeclarationIdentifier
	parent       *pDeclaration
	parameters   []pParameter
	declarations []pDeclaration
}

func (e *pDeclaration) short() string {
	return e.id.repr()
}

func (e *pDeclaration) repr() string {
	res := e.short()

	var parameters []string
	for _, v := range e.parameters {
		parameters = append(parameters, v.repr())
	}

	var groups []string
	for _, v := range e.declarations {
		groups = append(groups, fmt.Sprintf("-> %s", v.short()))
	}

	if e.parent != nil {
		res = fmt.Sprintf("%s parent:%s", res, e.parent.short())
	}

	return fmt.Sprintf("%s\n%s\n%s\n", res, strings.Join(parameters, "\n"), strings.Join(groups, "\n"))
}

type pDeclarationGlobal struct{}

func (e pDeclarationGlobal) repr() string { return fmt.Sprintf("{global}") }

type pDeclarationShared struct{ name string }

func (e pDeclarationShared) repr() string { return fmt.Sprintf("{shared-network %s}", e.name) }

type pDeclarationSubnet4 struct{ net.IPNet }

func (e pDeclarationSubnet4) repr() string { return fmt.Sprintf("{subnet4 %s}", e.String()) }

type pDeclarationSubnet6 struct{ net.IPNet }

func (e pDeclarationSubnet6) repr() string { return fmt.Sprintf("{subnet6 %s}", e.String()) }

type pDeclarationHost struct{ name string }

func (e pDeclarationHost) repr() string { return fmt.Sprintf("{host name:%s}", e.name) }

type pDeclarationPool struct{}

func (e pDeclarationPool) repr() string { return fmt.Sprintf("{pool}") }

type pDeclarationGroup struct{}

func (e pDeclarationGroup) repr() string { return fmt.Sprintf("{group}") }

type pDeclarationClass struct{ name string }

func (e pDeclarationClass) repr() string { return fmt.Sprintf("{class}") }

/** parsers */
func parseParameter(val tkParameter) (pParameter, error) {
	switch val.name {
	case "include":
		if len(val.operand) != 2 {
			return nil, fmt.Errorf("Invalid number of parameters for pParameterInclude : %v", val.operand)
		}

		name := val.operand[0]
		return pParameterInclude{filename: name}, nil

	case "option":
		if len(val.operand) != 2 {
			return nil, fmt.Errorf("Invalid number of parameters for pParameterOption : %v", val.operand)
		}

		name, value := val.operand[0], val.operand[1]
		return pParameterOption{name: name, value: value}, nil

	case "allow":
		fallthrough
	case "deny":
		fallthrough
	case "ignore":
		if len(val.operand) < 1 {
			return nil, fmt.Errorf("Invalid number of parameters for pParameterGrant : %v", val.operand)
		}

		attribute := strings.Join(val.operand, " ")
		return pParameterGrant{verb: strings.ToLower(val.name), attribute: attribute}, nil

	case "range":
		if len(val.operand) < 1 {
			return nil, fmt.Errorf("Invalid number of parameters for pParameterRange4 : %v", val.operand)
		}

		idxAddress := map[bool]int{true: 1, false: 0}[strings.ToLower(val.operand[0]) == "bootp"]
		if len(val.operand) > 2+idxAddress {
			return nil, fmt.Errorf("Invalid number of parameters for pParameterRange : %v", val.operand)
		}

		if idxAddress+1 > len(val.operand) {
			res := net.ParseIP(val.operand[idxAddress])
			return pParameterRange4{min: res, max: res}, nil
		}

		addr1 := net.ParseIP(val.operand[idxAddress])
		addr2 := net.ParseIP(val.operand[idxAddress+1])
		return pParameterRange4{min: addr1, max: addr2}, nil

	case "range6":
		if len(val.operand) == 1 {
			address := val.operand[0]
			if strings.Contains(address, "/") {
				cidr := strings.SplitN(address, "/", 2)
				if len(cidr) != 2 {
					return nil, fmt.Errorf("Unknown ipv6 format : %v", address)
				}

				address := net.ParseIP(cidr[0])

				bits, err := strconv.Atoi(cidr[1])
				if err != nil {
					return nil, err
				}
				mask := net.CIDRMask(bits, net.IPv6len*8)

				// figure out the network address
				network := address.Mask(mask)

				// make a broadcast address
				broadcast := network
				networkSize, totalSize := mask.Size()
				hostSize := totalSize - networkSize
				for i := networkSize / 8; i < totalSize/8; i++ {
					broadcast[i] = byte(0xff)
				}

				octetIndex := network[networkSize/8]
				bitsLeft := (uint32)(hostSize % 8)
				broadcast[octetIndex] = network[octetIndex] | ((1 << bitsLeft) - 1)

				// FIXME: check that the broadcast address was made correctly
				return pParameterRange6{min: network, max: broadcast}, nil
			}
			res := net.ParseIP(address)
			return pParameterRange6{min: res, max: res}, nil
		}

		if len(val.operand) == 2 {
			addr := net.ParseIP(val.operand[0])
			if strings.ToLower(val.operand[1]) == "temporary" {
				return pParameterRange6{min: addr, max: addr}, nil
			}

			other := net.ParseIP(val.operand[1])
			return pParameterRange6{min: addr, max: other}, nil
		}
		return nil, fmt.Errorf("Invalid number of parameters for pParameterRange6 : %v", val.operand)

	case "prefix6":
		if len(val.operand) != 3 {
			return nil, fmt.Errorf("Invalid number of parameters for pParameterRange6 : %v", val.operand)
		}

		bits, err := strconv.Atoi(val.operand[2])
		if err != nil {
			return nil, fmt.Errorf("Invalid bits for pParameterPrefix6 : %v", val.operand[2])
		}

		minaddr := net.ParseIP(val.operand[0])
		maxaddr := net.ParseIP(val.operand[1])
		return pParameterPrefix6{min: minaddr, max: maxaddr, bits: bits}, nil

	case "hardware":
		if len(val.operand) != 2 {
			return nil, fmt.Errorf("Invalid number of parameters for pParameterHardware : %v", val.operand)
		}

		class := val.operand[0]
		octets := strings.Split(val.operand[1], ":")
		address := make([]byte, 0)
		for _, v := range octets {
			b, err := strconv.ParseInt(v, 16, 0)
			if err != nil {
				return nil, err
			}
			address = append(address, byte(b))
		}

		return pParameterHardware{class: class, address: address}, nil

	case "fixed-address":
		ip4addrs := make(pParameterAddress4, len(val.operand))
		copy(ip4addrs, val.operand)
		return ip4addrs, nil

	case "fixed-address6":
		ip6addrs := make(pParameterAddress6, len(val.operand))
		copy(ip6addrs, val.operand)
		return ip6addrs, nil

	case "host-identifier":
		if len(val.operand) != 3 {
			return nil, fmt.Errorf("Invalid number of parameters for pParameterClientMatch : %v", val.operand)
		}

		if val.operand[0] != "option" {
			return nil, fmt.Errorf("Invalid match parameter : %v", val.operand[0])
		}

		optionName := val.operand[1]
		optionData := val.operand[2]
		return pParameterClientMatch{name: optionName, data: optionData}, nil

	default:
		length := len(val.operand)

		if length < 1 {
			return pParameterBoolean{parameter: val.name, truancy: true}, nil

		} else if length > 1 {
			if val.operand[0] == "=" {
				return pParameterExpression{parameter: val.name, expression: strings.Join(val.operand[1:], "")}, nil
			}
		}

		if length != 1 {
			return nil, fmt.Errorf("Invalid number of parameters for pParameterOther : %v", val.operand)
		}

		if strings.ToLower(val.name) == "not" {
			return pParameterBoolean{parameter: val.operand[0], truancy: false}, nil
		}

		return pParameterOther{parameter: val.name, value: val.operand[0]}, nil
	}
}

func parseTokenGroup(val tkGroup) (*pDeclaration, error) {
	params := val.id.operand

	switch val.id.name {
	case "group":
		return &pDeclaration{id: pDeclarationGroup{}}, nil

	case "pool":
		return &pDeclaration{id: pDeclarationPool{}}, nil

	case "host":
		if len(params) == 1 {
			return &pDeclaration{id: pDeclarationHost{name: params[0]}}, nil
		}

	case "subnet":
		if len(params) == 3 && strings.ToLower(params[1]) == "netmask" {
			addr := make([]byte, 4)
			for i, v := range strings.SplitN(params[2], ".", 4) {
				res, err := strconv.ParseInt(v, 10, 0)
				if err != nil {
					return nil, err
				}
				addr[i] = byte(res)
			}
			oc1, oc2, oc3, oc4 := addr[0], addr[1], addr[2], addr[3]
			if subnet, mask := net.ParseIP(params[0]), net.IPv4Mask(oc1, oc2, oc3, oc4); subnet != nil && mask != nil {
				return &pDeclaration{id: pDeclarationSubnet4{net.IPNet{IP: subnet, Mask: mask}}}, nil
			}
		}

	case "subnet6":
		if len(params) == 1 {
			ip6 := strings.SplitN(params[0], "/", 2)
			if len(ip6) == 2 && strings.Contains(ip6[0], ":") {
				address := net.ParseIP(ip6[0])
				prefix, err := strconv.Atoi(ip6[1])
				if err != nil {
					return nil, err
				}
				return &pDeclaration{id: pDeclarationSubnet6{net.IPNet{IP: address, Mask: net.CIDRMask(prefix, net.IPv6len*8)}}}, nil
			}
		}

	case "shared-network":
		if len(params) == 1 {
			return &pDeclaration{id: pDeclarationShared{name: params[0]}}, nil
		}

	case "":
		return &pDeclaration{id: pDeclarationGlobal{}}, nil
	}
	return nil, fmt.Errorf("Invalid pDeclaration : %v : %v", val.id.name, params)
}

func flattenDhcpConfig(root tkGroup) (*pDeclaration, error) {
	result, err := parseTokenGroup(root)

	if err != nil {
		return nil, err
	}

	for _, p := range root.params {
		param, err := parseParameter(p)
		if err != nil {
			return nil, err
		}
		result.parameters = append(result.parameters, param)
	}

	for _, p := range root.groups {
		group, err := flattenDhcpConfig(*p)
		if err != nil {
			return nil, err
		}
		group.parent = result
		result.declarations = append(result.declarations, *group)
	}

	return result, nil
}

/** reduce the tree into the things that we care about */
type grant uint

const (
	ALLOW  grant = iota
	IGNORE grant = iota
	DENY   grant = iota
)

type configDeclaration struct {
	id         []pDeclarationIdentifier
	composites []pDeclaration

	address []pParameter

	options     map[string]string
	grants      map[string]grant
	attributes  map[string]bool
	parameters  map[string]string
	expressions map[string]string

	hostid []pParameterClientMatch
}

func createDeclaration(node pDeclaration) configDeclaration {
	var hierarchy []pDeclaration

	for n := &node; n != nil; n = n.parent {
		hierarchy = append(hierarchy, *n)
	}

	var result configDeclaration
	result.address = make([]pParameter, 0)

	result.options = make(map[string]string)
	result.grants = make(map[string]grant)
	result.attributes = make(map[string]bool)
	result.parameters = make(map[string]string)
	result.expressions = make(map[string]string)

	result.hostid = make([]pParameterClientMatch, 0)

	// walk from globals to pDeclaration collecting all parameters
	for i := len(hierarchy) - 1; i >= 0; i-- {
		result.composites = append(result.composites, hierarchy[(len(hierarchy)-1)-i])
		result.id = append(result.id, hierarchy[(len(hierarchy)-1)-i].id)

		// update configDeclaration parameters
		for _, p := range hierarchy[i].parameters {
			switch p := p.(type) {
			case pParameterOption:
				result.options[p.name] = p.value
			case pParameterGrant:
				Grant := map[string]grant{"ignore": IGNORE, "allow": ALLOW, "deny": DENY}
				result.grants[p.attribute] = Grant[p.verb]
			case pParameterBoolean:
				result.attributes[p.parameter] = p.truancy
			case pParameterClientMatch:
				result.hostid = append(result.hostid, p)
			case pParameterExpression:
				result.expressions[p.parameter] = p.expression
			case pParameterOther:
				result.parameters[p.parameter] = p.value
			default:
				result.address = append(result.address, p)
			}
		}
	}
	return result
}

func (e *configDeclaration) repr() string {
	var result []string

	res := make([]string, 0)
	for _, v := range e.id {
		res = append(res, v.repr())
	}
	result = append(result, strings.Join(res, ","))

	if len(e.address) > 0 {
		res := make([]string, 0)
		for _, v := range e.address {
			res = append(res, v.repr())
		}
		result = append(result, fmt.Sprintf("address : %v", strings.Join(res, ",")))
	}

	if len(e.options) > 0 {
		result = append(result, fmt.Sprintf("options : %v", e.options))
	}
	if len(e.grants) > 0 {
		result = append(result, fmt.Sprintf("grants : %v", e.grants))
	}
	if len(e.attributes) > 0 {
		result = append(result, fmt.Sprintf("attributes : %v", e.attributes))
	}
	if len(e.parameters) > 0 {
		result = append(result, fmt.Sprintf("parameters : %v", e.parameters))
	}
	if len(e.expressions) > 0 {
		result = append(result, fmt.Sprintf("parameter-expressions : %v", e.expressions))
	}

	if len(e.hostid) > 0 {
		res := make([]string, 0)
		for _, v := range e.hostid {
			res = append(res, v.repr())
		}
		result = append(result, fmt.Sprintf("hostid : %v", strings.Join(res, " ")))
	}

	return strings.Join(result, "\n") + "\n"
}

func (e *configDeclaration) IP4() (net.IP, error) {
	var result []string

	for _, entry := range e.address {
		switch entry.(type) {
		case pParameterAddress4:
			for _, s := range entry.(pParameterAddress4) {
				result = append(result, s)
			}
		}
	}

	if len(result) > 1 {
		return nil, fmt.Errorf("More than one address4 returned : %v", result)

	} else if len(result) == 0 {
		return nil, fmt.Errorf("No IP4 addresses found")
	}

	// Try and parse it as an IP4. If so, then it's good to return it as-is.
	if res := net.ParseIP(result[0]); res != nil {
		return res, nil
	}

	// Otherwise make an attempt to resolve it to an address.
	res, err := net.ResolveIPAddr("ip4", result[0])
	if err != nil {
		return nil, err
	}

	return res.IP, nil
}

func (e *configDeclaration) IP6() (net.IP, error) {
	var result []string

	for _, entry := range e.address {
		switch entry.(type) {
		case pParameterAddress6:
			for _, s := range entry.(pParameterAddress6) {
				result = append(result, s)
			}
		}
	}

	if len(result) > 1 {
		return nil, fmt.Errorf("More than one address6 returned : %v", result)

	} else if len(result) == 0 {
		return nil, fmt.Errorf("No IP6 addresses found")
	}

	// If we were able to parse it into an IP, then we can just return it.
	if res := net.ParseIP(result[0]); res != nil {
		return res, nil
	}

	// Otherwise, try to resolve it into an address.
	res, err := net.ResolveIPAddr("ip6", result[0])
	if err != nil {
		return nil, err
	}
	return res.IP, nil
}

func (e *configDeclaration) Hardware() (net.HardwareAddr, error) {
	var result []pParameterHardware

	for _, addr := range e.address {
		switch addr.(type) {
		case pParameterHardware:
			result = append(result, addr.(pParameterHardware))
		}
	}

	if len(result) > 0 {
		return nil, fmt.Errorf("More than one hardware address returned : %v", result)
	}

	res := make(net.HardwareAddr, 0)
	for _, by := range result[0].address {
		res = append(res, by)
	}
	return res, nil
}

/*** Dhcp Configuration */
type DhcpConfiguration []configDeclaration

func ReadDhcpConfiguration(fd *os.File) (DhcpConfiguration, error) {
	fromfile := consumeFile(fd)
	uncommented := uncomment(fromfile)
	tokenized := tokenizeDhcpConfig(uncommented)

	// Parse the tokenized DHCP configuration into a tree. We need it as a tree
	// because DHCP declarations can inherit options from their parent
	parsetree, err := parseDhcpConfig(tokenized)
	if err != nil {
		return nil, err
	}

	// Flatten the tree into a list of pDeclaration objects. This is responsible
	// for actually propagating options from the parent pDeclaration into all of
	// its children.
	global, err := flattenDhcpConfig(parsetree)
	if err != nil {
		return nil, err
	}

	// This closure is just to the goro that follows it in recursively walking
	// through all of the declarations and writing them individually to a chan.
	var walkDeclarations func(root pDeclaration, out chan *configDeclaration)

	walkDeclarations = func(root pDeclaration, out chan *configDeclaration) {
		res := createDeclaration(root)
		out <- &res
		for _, p := range root.declarations {
			walkDeclarations(p, out)
		}
	}

	// That way this goro can take each individual declaration and write it to
	// a channel.
	each := make(chan *configDeclaration)
	go func(out chan *configDeclaration) {
		walkDeclarations(*global, out)
		out <- nil
	}(each)

	// For this loop to convert it into a itemized list.
	var result DhcpConfiguration
	for decl := <-each; decl != nil; decl = <-each {
		result = append(result, *decl)
	}
	return result, nil
}

func (e *DhcpConfiguration) Global() configDeclaration {
	result := (*e)[0]
	if len(result.id) != 1 {
		panic(fmt.Errorf("Something that can't happen happened"))
	}
	return result
}

func (e *DhcpConfiguration) SubnetByAddress(address net.IP) (configDeclaration, error) {
	var result []configDeclaration
	for _, entry := range *e {
		switch entry.id[0].(type) {
		case pDeclarationSubnet4:
			id := entry.id[0].(pDeclarationSubnet4)
			if id.Contains(address) {
				result = append(result, entry)
			}
		case pDeclarationSubnet6:
			id := entry.id[0].(pDeclarationSubnet6)
			if id.Contains(address) {
				result = append(result, entry)
			}
		}
	}
	if len(result) == 0 {
		return configDeclaration{}, fmt.Errorf("No network declarations containing %s found", address.String())
	}
	if len(result) > 1 {
		return configDeclaration{}, fmt.Errorf("More than 1 network declaration found : %v", result)
	}
	return result[0], nil
}

func (e *DhcpConfiguration) HostByName(host string) (configDeclaration, error) {
	var result []configDeclaration
	for _, entry := range *e {
		switch entry.id[0].(type) {
		case pDeclarationHost:
			id := entry.id[0].(pDeclarationHost)
			if strings.EqualFold(id.name, host) {
				result = append(result, entry)
			}
		}
	}
	if len(result) == 0 {
		return configDeclaration{}, fmt.Errorf("No host declarations containing %s found", host)
	}
	if len(result) > 1 {
		return configDeclaration{}, fmt.Errorf("More than 1 host declaration found : %v", result)
	}
	return result[0], nil
}

/*** Network Map */
type NetworkMap []map[string]string

type NetworkNameMapper interface {
	NameIntoDevices(string) ([]string, error)
	DeviceIntoName(string) (string, error)
}

func ReadNetworkMap(fd *os.File) (NetworkMap, error) {
	fromfile := consumeFile(fd)
	uncommented := uncomment(fromfile)
	tokenized := tokenizeNetworkMapConfig(uncommented)

	// Now that we've tokenized the network map, we just need to parse it into
	// a list of maps.
	result, err := parseNetworkMapConfig(tokenized)
	if err != nil {
		return nil, err
	}

	return result, nil
}

func (e NetworkMap) NameIntoDevices(name string) ([]string, error) {
	var devices []string

	for _, val := range e {
		if strings.EqualFold(val["name"], name) {
			devices = append(devices, val["device"])
		}
	}

	if len(devices) > 0 {
		return devices, nil
	}

	return make([]string, 0), fmt.Errorf("Network name not found : %v", name)
}
func (e NetworkMap) DeviceIntoName(device string) (string, error) {
	for _, val := range e {
		if strings.EqualFold(val["device"], device) {
			return val["name"], nil
		}
	}

	return "", fmt.Errorf("Device name not found : %v", device)
}
func (e *NetworkMap) repr() string {
	var result []string

	for idx, val := range *e {
		result = append(result, fmt.Sprintf("network%d.name = \"%s\"", idx, val["name"]))
		result = append(result, fmt.Sprintf("network%d.device = \"%s\"", idx, val["device"]))
	}

	return strings.Join(result, "\n")
}

/*** parser for VMware Fusion's networking file */
func tokenizeNetworkingConfig(in chan byte) chan string {
	var state string
	var repeat_newline bool

	out := make(chan string)
	go func(out chan string) {
		for {
			by, ok := <-in
			if !ok {
				break
			}

			switch by {
			case '\t':
				fallthrough
			case ' ':
				// If we receive whitespace, then this is just a write to our
				// state and then we reset.
				if len(state) == 0 {
					continue
				}
				out <- state
				state = ""

			case '\r':
				// If windows has tampered with our newlines, then we normalize
				// things by converting its value from 0x0d to 0x0a.
				fallthrough
			case '\n':
				// Newlines can repeat, so this case is responsible for writing
				// to the chan, and consolidating multiple newlines into a single.
				if repeat_newline {
					continue
				}
				if len(state) > 0 {
					out <- state
				}
				out <- "\n"
				state = ""
				repeat_newline = true
				continue

			default:
				// Anything other bytes just need to be aggregated into a string.
				state += string(by)
			}
			repeat_newline = false
		}

		// If there's anything left in our state after the chan has been closed,
		// then the input just wasn't terminated properly. It's still valid, so
		// write we have to the channel.
		if len(state) > 0 {
			out <- state
		}
		close(out)
	}(out)
	return out
}

func splitNetworkingConfig(in chan string) chan []string {
	out := make(chan []string)

	// This goroutine is simple in that it takes a chan of tokens, and splits
	// them across the newlines.

	go func(out chan []string) {
		row := make([]string, 0)
		for {
			tk, ok := <-in
			if !ok {
				break
			}

			if tk == "\n" {
				// If we received a newline token, then we need to write our
				// aggregated list of tokens and reset our "splitting" state.

				if len(row) > 0 {
					out <- row
				}

				row = make([]string, 0)

			} else {
				// Anything else just requires us to aggregate the token into
				// our list.
				row = append(row, tk)
			}
		}

		if len(row) > 0 {
			out <- row
		}
		close(out)
	}(out)
	return out
}

/// All token types in networking file.
// VERSION token
type networkingVERSION struct {
	value string
}

func networkingReadVersion(row []string) (*networkingVERSION, error) {
	if len(row) != 1 {
		return nil, fmt.Errorf("Unexpected format for VERSION entry : %v", row)
	}
	res := &networkingVERSION{value: row[0]}
	if !res.Valid() {
		return nil, fmt.Errorf("Unexpected format for VERSION entry : %v", row)
	}
	return res, nil
}

func (s networkingVERSION) Repr() string {
	if !s.Valid() {
		return fmt.Sprintf("VERSION{INVALID=\"%v\"}", s.value)
	}
	return fmt.Sprintf("VERSION{%f}", s.Number())
}

func (s networkingVERSION) Valid() bool {
	tokens := strings.SplitN(s.value, "=", 2)
	if len(tokens) != 2 || tokens[0] != "VERSION" {
		return false
	}

	tokens = strings.Split(tokens[1], ",")
	if len(tokens) != 2 {
		return false
	}

	for _, t := range tokens {
		_, err := strconv.ParseUint(t, 10, 64)
		if err != nil {
			return false
		}
	}
	return true
}

func (s networkingVERSION) Number() float64 {
	var result float64
	tokens := strings.SplitN(s.value, "=", 2)
	tokens = strings.Split(tokens[1], ",")

	integer, err := strconv.ParseUint(tokens[0], 10, 64)
	if err != nil {
		integer = 0
	}
	result = float64(integer)

	mantissa, err := strconv.ParseUint(tokens[1], 10, 64)
	if err != nil {
		return result
	}
	denomination := math.Pow(10.0, float64(len(tokens[1])))
	return result + (float64(mantissa) / denomination)
}

// VNET_X token
type networkingVNET struct {
	value string
}

func (s networkingVNET) Valid() bool {
	if strings.ToUpper(s.value) != s.value {
		return false
	}
	tokens := strings.SplitN(s.value, "_", 3)
	if len(tokens) != 3 || tokens[0] != "VNET" {
		return false
	}
	_, err := strconv.ParseUint(tokens[1], 10, 64)
	if err != nil {
		return false
	}
	return true
}

func (s networkingVNET) Number() int {
	tokens := strings.SplitN(s.value, "_", 3)
	res, err := strconv.Atoi(tokens[1])
	if err != nil {
		return ^int(0)
	}
	return res
}

func (s networkingVNET) Option() string {
	tokens := strings.SplitN(s.value, "_", 3)
	if len(tokens) == 3 {
		return tokens[2]
	}
	return ""
}

func (s networkingVNET) Repr() string {
	if !s.Valid() {
		tokens := strings.SplitN(s.value, "_", 3)
		return fmt.Sprintf("VNET{INVALID=%v}", tokens)
	}
	return fmt.Sprintf("VNET{%d} %s", s.Number(), s.Option())
}

// Interface name
type networkingInterface struct {
	name string
}

func (s networkingInterface) Interface() (*net.Interface, error) {
	return net.InterfaceByName(s.name)
}

// networking command entry types
type networkingCommandEntry_answer struct {
	vnet  networkingVNET
	value string
}
type networkingCommandEntry_remove_answer struct {
	vnet networkingVNET
}
type networkingCommandEntry_add_nat_portfwd struct {
	vnet        int
	protocol    string
	port        int
	target_host net.IP
	target_port int
}
type networkingCommandEntry_remove_nat_portfwd struct {
	vnet     int
	protocol string
	port     int
}
type networkingCommandEntry_add_dhcp_mac_to_ip struct {
	vnet int
	mac  net.HardwareAddr
	ip   net.IP
}
type networkingCommandEntry_remove_dhcp_mac_to_ip struct {
	vnet int
	mac  net.HardwareAddr
}
type networkingCommandEntry_add_bridge_mapping struct {
	intf networkingInterface
	vnet int
}
type networkingCommandEntry_remove_bridge_mapping struct {
	intf networkingInterface
}
type networkingCommandEntry_add_nat_prefix struct {
	vnet   int
	prefix int
}
type networkingCommandEntry_remove_nat_prefix struct {
	vnet   int
	prefix int
}

type networkingCommandEntry struct {
	entry                 interface{}
	answer                *networkingCommandEntry_answer
	remove_answer         *networkingCommandEntry_remove_answer
	add_nat_portfwd       *networkingCommandEntry_add_nat_portfwd
	remove_nat_portfwd    *networkingCommandEntry_remove_nat_portfwd
	add_dhcp_mac_to_ip    *networkingCommandEntry_add_dhcp_mac_to_ip
	remove_dhcp_mac_to_ip *networkingCommandEntry_remove_dhcp_mac_to_ip
	add_bridge_mapping    *networkingCommandEntry_add_bridge_mapping
	remove_bridge_mapping *networkingCommandEntry_remove_bridge_mapping
	add_nat_prefix        *networkingCommandEntry_add_nat_prefix
	remove_nat_prefix     *networkingCommandEntry_remove_nat_prefix
}

func (e networkingCommandEntry) Name() string {
	switch e.entry.(type) {
	case networkingCommandEntry_answer:
		return "answer"
	case networkingCommandEntry_remove_answer:
		return "remove_answer"
	case networkingCommandEntry_add_nat_portfwd:
		return "add_nat_portfwd"
	case networkingCommandEntry_remove_nat_portfwd:
		return "remove_nat_portfwd"
	case networkingCommandEntry_add_dhcp_mac_to_ip:
		return "add_dhcp_mac_to_ip"
	case networkingCommandEntry_remove_dhcp_mac_to_ip:
		return "remove_dhcp_mac_to_ip"
	case networkingCommandEntry_add_bridge_mapping:
		return "add_bridge_mapping"
	case networkingCommandEntry_remove_bridge_mapping:
		return "remove_bridge_mapping"
	case networkingCommandEntry_add_nat_prefix:
		return "add_nat_prefix"
	case networkingCommandEntry_remove_nat_prefix:
		return "remove_nat_prefix"
	}
	return ""
}

func (e networkingCommandEntry) Entry() reflect.Value {
	this := reflect.ValueOf(e)
	switch e.entry.(type) {
	case networkingCommandEntry_answer:
		return reflect.Indirect(this.FieldByName("answer"))
	case networkingCommandEntry_remove_answer:
		return reflect.Indirect(this.FieldByName("remove_answer"))
	case networkingCommandEntry_add_nat_portfwd:
		return reflect.Indirect(this.FieldByName("add_nat_portfwd"))
	case networkingCommandEntry_remove_nat_portfwd:
		return reflect.Indirect(this.FieldByName("remove_nat_portfwd"))
	case networkingCommandEntry_add_dhcp_mac_to_ip:
		return reflect.Indirect(this.FieldByName("add_dhcp_mac_to_ip"))
	case networkingCommandEntry_remove_dhcp_mac_to_ip:
		return reflect.Indirect(this.FieldByName("remove_dhcp_mac_to_ip"))
	case networkingCommandEntry_add_bridge_mapping:
		return reflect.Indirect(this.FieldByName("add_bridge_mapping"))
	case networkingCommandEntry_remove_bridge_mapping:
		return reflect.Indirect(this.FieldByName("remove_bridge_mapping"))
	case networkingCommandEntry_add_nat_prefix:
		return reflect.Indirect(this.FieldByName("add_nat_prefix"))
	case networkingCommandEntry_remove_nat_prefix:
		return reflect.Indirect(this.FieldByName("remove_nat_prefix"))
	}
	return reflect.Value{}
}

func (e networkingCommandEntry) Repr() string {
	result := make(map[string]interface{})

	entryN, entry := e.Name(), e.Entry()
	entryT := entry.Type()
	for i := 0; i < entry.NumField(); i++ {
		fld, fldT := entry.Field(i), entryT.Field(i)
		result[fldT.Name] = fld
	}
	return fmt.Sprintf("%s -> %v", entryN, result)
}

// networking command entry parsers
func parseNetworkingCommand_answer(row []string) (*networkingCommandEntry, error) {
	if len(row) != 2 {
		return nil, fmt.Errorf("Expected %d arguments. Received only %d.", 2, len(row))
	}

	vnet := networkingVNET{value: row[0]}
	if !vnet.Valid() {
		return nil, fmt.Errorf("Invalid format for VNET.")
	}

	result := networkingCommandEntry_answer{vnet: vnet, value: row[1]}
	return &networkingCommandEntry{entry: result, answer: &result}, nil
}
func parseNetworkingCommand_remove_answer(row []string) (*networkingCommandEntry, error) {
	if len(row) != 1 {
		return nil, fmt.Errorf("Expected %d argument. Received %d.", 1, len(row))
	}
	vnet := networkingVNET{value: row[0]}
	if !vnet.Valid() {
		return nil, fmt.Errorf("Invalid format for VNET.")
	}

	result := networkingCommandEntry_remove_answer{vnet: vnet}
	return &networkingCommandEntry{entry: result, remove_answer: &result}, nil
}
func parseNetworkingCommand_add_nat_portfwd(row []string) (*networkingCommandEntry, error) {
	if len(row) != 5 {
		return nil, fmt.Errorf("Expected %d arguments. Received only %d.", 5, len(row))
	}

	vnet, err := strconv.Atoi(row[0])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse first argument as an integer. : %v", row[0])
	}

	protocol := strings.ToLower(row[1])
	if !(protocol == "tcp" || protocol == "udp") {
		return nil, fmt.Errorf("Expected \"tcp\" or \"udp\" for second argument. : %v", row[1])
	}

	sport, err := strconv.Atoi(row[2])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse third argument as an integer. : %v", row[2])
	}

	dest := net.ParseIP(row[3])
	if dest == nil {
		return nil, fmt.Errorf("Unable to parse fourth argument as an IPv4 address. : %v", row[2])
	}

	dport, err := strconv.Atoi(row[4])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse fifth argument as an integer. : %v", row[4])
	}

	result := networkingCommandEntry_add_nat_portfwd{vnet: vnet - 1, protocol: protocol, port: sport, target_host: dest, target_port: dport}
	return &networkingCommandEntry{entry: result, add_nat_portfwd: &result}, nil
}
func parseNetworkingCommand_remove_nat_portfwd(row []string) (*networkingCommandEntry, error) {
	if len(row) != 3 {
		return nil, fmt.Errorf("Expected %d arguments. Received only %d.", 3, len(row))
	}

	vnet, err := strconv.Atoi(row[0])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse first argument as an integer. : %v", row[0])
	}

	protocol := strings.ToLower(row[1])
	if !(protocol == "tcp" || protocol == "udp") {
		return nil, fmt.Errorf("Expected \"tcp\" or \"udp\" for second argument. : %v", row[1])
	}

	sport, err := strconv.Atoi(row[2])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse third argument as an integer. : %v", row[2])
	}

	result := networkingCommandEntry_remove_nat_portfwd{vnet: vnet - 1, protocol: protocol, port: sport}
	return &networkingCommandEntry{entry: result, remove_nat_portfwd: &result}, nil
}
func parseNetworkingCommand_add_dhcp_mac_to_ip(row []string) (*networkingCommandEntry, error) {
	if len(row) != 3 {
		return nil, fmt.Errorf("Expected %d arguments. Received only %d.", 3, len(row))
	}

	vnet, err := strconv.Atoi(row[0])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse first argument as an integer. : %v", row[0])
	}

	mac, err := net.ParseMAC(row[1])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse second argument as hwaddr. : %v", row[1])
	}

	ip := net.ParseIP(row[2])
	if ip == nil {
		return nil, fmt.Errorf("Unable to parse third argument as ipv4. : %v", row[2])
	}

	result := networkingCommandEntry_add_dhcp_mac_to_ip{vnet: vnet - 1, mac: mac, ip: ip}
	return &networkingCommandEntry{entry: result, add_dhcp_mac_to_ip: &result}, nil
}
func parseNetworkingCommand_remove_dhcp_mac_to_ip(row []string) (*networkingCommandEntry, error) {
	if len(row) != 2 {
		return nil, fmt.Errorf("Expected %d arguments. Received only %d.", 2, len(row))
	}

	vnet, err := strconv.Atoi(row[0])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse first argument as an integer. : %v", row[0])
	}

	mac, err := net.ParseMAC(row[1])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse second argument as hwaddr. : %v", row[1])
	}

	result := networkingCommandEntry_remove_dhcp_mac_to_ip{vnet: vnet - 1, mac: mac}
	return &networkingCommandEntry{entry: result, remove_dhcp_mac_to_ip: &result}, nil
}
func parseNetworkingCommand_add_bridge_mapping(row []string) (*networkingCommandEntry, error) {
	if len(row) != 2 {
		return nil, fmt.Errorf("Expected %d arguments. Received only %d.", 2, len(row))
	}
	intf := networkingInterface{name: row[0]}

	vnet, err := strconv.Atoi(row[1])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse second argument as an integer. : %v", row[2])
	}

	result := networkingCommandEntry_add_bridge_mapping{intf: intf, vnet: vnet - 1}
	return &networkingCommandEntry{entry: result, add_bridge_mapping: &result}, nil
}
func parseNetworkingCommand_remove_bridge_mapping(row []string) (*networkingCommandEntry, error) {
	if len(row) != 1 {
		return nil, fmt.Errorf("Expected %d argument. Received %d.", 1, len(row))
	}
	intf := networkingInterface{name: row[0]}
	/*
		number, err := strconv.Atoi(row[0])
		if err != nil {
			return nil, fmt.Errorf("Unable to parse first argument as an integer. : %v", row[0])
		}
	*/
	result := networkingCommandEntry_remove_bridge_mapping{intf: intf}
	return &networkingCommandEntry{entry: result, remove_bridge_mapping: &result}, nil
}
func parseNetworkingCommand_add_nat_prefix(row []string) (*networkingCommandEntry, error) {
	if len(row) != 2 {
		return nil, fmt.Errorf("Expected %d arguments. Received only %d.", 2, len(row))
	}

	vnet, err := strconv.Atoi(row[0])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse first argument as an integer. : %v", row[0])
	}

	if !strings.HasPrefix(row[1], "/") {
		return nil, fmt.Errorf("Expected second argument to begin with \"/\". : %v", row[1])
	}

	prefix, err := strconv.Atoi(row[1][1:])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse prefix out of second argument. : %v", row[1])
	}

	result := networkingCommandEntry_add_nat_prefix{vnet: vnet - 1, prefix: prefix}
	return &networkingCommandEntry{entry: result, add_nat_prefix: &result}, nil
}
func parseNetworkingCommand_remove_nat_prefix(row []string) (*networkingCommandEntry, error) {
	if len(row) != 2 {
		return nil, fmt.Errorf("Expected %d arguments. Received only %d.", 2, len(row))
	}

	vnet, err := strconv.Atoi(row[0])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse first argument as an integer. : %v", row[0])
	}

	if !strings.HasPrefix(row[1], "/") {
		return nil, fmt.Errorf("Expected second argument to begin with \"/\". : %v", row[1])
	}

	prefix, err := strconv.Atoi(row[1][1:])
	if err != nil {
		return nil, fmt.Errorf("Unable to parse prefix out of second argument. : %v", row[1])
	}

	result := networkingCommandEntry_remove_nat_prefix{vnet: vnet - 1, prefix: prefix}
	return &networkingCommandEntry{entry: result, remove_nat_prefix: &result}, nil
}

type networkingCommandParser struct {
	command  string
	callback func([]string) (*networkingCommandEntry, error)
}

var NetworkingCommandParsers = []networkingCommandParser{
	/* DictRecordParseFunct */ {command: "answer", callback: parseNetworkingCommand_answer},
	/* DictRecordParseFunct */ {command: "remove_answer", callback: parseNetworkingCommand_remove_answer},
	/* NatFwdRecordParseFunct */ {command: "add_nat_portfwd", callback: parseNetworkingCommand_add_nat_portfwd},
	/* NatFwdRecordParseFunct */ {command: "remove_nat_portfwd", callback: parseNetworkingCommand_remove_nat_portfwd},
	/* DhcpMacRecordParseFunct */ {command: "add_dhcp_mac_to_ip", callback: parseNetworkingCommand_add_dhcp_mac_to_ip},
	/* DhcpMacRecordParseFunct */ {command: "remove_dhcp_mac_to_ip", callback: parseNetworkingCommand_remove_dhcp_mac_to_ip},
	/* BridgeMappingRecordParseFunct */ {command: "add_bridge_mapping", callback: parseNetworkingCommand_add_bridge_mapping},
	/* BridgeMappingRecordParseFunct */ {command: "remove_bridge_mapping", callback: parseNetworkingCommand_remove_bridge_mapping},
	/* NatPrefixRecordParseFunct */ {command: "add_nat_prefix", callback: parseNetworkingCommand_add_nat_prefix},
	/* NatPrefixRecordParseFunct */ {command: "remove_nat_prefix", callback: parseNetworkingCommand_remove_nat_prefix},
}

func NetworkingParserByCommand(command string) *func([]string) (*networkingCommandEntry, error) {
	for _, p := range NetworkingCommandParsers {
		if p.command == command {
			return &p.callback
		}
	}
	return nil
}

func parseNetworkingConfig(rows chan []string) chan networkingCommandEntry {
	out := make(chan networkingCommandEntry)

	go func(in chan []string, out chan networkingCommandEntry) {
		for {
			row, ok := <-in
			if !ok {
				break
			}

			if len(row) >= 1 {
				parser := NetworkingParserByCommand(row[0])
				if parser == nil {
					log.Printf("Invalid command : %v", row)
					continue
				}

				callback := *parser

				entry, err := callback(row[1:])
				if err != nil {
					log.Printf("Unable to parse command : %v %v", err, row)
					continue
				}
				out <- *entry
			}
		}
		close(out)
	}(rows, out)
	return out
}

type NetworkingConfig struct {
	answer         map[int]map[string]string
	nat_portfwd    map[int]map[string]string
	dhcp_mac_to_ip map[int]map[string]net.IP
	//bridge_mapping map[net.Interface]uint64	// XXX: we don't need the actual interface for anything but informing the user.
	bridge_mapping map[string]int
	nat_prefix     map[int][]int
}

func (c NetworkingConfig) repr() string {
	return fmt.Sprintf("answer -> %v\nnat_portfwd -> %v\ndhcp_mac_to_ip -> %v\nbridge_mapping -> %v\nnat_prefix -> %v", c.answer, c.nat_portfwd, c.dhcp_mac_to_ip, c.bridge_mapping, c.nat_prefix)
}

func flattenNetworkingConfig(in chan networkingCommandEntry) NetworkingConfig {
	var result NetworkingConfig
	var vmnet int

	result.answer = make(map[int]map[string]string)
	result.nat_portfwd = make(map[int]map[string]string)
	result.dhcp_mac_to_ip = make(map[int]map[string]net.IP)
	result.bridge_mapping = make(map[string]int)
	result.nat_prefix = make(map[int][]int)

	for {
		e, ok := <-in
		if !ok {
			break
		}

		switch e.entry.(type) {
		case networkingCommandEntry_answer:
			vnet := e.answer.vnet
			answers, exists := result.answer[vnet.Number()]
			if !exists {
				answers = make(map[string]string)
				result.answer[vnet.Number()] = answers
			}
			answers[vnet.Option()] = e.answer.value

		case networkingCommandEntry_remove_answer:
			vnet := e.remove_answer.vnet
			answers, exists := result.answer[vnet.Number()]
			if exists {
				delete(answers, vnet.Option())
			} else {
				log.Printf("Unable to remove answer %s as specified by `remove_answer`.\n", vnet.Repr())
			}

		case networkingCommandEntry_add_nat_portfwd:
			vmnet = e.add_nat_portfwd.vnet
			protoport := fmt.Sprintf("%s/%d", e.add_nat_portfwd.protocol, e.add_nat_portfwd.port)
			target := fmt.Sprintf("%s:%d", e.add_nat_portfwd.target_host, e.add_nat_portfwd.target_port)
			portfwds, exists := result.nat_portfwd[vmnet]
			if !exists {
				portfwds = make(map[string]string)
				result.nat_portfwd[vmnet] = portfwds
			}
			portfwds[protoport] = target

		case networkingCommandEntry_remove_nat_portfwd:
			vmnet = e.remove_nat_portfwd.vnet
			protoport := fmt.Sprintf("%s/%d", e.remove_nat_portfwd.protocol, e.remove_nat_portfwd.port)
			portfwds, exists := result.nat_portfwd[vmnet]
			if exists {
				delete(portfwds, protoport)
			} else {
				log.Printf("Unable to remove nat port-forward %s from interface %s%d as requested by `remove_nat_portfwd`.\n", protoport, NetworkingInterfacePrefix, vmnet)
			}

		case networkingCommandEntry_add_dhcp_mac_to_ip:
			vmnet = e.add_dhcp_mac_to_ip.vnet
			dhcpmacs, exists := result.dhcp_mac_to_ip[vmnet]
			if !exists {
				dhcpmacs = make(map[string]net.IP)
				result.dhcp_mac_to_ip[vmnet] = dhcpmacs
			}
			dhcpmacs[e.add_dhcp_mac_to_ip.mac.String()] = e.add_dhcp_mac_to_ip.ip

		case networkingCommandEntry_remove_dhcp_mac_to_ip:
			vmnet = e.remove_dhcp_mac_to_ip.vnet
			dhcpmacs, exists := result.dhcp_mac_to_ip[vmnet]
			if exists {
				delete(dhcpmacs, e.remove_dhcp_mac_to_ip.mac.String())
			} else {
				log.Printf("Unable to remove dhcp_mac_to_ip entry %v from interface %s%d as specified by `remove_dhcp_mac_to_ip`.\n", e.remove_dhcp_mac_to_ip, NetworkingInterfacePrefix, vmnet)
			}

		case networkingCommandEntry_add_bridge_mapping:
			intf := e.add_bridge_mapping.intf
			if _, err := intf.Interface(); err != nil {
				log.Printf("Interface \"%s\" as specified by `add_bridge_mapping` was not found on the current platform. This is a non-critical error. Ignoring.", intf.name)
			}
			result.bridge_mapping[intf.name] = e.add_bridge_mapping.vnet

		case networkingCommandEntry_remove_bridge_mapping:
			intf := e.remove_bridge_mapping.intf
			if _, err := intf.Interface(); err != nil {
				log.Printf("Interface \"%s\" as specified by `remove_bridge_mapping` was not found on the current platform. This is a non-critical error. Ignoring.", intf.name)
			}
			delete(result.bridge_mapping, intf.name)

		case networkingCommandEntry_add_nat_prefix:
			vmnet = e.add_nat_prefix.vnet
			_, exists := result.nat_prefix[vmnet]
			if exists {
				result.nat_prefix[vmnet] = append(result.nat_prefix[vmnet], e.add_nat_prefix.prefix)
			} else {
				result.nat_prefix[vmnet] = []int{e.add_nat_prefix.prefix}
			}

		case networkingCommandEntry_remove_nat_prefix:
			vmnet = e.remove_nat_prefix.vnet
			prefixes, exists := result.nat_prefix[vmnet]
			if exists {
				for index := 0; index < len(prefixes); index++ {
					if prefixes[index] == e.remove_nat_prefix.prefix {
						result.nat_prefix[vmnet] = append(prefixes[:index], prefixes[index+1:]...)
						break
					}
				}

			} else {
				log.Printf("Unable to remove nat prefix /%d from interface %s%d as specified by `remove_nat_prefix`.\n", e.remove_nat_prefix.prefix, NetworkingInterfacePrefix, vmnet)
			}
		}
	}
	return result
}

// Constructor for networking file
func ReadNetworkingConfig(fd *os.File) (NetworkingConfig, error) {

	// start piecing together all of the differents parts of the file and split
	// it into its individual rows.
	fromfile := consumeFile(fd)
	tokenized := tokenizeNetworkingConfig(fromfile)
	rows := splitNetworkingConfig(tokenized)

	// consume the version _first_. this is important because if the version is
	// wrong, then there's likely tokens that we won't know how to interpret.
	parsed_version, err := networkingReadVersion(<-rows)
	if err != nil {
		return NetworkingConfig{}, err
	}

	// verify that it's 1.0 since that's all we support for now.
	if version := parsed_version.Number(); version != 1.0 {
		return NetworkingConfig{}, fmt.Errorf("Expected version %f of networking file. Received version %f.", 1.0, version)
	}

	// now that our version has been confirmed, we can proceed to parse the
	// rest of the file and parseNetworkingConfig is free to consume rows as
	// much as it wants to.
	entries := parseNetworkingConfig(rows)

	// convert what we've parsed into a configuration that's easy to interpret
	return flattenNetworkingConfig(entries), nil
}

// netmapper interface
type NetworkingType int

const (
	NetworkingType_HOSTONLY = iota + 1
	NetworkingType_NAT
	NetworkingType_BRIDGED
)

func networkingConfig_InterfaceTypes(config NetworkingConfig) map[int]NetworkingType {
	result := make(map[int]NetworkingType)

	// defaults
	result[0] = NetworkingType_BRIDGED
	result[1] = NetworkingType_HOSTONLY
	result[8] = NetworkingType_NAT

	// walk through config collecting bridged interfaces
	for _, vmnet := range config.bridge_mapping {
		result[vmnet] = NetworkingType_BRIDGED
	}

	// walk through answers finding out which ones are nat versus hostonly
	for vmnet, table := range config.answer {

		// everything should be defined as a virtual adapter...
		if table["VIRTUAL_ADAPTER"] == "yes" {

			// validate that the VNET entry contains everything we expect it to
			_, subnetQ := table["HOSTONLY_SUBNET"]
			_, netmaskQ := table["HOSTONLY_NETMASK"]
			if !(subnetQ && netmaskQ) {
				log.Printf("Interface %s%d is missing some expected keys (HOSTONLY_SUBNET, HOSTONLY_NETMASK). This is non-critical. Ignoring..", NetworkingInterfacePrefix, vmnet)
			}

			// distinguish between nat or hostonly
			if table["NAT"] == "yes" {
				result[vmnet] = NetworkingType_NAT

			} else {
				result[vmnet] = NetworkingType_HOSTONLY
			}

		} else {
			// if it's not a virtual_adapter, then it must be an alias (really a bridge).
			result[vmnet] = NetworkingType_BRIDGED
		}
	}
	return result
}

func networkingConfig_NamesToVmnet(config NetworkingConfig) map[NetworkingType][]int {
	types := networkingConfig_InterfaceTypes(config)

	// now sort the keys
	var keys []int
	for vmnet := range types {
		keys = append(keys, vmnet)
	}
	sort.Ints(keys)

	// build result dictionary
	result := make(map[NetworkingType][]int)

	for i := 0; i < len(keys); i++ {
		t := types[keys[i]]
		result[t] = append(result[t], keys[i])
	}
	return result
}

const NetworkingInterfacePrefix = "vmnet"

func (e NetworkingConfig) NameIntoDevices(name string) ([]string, error) {
	netmapper := networkingConfig_NamesToVmnet(e)
	name = strings.ToLower(name)

	var vmnets []string
	var networkingType NetworkingType
	if name == "hostonly" && len(netmapper[NetworkingType_HOSTONLY]) > 0 {
		networkingType = NetworkingType_HOSTONLY

	} else if name == "nat" && len(netmapper[NetworkingType_NAT]) > 0 {
		networkingType = NetworkingType_NAT

	} else if name == "bridged" && len(netmapper[NetworkingType_BRIDGED]) > 0 {
		networkingType = NetworkingType_BRIDGED

	} else {
		return make([]string, 0), fmt.Errorf("Network name not found: %v", name)
	}

	for i := 0; i < len(netmapper[networkingType]); i++ {
		vmnets = append(vmnets, fmt.Sprintf("%s%d", NetworkingInterfacePrefix, netmapper[networkingType][i]))
	}
	return vmnets, nil
}

func (e NetworkingConfig) DeviceIntoName(device string) (string, error) {
	types := networkingConfig_InterfaceTypes(e)

	lowerdevice := strings.ToLower(device)
	if !strings.HasPrefix(lowerdevice, NetworkingInterfacePrefix) {
		return device, nil
	}
	vmnet, err := strconv.Atoi(lowerdevice[len(NetworkingInterfacePrefix):])
	if err != nil {
		return "", err
	}
	network := types[vmnet]
	switch network {
	case NetworkingType_HOSTONLY:
		return "hostonly", nil

	case NetworkingType_NAT:
		return "nat", nil

	case NetworkingType_BRIDGED:
		return "bridged", nil
	}
	return "", fmt.Errorf("Unable to determine network type for device %s%d.", NetworkingInterfacePrefix, vmnet)
}

/** generic async file reader */
func consumeFile(fd *os.File) chan byte {
	fromFile := make(chan byte)
	go func() {
		b := make([]byte, 1)
		for {
			_, err := fd.Read(b)
			if err != nil {
				// In case of any error we must stop
				// ErrClosed may appear since file is closed and this goroutine still left running
				break
			}
			fromFile <- b[0]
		}
		close(fromFile)
	}()
	return fromFile
}

/** Consume a byte channel until a terminal byte is reached, and write each list of bytes to a channel */
func consumeUntilSentinel(sentinel byte, in chan byte) (result []byte, ok bool) {

	// This is a simple utility that will consume from a channel until a sentinel
	// byte has been reached. Consumed data is returned in `result, and if
	// there's no more data to read, then `ok` will be false.
	for ok = true; ; {
		if by, success := <-in; !success {
			ok = false
			break

		} else if by == sentinel {
			break

		} else {
			result = append(result, by)

		}
	}
	return
}

/** Simple utility to ignore chars when consuming a channel */
func filterOutCharacters(ignore []byte, in chan byte) chan byte {
	out := make(chan byte)

	go func(ignore_s string) {
		for {
			if by, ok := <-in; !ok {
				break

			} else if !strings.ContainsAny(ignore_s, string(by)) {
				out <- by
			}
		}
		close(out)
	}(string(ignore))

	return out
}

/**
This consumes bytes within a pair of some bytes, like parentheses, brackets, braces...

We start by reading bytes until we encounter openByte. These will be returned as
the first parameter. Then we can enter a goro and consume bytes until we get to
closeByte. At that point we're done, and we can exit.
**/
func consumeOpenClosePair(openByte, closeByte byte, in chan byte) ([]byte, chan byte) {
	result := make([]byte, 0)

	// Consume until we get to openByte. We'll return what we consumed because
	// it isn't actually relevant to what we're trying to accomplish.
	for by := range in {
		if by == openByte {
			break

		} else {
			result = append(result, by)
		}
	}

	// Now we can feed input to our goro and a consumer can see what's contained
	// between their requested pairs
	out := make(chan byte)
	go func(out chan byte) {
		by := openByte

		// We only made it here because we received an openByte, so let's make
		// sure we send it down the channel.
		out <- by

		// Now just spin in a loop shipping bytes down the channel until we hit
		// closeByte, or we're at the very end...whichever comes first.
		var ok bool
		for by != closeByte {
			by, ok = <-in
			if !ok {
				by = closeByte
			}

			out <- by
		}
		close(out)
	}(out)

	// Return what we consumed, and a channel that yields everything in between
	// the openByte and closeByte pair.
	return result, out
}

// Basic decoding of a dhcpd lease address
func decodeDhcpdLeaseBytes(input string) ([]byte, error) {
	processed := &bytes.Buffer{}

	// Split the string into pieces as we'll need to validate it.
	for _, item := range strings.Split(input, ":") {
		if len(item) != 2 {
			return []byte{}, fmt.Errorf("bytes are not well-formed (%v)", input)
		}
		processed.WriteString(item)
	}

	length := hex.DecodedLen(processed.Len())

	// Decode the processed data into the result...
	result := make([]byte, length)
	if n, err := hex.Decode(result, processed.Bytes()); err != nil {
		return []byte{}, err

		// Check that our decode length corresponds to what was intended
	} else if n != length {
		return []byte{}, fmt.Errorf("expected to decode %d bytes, got %d instead", length, n)
	}

	// ...and then return it.
	return result, nil
}

/*** Dhcp Leases */
type dhcpLeaseEntry struct {
	address                      string
	starts, ends                 time.Time
	starts_weekday, ends_weekday int
	ether, uid                   []byte
	extra                        []string
}

func readDhcpdLeaseEntry(in chan byte) (entry *dhcpLeaseEntry, err error) {

	// Build the regexes we'll use to legitimately parse each item
	ipLineRe := regexp.MustCompile(`lease\s+(.+?)\s*$`)
	startTimeLineRe := regexp.MustCompile(`starts\s+(\d+)\s+(.+?)\s*$`)
	endTimeLineRe := regexp.MustCompile(`ends\s+(\d+)\s+(.+?)\s*$`)
	macLineRe := regexp.MustCompile(`hardware\s+ethernet\s+(.+?)\s*$`)
	uidLineRe := regexp.MustCompile(`uid\s+(.+?)\s*$`)

	/// Read up to the lease item and validate that it actually matches
	lease, ch := consumeOpenClosePair('{', '}', in)

	// If we couldn't read the lease, then this item is busted and we're prolly
	// done reading the channel.
	if len(lease) == 0 {
		return nil, nil
	}

	matches := ipLineRe.FindStringSubmatch(string(lease))
	if matches == nil {
		res := strings.TrimSpace(string(lease))
		return &dhcpLeaseEntry{extra: []string{res}}, fmt.Errorf("Unable to parse lease entry (%#v)", string(lease))
	}

	if by, ok := <-ch; ok && by == '{' {
		// If we found a lease match and we're definitely beginning a lease
		// entry, then create our storage.
		entry = &dhcpLeaseEntry{address: matches[1]}

	} else if ok {
		// If we didn't see a begin brace, then this entry is mangled which
		// means that we should probably bail
		return &dhcpLeaseEntry{address: matches[1]}, fmt.Errorf("Missing parameters for lease entry %v", matches[1])

	} else if !ok {
		// If our channel is closed, so we bail "cleanly".
		return nil, nil
	}

	/// Now we can parse the inside of the block.
	for insideBraces := true; insideBraces; {
		item, ok := consumeUntilSentinel(';', ch)
		item_s := string(item)

		if !ok {
			insideBraces = false
		}

		// Parse out the start time
		matches = startTimeLineRe.FindStringSubmatch(item_s)
		if matches != nil {
			if entry.starts, err = time.Parse("2006/01/02 15:04:05", matches[2]); err != nil {
				log.Printf("Error trying to parse start time (%v) for entry %v", matches[2], entry.address)
			}
			if entry.starts_weekday, err = strconv.Atoi(matches[1]); err != nil {
				log.Printf("Error trying to parse start weekday (%v) for entry %v", matches[1], entry.address)
			}
			continue
		}

		// Parse out the end time
		matches = endTimeLineRe.FindStringSubmatch(item_s)
		if matches != nil {
			if entry.ends, err = time.Parse("2006/01/02 15:04:05", matches[2]); err != nil {
				log.Printf("Error trying to parse end time (%v) for entry %v", matches[2], entry.address)
			}
			if entry.ends_weekday, err = strconv.Atoi(matches[1]); err != nil {
				log.Printf("Error trying to parse end weekday (%v) for entry %v", matches[1], entry.address)
			}
			continue
		}

		// Parse out the hardware ethernet
		matches = macLineRe.FindStringSubmatch(item_s)
		if matches != nil {
			if entry.ether, err = decodeDhcpdLeaseBytes(matches[1]); err != nil {
				log.Printf("Error trying to parse hardware ethernet address (%v) for entry %v", matches[1], entry.address)
			}
			continue
		}

		// Parse out the uid
		matches = uidLineRe.FindStringSubmatch(item_s)
		if matches != nil {
			if entry.uid, err = decodeDhcpdLeaseBytes(matches[1]); err != nil {
				log.Printf("Error trying to parse uid (%v) for entry %v", matches[1], entry.address)
			}
			continue
		}

		// Check to see if we're terminating the brace, so we can skip
		// to the next iteration.
		if strings.HasSuffix(item_s, "}") {
			continue
		}

		// Just stash it for now because we have no idea what it is.
		entry.extra = append(entry.extra, strings.TrimSpace(item_s))
	}

	return entry, nil
}

func ReadDhcpdLeaseEntries(fd *os.File) ([]dhcpLeaseEntry, error) {
	fch := consumeFile(fd)
	uncommentedch := uncomment(fch)
	wch := filterOutCharacters([]byte{'\n', '\r', '\v'}, uncommentedch)

	result := make([]dhcpLeaseEntry, 0)
	errors := make([]error, 0)

	// Consume dhcpd lease entries from the channel until we just plain run out.
	for i := 0; ; i += 1 {
		if entry, err := readDhcpdLeaseEntry(wch); entry == nil {
			// If our entry is nil, then we've run out of input and finished
			// parsing the file to completion.
			break

		} else if err != nil {
			// If we received an error, then log it and keep track of it. This
			// way we can warn the user later which entries we had issues with.
			log.Printf("Error parsing dhcpd lease entry #%d: %s", 1+i, err)
			errors = append(errors, err)

		} else {
			// If we've parsed an entry successfully, then aggregate it to
			// our slice of results.
			result = append(result, *entry)
		}
	}

	// If we received any errors then include alongside our results.
	if len(errors) > 0 {
		return result, fmt.Errorf("Errors found while parsing dhcpd lease entries: %v", errors)
	}
	return result, nil
}

/*** Apple Dhcp Leases */

// Here is what an Apple DHCPD lease entry looks like:
// {
// 	ip_address=192.168.111.2
// 	hw_address=1,0:50:56:20:ac:33
// 	identifier=1,0:50:56:20:ac:33
// 	lease=0x5fd72edc
// 	name=vagrant-2019
// }

type appleDhcpLeaseEntry struct {
	ipAddress     string
	hwAddress, id []byte
	lease         string
	name          string
	extra         map[string]string
}

func readAppleDhcpdLeaseEntry(in chan byte) (entry *appleDhcpLeaseEntry, err error) {
	entry = &appleDhcpLeaseEntry{extra: map[string]string{}}
	mandatoryFieldCount := 0
	// Read up to the lease item and validate that it actually matches
	_, ch := consumeOpenClosePair('{', '}', in)
	for insideBraces := true; insideBraces; {
		item, ok := consumeUntilSentinel('\n', ch)
		item_s := strings.TrimSpace(string(item))

		if !ok {
			insideBraces = false
		}
		if strings.Contains(item_s, "{") || strings.Contains(item_s, "}") {
			continue
		}
		splittedLine := strings.Split(item_s, "=")
		var key, val string
		switch len(splittedLine) {
		case 0:
			// should never happens as Split always returns at least 1 item
			fallthrough
		case 1:
			log.Printf("Error parsing invalid line: `%s`", item_s)
			continue
		case 2:
			key = strings.TrimSpace(splittedLine[0])
			val = strings.TrimSpace(splittedLine[1])
		default:
			// There were more than one '=' on this line, we'll keep the part before the first '=' as the key and
			// the rest will be the value
			key = strings.TrimSpace(splittedLine[0])
			val = strings.TrimSpace(strings.Join(splittedLine[1:], "="))
		}
		switch key {
		case "ip_address":
			entry.ipAddress = val
			mandatoryFieldCount++
		case "identifier":
			fallthrough
		case "hw_address":
			if strings.Count(val, ",") != 1 {
				log.Printf("Error %s `%s` is not properly formatted for entry %s", key, val, entry.name)
				break
			}
			splittedVal := strings.Split(val, ",")
			mac := splittedVal[1]
			splittedMac := strings.Split(mac, ":")
			// Pad the retrieved hw address with '0' when necessary
			for idx := range splittedMac {
				if len(splittedMac[idx]) == 1 {
					splittedMac[idx] = "0" + splittedMac[idx]
				}
			}
			mac = strings.Join(splittedMac, ":")
			decodedLease, err := decodeDhcpdLeaseBytes(mac)
			if err != nil {
				log.Printf("Error trying to parse %s (%v) for entry %s - %v", key, val, entry.name, mac)
				break
			}
			if key == "identifier" {
				entry.id = decodedLease
			} else {
				entry.hwAddress = decodedLease
			}
			mandatoryFieldCount++
		case "lease":
			entry.lease = val
		case "name":
			entry.name = val
		default:
			// Just stash it for now because we have no idea what it is.
			entry.extra[key] = val
		}
	}
	// we have most likely parsed the whole file
	if mandatoryFieldCount == 0 {
		return nil, nil
	}
	// an entry is composed of 3 mandatory fields, we'll check that they all have been set during the parsing
	if mandatoryFieldCount < 3 {
		return entry, fmt.Errorf("Error entry `%v` is missing mandatory information", entry)
	}
	return entry, nil
}

func ReadAppleDhcpdLeaseEntries(fd *os.File) ([]appleDhcpLeaseEntry, error) {
	fch := consumeFile(fd)
	uncommentedch := uncomment(fch)
	wch := filterOutCharacters([]byte{'\r', '\v'}, uncommentedch)

	result := make([]appleDhcpLeaseEntry, 0)
	errors := make([]error, 0)

	// Consume apple dhcpd lease entries from the channel until we just plain run out.
	for i := 0; ; i++ {
		if entry, err := readAppleDhcpdLeaseEntry(wch); entry == nil {
			// If our entry is nil, then we've run out of input and finished
			// parsing the file to completion.
			break

		} else if err != nil {
			// If we received an error, then log it and keep track of it. This
			// way we can warn the user later which entries we had issues with.
			log.Printf("Error parsing apple dhcpd lease entry #%d: %s", 1+i, err)
			errors = append(errors, err)

		} else {
			// If we've parsed an entry successfully, then aggregate it to
			// our slice of results.
			result = append(result, *entry)
		}
	}

	// If we received any errors then include alongside our results.
	if len(errors) > 0 {
		return result, fmt.Errorf("Errors found while parsing apple dhcpd lease entries: %v", errors)
	}
	return result, nil
}