aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/core/messageHandler.h
blob: a8b2a3e54c2df675cfb00f9576ecf6ea5a4f45c2 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/*
 * 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.
 */

#ifndef messageHandler
#define messageHandler

#include <stdlib.h>
#ifndef _WIN32
#include <unistd.h> // close
#endif

#include <hicn/hicn.h>
#include <hicn/core/messagePacketType.h>

#define H(packet) ((hicn_header_t *)packet)
#define H6(packet) (H(packet)->v6.ip)
#define H6T(packet) (H(packet)->v6.tcp)
#define H4(packet) (H(packet)->v4.ip)
#define H4T(packet) (H(packet)->v4.tcp)

#define HICN_V6_LEN(packet) (H6(packet).len)
#define HICN_V4_LEN(packet) (H4(packet).len)

/*** codes and types ***/
#define IPv6_TYPE 6
#define IPv4_TYPE 4
#define ICMP_WLDR_TYPE 42
#define ICMP_WLDR_CODE 0
#define ICMP_LB_TYPE 43

/*** masks and constants ***/
#define PATH_LABEL_MASK 0x8000      // 1000 0000 0000 0000
#define NOT_PATH_LABEL_MASK 0x7fff  // 0111 0000 0000 0000
#define UINT16_T_MASK 0x0000ffff    // 1111 1111 1111 1111

/*** HICN ALLOWED PORTS ***/
#define CONTROL_PORT 9695
#define HTTP_PORT 8080

#define IPV6_DEFAULT_VERSION 6
#define IPV6_DEFAULT_TRAFFIC_CLASS 0
#define IPV6_DEFAULT_FLOW_LABEL 0

#define expected_lbl wldr_notification_lbl.expected_lbl
#define received_lbl wldr_notification_lbl.received_lbl

#include <hicn/core/forwarder.h>

#ifdef WITH_MAPME
#include <hicn/core/mapme.h>
#include <hicn/socket/api.h>
#endif /* WITH_MAPME */

#define CONNECTION_ID_UNDEFINED -1

#define BFD_PORT 3784

static inline uint8_t messageHandler_GetIPPacketType(const uint8_t *message) {
  return HICN_IP_VERSION(message);
}

static inline void messageHandler_UpdateTCPCheckSum(uint8_t *message,
                                                    uint16_t *old_val,
                                                    uint16_t *new_val,
                                                    uint8_t size) {
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv4_TYPE:
      for (uint8_t i = 0; i < size; i++) {
        uint16_t old_csum = ~(H4T(message).csum);
        uint16_t not_old_val = ~(*old_val);
        uint32_t sum = (uint32_t)old_csum + not_old_val + *new_val;

        while (sum >> 16) {
          sum = (sum >> 16) + (sum & UINT16_T_MASK);
        }

        H4T(message).csum = ~sum;
        ++old_val;
        ++new_val;
      }
      break;
    case IPv6_TYPE:
      for (uint8_t i = 0; i < size; i++) {
        uint16_t old_csum = ~(H6T(message).csum);
        uint16_t not_old_val = ~(*old_val);
        uint32_t sum = (uint32_t)old_csum + not_old_val + *new_val;

        while (sum >> 16) {
          sum = (sum >> 16) + (sum & UINT16_T_MASK);
        }

        H6T(message).csum = ~sum;
        ++old_val;
        ++new_val;
      }
      break;
    default:
      return;
  }
}

static inline void messageHandler_UpdateIPv4CheckSum(uint8_t *message,
                                                     uint16_t *old_val,
                                                     uint16_t *new_val,
                                                     uint8_t size) {
  for (uint8_t i = 0; i < size; i++) {
    uint16_t old_csum = ~(H4(message).csum);
    uint16_t not_old_val = ~(*old_val);
    uint32_t sum = (uint32_t)old_csum + not_old_val + *new_val;

    while (sum >> 16) {
      sum = (sum >> 16) + (sum & UINT16_T_MASK);
    }

    H4(message).csum = ~sum;
    ++old_val;
    ++new_val;
  }
}

