Code example for DatagramPacket

Methods: getData, getLength, getSocketAddress

0
    /** 
     * Sends a byte array as a DatagramPacket response to the client who sent the given DatagramPacket 
     */ 
    private void sendResponsePacket(byte[] par1ArrayOfByte, DatagramPacket par2DatagramPacket) throws IOException
    { 
        this.querySocket.send(new DatagramPacket(par1ArrayOfByte, par1ArrayOfByte.length, par2DatagramPacket.getSocketAddress()));
    } 
 
    /** 
     * Parses an incoming DatagramPacket, returning true if the packet was valid 
     */ 
    private boolean parseIncomingPacket(DatagramPacket par1DatagramPacket) throws IOException
    { 
        byte[] var2 = par1DatagramPacket.getData();
        int var3 = par1DatagramPacket.getLength();
        SocketAddress var4 = par1DatagramPacket.getSocketAddress();
        this.logDebug("Packet len " + var3 + " [" + var4 + "]");
 
        if (3 <= var3 && -2 == var2[0] && -3 == var2[1])
        { 
            this.logDebug("Packet \'" + RConUtils.getByteAsHexString(var2[2]) + "\' [" + var4 + "]");
 
            switch (var2[2])
            { 
                case 0: 
                    if (!this.verifyClientAuth(par1DatagramPacket).booleanValue())
                    { 
                        this.logDebug("Invalid challenge [" + var4 + "]");
                        return false; 
                    } 
                    else if (15 == var3)
                    { 
                        this.sendResponsePacket(this.createQueryResponse(par1DatagramPacket), par1DatagramPacket);
                        this.logDebug("Rules [" + var4 + "]");
                    } 
                    else 
                    { 
                        RConOutputStream var5 = new RConOutputStream(1460);
                        var5.writeInt(0);
                        var5.writeByteArray(this.getRequestID(par1DatagramPacket.getSocketAddress()));
                        var5.writeString(this.serverMotd);
                        var5.writeString("SMP");
                        var5.writeString(this.worldName);
                        var5.writeString(Integer.toString(this.getNumberOfPlayers()));
                        var5.writeString(Integer.toString(this.maxPlayers));
                        var5.writeShort((short)this.serverPort);
                        var5.writeString(this.queryHostname);
                        this.sendResponsePacket(var5.toByteArray(), par1DatagramPacket);
                        this.logDebug("Status [" + var4 + "]");
                    } 
 
                case 9: 
                    this.sendAuthChallenge(par1DatagramPacket);
                    this.logDebug("Challenge [" + var4 + "]");
                    return true; 
 
                default: 
                    return true; 
            } 
        } 
        else 
        { 
            this.logDebug("Invalid packet [" + var4 + "]");
            return false; 
        } 
    } 
 
    /** 
     * Creates a query response as a byte array for the specified query DatagramPacket 
     */ 
    private byte[] createQueryResponse(DatagramPacket par1DatagramPacket) throws IOException
    { 
        long var2 = System.currentTimeMillis();
 
        if (var2 < this.lastQueryResponseTime + 5000L)
        { 
            byte[] var7 = this.output.toByteArray();
            byte[] var8 = this.getRequestID(par1DatagramPacket.getSocketAddress());
            var7[1] = var8[0];
            var7[2] = var8[1];
            var7[3] = var8[2];
            var7[4] = var8[3];
            return var7;
        } 
        else 
        { 
            this.lastQueryResponseTime = var2;
            this.output.reset();
            this.output.writeInt(0);
            this.output.writeByteArray(this.getRequestID(par1DatagramPacket.getSocketAddress()));
            this.output.writeString("splitnum");
            this.output.writeInt(128);
            this.output.writeInt(0);
            this.output.writeString("hostname");
            this.output.writeString(this.serverMotd);
            this.output.writeString("gametype");
            this.output.writeString("SMP");
            this.output.writeString("game_id");
            this.output.writeString("MINECRAFT");
            this.output.writeString("version");
            this.output.writeString(this.server.getMinecraftVersion());
            this.output.writeString("plugins");
            this.output.writeString(this.server.getPlugins());
            this.output.writeString("map");
            this.output.writeString(this.worldName);
            this.output.writeString("numplayers");
            this.output.writeString("" + this.getNumberOfPlayers());
            this.output.writeString("maxplayers");
            this.output.writeString("" + this.maxPlayers);
            this.output.writeString("hostport");
            this.output.writeString("" + this.serverPort);
            this.output.writeString("hostip");
            this.output.writeString(this.queryHostname);
            this.output.writeInt(0);
            this.output.writeInt(1);
            this.output.writeString("player_");
            this.output.writeInt(0);
            String[] var4 = this.server.getAllUsernames();
            byte var5 = (byte)var4.length;
 
            for (byte var6 = (byte)(var5 - 1); var6 >= 0; --var6)
            { 
                this.output.writeString(var4[var6]);
            } 
 
            this.output.writeInt(0);
            return this.output.toByteArray();
        } 
    } 
 
    /** 
     * Returns the request ID provided by the authorized client 
     */ 
    private byte[] getRequestID(SocketAddress par1SocketAddress)
    { 
        return ((RConThreadQueryAuth)this.queryClients.get(par1SocketAddress)).getRequestId();
    } 
 
    /** 
     * Returns true if the client has a valid auth, otherwise false 
     */ 
    private Boolean verifyClientAuth(DatagramPacket par1DatagramPacket)
    { 
        SocketAddress var2 = par1DatagramPacket.getSocketAddress();
 
        if (!this.queryClients.containsKey(var2))
        { 
            return Boolean.valueOf(false);
        } 
        else 
        { 
            byte[] var3 = par1DatagramPacket.getData();
            return ((RConThreadQueryAuth)this.queryClients.get(var2)).getRandomChallenge() != RConUtils.getBytesAsBEint(var3, 7, par1DatagramPacket.getLength()) ? Boolean.valueOf(false) : Boolean.valueOf(true);
        } 
    } 
 
    /** 
     * Sends an auth challenge DatagramPacket to the client and adds the client to the queryClients map 
     */ 
    private void sendAuthChallenge(DatagramPacket par1DatagramPacket) throws IOException
    { 
        RConThreadQueryAuth var2 = new RConThreadQueryAuth(this, par1DatagramPacket);
        this.queryClients.put(par1DatagramPacket.getSocketAddress(), var2);
        this.sendResponsePacket(var2.getChallengeValue(), par1DatagramPacket);
    } 
 
    /** 
     * Removes all clients whose auth is no longer valid