]> git.ipfire.org Git - people/ms/network.git/blame - functions.util
network: New function mac_format.
[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() {
24 echo -e " ${FAIL}ERROR${NORMAL} : $@" >&2
25}
26
1b7a1578
MT
27function error_log() {
28 error "$@"
29 log ERROR "$@"
30}
31
1848564d
MT
32# Print a pretty warn message
33function warning() {
34 echo -e " ${WARN}WARNING${NORMAL}: $@" >&2
35}
36
1b7a1578
MT
37function warning_log() {
38 warning "$@"
39 log WARNING "$@"
40}
41
1848564d
MT
42function listsort() {
43 local i
44 for i in $@; do
45 echo "${i}"
46 done | sort | tr "\n" " "
47}
48
49function config_read() {
50 local config_file=${1}
51
52 if [ -e "${config_file}" ]; then
53 . ${config_file}
54 config_check
55 fi
56}
57
58function config_write() {
59 local config_file=${1}
60 shift
61
62 # Check if all values to be written are sane
63 config_check
64
1b7a1578
MT
65 log DEBUG "Writing configuration file ${config_file}."
66
1848564d
MT
67 > ${config_file}
68
69 local param
70 for param in $(listsort $@); do
71 echo "${param}=\"${!param}\"" >> ${config_file}
72 done
73}
74
75function config_print() {
76 local param
77
78 for param in $(listsort $@); do
79 printf "%-16s = %s\n" "${param}" "${!param}"
80 done
81}
82
83function config_check() {
84 # If there is a function defined that is called __check
85 # we call that function
86 [ -n "$(type -t _check)" ] && _check
87}
88
89function network_config_set() {
90 while [ $# -gt 0 ]; do
91 case "${1}" in
92 *=*)
1b7a1578
MT
93 log INFO "Setting configuration option '${1}'".
94 eval ${1}
1848564d
MT
95 ;;
96 *)
97 warning "Invalid parameter given: ${1}"
98 ;;
99 esac
100 shift
101 done
102
103 # Write configuration to disk
104 network_config_write
105}
106
107function network_config_read() {
108 config_read ${CONFIG_FILE}
109}
110
111function network_config_write() {
112 config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
113}
114
115function network_config_print() {
116 config_print ${CONFIG_FILE_PARAMS}
117}
118
119# Speedup function to avoid a call of the basename binary
120function basename() {
121 echo "${1##*/}"
122}
123
124function enabled() {
125 local param=${1}
126
127 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
128}
129
130function mac_generate() {
131 local mac=()
132 for i in $(seq 0 5); do
21dbdbb9
MT
133 mac[i]="$(uuid)"
134 mac[i]="0x${mac[i]:0:2}"
1848564d
MT
135 done
136
137 # Remove multicast bit
138 # and set address is software assigned
139 # XXX must doublecheck if this works
140 mac[0]=$((mac[0] & 0xfe))
141 mac[0]=$((mac[0] | 0x02))
142
143 local output
144 for i in ${mac[*]}; do
21dbdbb9
MT
145 if [ -n "${output}" ]; then
146 output="${output}:"
147 fi
1848564d 148
21dbdbb9 149 output="${output}$(printf "%02x" ${i})"
1848564d
MT
150 done
151
152 # Check if output is valid
153 assert mac_is_valid ${output}
154
155 echo ${output}
156}
157
18b43372
MT
158function mac_format() {
159 local mac=${1}
160
161 local output
162
163 if [ "${#mac}" = "12" ]; then
164 # Add colons (:) to mac address
165 output=${mac:0:2}
166 local i
167 for i in 2 4 6 8 10; do
168 output="${output}:${mac:${i}:2}"
169 done
170 fi
171
172 assert mac_is_valid ${output}
173
174 echo "${output}"
175}
176
1848564d
MT
177function mac_is_valid() {
178 local mac=${1}
179
180 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
181}
182
183function uuid() {
184 cat /proc/sys/kernel/random/uuid
185}
186
187function isset() {
188 local var=${1}
189
190 [ -n "${!var}" ]
191}
192
193function isoneof() {
194 local var=${!1}
195 shift
196
197 for i in $@; do
198 [ "${var}" = "${i}" ] && return ${EXIT_OK}
199 done
200
201 return ${EXIT_ERROR}
202}
203
204function isbool() {
205 local var=${1}
206
207 isoneof ${var} 0 1 no yes on off
208}
209
210function isinteger() {
211 local var=${!1}
212
213 [[ ${var} =~ ^[0-9]+$ ]]
214}
215
216function ismac() {
217 local mac=${!1}
218
219 mac_is_valid ${mac}
220}
221
222function assert() {
223 local assertion="$@"
224
225 if ! ${assertion}; then
4c670d7c 226 error_log "Assertion '${assertion}' failed."
1848564d
MT
227 exit ${EXIT_ERROR}
228 fi
229
230 return ${EXIT_OK}
231}