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