]> git.ipfire.org Git - people/stevee/network.git/blame - functions.ports
network: Some code cleanup.
[people/stevee/network.git] / functions.ports
CommitLineData
711ffac1 1#!/bin/bash
1578dae9
MT
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###############################################################################
711ffac1
MT
21
22function port_dir() {
23 echo "${CONFIG_DIR}/ports"
24}
25
26function port_file() {
27 local port=${1}
28
29 assert isset port
30
31 echo "$(port_dir)/${port}"
32}
33
34function port_exists() {
35 local port=${1}
36
37 [ -f "${CONFIG_DIR}/ports/${port}" ]
38}
39
40function port_get_hook() {
41 local port=${1}
42
43 assert isset port
44
45 config_get_hook $(port_file ${port})
46}
47
48function port_is_attached() {
49 local port=${1}
50 shift
51
52 assert isset port
53
54 local zone
55 for zone in $(zones_get_all); do
56
57 assert isset zone
58 assert zone_exists ${zone}
59
60 if listmatch ${port} $(zone_get_ports ${zone}); then
61 echo "${zone}"
62 return ${EXIT_OK}
63 fi
64 done
65
66 return ${EXIT_ERROR}
67}
68
69function port_create() {
70 #local port=${1}
71 #shift
72 #
73 #if port_exists ${port}; then
74 # error "Port '${port}' does already exist."
75 # return ${EXIT_ERROR}
76 #fi
77
78 local hook=${1}
79 shift
80
81 if ! hook_exists port ${hook}; then
82 error "Port hook '${hook}' does not exist."
83 return ${EXIT_ERROR}
84 fi
85
86 #port_edit ${port} ${hook} $@
87 #
88 #if [ $? -ne ${EXIT_OK} ]; then
89 # port_destroy ${port}
90 #fi
91
92 hook_exec port ${hook} create $@
93}
94
95function port_destroy() {
96 local port=${1}
97
98 assert isset port
99
100 port_exists ${port} || return ${EXIT_OK}
101
102 local attached_zone=$(port_is_attached ${port})
103
104 if [ -n "${attached_zone}" ]; then
105 error "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
106 return ${EXIT_ERROR}
107 fi
108
109 port_down ${port}
110
111 rm -f $(port_file ${port})
112}
113
f90e550b
MT
114function port_remove() {
115 port_destroy $@
116}
117
711ffac1
MT
118function port_edit() {
119 port_cmd edit $@
120}
121
122# XXX? Compatibility function
123function port_show() {
124 port_status $@
125}
126
127function port_up() {
128 port_cmd up $@
129}
130
131function port_down() {
132 port_cmd down $@
133}
134
135function port_status() {
136 port_cmd status $@
137}
138
139function port_cmd() {
140 local cmd=${1}
141 local port=${2}
142 shift 2
143
144 assert isset cmd
145 assert isset port
146
147 local hook=$(port_get_hook ${port})
148
149 assert isset hook
150
151 hook_exec port ${hook} ${cmd} ${port} $@
152}
f90e550b
MT
153
154function ports_get() {
155 local port
156 for port in $(port_dir)/*; do
157 port=$(basename ${port})
158 if port_exists ${port}; then
159 echo "${port}"
160 fi
161 done
162}
2ae0fb8d
MT
163
164# This function automatically creates the real ethernet devices
165# that do not exists in the configuration.
166# Saves some work for the administrator.
167function ports_init() {
168 local device
169 for device in $(devices_get_all); do
170 if device_is_real ${device}; then
171 if ! port_exists ${device}; then
172 port_create ethernet ${device}
173 fi
174 fi
175 done
176}
177
178init_register ports_init
d76f5107
MT
179
180function port_find_free() {
181 local pattern=${1}
182
183 assert isset pattern
184
185 local port
186 local i=0
187
188 while [ ${i} -lt 99 ]; do
189 port=${pattern//N/${i}}
190 if ! port_exists ${port} && ! device_exists ${port}; then
191 echo "${port}"
192 break
193 fi
194 i=$(( ${i} + 1 ))
195 done
196}