]>
git.ipfire.org Git - people/arne_f/network.git/blob - functions.util
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
22 # Print a pretty error message
24 echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2
27 function error_log
() {
32 # Print a pretty warn message
34 echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
37 function warning_log
() {
47 done |
sort |
tr "\n" " "
50 function listmatch
() {
58 [ "${match}" = "${i}" ] && return ${EXIT_OK}
64 function listlength() {
69 length=$(( ${length} + 1 ))
75 function config_read() {
76 local config_file=${1}
78 if [ -e "${config_file}" ]; then
84 function config_write() {
85 local config_file=${1}
88 # Check if all values to be written are sane
91 log DEBUG "Writing configuration
file ${config_file}.
"
96 for param in $(listsort $@); do
97 echo "${param}=\"${!param}\"" >> ${config_file}
101 function config_print
() {
104 for param
in $
(listsort $@
); do
105 printf "%-16s = %s\n" "${param}" "${!param}"
109 function config_check
() {
110 # If there is a function defined that is called __check
111 # we call that function
112 [ -n "$(type -t _check)" ] && _check
115 function network_config_set
() {
116 while [ $# -gt 0 ]; do
119 log INFO
"Setting configuration option '${1}'".
123 warning
"Invalid parameter given: ${1}"
129 # Write configuration to disk
133 function network_config_read
() {
134 config_read
${CONFIG_FILE}
137 function network_config_write
() {
138 config_write
${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
141 function network_config_print
() {
142 config_print
${CONFIG_FILE_PARAMS}
145 # Speedup function to avoid a call of the basename binary
146 function basename() {
153 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
156 function mac_generate
() {
158 for i
in $
(seq 0 5); do
160 mac
[i
]="0x${mac[i]:0:2}"
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))
170 for i
in ${mac[*]}; do
171 if [ -n "${output}" ]; then
175 output
="${output}$(printf "%02x
" ${i})"
178 # Check if output is valid
179 assert mac_is_valid
${output}
184 function mac_format
() {
189 if [ "${#mac}" = "12" ]; then
190 # Add colons (:) to mac address
193 for i
in 2 4 6 8 10; do
194 output
="${output}:${mac:${i}:2}"
198 assert mac_is_valid
${output}
203 function mac_is_valid
() {
206 [[ ${mac} =~ ^
([0-9a-f]{2}\
:){5}[0-9a-f]{2}$
]]
210 echo $
(</proc
/sys
/kernel
/random
/uuid
)
219 # XXX Nearly same as listmatch
225 [ "${var}" = "${i}" ] && return ${EXIT_OK}
234 isoneof ${var} 0 1 no yes on off
237 function isinteger() {
240 [[ ${var} =~ ^[0-9]+$ ]]
249 function backtrace() {
253 error_log "Backtrace
(most recent call
in first line
):"
256 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
257 [ -z "${FUNCNAME[${i}]}" ] && continue
258 [ "${FUNCNAME[${i}]}" == "main
" ] && continue
260 error_log " $
(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
267 if ! ${assertion}; then
268 error_log
"Assertion '${assertion}' failed."
276 function exec_cmd
() {
279 log DEBUG
"Running command: ${cmd}"
284 #log DEBUG "Returned with code '${ret}'"
286 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
287 error_log
"Stopping parent process due to assertion error in child process: ${cmd}"
288 exit ${EXIT_ERROR_ASSERT}
294 function uppercase
() {
300 function lowercase
() {
307 if [ $# -eq 2 ]; then
308 eval echo {${1}..
${2}}
309 elif [ $# -eq 3 ]; then
310 eval echo {${1}..${3}..${2}}
314 function beautify_time
() {
319 for unit
in s m h d w
; do
332 [ ${value} -lt ${limit} ] && break
334 value
=$
(( ${value} / ${limit} ))
337 echo "${value}${unit}"
340 function beautify_bytes
() {
345 for unit
in B k M G T
; do
346 [ ${value} -lt ${limit} ] && break
347 value
=$
(( ${value} / ${limit} ))
350 echo "${value}${unit}"
353 function module_load
() {
356 if ! grep -q "^${module}" /proc
/modules
; then
357 log DEBUG
"Loading module '${module}'."