This file is indexed.

/usr/sbin/fanatic is in ubuntu-fan 0.9.0.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

   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
#!/bin/sh
#
# fanatic -- intialise a with a default fan setup.
#

if [ `id -u` -ne 0 ]; then
	exec sudo -- "$0" "$@"
fi

network_overlay_default='250.0.0.0/8'
##network_type='ipip'
##network_mtu=1480
network_type='vxlan'
network_mtu=1450
network_mode='compact'
#network_overlay_width='8'
network_underlay_width='16'

usage()
{
	echo "Usage: $0 $@" 1>&2
	exit 2
}
fail()
{
	echo "$0: $@" 1>&2
	exit 1
}
end()
{
	echo "$0: $@"
	exit 0
}
progress()
{
	[ "$interactive" -eq 0 ] && echo "$@"
}

detect_network()
{
       local a1="$1"

	[ "$a1" = "default" ] && a1=`ip route show 0.0.0.0/0 | awk '{print $5; exit}'`
       RET="$a1"

	a1="`ip -4 addr show dev "$a1" | grep inet | awk -F '[/ ]*' '{print $3; exit}'`"
	[ "$a1" = "" ] && fail "$a1: device address not found"

       RET2="$a1"
}

twiddle_fan_config()
{
	local cmd="$1"
	local tag="$2"
	local overlay="$3"
	local underlay="$4"

	local conf="/etc/network/fan"

	# First check we have a valid mapping from underlay to overlay, if not
	# we need to make one.
	mapping_present=$( awk -v overlay="$overlay" -v underlay="$underlay" '($1 == underlay && $2 == overlay) { print }' "$conf" 2>/dev/null | wc -l )
	mapping_mine=$( awk -v overlay="$overlay" -v underlay="$underlay" '($1 == underlay && $2 == overlay && $0 ~ /# fanatic configured/) { print }' "$conf" 2>/dev/null | wc -l )

	# We also want to know if there is a valid configuration for it.
	config_present=$( fanctl config show -u "$underlay" -o "$overlay" | wc -l )
	config_mine=$( fanctl config show -u "$underlay" -o "$overlay" | grep "comment='fanatic configured'" | wc -l )

	#echo "twiddle_fan_config cmd<$cmd> mapping_present<$mapping_present> mapping_mine<$mapping_mine> config_present<$config_present> config_mine<$config_mine>"

	case "$cmd" in
	add-check)
		if [ "$config_mine" != 0 ]; then
			return 1
		fi
		if [ "$config_present" != 0 ]; then
			return 1
		fi
		return 0
		;;
	add)
		if [ "$config_mine" != 0 ]; then
			fail "$conf: fan already configured unable to configure"
		fi
		if [ "$config_present" != 0 ]; then
			fail "$conf: fan configured manually unable to configure"
		fi

		if [ "$mapping_present" = 0 ]; then
			cat - <<EOL >>"$conf"
$underlay $overlay 		# fanatic configured
EOL
		fi

		fanctl config set -u "$underlay" -o "$overlay" --dhcp --enable --comment='fanatic configured'
		;;
	remove-check)
		if [ "$config_present" = 0 ]; then
			return 1
		fi
		if [ "$config_mine" = 0 ]; then
			return 1
		fi
		return 0
		;;
	remove)
		if [ "$config_present" = 0 ]; then
			fail "$conf: not configured"
		fi
		if [ "$config_mine" = 0 ]; then
			fail "$conf: configured manually not removing"
		fi

		fanctl config set -u "$underlay" -o "$overlay"

		# Remove the configuration if we added it.
		if [ "$mapping_mine" != 0 ]; then
			sed -i.bak -e "\@$underlay[ \t][ \t]*$overlay[ \t].*# fanatic configured@d" "$conf"
		fi
		;;
	esac
}

enable_fan()
{
	local device="$1"
	local overlay="$2"
	local underlay="$3"

	if twiddle_fan_config add-check "$device" "$overlay" "$underlay"; then
#		if [ "$interactive" -eq 1 ]; then
#			while :
#			do
#				read -p "Create fan network $overlay $underlay [Yn]: " confirm
#				case "$confirm" in
#				n*|N*)		fail "Abort" ;;
#				y*|Y*)		break ;;
#				'')		break ;;
#				esac
#			done
#		fi
		progress "configuring fan underlay:$undelay overlay:$overlay"
		twiddle_fan_config add "$device" "$overlay" "$underlay"
		fanctl up -o "$overlay" -u "$underlay" --auto || fail "unable to bring fan up, unable to configure"
	fi
}

