#include
<linux/netlink.h>
#include
<linux/rtnetlink.h>
#include
<linux/genetlink.h>
#include
<linux/netfilter/nfnetlink.h>
#include
"../src/shared/netlink.h"
#define
NFGEN_DATA
(
nlh
) ((void *)((char *)(nlh) + \
NLMSG_ALIGN(sizeof(struct nfgenmsg))))
#define
NLA_DATA
(
nla
) ((void *)((char*)(nla) + NLA_HDRLEN))
#define
NLA_OK
(
nla
,
len
) ((len) >= (int)sizeof(struct nlattr) && \
(nla)->nla_len >= sizeof(struct nlattr) && \
#define
NLA_NEXT
(
nla
,
attrlen
) ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
(struct nlattr*)(((char*)(nla)) + \
NLA_ALIGN((nla)->nla_len)))
static
GMainLoop
*
mainloop
;
static
void
do_debug
(
const
char
*
str
,
void
*
user_data
)
const
char
*
prefix
=
user_data
;
printf
(
"%s%s\n"
,
prefix
,
str
);
static
void
getlink_callback
(
unsigned
int
error
,
uint16_t
type
,
const
void
*
data
,
uint32_t
len
,
void
*
user_data
)
const
struct
ifinfomsg
*
ifi
=
data
;
char
ifname
[
IF_NAMESIZE
];
g_assert_cmpuint
(
error
,
=
=
,
0
);
bytes
=
len
-
NLMSG_ALIGN
(
sizeof
(
struct
ifinfomsg
));
memset
(
ifname
,
0
,
sizeof
(
ifname
));
index
=
ifi
->
ifi_index
;
flags
=
ifi
->
ifi_flags
;
for
(
rta
=
IFLA_RTA
(
ifi
);
RTA_OK
(
rta
,
bytes
);
rta
=
RTA_NEXT
(
rta
,
bytes
)) {
switch
(
rta
->
rta_type
) {
if
(
RTA_PAYLOAD
(
rta
) <=
IF_NAMESIZE
)
strcpy
(
ifname
,
RTA_DATA
(
rta
));
printf
(
"index=%d flags=0x%08x name=%s\n"
,
index
,
flags
,
ifname
);
g_main_loop_quit
(
mainloop
);
static
void
test_case_1
(
void
)
struct
netlink_info
*
netlink
;
netlink
=
netlink_new
(
NETLINK_ROUTE
);
netlink_set_debug
(
netlink
,
do_debug
,
"[NETLINK] "
,
NULL
);
memset
(
&
msg
,
0
,
sizeof
(
msg
));
netlink_send
(
netlink
,
RTM_GETLINK
,
NLM_F_DUMP
,
&
msg
,
sizeof
(
msg
),
getlink_callback
,
NULL
,
NULL
);
mainloop
=
g_main_loop_new
(
NULL
, FALSE);
g_main_loop_run
(
mainloop
);
g_main_loop_unref
(
mainloop
);
netlink_destroy
(
netlink
);
int
main
(
int
argc
,
char
*
argv
[])
g_test_init
(
&
argc
,
&
argv
,
NULL
);
g_test_add_func
(
"/netlink/Test case 1"
,
test_case_1
);