Project

General

Profile

Download (5.18 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	lb.php
5
	part of pfSense (www.pfSense.com)
6

    
7
	Copyright (C) 2005 Chris Dionissopoulos <chdio@bug.gr>
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
/*
33
global config data
34
~~~~~~~~~~~~~~~~~~
35
['lb'] --> unique,required
36
['lb']['enable'] --> unique , required, int(0|1)
37
['lb']['gateway'] --> ARRAY , optional
38
['lb']['gateway']['enable'] --> unique , required, int(0|1)
39
['lb']['gateway']['state']  --> unique , required, int(0|1)
40
['lb']['gateway']['gateway']  --> unique , required, string
41
['lb']['gateway']['monitor']  --> unique , required, string
42
['lb']['gateway']['weight']  --> unique , required, int(1..10)
43
['lb']['gateway']['default'] --> unique , optional, int(0|1)
44

    
45
*/
46

    
47
require_once("functions.inc");
48

    
49
	function addgateway($enable,$state,$gateway,$monitor,$weight,$default){
50
		global $config,$g;
51
		$exists = false;
52
		$lb = &$config['lb'];
53
		
54
		foreach($lb['gateway'] as $tgate)
55
			if ($gateway == $tgate['gateway']){
56
				$exists = true;
57
				break;
58
			}
59
		}
60
		if (!$exists) {
61
			$tgate = array();
62
			$tgate['enable'] = $enable;
63
			if ($enable) 
64
				$tgate['state'] = $this->getstate($monitor);
65
			else 
66
				$tgate['state'] = 0;
67
			$tgate['gateway'] = $gateway;
68
			$tgate['monitor'] = $monitor;
69
			$tgate['weight'] = $weight;
70
			if ($default==1) {
71
				$i=0;
72
				foreach($lb['gateway'] as $ttgate) {
73
					if (isset($this->lb['gateway'][$i]['default']))
74
						unset($this->lb['gateway'][$i]['default']);
75
					$i++;
76
				}
77
				$tgate['default'] = 1;
78
			} else 
79
				$tgate['default'] = 0;
80
			$lb['gateway'][] = $tgate;
81
			return true;
82
		} else {
83
			return "error: gateway exists!";
84
	}
85
		
86
	function edit_gateway($num,$enable,$state,$gateway,$monitor,$weight,$default){
87
		global $config,$g;
88
		$i=0;
89
		$exists = false;
90
		$lb = &$config['lb'];
91
		
92
		foreach($lb['gateway'] as $tgate)
93
			if ($gateway == $tgate['gateway'] && $i!=$num){
94
				$exists = true;
95
				break;
96
			}
97
			$i++;
98
		}
99
		if (!$exists) {
100
			$tgate = array();
101
			$tgate['enable'] = $enable;
102
			if ($enable) 
103
				$tgate['state'] = get_state($monitor);
104
			else 
105
				$tgate['state'] = 0;
106
			$tgate['gateway'] = $gateway;
107
			$tgate['monitor'] = $monitor;
108
			$tgate['weight'] = $weight;
109
			if ($default==1) {
110
				$i=0;
111
				foreach($lb['gateway'] as $ttgate){
112
					if (isset($lb['gateway'][$i]['default']))
113
						unset($lb['gateway'][$i]['default']);
114
					$i++;
115
				}
116
				$tgate['default'] = 1;
117
			} else 
118
				$tgate['default'] = 0;
119
			$lb['gateway'][$num] = $tgate;
120
			return true;
121
		} else {
122
			return "error: gateway exists!";
123
	}
124
	
125
	function get_state($ip){
126
		$pingcmd = exec("/usr/local/bin/sudo /sbin/ping -c1 -t1 -n ".$ip,$result);
127
		if (preg_match_all('/.*100\%\spacket\sloss.*/',$result[count($result)-1], $match))
128
			return 0;
129
		if (preg_match_all('/.*0\%\spacket\sloss.*/',$result[count($result)-2], $match))
130
			return 1;
131
	}
132
	
133
	function rules(){
134
		global $config,$g;
135
		$lb = &$config['lb'];
136
		$lancfg = $config['interfaces']['lan'];
137
		$wancfg = $config['interfaces']['wan'];
138
		$out_flow = ' ip from '.$lancfg['ipaddr'].'/'.$lancfg['subnet']
139
					.' to not '. $lancfg['ipaddr'].'/'.$lancfg['subnet'];
140
		$in_flow = 'ip from not '.$lancfg['ipaddr'].'/'.$lancfg['subnet']
141
					.' to '.$lancfg['ipaddr'].'/'.$lancfg['subnet'];
142
					
143
		$rules ="";
144
		$num = 1;
145
		$i=0;
146
		$gates = count($lb['gateway']);
147
		foreach($lb['gateway'] as $tgate){
148
			$num++;
149
			$in_num = 10000+$num;
150
			$out_num = 11000+$num;
151
			$skipto = 20000 + $num*10;
152
			$sw[$i] = 0;
153
			for($j=$i;$j<$gates;$j++)
154
				$sw[$i] +=$lb['gateway'][$j]['weight'];
155
			$prob = round($tgate['weight']/$sw[$i], 2);
156
					
157
			$rules .='/sbin/ipfw add $in_num set 5 skipto '.$skipto
158
					.' '.$in_flow.' mac any '.arp_get_mac_by_ip($tgate['gateway'])
159
					.' in recv '.$wancfg['if'].' keep-state\n';
160
					
161
			$rules .='/sbin/ipfw add $out_num set 6 prob '.$prob.' skipto '.$skipto
162
				.' '.$out_flow.' in recv '. $lancfg['if'].' keep-state\n';
163
					
164
			$rules .='/sbin/ipfw add $skipto set 6 fwd '.$tgate['gateway']
165
					.' '.$out_flow.' in recv '.$lancfg['if'].'\n';
166
					
167
			$skipto++;
168
			$rule .= '/sbin/ipfw add $skipto set 6 skipto 65535 ip from any to any\n';
169
			$i++;
170
		}
171
		return $rules;
172
	}
173
	
174
	
175

    
176

    
177
?>
(8-8/21)