Skip to content

NetAPI Implementation

opencode-agent[bot] edited this page May 11, 2026 · 2 revisions

NetAPI Implementation

JNode's network API implementation that bridges the standard Java java.net package with JNode's custom pure-Java network stack.

Overview

NetAPI Implementation provides the critical interface between application-level networking (standard Java java.net API) and JNode's custom network stack. Since JNode does not run on a host OS, it cannot rely on host socket implementations. Instead, JNode implements the complete OSI model in pure Java, and NetAPI serves as the bridge that connects standard Java networking classes to this custom implementation.

The implementation consists of a small, focused set of classes that expose network device information, address resolution, and DNS lookup capabilities to Java applications.

Key Components

Class Purpose
NetAPIImpl Core implementation of VMNetAPI, provides network device access and DNS resolution
NetDeviceImpl Wrapper around Device that implements VMNetDevice
DefaultNetworkLayerManager Registry for network protocol layers, handles packet routing
NetPlugin Plugin lifecycle management, registers NetAPI with the system

Location: net/src/net/org/jnode/net/service/

How It Works

1. Plugin Initialization

When the NetPlugin starts, it:

  • Creates a DefaultNetworkLayerManager instance
  • Instantiates a NetAPIImpl with the manager
  • Binds the manager to InitialNaming for system-wide access
  • Registers the API with VMNetUtils
// From NetPlugin.java:55-70
public NetPlugin(PluginDescriptor descriptor) {
    super(descriptor);
    ptm = new DefaultNetworkLayerManager(descriptor.getExtensionPoint("networkLayers"));
    api = new NetAPIImpl(ptm);
    packetProcessorThread = new QueueProcessorThread<SocketBuffer>("net-packet-processor", ptm.getQueue(), ptm);
}

protected void startPlugin() throws PluginException {
    InitialNaming.bind(NetworkLayerManager.NAME, ptm);
    packetProcessorThread.start();
    VMNetUtils.setAPI(api, this);
}

2. Network Device Enumeration

The NetAPIImpl.getNetDevices() method returns all registered network devices:

// From NetAPIImpl.java:124-132
public Collection<VMNetDevice> getNetDevices() {
    final ArrayList<VMNetDevice> list = new ArrayList<VMNetDevice>();
    final Collection<Device> devs = DeviceUtils.getDevicesByAPI(NetDeviceAPI.class);
    for (Device dev : devs) {
        list.add(new NetDeviceImpl(dev));
    }
    return list;
}

This iterates through all devices implementing NetDeviceAPI and wraps them in NetDeviceImpl instances.

3. Address Resolution

The getInetAddresses(VMNetDevice) method retrieves IP addresses bound to a network device:

// From NetAPIImpl.java:56-81
public List<InetAddress> getInetAddresses(VMNetDevice netDevice) {
    final ArrayList<InetAddress> list = new ArrayList<InetAddress>();
    final SecurityManager sm = System.getSecurityManager();
    final NetDeviceImpl netDeviceImpl = (NetDeviceImpl) netDevice;
    final NetDeviceAPI api = netDeviceImpl.getDevice().getAPI(NetDeviceAPI.class);
    final ProtocolAddressInfo info = api.getProtocolAddressInfo(EthernetConstants.ETH_P_IP);
    if (info != null) {
        for (ProtocolAddress ipaddr : info.addresses()) {
            if (sm != null) {
                try {
                    sm.checkConnect(ipaddr.toString(), 58000);
                    list.add(ipaddr.toInetAddress());
                } catch (SecurityException ex) {
                    // Just don't add
                }
            } else {
                list.add(ipaddr.toInetAddress());
            }
        }
    }
    return list;
}

The method:

  1. Gets the underlying NetDeviceAPI
  2. Queries protocol address info for IPv4 (ETH_P_IP)
  3. Applies security manager checks if present
  4. Converts internal ProtocolAddress to Java InetAddress

4. DNS Resolution

The getHostByName(String) method delegates to registered network layers:

// From NetAPIImpl.java:161-184
public byte[][] getHostByName(String hostname) throws UnknownHostException {
    ArrayList<byte[]> list = null;
    for (NetworkLayer layer : nlm.getNetworkLayers()) {
        final ProtocolAddress[] addrs = layer.getHostByName(hostname);
        if (addrs != null) {
            if (list == null) {
                list = new ArrayList<byte[]>();
            }
            // Collect addresses from each layer
        }
    }
    if ((list == null) || list.isEmpty()) {
        throw new UnknownHostException(hostname);
    }
    return (byte[][]) list.toArray(new byte[list.size()][]);
}

5. Default Local Address

getLocalAddress() returns the first configured IP address:

// From NetAPIImpl.java:139-159
public InetAddress getLocalAddress() throws UnknownHostException {
    final Collection<Device> devices = DeviceUtils.getDevicesByAPI(NetDeviceAPI.class);
    for (Device dev : devices) {
        final NetDeviceAPI api = dev.getAPI(NetDeviceAPI.class);
        final ProtocolAddressInfo addrInfo = api.getProtocolAddressInfo(EthernetConstants.ETH_P_IP);
        if (addrInfo != null) {
            final ProtocolAddress addr = addrInfo.getDefaultAddress();
            if (addr != null) {
                return addr.toInetAddress();
            }
        }
    }
    throw new UnknownHostException("No IP addresses have been configured for any registered network device");
}

6. Network Layer Management

The DefaultNetworkLayerManager registers network layers (IPv4, ARP, etc.) and routes packets:

// From DefaultNetworkLayerManager.java:80-83
protected synchronized void registerNetworkLayer(NetworkLayer pt)
    throws LayerAlreadyRegisteredException {
    layers.put(pt.getProtocolID(), pt);
}

Layers are loaded dynamically via the plugin extension point system, allowing new protocols to be added without modifying core code.

Gotchas

  1. No getHostByAddr: The getHostByAddr(byte[]) method throws UnknownHostException("Not implemented"). Reverse DNS lookups are not supported.

  2. Security Manager Integration: When a security manager is present, address retrieval applies connectivity checks. This can cause addresses to be silently omitted from results.

  3. Device Lookup by Address: getByInetAddress(InetAddress) throws SocketException if the address is not bound to any registered device, rather than returning null.

  4. Device Existence Check: getByName(String) returns null if the device is not a network device (unlike getByInetAddress which throws an exception).

Related Pages

Clone this wiki locally