Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.65 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <pcap.h>
  4.  
  5. void numbersAndOpen(FILE *TCP, FILE *UDP, FILE *ETHT, FILE *IP, int *a, int *b, int *c, int *d)
  6. {
  7.     char ch = 0;
  8.    
  9.     TCP = fopen("tcpports.txt", "r");
  10.     UDP = fopen("udpports.txt", "r");
  11.     ETHT = fopen("ethertype.txt", "r");
  12.     IP = fopen("IPheaderprotocols.txt", "r");
  13.  
  14.     while (ch != EOF)
  15.     {
  16.         ch = getc(TCP);
  17.         if (ch == '\n')a++;
  18.     }
  19.     while (ch != EOF)
  20.     {
  21.         ch = getc(UDP);
  22.         if (ch == '\n')b++;
  23.     }
  24.     while (ch != EOF)
  25.     {
  26.         ch = getc(ETHT);
  27.         if (ch == '\n')c++;
  28.     }
  29.     while (ch != EOF)
  30.     {
  31.         ch = getc(IP);
  32.         if (ch == '\n')d++;
  33.     }
  34.     rewind(TCP);
  35.     rewind(UDP);
  36.     rewind(ETHT);
  37.     rewind(IP);
  38.  
  39.  
  40. }
  41.  
  42. void TextFiles(FILE *TCP, FILE *UDP, FILE *ETHT, FILE *IP, char ***tcp, char ***udp, char ***etht, char ***ip, int a, int b, int c, int d)
  43. {
  44.     char ch=0;
  45.     int i,j,t=0;
  46.  
  47.     /*TCP = fopen("tcpports.txt", "r");
  48.     UDP = fopen("udpports.txt", "r");
  49.     ETHT = fopen("ethertype.txt", "r");
  50.     IP = fopen("IPheaderprotocols.txt","r");*/
  51.  
  52.     for (i = 0; i < a; i++)
  53.     {
  54.         *tcp[i] = (char *)malloc(2 * sizeof(char));
  55.     }
  56.     for (i = 0; i < b; i++)
  57.     {
  58.         *udp[i] = (char *)malloc(2 * sizeof(char));
  59.     }
  60.     for (i = 0; i < c; i++)
  61.     {
  62.         *etht[i] = (char *)malloc(2 * sizeof(char));
  63.     }
  64.     for (i = 0; i < d; i++)
  65.     {
  66.         *ip[i] = (char *)malloc(2 * sizeof(char));
  67.     }
  68.  
  69.     for (i = 0; i < a; i++)
  70.     {
  71.         for (j=0;j<2;j++)
  72.         {
  73.             tcp[i][j] = (char *)malloc(13 * sizeof(char));
  74.         }
  75.     }
  76.     for (i = 0; i < b; i++)
  77.     {
  78.         for (j = 0; j < 2; j++)
  79.         {
  80.             *udp[i] = (char *)malloc(12 * sizeof(char));
  81.         }
  82.     }
  83.     for (i = 0; i < c; i++)
  84.     {
  85.         for (j = 0; j < 2; j++)
  86.         {
  87.             *etht[i] = (char *)malloc(36 * sizeof(char));
  88.         }
  89.     }
  90.     for (i = 0; i < d; i++)
  91.     {
  92.         for (j = 0; j < 2; j++)
  93.         {
  94.             *ip[i] = (char *)malloc(6 * sizeof(char));
  95.         }
  96.     }
  97.     j = 0;
  98.     i = 0;
  99.     while((ch=getc(TCP))!=EOF)
  100.     {
  101.         if (ch == '\n') {
  102.             i++;
  103.             t = 0;
  104.             j = 0;
  105.             continue;
  106.         }
  107.  
  108.         if (ch == '\t')
  109.         {
  110.             t = 1;
  111.             continue;
  112.         }
  113.  
  114.         if (t == 0)
  115.         {
  116.             tcp[i][0][j] = ch;
  117.         }
  118.        
  119.         if (t == 1)
  120.         {
  121.             tcp[i][1][j] = ch;
  122.         }      
  123.         j++;
  124.     }
  125.     j = 0;
  126.     i = 0;
  127.     while ((ch=getc(UDP)) != EOF)
  128.     {
  129.         if (ch == '\n') {
  130.             i++;
  131.             t = 0;
  132.             j = 0;
  133.             continue;
  134.         }
  135.  
  136.         if (ch == '\t')
  137.         {
  138.             t = 1;
  139.             continue;
  140.         }
  141.  
  142.         if (t == 0)
  143.         {
  144.             udp[i][0][j] = ch;
  145.         }
  146.  
  147.         if (t == 1)
  148.         {
  149.             udp[i][1][j] = ch;
  150.         }
  151.         j++;
  152.     }
  153.     j = 0;
  154.     i = 0;
  155.     while ((ch=getc(ETHT)) != EOF)
  156.     {
  157.         if (ch == '\n') {
  158.             i++;
  159.             t = 0;
  160.             j = 0;
  161.             continue;
  162.         }
  163.  
  164.         if (ch == '\t')
  165.         {
  166.             t = 1;
  167.             continue;
  168.         }
  169.  
  170.         if (t == 0)
  171.         {
  172.             etht[i][0][j] = ch;
  173.         }
  174.  
  175.         if (t == 1)
  176.         {
  177.             etht[i][1][j] = ch;
  178.         }
  179.         j++;
  180.     }
  181.     j = 0;
  182.     i = 0;
  183.     while ((ch=getc(IP)) != EOF)
  184.     {
  185.         if (ch == '\n') {
  186.             i++;
  187.             t = 0;
  188.             j = 0;
  189.             continue;
  190.         }
  191.  
  192.         if (ch == '\t')
  193.         {
  194.             t = 1;
  195.             continue;
  196.         }
  197.  
  198.         if (t == 0)
  199.         {
  200.             ip[i][0][j] = ch;
  201.         }
  202.  
  203.         if (t == 1)
  204.         {
  205.             ip[i][1][j] = ch;
  206.         }
  207.         j++;
  208.     }
  209.  
  210. }
  211.  
  212. int main()
  213. {
  214.     FILE *TCP=NULL, *UDP=NULL, *ETHT=NULL, *IP=NULL;
  215.     char ***tcp, ***udp, ***etht, ***ip;
  216.     int returnValue,i,a=0,b=0,c=0,d=0;
  217.     u_int packetCount = 1;
  218.     char errbuff[255];
  219.     pcap_t *pcap;
  220.  
  221.     numbersAndOpen(TCP, UDP, ETHT, IP, &a, &b, &c, &d);
  222.  
  223.     **tcp = (char *)malloc(a * sizeof(char));
  224.     **udp = (char *)malloc(b * sizeof(char));
  225.     **etht = (char *)malloc(c * sizeof(char));
  226.     **ip = (char *)malloc(d * sizeof(char));
  227.    
  228.     TextFiles(TCP,UDP,ETHT,IP,tcp,udp,etht,ip,a,b,c,d);
  229.  
  230.     if ((pcap = pcap_open_offline("trace-23.pcap", errbuff)) == NULL)
  231.     {
  232.         printf("%s", errbuff);
  233.         getchar();
  234.         exit(-1);
  235.     }
  236.     struct pcap_pkthdr *header;
  237.     const u_char *data;
  238.  
  239.     while (returnValue = pcap_next_ex(pcap, &header, &data) >= 0)
  240.     {
  241.         printf("%d.\n",packetCount++);
  242.  
  243.         printf("Dlzka ramca podla pcap API: %dB\n", header->caplen);
  244.         printf("Dlzka ramca prenasaneho po mediu: ");
  245.  
  246.         if (header->len <= 60) printf("64");
  247.         else printf("%d",header->len + 4);
  248.         printf("B\n");
  249.  
  250.         printf("Typ ramca: ");
  251.         if (data[12] > 0x06) printf("Ethernet II\n");
  252.         else {
  253.             printf("IEEE 802.3 ");
  254.  
  255.             if (data[14] == 0xff)printf("- RAW\n");
  256.             else if (data[14] == 0xaa)printf("- LLC - SNAP\n");
  257.             else printf("- LLC\n");
  258.         }
  259.         printf("Zdrojova MAC adresa: ");
  260.         for (i = 6; i < 12; i++) {
  261.             printf("%.2x", data[i]);
  262.             if (i != 11)printf(" : ");
  263.         }
  264.         printf("\n");
  265.         printf("Cielova MAC adresa: ");
  266.         for (i = 0; i < 6; i++) {
  267.             printf("%.2x",data[i]);
  268.             if (i != 5)printf(" : ");
  269.         }
  270.        
  271.        
  272.         //printf("\n\n");
  273.         for (u_int i = 0; (i < header->caplen); i++)
  274.         {
  275.             if ((i % 8) == 0)printf(" ");
  276.             if ((i % 16) == 0) printf("\n");
  277.  
  278.  
  279.             printf("%.2x ", data[i]);
  280.         }
  281.         printf("\n\n");
  282.     }
  283.  
  284.     getchar();
  285.     return 0;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement