[PATCH 14/30] Prepare ft.c for Windows support


Michael Schonberg <mschonberg@...>
 

Use MAMACALLTYPE (__stdcall for windows) for callbacks. Move os dependent
network calls to common/.../_os_/. Fixed some compiler warnings.

Signed-off-by: Mike Schonberg <mschonberg@nyx.com>
---
common/c_cpp/configure.ac | 1 +
common/c_cpp/src/c/Makefile.am | 1 +
common/c_cpp/src/c/linux/network.c | 149 +++++++++++++++++++++++++++
common/c_cpp/src/c/linux/port.h | 2 +-
common/c_cpp/src/c/wombat/wCommon.h | 2 +
mama/c_cpp/src/c/ft.c | 189 ++++++-----------------------------
mama/c_cpp/src/c/mama/ft.h | 4 +-
7 files changed, 187 insertions(+), 161 deletions(-)
create mode 100644 common/c_cpp/src/c/linux/network.c

diff --git a/common/c_cpp/configure.ac b/common/c_cpp/configure.ac
index 39b145f..115602e 100755
--- a/common/c_cpp/configure.ac
+++ b/common/c_cpp/configure.ac
@@ -50,6 +50,7 @@ case $build_os in
src/c/machine_port.c:src/c/linux/machine.c
src/c/platform.c:src/c/linux/platform.c
src/c/wSemaphore.c:src/c/linux/wSemaphore.c
+ src/c/network.c:src/c/linux/network.c
src/c/environment.c:src/c/linux/environment.c]) ;;
esac

diff --git a/common/c_cpp/src/c/Makefile.am b/common/c_cpp/src/c/Makefile.am
index 362d457..9258c21 100644
--- a/common/c_cpp/src/c/Makefile.am
+++ b/common/c_cpp/src/c/Makefile.am
@@ -66,6 +66,7 @@ libwombatcommon_la_SOURCES = \
list.c \
lookup2.c \
machine.c \
+ network.c \
machine_port.c \
platform.c \
properties.l \
diff --git a/common/c_cpp/src/c/linux/network.c b/common/c_cpp/src/c/linux/network.c
new file mode 100644
index 0000000..f5923d9
--- /dev/null
+++ b/common/c_cpp/src/c/linux/network.c
@@ -0,0 +1,149 @@
+
+/*
+ * OpenMAMA: The open middleware agnostic messaging API
+ * Copyright (C) 2011 NYSE Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include "wombat/wCommon.h"
+
+
+struct in_addr resolve_ip (const char * arg)
+{
+
+ struct in_addr resolved;
+ struct in_addr addr;
+ unsigned long netip = 0;
+ const char * ifname = arg;
+ int sock = -1;
+ int i=0;
+ unsigned numreqs=0;
+ struct ifconf ifc;
+ struct ifreq *ifr;
+ resolved = inet_makeaddr(0,0);
+ addr = inet_makeaddr(0,0);
+
+
+ if (!arg || (strlen(arg)==0))
+ {
+ resolved.s_addr = INADDR_ANY;
+ return resolved;
+ }
+ /* 1. try interpreting as network or host IP address */
+ {
+ netip = inet_network (arg);
+ if (netip != (unsigned long)(-1))
+ {
+ addr = inet_makeaddr(netip, 0);
+ }
+ }
+ /* 2. try interpreting as network name */
+ if (! addr.s_addr)
+ {
+ struct netent * net = getnetbyname (arg);
+ if ((net != NULL) && (net->n_addrtype == AF_INET))
+ {
+ addr = inet_makeaddr (net->n_net, 0);
+ }
+ }
+ /* 3. try interpreting as host name */
+ if (! addr.s_addr)
+ {
+ struct hostent * host = gethostbyname (arg);
+ if ((host != NULL) && (host->h_addrtype == AF_INET))
+ {
+ addr = *(struct in_addr *)(host->h_addr_list[0]);
+ }
+ }
+
+ /* 4. try interpreting as a NIC interface name */
+ numreqs = 30;
+ sock = socket(AF_INET, SOCK_DGRAM, 0);
+
+ ifc.ifc_buf = NULL;
+ for (;;)
+ {
+ ifc.ifc_len = sizeof(struct ifreq) * numreqs;
+ ifc.ifc_buf = (char *)malloc(ifc.ifc_len);
+ if (ioctl(sock, SIOCGIFCONF, &ifc) < 0)
+ {
+ perror ("SIOCGIFCONF");
+ resolved.s_addr = (unsigned long) -1;
+ return resolved;
+ }
+ if (ifc.ifc_len == (sizeof(struct ifreq) * numreqs))
+ {
+ numreqs *= 2;
+ continue;
+ }
+ break;
+ }
+
+ ifr = ifc.ifc_req;
+ for (i = 0; i < ifc.ifc_len; i += sizeof(struct ifreq))
+ {
+ struct sockaddr_in hostaddr;
+ struct sockaddr_in netmask;
+ struct in_addr network = inet_makeaddr(0,0);
+
+ hostaddr.sin_addr = inet_makeaddr(0,0);
+ netmask.sin_addr = inet_makeaddr(0,0);
+
+ if (ioctl(sock, SIOCGIFADDR, ifr) < 0)
+ {
+ perror ("SIOCGIFADDR");
+ resolved.s_addr = (unsigned long)-1;
+ return resolved;
+ }
+ memcpy (&hostaddr, &(ifr->ifr_addr), sizeof(struct sockaddr_in));
+
+ if (ioctl(sock, SIOCGIFNETMASK, ifr) < 0)
+ {
+ perror ("SIOCGIFNETMASK");
+ resolved.s_addr = (unsigned long)-1;
+ return resolved;
+ }
+ memcpy (&netmask, &(ifr->ifr_addr), sizeof (struct sockaddr_in));
+ network.s_addr = hostaddr.sin_addr.s_addr & netmask.sin_addr.s_addr;
+ if (addr.s_addr)
+ {
+ if (addr.s_addr == hostaddr.sin_addr.s_addr)
+ {
+ resolved = hostaddr.sin_addr;
+ break;
+ }
+ else if (addr.s_addr == network.s_addr)
+ {
+ resolved = hostaddr.sin_addr;
+ break;
+ }
+ }
+
+ if (strcmp(ifr->ifr_name, ifname) == 0)
+ {
+ resolved = hostaddr.sin_addr;
+ break;
+ }
+ ifr++;
+ }
+
+ if (!resolved.s_addr)
+ {
+ resolved.s_addr = (unsigned long)-1;
+ }
+ return (resolved);
+}
diff --git a/common/c_cpp/src/c/linux/port.h b/common/c_cpp/src/c/linux/port.h
index 9e77781..86d2e50 100644
--- a/common/c_cpp/src/c/linux/port.h
+++ b/common/c_cpp/src/c/linux/port.h
@@ -113,7 +113,7 @@ int wsem_timedwait (wsem_t* sem, unsigned int ts);

