Project

General

Profile

Download (17.7 KB) Statistics
| Branch: | Tag: | Revision:
1 17623ab5 Bill Marquette
<?php
2
/*
3 cdcea13f Seth Mos
  Copyright (C) 2008 Bill Marquette, Seth Mos
4 c1191d5b Ermal
  Copyright (C) 2010 Ermal Lu?i
5 17623ab5 Bill Marquette
  All rights reserved.
6
7
  Redistribution and use in source and binary forms, with or without
8
  modification, are permitted provided that the following conditions are met:
9
10 c1191d5b Ermal
  1. Redistributions of source code must retain the above copyright notice,
11 17623ab5 Bill Marquette
  this list of conditions and the following disclaimer.
12
13
  2. Redistributions in binary form must reproduce the above copyright
14
  notice, this list of conditions and the following disclaimer in the
15
  documentation and/or other materials provided with the distribution.
16
17
  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19
  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
  AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
  POSSIBILITY OF SUCH DAMAGE.
27
28 523855b0 Scott Ullrich
	pfSense_BUILDER_BINARIES:	/usr/bin/killall	/sbin/route	/usr/local/sbin/apinger
29
	pfSense_MODULE:	routing
30
31 c1191d5b Ermal
 */
32 17623ab5 Bill Marquette
33 3d471a14 Ermal
/*
34
 * Creates monitoring configuration file and
35
 * adds apropriate static routes.
36 cdcea13f Seth Mos
 */
37
function setup_gateways_monitor() {
38 3d471a14 Ermal
	global $config, $g;
39 ffe76308 Seth Mos
40 3d471a14 Ermal
	$gateways_arr = return_gateways_array();
41
	if (!is_array($gateways_arr)) {
42
		log_error("No gateways to monitor. Apinger will not be run.");
43
		killbypid("{$g['varrun_path']}/apinger.pid");
44 1ffa44bb Scott Ullrich
		// TEMPORARY XXX
45
		exec("/usr/bin/killall -9 apinger");
46 3d471a14 Ermal
		@unlink("{$g['tmp_path']}/apinger.status");
47
		return;
48
	}
49 cdcea13f Seth Mos
50 3d471a14 Ermal
	/* Default settings. Probably should move to globals.inc? */
51 023920e7 Ermal
	$a_settings = array();
52
	$a_settings['latencylow'] = "200";
53
	$a_settings['latencyhigh'] = "500";
54
	$a_settings['losslow'] = "10";
55
	$a_settings['losshigh'] = "20";
56 37c98ef9 Seth Mos
57 cdcea13f Seth Mos
	$fd = fopen("{$g['varetc_path']}/apinger.conf", "w");
58
	$apingerconfig = <<<EOD
59
60
# pfSense apinger configuration file. Automatically Generated!
61
62
## User and group the pinger should run as
63 0534d60a Chris Buechler
user "root"
64
group "wheel"
65 cdcea13f Seth Mos
66
## Mailer to use (default: "/usr/lib/sendmail -t")
67
#mailer "/var/qmail/bin/qmail-inject" 
68
69
## Location of the pid-file (default: "/var/run/apinger.pid")
70
pid_file "{$g['varrun_path']}/apinger.pid"
71
72
## Format of timestamp (%s macro) (default: "%b %d %H:%M:%S")
73
#timestamp_format "%Y%m%d%H%M%S"
74
75
status {
76
	## File where the status information whould be written to
77 298e5e0a Ermal
	file "{$g['tmp_path']}/apinger.status"
78 cdcea13f Seth Mos
	## Interval between file updates
79
	## when 0 or not set, file is written only when SIGUSR1 is received
80 ecb08c6a Ermal
	interval 5s
81 cdcea13f Seth Mos
}
82
83
########################################
84
# RRDTool status gathering configuration
85
# Interval between RRD updates
86
rrd interval 60s;
87
88
## These parameters can be overriden in a specific alarm configuration
89
alarm default { 
90 0ae6daf8 Ermal
	command on "/usr/local/sbin/pfSctl -c 'filter reload'"
91
	command off "/usr/local/sbin/pfSctl -c 'filter reload'"
92 cdcea13f Seth Mos
	combine 10s
93
}
94
95
## "Down" alarm definition. 
96
## This alarm will be fired when target doesn't respond for 30 seconds.
97
alarm down "down" {
98
	time 10s
99
}
100
101
## "Delay" alarm definition. 
102
## This alarm will be fired when responses are delayed more than 200ms
103
## it will be canceled, when the delay drops below 100ms
104
alarm delay "delay" {
105 37c98ef9 Seth Mos
	delay_low {$a_settings['latencylow']}ms
106
	delay_high {$a_settings['latencyhigh']}ms
107 cdcea13f Seth Mos
}
108
109
## "Loss" alarm definition. 
110
## This alarm will be fired when packet loss goes over 20%
111
## it will be canceled, when the loss drops below 10%
112
alarm loss "loss" {
113 37c98ef9 Seth Mos
	percent_low {$a_settings['losslow']}
114
	percent_high {$a_settings['losshigh']}
115 cdcea13f Seth Mos
}
116
117
target default {
118
	## How often the probe should be sent	
119
	interval 1s
120
	
121
	## How many replies should be used to compute average delay 
122
	## for controlling "delay" alarms
123
	avg_delay_samples 10
124
	
125
	## How many probes should be used to compute average loss
126
	avg_loss_samples 50
127
128
	## The delay (in samples) after which loss is computed
129
	## without this delays larger than interval would be treated as loss
130
	avg_loss_delay_samples 20
131
132
	## Names of the alarms that may be generated for the target
133
	alarms "down","delay","loss"
134
135
	## Location of the RRD
136 1bce2729 Seth Mos
	#rrd file "{$g['vardb_path']}/rrd/apinger-%t.rrd"
137 cdcea13f Seth Mos
}
138
139
EOD;
140
141 3d471a14 Ermal
	foreach($gateways_arr as $name => $gateway) {
142
		if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) {
143
			if (is_ipaddr($gateway['gateway']))
144
				$gateway['monitor'] = $gateway['gateway'];
145
			else /* No chance to get an ip to monitor skip target. */
146 f31ab121 Ermal
				continue;
147 3d471a14 Ermal
		}
148 f31ab121 Ermal
149 3d471a14 Ermal
		/* Interface ip is needed since apinger will bind a socket to it. */
150 640b3a9a Seth Mos
		if (is_ipaddrv4($gateway['gateway'])) {
151
			$gwifip = find_interface_ip($gateway['interface'], true);
152
		}
153
		if (is_ipaddrv6($gateway['gateway'])) {
154
			$gwifip = find_interface_ipv6($gateway['interface'], true);
155
		}
156 3d471a14 Ermal
		if (!is_ipaddr($gwifip))
157
			continue; //Skip this target
158
159 f44f8eb5 Ermal
		$apingercfg = "target \"{$gateway['monitor']}\" {\n";
160 68f291ff Ermal
		$apingercfg .= "	description \"{$name}\"\n";
161 3d471a14 Ermal
		$apingercfg .= "	srcip \"{$gwifip}\"\n";
162
		$alarms = "";
163 f44f8eb5 Ermal
		$alarmscfg = "";
164 3d471a14 Ermal
		$override = false;
165
		if (!empty($gateway['lowloss'])) {
166 68f291ff Ermal
			$alarmscfg .= "alarm loss \"{$name}loss\" {\n";
167 3d471a14 Ermal
			$alarmscfg .= "\tpercent_low {$gateway['losslow']}\n";
168
       			$alarmscfg .= "\tpercent_high {$gateway['losshigh']}\n";
169
			$alarmscfg .= "}\n";
170 68f291ff Ermal
			$alarms .= "\"{$name}loss\"";
171 3d471a14 Ermal
			$override = true;
172
		} else {
173 023920e7 Ermal
			if ($override == true)
174 3d471a14 Ermal
				$alarms .= ",";
175
			$alarms .= "\"loss\"";
176
			$override = true;
177
		}
178
		if (!empty($gateway['latencylow'])) {
179 68f291ff Ermal
			$alarmscfg .= "alarm delay \"{$name}delay\" {\n";
180 3d471a14 Ermal
			$alarmscfg .= "\tdelay_low {$gateway['latencylow']}ms\n";
181
			$alarmscfg .= "\tdelay_high {$gateway['latencyhigh']}ms\n";
182
			$alarmscfg .= "}\n";
183
			if ($override == true)
184
				$alarms .= ",";
185 68f291ff Ermal
			$alarms .= "\"{$name}delay\"";
186 3d471a14 Ermal
			$override = true;
187
		} else {
188
			if ($override == true)
189
				$alarms .= ",";
190
			$alarms .= "\"delay\"";
191
			$override = true;
192
		}
193
		if (!empty($gateway['down'])) {
194 68f291ff Ermal
			$alarmscfg .= "alarm down \"{$name}down\" {\n";
195 3d471a14 Ermal
			$alarmscfg .= "\ttime {$gateway['down']}s\n";
196
			$alarmscfg .= "}\n";
197
			if ($override == true)
198
				$alarms .= ",";
199 68f291ff Ermal
			$alarms .= "\"{$name}down\"";
200 3d471a14 Ermal
			$override = true;
201
		} else {
202
			if ($override == true)
203
				$alarms .= ",";
204
			$alarms .= "\"down\"";
205
			$override = true;
206 cdcea13f Seth Mos
		}
207 3d471a14 Ermal
		if ($override == true)
208
			$apingercfg .= "\talarms override {$alarms};\n";
209
210
		$apingercfg .= "	rrd file \"{$g['vardb_path']}/rrd/{$gateway['name']}-quality.rrd\"\n";
211
		$apingercfg .= "}\n";
212
		$apingercfg .= "\n";
213
		/*
214
		 * If the gateway is the same as the monitor we do not add a
215
		 * route as this will break the routing table.
216
		 * Add static routes for each gateway with their monitor IP
217
		 * not strictly necessary but is a added level of protection.
218
		 */
219
		if (is_ipaddr($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
220
			log_error("Removing static route for monitor {$gateway['monitor']} and adding a new route through {$gateway['gateway']}");
221
			mwexec("/sbin/route delete -host " . escapeshellarg($gateway['monitor']), true);
222
			mwexec("/sbin/route add -host " . escapeshellarg($gateway['monitor']) .
223
				" " . escapeshellarg($gateway['gateway']));
224
		}
225
226 023920e7 Ermal
		$apingerconfig .= $alarmscfg;
227
		$apingerconfig .= $apingercfg;
228 cdcea13f Seth Mos
	}
229
	fwrite($fd, $apingerconfig);
230
	fclose($fd);
231
232 20f26a50 Ermal
	killbypid("{$g['varrun_path']}/apinger.pid");
233 1ffa44bb Scott Ullrich
	// TEMPORARY XXX
234
	exec("/usr/bin/killall -9 apinger");
235 20f26a50 Ermal
	if (is_dir("{$g['tmp_path']}"))
236
		chmod("{$g['tmp_path']}", 01777);
237 17649c87 Ermal
	if (!is_dir("{$g['vardb_path']}/rrd"))
238 21934843 jim-p
		mkdir("{$g['vardb_path']}/rrd", 0775);
239 17649c87 Ermal
240
	@chown("{$g['vardb_path']}/rrd", "nobody");
241
242 20f26a50 Ermal
	/* start a new apinger process */
243 298e5e0a Ermal
	@unlink("{$g['tmp_path']}/apinger.status");
244 20f26a50 Ermal
	mwexec_bg("/usr/local/sbin/apinger -c {$g['varetc_path']}/apinger.conf");
245 4f060616 Ermal
246 cdcea13f Seth Mos
	return 0;
247
}
248
249
/* return the status of the apinger targets as a array */
250 3d471a14 Ermal
function return_gateways_status($byname = false) {
251 ae9618af Ermal
	global $config, $g;
252 cdcea13f Seth Mos
253
	$apingerstatus = array();
254 d9dda2a5 Ermal
	if (file_exists("{$g['tmp_path']}/apinger.status")) {
255 cdcea13f Seth Mos
		$apingerstatus = file("{$g['tmp_path']}/apinger.status");
256 2328dcc5 Seth Mos
	}
257 cdcea13f Seth Mos
258 67ee1ec5 Ermal Luçi
	$status = array();
259 cdcea13f Seth Mos
	foreach($apingerstatus as $line) {
260 75466131 Ermal
		$info = explode("|", $line);
261 3d471a14 Ermal
		if ($byname == false)
262
			$target = $info[0];
263
		else
264
			$target = $info[2];
265
		$status[$target]['monitorip'] = $info[0];
266 79b7f498 Ermal
		$status[$target]['srcip'] = $info[1];
267
		$status[$target]['name'] = $info[2];
268
		$status[$target]['lastcheck'] = $info[5] ? date('r', $info[5]) : date('r');
269
		$status[$target]['delay'] = empty($info[6]) ? 0 : $info[6];
270
		$status[$target]['loss'] = empty($info[7]) ? "0.0%" : $info[7] . "";
271
		$status[$target]['status'] = trim($info[8]);
272 cdcea13f Seth Mos
	}
273 68f291ff Ermal
274 cdcea13f Seth Mos
	return($status);
275 17623ab5 Bill Marquette
}
276
277 2328dcc5 Seth Mos
/* Return all configured gateways on the system */
278 c795339e Ermal Lu?i
function return_gateways_array($disabled = false) {
279 8d3556c2 gnhb
	global $config, $g;
280 7901dff5 Ermal Luçi
281 c66b4242 Seth Mos
	$gateways_arr = array();
282
283 01ba0a72 Ermal Lu?i
	$i = 0;
284 3d471a14 Ermal
	/* Process/add all the configured gateways. */
285 ef05ae5f Ermal
	if (is_array($config['gateways']['gateway_item'])) {
286 2328dcc5 Seth Mos
		foreach($config['gateways']['gateway_item'] as $gateway) {
287 78ae2b14 Ermal
			if(empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
288 2328dcc5 Seth Mos
				$gateway['gateway'] = get_interface_gateway($gateway['interface']);
289
				/* no IP address found, set to dynamic */
290 3d471a14 Ermal
				if(! is_ipaddr($gateway['gateway']))
291 2328dcc5 Seth Mos
					$gateway['gateway'] = "dynamic";
292 a2532739 Ermal Lu?i
				$gateway['dynamic'] = true;
293 2328dcc5 Seth Mos
			}
294 3d471a14 Ermal
			if(empty($gateway['monitor']))
295 2328dcc5 Seth Mos
				$gateway['monitor'] = $gateway['gateway'];
296 3d471a14 Ermal
297 883c53c9 Ermal Lu?i
			$gateway['friendlyiface'] = $gateway['interface'];
298 3d471a14 Ermal
			$gateway['interface'] = get_real_interface($gateway['interface']);
299 bb8f919b Ermal
			/* Some interface like wan might be default but have no info recorded 
300
			 * the config.
301
			 */
302
			if ($gateway['friendlyiface'] == "wan" && !isset($gateway['defaultgw'])) {
303
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgw"))
304
					$gateway['defaultgw'] = true;
305
			}
306 3d471a14 Ermal
			/* include the gateway index as the attribute */
307
			$gateway['attribute'] = $i;
308 2328dcc5 Seth Mos
309
			$gateways_arr[$gateway['name']] = $gateway;
310
			$i++;
311
		}
312 883c53c9 Ermal Lu?i
	} 
313
314 ef05ae5f Ermal
	/* Loop through all interfaces with a gateway and add it to a array */
315
	if ($disabled == false)
316
		$iflist = get_configured_interface_with_descr();
317
	else
318
		$iflist = get_configured_interface_with_descr(false, true);
319
320 3d471a14 Ermal
	/* Process/add dynamic gateways. */
321 883c53c9 Ermal Lu?i
	foreach($iflist as $ifname => $friendly ) {
322 3d471a14 Ermal
		if(! interface_has_gateway($ifname))
323 883c53c9 Ermal Lu?i
			continue;
324 3d471a14 Ermal
325 86df2846 jim-p
		if (empty($config['interfaces'][$ifname]))
326
			continue;
327
328
		$ifcfg = &$config['interfaces'][$ifname];
329 e62fe748 Ermal
		if (!empty($ifcfg['ipaddr']) && is_ipaddr($ifcfg['ipaddr']))
330
			continue;
331
332 883c53c9 Ermal Lu?i
		$gateway = array();
333
		$gateway['dynamic'] = false;
334
		$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
335
		$gateway['interface'] = get_real_interface($ifname);
336
		$gateway['friendlyiface'] = $ifname;
337 ef05ae5f Ermal
		$gateway['name'] = $friendly;
338 883c53c9 Ermal Lu?i
		$gateway['attribute'] = "system";
339 2328dcc5 Seth Mos
	
340 6adc8e32 gnhb
		if ($gateway['dynamic'] === "default") {
341 999111cb Ermal
			$gateway['defaultgw'] = true;
342
			$gateway['dynamic'] = true;
343
		}
344 883c53c9 Ermal Lu?i
		/* Loopback dummy for dynamic interfaces without a IP */
345 999111cb Ermal
		if (!is_ipaddr($gateway['gateway']) && $gateway['dynamic'] == true)
346
			$gateway['gateway'] = "dynamic";
347 3439b033 Seth Mos
348 883c53c9 Ermal Lu?i
		/* automatically skip known static and dynamic gateways we have a array entry for */
349
		foreach($gateways_arr as $gateway_item) {
350 1ced293c gnhb
			if (($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) ||
351
				($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true))
352 7bed8222 Ermal
					continue 2;
353 883c53c9 Ermal Lu?i
		}
354 c66b4242 Seth Mos
355 e0df9716 gnhb
		if (is_ipaddr($gateway['gateway']))
356 883c53c9 Ermal Lu?i
			$gateway['monitor'] = $gateway['gateway'];
357 c65e1e0d Ermal
358
		$gateway['descr'] = "Interface {$friendly} Dynamic Gateway";
359
		$gateways_arr[$friendly] = $gateway;
360 c66b4242 Seth Mos
	}
361 883c53c9 Ermal Lu?i
362 c66b4242 Seth Mos
	return($gateways_arr);
363
}
364
365 3d471a14 Ermal
/*
366
 * Return an array with all gateway groups with name as key
367 fd8eeda5 Seth Mos
 * All gateway groups will be processed before returning the array.
368 3d471a14 Ermal
 */
