]> git.ipfire.org Git - people/ms/network.git/blame - functions.util
network: Remove some unneeded functions.
[people/ms/network.git] / functions.util
CommitLineData
1848564d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22# Print a pretty error message
23function error() {
2ab7f50f 24 echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2
1848564d
MT
25}
26
1b7a1578
MT
27function error_log() {
28 error "$@"
29 log ERROR "$@"
30}
31
1848564d
MT
32# Print a pretty warn message
33function warning() {
2ab7f50f 34 echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
1848564d
MT
35}
36
1b7a1578
MT
37function warning_log() {
38 warning "$@"
39 log WARNING "$@"
40}
41
711ffac1 42# XXX uses tr
1848564d
MT
43function listsort() {
44 local i
45 for i in $@; do
46 echo "${i}"
47 done | sort | tr "\n" " "
48}
49
711ffac1
MT
50function listmatch() {
51 local match=${1}
52 shift
53
54 assert isset match
55
56 local i
57 for i in $@; do
58 [ "${match}" = "${i}" ] && return ${EXIT_OK}
59 done
60
61 return ${EXIT_ERROR}
62}
63
64function listlength() {
65 local length=0
66
67 local i
68 for i in $@; do
69 length=$(( ${length} + 1 ))
70 done
71
72 echo "${length}"
73}
74
1848564d
MT
75function config_read() {
76 local config_file=${1}
77
78 if [ -e "${config_file}" ]; then
79 . ${config_file}
80 config_check
81 fi
82}
83
84function config_write() {
85 local config_file=${1}
86 shift
87
88 # Check if all values to be written are sane
89 config_check
90
1b7a1578
MT
91 log DEBUG "Writing configuration file ${config_file}."
92
1848564d
MT
93 > ${config_file}
94
95 local param
96 for param in $(listsort $@); do
97 echo "${param}=\"${!param}\"" >> ${config_file}
98 done
99}
100
101function config_print() {
102 local param
103
104 for param in $(listsort $@); do
105 printf "%-16s = %s\n" "${param}" "${!param}"
106 done
107}
108
109function config_check() {
110 # If there is a function defined that is called __check
111 # we call that function
112 [ -n "$(type -t _check)" ] && _check
113}
114
115function network_config_set() {
116 while [ $# -gt 0 ]; do
117 case "${1}" in
118 *=*)
1b7a1578
MT
119 log INFO "Setting configuration option '${1}'".
120 eval ${1}
1848564d
MT
121 ;;
122 *)
123 warning "Invalid parameter given: ${1}"
124 ;;
125 esac
126 shift
127 done
128
129 # Write configuration to disk
130 network_config_write
131}
132
133function network_config_read() {
134 config_read ${CONFIG_FILE}
135}
136
137function network_config_write() {
138 config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
139}
140
141function network_config_print() {
142 config_print ${CONFIG_FILE_PARAMS}
143}
144
145# Speedup function to avoid a call of the basename binary
146function basename() {
147 echo "${1##*/}"
148}
149
150function enabled() {
151 local param=${1}
152
153 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
154}
155
156function mac_generate() {
157 local mac=()
158 for i in $(seq 0 5); do
21dbdbb9
MT
159 mac[i]="$(uuid)"
160 mac[i]="0x${mac[i]:0:2}"
1848564d
MT
161 done
162
163 # Remove multicast bit
164 # and set address is software assigned
165 # XXX must doublecheck if this works
166 mac[0]=$((mac[0] & 0xfe))
167 mac[0]=$((mac[0] | 0x02))
168
169 local output
170 for i in ${mac[*]}; do
21dbdbb9
MT
171 if [ -n "${output}" ]; then
172 output="${output}:"
173 fi
1848564d 174
21dbdbb9 175 output="${output}$(printf "%02x" ${i})"
1848564d
MT
176 done
177
178 # Check if output is valid
179 assert mac_is_valid ${output}
180
181 echo ${output}
182}
183
18b43372
MT
184function mac_format() {
185 local mac=${1}
186
187 local output
188
189 if [ "${#mac}" = "12" ]; then
190 # Add colons (:) to mac address
191 output=${mac:0:2}
192 local i
193 for i in 2 4 6 8 10; do
194 output="${output}:${mac:${i}:2}"
195 done
196 fi
197
198 assert mac_is_valid ${output}
199
200 echo "${output}"
201}
202
1848564d
MT
203function mac_is_valid() {
204 local mac=${1}
205
206 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
207}
208
209function uuid() {
de543653 210 echo $(</proc/sys/kernel/random/uuid)
1848564d
MT
211}
212
213function isset() {
214 local var=${1}
215
216 [ -n "${!var}" ]
217}
218
219function isoneof() {
220 local var=${!1}
221 shift
222
223 for i in $@; do
224 [ "${var}" = "${i}" ] && return ${EXIT_OK}
225 done
226
227 return ${EXIT_ERROR}
228}
229
230function isbool() {
231 local var=${1}
232
233 isoneof ${var} 0 1 no yes on off
234}
235
236function isinteger() {
237 local var=${!1}
238
239 [[ ${var} =~ ^[0-9]+$ ]]
240}
241
242function ismac() {
243 local mac=${!1}
244
245 mac_is_valid ${mac}
246}
247
711ffac1
MT
248function backtrace() {
249 local start=1
250
251 echo # Empty line
252 error_log "Backtrace (most recent call in first line):"
253
254 local i
255 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
256 [ -z "${FUNCNAME[${i}]}" ] && continue
257 [ "${FUNCNAME[${i}]}" == "main" ] && continue
258
259 error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
260 done
261}
262
1848564d
MT
263function assert() {
264 local assertion="$@"
265
266 if ! ${assertion}; then
4c670d7c 267 error_log "Assertion '${assertion}' failed."
711ffac1 268 backtrace
1848564d
MT
269 exit ${EXIT_ERROR}
270 fi
271
272 return ${EXIT_OK}
273}
cad8bd85 274
711ffac1
MT
275function exec_cmd() {
276 local cmd=$@
277
278 log DEBUG "Running command: ${cmd}"
279
280 ${SHELL} ${cmd}
281 local ret=$?
282
283 #log DEBUG "Returned with code '${ret}'"
284
285 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
286 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
287 exit ${EXIT_ERROR_ASSERT}
288 fi
289
290 return ${ret}
291}
292
cad8bd85
MT
293function uppercase() {
294 local input
295 read input
296 echo "${input^^}"
297}
d82cf370 298
3efecbb3
MT
299function lowercase() {
300 local input
301 read input
302 echo "${input,,}"
303}
304
305function seq() {
306 if [ $# -eq 2 ]; then
307 eval echo {${1}..${2}}
308 elif [ $# -eq 3 ]; then
309 eval echo {${1}..${3}..${2}}
310 fi
311}
312
d82cf370
MT
313function beautify_time() {
314 local value=${1}
315
316 local unit
317 local limit
318 for unit in s m h d w; do
319 case "${unit}" in
320 s|m|h)
321 limit=60
322 ;;
323 d)
324 limit=24
325 ;;
326 w)
327 limit=7
328 ;;
329 esac
330
331 [ ${value} -lt ${limit} ] && break
332
333 value=$(( ${value} / ${limit} ))
334 done
335
336 echo "${value}${unit}"
337}
711ffac1
MT
338
339function beautify_bytes() {
340 local value=${1}
341
342 local unit
343 local limit=1024
344 for unit in B k M G T; do
345 [ ${value} -lt ${limit} ] && break
346 value=$(( ${value} / ${limit} ))
347 done
348
349 echo "${value}${unit}"
350}