]>
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
)
224 [ "${var}" = "${i}" ] && return ${EXIT_OK}
233 isoneof ${var} 0 1 no yes on off
236 function isinteger() {
239 [[ ${var} =~ ^[0-9]+$ ]]
248 function backtrace() {
252 error_log "Backtrace
(most recent call
in first line
):"
255 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
256 [ -z "${FUNCNAME[${i}]}" ] && continue
257 [ "${FUNCNAME[${i}]}" == "main
" ] && continue
259 error_log " $
(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
266 if ! ${assertion}; then
267 error_log
"Assertion '${assertion}' failed."
275 function exec_cmd
() {
278 log DEBUG
"Running command: ${cmd}"
283 #log DEBUG "Returned with code '${ret}'"
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}
293 function uppercase
() {
299 function lowercase
() {
306 if [ $# -eq 2 ]; then
307 eval echo {${1}..
${2}}
308 elif [ $# -eq 3 ]; then
309 eval echo {${1}..${3}..${2}}
313 function beautify_time
() {
318 for unit
in s m h d w
; do
331 [ ${value} -lt ${limit} ] && break
333 value
=$
(( ${value} / ${limit} ))
336 echo "${value}${unit}"
339 function beautify_bytes
() {
344 for unit
in B k M G T
; do
345 [ ${value} -lt ${limit} ] && break
346 value
=$
(( ${value} / ${limit} ))
349 echo "${value}${unit}"