aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/core/content_object.cc
blob: 0c68ef559b3079db606b31da95b83c74f64894d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <hicn/transport/core/content_object.h>
#include <hicn/transport/errors/errors.h>
#include <hicn/transport/utils/branch_prediction.h>

extern "C" {
#ifndef _WIN32
TRANSPORT_CLANG_DISABLE_WARNING("-Wextern-c-compat")
#endif
#include <hicn/hicn.h>
#include <hicn/util/ip_address.h>
}

#include <cstring>
#include <memory>

namespace transport {

namespace core {

ContentObject::ContentObject(const Name &name, Packet::Format format,
                             std::size_t additional_header_size)
    : Packet(format, additional_header_size) {
  if (TRANSPORT_EXPECT_FALSE(
          hicn_data_set_name(format, packet_start_, &name.name_) < 0)) {
    throw errors::RuntimeException("Error filling the packet name.");
  }

  if (TRANSPORT_EXPECT_FALSE(hicn_data_get_name(format_, packet_start_,
                                                name_.getStructReference()) <
                             0)) {
    throw errors::MalformedPacketException();
  }
}

#ifdef __ANDROID__
ContentObject::ContentObject(hicn_format_t format,
                             std::size_t additional_header_size)
    : ContentObject(Name("0::0|0"), format, additional_header_size) {}
#else
ContentObject::ContentObject(hicn_format_t format,
                             std::size_t additional_header_size)
    : ContentObject(Packet::base_name, format, additional_header_size) {}
#endif

ContentObject::ContentObject(const Name &name, hicn_format_t format,
                             std::size_t additional_header_size,
                             const uint8_t *payload, std::size_t size)
    : ContentObject(name, format, additional_header_size) {
  appendPayload(payload, size);
}

ContentObject::ContentObject(ContentObject &&other) : Packet(std::move(other)) {
  name_ = std::move(other.name_);
}

ContentObject::ContentObject(const ContentObject &other) : Packet(other) {
  name_ = other.name_;
}

ContentObject &ContentObject::operator=(const ContentObject &other) {
  return (ContentObject &)Packet::operator=(other);
}

ContentObject::~ContentObject() {}

const Name &ContentObject::getName() const {
  if (!name_) {
    if (hicn_data_get_name(format_, packet_start_,
                           (hicn_name_t *)name_.getConstStructReference()) <
        0) {
      throw errors::MalformedPacketException();
    }
  }

  return name_;
}

Name &ContentObject::getWritableName() { return const_cast<Name &>(getName()); }

void ContentObject::setName(const Name &name) {
  if (hicn_data_set_name(format_, packet_start_,
                         name.getConstStructReference()) < 0) {
    throw errors::RuntimeException("Error setting content object name.");
  }

  if (hicn_data_get_name(format_, packet_start_, name_.getStructReference()) <
      0) {
    throw errors::MalformedPacketException();
  }
}

void ContentObject::setName(Name &&name) {
  if (hicn_data_set_name(format_, packet_start_, name.getStructReference()) <
      0) {
    throw errors::RuntimeException(
        "Error getting the payload length from content object.");
  }

  if (hicn_data_get_name(format_, packet_start_, name_.getStructReference()) <
      0) {
    throw errors::MalformedPacketException();
  }
}

uint32_t ContentObject::getPathLabel() const {
  uint32_t path_label;
  if (hicn_data_get_path_label(packet_start_, &path_label) < 0) {
    throw errors::RuntimeException(
        "Error retrieving the path label from content object");
  }

  return ntohl(path_label);
}

ContentObject &ContentObject::setPathLabel(uint32_t path_label) {
  path_label = htonl(path_label);
  if (hicn_data_set_path_label((hicn_header_t *)packet_start_, path_label) <
      0) {
    throw errors::RuntimeException(
        "Error setting the path label from content object");
  }

  return *this;
}

void ContentObject::setLocator(const ip_address_t &ip_address) {
  if (hicn_data_set_locator(format_, packet_start_, &ip_address) < 0) {
    throw errors::RuntimeException("Error setting content object locator");
  }

  return;
}

ip_address_t ContentObject::getLocator() const {
  ip_address_t ip;

  if (hicn_data_get_locator(format_, packet_start_, &ip) < 0) {
    throw errors::RuntimeException("Error getting content object locator.");
  }

  return ip;
}

void ContentObject::setLifetime(uint32_t lifetime) {
  if (hicn_data_set_expiry_time(packet_start_, lifetime) < 0) {
    throw errors::MalformedPacketException();
  }
}

uint32_t ContentObject::getLifetime() const {
  uint32_t lifetime = 0;

  if (hicn_data_get_expiry_time(packet_start_, &lifetime) < 0) {
    throw errors::MalformedPacketException();
  }

  return lifetime;
}

void ContentObject::resetForHash() {
  if (hicn_data_reset_for_hash(
          format_, reinterpret_cast<hicn_header_t *>(packet_start_)) < 0) {
    throw errors::RuntimeException(
        "Error resetting content object fields for hash computation.");
  }
}

}  // end namespace core

}  // end namespace transport