]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
table: add TABLE_IN_ADDR and TABLE_IN6_ADDR
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 28 Jul 2019 04:43:28 +0000 (13:43 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 29 Jul 2019 14:22:14 +0000 (23:22 +0900)
src/shared/format-table.c
src/shared/format-table.h

index aede59bf3463d38e23495bb0342d5f8cbf073ca4..6de7da0df0c97d42fc41f0678fb0c6be4034a206 100644 (file)
@@ -9,6 +9,7 @@
 #include "format-table.h"
 #include "format-util.h"
 #include "gunicode.h"
 #include "format-table.h"
 #include "format-util.h"
 #include "gunicode.h"
+#include "in-addr-util.h"
 #include "memory-util.h"
 #include "pager.h"
 #include "parse-util.h"
 #include "memory-util.h"
 #include "pager.h"
 #include "parse-util.h"
@@ -84,6 +85,7 @@ typedef struct TableData {
                 uint64_t uint64;
                 int percent;        /* we use 'int' as datatype for percent values in order to match the result of parse_percent() */
                 int ifindex;
                 uint64_t uint64;
                 int percent;        /* we use 'int' as datatype for percent values in order to match the result of parse_percent() */
                 int ifindex;
+                union in_addr_union address;
                 /* … add more here as we start supporting more cell data types … */
         };
 } TableData;
                 /* … add more here as we start supporting more cell data types … */
         };
 } TableData;
@@ -252,6 +254,12 @@ static size_t table_data_size(TableDataType type, const void *data) {
         case TABLE_IFINDEX:
                 return sizeof(int);
 
         case TABLE_IFINDEX:
                 return sizeof(int);
 
+        case TABLE_IN_ADDR:
+                return sizeof(struct in_addr);
+
+        case TABLE_IN6_ADDR:
+                return sizeof(struct in6_addr);
+
         default:
                 assert_not_reached("Uh? Unexpected cell type");
         }
         default:
                 assert_not_reached("Uh? Unexpected cell type");
         }
@@ -705,6 +713,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         int percent;
                         int ifindex;
                         bool b;
                         int percent;
                         int ifindex;
                         bool b;
+                        union in_addr_union address;
                 } buffer;
 
                 switch (type) {
                 } buffer;
 
                 switch (type) {
@@ -777,6 +786,16 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         data = &buffer.ifindex;
                         break;
 
                         data = &buffer.ifindex;
                         break;
 
+                case TABLE_IN_ADDR:
+                        buffer.address = *va_arg(ap, union in_addr_union *);
+                        data = &buffer.address.in;
+                        break;
+
+                case TABLE_IN6_ADDR:
+                        buffer.address = *va_arg(ap, union in_addr_union *);
+                        data = &buffer.address.in6;
+                        break;
+
                 case TABLE_SET_MINIMUM_WIDTH: {
                         size_t w = va_arg(ap, size_t);
 
                 case TABLE_SET_MINIMUM_WIDTH: {
                         size_t w = va_arg(ap, size_t);
 
@@ -976,6 +995,12 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
                 case TABLE_IFINDEX:
                         return CMP(a->ifindex, b->ifindex);
 
                 case TABLE_IFINDEX:
                         return CMP(a->ifindex, b->ifindex);
 
+                case TABLE_IN_ADDR:
+                        return CMP(a->address.in.s_addr, b->address.in.s_addr);
+
+                case TABLE_IN6_ADDR:
+                        return memcmp(&a->address.in6, &b->address.in6, FAMILY_ADDRESS_SIZE(AF_INET6));
+
                 default:
                         ;
                 }
                 default:
                         ;
                 }
@@ -1218,6 +1243,18 @@ static const char *table_data_format(TableData *d) {
                 break;
         }
 
                 break;
         }
 
+        case TABLE_IN_ADDR:
+        case TABLE_IN6_ADDR: {
+                _cleanup_free_ char *p = NULL;
+
+                if (in_addr_to_string(d->type == TABLE_IN_ADDR ? AF_INET : AF_INET6,
+                                      &d->address, &p) < 0)
+                        return NULL;
+
+                d->formatted = TAKE_PTR(p);
+                break;
+        }
+
         default:
                 assert_not_reached("Unexpected type?");
         }
         default:
                 assert_not_reached("Unexpected type?");
         }
@@ -1749,6 +1786,12 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
         case TABLE_IFINDEX:
                 return json_variant_new_integer(ret, d->ifindex);
 
         case TABLE_IFINDEX:
                 return json_variant_new_integer(ret, d->ifindex);
 
+        case TABLE_IN_ADDR:
+                return json_variant_new_array_bytes(ret, &d->address, FAMILY_ADDRESS_SIZE(AF_INET));
+
+        case TABLE_IN6_ADDR:
+                return json_variant_new_array_bytes(ret, &d->address, FAMILY_ADDRESS_SIZE(AF_INET6));
+
         default:
                 return -EINVAL;
         }
         default:
                 return -EINVAL;
         }
index 452f1706c0cea8d0f82bf96e5f38bd39b79b1754..d402e95e0f1d8004bd33a5e28a2832a944139bfb 100644 (file)
@@ -27,6 +27,8 @@ typedef enum TableDataType {
         TABLE_UINT64,
         TABLE_PERCENT,
         TABLE_IFINDEX,
         TABLE_UINT64,
         TABLE_PERCENT,
         TABLE_IFINDEX,
+        TABLE_IN_ADDR,  /* Takes a union in_addr_union (or a struct in_addr) */
+        TABLE_IN6_ADDR, /* Takes a union in_addr_union (or a struct in6_addr) */
         _TABLE_DATA_TYPE_MAX,
 
         /* The following are not really data types, but commands for table_add_cell_many() to make changes to
         _TABLE_DATA_TYPE_MAX,
 
         /* The following are not really data types, but commands for table_add_cell_many() to make changes to