1 | <?php |
---|
2 | |
---|
3 | include_once("EncripDescrip.php"); |
---|
4 | |
---|
5 | /*================================================================================ |
---|
6 | Clase para conectarse con el servidor hidra y enviar comandos |
---|
7 | Cualquier error producido en los procesos se puede recuperar con los m�odos |
---|
8 | ================================================================================*/ |
---|
9 | class SockHidra{ |
---|
10 | var $ultimoerror; // Ultimo error detectado |
---|
11 | var $descripultimoerror; // Descripción del ltimo error detectado |
---|
12 | var $socket; // Stream socket |
---|
13 | var $servidor; // El servidor hidra |
---|
14 | var $puerto; // El puerto odnde se conectar� |
---|
15 | var $timeout; // El tiempo de espera para la conexi� |
---|
16 | var $encripdescrip; // El encriptador |
---|
17 | var $LONGITUD_TRAMA; // M�ima longitud de la trama |
---|
18 | |
---|
19 | //________________________________________________________________________________________ |
---|
20 | // |
---|
21 | // Constructor |
---|
22 | // Parámetros: |
---|
23 | // - servidor: El nombre o la IP del servidor |
---|
24 | // - puerto: El puerto usado para las comunicaciones |
---|
25 | // - timeout: El tiempo de espera para la conexi� |
---|
26 | //________________________________________________________________________________________ |
---|
27 | function SockHidra($servidor,$puerto,$timeout=30){ |
---|
28 | $this->servidor=$servidor; |
---|
29 | if (!$this->_esIP($this->servidor)) |
---|
30 | $this->servidor = gethostbyname ($servidor); |
---|
31 | $this->puerto=$puerto; |
---|
32 | $this->timeout=$timeout; |
---|
33 | $this->LONGITUD_TRAMA=4048; |
---|
34 | |
---|
35 | $this->encripdescrip=new EncripDescrip(); |
---|
36 | } |
---|
37 | //________________________________________________________________________________________ |
---|
38 | // |
---|
39 | // Averigua si el parametro pasado es una IP. devuelve true en caso afirmativo |
---|
40 | //________________________________________________________________________________________ |
---|
41 | function _esIP(){ |
---|
42 | return(false); |
---|
43 | } |
---|
44 | //________________________________________________________________________________________ |
---|
45 | // |
---|
46 | // Conecta con el servidor |
---|
47 | // Devuelve: |
---|
48 | // - false: Si falla la conexi� |
---|
49 | // - true: En caso contrario |
---|
50 | //________________________________________________________________________________________ |
---|
51 | function conectar(){ |
---|
52 | $this->socket = socket_create (AF_INET, SOCK_STREAM, 0); |
---|
53 | if ($this->socket < 0) { |
---|
54 | $this->ultimoerror=socket_strerror($socket); |
---|
55 | $this->descripultimoerror="socket_create() fallo"; |
---|
56 | return(false); |
---|
57 | } |
---|
58 | $result = socket_connect ($this->socket,$this->servidor,$this->puerto); |
---|
59 | if ($result < 0) { |
---|
60 | $this->ultimoerror=socket_strerror($result); |
---|
61 | $this->descripultimoerror="socket_connect() fallo"; |
---|
62 | return(false); |
---|
63 | } |
---|
64 | return(true); |
---|
65 | } |
---|
66 | //________________________________________________________________________________________ |
---|
67 | // |
---|
68 | // Cerrar la conexióncon el servidor |
---|
69 | // Devuelve: |
---|
70 | // - false: Si falla la conexi� |
---|
71 | // - true: En caso contrario |
---|
72 | //________________________________________________________________________________________ |
---|
73 | function desconectar(){ |
---|
74 | socket_close ($this->socket); |
---|
75 | } |
---|
76 | //________________________________________________________________________________________ |
---|
77 | // |
---|
78 | // Devuelve el c�igo del ltimo error ocurrido durante el proceso anterior. |
---|
79 | //________________________________________________________________________________________ |
---|
80 | function UltimoError(){ |
---|
81 | return($this->ultimoerror); |
---|
82 | } |
---|
83 | //________________________________________________________________________________________ |
---|
84 | // |
---|
85 | // Devuelve una cadena con el mensage del ltimo error ocurrido durante el proceso anterior. |
---|
86 | //________________________________________________________________________________________ |
---|
87 | function DescripUltimoError(){ |
---|
88 | return($this->descripultimoerror); |
---|
89 | } |
---|
90 | //________________________________________________________________________________________ |
---|
91 | // |
---|
92 | // Envia una petición de comando al servidor |
---|
93 | // Parámetros: |
---|
94 | // - Parámetros: Parámetros del mensaje |
---|
95 | //________________________________________________________________________________________ |
---|
96 | function envia_comando($parametros) |
---|
97 | { |
---|
98 | global $MSG_COMANDO; |
---|
99 | |
---|
100 | $tipo=$MSG_COMANDO; |
---|
101 | return($this->envia_trama($parametros,$tipo)); |
---|
102 | } |
---|
103 | //________________________________________________________________________________________ |
---|
104 | // |
---|
105 | // Envia una petición de información al servidor |
---|
106 | // Parámetros: |
---|
107 | // - Parámetros: Parámetros del mensaje |
---|
108 | //________________________________________________________________________________________ |
---|
109 | function envia_peticion($parametros) |
---|
110 | { |
---|
111 | global $MSG_PETICION; |
---|
112 | |
---|
113 | $tipo=$MSG_PETICION; |
---|
114 | return($this->envia_trama($parametros,$tipo)); |
---|
115 | } |
---|
116 | //________________________________________________________________________________________ |
---|
117 | // |
---|
118 | // Envia un mensaje al servidor |
---|
119 | // Parámetros: |
---|
120 | // - trama: Trama a enviar |
---|
121 | // - tipo: Tipo de mensaje |
---|
122 | //________________________________________________________________________________________ |
---|
123 | function envia_trama($parametros,$tipo) |
---|
124 | { |
---|
125 | global $LONHEXPRM; |
---|
126 | global $LONCABECERA; |
---|
127 | |
---|
128 | $arroba="@"; |
---|
129 | $identificador="JMMLCAMDJ_MCDJ"; |
---|
130 | |
---|
131 | $lonprm=strlen($parametros); |
---|
132 | /* Encripta los parámetros */ |
---|
133 | $parametros=$this->encripdescrip->Encriptar($parametros,$lonprm); |
---|
134 | /* Pasa a hexadecimal la longitud de los parámetros ya encriptados para incluirla dentro de la cabecera */ |
---|
135 | $hlonprm=str_pad(dechex($LONCABECERA+$LONHEXPRM+$lonprm),$LONHEXPRM,"0",STR_PAD_LEFT); // Rellena con ceros |
---|
136 | |
---|
137 | $trama=$arroba.$identificador.$tipo.$hlonprm.$parametros; |
---|
138 | $resul=socket_write($this->socket,$trama,$LONCABECERA+$LONHEXPRM+$lonprm); |
---|
139 | if (!$resul) { |
---|
140 | $this->ultimoerror=socket_strerror($resul); |
---|
141 | $this->descripultimoerror="socket_write() fallo"; |
---|
142 | return(false); |
---|
143 | } |
---|
144 | return(true); |
---|
145 | } |
---|
146 | //________________________________________________________________________________________ |
---|
147 | // |
---|
148 | // Recibe una trama del servidor |
---|
149 | // Parámetros: |
---|
150 | // - lon: Longitud de la trama |
---|
151 | // Devuelve: |
---|
152 | // La trama recibida |
---|
153 | //________________________________________________________________________________________ |
---|
154 | function recibe_respuesta() |
---|
155 | { |
---|
156 | global $LONHEXPRM; |
---|
157 | global $LONCABECERA; |
---|
158 | global $LONBLK; |
---|
159 | |
---|
160 | $lon=$lSize=0; |
---|
161 | $buffer=""; |
---|
162 | $cadenaret=""; |
---|
163 | do{ |
---|
164 | $bloque = socket_read ($this->socket,$LONBLK);// Lee bloque |
---|
165 | $buffer.=$bloque; // Añade bloque |
---|
166 | $lon+=strlen($bloque); |
---|
167 | if($lSize==0){ // Comprueba tipo de trama y longitud total de los parámetros |
---|
168 | if (substr($buffer,0,15)!="@JMMLCAMDJ_MCDJ") |
---|
169 | return($cadenaret); // No se reconoce la trama |
---|
170 | $hlonprm=hexdec(substr($buffer,$LONCABECERA,$LONHEXPRM)); |
---|
171 | $lSize=$hlonprm; // Longitud total de la trama con los parametros encriptados |
---|
172 | } |
---|
173 | }while($lon<$lSize); |
---|
174 | |
---|
175 | $lon=$lSize-($LONCABECERA+$LONHEXPRM); // Longitud de los parametros aún encriptados |
---|
176 | $parametros=substr($buffer,$LONCABECERA+$LONHEXPRM,$lon); // Parametros encriptados |
---|
177 | $parametros=$this->encripdescrip->Desencriptar($parametros,$hlonprm); // Parametros sin encriptar |
---|
178 | $hlonprm=str_pad(dechex($lon),$LONHEXPRM,"0",STR_PAD_LEFT); // Rellena con ceros |
---|
179 | $cadenaret=substr($buffer,0,$LONCABECERA).$hlonprm.$parametros; |
---|
180 | return($cadenaret); |
---|
181 | } |
---|
182 | } |
---|
183 | ?> |
---|