Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdio.h>
- #include <pcap.h>
- void numbersAndOpen(FILE *TCP, FILE *UDP, FILE *ETHT, FILE *IP, int *a, int *b, int *c, int *d)
- {
- char ch = 0;
- TCP = fopen("tcpports.txt", "r");
- UDP = fopen("udpports.txt", "r");
- ETHT = fopen("ethertype.txt", "r");
- IP = fopen("IPheaderprotocols.txt", "r");
- while (ch != EOF)
- {
- ch = getc(TCP);
- if (ch == '\n')a++;
- }
- while (ch != EOF)
- {
- ch = getc(UDP);
- if (ch == '\n')b++;
- }
- while (ch != EOF)
- {
- ch = getc(ETHT);
- if (ch == '\n')c++;
- }
- while (ch != EOF)
- {
- ch = getc(IP);
- if (ch == '\n')d++;
- }
- rewind(TCP);
- rewind(UDP);
- rewind(ETHT);
- rewind(IP);
- }
- 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)
- {
- char ch=0;
- int i,j,t=0;
- /*TCP = fopen("tcpports.txt", "r");
- UDP = fopen("udpports.txt", "r");
- ETHT = fopen("ethertype.txt", "r");
- IP = fopen("IPheaderprotocols.txt","r");*/
- for (i = 0; i < a; i++)
- {
- *tcp[i] = (char *)malloc(2 * sizeof(char));
- }
- for (i = 0; i < b; i++)
- {
- *udp[i] = (char *)malloc(2 * sizeof(char));
- }
- for (i = 0; i < c; i++)
- {
- *etht[i] = (char *)malloc(2 * sizeof(char));
- }
- for (i = 0; i < d; i++)
- {
- *ip[i] = (char *)malloc(2 * sizeof(char));
- }
- for (i = 0; i < a; i++)
- {
- for (j=0;j<2;j++)
- {
- tcp[i][j] = (char *)malloc(13 * sizeof(char));
- }
- }
- for (i = 0; i < b; i++)
- {
- for (j = 0; j < 2; j++)
- {
- *udp[i] = (char *)malloc(12 * sizeof(char));
- }
- }
- for (i = 0; i < c; i++)
- {
- for (j = 0; j < 2; j++)
- {
- *etht[i] = (char *)malloc(36 * sizeof(char));
- }
- }
- for (i = 0; i < d; i++)
- {
- for (j = 0; j < 2; j++)
- {
- *ip[i] = (char *)malloc(6 * sizeof(char));
- }
- }
- j = 0;
- i = 0;
- while((ch=getc(TCP))!=EOF)
- {
- if (ch == '\n') {
- i++;
- t = 0;
- j = 0;
- continue;
- }
- if (ch == '\t')
- {
- t = 1;
- continue;
- }
- if (t == 0)
- {
- tcp[i][0][j] = ch;
- }
- if (t == 1)
- {
- tcp[i][1][j] = ch;
- }
- j++;
- }
- j = 0;
- i = 0;
- while ((ch=getc(UDP)) != EOF)
- {
- if (ch == '\n') {
- i++;
- t = 0;
- j = 0;
- continue;
- }
- if (ch == '\t')
- {
- t = 1;
- continue;
- }
- if (t == 0)
- {
- udp[i][0][j] = ch;
- }
- if (t == 1)
- {
- udp[i][1][j] = ch;
- }
- j++;
- }
- j = 0;
- i = 0;
- while ((ch=getc(ETHT)) != EOF)
- {
- if (ch == '\n') {
- i++;
- t = 0;
- j = 0;
- continue;
- }
- if (ch == '\t')
- {
- t = 1;
- continue;
- }
- if (t == 0)
- {
- etht[i][0][j] = ch;
- }
- if (t == 1)
- {
- etht[i][1][j] = ch;
- }
- j++;
- }
- j = 0;
- i = 0;
- while ((ch=getc(IP)) != EOF)
- {
- if (ch == '\n') {
- i++;
- t = 0;
- j = 0;
- continue;
- }
- if (ch == '\t')
- {
- t = 1;
- continue;
- }
- if (t == 0)
- {
- ip[i][0][j] = ch;
- }
- if (t == 1)
- {
- ip[i][1][j] = ch;
- }
- j++;
- }
- }
- int main()
- {
- FILE *TCP=NULL, *UDP=NULL, *ETHT=NULL, *IP=NULL;
- char ***tcp, ***udp, ***etht, ***ip;
- int returnValue,i,a=0,b=0,c=0,d=0;
- u_int packetCount = 1;
- char errbuff[255];
- pcap_t *pcap;
- numbersAndOpen(TCP, UDP, ETHT, IP, &a, &b, &c, &d);
- **tcp = (char *)malloc(a * sizeof(char));
- **udp = (char *)malloc(b * sizeof(char));
- **etht = (char *)malloc(c * sizeof(char));
- **ip = (char *)malloc(d * sizeof(char));
- TextFiles(TCP,UDP,ETHT,IP,tcp,udp,etht,ip,a,b,c,d);
- if ((pcap = pcap_open_offline("trace-23.pcap", errbuff)) == NULL)
- {
- printf("%s", errbuff);
- getchar();
- exit(-1);
- }
- struct pcap_pkthdr *header;
- const u_char *data;
- while (returnValue = pcap_next_ex(pcap, &header, &data) >= 0)
- {
- printf("%d.\n",packetCount++);
- printf("Dlzka ramca podla pcap API: %dB\n", header->caplen);
- printf("Dlzka ramca prenasaneho po mediu: ");
- if (header->len <= 60) printf("64");
- else printf("%d",header->len + 4);
- printf("B\n");
- printf("Typ ramca: ");
- if (data[12] > 0x06) printf("Ethernet II\n");
- else {
- printf("IEEE 802.3 ");
- if (data[14] == 0xff)printf("- RAW\n");
- else if (data[14] == 0xaa)printf("- LLC - SNAP\n");
- else printf("- LLC\n");
- }
- printf("Zdrojova MAC adresa: ");
- for (i = 6; i < 12; i++) {
- printf("%.2x", data[i]);
- if (i != 11)printf(" : ");
- }
- printf("\n");
- printf("Cielova MAC adresa: ");
- for (i = 0; i < 6; i++) {
- printf("%.2x",data[i]);
- if (i != 5)printf(" : ");
- }
- //printf("\n\n");
- for (u_int i = 0; (i < header->caplen); i++)
- {
- if ((i % 8) == 0)printf(" ");
- if ((i % 16) == 0) printf("\n");
- printf("%.2x ", data[i]);
- }
- printf("\n\n");
- }
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement