Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/slash
43 changes: 9 additions & 34 deletions src/csp_init_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ static int csp_init_cmd(struct slash *slash) {
int version = 2;
int dedup = 3;

// optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp init", "");
/* No __attribute__((cleanup(optparse_del))), as the arg string pointers might be used */
optparse_t * parser = optparse_new("csp init", "");
optparse_add_help(parser);
optparse_add_string(parser, 'n', "host", "HOSTNAME", &hostname, "Hostname (default = linux hostname)");
Expand Down Expand Up @@ -126,7 +128,7 @@ static int csp_ifadd_zmq_cmd(struct slash *slash) {
unsigned int subport = 0;
unsigned int pubport = 0;

optparse_t * parser = optparse_new("csp add zmq", "<addr> <server>");
optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add zmq", "<addr> <server>");
optparse_add_help(parser);
optparse_add_set(parser, 'p', "promisc", 1, &promisc, "Promiscuous Mode");
optparse_add_int(parser, 'm', "mask", "NUM", 0, &mask, "Netmask (defaults to 8)");
Expand All @@ -138,21 +140,18 @@ static int csp_ifadd_zmq_cmd(struct slash *slash) {
int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1);

if (argi < 0) {
optparse_del(parser);
return SLASH_EINVAL;
}

if (++argi >= slash->argc) {
printf("missing parameter addr\n");
optparse_del(parser);
return SLASH_EINVAL;
}
char * endptr;
unsigned int addr = strtoul(slash->argv[argi], &endptr, 10);

if (++argi >= slash->argc) {
printf("missing parameter server\n");
optparse_del(parser);
return SLASH_EINVAL;
}
char * server = slash->argv[argi];
Expand All @@ -179,23 +178,20 @@ static int csp_ifadd_zmq_cmd(struct slash *slash) {
FILE *file = fopen(key_file_local, "r");
if(file == NULL){
printf("Could not open config %s\n", key_file_local);
optparse_del(parser);
return SLASH_EINVAL;
}

sec_key = malloc(CURVE_KEYLEN * sizeof(char));
if (sec_key == NULL) {
printf("Failed to allocate memory for secret key.\n");
fclose(file);
optparse_del(parser);
return SLASH_EINVAL;
}

if (fgets(sec_key, CURVE_KEYLEN, file) == NULL) {
printf("Failed to read secret key from file.\n");
free(sec_key);
fclose(file);
optparse_del(parser);
return SLASH_EINVAL;
}
fclose(file);
Expand All @@ -210,7 +206,6 @@ static int csp_ifadd_zmq_cmd(struct slash *slash) {
if (sec_key != NULL) {
free(sec_key);
}
optparse_del(parser);
return SLASH_SUCCESS;
}

Expand All @@ -229,7 +224,7 @@ static int csp_ifadd_kiss_cmd(struct slash *slash) {
int baud = 1000000;
char * device = "/dev/ttyUSB0";

optparse_t * parser = optparse_new("csp add kiss", "<addr>");
optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add kiss", "<addr>");
optparse_add_help(parser);
optparse_add_set(parser, 'p', "promisc", 1, &promisc, "Promiscuous Mode");
optparse_add_int(parser, 'm', "mask", "NUM", 0, &mask, "Netmask (defaults to 8)");
Expand All @@ -245,7 +240,6 @@ static int csp_ifadd_kiss_cmd(struct slash *slash) {

if (++argi >= slash->argc) {
printf("missing parameter addr\n");
optparse_del(parser);
return SLASH_EINVAL;
}
char * endptr;
Expand All @@ -265,7 +259,6 @@ static int csp_ifadd_kiss_cmd(struct slash *slash) {
int error = csp_usart_open_and_add_kiss_interface(&conf, name, &iface);
if (error != CSP_ERR_NONE) {
printf("Failed to add kiss interface\n");
optparse_del(parser);
return SLASH_EINVAL;
}

Expand Down Expand Up @@ -293,7 +286,7 @@ static int csp_ifadd_can_cmd(struct slash *slash) {
int baud = 1000000;
char * device = "can0";

optparse_t * parser = optparse_new("csp add can", "<addr>");
optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add can", "<addr>");
optparse_add_help(parser);
optparse_add_set(parser, 'p', "promisc", 1, &promisc, "Promiscous Mode");
optparse_add_int(parser, 'm', "mask", "NUM", 0, &mask, "Netmask (defaults to 8)");
Expand All @@ -304,13 +297,11 @@ static int csp_ifadd_can_cmd(struct slash *slash) {
int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1);

if (argi < 0) {
optparse_del(parser);
return SLASH_EINVAL;
}

if (++argi >= slash->argc) {
printf("missing parameter addr\n");
optparse_del(parser);
return SLASH_EINVAL;
}
char * endptr;
Expand All @@ -321,7 +312,6 @@ static int csp_ifadd_can_cmd(struct slash *slash) {
int error = csp_can_socketcan_open_and_add_interface(device, name, addr, baud, promisc, &iface);
if (error != CSP_ERR_NONE) {
csp_print("failed to add CAN interface [%s], error: %d", device, error);
optparse_del(parser);
return SLASH_EINVAL;
}

Expand Down Expand Up @@ -377,7 +367,7 @@ static int csp_ifadd_eth_cmd(struct slash *slash) {
int dfl = 0;
int mtu = 1200;

optparse_t * parser = optparse_new("csp add eth", "<addr>");
optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add eth", "<addr>");
optparse_add_help(parser);
optparse_add_string(parser, 'e', "device", "STR", (char **)&device, "Ethernet device name or name prefix (defaults to enp)");
optparse_add_set(parser, 'p', "promisc", 1, &promisc, "Promiscous Mode");
Expand All @@ -393,15 +383,13 @@ static int csp_ifadd_eth_cmd(struct slash *slash) {

if (++argi >= slash->argc) {
printf("missing parameter addr\n");
optparse_del(parser);
return SLASH_EINVAL;
}
char * endptr;
unsigned int addr = strtoul(slash->argv[argi], &endptr, 10);

eth_select_interface(&device);
if (strlen(device) == 0) {
optparse_del(parser);
return SLASH_EINVAL;
}

Expand All @@ -414,7 +402,6 @@ static int csp_ifadd_eth_cmd(struct slash *slash) {
iface->addr = addr;
iface->netmask = mask;

optparse_del(parser);
return SLASH_SUCCESS;
}

Expand All @@ -433,7 +420,7 @@ static int csp_ifadd_udp_cmd(struct slash *slash) {
int listen_port = 9220;
int remote_port = 9220;

optparse_t * parser = optparse_new("csp add udp", "<addr> <server>");
optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add udp", "<addr> <server>");
optparse_add_help(parser);
optparse_add_set(parser, 'p', "promisc", 1, &promisc, "Promiscous Mode");
optparse_add_int(parser, 'm', "mask", "NUM", 0, &mask, "Netmask (defaults to 8)");
Expand All @@ -444,21 +431,18 @@ static int csp_ifadd_udp_cmd(struct slash *slash) {
int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1);

if (argi < 0) {
optparse_del(parser);
return SLASH_EINVAL;
}

if (++argi >= slash->argc) {
printf("missing parameter addr\n");
optparse_del(parser);
return SLASH_EINVAL;
}
char * endptr;
unsigned int addr = strtoul(slash->argv[argi], &endptr, 10);

if (++argi >= slash->argc) {
printf("missing parameter server\n");
optparse_del(parser);
return SLASH_EINVAL;
}
char * server = slash->argv[argi];
Expand All @@ -476,7 +460,6 @@ static int csp_ifadd_udp_cmd(struct slash *slash) {
iface->addr = addr;
iface->netmask = mask;

optparse_del(parser);
return SLASH_SUCCESS;
}

Expand All @@ -493,7 +476,7 @@ static int csp_ifadd_tun_cmd(struct slash *slash) {
int mask = 8;
int dfl = 0;

optparse_t * parser = optparse_new("csp add tun", "<ifaddr> <tun src> <tun dst>");
optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add tun", "<ifaddr> <tun src> <tun dst>");
optparse_add_help(parser);
optparse_add_set(parser, 'p', "promisc", 1, &promisc, "Promiscous Mode");
optparse_add_int(parser, 'm', "mask", "NUM", 0, &mask, "Netmask (defaults to 8)");
Expand All @@ -507,22 +490,19 @@ static int csp_ifadd_tun_cmd(struct slash *slash) {

if (++argi >= slash->argc) {
printf("missing parameter addr\n");
optparse_del(parser);
return SLASH_EINVAL;
}
char * endptr;
unsigned int addr = strtoul(slash->argv[argi], &endptr, 10);

if (++argi >= slash->argc) {
printf("missing parameter tun src\n");
optparse_del(parser);
return SLASH_EINVAL;
}
unsigned int tun_src = strtoul(slash->argv[argi], &endptr, 10);

if (++argi >= slash->argc) {
printf("missing parameter tun dst\n");
optparse_del(parser);
return SLASH_EINVAL;
}
unsigned int tun_dst = strtoul(slash->argv[argi], &endptr, 10);
Expand All @@ -539,7 +519,6 @@ static int csp_ifadd_tun_cmd(struct slash *slash) {
iface->addr = addr;
iface->netmask = mask;

optparse_del(parser);
return SLASH_SUCCESS;
}

Expand All @@ -550,7 +529,7 @@ static int csp_routeadd_cmd(struct slash *slash) {

char route[50];

optparse_t * parser = optparse_new("csp add route", "<addr>/<mask> <ifname>");
optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp add route", "<addr>/<mask> <ifname>");
optparse_add_help(parser);

int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1);
Expand All @@ -562,7 +541,6 @@ static int csp_routeadd_cmd(struct slash *slash) {
/* Build string from the two slash input arguments */
if (++argi >= slash->argc) {
printf("missing parameter addr/mask\n");
optparse_del(parser);
return SLASH_EINVAL;
}
strcpy(route, slash->argv[argi]);
Expand All @@ -571,19 +549,16 @@ static int csp_routeadd_cmd(struct slash *slash) {

if (++argi >= slash->argc) {
printf("missing parameter ifname\n");
optparse_del(parser);
return SLASH_EINVAL;
}

strcpy(&route[strlen(route)], slash->argv[argi]);

if (csp_rtable_load(route) == 1) { /* function returns number of routes added */
printf("Added route %s\n", route);
optparse_del(parser);
return SLASH_SUCCESS;
} else {
printf("Error during route add\n");
optparse_del(parser);
return SLASH_EINVAL;
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/csp_scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ static int csp_scan(struct slash *slash)
unsigned int end = 0x3FFE;
char * search_str = 0;

optparse_t * parser = optparse_new("csp scan", NULL);
optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("csp scan", NULL);
optparse_add_help(parser);
optparse_add_unsigned(parser, 'b', "begin", "NUM", 0, &begin, "begin at node");
optparse_add_unsigned(parser, 'e', "end", "NUM", 0, &end, "end at node");
optparse_add_string(parser, 's', "search", "STR", &search_str, "host name search sub-string");
int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1);
if (argi < 0) {
optparse_del(parser);
return SLASH_EINVAL;
}

Expand Down Expand Up @@ -47,7 +46,6 @@ static int csp_scan(struct slash *slash)
break;
}
printf("\r");
optparse_del(parser);
return SLASH_SUCCESS;
}

Expand Down
4 changes: 1 addition & 3 deletions src/hk_param_sniffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ void hk_set_epoch(time_t epoch, uint16_t node) {
static int hk_timeoffset(struct slash *slash) {

unsigned int node = slash_dfl_node;
optparse_t * parser = optparse_new("hk timeoffset [epoch]", "Satellite epoch time in seconds relative to Jan 1th 1970");
optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("hk timeoffset [epoch]", "Satellite epoch time in seconds relative to Jan 1th 1970");
optparse_add_unsigned(parser, 'n', "node", "NUM", 0, &node, "node (default = <env>)");
optparse_add_help(parser);
int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1);
if (argi < 0) {
optparse_del(parser);
return SLASH_EINVAL;
}

Expand All @@ -77,7 +76,6 @@ static int hk_timeoffset(struct slash *slash) {
}
}

optparse_del(parser);
return SLASH_SUCCESS;
}

Expand Down
5 changes: 1 addition & 4 deletions src/known_hosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,24 @@ static int cmd_hosts_add(struct slash *slash)

int node = slash_dfl_node;

optparse_t * parser = optparse_new("hosts add", "<name>");
optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("hosts add", "<name>");
optparse_add_help(parser);
optparse_add_int(parser, 'n', "node", "NUM", 0, &node, "node (default = <env>)");

int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1);
if (argi < 0) {
optparse_del(parser);
return SLASH_EINVAL;
}

/* Check if name is present */
if (++argi >= slash->argc) {
printf("missing parameter name\n");
optparse_del(parser);
return SLASH_EINVAL;
}

char * name = slash->argv[argi];

known_hosts_add(node, name);
optparse_del(parser);
return SLASH_SUCCESS;
}

Expand Down
6 changes: 1 addition & 5 deletions src/loki.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ static int loki_start_cmd(struct slash * slash) {
char * tmp_username = NULL;
char * tmp_password = NULL;

optparse_t * parser = optparse_new("loki start", "<server>");
optparse_t * parser __attribute__((cleanup(optparse_del))) = optparse_new("loki start", "<server>");
optparse_add_help(parser);
optparse_add_string(parser, 'u', "user", "STRING", &tmp_username, "Username for vmauth");
optparse_add_string(parser, 'p', "pass", "STRING", &tmp_password, "Password for vmauth");
Expand All @@ -259,20 +259,17 @@ static int loki_start_cmd(struct slash * slash) {
int argi = optparse_parse(parser, slash->argc - 1, (const char **)slash->argv + 1);

if (argi < 0) {
optparse_del(parser);
return SLASH_EINVAL;
}

if (++argi >= slash->argc) {
printf("Missing server ip/domain\n");
optparse_del(parser);
return SLASH_EINVAL;
}

if (tmp_username) {
if (!tmp_password) {
printf("Provide password with -p\n");
optparse_del(parser);
return SLASH_EINVAL;
}
args.username = strdup(tmp_username);
Expand Down Expand Up @@ -346,7 +343,6 @@ static int loki_start_cmd(struct slash * slash) {
loki_running = 1;
printf("Loki logging started\n");

optparse_del(parser);

return SLASH_SUCCESS;
}
Expand Down
Loading