Project

General

Profile

Download (5.21 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
		$result = "";
127
		$pingcmd = exec("/usr/local/bin/sudo /sbin/ping -c1 -t1 -n ".$ip,$result);
128
		$match = "";
129
		if (preg_match_all('/.*100\%\spacket\sloss.*/',$result[count($result)-1], $match))
130
			return 0;
131
		if (preg_match_all('/.*0\%\spacket\sloss.*/',$result[count($result)-2], $match))
132
			return 1;
133
	}
134
	
135
	function rules(){
136
		global $config,$g;
137
		$lb = &$config['lb'];
138
		$lancfg = $config['interfaces']['lan'];
139
		$wancfg = $config['interfaces']['wan'];
140
		$out_flow = ' ip from '.$lancfg['ipaddr'].'/'.$lancfg['subnet']
141
					.' to not '. $lancfg['ipaddr'].'/'.$lancfg['subnet'];
142
		$in_flow = 'ip from not '.$lancfg['ipaddr'].'/'.$lancfg['subnet']
143
					.' to '.$lancfg['ipaddr'].'/'.$lancfg['subnet'];
144
					
145
		$rules ="";
146
		$num = 1;
147
		$i=0;
148
		$gates = count($lb['gateway']);
149
		foreach($lb['gateway'] as $tgate){
150
			$num++;
151
			$in_num = 10000+$num;
152
			$out_num = 11000+$num;
153
			$skipto = 20000 + $num*10;
154
			$sw[$i] = 0;
155
			for($j=$i;$j<$gates;$j++)
156
				$sw[$i] +=$lb['gateway'][$j]['weight'];
157
			$prob = round($tgate['weight']/$sw[$i], 2);
158
					
159
			$rules .='/sbin/ipfw add $in_num set 5 skipto '.$skipto
160
					.' '.$in_flow.' mac any '.arp_get_mac_by_ip($tgate['gateway'])
161
					.' in recv '.$wancfg['if'].' keep-state\n';
162
					
163
			$rules .='/sbin/ipfw add $out_num set 6 prob '.$prob.' skipto '.$skipto
164
				.' '.$out_flow.' in recv '. $lancfg['if'].' keep-state\n';
165
					
166
			$rules .='/sbin/ipfw add $skipto set 6 fwd '.$tgate['gateway']
167
					.' '.$out_flow.' in recv '.$lancfg['if'].'\n';
168
					
169
			$skipto++;
170
			$rule .= '/sbin/ipfw add $skipto set 6 skipto 65535 ip from any to any\n';
171
			$i++;
172
		}
173
		return $rules;
174
	}
175
	
176
	
177

    
178

    
179
?>
(11-11/27)