/* Windows does not support AF_UNIX sockets, socketpairs, etc */
#define wsocketpair(dom, type, prot, pair) (socketpair((dom),(type),(prot),(pair)))
-#define wsetnonblock(s) (fcntl((s), F_SETFL, O_NONBLOCK))
+#define wsetnonblock(s) (fcntl((s), F_SETFL, fcntl((s), F_GETFL) | O_NONBLOCK))

#define CPU_AFFINITY_SET cpu_set_t

diff --git a/common/c_cpp/src/c/wombat/wCommon.h b/common/c_cpp/src/c/wombat/wCommon.h
index 93c99d1..c40112f 100644
--- a/common/c_cpp/src/c/wombat/wCommon.h
+++ b/common/c_cpp/src/c/wombat/wCommon.h
@@ -25,4 +25,6 @@
/* Moved to _os_/port.h */
#include "port.h"

+struct in_addr resolve_ip(const char * arg);
+
#endif /* _WOMBAT_WCOMMON_H */
diff --git a/mama/c_cpp/src/c/ft.c b/mama/c_cpp/src/c/ft.c
index 700a022..1948650 100644
--- a/mama/c_cpp/src/c/ft.c
+++ b/mama/c_cpp/src/c/ft.c
@@ -163,10 +163,10 @@ typedef struct mamaFtMemberImpl_
/*****************************************
* General callback functions
******************************************/
-static void
+static void MAMACALLTYPE
ftHeartbeatTimerCb (mamaTimer timer, void* closure);

-static void
+static void MAMACALLTYPE
ftTimeoutTimerCb (mamaTimer timer, void* closure);


@@ -178,7 +178,7 @@ static char* genInstanceId (const char* groupName);
static char* genSymbol (const char* groupName);

