From 578559d8f398f516fe6e1730af0a72b069f94cb4 Mon Sep 17 00:00:00 2001 From: Mathieu Jobin Date: Mon, 8 Jun 2020 14:56:24 +0900 Subject: [PATCH] give interface a nice name from the netname if tapcfg does not support the name for length or special chars it will fallback to tap0, tap1, etc. I thought of reusing vlink->netname but I needed const char* --- src/control.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/control.c b/src/control.c index ef82246..d2a66b0 100644 --- a/src/control.c +++ b/src/control.c @@ -776,6 +776,8 @@ void tapcfg_log(int level, char *msg) log_warnx("%s: %s", __func__, msg); } +const char* strcat_copy(const char *str1, const char *str2); + int control_init(const char *network_name) { @@ -809,13 +811,15 @@ control_init(const char *network_name) tapcfg_set_log_callback(vlink->tapcfg, tapcfg_log); - if ((vlink->tapfd = tapcfg_start(vlink->tapcfg, "netvfy0", 1)) < 0) { - log_warnx("%s: tapcfg_start", __func__); + if ((vlink->netname = strdup(network_name)) == NULL) { + log_warn("%s: strdup", __func__); goto error; } - if ((vlink->netname = strdup(network_name)) == NULL) { - log_warn("%s: strdup", __func__); + + const char* ifname = strcat_copy(vlink->netname, "0"); + if ((vlink->tapfd = tapcfg_start(vlink->tapcfg, ifname, 1)) < 0) { + log_warnx("%s: tapcfg_start", __func__); goto error; } @@ -852,3 +856,22 @@ control_fini(void) { vlink_free(vlink); } + +const char* strcat_copy(const char *str1, const char *str2) { + int str1_len, str2_len; + char *new_str; + + /* null check */ + + str1_len = strlen(str1); + str2_len = strlen(str2); + + new_str = malloc(str1_len + str2_len + 1); + + /* null check */ + + memcpy(new_str, str1, str1_len); + memcpy(new_str + str1_len, str2, str2_len + 1); + + return new_str; +}