struct cbor_writer {
int pt; // where will next byte go
int capacity;
- struct linpool *lp;
int8_t *cbor;
+ struct linpool *lp;
};
void write_item(struct cbor_writer *writer, int8_t major, int num);
-struct cbor_writer *cbor_init(struct linpool *lp, int size_guess) {
- struct cbor_writer *writer = (struct cbor_writer *) lp_alloc(lp, sizeof(struct cbor_writer));
- writer->cbor = lp_alloc(lp, size_guess * sizeof(int8_t));
- writer->capacity = size_guess;
- writer->lp = lp;
+struct cbor_writer *cbor_init(byte *buff, uint capacity, struct linpool *lp) {
+ struct cbor_writer *writer = (struct cbor_writer*)lp_alloc(lp, sizeof(struct cbor_writer));
+ writer->cbor = buff;
+ writer->capacity = capacity;
writer->pt =0;
+ writer->lp = lp;
return writer;
}
void check_memory(struct cbor_writer *writer, int add_size) {
if (writer->capacity - writer->pt-add_size < 0) {
- int8_t *a = writer->cbor;
- writer->cbor = lp_alloc(writer->lp, writer->capacity*2);
- memcpy(writer->cbor, a, writer->pt);
+ bug("There is not enough space for cbor response in given buffer");
}
}
* first one) and schedules cli.event .
*
*/
+#include <stdio.h>
+#include <stdlib.h>
#include "nest/bird.h"
#include "nest/cli.h"
#include "lib/string.h"
pool *cli_pool;
+pool *yi_pool;
static byte *
cli_alloc_out(cli *c, int size)
ev_schedule(c->event);
}
+uint
+yi_process(uint size, byte *rbuf, byte *tbuf, uint tbsize) {
+ return cmd_show_memory_cbor(tbuf, tbsize);
+}
+
static list cli_log_hooks;
static int cli_log_inited;
+static list yi_log_hooks;
+static int yi_log_inited;
+
void
cli_set_log_echo(cli *c, uint mask, uint size)
{
init_list(&cli_log_hooks);
cli_log_inited = 1;
}
+
+void
+yi_init(void)
+{
+ yi_pool = rp_new(&root_pool, "YI");
+ init_list(&yi_log_hooks); /*we may be do not need this*/
+ yi_log_inited = 1;
+}
extern pool *cli_pool;
extern struct cli *this_cli; /* Used during parsing */
+extern pool *yi_pool;
#define CLI_ASYNC_CODE 10000
cli *cli_new(void *);
void cli_init(void);
+void yi_init(void);
void cli_free(cli *);
void cli_kick(cli *);
+uint yi_process(uint size, byte *rbuf, byte *tbuf, uint tbsize);
void cli_written(cli *);
void cli_echo(uint class, byte *msg);
cli_msg(13, "Daemon is up and running");
- byte time[TM_DATETIME_BUFFER_SIZE];
+ /*byte time[TM_DATETIME_BUFFER_SIZE];
tm_format_time(time, &config->tf_base, current_time());
struct cbor_writer *w = cbor_init(lp_new(proto_pool), 1000);
+ cbor_open_block_with_length(w, 1);
+ cbor_add_string(w, "show_status:message");
+
cbor_open_block_with_length(w, 3);
cbor_string_string(w, "BIRD", BIRD_VERSION);
cbor_add_string(w, "body");
cbor_add_string(w, "Reconfiguration in progress");
else
cbor_add_string(w, "Daemon is up and running");
- cbor_write_to_file(w, "test.cbor");
+ cbor_write_to_file(w, "test.cbor");*/
}
void
#endif
print_size("Total:", total);
cli_msg(0, "");
-
-
- struct cbor_writer *w = cbor_init(lp_new(proto_pool), 1000);
+}
+
+uint
+cmd_show_memory_cbor(byte *tbuf, uint capacity) {
+ bug("in cmd_show_memory_cbor");
+ struct cbor_writer *w = cbor_init(tbuf, capacity, lp_new(proto_pool));
cbor_open_block_with_length(w, 1);
cbor_add_string(w, "show_memory:message");
cbor_close_block_or_list(w); // we do not know for sure, that standby memory will be printed, so we do not know number of block items. If we know that, we open the block for 6 (or 5) items and we do not close anything
cbor_write_to_file(w, "show_memory.cbor");
-
+ return w->pt;
}
void
void cmd_show_status(void);
void cmd_show_symbols(struct sym_show_data *sym);
void cmd_show_memory(void);
+uint cmd_show_memory_cbor(byte *tbuf, uint capacity);
struct f_line;
void cmd_eval(const struct f_line *expr);
static void
call_rx_hook(sock *s, int size)
{
+ log(L_WARN "soc rx_hook");
if (s->rx_hook(s, size))
{
/* We need to be careful since the socket could have been deleted by the hook */
*/
static sock *cli_sk;
+static sock *yi_sk;
static char *path_control_socket = PATH_CONTROL_SOCKET;
+static char *path_control_socket_yi = NULL;
static void
static int
cli_rx(sock *s, uint size UNUSED)
{
+ log("rcli_rx");
cli_kick(s->data);
return 0;
}
+
+static int
+yi_rx(sock *s, uint size)
+{
+ /* zpracuj data délky len začínající na s->rbuf */
+ /* zapiš výsledek do s->tbuf */
+ log("size tbuf %ui", s->tbsize);
+ uint tx_len = yi_process(size, s->rbuf, s->tbuf, s->tbsize);
+ sk_send(s, tx_len);
+ return 1;
+}
+
static void
cli_err(sock *s, int err)
{
log(L_INFO "Failed to accept CLI connection: %s", strerror(err));
}
+static void
+yi_connect_err(sock *s UNUSED, int err)
+{
+ ASSERT_DIE(err);
+ if (config->cli_debug)
+ log(L_INFO "Failed to accept YI connection: %s", strerror(err));
+}
+
static int
cli_connect(sock *s, uint size UNUSED)
{
cli *c;
- if (config->cli_debug)
+ //if (config->cli_debug)
log(L_INFO "CLI connect");
s->rx_hook = cli_rx;
s->tx_hook = cli_tx;
return 1;
}
+static int
+yi_connect(sock *s, uint size UNUSED)
+{
+ cli *c;
+ log("YI connect");
+ //if (config->cli_debug)
+ log(L_INFO "YANG connect");
+ s->rx_hook = yi_rx;
+ s->tx_hook = cli_tx;
+ s->err_hook = cli_err;
+ s->data = c = cli_new(s);
+ s->pool = c->pool;
+ s->fast_rx = 1;
+ c->rx_pos = c->rx_buf;
+ rmove(s, c->pool);
+ log("connect ok");
+ return 1;
+}
+
+
+static void
+yi_init_unix(uid_t use_uid, gid_t use_gid)
+{
+ sock *s;
+ log("yi_init_unix before");
+ yi_init();
+ s = yi_sk = sk_new(yi_pool);
+ s->type = SK_UNIX_PASSIVE;
+ s->rx_hook = yi_connect;
+ s->err_hook = yi_connect_err;
+ s->rbsize = 1024;
+ s->tbsize = 16384;
+ s->fast_rx = 1;
+ log("yi_init_unix after set");
+ /* Return value intentionally ignored */
+ unlink(path_control_socket_yi);
+
+ if (sk_open_unix(s, path_control_socket_yi) < 0)
+ die("Cannot create control socket %s: %m", path_control_socket_yi);
+
+ if (use_uid || use_gid)
+ if (chown(path_control_socket_yi, use_uid, use_gid) < 0)
+ die("chown: %m");
+
+ if (chmod(path_control_socket_yi, 0660) < 0)
+ die("chmod: %m");
+ log("yi_init_unix after fc");
+}
+
+
static void
cli_init_unix(uid_t use_uid, gid_t use_gid)
{
die("chmod: %m");
}
+
/*
* PID file
*/
* Parsing of command-line arguments
*/
-static char *opt_list = "bc:dD:ps:P:u:g:flRh";
+static char *opt_list = "bc:dD:ps:Y:P:u:g:flRh";
int parse_and_exit;
char *bird_name;
static char *use_user;
path_control_socket = optarg;
socket_changed = 1;
break;
+ case 'Y':
+ path_control_socket_yi = optarg;
+ break;
case 'P':
pid_file = optarg;
break;
{
test_old_bird(path_control_socket);
cli_init_unix(use_uid, use_gid);
+ if (path_control_socket_yi)
+ {
+ yi_init_unix(use_uid, use_gid);
+ }
+ else { //todo delete
+ path_control_socket_yi = "bird-yang.ctl";
+ log(L_INFO "before function");
+ yi_init_unix(use_uid, use_gid);
+ }
}
if (use_gid)
dup2(0, 1);
dup2(0, 2);
}
-
+ log("before main thread init");
main_thread_init();
write_pid_file();
{
- "show_status:message": {
- "header": {
- "bird": "BIRD ",
- "version": "2.14"
- },
- "body": {
- "router_id": "35496",
- "hostname": "bird",
- "server_time": "2020-10-10T10:10:10+00:00" ,
- "last_reboot": "2020-10-10T10:10:10+00:00" ,
- "last_reconfiguration": "2020-10-10T10:10:10+00:00"
- },
- "state": "Daemon is up and running"
+ "show_memory:message": {
+ "header": "BIRD memory usage",
+ "body": {
+ "routing_tables": {
+ "effective": 34604,
+ "overhead": 2848
+ },
+ "route_attributes": {
+ "effective": 26826,
+ "overhead": 13448
+ },
+ "protocols": {
+ "effective": 63564,
+ "overhead": 15312
+ },
+ "current_config": {
+ "effective": 299744,
+ "overhead": 2152
+ },
+ "standby_memory": {
+ "effective": 0,
+ "overhead": 348160
+ },
+ "total": {
+ "effective": 506754,
+ "overhead": 388960
+ }
+ }
}
-}
+}
\ No newline at end of file