aboutsummaryrefslogtreecommitdiff
path: root/Semestr 4/sieci/pracownia2/router
diff options
context:
space:
mode:
Diffstat (limited to 'Semestr 4/sieci/pracownia2/router')
-rw-r--r--Semestr 4/sieci/pracownia2/router/config.h4
-rw-r--r--Semestr 4/sieci/pracownia2/router/dist_vector.c20
-rw-r--r--Semestr 4/sieci/pracownia2/router/linked_list.c2
-rw-r--r--Semestr 4/sieci/pracownia2/router/network_addr.h2
-rw-r--r--Semestr 4/sieci/pracownia2/router/router.c29
-rw-r--r--Semestr 4/sieci/pracownia2/router/utils.c39
-rw-r--r--Semestr 4/sieci/pracownia2/router/utils.h3
7 files changed, 56 insertions, 43 deletions
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 <stdio.h>
+#include <time.h>
+#include <string.h>
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);