package org.graylog2.syslog4j.server.impl.net.udp;

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

import org.graylog2.syslog4j.SyslogConstants;
import org.graylog2.syslog4j.SyslogRuntimeException;
import org.graylog2.syslog4j.server.SyslogServerEventIF;
import org.graylog2.syslog4j.server.impl.AbstractSyslogServer;
import org.graylog2.syslog4j.util.SyslogUtility;

/**
 * UDPNetSyslogServer provides a simple non-threaded UDP/IP server implementation.
 * <p/>
 * <p>Syslog4j is licensed under the Lesser GNU Public License v2.1.  A copy
 * of the LGPL license is available in the META-INF folder in all
 * distributions of Syslog4j and in the base directory of the "doc" ZIP.</p>
 *
 * @author &lt;syslog4j@productivity.org&gt;
 * @version $Id: UDPNetSyslogServer.java,v 1.16 2010/11/12 03:43:15 cvs Exp $
 */
public class UDPNetSyslogServer extends AbstractSyslogServer {
    protected DatagramSocket ds = null;

    public void initialize() throws SyslogRuntimeException {
        //
    }

    public void shutdown() {
        super.shutdown();

        if (this.syslogServerConfig.getShutdownWait() > 0) {
            SyslogUtility.sleep(this.syslogServerConfig.getShutdownWait());
        }

        if (this.ds != null && !this.ds.isClosed()) {
            this.ds.close();
        }
    }

    protected DatagramSocket createDatagramSocket() throws SocketException, UnknownHostException {
        DatagramSocket datagramSocket = null;

        if (this.syslogServerConfig.getHost() != null) {
            InetAddress inetAddress = InetAddress.getByName(this.syslogServerConfig.getHost());

            datagramSocket = new DatagramSocket(this.syslogServerConfig.getPort(), inetAddress);

        } else {
            datagramSocket = new DatagramSocket(this.syslogServerConfig.getPort());
        }

        return datagramSocket;
    }

    public void run() {
        try {
            this.ds = createDatagramSocket();
            this.shutdown = false;

        } catch (SocketException se) {
            return;

        } catch (UnknownHostException uhe) {
            return;
        }

        byte[] receiveData = new byte[SyslogConstants.SYSLOG_BUFFER_SIZE];

        handleInitialize(this);

        while (!this.shutdown) {
            DatagramPacket dp = null;

            try {
                dp = new DatagramPacket(receiveData, receiveData.length);

                this.ds.receive(dp);

                SyslogServerEventIF event = createEvent(this.getConfig(), receiveData, dp.getLength(), dp.getAddress());

                handleEvent(null, this, dp, event);

            } catch (SocketException se) {
                int i = se.getMessage() == null ? -1 : se.getMessage().toLowerCase().indexOf("socket closed");

                if (i == -1) {
                    handleException(null, this, dp.getSocketAddress(), se);
                }

            } catch (IOException ioe) {
                handleException(null, this, dp.getSocketAddress(), ioe);
            }
        }

        handleDestroy(this);
    }
}

@@ -2634,24 +2634,70 @@
 ive(dp);%0D%0A%0D%0A
+                if(dp.getLength() %3E 0) %7B%0D%0A    
             
@@ -2802,26 +2802,28 @@
 ddress());%0D%0A
-%0D%0A
+    
             
@@ -2859,24 +2859,41 @@
 p, event);%0D%0A
+                %7D
 %0D%0A          

Add length check of UDP packet to UDPNetSyslogServer#run()