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-route-user.h"
11
rt_route_print(struct rt_route_getroute_rsp * r)12 static void rt_route_print(struct rt_route_getroute_rsp *r)
13 {
14 char ifname[IF_NAMESIZE];
15 char route_str[64];
16 const char *route;
17 const char *name;
18
19 /* Ignore local */
20 if (r->_hdr.rtm_table == RT_TABLE_LOCAL)
21 return;
22
23 if (r->_present.oif) {
24 name = if_indextoname(r->oif, ifname);
25 if (name)
26 printf("oif: %-16s ", name);
27 }
28
29 if (r->_len.dst) {
30 route = inet_ntop(r->_hdr.rtm_family, r->dst,
31 route_str, sizeof(route_str));
32 printf("dst: %s/%d", route, r->_hdr.rtm_dst_len);
33 }
34
35 if (r->_len.gateway) {
36 route = inet_ntop(r->_hdr.rtm_family, r->gateway,
37 route_str, sizeof(route_str));
38 printf("gateway: %s ", route);
39 }
40
41 printf("\n");
42 }
43
main(int argc,char ** argv)44 int main(int argc, char **argv)
45 {
46 struct rt_route_getroute_req_dump *req;
47 struct rt_route_getroute_list *rsp;
48 struct ynl_error yerr;
49 struct ynl_sock *ys;
50
51 ys = ynl_sock_create(&ynl_rt_route_family, &yerr);
52 if (!ys) {
53 fprintf(stderr, "YNL: %s\n", yerr.msg);
54 return 1;
55 }
56
57 req = rt_route_getroute_req_dump_alloc();
58 if (!req)
59 goto err_destroy;
60
61 rsp = rt_route_getroute_dump(ys, req);
62 rt_route_getroute_req_dump_free(req);
63 if (!rsp)
64 goto err_close;
65
66 if (ynl_dump_empty(rsp))
67 fprintf(stderr, "Error: no routeesses reported\n");
68 ynl_dump_foreach(rsp, route)
69 rt_route_print(route);
70 rt_route_getroute_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