]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util: introduce ifname_valid_full()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 15 Dec 2019 14:01:54 +0000 (23:01 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 16 Dec 2019 01:52:22 +0000 (10:52 +0900)
src/basic/socket-util.c
src/basic/socket-util.h
src/test/test-socket-util.c

index 19dd053e362df32d9e42830b4c5c027de0f927c0..64bc79712f10667b1d25ff3944d18a2240bd0fdf 100644 (file)
@@ -13,6 +13,7 @@
 #include <stdlib.h>
 #include <sys/ioctl.h>
 #include <unistd.h>
+#include <linux/if.h>
 
 #include "alloc-util.h"
 #include "errno-util.h"
@@ -909,7 +910,7 @@ static const char* const ip_tos_table[] = {
 
 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
 
-bool ifname_valid(const char *p) {
+bool ifname_valid_full(const char *p, bool alternative) {
         bool numeric = true;
 
         /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources
@@ -919,8 +920,13 @@ bool ifname_valid(const char *p) {
         if (isempty(p))
                 return false;
 
-        if (strlen(p) >= IFNAMSIZ)
-                return false;
+        if (alternative) {
+                if (strlen(p) >= ALTIFNAMSIZ)
+                        return false;
+        } else {
+                if (strlen(p) >= IFNAMSIZ)
+                        return false;
+        }
 
         if (dot_or_dot_dot(p))
                 return false;
index 48a22415df944f6711e2244aedde8780e11d1afc..17c20abc32fd139e588261de620c1ce65e4ceafb 100644 (file)
@@ -130,7 +130,10 @@ int fd_inc_rcvbuf(int fd, size_t n);
 int ip_tos_to_string_alloc(int i, char **s);
 int ip_tos_from_string(const char *s);
 
-bool ifname_valid(const char *p);
+bool ifname_valid_full(const char *p, bool alternative);
+static inline bool ifname_valid(const char *p) {
+        return ifname_valid_full(p, false);
+}
 bool address_label_valid(const char *p);
 
 int getpeercred(int fd, struct ucred *ucred);
index b36e00526459e3511ee7b98714d3056c40a39113..2489d00e263b8d91095ae8d70df9a254464ae15e 100644 (file)
@@ -45,6 +45,7 @@ static void test_ifname_valid(void) {
 
         assert(ifname_valid("xxxxxxxxxxxxxxx"));
         assert(!ifname_valid("xxxxxxxxxxxxxxxx"));
+        assert(ifname_valid_full("xxxxxxxxxxxxxxxx", true));
 }
 
 static void test_socket_address_parse_one(const char *in, int ret, int family, const char *expected) {