76 static void *SCHInfoAllocUserDataOSPolicy(
const char *host_os)
78 int *user_data = NULL;
80 if ( (user_data =
SCMalloc(
sizeof(
int))) == NULL) {
81 FatalError(
"Error allocating memory. Exiting");
87 "SCHInfoAddHostOSInfo()");
92 return (
void *)user_data;
100 static void SCHInfoFreeUserDataOSPolicy(
void *data)
123 char *ip_str_rem = NULL;
124 struct in_addr *ipv4_addr = NULL;
125 struct in6_addr *ipv6_addr = NULL;
126 char *netmask_str = NULL;
127 uint8_t netmask_value = 0;
128 int *user_data = NULL;
129 bool recursive =
false;
131 if (host_os == NULL || host_os_ip_range == NULL ||
132 strlen(host_os_ip_range) == 0) {
138 if (sc_hinfo_tree == NULL)
142 if ( (user_data = SCHInfoAllocUserDataOSPolicy(host_os)) == NULL) {
149 if ( (strcasecmp(host_os_ip_range,
"default")) == 0) {
151 host_os_ip_range =
"0.0.0.0/0";
153 host_os_ip_range =
"::/0";
156 if ( (ip_str =
SCStrdup(host_os_ip_range)) == NULL) {
161 if ((ip_str_rem = strchr(ip_str,
',')) != NULL) {
162 ip_str_rem[0] =
'\0';
168 if ( (netmask_str = strchr(ip_str,
'/')) != NULL) {
169 netmask_str[0] =
'\0';
173 if (strchr(ip_str,
':') == NULL) {
177 SCHInfoFreeUserDataOSPolicy(user_data);
182 if (netmask_str == NULL) {
189 SCHInfoFreeUserDataOSPolicy(user_data);
197 (
void *)user_data, netmask_value);
203 SCHInfoFreeUserDataOSPolicy(user_data);
208 if (netmask_str == NULL) {
215 SCHInfoFreeUserDataOSPolicy(user_data);
223 (
void *)user_data, netmask_value);
232 if (ipv4_addr != NULL)
234 if (ipv6_addr != NULL)
248 struct in_addr *ipv4_addr = NULL;
249 struct in6_addr *ipv6_addr = NULL;
250 void *user_data = NULL;
252 if (ip_addr_str == NULL || strchr(ip_addr_str,
'/') != NULL)
255 if (strchr(ip_addr_str,
':') != NULL) {
263 if (user_data == NULL)
266 return *((
int *)user_data);
275 if (user_data == NULL)
278 return *((
int *)user_data);
292 void *user_data = NULL;
294 if (user_data == NULL)
297 return *((
int *)user_data);
310 void *user_data = NULL;
312 if (user_data == NULL)
315 return *((
int *)user_data);
320 if (sc_hinfo_tree != NULL) {
322 sc_hinfo_tree = NULL;
342 if (host->
val != NULL && strchr(host->
val,
':') != NULL)
345 SCLogError(
"Failed to add host \"%s\" with policy \"%s\" to host "
359 static void SCHInfoCreateContextBackup(
void)
361 sc_hinfo_tree_backup = sc_hinfo_tree;
362 sc_hinfo_tree = NULL;
365 static void SCHInfoRestoreContextBackup(
void)
367 sc_hinfo_tree = sc_hinfo_tree_backup;
368 sc_hinfo_tree_backup = NULL;
375 static int SCHInfoTestInvalidOSFlavour01(
void)
377 SCHInfoCreateContextBackup();
433 SCHInfoRestoreContextBackup();
442 static int SCHInfoTestInvalidIPV4Address02(
void)
444 SCHInfoCreateContextBackup();
471 SCHInfoRestoreContextBackup();
480 static int SCHInfoTestInvalidIPV6Address03(
void)
482 SCHInfoCreateContextBackup();
502 "1921.6311:6241:6422:7352:ABBB:DDDD:EEEE/129",
511 SCHInfoRestoreContextBackup();
522 static int SCHInfoTestValidIPV4Address04(
void)
524 SCHInfoCreateContextBackup();
623 SCHInfoRestoreContextBackup();
634 static int SCHInfoTestValidIPV4Address05(
void)
636 SCHInfoCreateContextBackup();
734 if (inet_pton(AF_INET,
"111.162.208.100", &in) < 0) {
749 if (inet_pton(AF_INET,
"111.162.208.200", &in) < 0) {
764 SCHInfoRestoreContextBackup();
775 static int SCHInfoTestValidIPV6Address06(
void)
777 SCHInfoCreateContextBackup();
782 "2351:2512:6211:6246:235A:6242:2352:62AD",
787 "6961:6121:2132:6241:423A:2135:2461:621D",
792 "DD13:613D:F312:62DD:6213:421A:6212:2652",
797 "9891:2131:2151:6426:1342:674D:622F:2342",
802 "3525:2351:4223:6211:2311:2667:6242:2154",
807 "1511:6211:6726:7777:1212:2333:6222:7722",
812 "2666:6222:7222:2335:6223:7722:3425:2362",
817 "8762:2352:6241:7245:EE23:21AD:2312:622C",
822 "6422:EE1A:2621:34AD:2462:432D:642E:E13A",
827 "3521:7622:6241:6242:7277:1234:2352:6234",
832 "2141:6232:6252:2223:7734:2345:6245:6222",
837 "5222:6432:6432:2322:6662:3423:4322:3245",
900 SCHInfoRestoreContextBackup();
911 static int SCHInfoTestValidIPV6Address07(
void)
913 SCHInfoCreateContextBackup();
918 "2351:2512:6211:6246:235A:6242:2352:62AD",
923 "6961:6121:2132:6241:423A:2135:2461:621D",
928 "DD13:613D:F312:62DD:6213:421A:6212:2652",
933 "9891:2131:2151:6426:1342:674D:622F:2342",
938 "3525:2351:4223:6211:2311:2667:6242:2154",
943 "1511:6211:6726:7777:1212:2333:6222:7722",
948 "2666:6222:7222:2335:6223:7722:3425:2362",
953 "8762:2352:6241:7245:EE23:21AD:2312:622C/68",
958 "8762:2352:6241:7245:EE23:21AD:2412:622C",
963 "8762:2352:6241:7245:EE23:21AD:FFFF:622C",
968 "8762:2352:6241:7245:EE23:21AD:2312:62FF",
973 "8762:2352:6241:7245:EE23:21AD:2121:1212",
1056 SCHInfoRestoreContextBackup();
1067 static int SCHInfoTestValidIPV6Address08(
void)
1069 SCHInfoCreateContextBackup();
1071 struct in6_addr in6;
1075 "2351:2512:6211:6246:235A:6242:2352:62AD",
1080 "6961:6121:2132:6241:423A:2135:2461:621D",
1085 "DD13:613D:F312:62DD:6213:421A:6212:2652",
1090 "9891:2131:2151:6426:1342:674D:622F:2342",
1095 "3525:2351:4223:6211:2311:2667:6242:2154",
1100 "1511:6211:6726:7777:1212:2333:6222:7722",
1105 "2666:6222:7222:2335:6223:7722:3425:2362",
1110 "8762:2352:6241:7245:EE23:21AD:2312:622C/68",
1115 "8762:2352:6241:7245:EE23:21AD:2412:622C",
1120 "8762:2352:6241:7245:EE23:21AD:FFFF:622C",
1125 "8762:2352:6241:7245:EE23:21AD:2312:62FF",
1130 "8762:2352:6241:7245:EE23:21AD:2121:1212",
1213 if (inet_pton(AF_INET6,
"8762:2352:6241:7245:E000:0000:0000:0000", &in6) < 0) {
1220 if (inet_pton(AF_INET6,
"AD23:2DDA:6D1D:A223:E235:0232:1241:1666", &in6) < 0) {
1235 SCHInfoRestoreContextBackup();
1246 static int SCHInfoTestValidIPV4Address09(
void)
1248 SCHInfoCreateContextBackup();
1292 struct sockaddr_in servaddr;
1293 memset(&servaddr, 0,
sizeof(servaddr));
1294 if (inet_pton(AF_INET,
"192.168.0.0", &servaddr.sin_addr) <= 0) {
1305 memset(&servaddr, 0,
sizeof(servaddr));
1306 if (inet_pton(AF_INET,
"192.168.0.0", &servaddr.sin_addr) <= 0) {
1329 memset(&servaddr, 0,
sizeof(servaddr));
1330 if (inet_pton(AF_INET,
"192.168.0.0", &servaddr.sin_addr) <= 0) {
1341 memset(&servaddr, 0,
sizeof(servaddr));
1342 if (inet_pton(AF_INET,
"192.168.0.0", &servaddr.sin_addr) <= 0) {
1355 SCHInfoRestoreContextBackup();
1363 static int SCHInfoTestLoadFromConfig01(
void)
1370 windows: [10.0.0.0/8, 192.168.1.0/24]\n\
1371 linux: [10.0.0.5/32]\n\
1376 SCHInfoCreateContextBackup();
1398 SCHInfoRestoreContextBackup();
1406 static int SCHInfoTestLoadFromConfig02(
void)
1412 one-two: [0.0.0.0/0]\n\
1415 six-seven_eight: [10.0.0.0/8, 192.168.1.0/24]\n\
1416 nine_ten_eleven: [10.0.0.5/32]\n\
1421 SCHInfoCreateContextBackup();
1437 if (strcmp(
"one-two", policy->
name) != 0)
1441 if (strcmp(
"one-two-three", policy->
name) != 0)
1445 if (strcmp(
"four-five", policy->
name) != 0)
1449 if (strcmp(
"six-seven-eight", policy->
name) != 0)
1453 if (strcmp(
"nine-ten-eleven", policy->
name) != 0)
1466 SCHInfoRestoreContextBackup();
1474 static int SCHInfoTestLoadFromConfig03(
void)
1480 bsd-right: [0.0.0.1]\n\
1481 old-linux: [0.0.0.2]\n\
1482 old-solaris: [0.0.0.3]\n\
1483 windows: [0.0.0.4]\n\
1489 SCHInfoCreateContextBackup();
1502 printf(
"Invalid enum map inside\n");
1513 SCHInfoRestoreContextBackup();
1520 static int SCHInfoTestLoadFromConfig04(
void)
1526 bsd_right: [0.0.0.1]\n\
1527 old_linux: [0.0.0.2]\n\
1528 old_solaris: [0.0.0.3]\n\
1529 windows: [0.0.0.4]\n\
1535 SCHInfoCreateContextBackup();
1548 printf(
"Invalid enum map inside\n");
1559 SCHInfoRestoreContextBackup();
1566 static int SCHInfoTestLoadFromConfig05(
void)
1572 bsd_right: [0.0.0.1]\n\
1573 old_linux: [0.0.0.2]\n\
1574 old-solaris: [0.0.0.3]\n\
1575 windows: [0.0.0.4]\n\
1579 SCHInfoCreateContextBackup();
1596 SCHInfoRestoreContextBackup();
1608 SCHInfoTestInvalidOSFlavour01);
1610 SCHInfoTestInvalidIPV4Address02);
1612 SCHInfoTestInvalidIPV6Address03);
1614 SCHInfoTestValidIPV4Address04);
1616 SCHInfoTestValidIPV4Address05);
1618 SCHInfoTestValidIPV6Address06);
1620 SCHInfoTestValidIPV6Address07);
1622 SCHInfoTestValidIPV6Address08);
1624 SCHInfoTestValidIPV4Address09);
1626 UtRegisterTest(
"SCHInfoTestLoadFromConfig01", SCHInfoTestLoadFromConfig01);
1627 UtRegisterTest(
"SCHInfoTestLoadFromConfig02", SCHInfoTestLoadFromConfig02);
1628 UtRegisterTest(
"SCHInfoTestLoadFromConfig03", SCHInfoTestLoadFromConfig03);
1629 UtRegisterTest(
"SCHInfoTestLoadFromConfig04", SCHInfoTestLoadFromConfig04);
1630 UtRegisterTest(
"SCHInfoTestLoadFromConfig05", SCHInfoTestLoadFromConfig05);