static inline size_t messageHandler_GetEmptyTCPPacketSize(unsigned ipVersion) {
  if (ipVersion == IPv4_TYPE)
    return IPV4_HDRLEN + TCP_HDRLEN;
  else if (ipVersion == IPv6_TYPE)
    return IPV6_HDRLEN + TCP_HDRLEN;
  else
    return 0;
}

static inline size_t messageHandler_GetICMPPacketSize(unsigned ipVersion) {
  if (ipVersion == IPv4_TYPE)
    return IPV4_HDRLEN + ICMP_HDRLEN;
  else if (ipVersion == IPv6_TYPE)
    return IPV6_HDRLEN + ICMP_HDRLEN;
  else
    return 0;
}

static inline size_t messageHandler_GetIPHeaderLength(unsigned ipVersion) {
  if (ipVersion == IPv4_TYPE)
    return IPV4_HDRLEN;
  else if (ipVersion == IPv6_TYPE)
    return IPV6_HDRLEN;
  else
    return 0;
}

static inline bool messageHandler_IsValidHicnPacket(const uint8_t *message) {
  uint8_t version = messageHandler_GetIPPacketType(message);
  if (version == IPv6_TYPE || version == IPv4_TYPE) {
    return true;
  }
  return false;
}

static inline uint8_t messageHandler_NextHeaderType(const uint8_t *message) {
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      return (uint8_t)H6(message).nxt;
    case IPv4_TYPE:
      return (uint8_t)H4(message).protocol;
    default:
      return 0;
  }
}

/* Forward declarations */
static inline void * messageHandler_GetSource(const uint8_t *message);
static inline void *messageHandler_GetDestination(const uint8_t *message);

static const
AddressPair *
_createRecvAddressPairFromPacket(const uint8_t *msgBuffer) {
  Address *packetSrcAddr = NULL; /* This one is in the packet */
  Address *localAddr = NULL; /* This one is to be determined */

  if (messageHandler_GetIPPacketType(msgBuffer) == IPv6_TYPE) {
    struct sockaddr_in6 addr_in6;
    addr_in6.sin6_family = AF_INET6;
    addr_in6.sin6_port = htons(1234);
    addr_in6.sin6_flowinfo = 0;
    addr_in6.sin6_scope_id = 0;
    memcpy(&addr_in6.sin6_addr,
           (struct in6_addr *)messageHandler_GetSource(msgBuffer), 16);
    packetSrcAddr = addressCreateFromInet6(&addr_in6);

    /* We now determine the local address used to reach the packet src address */
    int sock = (int)socket (AF_INET6, SOCK_DGRAM, 0);
    if (sock < 0)
      goto ERR;

    struct sockaddr_in6 remote, local;
    memset(&remote, 0, sizeof(remote));
    remote.sin6_family = AF_INET6;
    remote.sin6_addr = addr_in6.sin6_addr;
    remote.sin6_port = htons(1234);

    socklen_t locallen = sizeof(local);
    if (connect(sock, (const struct sockaddr*)&remote, sizeof(remote)) == -1)
      goto ERR;
    if (getsockname(sock, (struct sockaddr*) &local, &locallen) == -1)
      goto ERR;

    local.sin6_port = htons(1234);
    localAddr = addressCreateFromInet6(&local);

    close(sock);

  } else if (messageHandler_GetIPPacketType(msgBuffer) == IPv4_TYPE) {
    struct sockaddr_in addr_in;
    addr_in.sin_family = AF_INET;
    addr_in.sin_port = htons(1234);
    memcpy(&addr_in.sin_addr,
           (struct in_addr *)messageHandler_GetSource(msgBuffer), 4);
    packetSrcAddr = addressCreateFromInet(&addr_in);

    /* We now determine the local address used to reach the packet src address */

    int sock = (int)socket (AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) {
      perror("Socket error");
      goto ERR;
    }

    struct sockaddr_in remote, local;
    memset(&remote, 0, sizeof(remote));
    remote.sin_family = AF_INET;
    remote.sin_addr = addr_in.sin_addr;
    remote.sin_port = htons(1234);

    socklen_t locallen = sizeof(local);
    if (connect(sock, (const struct sockaddr*)&remote, sizeof(remote)) == -1)
      goto ERR;
    if (getsockname(sock, (struct sockaddr*) &local, &locallen) == -1)
      goto ERR;

    local.sin_port = htons(1234);
    localAddr = addressCreateFromInet(&local);

    close(sock);
  }
  /* As this is a receive pair, we swap src and dst */
  return addressPair_Create(localAddr, packetSrcAddr);

ERR:
  perror("Socket error");
  return NULL;
}

/* Main hook handler */

/**
 * \brief Handle incoming messages
 * \param [in] forwarder - Reference to the Forwarder instance
 * \param [in] packet - Packet buffer
 * \param [in] conn_id - A hint on the connection ID on which the packet
 *      was received
 * \return Flag indicating whether the packet matched a hook and was
 *      (successfully or not) processed.
 */
static inline bool messageHandler_handleHooks(Forwarder * forwarder,
        const uint8_t * packet, ListenerOps * listener, int fd, AddressPair * pair)
{
  bool is_matched = false;

  /* BEGIN Match */

#ifdef WITH_MAPME
  bool is_mapme = mapme_isMapMe(packet);
  is_matched |= is_mapme;
#endif /* WITH_MAPME */

  /* ... */

  /* END Match */

  if (!is_matched)
    return false;

  /*
   * Find existing connection or create a new one (we assume all processing
   * requires a valid connection.
   */

  if (!pair) {
    /* The hICN listener does not provide any address pair while UDP does */
    const AddressPair * pair = _createRecvAddressPairFromPacket(packet);
    if (!pair)
      return false;
  }

  /* Find connection and eventually create it */
  const Connection * conn = connectionTable_FindByAddressPair(
        forwarder_GetConnectionTable(forwarder), pair);
  unsigned conn_id;
  if (conn == NULL) {
    conn_id = listener->createConnection(listener, fd, pair);
  } else {
    conn_id = connection_GetConnectionId(conn);
  }

  /* BEGIN Process */

#ifdef WITH_MAPME
  if (mapme_isMapMe(packet))
    forwarder_ProcessMapMe(forwarder, packet, conn_id);
#endif /* WITH_MAPME */

  /* ... */

  /* END Process */

  return true;
}

static inline bool messageHandler_IsTCP(const uint8_t *message) {
  if (messageHandler_NextHeaderType(message) != IPPROTO_TCP) return false;
  return true;
}

static inline bool messageHandler_IsInterest(const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return false;

  bool flag;
  hicn_packet_test_ece((hicn_header_t *)message,
                       &flag);  // ECE flag is set to 0 in interest packets
  if (flag == false) return true;
  return false;
}

static inline bool messageHandler_IsData(const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return false;

  bool flag;
  hicn_packet_test_ece((hicn_header_t *)message,
                       &flag);  // ECE flag is set to 1 in data packets
  if (flag == true) return true;
  return false;
}

static inline bool messageHandler_IsWldrNotification(const uint8_t *message) {
  // this function returns true only if the packet is an ICMP packet in Wldr
  // form. type must be equal to ICMP_WLDR_TYPE and code equal to ICMP_WLDR_CODE
  uint8_t next_header = messageHandler_NextHeaderType(message);

  const uint8_t *icmp_ptr;
  if (next_header == IPPROTO_ICMP) {
    icmp_ptr = message + IPV4_HDRLEN;
  } else if (next_header == IPPROTO_ICMPV6) {
    icmp_ptr = message + IPV6_HDRLEN;
  } else {
    return false;
  }

  uint8_t type = ((_icmp_header_t *)icmp_ptr)->type;
  uint8_t code = ((_icmp_header_t *)icmp_ptr)->code;
  if (type == ICMP_WLDR_TYPE && code == ICMP_WLDR_CODE) {
    return true;
  }

  return false;
}

static inline bool messageHandler_IsLoadBalancerProbe(const uint8_t *message) {
  uint8_t next_header = messageHandler_NextHeaderType(message);

  const uint8_t *icmp_ptr;
  if (next_header == IPPROTO_ICMP) {
    icmp_ptr = message + IPV4_HDRLEN;
  } else if (next_header == IPPROTO_ICMPV6) {
    icmp_ptr = message + IPV6_HDRLEN;
  } else {
    return false;
  }

  uint8_t type = ((_icmp_header_t *)icmp_ptr)->type;
  if (type == ICMP_LB_TYPE) {
    return true;
  }

  return false;
}

static inline uint16_t messageHandler_GetTotalPacketLength(
    const uint8_t *message) {
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      return ntohs((uint16_t)HICN_V6_LEN(message)) + IPV6_HDRLEN;
    case IPv4_TYPE:
      return ntohs((uint16_t)HICN_V4_LEN(message));
    default:
      return 0;
  }
}

static inline uint32_t messageHandler_GetSegment(const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return 0;

  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      return ntohl((uint32_t)H6T(message).seq);
    case IPv4_TYPE:
      return ntohl((uint32_t)H4T(message).seq);
    default:
      return 0;
  }
}

static inline uint16_t messageHandler_GetExpectedWldrLabel(
    const uint8_t *message) {
  const uint8_t *icmp_ptr;
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      icmp_ptr = message + IPV6_HDRLEN;
      break;
    case IPv4_TYPE:
      icmp_ptr = message + IPV4_HDRLEN;
      break;
    default:
      return 0;
  }

  return ntohs(((_icmp_wldr_header_t *)icmp_ptr)->expected_lbl);
}

static inline uint16_t messageHandler_GetWldrLastReceived(
    const uint8_t *message) {
  const uint8_t *icmp_ptr;
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      icmp_ptr = message + IPV6_HDRLEN;
      break;
    case IPv4_TYPE:
      icmp_ptr = message + IPV4_HDRLEN;
      break;
    default:
      return 0;
  }

  return ntohs(((_icmp_wldr_header_t *)icmp_ptr)->received_lbl);
}

static inline uint16_t messageHandler_GetWldrLabel(const uint8_t *message) {
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      return ntohs((uint16_t)H6T(message).window);
    case IPv4_TYPE:
      return ntohs((uint16_t)H4T(message).window);
    default:
      return 0;
  }
}

static inline void messageHandler_SetWldrLabel(uint8_t *message,
                                               uint16_t label) {
  uint16_t old_val = messageHandler_GetWldrLabel(message);

  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      H6T(message).window = htons(label);
      break;
    case IPv4_TYPE:
      H4T(message).window = htons(label);
      break;
    default:
      break;
  }

  messageHandler_UpdateTCPCheckSum(message, &old_val, &label, 1);
}

static inline void messageHandler_ResetWldrLabel(uint8_t *message) {
  messageHandler_SetWldrLabel(message, 0);
}

static inline bool messageHandler_HasWldr(const uint8_t *message) {
  if (messageHandler_IsTCP(message)) {
    uint16_t lbl = messageHandler_GetWldrLabel(message);
    if (lbl != 0) {
      return true;
    }
  }
  return false;
}

static inline uint32_t messageHandler_GetPathLabel(const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return 0;

  uint32_t path_label;
  int res = hicn_data_get_path_label((hicn_header_t *)message, &path_label);
  if (res < 0) return 0;
  return path_label;
}

static inline void messageHandler_SetPathLabel(uint8_t *message,
                                               uint32_t new_path_label) {
  if (!messageHandler_IsTCP(message)) return;

  uint32_t old_path_label;
  int res = hicn_data_get_path_label((hicn_header_t *)message, &old_path_label);
  if (res < 0) return;

  hicn_data_set_path_label((hicn_header_t *)message, new_path_label);

  messageHandler_UpdateTCPCheckSum(message, (uint16_t *)&old_path_label,
                                   (uint16_t *)&new_path_label, 2);
}

static inline void messageHandler_UpdatePathLabel(uint8_t *message,
                                                  uint8_t outFace) {
  if (!messageHandler_IsTCP(message)) return;

  uint32_t pl_old_32bit = messageHandler_GetPathLabel(message);
  uint8_t pl_old_8bit = (uint8_t)(pl_old_32bit >> 24UL);
  uint32_t pl_new_32bit =
      (uint32_t)((((pl_old_8bit << 1) | (pl_old_8bit >> 7)) ^ outFace) << 24UL);

  hicn_data_set_path_label((hicn_header_t *)message, pl_new_32bit);

  messageHandler_UpdateTCPCheckSum(message, (uint16_t *)&pl_old_32bit,
                                   (uint16_t *)&pl_new_32bit, 2);
}

