0% found this document useful (0 votes)
8 views2 pages

3

language c

Uploaded by

juden99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

3

language c

Uploaded by

juden99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/if_ether.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

#ifndef ETH_FRAME_LEN
#define ETH_FRAME_LEN 2048
#endif

/*
* zadanie01.c
*
* Created on: Dec 19, 2024
* Author: Dawid Kusik
*/

int main(void) {
printf("Uruchamiam odbieranie ramek Ethernet.\n"); /* prints */

// Utworzenie bufora dla odbieranych ramek Ethernet


unsigned char *buffer = (void*) malloc(ETH_FRAME_LEN);
if (!buffer) {
perror("Nie moge zaalokowac pamieci dla bufora");
return EXIT_FAILURE;
}

// Otwarcie gniazda pozwalającego na odbior wszystkich ramek Ethernet


int iEthSockHandl = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (iEthSockHandl < 0) {
perror("Problem z otwarciem gniazda");
free(buffer);
return EXIT_FAILURE;
}

// Zmienna do przechowywania rozmiaru odebranych danych


int iDataLen = 0;
unsigned short typ_ramki;

// Petla nieskonczona do odbierania ramek Ethernet


while (1) {
// Odebranie ramki z utworzonego wczesniej gniazda i zapisanie jej do
bufora
iDataLen = recvfrom(iEthSockHandl, buffer, ETH_FRAME_LEN, 0, NULL, NULL);
if (iDataLen == -1) {
perror("Nie moge odebrac ramki");
continue;
}

memcpy(&typ_ramki, buffer + 12, 2);


typ_ramki = ntohs(typ_ramki);
printf("Typ ramki: 0x%.4x \n", typ_ramki);

// Sprawdzanie, czy ramka zawiera pakiet IPv4


if (typ_ramki == 0x0800) {
struct iphdr *ip_header = (struct iphdr *)(buffer + 14);
unsigned int ip_header_len = ip_header->ihl * 4;

printf("----- Naglowek IPv4 -----\n");


printf("Adres zrodlowy: %s\n", inet_ntoa(*(struct in_addr*)&ip_header-
>saddr));
printf("Adres docelowy: %s\n", inet_ntoa(*(struct in_addr*)&ip_header-
>daddr));
printf("Protokol: 0x%x\n", ip_header->protocol);

// Sprawdzanie, czy protokol to TCP


if (ip_header->protocol == IPPROTO_TCP) {
struct tcphdr *tcp_header = (struct tcphdr *)(buffer + 14 +
ip_header_len);
unsigned int tcp_header_len = tcp_header->doff * 4;

printf("----- Naglowek TCP -----\n");


printf("Port zrodlowy: 0x%x (%d)\n", ntohs(tcp_header->source),
ntohs(tcp_header->source));
printf("Port docelowy: 0x%x (%d)\n", ntohs(tcp_header->dest),
ntohs(tcp_header->dest));

// Filtrowanie na podstawie portu docelowego (443)


if (ntohs(tcp_header->dest) == 443) {
printf("----- Odebrano pakiet TCP do portu 443 -----\n");
printf("Numer sekwencji: %u\n", ntohl(tcp_header->seq));
printf("Numer potwierdzenia: %u\n", ntohl(tcp_header-
>ack_seq));

// Obliczenie dlugosci danych TCP


int tcp_data_len = iDataLen - (14 + ip_header_len +
tcp_header_len);

// Dane TCP
if (tcp_data_len > 0) {
printf("----- Dane TCP (%d bajtow) -----\n", tcp_data_len);
unsigned char *tcp_data = (unsigned char *)(buffer + 14 +
ip_header_len + tcp_header_len);
for (int i = 0; i < tcp_data_len; i++) {
printf("%.2x ", tcp_data[i]);
if ((i + 1) % 16 == 0)
printf("\n");
}
printf("\n");
}
}
}
}
}

free(buffer);
close(iEthSockHandl);
return EXIT_SUCCESS;
}

You might also like