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