/* Copyright (c) 1999 by Inria Lorraine.  All Rights Reserved */

/***
   NAME
     RawPacket
   PURPOSE
     
   NOTES
     
   HISTORY
     chelius - Jul 25, 1999: Created.
     $Log: RawPacket.java,v $
     Revision 1.2  2000/04/03 08:05:34  chelius
     Javadoc

     Revision 1.1.1.1  2000/02/08 16:31:59  chelius
     Initial import

     Revision 1.1.1.1  2000/02/01 11:10:46  chelius
     Mise sous CVS des sources de libnet6 for jdk1.2.*

     Revision 1.1.1.1  1999/07/25 15:01:57  chelius
     First release of the ipv6 packages for java

***/
package java.net;


import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InterruptedIOException;

/**
 * This class represents a raw packet. 
 * <p>
 * Raw packets are used to implement a connectionless raw packet 
 * delivery service. Each message is routed from one machine to 
 * another based solely on information contained within that packet. 
 * Multiple packets sent from one machine to another might be routed 
 * differently, and might arrive in any order. 
 *
 * @author  Guillaume Chelius (IPv6 modifications)
 * @version 1.0, 02/04/2000
 * @since   JDK1.2
 */
public  
class RawPacket {

    /**
     * Perform class initialization
     */
    static {
	java.security.AccessController.doPrivileged(
		  new sun.security.action.LoadLibraryAction("net6"));
	init();
    }

    /*
     * The fields of this class are package-private since RawSocketImpl 
     * classes needs to access them.
     */
    byte[] buf;
    int offset;
    int length;
    InetAddress address;

    /**
     * Constructs a <code>RawPacket</code> for receiving packets of 
     * length <code>ilength</code>. 
     * <p>
     * The <code>length</code> argument must be less than or equal to 
     * <code>ibuf.length</code>. 
     *
     * @param   buf      buffer for holding the incoming datagram.
     * @param   length   the number of bytes to read.
     * @since   JDK1.2
     */
    public RawPacket(byte buf[], int length) {
	this (buf, 0, length);
    }    

    /**
     * Constructs a <code>RawPacket</code> for receiving packets of 
     * length <code>ilength</code>. 
     * 
     *
     * @param   buf      buffer for holding the incoming datagram.
     * @param   offset   offset to put the data at.
     * @param   length   the number of bytes to read.
     * @since   JDK1.2
     */
    public RawPacket(byte buf[], int offset, int length) {
	setData(buf, offset, length);
	this.address = null;
    }

    /**
     * Constructs a raw packet for sending packets of length 
     * <code>ilength</code> on the specified 
     * host. 
     *
     * @param   buf      the packet data.
     * @param   length   the packet length.
     * @param   offset   offset to put the data at.
     * @param   addres     the destination address.
     * @see     java.net.InetAddress
     * @since   JDK1.2
     */
    public RawPacket(byte buf[], int offset, int length,
			  InetAddress address) {
	setData(buf, offset, length);
	setAddress(address);
    }

    /**
     * Constructs a raw packet for sending packets of length 
     * <code>ilength</code> on the specified 
     * host. 
     *
     * @param   buf      the packet data.
     * @param   length   the packet length.
     * @param   addres     the destination address.
     * @see     java.net.InetAddress
     * @since   JDK1.2
     */
    public RawPacket(byte buf[], int length,
			  InetAddress address) {
	this(buf, 0, length, address);
    }    

    /**
     * Returns the IP address of the machine to which this packet is being
     * sent or from which the packet was received.
     *
     * @return  the IP address of the machine to which this packet is being
     *          sent or from which the packet was received.
     * @see     java.net.InetAddress
     * @since   JDK1.2
     */
    public synchronized InetAddress getAddress() {
	return address;
    }
    
    /**
     * Returns the data received or the data to be sent.
     *
     * @return  the data received or the data to be sent.
     * @since   JDK1.2
     */
    public synchronized byte[] getData() {
	return buf;
    }
    
    /**
     * Returns the current position of the offset
     *
     * @return  the offset that is curently used
     * @since   JDK1.2
     */
    public synchronized int getOffset() {
	return offset;
    }

    /**
     * Returns the length of the data to be sent or the length of the
     * data received.
     *
     * @return  the length of the data to be sent or the length of the
     *          data received.
     * @since   JDK1.2
     */    
    public synchronized int getLength() {
	return length;
    }


    /**
     * Insert data of size <code>length<\code> at offset <code>offset<\code>.
     * 
     * @param buf the data to insert
     * @param offset the offset to put the data at
     * @param length the length of the data to insert
     */
    public synchronized void setData(byte[] buf, int offset, int length) {
	/* this will check to see if buf is null */
	if (length < 0 || offset < 0 ||
	    ((length + offset) > buf.length)) {
	    throw new IllegalArgumentException("illegal length or offset");
	}
	this.buf = buf;
	this.length = length;
	this.offset = offset;
    }


    /**
     * @since   JDK1.2
     */
    public synchronized void setAddress(InetAddress iaddr) {
	address = iaddr;
    }

    /**
     * Set the data in the packet
     *
     * @param buf the data to put in the packet
     */
    public synchronized void setData(byte[] buf) {
	if (buf == null) {
	    throw new NullPointerException("null packet buffer");
	}
	this.buf = buf;

	if (length > buf.length) {
	    setLength(buf.length);
	}
    }

    
    /**  
     * Set the length for this packet. The length of the packet is
     * the number of bytes from the packet's data buffer that will be
     * sent, or the number of bytes of the packet's data buffer that
     * will be used for receiving data. The length must be lesser or
     * equal to the length of the packet's buffer.
     *
     * @param length the length to set for this packet.
     * 
     * @exception IllegalArgumentException if the length is negative
     * of if the length is greater than the packet's data buffer
     * length.
     *
     * @see #getLength
     * @see #setData
     *
     * @since JDK1.2
     */
    public synchronized void setLength(int length) {
	if (length > buf.length || length < 0) {
	    throw new IllegalArgumentException("illegal length");
	}
	this.length = length;
    }

    /**
     * Perform class load-time initializations.  
     */
    private native static void init();
}