/* A callback function that does nothing. */
-static void
+static void MAMACALLTYPE
mamaFtCallbackNoOp (
mamaFtMember ftMember,
const char* groupName,
@@ -188,7 +188,7 @@ mamaFtCallbackNoOp (
}

/* FT callbacks: */
-static void
+static void MAMACALLTYPE
ioCallback (mamaIo io, mamaIoType ioType, void* closure);

static mamaIo gReadHandler = NULL;
@@ -196,27 +196,27 @@ static mamaIo gReadHandler = NULL;
/* General implementation functions. */
int receiveHeartbeat (mamaFtMemberImpl* impl);

- void checkHeartbeat (mamaFtMemberImpl* impl,
- int hbWeight,
- int hbIncarnation,
- int hbPid,
- int hbIpAddr,
- int hbPrimary,
- char *hbGroupName);
+void checkHeartbeat (mamaFtMemberImpl* impl,
+ unsigned int hbWeight,
+ unsigned int hbIncarnation,
+ int hbPid,
+ int hbIpAddr,
+ int hbPrimary,
+ char* hbGroupName);

struct in_addr resolve_ip (const char * arg);

/* FT callbacks: */
-static void
+static void MAMACALLTYPE
ftSubCreateCb (mamaSubscription subscription, void* closure);

-static void
+static void MAMACALLTYPE
ftSubErrorCb (mamaSubscription subscription,
mama_status status,
void* platformError,
const char* subject,
void* closure);
-static void
+static void MAMACALLTYPE
ftSubMsgCb (mamaSubscription subscription,
mamaMsg msg,
void* closure,
@@ -608,7 +608,7 @@ static void checkState (mamaFtMemberImpl* impl,
}
}

-static void
+static void MAMACALLTYPE
ftHeartbeatTimerCb (mamaTimer timer,
void* closure)
{
@@ -617,7 +617,7 @@ ftHeartbeatTimerCb (mamaTimer timer,
impl->ftSendHeartbeat (impl);
}

-static void
+static void MAMACALLTYPE
ftTimeoutTimerCb (mamaTimer timer,
void* closure)
{
@@ -665,7 +665,8 @@ mama_status resetTimeoutTimer (mamaFtMemberImpl* impl)
return mamaTimer_reset (impl->myTimeoutTimer);
}

-int betterCredentials(mamaFtMemberImpl* impl, int weight, int incarnation, in_addr_t ipAddr, int pid)
+int betterCredentials(mamaFtMemberImpl* impl, unsigned int weight,
+ unsigned int incarnation, in_addr_t ipAddr, int pid)
{
if (weight > impl->myWeight)
{
@@ -697,14 +698,14 @@ int betterCredentials(mamaFtMemberImpl* impl, int weight, int incarnation, in_a
}


- void checkHeartbeat (mamaFtMemberImpl* impl,
- int hbWeight,
- int hbIncarnation,
- int hbPid,
- int hbIpAddr,
- int hbPrimary,
- char *hbGroupName)
- {
+void checkHeartbeat (mamaFtMemberImpl* impl,
+ unsigned int hbWeight,
+ unsigned int hbIncarnation,
+ int hbPid,
+ int hbIpAddr,
+ int hbPrimary,
+ char* hbGroupName)
+{

if (strcmp (hbGroupName, impl->myGroupName) != 0)
{
@@ -745,132 +746,6 @@ int betterCredentials(mamaFtMemberImpl* impl, int weight, int incarnation, in_a
}
}

-struct in_addr resolve_ip (const char * arg)
-{
-
- struct in_addr resolved;
- struct in_addr addr;
- unsigned long netip = 0;
- const char * ifname = arg;
- int sock = -1;
- int i=0;
- unsigned numreqs=0;
- struct ifconf ifc;
- struct ifreq *ifr;
- resolved = inet_makeaddr(0,0);
- addr = inet_makeaddr(0,0);
-
-
- if (!arg || (strlen(arg)==0))
- {
- resolved.s_addr = INADDR_ANY;
- return resolved;
- }
- /* 1. try interpreting as network or host IP address */
- {
- netip = inet_network (arg);
- if (netip != (unsigned long)(-1))
- {
- addr = inet_makeaddr(netip, 0);
- }
- }
- /* 2. try interpreting as network name */
- if (! addr.s_addr)
- {
- struct netent * net = getnetbyname (arg);
- if ((net != NULL) && (net->n_addrtype == AF_INET))
- {
- addr = inet_makeaddr (net->n_net, 0);
- }
- }
- /* 3. try interpreting as host name */
- if (! addr.s_addr)
- {
- struct hostent * host = gethostbyname (arg);
- if ((host != NULL) && (host->h_addrtype == AF_INET))
- {
- addr = *(struct in_addr *)(host->h_addr_list[0]);
- }
- }
-
- /* 4. try interpreting as a NIC interface name */
- numreqs = 30;
- sock = socket(AF_INET, SOCK_DGRAM, 0);
-
- ifc.ifc_buf = NULL;
- for (;;)
- {
- ifc.ifc_len = sizeof(struct ifreq) * numreqs;
- ifc.ifc_buf = (char *)malloc(ifc.ifc_len);
- if (ioctl(sock, SIOCGIFCONF, &ifc) < 0)
- {
- perror ("SIOCGIFCONF");
- resolved.s_addr = INADDR_NONE;
- return resolved;
- }
- if (ifc.ifc_len == (sizeof(struct ifreq) * numreqs))
- {
- numreqs *= 2;
- continue;
- }
- break;
- }
-
- ifr = ifc.ifc_req;
- for (i = 0; i < ifc.ifc_len; i += sizeof(struct ifreq))
- {
- struct sockaddr_in hostaddr;
- struct sockaddr_in netmask;
- struct in_addr network = inet_makeaddr(0,0);
-
- hostaddr.sin_addr = inet_makeaddr(0,0);
- netmask.sin_addr = inet_makeaddr(0,0);
-
- if (ioctl(sock, SIOCGIFADDR, ifr) < 0)
- {
- perror ("SIOCGIFADDR");
- resolved.s_addr = INADDR_NONE;
- return resolved;
- }
- memcpy (&hostaddr, &(ifr->ifr_addr), sizeof(struct sockaddr_in));
-
- if (ioctl(sock, SIOCGIFNETMASK, ifr) < 0)
- {
- perror ("SIOCGIFNETMASK");
- resolved.s_addr = INADDR_NONE;
- return resolved;
- }
- memcpy (&netmask, &(ifr->ifr_addr), sizeof (struct sockaddr_in));
- network.s_addr = hostaddr.sin_addr.s_addr & netmask.sin_addr.s_addr;
- if (addr.s_addr)
- {
- if (addr.s_addr == hostaddr.sin_addr.s_addr)
- {
- resolved = hostaddr.sin_addr;
- break;
- }
- else if (addr.s_addr == network.s_addr)
- {
- resolved = hostaddr.sin_addr;
- break;
- }
- }
-
- if (strcmp(ifr->ifr_name, ifname) == 0)
- {
- resolved = hostaddr.sin_addr;
- break;
- }
- ifr++;
- }
-
- if (!resolved.s_addr)
- {
- resolved.s_addr = INADDR_NONE;
- }
- return (resolved);
-}
-
/****************************************************************************
* Multicast FT specific
******************************************************************************/
@@ -990,7 +865,7 @@ multicastFt_setup (
ftTtl = multicastFt_getProperty (propertyName,
"mama.native.transport.%s.ttl", transportName);
if (ftTtl != NULL)
- ttl = atol (ftTtl);
+ ttl = (unsigned char)atol (ftTtl);
}

iorecvstr = multicastFt_getProperty (propertyName,
@@ -1055,7 +930,7 @@ multicastFt_setup (
"MAMA multicast FT: multicast socket failed %s",strerror(errno));
return MAMA_STATUS_SYSTEM_ERROR;
}
- iRet = fcntl (sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK);
+ iRet = wsetnonblock(sock);
if (iRet != 0)
{
mama_log (MAMA_LOG_LEVEL_ERROR,
@@ -1224,7 +1099,7 @@ static void multicastFt_sendHeartbeat (void* member)
}
}

-static void
+static void MAMACALLTYPE
ioCallback (mamaIo io, mamaIoType ioType, void *closure)
{
int i = 0;
@@ -1390,7 +1265,7 @@ bridgeFt_deactivate (
return MAMA_STATUS_OK;
}

-static void
+static void MAMACALLTYPE
ftSubCreateCb (mamaSubscription subscription,
void* closure)
{
@@ -1398,7 +1273,7 @@ ftSubCreateCb (mamaSubscription subscription,
"MAMA FT: heartbeat subscription created successfully");
}

-static void
+static void MAMACALLTYPE
ftSubErrorCb (mamaSubscription subscription,
mama_status status,
void* platformError,
@@ -1448,7 +1323,7 @@ void bridgeFt_sendHeartbeat (mamaFtMember member)
mamaPublisher_send (impl->myPublisher, impl->myHeartbeatMsg);
}

-static void
+static void MAMACALLTYPE
ftSubMsgCb (mamaSubscription subscription,
mamaMsg msg,
void* closure,
diff --git a/mama/c_cpp/src/c/mama/ft.h b/mama/c_cpp/src/c/mama/ft.h
index d3b8302..2a96db4 100644
--- a/mama/c_cpp/src/c/mama/ft.h
+++ b/mama/c_cpp/src/c/mama/ft.h
@@ -49,9 +49,7 @@ typedef void* mamaFtMember;
* Callback function type for MAMA fault tolerance events.
*/
typedef void (
-#ifdef WIN32
-__stdcall
-#endif
+MAMACALLTYPE
*mamaFtMemberOnFtStateChangeCb) (mamaFtMember ftMember,
const char* groupName,
mamaFtState state,
--
1.7.7.6