summaryrefslogtreecommitdiffhomepage
path: root/contrib/package/olsrd-luci/files/etc/init.d/olsrd
blob: 54f5502bfcafd1c487fbd34e82db1f0dd559f627 (plain)
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
#!/bin/sh /etc/rc.common
# Copyright (C) 2008  Alina Friedrichsen <x-alina@gmx.net>
# Special thanks to bittorf wireless ))
START=50

BIN=/usr/sbin/olsrd
CONF=/var/etc/olsrd.conf
PID=/var/run/olsrd.pid

OLSRD_OLSRD_SCHEMA='ignore:internal config_file:internal DebugLevel=0 IpVersion=4 AllowNoInt:bool=1 Pollrate=0.025 TcRedundancy=2 MprCoverage=3 LinkQualityFishEye=1 LinkQualityWinSize=100 LinkQualityDijkstraLimit=0+9.0 LinkQualityLevel=2 UseHysteresis:bool=0 FIBMetric=flat ClearScreen:bool=1 Willingness=3 LinkQualityAging=0.1 LinkQualityAlgorithm=etx_fpm'
OLSRD_IPCCONNECT_SCHEMA='ignore:internal Host:list Net:list2'
OLSRD_LOADPLUGIN_SCHEMA='ignore:internal library:internal Host:list Net:list2 Ping:list redistribute:list NonOlsrIf:list name:list lat lon latlon_infile HNA:list2 hosts:list2'
OLSRD_INTERFACE_SCHEMA='ignore:internal interface:internal AutoDetectChanges:bool Ip4Broadcast=255.255.255.255 HelloInterval=2.0 HelloValidityTime=40.0 TcInterval=5.0 TcValidityTime=100.0 MidInterval=18.0 MidValidityTime=324.0 HnaInterval=18.0 HnaValidityTime=108.0'

T='	'
N='
'

validate_varname() {
	local varname=$1
	[ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1
	return 0
}

validate_ifname() {
	local ifname=$1
	[ -z "$ifname" -o "$ifname" != "${ifname%%[!A-Za-z0-9.:_-]*}" ] && return 1
	return 0
}

validate_olsrd_option() {
	local str=$1
	[ -z "$str" -o "$str" != "${str%%[! 	0-9A-Za-z./:_-]*}" ] && return 1
	return 0
}

get_ifname() {
	IFNAME=
	local interface=$1
	validate_varname "$interface" || return 1
	local ifname

	config_get ifname "$interface" ifname
	validate_ifname "$ifname" || return 1
	IFNAME=$ifname

	return 0
}

system_config() {
	local cfg=$1
	local cfgt
	local hostname
	local latlon

	config_get cfgt "$cfg" TYPE

	if [ "$cfgt" = "system" ]; then
		config_get hostname "$cfg" hostname
		hostname=${hostname:-OpenWrt}
		SYSTEM_HOSTNAME=$hostname
	fi

	if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
		config_get latlon "$cfg" latlon
		IFS=" ${T}${N},"
		set -- $latlon
		unset IFS
		SYSTEM_LAT=$1
		SYSTEM_LON=$2
	fi
}

olsrd_find_config_file() {
	local cfg=$1
	validate_varname "$cfg" || return 0

	config_get_bool ignore "$cfg" ignore 0
	[ "$ignore" -ne 0 ] && return 0
	config_get OLSRD_CONFIG_FILE "$cfg" config_file

	return 0
}

warning_invalid_value() {
	local package=$1
	validate_varname "$package" || package=
	local config=$2
	validate_varname "$config" || config=
	local option=$3
	validate_varname "$option" || option=

	echo -n "Warning: Invalid value" 1>&2

	if [ -n "$package" -a -n "$config" ]; then
		echo -n " in option '$package.$config${option:+.}$option'" 1>&2
	fi

	echo ", skipped" 1>&2

	return 0
}

olsrd_write_option() {
	local param=$1
	local cfg=$2
	validate_varname "$cfg" || return 1
	local option=$3
	validate_varname "$option" || return 1
	local value=$4
	local option_type=$5

	if [ "$option_type" = bool ]; then
		case "$value" in
			1|on|true|enabled|yes) value=yes;;
			0|off|false|disabled|no) value=no;;
			*) warning_invalid_value olsrd "$cfg" "$option"; return 1;;
		esac
	fi

	if ! validate_olsrd_option "$value"; then
		warning_invalid_value olsrd "$cfg" "$option"
		return 1
	fi

	if [ "$value" != "${value%%[G-Zg-z_-]*}" ]; then
		if [ "$value" != "yes" -a "$value" != "no" ]; then
			value="\"$value\""
		fi
	fi

	echo -n "${N}$param$option $value"

	return 0
}