369 fd8eeda5 Seth Mos
function return_gateway_groups_array() {
370 962625aa Ermal
	global $config, $g;
371 fd8eeda5 Seth Mos
372
	/* fetch the current gateways status */
373 3d471a14 Ermal
	$gateways_status = return_gateways_status(true);
374 fd8eeda5 Seth Mos
	$gateways_arr = return_gateways_array();
375
	$gateway_groups_array = array();
376
377
	if (is_array($config['gateways']['gateway_group'])) {
378
		foreach($config['gateways']['gateway_group'] as $group) {
379
			/* create array with group gateways members seperated by tier */
380
			$tiers = array();
381 3d471a14 Ermal
			$backupplan = array();
382 fd8eeda5 Seth Mos
			foreach($group['item'] as $item) {
383
				$itemsplit = explode("|", $item);
384
				$tier = $itemsplit[1];
385
				$gwname = $itemsplit[0];
386 3d471a14 Ermal
387
				/* Do it here rather than reiterating again the group in case no member is up. */
388 d8bf779b Ermal
				$backupplan[$tier][] = $gwname;
389 3d471a14 Ermal
390 fd8eeda5 Seth Mos
				/* check if the gateway is available before adding it to the array */
391 54b78cc1 Ermal
				if (!empty($gateways_status[$gwname])) {
392
					$status = $gateways_status[$gwname];
393 3d471a14 Ermal
					$gwdown = false;
394 9af4b31b Ermal
					if (stristr($status['status'], "down")) {
395 3d471a14 Ermal
						$msg = "MONITOR: {$gwname} has high latency, removing from routing group";
396
						$gwdown = true;
397
					} else if (stristr($status['status'], "loss") && strstr($group['trigger'], "loss")) {
398
						/* packet loss */
399
						$msg = "MONITOR: {$gwname} has packet loss, removing from routing group";
400
						$gwdown = true;
401
					} else if (stristr($status['status'], "delay") && strstr($group['trigger'] , "latency")) {
402
						/* high latency */
403
						$msg = "MONITOR: {$gwname} has high latency, removing from routing group";
404
						$gwdown = true;
405
					}
406
					if ($gwdown == true) {
407 9f1d61d3 Scott Ullrich
						log_error($msg);
408
						notify_via_growl($msg);
409 3d471a14 Ermal
					} else
410 857ce5f3 Seth Mos
						/* Online add member */
411
						$tiers[$tier][] = $gwname;
412 fd8eeda5 Seth Mos
				}
413
			}
414
			$tiers_count = count($tiers);
415
			if($tiers_count == 0) {
416
				/* Oh dear, we have no members! Engage Plan B */
417 3d471a14 Ermal
				$msg = "Gateways status could not be determined, considering all as up/active.";
418 9f1d61d3 Scott Ullrich
				log_error($msg);
419
				notify_via_growl($msg);
420 3d471a14 Ermal
				$tiers = $backupplan;
421 fd8eeda5 Seth Mos
			}
422 09ae0c17 Seth Mos
			/* sort the tiers array by the tier key */
423
			ksort($tiers);
424 3d471a14 Ermal
425 fd8eeda5 Seth Mos
			/* we do not really foreach the tiers as we stop after the first tier */
426 fe22a89b Ermal
			foreach($tiers as $tier) {
427 fd8eeda5 Seth Mos
				/* process all gateways in this tier */
428 fe22a89b Ermal
				foreach($tier as $member) {
429 fd8eeda5 Seth Mos
					/* determine interface gateway */
430 5f53260a Ermal
					if (isset($gateways_arr[$member])) {
431
						$gateway = $gateways_arr[$member];
432
						$int = $gateway['interface'];
433
						$gatewayip = "";
434
						if(is_ipaddr($gateway['gateway'])) 
435
							$gatewayip = $gateway['gateway'];
436
						else if ($int <> "")
437
							$gatewayip = get_interface_gateway($gateway['friendlyiface']);
438 3d471a14 Ermal
					
439
						if (($int <> "") && is_ipaddr($gatewayip)) {
440
							$groupmember = array();
441
							$groupmember['int']  = $int;
442
							$groupmember['gwip']  = $gatewayip;
443
							$groupmember['weight']  = isset($gateway['weight']) ? $gateway['weight'] : 1;
444
							$gateway_groups_array[$group['name']][] = $groupmember;
445
						}
446 fd8eeda5 Seth Mos
					}
447
				}
448
				/* we should have the 1st available tier now, exit stage left */
449
				break;
450
			}
451
		}
452
	}
453 fe22a89b Ermal
	return ($gateway_groups_array);
454 fd8eeda5 Seth Mos
}
455
456 2468ae76 Scott Ullrich
/* Update DHCP WAN Interface ip address in gateway group item */
457
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
458
	global $config, $g;