disable_fan()
{
	local device="$1"
	local overlay="$2"
	local underlay="$3"

	local fan_bridge=$( fan_to_bridge "$overlay" "$underlay" "$network_mode" )

	if twiddle_lxd_config remove-check "$device" "$fan_bridge"; then
		fail "LXD still configured, unable to disable fan"
	fi
	if twiddle_docker_config remove-check "$device"; then
		fail "docker still configured, unable to disable fan"
	fi

	if twiddle_fan_config remove-check "$device" "$overlay" "$underlay"; then
#		if [ "$interactive" -eq 1 ]; then
#			while :
#			do
#				read -p "Remove fan network for $overlay $underlay [Yn]: " confirm
#				case "$confirm" in
#				n*|N*)		return ;;
#				y*|Y*)		break ;;
#				'')		break ;;
#				esac
#			done
#		fi
		progress "de-configuring fan underlay:$underlay overlay:$overlay"
		fanctl down -o "$overlay" -u "$underlay" || fail "unable to bring fan down, unable to deconfigure"
		twiddle_fan_config remove "$device" "$overlay" "$underlay"
	fi
}

twiddle_docker_config()
{
	local cmd="$1"
	local tag="$2"

	dconf="/etc/default/docker"

	configured=$( egrep "^DOCKER_OPTS=" "$dconf" 2>/dev/null | wc -l )
	myconfig=$( egrep "# autoconfig start -- fanatic $tag --" "$dconf" 2>/dev/null | wc -l )

	#echo "configured<$configured> myconfig<$myconfig>"

	case "$cmd" in
	add)
		local bridge="$3"
		local cidr="$4"
		local mtu="$5"

		if [ "$configured" != 0 ]; then
			fail "$0: $dconf: configured already, unable to configure"
		fi
		if [ "$configured" != 0 -a "$myconfig" != "$configured" ]; then
			fail "$0: $dconf: docker already configured for fan use"
		fi

		# Determine the docker version.
		local dockerver=$( docker -v | awk '{ print $3 }' | sed -e 's/,$//' )
		local dockerflags

		if dpkg --compare-versions "$dockerver" lt 1.10; then
			dockerflags=-d
		fi

		# Add the configuration
		sed -i.bak -e '/#DOCKER_OPTS=/a\
# autoconfig start -- fanatic '"$tag"' --\
DOCKER_OPTS="'"$dockerflags"' -b '"$bridge"' --mtu='"$mtu"' --iptables=false --fixed-cidr='"$cidr"'"\
# autoconfig end -- fanatic '"$tag"' --
' "$dconf"
		;;
	remove)
		if [ "$configured" = 0 ]; then
			fail "$0: $dconf: not configured"
		fi
		if [ "$myconfig" != "$configured" ]; then
			fail "$0: $dconf: configured manually not removing"
		fi

		# Add the configuration
		sed -i.bak -e '/# autoconfig start -- fanatic '"$tag"' --/,/# autoconfig end -- fanatic '"$tag"' --/d' "$dconf"
		;;
	remove-check)
		if [ "$configured" = 0 ]; then
			return 1
		fi
		if [ "$myconfig" != "$configured" ]; then
			return 1
		fi
		return 0
		;;
	esac
}

enable_docker()
{
	local device="$1"
	local overlay="$2"
	local underlay="$3"
	local underlay_addr="$4"

	local fan_bridge=$( fan_to_bridge "$overlay" "$underlay" "$network_mode" )
	local fan_cidr=$( fan_to_docker_cidr "$overlay" "$underlay_addr" "$network_mode" )

	# Do not attempt to configure docker if it is not installed.
	if [ ! -e /usr/bin/docker ]; then
		echo "Docker not installed, unable to configure"
		return
	fi

	# If we are alreday configured, skip.
	if twiddle_docker_config remove-check "$device"; then
		echo "Docker already configured"
		return
	fi

	if [ "$interactive" -eq 1 ]; then
		while :
		do
			read -p "Create docker networking for underlay:$underlay overlay:$overlay [Yn]: " confirm
			case "$confirm" in
			n*|N*)		return ;;
			y*|Y*)		break ;;
			'')		break ;;
			esac
		done
	fi
	progress "configuring docker for underlay:$underlay overlay:$overlay ($fan_bridge $fan_cidr)"
	twiddle_docker_config add "$device" "$fan_bridge" "$fan_cidr" "$network_mtu"

	# Restart docker for this to take effect.
	[ "$O_docker_restart" = '' ] && service docker restart
}

