Fix derefrencing of NULL pointer
[platform/upstream/connman.git] / tools / netlink-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2013-2014  BMW Car IT GmbH.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/socket.h>
27 #include <linux/netlink.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/genetlink.h>
30 #include <linux/netfilter/nfnetlink.h>
31 #include <net/if.h>
32
33 #include <string.h>
34 #include <stdio.h>
35 #include <inttypes.h>
36 #include <errno.h>
37
38 #include <glib.h>
39
40 #include "../src/shared/netlink.h"
41
42 #define NFGEN_DATA(nlh) ((void *)((char *)(nlh) +                       \
43                                 NLMSG_ALIGN(sizeof(struct nfgenmsg))))
44 #define NLA_DATA(nla)  ((void *)((char*)(nla) + NLA_HDRLEN))
45 #define NLA_OK(nla,len) ((len) >= (int)sizeof(struct nlattr) &&         \
46                                 (nla)->nla_len >= sizeof(struct nlattr) && \
47                                 (nla)->nla_len <= (len))
48 #define NLA_NEXT(nla,attrlen) ((attrlen) -= NLA_ALIGN((nla)->nla_len),  \
49                                 (struct nlattr*)(((char*)(nla)) +       \
50                                                 NLA_ALIGN((nla)->nla_len)))
51
52 static GMainLoop *mainloop;
53
54 static void do_debug(const char *str, void *user_data)
55 {
56         const char *prefix = user_data;
57
58         printf("%s%s\n", prefix, str);
59 }
60
61 static void getlink_callback(unsigned int error, uint16_t type, const void *data,
62                                                 uint32_t len, void *user_data)
63 {
64         const struct ifinfomsg *ifi = data;
65         struct rtattr *rta;
66         int bytes;
67         char ifname[IF_NAMESIZE];
68         uint32_t index, flags;
69
70         g_assert_cmpuint(error, ==, 0);
71
72         bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg));
73
74         memset(ifname, 0, sizeof(ifname));
75
76         index = ifi->ifi_index;
77         flags = ifi->ifi_flags;
78
79         for (rta = IFLA_RTA(ifi); RTA_OK(rta, bytes);
80                                         rta = RTA_NEXT(rta, bytes)) {
81                 switch (rta->rta_type) {
82                 case IFLA_IFNAME:
83                         if (RTA_PAYLOAD(rta) <= IF_NAMESIZE)
84                                 strcpy(ifname, RTA_DATA(rta));
85                         break;
86                 }
87         }
88
89         printf("index=%d flags=0x%08x name=%s\n", index, flags, ifname);
90
91         g_main_loop_quit(mainloop);
92 }
93
94 static void test_case_1(void)
95 {
96         struct netlink_info *netlink;
97         struct ifinfomsg msg;
98
99         netlink = netlink_new(NETLINK_ROUTE);
100
101         printf("\n");
102         netlink_set_debug(netlink, do_debug, "[NETLINK] ", NULL);
103
104         memset(&msg, 0, sizeof(msg));
105
106         netlink_send(netlink, RTM_GETLINK, NLM_F_DUMP, &msg, sizeof(msg),
107                                                 getlink_callback, NULL, NULL);
108
109         mainloop = g_main_loop_new(NULL, FALSE);
110         g_main_loop_run(mainloop);
111         g_main_loop_unref(mainloop);
112
113         netlink_destroy(netlink);
114 }
115
116 int main(int argc, char *argv[])
117 {
118         g_test_init(&argc, &argv, NULL);
119
120         g_test_add_func("/netlink/Test case 1", test_case_1);
121
122         return g_test_run();
123 }