package org.bouncycastle.crypto.tls;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPTransport
    implements DatagramTransport
{

    private final static int MIN_IP_OVERHEAD = 20;
    private final static int MAX_IP_OVERHEAD = MIN_IP_OVERHEAD + 64;
    private final static int UDP_OVERHEAD = 8;

    private final DatagramSocket socket;
    private final int receiveLimit, sendLimit;

    public UDPTransport(DatagramSocket socket, int mtu)
        throws IOException
    {

        if (!socket.isBound() || !socket.isConnected())
        {
            throw new IllegalArgumentException("'socket' must be bound and connected");
        }

        this.socket = socket;

        // NOTE: As of JDK 1.6, can use NetworkInterface.getMTU

        this.receiveLimit = mtu - MIN_IP_OVERHEAD - UDP_OVERHEAD;
        this.sendLimit = mtu - MAX_IP_OVERHEAD - UDP_OVERHEAD;
    }

    public int getReceiveLimit()
    {
        return receiveLimit;
    }

    public int getSendLimit()
    {
        // TODO[DTLS] Implement Path-MTU discovery?
        return sendLimit;
    }

    public int receive(byte[] buf, int off, int len, int waitMillis)
        throws IOException
    {
        socket.setSoTimeout(waitMillis);
        DatagramPacket packet = new DatagramPacket(buf, off, len);
        socket.receive(packet);
        return packet.getLength();
    }

    public void send(byte[] buf, int off, int len)
        throws IOException
    {
        if (len > getSendLimit())
        {
            /*
             * RFC 4347 4.1.1. "If the application attempts to send a record larger than the MTU,
             * the DTLS implementation SHOULD generate an error, thus avoiding sending a packet
             * which will be fragmented."
             */
            // TODO Exception
        }

        DatagramPacket packet = new DatagramPacket(buf, off, len);
        socket.send(packet);
    }

    public void close()
        throws IOException
    {
        socket.close();
    }
}

@@ -185,28 +185,29 @@
 sport%0A%7B%0A
-%0A
     pr
-iva
+otec
 te
+d
  final s
@@ -236,37 +236,39 @@
 EAD = 20;%0A    pr
-iva
+otec
 te
+d
  final static in
@@ -311,29 +311,31 @@
 + 64;%0A    pr
-iva
+otec
 te
+d
  final stati
@@ -361,29 +361,31 @@
 = 8;%0A%0A    pr
-ivate
+otected
  final Datag
@@ -412,13 +412,15 @@
   pr
-ivate
+otected
  fin
@@ -541,17 +541,16 @@
 n%0A    %7B%0A
-%0A
         
@@ -1829,25 +1829,65 @@
     
-// TODO Exception
+throw new TlsFatalAlert(AlertDescription.internal_error);
 %0A   

Make fields protected Throw internal_error for sends exceeding limit