blob: f9145cc70c7c225c0e6d03217c3836e17c78accc [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ndn-header-helper.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070022
23#include "ns3/log.h"
24#include "ns3/packet.h"
25#include "ns3/header.h"
26#include "ns3/object.h"
27
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080028#include "ns3/ndn-interest.h"
29#include "ns3/ndn-content-object.h"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070030#include <iomanip>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070031
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070032NS_LOG_COMPONENT_DEFINE ("ndn.HeaderHelper");
33
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080034const uint8_t INTEREST_CCNB_BYTES[] = {0x01, 0xD2};
35const uint8_t CONTENT_OBJECT_CCNB_BYTES[] = {0x04, 0x82};
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080037const uint8_t INTEREST_NDNSIM_BYTES[] = {0x80, 0x00};
38const uint8_t CONTENT_OBJECT_NDNSIM_BYTES[] = {0x80, 0x01};
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070039
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070040namespace ns3 {
41namespace ndn {
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070042
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043HeaderHelper::Type
44HeaderHelper::GetNdnHeaderType (Ptr<const Packet> packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070045{
46 uint8_t type[2];
47 uint32_t read=packet->CopyData (type,2);
Ilya Moiseenko2a59e0e2011-10-28 13:05:32 -070048
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049 if (read!=2) throw UnknownHeaderException();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070050
Alexander Afanasyevd459ec32012-04-30 13:58:20 -070051 NS_LOG_DEBUG (*packet);
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080052 if (type[0] == INTEREST_CCNB_BYTES[0] && type[1] == INTEREST_CCNB_BYTES[1])
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070053 {
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080054 return HeaderHelper::INTEREST_CCNB;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070055 }
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080056 else if (type[0] == CONTENT_OBJECT_CCNB_BYTES[0] && type[1] == CONTENT_OBJECT_CCNB_BYTES[1])
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070057 {
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080058 return HeaderHelper::CONTENT_OBJECT_CCNB;
59 }
60 else if (type[0] == INTEREST_NDNSIM_BYTES[0] && type[1] == INTEREST_NDNSIM_BYTES[1])
61 {
62 return HeaderHelper::INTEREST_NDNSIM;
63 }
64 else if (type[0] == CONTENT_OBJECT_NDNSIM_BYTES[0] && type[1] == CONTENT_OBJECT_NDNSIM_BYTES[1])
65 {
66 return HeaderHelper::CONTENT_OBJECT_NDNSIM;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070067 }
68
Alexander Afanasyevd459ec32012-04-30 13:58:20 -070069 NS_LOG_DEBUG (*packet);
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070070 throw UnknownHeaderException();
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070071}
72
Alexander Afanasyevc45c4d62013-02-01 13:01:18 -080073Ptr<const Name>
74HeaderHelper::GetName (Ptr<const Packet> p)
75{
76 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
77 try
78 {
79 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (p);
80 switch (type)
81 {
82 case HeaderHelper::INTEREST_NDNSIM:
83 {
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070084 Ptr<Interest> header = Create<Interest> ();
Alexander Afanasyevc45c4d62013-02-01 13:01:18 -080085
86 // Deserialization. Exception may be thrown
87 packet->RemoveHeader (*header);
88 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
89
90 return header->GetNamePtr ();
91 break;
92 }
93 case HeaderHelper::CONTENT_OBJECT_NDNSIM:
94 {
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070095 Ptr<ContentObject> header = Create<ContentObject> ();
Alexander Afanasyevc45c4d62013-02-01 13:01:18 -080096
97 // Deserialization. Exception may be thrown
98 packet->RemoveHeader (*header);
99 return header->GetNamePtr ();
100 break;
101 }
102 case HeaderHelper::INTEREST_CCNB:
103 case HeaderHelper::CONTENT_OBJECT_CCNB:
104 NS_FATAL_ERROR ("ccnb support is broken in this implementation");
105 break;
106 }
107
108 // exception will be thrown if packet is not recognized
109 }
110 catch (UnknownHeaderException)
111 {
112 return 0;
113 }
114
115 return 0;
116}
117
118
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700119} // namespace ndn
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700120} // namespace ns3