disable_docker()
{
	local device="$1"
	local overlay="$2"
	local underlay="$3"
	local underlay_addr="$4"

	if twiddle_docker_config remove-check "$device"; then
		if [ "$interactive" -eq 1 ]; then
			while :
			do
				read -p "Remove docker networking for underlay:$underlay overlay:$overlay [Yn]: " confirm
				case "$confirm" in
				n*|N*)		return ;;
				y*|Y*)		break ;;
				'')		break ;;
				esac
			done
		fi
		progress "de-configuring docker underlay:$underlay overlay:$overlay"
		twiddle_docker_config remove "$device"

		# Restart docker for this to take effect.
		[ "$O_docker_restart" = '' ] && service docker restart
	fi
}

twiddle_lxd_config()
{
	local cmd="$1"
	local tag="$2"
	local bridge="$3"

	local who
	configured=0
	who=$( lxc profile get "$bridge" 'user.profile_autoconfig' 2>/dev/null )
	[ "$?" -eq 0 ] && configured=1
	myconfig=0
	[ "$who" = 'fanatic' ] && myconfig=1
	
	#echo "configured<$configured> myconfig<$myconfig>"

	case "$cmd" in
	add)
		local mtu="$4"

		if [ "$configured" != 0 ]; then
			fail "$0: configured already, unable to configure"
		fi

		# Add the configuration
		lxc profile create "$bridge"
		lxc profile edit "$bridge" <<EOL
name: $bridge
config:
  user.profile_autoconfig: fanatic
devices:
  eth0:
    nictype: bridged
    parent: $bridge
    type: nic
    mtu: $mtu
EOL
		;;
	remove)
		if [ "$configured" = 0 ]; then
			fail "$0: $lconf: not configured"
		fi
		if [ "$myconfig" != "$configured" ]; then
			fail "$0: $lconf: configured manually not removing"
		fi

		lxc profile delete "$bridge"
		;;
	remove-check)
		if [ "$configured" = 0 ]; then
			return 1
		fi
		if [ "$myconfig" != "$configured" ]; then
			return 1
		fi
		return 0
		;;
	esac
}

enable_lxd()
{
	local device="$1"
	local overlay="$2"
	local underlay="$3"

	local fan_bridge=$( fan_to_bridge "$overlay" "$underlay" "$network_mode" )
	
	# Do not attempt to configure LXD if it is not installed.
	if [ ! -e /usr/bin/lxd ]; then
		echo "LXD not installed, unable to configure"
		return
	fi

	# If we are alreday configured, skip.
	if twiddle_lxd_config remove-check "$device" "$fan_bridge"; then
		echo "LXD already configured"
		return
	fi

	if [ "$interactive" -eq 1 ]; then
		while :
		do
			read -p "Create LXD networking for underlay:$underlay overlay:$overlay [Yn]: " confirm
			case "$confirm" in
			n*|N*)		return ;;
			y*|Y*)		break ;;
			'')		break ;;
			esac
		done
	fi
	progress "configuring LXD for underlay:$underlay overlay:$overlay ($fan_bridge)"
	twiddle_lxd_config add "$device" "$fan_bridge" "$network_mtu"
}

disable_lxd()
{
	local device="$1"
	local overlay="$2"
	local underlay="$3"

	local fan_bridge=$( fan_to_bridge "$overlay" "$underlay" "$network_mode" )

	if twiddle_lxd_config remove-check "$device" "$fan_bridge"; then
		if [ "$interactive" -eq 1 ]; then
			while :
			do
				read -p "Remove LXD networking for underlay:$underlay overlay:$overlay [Yn]: " confirm
				case "$confirm" in
				n*|N*)		return ;;
				y*|Y*)		break ;;
				'')		break ;;
				esac
			done
		fi
		progress "de-configuring LXD underlay:$underlay overlay:$overlay"
		twiddle_lxd_config remove "$device" "$fan_bridge"
	fi
}

identify_remote()
{
	local rip="$O_remote"

	echo "This host IP address: ${network_underlay_addr%/*}"

	[ "$rip" = 'none' ] && end 'Testing skipped'
	if [ "$rip" = '' ]; then
		while :
		do
			read -p "Remote test host IP address (none to skip): " rip
			case "$rip" in
			[0-9]*.[0-9]*.[0-9]*.[0-9]*)	break ;;
			''|none)			end 'Testing skipped' ;;
			*)
				echo "$nrip: badly formed IP address"
				;;
			esac
		done
	fi
	remote_ip="$rip"
}

#
# TEST-GROUP: test_local_all
#
test_local_all()
{
	local device="$1"
	local overlay="$2"
	local underlay="$3"

	test_local_lxd "$@"
	test_local_docker "$@"
}

#
# TEST-GROUP: test_all
#
test_all()
{
	local device="$1"
	local overlay="$2"
	local underlay="$3"
	local ip_th="$4"

	test_host "$@"
}

