8

I am using android Studio for the app development and i want to set the DSCP value in the ip header using UDP sockets. I am following this example.

import android.os.Message;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UdpClientThread extends Thread{

    String dstAddress;
    int dstPort;
    private boolean running;
    MainActivity.UdpClientHandler handler;
    DatagramSocket socket;
    InetAddress address;

    public UdpClientThread(String addr, int port, MainActivity.UdpClientHandler handler) {
        super();
        dstAddress = addr;
        dstPort = port;
        this.handler = handler;

    }

    public void setRunning(boolean running){
        this.running = running;
    }

    private void sendState(String state){
        handler.sendMessage(
                Message.obtain(handler,
                        MainActivity.UdpClientHandler.UPDATE_STATE, state));
    }

    @Override
    public void run() {
        sendState("connecting...");

        running = true;
        System.setProperty("java.net.preferIPv4Stack", "true");


        try {
            socket = new DatagramSocket();
            socket.setTrafficClass(128); //Setting the DSCP value
            address = InetAddress.getByName(dstAddress);

            // send request
            byte[] buf = new byte[256];

            DatagramPacket packet =
                    new DatagramPacket(buf, buf.length, address, dstPort);


            socket.send(packet);

            sendState("connected");

            // get response
            packet = new DatagramPacket(buf, buf.length);


            socket.receive(packet);
            String line = new String(packet.getData(), 0, packet.getLength());

            handler.sendMessage(
                    Message.obtain(handler, MainActivity.UdpClientHandler.UPDATE_MSG, line));

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(socket != null){
                socket.close();
                handler.sendEmptyMessage(MainActivity.UdpClientHandler.UPDATE_END);
            }
        }

    }
}

I have searched on this forum and i came to know that using System.setProperty("java.net.preferIPv4Stack", "true") we can manipulate the DSCP values. But this seems not be working on the android app. How can i achieve the desired behavior? Am i missing something over here? The code works without any errors but when i check in the wireshark(capturing 'any' interface and then applying the filter for udp) the value of DSCP of the packet, it is unchanged. I am using Emulator on ubuntu 16 to to test the scenario. Any help is appreciated.

Community
  • 1
  • 1
Hassan Abbas
  • 1,166
  • 20
  • 47
  • 1
    Have you ever tried it on a physical device? – Sebastian Hojas May 23 '17 at 16:51
  • @Sebastian Hojas, [`DatagramSocket.receive()` is a blocking call](https://stackoverflow.com/questions/40055175/how-to-interrupt-a-blocking-call-to-udp-sockets-receive/40055466#40055466), so the packet may not be handled. Are you sure the code lines below the call are being executed? – Onik May 24 '17 at 21:15

1 Answers1

1

The above code works on the android device(tested on samsung galaxy S4) but not on the emulator. However, i was not able to set this System.setProperty("java.net.preferIPv4Stack", "true"); at run time through the code. The DSCP can be set without explicitly setting this property in android. In addition to this, if you want to change the DSCP value in the IP header using simple java program other than the android, you can take a look at this answer.

Hassan Abbas
  • 1,166
  • 20
  • 47
  • Excuse me. Is there a way to set DSCP value on `HttpUrlConnection` but not `Socket`? – Yeung Jan 15 '18 at 08:43
  • @Yeung, you can check the getPermission() method of the HttpUrlConnection. My idea is that you get the socket and then set the DSCP Class on that socket of the http url connection. – Hassan Abbas Jan 16 '18 at 09:55