[Spread-cvs] commit: r554 - trunk/daemon
jschultz at spread.org
jschultz at spread.org
Mon Mar 18 13:48:41 EDT 2013
Author: jschultz
Date: 2013-03-18 13:48:41 -0400 (Mon, 18 Mar 2013)
New Revision: 554
Added:
trunk/daemon/ip_enum.c
trunk/daemon/ip_enum.h
Modified:
trunk/daemon/Makefile.in
trunk/daemon/configuration.c
Log:
Change the way we look for the local daemon in the configuration file when no daemon name is specified. We now consult both the addresses returned by gethostbyname(gethostname()) and by directly querying the network interfaces.
Modified: trunk/daemon/Makefile.in
===================================================================
--- trunk/daemon/Makefile.in 2013-03-13 20:15:32 UTC (rev 553)
+++ trunk/daemon/Makefile.in 2013-03-18 17:48:41 UTC (rev 554)
@@ -49,11 +49,11 @@
TARGETS=spread$(EXEEXT) spmonitor$(EXEEXT)
-SPREADOBJS= spread.o protocol.o session.o groups.o membership.o network.o status.o log.o flow_control.o message.o lex.yy.o y.tab.o configuration.o acm.o acp-permit.o auth-null.o auth-ip.o
+SPREADOBJS= spread.o protocol.o session.o groups.o membership.o network.o status.o log.o flow_control.o message.o lex.yy.o y.tab.o configuration.o acm.o acp-permit.o auth-null.o auth-ip.o ip_enum.o
-MONITOR_OBJS= monitor.o lex.yy.o y.tab.o configuration.o acm.o
+MONITOR_OBJS= monitor.o lex.yy.o y.tab.o configuration.o ip_enum.o acm.o
-TMONITOR_OBJS= monitor.to lex.yy.to y.tab.to configuration.to acm.to
+TMONITOR_OBJS= monitor.to lex.yy.to y.tab.to configuration.to ip_enum.to acm.to
all: $(TARGETS)
Modified: trunk/daemon/configuration.c
===================================================================
--- trunk/daemon/configuration.c 2013-03-13 20:15:32 UTC (rev 553)
+++ trunk/daemon/configuration.c 2013-03-18 17:48:41 UTC (rev 554)
@@ -75,6 +75,7 @@
#include <assert.h>
#include "configuration.h"
+#include "ip_enum.h"
#define ext_conf_body
#include "conf_body.h"
@@ -440,26 +441,21 @@
/* Match my IP address to entry in configuration file */
if( my_name == NULL ){
- gethostname(machine_name,sizeof(machine_name));
- host_ptr = gethostbyname(machine_name);
- if( host_ptr == 0 )
- Alarm( EXIT, "Conf_load_conf_file: could not get my ip address (my name is %s)\n",
- machine_name );
- if (host_ptr->h_addrtype != AF_INET)
- Alarmp(SPLOG_FATAL, CONF_SYS, "Conf_load_conf_file: Sorry, cannot handle addr types other than IPv4\n");
- if (host_ptr->h_length != 4)
- Alarmp(SPLOG_FATAL, CONF_SYS, "Conf_load_conf_file: Bad IPv4 address length\n");
-
- i = -1; /* in case host_ptr->h_length == 0 */
- for (j = 0; host_ptr->h_addr_list[j] != NULL; j++) {
- memcpy(&My.id, host_ptr->h_addr_list[j], sizeof(struct in_addr));
- My.id = ntohl( My.id );
+ int32u *ips = Ip_enum_all();
+
+ i = -1;
+ for (j = 0; ips[j] != 0; j++) {
+ My.id = ntohl( ips[j] );
+ Alarmp( SPLOG_DEBUG, CONF_SYS, "Conf_load_conf_file: trying IP %d.%d.%d.%d\n", IP1(My.id), IP2(My.id), IP3(My.id), IP4(My.id) );
i = Conf_proc_by_id( My.id, &My );
if( i >= 0 ) break;
}
- if( i < 0 ) Alarm( EXIT,
- "Conf_load_conf_file: My proc id (%d.%d.%d.%d) is not in configuration\n", IP1(My.id),IP2(My.id),IP3(My.id),IP4(My.id) );
+ if ( i < 0 ) {
+ Alarm( EXIT, "Conf_load_conf_file: None of local IPs found in configuration\n" );
+ }
+ free(ips);
+
}else if( ! strcmp( my_name, "Monitor" ) ){
gethostname(machine_name,sizeof(machine_name));
host_ptr = gethostbyname(machine_name);
Added: trunk/daemon/ip_enum.c
===================================================================
--- trunk/daemon/ip_enum.c (rev 0)
+++ trunk/daemon/ip_enum.c 2013-03-18 17:48:41 UTC (rev 554)
@@ -0,0 +1,200 @@
+/*
+ * The Spread Toolkit.
+ *
+ * The contents of this file are subject to the Spread Open-Source
+ * License, Version 1.0 (the ``License''); you may not use
+ * this file except in compliance with the License. You may obtain a
+ * copy of the License at:
+ *
+ * http://www.spread.org/license/
+ *
+ * or in the file ``license.txt'' found in this distribution.
+ *
+ * Software distributed under the License is distributed on an AS IS basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Creators of Spread are:
+ * Yair Amir, Michal Miskin-Amir, Jonathan Stanton, John Schultz.
+ *
+ * Copyright (C) 1993-2012 Spread Concepts LLC <info at spreadconcepts.com>
+ *
+ * All Rights Reserved.
+ *
+ * Major Contributor(s):
+ * ---------------
+ * Ryan Caudy rcaudy at gmail.com - contributions to process groups.
+ * Claudiu Danilov claudiu at acm.org - scalable wide area support.
+ * Cristina Nita-Rotaru crisn at cs.purdue.edu - group communication security.
+ * Theo Schlossnagle jesus at omniti.com - Perl, autoconf, old skiplist.
+ * Dan Schoenblum dansch at cnds.jhu.edu - Java interface.
+ *
+ */
+
+#include <string.h>
+#include <netdb.h>
+
+#ifndef ARCH_PC_WIN95
+# include <net/if.h>
+# include <sys/ioctl.h>
+# include <netinet/in.h>
+# ifdef sun
+# include <sys/sockio.h> /* for SIOCGIFCONF */
+# endif
+#endif
+
+#include "ip_enum.h"
+#include "spu_alarm.h"
+
+#define MAX_IF 8192 /* max interfaces to look up */
+#define MIN_IF 10 /* must be able to look up at least this many */
+
+#ifdef ARCH_PC_WIN95
+typedef INTERFACE_INFO If_info;
+#else
+typedef struct ifreq If_info;
+# define closesocket(s) close(s)
+#endif
+
+#if defined(_SIZEOF_ADDR_IFREQ)
+# define NEXT_IF(ifr) (struct ifreq*)((char*)(ifr) + _SIZEOF_ADDR_IFREQ(*(ifr)))
+#else
+/* Assume fixed-length IFR's. WARNING: this may not be true for
+ all systems! If it has ifr_addr.sa_len, then we need to compute
+ ifr + sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len
+*/
+# define NEXT_IF(ifr) ((ifr) + 1)
+#endif
+
+struct Ip_Array
+{
+ int32u *ips;
+ int size;
+ int next;
+};
+
+static void Add_ip(struct Ip_Array *array, int32u ip)
+{
+ void *new_array;
+
+ if (array->next >= array->size) {
+
+ array->size = (array->size == 0 ? 4 : 2 * array->size);
+
+ if ((new_array = realloc(array->ips, array->size * sizeof(int32u))) == NULL) {
+ Alarm(EXIT, "Ip_enum_all: Failed to allocate memory\n");
+ }
+
+ array->ips = (int32u*) new_array;
+ }
+
+ array->ips[array->next++] = ip;
+}
+
+int32u *Ip_enum_all()
+{
+ struct Ip_Array array = { 0, 0, 0 };
+
+ struct hostent *host_ptr;
+ char machine_name[256] = { 0 };
+ int32u addr;
+
+ int s;
+ void *buffer;
+ int bufferSize;
+ int n;
+
+#ifdef ARCH_PC_WIN95
+ DWORD bytesReturned;
+ INTERFACE_INFO *ifr;
+#else
+ struct ifconf ifc;
+ struct ifreq *end;
+ struct ifreq *ifr;
+#endif
+
+ /* get IPs from hostname */
+
+ if (gethostname(machine_name, sizeof(machine_name)) == 0) {
+ host_ptr = gethostbyname(machine_name);
+
+ if (host_ptr != NULL && host_ptr->h_addrtype == AF_INET && host_ptr->h_length == 4) {
+
+ for (n = 0; host_ptr->h_addr_list[n] != NULL; n++) {
+
+ memcpy(&addr, host_ptr->h_addr_list[n], sizeof(addr));
+
+ if (addr != 0) {
+ Add_ip(&array, addr);
+ }
+ }
+ }
+ }
+
+ /* get IPs from interfaces */
+
+ if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) >= 0) {
+
+ bufferSize = sizeof(If_info) * MAX_IF;
+ if ((buffer = malloc(bufferSize)) == NULL) {
+ Alarm(EXIT, "Ip_enum_all: Failed to allocate memory\n");
+ }
+
+#ifdef ARCH_PC_WIN95
+ if (WSAIoctl(s, SIO_GET_INTERFACE_LIST, 0, 0, buffer, bufferSize,
+ &bytesReturned, 0, 0) == SOCKET_ERROR) {
+ Alarm(EXIT, "Ip_enum_all: Error getting interface list: %d\n", sock_errno);
+ }
+
+ ifr = (INTERFACE_INFO*)buffer;
+
+ for (n = bytesReturned / sizeof(INTERFACE_INFO); n; ifr++, n--) {
+ struct sockaddr_in *sa = &ifr->iiAddress.AddressIn;
+
+ if (sa->sin_family == AF_INET && sa->sin_addr.s_addr != 0) {
+ Add_ip(&array, sa->sin_addr.s_addr);
+ }
+ }
+
+#else /* !ARCH_PC_WIN95 */
+
+ ifc.ifc_len = bufferSize;
+ ifc.ifc_buf = buffer;
+
+ while (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
+# ifdef __linux__
+ /* Workaround for a failure to allocate kernel memory
+ of the same size as our 'buffer'. */
+ if (errno == ENOMEM) {
+ if (bufferSize / 2 >= MIN_IF * (int)sizeof(struct ifreq)) {
+ bufferSize /= 2;
+
+ ifc.ifc_len = bufferSize;
+ ifc.ifc_buf = buffer;
+ continue;
+ }
+ }
+# endif
+ Alarm(EXIT, "Ip_enum_all: Error enumerating IP addresses: %d\n", sock_errno);
+ }
+
+ end = (struct ifreq*)(ifc.ifc_buf + ifc.ifc_len - (sizeof(struct ifreq) - 1));
+
+ for (ifr = ifc.ifc_req; ifr < end; ifr = NEXT_IF(ifr)) {
+ struct sockaddr_in *sa = (struct sockaddr_in*)&ifr->ifr_addr;
+
+ if (sa->sin_family == AF_INET && sa->sin_addr.s_addr != 0) {
+ Add_ip(&array, sa->sin_addr.s_addr);
+ }
+ }
+#endif
+
+ closesocket(s);
+ free(buffer);
+ }
+
+ Add_ip(&array, 0); /* 0.0.0.0 ends the array */
+
+ return array.ips;
+}
Added: trunk/daemon/ip_enum.h
===================================================================
--- trunk/daemon/ip_enum.h (rev 0)
+++ trunk/daemon/ip_enum.h 2013-03-18 17:48:41 UTC (rev 554)
@@ -0,0 +1,42 @@
+/*
+ * The Spread Toolkit.
+ *
+ * The contents of this file are subject to the Spread Open-Source
+ * License, Version 1.0 (the ``License''); you may not use
+ * this file except in compliance with the License. You may obtain a
+ * copy of the License at:
+ *
+ * http://www.spread.org/license/
+ *
+ * or in the file ``license.txt'' found in this distribution.
+ *
+ * Software distributed under the License is distributed on an AS IS basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Creators of Spread are:
+ * Yair Amir, Michal Miskin-Amir, Jonathan Stanton, John Schultz.
+ *
+ * Copyright (C) 1993-2012 Spread Concepts LLC <info at spreadconcepts.com>
+ *
+ * All Rights Reserved.
+ *
+ * Major Contributor(s):
+ * ---------------
+ * Ryan Caudy rcaudy at gmail.com - contributions to process groups.
+ * Claudiu Danilov claudiu at acm.org - scalable wide area support.
+ * Cristina Nita-Rotaru crisn at cs.purdue.edu - group communication security.
+ * Theo Schlossnagle jesus at omniti.com - Perl, autoconf, old skiplist.
+ * Dan Schoenblum dansch at cnds.jhu.edu - Java interface.
+ *
+ */
+
+#ifndef INC_IP_ENUM
+#define INC_IP_ENUM
+
+#include "arch.h"
+
+int32u *Ip_enum_all(void);
+
+#endif
More information about the Spread-cvs
mailing list