da2bf9
From 1b12b8e9c0f6f230e12ca13bd70f27ef0a2fcfdd Mon Sep 17 00:00:00 2001
da2bf9
From: Yu Watanabe <watanabe.yu+github@gmail.com>
da2bf9
Date: Sun, 15 Dec 2019 23:01:54 +0900
da2bf9
Subject: [PATCH] util: introduce ifname_valid_full()
da2bf9
da2bf9
(cherry picked from commit 4252696aec9ec038ff312a164e25f039da25126f)
da2bf9
da2bf9
Related: #2005008
da2bf9
---
da2bf9
 src/basic/socket-util.c     | 12 +++++++++---
da2bf9
 src/basic/socket-util.h     |  5 ++++-
da2bf9
 src/test/test-socket-util.c |  1 +
da2bf9
 3 files changed, 14 insertions(+), 4 deletions(-)
da2bf9
da2bf9
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
da2bf9
index 053bcba670..7f8066123b 100644
da2bf9
--- a/src/basic/socket-util.c
da2bf9
+++ b/src/basic/socket-util.c
da2bf9
@@ -13,6 +13,7 @@
da2bf9
 #include <stdlib.h>
da2bf9
 #include <string.h>
da2bf9
 #include <unistd.h>
da2bf9
+#include <linux/if.h>
da2bf9
 
da2bf9
 #include "alloc-util.h"
da2bf9
 #include "fd-util.h"
da2bf9
@@ -868,7 +869,7 @@ static const char* const ip_tos_table[] = {
da2bf9
 
da2bf9
 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
da2bf9
 
da2bf9
-bool ifname_valid(const char *p) {
da2bf9
+bool ifname_valid_full(const char *p, bool alternative) {
da2bf9
         bool numeric = true;
da2bf9
 
da2bf9
         /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources
da2bf9
@@ -878,8 +879,13 @@ bool ifname_valid(const char *p) {
da2bf9
         if (isempty(p))
da2bf9
                 return false;
da2bf9
 
da2bf9
-        if (strlen(p) >= IFNAMSIZ)
da2bf9
-                return false;
da2bf9
+        if (alternative) {
da2bf9
+                if (strlen(p) >= ALTIFNAMSIZ)
da2bf9
+                        return false;
da2bf9
+        } else {
da2bf9
+                if (strlen(p) >= IFNAMSIZ)
da2bf9
+                        return false;
da2bf9
+        }
da2bf9
 
da2bf9
         if (dot_or_dot_dot(p))
da2bf9
                 return false;
da2bf9
diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h
da2bf9
index c7c9ad34d6..30baba6c03 100644
da2bf9
--- a/src/basic/socket-util.h
da2bf9
+++ b/src/basic/socket-util.h
da2bf9
@@ -123,7 +123,10 @@ int fd_inc_rcvbuf(int fd, size_t n);
da2bf9
 int ip_tos_to_string_alloc(int i, char **s);
da2bf9
 int ip_tos_from_string(const char *s);
da2bf9
 
da2bf9
-bool ifname_valid(const char *p);
da2bf9
+bool ifname_valid_full(const char *p, bool alternative);
da2bf9
+static inline bool ifname_valid(const char *p) {
da2bf9
+        return ifname_valid_full(p, false);
da2bf9
+}
da2bf9
 bool address_label_valid(const char *p);
da2bf9
 
da2bf9
 int getpeercred(int fd, struct ucred *ucred);
da2bf9
diff --git a/src/test/test-socket-util.c b/src/test/test-socket-util.c
da2bf9
index 19c5395b92..c545622c09 100644
da2bf9
--- a/src/test/test-socket-util.c
da2bf9
+++ b/src/test/test-socket-util.c
da2bf9
@@ -39,6 +39,7 @@ static void test_ifname_valid(void) {
da2bf9
 
da2bf9
         assert(ifname_valid("xxxxxxxxxxxxxxx"));
da2bf9
         assert(!ifname_valid("xxxxxxxxxxxxxxxx"));
da2bf9
+        assert(ifname_valid_full("xxxxxxxxxxxxxxxx", true));
da2bf9
 }
da2bf9
 
da2bf9
 static void test_socket_address_parse(void) {