Commit f440c7bfd6fb1b31f65f641a622b249cc0991e35
Exists in
master
and in
1 other branch
Merge branch 'master' of https://github.com/lightbase/cacic
Showing
3 changed files
with
125 additions
and
9 deletions
Show diff stats
src/Cacic/CommonBundle/Entity/RedeRepository.php
| ... | ... | @@ -4,6 +4,7 @@ namespace Cacic\CommonBundle\Entity; |
| 4 | 4 | |
| 5 | 5 | use Doctrine\ORM\EntityRepository; |
| 6 | 6 | use Symfony\Component\HttpFoundation\Request; |
| 7 | +use Cacic\WSBundle\Helper\IPv4; | |
| 7 | 8 | |
| 8 | 9 | /** |
| 9 | 10 | * |
| ... | ... | @@ -139,18 +140,22 @@ class RedeRepository extends EntityRepository |
| 139 | 140 | /* |
| 140 | 141 | * Método responsável por coletar verificar dados de rede |
| 141 | 142 | */ |
| 142 | - public function getDadosRedePreColeta( Request $request ) | |
| 143 | + public function getDadosRedePreColeta( $ip_computador, $netmask ) | |
| 143 | 144 | { |
| 144 | - //obtem IP da maquina coletada | |
| 145 | - $ip_computador = $request->get('te_ip_computador'); | |
| 146 | - $ip_computador = !empty( $ip_computador ) ?: $_SERVER['REMOTE_ADDR']; | |
| 147 | 145 | |
| 148 | 146 | //obtem IP da Rede que a maquina coletada pertence |
| 149 | - $ip = explode( '.', $ip_computador ); | |
| 150 | - $te_ip_rede = $ip[0].".".$ip[1].".".$ip[2].".0"; //Pega ip da REDE sendo esse X.X.X.0 | |
| 147 | + $cidr = IPv4::mask2cidr($netmask); | |
| 148 | + $ipv4 = new IPv4($ip_computador, $cidr); | |
| 151 | 149 | |
| 150 | + $te_ip_rede = $ipv4->network(); | |
| 151 | + //error_log("Endereço IP do computador: $ip_computador \nEndereço Broadcast da rede: $te_ip_rede"); | |
| 152 | 152 | $rede = $this->findOneBy( array( 'teIpRede'=> $te_ip_rede ) ); //procura rede |
| 153 | - $rede = empty( $rede ) ? $this->getPrimeiraRedeValida() : $rede ; // se rede não existir instancio uma nova rede | |
| 153 | + | |
| 154 | + // Se a rede não existir, procuro uma com endereço 0.0.0.0 | |
| 155 | + if (empty($rede)) { | |
| 156 | + $rede = $this->findOneBy( array( 'teIpRede'=> '0.0.0.0' ) ); | |
| 157 | + } | |
| 158 | + //$rede = empty( $rede ) ? $this->getPrimeiraRedeValida() : $rede ; // se rede não existir instancio uma nova rede | |
| 154 | 159 | |
| 155 | 160 | return $rede; |
| 156 | 161 | } | ... | ... |
src/Cacic/WSBundle/Controller/DefaultController.php
| ... | ... | @@ -69,12 +69,15 @@ class DefaultController extends Controller |
| 69 | 69 | $strOperatingSystem = OldCacicHelper::deCrypt( $request, $request->request->get('OperatingSystem') ); |
| 70 | 70 | |
| 71 | 71 | $te_node_address = TagValueHelper::getValueFromTags( 'MACAddress', $strNetworkAdapterConfiguration ); |
| 72 | + $netmask = TagValueHelper::getValueFromTags( 'IPSubnet', $strNetworkAdapterConfiguration ); | |
| 72 | 73 | $te_so = $request->get( 'te_so' ); |
| 73 | 74 | $ultimo_login = TagValueHelper::getValueFromTags( 'UserName' , $strComputerSystem); |
| 75 | + $ip_computador = $request->get('te_ip_computador'); | |
| 76 | + $ip_computador = !empty( $ip_computador ) ?: $_SERVER['REMOTE_ADDR']; | |
| 74 | 77 | |
| 75 | 78 | //vefifica se existe SO coletado se não, insere novo SO |
| 76 | 79 | $so = $this->getDoctrine()->getRepository('CacicCommonBundle:So')->createIfNotExist( $te_so ); |
| 77 | - $rede = $this->getDoctrine()->getRepository('CacicCommonBundle:Rede')->getDadosRedePreColeta( $request ); | |
| 80 | + $rede = $this->getDoctrine()->getRepository('CacicCommonBundle:Rede')->getDadosRedePreColeta( $ip_computador, $netmask ); | |
| 78 | 81 | $computador = $this->getDoctrine()->getRepository('CacicCommonBundle:Computador')->getComputadorPreCole( $request, $te_so, $te_node_address, $rede, $so ); |
| 79 | 82 | //$local = $this->getDoctrine()->getRepository('CacicCommonBundle:Local')->findOneBy(array( 'idLocal' => $rede->getIdLocal() )); |
| 80 | 83 | $local = $rede->getIdLocal(); |
| ... | ... | @@ -142,9 +145,14 @@ class DefaultController extends Controller |
| 142 | 145 | { |
| 143 | 146 | OldCacicHelper::autenticaAgente($request); |
| 144 | 147 | |
| 148 | + $strNetworkAdapterConfiguration = OldCacicHelper::deCrypt( $request, $request->get('NetworkAdapterConfiguration') ); | |
| 149 | + $netmask = TagValueHelper::getValueFromTags( 'IPSubnet', $strNetworkAdapterConfiguration ); | |
| 150 | + $ip_computador = $request->get('te_ip_computador'); | |
| 151 | + $ip_computador = !empty( $ip_computador ) ?: $_SERVER['REMOTE_ADDR']; | |
| 152 | + | |
| 145 | 153 | $te_node_adress = TagValueHelper::getValueFromTags( 'MACAddress', OldCacicHelper::deCrypt( $request, $request->get('NetworkAdapterConfiguration'))); |
| 146 | 154 | $so = $this->getDoctrine()->getRepository('CacicCommonBundle:So')->findOneBy( array('teSo'=>$request->get( 'te_so' ))); |
| 147 | - $rede = $this->getDoctrine()->getRepository('CacicCommonBundle:Rede')->getDadosRedePreColeta( $request ); | |
| 155 | + $rede = $this->getDoctrine()->getRepository('CacicCommonBundle:Rede')->getDadosRedePreColeta( $ip_computador, $netmask ); | |
| 148 | 156 | $computador = $this->getDoctrine()->getRepository('CacicCommonBundle:Computador')->getComputadorPreCole( $request, $request->get( 'te_so' ),$te_node_adress, $rede, $so ); |
| 149 | 157 | //$local = $this->getDoctrine()->getRepository('CacicCommonBundle:Local')->findOneBy(array( 'idLocal' => $rede->getIdLocal() )); |
| 150 | 158 | $local = $rede->getIdLocal(); | ... | ... |
| ... | ... | @@ -0,0 +1,103 @@ |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Created by PhpStorm. | |
| 4 | + * User: eduardo | |
| 5 | + * Date: 14/04/14 | |
| 6 | + * Time: 11:23 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +namespace Cacic\WSBundle\Helper; | |
| 10 | + | |
| 11 | +/****************************************** | |
| 12 | + * IPv4 Network Class | |
| 13 | + * Author: Mike Mackintosh | |
| 14 | + * Date: 12/27/2011 2121 | |
| 15 | + * | |
| 16 | + * @Usage: new IPv4 ('10.1.1.1', 28); | |
| 17 | + * | |
| 18 | + * Source: http://www.highonphp.com/tag/php-subnet-calculator | |
| 19 | + * | |
| 20 | + *****************************************/ | |
| 21 | + | |
| 22 | +class IPv4 | |
| 23 | +{ | |
| 24 | + private $ip_long; | |
| 25 | + public $ip; | |
| 26 | + public $cidr; | |
| 27 | + | |
| 28 | + function __construct($ip,$cidr) | |
| 29 | + { | |
| 30 | + $this->ip = $ip; | |
| 31 | + $this->ip_long = ip2long($ip); | |
| 32 | + $this->cidr = $cidr; | |
| 33 | + } | |
| 34 | + | |
| 35 | + function __toString(){ | |
| 36 | + $methods = get_class_methods($this); | |
| 37 | + | |
| 38 | + foreach($methods as $meth){ | |
| 39 | + if(substr($meth, 0, 2) != '__' && $meth != 'mask2cidr' && $meth != 'cidr2mask'){ | |
| 40 | + $r[] = $this->$meth(); | |
| 41 | + } | |
| 42 | + } | |
| 43 | + | |
| 44 | + return json_encode($r); | |
| 45 | + } | |
| 46 | + | |
| 47 | + static function mask2cidr($mask) | |
| 48 | + { | |
| 49 | + $mask = ip2long($mask); | |
| 50 | + $base = ((1<<32)-1); | |
| 51 | + return 32-log(($mask ^ $base)+1,2); | |
| 52 | + } | |
| 53 | + | |
| 54 | + static function cidr2mask($cidr) | |
| 55 | + { | |
| 56 | + $mask = ip2long('255.255.255.255'); | |
| 57 | + $base = ((1<<$cidr)-1); | |
| 58 | + return 32-log(($mask ^ $base)+1,2); | |
| 59 | + } | |
| 60 | + | |
| 61 | + function address(){ | |
| 62 | + return $this->ip; | |
| 63 | + } | |
| 64 | + | |
| 65 | + function cidr() { | |
| 66 | + return $this->cidr; | |
| 67 | + } | |
| 68 | + | |
| 69 | + function netmask() | |
| 70 | + { | |
| 71 | + $netmask = ((1<<32) -1) << (32-$this->cidr); | |
| 72 | + return long2ip($netmask); | |
| 73 | + } | |
| 74 | + | |
| 75 | + function network() | |
| 76 | + { | |
| 77 | + $network = $this->ip_long & (ip2long($this->netmask())); | |
| 78 | + return long2ip($network); | |
| 79 | + } | |
| 80 | + | |
| 81 | + function broadcast() | |
| 82 | + { | |
| 83 | + $broadcast = ip2long($this->network()) | (~(ip2long($this->netmask()))); | |
| 84 | + return long2ip($broadcast); | |
| 85 | + } | |
| 86 | + | |
| 87 | + function wildcard() | |
| 88 | + { | |
| 89 | + $inverse = ~(((1<<32) -1) << (32-$this->cidr)); | |
| 90 | + return long2ip($inverse); | |
| 91 | + } | |
| 92 | + | |
| 93 | + function availablehosts() | |
| 94 | + { | |
| 95 | + $hosts = (ip2long($this->broadcast()) - ip2long($this->network())) -1; | |
| 96 | + return $hosts; | |
| 97 | + } | |
| 98 | + | |
| 99 | + function availablenetworks() | |
| 100 | + { | |
| 101 | + return pow(2, 24)/($this->availablehosts()+2); | |
| 102 | + } | |
| 103 | +} | |
| 0 | 104 | \ No newline at end of file | ... | ... |