A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ethernet-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
19  */
20 
21 #include <iomanip>
22 #include <iostream>
23 #include "ns3/assert.h"
24 #include "ns3/log.h"
25 #include "ns3/header.h"
26 #include "ethernet-header.h"
27 #include "address-utils.h"
28 
29 NS_LOG_COMPONENT_DEFINE ("EthernetHeader");
30 
31 namespace ns3 {
32 
33 NS_OBJECT_ENSURE_REGISTERED (EthernetHeader);
34 
36  : m_enPreambleSfd (hasPreamble),
37  m_lengthType (0)
38 {
39 }
40 
42  : m_enPreambleSfd (false),
43  m_lengthType (0)
44 {
45 }
46 
47 void
48 EthernetHeader::SetLengthType (uint16_t lengthType)
49 {
50  m_lengthType = lengthType;
51 }
52 uint16_t
54 {
55  return m_lengthType;
56 }
57 
58 void
59 EthernetHeader::SetPreambleSfd (uint64_t preambleSfd)
60 {
61  m_preambleSfd = preambleSfd;
62 }
63 uint64_t
65 {
66  return m_preambleSfd;
67 }
68 
69 void
71 {
72  m_source = source;
73 }
76 {
77  return m_source;
78 }
79 
80 void
82 {
83  m_destination = dst;
84 }
87 {
88  return m_destination;
89 }
90 
93 {
94  return LENGTH;
95 }
96 
97 uint32_t
99 {
100  return GetSerializedSize ();
101 }
102 
103 
104 TypeId
106 {
107  static TypeId tid = TypeId ("ns3::EthernetHeader")
108  .SetParent<Header> ()
109  .AddConstructor<EthernetHeader> ()
110  ;
111  return tid;
112 }
113 TypeId
115 {
116  return GetTypeId ();
117 }
118 void
119 EthernetHeader::Print (std::ostream &os) const
120 {
121  // ethernet, right ?
122  if (m_enPreambleSfd)
123  {
124  os << "preamble/sfd=" << m_preambleSfd << ",";
125  }
126 
127  os << " length/type=0x" << std::hex << m_lengthType << std::dec
128  << ", source=" << m_source
129  << ", destination=" << m_destination;
130 }
131 uint32_t
133 {
134  if (m_enPreambleSfd)
135  {
137  }
138  else
139  {
140  return LENGTH_SIZE + 2*MAC_ADDR_SIZE;
141  }
142 }
143 
144 void
146 {
148 
149  if (m_enPreambleSfd)
150  {
152  }
153  WriteTo (i, m_destination);
154  WriteTo (i, m_source);
156 }
157 uint32_t
159 {
161 
162  if (m_enPreambleSfd)
163  {
164  m_enPreambleSfd = i.ReadU64 ();
165  }
166 
167  ReadFrom (i, m_destination);
168  ReadFrom (i, m_source);
169  m_lengthType = i.ReadNtohU16 ();
170 
171  return GetSerializedSize ();
172 }
173 
174 } // namespace ns3