Commit 3f6eb0e7 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Added NonNull/Nullable annotations

parent e8f33126
...@@ -4,6 +4,7 @@ import android.net.VpnService; ...@@ -4,6 +4,7 @@ import android.net.VpnService;
import android.os.Build; import android.os.Build;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import android.system.ErrnoException; import android.system.ErrnoException;
import android.system.Os; import android.system.Os;
...@@ -198,7 +199,7 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -198,7 +199,7 @@ public class DNSTCPProxy extends DNSProxy{
} }
private int pollTries = 0; private int pollTries = 0;
private void poll(StructPollfd[] polls, int timeout) throws ErrnoException { private void poll(@NonNull StructPollfd[] polls, int timeout) throws ErrnoException {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){ if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
pollTries++; pollTries++;
try{ try{
...@@ -214,7 +215,7 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -214,7 +215,7 @@ public class DNSTCPProxy extends DNSProxy{
} }
} }
private void handleDeviceDNSPacket(InputStream inputStream, byte[] packetBytes) throws IOException{ private void handleDeviceDNSPacket(@NonNull InputStream inputStream, @NonNull byte[] packetBytes) throws IOException{
packetBytes = Arrays.copyOfRange(packetBytes, 0, inputStream.read(packetBytes)); packetBytes = Arrays.copyOfRange(packetBytes, 0, inputStream.read(packetBytes));
IpPacket packet; IpPacket packet;
try { try {
...@@ -255,7 +256,7 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -255,7 +256,7 @@ public class DNSTCPProxy extends DNSProxy{
} }
} }
private void sendPacketToUpstreamDNSServer(DatagramPacket outgoingPacket, IpPacket ipPacket){ private void sendPacketToUpstreamDNSServer(@NonNull DatagramPacket outgoingPacket, @Nullable IpPacket ipPacket){
try{ try{
Socket socket = SocketChannel.open().socket(); Socket socket = SocketChannel.open().socket();
vpnService.protect(socket); //The sent packets shouldn't be handled by this class vpnService.protect(socket); //The sent packets shouldn't be handled by this class
...@@ -271,13 +272,13 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -271,13 +272,13 @@ public class DNSTCPProxy extends DNSProxy{
outputStream.close(); //Closes the associated socket outputStream.close(); //Closes the associated socket
} }
}catch(IOException exception){ }catch(IOException exception){
if(!(exception instanceof SocketTimeoutException)){ if(!(exception instanceof SocketTimeoutException) && ipPacket != null){
handleUpstreamDNSResponse(ipPacket, outgoingPacket.getData()); handleUpstreamDNSResponse(ipPacket, outgoingPacket.getData());
} }
} }
} }
private void handleRawUpstreamDNSResponse(Socket dnsSocket, IpPacket parsedPacket){ private void handleRawUpstreamDNSResponse(@NonNull Socket dnsSocket, @NonNull IpPacket parsedPacket){
try { try {
DataInputStream inputStream = new DataInputStream(dnsSocket.getInputStream()); DataInputStream inputStream = new DataInputStream(dnsSocket.getInputStream());
byte[] data = new byte[inputStream.readUnsignedShort()]; byte[] data = new byte[inputStream.readUnsignedShort()];
...@@ -288,7 +289,7 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -288,7 +289,7 @@ public class DNSTCPProxy extends DNSProxy{
} }
} }
private void handleUpstreamDNSResponse(IpPacket packet, byte[] payloadData){ private void handleUpstreamDNSResponse(@NonNull IpPacket packet, @NonNull byte[] payloadData){
UdpPacket dnsPacket = (UdpPacket) packet.getPayload(); UdpPacket dnsPacket = (UdpPacket) packet.getPayload();
UdpPacket.Builder dnsPayloadBuilder = new UdpPacket.Builder(dnsPacket) UdpPacket.Builder dnsPayloadBuilder = new UdpPacket.Builder(dnsPacket)
.srcPort(dnsPacket.getHeader().getDstPort()) .srcPort(dnsPacket.getHeader().getDstPort())
...@@ -356,15 +357,15 @@ public class DNSTCPProxy extends DNSProxy{ ...@@ -356,15 +357,15 @@ public class DNSTCPProxy extends DNSProxy{
} }
private class PacketWrap{ private class PacketWrap{
private IpPacket packet; @NonNull private IpPacket packet;
private final long time; private final long time;
PacketWrap(IpPacket packet) { PacketWrap(@NonNull IpPacket packet) {
this.packet = packet; this.packet = packet;
this.time = System.currentTimeMillis(); this.time = System.currentTimeMillis();
} }
IpPacket getPacket() { @NonNull IpPacket getPacket() {
return packet; return packet;
} }
......
...@@ -3,6 +3,8 @@ package com.frostnerd.dnschanger.util.dnsproxy; ...@@ -3,6 +3,8 @@ package com.frostnerd.dnschanger.util.dnsproxy;
import android.net.VpnService; import android.net.VpnService;
import android.os.Build; import android.os.Build;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import android.system.ErrnoException; import android.system.ErrnoException;
import android.system.Os; import android.system.Os;
...@@ -190,7 +192,7 @@ public class DNSUDPProxy extends DNSProxy{ ...@@ -190,7 +192,7 @@ public class DNSUDPProxy extends DNSProxy{
} }
private int pollTries = 0; private int pollTries = 0;
private void poll(StructPollfd[] polls, int timeout) throws ErrnoException { private void poll(@NonNull StructPollfd[] polls, int timeout) throws ErrnoException {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){ if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
pollTries++; pollTries++;
try{ try{
...@@ -206,7 +208,7 @@ public class DNSUDPProxy extends DNSProxy{ ...@@ -206,7 +208,7 @@ public class DNSUDPProxy extends DNSProxy{
} }
} }
private void handleDeviceDNSPacket(InputStream inputStream, byte[] packetBytes) throws IOException{ private void handleDeviceDNSPacket(@NonNull InputStream inputStream, @NonNull byte[] packetBytes) throws IOException{
packetBytes = Arrays.copyOfRange(packetBytes, 0, inputStream.read(packetBytes)); packetBytes = Arrays.copyOfRange(packetBytes, 0, inputStream.read(packetBytes));
IpPacket packet; IpPacket packet;
try { try {
...@@ -247,7 +249,7 @@ public class DNSUDPProxy extends DNSProxy{ ...@@ -247,7 +249,7 @@ public class DNSUDPProxy extends DNSProxy{
} }
} }
private void sendPacketToUpstreamDNSServer(DatagramPacket outgoingPacket, IpPacket ipPacket){ private void sendPacketToUpstreamDNSServer(@NonNull DatagramPacket outgoingPacket, @Nullable IpPacket ipPacket){
try{ try{
DatagramSocket socket = new DatagramSocket(); DatagramSocket socket = new DatagramSocket();
vpnService.protect(socket); //The sent packets shouldn't be handled by this class vpnService.protect(socket); //The sent packets shouldn't be handled by this class
...@@ -255,23 +257,20 @@ public class DNSUDPProxy extends DNSProxy{ ...@@ -255,23 +257,20 @@ public class DNSUDPProxy extends DNSProxy{
if(ipPacket != null) futureSocketAnswers.put(socket, new PacketWrap(ipPacket)); if(ipPacket != null) futureSocketAnswers.put(socket, new PacketWrap(ipPacket));
else socket.close(); else socket.close();
}catch(IOException exception){ }catch(IOException exception){
LogFactory.writeStackTrace(vpnService, LOG_TAG, exception); if(ipPacket != null)handleUpstreamDNSResponse(ipPacket, outgoingPacket.getData());
handleUpstreamDNSResponse(ipPacket, outgoingPacket.getData());
} }
} }
private void handleRawUpstreamDNSResponse(DatagramSocket dnsSocket, IpPacket parsedPacket){ private void handleRawUpstreamDNSResponse(@NonNull DatagramSocket dnsSocket, @NonNull IpPacket parsedPacket){
try { try {
byte[] datagramData = new byte[1024]; byte[] datagramData = new byte[1024];
DatagramPacket replyPacket = new DatagramPacket(datagramData, datagramData.length); DatagramPacket replyPacket = new DatagramPacket(datagramData, datagramData.length);
dnsSocket.receive(replyPacket); dnsSocket.receive(replyPacket);
handleUpstreamDNSResponse(parsedPacket, datagramData); handleUpstreamDNSResponse(parsedPacket, datagramData);
} catch (IOException e) { } catch (IOException ignored) {}
LogFactory.writeStackTrace(vpnService, LOG_TAG, e);
}
} }
private void handleUpstreamDNSResponse(IpPacket packet, byte[] payloadData){ private void handleUpstreamDNSResponse(@NonNull IpPacket packet, @NonNull byte[] payloadData){
UdpPacket dnsPacket = (UdpPacket) packet.getPayload(); UdpPacket dnsPacket = (UdpPacket) packet.getPayload();
UdpPacket.Builder dnsPayloadBuilder = new UdpPacket.Builder(dnsPacket) UdpPacket.Builder dnsPayloadBuilder = new UdpPacket.Builder(dnsPacket)
.srcPort(dnsPacket.getHeader().getDstPort()) .srcPort(dnsPacket.getHeader().getDstPort())
...@@ -335,15 +334,15 @@ public class DNSUDPProxy extends DNSProxy{ ...@@ -335,15 +334,15 @@ public class DNSUDPProxy extends DNSProxy{
} }
private class PacketWrap{ private class PacketWrap{
private IpPacket packet; @NonNull private IpPacket packet;
private final long time; private final long time;
PacketWrap(IpPacket packet) { PacketWrap(@NonNull IpPacket packet) {
this.packet = packet; this.packet = packet;
this.time = System.currentTimeMillis(); this.time = System.currentTimeMillis();
} }
IpPacket getPacket() { @NonNull IpPacket getPacket() {
return packet; return packet;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment