Facebook
From mat, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 239
  1. src h
  2.  
  3.  
  4.  
  5. #ifndef SRC_IPV6_H_
  6. #define SRC_IPV6_H_
  7.  
  8.  
  9.  
  10. unsigned char * CreateIPV6Packet ();
  11.  
  12. #endif /* SRC_IPV6_H_ */
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. imcp v6 c
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>           // close()
  26. #include <string.h>           // strcpy, memset(), and memcpy()
  27.  
  28. #include <netinet/in.h>       // IPPROTO_RAW, IPPROTO_UDP, INET6_ADDRSTRLEN
  29. #include <netinet/ip6.h>      // struct ip6_hdr
  30. #include <netinet/icmp6.h>
  31.  
  32.  
  33.  
  34. void CreateICMP6Packet (unsigned char *datagram) {
  35.  
  36.         struct ip6_hdr *iphdr = (struct ip6_hdr*) datagram;
  37.         char input [32];
  38.         struct icmp6_hdr *icmphdr = (struct icmp6_hdr *) (datagram + sizeof (struct ip6_hdr));
  39.  
  40.         printf ("ICMP type (128 PING):");
  41.         fgets (input, 32, stdin);
  42.         if (atoi(input)== 0)
  43.                 icmphdr->icmp6_type = 128;
  44.         else
  45.                 icmphdr->icmp6_type = atoi (input);
  46.  
  47.         printf ("ICMP code (0):");
  48.         fgets (input, 32, stdin);
  49.         if (atoi(input)== 0)
  50.                 icmphdr->icmp6_code = 0;
  51.         else
  52.                 icmphdr->icmp6_code = atoi (input);
  53.  
  54.         printf ("ICMP id (12345):");
  55.         fgets (input, 32, stdin);
  56.         if (atoi(input)== 0)
  57.                 icmphdr->icmp6_dataun.icmp6_un_data16 [0] = htons (12345);
  58.         else
  59.                 icmphdr->icmp6_dataun.icmp6_un_data16 [0] = htons (atoi (input));
  60.  
  61.         printf ("ICMP sequence number (0):");
  62.         fgets (input, 32, stdin);
  63.         if (atoi(input)== 0)
  64.                 icmphdr->icmp6_dataun.icmp6_un_data16 [1] = htons (0);
  65.         else
  66.                 icmphdr->icmp6_dataun.icmp6_un_data16 [1] = htons (atoi (input));
  67.  
  68.  
  69.  
  70.         iphdr->ip6_ctlun.ip6_un1.ip6_un1_plen = htons(sizeof (struct icmp6_hdr));
  71.  
  72.         icmphdr->icmp6_cksum = 0;
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80. icmpv6 h
  81.  
  82.  
  83. #ifndef SRC_ICMP6_H_
  84. #define SRC_ICMP6_H_
  85.  
  86. void CreateICMP6Packet (unsigned char *datagram);
  87.  
  88. #endif /* SRC_ICMP6_H_ */
  89.  
  90.  
  91.  
  92.  
  93.  
  94. glowny c
  95.  
  96. /*
  97.  ============================================================================
  98.  Name        : projektC.c
  99.  Author      :
  100.  Version     :
  101.  Copyright   : Your copyright notice
  102.  Description : Hello World in C, Ansi-style
  103.  ============================================================================
  104.  */
  105.  
  106. #include <stdio.h>
  107. #include <stdlib.h>
  108. #include "program_lib.h"
  109. #include <dlfcn.h>
  110. #include <netinet/in.h>
  111. #include <netinet/ip6.h>
  112.  
  113.  
  114.  
  115. int main (void) {
  116.  
  117.         void * Ipv6_lib = malloc(sizeof (void));
  118.         void * Icmp6_lib = malloc(sizeof (void));
  119.  
  120.         char input [32];
  121.         int *count = malloc (sizeof (int));
  122.         struct ip6_hdr *iphdr;
  123.         unsigned char *datagram;
  124.         unsigned char * (*ipv6) () = malloc (sizeof (unsigned short *));
  125.         void (*icmp6) (unsigned char *) = malloc (sizeof (void));
  126.  
  127.         datagram = malloc (sizeof (unsigned char *));
  128.  
  129.  
  130.         Ipv6_lib = LoadIpv6();
  131.  
  132.         ipv6 = dlsym(Ipv6_lib, "CreateIPV6Packet");
  133.         datagram = (*ipv6)();
  134.         iphdr = (struct ip6_hdr *) datagram;
  135.         if ( iphdr->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_ICMPV6){
  136.                 Icmp6_lib = LoadIcmp6();
  137.                 icmp6 = dlsym(Icmp6_lib, "CreateICMP6Packet");
  138.                 (*icmp6) (datagram);
  139.         }
  140.  
  141.         printf ("How many packets do you want to send?");
  142.         fgets (input, 32, stdin);
  143.         *count = atoi(input);
  144.  
  145.         LoadToList( count, datagram );
  146.  
  147.  
  148.         SendPacket(datagram);
  149.         EXIT_SUCCESS;
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158. programlib c
  159.  
  160. #include <dlfcn.h>
  161. #include <stdio.h>
  162. #include <netinet/in.h>
  163. #include <stdlib.h>
  164. #include <net/if.h>
  165. #include "program_lib.h"
  166. #include <string.h>
  167. #include <fcntl.h>
  168. #include <unistd.h>
  169. #include <netdb.h>            // struct addrinfo
  170. #include <sys/types.h>        // needed for socket(), uint8_t, uint16_t
  171. #include <sys/socket.h>       // needed for socket()
  172. #include <netinet/ip6.h>      // struct ip6_hdr
  173. #include <netinet/icmp6.h>
  174. #include <arpa/inet.h>        // inet_pton() and inet_ntop()
  175. #include <sys/ioctl.h>        // macro ioctl is defined
  176. #include <bits/ioctls.h>      // defines values for argument "request" of ioctl.
  177. #include <linux/if_ether.h>   // ETH_P_IP = 0x0800, ETH_P_IPV6 = 0x86DD
  178. #include <linux/if_packet.h>  // struct sockaddr_ll (see man 7 packet)
  179. #include <net/ethernet.h>
  180.  
  181. #include <errno.h>            // errno, perror()
  182.  
  183. struct Node *head = NULL;       //head of the list
  184.  
  185. uint16_t icmp6_checksum (struct ip6_hdr *iphdr, struct icmp6_hdr *icmp6hdr);
  186. uint16_t checksum (uint16_t *addr, int len);
  187.  
  188. //function to load IPv6 library
  189. void * LoadIpv6 (){
  190.  
  191.         void *IpLib = malloc ( sizeof (void ) );        //handle to IP lib
  192.  
  193.         IpLib = dlopen("./ipv6.so", RTLD_LAZY);
  194.                 if (!IpLib)
  195.                         return 0;
  196.                 else
  197.                         return IpLib;
  198.  
  199. }
  200.  
  201. //function to load ICMP library
  202. void * LoadIcmp6 (){
  203.  
  204.         void *IcmpLib6 = malloc ( sizeof (void ) );     //handle to ICMP lib
  205.  
  206.         IcmpLib6 = dlopen("./icmp6.so", RTLD_LAZY);
  207.                 if (!IcmpLib6)
  208.                         return 0;
  209.                 else
  210.                         return IcmpLib6;
  211.  
  212. }
  213.  
  214. //function to send packets from linked list
  215. void SendPacket ( unsigned char *datagram ){
  216.           int i, status, frame_length, sd, bytes;
  217.           char *interface, *src_ip, *dst_ip, cmd [30], input [128];
  218.           struct ip6_hdr *iphdr;
  219.           uint8_t *src_mac, *dst_mac;
  220.           struct addrinfo hints; //*res;
  221.           struct icmp6_hdr *icmphdr = (struct icmp6_hdr *)(datagram + sizeof (struct ip6_hdr));
  222.           //struct sockaddr_in6 *ipv6;
  223.           struct sockaddr_ll device;
  224.           struct ifreq ifr;
  225.  
  226.           iphdr = (struct ip6_hdr *) datagram;
  227.           //icmphdr = (struct icmp6_hdr *) (datagram + sizeof (struct ip6_hdr));
  228.           src_mac = malloc (6);
  229.           dst_mac = malloc (6);
  230.           interface = malloc (INET6_ADDRSTRLEN);
  231.           //target = malloc (INET6_ADDRSTRLEN);
  232.           src_ip = malloc (INET6_ADDRSTRLEN);
  233.           dst_ip = malloc (INET6_ADDRSTRLEN);
  234.  
  235.           printf ("\033[H\033[J");      //cleans console
  236.           printf ("Which interface do you want to use? Type name of interface.\n");
  237.  
  238.  
  239.           //use sys command
  240.           sprintf(cmd, "/bin/ip link");
  241.           system(cmd);
  242.  
  243.           printf ("Name:");
  244.           fgets ( input, 32, stdin );   //get name of interface
  245.           memcpy ( interface, input, strlen (input)-1 );
  246.  
  247.  
  248.           // Submit request for a socket descriptor to look up interface.
  249.           if ((sd = socket (AF_INET6, SOCK_RAW, IPPROTO_RAW)) < 0) {
  250.             perror ("socket() failed to a socket descriptor for using ioctl() ");
  251.             exit (EXIT_FAILURE);
  252.           }
  253.  
  254.           // Use ioctl() to look up interface name and get its MAC address.
  255.           memset (&ifr, 0, sizeof (ifr));
  256.           snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), "%s", interface);
  257.           if (ioctl (sd, SIOCGIFHWADDR, &ifr) < 0) {
  258.             perror ("ioctl() failed to get source MAC address ");
  259.             printf ("please restart program");
  260.             //return (EXIT_FAILURE);
  261.           }
  262.           close (sd);
  263.  
  264.           // Copy source MAC address.
  265.           memcpy (src_mac, ifr.ifr_hwaddr.sa_data, 6 * sizeof (uint8_t));
  266.  
  267.           // Report source MAC address to stdout.
  268.           printf ("MAC address for interface %s is ", interface);
  269.           for (i=0; i<5; i++) {
  270.             printf ("%02x:", src_mac[i]);
  271.           }
  272.           printf ("%02x\n", src_mac[5]);
  273.  
  274.           // Find interface index from interface name and store index in
  275.           // struct sockaddr_ll device, which will be used as an argument of sendto().
  276.           memset (&device, 0, sizeof (device));
  277.           if ((device.sll_ifindex = if_nametoindex (interface)) == 0) {
  278.             perror ("if_nametoindex() failed to obtain interface index ");
  279.             exit (EXIT_FAILURE);
  280.           }
  281.           printf ("Index for interface %s is %i\n", interface, device.sll_ifindex);
  282.  
  283.           // Set destination MAC address: you need to fill this out
  284.           dst_mac[0] = 0x12;
  285.           dst_mac[1] = 0xaf;
  286.           dst_mac[2] = 0x32;
  287.           dst_mac[3] = 0x11;
  288.           dst_mac[4] = 0x54;
  289.           dst_mac[5] = 0x65;
  290.  
  291.           // Source IPv6 address: you need to fill this out
  292.           printf ("[must be set] Type source IP (format 1000:1000::1000:1000:1000:1000): ");
  293.           fgets (input, INET6_ADDRSTRLEN, stdin);
  294.           if (strlen(input) != 0)
  295.                   memcpy ( src_ip, input, strlen (input)-1 );
  296.           else
  297.                   strcpy (src_ip, "1000:1000::1000:1000:1000:1000");
  298.           //if (strlen(src_ip) == 0)
  299.                 //  strcpy (src_ip, "2001:db8::214:51ff:fe2f:1556");
  300.  
  301.           // Destination URL or IPv6 address: you need to fill this out
  302.  
  303.           printf ("[must be set] Type destination IP (format 1000:1000::1000:1000:1000:1000): ");
  304.           fgets (input, INET6_ADDRSTRLEN, stdin);
  305.           if (strlen(input) != 0)
  306.                   memcpy ( dst_ip, input, strlen (input)-1 );
  307.           else
  308.                   strcpy (dst_ip, "1000:1000::1000:1000:1000:1000");
  309.  
  310.           memset (&hints, 0, sizeof (hints));
  311.           hints.ai_family = AF_INET6;
  312.           hints.ai_socktype = SOCK_STREAM;
  313.           hints.ai_flags = hints.ai_flags | AI_CANONNAME;
  314.  
  315.           device.sll_family = AF_PACKET;
  316.           device.sll_protocol = htons (ETH_P_IPV6);
  317.           memcpy (device.sll_addr, dst_mac, 6 * sizeof (uint8_t));
  318.           device.sll_halen = 6;
  319.  
  320.           if ((status = inet_pton (AF_INET6, src_ip, &(iphdr->ip6_src))) != 1) {
  321.             fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
  322.             exit (EXIT_FAILURE);
  323.           }
  324.  
  325.           if ((status = inet_pton (AF_INET6, dst_ip, &(iphdr->ip6_dst))) != 1) {
  326.             fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
  327.             exit (EXIT_FAILURE);
  328.           }
  329.  
  330.  
  331.           frame_length = sizeof (struct ip6_hdr)+ntohs(iphdr->ip6_ctlun.ip6_un1.ip6_un1_plen);
  332.  
  333.           if (iphdr->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_ICMPV6){
  334.                   icmphdr->icmp6_cksum = 0;
  335.                   icmphdr->icmp6_cksum = icmp6_checksum (iphdr, icmphdr);
  336.           }
  337.  
  338.           // Open raw socket descriptor.
  339.           if ((sd = socket (PF_PACKET, SOCK_DGRAM, htons (ETH_P_ALL))) < 0) {
  340.             perror ("socket() failed ");
  341.             exit (EXIT_FAILURE);
  342.           }
  343.  
  344.           //sizeof (struct ip6_hdr) + sizeof (struct icmp6_hdr)
  345.  
  346.           // Send ethernet frame to socket.
  347.           while ( head != NULL ) {
  348.           if ((bytes = sendto (sd, datagram, frame_length, 0, (struct sockaddr *) &device, sizeof (device))) <= 0) {
  349.             perror ("sendto() failed");
  350.             exit (EXIT_FAILURE);
  351.           }
  352.           else
  353.                   printf ("Bytes sent: %d\n", bytes);
  354.  
  355.           head = head -> next;
  356.  
  357.           }
  358.           close (sd);
  359.           DeleteList ();
  360. }
  361.  
  362.  
  363. void LoadToList ( int *count, unsigned char *dtgr ){
  364.  
  365.         for ( int i = 0; i != *count; i++ )
  366.                 InsertTail ( dtgr );
  367. }
  368.  
  369.  
  370.  
  371. //function to reserve memory for new list element
  372. struct Node *ReserveMem ( unsigned char *datagram ){
  373.  
  374.         //create new node
  375.         struct Node *new_node;
  376.         static int id = 1;
  377.  
  378.         //reserve memory for new node
  379.         new_node = malloc(sizeof (struct Node));
  380.  
  381.  
  382.         if (new_node == NULL) {
  383.                 printf("Cannot create new node");
  384.                 return NULL;
  385.         }
  386.  
  387.         //add data to new node
  388.         new_node->id = id;
  389.         id++;
  390.         new_node-> datagram = datagram;
  391.         new_node->next = NULL;
  392.         new_node->prev = NULL;
  393.  
  394.         //return address
  395.         return new_node;
  396. }
  397.  
  398. //function to put new element at the end of the list
  399. void InsertTail ( unsigned char *datagram ){
  400.  
  401.         struct Node *temp = head;
  402.         struct Node *new_node = ReserveMem ( datagram );
  403.  
  404.  
  405.         if (head == NULL){
  406.                 head = new_node;
  407.                 return;
  408.         }
  409.  
  410.         while (temp->next != NULL) //goto end of list
  411.                 temp = temp->next;
  412.  
  413.         //add new node to the end
  414.         temp->next = new_node;
  415.         new_node->prev = temp;
  416.         new_node->next = NULL;
  417. }
  418.  
  419. //function to print linked list
  420. void PrintList () {
  421.  
  422.         struct Node *temp = head;
  423.  
  424.         if ( temp == NULL)
  425.                 printf ("\nList empty\n");
  426.  
  427.         while (temp != NULL){
  428.                 printf ("id: %d, dtgr: %p\n", temp->id, (void *)(temp-> datagram) );
  429.                 temp = temp->next;
  430.         }
  431. }
  432.  
  433. //function to return head of the list
  434. struct Node * ReturnHead () {
  435.         return head;
  436. }
  437.  
  438. //function to delete linked list
  439. void DeleteList (){
  440.  
  441.         struct Node *temp = head;
  442.         struct Node *del = NULL;
  443.  
  444.         while ( temp != NULL ){
  445.                 del = temp;
  446.                 temp = temp -> next;
  447.                 free (del);
  448.         }
  449.         head = NULL;
  450.  
  451. }
  452.  
  453.  
  454. uint16_t
  455. icmp6_checksum (struct ip6_hdr *iphdr, struct icmp6_hdr *icmp6hdr)
  456. {
  457.   char buf[4096];
  458.   char *ptr;
  459.   int chksumlen = 0;
  460.   //int i;
  461.  
  462.   ptr = &buf[0];  // ptr points to beginning of buffer buf
  463.  
  464.   // Copy source IP address into buf (128 bits)
  465.   memcpy (ptr, &iphdr->ip6_src, sizeof (iphdr->ip6_src));
  466.   ptr += sizeof (iphdr->ip6_src);
  467.   chksumlen += sizeof (iphdr->ip6_src);
  468.  
  469.   // Copy destination IP address into buf (128 bits)
  470.   memcpy (ptr, &iphdr->ip6_dst, sizeof (iphdr->ip6_dst));
  471.   ptr += sizeof (iphdr->ip6_dst);
  472.   chksumlen += sizeof (iphdr->ip6_dst);
  473.  
  474.   // Copy Upper Layer Packet length into buf (32 bits).
  475.   // Should not be greater than 65535 (i.e., 2 bytes).
  476.   *ptr = 0; ptr++;
  477.   *ptr = 0; ptr++;
  478.   *ptr = (sizeof (struct icmp6_hdr)) / 256;
  479.   ptr++;
  480.   *ptr = (sizeof (struct icmp6_hdr)) % 256;
  481.   ptr++;
  482.   chksumlen += 4;
  483.  
  484.   // Copy zero field to buf (24 bits)
  485.   *ptr = 0; ptr++;
  486.   *ptr = 0; ptr++;
  487.   *ptr = 0; ptr++;
  488.   chksumlen += 3;
  489.  
  490.   // Copy next header field to buf (8 bits)
  491.   memcpy (ptr, &iphdr->ip6_ctlun.ip6_un1.ip6_un1_nxt, sizeof (iphdr->ip6_ctlun.ip6_un1.ip6_un1_nxt));
  492.   ptr += sizeof (iphdr->ip6_ctlun.ip6_un1.ip6_un1_nxt);
  493.   chksumlen += sizeof (iphdr->ip6_ctlun.ip6_un1.ip6_un1_nxt);
  494.  
  495.   // Copy ICMPv6 type to buf (8 bits)
  496.   memcpy (ptr, &icmp6hdr->icmp6_type, sizeof (icmp6hdr->icmp6_type));
  497.   ptr += sizeof (icmp6hdr->icmp6_type);
  498.   chksumlen += sizeof (icmp6hdr->icmp6_type);
  499.  
  500.   // Copy ICMPv6 code to buf (8 bits)
  501.   memcpy (ptr, &icmp6hdr->icmp6_code, sizeof (icmp6hdr->icmp6_code));
  502.   ptr += sizeof (icmp6hdr->icmp6_code);
  503.   chksumlen += sizeof (icmp6hdr->icmp6_code);
  504.  
  505.   // Copy ICMPv6 ID to buf (16 bits)
  506.   memcpy (ptr, &icmp6hdr->icmp6_dataun.icmp6_un_data16 [0], sizeof (icmp6hdr->icmp6_dataun.icmp6_un_data16 [0]));
  507.   ptr += sizeof (icmp6hdr->icmp6_dataun.icmp6_un_data16 [0]);
  508.   chksumlen += sizeof (icmp6hdr->icmp6_dataun.icmp6_un_data16 [0]);
  509.  
  510.   // Copy ICMPv6 sequence number to buff (16 bits)
  511.   memcpy (ptr, &icmp6hdr->icmp6_dataun.icmp6_un_data16 [1], sizeof (icmp6hdr->icmp6_dataun.icmp6_un_data16 [1]));
  512.   ptr += sizeof (icmp6hdr->icmp6_dataun.icmp6_un_data16 [1]);
  513.   chksumlen += sizeof (icmp6hdr->icmp6_dataun.icmp6_un_data16 [1]);
  514.  
  515.   // Copy ICMPv6 checksum to buf (16 bits)
  516.   // Zero, since we don't know it yet.
  517.   *ptr = 0; ptr++;
  518.   *ptr = 0; ptr++;
  519.   chksumlen += 2;
  520.  
  521.  
  522.   return checksum ((uint16_t *) buf, chksumlen);
  523. }
  524.  
  525. uint16_t checksum (uint16_t *addr, int len)
  526. {
  527.   int count = len;
  528.   register uint32_t sum = 0;
  529.   uint16_t answer = 0;
  530.  
  531.   // Sum up 2-byte values until none or only one byte left.
  532.   while (count > 1) {
  533.     sum += *(addr++);
  534.     count -= 2;
  535.   }
  536.  
  537.   // Add left-over byte, if any.
  538.   if (count > 0) {
  539.     sum += *(uint8_t *) addr;
  540.   }
  541.  
  542.   // Fold 32-bit sum into 16 bits; we lose information by doing this,
  543.   // increasing the chances of a collision.
  544.   // sum = (lower 16 bits) + (upper 16 bits shifted right 16 bits)
  545.   while (sum >> 16) {
  546.     sum = (sum & 0xffff) + (sum >> 16);
  547.   }
  548.  
  549.   // Checksum is one's compliment of sum.
  550.   answer = ~sum;
  551.  
  552.   return (answer);
  553. }
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566. program lib h
  567.  
  568.  
  569.  
  570. #ifndef PROGRAM_LIB_H_
  571. #define PROGRAM_LIB_H_
  572.  
  573. //linked list struct
  574. struct Node {
  575.         int id;
  576.         unsigned char *datagram;
  577.         struct Node *next;
  578.         struct Node *prev;
  579. };
  580.  
  581.  
  582. void * LoadIpv6 ();     //function declaration to load IPv4 library
  583. void * LoadIcmp6 ();    //function declaration to load ICMP library
  584.  
  585. void SendPacket (unsigned char *interface );    //function to send packet from linked list
  586. void LoadToList ( int *count, unsigned char *dtgr );    //function to load data to list
  587.  
  588. struct Node *ReserveMem ( unsigned char *datagram );    //function to reserve memory for new list element
  589. void InsertTail ( unsigned char *datagram );    //function to put new element at the end of the list
  590. void PrintList ();      //function to print linked list
  591. void DeleteList ();     //function to delete linked list
  592. struct Node * ReturnHead (); //function to return head of the list
  593.  
  594. #endif /* PROGRAM_LIB_H_ */
  595.  
  596.  
  597.  
  598.  
  599.