use local private ip first as machine id
parent
324d6db66f
commit
f895a970c6
|
|
@ -49,8 +49,16 @@ int id_generator_init_ex(struct idg_context *context, const char *filename,
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const char *local_ip;
|
const char *local_ip;
|
||||||
|
const char *private_ip;
|
||||||
struct in_addr ip_addr;
|
struct in_addr ip_addr;
|
||||||
|
|
||||||
|
private_ip = get_first_local_private_ip();
|
||||||
|
if (private_ip != NULL)
|
||||||
|
{
|
||||||
|
local_ip = private_ip;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
local_ip = get_first_local_ip();
|
local_ip = get_first_local_ip();
|
||||||
if (local_ip == NULL)
|
if (local_ip == NULL)
|
||||||
{
|
{
|
||||||
|
|
@ -59,6 +67,13 @@ int id_generator_init_ex(struct idg_context *context, const char *filename,
|
||||||
context->fd = -1;
|
context->fd = -1;
|
||||||
return ENOENT;
|
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)
|
if (inet_pton(AF_INET, local_ip, &ip_addr) != 1)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,6 @@
|
||||||
#include "shared_func.h"
|
#include "shared_func.h"
|
||||||
#include "local_ip_func.h"
|
#include "local_ip_func.h"
|
||||||
|
|
||||||
#define LOCAL_LOOPBACK_IP "127.0.0.1"
|
|
||||||
|
|
||||||
int g_local_host_ip_count = 0;
|
int g_local_host_ip_count = 0;
|
||||||
char g_local_host_ip_addrs[FAST_MAX_LOCAL_IP_ADDRS * \
|
char g_local_host_ip_addrs[FAST_MAX_LOCAL_IP_ADDRS * \
|
||||||
IP_ADDRESS_SIZE];
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@
|
||||||
#define FAST_IF_ALIAS_PREFIX_MAX_SIZE 32
|
#define FAST_IF_ALIAS_PREFIX_MAX_SIZE 32
|
||||||
#define FAST_MAX_LOCAL_IP_ADDRS 16
|
#define FAST_MAX_LOCAL_IP_ADDRS 16
|
||||||
|
|
||||||
|
#define LOCAL_LOOPBACK_IP "127.0.0.1"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -35,6 +37,8 @@ bool is_local_host_ip(const char *client_ip);
|
||||||
const char *get_first_local_ip();
|
const char *get_first_local_ip();
|
||||||
const char *get_next_local_ip(const char *previous_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);
|
int insert_into_local_host_ip(const char *client_ip);
|
||||||
void log_local_host_ip_addrs();
|
void log_local_host_ip_addrs();
|
||||||
void print_local_host_ip_addrs();
|
void print_local_host_ip_addrs();
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ int main(int argc, char *argv[])
|
||||||
int result;
|
int result;
|
||||||
int i;
|
int i;
|
||||||
int64_t id;
|
int64_t id;
|
||||||
const int machine_id = 192;
|
const int machine_id = 0;
|
||||||
const int mid_bits = 8;
|
const int mid_bits = 8;
|
||||||
|
|
||||||
log_init();
|
log_init();
|
||||||
|
|
@ -36,7 +36,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
//id_generator_next(&context, &id);
|
//id_generator_next(&context, &id);
|
||||||
//printf("id: %"PRId64", %016llX\n", id, 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);
|
result = id_generator_next(&context, &id);
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue