From c5fcf7179a83ef65c86c6a4a390029149e518649 Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Tue, 5 Oct 2021 21:49:54 +0200 Subject: Duzy commit ze smieciami --- Semestr 4/sieci/cwiczenia1/c1.pdf | Bin 164919 -> 0 bytes Semestr 4/sieci/cwiczenia1/cw1.pdf | Bin 1647345 -> 0 bytes Semestr 4/sieci/cwiczenia1/out.pdf | Bin 1613073 -> 0 bytes Semestr 4/sieci/cwiczenia1/rozw.pdf | Bin 26846 -> 0 bytes Semestr 4/sieci/cwiczenia2/c2.pdf | Bin 179367 -> 0 bytes Semestr 4/sieci/pracownia1/makefile | 19 -- Semestr 4/sieci/pracownia1/p1.pdf | Bin 79285 -> 0 bytes Semestr 4/sieci/pracownia1/traceroute.c | 240 --------------------- .../sieci/pracownia2/franciszek_malinka.tar.xz | Bin 6012 -> 0 bytes .../sieci/pracownia2/franciszek_malinka/config.h | 15 -- .../pracownia2/franciszek_malinka/dist_vector.c | 114 ---------- .../pracownia2/franciszek_malinka/dist_vector.h | 36 ---- .../pracownia2/franciszek_malinka/linked_list.c | 79 ------- .../pracownia2/franciszek_malinka/linked_list.h | 42 ---- .../sieci/pracownia2/franciszek_malinka/makefile | 34 --- .../pracownia2/franciszek_malinka/network_addr.c | 65 ------ .../pracownia2/franciszek_malinka/network_addr.h | 28 --- .../sieci/pracownia2/franciszek_malinka/router.c | 60 ------ .../sieci/pracownia2/franciszek_malinka/test.c | 52 ----- .../sieci/pracownia2/franciszek_malinka/utils.c | 208 ------------------ .../sieci/pracownia2/franciszek_malinka/utils.h | 52 ----- Semestr 4/sieci/pracownia2/p2.pdf | Bin 98190 -> 0 bytes Semestr 4/sieci/pracownia2/router/config.h | 15 -- Semestr 4/sieci/pracownia2/router/dist_vector.c | 114 ---------- Semestr 4/sieci/pracownia2/router/dist_vector.h | 36 ---- Semestr 4/sieci/pracownia2/router/linked_list.c | 79 ------- Semestr 4/sieci/pracownia2/router/linked_list.h | 42 ---- Semestr 4/sieci/pracownia2/router/makefile | 34 --- Semestr 4/sieci/pracownia2/router/network_addr.c | 65 ------ Semestr 4/sieci/pracownia2/router/network_addr.h | 28 --- Semestr 4/sieci/pracownia2/router/router.c | 60 ------ Semestr 4/sieci/pracownia2/router/test.c | 52 ----- Semestr 4/sieci/pracownia2/router/utils.c | 208 ------------------ Semestr 4/sieci/pracownia2/router/utils.h | 52 ----- Semestr 4/sieci/pracownia2/udp_client.c | 41 ---- Semestr 4/sieci/pracownia2/udp_server.c | 59 ----- Semestr 4/sieci/pracownia3/config.h | 10 - Semestr 4/sieci/pracownia3/p3.pdf | Bin 95110 -> 0 bytes Semestr 4/sieci/pracownia3/transport.c | 149 ------------- Semestr 4/sieci/pracownia3/utils.c | 58 ----- Semestr 4/sieci/pracownia3/utils.h | 18 -- Semestr 4/sieci/pracownia3/window.c | 42 ---- Semestr 4/sieci/pracownia3/window.h | 24 --- Semestr 4/sieci/pracownia4/p4.pdf | Bin 151294 -> 0 bytes Semestr 4/sieci/warsztaty1/w1.pdf | Bin 93952 -> 0 bytes Semestr 4/sieci/warsztaty2/w2.pdf | Bin 190886 -> 0 bytes Semestr 4/sieci/warsztaty3/w3.pdf | Bin 112755 -> 0 bytes Semestr 4/sieci/warsztaty4/w4.pdf | Bin 210974 -> 0 bytes Semestr 4/sieci/warsztaty5/w5.pdf | Bin 121273 -> 0 bytes Semestr 4/sieci/warsztaty6/w6.pdf | Bin 75165 -> 0 bytes Semestr 4/sieci/wyklady/lec1.pdf | Bin 8280250 -> 0 bytes Semestr 4/sieci/wyklady/lec2.pdf | Bin 5058364 -> 0 bytes Semestr 4/sieci/wyklady/lec3.pdf | Bin 8701267 -> 0 bytes Semestr 4/sieci/wyklady/lec4.pdf | Bin 5658130 -> 0 bytes Semestr 4/sieci/wyklady/lec5.pdf | Bin 4572488 -> 0 bytes Semestr 4/sieci/wyklady/lec6.pdf | Bin 3339096 -> 0 bytes Semestr 4/sieci/wyklady/lec7.pdf | Bin 1825393 -> 0 bytes Semestr 4/sieci/wyklady/lec8.pdf | Bin 2144653 -> 0 bytes 58 files changed, 2230 deletions(-) delete mode 100644 Semestr 4/sieci/cwiczenia1/c1.pdf delete mode 100644 Semestr 4/sieci/cwiczenia1/cw1.pdf delete mode 100644 Semestr 4/sieci/cwiczenia1/out.pdf delete mode 100644 Semestr 4/sieci/cwiczenia1/rozw.pdf delete mode 100644 Semestr 4/sieci/cwiczenia2/c2.pdf delete mode 100644 Semestr 4/sieci/pracownia1/makefile delete mode 100644 Semestr 4/sieci/pracownia1/p1.pdf delete mode 100644 Semestr 4/sieci/pracownia1/traceroute.c delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka.tar.xz delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka/config.h delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka/dist_vector.c delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka/dist_vector.h delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka/linked_list.c delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka/linked_list.h delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka/makefile delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka/network_addr.c delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka/network_addr.h delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka/router.c delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka/test.c delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka/utils.c delete mode 100644 Semestr 4/sieci/pracownia2/franciszek_malinka/utils.h delete mode 100644 Semestr 4/sieci/pracownia2/p2.pdf delete mode 100644 Semestr 4/sieci/pracownia2/router/config.h delete mode 100644 Semestr 4/sieci/pracownia2/router/dist_vector.c delete mode 100644 Semestr 4/sieci/pracownia2/router/dist_vector.h delete mode 100644 Semestr 4/sieci/pracownia2/router/linked_list.c delete mode 100644 Semestr 4/sieci/pracownia2/router/linked_list.h delete mode 100644 Semestr 4/sieci/pracownia2/router/makefile delete mode 100644 Semestr 4/sieci/pracownia2/router/network_addr.c delete mode 100644 Semestr 4/sieci/pracownia2/router/network_addr.h delete mode 100644 Semestr 4/sieci/pracownia2/router/router.c delete mode 100644 Semestr 4/sieci/pracownia2/router/test.c delete mode 100644 Semestr 4/sieci/pracownia2/router/utils.c delete mode 100644 Semestr 4/sieci/pracownia2/router/utils.h delete mode 100755 Semestr 4/sieci/pracownia2/udp_client.c delete mode 100755 Semestr 4/sieci/pracownia2/udp_server.c delete mode 100644 Semestr 4/sieci/pracownia3/config.h delete mode 100644 Semestr 4/sieci/pracownia3/p3.pdf delete mode 100644 Semestr 4/sieci/pracownia3/transport.c delete mode 100644 Semestr 4/sieci/pracownia3/utils.c delete mode 100644 Semestr 4/sieci/pracownia3/utils.h delete mode 100644 Semestr 4/sieci/pracownia3/window.c delete mode 100644 Semestr 4/sieci/pracownia3/window.h delete mode 100644 Semestr 4/sieci/pracownia4/p4.pdf delete mode 100644 Semestr 4/sieci/warsztaty1/w1.pdf delete mode 100644 Semestr 4/sieci/warsztaty2/w2.pdf delete mode 100644 Semestr 4/sieci/warsztaty3/w3.pdf delete mode 100644 Semestr 4/sieci/warsztaty4/w4.pdf delete mode 100644 Semestr 4/sieci/warsztaty5/w5.pdf delete mode 100644 Semestr 4/sieci/warsztaty6/w6.pdf delete mode 100644 Semestr 4/sieci/wyklady/lec1.pdf delete mode 100644 Semestr 4/sieci/wyklady/lec2.pdf delete mode 100644 Semestr 4/sieci/wyklady/lec3.pdf delete mode 100644 Semestr 4/sieci/wyklady/lec4.pdf delete mode 100644 Semestr 4/sieci/wyklady/lec5.pdf delete mode 100644 Semestr 4/sieci/wyklady/lec6.pdf delete mode 100644 Semestr 4/sieci/wyklady/lec7.pdf delete mode 100644 Semestr 4/sieci/wyklady/lec8.pdf (limited to 'Semestr 4/sieci') diff --git a/Semestr 4/sieci/cwiczenia1/c1.pdf b/Semestr 4/sieci/cwiczenia1/c1.pdf deleted file mode 100644 index ace2fa7..0000000 Binary files a/Semestr 4/sieci/cwiczenia1/c1.pdf and /dev/null differ diff --git a/Semestr 4/sieci/cwiczenia1/cw1.pdf b/Semestr 4/sieci/cwiczenia1/cw1.pdf deleted file mode 100644 index d217892..0000000 Binary files a/Semestr 4/sieci/cwiczenia1/cw1.pdf and /dev/null differ diff --git a/Semestr 4/sieci/cwiczenia1/out.pdf b/Semestr 4/sieci/cwiczenia1/out.pdf deleted file mode 100644 index ea7781d..0000000 Binary files a/Semestr 4/sieci/cwiczenia1/out.pdf and /dev/null differ diff --git a/Semestr 4/sieci/cwiczenia1/rozw.pdf b/Semestr 4/sieci/cwiczenia1/rozw.pdf deleted file mode 100644 index f2d1a62..0000000 Binary files a/Semestr 4/sieci/cwiczenia1/rozw.pdf and /dev/null differ diff --git a/Semestr 4/sieci/cwiczenia2/c2.pdf b/Semestr 4/sieci/cwiczenia2/c2.pdf deleted file mode 100644 index 3216f6f..0000000 Binary files a/Semestr 4/sieci/cwiczenia2/c2.pdf and /dev/null differ diff --git a/Semestr 4/sieci/pracownia1/makefile b/Semestr 4/sieci/pracownia1/makefile deleted file mode 100644 index 3c48d74..0000000 --- a/Semestr 4/sieci/pracownia1/makefile +++ /dev/null @@ -1,19 +0,0 @@ -CC=gcc -CFLAGS=-std=gnu17 -O2 -Wall -Wextra -TARGET=traceroute - -all: $(TARGET) - -$(TARGET): $(TARGET).o - $(CC) -o $(TARGET) $(TARGET).o - - -$(TARGET).o: $(TARGET).c - $(CC) $(CFLAGS) -c $(TARGET).c - -distclean: - $(RM) $(TARGET) - $(RM) $(TARGET).o - -clean: - $(RM) $(TARGET).o diff --git a/Semestr 4/sieci/pracownia1/p1.pdf b/Semestr 4/sieci/pracownia1/p1.pdf deleted file mode 100644 index 4b06555..0000000 Binary files a/Semestr 4/sieci/pracownia1/p1.pdf and /dev/null differ diff --git a/Semestr 4/sieci/pracownia1/traceroute.c b/Semestr 4/sieci/pracownia1/traceroute.c deleted file mode 100644 index 0e51c14..0000000 --- a/Semestr 4/sieci/pracownia1/traceroute.c +++ /dev/null @@ -1,240 +0,0 @@ -/* Pracownia programistyczna nr 1 - traceroute - * ImiÄ™ i nazwisko: Franciszek Malinka - * Nr indeksu: 316093 - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define MAX_TTL 30 -#define MESSAGES_PER_TTL 3 -#define NO_MESSAGES -1 -#define TOO_FEW_MESSAGES -2137 - -void print_as_bytes (unsigned char* buff, ssize_t length) -{ - for (ssize_t i = 0; i < length; i++, buff++) - printf ("%.2x ", *buff); -} - -void debug_received(uint8_t *buffer, struct sockaddr_in *sender, int packet_len) { - char ip_str[20]; - inet_ntop(AF_INET, &(sender->sin_addr), ip_str, sizeof(ip_str)); - - printf("IP packet with ICMP content from: %s\n", ip_str); - struct ip* ip_header = (struct ip*) buffer; - ssize_t ip_header_len = 4 * ip_header->ip_hl; - - printf ("IP header: "); - print_as_bytes (buffer, ip_header_len); - printf("\n"); - - printf ("IP data: "); - print_as_bytes (buffer + ip_header_len, packet_len - ip_header_len); - printf("\n\n"); -} - -struct sockaddr_in get_sockaddr_from_ip(char *ip) { - struct sockaddr_in sock; - bzero(&sock, sizeof(sock)); - sock.sin_family = AF_INET; - if (!inet_pton(AF_INET, ip, &sock.sin_addr)) { - fprintf(stderr, "Given ip is invalid: %s\n", ip); - exit(EXIT_FAILURE); - } - return sock; -} - -int create_raw_icmp_socket() { - int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); - if (sockfd < 0) { - fprintf(stderr, "socket error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - return sockfd; -} - -uint16_t compute_icmp_checksum(const void *buff, int length) -{ - uint32_t sum; - const uint16_t* ptr = buff; - assert (length % 2 == 0); - for (sum = 0; length > 0; length -= 2) - sum += *ptr++; - sum = (sum >> 16) + (sum & 0xffff); - return (uint16_t)(~(sum + (sum >> 16))); -} - -struct icmp create_icmp_header(uint16_t seq) { - struct icmp header; - header.icmp_type = ICMP_ECHO; - header.icmp_code = 0; - header.icmp_id = (uint16_t)getpid(); - header.icmp_seq = seq; - header.icmp_cksum = 0; - header.icmp_cksum = compute_icmp_checksum( - (uint16_t*)&header, sizeof(header)); - - return header; -} - -void send_icmp_packet(int sockfd, struct sockaddr_in *destination, int ttl) { - struct icmp header = create_icmp_header(ttl); - setsockopt(sockfd, IPPROTO_IP, IP_TTL, &ttl, sizeof(int)); - - ssize_t bytes_sent = sendto( - sockfd, - &header, - sizeof(header), - 0, - (struct sockaddr*)destination, - sizeof(*destination) - ); - if (bytes_sent == -1) { - fprintf(stderr, "Error while sending ICMP packet: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } -} - -/* Return ip address of the sender of recceived package */ -ssize_t recv_packet(int sockfd, struct sockaddr_in *sender, uint8_t *buffer) { - socklen_t sender_len = sizeof(*sender); - ssize_t packet_len = recvfrom(sockfd, buffer, IP_MAXPACKET, 0, - (struct sockaddr*)sender, &sender_len); - - if (packet_len == -1) { - fprintf(stderr, "Error while recieving a packet: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - return packet_len; -} - -void send_icmp_requests(int sockfd, struct sockaddr_in *destination, int ttl, int tries) { - for (int i = 0; i < tries; i++) { - send_icmp_packet(sockfd, destination, ttl); - } -} - -int try_select(int sockfd, struct timeval *tv) { - fd_set descriptors; - FD_ZERO(&descriptors); - FD_SET(sockfd, &descriptors); - return select(sockfd+1, &descriptors, NULL, NULL, tv); -} - -void pretty_print_router(int ttl, struct in_addr *senders, float mean_wait_time, int messages_received) { - char ip_str[20]; - printf("%d.\t", ttl); - if (messages_received == 0) { - printf("*\n"); - } - else { - for (int i = 0; i < messages_received; i++) { - bool already_printed = false; - for (int j = 0; j < i; j++) { - if (senders[i].s_addr == senders[j].s_addr) { - already_printed = true; - break; - } - } - if (already_printed) continue; - inet_ntop(AF_INET, senders + i, ip_str, sizeof(ip_str)); - printf("%-15s ", ip_str); - } - if (messages_received < MESSAGES_PER_TTL) printf("\t???\n"); - else printf("\t%.2fms\n", mean_wait_time); - } -} - -// This function decapsulates important data from the IMCP replies -void get_important_data(uint8_t *buffer, uint8_t *code, uint16_t *id, uint16_t *seq) { - struct ip* ip_header = (struct ip*) buffer; - ssize_t offset = 4 * ip_header->ip_hl; - *code = ((struct icmp *)(buffer + offset))->icmp_type; - if (*code != ICMP_ECHOREPLY && *code != ICMP_TIMXCEED) { - return; - } else if (*code == ICMP_TIMXCEED) { - offset += ICMP_MINLEN; - offset += 4 * ((struct ip *)(buffer + offset))->ip_hl; - *seq = ((struct icmp *)(buffer + offset))->icmp_seq; - *id = ((struct icmp *)(buffer + offset))->icmp_id; - } else { - *seq = ((struct icmp *)(buffer + offset))->icmp_seq; - *id = ((struct icmp *)(buffer + offset))->icmp_id; - } -} - -// timeout in milliseconds -float get_replies(int sockfd, long timeout, int ttl, int *messages_received, struct in_addr *senders) { - struct sockaddr_in sender; - uint8_t buffer[IP_MAXPACKET]; - int ready; - long mean_wait_time = 0; - struct timeval tv; tv.tv_sec = timeout / 1000; tv.tv_usec = 0; - *messages_received = 0; - - while ((ready = try_select(sockfd, &tv))) { - if (ready < 0) { - fprintf(stderr, "Select error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - - recv_packet(sockfd, &sender, buffer); - uint8_t icmp_code = 0; - uint16_t seq = 0, id = 0; - - get_important_data(buffer, &icmp_code, &id, &seq); - if (seq == ttl) { - senders[(*messages_received)++] = sender.sin_addr; - mean_wait_time += timeout * 1000 - tv.tv_usec; - - if (*messages_received == MESSAGES_PER_TTL) break; - } - } - // changing from microseconds to miliseconds - return (float)mean_wait_time / 3.0 / 1000.0; -} - -// Core of the program. -void traceroute(struct sockaddr_in *destination) { - int sockfd = create_raw_icmp_socket(), messages_received = 0; - struct in_addr senders[MESSAGES_PER_TTL]; - - for (int ttl = 1; ttl <= MAX_TTL; ++ttl) { - send_icmp_requests(sockfd, destination, ttl, MESSAGES_PER_TTL); - float mean_wait_time = get_replies(sockfd, 1000, ttl, &messages_received, senders); - pretty_print_router(ttl, senders, mean_wait_time, messages_received); - - // what if our packet branched and we got to the destination faster by some path? - // we gotta make a loop, not an if - for (int i = 0; i < messages_received; i++) { - if (senders[i].s_addr == destination->sin_addr.s_addr) { - return; - } - } - } -} - -int main(int argc, char * argv[]) { - if (argc < 2) { - fprintf(stderr, "Usage:\n\t%s [host ip]\n", argv[0]); - return 1; - } - - struct sockaddr_in destination = get_sockaddr_from_ip(argv[1]); - fprintf(stdout, "traceroute to %s, %d hops max\n", argv[1], MAX_TTL); - traceroute(&destination); - - return 0; -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka.tar.xz b/Semestr 4/sieci/pracownia2/franciszek_malinka.tar.xz deleted file mode 100644 index ac3b1f7..0000000 Binary files a/Semestr 4/sieci/pracownia2/franciszek_malinka.tar.xz and /dev/null differ diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka/config.h b/Semestr 4/sieci/pracownia2/franciszek_malinka/config.h deleted file mode 100644 index ee4eac0..0000000 --- a/Semestr 4/sieci/pracownia2/franciszek_malinka/config.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef CONFIG_H -#define CONFIG_H - -#define SERVER_PORT 54321 -#define TURN_LEN_S 20 - -#define INFINITY_DIST 16 -#define REACHABILITY_WAIT_TIME 3 - -#define DV_DATAGRAM_LEN 9 - -#define TURN_LEN_MS (1000 * TURN_LEN_S) -#define TURN_LEN_US (1000000 * TURN_LEN_S) - -#endif diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka/dist_vector.c b/Semestr 4/sieci/pracownia2/franciszek_malinka/dist_vector.c deleted file mode 100644 index 74ae82e..0000000 --- a/Semestr 4/sieci/pracownia2/franciszek_malinka/dist_vector.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Program: router - * Autor: Franciszek Malinka, 316093 - */ - -#include "dist_vector.h" -#include -#include -#include - -bool is_connected_directly(struct vector_item item) { - return item.is_connected_directly; -} - -bool is_reachable(struct vector_item item) { - return (item.distance < INFINITY_DIST); -} - -void init_dv(list_t *dv, int n_cnt, struct network_addr *neighbours) { - for (int i = 0; i < n_cnt; i++) { - struct vector_item new_item; - new_item.network = neighbours[i]; - new_item.distance = INFINITY_DIST; - new_item.is_connected_directly = true; - insert(dv, &new_item, sizeof(new_item)); - } -} - -void update_dv_reachability(list_t *distance_vector) { - reset(distance_vector); - while (distance_vector->it != NULL) { - struct vector_item *current = (struct vector_item *)distance_vector->it->data; - if(++current->reachable > REACHABILITY_WAIT_TIME) { - if (current->distance >= INFINITY_DIST) { - if (!is_connected_directly(*current)) { - erase_it(distance_vector); - } - } else { - current->distance = INFINITY_DIST; - current->reachable = 0; - } - } - iterate(distance_vector); - } -} - -void update_dv_new_item(list_t *distance_vector, struct vector_item new_item) { - bool new_entry = true; - reset(distance_vector); - while (distance_vector->it != NULL) { - struct vector_item *current = (struct vector_item *)distance_vector->it->data; - - /* If the network is already in the distance vector, then we possibly want to change it: - * - if the new item has better distance than the previous one, then we just want to change it no matter what, - * - if the new item has the same via ip, then we want to check two things: - * - if new item has infinity dist, then we want to set infinity (if it wasn't set, then we want to change reachable to 0) - * - if new item has < inf dist, then we want to change reachable to 0 and set our dist accordingly. - * - else we ignore the entry. - */ - if (get_network_address(current->network).s_addr == get_network_address(new_item.network).s_addr) { - if (current->distance > new_item.distance) { - *current = new_item; - current->reachable = 0; - } else if(current->via_ip.s_addr == new_item.via_ip.s_addr) { - if (new_item.distance >= INFINITY_DIST) { - if (current->distance < INFINITY_DIST) { - current->distance = INFINITY_DIST; - current->reachable = 0; - } - } else { - current->distance = new_item.distance; - current->reachable = 0; - } - } - new_entry = false; - } - - iterate(distance_vector); - } - - if (new_entry && new_item.reachable < INFINITY_DIST) { - insert(distance_vector, &new_item, sizeof(new_item)); - } -} - -void print_dv(list_t *distance_vector) { - time_t rawtime; - struct tm * timeinfo; - - time ( &rawtime ); - timeinfo = localtime ( &rawtime ); - char t[100]; - strcpy(t, asctime(timeinfo)); - t[strlen(t) - 1] = 0; - printf("Distance vector [%s]:\n", t); - reset(distance_vector); - while (distance_vector->it != NULL) { - char addr[20], via_addr[20]; - struct vector_item current = *(struct vector_item *)distance_vector->it->data; - struct in_addr net_addr = get_network_address(current.network); - inet_ntop(AF_INET, &net_addr, addr, sizeof(addr)); - printf("%s/%d ", addr, current.network.netmask); - - if (is_reachable(current)) printf("distance %d ", current.distance); - else printf("unreachable "); - - inet_ntop(AF_INET, ¤t.via_ip, via_addr, sizeof(via_addr)); - if (is_connected_directly(current)) printf("connected directly\n"); - else printf("via %s\n", via_addr); - - iterate(distance_vector); - } - printf("\n"); -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka/dist_vector.h b/Semestr 4/sieci/pracownia2/franciszek_malinka/dist_vector.h deleted file mode 100644 index 14159b4..0000000 --- a/Semestr 4/sieci/pracownia2/franciszek_malinka/dist_vector.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef DIST_VECTOR_H -#define DIST_VECTOR_H - -#include "linked_list.h" -#include "network_addr.h" -#include "config.h" - -/* Item of the distance vector. - * If <> is set to 0, then it means that the network is reachable. - * If <> has positive value, then it indicates that the network was - * unreachable for <> turns. - */ -struct vector_item { - struct network_addr network; - struct in_addr via_ip; - uint16_t distance; - uint8_t reachable; - bool is_connected_directly; -}; - -/* Initis distance vector with given neighbours array. */ -void init_dv(list_t *dv, int n_cnt, struct network_addr *neighbours); - -/* Returns true if given distance vector item is connected directly, false otherwise. */ -bool is_connected_directly(struct vector_item item); - -/* Updates the distance vector. */ -void update_dv_new_item(list_t *distance_vector, struct vector_item new_item); - -/* Updates reachabilities. */ -void update_dv_reachability(list_t *distance_vector); - -/* Print distance vector. */ -void print_dv(list_t *distance_vector); - -#endif \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka/linked_list.c b/Semestr 4/sieci/pracownia2/franciszek_malinka/linked_list.c deleted file mode 100644 index 16113ac..0000000 --- a/Semestr 4/sieci/pracownia2/franciszek_malinka/linked_list.c +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Program: router - * Autor: Franciszek Malinka, 316093 - */ - -#include "linked_list.h" -#include -#include - -node_t *_next(node_t *node) { - return (node == NULL) ? NULL : node->next; -} - -void _insert(node_t **head, void *data, size_t data_size) { - node_t *new_node = (node_t *)malloc(sizeof(node_t)); - new_node->data = malloc(data_size); - for (int i = 0; i < data_size; i++) - *(uint8_t *)(new_node->data + i) = *(uint8_t *)(data + i); - new_node->next = *head; - *head = new_node; -} - -void _free_node(node_t *node) { - free(node->data); - free(node); -} - -void _erase(node_t **head) { - node_t *next_node = _next(*head); - _free_node(*head); - *head = next_node; -} - -void _free_list(node_t *head) { - if (head == NULL) return; - _free_list(head->next); - _free_node(head); -} - -list_t create_list() { - list_t ret; - ret.head = NULL; - ret.it = NULL; - return ret; -} - -void insert(list_t *list, void *data, size_t data_size) { - _insert(&list->head, data, data_size); -} - -void erase(list_t *list) { - _erase(&list->head); -} - -void erase_it(list_t *list) { - if(list->it == NULL) return; - if(list->prev_it == NULL) { - erase(list); - reset(list); - return; - } - list->prev_it->next = _next(list->it); - _free_node(list->it); - list->it = list->prev_it->next; -} - -void iterate(list_t *list) { - list->prev_it = list->it; - list->it = _next(list->it); -} - -void reset(list_t *list) { - list->prev_it = NULL; - list->it = list->head; -} - -void free_list(list_t *list) { - _free_list(list->head); -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka/linked_list.h b/Semestr 4/sieci/pracownia2/franciszek_malinka/linked_list.h deleted file mode 100644 index 1574b2f..0000000 --- a/Semestr 4/sieci/pracownia2/franciszek_malinka/linked_list.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef LINKED_LIST_H -#define LINKED_LIST_H - -#include - -typedef struct node { - void *data; - struct node *next; -} node_t; - - -typedef struct list_t { - node_t *head; - node_t *it; - node_t *prev_it; -} list_t; - -/* Creates an empty list. */ -list_t create_list(); - -/* Insert a new node in the begining of a list. */ -void insert(list_t *list, void *data, size_t data_size); - -/* Erases first node from the list. */ -void erase(list_t *list); - -/* Erases element under iterator and sets iterator to the next one. */ -void erase_it(list_t *list); - -/* Moves iterator one step. */ -void iterate(list_t *list); - -/* Resets the iterator. - * Should execute the function after if you want to itarate unless you didnt insert or erase anything from the list. - */ -void reset(list_t *list); - -/* Deletes the whole list. */ -void free_list(list_t *list); - - -#endif \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka/makefile b/Semestr 4/sieci/pracownia2/franciszek_malinka/makefile deleted file mode 100644 index bf6a327..0000000 --- a/Semestr 4/sieci/pracownia2/franciszek_malinka/makefile +++ /dev/null @@ -1,34 +0,0 @@ -CC := gcc -CFLAGS := -O2 -std=gnu17 -Wall -Wall -Wno-unused-result -TARGET := router -TEST := test -DEPS := config.h - -ODIR := obj -_OBJ := router.o utils.o linked_list.o network_addr.o dist_vector.o -OBJ := $(patsubst %,$(ODIR)/%,$(_OBJ)) - -_TEST_OBJ := test.o linked_list.o -TEST_OBJ := $(patsubst %,$(ODIR)/%,$(_TEST_OBJ)) - - -all: $(TARGET) -test: $(TEST) - -$(ODIR)/%.o: %.c $(DEPS) - $(CC) $(CFLAGS) -c -o $@ $< - -$(TARGET): $(OBJ) - $(CC) -o $@ $^ $(CFLAGS) - -$(TEST): $(TEST_OBJ) - $(CC) -o $@ $^ $(CFLAGS) - -clean: - rm -rf $(TARGET) - rm -rf test - -distclean: - rm -rf $(TARGET) - rm -rf test - rm -rf $(ODIR)/*.o diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka/network_addr.c b/Semestr 4/sieci/pracownia2/franciszek_malinka/network_addr.c deleted file mode 100644 index cac1060..0000000 --- a/Semestr 4/sieci/pracownia2/franciszek_malinka/network_addr.c +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Program: router - * Autor: Franciszek Malinka, 316093 - */ - -#include "network_addr.h" -#include -#include -#include - -struct in_addr _get_broadcast_address(struct in_addr addr, uint16_t netmask) { - struct in_addr result = addr; - result.s_addr = ntohl(result.s_addr); - /* bitshift by more than 31 is UB */ - if (netmask == 0) { - result.s_addr = -1; - } - else { - result.s_addr |= ((1 << (32 - netmask)) - 1); - } - result.s_addr = htonl(result.s_addr); - - return result; -} - -struct in_addr _get_network_address(struct in_addr addr, uint16_t netmask) { - struct in_addr result = addr; - result.s_addr = ntohl(result.s_addr); - - if (netmask == 0) { - result.s_addr = 0; - } - else { - result.s_addr &= ~((1 << (32 - netmask)) - 1); - } - result.s_addr = htonl(result.s_addr); - - return result; -} - -struct in_addr get_broadcast_address(struct network_addr na) { - return _get_broadcast_address(na.addr, na.netmask); -} - -struct in_addr get_network_address(struct network_addr na) { - return _get_network_address(na.addr, na.netmask); -} - -void pretty_print_network(struct network_addr na) { - char ip_addr[20]; - inet_ntop(AF_INET, &na.addr, ip_addr, sizeof(ip_addr)); - printf("%s/%d\n", ip_addr, na.netmask); -} - -struct network_addr stona(char *str) { - struct network_addr result; - char addr[20]; - size_t ip_preffix = strcspn(str, "/"); - - strncpy(addr, str, ip_preffix); - addr[ip_preffix] = 0; - inet_pton(AF_INET, addr, &(result.addr)); - result.netmask = atoi(str + ip_preffix + 1); - return result; -} diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka/network_addr.h b/Semestr 4/sieci/pracownia2/franciszek_malinka/network_addr.h deleted file mode 100644 index 6347bbd..0000000 --- a/Semestr 4/sieci/pracownia2/franciszek_malinka/network_addr.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef ROUTER_ADDR_H -#define ROUTER_ADDR_H - -#include -#include -#include - -/* Network address with netmask. */ -struct network_addr { - struct in_addr addr; - uint8_t netmask; -}; - -typedef struct network_addr router_addr; - -/* Returns broadcast address of a given network. */ -struct in_addr get_broadcast_address(struct network_addr na); - -/* Returns network address of a given network. */ -struct in_addr get_network_address(struct network_addr na); - -/* Prints network_addr via stdio. */ -void pretty_print_network(struct network_addr na); - -/* Converts string of ip in CIDR notation with a netmask to network_addr. */ -struct network_addr stona(char *str); - -#endif \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka/router.c b/Semestr 4/sieci/pracownia2/franciszek_malinka/router.c deleted file mode 100644 index b5e732c..0000000 --- a/Semestr 4/sieci/pracownia2/franciszek_malinka/router.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Program: router - * Autor: Franciszek Malinka, 316093 - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "network_addr.h" -#include "utils.h" -#include "dist_vector.h" - -uint16_t read_configuration(struct network_addr** networks, uint16_t **dists) { - uint16_t n; - scanf("%hd", &n); - *networks = malloc(n * sizeof(struct network_addr)); - *dists = malloc(n * sizeof(uint16_t)); - for (int i = 0; i < n; i++) { - char addr[20]; - char _dist[10]; - uint16_t dist; - scanf(" %s %s %hd", addr, _dist, &dist); - (*networks)[i] = stona(addr); - (*dists)[i] = dist; - } - return n; -} - -void router_loop(int sockfd, int networks_number, struct network_addr *networks, uint16_t *dists) { - list_t dv = create_list(); - init_dv(&dv, networks_number, networks); - - printf("Starting the router loop...\n"); - for (;;) { - print_dv(&dv); - propagate_distance_vector(sockfd, networks_number, networks, dists, &dv); - listen_for_routers(sockfd, TURN_LEN_MS, networks_number, networks, dists, &dv); - } -} - -int main() { - struct network_addr* networks; - uint16_t *dists; - int n = read_configuration(&networks, &dists); - int sockfd = get_socket(); - bind_to_port(sockfd, SERVER_PORT); - - router_loop(sockfd, n, networks, dists); - - close(sockfd); - free(networks); - free(dists); -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka/test.c b/Semestr 4/sieci/pracownia2/franciszek_malinka/test.c deleted file mode 100644 index 958ff36..0000000 --- a/Semestr 4/sieci/pracownia2/franciszek_malinka/test.c +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Program: router - * Autor: Franciszek Malinka, 316093 - */ - -#include "linked_list.h" -#include -#include - -/* Prints the list of ints to stdio */ -void print_list(list_t list) { - printf("List: "); - reset(&list); - while (list.it != NULL) { - printf("%d, ", *(int *)(list.it->data)); - iterate(&list); - } - printf("\n"); - reset(&list); -} - -int main() { - int n; - scanf("%d", &n); - list_t list = create_list(); - - for (int i = 0; i < n; i++) { - int t; - scanf("%d", &t); - // insert - if (t == 0) { - int val = 0; - scanf("%d", &val); - insert(&list, &val, sizeof(int)); - reset(&list); - } - if (t == 1) { - iterate(&list); - if (list.it != NULL) - printf("it: %d\n", *(int *)list.it->data); - else printf("End of list.\n"); - } - if (t == 2) { - erase_it(&list); - } - if (t == 3) { - print_list(list); - } - } - - free_list(&list); -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka/utils.c b/Semestr 4/sieci/pracownia2/franciszek_malinka/utils.c deleted file mode 100644 index 0c0cae5..0000000 --- a/Semestr 4/sieci/pracownia2/franciszek_malinka/utils.c +++ /dev/null @@ -1,208 +0,0 @@ -/* - * Program: router - * Autor: Franciszek Malinka, 316093 - */ - -#include "utils.h" -#include -#include -#include -#include -#include -#include -#include - -int get_socket() { - int sockfd = socket(AF_INET, SOCK_DGRAM, 0); - if (sockfd < 0) { - fprintf(stderr, "Socket error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - return sockfd; -} - -void bind_to_port(int sockfd, uint16_t port) { - struct sockaddr_in server_address; - bzero(&server_address, sizeof(server_address)); - server_address.sin_family = AF_INET; - server_address.sin_port = htons(port); - server_address.sin_addr.s_addr = htonl(INADDR_ANY); - - if (bind(sockfd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) { - fprintf(stderr, "Bind error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - - int broadcastPermission = 1; - setsockopt (sockfd, SOL_SOCKET, SO_BROADCAST, (void *)&broadcastPermission, sizeof(broadcastPermission)); -} - -long get_time_interval(struct timespec start, struct timespec finish) { - return S_TO_MS(finish.tv_sec - start.tv_sec) + NS_TO_MS(finish.tv_nsec - start.tv_nsec); -} - -int poll_socket_modify_timeout(int sockfd, int *timeout) { - if (*timeout < 0) { - *timeout = 0; - return 0; - } - - struct pollfd fds; - struct timespec start; - struct timespec finish; - - fds.fd = sockfd; - fds.events = POLLIN; - fds.revents = 0; - - clock_gettime(CLOCK_REALTIME, &start); - int result = poll(&fds, 1, *timeout); - clock_gettime(CLOCK_REALTIME, &finish); - - if (result == -1) { - fprintf(stderr, "poll error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - if (result == 0) { - *timeout = 0; - return 0; - } - - *timeout -= get_time_interval(start, finish); - return result; -} - -size_t send_message(int sockfd, char *buffer, int buffer_len, struct in_addr network) { - struct sockaddr_in network_address; - bzero (&network_address, sizeof(network_address)); - network_address.sin_family = AF_INET; - network_address.sin_port = htons(SERVER_PORT); - network_address.sin_addr = network; - - return sendto(sockfd, buffer, buffer_len, 0, (struct sockaddr*) &network_address, sizeof(network_address)); -} - -size_t recv_message(int sockfd, char *buffer, struct sockaddr_in *sender) { - socklen_t sender_len = sizeof(*sender); - for (int i = 0; i < DV_DATAGRAM_LEN; i++) buffer[i] = 0; - size_t datagram_len = recvfrom(sockfd, buffer, IP_MAXPACKET, 0, - (struct sockaddr*)sender, &sender_len); - if (datagram_len < 0) { - fprintf(stderr, "recvfrom error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - - return datagram_len; -} - -struct vector_item parse_message(char *buffer, struct sockaddr_in *sender) { - struct vector_item res; - uint32_t ip_addr = *(uint32_t *)buffer; - uint32_t dist = *(uint32_t *)(buffer + 5); - dist = ntohl(dist); - - res.network.addr.s_addr = ip_addr; - res.network.netmask = buffer[4]; - res.is_connected_directly = true; - res.via_ip = sender->sin_addr; - res.distance = (dist < INFINITY_DIST ? dist : INFINITY_DIST); - res.reachable = 0; - - char addr[20]; - inet_ntop(AF_INET, &res.network.addr, addr, sizeof(addr)); - char via[20]; - inet_ntop(AF_INET, &sender->sin_addr, via, sizeof(via)); - return res; -} - -void _get_message(struct vector_item item, char *message) { - *(uint32_t *)message = item.network.addr.s_addr; - message[4] = item.network.netmask; - uint32_t distance = htonl(item.distance >= INFINITY_DIST ? INT_MAX : item.distance); - for (int i = 0; i < 4; i++) { - *(message + 5 + i) = *((char *)(&distance) + i); - } -} - -int _send_item(int sockfd, struct network_addr network, struct vector_item item) { - char message[DV_DATAGRAM_LEN + 1]; - _get_message(item, message); - message[DV_DATAGRAM_LEN] = 0; - ssize_t message_len = DV_DATAGRAM_LEN; - - struct in_addr na = get_broadcast_address(network); - - char addr[20]; - inet_ntop(AF_INET, &na, addr, sizeof(addr)); - - int result; - if ((result = send_message(sockfd, message, message_len, na)) != message_len) { - // fprintf(stderr, "sendto error: %s\n", strerror(errno)); - } - return result; -} - -void listen_for_routers(int sockfd, int timeout, int networks_number, struct network_addr *networks, uint16_t *dists, list_t *dv) { - char buffer[IP_MAXPACKET + 1]; - struct sockaddr_in sender; - - while (poll_socket_modify_timeout(sockfd, &timeout)) { - recv_message(sockfd, buffer, &sender); - struct vector_item new_item = parse_message(buffer, &sender); - - bool is_neighbour = false; - for (int i = 0; i < networks_number; i++) { - if (is_from_network(sender.sin_addr, networks[i])) { - is_neighbour = true; - break; - } - } - - /* Shouldn't happen, just in case. */ - if (!is_neighbour) { - char addr[20]; - inet_ntop(AF_INET, &sender.sin_addr, addr, sizeof(addr)); - fprintf(stderr, "Received datagram from %s, he is in none of my networks, ignoring\n. Maybe his VM routing table is configured incorrectly?\n", addr); - continue; - } - - if (!is_from_network(sender.sin_addr, new_item.network)) { - new_item.is_connected_directly = false; - - for (int i = 0; i < networks_number; i++) { - if (is_from_network(sender.sin_addr, networks[i])) { - new_item.distance += dists[i]; - break; - } - } - } - - update_dv_new_item(dv, new_item); - } - update_dv_reachability(dv); -} - -void propagate_distance_vector(int sockfd, int networks_number, struct network_addr *networks, uint16_t *dists, list_t *dv) { - for (int i = 0; i < networks_number; i++) { - reset(dv); - while (dv->it != NULL) { - struct vector_item data = *(struct vector_item *)dv->it->data; - if (!(get_network_address(data.network).s_addr == get_network_address(networks[i]).s_addr) && data.reachable <= REACHABILITY_WAIT_TIME) { - _send_item(sockfd, networks[i], data); - } - iterate(dv); - } - - struct vector_item self_item; - self_item.distance = dists[i]; - self_item.network = networks[i]; - _send_item(sockfd, networks[i], self_item); - } -} - -bool is_from_network(struct in_addr ip_addr, struct network_addr network) { - struct network_addr temp; - temp.addr= ip_addr; - temp.netmask = network.netmask; - return (get_network_address(temp).s_addr == get_network_address(network).s_addr); -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/franciszek_malinka/utils.h b/Semestr 4/sieci/pracownia2/franciszek_malinka/utils.h deleted file mode 100644 index edf90d0..0000000 --- a/Semestr 4/sieci/pracownia2/franciszek_malinka/utils.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef UTILS_H -#define UTILS_H -#define UTILS_H - -#include "config.h" -#include -#include -#include -#include "network_addr.h" -#include "dist_vector.h" - -#define NS_TO_MS(X) ((long)(X) / (long)1000000) -#define S_TO_MS(X) ((long)(X) * (long)1000) - -/* Returns a UDP socket. */ -int get_socket(); - -/* Binds socket to given port and set the broadcast permission. */ -void bind_to_port(int sockfd, uint16_t port); - -/* Computes the time elapsed between start and finish in miliseconds. */ -long get_time_interval(struct timespec start, struct timespec finish); - -/* Polls given socket with given timeout and changes the timeout accordingly. */ -int poll_socket_modify_timeout(int sockfd, int *timeout); - -/* For debug purposes only. Recieves and prints UDP message. */ -void recv_and_print(int sockfd, int networks_number, struct network_addr *networks); - -/* Sends message in buffer of length buffer_len to addr through given socket. - * IT DOES NOT TERMINATE THE PROGRAM IF SENDTO RETURNS ANY ERRORS! - * One must handle the errors on their own. - */ -size_t send_message(int sockfd, char *buffer, int buffer_len, struct in_addr addr); - -/* Receive message and write it to buffer. */ -size_t recv_message(int sockfd, char *buffer, struct sockaddr_in *sender); - -/* Parse datagram into a vector item. */ -struct vector_item parse_message(char *buffer, struct sockaddr_in *sender); - -/* Listnes for routers for timeout ms. */ -void listen_for_routers(int sockfd, int timeout, int networks_number, struct network_addr *networks, uint16_t *dists, list_t *dv); - -/* Propagates dv to all connected networks. */ -void propagate_distance_vector(int sockfd, int networks_number, struct network_addr *networks, uint16_t *dists, list_t *dv); - -/* Checks if given address is in network range. */ -bool is_from_network(struct in_addr ip_addr, struct network_addr network); - - -#endif \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/p2.pdf b/Semestr 4/sieci/pracownia2/p2.pdf deleted file mode 100644 index f0cc942..0000000 Binary files a/Semestr 4/sieci/pracownia2/p2.pdf and /dev/null differ diff --git a/Semestr 4/sieci/pracownia2/router/config.h b/Semestr 4/sieci/pracownia2/router/config.h deleted file mode 100644 index f83c556..0000000 --- a/Semestr 4/sieci/pracownia2/router/config.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef CONFIG_H -#define CONFIG_H - -#define SERVER_PORT 54321 -#define TURN_LEN_S 5 - -#define INFINITY_DIST 16 -#define REACHABILITY_WAIT_TIME 3 - -#define DV_DATAGRAM_LEN 9 - -#define TURN_LEN_MS (1000 * TURN_LEN_S) -#define TURN_LEN_US (1000000 * TURN_LEN_S) - -#endif diff --git a/Semestr 4/sieci/pracownia2/router/dist_vector.c b/Semestr 4/sieci/pracownia2/router/dist_vector.c deleted file mode 100644 index 74ae82e..0000000 --- a/Semestr 4/sieci/pracownia2/router/dist_vector.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Program: router - * Autor: Franciszek Malinka, 316093 - */ - -#include "dist_vector.h" -#include -#include -#include - -bool is_connected_directly(struct vector_item item) { - return item.is_connected_directly; -} - -bool is_reachable(struct vector_item item) { - return (item.distance < INFINITY_DIST); -} - -void init_dv(list_t *dv, int n_cnt, struct network_addr *neighbours) { - for (int i = 0; i < n_cnt; i++) { - struct vector_item new_item; - new_item.network = neighbours[i]; - new_item.distance = INFINITY_DIST; - new_item.is_connected_directly = true; - insert(dv, &new_item, sizeof(new_item)); - } -} - -void update_dv_reachability(list_t *distance_vector) { - reset(distance_vector); - while (distance_vector->it != NULL) { - struct vector_item *current = (struct vector_item *)distance_vector->it->data; - if(++current->reachable > REACHABILITY_WAIT_TIME) { - if (current->distance >= INFINITY_DIST) { - if (!is_connected_directly(*current)) { - erase_it(distance_vector); - } - } else { - current->distance = INFINITY_DIST; - current->reachable = 0; - } - } - iterate(distance_vector); - } -} - -void update_dv_new_item(list_t *distance_vector, struct vector_item new_item) { - bool new_entry = true; - reset(distance_vector); - while (distance_vector->it != NULL) { - struct vector_item *current = (struct vector_item *)distance_vector->it->data; - - /* If the network is already in the distance vector, then we possibly want to change it: - * - if the new item has better distance than the previous one, then we just want to change it no matter what, - * - if the new item has the same via ip, then we want to check two things: - * - if new item has infinity dist, then we want to set infinity (if it wasn't set, then we want to change reachable to 0) - * - if new item has < inf dist, then we want to change reachable to 0 and set our dist accordingly. - * - else we ignore the entry. - */ - if (get_network_address(current->network).s_addr == get_network_address(new_item.network).s_addr) { - if (current->distance > new_item.distance) { - *current = new_item; - current->reachable = 0; - } else if(current->via_ip.s_addr == new_item.via_ip.s_addr) { - if (new_item.distance >= INFINITY_DIST) { - if (current->distance < INFINITY_DIST) { - current->distance = INFINITY_DIST; - current->reachable = 0; - } - } else { - current->distance = new_item.distance; - current->reachable = 0; - } - } - new_entry = false; - } - - iterate(distance_vector); - } - - if (new_entry && new_item.reachable < INFINITY_DIST) { - insert(distance_vector, &new_item, sizeof(new_item)); - } -} - -void print_dv(list_t *distance_vector) { - time_t rawtime; - struct tm * timeinfo; - - time ( &rawtime ); - timeinfo = localtime ( &rawtime ); - char t[100]; - strcpy(t, asctime(timeinfo)); - t[strlen(t) - 1] = 0; - printf("Distance vector [%s]:\n", t); - reset(distance_vector); - while (distance_vector->it != NULL) { - char addr[20], via_addr[20]; - struct vector_item current = *(struct vector_item *)distance_vector->it->data; - struct in_addr net_addr = get_network_address(current.network); - inet_ntop(AF_INET, &net_addr, addr, sizeof(addr)); - printf("%s/%d ", addr, current.network.netmask); - - if (is_reachable(current)) printf("distance %d ", current.distance); - else printf("unreachable "); - - inet_ntop(AF_INET, ¤t.via_ip, via_addr, sizeof(via_addr)); - if (is_connected_directly(current)) printf("connected directly\n"); - else printf("via %s\n", via_addr); - - iterate(distance_vector); - } - printf("\n"); -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/router/dist_vector.h b/Semestr 4/sieci/pracownia2/router/dist_vector.h deleted file mode 100644 index 14159b4..0000000 --- a/Semestr 4/sieci/pracownia2/router/dist_vector.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef DIST_VECTOR_H -#define DIST_VECTOR_H - -#include "linked_list.h" -#include "network_addr.h" -#include "config.h" - -/* Item of the distance vector. - * If <> is set to 0, then it means that the network is reachable. - * If <> has positive value, then it indicates that the network was - * unreachable for <> turns. - */ -struct vector_item { - struct network_addr network; - struct in_addr via_ip; - uint16_t distance; - uint8_t reachable; - bool is_connected_directly; -}; - -/* Initis distance vector with given neighbours array. */ -void init_dv(list_t *dv, int n_cnt, struct network_addr *neighbours); - -/* Returns true if given distance vector item is connected directly, false otherwise. */ -bool is_connected_directly(struct vector_item item); - -/* Updates the distance vector. */ -void update_dv_new_item(list_t *distance_vector, struct vector_item new_item); - -/* Updates reachabilities. */ -void update_dv_reachability(list_t *distance_vector); - -/* Print distance vector. */ -void print_dv(list_t *distance_vector); - -#endif \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/router/linked_list.c b/Semestr 4/sieci/pracownia2/router/linked_list.c deleted file mode 100644 index 16113ac..0000000 --- a/Semestr 4/sieci/pracownia2/router/linked_list.c +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Program: router - * Autor: Franciszek Malinka, 316093 - */ - -#include "linked_list.h" -#include -#include - -node_t *_next(node_t *node) { - return (node == NULL) ? NULL : node->next; -} - -void _insert(node_t **head, void *data, size_t data_size) { - node_t *new_node = (node_t *)malloc(sizeof(node_t)); - new_node->data = malloc(data_size); - for (int i = 0; i < data_size; i++) - *(uint8_t *)(new_node->data + i) = *(uint8_t *)(data + i); - new_node->next = *head; - *head = new_node; -} - -void _free_node(node_t *node) { - free(node->data); - free(node); -} - -void _erase(node_t **head) { - node_t *next_node = _next(*head); - _free_node(*head); - *head = next_node; -} - -void _free_list(node_t *head) { - if (head == NULL) return; - _free_list(head->next); - _free_node(head); -} - -list_t create_list() { - list_t ret; - ret.head = NULL; - ret.it = NULL; - return ret; -} - -void insert(list_t *list, void *data, size_t data_size) { - _insert(&list->head, data, data_size); -} - -void erase(list_t *list) { - _erase(&list->head); -} - -void erase_it(list_t *list) { - if(list->it == NULL) return; - if(list->prev_it == NULL) { - erase(list); - reset(list); - return; - } - list->prev_it->next = _next(list->it); - _free_node(list->it); - list->it = list->prev_it->next; -} - -void iterate(list_t *list) { - list->prev_it = list->it; - list->it = _next(list->it); -} - -void reset(list_t *list) { - list->prev_it = NULL; - list->it = list->head; -} - -void free_list(list_t *list) { - _free_list(list->head); -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/router/linked_list.h b/Semestr 4/sieci/pracownia2/router/linked_list.h deleted file mode 100644 index 1574b2f..0000000 --- a/Semestr 4/sieci/pracownia2/router/linked_list.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef LINKED_LIST_H -#define LINKED_LIST_H - -#include - -typedef struct node { - void *data; - struct node *next; -} node_t; - - -typedef struct list_t { - node_t *head; - node_t *it; - node_t *prev_it; -} list_t; - -/* Creates an empty list. */ -list_t create_list(); - -/* Insert a new node in the begining of a list. */ -void insert(list_t *list, void *data, size_t data_size); - -/* Erases first node from the list. */ -void erase(list_t *list); - -/* Erases element under iterator and sets iterator to the next one. */ -void erase_it(list_t *list); - -/* Moves iterator one step. */ -void iterate(list_t *list); - -/* Resets the iterator. - * Should execute the function after if you want to itarate unless you didnt insert or erase anything from the list. - */ -void reset(list_t *list); - -/* Deletes the whole list. */ -void free_list(list_t *list); - - -#endif \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/router/makefile b/Semestr 4/sieci/pracownia2/router/makefile deleted file mode 100644 index bf6a327..0000000 --- a/Semestr 4/sieci/pracownia2/router/makefile +++ /dev/null @@ -1,34 +0,0 @@ -CC := gcc -CFLAGS := -O2 -std=gnu17 -Wall -Wall -Wno-unused-result -TARGET := router -TEST := test -DEPS := config.h - -ODIR := obj -_OBJ := router.o utils.o linked_list.o network_addr.o dist_vector.o -OBJ := $(patsubst %,$(ODIR)/%,$(_OBJ)) - -_TEST_OBJ := test.o linked_list.o -TEST_OBJ := $(patsubst %,$(ODIR)/%,$(_TEST_OBJ)) - - -all: $(TARGET) -test: $(TEST) - -$(ODIR)/%.o: %.c $(DEPS) - $(CC) $(CFLAGS) -c -o $@ $< - -$(TARGET): $(OBJ) - $(CC) -o $@ $^ $(CFLAGS) - -$(TEST): $(TEST_OBJ) - $(CC) -o $@ $^ $(CFLAGS) - -clean: - rm -rf $(TARGET) - rm -rf test - -distclean: - rm -rf $(TARGET) - rm -rf test - rm -rf $(ODIR)/*.o diff --git a/Semestr 4/sieci/pracownia2/router/network_addr.c b/Semestr 4/sieci/pracownia2/router/network_addr.c deleted file mode 100644 index cac1060..0000000 --- a/Semestr 4/sieci/pracownia2/router/network_addr.c +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Program: router - * Autor: Franciszek Malinka, 316093 - */ - -#include "network_addr.h" -#include -#include -#include - -struct in_addr _get_broadcast_address(struct in_addr addr, uint16_t netmask) { - struct in_addr result = addr; - result.s_addr = ntohl(result.s_addr); - /* bitshift by more than 31 is UB */ - if (netmask == 0) { - result.s_addr = -1; - } - else { - result.s_addr |= ((1 << (32 - netmask)) - 1); - } - result.s_addr = htonl(result.s_addr); - - return result; -} - -struct in_addr _get_network_address(struct in_addr addr, uint16_t netmask) { - struct in_addr result = addr; - result.s_addr = ntohl(result.s_addr); - - if (netmask == 0) { - result.s_addr = 0; - } - else { - result.s_addr &= ~((1 << (32 - netmask)) - 1); - } - result.s_addr = htonl(result.s_addr); - - return result; -} - -struct in_addr get_broadcast_address(struct network_addr na) { - return _get_broadcast_address(na.addr, na.netmask); -} - -struct in_addr get_network_address(struct network_addr na) { - return _get_network_address(na.addr, na.netmask); -} - -void pretty_print_network(struct network_addr na) { - char ip_addr[20]; - inet_ntop(AF_INET, &na.addr, ip_addr, sizeof(ip_addr)); - printf("%s/%d\n", ip_addr, na.netmask); -} - -struct network_addr stona(char *str) { - struct network_addr result; - char addr[20]; - size_t ip_preffix = strcspn(str, "/"); - - strncpy(addr, str, ip_preffix); - addr[ip_preffix] = 0; - inet_pton(AF_INET, addr, &(result.addr)); - result.netmask = atoi(str + ip_preffix + 1); - return result; -} diff --git a/Semestr 4/sieci/pracownia2/router/network_addr.h b/Semestr 4/sieci/pracownia2/router/network_addr.h deleted file mode 100644 index 6347bbd..0000000 --- a/Semestr 4/sieci/pracownia2/router/network_addr.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef ROUTER_ADDR_H -#define ROUTER_ADDR_H - -#include -#include -#include - -/* Network address with netmask. */ -struct network_addr { - struct in_addr addr; - uint8_t netmask; -}; - -typedef struct network_addr router_addr; - -/* Returns broadcast address of a given network. */ -struct in_addr get_broadcast_address(struct network_addr na); - -/* Returns network address of a given network. */ -struct in_addr get_network_address(struct network_addr na); - -/* Prints network_addr via stdio. */ -void pretty_print_network(struct network_addr na); - -/* Converts string of ip in CIDR notation with a netmask to network_addr. */ -struct network_addr stona(char *str); - -#endif \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/router/router.c b/Semestr 4/sieci/pracownia2/router/router.c deleted file mode 100644 index b5e732c..0000000 --- a/Semestr 4/sieci/pracownia2/router/router.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Program: router - * Autor: Franciszek Malinka, 316093 - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "network_addr.h" -#include "utils.h" -#include "dist_vector.h" - -uint16_t read_configuration(struct network_addr** networks, uint16_t **dists) { - uint16_t n; - scanf("%hd", &n); - *networks = malloc(n * sizeof(struct network_addr)); - *dists = malloc(n * sizeof(uint16_t)); - for (int i = 0; i < n; i++) { - char addr[20]; - char _dist[10]; - uint16_t dist; - scanf(" %s %s %hd", addr, _dist, &dist); - (*networks)[i] = stona(addr); - (*dists)[i] = dist; - } - return n; -} - -void router_loop(int sockfd, int networks_number, struct network_addr *networks, uint16_t *dists) { - list_t dv = create_list(); - init_dv(&dv, networks_number, networks); - - printf("Starting the router loop...\n"); - for (;;) { - print_dv(&dv); - propagate_distance_vector(sockfd, networks_number, networks, dists, &dv); - listen_for_routers(sockfd, TURN_LEN_MS, networks_number, networks, dists, &dv); - } -} - -int main() { - struct network_addr* networks; - uint16_t *dists; - int n = read_configuration(&networks, &dists); - int sockfd = get_socket(); - bind_to_port(sockfd, SERVER_PORT); - - router_loop(sockfd, n, networks, dists); - - close(sockfd); - free(networks); - free(dists); -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/router/test.c b/Semestr 4/sieci/pracownia2/router/test.c deleted file mode 100644 index 958ff36..0000000 --- a/Semestr 4/sieci/pracownia2/router/test.c +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Program: router - * Autor: Franciszek Malinka, 316093 - */ - -#include "linked_list.h" -#include -#include - -/* Prints the list of ints to stdio */ -void print_list(list_t list) { - printf("List: "); - reset(&list); - while (list.it != NULL) { - printf("%d, ", *(int *)(list.it->data)); - iterate(&list); - } - printf("\n"); - reset(&list); -} - -int main() { - int n; - scanf("%d", &n); - list_t list = create_list(); - - for (int i = 0; i < n; i++) { - int t; - scanf("%d", &t); - // insert - if (t == 0) { - int val = 0; - scanf("%d", &val); - insert(&list, &val, sizeof(int)); - reset(&list); - } - if (t == 1) { - iterate(&list); - if (list.it != NULL) - printf("it: %d\n", *(int *)list.it->data); - else printf("End of list.\n"); - } - if (t == 2) { - erase_it(&list); - } - if (t == 3) { - print_list(list); - } - } - - free_list(&list); -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/router/utils.c b/Semestr 4/sieci/pracownia2/router/utils.c deleted file mode 100644 index 0c0cae5..0000000 --- a/Semestr 4/sieci/pracownia2/router/utils.c +++ /dev/null @@ -1,208 +0,0 @@ -/* - * Program: router - * Autor: Franciszek Malinka, 316093 - */ - -#include "utils.h" -#include -#include -#include -#include -#include -#include -#include - -int get_socket() { - int sockfd = socket(AF_INET, SOCK_DGRAM, 0); - if (sockfd < 0) { - fprintf(stderr, "Socket error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - return sockfd; -} - -void bind_to_port(int sockfd, uint16_t port) { - struct sockaddr_in server_address; - bzero(&server_address, sizeof(server_address)); - server_address.sin_family = AF_INET; - server_address.sin_port = htons(port); - server_address.sin_addr.s_addr = htonl(INADDR_ANY); - - if (bind(sockfd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) { - fprintf(stderr, "Bind error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - - int broadcastPermission = 1; - setsockopt (sockfd, SOL_SOCKET, SO_BROADCAST, (void *)&broadcastPermission, sizeof(broadcastPermission)); -} - -long get_time_interval(struct timespec start, struct timespec finish) { - return S_TO_MS(finish.tv_sec - start.tv_sec) + NS_TO_MS(finish.tv_nsec - start.tv_nsec); -} - -int poll_socket_modify_timeout(int sockfd, int *timeout) { - if (*timeout < 0) { - *timeout = 0; - return 0; - } - - struct pollfd fds; - struct timespec start; - struct timespec finish; - - fds.fd = sockfd; - fds.events = POLLIN; - fds.revents = 0; - - clock_gettime(CLOCK_REALTIME, &start); - int result = poll(&fds, 1, *timeout); - clock_gettime(CLOCK_REALTIME, &finish); - - if (result == -1) { - fprintf(stderr, "poll error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - if (result == 0) { - *timeout = 0; - return 0; - } - - *timeout -= get_time_interval(start, finish); - return result; -} - -size_t send_message(int sockfd, char *buffer, int buffer_len, struct in_addr network) { - struct sockaddr_in network_address; - bzero (&network_address, sizeof(network_address)); - network_address.sin_family = AF_INET; - network_address.sin_port = htons(SERVER_PORT); - network_address.sin_addr = network; - - return sendto(sockfd, buffer, buffer_len, 0, (struct sockaddr*) &network_address, sizeof(network_address)); -} - -size_t recv_message(int sockfd, char *buffer, struct sockaddr_in *sender) { - socklen_t sender_len = sizeof(*sender); - for (int i = 0; i < DV_DATAGRAM_LEN; i++) buffer[i] = 0; - size_t datagram_len = recvfrom(sockfd, buffer, IP_MAXPACKET, 0, - (struct sockaddr*)sender, &sender_len); - if (datagram_len < 0) { - fprintf(stderr, "recvfrom error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - - return datagram_len; -} - -struct vector_item parse_message(char *buffer, struct sockaddr_in *sender) { - struct vector_item res; - uint32_t ip_addr = *(uint32_t *)buffer; - uint32_t dist = *(uint32_t *)(buffer + 5); - dist = ntohl(dist); - - res.network.addr.s_addr = ip_addr; - res.network.netmask = buffer[4]; - res.is_connected_directly = true; - res.via_ip = sender->sin_addr; - res.distance = (dist < INFINITY_DIST ? dist : INFINITY_DIST); - res.reachable = 0; - - char addr[20]; - inet_ntop(AF_INET, &res.network.addr, addr, sizeof(addr)); - char via[20]; - inet_ntop(AF_INET, &sender->sin_addr, via, sizeof(via)); - return res; -} - -void _get_message(struct vector_item item, char *message) { - *(uint32_t *)message = item.network.addr.s_addr; - message[4] = item.network.netmask; - uint32_t distance = htonl(item.distance >= INFINITY_DIST ? INT_MAX : item.distance); - for (int i = 0; i < 4; i++) { - *(message + 5 + i) = *((char *)(&distance) + i); - } -} - -int _send_item(int sockfd, struct network_addr network, struct vector_item item) { - char message[DV_DATAGRAM_LEN + 1]; - _get_message(item, message); - message[DV_DATAGRAM_LEN] = 0; - ssize_t message_len = DV_DATAGRAM_LEN; - - struct in_addr na = get_broadcast_address(network); - - char addr[20]; - inet_ntop(AF_INET, &na, addr, sizeof(addr)); - - int result; - if ((result = send_message(sockfd, message, message_len, na)) != message_len) { - // fprintf(stderr, "sendto error: %s\n", strerror(errno)); - } - return result; -} - -void listen_for_routers(int sockfd, int timeout, int networks_number, struct network_addr *networks, uint16_t *dists, list_t *dv) { - char buffer[IP_MAXPACKET + 1]; - struct sockaddr_in sender; - - while (poll_socket_modify_timeout(sockfd, &timeout)) { - recv_message(sockfd, buffer, &sender); - struct vector_item new_item = parse_message(buffer, &sender); - - bool is_neighbour = false; - for (int i = 0; i < networks_number; i++) { - if (is_from_network(sender.sin_addr, networks[i])) { - is_neighbour = true; - break; - } - } - - /* Shouldn't happen, just in case. */ - if (!is_neighbour) { - char addr[20]; - inet_ntop(AF_INET, &sender.sin_addr, addr, sizeof(addr)); - fprintf(stderr, "Received datagram from %s, he is in none of my networks, ignoring\n. Maybe his VM routing table is configured incorrectly?\n", addr); - continue; - } - - if (!is_from_network(sender.sin_addr, new_item.network)) { - new_item.is_connected_directly = false; - - for (int i = 0; i < networks_number; i++) { - if (is_from_network(sender.sin_addr, networks[i])) { - new_item.distance += dists[i]; - break; - } - } - } - - update_dv_new_item(dv, new_item); - } - update_dv_reachability(dv); -} - -void propagate_distance_vector(int sockfd, int networks_number, struct network_addr *networks, uint16_t *dists, list_t *dv) { - for (int i = 0; i < networks_number; i++) { - reset(dv); - while (dv->it != NULL) { - struct vector_item data = *(struct vector_item *)dv->it->data; - if (!(get_network_address(data.network).s_addr == get_network_address(networks[i]).s_addr) && data.reachable <= REACHABILITY_WAIT_TIME) { - _send_item(sockfd, networks[i], data); - } - iterate(dv); - } - - struct vector_item self_item; - self_item.distance = dists[i]; - self_item.network = networks[i]; - _send_item(sockfd, networks[i], self_item); - } -} - -bool is_from_network(struct in_addr ip_addr, struct network_addr network) { - struct network_addr temp; - temp.addr= ip_addr; - temp.netmask = network.netmask; - return (get_network_address(temp).s_addr == get_network_address(network).s_addr); -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/router/utils.h b/Semestr 4/sieci/pracownia2/router/utils.h deleted file mode 100644 index edf90d0..0000000 --- a/Semestr 4/sieci/pracownia2/router/utils.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef UTILS_H -#define UTILS_H -#define UTILS_H - -#include "config.h" -#include -#include -#include -#include "network_addr.h" -#include "dist_vector.h" - -#define NS_TO_MS(X) ((long)(X) / (long)1000000) -#define S_TO_MS(X) ((long)(X) * (long)1000) - -/* Returns a UDP socket. */ -int get_socket(); - -/* Binds socket to given port and set the broadcast permission. */ -void bind_to_port(int sockfd, uint16_t port); - -/* Computes the time elapsed between start and finish in miliseconds. */ -long get_time_interval(struct timespec start, struct timespec finish); - -/* Polls given socket with given timeout and changes the timeout accordingly. */ -int poll_socket_modify_timeout(int sockfd, int *timeout); - -/* For debug purposes only. Recieves and prints UDP message. */ -void recv_and_print(int sockfd, int networks_number, struct network_addr *networks); - -/* Sends message in buffer of length buffer_len to addr through given socket. - * IT DOES NOT TERMINATE THE PROGRAM IF SENDTO RETURNS ANY ERRORS! - * One must handle the errors on their own. - */ -size_t send_message(int sockfd, char *buffer, int buffer_len, struct in_addr addr); - -/* Receive message and write it to buffer. */ -size_t recv_message(int sockfd, char *buffer, struct sockaddr_in *sender); - -/* Parse datagram into a vector item. */ -struct vector_item parse_message(char *buffer, struct sockaddr_in *sender); - -/* Listnes for routers for timeout ms. */ -void listen_for_routers(int sockfd, int timeout, int networks_number, struct network_addr *networks, uint16_t *dists, list_t *dv); - -/* Propagates dv to all connected networks. */ -void propagate_distance_vector(int sockfd, int networks_number, struct network_addr *networks, uint16_t *dists, list_t *dv); - -/* Checks if given address is in network range. */ -bool is_from_network(struct in_addr ip_addr, struct network_addr network); - - -#endif \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia2/udp_client.c b/Semestr 4/sieci/pracownia2/udp_client.c deleted file mode 100755 index 4e7dce4..0000000 --- a/Semestr 4/sieci/pracownia2/udp_client.c +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -int main(int argc, char * argv[]) -{ - if (argc < 2) { - printf("Usage:\n\t%s [server ip]\n", argv[0]); - return -1; - } - - int sockfd = socket(AF_INET, SOCK_DGRAM, 0); - if (sockfd < 0) { - fprintf(stderr, "socket error: %s\n", strerror(errno)); - return EXIT_FAILURE; - } - - struct sockaddr_in server_address; - bzero (&server_address, sizeof(server_address)); - server_address.sin_family = AF_INET; - server_address.sin_port = htons(54321); - if (!inet_pton(AF_INET, argv[1], &server_address.sin_addr)) { - printf("Inavlid ip address\n"); - return -1; - } - - char* message = "Hello server!"; - ssize_t message_len = strlen(message); - if (sendto(sockfd, message, message_len, 0, (struct sockaddr*) &server_address, sizeof(server_address)) != message_len) { - fprintf(stderr, "sendto error: %s\n", strerror(errno)); - return EXIT_FAILURE; - } - - close (sockfd); - return EXIT_SUCCESS; -} - diff --git a/Semestr 4/sieci/pracownia2/udp_server.c b/Semestr 4/sieci/pracownia2/udp_server.c deleted file mode 100755 index b5ae6d1..0000000 --- a/Semestr 4/sieci/pracownia2/udp_server.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -int main() -{ - int sockfd = socket(AF_INET, SOCK_DGRAM, 0); - if (sockfd < 0) { - fprintf(stderr, "socket error: %s\n", strerror(errno)); - return EXIT_FAILURE; - } - - struct sockaddr_in server_address; - bzero (&server_address, sizeof(server_address)); - server_address.sin_family = AF_INET; - server_address.sin_port = htons(54321); - server_address.sin_addr.s_addr = htonl(INADDR_ANY); - if (bind (sockfd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) { - fprintf(stderr, "bind error: %s\n", strerror(errno)); - return EXIT_FAILURE; - } - - for (;;) { - - struct sockaddr_in sender; - socklen_t sender_len = sizeof(sender); - u_int8_t buffer[IP_MAXPACKET+1]; - - ssize_t datagram_len = recvfrom (sockfd, buffer, IP_MAXPACKET, 0, (struct sockaddr*)&sender, &sender_len); - if (datagram_len < 0) { - fprintf(stderr, "recvfrom error: %s\n", strerror(errno)); - return EXIT_FAILURE; - } - - char sender_ip_str[20]; - inet_ntop(AF_INET, &(sender.sin_addr), sender_ip_str, sizeof(sender_ip_str)); - printf ("Received UDP packet from IP address: %s, port: %d\n", sender_ip_str, ntohs(sender.sin_port)); - - buffer[datagram_len] = 0; - printf ("%ld-byte message: +%s+\n", datagram_len, buffer); - - char* reply = "Thank you!"; - ssize_t reply_len = strlen(reply); - if (sendto(sockfd, reply, reply_len, 0, (struct sockaddr*)&sender, sender_len) != reply_len) { - fprintf(stderr, "sendto error: %s\n", strerror(errno)); - return EXIT_FAILURE; - } - - fflush(stdout); - } - - close (sockfd); - return EXIT_SUCCESS; -} - diff --git a/Semestr 4/sieci/pracownia3/config.h b/Semestr 4/sieci/pracownia3/config.h deleted file mode 100644 index 3b81646..0000000 --- a/Semestr 4/sieci/pracownia3/config.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef CONFIG_H -#define CONFIG_H - -#define DATAGRAM_LEN 1000 -#define HEADER_LEN 40 - -#define WINDOW_SIZE 3000 -#define TIMEOUT 100 - -#endif diff --git a/Semestr 4/sieci/pracownia3/p3.pdf b/Semestr 4/sieci/pracownia3/p3.pdf deleted file mode 100644 index 14f1261..0000000 Binary files a/Semestr 4/sieci/pracownia3/p3.pdf and /dev/null differ diff --git a/Semestr 4/sieci/pracownia3/transport.c b/Semestr 4/sieci/pracownia3/transport.c deleted file mode 100644 index 9226d69..0000000 --- a/Semestr 4/sieci/pracownia3/transport.c +++ /dev/null @@ -1,149 +0,0 @@ -/* Projekt: Transport - * Autor: Franciszek Malinka 316093 - */ - -#include -#include -#include -#include -#include -#include -#include -#include "config.h" -#include "window.h" -#include "utils.h" - -size_t send_datagram(int sockfd, struct sockaddr_in server_address, char *buffer, size_t buffer_len) { - return sendto(sockfd, buffer, buffer_len, 0, (struct sockaddr*) &server_address, sizeof(server_address)); -} - -void send_data_request(int sockfd, struct sockaddr_in server_address, size_t pos, size_t bytes) { - char buffer[40]; - sprintf(buffer, "GET %ld %ld\n", pos, bytes); - size_t buffer_len = strlen(buffer); - if (send_datagram(sockfd, server_address, buffer, buffer_len) != buffer_len) { - fprintf(stderr, "sendto error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } -} - -size_t recv_message(int sockfd, char *buffer, struct sockaddr_in *sender) { - socklen_t sender_len = sizeof(*sender); - bzero(buffer, HEADER_LEN + DATAGRAM_LEN); - bzero(sender, sizeof(*sender)); - size_t datagram_len = recvfrom(sockfd, buffer, IP_MAXPACKET, 0, - (struct sockaddr*)sender, &sender_len); - - if (datagram_len < 0) { - fprintf(stderr, "recvfrom error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - - return datagram_len; -} - -void request_data(int sockfd, struct sockaddr_in server_address, window_t *w, size_t bytes_writen, size_t remaining_bytes) { - int pos = w->first_pos; - for (int i = 0; i < w->size && i*DATAGRAM_LEN < remaining_bytes; i++) { - if (w->uptodate[pos] == false) { - size_t bytes_to_request = min(DATAGRAM_LEN, remaining_bytes - i*DATAGRAM_LEN); - send_data_request(sockfd, server_address, bytes_writen + i*DATAGRAM_LEN, bytes_to_request); - } - pos = (pos + 1) % w->size; - } -} - -void update_file(FILE *fd, window_t *w, size_t *bytes_writen, size_t *remaining_bytes) { - while (w->uptodate[w->first_pos] && *remaining_bytes > 0) { - // printf("Writing %ld\n", *bytes_writen); - size_t bytes_to_write = min(DATAGRAM_LEN, *remaining_bytes); - fwrite(w->ar[w->first_pos], sizeof(char), bytes_to_write, fd); - *bytes_writen += bytes_to_write; - *remaining_bytes -= bytes_to_write; - shift(w); - } -} - -size_t recv_datagram(int sockfd, char *buffer, struct sockaddr_in server_address) { - struct sockaddr_in sender; - - size_t received_bytes = recv_message(sockfd, buffer, &sender); - if (sender.sin_addr.s_addr != server_address.sin_addr.s_addr || sender.sin_port != server_address.sin_port) { - printf("Smieci!\n"); - return 0; - } - return received_bytes; -} - - -void receive_file(int sockfd, struct sockaddr_in server_address, const char *file_name, size_t remaining_bytes) { - FILE *fd = fopen(file_name, "w"); - if (!fd) { - fprintf(stderr, "fopen error: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - size_t bytes_writen = 0; - - size_t recv_pos, recv_len; - char buffer[DATAGRAM_LEN + HEADER_LEN]; - int prev_len = 0; - window_t w; - init_window(&w, WINDOW_SIZE, DATAGRAM_LEN); - - while (remaining_bytes) { - request_data(sockfd, server_address, &w, bytes_writen, remaining_bytes); - int timeout = TIMEOUT; - while (poll_socket_modify_timeout(sockfd, &timeout)) { - size_t received_bytes = recv_datagram(sockfd, buffer, server_address); - if (received_bytes == 0) continue; - sscanf(buffer, "DATA %ld %ld\n", &recv_pos, &recv_len); - if (recv_pos < bytes_writen) continue; - - int pos = (recv_pos - bytes_writen) / DATAGRAM_LEN; - pos = (pos + w.first_pos) % w.size; - if (!w.uptodate[pos]) { - for (int i = 0; i < recv_len; i++) { - w.ar[pos][i] = buffer[i + received_bytes - recv_len]; - } - w.uptodate[pos] = true; - } - update_file(fd, &w, &bytes_writen, &remaining_bytes); - } - - if (prev_len != bytes_writen) { - prev_len = bytes_writen; - printf("%.3f%%\n", 100.0 * (float)(bytes_writen) / (float)(remaining_bytes+bytes_writen)); - } - } - destroy_window(&w); - fclose(fd); -} - -int main(int argc, char *argv[]) { - if (argc != 5) { - printf("Usage:\n\t%s [server ip] [server port] [output file name] [file size]\n", argv[0]); - return -1; - } - - int sockfd = get_socket(); - struct sockaddr_in server_address; - bzero(&server_address, sizeof(server_address)); - server_address.sin_family = AF_INET; - if (!inet_pton(AF_INET, argv[1], &server_address.sin_addr)) { - fprintf(stderr, "Invalid ip address: %s\n", argv[1]); - return -1; - } - server_address.sin_port = htons(atoi(argv[2])); - if (server_address.sin_port == 0) { - fprintf(stderr, "Invalid port: %s\n", argv[2]); - return -1; - } - - size_t file_len = atoi(argv[4]); - if (file_len == 0) { - printf("File len is 0, nothing to do here.\n"); - return 0; - } - - receive_file(sockfd, server_address, argv[3], file_len); -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia3/utils.c b/Semestr 4/sieci/pracownia3/utils.c deleted file mode 100644 index 4c2f449..0000000 --- a/Semestr 4/sieci/pracownia3/utils.c +++ /dev/null @@ -1,58 +0,0 @@ -/* Projekt: Transport - * Autor: Franciszek Malinka 316093 - */ - -#include "utils.h" -#include -#include -#include -#include -#include -#include -#include - -size_t min(size_t x, size_t y) { return (x -#include - -#define NS_TO_MS(X) ((long)(X) / (long)1000000) -#define S_TO_MS(X) ((long)(X) * (long)1000) - -size_t min(size_t x, size_t y); - -int get_socket(); - -long get_time_interval(struct timespec start, struct timespec finish); - -int poll_socket_modify_timeout(int sockfd, int *timeout); - -#endif \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia3/window.c b/Semestr 4/sieci/pracownia3/window.c deleted file mode 100644 index c8bffad..0000000 --- a/Semestr 4/sieci/pracownia3/window.c +++ /dev/null @@ -1,42 +0,0 @@ -/* Projekt: Transport - * Autor: Franciszek Malinka 316093 - */ - -#include "window.h" -#include -#include - -void init_window(window_t *w, int window_size, int window_width) { - w->ar = malloc(window_size * sizeof(char *)); - for (int i = 0; i < window_size; i++) - w->ar[i] = malloc(window_width * sizeof(char)); - w->uptodate = malloc(window_size); - bzero (w->uptodate, window_size); - w->first_pos = 0; - w->size = window_size; -} - -void destroy_window(window_t *w) { - for (int i = 0; i < w->size; i++) - free(w->ar[i]); - free(w->ar); - free(w->uptodate); -} - -void shift_while_uptodate(window_t *w) { - while (w->uptodate[w->first_pos]) { - w->uptodate[w->first_pos] = false; - w->first_pos = (w->first_pos + 1) % w->size; - } -} - -void shift(window_t *w) { - w->uptodate[w->first_pos] = false; - w->first_pos = (w->first_pos + 1) % w->size; -} - -void update(window_t *w, int pos, char *buffer, size_t buf_size) { - pos = (w->first_pos + pos) % w->size; - for (int i = 0; i < buf_size; i++) w->ar[pos][i] = buffer[i]; - w->uptodate[pos] = true; -} \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia3/window.h b/Semestr 4/sieci/pracownia3/window.h deleted file mode 100644 index 63e89cc..0000000 --- a/Semestr 4/sieci/pracownia3/window.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef WINDOW_H -#define WINDOW_H - -#include -#include - -typedef struct { - char **ar; - bool *uptodate; - int size; - int first_pos; -} window_t; - -void init_window(window_t *w, int window_size, int window_width); - -void destroy_window(window_t *w); - -void shift_while_uptodate(window_t *w); - -void shift(window_t *w); - -void update(window_t *w, int pos, char *buffer, size_t buf_size); - -#endif \ No newline at end of file diff --git a/Semestr 4/sieci/pracownia4/p4.pdf b/Semestr 4/sieci/pracownia4/p4.pdf deleted file mode 100644 index bdaa771..0000000 Binary files a/Semestr 4/sieci/pracownia4/p4.pdf and /dev/null differ diff --git a/Semestr 4/sieci/warsztaty1/w1.pdf b/Semestr 4/sieci/warsztaty1/w1.pdf deleted file mode 100644 index 5b50091..0000000 Binary files a/Semestr 4/sieci/warsztaty1/w1.pdf and /dev/null differ diff --git a/Semestr 4/sieci/warsztaty2/w2.pdf b/Semestr 4/sieci/warsztaty2/w2.pdf deleted file mode 100644 index 60cdc33..0000000 Binary files a/Semestr 4/sieci/warsztaty2/w2.pdf and /dev/null differ diff --git a/Semestr 4/sieci/warsztaty3/w3.pdf b/Semestr 4/sieci/warsztaty3/w3.pdf deleted file mode 100644 index b0266ad..0000000 Binary files a/Semestr 4/sieci/warsztaty3/w3.pdf and /dev/null differ diff --git a/Semestr 4/sieci/warsztaty4/w4.pdf b/Semestr 4/sieci/warsztaty4/w4.pdf deleted file mode 100644 index 1d6b54d..0000000 Binary files a/Semestr 4/sieci/warsztaty4/w4.pdf and /dev/null differ diff --git a/Semestr 4/sieci/warsztaty5/w5.pdf b/Semestr 4/sieci/warsztaty5/w5.pdf deleted file mode 100644 index e55fb7b..0000000 Binary files a/Semestr 4/sieci/warsztaty5/w5.pdf and /dev/null differ diff --git a/Semestr 4/sieci/warsztaty6/w6.pdf b/Semestr 4/sieci/warsztaty6/w6.pdf deleted file mode 100644 index 2f6b6c9..0000000 Binary files a/Semestr 4/sieci/warsztaty6/w6.pdf and /dev/null differ diff --git a/Semestr 4/sieci/wyklady/lec1.pdf b/Semestr 4/sieci/wyklady/lec1.pdf deleted file mode 100644 index 5da8f03..0000000 Binary files a/Semestr 4/sieci/wyklady/lec1.pdf and /dev/null differ diff --git a/Semestr 4/sieci/wyklady/lec2.pdf b/Semestr 4/sieci/wyklady/lec2.pdf deleted file mode 100644 index edb1398..0000000 Binary files a/Semestr 4/sieci/wyklady/lec2.pdf and /dev/null differ diff --git a/Semestr 4/sieci/wyklady/lec3.pdf b/Semestr 4/sieci/wyklady/lec3.pdf deleted file mode 100644 index ae48ec2..0000000 Binary files a/Semestr 4/sieci/wyklady/lec3.pdf and /dev/null differ diff --git a/Semestr 4/sieci/wyklady/lec4.pdf b/Semestr 4/sieci/wyklady/lec4.pdf deleted file mode 100644 index 352b71d..0000000 Binary files a/Semestr 4/sieci/wyklady/lec4.pdf and /dev/null differ diff --git a/Semestr 4/sieci/wyklady/lec5.pdf b/Semestr 4/sieci/wyklady/lec5.pdf deleted file mode 100644 index 3a7b39b..0000000 Binary files a/Semestr 4/sieci/wyklady/lec5.pdf and /dev/null differ diff --git a/Semestr 4/sieci/wyklady/lec6.pdf b/Semestr 4/sieci/wyklady/lec6.pdf deleted file mode 100644 index 04975ce..0000000 Binary files a/Semestr 4/sieci/wyklady/lec6.pdf and /dev/null differ diff --git a/Semestr 4/sieci/wyklady/lec7.pdf b/Semestr 4/sieci/wyklady/lec7.pdf deleted file mode 100644 index 0b28c4d..0000000 Binary files a/Semestr 4/sieci/wyklady/lec7.pdf and /dev/null differ diff --git a/Semestr 4/sieci/wyklady/lec8.pdf b/Semestr 4/sieci/wyklady/lec8.pdf deleted file mode 100644 index a4fbb9c..0000000 Binary files a/Semestr 4/sieci/wyklady/lec8.pdf and /dev/null differ -- cgit v1.2.3