summaryrefslogtreecommitdiffstats
path: root/src/tuntap.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2013-08-12 18:18:26 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2013-08-12 18:34:11 +0200
commit78de22fd21b348fc00dde6fa0bfec73abb8fd651 (patch)
tree1efa14c85f33648a05fb2927ff3dcaf12724ed23 /src/tuntap.c
parentd351a2403c5946d07ae6e6b6e075ff9896c6bcb6 (diff)
downloadfastd-78de22fd21b348fc00dde6fa0bfec73abb8fd651.tar
fastd-78de22fd21b348fc00dde6fa0bfec73abb8fd651.zip
Improve interface name handling
Diffstat (limited to 'src/tuntap.c')
-rw-r--r--src/tuntap.c37
1 files changed, 19 insertions, 18 deletions
diff --git a/src/tuntap.c b/src/tuntap.c
index bf0c49c..64633a8 100644
--- a/src/tuntap.c
+++ b/src/tuntap.c
@@ -157,22 +157,29 @@ void fastd_tuntap_open(fastd_context_t *ctx) {
pr_debug(ctx, "initializing tun/tap device...");
char ifname[5+IFNAMSIZ] = "/dev/";
+ const char *type;
+
+ switch (ctx->conf->mode) {
+ case MODE_TAP:
+ type = "tap";
+ break;
+
+ case MODE_TUN:
+ type = "tun";
+ break;
+
+ default:
+ exit_bug(ctx, "invalid mode");
+ }
+
if (ctx->conf->ifname) {
+ if (strncmp(ctx->conf->ifname, type, 3) != 0)
+ exit_error(ctx, "`%s' doesn't seem to be a %s device", ctx->conf->ifname, type);
+
strncat(ifname, ctx->conf->ifname, IFNAMSIZ-1);
}
else {
- switch (ctx->conf->mode) {
- case MODE_TAP:
- strncat(ifname, "tap", IFNAMSIZ-1);
- break;
-
- case MODE_TUN:
- strncat(ifname, "tun", IFNAMSIZ-1);
- break;
-
- default:
- exit_bug(ctx, "invalid mode");
- }
+ strncat(ifname, type, IFNAMSIZ-1);
}
if ((ctx->tunfd = open(ifname, O_RDWR|O_CLOEXEC|O_NONBLOCK)) < 0)
@@ -183,16 +190,10 @@ void fastd_tuntap_open(fastd_context_t *ctx) {
switch (ctx->conf->mode) {
case MODE_TAP:
- if (strncmp(ctx->ifname, "tap", 3) != 0)
- exit_error(ctx, "opened device doesn't seem to be a tap device");
-
setup_tap(ctx);
break;
case MODE_TUN:
- if (strncmp(ctx->ifname, "tun", 3) != 0)
- exit_error(ctx, "opened device doesn't seem to be a tun device");
-
setup_tun(ctx);
break;