Recently I learn how to generate UUID. I find the class TimeBasedUUIDGenerator.java in elasticsearch.
I find it use mac adress to identify current machine(MacAddressProvide.java). But it XOR the mac adress with random bytes (as the code snippet below). As I know this will make the mac adress to random and increase the probability of conflict. Why we don't use mac adress directly?
public static byte[] getSecureMungedAddress() {
byte[] address = null;
try {
address = getMacAddress();
} catch (SocketException e) {
// address will be set below
}
if (!isValidAddress(address)) {
address = constructDummyMulticastAddress();
}
byte[] mungedBytes = new byte[6];
SecureRandomHolder.INSTANCE.nextBytes(mungedBytes);
for (int i = 0; i < 6; ++i) {
mungedBytes[i] ^= address[i];
}
return mungedBytes;
}
The code author is not sure about this either.
He said the reason may be not reveal the server's real mac address for security concern.
I think this also enable multi UUID generators deploy in the same server.
Related
I ve got an issue.
I want to send file from my android to a hardware.
But unfortunetely when the file has more than 10 kb , it seems some line of text are missing. Its doesnt send and write the file perfectly to my hardware when i check it. but if its less than, its seems working fine.
I have no much knowleges about micropython or similiar things like microcontroller stuff. But i think its regarding the way i write the data as byte are not quite faster might be the thing cause the issue.
Maybe some of you guys can solve my problem?
Maybe like an ampy functionality.
import com.felhr.usbserial.UsbSerialDevice;
private UsbSerialDevice serialPort
public void writeBytes(byte[] bytes) {
if (!usbServiceStarted) {
eventEmit(onErrorEvent, createError(com.melihyarikkaya.rnserialport.Definitions.ERROR_USB_SERVICE_NOT_STARTED, com.melihyarikkaya.rnserialport.Definitions.ERROR_USB_SERVICE_NOT_STARTED_MESSAGE));
return;
}
serialPort.write(bytes);
}
public void send (String content, boolean stop) {
int i = 0;
int step = 256;
byte[] command_bytes = content.getBytes(StandardCharsets.UTF_8);
===> probably starting from this line are the issue
while(i < command_bytes.length) {
int end = Math.min(i + step, command_bytes.length);
byte[] slice = Arrays.copyOfRange(command_bytes, i, end); ===> probably this is the issue
writeBytes(slice);
i += step;
}
if(stop) {
stop();
}
}
I have a software running in OpenShift (Kubernetes) where the licence is based on the MAC address. When restarting the app, the MAC address of the container changes and I have to apply for a new licence file.
Since there are no static MAC-Adressess in k8s pods, I want to spoof the Java call to NetworkInterface.getHardwareAddress() to trick the software into thinking the MAC Address is still the same.
Enumeration enumeration = NetworkInterface.getNetworkInterfaces();
while (enumeration.hasMoreElements()) {
NetworkInterface networkInterface = (NetworkInterface) enumeration.nextElement();
if (networkInterface.isLoopback() || networkInterface.isPointToPoint() || networkInterface.isVirtual()) {
continue;
}
if (networkInterface.isUp()) {
byte[] arrayOfByte = networkInterface.getHardwareAddress();
if (arrayOfByte != null && arrayOfByte.length == 6) {
StringBuilder stringBuilder = new StringBuilder();
for (byte b = 0; b < arrayOfByte.length; b++) {
if (b != 0) {
stringBuilder.append(":");
}
stringBuilder.append(String.format("%02x", arrayOfByte[b]));
}
System.out.println(networkInterface.getName() + ": " + stringBuilder);
}
}
}
actual: eth0: 01:14:4d:ec:01:42
expected: eth0: ee:ee:ee:ee:ee:ee
Unfortunately, you cannot do this in Java.
The method NetworkInterface::getNetworkInterfaces() is implemented as native, which means it does not access any specific field to obtain its results. If it did, you might have luck hacking stuff with reflection, but as it stands, you have to manage it in your OS configuration instead.
EDIT: As for Kubernetes solutions, you might want to look here
I want to list all the devices connected to my network, I done like this
InetAddress i = InetAddress.getLocalHost();
byte[] ip1 = i.getAddress();
for (int b = 0; b <255;b++) {
ip1[3] = (byte)b;
InetAddress address = InetAddress.getByAddress(ip1);
if (address.isReachable(3000)) {
System.out.println("\tIP :"+address.getHostAddress());
} else if (!address.getHostAddress().equals(address.getHostName())) {
System.out.println("\tIP :"+address.getHostAddress());
} else {
}
}
It prints all the connected devices but how to I identify which are wired connection and which are wireless, among them
In pure Java, you are limited to what NetworkInterface provides.
Additional information may be found e.g. in this Stackoverflow question.
I'm having some troubles to detect client's private ip that conect to a web application I built.
Take a look at my tests results(In machines that runs windows):
1-In some machines(from different location ,countries..) the applet give me the correct ip but
2-In others I've obtained ip=127.0.0.1 :
What have I tried to solve this?
A- for example: I've stopped the avast program protection(web shield) and the applet start to give me the correct private ip.
B- In others machines I tried "point A" but It didn't work
C- I also edit host file but I didn't work as well
What I need from you is to help me to understand what is happening? where to look in order to resolve this...
Please don't answer saying "Why do you need the private ip? It could change..." ... I know all the machines that are going to connect to my web application so I can configure them.
Part of the source code that my applet use:
private String PrivateIP(boolean flag)
{
String s1 = "unknown";
String s2 = getDocumentBase().getHost();
int i = 80;
if(getDocumentBase().getPort() != -1)
i = getDocumentBase().getPort();
try
{
String s = (new Socket(s2, i)).getLocalAddress().getHostAddress();
if(!s.equals("255.255.255.255"))
s1 = s;
}
catch(SecurityException _ex)
{
s1 = "FORBIDDEN";
}
catch(Exception _ex)
{
s1 = "ERROR";
}
if(flag)
try
{
s1 = (new Socket(s2, i)).getLocalAddress().getHostName();
}
catch(Exception _ex)
{
Stat = "Cannot Lookup this IP";
}
return s1;
}
I'll let you more information:
I've traid this http://www.auditmypc.com/digital-footprint.asp in order to obtain the ip from probably other method but the same result, I've also run http://www.auditmypc.com/firewall-test.asp and obtained in the machines that I couldn't obtained the correct ip a message like "Congratulations you don't have any port to be open" xD...
Thanks in advance!
First of all, there can be more than one IP address available on the client, if there is more than one network interface. Which one is returned by your method depends on which is used for new Socket() to open.
Now, you do not have to open sockets to get the client's IP. What you can do instead is to enumerate them like this:
String host = InetAddress.getLocalHost().getHostName();
InetAddress[] addressArray = InetAddress.getAllByName(host);
String[] ipArray = new String[addressArray.length];
for (int i = 0; i < addressArray.length; i++) {
InetAddress addr = addressArray[i];
ipArray[i] = addr.getHostAddress();
}
return ipArray;
Now the ipArray will hold a list of available IP adresses on client's workstation.
I am trying to send data over a USB to serial cable using the jUSB library. I'm coding in the NetBeans IDE on Windows.
What is the problem behind the message: "USB Host support is unavailable" in the following code:
package usb.core;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import usb.core.*;
public class Main {
public static void main(String[] args) throws IOException {
try {
// Bootstrap by getting the USB Host from the HostFactory.
Host host = HostFactory.getHost();
// Obtain a list of the USB buses available on the Host.
Bus[] bus = host.getBusses();
int total_bus = bus.length;
System.out.println(total_bus);
// Traverse through all the USB buses.
for (int i = 0; i < total_bus; i++) {
// Access the root hub on the USB bus and obtain the
// number of USB ports available on the root hub.
Device root = bus[i].getRootHub();
int total_port = root.getNumPorts();
// Traverse through all the USB ports available on the
// root hub. It should be mentioned that the numbering
// starts from 1, not 0.
for (int j = 1; j <= total_port; j++) {
// Obtain the Device connected to the port.
Device device = root.getChild(j);
if (device != null) {
// USB device available, do something here.
// Obtain the current Configuration of the device
// and the number of interfaces available under the
// current Configuration.
Configuration config = device.getConfiguration();
int total_interface = config.getNumInterfaces();
// Traverse through the Interfaces
for (int k = 0; k < total_interface; k++) {
// Access the current Interface and obtain the
// number of endpoints available on the Interface
Interface itf = config.getInterface(k, 0);
int total_ep = itf.getNumEndpoints();
// Traverse through all the endpoints.
for (int l = 0; l < total_ep; l++) {
// Access the endpoint and
// obtain its I/O type
Endpoint ep = itf.getEndpoint(l);
String io_type = ep.getType();
boolean input = ep.isInput();
// If the endpoint is an input endpoint,
// obtain its InputStream and read in data.
if (input) {
InputStream in;
in = ep.getInputStream();
// Read in data here
in.close();
}
else {
// If the Endpoint is an output Endpoint,
// obtain its OutputStream and
// write out data.
OutputStream out;
out = ep.getOutputStream();
// Write out data here.
out.close();
}
}
}
}
}
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
I Googled that error message and I found:
A forum post from 2005 that said that on Linux this can be due to something else having grabbed exclusive use of the USB controller: http://bytes.com/topic/java/answers/16736-jusb
An online copy of the source code, which indicates that this happens if getHost's attempt to create a (platform specific) HostFactory fails. Unfortunately, the code eats unexpected exceptions (*), so it you'll need to use a Java debugger to figure out what the real code is.
(* The code catches Exception in maybeGetHost and other places and throws away the diagnostics! This is a major no-no, and a big red flag on overall code quality of the library. If I were you, I'd be looking for a better quality library to use.)