From 7dd4ef3c7c724badfe66a027ea3289d12ec0da46 Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Thu, 22 Apr 2021 21:14:19 +0200 Subject: Ostatnie poprawki i testy routera --- Semestr 4/ask/lista7/eval.s | 18 +++++++++++ Semestr 4/ask/lista7/wrap.s | 18 +++++++++++ Semestr 4/ask/lista7/zad5.c | 38 ++++++++++++++++++++++ Semestr 4/ask/lista7/zad5.s | 0 Semestr 4/ask/lista7/zad7.cpp | 19 +++++++++++ Semestr 4/pstwo/wyklady/Wyklad 8.pdf | Bin 0 -> 2762167 bytes Semestr 4/sieci/pracownia2/router/config.h | 4 +-- Semestr 4/sieci/pracownia2/router/dist_vector.c | 20 ++++++++---- Semestr 4/sieci/pracownia2/router/linked_list.c | 2 ++ Semestr 4/sieci/pracownia2/router/network_addr.h | 2 +- Semestr 4/sieci/pracownia2/router/router.c | 29 ----------------- Semestr 4/sieci/pracownia2/router/utils.c | 39 ++++++++++++++++++++--- Semestr 4/sieci/pracownia2/router/utils.h | 3 ++ 13 files changed, 149 insertions(+), 43 deletions(-) create mode 100644 Semestr 4/ask/lista7/eval.s create mode 100644 Semestr 4/ask/lista7/wrap.s create mode 100644 Semestr 4/ask/lista7/zad5.c create mode 100644 Semestr 4/ask/lista7/zad5.s create mode 100644 Semestr 4/ask/lista7/zad7.cpp create mode 100644 Semestr 4/pstwo/wyklady/Wyklad 8.pdf diff --git a/Semestr 4/ask/lista7/eval.s b/Semestr 4/ask/lista7/eval.s new file mode 100644 index 0000000..f7340a0 --- /dev/null +++ b/Semestr 4/ask/lista7/eval.s @@ -0,0 +1,18 @@ + .global eval + + .text +eval: + movq %rdi, %rax + movq 16(%rsp), %rcx + movq 24(%rsp), %rdx + movq (%rdx), %rsi + movq %rcx, %rdx + imulq %rsi, %rdx + movq %rdx, (%rdi) + movq 8(%rsp), %rdx + movq %rdx, %rdi + subq %rsi, %rdi + movq %rdi, 8(%rax) + subq %rcx, %rdx + movq %rdx, 16(%rax) + ret \ No newline at end of file diff --git a/Semestr 4/ask/lista7/wrap.s b/Semestr 4/ask/lista7/wrap.s new file mode 100644 index 0000000..6f01dfc --- /dev/null +++ b/Semestr 4/ask/lista7/wrap.s @@ -0,0 +1,18 @@ + .global wrap + + .text +wrap: + subq $72, %rsp + movq %rdx, (%rsp) + movq %rsp, %rdx + leaq 8(%rsp), %rax + pushq %rdx + pushq %rsi + pushq %rdi + movq %rax, %rdi + call eval + movq 40(%rsp), %rax + addq 32(%rsp), %rax + imulq 48(%rsp), %rax + addq $96, %rsp + ret \ No newline at end of file diff --git a/Semestr 4/ask/lista7/zad5.c b/Semestr 4/ask/lista7/zad5.c new file mode 100644 index 0000000..d65cb4f --- /dev/null +++ b/Semestr 4/ask/lista7/zad5.c @@ -0,0 +1,38 @@ +#include + +typedef struct A { + long u[2]; + long *v; +} SA; + +typedef struct B { + long p[2]; + long q; +} SB; + +SB eval(SA s); +long wrap(long x, long y, long z); + +SB eval_decoded(SA a) { + SB ret; + ret.p[0] = a.u[1] * (*a.v); + ret.p[1] = a.u[0] - (*a.v); + ret.q = a.u[0] - a.u[1]; + return ret; +} + +long wrap_decoded(long x, long y, long z) { + SA a; + a.v = &z; + a.u[0] = x; + a.u[1] = y; + SB b = eval_decoded(a); + long result = (b.p[1] + b.p[0]) * b.q; + return result; +} + +int main() { + printf("%ld\n", wrap(15, 16, 17)); + printf("%ld\n", wrap_decoded(15, 16, 17)); + return 0; +} \ No newline at end of file diff --git a/Semestr 4/ask/lista7/zad5.s b/Semestr 4/ask/lista7/zad5.s new file mode 100644 index 0000000..e69de29 diff --git a/Semestr 4/ask/lista7/zad7.cpp b/Semestr 4/ask/lista7/zad7.cpp new file mode 100644 index 0000000..073224f --- /dev/null +++ b/Semestr 4/ask/lista7/zad7.cpp @@ -0,0 +1,19 @@ +struct Base { + Base(int n) : data(n) {} + int data; + virtual int doit(int n) { return n - data; } +}; +struct Derived : Base { + Derived(int n) : Base(n + 1) {} + int doit(int n) { return n * data; } +}; + +int doit(Base *bp) { + return bp->doit(1); +} + +int main(int argc, char *argv[]) { + Base b = Base(10); + Derived d = Derived(20); + return doit(&b) + doit(&d); +} \ No newline at end of file diff --git a/Semestr 4/pstwo/wyklady/Wyklad 8.pdf b/Semestr 4/pstwo/wyklady/Wyklad 8.pdf new file mode 100644 index 0000000..a635d14 Binary files /dev/null and b/Semestr 4/pstwo/wyklady/Wyklad 8.pdf differ diff --git a/Semestr 4/sieci/pracownia2/router/config.h b/Semestr 4/sieci/pracownia2/router/config.h index ef8bf47..f83c556 100644 --- a/Semestr 4/sieci/pracownia2/router/config.h +++ b/Semestr 4/sieci/pracownia2/router/config.h @@ -5,11 +5,11 @@ #define TURN_LEN_S 5 #define INFINITY_DIST 16 -#define REACHABILITY_WAIT_TIME 5 +#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 \ No newline at end of file +#endif diff --git a/Semestr 4/sieci/pracownia2/router/dist_vector.c b/Semestr 4/sieci/pracownia2/router/dist_vector.c index f1c8c67..15ad851 100644 --- a/Semestr 4/sieci/pracownia2/router/dist_vector.c +++ b/Semestr 4/sieci/pracownia2/router/dist_vector.c @@ -1,5 +1,7 @@ #include "dist_vector.h" #include +#include +#include bool is_connected_directly(struct vector_item item) { return item.is_connected_directly; @@ -60,10 +62,8 @@ void update_dv_new_item(list_t *distance_vector, struct vector_item new_item) { current->distance = INFINITY_DIST; current->reachable = 0; } - } else if (new_item.distance < INFINITY_DIST) { - if (current->distance >= INFINITY_DIST) { - current->distance = new_item.distance; - } + } else { + current->distance = new_item.distance; current->reachable = 0; } } @@ -73,13 +73,21 @@ void update_dv_new_item(list_t *distance_vector, struct vector_item new_item) { iterate(distance_vector); } - if (new_entry) { + if (new_entry && new_item.reachable < INFINITY_DIST) { insert(distance_vector, &new_item, sizeof(new_item)); } } void print_dv(list_t *distance_vector) { - printf("Distance vector:\n"); + 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]; diff --git a/Semestr 4/sieci/pracownia2/router/linked_list.c b/Semestr 4/sieci/pracownia2/router/linked_list.c index 09f6025..c0f1930 100644 --- a/Semestr 4/sieci/pracownia2/router/linked_list.c +++ b/Semestr 4/sieci/pracownia2/router/linked_list.c @@ -9,6 +9,8 @@ node_t *_next(node_t *node) { 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; } diff --git a/Semestr 4/sieci/pracownia2/router/network_addr.h b/Semestr 4/sieci/pracownia2/router/network_addr.h index 616c64f..6347bbd 100644 --- a/Semestr 4/sieci/pracownia2/router/network_addr.h +++ b/Semestr 4/sieci/pracownia2/router/network_addr.h @@ -16,7 +16,7 @@ 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 */ +/* Returns network address of a given network. */ struct in_addr get_network_address(struct network_addr na); /* Prints network_addr via stdio. */ diff --git a/Semestr 4/sieci/pracownia2/router/router.c b/Semestr 4/sieci/pracownia2/router/router.c index 7262ee6..3831aac 100644 --- a/Semestr 4/sieci/pracownia2/router/router.c +++ b/Semestr 4/sieci/pracownia2/router/router.c @@ -28,34 +28,6 @@ uint16_t read_configuration(struct network_addr** networks, uint16_t **dists) { return n; } -void listen_for_routers(int sockfd, int timeout, int networks_number, struct network_addr *networks, uint16_t *dists, list_t *dv) { - // printf("Listening for %dms.\n", timeout); - char buffer[IP_MAXPACKET + 1]; - struct sockaddr_in sender; - - while (poll_socket_modify_timeout(sockfd, &timeout)) { - size_t buf_len = recv_message(sockfd, buffer, &sender); - struct vector_item new_item = parse_message(buffer, &sender); - // char addr[20]; - // inet_ntop(AF_INET, &sender.sin_addr, addr, sizeof(addr)); - // printf("Via ip: %s\n", addr); - - if (!is_from_network(sender.sin_addr, new_item.network)) { - for (int i = 0; i < networks_number; i++) { - if (is_from_network(sender.sin_addr, networks[i])) { - new_item.distance += dists[i]; - break; - } - } - new_item.is_connected_directly = false; - } - - update_dv_new_item(dv, new_item); - } - update_dv_reachability(dv); - // printf("Finished listening\n\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); @@ -72,7 +44,6 @@ 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); diff --git a/Semestr 4/sieci/pracownia2/router/utils.c b/Semestr 4/sieci/pracownia2/router/utils.c index 3f9078e..b975934 100644 --- a/Semestr 4/sieci/pracownia2/router/utils.c +++ b/Semestr 4/sieci/pracownia2/router/utils.c @@ -38,8 +38,8 @@ long get_time_interval(struct timespec start, struct timespec finish) { int poll_socket_modify_timeout(int sockfd, int *timeout) { if (*timeout < 0) { - fprintf(stderr, "poll_socket_modify_timeout: timeout is negative.\n"); - exit(EXIT_FAILURE); + *timeout = 0; + return 0; } struct pollfd fds; @@ -49,9 +49,11 @@ int poll_socket_modify_timeout(int sockfd, int *timeout) { fds.fd = sockfd; fds.events = POLLIN; fds.revents = 0; - clock_gettime(CLOCK_REALTIME, &start); + 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); @@ -61,7 +63,6 @@ int poll_socket_modify_timeout(int sockfd, int *timeout) { return 0; } - clock_gettime(CLOCK_REALTIME, &finish); *timeout -= get_time_interval(start, finish); return result; } @@ -153,12 +154,40 @@ int _send_item(int sockfd, struct network_addr network, struct vector_item item) return result; } +void listen_for_routers(int sockfd, int timeout, int networks_number, struct network_addr *networks, uint16_t *dists, list_t *dv) { + // printf("Listening for %dms.\n", timeout); + char buffer[IP_MAXPACKET + 1]; + struct sockaddr_in sender; + + while (poll_socket_modify_timeout(sockfd, &timeout)) { + size_t buf_len = recv_message(sockfd, buffer, &sender); + struct vector_item new_item = parse_message(buffer, &sender); + // char addr[20]; + // inet_ntop(AF_INET, &sender.sin_addr, addr, sizeof(addr)); + // printf("Via ip: %s\n", addr); + + if (!is_from_network(sender.sin_addr, new_item.network)) { + for (int i = 0; i < networks_number; i++) { + if (is_from_network(sender.sin_addr, networks[i])) { + new_item.distance += dists[i]; + break; + } + } + new_item.is_connected_directly = false; + } + + update_dv_new_item(dv, new_item); + } + update_dv_reachability(dv); + // printf("Finished listening\n\n"); +} + 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)) { + 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); diff --git a/Semestr 4/sieci/pracownia2/router/utils.h b/Semestr 4/sieci/pracownia2/router/utils.h index 3642779..edf90d0 100644 --- a/Semestr 4/sieci/pracownia2/router/utils.h +++ b/Semestr 4/sieci/pracownia2/router/utils.h @@ -39,6 +39,9 @@ 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); -- cgit v1.2.3