]> git.ipfire.org Git - people/stevee/network.git/blob - src/header-port
Convert HOOK_SETTINGS into an array
[people/stevee/network.git] / src / header-port
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 INFO_SETTINGS="HOOK PORT_PARENTS PORT_CHILDREN"
23
24 HOOK_PORT_PATTERN="${PORT_PATTERN}"
25
26 # This function is called after a device has been plugged
27 # into the system and got its correct name.
28 # The function is intended to create child ports and things
29 # like that.
30 hook_hotplug() {
31 # If the hook does not handle the hotplug event, it
32 # must return EXIT_NOT_HANDLED.
33 exit ${EXIT_NOT_HANDLED}
34 }
35
36 # This function gets called when a device is plugged in
37 # to determine the right name.
38 # The first argument is the port which should be tested
39 # against the second argument which is the device that
40 # has been plugged in.
41 hook_hotplug_rename() {
42 exit ${EXIT_FALSE}
43 }
44
45 hook_default_new() {
46 local ${HOOK_SETTINGS[*]}
47
48 # Import all default variables
49 hook_set_defaults
50
51 if ! hook_parse_cmdline "$@"; then
52 return ${EXIT_ERROR}
53 fi
54
55 assert isset HOOK_PORT_PATTERN
56
57 local port=$(port_find_free ${HOOK_PORT_PATTERN})
58 assert isset port
59
60 port_settings_write "${port}" ${HOOK_SETTINGS[*]}
61
62 exit ${EXIT_OK}
63 }
64
65 hook_new() {
66 hook_default_new "$@"
67 }
68
69 hook_default_edit() {
70 local port=${1}
71 assert isset port
72 shift
73
74 # Read settings
75 if ! port_settings_read "${port}" ${HOOK_SETTINGS[*]}; then
76 error "Could not read settings for port ${port}"
77 return ${EXIT_ERROR}
78 fi
79
80 # Parse command line arguments
81 if ! hook_parse_cmdline "$@"; then
82 return ${EXIT_ERROR}
83 fi
84
85 # Save settings
86 if ! port_settings_write "${port}" ${HOOK_SETTINGS[*]}; then
87 error "Could not write settings for port ${port}"
88 return ${EXIT_ERROR}
89 fi
90
91 # Apply settings
92 port_restart "${port}"
93
94 return ${EXIT_OK}
95 }
96
97 hook_edit() {
98 hook_default_edit "$@"
99 }
100
101 # Returns a list of all children of this port
102 hook_children() {
103 local port="${1}"
104
105 if ! port_settings_read "${port}" ${HOOK_SETTINGS[*]}; then
106 log ERROR "Could not read port settings: ${port}"
107 return ${EXIT_OK}
108 fi
109
110 print "${SLAVES}"
111 }
112
113 hook_status() {
114 local port="${1}"
115 assert isset port
116
117 cli_device_headline "${port}" --long
118 exit ${EXIT_OK}
119 }
120
121 # Create any virtual devices, but don't bring them up
122 # Must tolerate that the device may already exist
123 hook_create() {
124 cmd_not_implemented
125 }
126
127 # Must tolerate that the device may not exist
128 hook_remove() {
129 cmd_not_implemented
130 }
131
132 # Just bring up the device
133 hook_default_up() {
134 local port="${1}"
135 assert isset port
136
137 if ! device_exists "${port}"; then
138 log ERROR "Port '${port}' does not exist and cannot be brought up"
139 exit ${EXIT_ERROR}
140 fi
141
142 # Bring up the port
143 device_set_up "${port}"
144
145 # Bring up all slaves if the port has any
146 local slave
147 for slave in $(port_get_slaves "${port}"); do
148 port_up "${slave}"
149 done
150 }
151
152 # Depends on the port existing
153 hook_up() {
154 hook_default_up "$@"
155 }
156
157 hook_default_down() {
158 local port="${1}"
159 assert isset port
160
161 if device_exists "${port}"; then
162 device_set_down "${port}"
163 fi
164
165 # Bring down all slaves if the port has any
166 local slave
167 for slave in $(port_get_slaves "${port}"); do
168 port_down "${slave}"
169 done
170 }
171
172 hook_down() {
173 hook_default_down "$@"
174 }