use local private ip first as machine id

pull/10/head
Yuqing 2016-04-10 20:22:03 +08:00
parent 324d6db66f
commit f895a970c6
4 changed files with 48 additions and 10 deletions

View File

@ -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)

View File

@ -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;
}

View File

@ -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();

View File

@ -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)