-
Notifications
You must be signed in to change notification settings - Fork 0
NetAPI Implementation
JNode's network API implementation that bridges the standard Java
java.netpackage with JNode's custom pure-Java network stack.
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.
| 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/
When the NetPlugin starts, it:
- Creates a
DefaultNetworkLayerManagerinstance - Instantiates a
NetAPIImplwith the manager - Binds the manager to
InitialNamingfor 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);
}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.
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:
- Gets the underlying
NetDeviceAPI - Queries protocol address info for IPv4 (
ETH_P_IP) - Applies security manager checks if present
- Converts internal
ProtocolAddressto JavaInetAddress
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()][]);
}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");
}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.
-
No getHostByAddr: The
getHostByAddr(byte[])method throwsUnknownHostException("Not implemented"). Reverse DNS lookups are not supported. -
Security Manager Integration: When a security manager is present, address retrieval applies connectivity checks. This can cause addresses to be silently omitted from results.
-
Device Lookup by Address:
getByInetAddress(InetAddress)throwsSocketExceptionif the address is not bound to any registered device, rather than returningnull. -
Device Existence Check:
getByName(String)returnsnullif the device is not a network device (unlikegetByInetAddresswhich throws an exception).
- Network-Stack - The complete custom network stack implementation
-
Sockets-Implementation - Bridge between
java.netAPI and native TCP stack - Network-Layers - Protocol layering and SocketBuffer
- IPv4-Protocol - IPv4 packet processing
- Driver-Framework - Device management and API pattern
- NetPermission - Security permission for network operations (DHCP, BOOTP)