Advertisement
Guest User

client.cpp

a guest
Jul 10th, 2019
786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.96 KB | None | 0 0
  1. /*  Copyright (C) 2011-2015  P.D. Buchan ([email protected])
  2.  
  3.     This program is free software: you can redistribute it and/or modify
  4.     it under the terms of the GNU General Public License as published by
  5.     the Free Software Foundation, either version 3 of the License, or
  6.     (at your option) any later version.
  7.  
  8.     This program is distributed in the hope that it will be useful,
  9.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.     GNU General Public License for more details.
  12.  
  13.     You should have received a copy of the GNU General Public License
  14.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>           // close()
  20. #include <string.h>           // strcpy, memset(), and memcpy()
  21.  
  22. #include <netdb.h>            // struct addrinfo
  23. #include <sys/types.h>        // needed for socket(), uint8_t, uint16_t, uint32_t
  24. #include <sys/socket.h>       // needed for socket()
  25. #include <netinet/in.h>       // IPPROTO_RAW, IPPROTO_IP, IPPROTO_TCP, INET_ADDRSTRLEN
  26. #include <netinet/ip.h>       // struct ip and IP_MAXPACKET (which is 65535)
  27. #include <arpa/inet.h>        // inet_pton() and inet_ntop()
  28. #include <sys/ioctl.h>        // macro ioctl is defined
  29. #include <bits/ioctls.h>      // defines values for argument "request" of ioctl.
  30. #include <net/if.h>           // struct ifreq
  31.  
  32. #include <errno.h>            // errno, perror()
  33.  
  34. // Define some constants.
  35. #define IP4_HDRLEN 20         // IPv4 header length
  36.  
  37. // Function prototypes
  38. uint16_t checksum (uint16_t *, int);
  39. char *allocate_strmem (int);
  40. uint8_t *allocate_ustrmem (int);
  41. int *allocate_intmem (int);
  42.  
  43. int
  44. main (int argc, char **argv)
  45. {
  46.     int status, sd, *ip_flags;
  47.     const int on = 1;
  48.     char *interface, *target, *src_ip, *dst_ip;
  49.     struct ip iphdr;
  50.     uint8_t *packet;
  51.     struct addrinfo hints, *res;
  52.     struct sockaddr_in *ipv4, sin;
  53.     struct ifreq ifr;
  54.     void *tmp;
  55.  
  56.     // Allocate memory for various arrays.
  57.     packet = allocate_ustrmem (IP_MAXPACKET);
  58.     interface = allocate_strmem (40);
  59.     target = allocate_strmem (40);
  60.     src_ip = allocate_strmem (INET_ADDRSTRLEN);
  61.     dst_ip = allocate_strmem (INET_ADDRSTRLEN);
  62.     ip_flags = allocate_intmem (4);
  63.  
  64.     // Interface to send packet through.
  65.     strcpy (interface, "enp2s0");
  66.  
  67.     // Submit request for a socket descriptor to look up interface.
  68.     if ((sd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
  69.         perror ("socket() failed to get socket descriptor for using ioctl() ");
  70.         exit (EXIT_FAILURE);
  71.     }
  72.  
  73.     // Use ioctl() to look up interface index which we will use to
  74.     // bind socket descriptor sd to specified interface with setsockopt() since
  75.     // none of the other arguments of sendto() specify which interface to use.
  76.     memset (&ifr, 0, sizeof (ifr));
  77.     snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), "%s", interface);
  78.     if (ioctl (sd, SIOCGIFINDEX, &ifr) < 0) {
  79.         perror ("ioctl() failed to find interface ");
  80.         return (EXIT_FAILURE);
  81.     }
  82.     close (sd);
  83.     printf ("Index for interface %s is %i\n", interface, ifr.ifr_ifindex);
  84.  
  85.     // Source IPv4 address: you need to fill this out
  86.     strcpy (src_ip, "192.168.0.1");
  87.  
  88.     // Destination URL or IPv4 address: you need to fill this out
  89.     strcpy (target, "192.168.0.168");
  90.  
  91.     // Fill out hints for getaddrinfo().
  92.     memset (&hints, 0, sizeof (struct addrinfo));
  93.     hints.ai_family = AF_INET;
  94.     hints.ai_socktype = SOCK_RAW;
  95.     hints.ai_flags = hints.ai_flags | AI_CANONNAME;
  96.  
  97.     // Resolve target using getaddrinfo().
  98.     if ((status = getaddrinfo (target, NULL, &hints, &res)) != 0) {
  99.         fprintf (stderr, "getaddrinfo() failed: %s\n", gai_strerror (status));
  100.         exit (EXIT_FAILURE);
  101.     }
  102.     ipv4 = (struct sockaddr_in *) res->ai_addr;
  103.     tmp = &(ipv4->sin_addr);
  104.     if (inet_ntop (AF_INET, tmp, dst_ip, INET_ADDRSTRLEN) == NULL) {
  105.         status = errno;
  106.         fprintf (stderr, "inet_ntop() failed.\nError message: %s", strerror (status));
  107.         exit (EXIT_FAILURE);
  108.     }
  109.     freeaddrinfo (res);
  110.  
  111.     // IPv4 header
  112.  
  113.     // IPv4 header length (4 bits): Number of 32-bit words in header = 5
  114.     iphdr.ip_hl = IP4_HDRLEN / sizeof (uint32_t);
  115.  
  116.     // Internet Protocol version (4 bits): IPv4
  117.     iphdr.ip_v = 4;
  118.  
  119.     // Type of service (8 bits)
  120.     iphdr.ip_tos = 0;
  121.  
  122.     // Total length of datagram (16 bits): IP header
  123.     iphdr.ip_len = htons (IP4_HDRLEN);
  124.  
  125.     // ID sequence number (16 bits): unused, since single datagram
  126.     iphdr.ip_id = htons (0);
  127.  
  128.     // Flags, and Fragmentation offset (3, 13 bits): 0 since single datagram
  129.  
  130.     // Zero (1 bit)
  131.     ip_flags[0] = 0;
  132.  
  133.     // Do not fragment flag (1 bit)
  134.     ip_flags[1] = 0;
  135.  
  136.     // More fragments following flag (1 bit)
  137.     ip_flags[2] = 0;
  138.  
  139.     // Fragmentation offset (13 bits)
  140.     ip_flags[3] = 0;
  141.  
  142.     iphdr.ip_off = htons ((ip_flags[0] << 15)
  143.                           + (ip_flags[1] << 14)
  144.                           + (ip_flags[2] << 13)
  145.                           +  ip_flags[3]);
  146.  
  147.     // Time-to-Live (8 bits): default to maximum value
  148.     iphdr.ip_ttl = 255;
  149.  
  150.     iphdr.ip_p = 200;
  151.  
  152.     // Source IPv4 address (32 bits)
  153.     if ((status = inet_pton (AF_INET, src_ip, &(iphdr.ip_src))) != 1) {
  154.         fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
  155.         exit (EXIT_FAILURE);
  156.     }
  157.  
  158.     // Destination IPv4 address (32 bits)
  159.     if ((status = inet_pton (AF_INET, dst_ip, &(iphdr.ip_dst))) != 1) {
  160.         fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
  161.         exit (EXIT_FAILURE);
  162.     }
  163.  
  164.     // IPv4 header checksum (16 bits): set to 0 when calculating checksum
  165.     iphdr.ip_sum = 0;
  166.     iphdr.ip_sum = checksum ((uint16_t *) &iphdr, IP4_HDRLEN);
  167.  
  168.     // Prepare packet.
  169.  
  170.     // First part is an IPv4 header.
  171.     memcpy (packet, &iphdr, IP4_HDRLEN * sizeof (uint8_t));
  172.  
  173.     // The kernel is going to prepare layer 2 information (ethernet frame header) for us.
  174.     // For that, we need to specify a destination for the kernel in order for it
  175.     // to decide where to send the raw datagram. We fill in a struct in_addr with
  176.     // the desired destination IP address, and pass this structure to the sendto() function.
  177.     memset (&sin, 0, sizeof (struct sockaddr_in));
  178.     sin.sin_family = AF_INET;
  179.     sin.sin_addr.s_addr = iphdr.ip_dst.s_addr;
  180.  
  181.     // Submit request for a raw socket descriptor.
  182.     if ((sd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
  183.         perror ("socket() failed ");
  184.         exit (EXIT_FAILURE);
  185.     }
  186.  
  187.     // Set flag so socket expects us to provide IPv4 header.
  188.     if (setsockopt (sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof (on)) < 0) {
  189.         perror ("setsockopt() failed to set IP_HDRINCL ");
  190.         exit (EXIT_FAILURE);
  191.     }
  192.  
  193.     // Bind socket to interface index.
  194.     if (setsockopt (sd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof (ifr)) < 0) {
  195.         perror ("setsockopt() failed to bind to interface ");
  196.         exit (EXIT_FAILURE);
  197.     }
  198.  
  199.     // Send packet.
  200.     if (sendto (sd, packet, IP4_HDRLEN, 0, (struct sockaddr *) &sin, sizeof (struct sockaddr)) < 0)  {
  201.         perror ("sendto() failed ");
  202.         exit (EXIT_FAILURE);
  203.      }
  204.  
  205.     printf("packet sent\n");
  206.  
  207.     // Close socket descriptor.
  208.     close (sd);
  209.  
  210.     // Free allocated memory.
  211.     free (packet);
  212.     free (interface);
  213.     free (target);
  214.     free (src_ip);
  215.     free (dst_ip);
  216.     free (ip_flags);
  217.  
  218.     return (EXIT_SUCCESS);
  219. }
  220.  
  221. // Computing the internet checksum (RFC 1071).
  222. uint16_t
  223. checksum (uint16_t *addr, int len)
  224. {
  225.     int count = len;
  226.     register uint32_t sum = 0;
  227.     uint16_t answer = 0;
  228.  
  229.     // Sum up 2-byte values until none or only one byte left.
  230.     while (count > 1) {
  231.         sum += *(addr++);
  232.         count -= 2;
  233.     }
  234.  
  235.     // Add left-over byte, if any.
  236.     if (count > 0) {
  237.         sum += *(uint8_t *) addr;
  238.     }
  239.  
  240.     // Fold 32-bit sum into 16 bits; we lose information by doing this,
  241.     // increasing the chances of a collision.
  242.     // sum = (lower 16 bits) + (upper 16 bits shifted right 16 bits)
  243.     while (sum >> 16) {
  244.         sum = (sum & 0xffff) + (sum >> 16);
  245.     }
  246.  
  247.     // Checksum is one's compliment of sum.
  248.     answer = ~sum;
  249.  
  250.     return (answer);
  251. }
  252.  
  253. // Allocate memory for an array of chars.
  254. char *
  255. allocate_strmem (int len)
  256. {
  257.     char *tmp;
  258.  
  259.     if (len <= 0) {
  260.         fprintf (stderr, "ERROR: Cannot allocate memory because len = %i in allocate_strmem().\n", len);
  261.         exit (EXIT_FAILURE);
  262.     }
  263.  
  264.     tmp = (char *) malloc (len * sizeof (char));
  265.     if (tmp != NULL) {
  266.         memset (tmp, 0, len * sizeof (char));
  267.         return (tmp);
  268.     } else {
  269.         fprintf (stderr, "ERROR: Cannot allocate memory for array allocate_strmem().\n");
  270.         exit (EXIT_FAILURE);
  271.     }
  272. }
  273.  
  274. // Allocate memory for an array of unsigned chars.
  275. uint8_t *
  276. allocate_ustrmem (int len)
  277. {
  278.     uint8_t *tmp;
  279.  
  280.     if (len <= 0) {
  281.         fprintf (stderr, "ERROR: Cannot allocate memory because len = %i in allocate_ustrmem().\n", len);
  282.         exit (EXIT_FAILURE);
  283.     }
  284.  
  285.     tmp = (uint8_t *) malloc (len * sizeof (uint8_t));
  286.     if (tmp != NULL) {
  287.         memset (tmp, 0, len * sizeof (uint8_t));
  288.         return (tmp);
  289.     } else {
  290.         fprintf (stderr, "ERROR: Cannot allocate memory for array allocate_ustrmem().\n");
  291.         exit (EXIT_FAILURE);
  292.     }
  293. }
  294.  
  295. // Allocate memory for an array of ints.
  296. int *
  297. allocate_intmem (int len)
  298. {
  299.     int *tmp;
  300.  
  301.     if (len <= 0) {
  302.         fprintf (stderr, "ERROR: Cannot allocate memory because len = %i in allocate_intmem().\n", len);
  303.         exit (EXIT_FAILURE);
  304.     }
  305.  
  306.     tmp = (int *) malloc (len * sizeof (int));
  307.     if (tmp != NULL) {
  308.         memset (tmp, 0, len * sizeof (int));
  309.         return (tmp);
  310.     } else {
  311.         fprintf (stderr, "ERROR: Cannot allocate memory for array allocate_intmem().\n");
  312.         exit (EXIT_FAILURE);
  313.     }
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement