⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connection.java

📁 sifi-0.1.6.tar.gz 出自http://www.ifi.unizh.ch/ikm/SINUS/firewall/ 一个linux的防火墙工具。
💻 JAVA
字号:
/* ----------------------------------------------------------------------   The SINUS Firewall -- a TCP/IP packet filter for Linux   Written within the SINUS project at the University of Zurich,   SWITCH, Telekurs Payserv AG, ETH Zurich.   originally based on the sf Firewall Software (C) 1996 by Robert   Muchsel and Roland Schmid.   This program 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.   This program 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.  See the   GNU General Public License for more details.   You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   SINUS Firewall resources:   SINUS Homepage: http://www.ifi.unizh.ch/ikm/SINUS/   Firewall Homepage: http://www.ifi.unizh.ch/ikm/SINUS/firewall.html   Frequently asked questions: http://www.ifi.unizh.ch/ikm/SINUS/sf_faq.html   Mailing list for comments, questions, bug reports: firewall@ifi.unizh.ch   ----------------------------------------------------------------------  */package sfclasses;import java.awt.*;import java.io.*;import java.net.*;import java.util.*;class Connection extends Thread {	/**	 * Constructor verifying the server address.	 * valid is set appropriate and can be queried by isValid().	 */	Connection(byte[] server) {				open = false;		valid = false;		try {			if (server.length != 4)				throw new UnknownHostException();			String s = "" + Utils.unsign(server[0]);			for (int i = 1; i < 4; i++)				s += "." + Utils.unsign(server[i]);			iaddr = InetAddress.getByName(s);			valid = true;		}		catch (UnknownHostException e) {			valid = false;		}	} // Connection	public boolean isValid() {		return valid;	} // validConnection	public boolean isOpen() {		return open;	} // validConnection	/**   * Open connection to this server	 * @return true upon success, false otherwise	 */	public synchronized boolean open() {			if (open)			return true;		try {			socket = new Socket(iaddr, serverPort);			in = new DataInputStream(				new PushbackInputStream(socket.getInputStream(), 100));			out = new DataOutputStream(socket.getOutputStream());				readHeader();			switch (readCommand) {				case Communicator.SERV_SERVER_HELLO:					writeHeader(Communicator.SERV_CLIENT_HELLO, 0);					open = true;					this.start();					return true;				default:					open = false;					return false;			}		}		catch (IOException e) {			System.out.println("Error: Unable to connect to server! "+e);			return false;		}		catch (Exception e) {			System.out.println("Error: Unexpected error connecting to server!: "+e);			return false;		}	} // open	/**   * Close connection to this server	 * @return true upon success, false otherwise	 */	public synchronized boolean close() {    try {      writeHeader(Communicator.SERV_CLOSE, 0);      readHeader();      switch (readCommand) {        case Communicator.SERV_CLOSE_ACK:					this.stop();          return true;        default:          Communicator.commError = Communicator.COMM_ERROR;          in.skipBytes(readLength);					this.stop();          return false;      }    }    catch (Exception e) {      Communicator.commError = Communicator.COMM_IO;			this.stop();      return false;    }	} // close   /**   * Write header to output stream. This methods writes the packet header   * to the output stream.   * @param command Command for the server   * @param length Number of bytes following the header   */  protected synchronized void writeHeader(int command, int length) throws IOException {    out.writeInt(magic);    out.writeInt(command);    out.writeInt(length);  }  /**   * Read header from output stream. The read command is stored in readCommand   * and the number of bytes following the header is stored in readLength.   */  protected synchronized void readHeader()		throws IOException, EOFException {		// If an exception is thrown here, it is probably an I/O error    Communicator.commError = Communicator.COMM_IO;    int mg = in.readInt();    if (mg != magic)      throw new IOException("Invalid magic");    readCommand = in.readInt();    readLength = in.readInt();		while (readCommand == Communicator.SERV_PING) {			// consume pending SERV_PING			// read command pending... do it.			mg = in.readInt();			if (mg != magic)				throw new IOException("Invalid magic");			readCommand = in.readInt();			readLength = in.readInt();		}    Communicator.commError = Communicator.COMM_OK;  }					public void run() {			while (true) {		try {			writeHeader(Communicator.SERV_PING_REPLY, 0);		}		catch (IOException e) {			System.out.println("IOException: "+e);		}		try {			sleep(1000*300);		}		catch (InterruptedException ignore) {}		}	}				  /**   * Input stream to read from server   */  protected static DataInputStream in;  /**   * Output stream to write to server   */  protected static DataOutputStream out;  /**   * The last command read from the server is saved in readCommand.   */  protected static int readCommand;   // last command read from in  /**   * The last packet data length read from the server is saved in readLength.   */  protected static int readLength;    // last data length read from in  /**   * TCP port on which the server is accepting connections   */  protected final static int serverPort = 7227;  // magic used in server communication  private final static int magic = 0xbedd1bed;protected InetAddress iaddr;private Thread t;private Socket socket;private boolean valid;private boolean open;} // class Connection

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -