src/node/ethernet-header.cc
author Guillaume Vu-Brugier <gvubrugier@gmail.com>
Fri Mar 07 11:36:33 2008 -0800 (2008-03-07)
changeset 2360 ff54bd92e43c
parent 1505 286ec92f4113
child 2643 2a3324f4dabe
permissions -rw-r--r--
store length type field in ethernet packets in network order.
     1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2005 INRIA
     4  * All rights reserved.
     5  *
     6  * This program is free software; you can redistribute it and/or modify
     7  * it under the terms of the GNU General Public License version 2 as
     8  * published by the Free Software Foundation;
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program; if not, write to the Free Software
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18  *
    19  * Author: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
    20  */
    21 
    22 #include <iomanip>
    23 #include <iostream>
    24 #include "ns3/assert.h"
    25 #include "ns3/log.h"
    26 #include "ns3/header.h"
    27 #include "ethernet-header.h"
    28 #include "address-utils.h"
    29 
    30 NS_LOG_COMPONENT_DEFINE ("EthernetHeader");
    31 
    32 namespace ns3 {
    33 
    34 NS_HEADER_ENSURE_REGISTERED (EthernetHeader);
    35 
    36 uint32_t
    37 EthernetHeader::GetUid (void)
    38 {
    39   static uint32_t uid = AllocateUid<EthernetHeader> ("EthernetHeader.ns3");
    40   return uid;
    41 }
    42 
    43 EthernetHeader::EthernetHeader (bool hasPreamble)
    44   : m_enPreambleSfd (hasPreamble),
    45     m_lengthType (0)
    46 {}
    47 
    48 EthernetHeader::EthernetHeader ()
    49   : m_enPreambleSfd (false),
    50     m_lengthType (0)
    51 {}
    52 
    53 void 
    54 EthernetHeader::SetLengthType (uint16_t lengthType)
    55 {
    56   m_lengthType = lengthType;
    57 }
    58 uint16_t 
    59 EthernetHeader::GetLengthType (void) const
    60 {
    61   return m_lengthType;
    62 }
    63 
    64 void 
    65 EthernetHeader::SetPreambleSfd (uint64_t preambleSfd)
    66 {
    67   m_preambleSfd = preambleSfd;
    68 }
    69 uint64_t 
    70 EthernetHeader::GetPreambleSfd (void) const
    71 {
    72   return m_preambleSfd;
    73 }
    74 
    75 void 
    76 EthernetHeader::SetSource (Mac48Address source)
    77 {
    78   m_source = source;
    79 }
    80 Mac48Address
    81 EthernetHeader::GetSource (void) const
    82 {
    83   return m_source;
    84 }
    85 
    86 void 
    87 EthernetHeader::SetDestination (Mac48Address dst)
    88 {
    89   m_destination = dst;
    90 }
    91 Mac48Address
    92 EthernetHeader::GetDestination (void) const
    93 {
    94   return m_destination;
    95 }
    96 
    97 ethernet_header_t 
    98 EthernetHeader::GetPacketType (void) const
    99 {
   100   return LENGTH;
   101 }
   102 
   103 uint32_t 
   104 EthernetHeader::GetHeaderSize (void) const
   105 {
   106   return GetSerializedSize();
   107 }
   108 
   109 std::string
   110 EthernetHeader::GetName (void) const
   111 {
   112   return "ETHERNET";
   113 }
   114 
   115 void 
   116 EthernetHeader::Print (std::ostream &os) const
   117 {
   118   // ethernet, right ?
   119   if (m_enPreambleSfd)
   120     {
   121       os << " preamble/sfd=" << m_preambleSfd << ",";
   122     }
   123 
   124   os << " length/type=0x" << std::hex << m_lengthType << std::dec
   125      << ", source=" << m_source
   126      << ", destination=" << m_destination;
   127 }
   128 uint32_t 
   129 EthernetHeader::GetSerializedSize (void) const
   130 {
   131   if (m_enPreambleSfd)
   132     {
   133       return PREAMBLE_SIZE + LENGTH_SIZE + 2*MAC_ADDR_SIZE;
   134     } 
   135   else 
   136     {
   137       return LENGTH_SIZE + 2*MAC_ADDR_SIZE;
   138     }
   139 }
   140 
   141 void
   142 EthernetHeader::Serialize (Buffer::Iterator start) const
   143 {
   144   Buffer::Iterator i = start;
   145   
   146   if (m_enPreambleSfd)
   147     {
   148       i.WriteU64(m_preambleSfd);
   149     }
   150   WriteTo (i, m_destination);
   151   WriteTo (i, m_source);
   152   i.WriteHtonU16 (m_lengthType);
   153 }
   154 uint32_t
   155 EthernetHeader::Deserialize (Buffer::Iterator start)
   156 {
   157   Buffer::Iterator i = start;
   158 
   159   if (m_enPreambleSfd)
   160     {
   161       m_enPreambleSfd = i.ReadU64 ();
   162     }
   163 
   164   ReadFrom (i, m_destination);
   165   ReadFrom (i, m_source);
   166   m_lengthType = i.ReadNtohU16 ();
   167 
   168   return GetSerializedSize ();
   169 }
   170 
   171 } // namespace ns3