Project

General

Profile

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

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

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

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

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

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

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

    
44
*/
45

    
46
require_once("functions.inc");
47

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

    
175

    
176
?>
(7-7/14)