I am sorry for my bad English. There is a simple Java server for controlling a smart house. I would like to organize a security check using a list of trusted mac addresses. The server will accept if client's mac address is trusted.
Well I have run into a problem. I can't get any client's mac at all and I have no any idea how to do it. Help me please :(
PS: I use standard java nio libs.
My code:
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel client = ssc.accept();
char [] tempIP = client.socket().getInetAddress().toString().toCharArray();
StringBuilder temp = new StringBuilder ();
for (char ch : tempIP) {
if (ch == '/')
continue;
temp.append(ch);
}
String clientIP = temp.toString();
System.out.println(clientIP);
InetAddress address = InetAddress.getByName(clientIP);
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
byte [] mac = ni.getHardwareAddress();
if (mac != null) {
for (int i=0; i!=mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
} else {
System.out.println("Address doen't not exist or is not accessiable");
}
} else {
System.out.println("Network Interface for the specified address is not found!");
}
client.configureBlocking(false);
client.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
client.setOption(StandardSocketOptions.TCP_NODELAY, true);
client.register(selector, SelectionKey.OP_READ);
System.out.println("Client is connected!");