static inline void messageHandler_ResetPathLabel(uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return;

  uint32_t pl_old_32bit = messageHandler_GetPathLabel(message);
  uint32_t pl_new_32bit = 0;
  hicn_data_set_path_label((hicn_header_t *)message, pl_new_32bit);
  messageHandler_UpdateTCPCheckSum(message, (uint16_t *)&pl_old_32bit,
                                   (uint16_t *)&pl_new_32bit, 2);
}

static inline uint16_t messageHandler_GetInterestLifetime(
    const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return 0;

  hicn_lifetime_t lifetime;
  int res = hicn_interest_get_lifetime((hicn_header_t *)message, &lifetime);
  if (res < 0) return 0;
  return lifetime;
}

static inline bool messageHandler_HasInterestLifetime(const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return false;

  if (messageHandler_GetInterestLifetime(message) == 0) return false;
  return true;
}

static inline uint32_t messageHandler_GetContentExpiryTime(
    const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return 0;

  uint32_t expirationTime;
  int res =
      hicn_data_get_expiry_time((hicn_header_t *)message, &expirationTime);
  if (res < 0) return 0;
  return expirationTime;
}

static inline bool messageHandler_HasContentExpiryTime(const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return 0;

  uint32_t expirationTime;
  int res =
      hicn_data_get_expiry_time((hicn_header_t *)message, &expirationTime);
  if (res < 0) return false;

  if (expirationTime == HICN_MAX_LIFETIME) return false;

  return true;
}

static inline void *messageHandler_GetSource(const uint8_t *message) {
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      return &H6(message).saddr;
      break;
    case IPv4_TYPE:
      return &H4(message).saddr;
      break;
    default:
      return NULL;
  }
}

static inline void *messageHandler_GetDestination(const uint8_t *message) {
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      return &H6(message).daddr;
      break;
    case IPv4_TYPE:
      return &H4(message).daddr;
      break;
    default:
      return NULL;
  }
}

static inline void messageHandler_SetSource_IPv6(uint8_t *message,
                                                 struct in6_addr *address) {
  if (messageHandler_IsTCP(message)) {
    uint16_t *old_src = (uint16_t *)messageHandler_GetSource(message);
    messageHandler_UpdateTCPCheckSum(message, old_src, (uint16_t *)address, 8);
  }
  H6(message).saddr.as_in6addr = *address;
}

static inline void messageHandler_SetDestination_IPv6(
    uint8_t *message, struct in6_addr *address) {
  if (messageHandler_IsTCP(message)) {
    uint16_t *old_dst = (uint16_t *)messageHandler_GetDestination(message);
    messageHandler_UpdateTCPCheckSum(message, old_dst, (uint16_t *)address, 8);
  }
  H6(message).daddr.as_in6addr = *address;
}

static inline void messageHandler_SetSource_IPv4(uint8_t *message,
                                                 uint32_t *address) {
  // update tcp checksum
  uint16_t *old_src = (uint16_t *)messageHandler_GetSource(message);
  if (messageHandler_IsTCP(message)) {
    messageHandler_UpdateTCPCheckSum(message, old_src, (uint16_t *)address, 2);
  }
  // update IPv4 cheksum
  // the IPv4 checksum is not part of the psudo header for TCP checksum
  // calculation we can update them separetelly
  messageHandler_UpdateIPv4CheckSum(message, old_src, (uint16_t *)address, 2);

  H4(message).saddr.as_u32 = *address;
}

static inline void messageHandler_SetDestination_IPv4(uint8_t *message,
                                                      uint32_t *address) {
  uint16_t *old_dst = (uint16_t *)messageHandler_GetDestination(message);
  if (messageHandler_IsTCP(message)) {
    messageHandler_UpdateTCPCheckSum(message, old_dst, (uint16_t *)address, 2);
  }
  messageHandler_UpdateIPv4CheckSum(message, old_dst, (uint16_t *)address, 2);
  H4(message).daddr.as_u32 = *address;
}

