1 // SPDX-License-Identifier: GPL-2.0 2 #include <stdio.h> 3 #include <string.h> 4 5 #include <ynl.h> 6 7 #include <arpa/inet.h> 8 #include <net/if.h> 9 10 #include "rt-addr-user.h" 11 12 static void rt_addr_print(struct rt_addr_getaddr_rsp *a) 13 { 14 char ifname[IF_NAMESIZE]; 15 char addr_str[64]; 16 const char *addr; 17 const char *name; 18 19 name = if_indextoname(a->_hdr.ifa_index, ifname); 20 if (name) 21 printf("%16s: ", name); 22 23 switch (a->_len.address) { 24 case 4: 25 addr = inet_ntop(AF_INET, a->address, 26 addr_str, sizeof(addr_str)); 27 break; 28 case 16: 29 addr = inet_ntop(AF_INET6, a->address, 30 addr_str, sizeof(addr_str)); 31 break; 32 default: 33 addr = NULL; 34 break; 35 } 36 if (addr) 37 printf("%s", addr); 38 else 39 printf("[%d]", a->_len.address); 40 41 printf("\n"); 42 } 43 44 int main(int argc, char **argv) 45 { 46 struct rt_addr_getaddr_list *rsp; 47 struct rt_addr_getaddr_req *req; 48 struct ynl_error yerr; 49 struct ynl_sock *ys; 50 51 ys = ynl_sock_create(&ynl_rt_addr_family, &yerr); 52 if (!ys) { 53 fprintf(stderr, "YNL: %s\n", yerr.msg); 54 return 1; 55 } 56 57 req = rt_addr_getaddr_req_alloc(); 58 if (!req) 59 goto err_destroy; 60 61 rsp = rt_addr_getaddr_dump(ys, req); 62 rt_addr_getaddr_req_free(req); 63 if (!rsp) 64 goto err_close; 65 66 if (ynl_dump_empty(rsp)) 67 fprintf(stderr, "Error: no addresses reported\n"); 68 ynl_dump_foreach(rsp, addr) 69 rt_addr_print(addr); 70 rt_addr_getaddr_list_free(rsp); 71 72 ynl_sock_destroy(ys); 73 return 0; 74 75 err_close: 76 fprintf(stderr, "YNL: %s\n", ys->err.msg); 77 err_destroy: 78 ynl_sock_destroy(ys); 79 return 2; 80 } 81