459
	foreach($config['gateways']['gateway_item'] as & $gw) {	
460
		if($gw['interface'] == $interface) {
461
			$current_gw = get_interface_gateway($interface);
462
			if($gw['gateway'] <> $current_gw) {
463
				$gw['gateway'] = $current_gw;
464
				$changed = true;
465
			}
466
		}
467
	}
468
	if($changed && $current_gw)
469
		write_config("Updating gateway group gateway for $interface - new gateway is $current_gw");
470
}
471 fd8eeda5 Seth Mos
472 6dc3a5c2 Ermal Lu?i
function lookup_gateway_ip_by_name($name) {
473 fe22a89b Ermal
474 66a96e72 Ermal
	$gateways_arr = return_gateways_array();
475 68f291ff Ermal
        foreach ($gateways_arr as $gname => $gw) {
476
                if ($gw['name'] == $name || $gname == $name)
477 6f77aca5 Ermal
                        return $gw['gateway'];
478
        }
479 66a96e72 Ermal
480
	return false;
481 6dc3a5c2 Ermal Lu?i
}
482
483
function lookup_gateway_monitor_ip_by_name($name) {
484 fe22a89b Ermal
485 6dc3a5c2 Ermal Lu?i
        $gateways_arr = return_gateways_array();
486 fe22a89b Ermal
	if (!empty($gateways_arr[$name])) {
487
		$gateway = $gateways_arr[$name];
488 68f291ff Ermal
		if(!is_ipaddr($gateway['monitor']))
489
			return $gateway['gateway'];
490 6dc3a5c2 Ermal Lu?i
491 9fd19334 gnhb
		return $gateway['monitor'];
492 6dc3a5c2 Ermal Lu?i
        }
493 fe22a89b Ermal
494
        return (false);
495 6dc3a5c2 Ermal Lu?i
}
496
497
function lookup_gateway_interface_by_name($name) {
498
499 fe22a89b Ermal
        $gateways_arr = return_gateways_array();
500
	if (!empty($gateways_arr[$name])) {
501
		$interfacegw = $gateway['interface'];
502
		return ($interfacegw);
503 6dc3a5c2 Ermal Lu?i
        }
504 fe22a89b Ermal
505
        return (false);
506 6dc3a5c2 Ermal Lu?i
}
507
508 a2532739 Ermal Lu?i
function get_interface_gateway($interface, &$dynamic = false) {
509 5a5413bb Seth Mos
	global $config, $g;
510 6dc3a5c2 Ermal Lu?i
511 5a5413bb Seth Mos
	$gw = NULL;
512 6dc3a5c2 Ermal Lu?i
513 5a5413bb Seth Mos
	$gwcfg = $config['interfaces'][$interface];
514
	if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
515
		foreach($config['gateways']['gateway_item'] as $gateway) {
516
			if(($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
517
				$gw = $gateway['gateway'];
518 66a96e72 Ermal
				break;
519
			}
520 5a5413bb Seth Mos
		}
521 66a96e72 Ermal
	}
522 6dc3a5c2 Ermal Lu?i
523 5a5413bb Seth Mos
	// for dynamic interfaces we handle them through the $interface_router file.
524
	if (!is_ipaddr($gw) && !is_ipaddr($gwcfg['ipaddr'])) {
525
		$realif = get_real_interface($interface);
526
		if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
527
				$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_router"), " \n");
528
			$dynamic = true;
529
		}
530
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgw"))
531
			$dynamic = "default";
532 d62b164a gnhb
			
533 5a5413bb Seth Mos
	}
