//-----------------------------------------------------------------------------
// File:  udp.hh
// Date: Thu May  8 11:05:53 PDT 2003
// Author: Norman Franke, Martin Casado
//
// Description:
//
// udp header construction
//
// ----------------------------------------------------------------------------
//
//    Copyright 2003. The Regents of the University of California.
//    All rights reserved.
//
//    This work was produced at the University of California, Lawrence Livermore
//    National Laboratory (UC LLNL) under contract no. W-7405-ENG-48 (Contract
//    48) between the U.S. Department of Energy (DOE) and The Regents of the
//    University of California (University) for the operation of UC LLNL.
//    Copyright is reserved to the University for purposes of controlled
//    dissemination, commercialization through formal licensing, or other
//    disposition under terms of Contract 48; DOE policies, regulations and
//    orders; and U.S. statutes. The rights of the Federal Government are
//    reserved under Contract 48 subject to the restrictions agreed upon by the
//    DOE and University as allowed under DOE Acquisition Letter 91-7.
//
//    DISCLAIMER
//    This software was prepared as an account of work sponsored by an
//    agency of the United States Government. Neither the United States
//    Government nor the University of California nor any of their employees,
//    makes any warranty, express or implied, or assumes any liability or
//    responsibility for the accuracy, completeness, or usefulness of any
//    information, apparatus, product, or process disclosed, or represents that
//    its use would not infringe protectedly-owned rights. Reference herein to any
//    specific commercial products, process, or service by trade name, trademark,
//    manufacturer, or otherwise, does not necessarily constitute or imply its
//    endorsement, recommendation, or favoring by the United States Government or
//    the University of California. The views and opinions of authors expressed
//    herein do not necessarily represent those of the United States
//    Government or the University of California, and shall not be used for
//    advertising or product endorsement purposes.
//
//-----------------------------------------------------------------------------

#ifndef UDP_HH
#define UDP_HH

#include "static_type_inference.hh"
#include "ip.hh"

extern "C"
{
#include <stdint.h>
#include <assert.h>
}

namespace pacman
{

template <typename T> struct udp;

//-----------------------------------------------------------------------------
struct udp_base : public ip_proto, public ip_transport
{
    // So IP knows our protocol
    static const uint8_t IPPROTO = ip_base::UDP;
    static const uint8_t DEF_PORT = 0;

    template <typename T>
    static void init(udp<T>& );

}__attribute__ ((__packed__));
//-----------------------------------------------------------------------------

template<typename T> struct udp;

//-----------------------------------------------------------------------------
template <>
struct udp<empty> : public udp_base
{
    typedef empty PayloadType;

    uint16_t sport;
    uint16_t dport;
    uint16_t len;       // including UDP header
    uint16_t check;		// checksum

    udp() { init(*this); }
}__attribute__ ((__packed__));
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
template <typename T = empty>
struct udp : public udp<empty>
{
    typedef T PayloadType;

    T        data;

    udp() { init(*this); }

}__attribute__ ((__packed__));
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
template <typename T>
inline void
udp_base::init(udp<T>& in)
{
    in.sport = 0;
    in.dport = htons_<get_ip_port<T, udp_base::DEF_PORT>::val>::val;
    in.len   = htons_<hdr<udp<T> >::len + szof<T>::len>::val;
    in.check = 0;
}
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
typedef pacman::ip< empty, pacman::udp< uint8_t[1500] > > udp_mtu_t;
//-----------------------------------------------------------------------------


} // -- namespace pacman


#endif  //-- UDP_HH
