|
From: <cr...@us...> - 2008-05-10 14:17:24
|
Revision: 4079
http://jnode.svn.sourceforge.net/jnode/?rev=4079&view=rev
Author: crawley
Date: 2008-05-10 07:17:22 -0700 (Sat, 10 May 2008)
Log Message:
-----------
Implemented an IPv4 Argument class. Converted IfconfigCommand.
Modified Paths:
--------------
trunk/net/descriptors/org.jnode.net.command.xml
trunk/net/src/net/org/jnode/net/command/IfconfigCommand.java
Added Paths:
-----------
trunk/net/src/net/org/jnode/net/syntax/
trunk/net/src/net/org/jnode/net/syntax/IPv4AddressArgument.java
Modified: trunk/net/descriptors/org.jnode.net.command.xml
===================================================================
--- trunk/net/descriptors/org.jnode.net.command.xml 2008-05-10 14:15:48 UTC (rev 4078)
+++ trunk/net/descriptors/org.jnode.net.command.xml 2008-05-10 14:17:22 UTC (rev 4079)
@@ -20,6 +20,7 @@
<library name="jnode-net.jar">
<export name="org.jnode.net.command.*"/>
<export name="org.jnode.net.help.argument.*"/>
+ <export name="org.jnode.net.syntax.*"/>
</library>
</runtime>
@@ -50,6 +51,15 @@
<syntax alias="dhcp">
<argument argLabel="device" description="Configure a network interface using DHCP"/>
</syntax>
+ <syntax alias="ifconfig">
+ <empty description="Print network addresses for all network devices"/>
+ <argument argLabel="device" description="Print network addresses for a given device"/>
+ <sequence description="Bind a given device to an IPv4 address">
+ <argument argLabel="device"/>
+ <argument argLabel="ipAddress"/>
+ <optional><argument argLabel="subnetMask"/></optional>
+ </sequence>
+ </syntax>
</extension>
<extension point="org.jnode.security.permissions">
Modified: trunk/net/src/net/org/jnode/net/command/IfconfigCommand.java
===================================================================
--- trunk/net/src/net/org/jnode/net/command/IfconfigCommand.java 2008-05-10 14:15:48 UTC (rev 4078)
+++ trunk/net/src/net/org/jnode/net/command/IfconfigCommand.java 2008-05-10 14:17:22 UTC (rev 4079)
@@ -24,83 +24,85 @@
import java.io.InputStream;
import java.io.PrintStream;
+import javax.naming.NameNotFoundException;
+
+import org.jnode.driver.ApiNotFoundException;
import org.jnode.driver.Device;
import org.jnode.driver.DeviceManager;
import org.jnode.driver.net.NetDeviceAPI;
+import org.jnode.driver.net.NetworkException;
import org.jnode.naming.InitialNaming;
import org.jnode.net.ethernet.EthernetConstants;
-import org.jnode.net.help.argument.HostArgument;
import org.jnode.net.ipv4.IPv4Address;
import org.jnode.net.ipv4.config.IPv4ConfigurationService;
+import org.jnode.net.syntax.IPv4AddressArgument;
import org.jnode.shell.AbstractCommand;
import org.jnode.shell.CommandLine;
-import org.jnode.shell.help.Help;
-import org.jnode.shell.help.Parameter;
-import org.jnode.shell.help.ParsedArguments;
-import org.jnode.shell.help.Syntax;
-import org.jnode.shell.help.argument.DeviceArgument;
+import org.jnode.shell.syntax.*;
/**
+ * This command class binds IP addresses to network devices, and displays bindings.
+ *
* @author epr
+ * @author cr...@jn...
*/
public class IfconfigCommand extends AbstractCommand {
+ // FIXME should support IPv6 and other address families.
- static final DeviceArgument ARG_DEVICE = new DeviceArgument("device", "the device", NetDeviceAPI.class);
- static final HostArgument ARG_IP_ADDRESS = new HostArgument("ip-address", "the IP address to bind the device to");
- static final HostArgument ARG_SUBNET_MASK = new HostArgument("subnet-mask", "if given, specifies the range of reachable subnets");
+ private final DeviceArgument ARG_DEVICE =
+ new DeviceArgument("device", Argument.OPTIONAL, "the device", NetDeviceAPI.class);
+
+ private final IPv4AddressArgument ARG_IP_ADDRESS =
+ new IPv4AddressArgument("ipAddress", Argument.OPTIONAL, "the IPv4 address to bind the device to");
+
+ private final IPv4AddressArgument ARG_SUBNET_MASK =
+ new IPv4AddressArgument("subnetMask", Argument.OPTIONAL, "the IPv4 subnet mask for the device");
- public static Help.Info HELP_INFO = new Help.Info(
- "ifconfig", new Syntax[]{
- new Syntax("Print status of all network devices"),
- new Syntax("Print status of a single network device",
- new Parameter[]{
- new Parameter(ARG_DEVICE, Parameter.MANDATORY),
- }
- ),
- new Syntax("Bind a device to an IP address",
- new Parameter[]{
- new Parameter(ARG_DEVICE, Parameter.MANDATORY),
- new Parameter(ARG_IP_ADDRESS, Parameter.MANDATORY),
- new Parameter(ARG_SUBNET_MASK, Parameter.OPTIONAL)
- }
- )
- }
- );
+
+ public IfconfigCommand() {
+ super("List or manage network interface bindings");
+ registerArguments(ARG_DEVICE, ARG_IP_ADDRESS, ARG_SUBNET_MASK);
+ }
- public static void main(String[] args)
- throws Exception {
+ public static void main(String[] args) throws Exception {
new IfconfigCommand().execute(args);
}
- public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
-
- if( cmdLine.size() == 0 ) {
- final DeviceManager dm = (DeviceManager)InitialNaming.lookup(DeviceManager.NAME);
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
+ throws NameNotFoundException, ApiNotFoundException, NetworkException {
+ if (!ARG_DEVICE.isSet()) {
+ // Print MAC address, MTU and IP address(es) for all network devices.
+ final DeviceManager dm = (DeviceManager) InitialNaming.lookup(DeviceManager.NAME);
for (Device dev : dm.getDevicesByAPI(NetDeviceAPI.class)) {
- final NetDeviceAPI api = (NetDeviceAPI)dev.getAPI(NetDeviceAPI.class);
- System.out.println(dev.getId() + ": MAC-Address " + api.getAddress() + " MTU " + api.getMTU());
- System.out.println(" " + api.getProtocolAddressInfo(EthernetConstants.ETH_P_IP));
- System.out.println();
+ final NetDeviceAPI api = (NetDeviceAPI) dev.getAPI(NetDeviceAPI.class);
+ out.println(dev.getId() + ": MAC-Address " + api.getAddress() + " MTU " + api.getMTU());
+ out.println(" " + api.getProtocolAddressInfo(EthernetConstants.ETH_P_IP));
}
- } else {
+ }
+ else {
+ final Device dev = ARG_DEVICE.getValue();
+ final NetDeviceAPI api = (NetDeviceAPI) dev.getAPI(NetDeviceAPI.class);
- Device dev = ARG_DEVICE.getDevice(cmdLine);
- NetDeviceAPI api = (NetDeviceAPI)dev.getAPI(NetDeviceAPI.class);
-
- if( cmdLine.size() == 1 ) {
- // Print address
- System.out.println("IP address(es) for " + dev.getId() + " " + api.getProtocolAddressInfo(EthernetConstants.ETH_P_IP));
- } else {
- // Set IP address
- IPv4Address ip = ARG_IP_ADDRESS.getAddress(cmdLine);
- IPv4Address mask = ARG_SUBNET_MASK.getAddress(cmdLine);
- final IPv4ConfigurationService cfg = (IPv4ConfigurationService)InitialNaming.lookup(IPv4ConfigurationService.NAME);
+ if (!ARG_IP_ADDRESS.isSet()) {
+ // Print IP address(es) for device
+ out.println("IP address(es) for " + dev.getId() +
+ " " + api.getProtocolAddressInfo(EthernetConstants.ETH_P_IP));
+ }
+ else {
+ // Set IP address for device
+ final IPv4Address ip = ARG_IP_ADDRESS.getValue();
+ final IPv4Address mask = ARG_SUBNET_MASK.getValue();
+ final IPv4ConfigurationService cfg = (IPv4ConfigurationService)
+ InitialNaming.lookup(IPv4ConfigurationService.NAME);
cfg.configureDeviceStatic(dev, ip, mask, true);
- System.out.println("IP address for " + dev.getId() + " set to " + api.getProtocolAddressInfo(EthernetConstants.ETH_P_IP));
+
+ // FIXME ... this doesn't show the device's new address because the
+ // IPv4 ConfigurationServiceImpl calls processor.apply with the
+ // waitUntilReady parameter == false. (The comment in the code
+ // talks about avoiding deadlocks.)
+ out.println("IP address for " + dev.getId() + " set to " +
+ api.getProtocolAddressInfo(EthernetConstants.ETH_P_IP));
}
}
- System.out.println();
}
-
}
Added: trunk/net/src/net/org/jnode/net/syntax/IPv4AddressArgument.java
===================================================================
--- trunk/net/src/net/org/jnode/net/syntax/IPv4AddressArgument.java (rev 0)
+++ trunk/net/src/net/org/jnode/net/syntax/IPv4AddressArgument.java 2008-05-10 14:17:22 UTC (rev 4079)
@@ -0,0 +1,63 @@
+package org.jnode.net.syntax;
+
+import java.net.InetAddress;
+import java.util.StringTokenizer;
+
+import org.jnode.driver.console.CompletionInfo;
+import org.jnode.net.ipv4.IPv4Address;
+
+import org.jnode.shell.CommandLine.Token;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.CommandSyntaxException;
+
+/**
+ * This Argument class accepts 4-part IPv4 addresses. It validates the address,
+ * but does no completion.
+ *
+ * @author cr...@jn...
+ */
+public class IPv4AddressArgument extends Argument<IPv4Address> {
+
+ public IPv4AddressArgument(String label, int flags,
+ String description) {
+ super(label, flags, new IPv4Address[0], description);
+ }
+
+ @Override
+ protected String argumentKind() {
+ return "IPv4 address";
+ }
+
+ @Override
+ protected IPv4Address doAccept(Token value) throws CommandSyntaxException {
+ final StringTokenizer tok = new StringTokenizer(value.token, ".");
+ if (tok.countTokens() != 4) {
+ throw new CommandSyntaxException("Wrong number of components for an IPv4 address");
+ }
+ try {
+ final byte b1 = parseUnsignedByte(tok.nextToken());
+ final byte b2 = parseUnsignedByte(tok.nextToken());
+ final byte b3 = parseUnsignedByte(tok.nextToken());
+ final byte b4 = parseUnsignedByte(tok.nextToken());
+ return new IPv4Address(new byte[]{b1, b2, b3, b4}, 0);
+ } catch (NumberFormatException ex) {
+ throw new CommandSyntaxException("Invalid component in IPv4 address");
+ }
+ }
+
+ /**
+ * Parse a number and check it is in the range 0 to 255.
+ *
+ * @param str
+ * @throws NumberFormatException if 'str' is not a number or if it is out of range
+ * @return the number cast as a byte.
+ */
+ private byte parseUnsignedByte(String str) {
+ final int v = Integer.parseInt(str);
+ if ((v >= 0) && (v < 256)) {
+ return (byte) v;
+ } else {
+ throw new NumberFormatException(str);
+ }
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|