acl/lib_acl/samples/ifconf/main.cpp

88 lines
1.9 KiB
C++
Raw Normal View History

2014-11-19 00:25:21 +08:00
#include "lib_acl.h"
2018-09-13 15:14:58 +08:00
static void test1(void)
2014-11-19 00:25:21 +08:00
{
ACL_IFCONF *ifconf; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
ACL_IFADDR *ifaddr; /* ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD> */
ACL_ITER iter; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
/* <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ */
ifconf = acl_get_ifaddrs();
if (ifconf == NULL) {
printf("acl_get_ifaddrs error: %s\r\n", acl_last_serror());
2018-09-13 15:14:58 +08:00
return;
2014-11-19 00:25:21 +08:00
}
2018-09-13 15:14:58 +08:00
printf("acl_get_ifaddrs:\r\n");
2014-11-19 00:25:21 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ */
acl_foreach(iter, ifconf) {
ifaddr = (ACL_IFADDR*) iter.data;
2018-09-13 18:38:03 +08:00
printf(">>>name=%s, addr=%s, type=%s\r\n",
ifaddr->name, ifaddr->addr,
ifaddr->saddr.sa.sa_family == AF_INET ?
"AF_INET" : (ifaddr->saddr.sa.sa_family == AF_INET6 ?
"AF_INET6" : "unknown"));
2014-11-19 00:25:21 +08:00
}
/* <20>ͷŲ<CDB7>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD> */
acl_free_ifaddrs(ifconf);
2018-09-13 15:14:58 +08:00
}
2014-11-19 00:25:21 +08:00
2018-09-13 15:14:58 +08:00
static void test2(const char *pattern)
{
2018-09-13 18:38:03 +08:00
ACL_ITER iter; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
ACL_IFCONF *ifconf;
2018-09-13 18:38:03 +08:00
ifconf = acl_ifconf_search(pattern);
if (ifconf == NULL) {
printf("acl_ifconf_search error\r\n");
2018-09-13 15:14:58 +08:00
return;
}
printf("pattern=%s\r\n", pattern);
2018-09-13 18:38:03 +08:00
acl_foreach(iter, ifconf) {
const ACL_IFADDR *ifaddr = (const ACL_IFADDR *) iter.data;
const char *type;
if (ifaddr->saddr.sa.sa_family == AF_INET)
type = "AF_INET";
else if (ifaddr->saddr.sa.sa_family == AF_INET6)
type = "AF_INET6";
else
type = "unknown";
printf(">>>name=%s, addr=%s, type=%s\r\n",
ifaddr->name, ifaddr->addr, type);
}
2018-09-13 15:14:58 +08:00
2018-09-13 18:38:03 +08:00
acl_free_ifaddrs(ifconf);
2018-09-13 15:14:58 +08:00
}
2018-09-13 15:14:58 +08:00
int main(void)
{
const char *patterns[] = {
"8190",
"#8190",
":8190",
"*#8190",
"*.*.*.*:8190",
"0.0.0.0:8190",
"127.*.*.*:8290, 192.168.*.*:8291, 172.16.*.*.:8292, 172.17.*.*.:8293, /unix_server@unix",
"127.*.*.*:8290, 0.0.0.0:8191, *.*.*.*:8292",
":8191, #8192, *:8193, *#8194",
NULL,
};
int i;
test1();
for (i = 0; patterns[i]; i++) {
if (i >= 0) {
printf("\r\n-----------------------------------\r\n");
test2(patterns[i]);
}
}
2014-11-19 00:25:21 +08:00
return 0;
}