test_host()
{
	local device="$1"
	local overlay="$2"
	local underlay="$3"
	local ip_th="$4"

	underlay_th="$ip_th/$network_underlay_width"

	local ip=$( fan_to_host_ip $overlay $underlay "$network_mode" )
	local ip_tho=$( fan_to_host_ip $overlay $underlay_th "$network_mode" )

	echo "host test: starting ..."

	__ip_to_num_width "$ip"
	local ip_num="$RET"
	__ip_to_num_width "$ip_tho"
	local ip_num_tho="$RET"

	local rc
	if [ "$ip_num" -lt "$ip_num_tho" ]; then
		local_test 'master' "$ip" "$ip_tho"
		rc="$?"
	else
		local_test 'slave' "$ip" "$ip_tho"
		rc="$?"
	fi
	echo "host test: test complete $( rc_to_result "$rc" ) (local=$rc)"
}

# library: IP address manipulation.
__ip_split()
{
	eval `echo "$1" | {
		IFS=./ read a b c d e f
		echo ip_a="$a" ip_b="$b" ip_c="$c" ip_d="$d" ip_e="$e"
	}`
}

__width_to_mask()
{
	local width="$1"
	local mask=""
	local zeros="$(( (32-$width)/4 ))"

	while [ "$width" -ge 4 ]; do
		mask="${mask}F"
		width="$(($width - 4))"
	done
	case "$width" in
	3)	mask="${mask}E" ;;
	2)	mask="${mask}C" ;;
	1)	mask="${mask}8" ;;
	esac

	while [ "$zeros" -gt 0 ]; do
		mask="${mask}0"
		zeros="$(($zeros - 1))"
	done

	#printf "%x %x\n" "$((0x$mask))" "$(( (~0x$mask) & 0xffffffff ))"

	RET="$(( 0x$mask ))"
}

__ip_to_num_width()
{
	local ip_a
	local ip_b
	local ip_c
	local ip_d
	local ip_e
	local ip_f

	__ip_split "$1"

	RET="$(( ($ip_a << 24) | ($ip_b << 16) | ($ip_c << 8) | $ip_d ))"
	RET2="$ip_e"
}

__num_to_ip()
{
	local ip_num="$1"
	local ip_sep="$2"

	[ "$ip_sep" = '' ] && ip_sep='.'

	RET="$(( (($ip_num >> 24) & 0xff) ))$ip_sep$(( (($ip_num >> 16) & 0xff) ))$ip_sep$(( (($ip_num >> 8) & 0xff) ))$ip_sep$(( $ip_num & 0xff ))"
}
# library: end

# library: Bridges.
__fan_to_bridge_ipnum()
{
	local overlay="$1"
	local underlay="$2"
	local mode="$3"

	__ip_to_num_width "$overlay"
	local overlay_ipnum="$RET"
	local overlay_width="$RET2"

	__ip_to_num_width "$underlay"
	local underlay_ipnum="$RET"
	local underlay_width="$RET2"
	__width_to_mask "$underlay_width"
	local underlay_mask="$(( ~ $RET ))"
	local underlay_shift="$(( $underlay_width - $overlay_width ))"

	local bridge_name_ipnum="$(( $overlay_ipnum | (($underlay_ipnum & $underlay_mask) << $underlay_shift) ))"
	RET2="$bridge_name_ipnum"

	case "$mode" in
	compact)
		bridge_name_ipnum="$overlay_ipnum"
		;;
	esac

	local subnet_width=$(( $overlay_width + $underlay_width ))
	RET3="$subnet_width"

	RET="$bridge_name_ipnum"
}

fan_to_bridge()
{
	__fan_to_bridge_ipnum "$@"

        __num_to_ip "$RET" "-"
        RET=${RET%-0}; RET=${RET%-0}; RET=${RET%-0};

	echo "fan-$RET"
}

fan_to_host_ip()
{
	__fan_to_bridge_ipnum "$@"

        __num_to_ip "$(( $RET2 + 1 ))"

	echo "$RET"
}

fan_to_docker_cidr()
{

	__fan_to_bridge_ipnum "$@"
	local width="$RET3"

        __num_to_ip "$(( $RET2 ))"

	echo "$RET/$width"
}
# library: end

