ipv4route.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 216 行
JAVA
216 行
/*
* $Id: IPv4Route.java,v 1.1 2003/11/25 11:52:24 epr Exp $
*/
package org.jnode.net.ipv4;
import org.jnode.driver.ApiNotFoundException;
import org.jnode.driver.Device;
import org.jnode.driver.net.NetDeviceAPI;
/**
* @author epr
*/
public class IPv4Route implements IPv4Constants {
private final IPv4Address destination;
private final IPv4Address subnetmask;
private final IPv4Address gateway;
private final Device device;
private int flags;
private final NetDeviceAPI deviceAPI;
private int useCount;
/**
* Create a new instance
* @param destination
* @param device
* @throws IllegalArgumentException If the device is not a network device.
*/
public IPv4Route(IPv4Address destination, Device device)
throws IllegalArgumentException {
this(destination, null, null, device);
}
/**
* Create a new instance
* @param destination
* @param device
* @throws IllegalArgumentException If the device is not a network device.
*/
public IPv4Route(IPv4Address destination, IPv4Address subnetmask, Device device)
throws IllegalArgumentException {
this(destination, subnetmask, null, device);
}
/**
* Create a new instance
* @param destination
* @param gateway
* @param device
* @throws IllegalArgumentException If the device is not a network device.
*/
public IPv4Route(IPv4Address destination, IPv4Address subnetmask, IPv4Address gateway, Device device)
throws IllegalArgumentException {
this.destination = destination;
if (subnetmask == null) {
this.subnetmask = destination.getDefaultSubnetmask();
} else {
this.subnetmask = subnetmask;
}
this.gateway = gateway;
this.device = device;
this.flags = RTF_UP;
if (gateway != null) {
this.flags |= RTF_GATEWAY;
}
try {
this.deviceAPI = (NetDeviceAPI)device.getAPI(NetDeviceAPI.class);
} catch (ApiNotFoundException ex) {
throw new IllegalArgumentException("Device " + device.getId() + " is not a network device");
}
}
/**
* Is this route up?
* @return True if this route is up, false otherwise
*/
public boolean isUp() {
return ((flags & RTF_UP) != 0);
}
/**
* Is the destination address a host address?
* @return True if the destination is a host address, false otherwise
* @see #isNetwork()
*/
public boolean isHost() {
return destination.isHost();
}
/**
* Is the destination address a network address?
* @return True if the destination is a network address, false otherwise
* @see #isHost()
*/
public boolean isNetwork() {
return destination.isNetwork();
}
/**
* Is this an indirect route?
*/
public boolean isGateway() {
return ((flags & RTF_GATEWAY) != 0);
}
/**
* Set the gateway flag
* @param on
* @throws IllegalArgumentException If on is true and no gateway address is set.
*/
public void setGateway(boolean on)
throws IllegalArgumentException {
if (on) {
if (gateway == null) {
throw new IllegalArgumentException("Cannot set gateway without a gateway address");
} else {
flags |= RTF_GATEWAY;
}
} else {
flags &= ~RTF_GATEWAY;
}
}
/**
* Set the up flag
* @param on
*/
public void setUp(boolean on) {
if (on) {
flags |= RTF_UP;
} else {
flags &= ~RTF_UP;
}
}
/**
* Gets the destination address of this route
*/
public IPv4Address getDestination() {
return destination;
}
/**
* Gets the device of this route
*/
public Device getDevice() {
return device;
}
/**
* Gets the NetDeviceAPI of the device of this route
*/
public NetDeviceAPI getDeviceAPI() {
return deviceAPI;
}
/**
* Gets the netmask of the destination address
*/
public IPv4Address getSubnetmask() {
return subnetmask;
}
/**
* Gets the gateway address.
* @return The gateway address, or null is <code>isGateway</code>
* returns false
*/
public IPv4Address getGateway() {
return gateway;
}
/**
* Convert to a String representation
* @see java.lang.Object#toString()
*/
public String toString() {
final StringBuffer b = new StringBuffer();
b.append(destination);
b.append(" - ");
b.append(subnetmask);
b.append(" - ");
b.append(gateway);
b.append(" - ");
if (isUp()) {
b.append('U');
}
if (isGateway()) {
b.append('G');
}
if (isHost()) {
b.append('H');
}
b.append(" - ");
b.append(useCount);
b.append(" - ");
b.append(device.getId());
return b.toString();
}
/**
* Gets the number of times this route has been used
*/
public int getUseCount() {
return useCount;
}
/**
* Increment the number of times this route has been used
*/
public void incUseCount() {
useCount++;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?