networkmodule.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 888 行 · 第 1/2 页
JAVA
888 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.quercus.lib;import com.caucho.quercus.QuercusModuleException;import com.caucho.quercus.annotation.NotNull;import com.caucho.quercus.annotation.Optional;import com.caucho.quercus.annotation.Reference;import com.caucho.quercus.annotation.ReturnNullAsFalse;import com.caucho.quercus.env.*;import com.caucho.quercus.lib.file.SocketInputOutput;import com.caucho.quercus.module.AbstractQuercusModule;import com.caucho.util.L10N;import javax.naming.NameNotFoundException;import javax.naming.NamingEnumeration;import javax.naming.NamingException;import javax.naming.directory.Attribute;import javax.naming.directory.Attributes;import javax.naming.directory.DirContext;import javax.naming.directory.InitialDirContext;import java.io.IOException;import java.net.InetAddress;import java.net.Socket;import java.util.LinkedHashMap;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;import java.util.regex.Pattern;/** * Information about PHP network */public class NetworkModule extends AbstractQuercusModule { private static final L10N L = new L10N(NetworkModule.class); private static final Logger log = Logger.getLogger(NetworkModule.class.getName()); private static final LinkedHashMap<String, LongValue> _protoToNum = new LinkedHashMap<String, LongValue>(); private static final LinkedHashMap<String, ServiceNode> _servToNum = new LinkedHashMap<String, ServiceNode>(); public static final int LOG_EMERG = 0; public static final int LOG_ALERT = 1; public static final int LOG_CRIT = 2; public static final int LOG_ERR = 3; public static final int LOG_WARNING = 4; public static final int LOG_NOTICE = 5; public static final int LOG_INFO = 6; public static final int LOG_DEBUG = 7; public static final int LOG_PID = 1; public static final int LOG_CONS = 2; public static final int LOG_NDELAY = 8; public static final int LOG_NOWAIT = 16; public static final int LOG_ODELAY = 4; public static final int LOG_PERROR = 32; public static final int LOG_AUTH = 32; public static final int LOG_AUTHPRIV = 80; public static final int LOG_CRON = 72; public static final int LOG_DAEMON = 24; public static final int LOG_KERN = 0; public static final int LOG_LOCAL0 = 128; public static final int LOG_LOCAL1 = 136; public static final int LOG_LOCAL2 = 144; public static final int LOG_LOCAL3 = 152; public static final int LOG_LOCAL4 = 160; public static final int LOG_LOCAL5 = 168; public static final int LOG_LOCAL6 = 176; public static final int LOG_LOCAL7 = 184; public static final int LOG_LPR = 48; public static final int LOG_MAIL = 16; public static final int LOG_NEWS = 56; public static final int LOG_SYSLOG = 40; public static final int LOG_USER = 8; public static final int LOG_UUCP = 64; public static final int DNS_A = 1; public static final int DNS_CNAME = 16; public static final int DNS_HINFO = 4096; public static final int DNS_MX = 16384; public static final int DNS_NS = 2; public static final int DNS_PTR = 2048; public static final int DNS_SOA = 32; public static final int DNS_TXT = 32768; public static final int DNS_AAAA = 134217728; public static final int DNS_SRV = 33554432; public static final int DNS_NAPTR = 67108864; public static final int DNS_A6 = 16777216; public static final int DNS_ALL = 251713587; public static final int DNS_ANY = 268435456; /** * Opens a socket */ public static SocketInputOutput fsockopen(Env env, String host, @Optional int port, @Optional @Reference Value errno, @Optional @Reference Value errstr, @Optional double timeout) { try { if (host == null) return null; String scheme = null; int p = host.indexOf("://"); if (p > 0) { scheme = host.substring(0, p); host = host.substring(p + 3); } p = host.indexOf(':'); if (p > 0) { String portStr = host.substring(p + 1); host = host.substring(0, p); if (port == 0) port = Integer.parseInt(portStr); } if (port == 0) port = 80; Socket s = new Socket(host, port); if (timeout > 0) s.setSoTimeout((int) (timeout * 1000)); else s.setSoTimeout(120000); SocketInputOutput stream; stream = new SocketInputOutput(env, s, SocketInputOutput.Domain.AF_INET); stream.init(); return stream; } catch (IOException e) { log.log(Level.FINER, e.toString(), e); if (errstr != null) errstr.set(env.createString(e.toString())); return null; } } /** * Converts string to long */ public static Value ip2long(String ip) { // php/1m00 if (ip == null) return LongValue.MINUS_ONE; long v = 0; int p = 0; int len = ip.length(); for (int i = 0; i < 4; i++) { int digit = 0; char ch = 0; for (; p < len && '0' <= (ch = ip.charAt(p)) && ch <= '9'; p++) { digit = 10 * digit + ch - '0'; } if (p < len && ch != '.') return BooleanValue.FALSE; else if (p == len && i < 3) return BooleanValue.FALSE; p++; v = 256 * v + digit; } return new LongValue(v); } /** * Returns the IP address of the given host name. If the IP address cannot * be obtained, then the provided host name is returned instead. * * @param hostname the host name who's IP to search for * * @return the IP for the given host name or, if the IP cannot be obtained, * the provided host name */ public static String gethostbyname(String hostname) { // php/1m01 if (hostname == null) return ""; InetAddress ip = null; try { ip = InetAddress.getByName(hostname); } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); return hostname; } return ip.getHostAddress(); } /** * Returns the IP addresses of the given host name. If the IP addresses * cannot be obtained, then the provided host name is returned instead. * * @param hostname the host name who's IP to search for * * @return the IPs for the given host name or, if the IPs cannot be obtained, * the provided host name */ public static Value gethostbynamel(Env env, String hostname) { // php/1m02 InetAddress ip[] = null; try{ ip = InetAddress.getAllByName(hostname); } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); return BooleanValue.FALSE; } ArrayValue ipArray = new ArrayValueImpl(); for (int k = 0; k < ip.length; k++) { String currentIPString = ip[k].getHostAddress(); StringValue currentIP = env.createString((currentIPString)); ipArray.append(currentIP); } return ipArray; } /** * Returns the IP address of the given host name. If the IP address cannot * be obtained, then the provided host name is returned instead. * * @return the IP for the given host name or, if the IP cannot be obtained, * the provided host name */ @ReturnNullAsFalse public static String gethostbyaddr(Env env, String ip) { // php/1m03 if (ip == null) { env.warning("Address must not be null."); return null; } String formIPv4 = "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"; CharSequence ipToCS = ip.subSequence(0, ip.length()); if (! (Pattern.matches(formIPv4, ipToCS))) { env.warning("Address is not in a.b.c.d form"); return null; } String splitIP[] = null; try { splitIP = ip.split("\\."); } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); env.warning("Regex expression invalid"); return ip; } byte addr[] = new byte[splitIP.length]; for (int k = 0; k < splitIP.length; k++) { Integer intForm = new Integer(splitIP[k]); addr[k] = intForm.byteValue(); } InetAddress host = null; try{ host = InetAddress.getByAddress(addr); } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); return ip; } return host.getHostName(); } /** * Returns the protocol number associated with the given protocol name. * * @param protoName the name of the protocol * * @return the number associated with the given protocol name */ public static Value getprotobyname(String protoName) { // php/1m04 if (! (_protoToNum.containsKey(protoName))) return BooleanValue.FALSE; return LongValue.create((_protoToNum.get(protoName).toLong())); } /** * Returns the protocol name associated with the given protocol number. */ @ReturnNullAsFalse public static String getprotobynumber(int protoNumber) { // php/1m05 for (Map.Entry<String, LongValue> entry: _protoToNum.entrySet()) if (entry.getValue().toLong() == protoNumber) return entry.getKey(); return null; } /** * Returns the port number associated with the given protocol and service * name. * * @param service the service name * @param protocol the protocol, either udp or tcp * * @return the number associated with the given protocol and service name */ public static Value getservbyname(String service, String protocol) { // php/1m06 if (! (_servToNum.containsKey(service))) return BooleanValue.FALSE; ServiceNode node = _servToNum.get(service); if (! (node.protocolCheck(protocol))) return BooleanValue.FALSE; return node.getPort(); } /** * Returns the service name associated it the given protocol name and * service port. * * @param port the service port number * @param protocol the protocol, either udp or tcp * * @return the service name */ @ReturnNullAsFalse public static String getservbyport(int port, String protocol) { // php/1m07 for (Map.Entry<String, ServiceNode> entry: _servToNum.entrySet()) { ServiceNode node = entry.getValue(); if (node.getPort().toLong() == port && node.protocolCheck(protocol)) return entry.getKey(); } return null; } public static boolean getmxrr(Env env, @NotNull String hostname, @Reference Value mxhosts, @Optional @Reference Value weight) { return dns_get(env, hostname, "MX", mxhosts, weight); } private static boolean dns_get(Env env, String hostname, String type, Value hostsRef, Value weightRef) { try { // php/1m08 if (hostname == null || type == null)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?