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