From fb35033502e2ca6313bdf0922f94ecb9a084958c Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 1 Apr 2020 17:28:15 +0200 Subject: [PATCH 01/59] =?UTF-8?q?Ajout=20d'une=20fonction=20de=20d=C3=A9bu?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/node.c | 8 ++++++++ src/node.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/src/node.c b/src/node.c index 499651d..d90071d 100644 --- a/src/node.c +++ b/src/node.c @@ -13,6 +13,12 @@ /* ---- Fonctions utilitaires ---- */ +void debug_print(char message_debug){ + if (debug_flag == 1) { + printf("\x1b[33m\x1b[4m>> Debug :\x1b[0m\x1b[33m %s\x1b[0m\n", message_debug ); + } +} + // Get list length int len_list(list *l) { int len = 0; @@ -292,6 +298,8 @@ void listen_for_packets(){ int main(int argc, const char *argv[]) { int cont = 1; + int debug_flag = 0; + while(cont){ diff --git a/src/node.h b/src/node.h index d4ba94b..be84adf 100644 --- a/src/node.h +++ b/src/node.h @@ -88,6 +88,8 @@ void t_get_network_state(); // Helper functions int len_list(list *l); +void debug_print(char message_debug); + neighbour_peer *get_random_neighbour(); #endif From 03e5b69553edddfc61da257489f900f8dc50dc85 Mon Sep 17 00:00:00 2001 From: n07070 Date: Sat, 18 Apr 2020 15:02:55 +0200 Subject: [PATCH 02/59] Added comments and changed signature or t_* functions --- src/node.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/node.h b/src/node.h index ba28d61..4e7ec5f 100644 --- a/src/node.h +++ b/src/node.h @@ -91,12 +91,20 @@ int send_tlv(tlv *tlv_to_send, int16_t length, struct sockaddr_in6 * dest_list, */ int send_tlvs(struct list * tlv_list, int16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num); -// threaded functions -int t_ask_for_more_peers(); +/* Check our peer list. If we have less than 5 peers, send out a + TLV NEIGHBOUR_REQUEST to a random peer +*/ +int t_ask_for_more_peers(list * neighbourhood); -int t_update_neighbours(); +/* We look at every peer, if he is marked as is_temporary, AND we didn't get a + packet from him in the last 70 sec, we remove him from the list. +*/ +int t_update_neighbours(list * neighbourhood); -int t_get_network_state(); +/* We send out a TLV Network Hash to every peer, and we expect getting a TLV + Network state from each of them. +*/ +int t_get_network_state(list * neighbourhood); // This function adds a message to the message table. int add_message(char * message, int message_len); From d6a2ca7c3bbb69e74ff15a82b5567bcd8f0e4f2d Mon Sep 17 00:00:00 2001 From: n07070 Date: Sat, 18 Apr 2020 15:24:11 +0200 Subject: [PATCH 03/59] Modified signature for t_* functions, run_node and bootstrap_node --- src/node.c | 40 ++++++++++++++++++++++++++++++---------- src/node.h | 11 +++++++---- src/node.o | Bin 17064 -> 17432 bytes 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/node.c b/src/node.c index a5d39e7..5d10615 100644 --- a/src/node.c +++ b/src/node.c @@ -718,19 +718,19 @@ int listen_for_packets(char * received_data_buffer[], int received_data_len, str } -int t_ask_for_more_peers(){ +int t_ask_for_more_peers(list * neighbourhood, int sock_fd){ return 0; } -int t_get_network_state(){ +int t_get_network_state(list * neighbourhood, int sock_fd){ return 0; } -int t_update_neighbours(){ +int t_update_neighbours(list * neighbourhood){ return 0; } -int run_node(int sock_fd){ +int run_node(int sock_fd, list * neighbourhood){ printf(">> Running node...\n"); int ret; @@ -756,11 +756,11 @@ int run_node(int sock_fd){ if (time(NULL) >= delay) { printf(">> Asking for more peers...\n"); - t_ask_for_more_peers(); + t_ask_for_more_peers(neighbourhood, sock_fd); printf(">> Updating neighbours...\n"); - t_update_neighbours(); + t_update_neighbours(neighbourhood); printf(">> Getting network state...\n"); - t_get_network_state(); + t_get_network_state(neighbourhood, sock_fd); delay = time(NULL) + 20 + (rand() % 10); } @@ -848,7 +848,9 @@ int run_node(int sock_fd){ return 0; } -int bootstrap_node(int * sock_fd){ + +// This function runs once, and sets the sock_fd as well as the neighbourhood +int bootstrap_node(int * sock_fd, list * neighbourhood){ printf(">> Boostraping node...\n"); struct sockaddr_in6 server_addr; @@ -870,6 +872,22 @@ int bootstrap_node(int * sock_fd){ return -2; } + /* Make the first peer*/ + struct neighbour_peer root_peer; + struct timeval root_peer_seen; + + int inet_p = inet_pton(AF_INET6, ROOT_PEER_ADDR, &root_peer.ip); + if(inet_p < 1){ + perror(">> Failed to create the root peer."); + return -3; + } + + root_peer.port = 1212; + root_peer.is_temporary = 0; + root_peer.last_seen = root_peer_seen; + + // TODO: Add the first peer to the neighbourhood + printf(">> Boostraping done.\n"); return 0; } @@ -878,9 +896,11 @@ int bootstrap_node(int * sock_fd){ int main(int argc, const char *argv[]) { printf(">> Starting node\n"); + list neighbourhood; + int sock_fd; - bootstrap_node(&sock_fd); - run_node(sock_fd); + bootstrap_node(&sock_fd, &neighbourhood); + run_node(sock_fd, &neighbourhood); close(sock_fd); return 0; diff --git a/src/node.h b/src/node.h index 4e7ec5f..065db07 100644 --- a/src/node.h +++ b/src/node.h @@ -59,6 +59,9 @@ typedef struct list { // The neighbour table has 15 entries #define NEIGHBOUR_MAX 15 +// The adress of the main peer +#define ROOT_PEER_ADDR "2001:660:3301:9200::51c2:1b9b" + // TODO // fonctions signatures @@ -79,7 +82,7 @@ int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, in int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num); // This function is in charge of saying how and what goes where. -int run_node(int sock_fd); +int run_node(int sock_fd, list * neighbourhood); /* Takes a TLV and sends it over to everyone in the list of addresses. * Returns -1 in case of error, 0 otherwise. @@ -94,7 +97,7 @@ int send_tlvs(struct list * tlv_list, int16_t length, struct sockaddr_in6 * dest /* Check our peer list. If we have less than 5 peers, send out a TLV NEIGHBOUR_REQUEST to a random peer */ -int t_ask_for_more_peers(list * neighbourhood); +int t_ask_for_more_peers(list * neighbourhood, int sock_fd); /* We look at every peer, if he is marked as is_temporary, AND we didn't get a packet from him in the last 70 sec, we remove him from the list. @@ -104,14 +107,14 @@ int t_update_neighbours(list * neighbourhood); /* We send out a TLV Network Hash to every peer, and we expect getting a TLV Network state from each of them. */ -int t_get_network_state(list * neighbourhood); +int t_get_network_state(list * neighbourhoodn, int sock_fd); // This function adds a message to the message table. int add_message(char * message, int message_len); // This functions creates the structures needed for the rest of the project, // creates the socket, and returns all of it. -int bootstrap_node(int * sock_fd); +int bootstrap_node(int * sock_fd, list * neighbourhood); // Helper functions int len_list(list *l); diff --git a/src/node.o b/src/node.o index 21df733281a8ca41dcd90bb128e14e188e5dfdb6..4a48b951cb62ef2e837c3d4b971c6f24c7f8e5f3 100644 GIT binary patch delta 2763 zcmZ8jU2GIp6rPz<(k;8~ZntHahRV`zp?|xxKizG+WoNskz_gUQP>Bc>x@_0b(9%-2 ziHRZs#DL8P$Wa3iU|&dh(O6du29Tnl7z1Jm(FY))U`Vur1fCG;xp!v5T~0FJoO{l9 zzI*PS-xGWJ!~6JyRl=OeM9mC)VXRLbn^9TCah$rp&8b4zK6hr}zK?MX>+Ify=Bnw} zoDMY|z$kYvAK^an_D;Urf9lK(tW1Z=`1>}ek9!kUJn9c^&PEavXDp zs^3g!9F41cF2vw-`vSWzL20L+O;1n%m4PcoW%;L`6aN6lGf(aJa~2XUPEj079*N(;VP26)mvt5xNR`g$wxw)@VUqbQb5p@a-I} zEJ;qtd0$vw&?3Z;vzTzeRxD*sl-q7+(fgAQ7dRgNA%hbs=tb20=yNXv_)t3=4zB(M7SI zVk^Z?ialVf6!~H5k3zIkls>?9k-fQEm_6e$*oN6J^z1`Ddsp9u(^O%S;w6fgDPE`e zCqe0fUVFPpQ>Hf;y3Z*m7Y;2D`Fw&>F)p)+)0d14dW+#%!tAwDzn61&F7`DL^n0k^ zNPS;sWMRF~+`l!M+BlpV+R6>a`?qq5fuZdvplXwH_)g&bc>ISCFR>UCuZNq}A|HS! z)v`q15eDZQhSLb)8kui^!5Y!Ra%Mq1D8h*vGYkaHFj?cI=Z7^;X%VATsb4_SMHpPQ zGT#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;5|_KL{fdLa8Y^jA}Ip09F&;|CnC zar~6y_YAe$S%+fFpWhb@7r5q6uDQ=OV&jWV1ajtiTBl zOZf$An&21IB#_H$u88|%X##-`l7Pbj9dUx=cL817DCwuf@iBc8%Ib9zfzEnL*2CWV z4$=;j7{_3)Ue`LLswx*waImTvxOJVRA+A&M7;MpXi0Ll(vZRQ^80K`isO#F}Sds1Z zpqqJEE*V%tmre)-sTM;ntEm;YkFC@NJ23l%lpU6`F&GZAQ=16pp2$r}6NS4$3dce& zw<&HW^KFB&kWRXx6EOkD5i>B!;~ztmL}4+cYu(bSMj=jcu-XvB4XPO!WjjvgWcOoM zzE@|Y>{b{vDCvQVhOT8KhfEPpa4`Q(uw<~V0%6wS4vxbdKMdzOzbRFP;cl3cZIIWX zlc%7uA=kfF{HB=yb~xTZNiV#L$_&gQcEcj-<^8^#qmMT-{g%dD&v$b)V_b9b0nJyE zCJIZ9Y^{Jv;g0DVX!o)mTYG*6pV`yw7CieI)k^$UNiX+$NaD9k`lAxxEAb~Sen4Rh ztdkN$Qi1$(UXyrvfj1;xUSLA-%-=8hpO^F*W%YkMz9J=}Qb82P{d9Uha>F(tz978k zI!W><#%!<`D<4WVXD$9|jI)QUOZ*mzUoY`}62IBvSMUX*(;H)9n|jp?M-yhjCH8V~ z+;GAB2~%ChuV%?3&{u8;SJFom2qhDU{fI7@N+#416Bd%q&i6{JvM_8&MVz;(HKXtX z&J@j>ZG+3Hh`Ol>7E;Y>p9V%tv-6Ztbbil4Xm3eF0_A2#@_#Z+%K rfzjIh@JN|uS?aW&>_XPVsn$A&I3g!pZjCtavHw6g@DqjZuB7W9Dgc?^ From 543a2a266e4fbcced36e8da89e3575e159aa0e7a Mon Sep 17 00:00:00 2001 From: gonzalef Date: Fri, 24 Apr 2020 19:07:50 +0200 Subject: [PATCH 04/59] compilation errors --- src/hash.o | Bin 2648 -> 0 bytes src/node.c | 8 ++++---- src/node.h | 4 ++-- src/node.o | Bin 17432 -> 0 bytes src/parser.o | Bin 2752 -> 0 bytes src/tlv.o | Bin 5064 -> 0 bytes 6 files changed, 6 insertions(+), 6 deletions(-) delete mode 100644 src/hash.o delete mode 100644 src/node.o delete mode 100644 src/parser.o delete mode 100644 src/tlv.o diff --git a/src/hash.o b/src/hash.o deleted file mode 100644 index 920c6ac2f74eb242bea0fe66f497943b0cf91af1..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2648 zcmbtU&2Jk;6d$i$XK~V9C*|O(Nbw;-4x%fYwonxbyh-eg(W*g>O+jf`9LL5eiH+=C zHK3}E4}$X{GdnkdWZeTLm=bL!^p`jEEZEjAxS3){ziAY4*MO z&F_8en~!TLBQ5!Sh>H)sf(~XJMd;1013O90B`K^4hnx-UUXfZE z=L%}!=G8vjc+koWwTz*!M}2G9`F`!8e;eOiFKIlVD^V{uEA8q|7VSc;MN){fq29_$ zk4pr6Ebw#K`Dp?>+qm)D*%|Bx24g_RPHHo;s;+byO&r3F)#HO(en5wAW9R2h5)C(V ztA_Ipb{^1aNv)<D>bWmlTv`v};=A^dBH;HaFhm$d>_rd6{m1yrb33VF+%$y@n0Of7TF z7$4RRb3C1%NM+5eK5V2+G=cRO&b)|Bvu@=Jvu2?@YnJlw&G8r{II0!%b8}UeqEfUj zS8KCqzKxY?#UfOO(0%$vNCN#7jNQGqJYqOG2|WUSjQuykj|zA~z*Paa1-w_lKW8|} z8xruF0_ETqvb*QV=rq!p_3^Ti5am8waU^Zw99_?#dd7)cnSEVCzIi-F{fkv#m~M;QJr z!@V!ks3$L@(-_<)`gs^6=7051fG?>l4nq{4|2Yu^n;oxxeNMncYUN+nKhFHU^!FL? ztr!$+-aih%21cj;FR~3K?-LQyfAU&$f8KxcKi=E`p(JbQ2K!-hSBbCJpTza{9v~#M a{l88og7E%vh`bG*{9CO4w>Xh$um3+O42qfn diff --git a/src/node.c b/src/node.c index 5d10615..cc010ff 100644 --- a/src/node.c +++ b/src/node.c @@ -468,7 +468,7 @@ int validate_tlv(char *data, int pos, int16_t packet_len){ // For every packet recivied, // then we make sure it's conform // We then extract the data from it to make it easy to work with -int check_header(char * received_data_buffer[], int received_data_len, struct packet * packet_to_return){ +int check_header(char * received_data_buffer, int received_data_len, struct packet * packet_to_return){ packet_to_return = (packet*) received_data_buffer; @@ -692,7 +692,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, return 0; } -int listen_for_packets(char * received_data_buffer[], int received_data_len, struct sockaddr_in6 * sender, int sock_fd){ +int listen_for_packets(char * received_data_buffer, int received_data_len, struct sockaddr_in6 * sender, int sock_fd){ // We verify the received packet is well formated, // and we return it in the struct designed to work with it. @@ -803,7 +803,7 @@ int run_node(int sock_fd, list * neighbourhood){ input_buffer[strcspn(input_buffer, "\n")] = 0; printf(">> Adding following message to the table : “%s”\n", input_buffer ); // Add message to the message table. - if (add_message(&input_buffer, bytes) < 0) { + if (add_message(input_buffer, bytes) < 0) { perror(">> Error while trying to add the message to the list of messages, please try again.."); } } @@ -833,7 +833,7 @@ int run_node(int sock_fd, list * neighbourhood){ if (bytes > 0) { printf("Received: %.*s\r", (int)bytes, output_buffer); // Treat incoming packets. - int work_tlv_status = listen_for_packets(&output_buffer, bytes, &sender, sock_fd); + int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd); if (work_tlv_status < 0) { perror(">> Error while treating the incoming packet."); } diff --git a/src/node.h b/src/node.h index 065db07..8c0542f 100644 --- a/src/node.h +++ b/src/node.h @@ -65,9 +65,9 @@ typedef struct list { // TODO // fonctions signatures -int listen_for_packets(char * received_data_buffer[], int received_data_len, struct sockaddr_in6 * sender, int sock_fd); +int listen_for_packets(char * received_data_buffer, int received_data_len, struct sockaddr_in6 * sender, int sock_fd); -int check_header(char * received_data_buffer[], int received_data_len, packet * packet_to_return); +int check_header(char * received_data_buffer, int received_data_len, packet * packet_to_return); int validate_tlv(char *data, int pos, int16_t packet_len); diff --git a/src/node.o b/src/node.o deleted file mode 100644 index 4a48b951cb62ef2e837c3d4b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+GlYnPX@)&L8D6kg$I17PFW1l>W*- Hjs5=@mKd(U diff --git a/src/parser.o b/src/parser.o deleted file mode 100644 index 0cdd296b3e6d45a1381d4f89dc493d8414779b6b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2752 zcmbtV&2Jl35FclqHVvuQDMbouA(n7Zis;I28fZBnZ$1~aYAYHem6B{6dtHarAN8)G z1qmABfL6h%NFc;5NQes@dT0;jfE*PO2@dqge^88&qNq}71tQG6eXm~DwuE3JzxmB? zKHl4TA9-g|pXv$(Ku7?dho&W1fXlm^IKf~7_Cg=1FRRIKzehryNuJi6KeXlFN?ib& zb9*xw4I-~?PB0Bxtr*l+Hhc8i7twW`BBQMZhlVM_sYY{{6R&GdL)(1+IxCO-q5I1l z8)$4aq&wPIx^r7wdxU(y(4ei>?U-J>7QKarUi&)wBayXH^2O^ItUKGf^DB#8xz{CE ze?Ui8E(W98S~XfnL++QfwU61<8Es@gTMGtEM8lf1LIIldv)n%(W?TeOgrOLs7()p} z35HG~I>pdDqIrf&h)Rg6(aTs%?q4Cr>?^KKirMR~O^Vr9U7M-ZEi|;{@2II~XVq8L zS#|EFJSe{O2DMmsw(fi>e_B5X)p|wYJ-E@!Jij+RGq@wjLF4XUJ3I1VmGFZ)-H5$) zzIw@@Qa}F8e2;O z(57S8cm-`X$B-2nIy<+gcNWU#1t^${=W+|hN*R&;MzOpUvFwy>LZO&3IV=?|8(?~T z{JF?*a-mYNE0JfFQ6+YGq{6KD+wo&c?C2qv#krX5=Q{A_OAxpi4jky)-E#$3f}aKl zx$g|EbSxD9uxl(dP?M%Zkq?4uX!yNuH56Of^L$5h=oIW={9a>kfZ z$Mi`9vgb_O0>fCeis|FW4a-WW3R$!*yPPgvGSayvW~91SbN!Do zg1OWp|BXl>M-BIbX_OClEY3!UIx%oq0)U;k4+UgQb=0?+q_!To>v{c}9O zC7d^8-d9-kU(ja|wA(-N#EX5R{i9rwFL?TnmbsCe=ktfTlkOzF`Tl&m+y1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K=&NfvE7O%xektd5_u-!k3#n*{72wJd>KIyy8hvR1U|(1x~KaO zuo;HqS%E_L7W`j_ua!UV0FFFom_FxUV7Nk`NrcdS1=yGC@O-}xev$EZA2f2yim|j`HcQPFH+XNiXeiYRA!N+-?KoA6R+QJnmJ0xs(J3%Kad>jEz7zb)XR{-*+dEsW3O zS`u)P=O+Odc@mgl5N96t3b?5Mnt;PwxH0Z&0mn0r`?D4t$ce zXC3%5YrpHj`8mAg!1+0h=Nbz4ho851J8*?LJK@03F#Jsi9%1=icH%5ABwRjRFw;Zu zHx2ur<26Iir^BW`VUjRCZo)(5G6@?cY@m8H+_|K9W4F4N#EYc`Csj$V2hl0H5ly*A0OWlqHzDYA2H}`W#7%7RCrd2 z!tMAU-wjxgh)PgD!0c~x;M|V?@gCrHQFV&hix}3Ox&?-@X= Date: Fri, 24 Apr 2020 20:23:57 +0200 Subject: [PATCH 05/59] check/add/update neighbours --- src/node.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/node.h | 7 ++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/node.c b/src/node.c index cc010ff..cb3e2fd 100644 --- a/src/node.c +++ b/src/node.c @@ -51,6 +51,63 @@ neighbour_peer *get_random_neighbour() { return (neighbour_peer*) tmp->data; } +// Search for this peer in the neighbour table +neighbour_peer *get_neighbour(struct in6_addr *ip) { + // Look for neighbour + list *tmp = neighbour_list; + neighbour_peer *peer; + + while(tmp != NULL) { + // check for same ip and same port + peer = (neighbour_peer*) tmp->data; + + if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0) + return peer; + + // if they differ, get next peer + tmp = tmp->next; + } + + return NULL; +} + +// Return -1 if the peer could not be added, 0 if it could or if it was already in the table +int add_n_update_neighbour(struct in6_addr *ip) { + neighbour_peer *peer = get_neighbour(ip); + struct timeval curtime; + + // peer is not in the neighbour list so we try to add it + if(ip == NULL) { + // check if there are less than 15 neighbours + if(len_list(neighbour_list) == 15) + return -1; + + // if there are less, initialize the new peer to add to the list + peer = (neighbour_peer*) malloc(sizeof(neighbour_peer)); + memcpy(&peer->ip, ip, sizeof(struct in6_addr)); + peer->port = LISTEN_PORT; + peer->is_temporary = 1; + + // set last_seen time + gettimeofday(&curtime, NULL); + memcpy(&peer->last_seen, &curtime, sizeof(struct timeval)); + + // set new peer as head of list + list *node = (list*) malloc(sizeof(list)); + node->data = (void*) peer; + node->next = neighbour_list; + neighbour_list = node; + + return 0; + } + + // if the peer was already in the list, update it + gettimeofday(&curtime, NULL); + memcpy(&peer->last_seen, &curtime, sizeof(struct timeval)); + + return 0; +} + // get data associated with id, if it doesn't exist return NULL pub_data *get_data(int64_t id) { list *tmp = data_list; @@ -702,7 +759,11 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc return -1; } - // TODO : Add the neighbour check here. + // Neighbour check + if(add_n_update_neighbour(sender) == -1) { + fprintf(stderr, ">> Maximum number of neighbour peers reached, aborting this packet.\n"); + return -1; + } // struct tlv_list received_tlvs; // if (validate_tlvs(formated_rec_datagram) < 0) diff --git a/src/node.h b/src/node.h index 8c0542f..4f87d31 100644 --- a/src/node.h +++ b/src/node.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -121,6 +122,12 @@ int len_list(list *l); neighbour_peer *get_random_neighbour(); +// Search for this peer in the neighbour table +neighbour_peer *get_neighbour(struct in6_addr *ip); + +// Return -1 if the peer could not be added, 0 if it could or if it was already in the table +int add_n_update_neighbour(struct in6_addr *ip); + // get data associated with id, if it doesn't exist return NULL pub_data *get_data(int64_t id); From dac138c92a2b444e12b8b41d45f55c12391a9532 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Fri, 24 Apr 2020 21:45:09 +0200 Subject: [PATCH 06/59] update neighbours --- src/node.c | 97 ++++++++++++++++++++++++++++++++++++++++++------------ src/node.h | 7 ++-- 2 files changed, 79 insertions(+), 25 deletions(-) diff --git a/src/node.c b/src/node.c index cb3e2fd..81c5dd6 100644 --- a/src/node.c +++ b/src/node.c @@ -52,7 +52,7 @@ neighbour_peer *get_random_neighbour() { } // Search for this peer in the neighbour table -neighbour_peer *get_neighbour(struct in6_addr *ip) { +neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { // Look for neighbour list *tmp = neighbour_list; neighbour_peer *peer; @@ -61,7 +61,7 @@ neighbour_peer *get_neighbour(struct in6_addr *ip) { // check for same ip and same port peer = (neighbour_peer*) tmp->data; - if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0) + if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 && peer->port == port) return peer; // if they differ, get next peer @@ -72,9 +72,9 @@ neighbour_peer *get_neighbour(struct in6_addr *ip) { } // Return -1 if the peer could not be added, 0 if it could or if it was already in the table -int add_n_update_neighbour(struct in6_addr *ip) { - neighbour_peer *peer = get_neighbour(ip); - struct timeval curtime; +int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { + neighbour_peer *peer = get_neighbour(ip, port); + time_t curtime; // peer is not in the neighbour list so we try to add it if(ip == NULL) { @@ -89,8 +89,8 @@ int add_n_update_neighbour(struct in6_addr *ip) { peer->is_temporary = 1; // set last_seen time - gettimeofday(&curtime, NULL); - memcpy(&peer->last_seen, &curtime, sizeof(struct timeval)); + time(&curtime); + peer->last_seen = curtime; // set new peer as head of list list *node = (list*) malloc(sizeof(list)); @@ -102,8 +102,8 @@ int add_n_update_neighbour(struct in6_addr *ip) { } // if the peer was already in the list, update it - gettimeofday(&curtime, NULL); - memcpy(&peer->last_seen, &curtime, sizeof(struct timeval)); + time(&curtime); + peer->last_seen = curtime; return 0; } @@ -225,6 +225,65 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { /* ---- Fin fonctions utilitaires ---- */ +// Update the neighbour list +int update_neighbours() { + list *tmp = neighbour_list, *last = NULL, *node_to_delete; + neighbour_peer *peer; + time_t curtime; + int deleted = 0; + + // check every neighbour + while(tmp != NULL) { + peer = (neighbour_peer*) tmp->data; + + // Check if peer is temporary + if(peer->is_temporary) { + // If it's been 70 seconds or more since we last received a packet from this peer then remove it from the list + time(&curtime); + + if(difftime(peer->last_seen, curtime) >= 70) { + // increase the count of deleted nodes + deleted++; + + // If head of the list + if(last == NULL) { + // Store node to delete + node_to_delete = tmp; + + // Update list + tmp = tmp->next; + neighbour_list = tmp; + + // Free allocated memory + free(node_to_delete->data); + free(node_to_delete); + + continue; + } + + // Store node to delete + node_to_delete = tmp; + + // Update list + tmp = tmp->next; + last->next = tmp; + + // Free allocated memory + free(node_to_delete->data); + free(node_to_delete); + + continue; + } + } + + last = tmp; + tmp = tmp->next; + } + + // returns the amount of nodes that were deleted + return deleted; +} + // Add TLV to packet, if it does not fit then send the packet and reset the packet buff to be able to add more TLVs that will be sent afterwards int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { char type = tlv->pad1->type, sent = 0, errval = 0; @@ -550,13 +609,6 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack return 0; } -// If the sender is not in the neighbourhood, and we have 15 neighbours, we -// ignore the packet. Otherwise, we add him to the neighbourhood, marked as -// temporary. -int update_neighbours(){ - return 0; -}; - int add_message(char * message, int message_len){ return 0; } @@ -760,7 +812,10 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc } // Neighbour check - if(add_n_update_neighbour(sender) == -1) { + struct in6_addr ip = sender->sin6_addr; + int16_t port = sender->sin6_port; + + if(add_n_update_neighbour(&ip, port) == -1) { fprintf(stderr, ">> Maximum number of neighbour peers reached, aborting this packet.\n"); return -1; } @@ -788,7 +843,7 @@ int t_get_network_state(list * neighbourhood, int sock_fd){ } int t_update_neighbours(list * neighbourhood){ - return 0; + return update_neighbours(); } int run_node(int sock_fd, list * neighbourhood){ @@ -802,7 +857,7 @@ int run_node(int sock_fd, list * neighbourhood){ // Init the ~20s delay for node update. srand(time(NULL)); - int delay = time(NULL) + 20; + time_t delay = time(NULL) + 20; /* Descriptor zero is stdin */ fds[0].fd = 0; @@ -815,7 +870,7 @@ int run_node(int sock_fd, list * neighbourhood){ */ while (1) { - if (time(NULL) >= delay) { + if(time(NULL) >= delay) { printf(">> Asking for more peers...\n"); t_ask_for_more_peers(neighbourhood, sock_fd); printf(">> Updating neighbours...\n"); @@ -935,7 +990,7 @@ int bootstrap_node(int * sock_fd, list * neighbourhood){ /* Make the first peer*/ struct neighbour_peer root_peer; - struct timeval root_peer_seen; + time_t root_peer_seen = time(NULL); int inet_p = inet_pton(AF_INET6, ROOT_PEER_ADDR, &root_peer.ip); if(inet_p < 1){ diff --git a/src/node.h b/src/node.h index 4f87d31..a90470c 100644 --- a/src/node.h +++ b/src/node.h @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -23,7 +22,7 @@ typedef struct neighbour_peer { struct in6_addr ip; int16_t port; char is_temporary; - struct timeval last_seen; + time_t last_seen; } neighbour_peer; // The strucuture to hold the messages @@ -123,10 +122,10 @@ int len_list(list *l); neighbour_peer *get_random_neighbour(); // Search for this peer in the neighbour table -neighbour_peer *get_neighbour(struct in6_addr *ip); +neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port); // Return -1 if the peer could not be added, 0 if it could or if it was already in the table -int add_n_update_neighbour(struct in6_addr *ip); +int add_n_update_neighbour(struct in6_addr *ip, int16_t port); // get data associated with id, if it doesn't exist return NULL pub_data *get_data(int64_t id); From 481903c24a1ccf3ff3b847677f9ee7bbd371edf1 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Fri, 24 Apr 2020 22:48:10 +0200 Subject: [PATCH 07/59] ask for peers --- src/node.c | 62 ++++++++++++++++++++++++++++++++++++++++-------------- src/node.h | 12 ++++++----- 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/src/node.c b/src/node.c index 81c5dd6..c5613c1 100644 --- a/src/node.c +++ b/src/node.c @@ -12,7 +12,6 @@ #include #include #include - #include "node.h" // Static variables @@ -21,6 +20,39 @@ static list *neighbour_list; /* ---- Fonctions utilitaires ---- */ +// Looks for more peers +int ask_for_peers(int socket_num) { + // Only ask for more peers if the neighbour list is small enough + if(len_list(neighbour_list) >= 5) + return 0; + + // Get random peer + neighbour_peer *peer = get_random_neighbour(); + struct in6_addr ip = peer->ip; + int16_t port = peer->port; + + int ifindex = if_nametoindex("eth0"); + + if(ifindex == 0) { + perror("if_nametoindex failed"); + return -1; + } + + // Initialize sockaddr + struct sockaddr_in6 dest; + memset(&dest, 0, sizeof(struct sockaddr_in6)); + dest.sin6_family = AF_INET6; + memcpy(&dest.sin6_addr, &ip, 16); + dest.sin6_port = htons(port); + dest.sin6_scope_id = ifindex; + + // Send neighbour request TLV + tlv neighbour_req; + build_neighbour_req(&neighbour_req); + + return send_single_tlv(&neighbour_req, &dest, socket_num); +} + // Get list length int len_list(list *l) { int len = 0; @@ -834,19 +866,19 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc } -int t_ask_for_more_peers(list * neighbourhood, int sock_fd){ +int t_ask_for_more_peers(int sock_fd){ + return ask_for_peers(sock_fd); +} + +int t_get_network_state(int sock_fd){ return 0; } -int t_get_network_state(list * neighbourhood, int sock_fd){ - return 0; -} - -int t_update_neighbours(list * neighbourhood){ +int t_update_neighbours(){ return update_neighbours(); } -int run_node(int sock_fd, list * neighbourhood){ +int run_node(int sock_fd){ printf(">> Running node...\n"); int ret; @@ -872,11 +904,11 @@ int run_node(int sock_fd, list * neighbourhood){ if(time(NULL) >= delay) { printf(">> Asking for more peers...\n"); - t_ask_for_more_peers(neighbourhood, sock_fd); + t_ask_for_more_peers(sock_fd); printf(">> Updating neighbours...\n"); - t_update_neighbours(neighbourhood); + t_update_neighbours(); printf(">> Getting network state...\n"); - t_get_network_state(neighbourhood, sock_fd); + t_get_network_state(sock_fd); delay = time(NULL) + 20 + (rand() % 10); } @@ -966,7 +998,7 @@ int run_node(int sock_fd, list * neighbourhood){ // This function runs once, and sets the sock_fd as well as the neighbourhood -int bootstrap_node(int * sock_fd, list * neighbourhood){ +int bootstrap_node(int * sock_fd){ printf(">> Boostraping node...\n"); struct sockaddr_in6 server_addr; @@ -1012,11 +1044,9 @@ int bootstrap_node(int * sock_fd, list * neighbourhood){ int main(int argc, const char *argv[]) { printf(">> Starting node\n"); - list neighbourhood; - int sock_fd; - bootstrap_node(&sock_fd, &neighbourhood); - run_node(sock_fd, &neighbourhood); + bootstrap_node(&sock_fd); + run_node(sock_fd); close(sock_fd); return 0; diff --git a/src/node.h b/src/node.h index a90470c..08e083b 100644 --- a/src/node.h +++ b/src/node.h @@ -73,6 +73,8 @@ int validate_tlv(char *data, int pos, int16_t packet_len); int update_neighbours(); +int ask_for_peers(int socket_num); + int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, int socket_num); int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num); @@ -82,7 +84,7 @@ int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, in int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num); // This function is in charge of saying how and what goes where. -int run_node(int sock_fd, list * neighbourhood); +int run_node(int sock_fd); /* Takes a TLV and sends it over to everyone in the list of addresses. * Returns -1 in case of error, 0 otherwise. @@ -97,24 +99,24 @@ int send_tlvs(struct list * tlv_list, int16_t length, struct sockaddr_in6 * dest /* Check our peer list. If we have less than 5 peers, send out a TLV NEIGHBOUR_REQUEST to a random peer */ -int t_ask_for_more_peers(list * neighbourhood, int sock_fd); +int t_ask_for_more_peers(int sock_fd); /* We look at every peer, if he is marked as is_temporary, AND we didn't get a packet from him in the last 70 sec, we remove him from the list. */ -int t_update_neighbours(list * neighbourhood); +int t_update_neighbours(); /* We send out a TLV Network Hash to every peer, and we expect getting a TLV Network state from each of them. */ -int t_get_network_state(list * neighbourhoodn, int sock_fd); +int t_get_network_state(int sock_fd); // This function adds a message to the message table. int add_message(char * message, int message_len); // This functions creates the structures needed for the rest of the project, // creates the socket, and returns all of it. -int bootstrap_node(int * sock_fd, list * neighbourhood); +int bootstrap_node(int * sock_fd); // Helper functions int len_list(list *l); From 723466fba85101722655233d9661ed9b1555ac19 Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 27 Apr 2020 15:50:23 +0200 Subject: [PATCH 08/59] Modification de len_list --- src/node.c | 68 +++++++++++++++++++++++++++++------------------------- src/node.h | 3 ++- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/node.c b/src/node.c index c5613c1..d167a90 100644 --- a/src/node.c +++ b/src/node.c @@ -23,34 +23,40 @@ static list *neighbour_list; // Looks for more peers int ask_for_peers(int socket_num) { // Only ask for more peers if the neighbour list is small enough - if(len_list(neighbour_list) >= 5) + int nbr_peers = len_list(neighbour_list); + if( nbr_peers >= 5){ return 0; - - // Get random peer - neighbour_peer *peer = get_random_neighbour(); - struct in6_addr ip = peer->ip; - int16_t port = peer->port; - - int ifindex = if_nametoindex("eth0"); - - if(ifindex == 0) { - perror("if_nametoindex failed"); + } else if (nbr_peers <= 0){ + printf(">> No peers found in the peer list, something terrible happened.\n"); return -1; + } else { + + // Get random peer + neighbour_peer *peer = get_random_neighbour(); + struct in6_addr ip = peer->ip; + int16_t port = peer->port; + + int ifindex = if_nametoindex("eth0"); + if(ifindex == 0) { + perror("if_nametoindex failed"); + return -1; + } + + // Initialize sockaddr + struct sockaddr_in6 dest; + memset(&dest, 0, sizeof(struct sockaddr_in6)); + dest.sin6_family = AF_INET6; + memcpy(&dest.sin6_addr, &ip, 16); + dest.sin6_port = htons(port); + dest.sin6_scope_id = ifindex; + + // Send neighbour request TLV + tlv neighbour_req; + build_neighbour_req(&neighbour_req); + + return send_single_tlv(&neighbour_req, &dest, socket_num); + } - - // Initialize sockaddr - struct sockaddr_in6 dest; - memset(&dest, 0, sizeof(struct sockaddr_in6)); - dest.sin6_family = AF_INET6; - memcpy(&dest.sin6_addr, &ip, 16); - dest.sin6_port = htons(port); - dest.sin6_scope_id = ifindex; - - // Send neighbour request TLV - tlv neighbour_req; - build_neighbour_req(&neighbour_req); - - return send_single_tlv(&neighbour_req, &dest, socket_num); } // Get list length @@ -62,7 +68,6 @@ int len_list(list *l) { tmp = tmp->next; len++; } - return len; } @@ -70,9 +75,9 @@ int len_list(list *l) { neighbour_peer *get_random_neighbour() { // Get a random number time_t t; - srand((unsigned) time(&t)); + srand((unsigned) time(NULL)); int n = rand() % len_list(neighbour_list); - + printf("%i\n", n ); // Get nth neighbour list *tmp = neighbour_list; @@ -93,8 +98,9 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { // check for same ip and same port peer = (neighbour_peer*) tmp->data; - if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 && peer->port == port) + if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 && peer->port == port) { return peer; + } // if they differ, get next peer tmp = tmp->next; @@ -303,7 +309,7 @@ int update_neighbours() { // Free allocated memory free(node_to_delete->data); free(node_to_delete); - + continue; } } @@ -889,7 +895,7 @@ int run_node(int sock_fd){ // Init the ~20s delay for node update. srand(time(NULL)); - time_t delay = time(NULL) + 20; + time_t delay = time(NULL) + 2; /* Descriptor zero is stdin */ fds[0].fd = 0; diff --git a/src/node.h b/src/node.h index 08e083b..18bee64 100644 --- a/src/node.h +++ b/src/node.h @@ -53,7 +53,8 @@ typedef struct list { #define LISTEN_PORT 1212 // The node ID -#define NODE_ID 42675882021843277 +// #define NODE_ID 42675882021843277 +#define NODE_ID 42013376900101010 // The number of neighbours // The neighbour table has 15 entries From 822b344a57905320eacdb1aade834f39c5887d8a Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 27 Apr 2020 17:22:41 +0200 Subject: [PATCH 09/59] Fixing sending packets --- src/node.c | 67 +++++++++++++++++++++++++++++++++++------------------- src/tlv.c | 7 ++++-- 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/node.c b/src/node.c index d167a90..04652ea 100644 --- a/src/node.c +++ b/src/node.c @@ -36,10 +36,13 @@ int ask_for_peers(int socket_num) { struct in6_addr ip = peer->ip; int16_t port = peer->port; - int ifindex = if_nametoindex("eth0"); + int ifindex = if_nametoindex("enp3s0"); if(ifindex == 0) { - perror("if_nametoindex failed"); - return -1; + int ifindex = if_nametoindex("eth0"); + if(ifindex == 0) { + perror("if_nametoindex failed"); + return -1; + } } // Initialize sockaddr @@ -52,10 +55,20 @@ int ask_for_peers(int socket_num) { // Send neighbour request TLV tlv neighbour_req; - build_neighbour_req(&neighbour_req); - - return send_single_tlv(&neighbour_req, &dest, socket_num); + neighbour_req.pad1 = NULL; + int rc = build_neighbour_req(&neighbour_req); + if (rc < 0) { + printf(">> Failed to build neighbour_req\n"); + } + rc = send_single_tlv(&neighbour_req, &dest, socket_num); + if (rc < 0) { + printf(">> Error while sending a TLV.\n"); + return -1; + } else { + printf(">> Send TLV.\n"); + return 0; + } } } @@ -73,15 +86,16 @@ int len_list(list *l) { // Get a random neighbour neighbour_peer *get_random_neighbour() { + printf(">> Getting random peer...\n"); // Get a random number time_t t; srand((unsigned) time(NULL)); - int n = rand() % len_list(neighbour_list); - printf("%i\n", n ); + int n = (rand() % len_list(neighbour_list)) + 1; + int l_l = len_list(neighbour_list); // Get nth neighbour list *tmp = neighbour_list; - for(int i=0; inext; } @@ -408,7 +422,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // Send length bytes from packet int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num) { // Vectorized buffer - struct iovec vec_buff = {.iov_len = length, .iov_base = packet_buff}; + struct iovec vec_buff = {.iov_len = sizeof(packet_buff), .iov_base = packet_buff}; int error_while_sending = 0; @@ -422,17 +436,16 @@ int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, in int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0); if (response_code < 0) { - // debug_print("Unable to send out the packet to peer %i", i); + printf(">> Unable to send out the packet to peer.\n"); error_while_sending = 1; } else if (response_code < length) { - // debug_print("Sent out only part of the packet."); + printf(">> Sent out only part of the packet.\n"); error_while_sending = 1; } else { - // debug_print("Send out packet to peer %i", i); + printf(">> Send out packet to peer\n"); } if (error_while_sending == 1) { - // debug_print("Error occured while sending out a packet."); return -1; } else { return 0; @@ -666,11 +679,13 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, packet pack = (packet) {.magic = 95, .version = 1, .length = 0}; memset(pack.body, 0, 1020); - int ifindex = if_nametoindex("eth0"); - + int ifindex = if_nametoindex("enp3s0"); if(ifindex == 0) { - perror("if_nametoindex failed"); - return -1; + int ifindex = if_nametoindex("eth0"); + if(ifindex == 0) { + perror("if_nametoindex failed"); + return -1; + } } while(pos < packet_len) { @@ -985,7 +1000,7 @@ int run_node(int sock_fd){ break; } if (bytes > 0) { - printf("Received: %.*s\r", (int)bytes, output_buffer); + printf("Received %i bytes as : %s\n", (int)bytes, output_buffer); // Treat incoming packets. int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd); if (work_tlv_status < 0) { @@ -1027,20 +1042,24 @@ int bootstrap_node(int * sock_fd){ } /* Make the first peer*/ - struct neighbour_peer root_peer; + struct neighbour_peer * root_peer = (struct neighbour_peer *) malloc(sizeof(struct neighbour_peer)); time_t root_peer_seen = time(NULL); - int inet_p = inet_pton(AF_INET6, ROOT_PEER_ADDR, &root_peer.ip); + int inet_p = inet_pton(AF_INET6, ROOT_PEER_ADDR, &root_peer->ip); if(inet_p < 1){ perror(">> Failed to create the root peer."); return -3; } - root_peer.port = 1212; - root_peer.is_temporary = 0; - root_peer.last_seen = root_peer_seen; + root_peer->port = 1212; + root_peer->is_temporary = 0; + root_peer->last_seen = root_peer_seen; // TODO: Add the first peer to the neighbourhood + printf(">> Adding the first root peer to the list...\n"); + neighbour_list = malloc(sizeof(struct list)); + neighbour_list->data = (void *) root_peer; + neighbour_list->next = NULL; printf(">> Boostraping done.\n"); return 0; diff --git a/src/tlv.c b/src/tlv.c index 1f5b4e8..82de813 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -62,12 +62,15 @@ int build_padn(tlv *tlv, size_t len) { int build_neighbour_req(tlv *tlv) { // Free the previously allocated memory - free(tlv->pad1); + // if (tlv != NULL) { + // free(tlv->pad1); + // } neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req)); - if(new == NULL) + if(new == NULL){ return -1; + } new->type = 2; new->length = 0; From ec754aec24c4dc9754f0092321264768917c7eaf Mon Sep 17 00:00:00 2001 From: gonzalef Date: Mon, 27 Apr 2020 17:25:53 +0200 Subject: [PATCH 10/59] set pad1 to NULL --- src/node.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/node.c b/src/node.c index d167a90..07aa586 100644 --- a/src/node.c +++ b/src/node.c @@ -52,6 +52,7 @@ int ask_for_peers(int socket_num) { // Send neighbour request TLV tlv neighbour_req; + tlv.pad1 = NULL; build_neighbour_req(&neighbour_req); return send_single_tlv(&neighbour_req, &dest, socket_num); From eb4770e36ca82ca07446a90523c178463e71e315 Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 27 Apr 2020 17:53:53 +0200 Subject: [PATCH 11/59] Modifcations of the sendout of packets --- src/node.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/node.c b/src/node.c index 04652ea..db88f0b 100644 --- a/src/node.c +++ b/src/node.c @@ -421,8 +421,10 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // Send length bytes from packet int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num) { + ((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length); + // Vectorized buffer - struct iovec vec_buff = {.iov_len = sizeof(packet_buff), .iov_base = packet_buff}; + struct iovec vec_buff = {.iov_len = length, .iov_base = packet_buff}; int error_while_sending = 0; @@ -442,7 +444,7 @@ int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, in printf(">> Sent out only part of the packet.\n"); error_while_sending = 1; } else { - printf(">> Send out packet to peer\n"); + printf(">> Send out packet to peer, size : %i\n", response_code); } if (error_while_sending == 1) { @@ -519,6 +521,7 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { } // Send the packet + printf("(%i)\n", pack.length ); return send_packet((char*) &pack, pack.length, dest, socket_num); } @@ -652,7 +655,8 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack return -1; } - if (packet_to_return->length + 4 > received_data_len ) { + printf("packet : %i, %i \n", packet_to_return->length + 4,received_data_len ); + if (packet_to_return->length + 4 < received_data_len ) { perror(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics."); return -1; } @@ -1002,7 +1006,7 @@ int run_node(int sock_fd){ if (bytes > 0) { printf("Received %i bytes as : %s\n", (int)bytes, output_buffer); // Treat incoming packets. - int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd); + int work_tlv_status = listen_for_packets(&output_buffer, bytes, &sender, sock_fd); if (work_tlv_status < 0) { perror(">> Error while treating the incoming packet."); } From ecc168cbf4b81bd2111beb8d931691d163cf78d5 Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 27 Apr 2020 19:15:24 +0200 Subject: [PATCH 12/59] Debugging receiving packets, validate_tlv --- src/node.c | 75 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/src/node.c b/src/node.c index e5afe0f..d71c883 100644 --- a/src/node.c +++ b/src/node.c @@ -45,6 +45,8 @@ int ask_for_peers(int socket_num) { } } + ifindex = 0; + // Initialize sockaddr struct sockaddr_in6 dest; memset(&dest, 0, sizeof(struct sockaddr_in6)); @@ -125,39 +127,49 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { // Return -1 if the peer could not be added, 0 if it could or if it was already in the table int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { + + char * buff_str_ip[1024]; + char * ip_str = inet_ntop(AF_INET6,ip, buff_str_ip, 1024); + + printf(">> Found peer %s:%i.\n", ip_str, ntohs(port)); + // We try to find a peer with this address and port. neighbour_peer *peer = get_neighbour(ip, port); time_t curtime; - // peer is not in the neighbour list so we try to add it - if(ip == NULL) { + if (peer == NULL) { + printf(">> We don't know this peer yet\n"); // check if there are less than 15 neighbours - if(len_list(neighbour_list) == 15) + if(len_list(neighbour_list) >= 15){ return -1; + } else { + printf(">> Adding them to the peer table.\n"); + // if there are less, initialize the new peer to add to the list + peer = (neighbour_peer*) malloc(sizeof(neighbour_peer)); + memcpy(&peer->ip, ip, sizeof(struct in6_addr)); + peer->port = LISTEN_PORT; + peer->is_temporary = 1; - // if there are less, initialize the new peer to add to the list - peer = (neighbour_peer*) malloc(sizeof(neighbour_peer)); - memcpy(&peer->ip, ip, sizeof(struct in6_addr)); - peer->port = LISTEN_PORT; - peer->is_temporary = 1; + // set last_seen time + time(&curtime); + peer->last_seen = curtime; - // set last_seen time + // set new peer as head of list + list *node = (list*) malloc(sizeof(list)); + node->data = (void*) peer; + node->next = neighbour_list; + neighbour_list = node; + return 0; + } + } else { + printf(">> Found peer %s:%i, updating the last seen time...\n", (char *) ip, port); + time_t curtime; + // if the peer was already in the list, update it time(&curtime); peer->last_seen = curtime; - // set new peer as head of list - list *node = (list*) malloc(sizeof(list)); - node->data = (void*) peer; - node->next = neighbour_list; - neighbour_list = node; - return 0; + } - - // if the peer was already in the list, update it - time(&curtime); - peer->last_seen = curtime; - - return 0; } // get data associated with id, if it doesn't exist return NULL @@ -421,7 +433,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // Send length bytes from packet int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num) { - ((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length); + ((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length); // Vectorized buffer struct iovec vec_buff = {.iov_len = length + 4, .iov_base = packet_buff}; @@ -587,21 +599,26 @@ int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list // We need to make sure the TLV announces a length that will no go onto // another tlv, as we might end up reading bullshit. int validate_tlv(char *data, int pos, int16_t packet_len){ + char type = data[pos]; // Nothing to do in this case - if(type == 0) + if(type == 0){ + printf(">> Found padding TLV type.\n"); return 0; + } // Check that we can read a length - if(pos + 1 >= packet_len) + if(pos + 1 >= packet_len){ return -1; + } unsigned char tlv_len = data[pos+1]; // Check that the tlv does not exceed the packet length - if(pos + tlv_len >= packet_len) + if(pos + tlv_len >= packet_len){ return -1; + } // Returns the type of the tlv or -1 if something went wrong switch(type) { @@ -655,6 +672,8 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack return -1; } + // Convert to hardware order. + ((packet*) packet_to_return)->length = ntohs(((packet*) packet_to_return)->length); printf("packet : %i, %i \n", packet_to_return->length + 4,received_data_len ); if (packet_to_return->length + 4 < received_data_len ) { perror(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics."); @@ -669,6 +688,7 @@ int add_message(char * message, int message_len){ } // We then look at the differents TLVs in the packet. int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, int socket_num){ + int pos = 0; unsigned char tlv_len, hash[16]; char warn[32]; @@ -692,6 +712,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, } } + ifindex = 0; while(pos < packet_len) { switch(validate_tlv(data, pos, packet_len)) { case 0: @@ -736,6 +757,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; + printf("fzfzfe\n"); break; case 4: @@ -841,6 +863,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, default: // A malformed packet was found so we stop looking for more packets and send a warning tlv strcpy(warn, "Packet is malformed."); + printf("Malformed packet\n"); build_warning(&new_tlv, warn, strlen(warn)); add_tlv(&pack, &new_tlv, sender, socket_num); @@ -914,7 +937,7 @@ int run_node(int sock_fd){ // Init the ~20s delay for node update. srand(time(NULL)); - time_t delay = time(NULL) + 2; + time_t delay = time(NULL) + 20; /* Descriptor zero is stdin */ fds[0].fd = 0; From c8b106838a6aa163eaa13cac48767ffe70e2511d Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 27 Apr 2020 19:49:33 +0200 Subject: [PATCH 13/59] Fixing validate_tlv --- src/node.c | 37 ++++++++++++++++++++++--------------- src/node.h | 3 +-- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/node.c b/src/node.c index d71c883..dce6529 100644 --- a/src/node.c +++ b/src/node.c @@ -90,10 +90,8 @@ int len_list(list *l) { neighbour_peer *get_random_neighbour() { printf(">> Getting random peer...\n"); // Get a random number - time_t t; srand((unsigned) time(NULL)); int n = (rand() % len_list(neighbour_list)) + 1; - int l_l = len_list(neighbour_list); // Get nth neighbour list *tmp = neighbour_list; @@ -125,13 +123,15 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { return NULL; } -// Return -1 if the peer could not be added, 0 if it could or if it was already in the table +// Return -1 if we have enough peers, +// 1 if it was added +// Return 0 if peer was updated as last_seen int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { char * buff_str_ip[1024]; - char * ip_str = inet_ntop(AF_INET6,ip, buff_str_ip, 1024); + char * ip_str = (char * ) inet_ntop(AF_INET6,ip,(char * restrict) buff_str_ip, 1024); - printf(">> Found peer %s:%i.\n", ip_str, ntohs(port)); + printf(">> Found peer %s @ %i.\n", ip_str, port); // We try to find a peer with this address and port. neighbour_peer *peer = get_neighbour(ip, port); time_t curtime; @@ -158,15 +158,14 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { node->data = (void*) peer; node->next = neighbour_list; neighbour_list = node; - return 0; + return 1; } } else { - printf(">> Found peer %s:%i, updating the last seen time...\n", (char *) ip, port); + printf(">> Found peer %s @ %i, updating the last seen time...\n", ip_str, port); time_t curtime; // if the peer was already in the list, update it time(&curtime); peer->last_seen = curtime; - return 0; } @@ -610,16 +609,20 @@ int validate_tlv(char *data, int pos, int16_t packet_len){ // Check that we can read a length if(pos + 1 >= packet_len){ + printf(">> Reading out of packet length. \n"); return -1; } unsigned char tlv_len = data[pos+1]; // Check that the tlv does not exceed the packet length - if(pos + tlv_len >= packet_len){ + if(pos + tlv_len > packet_len){ + printf(">> The TLV Length exceed the packet length\n"); return -1; } + printf(">> TLV has type %i\n", type ); + // Returns the type of the tlv or -1 if something went wrong switch(type) { case 1: @@ -893,15 +896,19 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc // Neighbour check struct in6_addr ip = sender->sin6_addr; - int16_t port = sender->sin6_port; + int16_t port = htons(sender->sin6_port); - if(add_n_update_neighbour(&ip, port) == -1) { - fprintf(stderr, ">> Maximum number of neighbour peers reached, aborting this packet.\n"); + + int rc = add_n_update_neighbour(&ip, port); + if( rc == -1) { + printf(">> We have enough peers, we won't add him..\n"); return -1; + } else if (rc == 1){ + printf(">> Peer was added to the table.\n"); + } else { + printf(">> Updated the time it was last seen.\n"); } - // struct tlv_list received_tlvs; - // if (validate_tlvs(formated_rec_datagram) < 0) int nbr_success_tlv = work_with_tlvs(received_data_buffer, received_data_len, sender, sock_fd); if (nbr_success_tlv < 0){ perror(">> Error while treating the TLVs of the packet."); @@ -937,7 +944,7 @@ int run_node(int sock_fd){ // Init the ~20s delay for node update. srand(time(NULL)); - time_t delay = time(NULL) + 20; + time_t delay = time(NULL) + 5; /* Descriptor zero is stdin */ fds[0].fd = 0; diff --git a/src/node.h b/src/node.h index 18bee64..20012f1 100644 --- a/src/node.h +++ b/src/node.h @@ -62,8 +62,7 @@ typedef struct list { // The adress of the main peer #define ROOT_PEER_ADDR "2001:660:3301:9200::51c2:1b9b" - -// TODO + // fonctions signatures int listen_for_packets(char * received_data_buffer, int received_data_len, struct sockaddr_in6 * sender, int sock_fd); From bda49e5e9254c402acd8f307367c64e18d486fc5 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Mon, 27 Apr 2020 20:08:30 +0200 Subject: [PATCH 14/59] work with tlvs check packet length --- src/node.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/node.c b/src/node.c index d71c883..2f959b2 100644 --- a/src/node.c +++ b/src/node.c @@ -36,16 +36,16 @@ int ask_for_peers(int socket_num) { struct in6_addr ip = peer->ip; int16_t port = peer->port; - int ifindex = if_nametoindex("enp3s0"); + /*int ifindex = if_nametoindex("enp3s0"); if(ifindex == 0) { int ifindex = if_nametoindex("eth0"); if(ifindex == 0) { perror("if_nametoindex failed"); return -1; } - } + }*/ - ifindex = 0; + int ifindex = 0; // Initialize sockaddr struct sockaddr_in6 dest; @@ -687,9 +687,15 @@ int add_message(char * message, int message_len){ return 0; } // We then look at the differents TLVs in the packet. -int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, int socket_num){ +int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *sender, int socket_num){ + int16_t packet_len = ntohs(((packet*) data)->length); - int pos = 0; + if(packet_len != total_packet_len - 4) { + fprintf(stderr, ">> Length indicated in packet differs from real length of packet received.\n"); + return -1; + } + + int pos = 4; unsigned char tlv_len, hash[16]; char warn[32]; tlv new_tlv, cur_tlv; @@ -703,6 +709,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, packet pack = (packet) {.magic = 95, .version = 1, .length = 0}; memset(pack.body, 0, 1020); +/* int ifindex = if_nametoindex("enp3s0"); if(ifindex == 0) { int ifindex = if_nametoindex("eth0"); @@ -711,6 +718,8 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, return -1; } } +*/ + int ifindex = 0; ifindex = 0; while(pos < packet_len) { From 9b2a3f1ea899539b99f278bcd087db4bfbc0b1aa Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 27 Apr 2020 20:38:31 +0200 Subject: [PATCH 15/59] fixed the neighbour update problem --- src/node.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/node.c b/src/node.c index d0815ed..d3c333f 100644 --- a/src/node.c +++ b/src/node.c @@ -612,17 +612,19 @@ int validate_tlv(char *data, int pos, int16_t packet_len){ printf(">> Reading out of packet length. \n"); return -1; } - + // 0 1 2 3 = Packet + // 4 = type 5 = tlv_len unsigned char tlv_len = data[pos+1]; // Check that the tlv does not exceed the packet length + printf("%i ; %i ; %i\n", pos, tlv_len, packet_len ); if(pos + tlv_len > packet_len){ printf(">> The TLV Length exceed the packet length\n"); return -1; } printf(">> TLV has type %i\n", type ); - + // Returns the type of the tlv or -1 if something went wrong switch(type) { case 1: @@ -691,7 +693,7 @@ int add_message(char * message, int message_len){ } // We then look at the differents TLVs in the packet. int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *sender, int socket_num){ - int16_t packet_len = ntohs(((packet*) data)->length); + int16_t packet_len = ((packet*) data)->length; if(packet_len != total_packet_len - 4) { fprintf(stderr, ">> Length indicated in packet differs from real length of packet received.\n"); @@ -725,8 +727,8 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s int ifindex = 0; ifindex = 0; - while(pos < packet_len) { - switch(validate_tlv(data, pos, packet_len)) { + while(pos < total_packet_len) { + switch(validate_tlv(data, pos, total_packet_len)) { case 0: // We received a padding tlv so it is ignored pos += 1; @@ -779,7 +781,8 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) == 0) { build_network_state_req(&new_tlv); - add_tlv(&pack, &new_tlv, sender, socket_num); + send_single_tlv(&new_tlv, sender, socket_num); + } // The position is updated From dceac0cecdfedf4c332dc1f06ee81d499a268202 Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 27 Apr 2020 22:31:31 +0200 Subject: [PATCH 16/59] Added debug_function and pretty print Added DEBUG_LEVEL if DEBUG_LEVEL > 3, the execution is step by step. --- src/debug.c | 11 +++ src/debug.h | 8 +++ src/node.c | 193 ++++++++++++++++++++++++++++++++-------------------- 3 files changed, 138 insertions(+), 74 deletions(-) create mode 100644 src/debug.c create mode 100644 src/debug.h diff --git a/src/debug.c b/src/debug.c new file mode 100644 index 0000000..bf3f3e8 --- /dev/null +++ b/src/debug.c @@ -0,0 +1,11 @@ +#include +#include "debug.h" + +void print_debug(char * msg){ + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m %s \n", msg); + } + if (DEBUG_LEVEL > 2) { + getchar(); + } +} diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..043bf8b --- /dev/null +++ b/src/debug.h @@ -0,0 +1,8 @@ +#ifndef DEBUG_H +#define DEBUG_H + +#define DEBUG_LEVEL 1 + +void print_debug(char * msg); + +#endif diff --git a/src/node.c b/src/node.c index d3c333f..adb6f67 100644 --- a/src/node.c +++ b/src/node.c @@ -13,6 +13,7 @@ #include #include #include "node.h" +#include "debug.h" // Static variables static list *data_list; @@ -27,7 +28,7 @@ int ask_for_peers(int socket_num) { if( nbr_peers >= 5){ return 0; } else if (nbr_peers <= 0){ - printf(">> No peers found in the peer list, something terrible happened.\n"); + print_debug(">> No peers found in the peer list, something terrible happened."); return -1; } else { @@ -60,15 +61,15 @@ int ask_for_peers(int socket_num) { neighbour_req.pad1 = NULL; int rc = build_neighbour_req(&neighbour_req); if (rc < 0) { - printf(">> Failed to build neighbour_req\n"); + print_debug(">> Failed to build neighbour_req"); } rc = send_single_tlv(&neighbour_req, &dest, socket_num); if (rc < 0) { - printf(">> Error while sending a TLV.\n"); + print_debug(">> Error while sending a TLV."); return -1; } else { - printf(">> Send TLV.\n"); + print_debug(">> Send TLV."); return 0; } } @@ -88,7 +89,7 @@ int len_list(list *l) { // Get a random neighbour neighbour_peer *get_random_neighbour() { - printf(">> Getting random peer...\n"); + print_debug(">> Getting random peer..."); // Get a random number srand((unsigned) time(NULL)); int n = (rand() % len_list(neighbour_list)) + 1; @@ -104,6 +105,14 @@ neighbour_peer *get_random_neighbour() { // Search for this peer in the neighbour table neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { + print_debug(">> Getting neighbour."); + + if (DEBUG_LEVEL > 1) { + char * buff_str_ip[1024]; + char * ip_str = (char * ) inet_ntop(AF_INET6,ip,(char * restrict) buff_str_ip, 1024); + printf("\x1b[31m[DEBUG]\x1b[0m >> Looking up %s @ %i\n", ip_str, port ); + } + // Look for neighbour list *tmp = neighbour_list; neighbour_peer *peer; @@ -128,21 +137,18 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { // Return 0 if peer was updated as last_seen int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { - char * buff_str_ip[1024]; - char * ip_str = (char * ) inet_ntop(AF_INET6,ip,(char * restrict) buff_str_ip, 1024); - printf(">> Found peer %s @ %i.\n", ip_str, port); // We try to find a peer with this address and port. neighbour_peer *peer = get_neighbour(ip, port); time_t curtime; if (peer == NULL) { - printf(">> We don't know this peer yet\n"); + print_debug(">> We don't know this peer yet"); // check if there are less than 15 neighbours if(len_list(neighbour_list) >= 15){ return -1; } else { - printf(">> Adding them to the peer table.\n"); + print_debug(">> Adding them to the peer table.\n"); // if there are less, initialize the new peer to add to the list peer = (neighbour_peer*) malloc(sizeof(neighbour_peer)); memcpy(&peer->ip, ip, sizeof(struct in6_addr)); @@ -161,7 +167,14 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { return 1; } } else { - printf(">> Found peer %s @ %i, updating the last seen time...\n", ip_str, port); + + if (DEBUG_LEVEL > 0) { + char * buff_str_ip[1024]; + char * ip_str = (char * ) inet_ntop(AF_INET6,ip,(char * restrict) buff_str_ip, 1024); + printf("\x1b[31m[DEBUG]\x1b[0m >> Found peer %s @ %i, updating the last seen time...\n", ip_str, port); + + } + time_t curtime; // if the peer was already in the list, update it time(&curtime); @@ -449,13 +462,13 @@ int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, in int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0); if (response_code < 0) { - printf(">> Unable to send out the packet to peer.\n"); + print_debug(">> Unable to send out the packet to peer."); error_while_sending = 1; } else if (response_code < length) { - printf(">> Sent out only part of the packet.\n"); + print_debug(">> Sent out only part of the packet."); error_while_sending = 1; } else { - printf(">> Send out packet to peer, size : %i\n", response_code); + print_debug(">> Send out packet to peer."); } if (error_while_sending == 1) { @@ -532,12 +545,11 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { } // Send the packet - printf("(%i)\n", pack.length ); return send_packet((char*) &pack, pack.length, dest, socket_num); } int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){ - // debug_print("Building packet to send a TLV."); + print_debug(">> Building packet to send a TLV."); // We first need to build the packet, char packet_buff[1024]; @@ -545,7 +557,7 @@ int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list pack.magic = 95; pack.version = 1; if (tlv_size > 1020) { - perror(">> Unable to send the tlv, it's size if above 1020 bytes."); + print_debug(">> Unable to send the tlv, it's size if above 1020 bytes."); return -1; } else { memcpy((void *) pack.body, tlv_to_send, tlv_size); @@ -556,7 +568,9 @@ int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list // packet_buff = (char *) pack; memcpy(&packet_buff,&pack,1024); - // debug_print("Packet has been built."); + if (DEBUG_LEVEL > 1) { + print_debug(">> Packet has been built."); + } // Vectorized buffer struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff }; @@ -575,20 +589,24 @@ int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list int response_code = sendmsg((int) socket_num, &packet_tlv_send_out, 0); if (response_code < 0) { - // debug_print("Unable to send out the packet to peer %i", i); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Unable to send out the packet to peer %li", i); + } error_while_sending = 1; continue; } else if (response_code < sizeof(packet_tlv_send_out)) { - // debug_print("Sent out only part of the packet."); + print_debug(">> Sent out only part of the packet."); error_while_sending = 1; continue; } else { - // debug_print("Send out packet to peer %i", i); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Send out packet to peer %li", i); + } } } if (error_while_sending == 1) { - // debug_print("Error occured while sending out a packet."); + print_debug(">> Error occured while sending out a packet."); return -1; } else { return 0; @@ -603,13 +621,13 @@ int validate_tlv(char *data, int pos, int16_t packet_len){ // Nothing to do in this case if(type == 0){ - printf(">> Found padding TLV type.\n"); + print_debug(">> Found padding TLV type."); return 0; } // Check that we can read a length if(pos + 1 >= packet_len){ - printf(">> Reading out of packet length. \n"); + print_debug(">> Reading outside of packet's max length."); return -1; } // 0 1 2 3 = Packet @@ -617,13 +635,14 @@ int validate_tlv(char *data, int pos, int16_t packet_len){ unsigned char tlv_len = data[pos+1]; // Check that the tlv does not exceed the packet length - printf("%i ; %i ; %i\n", pos, tlv_len, packet_len ); if(pos + tlv_len > packet_len){ - printf(">> The TLV Length exceed the packet length\n"); + print_debug(">> The TLV Length exceed the packet length\n"); return -1; } - printf(">> TLV has type %i\n", type ); + if (DEBUG_LEVEL > 1) { + printf("\x1b[31m[DEBUG]\x1b[0m >> TLV has type %i\n", type ); + } // Returns the type of the tlv or -1 if something went wrong switch(type) { @@ -667,21 +686,20 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack // We need to check a few things ; // The first byte must be worth 95, if (packet_to_return->magic != 95) { - perror(">> The magic number of the packet is no good."); + print_debug(">> The magic number of the packet is no good."); return -1; } // The second byte must be worth 1, if (packet_to_return->version != 1) { - perror(">> The version number of the packet is no good."); + print_debug(">> The version number of the packet is no good."); return -1; } // Convert to hardware order. ((packet*) packet_to_return)->length = ntohs(((packet*) packet_to_return)->length); - printf("packet : %i, %i \n", packet_to_return->length + 4,received_data_len ); if (packet_to_return->length + 4 < received_data_len ) { - perror(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics."); + print_debug(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics."); return -1; } @@ -696,7 +714,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s int16_t packet_len = ((packet*) data)->length; if(packet_len != total_packet_len - 4) { - fprintf(stderr, ">> Length indicated in packet differs from real length of packet received.\n"); + print_debug(">> Length indicated in packet differs from real length of packet received, disgarding packet."); return -1; } @@ -731,18 +749,20 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s switch(validate_tlv(data, pos, total_packet_len)) { case 0: // We received a padding tlv so it is ignored + print_debug(">> Received padding tlv, ignoring..."); pos += 1; break; case 1: // We received a padding tlv so it is ignored + print_debug(">> Received padding(n) tlv, ignoring..."); tlv_len = data[pos+1]; pos += tlv_len + 2; break; case 2: // We received a neighbour request so a random neighbor tlv has to be sent - + print_debug(">> Received neighbour request, sending out a neighbour address."); // Send a neighbour tlv random_neighbour = get_random_neighbour(); build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port); @@ -754,6 +774,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s break; case 3: + print_debug(">> Received neighbour tlv, sending back network hash."); // We received a neighbour tlv so a tlv network hash is sent to that address cur_tlv.neighbour = (neighbour*) (data + pos); @@ -771,19 +792,22 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; - printf("fzfzfe\n"); break; case 4: - // We reveived a network hash tlv so we compare the hash with our own, if they differ we send a network state request tlv + print_debug(">> Received network_hash, comparing with our own.."); + // We reveived a network hash tlv so we compare the hash with our own, + // if they differ we send a network state request tlv cur_tlv.network_hash = (network_hash*) (data + pos); hash_network(data_list, hash); if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) == 0) { - build_network_state_req(&new_tlv); + print_debug(">> Sending out our network hash."); + build_network_state_req(&new_tlv); send_single_tlv(&new_tlv, sender, socket_num); - - } + } else { + print_debug(">> We're up to date."); + } // The position is updated tlv_len = data[pos+1]; @@ -791,8 +815,9 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s break; case 5: - // We received a network state request tlv so a series of tlv node hash have to be sent for each data known - + // We received a network state request tlv + // so a series of tlv node hash have to be sent for each data known + print_debug(">> Received network state request, sending back hashes for messages."); // for each known data build a node hash and add to packet tmp_list = data_list; @@ -807,7 +832,11 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s break; case 6: - // We received a node hash tlv so if there is no entry for node_id in the data list or the hashes differ we send a node state request, if the hashes are identical nothing has to be done + // We received a node hash tlv so + // if there is no entry for node_id in the data list or the hashes differ + // we send a node state request, + //if the hashes are identical nothing has to be done + print_debug(">> Received node hash, updating message entry..."); cur_tlv.node_hash = (node_hash*) (data + pos); pdata = get_data(cur_tlv.node_hash->node_id); @@ -837,8 +866,11 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s break; case 7: - // We received a node state request tlv so a node state tlv for this node id has to be sent, if no pub_data exists for this id nothing is sent - cur_tlv.node_state_req = (node_state_req*) (data + pos); + // We received a node state request tlv + // so a node state tlv for this node id has to be sent, + // if no pub_data exists for this id nothing is sent + print_debug(">> Received node state request. Processing..."); + cur_tlv.node_state_req = (node_state_req*) (data + pos); pdata = get_data(cur_tlv.node_state_req->node_id); if(pdata != NULL) { @@ -852,7 +884,10 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s break; case 8: - // We received a node state tlv so we add it to the data list or update the data stored + // We received a node state tlv so + // we add it to the data list + // or update the data stored + print_debug(">> Received node state, updating..."); cur_tlv.node_state = (node_state*) (data + pos); add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); @@ -863,11 +898,12 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s break; case 9: + print_debug(">> Received warning !"); // We received a warning tlv so it's message is printed cur_tlv.warning = (warning*) (data + pos); // Print exactly new_tlv.length characters from new_tlv.message - sprintf(warn, ">> WARNING:\n%%.%ds", cur_tlv.warning->length + 1); + sprintf(warn, "\x1b[31m>> WARNING:\n%%.%ds \x1b[0m", cur_tlv.warning->length + 1); printf(warn, cur_tlv.warning->message); // The position is updated @@ -878,7 +914,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s default: // A malformed packet was found so we stop looking for more packets and send a warning tlv strcpy(warn, "Packet is malformed."); - printf("Malformed packet\n"); + print_debug(">> Malformed packet, we won't treat it."); build_warning(&new_tlv, warn, strlen(warn)); add_tlv(&pack, &new_tlv, sender, socket_num); @@ -890,8 +926,9 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s free(new_tlv.pad1); // If the packet still has data in it then send it - if(pack.length > 0) + if(pack.length > 0){ send_packet((char*) &pack, pack.length, sender, socket_num); + } return 0; } @@ -902,7 +939,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc // and we return it in the struct designed to work with it. struct packet formated_rec_datagram; if(check_header(received_data_buffer, received_data_len, &formated_rec_datagram) < 0){ - perror(">> Error while checking the header, aborting this packet, by choice, and conviction."); + print_debug(">> Error while checking the header, aborting this packet, by choice, and conviction."); return -1; } @@ -913,21 +950,23 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc int rc = add_n_update_neighbour(&ip, port); if( rc == -1) { - printf(">> We have enough peers, we won't add him..\n"); + print_debug(">> We have enough peers, we won't add him.."); return -1; } else if (rc == 1){ - printf(">> Peer was added to the table.\n"); + print_debug(">> Peer was added to the table."); } else { - printf(">> Updated the time it was last seen.\n"); + print_debug(">> Updated the time it was last seen."); } int nbr_success_tlv = work_with_tlvs(received_data_buffer, received_data_len, sender, sock_fd); if (nbr_success_tlv < 0){ - perror(">> Error while treating the TLVs of the packet."); - printf(">> Managed to deal with %i TLVs\n", -nbr_success_tlv ); + print_debug(">> Error while treating the TLVs of the packet."); + if (DEBUG_LEVEL > 1) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Managed to deal with %i TLVs", -nbr_success_tlv ); + } return -2; } else { - printf(">> Done working with the TLVs of the packet, listening for new packets.\n"); + print_debug(">> Done working with the TLVs of the packet, listening for new packets."); return 0; } @@ -946,7 +985,7 @@ int t_update_neighbours(){ } int run_node(int sock_fd){ - printf(">> Running node...\n"); + print_debug(">> Running node..."); int ret; ssize_t bytes; @@ -970,11 +1009,11 @@ int run_node(int sock_fd){ while (1) { if(time(NULL) >= delay) { - printf(">> Asking for more peers...\n"); + print_debug(">> Asking for more peers..."); t_ask_for_more_peers(sock_fd); - printf(">> Updating neighbours...\n"); + print_debug(">> Updating neighbours..."); t_update_neighbours(); - printf(">> Getting network state...\n"); + print_debug(">> Getting network state..."); t_get_network_state(sock_fd); delay = time(NULL) + 20 + (rand() % 10); } @@ -994,17 +1033,17 @@ int run_node(int sock_fd){ ret = poll(fds, 2, 5); if (ret < 0) { - printf(">> Error - poll returned error: %s\n", strerror(errno)); + print_debug(">> Error - poll returned error"); break; } else if (ret > 0) { /* Regardless of requested events, poll() can always return these */ if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { - printf("Error - poll indicated stdin error\n"); + print_debug("Error - poll indicated stdin error\n"); break; } if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) { - printf("Error - poll indicated socket error\n"); + print_debug("Error - poll indicated socket error\n"); break; } @@ -1012,14 +1051,16 @@ int run_node(int sock_fd){ if (fds[0].revents & (POLLIN | POLLPRI)) { bytes = read(0, input_buffer, sizeof(input_buffer)); if (bytes < 0) { - printf("Error - stdin error: %s\n", strerror(errno)); + print_debug(">> Error - stdin error"); break; } input_buffer[strcspn(input_buffer, "\n")] = 0; - printf(">> Adding following message to the table : “%s”\n", input_buffer ); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Adding following message to the table : “%s”\n", input_buffer ); + } // Add message to the message table. if (add_message(input_buffer, bytes) < 0) { - perror(">> Error while trying to add the message to the list of messages, please try again.."); + print_debug(">> Error while trying to add the message to the list of messages, please try again.."); } } @@ -1042,15 +1083,19 @@ int run_node(int sock_fd){ bytes = recvmsg(sock_fd, &msg_from_peer, 0); if (bytes < 0) { - printf("Error - recvfrom error: %s\n", strerror(errno)); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Error - recvfrom error: %s\n", strerror(errno)); + } break; } if (bytes > 0) { - printf("Received %i bytes as : %s\n", (int)bytes, output_buffer); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Received %i bytes as : %s\n", (int)bytes, output_buffer); + } // Treat incoming packets. int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd); if (work_tlv_status < 0) { - perror(">> Error while treating the incoming packet."); + print_debug(">> Error while treating the incoming packet."); } } } @@ -1066,14 +1111,14 @@ int run_node(int sock_fd){ // This function runs once, and sets the sock_fd as well as the neighbourhood int bootstrap_node(int * sock_fd){ - printf(">> Boostraping node...\n"); + print_debug(">> Boostraping node..."); struct sockaddr_in6 server_addr; /* Create UDP socket */ * sock_fd = socket(AF_INET6, SOCK_DGRAM, 0); if ( * sock_fd < 0) { - printf("Error - failed to open socket: %s\n", strerror(errno)); + print_debug(">> Error - failed to open socket"); return -1; } @@ -1083,8 +1128,8 @@ int bootstrap_node(int * sock_fd){ // server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY); server_addr.sin6_port = htons(LISTEN_PORT); if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) { - printf("Error - failed to bind socket: %s\n", strerror(errno)); - return -2; + print_debug(">> Error - failed to bind socket"); + return -2; } /* Make the first peer*/ @@ -1102,18 +1147,18 @@ int bootstrap_node(int * sock_fd){ root_peer->last_seen = root_peer_seen; // TODO: Add the first peer to the neighbourhood - printf(">> Adding the first root peer to the list...\n"); + print_debug(">> Adding the first root peer to the list..."); neighbour_list = malloc(sizeof(struct list)); neighbour_list->data = (void *) root_peer; neighbour_list->next = NULL; - printf(">> Boostraping done.\n"); + print_debug(">> Boostraping done."); return 0; } int main(int argc, const char *argv[]) { - printf(">> Starting node\n"); + print_debug(">> Starting node"); int sock_fd; bootstrap_node(&sock_fd); From 4539b8c3c7275d59467cbe3d5a28398456698c24 Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 27 Apr 2020 23:42:11 +0200 Subject: [PATCH 17/59] Added printing of network hash --- src/debug.h | 2 +- src/node.c | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/debug.h b/src/debug.h index 043bf8b..bf199d7 100644 --- a/src/debug.h +++ b/src/debug.h @@ -1,7 +1,7 @@ #ifndef DEBUG_H #define DEBUG_H -#define DEBUG_LEVEL 1 +#define DEBUG_LEVEL 2 void print_debug(char * msg); diff --git a/src/node.c b/src/node.c index adb6f67..3775236 100644 --- a/src/node.c +++ b/src/node.c @@ -23,6 +23,7 @@ static list *neighbour_list; // Looks for more peers int ask_for_peers(int socket_num) { + print_debug(">> Asking for more peers..."); // Only ask for more peers if the neighbour list is small enough int nbr_peers = len_list(neighbour_list); if( nbr_peers >= 5){ @@ -214,6 +215,7 @@ pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // Add new data to data list void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { + print_debug(">> Adding data to the data list."); // If id is the same as this node's id then we only update seqno if(id == NODE_ID) { pub_data *node_data = get_data(NODE_ID); @@ -303,6 +305,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // Update the neighbour list int update_neighbours() { + print_debug(">> Updating neighbours."); list *tmp = neighbour_list, *last = NULL, *node_to_delete; neighbour_peer *peer; time_t curtime; @@ -311,7 +314,11 @@ int update_neighbours() { // check every neighbour while(tmp != NULL) { peer = (neighbour_peer*) tmp->data; - + if (DEBUG_LEVEL > 1) { + char * buff_str_ip[1024]; + char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); + printf("\x1b[31m[DEBUG]\x1b[0m >> Checking for neighbour %s\n", ip_str ); + } // Check if peer is temporary if(peer->is_temporary) { // If it's been 70 seconds or more since we last received a packet from this peer then remove it from the list @@ -321,6 +328,8 @@ int update_neighbours() { // increase the count of deleted nodes deleted++; + print_debug(">> They have not been seen for the past 70 seconds, deleting..."); + // If head of the list if(last == NULL) { // Store node to delete @@ -349,7 +358,11 @@ int update_neighbours() { free(node_to_delete); continue; + } else { + print_debug(">> Peer has been seen in the last 70 seconds, keeping him in."); } + } else { + print_debug(">> Peer is not temporary, keeping him in."); } last = tmp; @@ -362,6 +375,7 @@ int update_neighbours() { // Add TLV to packet, if it does not fit then send the packet and reset the packet buff to be able to add more TLVs that will be sent afterwards int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { + print_debug(">> Adding tlv to packet"); char type = tlv->pad1->type, sent = 0, errval = 0; unsigned char len; @@ -435,6 +449,8 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { return -1; } + print_debug(">> Finished adding the TLVs to the packet"); + // If the previous packet was went return 1 or -1 if there was an error sending it if(sent) return errval? -1:1; @@ -801,6 +817,21 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s cur_tlv.network_hash = (network_hash*) (data + pos); hash_network(data_list, hash); + if (DEBUG_LEVEL > 1) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Our hash : "); + for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){ + printf("%02x", hash[x]); + fflush(0); + } + printf("\n"); + printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); + for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){ + printf("%02x", cur_tlv.network_hash->network_hash[x]); + fflush(0); + } + printf("\n"); + } + if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) == 0) { print_debug(">> Sending out our network hash."); build_network_state_req(&new_tlv); @@ -898,7 +929,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s break; case 9: - print_debug(">> Received warning !"); + print_debug(">> \aReceived warning !"); // We received a warning tlv so it's message is printed cur_tlv.warning = (warning*) (data + pos); @@ -953,7 +984,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc print_debug(">> We have enough peers, we won't add him.."); return -1; } else if (rc == 1){ - print_debug(">> Peer was added to the table."); + print_debug(">> Peer was added to the table.\a"); } else { print_debug(">> Updated the time it was last seen."); } @@ -977,6 +1008,7 @@ int t_ask_for_more_peers(int sock_fd){ } int t_get_network_state(int sock_fd){ + print_debug(">> Getting network state..."); return 0; } @@ -1009,11 +1041,8 @@ int run_node(int sock_fd){ while (1) { if(time(NULL) >= delay) { - print_debug(">> Asking for more peers..."); t_ask_for_more_peers(sock_fd); - print_debug(">> Updating neighbours..."); t_update_neighbours(); - print_debug(">> Getting network state..."); t_get_network_state(sock_fd); delay = time(NULL) + 20 + (rand() % 10); } From a178c761164b64ceffac48060cbba1e12b17222a Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 28 Apr 2020 00:01:43 +0200 Subject: [PATCH 18/59] Fixed bad memcmp, activated receiving messages. --- src/node.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/node.c b/src/node.c index 3775236..0aee474 100644 --- a/src/node.c +++ b/src/node.c @@ -832,7 +832,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s printf("\n"); } - if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) == 0) { + if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) != 0) { print_debug(">> Sending out our network hash."); build_network_state_req(&new_tlv); send_single_tlv(&new_tlv, sender, socket_num); @@ -921,6 +921,10 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s print_debug(">> Received node state, updating..."); cur_tlv.node_state = (node_state*) (data + pos); + print_debug(">> Received message ! "); + if (DEBUG_LEVEL > 0) { + printf("\n\t %s \n", (char *) cur_tlv.node_state->data); + } add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); // The position is updated From 023dd46a26e1a606bc541d06d7fbf300156fd932 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 28 Apr 2020 17:37:51 +0200 Subject: [PATCH 19/59] Modification of the malloc's --- src/node.c | 9 +++++---- src/node.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/node.c b/src/node.c index 0aee474..ef470d7 100644 --- a/src/node.c +++ b/src/node.c @@ -802,6 +802,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s new_neighbour.sin6_scope_id = ifindex; // Build network hash + // memset(&new_tlv,0,sizeof(struct network_hash)); build_network_hash(&new_tlv, data_list); send_single_tlv(&new_tlv, &new_neighbour, socket_num); @@ -819,13 +820,13 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s if (DEBUG_LEVEL > 1) { printf("\x1b[31m[DEBUG]\x1b[0m >> Our hash : "); - for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){ + for(int x = 0; x < 16; x++){ printf("%02x", hash[x]); fflush(0); } printf("\n"); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); - for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){ + for(int x = 0; x < 16; x++){ printf("%02x", cur_tlv.network_hash->network_hash[x]); fflush(0); } @@ -923,7 +924,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s print_debug(">> Received message ! "); if (DEBUG_LEVEL > 0) { - printf("\n\t %s \n", (char *) cur_tlv.node_state->data); + printf("\nID : %li \nSeqno : %i\n “%s” \n", cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, (char *) cur_tlv.node_state->data); } add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); @@ -1048,7 +1049,7 @@ int run_node(int sock_fd){ t_ask_for_more_peers(sock_fd); t_update_neighbours(); t_get_network_state(sock_fd); - delay = time(NULL) + 20 + (rand() % 10); + delay = time(NULL) + 20 + (rand() % 5); } // This might be cool to add, but we need to find a way to write to stdin diff --git a/src/node.h b/src/node.h index 20012f1..0f2abc4 100644 --- a/src/node.h +++ b/src/node.h @@ -62,7 +62,7 @@ typedef struct list { // The adress of the main peer #define ROOT_PEER_ADDR "2001:660:3301:9200::51c2:1b9b" - + // fonctions signatures int listen_for_packets(char * received_data_buffer, int received_data_len, struct sockaddr_in6 * sender, int sock_fd); From 3e678b64bec3a8e98a7eebf1a11694229b920d12 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Tue, 28 Apr 2020 17:47:31 +0200 Subject: [PATCH 20/59] malloc(0) fixed --- src/hash.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/hash.c b/src/hash.c index 44e3827..61f06f9 100644 --- a/src/hash.c +++ b/src/hash.c @@ -17,7 +17,10 @@ void hash_data(pub_data *data, unsigned char *buf) { // Hash every data contained in data_list then return a network hash void hash_network(list *data_list, unsigned char *buf) { - unsigned char *concat = (unsigned char*) malloc(0); + // Get list length to initialize concat buffer + int concat_len = len_list(data_list) * SHA256_DIGEST_LENGTH; + unsigned char *concat = (unsigned char*) malloc(concat_len); + unsigned char hash[SHA256_DIGEST_LENGTH]; int totlen = 0; list *tmp = data_list; From 25e703b993332284ef9e053bc94686e245fb8805 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 28 Apr 2020 17:48:41 +0200 Subject: [PATCH 21/59] Added basic add_message function --- src/node.c | 24 ++++++++++++++++-------- src/node.h | 4 ++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/node.c b/src/node.c index 0aee474..c2ef9aa 100644 --- a/src/node.c +++ b/src/node.c @@ -214,7 +214,7 @@ pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) { } // Add new data to data list -void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { +int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { print_debug(">> Adding data to the data list."); // If id is the same as this node's id then we only update seqno if(id == NODE_ID) { @@ -224,7 +224,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { node_data->seqno = seqno ^ 1; } - return; + return 1; } // Copy data @@ -236,7 +236,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { data_list->data = (void*) new_data; data_list->next = NULL; - return; + return 2; } // Find correct position for new data @@ -257,7 +257,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { data_list->data = (void*) new_data; data_list->next = tmp; - return; + return 2; } // Else, we update the last node @@ -278,13 +278,13 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // Free old data free(cur_data); - return; + return 2; } // seqno is smaller so the new data allocated is freed and nothing else is done free(new_data); - return; + return 2; } // Get next node in list @@ -299,6 +299,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { new_node->data = (void*) new_data; new_node->next = NULL; last->next = new_node; + return 3; } /* ---- Fin fonctions utilitaires ---- */ @@ -723,6 +724,11 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack } int add_message(char * message, int message_len){ + int seqno = 0; + int rc = add_data(message_len, NODE_ID ,seqno, message); + if (rc > 0) { + print_debug(">> Message added."); + } return 0; } // We then look at the differents TLVs in the packet. @@ -925,8 +931,10 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s if (DEBUG_LEVEL > 0) { printf("\n\t %s \n", (char *) cur_tlv.node_state->data); } - add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); - + int rc = add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); + if (rc < 0) { + print_debug(">> Error while adding note state !"); + } // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; diff --git a/src/node.h b/src/node.h index 20012f1..a45a61e 100644 --- a/src/node.h +++ b/src/node.h @@ -62,7 +62,7 @@ typedef struct list { // The adress of the main peer #define ROOT_PEER_ADDR "2001:660:3301:9200::51c2:1b9b" - + // fonctions signatures int listen_for_packets(char * received_data_buffer, int received_data_len, struct sockaddr_in6 * sender, int sock_fd); @@ -136,6 +136,6 @@ pub_data *get_data(int64_t id); pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data); // add new data to data list -void add_data(unsigned char len, int64_t id, int16_t seqno, char *data); +int add_data(unsigned char len, int64_t id, int16_t seqno, char *data); #endif From aa5ac80337dca8f9034bc889d85f9e333b998127 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Tue, 28 Apr 2020 18:02:34 +0200 Subject: [PATCH 22/59] concat hash no realloc --- src/hash.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/hash.c b/src/hash.c index 61f06f9..c2324b2 100644 --- a/src/hash.c +++ b/src/hash.c @@ -58,6 +58,5 @@ void concat_data(pub_data *data, unsigned char *buf) { // Concat hash2 to hash1 (hash1 is modified) void concat_hash(unsigned char *hash1, unsigned char *hash2, size_t size) { - hash1 = (unsigned char*) realloc(hash1, size + 16); memcpy(hash1+size, hash2, 16); } From 27d38c3fe5ad3209ecd98f12b358eae0c1ac3e46 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Tue, 28 Apr 2020 18:07:06 +0200 Subject: [PATCH 23/59] concat_len --- src/hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hash.c b/src/hash.c index c2324b2..a205bad 100644 --- a/src/hash.c +++ b/src/hash.c @@ -18,7 +18,7 @@ void hash_data(pub_data *data, unsigned char *buf) { // Hash every data contained in data_list then return a network hash void hash_network(list *data_list, unsigned char *buf) { // Get list length to initialize concat buffer - int concat_len = len_list(data_list) * SHA256_DIGEST_LENGTH; + int concat_len = len_list(data_list) * 16; unsigned char *concat = (unsigned char*) malloc(concat_len); unsigned char hash[SHA256_DIGEST_LENGTH]; From 956b249259bb837e260a4f01662cbdaa7160b87e Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 28 Apr 2020 18:37:21 +0200 Subject: [PATCH 24/59] Added a new way to add data to a list of data --- src/node.c | 64 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/src/node.c b/src/node.c index c2ef9aa..4205132 100644 --- a/src/node.c +++ b/src/node.c @@ -217,15 +217,55 @@ pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) { int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { print_debug(">> Adding data to the data list."); // If id is the same as this node's id then we only update seqno + if(id == NODE_ID) { - pub_data *node_data = get_data(NODE_ID); + // pub_data *node_data = get_data(NODE_ID); + // + // if (node_data == NULL) { + // if (datagram == NULL) { + // data_list = (list*) malloc(sizeof(list)); + // } + // data_list->data = (void*) new_data; + // data_list->next = NULL; + // } + // + // if(seqno >= node_data->seqno) { + // node_data->seqno = seqno ^ 1; + // } + // + // return 1; - if(seqno >= node_data->seqno) { - node_data->seqno = seqno ^ 1; - } + // We create our pub_data. + pub_data * message = malloc(sizeof(struct pub_data)); + message->length = len; + message->id = id; + message->seqno = seqno; + message->data = data; - return 1; - } + // If the data list has never been used, or is empty ( same thing ) + if (data_list = NULL) { + data_list = (list*) malloc(sizeof(list)); + } else { + // Otherwise, we move until the last element of the dala_list, + // and add or data there. + + // We use a temporary address to avoid writing to the static list. + // Seems weird but ok. + list *tmp = data_list; + while(tmp->next != NULL){ + tmp = tmp->next; + } + + // We create the next node of the linked list. + list * new_node = malloc(sizeof(struct list)); + new_node->data = (void *) message; + + // Adding the message to the list. + tmp->next = (void *) new_node; + } + } else { + + } // Copy data pub_data *new_data = copy_data(len, id, seqno, data); @@ -266,7 +306,7 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { new_node->next = tmp; last->next = new_node; - return; + return 1; } else if(id == cur_id) { // If data already exists for this id then we update it if it's seqno is greater than the one stored pub_data *cur_data = (pub_data*) tmp->data; @@ -724,13 +764,15 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack } int add_message(char * message, int message_len){ - int seqno = 0; - int rc = add_data(message_len, NODE_ID ,seqno, message); + int seqno = 1337; + // int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { + int rc = add_data((unsigned char) message_len, (int64_t) NODE_ID ,(int16_t) seqno, message); if (rc > 0) { print_debug(">> Message added."); } return 0; } + // We then look at the differents TLVs in the packet. int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *sender, int socket_num){ int16_t packet_len = ((packet*) data)->length; @@ -825,13 +867,13 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s if (DEBUG_LEVEL > 1) { printf("\x1b[31m[DEBUG]\x1b[0m >> Our hash : "); - for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){ + for(int x = 0; x < 16; x++){ printf("%02x", hash[x]); fflush(0); } printf("\n"); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); - for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){ + for(int x = 0; x < 16; x++){ printf("%02x", cur_tlv.network_hash->network_hash[x]); fflush(0); } From 897550a0acafed8b5b9d73f1722b1cbc095e3b71 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Tue, 28 Apr 2020 19:02:02 +0200 Subject: [PATCH 25/59] warning sprintf --- src/debug.o | Bin 0 -> 1584 bytes src/hash.o | Bin 0 -> 2584 bytes src/node.c | 18 +++++++++--------- src/tlv.c | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 src/debug.o create mode 100644 src/hash.o diff --git a/src/debug.o b/src/debug.o new file mode 100644 index 0000000000000000000000000000000000000000..f402994105591610dd5628c2fc860deaac488e3e GIT binary patch literal 1584 zcmbtUL2DCH5T0$+wi=}tkzzdTMNzSjU2CxjmTh8^3X2781PS`sq+28HCS~^ps)u^j zgWyU0H+uB1c+;y0b>`&_Szk6Dbi(e;d~d#a^Jd=WO+9SPI1aElunuF7QGo9=V>{4h z0B4~Lo9|c2+kE%a|MHoL-^i`BSNx=Xzg}%MpHId2=k+6S4i}tCd3Npt<|q8JUIDmh^xqVfbOL@H7sZqc zPYO`NUy+nBQ<0S5WD$&6W~bnD6ZpJFrsX*`5q}Pm#a#W|i%WE z3nHv}aFD+2E2$xn&2X!_5z4Jb<8ggkZf{h>x&$e8%&zSA_GlaK6kQKG)vx1`EYczh zb*zjJ<1X4H6ra0N03v&psHlTlr6b*AD~x_cN$?6@a)(L+m3y1_~);Z}t?Tlac`Oo805&x|zsyC);%jW+E%o}ct literal 0 HcmV?d00001 diff --git a/src/hash.o b/src/hash.o new file mode 100644 index 0000000000000000000000000000000000000000..9de82471d9309121a76665a50f123532c62eb3f9 GIT binary patch literal 2584 zcmbtU&2Jk;6d$i$$2jS((<0%jNb#XTF=|&fZJ{a>c$3(kpj9Ign}XV~IF600_(QVS zkfN#}xv5w!L0sTJ;KqSd!T}_w2~v9Dh$?!CkV+Fks8vK{MAY)$?o2W=4npv(o%iN9 zzxOe3X8e9qPe~pR;o>1Lk^LD*2?_V@+X=@^kO9(9wv}3+GU#7bhHf}0wbKE8F%_hi zUs+CvidHJ1EGg8!7;gO$XdK&F@ip~@+h)JAB<-w7AEtuM>^3+PYD?5g1+8^zUz0i$ z)-}?hi`S3P*8R>zw4+CFjeAz8_5I40cbk5`T2y&HTXMYYjI^s+8L|tpKEOh(b!9mt zJt`vjNZ@Cwb#IDV+qCuTxmjxahDU%*t>k9Btt|EFi!?}E?PJ3aynsg6sP)q(Mx%?_ zwr+h#t^3ZjsML~eY9}{m>1j8DGU#pksXg7fy~-G@AKGhLcEZ}xGFiy_CuEHYS;tR| z#M=&kWy44F9XHp)-3e>WeCL}GB%ZMD6_ufJsK9YcU{Qn$|9B)7qO`xWD~=#&;m)FUw9MWhjB? z>sJPSPpy*l*x32VNOq=KHJgza)U#^r)aj-(i(ikQQ)6eME{gF8dD2zH^WLk(GavFC zALt8w0v8K6^)y=xFpXG@*`F~T0S()BfWqG|mJfJ?Xr@-+^l za_D1eR1PnClXB!EpC*soJfz97mOm}OCEWfyaU%5dyC3h(z2 z*vS9VK?!ks4t5ki&SguEdiel6bpU=5aEu>ee=g2>0XBO8zj6R>9)Mp39OG}W_z#DDW(p7fK^P;s7GShqdiK zcI6?0Ab1l7;N$pFwhzP)3;39TD*}E&!126zoEpP1Z&1KL75HM_I|44|y(Qp-LYxTL zC>S3x8&i?+! zj>7_;eS#C1F0rS6ht2UWVJQB-!wi3h;obi{QSp!%7gbj|7iCQ_&VW&M-PznlI#1KMIxuzCMD{0bPo`k!D2I?ssMfBaYG{=EOVkM7*) v5@rouXB)*giN4+b7`MCk0FezW3S_}mdn4}~2k?#d^8bp}{{|;A-R=Ju5(aYf literal 0 HcmV?d00001 diff --git a/src/node.c b/src/node.c index ef470d7..6dbea63 100644 --- a/src/node.c +++ b/src/node.c @@ -461,10 +461,11 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // Send length bytes from packet int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num) { - ((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length); + ((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length); // Vectorized buffer - struct iovec vec_buff = {.iov_len = length + 4, .iov_base = packet_buff}; + struct iovec vec_buff[1]; + vec_buff[0] = {.iov_len = length + 4, .iov_base = packet_buff}; int error_while_sending = 0; @@ -472,7 +473,7 @@ int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, in struct msghdr packet_tlv_send_out = { .msg_name = dest, .msg_namelen = sizeof(struct sockaddr_in6), - .msg_iov = &vec_buff, + .msg_iov = vec_buff, .msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2. }; @@ -802,7 +803,6 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s new_neighbour.sin6_scope_id = ifindex; // Build network hash - // memset(&new_tlv,0,sizeof(struct network_hash)); build_network_hash(&new_tlv, data_list); send_single_tlv(&new_tlv, &new_neighbour, socket_num); @@ -820,13 +820,13 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s if (DEBUG_LEVEL > 1) { printf("\x1b[31m[DEBUG]\x1b[0m >> Our hash : "); - for(int x = 0; x < 16; x++){ + for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){ printf("%02x", hash[x]); fflush(0); } printf("\n"); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); - for(int x = 0; x < 16; x++){ + for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){ printf("%02x", cur_tlv.network_hash->network_hash[x]); fflush(0); } @@ -924,7 +924,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s print_debug(">> Received message ! "); if (DEBUG_LEVEL > 0) { - printf("\nID : %li \nSeqno : %i\n “%s” \n", cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, (char *) cur_tlv.node_state->data); + printf("\n\t %s \n", (char *) cur_tlv.node_state->data); } add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); @@ -939,7 +939,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s cur_tlv.warning = (warning*) (data + pos); // Print exactly new_tlv.length characters from new_tlv.message - sprintf(warn, "\x1b[31m>> WARNING:\n%%.%ds \x1b[0m", cur_tlv.warning->length + 1); + sprintf(warn, ">> WARNING:\n%%.%ds", cur_tlv.warning->length + 1); printf(warn, cur_tlv.warning->message); // The position is updated @@ -1049,7 +1049,7 @@ int run_node(int sock_fd){ t_ask_for_more_peers(sock_fd); t_update_neighbours(); t_get_network_state(sock_fd); - delay = time(NULL) + 20 + (rand() % 5); + delay = time(NULL) + 20 + (rand() % 10); } // This might be cool to add, but we need to find a way to write to stdin diff --git a/src/tlv.c b/src/tlv.c index 82de813..140a9fb 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -92,7 +92,7 @@ int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) { new->type = 3; new->length = 18; new->ip = ip; - new->port = port; + new->port = htons(port); tlv->neighbour = new; From 61041efd02828bb5fbf1ae94973f13639e40e2bc Mon Sep 17 00:00:00 2001 From: gonzalef Date: Tue, 28 Apr 2020 19:05:10 +0200 Subject: [PATCH 26/59] send_packet --- src/node.c | 3 ++- src/node.o | Bin 0 -> 27552 bytes src/parser.o | Bin 0 -> 2752 bytes src/tlv.o | Bin 0 -> 5024 bytes 4 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 src/node.o create mode 100644 src/parser.o create mode 100644 src/tlv.o diff --git a/src/node.c b/src/node.c index 6dbea63..40b127a 100644 --- a/src/node.c +++ b/src/node.c @@ -465,7 +465,8 @@ int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, in // Vectorized buffer struct iovec vec_buff[1]; - vec_buff[0] = {.iov_len = length + 4, .iov_base = packet_buff}; + vec_buff[0].iov_len = length + 4; + vec_buff[0].iov_base = packet_buff; int error_while_sending = 0; diff --git a/src/node.o b/src/node.o new file mode 100644 index 0000000000000000000000000000000000000000..7dc7f55f97aeacd9de06e9925c9963160d85aef8 GIT binary patch literal 27552 zcmd6Pe|%KcweQIfU<8_pNHw;!9KC~v7SjyjH`Zpz04F*Em7v_B4I!B!DM=>HO!!f$ zgic~P4x_nNtG%_i-siiw^tQLxduR|7+H0@9_TJ|l+_pHfq_CjCGNr(}#>!n8)UqCo<<6`5wAz|% zU1SCOo!+73r7{219!8z?Heag00ASbKXAZ{t&K&elt+rwn+54GK6VFM#<~v0v!681l zNBO{iob+s_^uCh}IqqA*A(G&vYkjGsko)7Vx4-f3Q17wZ$})=z9wSrzQ^S^(+F#(L zH~Qv3-St-|J=RGb@Lu3LQ$@XclYFCT_1+M5%emr;#A%gyJ($&7y z0rX>k*W0Q0O7;w)Z2KRw);`Ent~}$9FNWI>I%JXmLTk?x=!PRTlL}&PO{vxZ3-^_h z61S#IL(2bdnd2(;?h4-?>Yjml(8ZiS5s&Gtjgy_U;FR{fT_t&-VGugOy+?O2pFl$S_?+fo}vm)-7eX+3HRqD8l z%Mw?xT9Z6uCb>Wk@>F)CgBUrmKI|>~4ugZIS?mqL@y>BhL*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KPc=?{SSj%51pn&-KQ8!n0)I>JW&9WE_oF0#mEij{?#(|71^-6Dug=3? zDfl-D{^~sZ2Ekt~_^~|vHi6$K_(_4^FYsH1{J$6Y9fB|YbC1B~_}wMsd``&OFZePZ zjtTr91;2u+we?Z{Z3jWOkUnBU^{;0sE zJ*`5{Eke$10;jf_cBO^S&7#{27f`dhpE}pXI@~YJ8ptH{Tmx?ZM6S z%4!e3L(2(!@SPfWJb0hRBOd%7jnkY<#q{qAzq+OzOU2;#l&EXUT~rLdOXD{yIEz1_ z@wFcO1&ud(@V7PI?7@B79?D%*jNX98I~1J7t2LhV;7uCe?7>g!{nb_v{)EQ6J-FG2 z^my>CI$(Er@Q3w&c&7)i(|Df;FVpxv9{dmb`NjPnyj0^4co>UhZqfX^ z1%sRK6Y2X&Dh4;-A)5EI1~=a!R_hKnxcLt8W)E&Y0_*VLrKL)r`S$}xj`_}x{`b^W z3~s)&E2WMVxcSbG{#R@gH{aP^?ZM4=cJ%$5_6S_3Dn+bFY-z*4A-5{<-G(|g zo3qYnYePjcx;bf8@MkL(>krYjJR_XU&T5Pucrat8}I;U|U zOZO+#)q@hmxenhfx?E$`Swx+zrbQl#8Gq#earl|`8~--|BmLg~x9MdL%4&7U{`LCv zX51OA|H~Rxl|EgQ^wUz#t3M8m^y}#fXR2)0`VHUU=Gq*$;>Kw8yLEl9z2@2+@6h!t zjRJk_)Ae;XWbcwLD(3h#an3jNpS`-i#OU1AGx#ozdHcU#*Z-xb!%e$Q{fBTen*Voc z|5t0l;EsGCtDN% literal 0 HcmV?d00001 diff --git a/src/parser.o b/src/parser.o new file mode 100644 index 0000000000000000000000000000000000000000..0cdd296b3e6d45a1381d4f89dc493d8414779b6b GIT binary patch literal 2752 zcmbtV&2Jl35FclqHVvuQDMbouA(n7Zis;I28fZBnZ$1~aYAYHem6B{6dtHarAN8)G z1qmABfL6h%NFc;5NQes@dT0;jfE*PO2@dqge^88&qNq}71tQG6eXm~DwuE3JzxmB? zKHl4TA9-g|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d=y=Mv{(YYQ+1{IU4?loPkEy?}S(zf0)zskjIn zOkjJBWJx61AH^m{CaW`rsWhM@J)As}(39ySLqns3W9czHksM5ubb8L3DcQMnHa|<| zOv@@;+-5E_6(c#zG)Xa2D3r4;xWr$Zhe8_82shU3v%?~t&s6dRn>0T@T;uF8k$K=y zk&LD?b_QYvt?Vp@n$Bfr3W5SvJK8f4%!+0)J9m<$=+xsx#wyK}U^0E1mkmD$uDZ|h z>J4%i$FaPsgdgNM>UT*v?)@mJAAq0W`5A&Bh~wH5_#}cLh~ouR;HwCNxb=VKz-{_s zp164)g!zHu=INJk+3y(%m*ad{!e#xpCHz+KBjW#)gv&gClW>{mVJswwJI)~qm-Sa9 zd>_~ezYPh;y-eUANw^%(KO|h{{|R4z?s&!}T-JX@!ta#)zANE!oZmrW5PZmSJ|N+8 zoVtX|ev1+=`_+&f1k>XwvVNSkE&OrbKjy+0d4I))-{Ac>UAWk9ueflr-{Ss-BH|JI z>isTU?5p!GT?;$)cT^gr{ve&%AiE%t9t&oAWk_ux%wWS*ehS zRmKjg$0B`OyD<{UmWxHR1Qw;TZAONFoVu57MWBadWGDXwL_5W!#FvgVJCwd$( zl(Ql~r`vWm;G9C0I3E{}OZ<8y>WN+SBW@u0&l=@>@I#!J7a=Aex%>*8mtS<*9UD2n zu6+z_k@JU5M(0KN$NPdTB7PA^90ohtAL3Uh+^b{}cH)Wm1ALFlO7NcK_TP8m!cIJK zzZdVa>Tzx_WBBgYEpkj=yQnwM?Zd7Ckw5C7@&Yzdb Date: Tue, 28 Apr 2020 19:19:45 +0200 Subject: [PATCH 27/59] remove .o files --- src/debug.o | Bin 1584 -> 0 bytes src/hash.o | Bin 2584 -> 0 bytes src/node.c | 4 ++-- src/node.o | Bin 27552 -> 0 bytes src/parser.o | Bin 2752 -> 0 bytes src/tlv.o | Bin 5024 -> 0 bytes 6 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 src/debug.o delete mode 100644 src/hash.o delete mode 100644 src/node.o delete mode 100644 src/parser.o delete mode 100644 src/tlv.o diff --git a/src/debug.o b/src/debug.o deleted file mode 100644 index f402994105591610dd5628c2fc860deaac488e3e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1584 zcmbtUL2DCH5T0$+wi=}tkzzdTMNzSjU2CxjmTh8^3X2781PS`sq+28HCS~^ps)u^j zgWyU0H+uB1c+;y0b>`&_Szk6Dbi(e;d~d#a^Jd=WO+9SPI1aElunuF7QGo9=V>{4h z0B4~Lo9|c2+kE%a|MHoL-^i`BSNx=Xzg}%MpHId2=k+6S4i}tCd3Npt<|q8JUIDmh^xqVfbOL@H7sZqc zPYO`NUy+nBQ<0S5WD$&6W~bnD6ZpJFrsX*`5q}Pm#a#W|i%WE z3nHv}aFD+2E2$xn&2X!_5z4Jb<8ggkZf{h>x&$e8%&zSA_GlaK6kQKG)vx1`EYczh zb*zjJ<1X4H6ra0N03v&psHlTlr6b*AD~x_cN$?6@a)(L+m3y1_~);Z}t?Tlac`Oo805&x|zsyC);%jW+E%o}ct diff --git a/src/hash.o b/src/hash.o deleted file mode 100644 index 9de82471d9309121a76665a50f123532c62eb3f9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2584 zcmbtU&2Jk;6d$i$$2jS((<0%jNb#XTF=|&fZJ{a>c$3(kpj9Ign}XV~IF600_(QVS zkfN#}xv5w!L0sTJ;KqSd!T}_w2~v9Dh$?!CkV+Fks8vK{MAY)$?o2W=4npv(o%iN9 zzxOe3X8e9qPe~pR;o>1Lk^LD*2?_V@+X=@^kO9(9wv}3+GU#7bhHf}0wbKE8F%_hi zUs+CvidHJ1EGg8!7;gO$XdK&F@ip~@+h)JAB<-w7AEtuM>^3+PYD?5g1+8^zUz0i$ z)-}?hi`S3P*8R>zw4+CFjeAz8_5I40cbk5`T2y&HTXMYYjI^s+8L|tpKEOh(b!9mt zJt`vjNZ@Cwb#IDV+qCuTxmjxahDU%*t>k9Btt|EFi!?}E?PJ3aynsg6sP)q(Mx%?_ zwr+h#t^3ZjsML~eY9}{m>1j8DGU#pksXg7fy~-G@AKGhLcEZ}xGFiy_CuEHYS;tR| z#M=&kWy44F9XHp)-3e>WeCL}GB%ZMD6_ufJsK9YcU{Qn$|9B)7qO`xWD~=#&;m)FUw9MWhjB? z>sJPSPpy*l*x32VNOq=KHJgza)U#^r)aj-(i(ikQQ)6eME{gF8dD2zH^WLk(GavFC zALt8w0v8K6^)y=xFpXG@*`F~T0S()BfWqG|mJfJ?Xr@-+^l za_D1eR1PnClXB!EpC*soJfz97mOm}OCEWfyaU%5dyC3h(z2 z*vS9VK?!ks4t5ki&SguEdiel6bpU=5aEu>ee=g2>0XBO8zj6R>9)Mp39OG}W_z#DDW(p7fK^P;s7GShqdiK zcI6?0Ab1l7;N$pFwhzP)3;39TD*}E&!126zoEpP1Z&1KL75HM_I|44|y(Qp-LYxTL zC>S3x8&i?+! zj>7_;eS#C1F0rS6ht2UWVJQB-!wi3h;obi{QSp!%7gbj|7iCQ_&VW&M-PznlI#1KMIxuzCMD{0bPo`k!D2I?ssMfBaYG{=EOVkM7*) v5@rouXB)*giN4+b7`MCk0FezW3S_}mdn4}~2k?#d^8bp}{{|;A-R=Ju5(aYf diff --git a/src/node.c b/src/node.c index 40b127a..d420d2a 100644 --- a/src/node.c +++ b/src/node.c @@ -192,7 +192,7 @@ pub_data *get_data(int64_t id) { while(tmp != NULL) { data = (pub_data*) tmp->data; - + tmp = tmp->next; if(data->id == id) return data; } @@ -464,7 +464,7 @@ int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, in ((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length); // Vectorized buffer - struct iovec vec_buff[1]; + struct iovec vec_buff[1]; vec_buff[0].iov_len = length + 4; vec_buff[0].iov_base = packet_buff; diff --git a/src/node.o b/src/node.o deleted file mode 100644 index 7dc7f55f97aeacd9de06e9925c9963160d85aef8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 27552 zcm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$5_6S_3Dn+bFY-z*4A-5{<-G(|g zo3qYnYePjcx;bf8@MkL(>krYjJR_XU&T5Pucrat8}I;U|U zOZO+#)q@hmxenhfx?E$`Swx+zrbQl#8Gq#earl|`8~--|BmLg~x9MdL%4&7U{`LCv zX51OA|H~Rxl|EgQ^wUz#t3M8m^y}#fXR2)0`VHUU=Gq*$;>Kw8yLEl9z2@2+@6h!t zjRJk_)Ae;XWbcwLD(3h#an3jNpS`-i#OU1AGx#ozdHcU#*Z-xb!%e$Q{fBTen*Voc z|5t0l;EsGCtDN% diff --git a/src/parser.o b/src/parser.o deleted file mode 100644 index 0cdd296b3e6d45a1381d4f89dc493d8414779b6b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2752 zcmbtV&2Jl35FclqHVvuQDMbouA(n7Zis;I28fZBnZ$1~aYAYHem6B{6dtHarAN8)G z1qmABfL6h%NFc;5NQes@dT0;jfE*PO2@dqge^88&qNq}71tQG6eXm~DwuE3JzxmB? zKHl4TA9-g|pXv$(Ku7?dho&W1fXlm^IKf~7_Cg=1FRRIKzehryNuJi6KeXlFN?ib& zb9*xw4I-~?PB0Bxtr*l+Hhc8i7twW`BBQMZhlVM_sYY{{6R&GdL)(1+IxCO-q5I1l z8)$4aq&wPIx^r7wdxU(y(4ei>?U-J>7QKarUi&)wBayXH^2O^ItUKGf^DB#8xz{CE ze?Ui8E(W98S~XfnL++QfwU61<8Es@gTMGtEM8lf1LIIldv)n%(W?TeOgrOLs7()p} z35HG~I>pdDqIrf&h)Rg6(aTs%?q4Cr>?^KKirMR~O^Vr9U7M-ZEi|;{@2II~XVq8L zS#|EFJSe{O2DMmsw(fi>e_B5X)p|wYJ-E@!Jij+RGq@wjLF4XUJ3I1VmGFZ)-H5$) zzIw@@Qa}F8e2;O z(57S8cm-`X$B-2nIy<+gcNWU#1t^${=W+|hN*R&;MzOpUvFwy>LZO&3IV=?|8(?~T z{JF?*a-mYNE0JfFQ6+YGq{6KD+wo&c?C2qv#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5p!GT?;$)cT^gr{ve&%AiE%t9t&oAWk_ux%wWS*ehS zRmKjg$0B`OyD<{UmWxHR1Qw;TZAONFoVu57MWBadWGDXwL_5W!#FvgVJCwd$( zl(Ql~r`vWm;G9C0I3E{}OZ<8y>WN+SBW@u0&l=@>@I#!J7a=Aex%>*8mtS<*9UD2n zu6+z_k@JU5M(0KN$NPdTB7PA^90ohtAL3Uh+^b{}cH)Wm1ALFlO7NcK_TP8m!cIJK zzZdVa>Tzx_WBBgYEpkj=yQnwM?Zd7Ckw5C7@&Yzdb Date: Tue, 28 Apr 2020 19:36:30 +0200 Subject: [PATCH 28/59] messages are now added to the data_list --- src/node.c | 202 ++++++++++++++++++++++++----------------------------- 1 file changed, 92 insertions(+), 110 deletions(-) diff --git a/src/node.c b/src/node.c index 4205132..6aacb35 100644 --- a/src/node.c +++ b/src/node.c @@ -219,22 +219,6 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // If id is the same as this node's id then we only update seqno if(id == NODE_ID) { - // pub_data *node_data = get_data(NODE_ID); - // - // if (node_data == NULL) { - // if (datagram == NULL) { - // data_list = (list*) malloc(sizeof(list)); - // } - // data_list->data = (void*) new_data; - // data_list->next = NULL; - // } - // - // if(seqno >= node_data->seqno) { - // node_data->seqno = seqno ^ 1; - // } - // - // return 1; - // We create our pub_data. pub_data * message = malloc(sizeof(struct pub_data)); message->length = len; @@ -243,103 +227,101 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { message->data = data; // If the data list has never been used, or is empty ( same thing ) - if (data_list = NULL) { - data_list = (list*) malloc(sizeof(list)); - } else { - // Otherwise, we move until the last element of the dala_list, - // and add or data there. - - // We use a temporary address to avoid writing to the static list. - // Seems weird but ok. - list *tmp = data_list; - while(tmp->next != NULL){ - tmp = tmp->next; - } - - // We create the next node of the linked list. - list * new_node = malloc(sizeof(struct list)); - new_node->data = (void *) message; - - // Adding the message to the list. - tmp->next = (void *) new_node; + if (data_list == NULL) { + data_list = (list*) malloc(sizeof(struct list)); + } + // we move until the last element of the dala_list, + // and add or data there. + // We use a temporary address to avoid writing to the static list. + // Seems weird but ok. + list *tmp = data_list; + while(tmp->next != NULL){ + tmp = tmp->next; } - } else { + // We create the next node of the linked list. + list * new_node = malloc(sizeof(struct list)); + new_node->data = (void *) message; + + // Adding the message to the list. + tmp->next = (void *) new_node; + + return 1; + } else { + // Copy data + pub_data *new_data = copy_data(len, id, seqno, data); + + if(data_list == NULL) { + // Update list + data_list = (list*) malloc(sizeof(list)); + data_list->data = (void*) new_data; + data_list->next = NULL; + + return 2; + } + + // Find correct position for new data + list *tmp = data_list; + list *last = NULL; + list *new_node; + int64_t cur_id; + + while(tmp != NULL) { + cur_id = ((pub_data*) tmp->data)->id; + + // If id is smaller than cur_id then the new data has to be added at this position + if(id < cur_id) { + // If last hasn't been set then the new data becomes the head of the list + if(last == NULL) { + // Update list + data_list = (list*) malloc(sizeof(list)); + data_list->data = (void*) new_data; + data_list->next = tmp; + + return 2; + } + + // Else, we update the last node + new_node = (list*) malloc(sizeof(list)); + new_node->data = (void*) new_data; + new_node->next = tmp; + last->next = new_node; + + return 1; + } else if(id == cur_id) { + // If data already exists for this id then we update it if it's seqno is greater than the one stored + pub_data *cur_data = (pub_data*) tmp->data; + + if(seqno > cur_data->seqno) { + // Updata data + tmp->data = (void*) new_data; + + // Free old data + free(cur_data); + + return 2; + } + + // seqno is smaller so the new data allocated is freed and nothing else is done + free(new_data); + + return 2; + } + + // Get next node in list + last = tmp; + tmp = tmp->next; + } + + // If no correct position was found then the new data has to be added at the end of the list + + // Update list + new_node = (list*) malloc(sizeof(list)); + new_node->data = (void*) new_data; + new_node->next = NULL; + last->next = new_node; + return 3; } - - // Copy data - pub_data *new_data = copy_data(len, id, seqno, data); - - if(data_list == NULL) { - // Update list - data_list = (list*) malloc(sizeof(list)); - data_list->data = (void*) new_data; - data_list->next = NULL; - - return 2; - } - - // Find correct position for new data - list *tmp = data_list; - list *last = NULL; - list *new_node; - int64_t cur_id; - - while(tmp != NULL) { - cur_id = ((pub_data*) tmp->data)->id; - - // If id is smaller than cur_id then the new data has to be added at this position - if(id < cur_id) { - // If last hasn't been set then the new data becomes the head of the list - if(last == NULL) { - // Update list - data_list = (list*) malloc(sizeof(list)); - data_list->data = (void*) new_data; - data_list->next = tmp; - - return 2; - } - - // Else, we update the last node - new_node = (list*) malloc(sizeof(list)); - new_node->data = (void*) new_data; - new_node->next = tmp; - last->next = new_node; - - return 1; - } else if(id == cur_id) { - // If data already exists for this id then we update it if it's seqno is greater than the one stored - pub_data *cur_data = (pub_data*) tmp->data; - - if(seqno > cur_data->seqno) { - // Updata data - tmp->data = (void*) new_data; - - // Free old data - free(cur_data); - - return 2; - } - - // seqno is smaller so the new data allocated is freed and nothing else is done - free(new_data); - - return 2; - } - - // Get next node in list - last = tmp; - tmp = tmp->next; - } - - // If no correct position was found then the new data has to be added at the end of the list - - // Update list - new_node = (list*) malloc(sizeof(list)); - new_node->data = (void*) new_data; - new_node->next = NULL; - last->next = new_node; - return 3; } /* ---- Fin fonctions utilitaires ---- */ From fae758e4dd50240d985e55e52a21acedc064bb31 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 28 Apr 2020 19:51:53 +0200 Subject: [PATCH 29/59] Fixed adding message to data list before receiving messages --- src/node.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/node.c b/src/node.c index 7c8594f..d8c4dec 100644 --- a/src/node.c +++ b/src/node.c @@ -229,23 +229,29 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // If the data list has never been used, or is empty ( same thing ) if (data_list == NULL) { data_list = (list*) malloc(sizeof(struct list)); + + list *tmp = data_list; + // We create the next node of the linked list. + tmp->data = (void *) message; + tmp->next = NULL; + + } else { + // we move until the last element of the dala_list, + // and add or data there. + // We use a temporary address to avoid writing to the static list. + // Seems weird but ok. + list *tmp = data_list; + while(tmp->next != NULL){ + tmp = tmp->next; + } + + // We create the next node of the linked list. + list * new_node = malloc(sizeof(struct list)); + new_node->data = (void *) message; + + // Adding the message to the list. + tmp->next = (void *) new_node; } - // we move until the last element of the dala_list, - // and add or data there. - // We use a temporary address to avoid writing to the static list. - // Seems weird but ok. - list *tmp = data_list; - while(tmp->next != NULL){ - tmp = tmp->next; - } - - // We create the next node of the linked list. - list * new_node = malloc(sizeof(struct list)); - new_node->data = (void *) message; - - // Adding the message to the list. - tmp->next = (void *) new_node; - return 1; } else { // Copy data From 70ca7f5f109a5d10569022b7c89a9fd3ff597c0c Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 28 Apr 2020 20:12:59 +0200 Subject: [PATCH 30/59] Removec old comment --- src/node.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/node.c b/src/node.c index d8c4dec..b71cf1e 100644 --- a/src/node.c +++ b/src/node.c @@ -234,7 +234,7 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // We create the next node of the linked list. tmp->data = (void *) message; tmp->next = NULL; - + } else { // we move until the last element of the dala_list, // and add or data there. @@ -755,7 +755,6 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack int add_message(char * message, int message_len){ int seqno = 1337; - // int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { int rc = add_data((unsigned char) message_len, (int64_t) NODE_ID ,(int16_t) seqno, message); if (rc > 0) { print_debug(">> Message added."); From 904f0fcc793a656c7928e8f3c2e11a24f660483c Mon Sep 17 00:00:00 2001 From: gonzalef Date: Tue, 28 Apr 2020 20:22:53 +0200 Subject: [PATCH 31/59] big endian/little endian conversions --- src/node.c | 10 +++++----- src/tlv.c | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/node.c b/src/node.c index d8c4dec..e288341 100644 --- a/src/node.c +++ b/src/node.c @@ -836,7 +836,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s memset(&new_neighbour, 0, sizeof(new_neighbour)); new_neighbour.sin6_family = AF_INET6; memcpy(&new_neighbour.sin6_addr, &cur_tlv.neighbour->ip, 16); - new_neighbour.sin6_port = htons(LISTEN_PORT); + new_neighbour.sin6_port = ntohs(cur_tlv.neighbour->port); new_neighbour.sin6_scope_id = ifindex; // Build network hash @@ -907,7 +907,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s //if the hashes are identical nothing has to be done print_debug(">> Received node hash, updating message entry..."); cur_tlv.node_hash = (node_hash*) (data + pos); - pdata = get_data(cur_tlv.node_hash->node_id); + pdata = get_data(ntohl(cur_tlv.node_hash->node_id)); // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { @@ -926,7 +926,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s } // If no pub_data was found or the hashes differ then we send a node state request - build_node_state_req(&new_tlv, cur_tlv.node_hash->node_id); + build_node_state_req(&new_tlv, ntohl(cur_tlv.node_hash->node_id)); add_tlv(&pack, &new_tlv, sender, socket_num); // The position is updated @@ -940,7 +940,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s // if no pub_data exists for this id nothing is sent print_debug(">> Received node state request. Processing..."); cur_tlv.node_state_req = (node_state_req*) (data + pos); - pdata = get_data(cur_tlv.node_state_req->node_id); + pdata = get_data(ntohl(cur_tlv.node_state_req->node_id)); if(pdata != NULL) { build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); @@ -963,7 +963,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s if (DEBUG_LEVEL > 0) { printf("\n\t %s \n", (char *) cur_tlv.node_state->data); } - int rc = add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); + int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data); if (rc < 0) { print_debug(">> Error while adding note state !"); } diff --git a/src/tlv.c b/src/tlv.c index 140a9fb..70207a7 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -145,8 +145,8 @@ int build_node_hash(tlv *tlv, int64_t node_id, int16_t seqno, char *data) { new->type = 6; new->length = 26; - new->node_id = node_id; - new->seqno = seqno; + new->node_id = htonl(node_id); + new->seqno = htons(seqno); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; hash_data(&pdata, (unsigned char*) new->node_hash); @@ -167,7 +167,7 @@ int build_node_state_req(tlv *tlv, int64_t node_id) { new->type = 7; new->length = 8; - new->node_id = node_id; + new->node_id = htonl(node_id); tlv->node_state_req = new; @@ -192,8 +192,8 @@ int build_node_state(tlv *tlv, int64_t node_id, int16_t seqno, char *data, size_ new->type = 8; new->length = 26 + len; - new->node_id = node_id; - new->seqno = seqno; + new->node_id = htonl(node_id); + new->seqno = htons(seqno); memcpy(new->data, data, len); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; From 06fa230463f491c592f8e9af42cb2320b5322fa8 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 13:59:58 +0200 Subject: [PATCH 32/59] Added gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2aad53d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +dazibao +*.o +debug.* From 055ecfd0ba3760a67141c476e1733490796a0bc1 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 14:30:05 +0200 Subject: [PATCH 33/59] Revert "Merge branch 'master' into add-message" This reverts commit 8da14978d6a41dc520964b9f310851ecdc478a26, reversing changes made to 704ed2a5239cf28cae8c84ed1ca212511734119d. --- .gitignore | 3 --- src/node.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++---- src/node.h | 2 -- 3 files changed, 53 insertions(+), 9 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 2aad53d..0000000 --- a/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -dazibao -*.o -debug.* diff --git a/src/node.c b/src/node.c index 40d52fb..41e648c 100644 --- a/src/node.c +++ b/src/node.c @@ -21,10 +21,59 @@ static list *neighbour_list; /* ---- Fonctions utilitaires ---- */ -void debug_print(char message_debug){ - if (debug_flag == 1) { - printf("\x1b[33m\x1b[4m>> Debug :\x1b[0m\x1b[33m %s\x1b[0m\n", message_debug ); - } +// Looks for more peers +int ask_for_peers(int socket_num) { + print_debug(">> Asking for more peers..."); + // Only ask for more peers if the neighbour list is small enough + int nbr_peers = len_list(neighbour_list); + if( nbr_peers >= 5){ + return 0; + } else if (nbr_peers <= 0){ + print_debug(">> No peers found in the peer list, something terrible happened."); + return -1; + } else { + + // Get random peer + neighbour_peer *peer = get_random_neighbour(); + struct in6_addr ip = peer->ip; + int16_t port = peer->port; + + /*int ifindex = if_nametoindex("enp3s0"); + if(ifindex == 0) { + int ifindex = if_nametoindex("eth0"); + if(ifindex == 0) { + perror("if_nametoindex failed"); + return -1; + } + }*/ + + int ifindex = 0; + + // Initialize sockaddr + struct sockaddr_in6 dest; + memset(&dest, 0, sizeof(struct sockaddr_in6)); + dest.sin6_family = AF_INET6; + memcpy(&dest.sin6_addr, &ip, 16); + dest.sin6_port = htons(port); + dest.sin6_scope_id = ifindex; + + // Send neighbour request TLV + tlv neighbour_req; + neighbour_req.pad1 = NULL; + int rc = build_neighbour_req(&neighbour_req); + if (rc < 0) { + print_debug(">> Failed to build neighbour_req"); + } + + rc = send_single_tlv(&neighbour_req, &dest, socket_num); + if (rc < 0) { + print_debug(">> Error while sending a TLV."); + return -1; + } else { + print_debug(">> Send TLV."); + return 0; + } + } } // Get list length diff --git a/src/node.h b/src/node.h index 444ca0c..a45a61e 100644 --- a/src/node.h +++ b/src/node.h @@ -121,8 +121,6 @@ int bootstrap_node(int * sock_fd); // Helper functions int len_list(list *l); -void debug_print(char message_debug); - neighbour_peer *get_random_neighbour(); // Search for this peer in the neighbour table From ba0cf32cb00435c809aad24ab3caefcc762c26de Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 16:17:22 +0200 Subject: [PATCH 34/59] Added new print_error function Added checks for NULL values Added comments and quesitons as // TODO --- src/debug.c | 7 +++++ src/debug.h | 1 + src/hash.c | 25 +++++++++++---- src/node.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++------- src/tlv.c | 10 ++++-- 5 files changed, 114 insertions(+), 20 deletions(-) diff --git a/src/debug.c b/src/debug.c index bf3f3e8..e00c265 100644 --- a/src/debug.c +++ b/src/debug.c @@ -9,3 +9,10 @@ void print_debug(char * msg){ getchar(); } } + +void print_error(char * msg){ + printf("\x1b[41m[97m[ERROR] %s \x1b[0m\n", msg); + if (DEBUG_LEVEL > 1) { + getchar(); + } +} diff --git a/src/debug.h b/src/debug.h index bf199d7..33e51db 100644 --- a/src/debug.h +++ b/src/debug.h @@ -5,4 +5,5 @@ void print_debug(char * msg); +void print_error(char * msg); #endif diff --git a/src/hash.c b/src/hash.c index a205bad..a3665a0 100644 --- a/src/hash.c +++ b/src/hash.c @@ -1,4 +1,5 @@ #include "hash.h" +#include "debug.h" // Hash a single data void hash_data(pub_data *data, unsigned char *buf) { @@ -34,7 +35,9 @@ void hash_network(list *data_list, unsigned char *buf) { } // Hash all of concat to obtain the network hash - SHA256(concat, totlen, hash); + if (SHA256(concat, totlen, hash) == NULL) { + print_debug(">> Doing the hash failed : function return NULL, should return a pointer."); + }; // Put truncated hash into buf hash_trunc(hash, buf); @@ -46,17 +49,27 @@ void hash_network(list *data_list, unsigned char *buf) { // Truncate 32 octet hash to 16 octets void hash_trunc(unsigned char *hash32oct, unsigned char *buf) { // Copy the first 16 octets from hash32oct - memcpy(buf, hash32oct, 16); + if (memcpy(buf, hash32oct, 16) == NULL) { + print_debug(">> Truncating the hashs didn't work !"); + } } // Concat all fields of data and put them in buf void concat_data(pub_data *data, unsigned char *buf) { - memcpy(buf, &(data->id), 8); - memcpy(buf+8, &(data->seqno), 2); - memcpy(buf+10, data->data, data->length); + if (memcpy(buf, &(data->id), 8) == NULL) { + print_debug(">> Concat the data (id) didn't work !"); + }; + if (memcpy(buf+8, &(data->seqno), 2) == NULL) { + print_debug(">> concat the data (seqno) didn't work !"); + } + if (memcpy(buf+10, data->data, data->length) == NULL) { + print_debug(">> Contact the data (data) didn't work !"); + } } // Concat hash2 to hash1 (hash1 is modified) void concat_hash(unsigned char *hash1, unsigned char *hash2, size_t size) { - memcpy(hash1+size, hash2, 16); + if(memcpy(hash1+size, hash2, 16) == NULL){ + print_debug(">> Concat the hash didn't work !"); + } } diff --git a/src/node.c b/src/node.c index 41e648c..809b4b2 100644 --- a/src/node.c +++ b/src/node.c @@ -204,11 +204,19 @@ pub_data *get_data(int64_t id) { pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) { pub_data *new_data = (pub_data*) malloc(sizeof(pub_data)); char *_data = (char*) malloc(len); + if (_data == NULL) { + print_error("Failed to allocate memory for copying the data !"); + return NULL; + } new_data->length = len; new_data->id = id; new_data->seqno = seqno; new_data->data = _data; - memcpy(_data, data, len); + if(memcpy(_data, data, len) == NULL){ + print_error("Failed to copy data !"); + return NULL; + } + return new_data; } @@ -221,6 +229,11 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { if(id == NODE_ID) { // We create our pub_data. pub_data * message = malloc(sizeof(struct pub_data)); + if (message == NULL) { + print_error("Failed to allocate memory for the message !"); + return -1; + } + message->length = len; message->id = id; message->seqno = seqno; @@ -229,7 +242,10 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // If the data list has never been used, or is empty ( same thing ) if (data_list == NULL) { data_list = (list*) malloc(sizeof(struct list)); - + if (data_list == NULL) { + print_error("Failed to allocate memory to create the data_list !"); + return -1; + } list *tmp = data_list; // We create the next node of the linked list. tmp->data = (void *) message; @@ -247,19 +263,33 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // We create the next node of the linked list. list * new_node = malloc(sizeof(struct list)); + if (new_node == NULL) { + print_error("Failed to allocate memory for the new node in data list."); + return -1; + } new_node->data = (void *) message; + new_node->next = NULL; // Adding the message to the list. tmp->next = (void *) new_node; + } return 1; } else { // Copy data pub_data *new_data = copy_data(len, id, seqno, data); + if (new_data == NULL) { + print_error("Failed to copy data to new_data !"); + return -1; + } if(data_list == NULL) { // Update list data_list = (list*) malloc(sizeof(list)); + if (data_list == NULL) { + print_error("Failed to allocate memory to create the data_list !"); + return -1; + } data_list->data = (void*) new_data; data_list->next = NULL; @@ -280,7 +310,11 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // If last hasn't been set then the new data becomes the head of the list if(last == NULL) { // Update list - data_list = (list*) malloc(sizeof(list)); + data_list = (list*) malloc(sizeof(struct list)); + if (data_list == NULL) { + print_error("Failed to allocate memory to create the data_list !"); + return -1; + } data_list->data = (void*) new_data; data_list->next = tmp; @@ -288,7 +322,11 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { } // Else, we update the last node - new_node = (list*) malloc(sizeof(list)); + new_node = (list*) malloc(sizeof(struct list)); + if (new_node == NULL) { + print_error("Failed to allocate memory for the new node in data list."); + return -1; + } new_node->data = (void*) new_data; new_node->next = tmp; last->next = new_node; @@ -323,6 +361,10 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // Update list new_node = (list*) malloc(sizeof(list)); + if (new_node == NULL) { + print_error("Failed to allocate memory for the new node in data list."); + return -1; + } new_node->data = (void*) new_data; new_node->next = NULL; last->next = new_node; @@ -419,6 +461,8 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { sent = 1; } } else { + // In case we need to add a padding packet, and the packet is full, + // we just ignore that padding, and send out the packet. if(pack->length >= 1020) { errval = send_packet((char*) pack, pack->length, dest, socket_num); *pack = (packet) {.magic = 95, .version = 1, .length = 0}; @@ -480,9 +524,10 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { print_debug(">> Finished adding the TLVs to the packet"); - // If the previous packet was went return 1 or -1 if there was an error sending it - if(sent) + // If the previous packet was sent return 1 or -1 if there was an error sending it + if(sent){ return errval? -1:1; + } // Return 0 if the TLV was added to the packet return 0; @@ -774,14 +819,23 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s int pos = 4; unsigned char tlv_len, hash[16]; char warn[32]; - tlv new_tlv, cur_tlv; + + // The TLV we are going to send back. + tlv new_tlv; + // The tlv we are currently looking at. + tlv cur_tlv; + new_tlv.pad1 = NULL; cur_tlv.pad1 = NULL; list *tmp_list; pub_data *pdata; + + // holds the random neighbour we send back in case of a neighbour_req. struct neighbour_peer *random_neighbour; + // Holds the new neighbour send to us by a "neighbour tlv" struct sockaddr_in6 new_neighbour; + // We create the packet in which we are going to send back our responses. packet pack = (packet) {.magic = 95, .version = 1, .length = 0}; memset(pack.body, 0, 1020); @@ -797,8 +851,8 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s */ int ifindex = 0; - ifindex = 0; while(pos < total_packet_len) { + // Making sure the current TLV we are looking at is valid. switch(validate_tlv(data, pos, total_packet_len)) { case 0: // We received a padding tlv so it is ignored @@ -818,7 +872,15 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s print_debug(">> Received neighbour request, sending out a neighbour address."); // Send a neighbour tlv random_neighbour = get_random_neighbour(); + if (random_neighbour == NULL) { + print_debug(">> Failed to get a random neighbour, failing..."); + return -1; + } + + // TODO : Seems to be a bug here, as this frees the new_tlv build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port); + // TODO : I suppose that since new_tlv is freed above, add_tlv ads an empty + // value to the packet ? add_tlv(&pack, &new_tlv, sender, socket_num); // The position is updated @@ -938,7 +1000,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s // so a node state tlv for this node id has to be sent, // if no pub_data exists for this id nothing is sent print_debug(">> Received node state request. Processing..."); - cur_tlv.node_state_req = (node_state_req*) (data + pos); + cur_tlv.node_state_req = (node_state_req*) (data[pos]); pdata = get_data(ntohl(cur_tlv.node_state_req->node_id)); if(pdata != NULL) { @@ -960,11 +1022,15 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s print_debug(">> Received message ! "); if (DEBUG_LEVEL > 0) { - printf("\n\t %s \n", (char *) cur_tlv.node_state->data); + if (cur_tlv.node_state->data == NULL) { + print_error("The data in the current node is NULL !"); + return -1; + } + printf("\x1b[31m[DEBUG]\x1b[0m >> “%s”\n", cur_tlv.node_state->data); } int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data); if (rc < 0) { - print_debug(">> Error while adding note state !"); + print_debug(">> Error while adding node state !"); } // The position is updated tlv_len = data[pos+1]; @@ -1172,6 +1238,9 @@ int run_node(int sock_fd){ } } } else { + // TODO : Here, we can write all of the current messages we have in stack + // to a file, or to stdout. + // TODO : Same as above, but for the peers have know about. continue; } diff --git a/src/tlv.c b/src/tlv.c index 70207a7..ab66f6a 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -1,5 +1,6 @@ #include "tlv.h" +// TODO this can be deleted ? // creer un tlv int build_tlv(tlv *tlv, cmd_token token) { switch(token.type) { @@ -25,6 +26,7 @@ int build_tlv(tlv *tlv, cmd_token token) { return -1; } +// end deletion int build_pad1(tlv *tlv) { // Free the previously allocated memory @@ -62,9 +64,7 @@ int build_padn(tlv *tlv, size_t len) { int build_neighbour_req(tlv *tlv) { // Free the previously allocated memory - // if (tlv != NULL) { - // free(tlv->pad1); - // } + free(tlv->pad1); neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req)); @@ -82,6 +82,10 @@ int build_neighbour_req(tlv *tlv) { int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) { // Free the previously allocated memory + // TODO : why do we free this part ? Doesn't it mean that, once the + // tlv has entered this function, it's not on the heap anymore, + // and thus, setting a value will not be accessible from other part of the + // code ? free(tlv->pad1); neighbour *new = (neighbour*) malloc(sizeof(neighbour)); From ab1f278685b34ac305ed41a5593e6bb8ebcbc6ad Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 16:19:05 +0200 Subject: [PATCH 35/59] Added another print_error --- src/node.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node.c b/src/node.c index 809b4b2..e8b3e31 100644 --- a/src/node.c +++ b/src/node.c @@ -1030,7 +1030,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s } int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data); if (rc < 0) { - print_debug(">> Error while adding node state !"); + print_error("Error while adding node state !"); } // The position is updated tlv_len = data[pos+1]; From 72b00cb07d0b99f567c36b5c1da31fa476466e33 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 16:40:01 +0200 Subject: [PATCH 36/59] Added printing the peer list --- src/debug.c | 41 ++++++++++++++++++++++++++++++++++++++++- src/debug.h | 6 ++++++ src/node.c | 4 ++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/debug.c b/src/debug.c index e00c265..1c46f4e 100644 --- a/src/debug.c +++ b/src/debug.c @@ -1,11 +1,13 @@ #include #include "debug.h" +#include "node.h" +#include void print_debug(char * msg){ if (DEBUG_LEVEL > 0) { printf("\x1b[31m[DEBUG]\x1b[0m %s \n", msg); } - if (DEBUG_LEVEL > 2) { + if (DEBUG_LEVEL > 9) { getchar(); } } @@ -16,3 +18,40 @@ void print_error(char * msg){ getchar(); } } + +// typedef struct neighbour_peer { +// struct in6_addr ip; +// int16_t port; +// char is_temporary; +// time_t last_seen; +// } neighbour_peer; + +void print_peers(list * l){ + // Print out the peers we have in the neighbour_list + int nbr_peers = 0; + if (l == NULL) { + print_error("The neighbour_list is empty !"); + } else { + print_debug(">> Printing out peer list :"); + while(l != NULL){ + neighbour_peer * peer = (neighbour_peer *) l->data; + char * buff_str_ip[1024]; + char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); + int last_seen = time(NULL) - peer->last_seen; + printf("\x1b[31m[DEBUG]\x1b[0m >> %s @ %i | is temporary ? %s | last seen %i secs ago. |\n", ip_str, peer->port, peer->is_temporary ? "yes":"no", last_seen); + nbr_peers++; + if (l->next == NULL) { + break; + } else { + l = l->next; + } + + } + printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i peers.\n", nbr_peers); + print_debug(">> Finished printing peer list."); + } +} + +void print_data(list * l){ + +} diff --git a/src/debug.h b/src/debug.h index 33e51db..a8f01d6 100644 --- a/src/debug.h +++ b/src/debug.h @@ -1,9 +1,15 @@ #ifndef DEBUG_H #define DEBUG_H +#include "node.h" + #define DEBUG_LEVEL 2 void print_debug(char * msg); void print_error(char * msg); + +void print_peers(list * l); + +void print_data(list * l); #endif diff --git a/src/node.c b/src/node.c index e8b3e31..6960057 100644 --- a/src/node.c +++ b/src/node.c @@ -24,9 +24,12 @@ static list *neighbour_list; // Looks for more peers int ask_for_peers(int socket_num) { print_debug(">> Asking for more peers..."); + // Print out the current peer list. + // Only ask for more peers if the neighbour list is small enough int nbr_peers = len_list(neighbour_list); if( nbr_peers >= 5){ + print_debug(">> We have enough peers, skipping..."); return 0; } else if (nbr_peers <= 0){ print_debug(">> No peers found in the peer list, something terrible happened."); @@ -1113,6 +1116,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc } int t_ask_for_more_peers(int sock_fd){ + print_peers(neighbour_list); return ask_for_peers(sock_fd); } From c19f5e7c3c2d93daccdd5fac8d7aca68798f7918 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 19:03:46 +0200 Subject: [PATCH 37/59] Trying to fix print_out, but too many memory errors --- src/debug.c | 33 ++++++++++++++++++++++++--------- src/node.c | 20 +++++++++++++++++--- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/debug.c b/src/debug.c index 1c46f4e..524d359 100644 --- a/src/debug.c +++ b/src/debug.c @@ -1,7 +1,10 @@ #include +#include +#include +#include +#include #include "debug.h" #include "node.h" -#include void print_debug(char * msg){ if (DEBUG_LEVEL > 0) { @@ -13,19 +16,12 @@ void print_debug(char * msg){ } void print_error(char * msg){ - printf("\x1b[41m[97m[ERROR] %s \x1b[0m\n", msg); + printf("\x1b[41m\x1b[97m[ERROR] >> %s \x1b[0m\n", msg); if (DEBUG_LEVEL > 1) { getchar(); } } -// typedef struct neighbour_peer { -// struct in6_addr ip; -// int16_t port; -// char is_temporary; -// time_t last_seen; -// } neighbour_peer; - void print_peers(list * l){ // Print out the peers we have in the neighbour_list int nbr_peers = 0; @@ -53,5 +49,24 @@ void print_peers(list * l){ } void print_data(list * l){ + if (l == NULL) { + print_error("Message list is empty !"); + } else { + print_debug(">> Printing messages we know of so far :"); + int nbr_msg = 0; + setlocale(LC_ALL, "en_US.utf8"); + print_debug(">> 🏂🏂🏂🏂 Peer ID | Seqno | Length | Message "); + while(l != NULL){ + pub_data * message = l->data; + printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); + nbr_msg++; + if (l->next == NULL) { + break; + } else { + l = l->next; + } + } + printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i messages.\n", nbr_msg); + } } diff --git a/src/node.c b/src/node.c index 6960057..20ad75f 100644 --- a/src/node.c +++ b/src/node.c @@ -9,9 +9,11 @@ #include #include #include +#include #include #include #include +#include #include "node.h" #include "debug.h" @@ -1029,7 +1031,8 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s print_error("The data in the current node is NULL !"); return -1; } - printf("\x1b[31m[DEBUG]\x1b[0m >> “%s”\n", cur_tlv.node_state->data); + printf("%s\n", data + pos ); + printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_tlv.node_state->data); } int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data); if (rc < 0) { @@ -1105,7 +1108,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc if (nbr_success_tlv < 0){ print_debug(">> Error while treating the TLVs of the packet."); if (DEBUG_LEVEL > 1) { - printf("\x1b[31m[DEBUG]\x1b[0m >> Managed to deal with %i TLVs", -nbr_success_tlv ); + printf("\x1b[31m[DEBUG]\x1b[0m >> Managed to deal with %i TLVs\n", -nbr_success_tlv ); } return -2; } else { @@ -1116,12 +1119,20 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc } int t_ask_for_more_peers(int sock_fd){ - print_peers(neighbour_list); + if (DEBUG_LEVEL > 1) { + print_peers(neighbour_list); + sleep(3); + } return ask_for_peers(sock_fd); } +/* + + +*/ int t_get_network_state(int sock_fd){ print_debug(">> Getting network state..."); + return 0; } @@ -1157,6 +1168,8 @@ int run_node(int sock_fd){ t_ask_for_more_peers(sock_fd); t_update_neighbours(); t_get_network_state(sock_fd); + print_data(data_list); + sleep(3); delay = time(NULL) + 20 + (rand() % 10); } @@ -1303,6 +1316,7 @@ int bootstrap_node(int * sock_fd){ int main(int argc, const char *argv[]) { + setlocale(LC_ALL, ""); print_debug(">> Starting node"); int sock_fd; From 5e8b4fa92858204101c0bfd2c543d6041383bcc9 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Wed, 29 Apr 2020 19:53:19 +0200 Subject: [PATCH 38/59] add message and add data --- src/node.c | 275 ++++++++++++++++++++++++++++------------------------- src/node.h | 22 +++-- src/tlv.c | 4 +- src/tlv.h | 10 +- 4 files changed, 166 insertions(+), 145 deletions(-) diff --git a/src/node.c b/src/node.c index 41e648c..b5e69af 100644 --- a/src/node.c +++ b/src/node.c @@ -1,19 +1,6 @@ // This is the main file of the Dazibao project. It represents the node, and // handles all of the main logic, including the network connexions. -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include "node.h" -#include "debug.h" // Static variables static list *data_list; @@ -201,7 +188,7 @@ pub_data *get_data(int64_t id) { } // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) { +pub_data *copy_data(unsigned char len, int64_t id, uint16_t seqno, char *data) { pub_data *new_data = (pub_data*) malloc(sizeof(pub_data)); char *_data = (char*) malloc(len); new_data->length = len; @@ -213,121 +200,102 @@ pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) { return new_data; } -// Add new data to data list -int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { - print_debug(">> Adding data to the data list."); - // If id is the same as this node's id then we only update seqno +// A node state TLV was received and either no data associated to it's id is in our data list or the data was updated, return -1 if an error occurend, 0 if nothing had to be done and 1 if something was updated/added +int add_data(unsigned char len, int64_t id, uint16_t seqno, char *data, pub_data *found) { + // Check if it's our own id + if(id == NODE_ID) { + // wtf + if(found == NULL) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Our own node is not in the data list, something went terribly wrong.\n"); + return -1; + } - if(id == NODE_ID) { - // We create our pub_data. - pub_data * message = malloc(sizeof(struct pub_data)); - message->length = len; - message->id = id; - message->seqno = seqno; - message->data = data; + // If seqno is bigger or equals than our stored seqno then update seqno + if( ((seqno - found->seqno) & 32768) == 0 ) { + printf(">> Updating seqno of our own published data.\n"); + found->seqno = (seqno + 1) % (65535); + return 1; + } + + // Else, do nothing + printf(">> Our own seqno didn't need to be updated.\n"); + return 0; + } - // If the data list has never been used, or is empty ( same thing ) - if (data_list == NULL) { - data_list = (list*) malloc(sizeof(struct list)); + // If it's not our own id, update the data if it's already in our data list and seqno is bigger than our stored seqno + if(found != NULL) { + // Check if seqno is smaller or equals to our stored seqno + if( ((found->seqno - seqno) & 32768) == 0 ) { + printf(">> Data received has smaller seqno than stored seqno, nothing has to be done.\n"); + return 0; + } - list *tmp = data_list; - // We create the next node of the linked list. - tmp->data = (void *) message; - tmp->next = NULL; + // Update data + found->length = len; + found->id = id; + found->seqno = seqno; - } else { - // we move until the last element of the dala_list, - // and add or data there. - // We use a temporary address to avoid writing to the static list. - // Seems weird but ok. - list *tmp = data_list; - while(tmp->next != NULL){ - tmp = tmp->next; + // Updata message + free(found->data); + found->data = (char*) malloc(len); + memcpy(found->data, data, len); + + printf(">> Updated %li's published data.\n", id); + + return 1; + } + + // Else, add new data + pub_data *new_data = copy_data(len, id, seqno, data); + + // Find correct position for new data + list *tmp = data_list; + list *last = NULL; + list *new_node; + int64_t cur_id; + + while(tmp != NULL) { + cur_id = ((pub_data*) tmp->data)->id; + + // If id is smaller than cur_id then the new data has to be added at this position + if(id < cur_id) { + // If last hasn't been set then the new data becomes the head of the list + if(last == NULL) { + // Update list + data_list = (list*) malloc(sizeof(list)); + data_list->data = (void*) new_data; + data_list->next = tmp; + + printf(">> Added new message to data list.\n"); + + return 1; } - // We create the next node of the linked list. - list * new_node = malloc(sizeof(struct list)); - new_node->data = (void *) message; + // Else, we update the last node + new_node = (list*) malloc(sizeof(list)); + new_node->data = (void*) new_data; + new_node->next = tmp; + last->next = new_node; - // Adding the message to the list. - tmp->next = (void *) new_node; + printf(">> Added new message to data list.\n"); + + return 1; } - return 1; - } else { - // Copy data - pub_data *new_data = copy_data(len, id, seqno, data); - if(data_list == NULL) { - // Update list - data_list = (list*) malloc(sizeof(list)); - data_list->data = (void*) new_data; - data_list->next = NULL; - - return 2; - } - - // Find correct position for new data - list *tmp = data_list; - list *last = NULL; - list *new_node; - int64_t cur_id; - - while(tmp != NULL) { - cur_id = ((pub_data*) tmp->data)->id; - - // If id is smaller than cur_id then the new data has to be added at this position - if(id < cur_id) { - // If last hasn't been set then the new data becomes the head of the list - if(last == NULL) { - // Update list - data_list = (list*) malloc(sizeof(list)); - data_list->data = (void*) new_data; - data_list->next = tmp; - - return 2; - } - - // Else, we update the last node - new_node = (list*) malloc(sizeof(list)); - new_node->data = (void*) new_data; - new_node->next = tmp; - last->next = new_node; - - return 1; - } else if(id == cur_id) { - // If data already exists for this id then we update it if it's seqno is greater than the one stored - pub_data *cur_data = (pub_data*) tmp->data; - - if(seqno > cur_data->seqno) { - // Updata data - tmp->data = (void*) new_data; - - // Free old data - free(cur_data); - - return 2; - } - - // seqno is smaller so the new data allocated is freed and nothing else is done - free(new_data); - - return 2; - } - - // Get next node in list - last = tmp; - tmp = tmp->next; - } - - // If no correct position was found then the new data has to be added at the end of the list - - // Update list - new_node = (list*) malloc(sizeof(list)); - new_node->data = (void*) new_data; - new_node->next = NULL; - last->next = new_node; - return 3; + // Get next node in list + last = tmp; + tmp = tmp->next; } + + // If no correct position was found then the new data has to be added at the end of the list + new_node = (list*) malloc(sizeof(list)); + new_node->data = (void*) new_data; + new_node->next = NULL; + last->next = new_node; + + printf(">> Added new message to data list.\n"); + + return 1; } /* ---- Fin fonctions utilitaires ---- */ @@ -489,7 +457,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { } // Send length bytes from packet -int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num) { +int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, int socket_num) { ((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length); // Vectorized buffer @@ -595,7 +563,7 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { return send_packet((char*) &pack, pack.length, dest, socket_num); } -int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){ +int send_tlv(tlv *tlv_to_send, uint16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){ print_debug(">> Building packet to send a TLV."); // We first need to build the packet, @@ -662,7 +630,7 @@ int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list // We need to make sure the TLV announces a length that will no go onto // another tlv, as we might end up reading bullshit. -int validate_tlv(char *data, int pos, int16_t packet_len){ +int validate_tlv(char *data, int pos, uint16_t packet_len){ char type = data[pos]; @@ -754,17 +722,32 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack } int add_message(char * message, int message_len){ - int seqno = 1337; - int rc = add_data((unsigned char) message_len, (int64_t) NODE_ID ,(int16_t) seqno, message); - if (rc > 0) { + // Don't update the message if it's empty + if(message_len == 0) + return -1; + + // If not, get our data in the list and update it + pub_data *our_data = get_data(NODE_ID); + + if(our_data != NULL) { + our_data->seqno = (our_data->seqno + 1) % 65535; + our_data->length = message_len; + free(our_data->data); + our_data->data = (char*) malloc(message_len); + memcpy(our_data->data, message, message_len); + print_debug(">> Message added."); + + return 0; } - return 0; + + print_debug(">> Message could not be added because our own ID is not in the data_list, something went wrong."); + return -1; } // We then look at the differents TLVs in the packet. -int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *sender, int socket_num){ - int16_t packet_len = ((packet*) data)->length; +int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *sender, int socket_num){ + uint16_t packet_len = ((packet*) data)->length; if(packet_len != total_packet_len - 4) { print_debug(">> Length indicated in packet differs from real length of packet received, disgarding packet."); @@ -914,7 +897,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s hash_data(pdata, hash); // If both hashes are the same then nothing has to be done - if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) != 0) { + if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) { // The position is updated tlv_len = data[pos+1]; pos += 2; @@ -962,7 +945,28 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s if (DEBUG_LEVEL > 0) { printf("\n\t %s \n", (char *) cur_tlv.node_state->data); } - int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data); + + // Compare hashes + pdata = get_data(ntohl(cur_tlv.node_state->node_id)); + + // If data is found for this id then we check that both hashes are the same + if(pdata != NULL) { + // We hash the data stored in the data list + hash_data(pdata, hash); + + // If both hashes are the same then nothing has to be done + if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) { + // The position is updated + tlv_len = data[pos+1]; + pos += 2; + + break; + } + + } + + // Else, we update the data + int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data, pdata); if (rc < 0) { print_debug(">> Error while adding note state !"); } @@ -1224,6 +1228,17 @@ int bootstrap_node(int * sock_fd){ neighbour_list->data = (void *) root_peer; neighbour_list->next = NULL; + print_debug(">> Initializing data list..."); + data_list = (list*) malloc(sizeof(list)); + data_list->data = malloc(sizeof(pub_data)); + data_list->next = NULL; + + pub_data *our_data = (pub_data*) data_list->data; + our_data->length = 0; + our_data->id = NODE_ID; + our_data->seqno = 1337; + our_data->data = NULL; + print_debug(">> Boostraping done."); return 0; } diff --git a/src/node.h b/src/node.h index a45a61e..597de2f 100644 --- a/src/node.h +++ b/src/node.h @@ -12,6 +12,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include "debug.h" /* la table de voisins, qui est indexée par adresses de socket (des paires (IP, Port)), * et dont chaque entrée contient un booléen indiquant si le pair est permanent @@ -35,7 +41,7 @@ typedef struct neighbour_peer { typedef struct pub_data { unsigned char length; int64_t id; - int16_t seqno; + uint16_t seqno; char *data; } pub_data; @@ -69,17 +75,17 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc int check_header(char * received_data_buffer, int received_data_len, packet * packet_to_return); -int validate_tlv(char *data, int pos, int16_t packet_len); +int validate_tlv(char *data, int pos, uint16_t packet_len); int update_neighbours(); int ask_for_peers(int socket_num); -int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, int socket_num); +int work_with_tlvs(char * data, uint16_t packet_len, struct sockaddr_in6 *sender, int socket_num); int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num); -int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num); +int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, int socket_num); int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num); @@ -89,12 +95,12 @@ int run_node(int sock_fd); /* Takes a TLV and sends it over to everyone in the list of addresses. * Returns -1 in case of error, 0 otherwise. */ -int send_tlv(tlv *tlv_to_send, int16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num); +int send_tlv(tlv *tlv_to_send, uint16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num); /* Takes a list of TLV and sends them over to everyone in the list of addresses. * Returns -1 in case of error, 0 otherwise. */ -int send_tlvs(struct list * tlv_list, int16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num); +int send_tlvs(struct list * tlv_list, uint16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num); /* Check our peer list. If we have less than 5 peers, send out a TLV NEIGHBOUR_REQUEST to a random peer @@ -133,9 +139,9 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port); pub_data *get_data(int64_t id); // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data); +pub_data *copy_data(unsigned char len, int64_t id, uint16_t seqno, char *data); // add new data to data list -int add_data(unsigned char len, int64_t id, int16_t seqno, char *data); +int add_data(unsigned char len, int64_t id, uint16_t seqno, char *data, pub_data *found); #endif diff --git a/src/tlv.c b/src/tlv.c index 70207a7..4c4acd8 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -134,7 +134,7 @@ int build_network_state_req(tlv *tlv) { return 0; } -int build_node_hash(tlv *tlv, int64_t node_id, int16_t seqno, char *data) { +int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data) { // Free the previously allocated memory free(tlv->pad1); @@ -174,7 +174,7 @@ int build_node_state_req(tlv *tlv, int64_t node_id) { return 0; } -int build_node_state(tlv *tlv, int64_t node_id, int16_t seqno, char *data, size_t data_len) { +int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, char *data, size_t data_len) { // Free the previously allocated memory free(tlv->pad1); diff --git a/src/tlv.h b/src/tlv.h index d10d3c9..10b4eee 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -19,7 +19,7 @@ typedef struct packet { unsigned char magic; // 95 (si autre, ignorer) unsigned char version; // 1 (si autre, ignorer) - int16_t length; // 1020 max + uint16_t length; // 1020 max char body[1020]; } packet; @@ -67,7 +67,7 @@ typedef struct node_hash { unsigned char type; unsigned char length; int64_t node_id; - int16_t seqno; + uint16_t seqno; char node_hash[16]; } node_hash; @@ -83,7 +83,7 @@ typedef struct node_state { unsigned char type; unsigned char length; int64_t node_id; - int16_t seqno; + uint16_t seqno; char node_hash[16]; char data[192]; } node_state; @@ -122,9 +122,9 @@ int build_neighbour_req(union tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port); int build_network_hash(tlv *tlv, list *data_list); int build_network_state_req(tlv *tlv); -int build_node_hash(tlv *tlv, int64_t node_id, int16_t seqno, char *data); +int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data); int build_node_state_req(tlv *tlv, int64_t node_id); -int build_node_state(tlv *tlv, int64_t node_id, int16_t seqno, char *data, size_t data_len); +int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, char *data, size_t data_len); int build_warning(tlv *tlv, char *message, size_t message_len); #endif From f9c1cda499628ffe3ce121914f7cead176718611 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 20:19:02 +0200 Subject: [PATCH 39/59] Fixing unintialized values --- src/node.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++--- src/tlv.c | 13 +++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/node.c b/src/node.c index 20ad75f..9026f78 100644 --- a/src/node.c +++ b/src/node.c @@ -1126,13 +1126,64 @@ int t_ask_for_more_peers(int sock_fd){ return ask_for_peers(sock_fd); } -/* - - +/* For every peer we know about, we send out a TLV network hash */ int t_get_network_state(int sock_fd){ print_debug(">> Getting network state..."); + print_debug(">> Sending out a TLV network hash to every peer we know of."); + if (neighbour_list == NULL) { + print_error("Our peer list is empty !"); + print_error("Skipping.."); + return -1; + } else { + // Build the network hash + // We use another variable so that we don't interfier with the current list + list * tmp_list = neighbour_list; + while (tmp_list != NULL) { + neighbour_peer * peer = (neighbour_peer *) tmp_list->data; + tlv * new_tlv = malloc(sizeof(union tlv)); + memset(new_tlv, 0, sizeof(union tlv)); + if (new_tlv == NULL) { + print_error("Error while allocating memory for the TLV !"); + return -1; + } + + // Create the structure for the receiver. + struct sockaddr_in6 * receiver = malloc(sizeof(struct sockaddr_in6)); + memset(receiver, 0, sizeof(struct sockaddr_in6)); + if (receiver == NULL) { + print_error("Error while allocating memory for the peer address!"); + return -1; + } + receiver->sin6_family = AF_INET6; + receiver->sin6_addr = peer->ip; + receiver->sin6_port = htons(peer->port); + receiver->sin6_scope_id = 0; + + // Send out a TLV network state. + list * tmp_data_list = data_list; + + if (build_network_hash(new_tlv, tmp_data_list) < 0) { + print_error("Error while building a network hash."); + return -1; + } else { + if (send_single_tlv(new_tlv, receiver, sock_fd) < 0) { + print_error("Error while sending a network hash to a peer."); + return -1; + } else { + print_debug(">> Sent network hash to a peer."); + } + } + free(new_tlv); + free(receiver); + if (tmp_list->next == NULL) { + break; + } else { + tmp_list = tmp_list->next; + } + } + } return 0; } diff --git a/src/tlv.c b/src/tlv.c index ab66f6a..953f91d 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -33,6 +33,7 @@ int build_pad1(tlv *tlv) { free(tlv->pad1); pad1 *new = (pad1*) malloc(sizeof(pad1)); + memset(new, 0, sizeof(pad1)); if(new == NULL) return -1; @@ -49,6 +50,7 @@ int build_padn(tlv *tlv, size_t len) { free(tlv->pad1); padn *new = (padn*) malloc(sizeof(padn)); + memset(new, 0, sizeof(padn)); if(new == NULL) return -1; @@ -67,6 +69,7 @@ int build_neighbour_req(tlv *tlv) { free(tlv->pad1); neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req)); + memset(new, 0, sizeof(neighbour_req)); if(new == NULL){ return -1; @@ -89,6 +92,7 @@ int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) { free(tlv->pad1); neighbour *new = (neighbour*) malloc(sizeof(neighbour)); + memset(new, 0, sizeof(neighbour)); if(new == NULL) return -1; @@ -108,6 +112,7 @@ int build_network_hash(tlv *tlv, list *data_list) { free(tlv->pad1); network_hash *new = (network_hash*) malloc(sizeof(network_hash)); + memset(new, 0, sizeof(network_hash)); if(new == NULL) return -1; @@ -126,6 +131,7 @@ int build_network_state_req(tlv *tlv) { free(tlv->pad1); network_state_req *new = (network_state_req*) malloc(sizeof(network_state_req)); + memset(new,0,sizeof(network_state_req)); if(new == NULL) return -1; @@ -143,6 +149,7 @@ int build_node_hash(tlv *tlv, int64_t node_id, int16_t seqno, char *data) { free(tlv->pad1); node_hash *new = (node_hash*) malloc(sizeof(node_hash)); + memset(new,0,sizeof(node_hash)); if(new == NULL) return -1; @@ -165,6 +172,8 @@ int build_node_state_req(tlv *tlv, int64_t node_id) { free(tlv->pad1); node_state_req *new = (node_state_req*) malloc(sizeof(node_state_req)); + memset(new, 0, sizeof(node_state_req)); + if(new == NULL) return -1; @@ -183,6 +192,8 @@ int build_node_state(tlv *tlv, int64_t node_id, int16_t seqno, char *data, size_ free(tlv->pad1); node_state *new = (node_state*) malloc(sizeof(node_state)); + memset(new, 0, sizeof(node_state)); + int len = data_len + 26; if(new == NULL) @@ -213,6 +224,8 @@ int build_warning(tlv *tlv, char *message, size_t message_len) { free(tlv->pad1); warning *new = (warning*) malloc(sizeof(warning)); + memset(new, 0, sizeof(warning)); + int len = message_len; if(new == NULL) From afa8b4c822086614061225fa52b2141c1374f494 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Wed, 29 Apr 2020 20:25:04 +0200 Subject: [PATCH 40/59] free en commentaire --- src/tlv.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/tlv.c b/src/tlv.c index 4c4acd8..86362cf 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -62,9 +62,7 @@ int build_padn(tlv *tlv, size_t len) { int build_neighbour_req(tlv *tlv) { // Free the previously allocated memory - // if (tlv != NULL) { - // free(tlv->pad1); - // } + free(tlv->pad1); neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req)); From 352aed242d38b5074e6d5ced933edb0ff73a2c86 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Wed, 29 Apr 2020 20:58:56 +0200 Subject: [PATCH 41/59] include --- src/node.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node.h b/src/node.h index 597de2f..62f33a4 100644 --- a/src/node.h +++ b/src/node.h @@ -17,7 +17,6 @@ #include #include #include -#include "debug.h" /* la table de voisins, qui est indexée par adresses de socket (des paires (IP, Port)), * et dont chaque entrée contient un booléen indiquant si le pair est permanent @@ -54,6 +53,7 @@ typedef struct list { #include "tlv.h" #include "hash.h" #include "parser.h" +#include "debug.h" // On which port do we listen to #define LISTEN_PORT 1212 From 0bb80d078c34313377c5cb617de65a743735ca89 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Wed, 29 Apr 2020 22:22:30 +0200 Subject: [PATCH 42/59] int64_t to uint64_t and endianness problem --- src/node.c | 56 +++++++++++++++++++----------------------------------- src/node.h | 12 +++++++----- src/tlv.c | 18 +++++++++--------- src/tlv.h | 12 ++++++------ 4 files changed, 42 insertions(+), 56 deletions(-) diff --git a/src/node.c b/src/node.c index 7319011..6a2518f 100644 --- a/src/node.c +++ b/src/node.c @@ -1,22 +1,6 @@ // This is the main file of the Dazibao project. It represents the node, and // handles all of the main logic, including the network connexions. -<<<<<<< HEAD -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -======= ->>>>>>> add-message + #include "node.h" // Static variables @@ -61,7 +45,7 @@ int ask_for_peers(int socket_num) { memset(&dest, 0, sizeof(struct sockaddr_in6)); dest.sin6_family = AF_INET6; memcpy(&dest.sin6_addr, &ip, 16); - dest.sin6_port = htons(port); + dest.sin6_port = htobe16(port); dest.sin6_scope_id = ifindex; // Send neighbour request TLV @@ -193,7 +177,7 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { } // get data associated with id, if it doesn't exist return NULL -pub_data *get_data(int64_t id) { +pub_data *get_data(uint64_t id) { list *tmp = data_list; pub_data *data; @@ -208,7 +192,7 @@ pub_data *get_data(int64_t id) { } // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, int64_t id, uint16_t seqno, char *data) { +pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data) { pub_data *new_data = (pub_data*) malloc(sizeof(pub_data)); char *_data = (char*) malloc(len); if (_data == NULL) { @@ -229,7 +213,7 @@ pub_data *copy_data(unsigned char len, int64_t id, uint16_t seqno, char *data) { } // A node state TLV was received and either no data associated to it's id is in our data list or the data was updated, return -1 if an error occurend, 0 if nothing had to be done and 1 if something was updated/added -int add_data(unsigned char len, int64_t id, uint16_t seqno, char *data, pub_data *found) { +int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found) { // Check if it's our own id if(id == NODE_ID) { // wtf @@ -280,7 +264,7 @@ int add_data(unsigned char len, int64_t id, uint16_t seqno, char *data, pub_data list *tmp = data_list; list *last = NULL; list *new_node; - int64_t cur_id; + uint64_t cur_id; while(tmp != NULL) { cur_id = ((pub_data*) tmp->data)->id; @@ -489,7 +473,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // Send length bytes from packet int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, int socket_num) { - ((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length); + ((packet*) packet_buff)->length = htobe16(((packet*) packet_buff)->length); // Vectorized buffer struct iovec vec_buff[1]; @@ -743,7 +727,7 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack } // Convert to hardware order. - ((packet*) packet_to_return)->length = ntohs(((packet*) packet_to_return)->length); + ((packet*) packet_to_return)->length = be16toh(((packet*) packet_to_return)->length); if (packet_to_return->length + 4 < received_data_len ) { print_debug(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics."); return -1; @@ -866,7 +850,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * memset(&new_neighbour, 0, sizeof(new_neighbour)); new_neighbour.sin6_family = AF_INET6; memcpy(&new_neighbour.sin6_addr, &cur_tlv.neighbour->ip, 16); - new_neighbour.sin6_port = ntohs(cur_tlv.neighbour->port); + new_neighbour.sin6_port = be16toh(cur_tlv.neighbour->port); new_neighbour.sin6_scope_id = ifindex; // Build network hash @@ -924,6 +908,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * pdata = (pub_data*) tmp_list->data; build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data); add_tlv(&pack, &new_tlv, sender, socket_num); + tmp_list = tmp_list->next; } // The position is updated @@ -937,7 +922,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * //if the hashes are identical nothing has to be done print_debug(">> Received node hash, updating message entry..."); cur_tlv.node_hash = (node_hash*) (data + pos); - pdata = get_data(ntohl(cur_tlv.node_hash->node_id)); + pdata = get_data(be64toh(cur_tlv.node_hash->node_id)); // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { @@ -956,7 +941,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * } // If no pub_data was found or the hashes differ then we send a node state request - build_node_state_req(&new_tlv, ntohl(cur_tlv.node_hash->node_id)); + build_node_state_req(&new_tlv, be64toh(cur_tlv.node_hash->node_id)); add_tlv(&pack, &new_tlv, sender, socket_num); // The position is updated @@ -969,8 +954,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // so a node state tlv for this node id has to be sent, // if no pub_data exists for this id nothing is sent print_debug(">> Received node state request. Processing..."); - cur_tlv.node_state_req = (node_state_req*) (data[pos]); - pdata = get_data(ntohl(cur_tlv.node_state_req->node_id)); + cur_tlv.node_state_req = (node_state_req*) (data + pos); + pdata = get_data(be64toh(cur_tlv.node_state_req->node_id)); if(pdata != NULL) { build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); @@ -995,12 +980,11 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * print_error("The data in the current node is NULL !"); return -1; } - printf("%s\n", data + pos ); printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_tlv.node_state->data); } // Compare hashes - pdata = get_data(ntohl(cur_tlv.node_state->node_id)); + pdata = get_data(be64toh(cur_tlv.node_state->node_id)); // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { @@ -1008,7 +992,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * hash_data(pdata, hash); // If both hashes are the same then nothing has to be done - if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) { + if(memcmp(hash, cur_tlv.node_state->node_hash, 16) == 0) { // The position is updated tlv_len = data[pos+1]; pos += 2; @@ -1019,7 +1003,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * } // Else, we update the data - int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data, pdata); + int rc = add_data(cur_tlv.node_state->length - 26, be64toh(cur_tlv.node_state->node_id), be16toh(cur_tlv.node_state->seqno), cur_tlv.node_state->data, pdata); if (rc < 0) { print_error("Error while adding node state !"); } @@ -1076,7 +1060,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc // Neighbour check struct in6_addr ip = sender->sin6_addr; - int16_t port = htons(sender->sin6_port); + int16_t port = htobe16(sender->sin6_port); int rc = add_n_update_neighbour(&ip, port); @@ -1143,7 +1127,7 @@ int t_get_network_state(int sock_fd){ } receiver->sin6_family = AF_INET6; receiver->sin6_addr = peer->ip; - receiver->sin6_port = htons(peer->port); + receiver->sin6_port = htobe16(peer->port); receiver->sin6_scope_id = 0; // Send out a TLV network state. @@ -1320,7 +1304,7 @@ int bootstrap_node(int * sock_fd){ memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin6_family = AF_INET6; // server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY); - server_addr.sin6_port = htons(LISTEN_PORT); + server_addr.sin6_port = htobe16(LISTEN_PORT); if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) { print_debug(">> Error - failed to bind socket"); return -2; diff --git a/src/node.h b/src/node.h index 62f33a4..988c1f1 100644 --- a/src/node.h +++ b/src/node.h @@ -13,10 +13,12 @@ #include #include #include -#include +#include #include #include #include +#include +#include /* la table de voisins, qui est indexée par adresses de socket (des paires (IP, Port)), * et dont chaque entrée contient un booléen indiquant si le pair est permanent @@ -39,7 +41,7 @@ typedef struct neighbour_peer { typedef struct pub_data { unsigned char length; - int64_t id; + uint64_t id; uint16_t seqno; char *data; } pub_data; @@ -136,12 +138,12 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port); int add_n_update_neighbour(struct in6_addr *ip, int16_t port); // get data associated with id, if it doesn't exist return NULL -pub_data *get_data(int64_t id); +pub_data *get_data(uint64_t id); // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, int64_t id, uint16_t seqno, char *data); +pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data); // add new data to data list -int add_data(unsigned char len, int64_t id, uint16_t seqno, char *data, pub_data *found); +int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found); #endif diff --git a/src/tlv.c b/src/tlv.c index 0cb470d..55c306f 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -100,7 +100,7 @@ int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) { new->type = 3; new->length = 18; new->ip = ip; - new->port = htons(port); + new->port = htobe16(port); tlv->neighbour = new; @@ -144,7 +144,7 @@ int build_network_state_req(tlv *tlv) { return 0; } -int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data) { +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data) { // Free the previously allocated memory free(tlv->pad1); @@ -156,8 +156,8 @@ int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data) { new->type = 6; new->length = 26; - new->node_id = htonl(node_id); - new->seqno = htons(seqno); + new->node_id = htobe64(node_id); + new->seqno = htobe16(seqno); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; hash_data(&pdata, (unsigned char*) new->node_hash); @@ -167,7 +167,7 @@ int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data) { return 0; } -int build_node_state_req(tlv *tlv, int64_t node_id) { +int build_node_state_req(tlv *tlv, uint64_t node_id) { // Free the previously allocated memory free(tlv->pad1); @@ -180,14 +180,14 @@ int build_node_state_req(tlv *tlv, int64_t node_id) { new->type = 7; new->length = 8; - new->node_id = htonl(node_id); + new->node_id = htobe64(node_id); tlv->node_state_req = new; return 0; } -int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, char *data, size_t data_len) { +int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len) { // Free the previously allocated memory free(tlv->pad1); @@ -207,8 +207,8 @@ int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, char *data, size new->type = 8; new->length = 26 + len; - new->node_id = htonl(node_id); - new->seqno = htons(seqno); + new->node_id = htobe64(node_id); + new->seqno = htobe16(seqno); memcpy(new->data, data, len); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; diff --git a/src/tlv.h b/src/tlv.h index 10b4eee..388957e 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -66,7 +66,7 @@ typedef struct network_state_req { typedef struct node_hash { unsigned char type; unsigned char length; - int64_t node_id; + uint64_t node_id; uint16_t seqno; char node_hash[16]; } node_hash; @@ -75,14 +75,14 @@ typedef struct node_hash { typedef struct node_state_req { unsigned char type; unsigned char length; - int64_t node_id; + uint64_t node_id; } node_state_req; // 28 octets min, 220 octets max (data 0 -> 192) typedef struct node_state { unsigned char type; unsigned char length; - int64_t node_id; + uint64_t node_id; uint16_t seqno; char node_hash[16]; char data[192]; @@ -122,9 +122,9 @@ int build_neighbour_req(union tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port); int build_network_hash(tlv *tlv, list *data_list); int build_network_state_req(tlv *tlv); -int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data); -int build_node_state_req(tlv *tlv, int64_t node_id); -int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, char *data, size_t data_len); +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data); +int build_node_state_req(tlv *tlv, uint64_t node_id); +int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len); int build_warning(tlv *tlv, char *message, size_t message_len); #endif From ae893c91fc55d2b157b5cdc57bfa39c8b64fd908 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 22:22:58 +0200 Subject: [PATCH 43/59] Some changes --- src/debug.h | 1 + src/node.c | 40 ++++++++++++++++++---------------------- src/node.h | 2 ++ src/tlv.h | 4 ++-- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/debug.h b/src/debug.h index a8f01d6..c0a5688 100644 --- a/src/debug.h +++ b/src/debug.h @@ -1,5 +1,6 @@ #ifndef DEBUG_H #define DEBUG_H + #include "node.h" diff --git a/src/node.c b/src/node.c index 7319011..8247348 100644 --- a/src/node.c +++ b/src/node.c @@ -1,22 +1,5 @@ // This is the main file of the Dazibao project. It represents the node, and // handles all of the main logic, including the network connexions. -<<<<<<< HEAD -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -======= ->>>>>>> add-message #include "node.h" // Static variables @@ -837,6 +820,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * break; case 2: + // We received a neighbour request so a random neighbor tlv has to be sent print_debug(">> Received neighbour request, sending out a neighbour address."); // Send a neighbour tlv @@ -858,6 +842,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * break; case 3: + print_debug(">> Received neighbour tlv, sending back network hash."); // We received a neighbour tlv so a tlv network hash is sent to that address cur_tlv.neighbour = (neighbour*) (data + pos); @@ -879,6 +864,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * break; case 4: + print_debug(">> Received network_hash, comparing with our own.."); // We reveived a network hash tlv so we compare the hash with our own, // if they differ we send a network state request tlv @@ -924,6 +910,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * pdata = (pub_data*) tmp_list->data; build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data); add_tlv(&pack, &new_tlv, sender, socket_num); + tmp_list = tmp_list->next; } // The position is updated @@ -965,11 +952,13 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * break; case 7: + // We received a node state request tlv // so a node state tlv for this node id has to be sent, // if no pub_data exists for this id nothing is sent print_debug(">> Received node state request. Processing..."); - cur_tlv.node_state_req = (node_state_req*) (data[pos]); + + cur_tlv.node_state_req = (node_state_req*) (data + pos); pdata = get_data(ntohl(cur_tlv.node_state_req->node_id)); if(pdata != NULL) { @@ -990,25 +979,32 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * cur_tlv.node_state = (node_state*) (data + pos); print_debug(">> Received message ! "); + printf("%d\n", cur_tlv.node_state->type ); + printf("%d\n", cur_tlv.node_state->length ); + printf("%lu\n", cur_tlv.node_state->node_id ); + printf("%hu\n", cur_tlv.node_state->seqno ); + printf("%.16s\n", cur_tlv.node_state->node_hash); + printf("%s\n", cur_tlv.node_state->data); + if (DEBUG_LEVEL > 0) { if (cur_tlv.node_state->data == NULL) { print_error("The data in the current node is NULL !"); return -1; } - printf("%s\n", data + pos ); printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_tlv.node_state->data); } // Compare hashes - pdata = get_data(ntohl(cur_tlv.node_state->node_id)); + pdata = get_data(cur_tlv.node_state->node_id); // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { // We hash the data stored in the data list + memset(hash, 0, 16); hash_data(pdata, hash); // If both hashes are the same then nothing has to be done - if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) { + if(memcmp(hash, cur_tlv.node_state->node_hash, 16) == 0) { // The position is updated tlv_len = data[pos+1]; pos += 2; @@ -1060,7 +1056,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * if(pack.length > 0){ send_packet((char*) &pack, pack.length, sender, socket_num); } - + print_data(data_list); return 0; } diff --git a/src/node.h b/src/node.h index 62f33a4..c1d9e35 100644 --- a/src/node.h +++ b/src/node.h @@ -17,6 +17,8 @@ #include #include #include +#include + /* la table de voisins, qui est indexée par adresses de socket (des paires (IP, Port)), * et dont chaque entrée contient un booléen indiquant si le pair est permanent diff --git a/src/tlv.h b/src/tlv.h index 10b4eee..825de6b 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -66,7 +66,7 @@ typedef struct network_state_req { typedef struct node_hash { unsigned char type; unsigned char length; - int64_t node_id; + uint64_t node_id; uint16_t seqno; char node_hash[16]; } node_hash; @@ -82,7 +82,7 @@ typedef struct node_state_req { typedef struct node_state { unsigned char type; unsigned char length; - int64_t node_id; + uint64_t node_id; uint16_t seqno; char node_hash[16]; char data[192]; From 201084d9f102084c69a2e69068098e16a683df19 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 22:45:58 +0200 Subject: [PATCH 44/59] Trying to fix --- src/node.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/node.c b/src/node.c index 585d874..859b661 100644 --- a/src/node.c +++ b/src/node.c @@ -984,7 +984,11 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * printf("%lu\n", cur_tlv.node_state->node_id ); printf("%hu\n", cur_tlv.node_state->seqno ); printf("%.16s\n", cur_tlv.node_state->node_hash); - printf("%s\n", cur_tlv.node_state->data); + printf("%s\0\n", cur_tlv.node_state->data); + + sprintf(warn, ">> WARNING:\n%%.%ds", cur_tlv.node_state->length); + printf(warn, cur_tlv.node_state->data); + sleep(3); if (DEBUG_LEVEL > 0) { if (cur_tlv.node_state->data == NULL) { From bd8f5c452b60c3b3d3eb11c916088f66f1393e48 Mon Sep 17 00:00:00 2001 From: n07070 Date: Thu, 30 Apr 2020 19:56:58 +0200 Subject: [PATCH 45/59] Trying to fix the receiving of messages --- src/debug.c | 2 +- src/node.c | 281 ++++++++++++++++++++++++++++++---------------------- src/node.h | 12 +-- src/tlv.c | 8 +- src/tlv.h | 16 +-- 5 files changed, 180 insertions(+), 139 deletions(-) diff --git a/src/debug.c b/src/debug.c index 524d359..a0bbbcd 100644 --- a/src/debug.c +++ b/src/debug.c @@ -55,7 +55,7 @@ void print_data(list * l){ print_debug(">> Printing messages we know of so far :"); int nbr_msg = 0; setlocale(LC_ALL, "en_US.utf8"); - print_debug(">> 🏂🏂🏂🏂 Peer ID | Seqno | Length | Message "); + print_debug(">> Peer ID | Seqno | Length | Message "); while(l != NULL){ pub_data * message = l->data; printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); diff --git a/src/node.c b/src/node.c index 859b661..beae120 100644 --- a/src/node.c +++ b/src/node.c @@ -192,9 +192,9 @@ pub_data *get_data(uint64_t id) { } // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data) { +pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data) { pub_data *new_data = (pub_data*) malloc(sizeof(pub_data)); - char *_data = (char*) malloc(len); + unsigned char *_data = (unsigned char*) malloc(len); if (_data == NULL) { print_error("Failed to allocate memory for copying the data !"); return NULL; @@ -213,7 +213,7 @@ pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data) } // A node state TLV was received and either no data associated to it's id is in our data list or the data was updated, return -1 if an error occurend, 0 if nothing had to be done and 1 if something was updated/added -int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found) { +int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found) { // Check if it's our own id if(id == NODE_ID) { // wtf @@ -249,7 +249,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat // Updata message free(found->data); - found->data = (char*) malloc(len); + found->data = (unsigned char*) malloc(len); memcpy(found->data, data, len); printf(">> Updated %li's published data.\n", id); @@ -492,7 +492,7 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0); if (response_code < 0) { - print_debug(">> Unable to send out the packet to peer."); + print_error("Unable to send out the packet to peer."); error_while_sending = 1; } else if (response_code < length) { print_debug(">> Sent out only part of the packet."); @@ -578,70 +578,70 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { return send_packet((char*) &pack, pack.length, dest, socket_num); } -int send_tlv(tlv *tlv_to_send, uint16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){ - print_debug(">> Building packet to send a TLV."); - - // We first need to build the packet, - char packet_buff[1024]; - struct packet pack; - pack.magic = 95; - pack.version = 1; - if (tlv_size > 1020) { - print_debug(">> Unable to send the tlv, it's size if above 1020 bytes."); - return -1; - } else { - memcpy((void *) pack.body, tlv_to_send, tlv_size); - } - - // Move the content of the paquet struct to a buffer - // That will be send out in a vectorized buffer. - // packet_buff = (char *) pack; - memcpy(&packet_buff,&pack,1024); - - if (DEBUG_LEVEL > 1) { - print_debug(">> Packet has been built."); - } - - // Vectorized buffer - struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff }; - - int error_while_sending = 0; - - // For every dest - for (size_t i = 0; i < dest_list_size; i++) { - // Creating the struct to send out with sendmsg - struct msghdr packet_tlv_send_out = { - .msg_name = &dest_list[i], - .msg_namelen = sizeof(dest_list[i]), - .msg_iov = &vec_buff, - .msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2. - }; - - int response_code = sendmsg((int) socket_num, &packet_tlv_send_out, 0); - if (response_code < 0) { - if (DEBUG_LEVEL > 0) { - printf("\x1b[31m[DEBUG]\x1b[0m >> Unable to send out the packet to peer %li", i); - } - error_while_sending = 1; - continue; - } else if (response_code < sizeof(packet_tlv_send_out)) { - print_debug(">> Sent out only part of the packet."); - error_while_sending = 1; - continue; - } else { - if (DEBUG_LEVEL > 0) { - printf("\x1b[31m[DEBUG]\x1b[0m >> Send out packet to peer %li", i); - } - } - } - - if (error_while_sending == 1) { - print_debug(">> Error occured while sending out a packet."); - return -1; - } else { - return 0; - } -} +// TODO This function can be deleted. +// int send_tlv(tlv *tlv_to_send, uint16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){ +// print_debug(">> Building packet to send a TLV."); +// +// // We first need to build the packet, +// char packet_buff[1024]; +// struct packet pack; +// pack.magic = 95; +// pack.version = 1; +// if (tlv_size > 1020) { +// print_debug(">> Unable to send the tlv, it's size is above 1020 bytes."); +// return -1; +// } else { +// memcpy((void *) pack.body, tlv_to_send, tlv_size); +// } +// +// // Move the content of the paquet struct to a buffer +// // That will be send out in a vectorized buffer. +// memcpy(&packet_buff,&pack,1024); +// +// if (DEBUG_LEVEL > 1) { +// print_debug(">> Packet has been built."); +// } +// +// // Vectorized buffer +// struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff }; +// +// int error_while_sending = 0; +// +// // For every dest +// for (size_t i = 0; i < dest_list_size; i++) { +// // Creating the struct to send out with sendmsg +// struct msghdr packet_tlv_send_out = { +// .msg_name = &dest_list[i], +// .msg_namelen = sizeof(dest_list[i]), +// .msg_iov = &vec_buff, +// .msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2. +// }; +// +// int response_code = sendmsg((int) socket_num, &packet_tlv_send_out, 0); +// if (response_code < 0) { +// if (DEBUG_LEVEL > 0) { +// printf("\x1b[31m[DEBUG]\x1b[0m >> Unable to send out the packet to peer %li", i); +// } +// error_while_sending = 1; +// continue; +// } else if (response_code < sizeof(packet_tlv_send_out)) { +// print_debug(">> Sent out only part of the packet."); +// error_while_sending = 1; +// continue; +// } else { +// if (DEBUG_LEVEL > 0) { +// printf("\x1b[31m[DEBUG]\x1b[0m >> Sent out packet to peer %i", i); +// } +// } +// } +// +// if (error_while_sending == 1) { +// print_debug(">> Error occured while sending out a packet."); +// return -1; +// } else { +// return 0; +// } +// } // We need to make sure the TLV announces a length that will no go onto // another tlv, as we might end up reading bullshit. @@ -660,13 +660,13 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){ print_debug(">> Reading outside of packet's max length."); return -1; } - // 0 1 2 3 = Packet - // 4 = type 5 = tlv_len + // 0 1 2 3 = Packet + // 4 = type 5 = tlv_len unsigned char tlv_len = data[pos+1]; // Check that the tlv does not exceed the packet length if(pos + tlv_len > packet_len){ - print_debug(">> The TLV Length exceed the packet length\n"); + print_debug(">> The TLV Length exceeds the packet length\n"); return -1; } @@ -738,8 +738,9 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack int add_message(char * message, int message_len){ // Don't update the message if it's empty - if(message_len == 0) + if(message_len == 0){ return -1; + } // If not, get our data in the list and update it pub_data *our_data = get_data(NODE_ID); @@ -748,8 +749,8 @@ int add_message(char * message, int message_len){ our_data->seqno = (our_data->seqno + 1) % 65535; our_data->length = message_len; free(our_data->data); - our_data->data = (char*) malloc(message_len); - memcpy(our_data->data, message, message_len); + our_data->data = (unsigned char*) malloc(message_len); + memcpy(our_data->data, (unsigned char *) message, message_len); print_debug(">> Message added."); @@ -768,6 +769,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * print_debug(">> Length indicated in packet differs from real length of packet received, disgarding packet."); return -1; } + int nbr_of_tlvs = 0; int pos = 4; unsigned char tlv_len, hash[16]; @@ -780,11 +782,13 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * new_tlv.pad1 = NULL; cur_tlv.pad1 = NULL; - list *tmp_list; - pub_data *pdata; + list * tmp_list; + pub_data * pdata; // holds the random neighbour we send back in case of a neighbour_req. struct neighbour_peer *random_neighbour; + // memset(random_neighbour, 0, sizeof(struct neighbour_peer)); + // Holds the new neighbour send to us by a "neighbour tlv" struct sockaddr_in6 new_neighbour; @@ -792,32 +796,31 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * packet pack = (packet) {.magic = 95, .version = 1, .length = 0}; memset(pack.body, 0, 1020); -/* - int ifindex = if_nametoindex("enp3s0"); - if(ifindex == 0) { - int ifindex = if_nametoindex("eth0"); - if(ifindex == 0) { - perror("if_nametoindex failed"); - return -1; - } - } -*/ int ifindex = 0; while(pos < total_packet_len) { // Making sure the current TLV we are looking at is valid. + // TODO : I think we should reset all the structs here. + // memset(random_neighbour, 0, sizeof(struct neighbour_peer)); + // memset(pdata, 0, sizeof(struct pub_data)); + // memset(tmp_list, 0, sizeof(struct list)); + memset(hash, 0, 16); + memset(warn, 0, 32); + tlv_len = 0; + switch(validate_tlv(data, pos, total_packet_len)) { case 0: // We received a padding tlv so it is ignored print_debug(">> Received padding tlv, ignoring..."); pos += 1; - + nbr_of_tlvs++; break; case 1: // We received a padding tlv so it is ignored print_debug(">> Received padding(n) tlv, ignoring..."); tlv_len = data[pos+1]; pos += tlv_len + 2; + nbr_of_tlvs++; break; case 2: @@ -840,6 +843,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; + nbr_of_tlvs++; break; case 3: @@ -862,6 +866,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; + nbr_of_tlvs++; break; case 4: @@ -898,6 +903,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; + nbr_of_tlvs++; break; case 5: @@ -915,7 +921,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * } // The position is updated - pos += 2; + tlv_len = data[pos+1]; + pos += tlv_len + 2; break; case 6: @@ -936,13 +943,14 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) { // The position is updated tlv_len = data[pos+1]; - pos += 2; - + pos += tlv_len + 2; + print_debug(">> Both hashs are the same, nothing to do."); break; } } + print_debug(">> No hash found, or hashs are differents, sending back node state request."); // If no pub_data was found or the hashes differ then we send a node state request build_node_state_req(&new_tlv, be64toh(cur_tlv.node_hash->node_id)); add_tlv(&pack, &new_tlv, sender, socket_num); @@ -964,7 +972,9 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * if(pdata != NULL) { build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); add_tlv(&pack, &new_tlv, sender, socket_num); - } + } else { + print_debug(">> Found no data for the requested node, skipping..."); + } // The position is updated tlv_len = data[pos+1]; @@ -976,19 +986,23 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // we add it to the data list // or update the data stored print_debug(">> Received node state, updating..."); - cur_tlv.node_state = (node_state*) (data + pos); + cur_tlv.node_state = (node_state*) (data + pos); print_debug(">> Received message ! "); - printf("%d\n", cur_tlv.node_state->type ); - printf("%d\n", cur_tlv.node_state->length ); - printf("%lu\n", cur_tlv.node_state->node_id ); - printf("%hu\n", cur_tlv.node_state->seqno ); - printf("%.16s\n", cur_tlv.node_state->node_hash); - printf("%s\0\n", cur_tlv.node_state->data); - - sprintf(warn, ">> WARNING:\n%%.%ds", cur_tlv.node_state->length); - printf(warn, cur_tlv.node_state->data); - sleep(3); + printf("Type : %d\n", cur_tlv.node_state->type ); + printf("Length : %d\n", cur_tlv.node_state->length ); + printf("Node ID : %lu\n", be64toh(cur_tlv.node_state->node_id) ); + printf("Seqno : %hu\n", be16toh(cur_tlv.node_state->seqno) ); + printf("Hash :"); + for(int x = 0; x < 16; x++){ + printf("%02x", cur_tlv.node_state->node_hash[x]); + fflush(0); + } + printf("\n"); + for(int x = 0; x < 192; x++){ + printf("%d", cur_tlv.node_state->data[x]); + fflush(0); + } if (DEBUG_LEVEL > 0) { if (cur_tlv.node_state->data == NULL) { @@ -996,6 +1010,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * return -1; } printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_tlv.node_state->data); + sleep(1); } // Compare hashes @@ -1011,7 +1026,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * if(memcmp(hash, cur_tlv.node_state->node_hash, 16) == 0) { // The position is updated tlv_len = data[pos+1]; - pos += 2; + pos += tlv_len + 2; break; } @@ -1046,7 +1061,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // A malformed packet was found so we stop looking for more packets and send a warning tlv strcpy(warn, "Packet is malformed."); print_debug(">> Malformed packet, we won't treat it."); - build_warning(&new_tlv, warn, strlen(warn)); + print_debug(">> Sending back a Warning to sender."); + build_warning(&new_tlv,(unsigned char *) warn, strlen(warn)); add_tlv(&pack, &new_tlv, sender, socket_num); return -1; @@ -1221,20 +1237,20 @@ int run_node(int sock_fd){ /* Call poll() */ - ret = poll(fds, 2, 5); + ret = poll(fds, 2, 10); if (ret < 0) { - print_debug(">> Error - poll returned error"); + print_error("Poll returned error"); break; } else if (ret > 0) { /* Regardless of requested events, poll() can always return these */ if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { - print_debug("Error - poll indicated stdin error\n"); + print_error("Poll indicated stdin error\n"); break; } if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) { - print_debug("Error - poll indicated socket error\n"); + print_error("Poll indicated socket error\n"); break; } @@ -1242,9 +1258,10 @@ int run_node(int sock_fd){ if (fds[0].revents & (POLLIN | POLLPRI)) { bytes = read(0, input_buffer, sizeof(input_buffer)); if (bytes < 0) { - print_debug(">> Error - stdin error"); + print_error("stdin error"); break; } + // Remove trailing \n input_buffer[strcspn(input_buffer, "\n")] = 0; if (DEBUG_LEVEL > 0) { printf("\x1b[31m[DEBUG]\x1b[0m >> Adding following message to the table : “%s”\n", input_buffer ); @@ -1252,7 +1269,9 @@ int run_node(int sock_fd){ // Add message to the message table. if (add_message(input_buffer, bytes) < 0) { print_debug(">> Error while trying to add the message to the list of messages, please try again.."); - } + // Reset buffer + memset(input_buffer, 0, sizeof(input_buffer)); + } } // Read data from the socket ( incoming packet ) @@ -1288,6 +1307,8 @@ int run_node(int sock_fd){ if (work_tlv_status < 0) { print_debug(">> Error while treating the incoming packet."); } + // Reset buffer + memset(output_buffer, 0, sizeof(output_buffer)); } } } else { @@ -1304,7 +1325,7 @@ int run_node(int sock_fd){ // This function runs once, and sets the sock_fd as well as the neighbourhood -int bootstrap_node(int * sock_fd){ +int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ print_debug(">> Boostraping node..."); struct sockaddr_in6 server_addr; @@ -1312,42 +1333,46 @@ int bootstrap_node(int * sock_fd){ /* Create UDP socket */ * sock_fd = socket(AF_INET6, SOCK_DGRAM, 0); if ( * sock_fd < 0) { - print_debug(">> Error - failed to open socket"); - return -1; + print_error("Failed to open socket"); + perror(""); + exit(-1); } /* Bind socket */ memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin6_family = AF_INET6; // server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY); - server_addr.sin6_port = htobe16(LISTEN_PORT); + server_addr.sin6_port = LISTEN_PORT; if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) { - print_debug(">> Error - failed to bind socket"); - return -2; + print_error("Failed to bind socket"); + perror(""); + exit(-1); } /* Make the first peer*/ struct neighbour_peer * root_peer = (struct neighbour_peer *) malloc(sizeof(struct neighbour_peer)); time_t root_peer_seen = time(NULL); - int inet_p = inet_pton(AF_INET6, ROOT_PEER_ADDR, &root_peer->ip); + int inet_p = inet_pton(AF_INET6, root_peer_ip, &root_peer->ip); if(inet_p < 1){ perror(">> Failed to create the root peer."); return -3; } - root_peer->port = 1212; + root_peer->port = root_peer_port; root_peer->is_temporary = 0; root_peer->last_seen = root_peer_seen; // TODO: Add the first peer to the neighbourhood print_debug(">> Adding the first root peer to the list..."); neighbour_list = malloc(sizeof(struct list)); + memset(neighbour_list, 0, sizeof(struct list)); neighbour_list->data = (void *) root_peer; neighbour_list->next = NULL; print_debug(">> Initializing data list..."); data_list = (list*) malloc(sizeof(list)); + memset(data_list, 0, sizeof(struct list)); data_list->data = malloc(sizeof(pub_data)); data_list->next = NULL; @@ -1363,11 +1388,27 @@ int bootstrap_node(int * sock_fd){ int main(int argc, const char *argv[]) { + + if (argc != 3) { + print_error("Usage : ./dazibao "); + exit(-1); + } + + char *end; + intmax_t val = strtoimax(argv[2], &end, 10); + if (errno == ERANGE || val < 0 || val > UINT16_MAX || end == argv[2] || *end != '\0'){ + print_error("Conversion of port number failed, please choose a smaller number."); + } + + uint16_t root_peer_port = (uint16_t) val; + + char * root_peer_ip = (char *) argv[1]; + setlocale(LC_ALL, ""); print_debug(">> Starting node"); int sock_fd; - bootstrap_node(&sock_fd); + bootstrap_node(&sock_fd, root_peer_ip, root_peer_port); run_node(sock_fd); close(sock_fd); diff --git a/src/node.h b/src/node.h index 988c1f1..af0e515 100644 --- a/src/node.h +++ b/src/node.h @@ -43,7 +43,7 @@ typedef struct pub_data { unsigned char length; uint64_t id; uint16_t seqno; - char *data; + unsigned char *data; } pub_data; // General list @@ -62,14 +62,14 @@ typedef struct list { // The node ID // #define NODE_ID 42675882021843277 -#define NODE_ID 42013376900101010 +#define NODE_ID 13809235719890846928 // The number of neighbours // The neighbour table has 15 entries #define NEIGHBOUR_MAX 15 // The adress of the main peer -#define ROOT_PEER_ADDR "2001:660:3301:9200::51c2:1b9b" +#define ROOT_PEER_ADDR "::1" // fonctions signatures @@ -124,7 +124,7 @@ int add_message(char * message, int message_len); // This functions creates the structures needed for the rest of the project, // creates the socket, and returns all of it. -int bootstrap_node(int * sock_fd); +int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port); // Helper functions int len_list(list *l); @@ -141,9 +141,9 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port); pub_data *get_data(uint64_t id); // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data); +pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data); // add new data to data list -int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found); +int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found); #endif diff --git a/src/tlv.c b/src/tlv.c index 55c306f..0dcf492 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -144,7 +144,7 @@ int build_network_state_req(tlv *tlv) { return 0; } -int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data) { +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data) { // Free the previously allocated memory free(tlv->pad1); @@ -187,7 +187,7 @@ int build_node_state_req(tlv *tlv, uint64_t node_id) { return 0; } -int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len) { +int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data, size_t data_len) { // Free the previously allocated memory free(tlv->pad1); @@ -219,13 +219,13 @@ int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, siz return 0; } -int build_warning(tlv *tlv, char *message, size_t message_len) { +int build_warning(tlv *tlv, unsigned char *message, size_t message_len) { // Free the previously allocated memory free(tlv->pad1); warning *new = (warning*) malloc(sizeof(warning)); memset(new, 0, sizeof(warning)); - + int len = message_len; if(new == NULL) diff --git a/src/tlv.h b/src/tlv.h index 388957e..d2ebff5 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -53,7 +53,7 @@ typedef struct neighbour { typedef struct network_hash { unsigned char type; unsigned char length; - char network_hash[16]; + unsigned char network_hash[16]; } network_hash; // 2 octets @@ -68,7 +68,7 @@ typedef struct node_hash { unsigned char length; uint64_t node_id; uint16_t seqno; - char node_hash[16]; + unsigned char node_hash[16]; } node_hash; // 10 octets @@ -84,15 +84,15 @@ typedef struct node_state { unsigned char length; uint64_t node_id; uint16_t seqno; - char node_hash[16]; - char data[192]; + unsigned char node_hash[16]; + unsigned char data[192]; } node_state; // 2 octets min, 258 ocets max (unsigned char 0 -> 255) typedef struct warning { unsigned char type; unsigned char length; - char message[256]; + unsigned char message[256]; } warning; typedef union tlv { @@ -122,9 +122,9 @@ int build_neighbour_req(union tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port); int build_network_hash(tlv *tlv, list *data_list); int build_network_state_req(tlv *tlv); -int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data); +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data); int build_node_state_req(tlv *tlv, uint64_t node_id); -int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len); -int build_warning(tlv *tlv, char *message, size_t message_len); +int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data, size_t data_len); +int build_warning(tlv *tlv, unsigned char *message, size_t message_len); #endif From 733c700492b3dca19be90b7374309188ec49cff1 Mon Sep 17 00:00:00 2001 From: nis Date: Sun, 3 May 2020 00:19:54 +0200 Subject: [PATCH 46/59] struct padding error fix --- src/hash.c | 13 +++-- src/node.c | 168 +++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 145 insertions(+), 36 deletions(-) diff --git a/src/hash.c b/src/hash.c index a3665a0..7414934 100644 --- a/src/hash.c +++ b/src/hash.c @@ -10,7 +10,11 @@ void hash_data(pub_data *data, unsigned char *buf) { // The resulting buf is hashed and put into a buffer unsigned char hash[SHA256_DIGEST_LENGTH]; - SHA256(concat, totlen, hash); + + SHA256_CTX sha256; + SHA256_Init(&sha256); + SHA256_Update(&sha256, concat, totlen); + SHA256_Final(hash, &sha256); // Put truncated hash into buf hash_trunc(hash, buf); @@ -35,9 +39,10 @@ void hash_network(list *data_list, unsigned char *buf) { } // Hash all of concat to obtain the network hash - if (SHA256(concat, totlen, hash) == NULL) { - print_debug(">> Doing the hash failed : function return NULL, should return a pointer."); - }; + SHA256_CTX sha256; + SHA256_Init(&sha256); + SHA256_Update(&sha256, concat, concat_len); + SHA256_Final(hash, &sha256); // Put truncated hash into buf hash_trunc(hash, buf); diff --git a/src/node.c b/src/node.c index beae120..c2fa4af 100644 --- a/src/node.c +++ b/src/node.c @@ -698,6 +698,51 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){ return 7; case 8: if(tlv_len < MIN_LEN_NODE_STATE || tlv_len > MAX_LEN_NODE_STATE) return -1; + + /* + // Check if it has the right hash + uint16_t *seqno = (uint16_t*) (data + pos + 10); + uint64_t *id = (uint64_t*) (data + pos + 2); + + pub_data pdata = (pub_data) { + .length = tlv_len, + .id = *id, + .seqno = *seqno, + .data = (unsigned char*) malloc(tlv_len) + }; + + unsigned char *cur_hash = (unsigned char*) (data + pos + 12); + char *cur_message = data + pos + 28; + + unsigned char hash[16]; + + memcpy(pdata.data, cur_message, tlv_len); + + hash_data(&pdata, hash); + + if(memcmp(hash, cur_hash, 16) != 0) { + print_debug(">> Malformed hash."); + printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); + + for(int x = 0; x < 16; x++){ + printf("%02x", hash[x]); + fflush(0); + } + + printf("\n"); + printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); + + for(int x = 0; x < 16; x++){ + printf("%02x", cur_hash[x]); + fflush(0); + } + + printf("\n"); + + return -1; + + } + */ return 8; case 9: return 9; @@ -772,7 +817,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * int nbr_of_tlvs = 0; int pos = 4; - unsigned char tlv_len, hash[16]; + unsigned char tlv_len, hash[16], hash2[16]; char warn[32]; // The TLV we are going to send back. @@ -798,6 +843,12 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * int ifindex = 0; + // Temporary values + uint16_t *port, *seqno; + uint64_t *id; + unsigned char *cur_hash, *ip; + char *cur_message; + while(pos < total_packet_len) { // Making sure the current TLV we are looking at is valid. // TODO : I think we should reset all the structs here. @@ -850,13 +901,14 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * print_debug(">> Received neighbour tlv, sending back network hash."); // We received a neighbour tlv so a tlv network hash is sent to that address - cur_tlv.neighbour = (neighbour*) (data + pos); + ip = (unsigned char*) (data + pos + 2); + port = (uint16_t*) (data + pos + 18); // Init dest socket memset(&new_neighbour, 0, sizeof(new_neighbour)); new_neighbour.sin6_family = AF_INET6; - memcpy(&new_neighbour.sin6_addr, &cur_tlv.neighbour->ip, 16); - new_neighbour.sin6_port = be16toh(cur_tlv.neighbour->port); + memcpy(&new_neighbour.sin6_addr, ip, 16); + new_neighbour.sin6_port = be16toh(*port); new_neighbour.sin6_scope_id = ifindex; // Build network hash @@ -874,7 +926,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * print_debug(">> Received network_hash, comparing with our own.."); // We reveived a network hash tlv so we compare the hash with our own, // if they differ we send a network state request tlv - cur_tlv.network_hash = (network_hash*) (data + pos); + cur_hash = (unsigned char*) (data + pos + 2); + hash_network(data_list, hash); if (DEBUG_LEVEL > 1) { @@ -886,13 +939,13 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * printf("\n"); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); for(int x = 0; x < 16; x++){ - printf("%02x", cur_tlv.network_hash->network_hash[x]); + printf("%02x", cur_hash[x]); fflush(0); } printf("\n"); } - if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) != 0) { + if(memcmp(hash, cur_hash, 16) != 0) { print_debug(">> Sending out our network hash."); build_network_state_req(&new_tlv); send_single_tlv(&new_tlv, sender, socket_num); @@ -931,16 +984,20 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // we send a node state request, //if the hashes are identical nothing has to be done print_debug(">> Received node hash, updating message entry..."); - cur_tlv.node_hash = (node_hash*) (data + pos); - pdata = get_data(be64toh(cur_tlv.node_hash->node_id)); + + id = (uint64_t*) (data + pos + 2); + cur_hash = (unsigned char*) (data + pos + 12); + + pdata = get_data(be64toh(*id)); // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { + print_debug(">> Comparing hashes..."); // We hash the data stored in the data list hash_data(pdata, hash); // If both hashes are the same then nothing has to be done - if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) { + if(memcmp(hash, cur_hash, 16) == 0) { // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; @@ -950,9 +1007,9 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * } - print_debug(">> No hash found, or hashs are differents, sending back node state request."); + print_debug(">> No hash found, or hashs are different, sending back node state request."); // If no pub_data was found or the hashes differ then we send a node state request - build_node_state_req(&new_tlv, be64toh(cur_tlv.node_hash->node_id)); + build_node_state_req(&new_tlv, be64toh(*id)); add_tlv(&pack, &new_tlv, sender, socket_num); // The position is updated @@ -966,8 +1023,10 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // so a node state tlv for this node id has to be sent, // if no pub_data exists for this id nothing is sent print_debug(">> Received node state request. Processing..."); - cur_tlv.node_state_req = (node_state_req*) (data + pos); - pdata = get_data(be64toh(cur_tlv.node_state_req->node_id)); + + id = (uint64_t*) (data + pos + 2); + + pdata = get_data(be64toh(*id)); if(pdata != NULL) { build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); @@ -986,46 +1045,90 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // we add it to the data list // or update the data stored print_debug(">> Received node state, updating..."); - cur_tlv.node_state = (node_state*) (data + pos); + + tlv_len = data[pos + 1]; + id = (uint64_t*) (data + pos + 2); + seqno = (uint16_t*) (data + pos + 10); + cur_hash = (unsigned char*) (data + pos + 12); + cur_message = data + pos + 28; print_debug(">> Received message ! "); - printf("Type : %d\n", cur_tlv.node_state->type ); - printf("Length : %d\n", cur_tlv.node_state->length ); - printf("Node ID : %lu\n", be64toh(cur_tlv.node_state->node_id) ); - printf("Seqno : %hu\n", be16toh(cur_tlv.node_state->seqno) ); + printf("Type : %d\n", data[pos] ); + printf("Length : %d\n", tlv_len ); + printf("Node ID : %lu\n", be64toh(*id) ); + printf("Seqno : %hu\n", be16toh(*seqno) ); printf("Hash :"); for(int x = 0; x < 16; x++){ - printf("%02x", cur_tlv.node_state->node_hash[x]); + printf("%02x", cur_hash[x]); fflush(0); } printf("\n"); - for(int x = 0; x < 192; x++){ - printf("%d", cur_tlv.node_state->data[x]); + for(int x = 0; x < tlv_len; x++){ + printf("%c", cur_message[x]); fflush(0); } + printf("\n"); + // Check if it has the right hash + pub_data pdata_check = (pub_data) { + .length = tlv_len, + .id = *id, + .seqno = *seqno, + .data = (unsigned char*) malloc(tlv_len) + }; + + memcpy(pdata_check.data, cur_message, tlv_len); + + hash_data(&pdata_check, hash2); + + if(memcmp(hash2, cur_hash, 16) != 0) { + print_debug(">> Malformed hash."); + printf("\x1b[31m[DEBUG]\x1b[0m >> Calculated : "); + + for(int x = 0; x < 16; x++){ + printf("%02x", hash2[x]); + fflush(0); + } + + printf("\n"); + printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); + + for(int x = 0; x < 16; x++){ + printf("%02x", cur_hash[x]); + fflush(0); + } + + printf("\n"); + + pos += tlv_len + 2; + break; + } + + + /* if (DEBUG_LEVEL > 0) { - if (cur_tlv.node_state->data == NULL) { + if (cur_message == NULL) { print_error("The data in the current node is NULL !"); return -1; } - printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_tlv.node_state->data); + printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_message); sleep(1); } + */ // Compare hashes - pdata = get_data(be64toh(cur_tlv.node_state->node_id)); + pdata = get_data(be64toh(*id)); // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { + print_debug(">> Comparing hashes..."); // We hash the data stored in the data list memset(hash, 0, 16); hash_data(pdata, hash); // If both hashes are the same then nothing has to be done - if(memcmp(hash, cur_tlv.node_state->node_hash, 16) == 0) { + if(memcmp(hash, cur_hash, 16) == 0) { // The position is updated - tlv_len = data[pos+1]; pos += tlv_len + 2; break; @@ -1034,12 +1137,11 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * } // Else, we update the data - int rc = add_data(cur_tlv.node_state->length - 26, be64toh(cur_tlv.node_state->node_id), be16toh(cur_tlv.node_state->seqno), cur_tlv.node_state->data, pdata); + int rc = add_data(tlv_len - 26, be64toh(*id), be16toh(*seqno), (unsigned char*) cur_message, pdata); if (rc < 0) { print_error("Error while adding node state !"); } // The position is updated - tlv_len = data[pos+1]; pos += tlv_len + 2; break; @@ -1048,12 +1150,14 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // We received a warning tlv so it's message is printed cur_tlv.warning = (warning*) (data + pos); + tlv_len = data[pos+1]; + cur_message = data + pos + 2; + // Print exactly new_tlv.length characters from new_tlv.message - sprintf(warn, ">> WARNING:\n%%.%ds", cur_tlv.warning->length + 1); - printf(warn, cur_tlv.warning->message); + sprintf(warn, ">> WARNING:\n%%.%ds", tlv_len + 1); + printf(warn, cur_message); // The position is updated - tlv_len = data[pos+1]; pos += tlv_len + 2; break; From c56a15176294d077e792966813af0b84afe29eae Mon Sep 17 00:00:00 2001 From: nis Date: Sun, 3 May 2020 21:38:04 +0200 Subject: [PATCH 47/59] hash fix (not finished yet) --- Makefile | 2 +- src/hash.c | 45 ++++++++++++++++++++++++++++++++--- src/node.c | 70 +++++++++++++++++++++++++++++++++++------------------- src/node.h | 9 +++---- src/tlv.c | 41 +++++--------------------------- src/tlv.h | 6 ++--- 6 files changed, 103 insertions(+), 70 deletions(-) diff --git a/Makefile b/Makefile index 3d676e4..321bd99 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ TARGET ?= dazibao SRC_DIRS ?= ./src/* CC := gcc -CFLAGS= -O2 -Wall +CFLAGS= -O2 -Wall -g SRCS := $(shell find $(SRC_DIRS) -name *.c -or -name *.s) OBJS := $(addsuffix .o,$(basename $(SRCS))) DEPS := $(OBJS:.o=.d) diff --git a/src/hash.c b/src/hash.c index 7414934..853d474 100644 --- a/src/hash.c +++ b/src/hash.c @@ -3,6 +3,14 @@ // Hash a single data void hash_data(pub_data *data, unsigned char *buf) { + /* + data->length = 52 - 26; + data->id = 34538; + data->seqno = 4864; + data->data = "Luke, je suis ton \"pair\" !"; + printf("Hash received: 9ffe841a99776f6d1295ac75b53a58d7\n"); + */ + // All three fields are concatenated into a single buffer int totlen = data->length + 10; unsigned char concat[totlen]; @@ -18,6 +26,16 @@ void hash_data(pub_data *data, unsigned char *buf) { // Put truncated hash into buf hash_trunc(hash, buf); + + /* + printf("Hash built: "); + for(int i = 0; i < 16; i++) { + printf("%02x", buf[i]); + } + printf("\n"); + + exit(1); + */ } // Hash every data contained in data_list then return a network hash @@ -61,15 +79,36 @@ void hash_trunc(unsigned char *hash32oct, unsigned char *buf) { // Concat all fields of data and put them in buf void concat_data(pub_data *data, unsigned char *buf) { - if (memcpy(buf, &(data->id), 8) == NULL) { + // Turn seqno to big endian + uint16_t seqno = htobe16(data->seqno); + + if (memcpy(buf, (char*) &data->id, 8) == NULL) { print_debug(">> Concat the data (id) didn't work !"); - }; - if (memcpy(buf+8, &(data->seqno), 2) == NULL) { + } + if (memcpy(buf+8, (char*) &seqno, 2) == NULL) { print_debug(">> concat the data (seqno) didn't work !"); } if (memcpy(buf+10, data->data, data->length) == NULL) { print_debug(">> Contact the data (data) didn't work !"); } + + /* + uint64_t *id = (uint64_t *) buf; + uint16_t *seqno2 = (uint16_t *) (buf + 8); + char *message = (char*) (buf + 10); + char fuck[100]; + + printf("id: %ld\nseqno: %d\nmessage: ", *id, *seqno2); + for(int i = 0; i < data->length; i++) { + printf("%c", message[i]); + } + printf("\nORIGINAL\n"); + printf("id: %ld\nseqno: %d\nmessage: ", data->id, htobe16(data->seqno)); + for(int i = 0; i < data->length; i++) { + printf("%c", data->data[i]); + } + printf("\n"); + */ } // Concat hash2 to hash1 (hash1 is modified) diff --git a/src/node.c b/src/node.c index c2fa4af..3a44818 100644 --- a/src/node.c +++ b/src/node.c @@ -192,9 +192,9 @@ pub_data *get_data(uint64_t id) { } // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data) { +pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data) { pub_data *new_data = (pub_data*) malloc(sizeof(pub_data)); - unsigned char *_data = (unsigned char*) malloc(len); + char *_data = (char*) malloc(len); if (_data == NULL) { print_error("Failed to allocate memory for copying the data !"); return NULL; @@ -213,7 +213,7 @@ pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned cha } // A node state TLV was received and either no data associated to it's id is in our data list or the data was updated, return -1 if an error occurend, 0 if nothing had to be done and 1 if something was updated/added -int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found) { +int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found) { // Check if it's our own id if(id == NODE_ID) { // wtf @@ -249,7 +249,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data // Updata message free(found->data); - found->data = (unsigned char*) malloc(len); + found->data = (char*) malloc(len); memcpy(found->data, data, len); printf(">> Updated %li's published data.\n", id); @@ -483,12 +483,12 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i int error_while_sending = 0; // Creating the struct to send out with sendmsg - struct msghdr packet_tlv_send_out = { - .msg_name = dest, - .msg_namelen = sizeof(struct sockaddr_in6), - .msg_iov = vec_buff, - .msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2. - }; + struct msghdr packet_tlv_send_out; + memset(&packet_tlv_send_out, 0, sizeof(struct msghdr)); + packet_tlv_send_out.msg_name = dest; + packet_tlv_send_out.msg_namelen = sizeof(struct sockaddr_in6); + packet_tlv_send_out.msg_iov = vec_buff; + packet_tlv_send_out.msg_iovlen = 1; // We have only one iovec buffer. But if we had 2, we would write 2. int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0); if (response_code < 0) { @@ -504,6 +504,7 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i if (error_while_sending == 1) { return -1; } else { + printf(">> Packet successfully sent to peer. %d bytes sent.\n", response_code); return 0; } } @@ -794,8 +795,8 @@ int add_message(char * message, int message_len){ our_data->seqno = (our_data->seqno + 1) % 65535; our_data->length = message_len; free(our_data->data); - our_data->data = (unsigned char*) malloc(message_len); - memcpy(our_data->data, (unsigned char *) message, message_len); + our_data->data = (char*) malloc(message_len); + memcpy(our_data->data, message, message_len); print_debug(">> Message added."); @@ -990,6 +991,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * pdata = get_data(be64toh(*id)); + printf("Received: %ld\n", be64toh(*id)); + // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { print_debug(">> Comparing hashes..."); @@ -1001,13 +1004,13 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; - print_debug(">> Both hashs are the same, nothing to do."); + print_debug(">> Both hashes are the same, nothing to do."); break; } } - print_debug(">> No hash found, or hashs are different, sending back node state request."); + print_debug(">> No hash found, or hashes are different, sending back node state request."); // If no pub_data was found or the hashes differ then we send a node state request build_node_state_req(&new_tlv, be64toh(*id)); add_tlv(&pack, &new_tlv, sender, socket_num); @@ -1033,6 +1036,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * add_tlv(&pack, &new_tlv, sender, socket_num); } else { print_debug(">> Found no data for the requested node, skipping..."); + printf("Received: %ld\n", be64toh(*id)); } // The position is updated @@ -1063,7 +1067,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * fflush(0); } printf("\n"); - for(int x = 0; x < tlv_len; x++){ + for(int x = 0; x < tlv_len - 26; x++){ printf("%c", cur_message[x]); fflush(0); } @@ -1071,16 +1075,33 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // Check if it has the right hash pub_data pdata_check = (pub_data) { - .length = tlv_len, - .id = *id, - .seqno = *seqno, - .data = (unsigned char*) malloc(tlv_len) + .length = tlv_len - 26, + .id = be64toh(*id), + .seqno = be16toh(*seqno), + .data = (char*) malloc(tlv_len) }; - memcpy(pdata_check.data, cur_message, tlv_len); + memcpy(pdata_check.data, cur_message, tlv_len - 26); hash_data(&pdata_check, hash2); + print_debug(">> Built message: "); + printf("Type : %d\n", data[pos] ); + printf("Length : %d\n", pdata_check.length ); + printf("Node ID : %lu\n", pdata_check.id); + printf("Seqno : %hu\n", pdata_check.seqno); + printf("Hash :"); + for(int x = 0; x < 16; x++){ + printf("%02x", hash2[x]); + fflush(0); + } + printf("\n"); + for(int x = 0; x < tlv_len - 26; x++){ + printf("%c", pdata_check.data[x]); + fflush(0); + } + printf("\n"); + if(memcmp(hash2, cur_hash, 16) != 0) { print_debug(">> Malformed hash."); printf("\x1b[31m[DEBUG]\x1b[0m >> Calculated : "); @@ -1104,6 +1125,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * break; } + free(pdata_check.data); /* if (DEBUG_LEVEL > 0) { @@ -1137,7 +1159,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * } // Else, we update the data - int rc = add_data(tlv_len - 26, be64toh(*id), be16toh(*seqno), (unsigned char*) cur_message, pdata); + int rc = add_data(tlv_len - 26, be64toh(*id), be16toh(*seqno), cur_message, pdata); if (rc < 0) { print_error("Error while adding node state !"); } @@ -1154,7 +1176,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * cur_message = data + pos + 2; // Print exactly new_tlv.length characters from new_tlv.message - sprintf(warn, ">> WARNING:\n%%.%ds", tlv_len + 1); + sprintf(warn, ">> WARNING:\n%%.%ds\n", tlv_len + 1); printf(warn, cur_message); // The position is updated @@ -1166,7 +1188,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * strcpy(warn, "Packet is malformed."); print_debug(">> Malformed packet, we won't treat it."); print_debug(">> Sending back a Warning to sender."); - build_warning(&new_tlv,(unsigned char *) warn, strlen(warn)); + build_warning(&new_tlv, warn, strlen(warn)); add_tlv(&pack, &new_tlv, sender, socket_num); return -1; @@ -1371,7 +1393,7 @@ int run_node(int sock_fd){ printf("\x1b[31m[DEBUG]\x1b[0m >> Adding following message to the table : “%s”\n", input_buffer ); } // Add message to the message table. - if (add_message(input_buffer, bytes) < 0) { + if (add_message(input_buffer, bytes-1) < 0) { print_debug(">> Error while trying to add the message to the list of messages, please try again.."); // Reset buffer memset(input_buffer, 0, sizeof(input_buffer)); diff --git a/src/node.h b/src/node.h index af0e515..b27d116 100644 --- a/src/node.h +++ b/src/node.h @@ -43,7 +43,7 @@ typedef struct pub_data { unsigned char length; uint64_t id; uint16_t seqno; - unsigned char *data; + char *data; } pub_data; // General list @@ -62,7 +62,8 @@ typedef struct list { // The node ID // #define NODE_ID 42675882021843277 -#define NODE_ID 13809235719890846928 +// #define NODE_ID 13809235719890846928 +static uint64_t NODE_ID = 80927976239; // The number of neighbours // The neighbour table has 15 entries @@ -141,9 +142,9 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port); pub_data *get_data(uint64_t id); // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data); +pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data); // add new data to data list -int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found); +int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found); #endif diff --git a/src/tlv.c b/src/tlv.c index 0dcf492..4614db8 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -1,33 +1,5 @@ #include "tlv.h" -// TODO this can be deleted ? -// creer un tlv -int build_tlv(tlv *tlv, cmd_token token) { - switch(token.type) { - case NEIGHBOUR_REQ: - // a remplir - break; - case NETWORK_STATE_REQ: - // a remplir - break; - case NODE_STATE_REQ: - // a remplir - break; - case POST: - // a remplir - break; - case ERROR: - printf("Wrong format, use 'req {neighbour | network state | node state}' or 'post {message}'"); - break; - default: - perror("Unrecognized tlv type."); - return -1; - } - - return -1; -} -// end deletion - int build_pad1(tlv *tlv) { // Free the previously allocated memory free(tlv->pad1); @@ -119,7 +91,7 @@ int build_network_hash(tlv *tlv, list *data_list) { new->type = 4; new->length = 16; - hash_network(data_list, (unsigned char*) new->network_hash); + hash_network(data_list, new->network_hash); tlv->network_hash = new; @@ -144,7 +116,7 @@ int build_network_state_req(tlv *tlv) { return 0; } -int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data) { +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data) { // Free the previously allocated memory free(tlv->pad1); @@ -160,7 +132,7 @@ int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *d new->seqno = htobe16(seqno); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; - hash_data(&pdata, (unsigned char*) new->node_hash); + hash_data(&pdata, new->node_hash); tlv->node_hash = new; @@ -174,7 +146,6 @@ int build_node_state_req(tlv *tlv, uint64_t node_id) { node_state_req *new = (node_state_req*) malloc(sizeof(node_state_req)); memset(new, 0, sizeof(node_state_req)); - if(new == NULL) return -1; @@ -187,7 +158,7 @@ int build_node_state_req(tlv *tlv, uint64_t node_id) { return 0; } -int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data, size_t data_len) { +int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len) { // Free the previously allocated memory free(tlv->pad1); @@ -212,14 +183,14 @@ int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char * memcpy(new->data, data, len); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; - hash_data(&pdata, (unsigned char*) new->node_hash); + hash_data(&pdata, new->node_hash); tlv->node_state = new; return 0; } -int build_warning(tlv *tlv, unsigned char *message, size_t message_len) { +int build_warning(tlv *tlv, char *message, size_t message_len) { // Free the previously allocated memory free(tlv->pad1); diff --git a/src/tlv.h b/src/tlv.h index d2ebff5..de4399a 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -122,9 +122,9 @@ int build_neighbour_req(union tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port); int build_network_hash(tlv *tlv, list *data_list); int build_network_state_req(tlv *tlv); -int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data); +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data); int build_node_state_req(tlv *tlv, uint64_t node_id); -int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data, size_t data_len); -int build_warning(tlv *tlv, unsigned char *message, size_t message_len); +int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len); +int build_warning(tlv *tlv, char *message, size_t message_len); #endif From e5ec8b36aef3833492904853ed8b36f852425d7a Mon Sep 17 00:00:00 2001 From: nis Date: Sun, 3 May 2020 23:43:17 +0200 Subject: [PATCH 48/59] adding tlv to packet error fix --- src/hash.c | 2 - src/node.c | 189 +++++++++++++++++++++++++++++++---------------------- src/node.h | 2 +- src/tlv.c | 24 ++----- src/tlv.h | 6 +- 5 files changed, 121 insertions(+), 102 deletions(-) diff --git a/src/hash.c b/src/hash.c index 853d474..c1712e5 100644 --- a/src/hash.c +++ b/src/hash.c @@ -33,8 +33,6 @@ void hash_data(pub_data *data, unsigned char *buf) { printf("%02x", buf[i]); } printf("\n"); - - exit(1); */ } diff --git a/src/node.c b/src/node.c index 3a44818..bdcd4b6 100644 --- a/src/node.c +++ b/src/node.c @@ -394,7 +394,11 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { if(pack->length + len > 1020) { errval = send_packet((char*) pack, pack->length, dest, socket_num); - *pack = (packet) {.magic = 95, .version = 1, .length = 0}; + + pack->magic = 95; + pack->version = 1; + pack->length = 0; + memset(pack->body, 0, 1020); sent = 1; } @@ -403,7 +407,11 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // we just ignore that padding, and send out the packet. if(pack->length >= 1020) { errval = send_packet((char*) pack, pack->length, dest, socket_num); - *pack = (packet) {.magic = 95, .version = 1, .length = 0}; + + pack->magic = 95; + pack->version = 1; + pack->length = 0; + memset(pack->body, 0, 1020); sent = 1; } @@ -412,47 +420,73 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // Copy data from tlv into body switch(type) { case 1: - memcpy(pack->body + pack->length, tlv->pad1, 1); + memcpy(pack->body + pack->length, (char*) &tlv->pad1->type, 1); pack->length += 1; break; case 2: - memcpy(pack->body + pack->length, tlv->padn, len); + memcpy(pack->body + pack->length, (char*) &tlv->padn->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->padn->length, 1); + memcpy(pack->body + pack->length + 2, (char*) &tlv->padn->mbz, tlv->padn->length); + pack->length += len; break; case 3: - memcpy(pack->body + pack->length, tlv->neighbour, len); + memcpy(pack->body + pack->length, (char*) &tlv->neighbour->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->neighbour->length, 1); + pack->length += len; break; case 4: - memcpy(pack->body + pack->length, tlv->network_hash, len); + memcpy(pack->body + pack->length, (char*) &tlv->network_hash->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->network_hash->length, 1); + memcpy(pack->body + pack->length + 2, (char*) &tlv->network_hash->network_hash, 16); + pack->length += len; break; case 5: - memcpy(pack->body + pack->length, tlv->network_state_req, len); + memcpy(pack->body + pack->length, (char*) &tlv->network_state_req->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->network_state_req->length, 1); + pack->length += len; break; case 6: - memcpy(pack->body + pack->length, tlv->node_hash, len); + memcpy(pack->body + pack->length, (char*) &tlv->node_hash->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->node_hash->length, 1); + memcpy(pack->body + pack->length + 2, (char*) &tlv->node_hash->node_id, 8); + memcpy(pack->body + pack->length + 10, (char*) &tlv->node_hash->seqno, 2); + memcpy(pack->body + pack->length + 12, (char*) &tlv->node_hash->node_hash, 16); + pack->length += len; break; case 7: - memcpy(pack->body + pack->length, tlv->node_state_req, len); + memcpy(pack->body + pack->length, (char*) &tlv->node_state_req->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->node_state_req->length, 1); + pack->length += len; break; case 8: - memcpy(pack->body + pack->length, tlv->node_state, len); + memcpy(pack->body + pack->length, (char*) &tlv->node_state->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->node_state->length, 1); + memcpy(pack->body + pack->length + 2, (char*) &tlv->node_state->node_id, 8); + memcpy(pack->body + pack->length + 10, (char*) &tlv->node_state->seqno, 2); + memcpy(pack->body + pack->length + 12, (char*) &tlv->node_state->node_hash, 16); + memcpy(pack->body + pack->length + 28, (char*) &tlv->node_state->data, tlv->node_state->length - 26); + pack->length += len; break; case 9: - memcpy(pack->body + pack->length, tlv->warning, len); + memcpy(pack->body + pack->length, (char*) &tlv->warning->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->warning->length, 1); + memcpy(pack->body + pack->length + 2, (char*) &tlv->warning->message, tlv->warning->length); + pack->length += len; break; @@ -473,7 +507,8 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // Send length bytes from packet int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, int socket_num) { - ((packet*) packet_buff)->length = htobe16(((packet*) packet_buff)->length); + uint16_t be_len = htobe16(*(uint16_t*) (packet_buff + 2)); + memcpy(packet_buff + 2, &be_len, 2); // Vectorized buffer struct iovec vec_buff[1]; @@ -512,63 +547,80 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i // Send a single TLV to the specified addresses, return -1 if an error was encountered, 0 otherwise int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { char type = tlv->pad1->type; - unsigned char len; packet pack = (packet) {.magic = 95, .version = 1, .length = 0}; memset(pack.body, 0, 1020); // Copy data from tlv into body - switch(type) { + switch(type) { case 1: - memcpy(pack.body, tlv->pad1, 1); + memcpy(pack.body + pack.length, (char*) &tlv->pad1->type, 1); pack.length += 1; break; case 2: - len = tlv->padn->length + 2; - memcpy(pack.body, tlv->padn, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->padn->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->padn->length, 1); + memcpy(pack.body + pack.length + 2, (char*) &tlv->padn->mbz, tlv->padn->length); + + pack.length += tlv->padn->length + 2; break; case 3: - len = tlv->neighbour->length + 2; - memcpy(pack.body, tlv->neighbour, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->neighbour->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->neighbour->length, 1); + + pack.length += tlv->neighbour->length + 2; break; case 4: - len = tlv->network_hash->length + 2; - memcpy(pack.body, tlv->network_hash, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->network_hash->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->network_hash->length, 1); + memcpy(pack.body + pack.length + 2, (char*) &tlv->network_hash->network_hash, 16); + + pack.length += tlv->network_hash->length + 2; break; case 5: - len = tlv->network_state_req->length + 2; - memcpy(pack.body, tlv->network_state_req, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->network_state_req->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->network_state_req->length, 1); + + pack.length += tlv->network_state_req->length + 2; break; case 6: - len = tlv->node_hash->length + 2; - memcpy(pack.body, tlv->node_hash, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->node_hash->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->node_hash->length, 1); + memcpy(pack.body + pack.length + 2, (char*) &tlv->node_hash->node_id, 8); + memcpy(pack.body + pack.length + 10, (char*) &tlv->node_hash->seqno, 2); + memcpy(pack.body + pack.length + 12, (char*) &tlv->node_hash->node_hash, 16); + + pack.length += tlv->node_hash->length + 2; break; case 7: - len = tlv->node_state_req->length + 2; - memcpy(pack.body, tlv->node_state_req, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->node_state_req->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->node_state_req->length, 1); + + pack.length += tlv->node_state_req->length + 2; break; case 8: - len = tlv->node_state->length + 2; - memcpy(pack.body, tlv->node_state, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->node_state->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->node_state->length, 1); + memcpy(pack.body + pack.length + 2, (char*) &tlv->node_state->node_id, 8); + memcpy(pack.body + pack.length + 10, (char*) &tlv->node_state->seqno, 2); + memcpy(pack.body + pack.length + 12, (char*) &tlv->node_state->node_hash, 16); + memcpy(pack.body + pack.length + 28, (char*) &tlv->node_state->data, tlv->node_state->length - 26); + + pack.length += tlv->node_state->length + 2; break; case 9: - len = tlv->warning->length + 2; - memcpy(pack.body, tlv->warning, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->warning->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->warning->length, 1); + memcpy(pack.body + pack.length + 2, (char*) &tlv->warning->message, tlv->warning->length); + + pack.length += tlv->warning->length + 2; break; default: @@ -823,11 +875,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // The TLV we are going to send back. tlv new_tlv; - // The tlv we are currently looking at. - tlv cur_tlv; - new_tlv.pad1 = NULL; - cur_tlv.pad1 = NULL; + list * tmp_list; pub_data * pdata; @@ -969,9 +1018,11 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * while(tmp_list != NULL) { pdata = (pub_data*) tmp_list->data; - build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data); + build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); add_tlv(&pack, &new_tlv, sender, socket_num); tmp_list = tmp_list->next; + + printf("%lu\n", *(uint64_t*) (pack.body + 2)); } // The position is updated @@ -991,8 +1042,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * pdata = get_data(be64toh(*id)); - printf("Received: %ld\n", be64toh(*id)); - // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { print_debug(">> Comparing hashes..."); @@ -1015,6 +1064,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * build_node_state_req(&new_tlv, be64toh(*id)); add_tlv(&pack, &new_tlv, sender, socket_num); + printf(">> Sending node state request for node %lu...\n", be64toh(*id)); + // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; @@ -1036,7 +1087,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * add_tlv(&pack, &new_tlv, sender, socket_num); } else { print_debug(">> Found no data for the requested node, skipping..."); - printf("Received: %ld\n", be64toh(*id)); + printf("Skipped TLV:\nType: %d\nLength: %d\nNode ID: %lu\n", data[pos], data[pos+1], be64toh(*id)); } // The position is updated @@ -1087,7 +1138,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * print_debug(">> Built message: "); printf("Type : %d\n", data[pos] ); - printf("Length : %d\n", pdata_check.length ); + printf("Length : %d\n", pdata_check.length + 26); printf("Node ID : %lu\n", pdata_check.id); printf("Seqno : %hu\n", pdata_check.seqno); printf("Hash :"); @@ -1170,8 +1221,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * case 9: print_debug(">> \aReceived warning !"); // We received a warning tlv so it's message is printed - cur_tlv.warning = (warning*) (data + pos); - tlv_len = data[pos+1]; cur_message = data + pos + 2; @@ -1269,46 +1318,32 @@ int t_get_network_state(int sock_fd){ while (tmp_list != NULL) { neighbour_peer * peer = (neighbour_peer *) tmp_list->data; - tlv * new_tlv = malloc(sizeof(union tlv)); - memset(new_tlv, 0, sizeof(union tlv)); - if (new_tlv == NULL) { - print_error("Error while allocating memory for the TLV !"); - return -1; - } + tlv new_tlv; + memset(&new_tlv, 0, sizeof(union tlv)); // Create the structure for the receiver. - struct sockaddr_in6 * receiver = malloc(sizeof(struct sockaddr_in6)); - memset(receiver, 0, sizeof(struct sockaddr_in6)); - if (receiver == NULL) { - print_error("Error while allocating memory for the peer address!"); - return -1; - } - receiver->sin6_family = AF_INET6; - receiver->sin6_addr = peer->ip; - receiver->sin6_port = htobe16(peer->port); - receiver->sin6_scope_id = 0; + struct sockaddr_in6 receiver; + memset(&receiver, 0, sizeof(struct sockaddr_in6)); + + receiver.sin6_family = AF_INET6; + receiver.sin6_addr = peer->ip; + receiver.sin6_port = htobe16(peer->port); + receiver.sin6_scope_id = 0; // Send out a TLV network state. - list * tmp_data_list = data_list; - - if (build_network_hash(new_tlv, tmp_data_list) < 0) { + if (build_network_hash(&new_tlv, data_list) < 0) { print_error("Error while building a network hash."); return -1; } else { - if (send_single_tlv(new_tlv, receiver, sock_fd) < 0) { + if (send_single_tlv(&new_tlv, &receiver, sock_fd) < 0) { print_error("Error while sending a network hash to a peer."); return -1; } else { print_debug(">> Sent network hash to a peer."); } } - free(new_tlv); - free(receiver); - if (tmp_list->next == NULL) { - break; - } else { - tmp_list = tmp_list->next; - } + + tmp_list = tmp_list->next; } } return 0; diff --git a/src/node.h b/src/node.h index b27d116..dbce63b 100644 --- a/src/node.h +++ b/src/node.h @@ -63,7 +63,7 @@ typedef struct list { // The node ID // #define NODE_ID 42675882021843277 // #define NODE_ID 13809235719890846928 -static uint64_t NODE_ID = 80927976239; +#define NODE_ID 1312 // The number of neighbours // The neighbour table has 15 entries diff --git a/src/tlv.c b/src/tlv.c index 4614db8..ebf9830 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -116,7 +116,7 @@ int build_network_state_req(tlv *tlv) { return 0; } -int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data) { +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, unsigned char length) { // Free the previously allocated memory free(tlv->pad1); @@ -131,7 +131,7 @@ int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data) { new->node_id = htobe64(node_id); new->seqno = htobe16(seqno); - pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; + pub_data pdata = (pub_data) {.length = length, .id = node_id, .seqno = seqno, .data = data}; hash_data(&pdata, new->node_hash); tlv->node_hash = new; @@ -165,24 +165,16 @@ int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, siz node_state *new = (node_state*) malloc(sizeof(node_state)); memset(new, 0, sizeof(node_state)); - int len = data_len + 26; - if(new == NULL) return -1; - // en mettant cet octet à 0 on est surs de traiter un champ data de taille 192 max - if(len > 192) { - data[192] = 0; - len = 192; - } - new->type = 8; - new->length = 26 + len; + new->length = data_len + 26; new->node_id = htobe64(node_id); new->seqno = htobe16(seqno); - memcpy(new->data, data, len); + memcpy(new->data, data, data_len); - pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; + pub_data pdata = (pub_data) {.length = data_len, .id = node_id, .seqno = seqno, .data = data}; hash_data(&pdata, new->node_hash); tlv->node_state = new; @@ -202,12 +194,6 @@ int build_warning(tlv *tlv, char *message, size_t message_len) { if(new == NULL) return -1; - // en mettant cet octet à 0 on est surs de traiter un champ message de taille 256 max - if(len > 256) { - message[256] = 0; - len = 256; - } - new->type = 9; new->length = len; memcpy(new->message, message, len); diff --git a/src/tlv.h b/src/tlv.h index de4399a..d976faf 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -85,14 +85,14 @@ typedef struct node_state { uint64_t node_id; uint16_t seqno; unsigned char node_hash[16]; - unsigned char data[192]; + char data[192]; } node_state; // 2 octets min, 258 ocets max (unsigned char 0 -> 255) typedef struct warning { unsigned char type; unsigned char length; - unsigned char message[256]; + char message[256]; } warning; typedef union tlv { @@ -122,7 +122,7 @@ int build_neighbour_req(union tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port); int build_network_hash(tlv *tlv, list *data_list); int build_network_state_req(tlv *tlv); -int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data); +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, unsigned char length); int build_node_state_req(tlv *tlv, uint64_t node_id); int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len); int build_warning(tlv *tlv, char *message, size_t message_len); From bdec3db1c6b5e32fbb1ec824a936554a6ff7d1c1 Mon Sep 17 00:00:00 2001 From: nis Date: Mon, 4 May 2020 00:23:25 +0200 Subject: [PATCH 49/59] node state request fix --- src/node.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/node.c b/src/node.c index bdcd4b6..4406d67 100644 --- a/src/node.c +++ b/src/node.c @@ -467,6 +467,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { case 7: memcpy(pack->body + pack->length, (char*) &tlv->node_state_req->type, 1); memcpy(pack->body + pack->length + 1, (char*) &tlv->node_state_req->length, 1); + memcpy(pack->body + pack->length + 2, (char*) &tlv->node_state_req->node_id, 8); pack->length += len; @@ -600,6 +601,7 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { case 7: memcpy(pack.body + pack.length, (char*) &tlv->node_state_req->type, 1); memcpy(pack.body + pack.length + 1, (char*) &tlv->node_state_req->length, 1); + memcpy(pack.body + pack.length + 2, (char*) &tlv->node_state_req->node_id, 8); pack.length += tlv->node_state_req->length + 2; @@ -1087,7 +1089,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * add_tlv(&pack, &new_tlv, sender, socket_num); } else { print_debug(">> Found no data for the requested node, skipping..."); - printf("Skipped TLV:\nType: %d\nLength: %d\nNode ID: %lu\n", data[pos], data[pos+1], be64toh(*id)); } // The position is updated @@ -1252,6 +1253,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * send_packet((char*) &pack, pack.length, sender, socket_num); } print_data(data_list); + return 0; } From e757389e32d95b3bb5230b25005a6616d8aa0074 Mon Sep 17 00:00:00 2001 From: nis Date: Mon, 4 May 2020 00:40:54 +0200 Subject: [PATCH 50/59] YES --- src/hash.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hash.c b/src/hash.c index c1712e5..9c05afb 100644 --- a/src/hash.c +++ b/src/hash.c @@ -79,8 +79,9 @@ void hash_trunc(unsigned char *hash32oct, unsigned char *buf) { void concat_data(pub_data *data, unsigned char *buf) { // Turn seqno to big endian uint16_t seqno = htobe16(data->seqno); + uint64_t id = htobe64(data->id); - if (memcpy(buf, (char*) &data->id, 8) == NULL) { + if (memcpy(buf, (char*) &id, 8) == NULL) { print_debug(">> Concat the data (id) didn't work !"); } if (memcpy(buf+8, (char*) &seqno, 2) == NULL) { From e1364bc58e9075cc7e0fbb09cacc2bb37f48d46a Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 4 May 2020 12:59:08 +0200 Subject: [PATCH 51/59] Changed debug print, Added memcmp in get_neibourgh Found bug with port number conversion --- src/debug.c | 2 +- src/node.c | 39 ++++++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/debug.c b/src/debug.c index a0bbbcd..c4a8756 100644 --- a/src/debug.c +++ b/src/debug.c @@ -55,7 +55,7 @@ void print_data(list * l){ print_debug(">> Printing messages we know of so far :"); int nbr_msg = 0; setlocale(LC_ALL, "en_US.utf8"); - print_debug(">> Peer ID | Seqno | Length | Message "); + print_debug(">> Peer ID | Seqno | Length | Message "); while(l != NULL){ pub_data * message = l->data; printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); diff --git a/src/node.c b/src/node.c index 4406d67..9cca632 100644 --- a/src/node.c +++ b/src/node.c @@ -113,7 +113,8 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { // check for same ip and same port peer = (neighbour_peer*) tmp->data; - if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 && peer->port == port) { + if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 + && memcmp(&peer->port, &port, sizeof(int16_t)) == 0) { return peer; } @@ -151,10 +152,12 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { time(&curtime); peer->last_seen = curtime; + list * tmp = neighbour_list; + // set new peer as head of list list *node = (list*) malloc(sizeof(list)); node->data = (void*) peer; - node->next = neighbour_list; + node->next = tmp; neighbour_list = node; return 1; } @@ -397,7 +400,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { pack->magic = 95; pack->version = 1; - pack->length = 0; + pack->length = 0; memset(pack->body, 0, 1020); sent = 1; @@ -407,7 +410,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // we just ignore that padding, and send out the packet. if(pack->length >= 1020) { errval = send_packet((char*) pack, pack->length, dest, socket_num); - + pack->magic = 95; pack->version = 1; pack->length = 0; @@ -540,7 +543,9 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i if (error_while_sending == 1) { return -1; } else { - printf(">> Packet successfully sent to peer. %d bytes sent.\n", response_code); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Packet successfully sent to peer. %d bytes sent.\n", response_code); + } return 0; } } @@ -760,9 +765,9 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){ uint64_t *id = (uint64_t*) (data + pos + 2); pub_data pdata = (pub_data) { - .length = tlv_len, - .id = *id, - .seqno = *seqno, + .length = tlv_len, + .id = *id, + .seqno = *seqno, .data = (unsigned char*) malloc(tlv_len) }; @@ -772,7 +777,7 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){ unsigned char hash[16]; memcpy(pdata.data, cur_message, tlv_len); - + hash_data(&pdata, hash); if(memcmp(hash, cur_hash, 16) != 0) { @@ -783,7 +788,7 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){ printf("%02x", hash[x]); fflush(0); } - + printf("\n"); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); @@ -1127,14 +1132,14 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // Check if it has the right hash pub_data pdata_check = (pub_data) { - .length = tlv_len - 26, - .id = be64toh(*id), - .seqno = be16toh(*seqno), + .length = tlv_len - 26, + .id = be64toh(*id), + .seqno = be16toh(*seqno), .data = (char*) malloc(tlv_len) }; memcpy(pdata_check.data, cur_message, tlv_len - 26); - + hash_data(&pdata_check, hash2); print_debug(">> Built message: "); @@ -1162,7 +1167,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * printf("%02x", hash2[x]); fflush(0); } - + printf("\n"); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); @@ -1468,7 +1473,7 @@ int run_node(int sock_fd){ // Treat incoming packets. int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd); if (work_tlv_status < 0) { - print_debug(">> Error while treating the incoming packet."); + print_error("Error while treating the incoming packet."); } // Reset buffer memset(output_buffer, 0, sizeof(output_buffer)); @@ -1522,7 +1527,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ return -3; } - root_peer->port = root_peer_port; + root_peer->port = (int16_t) root_peer_port; root_peer->is_temporary = 0; root_peer->last_seen = root_peer_seen; From 3f6021ccd5e296dffe61affb5170ecd627b32e5b Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 4 May 2020 14:34:53 +0200 Subject: [PATCH 52/59] Fixed addin peer to peer table --- src/node.c | 124 ++++++++++++++++++++++++++--------------------------- src/node.h | 10 ++--- 2 files changed, 67 insertions(+), 67 deletions(-) diff --git a/src/node.c b/src/node.c index 9cca632..ccfc05f 100644 --- a/src/node.c +++ b/src/node.c @@ -96,7 +96,7 @@ neighbour_peer *get_random_neighbour() { } // Search for this peer in the neighbour table -neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { +neighbour_peer *get_neighbour(struct in6_addr *ip, uint16_t port) { print_debug(">> Getting neighbour."); if (DEBUG_LEVEL > 1) { @@ -114,7 +114,7 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { peer = (neighbour_peer*) tmp->data; if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 - && memcmp(&peer->port, &port, sizeof(int16_t)) == 0) { + && memcmp(&peer->port, &port, sizeof(uint16_t)) == 0) { return peer; } @@ -128,7 +128,7 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { // Return -1 if we have enough peers, // 1 if it was added // Return 0 if peer was updated as last_seen -int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { +int add_n_update_neighbour(struct in6_addr *ip, uint16_t port) { // We try to find a peer with this address and port. @@ -141,11 +141,18 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { if(len_list(neighbour_list) >= 15){ return -1; } else { - print_debug(">> Adding them to the peer table.\n"); + if (DEBUG_LEVEL > 0) { + if (DEBUG_LEVEL > 1) { + char * buff_str_ip[1024]; + char * ip_str = (char * ) inet_ntop(AF_INET6,ip,(char * restrict) buff_str_ip, 1024); + printf("\x1b[31m[DEBUG]\x1b[0m >> Adding peer %s @ %i to the peer table.\n", ip_str, port ); + sleep(3); + } + } // if there are less, initialize the new peer to add to the list peer = (neighbour_peer*) malloc(sizeof(neighbour_peer)); memcpy(&peer->ip, ip, sizeof(struct in6_addr)); - peer->port = LISTEN_PORT; + peer->port = port; peer->is_temporary = 1; // set last_seen time @@ -255,7 +262,9 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat found->data = (char*) malloc(len); memcpy(found->data, data, len); - printf(">> Updated %li's published data.\n", id); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Updated %li's published data.\n", id); + } return 1; } @@ -281,7 +290,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat data_list->data = (void*) new_data; data_list->next = tmp; - printf(">> Added new message to data list.\n"); + print_debug(">> Added new message to data list."); return 1; } @@ -292,7 +301,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat new_node->next = tmp; last->next = new_node; - printf(">> Added new message to data list.\n"); + print_debug(">> Added new message to data list."); return 1; } @@ -308,7 +317,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat new_node->next = NULL; last->next = new_node; - printf(">> Added new message to data list.\n"); + print_debug(">> Added new message to data list."); return 1; } @@ -1029,7 +1038,11 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * add_tlv(&pack, &new_tlv, sender, socket_num); tmp_list = tmp_list->next; - printf("%lu\n", *(uint64_t*) (pack.body + 2)); + if (DEBUG_LEVEL > 1) { + // TODO wtf is this ? + printf(" >> \x1b[31m[DEBUG]\x1b[0m %lu\n", *(uint64_t*) (pack.body + 2)); + } + } // The position is updated @@ -1114,21 +1127,21 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * cur_message = data + pos + 28; print_debug(">> Received message ! "); - printf("Type : %d\n", data[pos] ); - printf("Length : %d\n", tlv_len ); - printf("Node ID : %lu\n", be64toh(*id) ); - printf("Seqno : %hu\n", be16toh(*seqno) ); - printf("Hash :"); - for(int x = 0; x < 16; x++){ - printf("%02x", cur_hash[x]); - fflush(0); - } - printf("\n"); - for(int x = 0; x < tlv_len - 26; x++){ - printf("%c", cur_message[x]); - fflush(0); - } - printf("\n"); + // printf("Type : %d\n", data[pos] ); + // printf("Length : %d\n", tlv_len ); + // printf("Node ID : %lu\n", be64toh(*id) ); + // printf("Seqno : %hu\n", be16toh(*seqno) ); + // printf("Hash :"); + // for(int x = 0; x < 16; x++){ + // printf("%02x", cur_hash[x]); + // fflush(0); + // } + // printf("\n"); + // for(int x = 0; x < tlv_len - 26; x++){ + // printf("%c", cur_message[x]); + // fflush(0); + // } + // printf("\n"); // Check if it has the right hash pub_data pdata_check = (pub_data) { @@ -1142,22 +1155,22 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * hash_data(&pdata_check, hash2); - print_debug(">> Built message: "); - printf("Type : %d\n", data[pos] ); - printf("Length : %d\n", pdata_check.length + 26); - printf("Node ID : %lu\n", pdata_check.id); - printf("Seqno : %hu\n", pdata_check.seqno); - printf("Hash :"); - for(int x = 0; x < 16; x++){ - printf("%02x", hash2[x]); - fflush(0); - } - printf("\n"); - for(int x = 0; x < tlv_len - 26; x++){ - printf("%c", pdata_check.data[x]); - fflush(0); - } - printf("\n"); + // print_debug(">> Built message: "); + // printf("Type : %d\n", data[pos] ); + // printf("Length : %d\n", pdata_check.length + 26); + // printf("Node ID : %lu\n", pdata_check.id); + // printf("Seqno : %hu\n", pdata_check.seqno); + // printf("Hash :"); + // for(int x = 0; x < 16; x++){ + // printf("%02x", hash2[x]); + // fflush(0); + // } + // printf("\n"); + // for(int x = 0; x < tlv_len - 26; x++){ + // printf("%c", pdata_check.data[x]); + // fflush(0); + // } + // printf("\n"); if(memcmp(hash2, cur_hash, 16) != 0) { print_debug(">> Malformed hash."); @@ -1184,17 +1197,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * free(pdata_check.data); - /* - if (DEBUG_LEVEL > 0) { - if (cur_message == NULL) { - print_error("The data in the current node is NULL !"); - return -1; - } - printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_message); - sleep(1); - } - */ - // Compare hashes pdata = get_data(be64toh(*id)); @@ -1257,7 +1259,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * if(pack.length > 0){ send_packet((char*) &pack, pack.length, sender, socket_num); } - print_data(data_list); + // print_data(data_list); return 0; } @@ -1276,11 +1278,10 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc struct in6_addr ip = sender->sin6_addr; int16_t port = htobe16(sender->sin6_port); - int rc = add_n_update_neighbour(&ip, port); if( rc == -1) { print_debug(">> We have enough peers, we won't add him.."); - return -1; + return -3; } else if (rc == 1){ print_debug(">> Peer was added to the table.\a"); } else { @@ -1403,7 +1404,6 @@ int run_node(int sock_fd){ // } // printf("\n"); - /* Call poll() */ ret = poll(fds, 2, 10); @@ -1472,9 +1472,11 @@ int run_node(int sock_fd){ } // Treat incoming packets. int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd); - if (work_tlv_status < 0) { - print_error("Error while treating the incoming packet."); - } + if (work_tlv_status == -3) { + print_debug(">> Received paquet from peer not in peer list, and we have enough peers, ignoring..."); + } else if (work_tlv_status < 0) { + print_error("Error while treating the incoming packet."); + } // Reset buffer memset(output_buffer, 0, sizeof(output_buffer)); } @@ -1527,7 +1529,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ return -3; } - root_peer->port = (int16_t) root_peer_port; + root_peer->port = root_peer_port; root_peer->is_temporary = 0; root_peer->last_seen = root_peer_seen; @@ -1567,9 +1569,7 @@ int main(int argc, const char *argv[]) { if (errno == ERANGE || val < 0 || val > UINT16_MAX || end == argv[2] || *end != '\0'){ print_error("Conversion of port number failed, please choose a smaller number."); } - uint16_t root_peer_port = (uint16_t) val; - char * root_peer_ip = (char *) argv[1]; setlocale(LC_ALL, ""); diff --git a/src/node.h b/src/node.h index dbce63b..6fba513 100644 --- a/src/node.h +++ b/src/node.h @@ -27,7 +27,7 @@ */ typedef struct neighbour_peer { struct in6_addr ip; - int16_t port; + uint16_t port; char is_temporary; time_t last_seen; } neighbour_peer; @@ -61,9 +61,9 @@ typedef struct list { #define LISTEN_PORT 1212 // The node ID +#define NODE_ID 42009235719890846928 // #define NODE_ID 42675882021843277 -// #define NODE_ID 13809235719890846928 -#define NODE_ID 1312 +// #define NODE_ID 1312 // The number of neighbours // The neighbour table has 15 entries @@ -133,10 +133,10 @@ int len_list(list *l); neighbour_peer *get_random_neighbour(); // Search for this peer in the neighbour table -neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port); +neighbour_peer *get_neighbour(struct in6_addr *ip, uint16_t port); // Return -1 if the peer could not be added, 0 if it could or if it was already in the table -int add_n_update_neighbour(struct in6_addr *ip, int16_t port); +int add_n_update_neighbour(struct in6_addr *ip, uint16_t port); // get data associated with id, if it doesn't exist return NULL pub_data *get_data(uint64_t id); From 5d47c438640ee77968197a0531bcd0caed07aef0 Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 4 May 2020 16:00:41 +0200 Subject: [PATCH 53/59] Added UI for no debug mode --- src/debug.c | 88 ++++++++++++++++++++++++++++++++++++++--------------- src/debug.h | 9 +++++- src/node.c | 36 +++++++++++++++------- src/node.h | 2 +- 4 files changed, 97 insertions(+), 38 deletions(-) diff --git a/src/debug.c b/src/debug.c index c4a8756..e205e30 100644 --- a/src/debug.c +++ b/src/debug.c @@ -6,6 +6,18 @@ #include "debug.h" #include "node.h" +void welcome(){ + print_info("----------------------------------------"); + print_info(" 大字报"); + print_info("----------------------------------------"); + print_info(">> To write a message, just write it to the terminal and press enter."); + print_info(">> To show all messages, just press enter without writing anything."); +} + +void print_info(char * msg) { + printf("\x1b[1m\x1b[36m[大字报]\x1b[0m %s\n", msg); +} + void print_debug(char * msg){ if (DEBUG_LEVEL > 0) { printf("\x1b[31m[DEBUG]\x1b[0m %s \n", msg); @@ -23,42 +35,69 @@ void print_error(char * msg){ } void print_peers(list * l){ - // Print out the peers we have in the neighbour_list - int nbr_peers = 0; - if (l == NULL) { - print_error("The neighbour_list is empty !"); - } else { - print_debug(">> Printing out peer list :"); - while(l != NULL){ - neighbour_peer * peer = (neighbour_peer *) l->data; - char * buff_str_ip[1024]; - char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); - int last_seen = time(NULL) - peer->last_seen; - printf("\x1b[31m[DEBUG]\x1b[0m >> %s @ %i | is temporary ? %s | last seen %i secs ago. |\n", ip_str, peer->port, peer->is_temporary ? "yes":"no", last_seen); - nbr_peers++; - if (l->next == NULL) { - break; - } else { - l = l->next; - } + if (DEBUG_LEVEL > 0) { + // Print out the peers we have in the neighbour_list + int nbr_peers = 0; + if (l == NULL) { + print_error("The neighbour_list is empty !"); + } else { + print_debug(">> Printing out peer list :"); + while(l != NULL){ + neighbour_peer * peer = (neighbour_peer *) l->data; + char * buff_str_ip[1024]; + char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); + int last_seen = time(NULL) - peer->last_seen; + printf("\x1b[31m[DEBUG]\x1b[0m >> %s @ %i | is temporary ? %s | last seen %i secs ago. |\n", ip_str, peer->port, peer->is_temporary ? "yes":"no", last_seen); + nbr_peers++; + if (l->next == NULL) { + break; + } else { + l = l->next; + } + } + printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i peers.\n", nbr_peers); + print_debug(">> Finished printing peer list."); } - printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i peers.\n", nbr_peers); - print_debug(">> Finished printing peer list."); + } } void print_data(list * l){ + if (DEBUG_LEVEL > 0) { + + if (l == NULL) { + print_error("Message list is empty !"); + } else { + print_debug(">> Printing messages we know of so far :"); + int nbr_msg = 0; + setlocale(LC_ALL, "en_US.utf8"); + print_debug(">> Peer ID | Seqno | Length | Message "); + while(l != NULL){ + pub_data * message = l->data; + printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); + nbr_msg++; + if (l->next == NULL) { + break; + } else { + l = l->next; + } + } + printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i messages.\n", nbr_msg); + } + } +} + +void print_data_info(list * l){ if (l == NULL) { print_error("Message list is empty !"); } else { - print_debug(">> Printing messages we know of so far :"); int nbr_msg = 0; setlocale(LC_ALL, "en_US.utf8"); - print_debug(">> Peer ID | Seqno | Length | Message "); + print_info(">> Peer ID | Seqno | Length | Message "); while(l != NULL){ pub_data * message = l->data; - printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); + printf("\x1b[1m\x1b[36m[大字报]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); nbr_msg++; if (l->next == NULL) { break; @@ -66,7 +105,6 @@ void print_data(list * l){ l = l->next; } } - printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i messages.\n", nbr_msg); + printf("\x1b[1m\x1b[36m[大字报]\x1b[0m >> Found %i messages.\n", nbr_msg); } - } diff --git a/src/debug.h b/src/debug.h index c0a5688..9de638f 100644 --- a/src/debug.h +++ b/src/debug.h @@ -4,7 +4,11 @@ #include "node.h" -#define DEBUG_LEVEL 2 +#define DEBUG_LEVEL 0 + +void welcome(); + +void print_info(char * msg); void print_debug(char * msg); @@ -13,4 +17,7 @@ void print_error(char * msg); void print_peers(list * l); void print_data(list * l); + +void print_data_info(list * l); + #endif diff --git a/src/node.c b/src/node.c index ccfc05f..f2e739c 100644 --- a/src/node.c +++ b/src/node.c @@ -234,13 +234,13 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat // If seqno is bigger or equals than our stored seqno then update seqno if( ((seqno - found->seqno) & 32768) == 0 ) { - printf(">> Updating seqno of our own published data.\n"); + print_debug(">> Updating seqno of our own published data.\n"); found->seqno = (seqno + 1) % (65535); return 1; } // Else, do nothing - printf(">> Our own seqno didn't need to be updated.\n"); + print_debug(">> Our own seqno didn't need to be updated."); return 0; } @@ -248,7 +248,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat if(found != NULL) { // Check if seqno is smaller or equals to our stored seqno if( ((found->seqno - seqno) & 32768) == 0 ) { - printf(">> Data received has smaller seqno than stored seqno, nothing has to be done.\n"); + print_debug(">> Data received has smaller seqno than stored seqno, nothing has to be done."); return 0; } @@ -1084,7 +1084,9 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * build_node_state_req(&new_tlv, be64toh(*id)); add_tlv(&pack, &new_tlv, sender, socket_num); - printf(">> Sending node state request for node %lu...\n", be64toh(*id)); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Sending node state request for node %lu...\n", be64toh(*id)); + } // The position is updated tlv_len = data[pos+1]; @@ -1221,6 +1223,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * int rc = add_data(tlv_len - 26, be64toh(*id), be16toh(*seqno), cur_message, pdata); if (rc < 0) { print_error("Error while adding node state !"); + } else { + print_info("Received a message."); } // The position is updated pos += tlv_len + 2; @@ -1431,14 +1435,23 @@ int run_node(int sock_fd){ } // Remove trailing \n input_buffer[strcspn(input_buffer, "\n")] = 0; - if (DEBUG_LEVEL > 0) { + + + if (strcmp("",input_buffer) == 0){ + print_info(">> Printing all messages we know of so far :"); + print_data_info(data_list); + } else { + if (DEBUG_LEVEL > 0) { printf("\x1b[31m[DEBUG]\x1b[0m >> Adding following message to the table : “%s”\n", input_buffer ); - } - // Add message to the message table. - if (add_message(input_buffer, bytes-1) < 0) { - print_debug(">> Error while trying to add the message to the list of messages, please try again.."); - // Reset buffer - memset(input_buffer, 0, sizeof(input_buffer)); + } + // Add message to the message table. + if (add_message(input_buffer, bytes-1) < 0) { + print_debug(">> Error while trying to add the message to the list of messages, please try again.."); + // Reset buffer + memset(input_buffer, 0, sizeof(input_buffer)); + } else { + print_info(">> Added message to the message list."); + } } } @@ -1577,6 +1590,7 @@ int main(int argc, const char *argv[]) { int sock_fd; bootstrap_node(&sock_fd, root_peer_ip, root_peer_port); + welcome(); run_node(sock_fd); close(sock_fd); diff --git a/src/node.h b/src/node.h index 6fba513..af8b526 100644 --- a/src/node.h +++ b/src/node.h @@ -61,7 +61,7 @@ typedef struct list { #define LISTEN_PORT 1212 // The node ID -#define NODE_ID 42009235719890846928 +#define NODE_ID 4209169790617845760 // #define NODE_ID 42675882021843277 // #define NODE_ID 1312 From b85312bd9759004d716039a28c72ab40f7868d7b Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 4 May 2020 16:09:55 +0200 Subject: [PATCH 54/59] fixed node id --- src/debug.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/debug.c b/src/debug.c index e205e30..c596646 100644 --- a/src/debug.c +++ b/src/debug.c @@ -75,7 +75,7 @@ void print_data(list * l){ print_debug(">> Peer ID | Seqno | Length | Message "); while(l != NULL){ pub_data * message = l->data; - printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); + printf("\x1b[31m[DEBUG]\x1b[0m >> %lu | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); nbr_msg++; if (l->next == NULL) { break; @@ -97,7 +97,7 @@ void print_data_info(list * l){ print_info(">> Peer ID | Seqno | Length | Message "); while(l != NULL){ pub_data * message = l->data; - printf("\x1b[1m\x1b[36m[大字报]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); + printf("\x1b[1m\x1b[36m[大字报]\x1b[0m >> %lu | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); nbr_msg++; if (l->next == NULL) { break; From 57d04eb1c3a67d5ba3adcea28c5188c95615376a Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 4 May 2020 20:46:12 +0200 Subject: [PATCH 55/59] Added ability to add via hostname --- src/debug.c | 4 +++ src/debug.h | 2 +- src/node.c | 70 ++++++++++++++++++++++++++++++++++++++++------------- src/node.h | 1 + 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/debug.c b/src/debug.c index c596646..a23736d 100644 --- a/src/debug.c +++ b/src/debug.c @@ -44,6 +44,10 @@ void print_peers(list * l){ print_debug(">> Printing out peer list :"); while(l != NULL){ neighbour_peer * peer = (neighbour_peer *) l->data; + if (peer == NULL) { + print_error("Peer is empty ?"); + return; + } char * buff_str_ip[1024]; char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); int last_seen = time(NULL) - peer->last_seen; diff --git a/src/debug.h b/src/debug.h index 9de638f..99f6811 100644 --- a/src/debug.h +++ b/src/debug.h @@ -4,7 +4,7 @@ #include "node.h" -#define DEBUG_LEVEL 0 +#define DEBUG_LEVEL 7 void welcome(); diff --git a/src/node.c b/src/node.c index f2e739c..acca9e8 100644 --- a/src/node.c +++ b/src/node.c @@ -1517,14 +1517,13 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ * sock_fd = socket(AF_INET6, SOCK_DGRAM, 0); if ( * sock_fd < 0) { print_error("Failed to open socket"); - perror(""); exit(-1); } /* Bind socket */ memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin6_family = AF_INET6; - // server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY); + // server_addr.sin6_addr = INADDR_ANY; server_addr.sin6_port = LISTEN_PORT; if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) { print_error("Failed to bind socket"); @@ -1532,26 +1531,62 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ exit(-1); } - /* Make the first peer*/ - struct neighbour_peer * root_peer = (struct neighbour_peer *) malloc(sizeof(struct neighbour_peer)); - time_t root_peer_seen = time(NULL); + print_debug(">> Adding the first root peers to the list..."); - int inet_p = inet_pton(AF_INET6, root_peer_ip, &root_peer->ip); - if(inet_p < 1){ - perror(">> Failed to create the root peer."); - return -3; - } + // Get the address via getaddrinfo. + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_DGRAM; + hints.ai_protocol = 0; + hints.ai_flags = (AI_V4MAPPED | AI_ALL); // get back ipv4 mapped to ipv6 - root_peer->port = root_peer_port; - root_peer->is_temporary = 0; - root_peer->last_seen = root_peer_seen; + struct addrinfo *res; + print_debug(">> Resolving root peer name via getaddrinfo"); + int rc = getaddrinfo(root_peer_ip, "http", &hints, &res); + if(rc != 0) { + print_error("Failed to resolve hostname, exiting..."); + exit(1); + } - // TODO: Add the first peer to the neighbourhood - print_debug(">> Adding the first root peer to the list..."); neighbour_list = malloc(sizeof(struct list)); memset(neighbour_list, 0, sizeof(struct list)); - neighbour_list->data = (void *) root_peer; - neighbour_list->next = NULL; + + // For every address given to us by getaddrinfo, we create a new peer. + struct addrinfo *p; + struct neighbour_peer *peer ; + + for(p = res; p != NULL; p = p->ai_next) { + peer = (neighbour_peer*) malloc(sizeof(neighbour_peer)); + memcpy(&peer->ip, &((struct sockaddr_in6 *) p->ai_addr)->sin6_addr, sizeof(struct in6_addr)); + peer->port = root_peer_port; + peer->is_temporary = 0; + + // set last_seen time + peer->last_seen = time(NULL); + + if (DEBUG_LEVEL > 0) { + char * buff_str_ip[1024]; + char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); + printf("\x1b[31m[DEBUG]\x1b[0m >> Adding %s @ %i to peer list.\n", ip_str, root_peer_port ); + } + + list * tmp = neighbour_list; + + // Add new peer to the list. + list *node = (list*) malloc(sizeof(list)); + node->data = (void*) peer; + node->next = NULL; + + // Goint to the end of the list + while(tmp->data != NULL){ + tmp = tmp->next; + } + tmp->data = node; + tmp->next = NULL; + + } + print_debug(">> Initializing data list..."); data_list = (list*) malloc(sizeof(list)); @@ -1565,6 +1600,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ our_data->seqno = 1337; our_data->data = NULL; + freeaddrinfo(res); print_debug(">> Boostraping done."); return 0; } diff --git a/src/node.h b/src/node.h index af8b526..3dfe7db 100644 --- a/src/node.h +++ b/src/node.h @@ -19,6 +19,7 @@ #include #include #include +#include /* la table de voisins, qui est indexée par adresses de socket (des paires (IP, Port)), * et dont chaque entrée contient un booléen indiquant si le pair est permanent From 44cf00432b5ae24c5d325b8b536e173d9326d416 Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 4 May 2020 21:05:35 +0200 Subject: [PATCH 56/59] Fixed adding to list of peers. --- src/node.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/node.c b/src/node.c index acca9e8..54813e1 100644 --- a/src/node.c +++ b/src/node.c @@ -1556,6 +1556,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ struct addrinfo *p; struct neighbour_peer *peer ; + list * tmp = neighbour_list; for(p = res; p != NULL; p = p->ai_next) { peer = (neighbour_peer*) malloc(sizeof(neighbour_peer)); memcpy(&peer->ip, &((struct sockaddr_in6 *) p->ai_addr)->sin6_addr, sizeof(struct in6_addr)); @@ -1571,7 +1572,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ printf("\x1b[31m[DEBUG]\x1b[0m >> Adding %s @ %i to peer list.\n", ip_str, root_peer_port ); } - list * tmp = neighbour_list; + // Add new peer to the list. list *node = (list*) malloc(sizeof(list)); @@ -1579,14 +1580,16 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ node->next = NULL; // Goint to the end of the list - while(tmp->data != NULL){ + while(tmp->next != NULL){ tmp = tmp->next; } - tmp->data = node; - tmp->next = NULL; - + tmp->next = node; } + neighbour_list = tmp; + + + print_peers(neighbour_list); print_debug(">> Initializing data list..."); data_list = (list*) malloc(sizeof(list)); From dcdcbd6435b7a9b2060fce50aae4662b27dbad1e Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 5 May 2020 14:55:42 +0200 Subject: [PATCH 57/59] Removed a line --- src/node.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/node.c b/src/node.c index 54813e1..cb72406 100644 --- a/src/node.c +++ b/src/node.c @@ -1588,7 +1588,6 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ neighbour_list = tmp; - print_peers(neighbour_list); print_debug(">> Initializing data list..."); From bcd0ef4a3eebb1c3c57365293493ad8d9a945bd9 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 5 May 2020 15:17:54 +0200 Subject: [PATCH 58/59] Add gitignore --- .gitignore | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6127b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,52 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf From 77ec404de6423d38cb045eb4993b1b238e0c9ab9 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 5 May 2020 15:18:33 +0200 Subject: [PATCH 59/59] Added ignore to binary --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index c6127b3..4a116ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# binary +dazibao + # Prerequisites *.d