# test-payload: start
nc_receive()
{
	local ip="$1"
	local port="$2"

	echo "receiving on $ip:$port ..." 1>&2
	nc -d -n -l "$ip" "$port"
}
nc_send()
{
	local ip="$1"
	local port="$2"
	local data="$3"

	local file=0
	[ "$#" -eq 2 ] && file=1
		
	local cnt=30
	echo "sending to $ip:$port ..." 1>&2
	while [ "$cnt" -gt 0 ]
	do
		if [ "$file" -eq 0 ]; then
			echo "$data" | nc -n "$ip" "$port"
		else
			nc -n "$ip" "$port"
		fi
		if [ "$?" -eq 0 ]; then
			break
		fi
		sleep 2
		cnt="$(( $cnt+1 ))"
	done

	if [ "$cnt" -eq 0 ]; then
		fail "nc_send: failed to send -- timeout"
	fi
}

rc_to_result()
{
	if [ "$1" -eq 0 ]; then
		echo 'PASS'
	else
		echo 'FAIL'
	fi
}
test_report()
{
	local which="$1"
	local rc="$2"
	local ts="$3"

	echo "$which ... $( rc_to_result "$rc" )"
	if [ "$rc" != 0 ]; then
		echo "--- transcript start ---"
		cat "$ts"
		echo "--- transcript end ---"
	fi

	rm -f "$ts"
}
__payload_test()
{
	local master="$1"
	local lip="$2"
	local rip="$3"

	local out="/tmp/fantic-test-transcript.$$"

	# TEST: ping test
	echo "test $master: ping test ($rip) ..."
	(
		ping -c 5 "$rip"
	) >"$out" 2>&1
	test_report "test $master: ping test" "$?" "$out"

	# TEST: short data test
	echo "test $master: short data test ($lip -> $rip) ..."
	(
		if [ "$master" = 'master' ]; then
			nc_send "$rip" 9999 "HELLO $lip" || fail "test $master: unable to send data to $rip"
			data=$( nc_receive "$lip" 9999 )
		else
			data=$( nc_receive "$lip" 9999 )
			nc_send "$rip" 9999 "HELLO $lip"

		fi
		if [ "$data" != "HELLO $rip" ]; then
			fail "test $master: bad data receieved from $rip"
		fi
	) >"$out" 2>&1
	test_report "test $master: short data" "$?" "$out"

	# TEST: long data test
	echo "test $master: long data test ($lip -> $rip) ..."
	(
		local md5sum=$( cat /bin/* | md5sum - | awk '{print $1}' )
		local rsum_expected
		local rsum_is

		if [ "$master" = 'slave' ]; then
			nc_send "$rip" 9991 "$md5sum" || fail "test $master: unable to send md5sum data to $rip"
			cat /bin/* | nc_send "$rip" 9991 || fail "test $master: unable to send file data to $rip"

			rsum_expected=$( nc_receive "$lip" 9992 )
			rsum_is=$( nc_receive "$lip" 9992 | md5sum - | awk '{print $1}' )
		else
			rsum_expected=$( nc_receive "$lip" 9991 )
			rsum_is=$( nc_receive "$lip" 9991 | md5sum - | awk '{print $1}' )

			nc_send "$rip" 9992 "$md5sum" || fail "test $master: unable to send md5sum data to $rip"
			cat /bin/* | nc_send "$rip" 9992 || fail "test $master: unable to send file data to $rip"
		fi
		echo "rsum_expected<$rsum_expected> rsum_is<$rsum_is>"
		if [ "$rsum_expected" != "$rsum_is" ]; then
			fail "test $master: bad data receieved from $rip ($rsum_expected != $rsum_is)"
		fi
	) >"$out" 2>&1
	test_report "test $master: long data" "$?" "$out"
	##test_report "test $master: long data" "1" "$out"
}
# test-payload: end
local_test()
{
	(
		__payload_test "$@"
	)
}
payload_init()
{
	sed -n -e '/^# test-payload: start/,/^# test-payload: end/p' "$0";
}
payload_test()
{
	local master="$1"
	local lip="$2"
	local rip="$3"

	echo "__payload_test '$master' '$lip' '$rip'"
}

test_local_lxd()
{
	local device="$1"
	local overlay="$2"
	local underlay="$3"

	local fan_bridge=$( fan_to_bridge "$overlay" "$underlay" "$network_mode" )
	local ip=$( fan_to_host_ip $overlay $underlay "$network_mode" )
	local name="fanatic-test"

	if ! twiddle_lxd_config remove-check "$device" "$fan_bridge"; then
		return 0
	fi

	if [ "$interactive" -eq 1 ]; then
		while :
		do
			echo "Test LXD networking for underlay:$underlay overlay:$overlay"
			read -p "(NOTE: potentially triggers large image downloads) [Yn]: " confirm
			case "$confirm" in
			n*|N*)		return ;;
			y*|Y*)		break ;;
			'')		break ;;
			esac
		done
	fi
	
	# Check if we have images already...
	lxc image show fanatic-test >/dev/null 2>&1
	if [ "$?" -ne 0 ]; then
		echo "local lxd test: pulling container images ..."
		lxd-images import ubuntu --alias fanatic-test
		local rc="$?"
		if [ "$rc" -ne 0 ]; then
			echo "local lxd test: lxd-image import failure, skipping test."
			return "$rc"
		fi
	fi

	echo "local lxd test: creating test container ..."
	lxc launch fanatic-test fanatic-test -p "$fan_bridge"

	# wait for eth0 -- XXX: needs to timeout
	while :
	do
		echo "lxd test: Waiting for addresses on eth0 ..."
		ip_container="$(lxc exec fanatic-test -- ip -4 addr show dev eth0 | grep inet | awk -F '[/ ]*' '{print $3; exit}')"
		[ "$ip_container" != '' ] && break
		sleep 2
	done

	# Fire off the test payload in the LXC container.
	(
		{
			payload_init
			payload_test 'slave' "$ip_container" "$ip"
		} | lxc exec "$name" /bin/sh
		local rc="$?"
		exit "$rc"
	) &

	local rc=0
	local rc1
	local rc2

	# Run the payload locally as master.
	local_test 'master' "$ip" "$ip_container"
	rc1="$?"
	[ "$rc1" -ne 0 ] && rc="$rc1"

	# Wait for the LXD container payload to report.
	wait $!
	rc2="$?"
	[ "$rc2" -ne 0 ] && rc="$rc2"

	echo "local lxd test: destroying test container ..."
	lxc stop "$name"
	lxc delete "$name"

	echo "local lxd test: test complete $( rc_to_result "$rc" ) (master=$rc1 slave=$rc2)"
	return "$rc"
}

test_local_docker()
{
	local device="$1"
	local overlay="$2"
	local underlay="$3"

	local fan_bridge=$( fan_to_bridge "$overlay" "$underlay" "$network_mode" )
	local ip=$( fan_to_host_ip "$overlay" "$underlay" "$network_mode" )
	local name="fanatic-test"

	if ! twiddle_docker_config remove-check "$device"; then
		return
	fi

	if [ "$interactive" -eq 1 ]; then
		while :
		do
			echo "Test docker networking for underlay:$underlay overlay:$overlay"
			read -p "(NOTE: potentially triggers large image downloads) [Yn]: " confirm
			case "$confirm" in
			n*|N*)		return ;;
			y*|Y*)		break ;;
			'')		break ;;
			esac
		done
	fi


	# Check if we have images already...
	local images=$( docker images -q ubuntu | wc -l )
	if [ "$images" -eq 0 ]; then
		echo "local docker test: pulling container images ..."
		docker pull ubuntu
		local rc="$?"
		if [ "$rc" -ne 0 ]; then
			echo "local docker test: docker pull ubuntu failure, skipping test."
			return "$rc"
		fi
	fi

	# Start a test container.
	echo "local docker test: creating test container ..."
	docker run --name "$name" -d ubuntu sleep 3600
        ip_container=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$name")

	# Fire off the test payload in the LXC container.
	(
		{
			payload_init
			payload_test 'slave' "$ip_container" "$ip"
		} | docker exec -i "$name" /bin/sh
		local rc="$?"
		exit "$rc"
	) &

	local rc=0
	local rc1
	local rc2

	# Run the payload locally as master.
	local_test 'master' "$ip" "$ip_container"
	rc1="$?"
	[ "$rc1" -ne 0 ] && rc="$rc1"

	# Wait for the docker container payload to report.
	wait $!
	rc2="$?"
	[ "$rc2" -ne 0 ] && rc="$rc2"

	echo "local docker test: destroying test container ..."
	docker stop "$name"
	docker rm "$name"

	echo "local docker test: test complete $( rc_to_result "$rc" ) (master=$rc1 slave=$rc2)"
	return "$rc"
}

overlay_device()
{
	local format="$1"
	local a1="$2"

	case "$format" in
	A)		overlay="$a1" ;;
	*)		fail "$overlay: unknown overlay network format" ;;
	esac
}

underlay_device()
{
	local format="$1"
	local a1="$2"
	local a2="$3"

	case "$format" in
	A)		underlay="$a1" ;;
	B)		[ "$a1" = "default" ] && a1=`ip route show 0.0.0.0/0 | awk '{print $5; exit}'`
			a1="`ip -4 addr show dev "$a1" | grep inet | awk -F '[/ ]*' '{print $3; exit}'`"
			[ "$a1" = "" ] && fail "$a1: device address not found"
			underlay="$a1/$a2" 
			;;
	*)		fail "$underlay: unknown underlay network format" ;;
	esac
}

__underlay_lookup_address()
{
	local ipnum="$1"
	local width="$2"

	__width_to_mask "$width"
	local tmp
	local interface
	local what
	local address
	local address_mask="$RET"
	local address_ipnum
	local address_ipnum_prefix
	local address_prefix

	address_prefix="$(( $ipnum & $address_mask ))"
	# If an address on the network was specified find exactly that.
	if [ "$ipnum" -ne "$address_prefix" ]; then
		address_prefix="$ipnum"
		address_mask="$(( ~0 ))"
	fi

	ip -o addr show | \
	while read tmp interface what address tmp
	do
		case "$what" in
		inet)
			__ip_to_num_width "$address"
			address_ipnum="$RET"
			address_ipnum_prefix="$(( $RET & $address_mask ))"
			if [ "$address_ipnum_prefix" -eq "$address_prefix" ]; then
				__num_to_ip "$address_ipnum"
				echo "$RET/$width"
			fi
			;;
		esac
	done
}

underlay_lookup_address()
{
	eval RET=\'`__underlay_lookup_address "$@"`\'
}

networks_decode_addresses()
{
	local overlay="$1"
	local underlay="$2"

	local format="`echo "$overlay" | sed -n \
		-e 's@^\([0-9][0-9]*\)$@A \1.0.0.0/8@p' \
		-e 's@^\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\/[0-9][0-9]*\)$@A \1@p'
	`"
	overlay_device $format

	local format="`echo "$underlay" | sed -n \
		-e 's@^\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)$@A \1/16@p' \
		-e 's@^\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\/[0-9][0-9]*\)$@A \1@p' \
		-e 's@^\([a-z][a-z]*[0-9][0-9]*\|default\)$@B \1 16@p' \
		-e 's@^\([a-z][a-z]*[0-9][0-9]*\|default\)/\([0-9][0-9]*\)$@B \1 \2@p' \
	`"
	underlay_device $format

	# Find matching address(es) ...
        __ip_to_num_width "$underlay"
        local underlay_raw_ipnum="$RET"
        local underlay_width="$RET2"

        underlay_lookup_address "$underlay_raw_ipnum" "$underlay_width"
}

# Default the network interface to the one connected to our default route.
parse_commandline()
{
	local operation="$1"
	shift

	TEMP=$(getopt \
		-n "fanatic: $operation:" \
		-o 'u:o:r:' -l 'underlay:,overlay:,remote:' \
		-l 'no-docker-restart' \
		-- "$@"
	)
	if [ "$?" != 0 ]; then
		fail "$operation: invalid options"
	fi

	eval set -- "$TEMP"

	O_underlay=''
	O_overlay=''
	O_remote=''
	O_docker_restart=''

	# Legacy mode.
	if [ "$1" = '--' ]; then
		shift

		if [ "$#" -eq 0 ]; then
			return
		fi

		if [ "$#" -lt 2 ]; then
			fail "$operation: invalid options"
		fi
		O_overlay="$1"
		O_underlay="$2"
		shift 2

		if [ "$#" -lt 1 ]; then
			return
		fi
		O_remote="$1"
		shift

		if "$#" -gt 1 ]; then
			fail "$operation: invalid options"
		fi

		return
	fi

	while [ "$#" -gt 0 ]
	do
		case "$1" in
		--)
			shift
			break
			;;
		-u|--underlay)
			O_underlay="$2"
			shift 2
			;;
		-o|--overlay)
			O_overlay="$2"
			shift 2
			;;
		-r|--remote)
			O_remote="$2"
			shift 2
			;;
		--no-docker-restart)
			O_docker_restart=n
			shift
			;;
		esac
	done
}

identify_network()
{
	local operation="$1"

	local underlay="$O_underlay"
	local overlay="$O_overlay"

	local provided=1
	if [ "$overlay" = '' ]; then
		provided=0
		overlay='default'
	fi
	if [ "$underlay" = '' ]; then
		provided=0
		underlay='default'
	fi

	# Clear out the overlay/underlay specifiers.
	if [ "$overlay" = 'default' -o "$overlay" = '-' ]; then
		overlay="$network_overlay_default"
	fi

	if [ "$underlay" = 'default' -o "$underlay" = '-' ]; then
		# Work up a network prefix match for the default device.
		detect_network 'default'
		local underlay_addr="$RET2"

		__ip_to_num_width "$underlay_addr"
		local underlay_addr_num="$RET"

		__width_to_mask "$network_underlay_width"
		local underlay_mask="$RET"

		local underlay_prefix_num="$(( $underlay_addr_num & $underlay_mask ))"
		__num_to_ip "$underlay_prefix_num"

		underlay="$RET/$network_underlay_width"

	fi

	networks_decode_addresses "$overlay" "$underlay"

	#echo "overlay<$overlay> underlay<$underlay> RET<$RET>"

	network_tag=$( echo "$overlay" | sed -e 's@/@_@g' )
	network_overlay="$overlay"
	network_underlay="$underlay"
	network_underlay_addr="${RET%% *}"

	if [ "$provided" -eq 0 ]; then
		local over
		local under
		read -p "$operation fan underlay (hit return to accept, or specify alternative) [$underlay]: " under
		read -p "$operation fan overlay (hit return to accept, or specify alternative) [$overlay]: " over
		if [ "$over" != '' ]; then
			O_underlay="$under"
			O_overlay="$over"
			identify_network "$operation"
		fi
	fi
}

welcome()
{
	cat - <<EOM
Welcome to the fanatic fan networking wizard.  This will help you set
up an example fan network and optionally configure docker and/or LXD to
use this network.  See fanatic(1) for more details.

EOM
}

interactive=0
cmd="$1"
[ "$#" -ge 1 ] && shift 1
[ "$cmd" = '' ] && cmd='configure'

case "$cmd" in	
configure)
	interactive=1
	parse_commandline 'Configure' "$@"

	#echo "O_underlay<$O_underlay> O_overlay<$O_overlay> O_remote<$O_remote>"

	[ "$interactive" -eq 1 ] && welcome


	identify_network 'Configure'
	#echo "network_overlay<$network_overlay> network_underlay<$network_underlay>"

	enable_fan "$network_tag" "$network_overlay" "$network_underlay"
	enable_lxd "$network_tag" "$network_overlay" "$network_underlay"
	enable_docker "$network_tag" "$network_overlay" "$network_underlay" "$network_underlay_addr"

	test_local_all "$network_tag" "$network_overlay" "$network_underlay_addr"

	identify_remote
	#echo "remote_ip<$remote_up>"

	test_all "$network_tag" "$network_overlay" "$network_underlay_addr" "$remote_ip"
	;;
deconfigure|unconfigure)
	interactive=1
	parse_commandline 'Deconfigure' "$@"

	identify_network 'Deconfigure'
	disable_docker "$network_tag" "$network_overlay" "$network_underlay" "$network_underlay_addr"
	disable_lxd "$network_tag" "$network_overlay" "$network_underlay"
	disable_fan "$network_tag" "$network_overlay" "$network_underlay"
	;;
enable-fan)
	parse_commandline 'Reconfigure' "$@"
	identify_network 'Reconfigure'
	enable_fan "$network_tag" "$network_overlay" "$network_underlay"
	;;
disable-fan)
	parse_commandline 'Reconfigure' "$@"
	identify_network 'Reconfigure'
	disable_fan "$network_tag" "$network_overlay" "$network_underlay"
	;;
enable-docker)
	parse_commandline 'Reconfigure' "$@"
	identify_network 'Reconfigure'
	enable_docker "$network_tag" "$network_overlay" "$network_underlay" "$network_underlay_addr"
	;;
disable-docker)
	parse_commandline 'Reconfigure' "$@"
	identify_network 'Reconfigure'
	disable_docker "$network_tag" "$network_overlay" "$network_underlay" "$network_underlay_addr"
	;;
enable-lxd)
	parse_commandline 'Reconfigure' "$@"
	identify_network 'Reconfigure'
	enable_lxd "$network_tag" "$network_overlay" "$network_underlay"
	;;
disable-lxd)
	parse_commandline 'Reconfigure' "$@"
	identify_network 'Reconfigure'
	disable_lxd "$network_tag" "$network_overlay" "$network_underlay"
	;;
test)
	parse_commandline 'Reconfigure' "$@"
	identify_network 'Test'
	
	test_local_all "$network_tag" "$network_overlay" "$network_underlay_addr"

	identify_remote
	test_all "$network_tag" "$network_overlay" "$network_underlay_addr" "$remote_ip"
	;;
test-host)
	parse_commandline 'Reconfigure' "$@"
	identify_network 'Test'
	identify_remote
	test_host "$network_tag" "$network_overlay" "$network_underlay_addr" "$remote_ip"
	;;
test-local-lxd)
	parse_commandline 'Reconfigure' "$@"
	identify_network 'Test'
	test_local_lxd "$network_tag" "$network_overlay" "$network_underlay_addr"
	;;
test-local-docker)
	parse_commandline 'Reconfigure' "$@"
	identify_network 'Test'
	test_local_docker "$network_tag" "$network_overlay" "$network_underlay_addr"
	;;
*)
	usage "$cmd: uknown command"
	;;
esac