/*  Copyright (C) 2011-2026  P.D. Buchan (pdbuchan@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// Send an ICMPv6 echo request packet via raw socket at the link layer (Ethernet frame),
// and receive echo reply packet (i.e., ping). Includes some ICMP data.
// Need to have destination MAC address.

#define _GNU_SOURCE           // Sometimes required for GNU/Linux-specific interfaces. e.g., SO_BINDTODEVICE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>           // close()
#include <string.h>           // memset(), memcpy(), memcmp()
#include <stdint.h>           // uint8_t, uint16_t, uint32_t

#include <netdb.h>            // struct addrinfo
#include <sys/socket.h>       // socket()
#include <netinet/in.h>       // IPPROTO_ICMPV6, INET6_ADDRSTRLEN
#include <netinet/ip.h>       // IP_MAXPACKET (which is 65535)
#include <netinet/ip6.h>      // struct ip6_hdr
#include <netinet/icmp6.h>    // struct icmp6_hdr, ICMP6_ECHO_REQUEST
#include <arpa/inet.h>        // inet_pton(), inet_ntop()
#include <sys/ioctl.h>        // macro ioctl is defined
#include <net/if.h>           // struct ifreq
#include <linux/if_ether.h>   // ETH_HLEN, ETH_P_IPV6
#include <linux/if_packet.h>  // struct sockaddr_ll (see man 7 packet)
#include <poll.h>             // poll()
#include <time.h>             // clock_gettime()

#include <errno.h>            // errno

// Define some constants.
#define ETH_HDRLEN ETH_HLEN   // Ethernet header length
#define MAC_LEN 6             // Length of a hardware (MAC) address
#define IP6_HDRLEN 40         // IPv6 header length
#define ICMP_HDRLEN 8         // ICMP header length for echo request, excludes data
#define TIMEOUT 2             // Time for receive socket to wait for a reply (s)
#define HOSTNAME_LEN 255      // Maximum FQDN length including terminating null byte

// Function prototypes
uint16_t checksum (uint8_t *, int);
uint16_t icmp6_checksum (struct ip6_hdr, uint8_t *, int);
char *allocate_strmem (int);
uint8_t *allocate_ustrmem (int);

int
main (void) {

  int i, n, status, icmp_datalen, sd, sendsd, recvsd, frame_length, done;
  int ip_total_len, timeout_ms, trylim, trycount;
  ssize_t bytes;
  char *interface, *target, *src_ip, *dst_ip, *rec_ip;
  struct ip6_hdr send_iphdr, *recv_iphdr;
  struct icmp6_hdr send_icmphdr, *recv_icmphdr;
  uint8_t src_mac[MAC_LEN] = {0}, *send_ether_frame, *recv_ether_frame;
  struct addrinfo hints, *res;
  struct sockaddr_in6 dst;
  struct sockaddr_ll device, from;
  struct ifreq ifr;
  socklen_t fromlen;
  struct timespec t1, t2;
  struct pollfd pfd;
  double elapsed, remaining;

  memset (&send_iphdr, 0, sizeof (send_iphdr)); 
  memset (&send_icmphdr, 0, sizeof (send_icmphdr));

  // Allocate memory for various arrays.
  send_ether_frame = allocate_ustrmem (ETH_HDRLEN + IP_MAXPACKET);
  recv_ether_frame = allocate_ustrmem (ETH_HDRLEN + IP_MAXPACKET);
  interface = allocate_strmem (sizeof (ifr.ifr_name));
  target = allocate_strmem (HOSTNAME_LEN);
  src_ip = allocate_strmem (INET6_ADDRSTRLEN);
  dst_ip = allocate_strmem (INET6_ADDRSTRLEN);
  rec_ip = allocate_strmem (INET6_ADDRSTRLEN);

  // Random number seed
  srand ((unsigned) time (NULL));

  // Interface to send packet through.
  snprintf (interface, sizeof (ifr.ifr_name), "enp7s0");

  // Submit request for a socket descriptor to look up interface.
  if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {
    status = errno;
    fprintf (stderr, "socket() failed to get socket descriptor for using ioctl() and sending packets.\nError message: %s\n", strerror (status));
    exit (EXIT_FAILURE);
  }

  // Use ioctl() to look up interface name and get its MAC address.
  memset (&ifr, 0, sizeof (ifr));
  n = snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), "%s", interface);
  if ((n < 0) || (n >= (int) sizeof (ifr.ifr_name))) {
    fprintf (stderr, "Invalid interface name: %s\n", interface);
    exit (EXIT_FAILURE);
  }
  if (ioctl (sd, SIOCGIFHWADDR, &ifr) < 0) {
    fprintf (stderr, "ioctl(SIOCGIFHWADDR) failed to get source MAC address.\nError message: %s\n", strerror (errno));
    close (sd);
    exit (EXIT_FAILURE);
  }
  close (sd);

  // Copy source MAC address.
  memcpy (src_mac, ifr.ifr_hwaddr.sa_data, sizeof (src_mac));

  // Report source MAC address to stdout.
  fprintf (stdout, "MAC address for interface %s is ", interface);
  for (i = 0; i < (int) sizeof (src_mac); i++) {
    fprintf (stdout, "%02x%s", src_mac[i], (i < (int) sizeof (src_mac) - 1) ? ":" : "\n");
  }

  // Destination Ethernet MAC address: You need to fill these out.
  // For off-link destinations, this is normally the next-hop router's MAC address.
  uint8_t dst_mac[MAC_LEN] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x01};

  // Source IPv6 address: You need to fill this out.
  snprintf (src_ip, INET6_ADDRSTRLEN, "2001:db8::214:51ff:fe2f:1556");

  // Destination hostname or IPv6 address: You need to fill this out.
  snprintf (target, HOSTNAME_LEN, "ipv6.google.com");

  // Fill out hints for getaddrinfo().
  memset (&hints, 0, sizeof (hints));
  hints.ai_family = AF_INET6;
  hints.ai_socktype = 0;  // Address resolution only; any socket type.
  hints.ai_flags = hints.ai_flags | AI_CANONNAME;

  // Resolve target using getaddrinfo().
  if ((status = getaddrinfo (target, NULL, &hints, &res)) != 0) {
    fprintf (stderr, "getaddrinfo() failed for target.\nError message: %s\n", gai_strerror (status));
    exit (EXIT_FAILURE);
  }
  memset (&dst, 0, sizeof (dst));
  memcpy (&dst, res->ai_addr, res->ai_addrlen);
  if (inet_ntop (AF_INET6, &dst.sin6_addr, dst_ip, INET6_ADDRSTRLEN) == NULL) {
    status = errno;
    fprintf (stderr, "inet_ntop() failed for target.\nError message: %s\n", strerror (status));
    exit (EXIT_FAILURE);
  }
  freeaddrinfo (res);

  // Fill out device's sockaddr_ll struct.
  memset (&device, 0, sizeof (device));
  device.sll_family = AF_PACKET;
  device.sll_protocol = htons (ETH_P_IPV6);
  if ((device.sll_ifindex = if_nametoindex (interface)) == 0) {
    status = errno;
    fprintf (stderr, "if_nametoindex(\"%s\") failed to obtain interface index.\nError message: %s\n", interface, strerror (status));
    exit (EXIT_FAILURE);
  }
  fprintf (stdout, "Index for interface %s is %d\n", interface, device.sll_ifindex);
  memcpy (device.sll_addr, dst_mac, sizeof (dst_mac));
  device.sll_halen = sizeof (dst_mac);

  // ICMP data
  uint8_t icmp_data[] = {'T', 'e', 's', 't'};
  icmp_datalen = sizeof (icmp_data);

  // IPv6 header

  // IPv6 version (4 bits), Traffic class (8 bits), Flow label (20 bits)
  send_iphdr.ip6_flow = htonl ((6 << 28) | (0 << 20) | 0);

  // Payload length (16 bits): ICMP header + ICMP data
  send_iphdr.ip6_plen = htons (ICMP_HDRLEN + icmp_datalen);

  // Next header (8 bits): ICMPv6
  send_iphdr.ip6_nxt = IPPROTO_ICMPV6;

  // Hop limit (8 bits): Default to maximum value.
  send_iphdr.ip6_hops = 255;

  // Source IPv6 address (128 bits)
  if ((status = inet_pton (AF_INET6, src_ip, &(send_iphdr.ip6_src))) != 1) {
    if (status == 0) {
      fprintf (stderr, "inet_pton() failed for source address.\nError message: Invalid address\n");
    } else if (status < 0) {
      fprintf (stderr, "inet_pton() failed for source address.\nError message: %s\n", strerror (errno));
    }
    exit (EXIT_FAILURE);
  }

  // Destination IPv6 address (128 bits)
  if ((status = inet_pton (AF_INET6, dst_ip, &(send_iphdr.ip6_dst))) != 1) {
    if (status == 0) {
      fprintf (stderr, "inet_pton() failed for destination address.\nError message: Invalid address\n");
    } else if (status < 0) {
      fprintf (stderr, "inet_pton() failed for destination address.\nError message: %s\n", strerror (errno));
    }
    exit (EXIT_FAILURE);
  }

  // ICMP header

  // Message Type (8 bits): echo request
  send_icmphdr.icmp6_type = ICMP6_ECHO_REQUEST;

  // Message Code (8 bits): Not used for Echo Request and Echo Reply; Set to 0.
  send_icmphdr.icmp6_code = 0;

  // Identifier (16 bits): Usually pid of sending process; Pick a number.
  send_icmphdr.icmp6_id = htons (1000);

  // Sequence Number (16 bits): Starts at 0.
  send_icmphdr.icmp6_seq = htons (0);

  // ICMP header checksum (16 bits): Set to 0 when calculating checksum.
  send_icmphdr.icmp6_cksum = 0;

  // Fill out Ethernet frame header.

  // Ethernet frame length = Ethernet header (MAC + MAC + Ethernet type) + Ethernet data (IP header + ICMP header + ICMP data)
  frame_length = ETH_HDRLEN + IP6_HDRLEN + ICMP_HDRLEN + icmp_datalen;

  // Destination and Source MAC addresses
  memcpy (send_ether_frame, dst_mac, sizeof (dst_mac));
  memcpy (send_ether_frame + sizeof (dst_mac), src_mac, sizeof (src_mac));

  // EtherType (16 bits): ETH_P_IPV6
  // http://www.iana.org/assignments/ethernet-numbers
  send_ether_frame[12] = ETH_P_IPV6 / 256;
  send_ether_frame[13] = ETH_P_IPV6 % 256;

  // Next is Ethernet frame data (IPv6 header + ICMP header + ICMP data).

  // IPv6 header
  memcpy (send_ether_frame + ETH_HDRLEN, &send_iphdr, IP6_HDRLEN);

  // ICMP header
  memcpy (send_ether_frame + ETH_HDRLEN + IP6_HDRLEN, &send_icmphdr, ICMP_HDRLEN);

  // ICMP data
  memcpy (send_ether_frame + ETH_HDRLEN + IP6_HDRLEN + ICMP_HDRLEN, icmp_data, icmp_datalen);

  // ICMP header checksum (16 bits): Set to 0 when calculating checksum.
  // Already set to 0 above.
  send_icmphdr.icmp6_cksum = icmp6_checksum (send_iphdr, send_ether_frame + ETH_HDRLEN + IP6_HDRLEN, ICMP_HDRLEN + icmp_datalen);
  memcpy (send_ether_frame + ETH_HDRLEN + IP6_HDRLEN, &send_icmphdr, ICMP_HDRLEN);  // Save ICMP header with checksum to Ethernet frame.
  fprintf (stdout, "Checksum: 0x%x\n", ntohs (send_icmphdr.icmp6_cksum));

  // Submit request for a raw socket descriptor to send packets.
  if ((sendsd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) {
    status = errno;
    fprintf (stderr, "socket() failed to get send socket descriptor.\nError message: %s\n", strerror (status));
    exit (EXIT_FAILURE);
  }

  // Submit request for a raw socket descriptor to receive packets.
  // Use ETH_P_IPV6 in order to only look at IPv6 packets; could use ETH_P_ALL but likely slower on a busy network.
  if ((recvsd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_IPV6))) < 0) {
    status = errno;
    fprintf (stderr, "socket() failed to get receive socket descriptor.\nError message: %s\n", strerror (status));
    exit (EXIT_FAILURE);
  }

  // Set maximum number of tries to ping remote host before giving up.
  trylim = 3;
  trycount = 0;

  done = 0;
  for (;;) {

    // SEND

    // Set sequence number for this attempt and recompute ICMP checksum.
    // This prevents a delayed reply from an earlier attempt from matching a later attempt.
    send_icmphdr.icmp6_seq = htons (trycount);
    send_icmphdr.icmp6_cksum = 0;
    memcpy (send_ether_frame + ETH_HDRLEN + IP6_HDRLEN, &send_icmphdr, ICMP_HDRLEN);
    send_icmphdr.icmp6_cksum = icmp6_checksum (send_iphdr, send_ether_frame + ETH_HDRLEN + IP6_HDRLEN, ICMP_HDRLEN + icmp_datalen);
    memcpy (send_ether_frame + ETH_HDRLEN + IP6_HDRLEN, &send_icmphdr, ICMP_HDRLEN);  // Save ICMP header with checksum to Ethernet frame.

    // Send Ethernet frame to socket.
    bytes = sendto (sendsd, send_ether_frame, frame_length, 0, (struct sockaddr *) &device, sizeof (device));
    if (bytes == -1) {
      status = errno;
      fprintf (stderr, "sendto() failed.\nError message: %s\n", strerror (status));
      exit (EXIT_FAILURE);
    }
    // Check for short send.
    if (bytes != frame_length) {
      fprintf (stderr, "sendto() sent %zd bytes but expected to send %d bytes.\n", bytes, frame_length);
      exit (EXIT_FAILURE);
    }

    // Start timer.
    (void) clock_gettime (CLOCK_MONOTONIC, &t1);

    // Listen for incoming Ethernet frame from socket recvsd.
    // We expect an ICMPv6 Ethernet frame of the form:
    //     MAC (6 bytes) + MAC (6 bytes) + Ethernet Type (2 bytes)
    //     + Ethernet data (IPv6 header + ICMP header)
    // Keep listening for up to TIMEOUT seconds, or until an ICMPv6 Echo Reply is received.

    // RECEIVE LOOP
    for (;;) {

      memset (recv_ether_frame, 0, ETH_HDRLEN + IP_MAXPACKET);
      memset (&from, 0, sizeof (from));
      fromlen = sizeof (from);

      // Set up pollfd structure for poll().
      memset (&pfd, 0, sizeof (pfd));
      pfd.fd = recvsd;
      pfd.events = POLLIN;

      // Calculate elapsed and remaining times.
      clock_gettime (CLOCK_MONOTONIC, &t2);
      elapsed = (double) (t2.tv_sec - t1.tv_sec) + (double) (t2.tv_nsec - t1.tv_nsec) / 1000000000.0;
      remaining = TIMEOUT - elapsed;

      if (remaining <= 0.0) {
        fprintf (stdout, "No reply within %d seconds.\n", TIMEOUT);
        trycount++;
        break;
      }

      timeout_ms = (int) (remaining * 1000.0);  // milliseconds
      if (timeout_ms < 1) timeout_ms = 1;

      // Wait for data to be available on our receive socket, or until we time-out.
      status = poll (&pfd, 1, timeout_ms);
      if (status < 0) {
        status = errno;
        if (status == EINTR) {
          continue;
        } else {
          fprintf (stderr, "poll() failed. Error message: %s\n", strerror (status));
          exit (EXIT_FAILURE);
        }
      }

      // Receive socket timed-out.
      if (status == 0) {
        fprintf (stdout, "No reply within %d seconds.\n", TIMEOUT);
        trycount++;
        break;  // Break out of Receive loop.
      }

      // Check for socket error conditions reported by poll().
      if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
        fprintf (stderr, "poll() reported socket error: revents = 0x%x.\n", pfd.revents);
        exit (EXIT_FAILURE);
      }

      // If pfd has POLLIN set in revents, then recvsd (i.e., pfd.fd) is ready for reading.
      if (pfd.revents & POLLIN) {

        // Read available data from recvsd.
        bytes = recvfrom (recvsd, recv_ether_frame, ETH_HDRLEN + IP_MAXPACKET, 0, (struct sockaddr *) &from, &fromlen);

        // Deal with error conditions first.
        if (bytes < 0) {
          status = errno;
          if ((status == EINTR) || (status == EAGAIN) || (status == EWOULDBLOCK)) {  // EINTR = 4
            continue;  // System call interrupted by a signal before completion. Retry.
          } else {
            fprintf (stderr, "recvfrom() failed.\nError message: %s\n", strerror (status));
            exit (EXIT_FAILURE);
          }
        }

        // Ignore packets received on other interfaces.
        if (from.sll_ifindex != device.sll_ifindex) continue;

        // Check for malformed packet; insufficient bytes to parse Ethernet header.
        if ((bytes >= 0) && (bytes < ETH_HDRLEN)) continue;

      // poll() returned, but no readable data was available; keep listening.
      } else {
        continue;
      }

      if (bytes < (ETH_HDRLEN + IP6_HDRLEN)) continue;

      if (((recv_ether_frame[12] << 8) + recv_ether_frame[13]) != ETH_P_IPV6) continue;

      recv_iphdr = (struct ip6_hdr *) (recv_ether_frame + ETH_HDRLEN);

      if ((recv_iphdr->ip6_vfc >> 4) != 6) continue;

      ip_total_len = ntohs (recv_iphdr->ip6_plen);
      if (ip_total_len < ICMP_HDRLEN) continue;
      if (bytes < (ETH_HDRLEN + IP6_HDRLEN + ip_total_len)) continue;

      if (recv_iphdr->ip6_nxt != IPPROTO_ICMPV6) continue;

      recv_icmphdr = (struct icmp6_hdr *) (recv_ether_frame + ETH_HDRLEN + IP6_HDRLEN);


      // Check for an IPv6 Ethernet frame, carrying ICMPv6 echo reply. If not, ignore and keep listening.
      // Make sure it's an ICMPv6 ECHO REPLY with code 0, and match ID, Sequence #, source and destination addresses.
      if ((recv_icmphdr->icmp6_type == ICMP6_ECHO_REPLY) &&
          (recv_icmphdr->icmp6_code == 0) &&
          (recv_icmphdr->icmp6_id == send_icmphdr.icmp6_id) &&
          (recv_icmphdr->icmp6_seq == send_icmphdr.icmp6_seq) &&
          (memcmp (&recv_iphdr->ip6_src, &send_iphdr.ip6_dst, sizeof (struct in6_addr)) == 0) &&
          (memcmp (&recv_iphdr->ip6_dst, &send_iphdr.ip6_src, sizeof (struct in6_addr)) == 0)) {

        // Stop timer and calculate how long it took to get a reply.
        (void) clock_gettime (CLOCK_MONOTONIC, &t2);
        elapsed = (double) (t2.tv_sec - t1.tv_sec) + (double) (t2.tv_nsec - t1.tv_nsec) / 1000000000.0;
        remaining = TIMEOUT - elapsed;
        if (remaining < 0) remaining = 0;

        // Extract source IP address from received Ethernet frame.
        if (inet_ntop (AF_INET6, &(recv_iphdr->ip6_src), rec_ip, INET6_ADDRSTRLEN) == NULL) {
          status = errno;
          fprintf (stderr, "inet_ntop() failed.\nError message: %s\n", strerror (status));
          exit (EXIT_FAILURE);
        }

        // Report source IPv6 address and time for reply.
        fprintf (stdout, "%s  %g ms (%zd bytes received)\n", rec_ip, elapsed * 1000.0, bytes);
        done = 1;
        break;  // Break out of Receive loop.
      }  // End if IPv6 Ethernet frame carrying ICMP6_ECHO_REPLY
    }  // End of Receive loop.

    // The 'done' flag was set because an echo reply was received; break out of send loop.
    if (done == 1) {
      break;  // Break out of Send loop.
    }

    // We ran out of tries, so let's give up.
    if (trycount == trylim) {
      fprintf (stdout, "Recognized no echo replies from remote host after %d tries.\n", trylim);
      break;
    }

  }  // End of Send loop.

  // Close socket descriptors.
  close (sendsd);
  close (recvsd);

  // Free allocated memory.
  free (send_ether_frame);
  free (recv_ether_frame);
  free (interface);
  free (target);
  free (src_ip);
  free (dst_ip);
  free (rec_ip);

  return (EXIT_SUCCESS);
}

// Computing the internet checksum (RFC 1071).
// Note that the internet checksum is not guaranteed to preclude collisions.
uint16_t
checksum (uint8_t *addr, int len) {

  int count = len;
  uint32_t sum = 0;
  uint16_t answer = 0;

  // Sum up 2-byte values until none or only one byte left.
  while (count > 1) {
    sum += ((uint16_t) addr[0] << 8) + addr[1];
    addr += 2;
    count -= 2;
  }

  // Add left-over byte, if any. For an odd-length buffer, the
  // remaining byte is the high-order byte of the final 16-bit word.
  if (count > 0) {
    sum += ((uint16_t) addr[0] << 8);
  }

  // Fold the accumulated sum into 16 bits by repeatedly adding
  // carries back into the low 16 bits (one's-complement arithmetic).
  // sum = (lower 16 bits) + (upper 16 bits shifted right 16 bits)
  while (sum >> 16) {
    sum = (sum & 0xffff) + (sum >> 16);
  }

  // Checksum is one's-complement of sum. Return it in network byte order
  // so it can be copied directly into the packet header.
  answer = ~sum;

  return (htons (answer));
}

// Build ICMPv6 pseudo-header and call checksum function (Section 8.1 of RFC 2460).
uint16_t
icmp6_checksum (struct ip6_hdr iphdr, uint8_t *icmp_msg, int icmp_len) {

  uint8_t *buf, *ptr, cvalue = IPPROTO_ICMPV6;
  uint16_t answer = 0;
  uint32_t lvalue;

  if (icmp_len < 0) {
    fprintf (stderr, "ERROR: icmp_len must not be negative in icmp6_checksum().\n");
    exit (EXIT_FAILURE);
  }
  if (icmp_len < ICMP_HDRLEN) {
    fprintf (stderr, "ERROR: icmp_len is too small to hold an ICMPv6 header in icmp6_checksum().\n");
    exit (EXIT_FAILURE);
  }
  if (icmp_msg == NULL) {
    fprintf (stderr, "ERROR: icmp_msg is NULL in icmp6_checksum().\n");
    exit (EXIT_FAILURE);
  }

  // Allocate memory for buffer.
  buf = allocate_ustrmem (40 + icmp_len + 1);  // Add 1 for possible padding.
  ptr = &buf[0];  // ptr points to beginning of buffer buf

  // Copy source IP address into buf (128 bits)
  memcpy (ptr, &iphdr.ip6_src.s6_addr, sizeof (iphdr.ip6_src.s6_addr));
  ptr += sizeof (iphdr.ip6_src.s6_addr);

  // Copy destination IP address into buf (128 bits)
  memcpy (ptr, &iphdr.ip6_dst.s6_addr, sizeof (iphdr.ip6_dst.s6_addr));
  ptr += sizeof (iphdr.ip6_dst.s6_addr);

  // Copy Upper-Layer Packet Length into buf (32 bits).
  lvalue = htonl (icmp_len);
  memcpy (ptr, &lvalue, sizeof (lvalue));
  ptr += sizeof (lvalue);

  // Copy zero field to buf (24 bits)
  *ptr = 0; ptr++;
  *ptr = 0; ptr++;
  *ptr = 0; ptr++;

  // Copy next header field to buf (8 bits)
  memcpy (ptr, &cvalue, sizeof (cvalue));
  ptr += sizeof (cvalue);

  // Copy ICMP header and ICMP data.
  memcpy (ptr, icmp_msg, icmp_len);

  // ICMP checksum field is bytes 2 and 3 of the ICMP message.
  // Set to zero for checksum calculation.
  buf[40 + 2] = 0;
  buf[40 + 3] = 0;

  answer = checksum (buf, 40 + icmp_len);

  // Free allocated memory.
  free (buf);

  return (answer);
}

// Allocate memory for an array of chars.
char *
allocate_strmem (int len) {

  void *tmp;

  if (len <= 0) {
    fprintf (stderr, "ERROR: Cannot allocate memory because len = %d in allocate_strmem().\n", len);
    exit (EXIT_FAILURE);
  }

  tmp = calloc (len, sizeof (char));
  if (tmp != NULL) {
    return (tmp);
  } else {
    fprintf (stderr, "ERROR: Cannot allocate memory for array allocate_strmem().\n");
    exit (EXIT_FAILURE);
  }
}

// Allocate memory for an array of unsigned chars.
uint8_t *
allocate_ustrmem (int len) {

  void *tmp;

  if (len <= 0) {
    fprintf (stderr, "ERROR: Cannot allocate memory because len = %d in allocate_ustrmem().\n", len);
    exit (EXIT_FAILURE);
  }

  tmp = calloc (len, sizeof (uint8_t));
  if (tmp != NULL) {
    return (tmp);
  } else {
    fprintf (stderr, "ERROR: Cannot allocate memory for array allocate_ustrmem().\n");
    exit (EXIT_FAILURE);
  }
}
