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("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("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 
106 /**
107  * \brief Used to add the host-os-info data obtained from the conf
108  *
109  * \param host_os The host_os name/flavour from the conf file
110  * \param host_os_ip_range Pointer to a char string holding the ip/ip_netblock
111  * for the host_os specified in the first argument
112  * \param is_ipv4 Indicates if the ip address to be considered for the
113  * default configuration is IPV4; if not it is IPV6.
114  * Specified using SC_HINFO_IS_IPV6 or SC_HINFO_IS_IPV4
115  *
116  * \retval 0 On successfully adding the host os info to the Radix tree
117  * \retval -1 On failure
118  * \initonly (only specified from config, at the startup)
119  */
120 int SCHInfoAddHostOSInfo(const char *host_os, const char *host_os_ip_range, int is_ipv4)
121 {
122  char *ip_str = NULL;
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;
130 
131  if (host_os == NULL || host_os_ip_range == NULL ||
132  strlen(host_os_ip_range) == 0) {
133  SCLogError("Invalid arguments");
134  return -1;
135  }
136 
137  /* create the radix tree that would hold all the host os info */
138  if (sc_hinfo_tree == NULL)
139  sc_hinfo_tree = SCRadixCreateRadixTree(SCHInfoFreeUserDataOSPolicy, NULL);
140 
141  /* the host os flavour that has to be sent as user data */
142  if ( (user_data = SCHInfoAllocUserDataOSPolicy(host_os)) == NULL) {
143  SCLogError("Invalid enum map inside");
144  return -1;
145  }
146 
147  /* if we have a default configuration set the appropriate values for the
148  * netblocks */
149  if ( (strcasecmp(host_os_ip_range, "default")) == 0) {
150  if (is_ipv4)
151  host_os_ip_range = "0.0.0.0/0";
152  else
153  host_os_ip_range = "::/0";
154  }
155 
156  if ( (ip_str = SCStrdup(host_os_ip_range)) == NULL) {
157  FatalError("Error allocating memory");
158  }
159 
160  /* check if we have more addresses in the host_os_ip_range */
161  if ((ip_str_rem = strchr(ip_str, ',')) != NULL) {
162  ip_str_rem[0] = '\0';
163  ip_str_rem++;
164  recursive = true;
165  }
166 
167  /* check if we have received a netblock */
168  if ( (netmask_str = strchr(ip_str, '/')) != NULL) {
169  netmask_str[0] = '\0';
170  netmask_str++;
171  }
172 
173  if (strchr(ip_str, ':') == NULL) {
174  /* if we are here, we have an IPV4 address */
175  if ( (ipv4_addr = ValidateIPV4Address(ip_str)) == NULL) {
176  SCLogError("Invalid IPV4 address");
177  SCHInfoFreeUserDataOSPolicy(user_data);
178  SCFree(ip_str);
179  return -1;
180  }
181 
182  if (netmask_str == NULL) {
183  SCRadixAddKeyIPV4((uint8_t *)ipv4_addr, sc_hinfo_tree,
184  (void *)user_data);
185  } else {
186  if (StringParseU8RangeCheck(&netmask_value, 10, 0, (const char *)netmask_str, 0, 32) <
187  0) {
188  SCLogError("Invalid IPV4 Netblock");
189  SCHInfoFreeUserDataOSPolicy(user_data);
190  SCFree(ipv4_addr);
191  SCFree(ip_str);
192  return -1;
193  }
194 
195  MaskIPNetblock((uint8_t *)ipv4_addr, netmask_value, 32);
196  SCRadixAddKeyIPV4Netblock((uint8_t *)ipv4_addr, sc_hinfo_tree,
197  (void *)user_data, netmask_value);
198  }
199  } else {
200  /* if we are here, we have an IPV6 address */
201  if ( (ipv6_addr = ValidateIPV6Address(ip_str)) == NULL) {
202  SCLogError("Invalid IPV6 address inside");
203  SCHInfoFreeUserDataOSPolicy(user_data);
204  SCFree(ip_str);
205  return -1;
206  }
207 
208  if (netmask_str == NULL) {
209  SCRadixAddKeyIPV6((uint8_t *)ipv6_addr, sc_hinfo_tree,
210  (void *)user_data);
211  } else {
212  if (StringParseU8RangeCheck(&netmask_value, 10, 0, (const char *)netmask_str, 0, 128) <
213  0) {
214  SCLogError("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) {
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("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("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 
326 /**
327  * \brief Load the host os policy information from the configuration.
328  *
329  * \initonly (A mem alloc error should cause an exit failure)
330  */
332 {
333  ConfNode *root = ConfGetNode("host-os-policy");
334  if (root == NULL)
335  return;
336 
337  ConfNode *policy;
338  TAILQ_FOREACH(policy, &root->head, next) {
339  ConfNode *host;
340  TAILQ_FOREACH(host, &policy->head, next) {
341  int is_ipv4 = 1;
342  if (host->val != NULL && strchr(host->val, ':') != NULL)
343  is_ipv4 = 0;
344  if (SCHInfoAddHostOSInfo(policy->name, host->val, is_ipv4) == -1) {
345  SCLogError("Failed to add host \"%s\" with policy \"%s\" to host "
346  "info database",
347  host->val, policy->name);
348  exit(EXIT_FAILURE);
349  }
350  }
351  }
352 }
353 
354 /*------------------------------------Unit_Tests------------------------------*/
355 
356 #ifdef UNITTESTS
357 static SCRadixTree *sc_hinfo_tree_backup = NULL;
358 
359 static void SCHInfoCreateContextBackup(void)
360 {
361  sc_hinfo_tree_backup = sc_hinfo_tree;
362  sc_hinfo_tree = NULL;
363 }
364 
365 static void SCHInfoRestoreContextBackup(void)
366 {
367  sc_hinfo_tree = sc_hinfo_tree_backup;
368  sc_hinfo_tree_backup = NULL;
369 }
370 
371 /**
372  * \test Check if we the IPs with the right OS flavours are added to the host OS
373  * radix tree, and the IPS with invalid flavours returns an error(-1)
374  */
375 static int SCHInfoTestInvalidOSFlavour01(void)
376 {
377  SCHInfoCreateContextBackup();
378 
379  int result = 0;
380 
381  if (SCHInfoAddHostOSInfo("bamboo", "192.168.1.1", SC_HINFO_IS_IPV4) != -1) {
382  goto end;
383  }
384  if (SCHInfoAddHostOSInfo("linux", "192.168.1.1", SC_HINFO_IS_IPV4) !=
386  goto end;
387  }
388  if (SCHInfoAddHostOSInfo("windows", "192.168.1.1", SC_HINFO_IS_IPV4) !=
390  goto end;
391  }
392  if (SCHInfoAddHostOSInfo("solaris", "192.168.1.1", SC_HINFO_IS_IPV4) !=
394  goto end;
395  }
396  if (SCHInfoAddHostOSInfo("hpux10", "192.168.1.1", SC_HINFO_IS_IPV4) !=
398  goto end;
399  }
400  if (SCHInfoAddHostOSInfo("hpux11", "192.168.1.1", SC_HINFO_IS_IPV4) !=
402  goto end;
403  }
404  if (SCHInfoAddHostOSInfo("irix", "192.168.1.1", SC_HINFO_IS_IPV4) !=
406  goto end;
407  }
408  if (SCHInfoAddHostOSInfo("bsd", "192.168.1.1", SC_HINFO_IS_IPV4) !=
410  goto end;
411  }
412  if (SCHInfoAddHostOSInfo("old_linux", "192.168.1.1", SC_HINFO_IS_IPV4) !=
414  goto end;
415  }
416  if (SCHInfoAddHostOSInfo("macos", "192.168.1.1", SC_HINFO_IS_IPV4) !=
418  goto end;
419  }
420  if (SCHInfoAddHostOSInfo("vista", "192.168.1.1", SC_HINFO_IS_IPV4) !=
422  goto end;
423  }
424  if (SCHInfoAddHostOSInfo("windows2k3", "192.168.1.1", SC_HINFO_IS_IPV4) !=
426  goto end;
427  }
428 
429  result = 1;
430 
431  end:
433  SCHInfoRestoreContextBackup();
434 
435  return result;
436 }
437 
438 /**
439  * \test Check that invalid ipv4 addresses and ipv4 netblocks are rejected by
440  * the host os info API
441  */
442 static int SCHInfoTestInvalidIPV4Address02(void)
443 {
444  SCHInfoCreateContextBackup();
445 
446  int result = 0;
447 
448  if (SCHInfoAddHostOSInfo("linux", "192.168.1.566", SC_HINFO_IS_IPV4) != -1) {
449  goto end;
450  }
451  if (SCHInfoAddHostOSInfo("linux", "192.168.1", SC_HINFO_IS_IPV4) != -1) {
452  goto end;
453  }
454  if (SCHInfoAddHostOSInfo("linux", "192.", SC_HINFO_IS_IPV4) != -1) {
455  goto end;
456  }
457  if (SCHInfoAddHostOSInfo("linux", "192.168", SC_HINFO_IS_IPV4) != -1) {
458  goto end;
459  }
460  if (SCHInfoAddHostOSInfo("linux", "", SC_HINFO_IS_IPV4) != -1) {
461  goto end;
462  }
463  if (SCHInfoAddHostOSInfo("linux", "192.168.1.1/33", SC_HINFO_IS_IPV4) != -1) {
464  goto end;
465  }
466 
467  result = 1;
468 
469  end:
471  SCHInfoRestoreContextBackup();
472 
473  return result;
474 }
475 
476 /**
477  * \test Check that invalid ipv4 addresses and ipv4 netblocks are rejected by
478  * the host os info API
479  */
480 static int SCHInfoTestInvalidIPV6Address03(void)
481 {
482  SCHInfoCreateContextBackup();
483 
484  int result = 0;
485 
486  if (SCHInfoAddHostOSInfo("linux", "2362:7322", SC_HINFO_IS_IPV6) != -1) {
487  goto end;
488  }
489  if (SCHInfoAddHostOSInfo("linux", "19YW:", SC_HINFO_IS_IPV6) != -1) {
490  goto end;
491  }
492  if (SCHInfoAddHostOSInfo("linux", "1235", SC_HINFO_IS_IPV6) != -1) {
493  goto end;
494  }
495  if (SCHInfoAddHostOSInfo("linux", "1922:236115:", SC_HINFO_IS_IPV6) != -1) {
496  goto end;
497  }
498  if (SCHInfoAddHostOSInfo("linux", "", SC_HINFO_IS_IPV6) != -1) {
499  goto end;
500  }
501  if (SCHInfoAddHostOSInfo("linux",
502  "1921.6311:6241:6422:7352:ABBB:DDDD:EEEE/129",
503  SC_HINFO_IS_IPV6) != -1) {
504  goto end;
505  }
506 
507  result = 1;
508 
509  end:
511  SCHInfoRestoreContextBackup();
512 
513  return result;
514 }
515 
516 /**
517  * \test Check that valid ipv4 addresses are inserted into the host os radix
518  * tree, and the host os api retrieves the right value for the host os
519  * flavour, on supplying as arg an ipv4 addresses that has been added to
520  * the host os radix tree.
521  */
522 static int SCHInfoTestValidIPV4Address04(void)
523 {
524  SCHInfoCreateContextBackup();
525 
526  int result = 0;
527 
528  if (SCHInfoAddHostOSInfo("linux", "192.168.1.1", SC_HINFO_IS_IPV4) == -1) {
529  goto end;
530  }
531  if (SCHInfoAddHostOSInfo("windows", "192.192.1.2", SC_HINFO_IS_IPV4) == -1) {
532  goto end;
533  }
534  if (SCHInfoAddHostOSInfo("solaris", "192.168.1.100", SC_HINFO_IS_IPV4) == -1) {
535  goto end;
536  }
537  if (SCHInfoAddHostOSInfo("hpux10", "192.168.2.4", SC_HINFO_IS_IPV4) == -1) {
538  goto end;
539  }
540  if (SCHInfoAddHostOSInfo("linux", "192.192.1.5", SC_HINFO_IS_IPV4) == -1) {
541  goto end;
542  }
543  if (SCHInfoAddHostOSInfo("vista", "192.168.10.20", SC_HINFO_IS_IPV4) == -1) {
544  goto end;
545  }
546  if (SCHInfoAddHostOSInfo("solaris", "111.163.151.62", SC_HINFO_IS_IPV4) == -1) {
547  goto end;
548  }
549  if (SCHInfoAddHostOSInfo("solaris", "11.1.120.210", SC_HINFO_IS_IPV4) == -1) {
550  goto end;
551  }
552  if (SCHInfoAddHostOSInfo("linux", "19.18.110.210", SC_HINFO_IS_IPV4) == -1) {
553  goto end;
554  }
555  if (SCHInfoAddHostOSInfo("windows", "19.18.120.110", SC_HINFO_IS_IPV4) == -1) {
556  goto end;
557  }
558  if (SCHInfoAddHostOSInfo("hpux11", "191.168.11.128", SC_HINFO_IS_IPV4) == -1) {
559  goto end;
560  }
561  if (SCHInfoAddHostOSInfo("vista", "191.168.11.192", SC_HINFO_IS_IPV4) == -1) {
562  goto end;
563  }
564 
565  if (SCHInfoGetHostOSFlavour("192.168.1.1") !=
567  goto end;
568  }
569  if (SCHInfoGetHostOSFlavour("192.168.1.2") != -1) {
570  goto end;
571  }
572  if (SCHInfoGetHostOSFlavour("192.168.1.100") !=
574  goto end;
575  }
576  if (SCHInfoGetHostOSFlavour("192.192.2.4") != -1) {
577  goto end;
578  }
579  if (SCHInfoGetHostOSFlavour("192.168.2.4") !=
581  goto end;
582  }
583  if (SCHInfoGetHostOSFlavour("192.192.1.5") !=
585  goto end;
586  }
587  if (SCHInfoGetHostOSFlavour("192.168.10.20") !=
589  goto end;
590  }
591  if (SCHInfoGetHostOSFlavour("111.163.151.62") !=
593  goto end;
594  }
595  if (SCHInfoGetHostOSFlavour("11.1.120.210") !=
597  goto end;
598  }
599  if (SCHInfoGetHostOSFlavour("19.18.110.210") !=
601  goto end;
602  }
603  if (SCHInfoGetHostOSFlavour("19.18.120.110") !=
605  goto end;
606  }
607  if (SCHInfoGetHostOSFlavour("191.168.11.128") !=
609  goto end;
610  }
611  if (SCHInfoGetHostOSFlavour("191.168.11.192") !=
613  goto end;
614  }
615  if (SCHInfoGetHostOSFlavour("191.168.11.224") != -1) {
616  goto end;
617  }
618 
619  result = 1;
620 
621  end:
623  SCHInfoRestoreContextBackup();
624 
625  return result;
626 }
627 
628 /**
629  * \test Check that valid ipv4 addresses/netblocks are inserted into the host os
630  * radix tree, and the host os api retrieves the right value for the host
631  * os flavour, on supplying as arg an ipv4 addresses that has been added
632  * to the host os radix tree.
633  */
634 static int SCHInfoTestValidIPV4Address05(void)
635 {
636  SCHInfoCreateContextBackup();
637 
638  struct in_addr in;
639  int result = 0;
640 
641  if (SCHInfoAddHostOSInfo("linux", "192.168.1.1", SC_HINFO_IS_IPV4) == -1) {
642  goto end;
643  }
644  if (SCHInfoAddHostOSInfo("windows", "192.192.1.2", SC_HINFO_IS_IPV4) == -1) {
645  goto end;
646  }
647  if (SCHInfoAddHostOSInfo("solaris", "192.168.1.100", SC_HINFO_IS_IPV4) == -1) {
648  goto end;
649  }
650  if (SCHInfoAddHostOSInfo("hpux10", "192.168.2.4", SC_HINFO_IS_IPV4) == -1) {
651  goto end;
652  }
653  if (SCHInfoAddHostOSInfo("linux", "192.192.1.5", SC_HINFO_IS_IPV4) == -1) {
654  goto end;
655  }
656  if (SCHInfoAddHostOSInfo("vista", "192.168.10.20", SC_HINFO_IS_IPV4) == -1) {
657  goto end;
658  }
659  if (SCHInfoAddHostOSInfo("solaris", "111.163.151.62", SC_HINFO_IS_IPV4) == -1) {
660  goto end;
661  }
662  if (SCHInfoAddHostOSInfo("hpux11", "111.162.208.124/20", SC_HINFO_IS_IPV4) == -1) {
663  goto end;
664  }
665  if (SCHInfoAddHostOSInfo("windows", "111.162.240.1", SC_HINFO_IS_IPV4) == -1) {
666  goto end;
667  }
668  if (SCHInfoAddHostOSInfo("solaris", "111.162.214.100", SC_HINFO_IS_IPV4) == -1) {
669  goto end;
670  }
671  if (SCHInfoAddHostOSInfo("vista", "111.162.208.100", SC_HINFO_IS_IPV4) == -1) {
672  goto end;
673  }
674  if (SCHInfoAddHostOSInfo("linux", "111.162.194.112", SC_HINFO_IS_IPV4) == -1) {
675  goto end;
676  }
677 
678  if (SCHInfoGetHostOSFlavour("192.168.1.1") !=
680  goto end;
681  }
682  if (SCHInfoGetHostOSFlavour("192.168.1.2") != -1) {
683  goto end;
684  }
685  if (SCHInfoGetHostOSFlavour("192.168.1.100") !=
687  goto end;
688  }
689  if (SCHInfoGetHostOSFlavour("192.192.2.4") != -1) {
690  goto end;
691  }
692  if (SCHInfoGetHostOSFlavour("192.168.2.4") !=
694  goto end;
695  }
696  if (SCHInfoGetHostOSFlavour("192.192.1.5") !=
698  goto end;
699  }
700  if (SCHInfoGetHostOSFlavour("192.168.10.20") !=
702  goto end;
703  }
704  if (SCHInfoGetHostOSFlavour("111.163.151.62") !=
706  goto end;
707  }
708  if (SCHInfoGetHostOSFlavour("111.162.208.0") !=
710  goto end;
711  }
712  if (SCHInfoGetHostOSFlavour("111.162.210.1") !=
714  goto end;
715  }
716  if (SCHInfoGetHostOSFlavour("111.162.214.1") !=
718  goto end;
719  }
720  if (SCHInfoGetHostOSFlavour("111.162.0.0") != -1) {
721  goto end;
722  }
723  if (SCHInfoGetHostOSFlavour("111.162.240.112") != -1) {
724  goto end;
725  }
726  if (SCHInfoGetHostOSFlavour("111.162.240.1") !=
728  goto end;
729  }
730  if (SCHInfoGetHostOSFlavour("111.162.214.100") !=
732  goto end;
733  }
734  if (inet_pton(AF_INET, "111.162.208.100", &in) < 0) {
735  goto end;
736  }
737  if (SCHInfoGetIPv4HostOSFlavour((uint8_t *)&in) !=
739  goto end;
740  }
741  if (SCHInfoGetHostOSFlavour("111.162.194.112") !=
743  goto end;
744  }
745  if (SCHInfoGetHostOSFlavour("111.162.208.200") !=
747  goto end;
748  }
749  if (inet_pton(AF_INET, "111.162.208.200", &in) < 0) {
750  goto end;
751  }
752  if (SCHInfoGetIPv4HostOSFlavour((uint8_t *)&in) !=
754  goto end;
755  }
756  if (SCHInfoGetHostOSFlavour("111.162.200.201") != -1) {
757  goto end;
758  }
759 
760  result = 1;
761 
762  end:
764  SCHInfoRestoreContextBackup();
765 
766  return result;
767 }
768 
769 /**
770  * \test Check that valid ipv6 addresses are inserted into the host os radix
771  * tree, and the host os api retrieves the right value for the host os
772  * flavour, on supplying as arg an ipv6 address that has been added to
773  * the host os radix tree.
774  */
775 static int SCHInfoTestValidIPV6Address06(void)
776 {
777  SCHInfoCreateContextBackup();
778 
779  int result = 0;
780 
781  if (SCHInfoAddHostOSInfo("linux",
782  "2351:2512:6211:6246:235A:6242:2352:62AD",
783  SC_HINFO_IS_IPV6) == -1) {
784  goto end;
785  }
786  if (SCHInfoAddHostOSInfo("windows",
787  "6961:6121:2132:6241:423A:2135:2461:621D",
788  SC_HINFO_IS_IPV6) == -1) {
789  goto end;
790  }
791  if (SCHInfoAddHostOSInfo("solaris",
792  "DD13:613D:F312:62DD:6213:421A:6212:2652",
793  SC_HINFO_IS_IPV6) == -1) {
794  goto end;
795  }
796  if (SCHInfoAddHostOSInfo("hpux10",
797  "9891:2131:2151:6426:1342:674D:622F:2342",
798  SC_HINFO_IS_IPV6) == -1) {
799  goto end;
800  }
801  if (SCHInfoAddHostOSInfo("linux",
802  "3525:2351:4223:6211:2311:2667:6242:2154",
803  SC_HINFO_IS_IPV6) == -1) {
804  goto end;
805  }
806  if (SCHInfoAddHostOSInfo("vista",
807  "1511:6211:6726:7777:1212:2333:6222:7722",
808  SC_HINFO_IS_IPV6) == -1) {
809  goto end;
810  }
811  if (SCHInfoAddHostOSInfo("solaris",
812  "2666:6222:7222:2335:6223:7722:3425:2362",
813  SC_HINFO_IS_IPV6) == -1) {
814  goto end;
815  }
816  if (SCHInfoAddHostOSInfo("solaris",
817  "8762:2352:6241:7245:EE23:21AD:2312:622C",
818  SC_HINFO_IS_IPV6) == -1) {
819  goto end;
820  }
821  if (SCHInfoAddHostOSInfo("linux",
822  "6422:EE1A:2621:34AD:2462:432D:642E:E13A",
823  SC_HINFO_IS_IPV6) == -1) {
824  goto end;
825  }
826  if (SCHInfoAddHostOSInfo("windows",
827  "3521:7622:6241:6242:7277:1234:2352:6234",
828  SC_HINFO_IS_IPV6) == -1) {
829  goto end;
830  }
831  if (SCHInfoAddHostOSInfo("hpux11",
832  "2141:6232:6252:2223:7734:2345:6245:6222",
833  SC_HINFO_IS_IPV6) == -1) {
834  goto end;
835  }
836  if (SCHInfoAddHostOSInfo("vista",
837  "5222:6432:6432:2322:6662:3423:4322:3245",
838  SC_HINFO_IS_IPV6) == -1) {
839  goto end;
840  }
841 
842  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:62AD") !=
844  goto end;
845  }
846  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:6FFFE") != -1) {
847  goto end;
848  }
849  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2652") !=
851  goto end;
852  }
853  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2222") != -1) {
854  goto end;
855  }
856  if (SCHInfoGetHostOSFlavour("9891:2131:2151:6426:1342:674D:622F:2342") !=
858  goto end;
859  }
860  if (SCHInfoGetHostOSFlavour("3525:2351:4223:6211:2311:2667:6242:2154") !=
862  goto end;
863  }
864  if (SCHInfoGetHostOSFlavour("1511:6211:6726:7777:1212:2333:6222:7722") !=
866  goto end;
867  }
868  if (SCHInfoGetHostOSFlavour("2666:6222:7222:2335:6223:7722:3425:2362") !=
870  goto end;
871  }
872  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
874  goto end;
875  }
876  if (SCHInfoGetHostOSFlavour("6422:EE1A:2621:34AD:2462:432D:642E:E13A") !=
878  goto end;
879  }
880  if (SCHInfoGetHostOSFlavour("3521:7622:6241:6242:7277:1234:2352:6234") !=
882  goto end;
883  }
884  if (SCHInfoGetHostOSFlavour("2141:6232:6252:2223:7734:2345:6245:6222") !=
886  goto end;
887  }
888  if (SCHInfoGetHostOSFlavour("5222:6432:6432:2322:6662:3423:4322:3245") !=
890  goto end;
891  }
892  if (SCHInfoGetHostOSFlavour("5222:6432:6432:2322:6662:3423:4322:DDDD") != -1) {
893  goto end;
894  }
895 
896  result = 1;
897 
898  end:
900  SCHInfoRestoreContextBackup();
901 
902  return result;
903 }
904 
905 /**
906  * \test Check that valid ipv6 addresses/netblocks are inserted into the host os
907  * radix tree, and the host os api retrieves the right value for the host
908  * os flavour, on supplying as arg an ipv6 address that has been added to
909  * the host os radix tree.
910  */
911 static int SCHInfoTestValidIPV6Address07(void)
912 {
913  SCHInfoCreateContextBackup();
914 
915  int result = 0;
916 
917  if (SCHInfoAddHostOSInfo("linux",
918  "2351:2512:6211:6246:235A:6242:2352:62AD",
919  SC_HINFO_IS_IPV6) == -1) {
920  goto end;
921  }
922  if (SCHInfoAddHostOSInfo("windows",
923  "6961:6121:2132:6241:423A:2135:2461:621D",
924  SC_HINFO_IS_IPV6) == -1) {
925  goto end;
926  }
927  if (SCHInfoAddHostOSInfo("solaris",
928  "DD13:613D:F312:62DD:6213:421A:6212:2652",
929  SC_HINFO_IS_IPV6) == -1) {
930  goto end;
931  }
932  if (SCHInfoAddHostOSInfo("hpux10",
933  "9891:2131:2151:6426:1342:674D:622F:2342",
934  SC_HINFO_IS_IPV6) == -1) {
935  goto end;
936  }
937  if (SCHInfoAddHostOSInfo("linux",
938  "3525:2351:4223:6211:2311:2667:6242:2154",
939  SC_HINFO_IS_IPV6) == -1) {
940  goto end;
941  }
942  if (SCHInfoAddHostOSInfo("vista",
943  "1511:6211:6726:7777:1212:2333:6222:7722",
944  SC_HINFO_IS_IPV6) == -1) {
945  goto end;
946  }
947  if (SCHInfoAddHostOSInfo("solaris",
948  "2666:6222:7222:2335:6223:7722:3425:2362",
949  SC_HINFO_IS_IPV6) == -1) {
950  goto end;
951  }
952  if (SCHInfoAddHostOSInfo("solaris",
953  "8762:2352:6241:7245:EE23:21AD:2312:622C/68",
954  SC_HINFO_IS_IPV6) == -1) {
955  goto end;
956  }
957  if (SCHInfoAddHostOSInfo("linux",
958  "8762:2352:6241:7245:EE23:21AD:2412:622C",
959  SC_HINFO_IS_IPV6) == -1) {
960  goto end;
961  }
962  if (SCHInfoAddHostOSInfo("windows",
963  "8762:2352:6241:7245:EE23:21AD:FFFF:622C",
964  SC_HINFO_IS_IPV6) == -1) {
965  goto end;
966  }
967  if (SCHInfoAddHostOSInfo("hpux11",
968  "8762:2352:6241:7245:EE23:21AD:2312:62FF",
969  SC_HINFO_IS_IPV6) == -1) {
970  goto end;
971  }
972  if (SCHInfoAddHostOSInfo("vista",
973  "8762:2352:6241:7245:EE23:21AD:2121:1212",
974  SC_HINFO_IS_IPV6) == -1) {
975  goto end;
976  }
977 
978  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:62AD") !=
980  goto end;
981  }
982  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:6FFFE") != -1) {
983  goto end;
984  }
985  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2652") !=
987  goto end;
988  }
989  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2222") != -1) {
990  goto end;
991  }
992  if (SCHInfoGetHostOSFlavour("9891:2131:2151:6426:1342:674D:622F:2342") !=
994  goto end;
995  }
996  if (SCHInfoGetHostOSFlavour("3525:2351:4223:6211:2311:2667:6242:2154") !=
998  goto end;
999  }
1000  if (SCHInfoGetHostOSFlavour("1511:6211:6726:7777:1212:2333:6222:7722") !=
1002  goto end;
1003  }
1004  if (SCHInfoGetHostOSFlavour("2666:6222:7222:2335:6223:7722:3425:2362") !=
1006  goto end;
1007  }
1008  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
1010  goto end;
1011  }
1012  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2412:622C") !=
1014  goto end;
1015  }
1016  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:FFFF:622C") !=
1018  goto end;
1019  }
1020  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:62FF") !=
1022  goto end;
1023  }
1024  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2121:1212") !=
1026  goto end;
1027  }
1028  if (SCHInfoGetHostOSFlavour("5222:6432:6432:2322:6662:3423:4322:DDDD") != -1) {
1029  goto end;
1030  }
1031  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2121:1DDD") !=
1033  goto end;
1034  }
1035  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:FFFF:2121:1DDD") !=
1037  goto end;
1038  }
1039  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
1041  goto end;
1042  }
1043  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE00:0000:0000:0000") !=
1045  goto end;
1046  }
1047  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:E000:0000:0000:0000") !=
1049  goto end;
1050  }
1051 
1052  result = 1;
1053 
1054  end:
1056  SCHInfoRestoreContextBackup();
1057 
1058  return result;
1059 }
1060 
1061 /**
1062  * \test Check that valid ipv6 addresses/netblocks are inserted into the host os
1063  * radix tree, and the host os api retrieves the right value for the host
1064  * os flavour, on supplying as arg an ipv6 address that has been added to
1065  * the host os radix tree.
1066  */
1067 static int SCHInfoTestValidIPV6Address08(void)
1068 {
1069  SCHInfoCreateContextBackup();
1070 
1071  struct in6_addr in6;
1072  int result = 0;
1073 
1074  if (SCHInfoAddHostOSInfo("linux",
1075  "2351:2512:6211:6246:235A:6242:2352:62AD",
1076  SC_HINFO_IS_IPV6) == -1) {
1077  goto end;
1078  }
1079  if (SCHInfoAddHostOSInfo("windows",
1080  "6961:6121:2132:6241:423A:2135:2461:621D",
1081  SC_HINFO_IS_IPV6) == -1) {
1082  goto end;
1083  }
1084  if (SCHInfoAddHostOSInfo("solaris",
1085  "DD13:613D:F312:62DD:6213:421A:6212:2652",
1086  SC_HINFO_IS_IPV6) == -1) {
1087  goto end;
1088  }
1089  if (SCHInfoAddHostOSInfo("hpux10",
1090  "9891:2131:2151:6426:1342:674D:622F:2342",
1091  SC_HINFO_IS_IPV6) == -1) {
1092  goto end;
1093  }
1094  if (SCHInfoAddHostOSInfo("linux",
1095  "3525:2351:4223:6211:2311:2667:6242:2154",
1096  SC_HINFO_IS_IPV6) == -1) {
1097  goto end;
1098  }
1099  if (SCHInfoAddHostOSInfo("vista",
1100  "1511:6211:6726:7777:1212:2333:6222:7722",
1101  SC_HINFO_IS_IPV6) == -1) {
1102  goto end;
1103  }
1104  if (SCHInfoAddHostOSInfo("solaris",
1105  "2666:6222:7222:2335:6223:7722:3425:2362",
1106  SC_HINFO_IS_IPV6) == -1) {
1107  goto end;
1108  }
1109  if (SCHInfoAddHostOSInfo("solaris",
1110  "8762:2352:6241:7245:EE23:21AD:2312:622C/68",
1111  SC_HINFO_IS_IPV6) == -1) {
1112  goto end;
1113  }
1114  if (SCHInfoAddHostOSInfo("linux",
1115  "8762:2352:6241:7245:EE23:21AD:2412:622C",
1116  SC_HINFO_IS_IPV6) == -1) {
1117  goto end;
1118  }
1119  if (SCHInfoAddHostOSInfo("windows",
1120  "8762:2352:6241:7245:EE23:21AD:FFFF:622C",
1121  SC_HINFO_IS_IPV6) == -1) {
1122  goto end;
1123  }
1124  if (SCHInfoAddHostOSInfo("hpux11",
1125  "8762:2352:6241:7245:EE23:21AD:2312:62FF",
1126  SC_HINFO_IS_IPV6) == -1) {
1127  goto end;
1128  }
1129  if (SCHInfoAddHostOSInfo("vista",
1130  "8762:2352:6241:7245:EE23:21AD:2121:1212",
1131  SC_HINFO_IS_IPV6) == -1) {
1132  goto end;
1133  }
1134  if (SCHInfoAddHostOSInfo("vista", "8.8.8.0/24", SC_HINFO_IS_IPV4) == -1) {
1135  goto end;
1136  }
1137  if (SCHInfoAddHostOSInfo("irix", "default", SC_HINFO_IS_IPV6) == -1) {
1138  goto end;
1139  }
1140 
1141  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:62AD") !=
1143  goto end;
1144  }
1145  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:6FFF") !=
1147  goto end;
1148  }
1149  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2652") !=
1151  goto end;
1152  }
1153  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2222") !=
1155  goto end;
1156  }
1157  if (SCHInfoGetHostOSFlavour("9891:2131:2151:6426:1342:674D:622F:2342") !=
1159  goto end;
1160  }
1161  if (SCHInfoGetHostOSFlavour("3525:2351:4223:6211:2311:2667:6242:2154") !=
1163  goto end;
1164  }
1165  if (SCHInfoGetHostOSFlavour("1511:6211:6726:7777:1212:2333:6222:7722") !=
1167  goto end;
1168  }
1169  if (SCHInfoGetHostOSFlavour("2666:6222:7222:2335:6223:7722:3425:2362") !=
1171  goto end;
1172  }
1173  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
1175  goto end;
1176  }
1177  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2412:622C") !=
1179  goto end;
1180  }
1181  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:FFFF:622C") !=
1183  goto end;
1184  }
1185  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:62FF") !=
1187  goto end;
1188  }
1189  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2121:1212") !=
1191  goto end;
1192  }
1193  if (SCHInfoGetHostOSFlavour("5222:6432:6432:2322:6662:3423:4322:DDDD") !=
1195  goto end;
1196  }
1197  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2121:1DDD") !=
1199  goto end;
1200  }
1201  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:FFFF:2121:1DDD") !=
1203  goto end;
1204  }
1205  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
1207  goto end;
1208  }
1209  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE00:0000:0000:0000") !=
1211  goto end;
1212  }
1213  if (inet_pton(AF_INET6, "8762:2352:6241:7245:E000:0000:0000:0000", &in6) < 0) {
1214  goto end;
1215  }
1216  if (SCHInfoGetIPv6HostOSFlavour((uint8_t *)&in6) !=
1218  goto end;
1219  }
1220  if (inet_pton(AF_INET6, "AD23:2DDA:6D1D:A223:E235:0232:1241:1666", &in6) < 0) {
1221  goto end;
1222  }
1223  if (SCHInfoGetIPv6HostOSFlavour((uint8_t *)&in6) !=
1225  goto end;
1226  }
1227  if (SCHInfoGetHostOSFlavour("8.8.8.8") !=
1229  goto end;
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 }
OS_POLICY_HPUX10
@ OS_POLICY_HPUX10
Definition: stream-tcp-reassemble.h:43
util-byte.h
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:1359
OS_POLICY_VISTA
@ OS_POLICY_VISTA
Definition: stream-tcp-reassemble.h:48
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:187
ConfNode_::val
char * val
Definition: conf.h:34
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
OS_POLICY_SOLARIS
@ OS_POLICY_SOLARIS
Definition: stream-tcp-reassemble.h:42
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:181
SCRadixTree_
Structure for the radix tree.
Definition: util-radix-tree.h:86
OS_POLICY_LINUX
@ OS_POLICY_LINUX
Definition: stream-tcp-reassemble.h:40
OS_POLICY_MACOS
@ OS_POLICY_MACOS
Definition: stream-tcp-reassemble.h:46
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
OS_POLICY_NONE
@ OS_POLICY_NONE
Definition: stream-tcp-reassemble.h:36
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:1565
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:1623
util-unittest.h
OS_POLICY_IRIX
@ OS_POLICY_IRIX
Definition: stream-tcp-reassemble.h:45
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:939
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
util-error.h
SCRadixReleaseRadixTree
void SCRadixReleaseRadixTree(SCRadixTree *tree)
Frees a Radix tree and all its nodes.
Definition: util-radix-tree.c:440
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:130
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
SCHInfoRegisterTests
void SCHInfoRegisterTests(void)
Definition: util-host-os-info.c:1602
OS_POLICY_BSD_RIGHT
@ OS_POLICY_BSD_RIGHT
Definition: stream-tcp-reassemble.h:38
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:523
SCRadixCreateRadixTree
SCRadixTree * SCRadixCreateRadixTree(void(*Free)(void *), void(*PrintData)(void *))
Creates a new Radix tree.
Definition: util-radix-tree.c:405
conf-yaml-loader.h
stream-tcp-private.h
conf.h
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:669
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:120
StringParseU8RangeCheck
int StringParseU8RangeCheck(uint8_t *res, int base, size_t len, const char *str, uint8_t min, uint8_t max)
Definition: util-byte.c:462
SCHInfoCleanResources
void SCHInfoCleanResources(void)
Definition: util-host-os-info.c:318
util-host-os-info.h
OS_POLICY_WINDOWS
@ OS_POLICY_WINDOWS
Definition: stream-tcp-reassemble.h:47
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
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 address.
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
@ OS_POLICY_BSD
Definition: stream-tcp-reassemble.h:37
OS_POLICY_OLD_SOLARIS
@ OS_POLICY_OLD_SOLARIS
Definition: stream-tcp-reassemble.h:41
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:331
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:962
ConfRestoreContextBackup
void ConfRestoreContextBackup(void)
Restores the backup of the hash_table present in backup_conf_hash back to conf_hash.
Definition: conf.c:679
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
FatalError
#define FatalError(...)
Definition: util-debug.h:502
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
ConfInit
void ConfInit(void)
Initialize the configuration system.
Definition: conf.c:120
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
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
OS_POLICY_WINDOWS2K3
@ OS_POLICY_WINDOWS2K3
Definition: stream-tcp-reassemble.h:49
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
ConfDeInit
void ConfDeInit(void)
De-initializes the configuration system.
Definition: conf.c:688
OS_POLICY_OLD_LINUX
@ OS_POLICY_OLD_LINUX
Definition: stream-tcp-reassemble.h:39
OS_POLICY_HPUX11
@ OS_POLICY_HPUX11
Definition: stream-tcp-reassemble.h:44
util-enum.h