Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */ 2 : 3 : #include <stdio.h> 4 : 5 : #include "af-list.h" 6 : #include "alloc-util.h" 7 : #include "in-addr-util.h" 8 : #include "local-addresses.h" 9 : #include "tests.h" 10 : 11 2 : static void print_local_addresses(struct local_address *a, unsigned n) { 12 : unsigned i; 13 : 14 10 : for (i = 0; i < n; i++) { 15 8 : _cleanup_free_ char *b = NULL; 16 : 17 8 : assert_se(in_addr_to_string(a[i].family, &a[i].address, &b) >= 0); 18 8 : printf("%s if%i scope=%i metric=%u address=%s\n", af_to_name(a[i].family), a[i].ifindex, a[i].scope, a[i].metric, b); 19 : } 20 2 : } 21 : 22 1 : int main(int argc, char *argv[]) { 23 : struct local_address *a; 24 : int n; 25 : 26 1 : test_setup_logging(LOG_DEBUG); 27 : 28 1 : a = NULL; 29 1 : n = local_addresses(NULL, 0, AF_UNSPEC, &a); 30 1 : assert_se(n >= 0); 31 : 32 1 : printf("Local Addresses:\n"); 33 1 : print_local_addresses(a, (unsigned) n); 34 1 : a = mfree(a); 35 : 36 1 : n = local_gateways(NULL, 0, AF_UNSPEC, &a); 37 1 : assert_se(n >= 0); 38 : 39 1 : printf("Local Gateways:\n"); 40 1 : print_local_addresses(a, (unsigned) n); 41 1 : free(a); 42 : 43 1 : return 0; 44 : }