001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.functions.decision;
020
021import java.net.InetAddress;
022import java.net.UnknownHostException;
023
024import lucee.commons.lang.StringUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.ext.function.Function;
027
028/**
029 * Implements the CFML Function isleapyear
030 */
031public final class IsLocalHost implements Function {
032        
033        private static final long serialVersionUID = 5680807516948697186L;
034        
035        public static boolean call(PageContext pc , String ip) {
036                return invoke(ip);
037        }
038        public static boolean invoke(String ip) {
039                
040                if(StringUtil.isEmpty(ip,true)) return false;
041                ip=ip.trim().toLowerCase();
042                if(
043                                ip.equalsIgnoreCase("localhost") || 
044                                ip.equals("127.0.0.1") || 
045                                ip.equalsIgnoreCase("0:0:0:0:0:0:0:1") || 
046                                ip.equalsIgnoreCase("0:0:0:0:0:0:0:1%0") || 
047                                ip.equalsIgnoreCase("::1"))
048                        return true;
049
050                try {
051                        InetAddress addr = InetAddress.getByName(ip);
052                        InetAddress localHost = InetAddress.getLocalHost();
053                        if(localHost.equals(addr)) return true;
054                        
055                        InetAddress localHosts[] = InetAddress.getAllByName(localHost.getHostName());
056                        
057                        for(int i=0;i<localHosts.length;i++){
058                                if(localHosts[i].equals(addr)) return true;
059                        }
060        }
061        catch(UnknownHostException e){}
062        
063        return false;
064        }
065}