From: Katerina Kubecova Date: Thu, 11 Jan 2024 07:57:50 +0000 (+0100) Subject: show protocols all in cbor works, added header X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f8bc20a211247a7ae0427c0f1e702b328f36962b;p=thirdparty%2Fbird.git show protocols all in cbor works, added header --- diff --git a/client/client.c b/client/client.c index f0acb3d80..02774bce0 100644 --- a/client/client.c +++ b/client/client.c @@ -181,6 +181,8 @@ void write_args_cbor(char *cmd_buffer, struct cbor_writer *w) cbor_close_block_or_list(w); } +int serial_num = 0; + void make_cmd_cbor(char *cmd_buffer) { @@ -189,6 +191,13 @@ make_cmd_cbor(char *cmd_buffer) struct linpool *lp = lp_new(&root_pool); struct cbor_writer *w = cbor_init(cbor_buf, l*10, lp); + + write_item(w, 6, 24); // tag 24 - cbor binary + int length_pt = w->pt + 1; + cbor_write_item_with_constant_val_length_4(w, 2, 0); + cbor_open_list_with_length(w, 2); + cbor_write_item_with_constant_val_length_4(w, 0, serial_num); + cbor_open_block_with_length(w, 1); cbor_add_string(w, "command:do"); @@ -203,6 +212,7 @@ make_cmd_cbor(char *cmd_buffer) { cbor_string_int(w, "command", SHOW_MEMORY); cbor_close_block_or_list(w); + rewrite_4bytes_int(w, length_pt, w->pt); server_send_byte(cbor_buf, w->pt); lp_flush(lp); return; @@ -211,7 +221,7 @@ make_cmd_cbor(char *cmd_buffer) { cbor_string_int(w, "command", SHOW_STATUS); cbor_close_block_or_list(w); - cbor_write_to_file(w, "status_command.cbor"); + rewrite_4bytes_int(w, length_pt, w->pt); server_send_byte(cbor_buf, w->pt); lp_flush(lp); return; @@ -221,6 +231,7 @@ make_cmd_cbor(char *cmd_buffer) cbor_string_int(w, "command", SHOW_SYMBOLS); write_args_cbor(&cmd_buffer[buf_pt + strlen("symbols ")], w); cbor_close_block_or_list(w); + rewrite_4bytes_int(w, length_pt, w->pt); server_send_byte(cbor_buf, w->pt); lp_flush(lp); return; @@ -230,6 +241,7 @@ make_cmd_cbor(char *cmd_buffer) cbor_string_int(w, "command", SHOW_OSPF); write_args_cbor(&cmd_buffer[buf_pt + strlen("ospf")], w); cbor_close_block_or_list(w); + rewrite_4bytes_int(w, length_pt, w->pt); server_send_byte(cbor_buf, w->pt); lp_flush(lp); return; @@ -240,6 +252,7 @@ make_cmd_cbor(char *cmd_buffer) cbor_string_int(w, "command", SHOW_PROTOCOLS); write_args_cbor(&cmd_buffer[buf_pt + strlen("protocols")], w); cbor_close_block_or_list(w); + rewrite_4bytes_int(w, length_pt, w->pt); server_send_byte(cbor_buf, w->pt); lp_flush(lp); return; @@ -253,6 +266,7 @@ make_cmd_cbor(char *cmd_buffer) else if (compare_string(cmd_buffer, l, "down")) { cbor_add_string(w, "down"); + rewrite_4bytes_int(w, length_pt, w->pt); server_send_byte(cbor_buf, w->pt); die("Shutdown from client"); return; @@ -440,7 +454,6 @@ server_got_reply(char *x) void server_got_binary(int c) { - // TODO check cbor hello if (cbor_mode == 0) { byte expected[] = {0x87, 0x42, 0x49, 0x52, 0x44, 0x0D, 0x0A, 0x1A, 0x0A, 0x01}; @@ -456,7 +469,35 @@ server_got_binary(int c) cbor_mode = 1; } else { - print_cbor_response(server_read_buf, c); + int length = 0; + for (int i = 0; i < 4; i++) + { + length = length << 8; + length += server_read_buf[3 + i]; + } + printf("length of message is %i\n", length); + if (length > c - 7) + { + byte bigger_buf[length]; + memcpy(bigger_buf, server_read_buf, c); + int cc = read(server_fd, &bigger_buf[c], length - c + 7); + if (!cc) + die("Connection closed by server"); + if (cc < 0) + { + DIE("Server read error"); + } + print_cbor_response(&bigger_buf[13], length); + } + else + { + printf("no need to load more\n"); + for (int i = 0; i < 15; i++) + { + printf("%i %x\n",i, server_read_buf[i] ); + } + print_cbor_response(&server_read_buf[13], c); + } } busy = 0; skip_input = 0; diff --git a/client/print_cbor.c b/client/print_cbor.c index 818c0f9b0..4e526f742 100644 --- a/client/print_cbor.c +++ b/client/print_cbor.c @@ -116,7 +116,7 @@ void discard_key(struct buff_reader *buf_read) struct value val = get_value(buf_read); if(!(val.major == TEXT)) { - bug("key is not text but %i", val.major); + bug("key is not text but %i, pt is %i", val.major, buf_read->pt); } buf_read->pt+=val.val; } @@ -678,6 +678,66 @@ void print_show_symbols(struct buff_reader *buf_read) } } + +void print_route_change_line(struct buff_reader *buf_read) +{ + for (int i = 0; i < 5; i++) + { + struct value val = get_value(buf_read); + if (val.major == UINT) + printf(" %10lu", val.val); + else + printf(" ---"); + } + printf("\n"); +} + +void print_channel_show_stats(struct buff_reader *buf_read) +{ + struct value val = get_value(buf_read); //open block + discard_key(buf_read); + val = get_value(buf_read); + printf(" Routes: %lu imported, ", val.val); + val = get_value(buf_read); + if (compare_buff_str(buf_read, val.val, "serial_num")) + { + buf_read->pt += val.val; + val = get_value(buf_read); + printf("%lu filtered, ", val.val); + val = get_value(buf_read); + } + buf_read->pt += val.val; + val = get_value(buf_read); + printf("%lu exported, ", val.val); + discard_key(buf_read); + val = get_value(buf_read); + printf("%lu preferred\n", val.val); + + printf(" Route change stats: received rejected filtered ignored accepted\n"); + discard_key(buf_read); //import_updates + val = get_value(buf_read); //open list + printf(" Import updates: "); + print_route_change_line(buf_read); + + discard_key(buf_read); //import_updates + val = get_value(buf_read); //open list + printf(" Import withdraws: "); + print_route_change_line(buf_read); + + discard_key(buf_read); //import_updates + val = get_value(buf_read); //open list + printf(" Export updates: "); + print_route_change_line(buf_read); + + discard_key(buf_read); //import_updates + val = get_value(buf_read); //open list + printf(" Export withdraws: "); + print_route_change_line(buf_read); + + val = get_value(buf_read); //close block +} + + void print_channel_show_limit(struct buff_reader *buf_read) { struct value val = get_value(buf_read); @@ -703,19 +763,57 @@ void print_channel_show_limit(struct buff_reader *buf_read) val = get_value(buf_read); } -void print_route_change_line(struct buff_reader *buf_read) +void print_channel_show_info(struct buff_reader *buf_read) { - for (int i = 0; i < 5; i++) +// This function expect removed key channels and removed block opening. The reason is bgp channel list. + print_string_string(buf_read, " Channel "); + + print_string_string(buf_read, " State: "); + + print_string_string(buf_read, " Table: "); + + discard_key(buf_read); + struct value val = get_value(buf_read); + printf(" Preference: %ld\n", val.val); + + print_string_string(buf_read, " Input filter: "); + + print_string_string(buf_read, " Output filter: "); + + int pt = buf_read->pt; + val = get_value(buf_read); + if (compare_buff_str(buf_read, val.val, "gr_pending")) { - struct value val = get_value(buf_read); - if (val.major == UINT) - printf(" %10lu", val.val); - else - printf(" ---"); + buf_read->pt += val.val; + printf(" GR recovery: "); + val = get_value(buf_read); + if (val.val) + printf(" pending"); + discard_key(buf_read); + val = get_value(buf_read); + if (val.val) + printf(" waiting"); + printf("\n"); + } + else + { + buf_read->pt = pt; // this is not nice, but we need the name of the block. + //If the name of the block is allways same, we would need to create lists for limits. + } + print_channel_show_limit(buf_read); + print_channel_show_limit(buf_read); + print_channel_show_limit(buf_read); + + val = get_value(buf_read); + if (!val_is_break(val)) + { + buf_read->pt += val.val; + print_channel_show_stats(buf_read); + val = get_value(buf_read); } - printf("\n"); } + void print_pipe_show_stats(struct buff_reader *buf_read) { struct value val = get_value(buf_read); //open block @@ -745,6 +843,32 @@ void print_pipe_show_stats(struct buff_reader *buf_read) val = get_value(buf_read); //close block } +void print_rpki_show_proto_info_timer(struct buff_reader *buf_read) +{ + struct value val = get_value(buf_read); // name + printf(" "); + print_with_size_add_space(&buf_read->buff[buf_read->pt], val.val, 16); + buf_read->pt += val.val; + val = get_value(buf_read); //open block + val = get_value(buf_read); + if (!val_is_break(val)) + { + buf_read->pt += val.val; + val = get_value(buf_read); + printf(": "); + print_as_time(val.val); + printf("/"); + discard_key(buf_read); + val = get_value(buf_read); + printf("%lu\n", val.val); + val = get_value(buf_read); //close block + } + else + { + printf(": ---\n"); + } +} + void print_show_protocols_rpki(struct buff_reader *buf_read) { struct value val = get_value(buf_read); //open block @@ -754,6 +878,7 @@ void print_show_protocols_rpki(struct buff_reader *buf_read) print_with_size_(&buf_read->buff[buf_read->pt], val.val); printf("\n"); buf_read->pt += val.val; + val = get_value(buf_read); if (compare_buff_str(buf_read, val.val, "cache_port")) { buf_read->pt += val.val; @@ -762,7 +887,12 @@ void print_show_protocols_rpki(struct buff_reader *buf_read) val = get_value(buf_read); } - print_string_string(buf_read, " Status: "); + buf_read->pt += val.val; + val = get_value(buf_read); + printf(" Status: "); + print_with_size_(&buf_read->buff[buf_read->pt], val.val); + printf("\n"); + buf_read->pt += val.val; print_string_string(buf_read, " Transport: "); discard_key(buf_read); @@ -780,6 +910,7 @@ void print_show_protocols_rpki(struct buff_reader *buf_read) else printf("%lu\n", val.val); + int pt = buf_read->pt; val = get_value(buf_read); if (compare_buff_str(buf_read, val.val, "serial_num")) { @@ -788,110 +919,52 @@ void print_show_protocols_rpki(struct buff_reader *buf_read) printf(" Serial number: %lu\n", val.val); discard_key(buf_read); val = get_value(buf_read); - printf(" Last update: before %lu s\n", val.val); + printf(" Last update: before "); + print_as_time(val.val); + printf(" s\n"); } else { printf(" Serial number: ---\n"); printf(" Last update: ---\n"); + buf_read->pt = pt; } - val = get_value(buf_read); -} + print_rpki_show_proto_info_timer(buf_read); + print_rpki_show_proto_info_timer(buf_read); + print_rpki_show_proto_info_timer(buf_read); -void print_channel_show_stats(struct buff_reader *buf_read) -{ - struct value val = get_value(buf_read); //open block - discard_key(buf_read); - val = get_value(buf_read); - printf(" Routes: %lu imported, ", val.val); val = get_value(buf_read); - if (compare_buff_str(buf_read, val.val, "serial_num")) + + if (compare_buff_str(buf_read, val.val, "no_roa4")) + { + printf(" No roa4 channel\n"); + buf_read->pt += val.val; + } + else { buf_read->pt += val.val; val = get_value(buf_read); - printf("%lu filtered, ", val.val); - val = get_value(buf_read); + print_channel_show_info(buf_read); } - buf_read->pt += val.val; - val = get_value(buf_read); - printf("%lu exported, ", val.val); - discard_key(buf_read); val = get_value(buf_read); - printf("%lu preferred\n", val.val); - - printf(" Route change stats: received rejected filtered ignored accepted\n"); - discard_key(buf_read); //import_updates - val = get_value(buf_read); //open list - printf(" Import updates: "); - print_route_change_line(buf_read); - - discard_key(buf_read); //import_updates - val = get_value(buf_read); //open list - printf(" Import withdraws: "); - print_route_change_line(buf_read); - - discard_key(buf_read); //import_updates - val = get_value(buf_read); //open list - printf(" Export updates: "); - print_route_change_line(buf_read); - - discard_key(buf_read); //import_updates - val = get_value(buf_read); //open list - printf(" Export withdraws: "); - print_route_change_line(buf_read); - - val = get_value(buf_read); //close block -} - -void print_channel_show_info(struct buff_reader *buf_read) -{ - print_string_string(buf_read, " Channel "); - - print_string_string(buf_read, " State: "); - - print_string_string(buf_read, " Table: "); - - discard_key(buf_read); - struct value val = get_value(buf_read); - printf(" Preference: %ld\n", val.val); - print_string_string(buf_read, " Input filter: "); - - print_string_string(buf_read, " Output filter: "); - - int pt = buf_read->pt; - val = get_value(buf_read); - if (compare_buff_str(buf_read, val.val, "gr_pending")) + if (compare_buff_str(buf_read, val.val, "no_roa6")) { + printf(" No roa6 channel\n"); buf_read->pt += val.val; - printf(" GR recovery: "); - val = get_value(buf_read); - if (val.val) - printf(" pending"); - discard_key(buf_read); - val = get_value(buf_read); - if (val.val) - printf(" waiting"); - printf("\n"); } else - { - buf_read->pt = pt; // this is not nice, but we need the name of the block. - //If the name of the block is allways same, we would need to create lists for limits. - } - print_channel_show_limit(buf_read); - print_channel_show_limit(buf_read); - print_channel_show_limit(buf_read); - - val = get_value(buf_read); - if (!val_is_break(val)) { buf_read->pt += val.val; - print_channel_show_stats(buf_read); val = get_value(buf_read); + print_channel_show_info(buf_read); } + + val = get_value(buf_read); + val = get_value(buf_read); } + void print_bgp_show_afis(struct buff_reader *buf_read) { struct value val = get_value(buf_read); //open list @@ -926,10 +999,8 @@ void print_bgp_show_afis(struct buff_reader *buf_read) void print_bgp_capabilities(struct buff_reader *buf_read) { - printf(", %x %x>\n", &buf_read->buff[buf_read->pt], buf_read->buff[buf_read->pt], buf_read->buff[buf_read->pt+1]); discard_key(buf_read); struct value val = get_value(buf_read); //open block - printf("\n", val.major, val.val); val = get_value(buf_read); if (compare_buff_str(buf_read, val.val, "AF_announced")) { @@ -1040,6 +1111,7 @@ void print_bgp_capabilities(struct buff_reader *buf_read) val = get_value(buf_read); printf(" Hostname: "); print_with_size(&buf_read->buff[buf_read->pt], val.val); + buf_read->pt += val.val; printf("\n"); val = get_value(buf_read); } @@ -1049,6 +1121,7 @@ void print_bgp_capabilities(struct buff_reader *buf_read) val = get_value(buf_read); printf(" Role: "); print_with_size(&buf_read->buff[buf_read->pt], val.val); + buf_read->pt += val.val; printf("\n"); val = get_value(buf_read); } @@ -1092,32 +1165,16 @@ void print_show_protocols_bgp(struct buff_reader *buf_read) } else { - printf("buff[buf_read->pt], val.val); - printf("debug >"); buf_read->pt += val.val; } val = get_value(buf_read); - printf("\n", val.major, val.val); - for (int i = 0; i< 30; i++) - { - printf("\n", buf_read->buff[buf_read->pt+i]); - } printf(" Neighbor AS: %lu\n", val.val); - //discard_key(buf_read); - printf("\n ", val.major, val.val); - print_with_size(&buf_read->buff[buf_read->pt], val.val); - buf_read->pt += val.val; - printf(" debug>\n"); + discard_key(buf_read); val = get_value(buf_read); - printf("\n", val.major, val.val); printf(" Local AS: %lu\n", val.val); val = get_value(buf_read); - printf("\n", val.major, val.val); if (compare_buff_str(buf_read, val.val, "gr_active")) { printf(" Neighbor graceful restart active\n"); @@ -1129,7 +1186,9 @@ void print_show_protocols_bgp(struct buff_reader *buf_read) { buf_read->pt += val.val; val = get_value(buf_read); - printf(" Error wait: %lu/", val.val); + printf(" Error wait: "); + print_as_time(val.val); + printf("/"); discard_key(buf_read); val = get_value(buf_read); printf("%lu\n", val.val); @@ -1139,7 +1198,9 @@ void print_show_protocols_bgp(struct buff_reader *buf_read) { buf_read->pt += val.val; val = get_value(buf_read); - printf(" Connect delay: %lu/", val.val); + printf(" Connect delay: "); + print_as_time(val.val); + printf("/"); discard_key(buf_read); val = get_value(buf_read); printf("%lu\n", val.val); @@ -1149,18 +1210,15 @@ void print_show_protocols_bgp(struct buff_reader *buf_read) { buf_read->pt += val.val; val = get_value(buf_read); - printf(" Connect delay: %lu/-\n", val.val); + printf(" Connect delay: "); + print_as_time(val.val); + printf("/-\n"); val = get_value(buf_read); } if (compare_buff_str(buf_read, val.val, "neighbor_id")) { buf_read->pt += val.val; printf(" Neighbor ID: "); - printf(", %x %x>\n", &buf_read->buff[buf_read->pt], buf_read->buff[buf_read->pt], buf_read->buff[buf_read->pt+1]); - for (int i = 0; i< 30; i++) - { - printf("\n", buf_read->buff[buf_read->pt+i]); - } print_ip_addr(buf_read); printf("\n"); printf(" Local capabilities\n"); @@ -1201,35 +1259,43 @@ void print_show_protocols_bgp(struct buff_reader *buf_read) discard_key(buf_read); val = get_value(buf_read); printf("%lu\n", val.val); + val = get_value(buf_read); } if (compare_buff_str(buf_read, val.val, "last_err1")) { buf_read->pt += val.val; printf(" Last error: "); - discard_key(buf_read); + val = get_value(buf_read); print_with_size(&buf_read->buff[buf_read->pt], val.val); + buf_read->pt += val.val; printf(" "); discard_key(buf_read); + val = get_value(buf_read); print_with_size(&buf_read->buff[buf_read->pt], val.val); + buf_read->pt += val.val; printf("\n"); val = get_value(buf_read); } - discard_key(buf_read); //channels + buf_read->pt += val.val; //channels val = get_value(buf_read); //open list val = get_value(buf_read); //open block + val = get_value(buf_read); //channel while (!val_is_break(val)) { - discard_key(buf_read); //channel + buf_read->pt += val.val; val = get_value(buf_read); //open block + ASSERT(val.major == BLOCK); print_channel_show_info(buf_read); val = get_value(buf_read); + if (compare_buff_str(buf_read, val.val, "neighbor_gr")) { buf_read->pt += val.val; printf(" Neighbor GR: "); print_with_size(&buf_read->buff[buf_read->pt], val.val); + buf_read->pt += val.val; printf("\n"); val = get_value(buf_read); } @@ -1267,14 +1333,17 @@ void print_show_protocols_bgp(struct buff_reader *buf_read) val = get_value(buf_read); printf(" IGP IPv4 table: "); print_with_size(&buf_read->buff[buf_read->pt], val.val); + buf_read->pt += val.val; printf("\n"); val = get_value(buf_read); } - if (compare_buff_str(buf_read, val.val, "igp_ipv4_table")) + if (compare_buff_str(buf_read, val.val, "igp_ipv6_table")) { buf_read->pt += val.val; printf(" IGP IPv6 table: "); + val = get_value(buf_read); print_with_size(&buf_read->buff[buf_read->pt], val.val); + buf_read->pt += val.val; printf("\n"); val = get_value(buf_read); } @@ -1282,14 +1351,15 @@ void print_show_protocols_bgp(struct buff_reader *buf_read) { buf_read->pt += val.val; printf(" Base table: "); + val = get_value(buf_read); print_with_size(&buf_read->buff[buf_read->pt], val.val); + buf_read->pt += val.val; printf("\n"); val = get_value(buf_read); } val = get_value(buf_read); } val = get_value(buf_read); - printf("\n", val.major, val.val); } void print_show_protocols(struct buff_reader *buf_read) @@ -1398,12 +1468,14 @@ void print_show_protocols(struct buff_reader *buf_read) print_channel_show_limit(buf_read); print_channel_show_limit(buf_read); val = get_value(buf_read); + if (!val_is_break(val)) { buf_read->pt += val.val; // discarding key "stats" print_pipe_show_stats(buf_read); val = get_value(buf_read); } + val = get_value(buf_read); printf("\n"); } else if (compare_buff_str(buf_read, val.val, "bgp")) @@ -1411,7 +1483,6 @@ void print_show_protocols(struct buff_reader *buf_read) buf_read->pt += val.val; print_show_protocols_bgp(buf_read); val = get_value(buf_read); - printf("\n", val.major, val.val); printf("\n"); } else @@ -1432,7 +1503,6 @@ void print_show_protocols(struct buff_reader *buf_read) } } val = get_value(buf_read); - printf("\n", val.major, val.val); } } diff --git a/nest/cbor.c b/nest/cbor.c index ef2b7aa8c..76d0ff86e 100644 --- a/nest/cbor.c +++ b/nest/cbor.c @@ -12,7 +12,7 @@ struct cbor_writer *cbor_init(uint8_t *buff, uint32_t capacity, struct linpool * struct cbor_writer *writer = (struct cbor_writer*)lp_alloc(lp, sizeof(struct cbor_writer)); writer->cbor = buff; writer->capacity = capacity; - writer->pt =0; + writer->pt = 0; writer->lp = lp; return writer; } @@ -196,6 +196,33 @@ void write_item(struct cbor_writer *writer, uint8_t major, uint64_t num) writer->pt++; } +void cbor_write_item_with_constant_val_length_4(struct cbor_writer *writer, uint8_t major, uint64_t num) +{ +// this is only for headers which should be constantly long. + major = major<<5; + check_memory(writer, 10); + major += 0x1a; // reserving those bytes + writer->cbor[writer->pt] = major; + writer->pt++; + for (int i = 3; i>=0; i--) + { // write n-th byte of num + uint8_t to_write = (num>>(i*8)) & 0xff; + writer->cbor[writer->pt] = to_write; + writer->pt++; + } +} + + +void rewrite_4bytes_int(struct cbor_writer *writer, int pt, int num) +{ + for (int i = 3; i>=0; i--) + { + uint8_t to_write = (num>>(i*8)) & 0xff; + writer->cbor[pt] = to_write; + pt++; + } +} + void check_memory(struct cbor_writer *writer, int add_size) { if (writer->capacity - writer->pt-add_size < 0) diff --git a/nest/cbor.h b/nest/cbor.h index 02988c4cf..97320aab8 100644 --- a/nest/cbor.h +++ b/nest/cbor.h @@ -45,4 +45,10 @@ void cbor_add_string(struct cbor_writer *writer, const char *string); void cbor_nonterminated_string(struct cbor_writer *writer, const char *string, uint32_t length); +void write_item(struct cbor_writer *writer, uint8_t major, uint64_t num); + +void cbor_write_item_with_constant_val_length_4(struct cbor_writer *writer, uint8_t major, uint64_t num); + +void rewrite_4bytes_int(struct cbor_writer *writer, int pt, int num); + #endif diff --git a/nest/cbor_parse.c b/nest/cbor_parse.c index 61d250585..1d6db48aa 100644 --- a/nest/cbor_parse.c +++ b/nest/cbor_parse.c @@ -127,28 +127,52 @@ do_command(struct buff_reader *rbuf_read, struct buff_reader *tbuf_read, int ite { case SHOW_MEMORY: skip_optional_args(rbuf_read, items_in_block); - return cmd_show_memory_cbor(tbuf_read->buff, tbuf_read->size, lp); + return cmd_show_memory_cbor(&tbuf_read->buff[tbuf_read->pt], tbuf_read->size, lp); case SHOW_STATUS: log("show status"); skip_optional_args(rbuf_read, items_in_block); - return cmd_show_status_cbor(tbuf_read->buff, tbuf_read->size, lp); + return cmd_show_status_cbor(&tbuf_read->buff[tbuf_read->pt], tbuf_read->size, lp); case SHOW_SYMBOLS: args = parse_arguments(rbuf_read, items_in_block, lp); - return cmd_show_symbols_cbor(tbuf_read->buff, tbuf_read->size, args, lp); + return cmd_show_symbols_cbor(&tbuf_read->buff[tbuf_read->pt], tbuf_read->size, args, lp); case SHOW_OSPF: args = parse_arguments(rbuf_read, items_in_block, lp); log("args %i, pt %i", args, args->pt); - return cmd_show_ospf_cbor(tbuf_read->buff, tbuf_read->size, args, lp); + return cmd_show_ospf_cbor(&tbuf_read->buff[tbuf_read->pt], tbuf_read->size, args, lp); case SHOW_PROTOCOLS: args = parse_arguments(rbuf_read, items_in_block, lp); log("args %i, pt %i", args, args->pt); - return cmd_show_protocols_cbor(tbuf_read->buff, tbuf_read->size, args, lp); + return cmd_show_protocols_cbor(&tbuf_read->buff[tbuf_read->pt], tbuf_read->size, args, lp); default: bug("command %li not found", val.val); return 0; } } +uint +add_header(struct buff_reader *tbuf_read, struct linpool *lp, int serial_num) +{ + struct cbor_writer *w = cbor_init(tbuf_read->buff, tbuf_read->size, lp); + write_item(w, 6, 24); // tag 24 - cbor binary + int length_pt = w->pt + 1; // place where we will put final size + cbor_write_item_with_constant_val_length_4(w, 2, 0); + cbor_open_list_with_length(w, 2); + cbor_write_item_with_constant_val_length_4(w, 0, serial_num); + tbuf_read->pt+=w->pt; + return length_pt; +} + +uint +read_head(struct buff_reader *rbuf_read) +{ + struct value val = get_value(rbuf_read); //tag + val = get_value(rbuf_read); //bytestring + val = get_value(rbuf_read); //list + val = get_value(rbuf_read); //serial_num + int ret = val.val; + return ret; +} + uint detect_down(uint size, byte *rbuf) { @@ -156,6 +180,7 @@ detect_down(uint size, byte *rbuf) rbuf_read.buff = rbuf; rbuf_read.size = size; rbuf_read.pt = 0; + read_head(&rbuf_read); struct value val = get_value(&rbuf_read); ASSERT(val.major == BLOCK); val = get_value(&rbuf_read); @@ -165,7 +190,6 @@ detect_down(uint size, byte *rbuf) return (val.major = TEXT && compare_buff_str(&rbuf_read, val.val, "down")); } - uint parse_cbor(uint size, byte *rbuf, byte *tbuf, uint tbsize, struct linpool* lp) { @@ -178,10 +202,19 @@ parse_cbor(uint size, byte *rbuf, byte *tbuf, uint tbsize, struct linpool* lp) rbuf_read.pt = 0; tbuf_read.pt = 0; - if (size == 0) + if (size <=7) { return 0; } + + int serial_num = read_head(&rbuf_read); + int length_pt = add_header(&tbuf_read, lp, serial_num); + for (int i = 0; i < 15; i++) + { + log("%i %x",i, tbuf[i] ); + } + tbuf_read.size = tbsize - tbuf_read.pt; + struct value val = get_value(&rbuf_read); ASSERT(val.major == BLOCK); ASSERT(val.val <=1); @@ -207,7 +240,7 @@ parse_cbor(uint size, byte *rbuf, byte *tbuf, uint tbsize, struct linpool* lp) ASSERT(compare_buff_str(&rbuf_read, val.val, "command")); rbuf_read.pt+=val.val; - tbuf_read.pt = do_command(&rbuf_read, &tbuf_read, items_in_block, lp); + tbuf_read.pt += do_command(&rbuf_read, &tbuf_read, items_in_block, lp); if (items_in_block == -1) { val = get_value(&rbuf_read); @@ -216,6 +249,12 @@ parse_cbor(uint size, byte *rbuf, byte *tbuf, uint tbsize, struct linpool* lp) } } } + struct cbor_writer *w = cbor_init(tbuf_read.buff, tbuf_read.size, lp); + rewrite_4bytes_int(w, length_pt, tbuf_read.pt - 7); // add final length to header + for (int i = 0; i < 15; i++) + { + log("%i %x",i, tbuf[i] ); + } lp_flush(lp); log("parsed"); return tbuf_read.pt; diff --git a/nest/cli.c b/nest/cli.c index c829d3f21..857dc8038 100644 --- a/nest/cli.c +++ b/nest/cli.c @@ -399,6 +399,7 @@ cli_kick(cli *c) uint yi_process(uint size, byte *rbuf, byte *tbuf, uint tbsize) { log("capacity %i buffer %i", tbsize, tbuf); + //TODO if (detect_down(size, rbuf)) { order_shutdown(0); diff --git a/proto/pipe/pipe.c b/proto/pipe/pipe.c index 4e5bc9dcb..5120d73fe 100644 --- a/proto/pipe/pipe.c +++ b/proto/pipe/pipe.c @@ -269,7 +269,7 @@ pipe_show_stats_cbor(struct cbor_writer *w, struct pipe_proto *p) cbor_add_int(w, s1->imp_updates_accepted); cbor_add_string(w, "import_withdraws"); - cbor_open_block_with_length(w, 5); + cbor_open_list_with_length(w, 5); cbor_add_int(w, s2->exp_withdraws_received); cbor_add_int(w, s1->imp_withdraws_invalid); cbor_add_int(w, -1); @@ -277,7 +277,7 @@ pipe_show_stats_cbor(struct cbor_writer *w, struct pipe_proto *p) cbor_add_int(w, s1->imp_withdraws_accepted); cbor_add_string(w, "export_updates"); - cbor_open_block_with_length(w, 5); + cbor_open_list_with_length(w, 5); cbor_add_int(w, s1->exp_updates_received); cbor_add_int(w, s1->exp_updates_rejected + s2->imp_updates_invalid); cbor_add_int(w, s1->exp_updates_filtered); @@ -285,7 +285,7 @@ pipe_show_stats_cbor(struct cbor_writer *w, struct pipe_proto *p) cbor_add_int(w, s2->imp_updates_accepted); cbor_add_string(w, "export_withdraws"); - cbor_open_block_with_length(w, 5); + cbor_open_list_with_length(w, 5); cbor_add_int(w, s1->exp_withdraws_received); cbor_add_int(w, s2->imp_withdraws_invalid); cbor_add_int(w, -1); diff --git a/proto/rpki/rpki.c b/proto/rpki/rpki.c index 6674e76a5..0d1880b05 100644 --- a/proto/rpki/rpki.c +++ b/proto/rpki/rpki.c @@ -898,7 +898,7 @@ rpki_show_proto_info_timer_cbor(struct cbor_writer *w, const char *name, uint nu cbor_open_block(w); if (tm_active(t)) { - cbor_string_int(w, "time", tm_get_real_time(tm_remains(t)) TO_S); + cbor_string_int(w, "time", tm_remains(t)); cbor_string_int(w, "num", num); } cbor_close_block_or_list(w);