L
L
lenston2015-03-08 21:20:45
Java
lenston, 2015-03-08 21:20:45

How to use RAW sockets in Java?

It is necessary to send a packet over the local network

without creating a server
without opening ports
without binding to IP.

In SI, the packet was simply spat out into the network, and the client knew what to accept and what to discard..
In Java, RAW is not supported..
How to send such a packet??

I wanted to use UDP, but it's bound to a port..

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
asd111, 2015-03-09
@lenston

Look for a java wrapper for libpcap - a library for working with raw packages over the network.
like this one should fit
jnetpcap.com
example of sending your package

package org.jnetpcap.examples;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;

public class PcapSendPacketExample {
  public static void main(String[] args) {
    List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs
    StringBuilder errbuf = new StringBuilder(); // For any error msgs

    /***************************************************************************
     * First get a list of devices on this system
     **************************************************************************/
    int r = Pcap.findAllDevs(alldevs, errbuf);
    if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
      System.err.printf("Can't read list of devices, error is %s", errbuf.toString());
      return;
    }
    PcapIf device = alldevs.get(0); // We know we have atleast 1 device

    /*****************************************
     * Second we open a network interface
     *****************************************/
    int snaplen = 64 * 1024; // Capture all packets, no trucation
    int flags = Pcap.MODE_PROMISCUOUS; // capture all packets
    int timeout = 10 * 1000; // 10 seconds in millis
    Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);


    /*******************************************************
     * Third we create our crude packet we will transmit out
     * This creates a broadcast packet
     *******************************************************/
    byte[] a = new byte[14];
    Arrays.fill(a, (byte) 0xff);
    ByteBuffer b = ByteBuffer.wrap(a);

    /*******************************************************
     * Fourth We send our packet off using open device
     *******************************************************/
    if (pcap.sendPacket(b) != Pcap.OK) {
      System.err.println(pcap.getErr());
    }

    /********************************************************
     * Lastly we close
     ********************************************************/
    pcap.close();
  }
}

There are more examples on the specified site in the documentation examples section, I think you will understand

N
Nikita, 2015-03-08
@jetu

How do you imagine sending a packet without opening ports?! Where to send it? And what does RAW sockets have to do with it?

L
lenston, 2015-03-09
@lenston

I did it like this in C.

data[]=//заполнял данные
int sock=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_802_3));
send(sock,data,PACKET_LENGTH,0);

and the client listened to everything that happened on the network ..
and, depending on the data, rejected or accepted the packet ..
you need to repeat something similar to Java

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question