olsrd_write_plparam() {
	local param=$1
	local cfg=$2
	validate_varname "$cfg" || return 1
	local option=$3
	validate_varname "$option" || return 1
	local value=$4
	local option_type=$5
	local _option

	if [ "$option_type" = bool ]; then
		case "$value" in
			1|on|true|enabled|yes) value=yes;;
			0|off|false|disabled|no) value=no;;
			*) warning_invalid_value olsrd "$cfg" "$option"; return 1;;
		esac
	fi

	if ! validate_olsrd_option "$value"; then
		warning_invalid_value olsrd "$cfg" "$option"
		return 1
	fi

	IFS='-_'
	set -- $option
	option="$*"
	unset IFS
	_option="$option"
	if [ "$option" = 'hosts' ]; then
		set -- $value
		option=$1
		shift
		value="$*"
	fi

	echo -n "${N}${param}PlParam \"$option\" \"$value\""

	return 0
}

config_update_schema() {
	unset IFS
	local schema_varname=$1
	validate_varname "$schema_varname" || return 1
	local command=$2
	validate_varname "$command" || return 1
	local option=$3
	validate_varname "$option" || return 1
	local value=$4
	local schema
	local cur_option

	case "$varname" in
		*_LENGTH) return 0;;
		*_ITEM*) return 0;;
	esac

	eval "export -n -- \"schema=\${$schema_varname}\""

	for cur_option in $schema; do
		[ "${cur_option%%[:=]*}" = "$option" ] && return 0
	done

	if [ "$command" = list ]; then
		set -- $value
		if [ "$#" -ge "3" ]; then
			schema_entry="$option:list3"
		elif [ "$#" -ge "2" ]; then
			schema_entry="$option:list2"
		else
			schema_entry="$option:list"
		fi
	else
		schema_entry="$option"
	fi

	append "$schema_varname" "$schema_entry"

	return 0
}

config_write_options() {
	unset IFS
	local schema=$1
	local cfg=$2
	validate_varname "$cfg" || return 1
	local write_func=$3
	[ -z "$write_func" ] && output_func=echo
	local write_param=$4
	local schema_entry
	local option
	local option_length
	local option_type
	local default
	local value
	local list_size
	local list_item
	local list_value
	local i
	local position

	for schema_entry in $schema; do
		default=${schema_entry#*[=]}
		[ "$default" = "$schema_entry" ] && default=
		option=${schema_entry%%[=]*}
		IFS=':'
		set -- $option
		unset IFS
		option=$1
		option_type=$2
		validate_varname "$option" || continue
		[ -z "$option_type" ] || validate_varname "$option_type" || continue
		[ "$option_type" = internal ] && continue
		config_get value "$cfg" "$option"

		if [ -z "$value" ]; then
			IFS='+'
			set -- $default
			unset IFS
			value=$*
		elif [ "$value" = '-' -a -n "$default" ]; then
			continue
		fi

		[ -z "$value" ] && continue

		case "$option_type" in
			list) list_size=1;;
			list2) list_size=2;;
			list3) list_size=3;;
			*) list_size=0;;
		esac

		if [ "$list_size" -gt 0 ]; then
			config_get option_length "$cfg" "${option}_LENGTH"
			if [ -n "$option_length" ]; then
				i=1
				while [ "$i" -le "$option_length" ]; do
					config_get list_value "$cfg" "${option}_ITEM$i"
					"$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" || break
					i=$((i + 1))
				done
			else
				list_value=
				i=0
				for list_item in $value; do
					append "list_value" "$list_item"
					i=$((i + 1))
					position=$((i % list_size))
					if [ "$position" -eq 0 ]; then
						"$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" || break
						list_value=
					fi
				done
				[ "$position" -ne 0 ] && "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type"
			fi
		else
			"$write_func" "$write_param" "$cfg" "$option" "$value" "$option_type"
		fi
	done

	return 0
}