534
535
	/* return gateway */
536
	return ($gw);
537
}
538
539
function get_interface_gateway_v6($interface, &$dynamic = false) {
540
	global $config, $g;
541 6dc3a5c2 Ermal Lu?i
542 5a5413bb Seth Mos
	$gw = NULL;
543
	$gwcfg = $config['interfaces'][$interface];
544
	if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
545
		foreach($config['gateways']['gateway_item'] as $gateway) {
546 640b3a9a Seth Mos
			if(($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
547 5a5413bb Seth Mos
				$gw = $gateway['gateway'];
548
				break;
549
			}
550
		}
551
	}
552
553
	// for dynamic interfaces we handle them through the $interface_router file.
554
	if (!is_ipaddrv6($gw) && !is_ipaddr($gwcfg['ipaddrv6'])) {
555
			$realif = get_real_interface($interface);
556
			if (file_exists("{$g['tmp_path']}/{$realif}_routerv6")) {
557
				$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_routerv6"), " \n");
558
				$dynamic = true;
559
			}
560
			if (file_exists("{$g['tmp_path']}/{$realif}_defaultgwv6"))
561
				$dynamic = "default";
562
			
563
	}
564
	/* return gateway */
565
	return ($gw);
566 6dc3a5c2 Ermal Lu?i
}
567
568 315e1253 Ermal
?>