From f895a970c6cd29a02ed5f76299af89b3751bad67 Mon Sep 17 00:00:00 2001 From: Yuqing Date: Sun, 10 Apr 2016 20:22:03 +0800 Subject: [PATCH] use local private ip first as machine id --- src/id_generator.c | 27 +++++++++++++++++++++------ src/local_ip_func.c | 23 +++++++++++++++++++++-- src/local_ip_func.h | 4 ++++ src/tests/test_id_generator.c | 4 ++-- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/id_generator.c b/src/id_generator.c index 3c30eb3..a5fd9ea 100644 --- a/src/id_generator.c +++ b/src/id_generator.c @@ -49,15 +49,30 @@ int id_generator_init_ex(struct idg_context *context, const char *filename, else { const char *local_ip; + const char *private_ip; struct in_addr ip_addr; - local_ip = get_first_local_ip(); - if (local_ip == NULL) + private_ip = get_first_local_private_ip(); + if (private_ip != NULL) { - logError("file: "__FILE__", line: %d, " - "can't get local ip address", __LINE__); - context->fd = -1; - return ENOENT; + local_ip = private_ip; + } + else + { + local_ip = get_first_local_ip(); + if (local_ip == NULL) + { + logError("file: "__FILE__", line: %d, " + "can't get local ip address", __LINE__); + context->fd = -1; + return ENOENT; + } + else if (strcmp(local_ip, LOCAL_LOOPBACK_IP) == 0) + { + logWarning("file: "__FILE__", line: %d, " + "can't get local ip address, set to %s", + __LINE__, LOCAL_LOOPBACK_IP); + } } if (inet_pton(AF_INET, local_ip, &ip_addr) != 1) diff --git a/src/local_ip_func.c b/src/local_ip_func.c index 068ec5a..4817fa0 100644 --- a/src/local_ip_func.c +++ b/src/local_ip_func.c @@ -14,8 +14,6 @@ #include "shared_func.h" #include "local_ip_func.h" -#define LOCAL_LOOPBACK_IP "127.0.0.1" - int g_local_host_ip_count = 0; char g_local_host_ip_addrs[FAST_MAX_LOCAL_IP_ADDRS * \ IP_ADDRESS_SIZE]; @@ -180,3 +178,24 @@ const char *get_first_local_ip() } } +const char *get_first_local_private_ip() +{ + const char *ip; + + ip = NULL; + do + { + ip = get_next_local_ip(ip); + if (ip == NULL) + { + return NULL; + } + if (is_private_ip(ip)) + { + return ip; + } + } while (1); + + return NULL; +} + diff --git a/src/local_ip_func.h b/src/local_ip_func.h index 67ef721..4acd6c9 100644 --- a/src/local_ip_func.h +++ b/src/local_ip_func.h @@ -20,6 +20,8 @@ #define FAST_IF_ALIAS_PREFIX_MAX_SIZE 32 #define FAST_MAX_LOCAL_IP_ADDRS 16 +#define LOCAL_LOOPBACK_IP "127.0.0.1" + #ifdef __cplusplus extern "C" { #endif @@ -35,6 +37,8 @@ bool is_local_host_ip(const char *client_ip); const char *get_first_local_ip(); const char *get_next_local_ip(const char *previous_ip); +const char *get_first_local_private_ip(); + int insert_into_local_host_ip(const char *client_ip); void log_local_host_ip_addrs(); void print_local_host_ip_addrs(); diff --git a/src/tests/test_id_generator.c b/src/tests/test_id_generator.c index 02b7e33..2c4f130 100644 --- a/src/tests/test_id_generator.c +++ b/src/tests/test_id_generator.c @@ -20,7 +20,7 @@ int main(int argc, char *argv[]) int result; int i; int64_t id; - const int machine_id = 192; + const int machine_id = 0; const int mid_bits = 8; log_init(); @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) //id_generator_next(&context, &id); //printf("id: %"PRId64", %016llX\n", id, id); - for (i=0; i<100000; i++) + for (i=0; i<10000; i++) { result = id_generator_next(&context, &id); if (result != 0)