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

📄 ipv4controlblocklist.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
字号:
/*
 * $Id: IPv4ControlBlockList.java,v 1.1 2003/11/25 11:52:24 epr Exp $
 */
package org.jnode.net.ipv4;

import java.net.BindException;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * A list of IPv4ControlBlock's.
 * 
 * @author Ewout Prangsma (epr@users.sourceforge.net)
 */
public abstract class IPv4ControlBlockList {

	private final LinkedList list = new LinkedList();
	private int lastFreePort = IPv4Constants.IPPORT_RESERVED;

	/**
	 * Lookup the best matching control block for the given parameters.
	 * 
	 * @param fAddr
	 * @param fPort
	 * @param lAddr
	 * @param lPort
	 * @param allowWildcards
	 * @return Null if no match, the best matching Control Block otherwise.
	 */
	public IPv4ControlBlock lookup(IPv4Address fAddr, int fPort, IPv4Address lAddr, int lPort, boolean allowWildcards) {

		IPv4ControlBlock bestcb = null;
		int bestmatch = Integer.MAX_VALUE;

		for (Iterator i = list.iterator(); i.hasNext();) {
			final IPv4ControlBlock cb = (IPv4ControlBlock) i.next();

			final int match = cb.match(fAddr, fPort, lAddr, lPort, allowWildcards);

			//Syslog.debug("match:" + match + "bestmatch: " + bestmatch + " cb:" + cb);
			
			if (match == 0) {
				// Exact match
				return cb;
			} else if ((match >= 0) && (match < bestmatch)) {
				bestmatch = match;
				bestcb = cb;
			}
		}

		//Syslog.debug("bestmatch: " + bestmatch + " return:" + bestcb);
		return bestcb;
	}

	/**
	 * Create a binding for a local address & port.
	 * 
	 * @param lAddr
	 * @param lPort
	 * @return The created binding
	 */
	public synchronized IPv4ControlBlock bind(IPv4Address lAddr, int lPort) throws BindException {
		if (lPort != 0) {
			// Specific local port
			if (lookup(IPv4Address.ANY, 0, lAddr, lPort, true) != null) {
				throw new BindException("Address already in use");
			}
		} else {
			// Choose free port
			lPort = lastFreePort;
			do {
				lPort++;
				if ((lPort < IPv4Constants.IPPORT_RESERVED) ||
				    (lPort > IPv4Constants.IPPORT_USERRESERVED)) {
					lPort = IPv4Constants.IPPORT_RESERVED;
				}
			} while (lookup(IPv4Address.ANY, 0, lAddr, lPort, true) != null);
			lastFreePort = lPort;
		}
		final IPv4ControlBlock cb = createControlBlock(null);
		cb.bind(lAddr, lPort);
		list.add(cb);
		return cb;
	}

	/**
	 * Create an implementation specific control block.
	 * 
	 * @return The created control block
	 */
	protected abstract IPv4ControlBlock createControlBlock(IPv4ControlBlock parent);
	
	/**
	 * Add a block to the list
	 * @param cb
	 */
	final synchronized void add(IPv4ControlBlock cb) {
		list.add(cb);
	}
	
	/**
	 * Remove a block from the list
	 * @param cb
	 */
	final synchronized void remove(IPv4ControlBlock cb) {
		list.remove(cb);
	}
	
	/**
	 * Create an iterator over all entries
	 * @return The iterator
	 */
	protected Iterator iterator() {
		return list.iterator();
	}
}

⌨️ 快捷键说明

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