olsrd_write_olsrd() {
	local cfg=$1
	validate_varname "$cfg" || return 0
	local ignore

	config_get_bool ignore "$cfg" ignore 0
	[ "$ignore" -ne 0 ] && return 0

	[ "$OLSRD_COUNT" -gt 0 ] && return 0

	config_write_options "$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
	echo
	OLSRD_COUNT=$((OLSRD_COUNT + 1))

	return 0
}

olsrd_write_ipcconnect() {
	local cfg=$1
	validate_varname "$cfg" || return 0
	local ignore

	config_get_bool ignore "$cfg" ignore 0
	[ "$ignore" -ne 0 ] && return 0

	[ "$IPCCONNECT_COUNT" -gt 0 ] && return 0

	echo -n "${N}IpcConnect${N}{"
	config_write_options "$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option "${T}"
	echo "${N}}"
	IPCCONNECT_COUNT=$((IPCCONNECT_COUNT + 1))

	return 0
}

olsrd_write_hna4() {
	local cfg=$1
	validate_varname "$cfg" || return 0
	local ignore

	config_get_bool ignore "$cfg" ignore 0
	[ "$ignore" -ne 0 ] && return 0

	config_get netaddr "$cfg" netaddr
	if ! validate_olsrd_option "$netaddr"; then
		warning_invalid_value olsrd "$cfg" "netaddr"
		return 0
	fi

	config_get netmask "$cfg" netmask
	if ! validate_olsrd_option "$netmask"; then
		warning_invalid_value olsrd "$cfg" "netmask"
		return 0
	fi

	[ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
	echo -n "${N}${T}$netaddr $netmask"
	HNA4_COUNT=$((HNA4_COUNT + 1))

	return 0
}

olsrd_write_hna6() {
	local cfg=$1
	validate_varname "$cfg" || return 0
	local ignore

	config_get_bool ignore "$cfg" ignore 0
	[ "$ignore" -ne 0 ] && return 0

	config_get netaddr "$cfg" netaddr
	if ! validate_olsrd_option "$netaddr"; then
		warning_invalid_value olsrd "$cfg" "netaddr"
		return 0
	fi

	config_get prefix "$cfg" prefix
	if ! validate_olsrd_option "$prefix"; then
		warning_invalid_value olsrd "$cfg" "prefix"
		return 0
	fi

	[ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
	echo -n "${N}${T}$netaddr $prefix"
	HNA6_COUNT=$((HNA6_COUNT + 1))

	return 0
}

olsrd_write_loadplugin() {
	local cfg=$1
	validate_varname "$cfg" || return 0
	local ignore
	local name
	local suffix
	local lat
	local lon
	local latlon_infile

	config_get_bool ignore "$cfg" ignore 0
	[ "$ignore" -ne 0 ] && return 0

	config_get library "$cfg" library
	if ! validate_olsrd_option "$library"; then
		warning_invalid_value olsrd "$cfg" "library"
		return 0
	fi
	if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
		echo "Warning: Plugin library '$library' not found, skipped" 1>&2
		return 0
	fi

	case "$library" in
		olsrd_nameservice.*)
			config_get name "$cfg" name
			[ -z "$name" ] && config_set "$cfg" name $SYSTEM_HOSTNAME

			config_get suffix "$cfg" suffix
			[ -z "$suffix" ] && config_set "$cfg" suffix '.olsr'

			config_get lat "$cfg" lat
			config_get lon "$cfg" lon
			config_get latlon_infile "$cfg" latlon_infile
			if [ \( -z "$lat" -o -z "$lat" \) -a -z "$latlon_infile" ]; then
				if [ -f '/var/run/latlon.txt' ]; then
					config_set "$cfg" lat ''
					config_set "$cfg" lon ''
					config_set "$cfg" latlon_infile '/var/run/latlon.txt'
				else
					config_set "$cfg" lat "$SYSTEM_LAT"
					config_set "$cfg" lon "$SYSTEM_LON"
				fi
			fi

			config_get latlon_file "$cfg" latlon_file
			[ -z "$latlon_file" ] && config_set "$cfg" latlon_file '/var/run/latlon.js'
		;;
	esac

	echo -n "${N}LoadPlugin \"$library\"${N}{"
	config_write_options "$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam "${T}"
	echo "${N}}"

	return 0
}

olsrd_write_interface() {
	local cfg=$1
	validate_varname "$cfg" || return 0
	local ignore
	local interfaces
	local interface
	local ifnames

	config_get_bool ignore "$cfg" ignore 0
	[ "$ignore" -ne 0 ] && return 0

	ifnames=
	config_get interfaces "$cfg" interface
	for interface in $interfaces; do
		if validate_varname "$interface"; then
			if get_ifname "$interface"; then
				ifnames="$ifnames \"$IFNAME\""
			else
				echo "Warning: Interface '$interface' not found, skipped" 1>&2
			fi
		else
			warning_invalid_value olsrd "$cfg" "interface"
		fi
	done

	[ -z "$ifnames" ] && return 0

	echo -n "${N}Interface$ifnames${N}{"
	config_write_options "$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option "${T}"
	echo "${N}}"
	INTERFACES_COUNT=$((INTERFACES_COUNT + 1))

	return 0
}

olsrd_update_schema() {
	local command="$1"
	validate_varname "$command" || return 0
	local varname="$2"
	validate_varname "$varname" || return 0
	local value="$3"
	local cfg="$CONFIG_SECTION"
	local cfgt
	local cur_varname

	config_get cfgt "$cfg" TYPE
	case "$cfgt" in
		olsrd) config_update_schema OLSRD_OLSRD_SCHEMA "$command" "$varname" "$value";;
		IpcConnect) config_update_schema OLSRD_IPCCONNECT_SCHEMA "$command" "$varname" "$value";;
		LoadPlugin) config_update_schema OLSRD_LOADPLUGIN_SCHEMA "$command" "$varname" "$value";;
		Interface) config_update_schema OLSRD_INTERFACE_SCHEMA "$command" "$varname" "$value";;
	esac

	return 0
}

olsrd_write_config() {
	OLSRD_COUNT=0
	config_foreach olsrd_write_olsrd olsrd
	IPCCONNECT_COUNT=0
	config_foreach olsrd_write_ipcconnect IpcConnect
	HNA4_COUNT=0
	config_foreach olsrd_write_hna4 Hna4
	[ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
	HNA6_COUNT=0
	config_foreach olsrd_write_hna6 Hna6
	[ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
	config_foreach olsrd_write_loadplugin LoadPlugin
	INTERFACES_COUNT=0
	config_foreach olsrd_write_interface Interface
	echo

	return 0
}

start() {
	### check for running instance
	if [ -s $PID ]; then
		if kill -0 $(cat $PID) 2>&-; then
			echo "there is already a running instance ($(cat $PID))"
			return 1
		fi
	fi
	
	SYSTEM_HOSTNAME=
	SYSTEM_LAT=
	SYSTEM_LON=
	config_load system
	config_foreach system_config system

	option_cb() {
		olsrd_update_schema "option" "$@"
	}

	list_cb() {
		olsrd_update_schema "list" "$@"
	}

	include /lib/network
	scan_interfaces
	config_load olsrd
	reset_cb

	OLSRD_CONFIG_FILE=
	config_foreach olsrd_find_config_file olsrd

	if [ -z "$OLSRD_CONFIG_FILE" ]; then
		### generate config
		mkdir -p ${CONF%/*}
		olsrd_write_config > $CONF
		if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
			OLSRD_CONFIG_FILE=$CONF
		fi
	fi

	[ -z "$OLSRD_CONFIG_FILE" ] && return 1

	### start olsrd
	start-stop-daemon -b -m -p $PID -x $BIN -S -- -f $CONF -nofork
}

stop() {
	### stop olsrd
	start-stop-daemon -q -p $PID -a ${BIN##*/} -K
}

restart() {
	### override generic restart because we need some time between stop and start
	stop; sleep 3; start
}