Make custom ipv6 packets in java - java

I'm working on mobile ipv6 in java and want to make custom ipv6 packets.
how do I write class to make ipv6 packets and I put the payload myself?
Following is the example of the similar class:
CustomIPv6 ipv6 = new CustomIPv6();
ipv6.setSource("::1");
ipv6.setDestination("::1");
ipv6.setPayload(MyArrayOfByte); //The byte array which I made it myself. -> the mobile ipv6 extention
ipv6.setUpperLayerType(CustomIPv6.MobilityHeader);
ipv6.sendPacket();
byte[] recv = ipv6.ReceivPacket();
//Now is the time for process the received packet and send some kind of ack.
Thanks all friends in advance.

You can do this with http://jnetpcap.com/ however it requires libpcap or winpcap be installed. This facility is useful for network hacking which could be why it is not better supported. ;)

Related

How to extract the ip address from the captured packet in java in windows?

I am working on Network Monitoring. I wish to monitor the data and check the network usage details of the client PC from the admin PC.
I have figured that I can fulfill this with the help of captured the packets of the browser as shown below. (This is the packet of FB request)
New packet1459690210:290428 /192.168.0.138->/31.13.79.220 protocol(6) priority(0) hop(128) offset(0) ident(2687) TCP 49281 > 443 seq(1708554008) win(258) ack 938747547 P
But, how can I extract the ipaddress of destination from the captured packets.
I wish to store the data in the database and display as below in the image.
I also wish to have data usage in MB.
Many thanks for help.
(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\d*
This is what I got. This regex retrieves the ip addresses.
I still have to figure out how to get the used MBs. If any one can help, I shall be greatful

sending a mesage with a header and body out a serial port with java

I've searched high and low and havn't been able to find an answer to this question. I'm fairly certain that it isn't difficult, probably a case of my searches not using the right verbiage.
I'm working on communicating with a gimbal controller. I've been able to connect to the serial port that it's connected to. I'm unable to find information on how to format a message like this:
Each command consists
of the header and the body, both with checksum. Commands with the wrong header or body checksum, or
with the body size that differs from expected, should be ignored.
Can anyone point me in the direction on how to write to the serial port like that?
Thank you,
Loren
There are two typical approaches for communicating with a device over a serial port. In both cases, the end result is raw bits being sent over the wire. You can send ASCII strings if your device expects it but since your excerpt specifically mentioned packet packaging, I would venture to say that they want bytes.
The excerpt you copy pasted sounds like it came from a manual that explains the exact protocol that is required to communicate. In a nutshell, you will be doing the following.
Setup the serial port
Prepare your serial data
Send your serial data
I like to use jSSC for serial comms though lots of other folks use RXTX. jSSC has been more reliable for me so that's what I'll be using in my example. The manual for your device should specify the required baud rate, data bits, stop bits, parity, and handshake (if any).
Here we setup a port (replace your parameters as needed). See the docs on the details of this. https://github.com/scream3r/java-simple-serial-connector
SerialPort _port = new SerialPort(portName);
_port .openPort();
_port.setParams(baudRate, dataBits, stopBits, parity, setRTS, setDTR);
_port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
To construct your data packet, you will need to know:
Header format
Length requirement/restrictions
Checksum method
Here I am going to make a super simple packet that is just an example and is most likely not applicable to your use case.
// Format is: [length][7 data bytes][8 bit additive checksum]
// Create an empty byte array
byte[] packet = new byte[8];
// Our simple header
packet[0] = packet.length;
// Some data
byte[] dummyData = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
// Copy data to packet
System.arraycopy(dummyData, 0, packet, 0, dummyData.length);
// Go do the checksum (good exercise for you)
byte checksum = getChecksum(packet);
packet[7] = checksum;
So now we have a port, some data... and now what? Let's send it.
// Just send those bytes
_port.writeBytes(packet);
Once you get the hang of things, read into how you read the response, setup OnDataReceivedEvents, and how to more efficiently create packets. Some good terms to Google:
jSSC
Java RXTX
Baud Rate
Async/Sync data handling

Cannot capture my UDP packages broadcasted to 255.255.255.255, but can see other's

I have read the Q&A's about broadcasting and I got the general idea. However, when playing with my Java code, I am confused.
My IP address is 192.168.8.102. When I broadcast to 192.168.8.255, I can receive the package that from myself and have it captured on wireshark. But when I broadcast to 255.255.255.255, the wireshark seems missing it though my code still receives it. When I run someone else's app, the wireshark captures the package that it broadcasts to 255.255.255.255.
Any explainations? Much appreciated!
PS. here is part of my Java code:
DatagramSocket senderSocket = new DatagramSocket(null);
senderSocket.setReuseAddress(true);
senderSocket.setBroadcast(true);
senderSocket.bind(new InetSocketAddress(2000));
InetAddress address = InetAddress.getByName("255.255.255.255");
byte[] SendBuffer = contentSent.getBytes();
senderPacket = new DatagramPacket(SendBuffer, SendBuffer.length, address, 2000);
senderSocket.send(senderPacket);
If your socket is not bound to a specific interface, packets send to 255.255.255.255 will be sent out on the default interface. If that's not the one Wireshark is listening on, that explains why it doesn't see it but your code does.
If your socket is bound to a specific interface, that guarantees that sending on that socket goes out on that interface.
As EJP mentioned in the comments, broadcasting to 255.255.255.255 is not recommended, in part because of the limitation I mentioned. You're better off using the broadcast address for the link in question, i.e. 192.168.8.255 in your case.

How could I sniff network traffic in Java?

I was just looking around to find out how to make a program that would sniff my network traffic in Java, but I couldn't find anything. I wanted to know if there was any way to view the network traffic going by. I heard of an idea with a Socket, but I don't get how that would work. So anyways, just looking for an API or a way to write it myself.
EDIT:
I would gladly like an API, but I would also like clarification on the way to sniff traffic with a Socket.
jpcap, jNetPcap -- those are pcap wrapper projects in Java.
Kraken -- similar project, well documented with lots of examples.
simple example from the Kraken web site:
public static void main(String[] args) throws Exception {
File f = new File("sample.pcap");
EthernetDecoder eth = new EthernetDecoder();
IpDecoder ip = new IpDecoder();
TcpDecoder tcp = new TcpDecoder(new TcpPortProtocolMapper());
UdpDecoder udp = new UdpDecoder(new UdpPortProtocolMapper());
eth.register(EthernetType.IPV4, ip);
ip.register(InternetProtocol.TCP, tcp);
ip.register(InternetProtocol.UDP, udp);
PcapInputStream is = new PcapFileInputStream(f);
while (true) {
// getPacket() will throws EOFException and you should call is.close()
PcapPacket packet = is.getPacket();
eth.decode(packet);
}
}
Another Java libpcap wrapper is https://github.com/kaitoy/pcap4j
Pcap4J is a Java library for capturing, crafting and sending packets. Pcap4J wraps a native packet capture library (libpcap or WinPcap) via JNA and provides you Java-Oriented APIs.
You need a packet sniffer api, maybe netutils is what you need:
The 'netutils' package gives a low level java network library. It
contains extensive infrastructure for sniffing, injecting, building
and parsing Ethernet/IP/TCP/UDP/ICMP packets.
Not telling any API or java related thing but if you really want to only sniff data for analysis purpose then give try: WireShark. Its an application used for network analyse.
Its useful if someone is not aware of.

Listening to a multicast UDP address

I wrote an application in MATLAB to open a UDP socket and listen for incoming datagrams. Basically, something like this:
u = udp(rHost, rPort, 'LocalHost', lHost, 'LocalPort', lPort);
u.DatagramAvailableFcn = #(o,e) operateOnData(o,e);
fopen(u);
This works wonderfully when I'm listening to something in a unicast fashion. But I would now like to be able to listen to multicast traffic. Apparently, this isn't possible in MATLAB.
The workaround is, per above link,
As a workaround to connect to a UDP multicast, you can do the following:
Use a Java multicast socket to access it directly from MATLAB. For more information, see javadoc or tutorials for the "core java.net" classes from Sun, specifically "java.net.MulticastSocket". This could be found at:
http://java.sun.com/j2se/1.4.2/docs/api/java/net/MulticastSocket.html
I have no background in Java so this is a struggle for me. I've only been able to run the following to instantiate a MulticastSocket object:
>> ms = javaObject('java.net.MulticastSocket');
I looked around and found that I also need a java.net.Datagram object to actually contain the incoming stream.
How do I use the MulticastSocket and Datagram objects within the context of MATLAB? I'm trying to replicate the functionality of u.DatagramAvailableFcn, i.e., fire a callback to operate on the contents of the datagram once I receive one.
EDIT: Looks like this is how I want to go about this in terms of the Java, but now it's getting this back into MATLAB-land...
I successfully subscribed and received a packet from a multicast stream, by the following:
socket = java.net.MultiSocket(streamPort);
socket.joinGroup(java.net.InetAddress.getByName(streamIP));
socket.setReuseAddress(1);
packet = java.net.DatagramPacket(zeros(1, intmax('uint16'), 'int8'), intmax('uint16'));
socket.receive(packet);
socket.leaveGroup(InetAddress.getByName(streamIP));
socket.close;
msg = packet.getData;
msg = msg(1:packet.getLength);
This was essentially lifted from judp availble on the MathWorks File Exchange.
I am still looking for a way to get some equivalent of a DatagramReceivedFcn - right now it looks like the socket.receive call is blocking until it times out. I can use timer objects to fire the "callback" on a regular basis but that's of course not the same as having a DatagramReceivedFcn.

Categories