On Sun, 28 Mar 2010 21:14:35 +0900 FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp> wrote: > This is against the next branch. > > = > From: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp> > Date: Sun, 28 Mar 2010 21:10:43 +0900 > Subject: [PATCH] get ip address from corosync > > Better get the ip address info from corosync instead of trying to get > it in a hacky way. > > Signed-off-by: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp> > --- > collie/Makefile | 2 +- > collie/group.c | 89 +++++++++++++++++++++++++++---------------------------- Oops, this is the correct patch. = >From ccd39f0c72455519785436cbed696f0fab654fb8 Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp> Date: Sun, 28 Mar 2010 21:32:17 +0900 Subject: [PATCH] get ip address from corosync Signed-off-by: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp> --- collie/Makefile | 2 +- collie/group.c | 94 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/collie/Makefile b/collie/Makefile index 2bfc9c6..c2f7eb9 100644 --- a/collie/Makefile +++ b/collie/Makefile @@ -2,7 +2,7 @@ sbindir ?= $(PREFIX)/sbin CFLAGS += -g -O2 -Wall -Wstrict-prototypes -I../include CFLAGS += -D_GNU_SOURCE -DSD_VERSION=\"$(VERSION)\" -LIBS += -lpthread -lcpg +LIBS += -lpthread -lcpg -lcfg PROGRAMS = collie COLLIE_OBJS = collie.o net.o vdi.o group.o store.o work.o ../lib/event.o ../lib/net.o ../lib/logger.o diff --git a/collie/group.c b/collie/group.c index 2067870..e82a009 100644 --- a/collie/group.c +++ b/collie/group.c @@ -12,8 +12,10 @@ #include <stdlib.h> #include <unistd.h> #include <netdb.h> +#include <arpa/inet.h> #include <sys/time.h> #include <corosync/cpg.h> +#include <corosync/cfg.h> #include "sheepdog_proto.h" #include "collie.h" @@ -981,12 +983,56 @@ int build_node_list(struct list_head *node_list, return nr; } +static void set_addr(unsigned int nodeid) +{ + int ret, nr; + corosync_cfg_handle_t handle; + corosync_cfg_node_address_t addr; + struct sockaddr_storage *ss = (struct sockaddr_storage *)addr.address; + struct sockaddr_in *sin = (struct sockaddr_in *)addr.address; + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr.address; + void *saddr; + char tmp[INET6_ADDRSTRLEN]; + + memset(sys->this_node.addr, 0, sizeof(sys->this_node.addr)); + + ret = corosync_cfg_initialize(&handle, NULL); + if (ret != CS_OK) { + vprintf(SDOG_ERR "failed to initiazize cfg %d\n", ret); + exit(1); + } + + ret = corosync_cfg_get_node_addrs(handle, nodeid, 1, &nr, &addr); + if (ret != CS_OK) { + vprintf(SDOG_ERR "failed to get addr %d\n", ret); + exit(1); + } + + if (!nr) { + vprintf(SDOG_ERR "we got no address\n"); + exit(1); + } + + if (ss->ss_family == AF_INET6) { + saddr = &sin6->sin6_addr; + memcpy(sys->this_node.addr, saddr, 16); + } else if (ss->ss_family == AF_INET) { + saddr = &sin->sin_addr; + memcpy(sys->this_node.addr + 12, saddr, 16); + } else { + vprintf(SDOG_ERR "unknown protocol %d\n", ss->ss_family); + exit(1); + } + + inet_ntop(ss->ss_family, saddr, tmp, sizeof(tmp)); + + vprintf(SDOG_INFO "addr = %s\n", tmp); +} + int create_cluster(int port) { int fd, ret; cpg_handle_t cpg_handle; - struct addrinfo hints, *res, *res0; - char name[INET6_ADDRSTRLEN]; struct cpg_name group = { 8, "sheepdog" }; cpg_callbacks_t cb = { &sd_deliver, &sd_confch }; unsigned int nodeid = 0; @@ -1028,49 +1074,7 @@ join_retry: sys->this_nodeid = nodeid; sys->this_pid = getpid(); - gethostname(name, sizeof(name)); - - memset(&hints, 0, sizeof(hints)); - - hints.ai_socktype = SOCK_STREAM; - ret = getaddrinfo(name, NULL, &hints, &res0); - if (ret) - exit(1); - - for (res = res0; res; res = res->ai_next) { - if (res->ai_family == AF_INET) { - struct sockaddr_in *addr; - addr = (struct sockaddr_in *)res->ai_addr; - - if (((char *) &addr->sin_addr)[0] == 127) - continue; - - memset(sys->this_node.addr, 0, 12); - memcpy(sys->this_node.addr + 12, &addr->sin_addr, 4); - break; - } else if (res->ai_family == AF_INET6) { - struct sockaddr_in6 *addr; - uint8_t localhost[16] = { 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 1 }; - - addr = (struct sockaddr_in6 *)res->ai_addr; - - if (memcmp(&addr->sin6_addr, localhost, 16) == 0) - continue; - - memcpy(sys->this_node.addr, &addr->sin6_addr, 16); - break; - } else - dprintf("unknown address family\n"); - } - - if (res == NULL) { - eprintf("failed to get address info\n"); - return -1; - } - - freeaddrinfo(res0); - + set_addr(nodeid); sys->this_node.port = port; hval = fnv_64a_buf(&sys->this_node.port, sizeof(sys->this_node.port), -- 1.7.0 |