Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 21 | #include "ndn-header-helper.h" |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 22 | |
| 23 | #include "ns3/log.h" |
| 24 | #include "ns3/packet.h" |
| 25 | #include "ns3/header.h" |
| 26 | #include "ns3/object.h" |
| 27 | |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 28 | #include "ns3/ndn-interest.h" |
| 29 | #include "ns3/ndn-content-object.h" |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 30 | #include <iomanip> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 32 | NS_LOG_COMPONENT_DEFINE ("ndn.HeaderHelper"); |
| 33 | |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 34 | const uint8_t INTEREST_CCNB_BYTES[] = {0x01, 0xD2}; |
| 35 | const uint8_t CONTENT_OBJECT_CCNB_BYTES[] = {0x04, 0x82}; |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 37 | const uint8_t INTEREST_NDNSIM_BYTES[] = {0x80, 0x00}; |
| 38 | const uint8_t CONTENT_OBJECT_NDNSIM_BYTES[] = {0x80, 0x01}; |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 40 | namespace ns3 { |
| 41 | namespace ndn { |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 43 | HeaderHelper::Type |
| 44 | HeaderHelper::GetNdnHeaderType (Ptr<const Packet> packet) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 45 | { |
| 46 | uint8_t type[2]; |
| 47 | uint32_t read=packet->CopyData (type,2); |
Ilya Moiseenko | 2a59e0e | 2011-10-28 13:05:32 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 49 | if (read!=2) throw UnknownHeaderException(); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 50 | |
Alexander Afanasyev | d459ec3 | 2012-04-30 13:58:20 -0700 | [diff] [blame] | 51 | NS_LOG_DEBUG (*packet); |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 52 | if (type[0] == INTEREST_CCNB_BYTES[0] && type[1] == INTEREST_CCNB_BYTES[1]) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 53 | { |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 54 | return HeaderHelper::INTEREST_CCNB; |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 55 | } |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 56 | else if (type[0] == CONTENT_OBJECT_CCNB_BYTES[0] && type[1] == CONTENT_OBJECT_CCNB_BYTES[1]) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 57 | { |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 58 | 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 Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Alexander Afanasyev | d459ec3 | 2012-04-30 13:58:20 -0700 | [diff] [blame] | 69 | NS_LOG_DEBUG (*packet); |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 70 | throw UnknownHeaderException(); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Alexander Afanasyev | c45c4d6 | 2013-02-01 13:01:18 -0800 | [diff] [blame] | 73 | Ptr<const Name> |
| 74 | HeaderHelper::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 Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 84 | Ptr<Interest> header = Create<Interest> (); |
Alexander Afanasyev | c45c4d6 | 2013-02-01 13:01:18 -0800 | [diff] [blame] | 85 | |
| 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 Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 95 | Ptr<ContentObject> header = Create<ContentObject> (); |
Alexander Afanasyev | c45c4d6 | 2013-02-01 13:01:18 -0800 | [diff] [blame] | 96 | |
| 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 Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 119 | } // namespace ndn |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 120 | } // namespace ns3 |