- 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-70publicNetPlugin(PluginDescriptordescriptor) {
super(descriptor);
ptm = newDefaultNetworkLayerManager(descriptor.getExtensionPoint("networkLayers"));
api = newNetAPIImpl(ptm);
packetProcessorThread = newQueueProcessorThread<SocketBuffer>("net-packet-processor", ptm.getQueue(), ptm);
}
protectedvoidstartPlugin() throwsPluginException {
InitialNaming.bind(NetworkLayerManager.NAME, ptm);
packetProcessorThread.start();
VMNetUtils.setAPI(api, this);
}The NetAPIImpl.getNetDevices() method returns all registered network devices:
// From NetAPIImpl.java:124-132publicCollection<VMNetDevice> getNetDevices() {
finalArrayList<VMNetDevice> list = newArrayList<VMNetDevice>();
finalCollection<Device> devs = DeviceUtils.getDevicesByAPI(NetDeviceAPI.class);
for (Devicedev : devs) {
list.add(newNetDeviceImpl(dev));
}
returnlist;
}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-81publicList<InetAddress> getInetAddresses(VMNetDevicenetDevice) {
finalArrayList<InetAddress> list = newArrayList<InetAddress>();
finalSecurityManagersm = System.getSecurityManager();
finalNetDeviceImplnetDeviceImpl = (NetDeviceImpl) netDevice;
finalNetDeviceAPIapi = netDeviceImpl.getDevice().getAPI(NetDeviceAPI.class);
finalProtocolAddressInfoinfo = api.getProtocolAddressInfo(EthernetConstants.ETH_P_IP);
if (info != null) {
for (ProtocolAddressipaddr : info.addresses()) {
if (sm != null) {
try {
sm.checkConnect(ipaddr.toString(), 58000);
list.add(ipaddr.toInetAddress());
} catch (SecurityExceptionex) {
// Just don't add
}
} else {
list.add(ipaddr.toInetAddress());
}
}
}
returnlist;
}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-184publicbyte[][] getHostByName(Stringhostname) throwsUnknownHostException {
ArrayList<byte[]> list = null;
for (NetworkLayerlayer : nlm.getNetworkLayers()) {
finalProtocolAddress[] addrs = layer.getHostByName(hostname);
if (addrs != null) {
if (list == null) {
list = newArrayList<byte[]>();
}
// Collect addresses from each layer
}
}
if ((list == null) || list.isEmpty()) {
thrownewUnknownHostException(hostname);
}
return (byte[][]) list.toArray(newbyte[list.size()][]);
}getLocalAddress() returns the first configured IP address:
// From NetAPIImpl.java:139-159publicInetAddressgetLocalAddress() throwsUnknownHostException {
finalCollection<Device> devices = DeviceUtils.getDevicesByAPI(NetDeviceAPI.class);
for (Devicedev : devices) {
finalNetDeviceAPIapi = dev.getAPI(NetDeviceAPI.class);
finalProtocolAddressInfoaddrInfo = api.getProtocolAddressInfo(EthernetConstants.ETH_P_IP);
if (addrInfo != null) {
finalProtocolAddressaddr = addrInfo.getDefaultAddress();
if (addr != null) {
returnaddr.toInetAddress();
}
}
}
thrownewUnknownHostException("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-83protectedsynchronizedvoidregisterNetworkLayer(NetworkLayerpt)
throwsLayerAlreadyRegisteredException {
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)