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.
Go to Computer configuration > Windows Settings > Policy-based QoS and right click on “Create new policy”. Check “Specify DSCP Value” and use the desired value (for example, 8). Click “Next”. Check “Only application with this executable name:” and fill with Process Name (executable).
The DSCP code is set in the IPv4 packet header. Filter on packets that contain a specified DSCP value by using the following command. The DSCP value is extracted from the second byte of the IPv4 packet header, (ip[1] & 0xfc >> 2) , then matched against the hex representation of the DSCP class code to filter packets.
Differentiated Services Code Point (DSCP) is a means of classifying and managing network traffic and of providing quality of service (QoS) in modern Layer 3 IP networks. It uses the 6-bit Differentiated Services (DS) field in the IP header for the purpose of packet classification.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With