suricata
util-host-os-info.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
22  *
23  * Host info utility functions
24  */
25 
26 #include "suricata-common.h"
27 #include "util-host-os-info.h"
28 #include "util-error.h"
29 #include "util-debug.h"
30 #include "util-ip.h"
31 #include "util-radix-tree.h"
32 #include "util-byte.h"
33 #include "stream-tcp-private.h"
34 #include "stream-tcp-reassemble.h"
35 
36 #include "conf.h"
37 #include "conf-yaml-loader.h"
38 
39 #include "util-enum.h"
40 #include "util-unittest.h"
41 
42 /** Enum map for the various OS flavours */
44  { "none", OS_POLICY_NONE },
45  { "bsd", OS_POLICY_BSD },
46  { "bsd-right", OS_POLICY_BSD_RIGHT },
47  { "old-linux", OS_POLICY_OLD_LINUX },
48  { "linux", OS_POLICY_LINUX },
49  { "old-solaris", OS_POLICY_OLD_SOLARIS },
50  { "solaris", OS_POLICY_SOLARIS },
51  { "hpux10", OS_POLICY_HPUX10 },
52  { "hpux11", OS_POLICY_HPUX11 },
53  { "irix", OS_POLICY_IRIX },
54  { "macos", OS_POLICY_MACOS },
55  { "windows", OS_POLICY_WINDOWS },
56  { "vista", OS_POLICY_VISTA },
57  { "windows2k3", OS_POLICY_WINDOWS2K3 },
58  { NULL, -1 },
59 };
60 
61 /** Radix tree that holds the host OS information */
62 static SCRadixTree *sc_hinfo_tree = NULL;
63 
64 
65 /**
66  * \brief Allocates the host_os flavour wrapped in user_data variable to be sent
67  * along with the key to the radix tree
68  *
69  * \param host_os Pointer to a character string containing the host_os flavour
70  *
71  * \retval user_data On success, pointer to the user_data that has to be sent
72  * along with the key, to be added to the Radix tree; NULL on
73  * failure
74  * \initonly
75  */
76 static void *SCHInfoAllocUserDataOSPolicy(const char *host_os)
77 {
78  int *user_data = NULL;
79 
80  if ( (user_data = SCMalloc(sizeof(int))) == NULL) {
81  FatalError(SC_ERR_FATAL, "Error allocating memory. Exiting");
82  }
83 
84  /* the host os flavour that has to be sent as user data */
85  if ( (*user_data = SCMapEnumNameToValue(host_os, sc_hinfo_os_policy_map)) == -1) {
86  SCLogError(SC_ERR_INVALID_ENUM_MAP, "Invalid enum map inside "
87  "SCHInfoAddHostOSInfo()");
88  SCFree(user_data);
89  return NULL;
90  }
91 
92  return (void *)user_data;
93 }
94 
95 /**
96  * \brief Used to free the user data that is allocated by host_os_info API
97  *
98  * \param Pointer to the data that has to be freed
99  */
100 static void SCHInfoFreeUserDataOSPolicy(void *data)
101 {
102  if (data != NULL)
103  SCFree(data);
104 
105  return;
106 }
107 
108 /**
109  * \brief Used to add the host-os-info data obtained from the conf
110  *
111  * \param host_os The host_os name/flavour from the conf file
112  * \param host_os_ip_range Pointer to a char string holding the ip/ip_netblock
113  * for the host_os specified in the first argument
114  * \param is_ipv4 Indicates if the ip address to be considered for the
115  * default configuration is IPV4; if not it is IPV6.
116  * Specified using SC_HINFO_IS_IPV6 or SC_HINFO_IS_IPV4
117  *
118  * \retval 0 On successfully adding the host os info to the Radix tree
119  * \retval -1 On failure
120  * \initonly (only specified from config, at the startup)
121  */
122 int SCHInfoAddHostOSInfo(const char *host_os, const char *host_os_ip_range, int is_ipv4)
123 {
124  char *ip_str = NULL;
125  char *ip_str_rem = NULL;
126  struct in_addr *ipv4_addr = NULL;
127  struct in6_addr *ipv6_addr = NULL;
128  char *netmask_str = NULL;
129  int netmask_value = 0;
130  int *user_data = NULL;
131  char recursive = FALSE;
132 
133  if (host_os == NULL || host_os_ip_range == NULL ||
134  strlen(host_os_ip_range) == 0) {
135  SCLogError(SC_ERR_INVALID_ARGUMENT, "Invalid arguments");
136  return -1;
137  }
138 
139  /* create the radix tree that would hold all the host os info */
140  if (sc_hinfo_tree == NULL)
141  sc_hinfo_tree = SCRadixCreateRadixTree(SCHInfoFreeUserDataOSPolicy, NULL);
142 
143  /* the host os flavour that has to be sent as user data */
144  if ( (user_data = SCHInfoAllocUserDataOSPolicy(host_os)) == NULL) {
145  SCLogError(SC_ERR_INVALID_ENUM_MAP, "Invalid enum map inside");
146  return -1;
147  }
148 
149  /* if we have a default configuration set the appropriate values for the
150  * netblocks */
151  if ( (strcasecmp(host_os_ip_range, "default")) == 0) {
152  if (is_ipv4)
153  host_os_ip_range = "0.0.0.0/0";
154  else
155  host_os_ip_range = "::/0";
156  }
157 
158  if ( (ip_str = SCStrdup(host_os_ip_range)) == NULL) {
159  FatalError(SC_ERR_FATAL, "Error allocating memory");
160  }
161 
162  /* check if we have more addresses in the host_os_ip_range */
163  if ((ip_str_rem = strchr(ip_str, ',')) != NULL) {
164  ip_str_rem[0] = '\0';
165  ip_str_rem++;
166  recursive = TRUE;
167  }
168 
169  /* check if we have received a netblock */
170  if ( (netmask_str = strchr(ip_str, '/')) != NULL) {
171  netmask_str[0] = '\0';
172  netmask_str++;
173  }
174 
175  if (strchr(ip_str, ':') == NULL) {
176  /* if we are here, we have an IPV4 address */
177  if ( (ipv4_addr = ValidateIPV4Address(ip_str)) == NULL) {
178  SCLogError(SC_ERR_INVALID_IPV4_ADDR, "Invalid IPV4 address");
179  SCHInfoFreeUserDataOSPolicy(user_data);
180  SCFree(ip_str);
181  return -1;
182  }
183 
184  if (netmask_str == NULL) {
185  SCRadixAddKeyIPV4((uint8_t *)ipv4_addr, sc_hinfo_tree,
186  (void *)user_data);
187  } else {
188  if (StringParseI32RangeCheck(&netmask_value, 10, 0, (const char *)netmask_str, 0, 32) < 0) {
189  SCLogError(SC_ERR_INVALID_IP_NETBLOCK, "Invalid IPV4 Netblock");
190  SCHInfoFreeUserDataOSPolicy(user_data);
191  SCFree(ipv4_addr);
192  SCFree(ip_str);
193  return -1;
194  }
195 
196  MaskIPNetblock((uint8_t *)ipv4_addr, netmask_value, 32);
197  SCRadixAddKeyIPV4Netblock((uint8_t *)ipv4_addr, sc_hinfo_tree,
198  (void *)user_data, netmask_value);
199  }
200  } else {
201  /* if we are here, we have an IPV6 address */
202  if ( (ipv6_addr = ValidateIPV6Address(ip_str)) == NULL) {
203  SCLogError(SC_ERR_INVALID_IPV6_ADDR, "Invalid IPV6 address inside");
204  SCHInfoFreeUserDataOSPolicy(user_data);
205  SCFree(ip_str);
206  return -1;
207  }
208 
209  if (netmask_str == NULL) {
210  SCRadixAddKeyIPV6((uint8_t *)ipv6_addr, sc_hinfo_tree,
211  (void *)user_data);
212  } else {
213  if (StringParseI32RangeCheck(&netmask_value, 10, 0, (const char *)netmask_str, 0, 128) < 0) {
214  SCLogError(SC_ERR_INVALID_IP_NETBLOCK, "Invalid IPV6 Netblock");
215  SCHInfoFreeUserDataOSPolicy(user_data);
216  SCFree(ipv6_addr);
217  SCFree(ip_str);
218  return -1;
219  }
220 
221  MaskIPNetblock((uint8_t *)ipv6_addr, netmask_value, 128);
222  SCRadixAddKeyIPV6Netblock((uint8_t *)ipv6_addr, sc_hinfo_tree,
223  (void *)user_data, netmask_value);
224  }
225  }
226 
227  if (recursive == TRUE) {
228  SCHInfoAddHostOSInfo(host_os, ip_str_rem, is_ipv4);
229  }
230 
231  SCFree(ip_str);
232  if (ipv4_addr != NULL)
233  SCFree(ipv4_addr);
234  if (ipv6_addr != NULL)
235  SCFree(ipv6_addr);
236  return *user_data;
237 }
238 
239 /**
240  * \brief Retrieves the host os flavour, given an ipv4/ipv6 address as a string.
241  *
242  * \param Pointer to a string containing an IP address
243  *
244  * \retval The OS flavour on success; -1 on failure, or on not finding the key
245  */
246 int SCHInfoGetHostOSFlavour(const char *ip_addr_str)
247 {
248  struct in_addr *ipv4_addr = NULL;
249  struct in6_addr *ipv6_addr = NULL;
250  void *user_data = NULL;
251 
252  if (ip_addr_str == NULL || strchr(ip_addr_str, '/') != NULL)
253  return -1;
254 
255  if (strchr(ip_addr_str, ':') != NULL) {
256  if ( (ipv6_addr = ValidateIPV6Address(ip_addr_str)) == NULL) {
257  SCLogError(SC_ERR_INVALID_IPV4_ADDR, "Invalid IPV4 address");
258  return -1;
259  }
260 
261  (void)SCRadixFindKeyIPV6BestMatch((uint8_t *)ipv6_addr, sc_hinfo_tree, &user_data);
262  SCFree(ipv6_addr);
263  if (user_data == NULL)
264  return -1;
265  else
266  return *((int *)user_data);
267  } else {
268  if ( (ipv4_addr = ValidateIPV4Address(ip_addr_str)) == NULL) {
269  SCLogError(SC_ERR_INVALID_IPV4_ADDR, "Invalid IPV4 address");
270  return -1;
271  }
272 
273  (void)SCRadixFindKeyIPV4BestMatch((uint8_t *)ipv4_addr, sc_hinfo_tree, &user_data);
274  SCFree(ipv4_addr);
275  if (user_data == NULL)
276  return -1;
277  else
278  return *((int *)user_data);
279  }
280 }
281 
282 /**
283  * \brief Retrieves the host os flavour, given an ipv4 address in the raw
284  * address format.
285  *
286  * \param Pointer to a raw ipv4 address.
287  *
288  * \retval The OS flavour on success; -1 on failure, or on not finding the key
289  */
290 int SCHInfoGetIPv4HostOSFlavour(uint8_t *ipv4_addr)
291 {
292  void *user_data = NULL;
293  (void)SCRadixFindKeyIPV4BestMatch(ipv4_addr, sc_hinfo_tree, &user_data);
294  if (user_data == NULL)
295  return -1;
296  else
297  return *((int *)user_data);
298 }
299 
300 /**
301  * \brief Retrieves the host os flavour, given an ipv6 address in the raw
302  * address format.
303  *
304  * \param Pointer to a raw ipv6 address.
305  *
306  * \retval The OS flavour on success; -1 on failure, or on not finding the key
307  */
308 int SCHInfoGetIPv6HostOSFlavour(uint8_t *ipv6_addr)
309 {
310  void *user_data = NULL;
311  (void)SCRadixFindKeyIPV6BestMatch(ipv6_addr, sc_hinfo_tree, &user_data);
312  if (user_data == NULL)
313  return -1;
314  else
315  return *((int *)user_data);
316 }
317 
319 {
320  if (sc_hinfo_tree != NULL) {
321  SCRadixReleaseRadixTree(sc_hinfo_tree);
322  sc_hinfo_tree = NULL;
323  }
324 
325  return;
326 }
327 
328 /**
329  * \brief Load the host os policy information from the configuration.
330  *
331  * \initonly (A mem alloc error should cause an exit failure)
332  */
334 {
335  ConfNode *root = ConfGetNode("host-os-policy");
336  if (root == NULL)
337  return;
338 
339  ConfNode *policy;
340  TAILQ_FOREACH(policy, &root->head, next) {
341  ConfNode *host;
342  TAILQ_FOREACH(host, &policy->head, next) {
343  int is_ipv4 = 1;
344  if (host->val != NULL && strchr(host->val, ':') != NULL)
345  is_ipv4 = 0;
346  if (SCHInfoAddHostOSInfo(policy->name, host->val, is_ipv4) == -1) {
348  "Failed to add host \"%s\" with policy \"%s\" to host "
349  "info database", host->val, policy->name);
350  exit(EXIT_FAILURE);
351  }
352  }
353  }
354 }
355 
356 /*------------------------------------Unit_Tests------------------------------*/
357 
358 #ifdef UNITTESTS
359 static SCRadixTree *sc_hinfo_tree_backup = NULL;
360 
361 static void SCHInfoCreateContextBackup(void)
362 {
363  sc_hinfo_tree_backup = sc_hinfo_tree;
364  sc_hinfo_tree = NULL;
365 
366  return;
367 }
368 
369 static void SCHInfoRestoreContextBackup(void)
370 {
371  sc_hinfo_tree = sc_hinfo_tree_backup;
372  sc_hinfo_tree_backup = NULL;
373 
374  return;
375 }
376 
377 /**
378  * \test Check if we the IPs with the right OS flavours are added to the host OS
379  * radix tree, and the IPS with invalid flavours returns an error(-1)
380  */
381 static int SCHInfoTestInvalidOSFlavour01(void)
382 {
383  SCHInfoCreateContextBackup();
384 
385  int result = 0;
386 
387  if (SCHInfoAddHostOSInfo("bamboo", "192.168.1.1", SC_HINFO_IS_IPV4) != -1) {
388  goto end;
389  }
390  if (SCHInfoAddHostOSInfo("linux", "192.168.1.1", SC_HINFO_IS_IPV4) !=
392  goto end;
393  }
394  if (SCHInfoAddHostOSInfo("windows", "192.168.1.1", SC_HINFO_IS_IPV4) !=
396  goto end;
397  }
398  if (SCHInfoAddHostOSInfo("solaris", "192.168.1.1", SC_HINFO_IS_IPV4) !=
400  goto end;
401  }
402  if (SCHInfoAddHostOSInfo("hpux10", "192.168.1.1", SC_HINFO_IS_IPV4) !=
404  goto end;
405  }
406  if (SCHInfoAddHostOSInfo("hpux11", "192.168.1.1", SC_HINFO_IS_IPV4) !=
408  goto end;
409  }
410  if (SCHInfoAddHostOSInfo("irix", "192.168.1.1", SC_HINFO_IS_IPV4) !=
412  goto end;
413  }
414  if (SCHInfoAddHostOSInfo("bsd", "192.168.1.1", SC_HINFO_IS_IPV4) !=
416  goto end;
417  }
418  if (SCHInfoAddHostOSInfo("old_linux", "192.168.1.1", SC_HINFO_IS_IPV4) !=
420  goto end;
421  }
422  if (SCHInfoAddHostOSInfo("macos", "192.168.1.1", SC_HINFO_IS_IPV4) !=
424  goto end;
425  }
426  if (SCHInfoAddHostOSInfo("vista", "192.168.1.1", SC_HINFO_IS_IPV4) !=
428  goto end;
429  }
430  if (SCHInfoAddHostOSInfo("windows2k3", "192.168.1.1", SC_HINFO_IS_IPV4) !=
432  goto end;
433  }
434 
435  result = 1;
436 
437  end:
439  SCHInfoRestoreContextBackup();
440 
441  return result;
442 }
443 
444 /**
445  * \test Check that invalid ipv4 addresses and ipv4 netblocks are rejected by
446  * the host os info API
447  */
448 static int SCHInfoTestInvalidIPV4Address02(void)
449 {
450  SCHInfoCreateContextBackup();
451 
452  int result = 0;
453 
454  if (SCHInfoAddHostOSInfo("linux", "192.168.1.566", SC_HINFO_IS_IPV4) != -1) {
455  goto end;
456  }
457  if (SCHInfoAddHostOSInfo("linux", "192.168.1", SC_HINFO_IS_IPV4) != -1) {
458  goto end;
459  }
460  if (SCHInfoAddHostOSInfo("linux", "192.", SC_HINFO_IS_IPV4) != -1) {
461  goto end;
462  }
463  if (SCHInfoAddHostOSInfo("linux", "192.168", SC_HINFO_IS_IPV4) != -1) {
464  goto end;
465  }
466  if (SCHInfoAddHostOSInfo("linux", "", SC_HINFO_IS_IPV4) != -1) {
467  goto end;
468  }
469  if (SCHInfoAddHostOSInfo("linux", "192.168.1.1/33", SC_HINFO_IS_IPV4) != -1) {
470  goto end;
471  }
472 
473  result = 1;
474 
475  end:
477  SCHInfoRestoreContextBackup();
478 
479  return result;
480 }
481 
482 /**
483  * \test Check that invalid ipv4 addresses and ipv4 netblocks are rejected by
484  * the host os info API
485  */
486 static int SCHInfoTestInvalidIPV6Address03(void)
487 {
488  SCHInfoCreateContextBackup();
489 
490  int result = 0;
491 
492  if (SCHInfoAddHostOSInfo("linux", "2362:7322", SC_HINFO_IS_IPV6) != -1) {
493  goto end;
494  }
495  if (SCHInfoAddHostOSInfo("linux", "19YW:", SC_HINFO_IS_IPV6) != -1) {
496  goto end;
497  }
498  if (SCHInfoAddHostOSInfo("linux", "1235", SC_HINFO_IS_IPV6) != -1) {
499  goto end;
500  }
501  if (SCHInfoAddHostOSInfo("linux", "1922:236115:", SC_HINFO_IS_IPV6) != -1) {
502  goto end;
503  }
504  if (SCHInfoAddHostOSInfo("linux", "", SC_HINFO_IS_IPV6) != -1) {
505  goto end;
506  }
507  if (SCHInfoAddHostOSInfo("linux",
508  "1921.6311:6241:6422:7352:ABBB:DDDD:EEEE/129",
509  SC_HINFO_IS_IPV6) != -1) {
510  goto end;
511  }
512 
513  result = 1;
514 
515  end:
517  SCHInfoRestoreContextBackup();
518 
519  return result;
520 }
521 
522 /**
523  * \test Check that valid ipv4 addresses are inserted into the host os radix
524  * tree, and the host os api retrieves the right value for the host os
525  * flavour, on supplying as arg an ipv4 addresses that has been added to
526  * the host os radix tree.
527  */
528 static int SCHInfoTestValidIPV4Address04(void)
529 {
530  SCHInfoCreateContextBackup();
531 
532  int result = 0;
533 
534  if (SCHInfoAddHostOSInfo("linux", "192.168.1.1", SC_HINFO_IS_IPV4) == -1) {
535  goto end;
536  }
537  if (SCHInfoAddHostOSInfo("windows", "192.192.1.2", SC_HINFO_IS_IPV4) == -1) {
538  goto end;
539  }
540  if (SCHInfoAddHostOSInfo("solaris", "192.168.1.100", SC_HINFO_IS_IPV4) == -1) {
541  goto end;
542  }
543  if (SCHInfoAddHostOSInfo("hpux10", "192.168.2.4", SC_HINFO_IS_IPV4) == -1) {
544  goto end;
545  }
546  if (SCHInfoAddHostOSInfo("linux", "192.192.1.5", SC_HINFO_IS_IPV4) == -1) {
547  goto end;
548  }
549  if (SCHInfoAddHostOSInfo("vista", "192.168.10.20", SC_HINFO_IS_IPV4) == -1) {
550  goto end;
551  }
552  if (SCHInfoAddHostOSInfo("solaris", "111.163.151.62", SC_HINFO_IS_IPV4) == -1) {
553  goto end;
554  }
555  if (SCHInfoAddHostOSInfo("solaris", "11.1.120.210", SC_HINFO_IS_IPV4) == -1) {
556  goto end;
557  }
558  if (SCHInfoAddHostOSInfo("linux", "19.18.110.210", SC_HINFO_IS_IPV4) == -1) {
559  goto end;
560  }
561  if (SCHInfoAddHostOSInfo("windows", "19.18.120.110", SC_HINFO_IS_IPV4) == -1) {
562  goto end;
563  }
564  if (SCHInfoAddHostOSInfo("hpux11", "191.168.11.128", SC_HINFO_IS_IPV4) == -1) {
565  goto end;
566  }
567  if (SCHInfoAddHostOSInfo("vista", "191.168.11.192", SC_HINFO_IS_IPV4) == -1) {
568  goto end;
569  }
570 
571  if (SCHInfoGetHostOSFlavour("192.168.1.1") !=
573  goto end;
574  }
575  if (SCHInfoGetHostOSFlavour("192.168.1.2") != -1) {
576  goto end;
577  }
578  if (SCHInfoGetHostOSFlavour("192.168.1.100") !=
580  goto end;
581  }
582  if (SCHInfoGetHostOSFlavour("192.192.2.4") != -1) {
583  goto end;
584  }
585  if (SCHInfoGetHostOSFlavour("192.168.2.4") !=
587  goto end;
588  }
589  if (SCHInfoGetHostOSFlavour("192.192.1.5") !=
591  goto end;
592  }
593  if (SCHInfoGetHostOSFlavour("192.168.10.20") !=
595  goto end;
596  }
597  if (SCHInfoGetHostOSFlavour("111.163.151.62") !=
599  goto end;
600  }
601  if (SCHInfoGetHostOSFlavour("11.1.120.210") !=
603  goto end;
604  }
605  if (SCHInfoGetHostOSFlavour("19.18.110.210") !=
607  goto end;
608  }
609  if (SCHInfoGetHostOSFlavour("19.18.120.110") !=
611  goto end;
612  }
613  if (SCHInfoGetHostOSFlavour("191.168.11.128") !=
615  goto end;
616  }
617  if (SCHInfoGetHostOSFlavour("191.168.11.192") !=
619  goto end;
620  }
621  if (SCHInfoGetHostOSFlavour("191.168.11.224") != -1) {
622  goto end;
623  }
624 
625  result = 1;
626 
627  end:
629  SCHInfoRestoreContextBackup();
630 
631  return result;
632 }
633 
634 /**
635  * \test Check that valid ipv4 addresses/netblocks are inserted into the host os
636  * radix tree, and the host os api retrieves the right value for the host
637  * os flavour, on supplying as arg an ipv4 addresses that has been added
638  * to the host os radix tree.
639  */
640 static int SCHInfoTestValidIPV4Address05(void)
641 {
642  SCHInfoCreateContextBackup();
643 
644  struct in_addr in;
645  int result = 0;
646 
647  if (SCHInfoAddHostOSInfo("linux", "192.168.1.1", SC_HINFO_IS_IPV4) == -1) {
648  goto end;
649  }
650  if (SCHInfoAddHostOSInfo("windows", "192.192.1.2", SC_HINFO_IS_IPV4) == -1) {
651  goto end;
652  }
653  if (SCHInfoAddHostOSInfo("solaris", "192.168.1.100", SC_HINFO_IS_IPV4) == -1) {
654  goto end;
655  }
656  if (SCHInfoAddHostOSInfo("hpux10", "192.168.2.4", SC_HINFO_IS_IPV4) == -1) {
657  goto end;
658  }
659  if (SCHInfoAddHostOSInfo("linux", "192.192.1.5", SC_HINFO_IS_IPV4) == -1) {
660  goto end;
661  }
662  if (SCHInfoAddHostOSInfo("vista", "192.168.10.20", SC_HINFO_IS_IPV4) == -1) {
663  goto end;
664  }
665  if (SCHInfoAddHostOSInfo("solaris", "111.163.151.62", SC_HINFO_IS_IPV4) == -1) {
666  goto end;
667  }
668  if (SCHInfoAddHostOSInfo("hpux11", "111.162.208.124/20", SC_HINFO_IS_IPV4) == -1) {
669  goto end;
670  }
671  if (SCHInfoAddHostOSInfo("windows", "111.162.240.1", SC_HINFO_IS_IPV4) == -1) {
672  goto end;
673  }
674  if (SCHInfoAddHostOSInfo("solaris", "111.162.214.100", SC_HINFO_IS_IPV4) == -1) {
675  goto end;
676  }
677  if (SCHInfoAddHostOSInfo("vista", "111.162.208.100", SC_HINFO_IS_IPV4) == -1) {
678  goto end;
679  }
680  if (SCHInfoAddHostOSInfo("linux", "111.162.194.112", SC_HINFO_IS_IPV4) == -1) {
681  goto end;
682  }
683 
684  if (SCHInfoGetHostOSFlavour("192.168.1.1") !=
686  goto end;
687  }
688  if (SCHInfoGetHostOSFlavour("192.168.1.2") != -1) {
689  goto end;
690  }
691  if (SCHInfoGetHostOSFlavour("192.168.1.100") !=
693  goto end;
694  }
695  if (SCHInfoGetHostOSFlavour("192.192.2.4") != -1) {
696  goto end;
697  }
698  if (SCHInfoGetHostOSFlavour("192.168.2.4") !=
700  goto end;
701  }
702  if (SCHInfoGetHostOSFlavour("192.192.1.5") !=
704  goto end;
705  }
706  if (SCHInfoGetHostOSFlavour("192.168.10.20") !=
708  goto end;
709  }
710  if (SCHInfoGetHostOSFlavour("111.163.151.62") !=
712  goto end;
713  }
714  if (SCHInfoGetHostOSFlavour("111.162.208.0") !=
716  goto end;
717  }
718  if (SCHInfoGetHostOSFlavour("111.162.210.1") !=
720  goto end;
721  }
722  if (SCHInfoGetHostOSFlavour("111.162.214.1") !=
724  goto end;
725  }
726  if (SCHInfoGetHostOSFlavour("111.162.0.0") != -1) {
727  goto end;
728  }
729  if (SCHInfoGetHostOSFlavour("111.162.240.112") != -1) {
730  goto end;
731  }
732  if (SCHInfoGetHostOSFlavour("111.162.240.1") !=
734  goto end;
735  }
736  if (SCHInfoGetHostOSFlavour("111.162.214.100") !=
738  goto end;
739  }
740  if (inet_pton(AF_INET, "111.162.208.100", &in) < 0) {
741  goto end;
742  }
743  if (SCHInfoGetIPv4HostOSFlavour((uint8_t *)&in) !=
745  goto end;
746  }
747  if (SCHInfoGetHostOSFlavour("111.162.194.112") !=
749  goto end;
750  }
751  if (SCHInfoGetHostOSFlavour("111.162.208.200") !=
753  goto end;
754  }
755  if (inet_pton(AF_INET, "111.162.208.200", &in) < 0) {
756  goto end;
757  }
758  if (SCHInfoGetIPv4HostOSFlavour((uint8_t *)&in) !=
760  goto end;
761  }
762  if (SCHInfoGetHostOSFlavour("111.162.200.201") != -1) {
763  goto end;
764  }
765 
766  result = 1;
767 
768  end:
770  SCHInfoRestoreContextBackup();
771 
772  return result;
773 }
774 
775 /**
776  * \test Check that valid ipv6 addresses are inserted into the host os radix
777  * tree, and the host os api retrieves the right value for the host os
778  * flavour, on supplying as arg an ipv6 address that has been added to
779  * the host os radix tree.
780  */
781 static int SCHInfoTestValidIPV6Address06(void)
782 {
783  SCHInfoCreateContextBackup();
784 
785  int result = 0;
786 
787  if (SCHInfoAddHostOSInfo("linux",
788  "2351:2512:6211:6246:235A:6242:2352:62AD",
789  SC_HINFO_IS_IPV6) == -1) {
790  goto end;
791  }
792  if (SCHInfoAddHostOSInfo("windows",
793  "6961:6121:2132:6241:423A:2135:2461:621D",
794  SC_HINFO_IS_IPV6) == -1) {
795  goto end;
796  }
797  if (SCHInfoAddHostOSInfo("solaris",
798  "DD13:613D:F312:62DD:6213:421A:6212:2652",
799  SC_HINFO_IS_IPV6) == -1) {
800  goto end;
801  }
802  if (SCHInfoAddHostOSInfo("hpux10",
803  "9891:2131:2151:6426:1342:674D:622F:2342",
804  SC_HINFO_IS_IPV6) == -1) {
805  goto end;
806  }
807  if (SCHInfoAddHostOSInfo("linux",
808  "3525:2351:4223:6211:2311:2667:6242:2154",
809  SC_HINFO_IS_IPV6) == -1) {
810  goto end;
811  }
812  if (SCHInfoAddHostOSInfo("vista",
813  "1511:6211:6726:7777:1212:2333:6222:7722",
814  SC_HINFO_IS_IPV6) == -1) {
815  goto end;
816  }
817  if (SCHInfoAddHostOSInfo("solaris",
818  "2666:6222:7222:2335:6223:7722:3425:2362",
819  SC_HINFO_IS_IPV6) == -1) {
820  goto end;
821  }
822  if (SCHInfoAddHostOSInfo("solaris",
823  "8762:2352:6241:7245:EE23:21AD:2312:622C",
824  SC_HINFO_IS_IPV6) == -1) {
825  goto end;
826  }
827  if (SCHInfoAddHostOSInfo("linux",
828  "6422:EE1A:2621:34AD:2462:432D:642E:E13A",
829  SC_HINFO_IS_IPV6) == -1) {
830  goto end;
831  }
832  if (SCHInfoAddHostOSInfo("windows",
833  "3521:7622:6241:6242:7277:1234:2352:6234",
834  SC_HINFO_IS_IPV6) == -1) {
835  goto end;
836  }
837  if (SCHInfoAddHostOSInfo("hpux11",
838  "2141:6232:6252:2223:7734:2345:6245:6222",
839  SC_HINFO_IS_IPV6) == -1) {
840  goto end;
841  }
842  if (SCHInfoAddHostOSInfo("vista",
843  "5222:6432:6432:2322:6662:3423:4322:3245",
844  SC_HINFO_IS_IPV6) == -1) {
845  goto end;
846  }
847 
848  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:62AD") !=
850  goto end;
851  }
852  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:6FFFE") != -1) {
853  goto end;
854  }
855  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2652") !=
857  goto end;
858  }
859  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2222") != -1) {
860  goto end;
861  }
862  if (SCHInfoGetHostOSFlavour("9891:2131:2151:6426:1342:674D:622F:2342") !=
864  goto end;
865  }
866  if (SCHInfoGetHostOSFlavour("3525:2351:4223:6211:2311:2667:6242:2154") !=
868  goto end;
869  }
870  if (SCHInfoGetHostOSFlavour("1511:6211:6726:7777:1212:2333:6222:7722") !=
872  goto end;
873  }
874  if (SCHInfoGetHostOSFlavour("2666:6222:7222:2335:6223:7722:3425:2362") !=
876  goto end;
877  }
878  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
880  goto end;
881  }
882  if (SCHInfoGetHostOSFlavour("6422:EE1A:2621:34AD:2462:432D:642E:E13A") !=
884  goto end;
885  }
886  if (SCHInfoGetHostOSFlavour("3521:7622:6241:6242:7277:1234:2352:6234") !=
888  goto end;
889  }
890  if (SCHInfoGetHostOSFlavour("2141:6232:6252:2223:7734:2345:6245:6222") !=
892  goto end;
893  }
894  if (SCHInfoGetHostOSFlavour("5222:6432:6432:2322:6662:3423:4322:3245") !=
896  goto end;
897  }
898  if (SCHInfoGetHostOSFlavour("5222:6432:6432:2322:6662:3423:4322:DDDD") != -1) {
899  goto end;
900  }
901 
902  result = 1;
903 
904  end:
906  SCHInfoRestoreContextBackup();
907 
908  return result;
909 }
910 
911 /**
912  * \test Check that valid ipv6 addresses/netblocks are inserted into the host os
913  * radix tree, and the host os api retrieves the right value for the host
914  * os flavour, on supplying as arg an ipv6 address that has been added to
915  * the host os radix tree.
916  */
917 static int SCHInfoTestValidIPV6Address07(void)
918 {
919  SCHInfoCreateContextBackup();
920 
921  int result = 0;
922 
923  if (SCHInfoAddHostOSInfo("linux",
924  "2351:2512:6211:6246:235A:6242:2352:62AD",
925  SC_HINFO_IS_IPV6) == -1) {
926  goto end;
927  }
928  if (SCHInfoAddHostOSInfo("windows",
929  "6961:6121:2132:6241:423A:2135:2461:621D",
930  SC_HINFO_IS_IPV6) == -1) {
931  goto end;
932  }
933  if (SCHInfoAddHostOSInfo("solaris",
934  "DD13:613D:F312:62DD:6213:421A:6212:2652",
935  SC_HINFO_IS_IPV6) == -1) {
936  goto end;
937  }
938  if (SCHInfoAddHostOSInfo("hpux10",
939  "9891:2131:2151:6426:1342:674D:622F:2342",
940  SC_HINFO_IS_IPV6) == -1) {
941  goto end;
942  }
943  if (SCHInfoAddHostOSInfo("linux",
944  "3525:2351:4223:6211:2311:2667:6242:2154",
945  SC_HINFO_IS_IPV6) == -1) {
946  goto end;
947  }
948  if (SCHInfoAddHostOSInfo("vista",
949  "1511:6211:6726:7777:1212:2333:6222:7722",
950  SC_HINFO_IS_IPV6) == -1) {
951  goto end;
952  }
953  if (SCHInfoAddHostOSInfo("solaris",
954  "2666:6222:7222:2335:6223:7722:3425:2362",
955  SC_HINFO_IS_IPV6) == -1) {
956  goto end;
957  }
958  if (SCHInfoAddHostOSInfo("solaris",
959  "8762:2352:6241:7245:EE23:21AD:2312:622C/68",
960  SC_HINFO_IS_IPV6) == -1) {
961  goto end;
962  }
963  if (SCHInfoAddHostOSInfo("linux",
964  "8762:2352:6241:7245:EE23:21AD:2412:622C",
965  SC_HINFO_IS_IPV6) == -1) {
966  goto end;
967  }
968  if (SCHInfoAddHostOSInfo("windows",
969  "8762:2352:6241:7245:EE23:21AD:FFFF:622C",
970  SC_HINFO_IS_IPV6) == -1) {
971  goto end;
972  }
973  if (SCHInfoAddHostOSInfo("hpux11",
974  "8762:2352:6241:7245:EE23:21AD:2312:62FF",
975  SC_HINFO_IS_IPV6) == -1) {
976  goto end;
977  }
978  if (SCHInfoAddHostOSInfo("vista",
979  "8762:2352:6241:7245:EE23:21AD:2121:1212",
980  SC_HINFO_IS_IPV6) == -1) {
981  goto end;
982  }
983 
984  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:62AD") !=
986  goto end;
987  }
988  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:6FFFE") != -1) {
989  goto end;
990  }
991  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2652") !=
993  goto end;
994  }
995  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2222") != -1) {
996  goto end;
997  }
998  if (SCHInfoGetHostOSFlavour("9891:2131:2151:6426:1342:674D:622F:2342") !=
1000  goto end;
1001  }
1002  if (SCHInfoGetHostOSFlavour("3525:2351:4223:6211:2311:2667:6242:2154") !=
1004  goto end;
1005  }
1006  if (SCHInfoGetHostOSFlavour("1511:6211:6726:7777:1212:2333:6222:7722") !=
1008  goto end;
1009  }
1010  if (SCHInfoGetHostOSFlavour("2666:6222:7222:2335:6223:7722:3425:2362") !=
1012  goto end;
1013  }
1014  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
1016  goto end;
1017  }
1018  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2412:622C") !=
1020  goto end;
1021  }
1022  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:FFFF:622C") !=
1024  goto end;
1025  }
1026  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:62FF") !=
1028  goto end;
1029  }
1030  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2121:1212") !=
1032  goto end;
1033  }
1034  if (SCHInfoGetHostOSFlavour("5222:6432:6432:2322:6662:3423:4322:DDDD") != -1) {
1035  goto end;
1036  }
1037  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2121:1DDD") !=
1039  goto end;
1040  }
1041  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:FFFF:2121:1DDD") !=
1043  goto end;
1044  }
1045  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
1047  goto end;
1048  }
1049  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE00:0000:0000:0000") !=
1051  goto end;
1052  }
1053  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:E000:0000:0000:0000") !=
1055  goto end;
1056  }
1057 
1058  result = 1;
1059 
1060  end:
1062  SCHInfoRestoreContextBackup();
1063 
1064  return result;
1065 }
1066 
1067 /**
1068  * \test Check that valid ipv6 addresses/netblocks are inserted into the host os
1069  * radix tree, and the host os api retrieves the right value for the host
1070  * os flavour, on supplying as arg an ipv6 address that has been added to
1071  * the host os radix tree.
1072  */
1073 static int SCHInfoTestValidIPV6Address08(void)
1074 {
1075  SCHInfoCreateContextBackup();
1076 
1077  struct in6_addr in6;
1078  int result = 0;
1079 
1080  if (SCHInfoAddHostOSInfo("linux",
1081  "2351:2512:6211:6246:235A:6242:2352:62AD",
1082  SC_HINFO_IS_IPV6) == -1) {
1083  goto end;
1084  }
1085  if (SCHInfoAddHostOSInfo("windows",
1086  "6961:6121:2132:6241:423A:2135:2461:621D",
1087  SC_HINFO_IS_IPV6) == -1) {
1088  goto end;
1089  }
1090  if (SCHInfoAddHostOSInfo("solaris",
1091  "DD13:613D:F312:62DD:6213:421A:6212:2652",
1092  SC_HINFO_IS_IPV6) == -1) {
1093  goto end;
1094  }
1095  if (SCHInfoAddHostOSInfo("hpux10",
1096  "9891:2131:2151:6426:1342:674D:622F:2342",
1097  SC_HINFO_IS_IPV6) == -1) {
1098  goto end;
1099  }
1100  if (SCHInfoAddHostOSInfo("linux",
1101  "3525:2351:4223:6211:2311:2667:6242:2154",
1102  SC_HINFO_IS_IPV6) == -1) {
1103  goto end;
1104  }
1105  if (SCHInfoAddHostOSInfo("vista",
1106  "1511:6211:6726:7777:1212:2333:6222:7722",
1107  SC_HINFO_IS_IPV6) == -1) {
1108  goto end;
1109  }
1110  if (SCHInfoAddHostOSInfo("solaris",
1111  "2666:6222:7222:2335:6223:7722:3425:2362",
1112  SC_HINFO_IS_IPV6) == -1) {
1113  goto end;
1114  }
1115  if (SCHInfoAddHostOSInfo("solaris",
1116  "8762:2352:6241:7245:EE23:21AD:2312:622C/68",
1117  SC_HINFO_IS_IPV6) == -1) {
1118  goto end;
1119  }
1120  if (SCHInfoAddHostOSInfo("linux",
1121  "8762:2352:6241:7245:EE23:21AD:2412:622C",
1122  SC_HINFO_IS_IPV6) == -1) {
1123  goto end;
1124  }
1125  if (SCHInfoAddHostOSInfo("windows",
1126  "8762:2352:6241:7245:EE23:21AD:FFFF:622C",
1127  SC_HINFO_IS_IPV6) == -1) {
1128  goto end;
1129  }
1130  if (SCHInfoAddHostOSInfo("hpux11",
1131  "8762:2352:6241:7245:EE23:21AD:2312:62FF",
1132  SC_HINFO_IS_IPV6) == -1) {
1133  goto end;
1134  }
1135  if (SCHInfoAddHostOSInfo("vista",
1136  "8762:2352:6241:7245:EE23:21AD:2121:1212",
1137  SC_HINFO_IS_IPV6) == -1) {
1138  goto end;
1139  }
1140  if (SCHInfoAddHostOSInfo("irix", "default", SC_HINFO_IS_IPV6) == -1) {
1141  goto end;
1142  }
1143 
1144  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:62AD") !=
1146  goto end;
1147  }
1148  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:6FFF") !=
1150  goto end;
1151  }
1152  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2652") !=
1154  goto end;
1155  }
1156  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2222") !=
1158  goto end;
1159  }
1160  if (SCHInfoGetHostOSFlavour("9891:2131:2151:6426:1342:674D:622F:2342") !=
1162  goto end;
1163  }
1164  if (SCHInfoGetHostOSFlavour("3525:2351:4223:6211:2311:2667:6242:2154") !=
1166  goto end;
1167  }
1168  if (SCHInfoGetHostOSFlavour("1511:6211:6726:7777:1212:2333:6222:7722") !=
1170  goto end;
1171  }
1172  if (SCHInfoGetHostOSFlavour("2666:6222:7222:2335:6223:7722:3425:2362") !=
1174  goto end;
1175  }
1176  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
1178  goto end;
1179  }
1180  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2412:622C") !=
1182  goto end;
1183  }
1184  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:FFFF:622C") !=
1186  goto end;
1187  }
1188  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:62FF") !=
1190  goto end;
1191  }
1192  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2121:1212") !=
1194  goto end;
1195  }
1196  if (SCHInfoGetHostOSFlavour("5222:6432:6432:2322:6662:3423:4322:DDDD") !=
1198  goto end;
1199  }
1200  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2121:1DDD") !=
1202  goto end;
1203  }
1204  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:FFFF:2121:1DDD") !=
1206  goto end;
1207  }
1208  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
1210  goto end;
1211  }
1212  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE00:0000:0000:0000") !=
1214  goto end;
1215  }
1216  if (inet_pton(AF_INET6, "8762:2352:6241:7245:E000:0000:0000:0000", &in6) < 0) {
1217  goto end;
1218  }
1219  if (SCHInfoGetIPv6HostOSFlavour((uint8_t *)&in6) !=
1221  goto end;
1222  }
1223  if (inet_pton(AF_INET6, "AD23:2DDA:6D1D:A223:E235:0232:1241:1666", &in6) < 0) {
1224  goto end;
1225  }
1226  if (SCHInfoGetIPv6HostOSFlavour((uint8_t *)&in6) !=
1228  goto end;
1229  }
1230 
1231  result = 1;
1232 
1233  end:
1235  SCHInfoRestoreContextBackup();
1236 
1237  return result;
1238 }
1239 
1240 /**
1241  * \test Check that valid ipv4 addresses are inserted into the host os radix
1242  * tree, and the host os api retrieves the right value for the host os
1243  * flavour, on supplying as arg an ipv4 addresses that has been added to
1244  * the host os radix tree.
1245  */
1246 static int SCHInfoTestValidIPV4Address09(void)
1247 {
1248  SCHInfoCreateContextBackup();
1249 
1250  int result = 0;
1251 
1252  if (SCHInfoAddHostOSInfo("linux", "192.168.1.0", SC_HINFO_IS_IPV4) == -1) {
1253  goto end;
1254  }
1255  if (SCHInfoAddHostOSInfo("windows", "192.192.1.2", SC_HINFO_IS_IPV4) == -1) {
1256  goto end;
1257  }
1258  if (SCHInfoGetHostOSFlavour("192.168.1.0") !=
1260  goto end;
1261  }
1262  if (SCHInfoAddHostOSInfo("solaris", "192.168.1.0/16", SC_HINFO_IS_IPV4) == -1) {
1263  goto end;
1264  }
1265  if (SCHInfoAddHostOSInfo("macos", "192.168.1.0/20", SC_HINFO_IS_IPV4) == -1) {
1266  goto end;
1267  }
1268  if (SCHInfoGetHostOSFlavour("192.168.1.0") !=
1270  goto end;
1271  }
1272  if (SCHInfoAddHostOSInfo("vista", "192.168.50.128/25", SC_HINFO_IS_IPV4) == -1) {
1273  goto end;
1274  }
1275  if (SCHInfoGetHostOSFlavour("192.168.50.128") !=
1277  goto end;
1278  }
1279  if (SCHInfoAddHostOSInfo("irix", "192.168.50.128", SC_HINFO_IS_IPV4) == -1) {
1280  goto end;
1281  }
1282  if (SCHInfoGetHostOSFlavour("192.168.50.128") !=
1284  goto end;
1285  }
1286 
1287  if (SCHInfoGetHostOSFlavour("192.168.1.100") !=
1289  goto end;
1290  }
1291 
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) {
1295  goto end;
1296  }
1297 
1298  SCRadixRemoveKeyIPV4Netblock((uint8_t *)&servaddr.sin_addr, sc_hinfo_tree, 16);
1299 
1300  if (SCHInfoGetHostOSFlavour("192.168.1.100") !=
1302  goto end;
1303  }
1304 
1305  memset(&servaddr, 0, sizeof(servaddr));
1306  if (inet_pton(AF_INET, "192.168.0.0", &servaddr.sin_addr) <= 0) {
1307  goto end;
1308  }
1309  SCRadixRemoveKeyIPV4Netblock((uint8_t *)&servaddr.sin_addr, sc_hinfo_tree, 20);
1310 
1311  if (SCHInfoGetHostOSFlavour("192.168.1.100") != -1) {
1312  goto end;
1313  }
1314  if (SCHInfoAddHostOSInfo("solaris", "192.168.1.0/16", SC_HINFO_IS_IPV4) == -1) {
1315  goto end;
1316  }
1317  if (SCHInfoAddHostOSInfo("macos", "192.168.1.0/20", SC_HINFO_IS_IPV4) == -1) {
1318  goto end;
1319  }
1320 
1321  /* 192.168.1.100 should match "macos" as its more specific than
1322  * "solaris". */
1323  if (SCHInfoGetHostOSFlavour("192.168.1.100") !=
1325  goto end;
1326  }
1327 
1328  /* Remove the 192.168.1.0/20 -> macos entry. */
1329  memset(&servaddr, 0, sizeof(servaddr));
1330  if (inet_pton(AF_INET, "192.168.0.0", &servaddr.sin_addr) <= 0) {
1331  goto end;
1332  }
1333  SCRadixRemoveKeyIPV4Netblock((uint8_t *)&servaddr.sin_addr, sc_hinfo_tree, 20);
1334 
1335  if (SCHInfoGetHostOSFlavour("192.168.1.100") !=
1337  goto end;
1338  }
1339 
1340  /* Remove the 192.168.1.0/16 -> solaris entry. */
1341  memset(&servaddr, 0, sizeof(servaddr));
1342  if (inet_pton(AF_INET, "192.168.0.0", &servaddr.sin_addr) <= 0) {
1343  goto end;
1344  }
1345  SCRadixRemoveKeyIPV4Netblock((uint8_t *)&servaddr.sin_addr, sc_hinfo_tree, 16);
1346 
1347  if (SCHInfoGetHostOSFlavour("192.168.1.100") != -1) {
1348  goto end;
1349  }
1350 
1351  result = 1;
1352 
1353  end:
1355  SCHInfoRestoreContextBackup();
1356 
1357  return result;
1358 }
1359 
1360 /**
1361  * \test Check the loading of host info from a configuration file.
1362  */
1363 static int SCHInfoTestLoadFromConfig01(void)
1364 {
1365  char config[] = "\
1366 %YAML 1.1\n\
1367 ---\n\
1368 host-os-policy:\n\
1369  bsd: [0.0.0.0/0]\n\
1370  windows: [10.0.0.0/8, 192.168.1.0/24]\n\
1371  linux: [10.0.0.5/32]\n\
1372 \n";
1373 
1374  int result = 0;
1375 
1376  SCHInfoCreateContextBackup();
1377 
1379  ConfInit();
1380  ConfYamlLoadString(config, strlen(config));
1381 
1383  if (SCHInfoGetHostOSFlavour("10.0.0.4") != OS_POLICY_WINDOWS)
1384  goto end;
1385  if (SCHInfoGetHostOSFlavour("10.0.0.5") != OS_POLICY_LINUX)
1386  goto end;
1387  if (SCHInfoGetHostOSFlavour("192.168.1.1") != OS_POLICY_WINDOWS)
1388  goto end;
1389  if (SCHInfoGetHostOSFlavour("172.168.1.1") != OS_POLICY_BSD)
1390  goto end;
1391 
1392  result = 1;
1393 
1394  end:
1395  ConfDeInit();
1397 
1398  SCHInfoRestoreContextBackup();
1399 
1400  return result;
1401 }
1402 
1403 /**
1404  * \test Check the loading of host info from a configuration file.
1405  */
1406 static int SCHInfoTestLoadFromConfig02(void)
1407 {
1408  char config[] = "\
1409 %YAML 1.1\n\
1410 ---\n\
1411 host-os-policy:\n\
1412  one-two: [0.0.0.0/0]\n\
1413  one-two-three:\n\
1414  four_five:\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\
1417 \n";
1418 
1419  int result = 0;
1420 
1421  SCHInfoCreateContextBackup();
1422 
1424  ConfInit();
1425  ConfYamlLoadString(config, strlen(config));
1426 
1427  ConfNode *root = ConfGetNode("host-os-policy");
1428  if (root == NULL)
1429  goto end;
1430 
1431  int count = 0;
1432 
1433  ConfNode *policy;
1434  TAILQ_FOREACH(policy, &root->head, next) {
1435  switch (count) {
1436  case 0:
1437  if (strcmp("one-two", policy->name) != 0)
1438  goto end;
1439  break;
1440  case 1:
1441  if (strcmp("one-two-three", policy->name) != 0)
1442  goto end;
1443  break;
1444  case 2:
1445  if (strcmp("four-five", policy->name) != 0)
1446  goto end;
1447  break;
1448  case 3:
1449  if (strcmp("six-seven-eight", policy->name) != 0)
1450  goto end;
1451  break;
1452  case 4:
1453  if (strcmp("nine-ten-eleven", policy->name) != 0)
1454  goto end;
1455  break;
1456  }
1457  count++;
1458  }
1459 
1460  result = 1;
1461 
1462  end:
1463  ConfDeInit();
1465 
1466  SCHInfoRestoreContextBackup();
1467 
1468  return result;
1469 }
1470 
1471 /**
1472  * \test Check the loading of host info from a configuration file.
1473  */
1474 static int SCHInfoTestLoadFromConfig03(void)
1475 {
1476  char config[] = "\
1477 %YAML 1.1\n\
1478 ---\n\
1479 host-os-policy:\n\
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\
1484  vista: [0.0.0.5]\n\
1485 \n";
1486 
1487  int result = 0;
1488 
1489  SCHInfoCreateContextBackup();
1490 
1492  ConfInit();
1493  ConfYamlLoadString(config, strlen(config));
1494 
1495  ConfNode *root = ConfGetNode("host-os-policy");
1496  if (root == NULL)
1497  goto end;
1498 
1499  ConfNode *policy;
1500  TAILQ_FOREACH(policy, &root->head, next) {
1501  if (SCMapEnumNameToValue(policy->name, sc_hinfo_os_policy_map) == -1) {
1502  printf("Invalid enum map inside\n");
1503  goto end;
1504  }
1505  }
1506 
1507  result = 1;
1508 
1509  end:
1510  ConfDeInit();
1512 
1513  SCHInfoRestoreContextBackup();
1514  return result;
1515 }
1516 
1517 /**
1518  * \test Check the loading of host info from a configuration file.
1519  */
1520 static int SCHInfoTestLoadFromConfig04(void)
1521 {
1522  char config[] = "\
1523 %YAML 1.1\n\
1524 ---\n\
1525 host-os-policy:\n\
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\
1530  vista: [0.0.0.5]\n\
1531 \n";
1532 
1533  int result = 0;
1534 
1535  SCHInfoCreateContextBackup();
1536 
1538  ConfInit();
1539  ConfYamlLoadString(config, strlen(config));
1540 
1541  ConfNode *root = ConfGetNode("host-os-policy");
1542  if (root == NULL)
1543  goto end;
1544 
1545  ConfNode *policy;
1546  TAILQ_FOREACH(policy, &root->head, next) {
1547  if (SCMapEnumNameToValue(policy->name, sc_hinfo_os_policy_map) == -1) {
1548  printf("Invalid enum map inside\n");
1549  goto end;
1550  }
1551  }
1552 
1553  result = 1;
1554 
1555  end:
1556  ConfDeInit();
1558 
1559  SCHInfoRestoreContextBackup();
1560  return result;
1561 }
1562 
1563 /**
1564  * \test Check the loading of host info from a configuration file.
1565  */
1566 static int SCHInfoTestLoadFromConfig05(void)
1567 {
1568  char config[] = "\
1569 %YAML 1.1\n\
1570 ---\n\
1571 host-os-policy:\n\
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\
1576  linux: [0.0.0.5]\n\
1577 \n";
1578 
1579  SCHInfoCreateContextBackup();
1580 
1582  ConfInit();
1583  ConfYamlLoadString(config, strlen(config));
1585 
1591  FAIL_IF (SCHInfoGetHostOSFlavour("0.0.0.0") != -1);
1592  FAIL_IF (SCHInfoGetHostOSFlavour("0.0.0.6") != -1);
1593 
1594  ConfDeInit();
1596  SCHInfoRestoreContextBackup();
1597  PASS;
1598 }
1599 
1600 #endif /* UNITTESTS */
1601 
1603 {
1604 
1605 #ifdef UNITTESTS
1606 
1607  UtRegisterTest("SCHInfoTesInvalidOSFlavour01",
1608  SCHInfoTestInvalidOSFlavour01);
1609  UtRegisterTest("SCHInfoTestInvalidIPV4Address02",
1610  SCHInfoTestInvalidIPV4Address02);
1611  UtRegisterTest("SCHInfoTestInvalidIPV6Address03",
1612  SCHInfoTestInvalidIPV6Address03);
1613  UtRegisterTest("SCHInfoTestValidIPV4Address04",
1614  SCHInfoTestValidIPV4Address04);
1615  UtRegisterTest("SCHInfoTestValidIPV4Address05",
1616  SCHInfoTestValidIPV4Address05);
1617  UtRegisterTest("SCHInfoTestValidIPV6Address06",
1618  SCHInfoTestValidIPV6Address06);
1619  UtRegisterTest("SCHInfoTestValidIPV6Address07",
1620  SCHInfoTestValidIPV6Address07);
1621  UtRegisterTest("SCHInfoTestValidIPV6Address08",
1622  SCHInfoTestValidIPV6Address08);
1623  UtRegisterTest("SCHInfoTestValidIPV4Address09",
1624  SCHInfoTestValidIPV4Address09);
1625 
1626  UtRegisterTest("SCHInfoTestLoadFromConfig01", SCHInfoTestLoadFromConfig01);
1627  UtRegisterTest("SCHInfoTestLoadFromConfig02", SCHInfoTestLoadFromConfig02);
1628  UtRegisterTest("SCHInfoTestLoadFromConfig03", SCHInfoTestLoadFromConfig03);
1629  UtRegisterTest("SCHInfoTestLoadFromConfig04", SCHInfoTestLoadFromConfig04);
1630  UtRegisterTest("SCHInfoTestLoadFromConfig05", SCHInfoTestLoadFromConfig05);
1631 #endif /* UNITTESTS */
1632 
1633 }
util-byte.h
OS_POLICY_LINUX
@ OS_POLICY_LINUX
Definition: stream-tcp-reassemble.h:40
SCRadixRemoveKeyIPV4Netblock
void SCRadixRemoveKeyIPV4Netblock(uint8_t *key_stream, SCRadixTree *tree, uint8_t netmask)
Removes an IPV4 address netblock key from the Radix tree.
Definition: util-radix-tree.c:1271
MaskIPNetblock
void MaskIPNetblock(uint8_t *stream, int netmask, int key_bitlen)
Culls the non-netmask portion of the IP address. For example an IP address 192.168....
Definition: util-ip.c:188
OS_POLICY_BSD
@ OS_POLICY_BSD
Definition: stream-tcp-reassemble.h:37
ConfNode_::val
char * val
Definition: conf.h:34
OS_POLICY_HPUX10
@ OS_POLICY_HPUX10
Definition: stream-tcp-reassemble.h:43
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
OS_POLICY_WINDOWS2K3
@ OS_POLICY_WINDOWS2K3
Definition: stream-tcp-reassemble.h:49
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:175
SC_ERR_INVALID_IPV6_ADDR
@ SC_ERR_INVALID_IPV6_ADDR
Definition: util-error.h:48
SCRadixTree_
Structure for the radix tree.
Definition: util-radix-tree.h:86
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
SCRadixAddKeyIPV6
SCRadixNode * SCRadixAddKeyIPV6(uint8_t *key_stream, SCRadixTree *tree, void *user)
Adds a new IPV6 address to the Radix tree.
Definition: util-radix-tree.c:876
stream-tcp-reassemble.h
SCRadixFindKeyIPV4BestMatch
SCRadixNode * SCRadixFindKeyIPV4BestMatch(uint8_t *key_stream, SCRadixTree *tree, void **user_data_result)
Checks if an IPV4 address is present in the tree under a netblock.
Definition: util-radix-tree.c:1488
SCHInfoGetHostOSFlavour
int SCHInfoGetHostOSFlavour(const char *ip_addr_str)
Retrieves the host os flavour, given an ipv4/ipv6 address as a string.
Definition: util-host-os-info.c:246
sc_hinfo_os_policy_map
SCEnumCharMap sc_hinfo_os_policy_map[]
Definition: util-host-os-info.c:43
SCRadixFindKeyIPV6BestMatch
SCRadixNode * SCRadixFindKeyIPV6BestMatch(uint8_t *key_stream, SCRadixTree *tree, void **user_data_result)
Checks if an IPV6 address is present in the tree under a netblock.
Definition: util-radix-tree.c:1554
util-unittest.h
SCRadixAddKeyIPV4Netblock
SCRadixNode * SCRadixAddKeyIPV4Netblock(uint8_t *key_stream, SCRadixTree *tree, void *user, uint8_t netmask)
Adds a new IPV4 netblock to the Radix tree.
Definition: util-radix-tree.c:896
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
util-error.h
OS_POLICY_VISTA
@ OS_POLICY_VISTA
Definition: stream-tcp-reassemble.h:48
SCRadixReleaseRadixTree
void SCRadixReleaseRadixTree(SCRadixTree *tree)
Frees a Radix tree and all its nodes.
Definition: util-radix-tree.c:463
ValidateIPV4Address
struct in_addr * ValidateIPV4Address(const char *addr_str)
Validates an IPV4 address and returns the network endian arranged version of the IPV4 address.
Definition: util-ip.c:129
util-ip.h
SCHInfoGetIPv6HostOSFlavour
int SCHInfoGetIPv6HostOSFlavour(uint8_t *ipv6_addr)
Retrieves the host os flavour, given an ipv6 address in the raw address format.
Definition: util-host-os-info.c:308
SC_ERR_INVALID_IP_NETBLOCK
@ SC_ERR_INVALID_IP_NETBLOCK
Definition: util-error.h:46
SCHInfoRegisterTests
void SCHInfoRegisterTests(void)
Definition: util-host-os-info.c:1602
SC_HINFO_IS_IPV6
#define SC_HINFO_IS_IPV6
Definition: util-host-os-info.h:27
ConfYamlLoadString
int ConfYamlLoadString(const char *string, size_t len)
Load configuration from a YAML string.
Definition: conf-yaml-loader.c:463
SCRadixCreateRadixTree
SCRadixTree * SCRadixCreateRadixTree(void(*Free)(void *), void(*PrintData)(void *))
Creates a new Radix tree.
Definition: util-radix-tree.c:424
SC_ERR_INVALID_ARGUMENT
@ SC_ERR_INVALID_ARGUMENT
Definition: util-error.h:43
TRUE
#define TRUE
Definition: suricata-common.h:33
FALSE
#define FALSE
Definition: suricata-common.h:34
conf-yaml-loader.h
stream-tcp-private.h
conf.h
OS_POLICY_IRIX
@ OS_POLICY_IRIX
Definition: stream-tcp-reassemble.h:45
util-radix-tree.h
ConfCreateContextBackup
void ConfCreateContextBackup(void)
Creates a backup of the conf_hash hash_table used by the conf API.
Definition: conf.c:698
OS_POLICY_OLD_LINUX
@ OS_POLICY_OLD_LINUX
Definition: stream-tcp-reassemble.h:39
SCHInfoAddHostOSInfo
int SCHInfoAddHostOSInfo(const char *host_os, const char *host_os_ip_range, int is_ipv4)
Used to add the host-os-info data obtained from the conf.
Definition: util-host-os-info.c:122
SC_ERR_INVALID_IPV4_ADDR
@ SC_ERR_INVALID_IPV4_ADDR
Definition: util-error.h:47
SCHInfoCleanResources
void SCHInfoCleanResources(void)
Definition: util-host-os-info.c:318
OS_POLICY_WINDOWS
@ OS_POLICY_WINDOWS
Definition: stream-tcp-reassemble.h:47
util-host-os-info.h
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:71
SC_HINFO_IS_IPV4
#define SC_HINFO_IS_IPV4
Definition: util-host-os-info.h:28
ValidateIPV6Address
struct in6_addr * ValidateIPV6Address(const char *addr_str)
Validates an IPV6 address and returns the network endian arranged version of the IPV6 addresss.
Definition: util-ip.c:159
SCMapEnumNameToValue
int SCMapEnumNameToValue(const char *enum_name, SCEnumCharMap *table)
Maps a string name to an enum value from the supplied table. Please specify the last element of any m...
Definition: util-enum.c:40
suricata-common.h
OS_POLICY_BSD_RIGHT
@ OS_POLICY_BSD_RIGHT
Definition: stream-tcp-reassemble.h:38
SCEnumCharMap_
Definition: util-enum.h:27
SCHInfoLoadFromConfig
void SCHInfoLoadFromConfig(void)
Load the host os policy information from the configuration.
Definition: util-host-os-info.c:333
ConfNode_::name
char * name
Definition: conf.h:33
SCRadixAddKeyIPV6Netblock
SCRadixNode * SCRadixAddKeyIPV6Netblock(uint8_t *key_stream, SCRadixTree *tree, void *user, uint8_t netmask)
Adds a new IPV6 netblock to the Radix tree.
Definition: util-radix-tree.c:916
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
ConfRestoreContextBackup
void ConfRestoreContextBackup(void)
Restores the backup of the hash_table present in backup_conf_hash back to conf_hash.
Definition: conf.c:710
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
FatalError
#define FatalError(x,...)
Definition: util-debug.h:532
OS_POLICY_NONE
@ OS_POLICY_NONE
Definition: stream-tcp-reassemble.h:36
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
ConfInit
void ConfInit(void)
Initialize the configuration system.
Definition: conf.c:113
SCHInfoGetIPv4HostOSFlavour
int SCHInfoGetIPv4HostOSFlavour(uint8_t *ipv4_addr)
Retrieves the host os flavour, given an ipv4 address in the raw address format.
Definition: util-host-os-info.c:290
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
SC_ERR_FATAL
@ SC_ERR_FATAL
Definition: util-error.h:203
OS_POLICY_OLD_SOLARIS
@ OS_POLICY_OLD_SOLARIS
Definition: stream-tcp-reassemble.h:41
OS_POLICY_MACOS
@ OS_POLICY_MACOS
Definition: stream-tcp-reassemble.h:46
SCRadixAddKeyIPV4
SCRadixNode * SCRadixAddKeyIPV4(uint8_t *key_stream, SCRadixTree *tree, void *user)
Adds a new IPV4 address to the Radix tree.
Definition: util-radix-tree.c:857
OS_POLICY_SOLARIS
@ OS_POLICY_SOLARIS
Definition: stream-tcp-reassemble.h:42
SC_ERR_INVALID_ENUM_MAP
@ SC_ERR_INVALID_ENUM_MAP
Definition: util-error.h:45
ConfDeInit
void ConfDeInit(void)
De-initializes the configuration system.
Definition: conf.c:721
OS_POLICY_HPUX11
@ OS_POLICY_HPUX11
Definition: stream-tcp-reassemble.h:44
util-enum.h
StringParseI32RangeCheck
int StringParseI32RangeCheck(int32_t *res, int base, uint16_t len, const char *str, int32_t min, int32_t max)
Definition: util-byte.c:704