summaryrefslogtreecommitdiff
path: root/src/osfp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/osfp.c')
-rw-r--r--src/osfp.c167
1 files changed, 10 insertions, 157 deletions
diff --git a/src/osfp.c b/src/osfp.c
index 1a23342..c43041f 100644
--- a/src/osfp.c
+++ b/src/osfp.c
@@ -1,3 +1,7 @@
+#include <sys/fcntl.h>
+
+#include "cJSON.h"
+
#include "osfp_common.h"
#include "osfp.h"
@@ -5,67 +9,6 @@
#include "osfp_score_db.h"
#include "osfp_log.h"
-#define OSFP_LOWEST_SCORE_LIMIT 20
-
-static struct osfp_result *osfp_result_build(struct osfp_os_class_score *os_class_score, const char *matched)
-{
- int i;
- unsigned int tmp_score;
- unsigned int likely_score;
- enum osfp_os_class_id likely_os_class;
- struct osfp_result *result;
-
- result = calloc(1, sizeof(struct osfp_result));
- if (result == NULL) {
- goto exit;
- }
-
- likely_score = 0;
- likely_os_class = OSFP_OS_CLASS_OTHERS;
-
- // likely os score
- for (i = 0; i < OSFP_OS_CLASS_MAX; i++) {
- tmp_score = os_class_score->scores[i];
-
- if (likely_score < tmp_score) {
- likely_score = tmp_score;
- likely_os_class = i;
- }
- result->details[i].score = tmp_score;
- }
-
- if (likely_score == OSFP_PERCENTILE) {
- // prefiltered
- ;
- } else if (likely_score < OSFP_LOWEST_SCORE_LIMIT) {
- // too low to tell os class
- likely_os_class = OSFP_OS_CLASS_OTHERS;
- } else {
- // when the tied likely scores appear between win/apple-like/unix-like, we throw unknown
- for (i = 0; i < OSFP_OS_CLASS_MAX; i++) {
- if (likely_os_class == i) {
- continue;
- }
- if (likely_score == os_class_score->scores[i]) {
- if (likely_os_class == OSFP_OS_CLASS_LINUX && i == OSFP_OS_CLASS_ANDROID) {
- continue;
- } else if (likely_os_class == OSFP_OS_CLASS_MAC_OS && i == OSFP_OS_CLASS_IOS) {
- continue;
- } else {
- likely_os_class = OSFP_OS_CLASS_UNKNOWN;
- break;
- }
- }
- }
- }
-
- result->likely_os_class = likely_os_class;
- result->matched = matched;
- return result;
-exit:
- return NULL;
-}
-
const char *osfp_result_os_name_get(struct osfp_result *result)
{
enum osfp_os_class_id os_class;
@@ -157,7 +100,7 @@ void osfp_result_free(struct osfp_result *result)
}
}
-struct osfp_result *osfp_ipv4_identify(struct osfp_db *db, struct iphdr* l3_hdr, struct tcphdr *l4_hdr, size_t l4_hdr_len)
+struct osfp_result *osfp_ip_identify(struct osfp_db *db, unsigned char *l3_hdr, unsigned char *l4_hdr, unsigned int l4_hdr_len, unsigned int ip_version)
{
int ret;
struct osfp_fingerprint fp;
@@ -165,39 +108,24 @@ struct osfp_result *osfp_ipv4_identify(struct osfp_db *db, struct iphdr* l3_hdr,
struct osfp_result *result;
const char *matched;
- osfp_profile_cycle(c1);
- osfp_profile_cycle(c2);
-
if (db == NULL || l3_hdr == NULL || l4_hdr == NULL || l4_hdr_len == 0) {
goto exit;
}
- osfp_profile_get_cycle(c1);
- ret = osfp_fingerprinting((unsigned char *)l3_hdr, (unsigned char *)l4_hdr, (unsigned int)l4_hdr_len, &fp, 4);
- osfp_profile_get_cycle(c2);
- osfp_profile_counter_update(&osfp_profile_fingerprinting, c2 - c1);
+ ret = osfp_fingerprinting(l3_hdr, l4_hdr, l4_hdr_len, &fp, ip_version);
if (ret != 0) {
goto exit;
}
- osfp_profile_get_cycle(c1);
matched = osfp_score_db_prefilter(db->score_db, &fp, &os_class_score);
- osfp_profile_get_cycle(c2);
- osfp_profile_counter_update(&osfp_profile_prefilter, c2 - c1);
if (matched == NULL) {
- osfp_profile_get_cycle(c1);
ret = osfp_score_db_score(db->score_db, 0, &fp, &os_class_score);
- osfp_profile_get_cycle(c2);
- osfp_profile_counter_update(&osfp_profile_score, c2 - c1);
if (ret != 0) {
goto exit;
}
}
- osfp_profile_get_cycle(c1);
result = osfp_result_build(&os_class_score, matched);
- osfp_profile_get_cycle(c2);
- osfp_profile_counter_update(&osfp_profile_result_build, c2 - c1);
if (result == NULL) {
goto exit;
}
@@ -207,89 +135,14 @@ exit:
return NULL;
}
-struct osfp_result *osfp_ipv6_identify(struct osfp_db *db, struct ip6_hdr* l3_hdr, struct tcphdr *l4_hdr, size_t l4_hdr_len)
+struct osfp_result *osfp_ipv4_identify(struct osfp_db *db, struct iphdr* l3_hdr, struct tcphdr *l4_hdr, size_t l4_hdr_len)
{
- int ret;
- struct osfp_fingerprint fp;
- struct osfp_os_class_score os_class_score;
- struct osfp_result *result;
- const char *matched;
-
- osfp_profile_cycle(c1);
- osfp_profile_cycle(c2);
-
- if (db == NULL || l3_hdr == NULL || l4_hdr == NULL || l4_hdr_len == 0) {
- goto exit;
- }
-
- osfp_profile_get_cycle(c1);
- ret = osfp_fingerprinting((unsigned char *)l3_hdr, (unsigned char *)l4_hdr, (unsigned int)l4_hdr_len, &fp, 6);
- osfp_profile_get_cycle(c2);
- osfp_profile_counter_update(&osfp_profile_fingerprinting, c2 - c1);
- if (ret != 0) {
- goto exit;
- }
-
- osfp_profile_get_cycle(c1);
- matched = osfp_score_db_prefilter(db->score_db, &fp, &os_class_score);
- osfp_profile_get_cycle(c2);
- osfp_profile_counter_update(&osfp_profile_prefilter, c2 - c1);
- if (matched == NULL) {
- osfp_profile_get_cycle(c1);
- ret = osfp_score_db_score(db->score_db, 0, &fp, &os_class_score);
- osfp_profile_get_cycle(c2);
- osfp_profile_counter_update(&osfp_profile_score, c2 - c1);
- if (ret != 0) {
- goto exit;
- }
- }
-
- osfp_profile_get_cycle(c1);
- result = osfp_result_build(&os_class_score, matched);
- osfp_profile_get_cycle(c2);
- osfp_profile_counter_update(&osfp_profile_result_build, c2 - c1);
- if (result == NULL) {
- goto exit;
- }
-
- return result;
-exit:
- return NULL;
+ return osfp_ip_identify(db, (unsigned char *)l3_hdr, (unsigned char *)l4_hdr, (unsigned int)l4_hdr_len, 4);
}
-struct osfp_result *osfp_json_identify(struct osfp_db *db, const char *json_str)
+struct osfp_result *osfp_ipv6_identify(struct osfp_db *db, struct ip6_hdr* l3_hdr, struct tcphdr *l4_hdr, size_t l4_hdr_len)
{
- int ret = OSFP_EINVAL;
- struct osfp_fingerprint fp;
- struct osfp_os_class_score os_class_score;
- struct osfp_result *result;
- const char *matched;
-
- if (db == NULL || json_str == NULL) {
- goto exit;
- }
-
- ret = osfp_fingerprint_from_json(&fp, (char *)json_str);
- if (ret != 0) {
- goto exit;
- }
-
- matched = osfp_score_db_prefilter(db->score_db, &fp, &os_class_score);
- if (matched == NULL) {
- ret = osfp_score_db_score(db->score_db, 0, &fp, &os_class_score);
- if (ret != 0) {
- goto exit;
- }
- }
-
- result = osfp_result_build(&os_class_score, matched);
- if (result == NULL) {
- goto exit;
- }
-
- return result;
-exit:
- return NULL;
+ return osfp_ip_identify(db, (unsigned char *)l3_hdr, (unsigned char *)l4_hdr, (unsigned int)l4_hdr_len, 6);
}
struct osfp_db *osfp_db_new(const char *fp_path)