static inline void messageHandler_SetWldrNotification(uint8_t *notification,
                                                      uint8_t *original,
                                                      uint16_t expected,
                                                      uint16_t received) {
  hicn_header_t *h = (hicn_header_t *)notification;
  switch (messageHandler_GetIPPacketType(original)) {
    case IPv6_TYPE: {
      *h = (hicn_header_t){.v6 = {
                               .ip =
                                   {
                                       .version_class_flow = htonl(
                                           (IPV6_DEFAULT_VERSION << 28) |
                                           (IPV6_DEFAULT_TRAFFIC_CLASS << 20) |
                                           (IPV6_DEFAULT_FLOW_LABEL & 0xfffff)),
                                       .len = htons(ICMP_HDRLEN),
                                       .nxt = IPPROTO_ICMPV6,
                                       .hlim = 5,
                                   },
                               .wldr =
                                   {
                                       .type = ICMP_WLDR_TYPE,
                                       .code = ICMP_WLDR_CODE,
                                       .expected_lbl = htons(expected),
                                       .received_lbl = htons(received),
                                   },
                           }};
      messageHandler_SetSource_IPv6(
          notification,
          (struct in6_addr *)messageHandler_GetDestination(original));
      messageHandler_SetDestination_IPv6(
          notification, (struct in6_addr *)messageHandler_GetSource(original));
      break;
    }
    case IPv4_TYPE: {
      break;
    }
    default:
      break;
  }
}

static inline uint8_t * messageHandler_CreateProbePacket(hicn_format_t format,
    uint32_t probe_lifetime){
  size_t header_length;
  hicn_packet_get_header_length_from_format(format, &header_length);

  uint8_t *pkt = parcMemory_AllocateAndClear(header_length);

  hicn_packet_init_header(format, (hicn_header_t *) pkt);

  hicn_packet_set_dst_port((hicn_header_t *) pkt, BFD_PORT);
  hicn_interest_set_lifetime ((hicn_header_t *) pkt, probe_lifetime);

  return pkt;
}

static inline void messageHandler_CreateProbeReply(uint8_t * probe,
                                                      hicn_format_t format){

  hicn_name_t probe_name;
  hicn_interest_get_name (format,
          (const hicn_header_t *) probe, &probe_name);
  ip_address_t probe_locator;
  hicn_interest_get_locator (format,
             (const hicn_header_t *) probe, &probe_locator);

  uint16_t src_prt;
  uint16_t dst_prt;
  hicn_packet_get_src_port((const hicn_header_t *) probe, &src_prt);
  hicn_packet_get_dst_port((const hicn_header_t *) probe, &dst_prt);
  hicn_packet_set_src_port((hicn_header_t *) probe, dst_prt);
  hicn_packet_set_dst_port((hicn_header_t *) probe, src_prt);

  hicn_data_set_name (format, (hicn_header_t *) probe, &probe_name);
  hicn_data_set_locator (format, (hicn_header_t *) probe, &probe_locator);
  hicn_data_set_expiry_time ((hicn_header_t *) probe, 0);
}

static inline hicn_name_t * messageHandler_CreateProbeName(const ip_prefix_t *address){
  hicn_name_t * name = parcMemory_AllocateAndClear(sizeof(hicn_name_t));
  hicn_name_create_from_ip_prefix(address, 0, name);
  return name;
}

static inline void messageHandler_SetProbeName(uint8_t * probe, hicn_format_t format,
                                               hicn_name_t * name, uint32_t seq){
  hicn_name_set_seq_number (name, seq);
  hicn_interest_set_name(format, (hicn_header_t *) probe, name);
}

static inline bool messageHandler_IsAProbe(const uint8_t *packet){
  uint16_t src_prt;
  uint16_t dst_prt;
  hicn_packet_get_src_port ((const hicn_header_t *) packet, &src_prt);
  hicn_packet_get_dst_port ((const hicn_header_t *) packet, &dst_prt);

  if(dst_prt == BFD_PORT){
    //interest probe
    return true;
  }

  if(src_prt == BFD_PORT){
    //data (could be a probe)
    uint32_t expiry_time;
    hicn_data_get_expiry_time ((const hicn_header_t *) packet, &expiry_time);
    if(expiry_time == 0){
      //this is a probe
      return true;
    }
  }

  return false;
}

#endif  // Metis_metis_MessageHandler