git » nmdb » commit d139ab2

Improve and fix support for compiling out TIPC, UDP and TCP from libnmdb.

author Alberto Bertogli
2007-08-09 07:06:48 UTC
committer Alberto Bertogli
2007-08-09 07:06:48 UTC
parent 31e5c9e756f62e975584f22a7900e0e050b88b5f

Improve and fix support for compiling out TIPC, UDP and TCP from libnmdb.

The support for compiling out TIPC, UDP and TCP in libnmdb was broken,
because while we did not include the code, we still had a dependency on
the header, because nmdb.h included the tipc and inet headers regardless
of the configuration options.

This patch fixes this by generating nmdb.h on build time, and setting
some internal constants accordingly. Not the nicest way on earth, but it's
not too dirty either, and it works.

It also fixes some issues with having UDP in and TCP out or viceversa,
because the server comparison function relied on TCP (it worked on UDP
just by chance because accesing the data through the TCP union member
is ABI compatible). Now there is only one union member for either, named
'in', accordingly to the headers and the sockaddr variant used.


Signed-off-by: Alberto Bertogli <albertito@gmail.com>

libnmdb/Makefile +10 -3
libnmdb/libnmdb.c +4 -4
libnmdb/{nmdb.h => nmdb.skel.h} +23 -6
libnmdb/tcp.c +6 -5
libnmdb/tipc.c +1 -0
libnmdb/udp.c +8 -7

diff --git a/libnmdb/Makefile b/libnmdb/Makefile
index 45370a3..46b12de 100644
--- a/libnmdb/Makefile
+++ b/libnmdb/Makefile
@@ -29,13 +29,20 @@ default: all
 
 all: libs
 
+nmdb.h:
+	@echo "generating nmdb.h"
+	@cat nmdb.skel.h | \
+		sed 's/++CONFIG_ENABLE_TIPC++/$(ENABLE_TIPC)/g' | \
+		sed 's/++CONFIG_ENABLE_UDP++/$(ENABLE_UDP)/g' | \
+		sed 's/++CONFIG_ENABLE_TCP++/$(ENABLE_TCP)/g' \
+		> nmdb.h
 
 libs: libnmdb.so libnmdb.a
 
-libnmdb.so: $(OBJS)
+libnmdb.so: nmdb.h $(OBJS)
 	$(CC) $(ALL_CFLAGS) -shared -fPIC $(OBJS) -o libnmdb.so
 
-libnmdb.a: $(OBJS)
+libnmdb.a: nmdb.h $(OBJS)
 	$(AR) cr libnmdb.a $(OBJS)
 
 
@@ -60,7 +67,7 @@ install: install-lib install-man
 	$(CC) $(ALL_CFLAGS) -c $< -o $@
 
 clean:
-	rm -f $(OBJS) libnmdb.so libnmdb.a
+	rm -f nmdb.h $(OBJS) libnmdb.so libnmdb.a
 	rm -f *.bb *.bbg *.da *.gcov *.gcda *.gcno gmon.out
 
 .PHONY: default all libs install-lib install-man install clean
diff --git a/libnmdb/libnmdb.c b/libnmdb/libnmdb.c
index a4a9fa8..61c42cd 100644
--- a/libnmdb/libnmdb.c
+++ b/libnmdb/libnmdb.c
@@ -42,15 +42,15 @@ int compare_servers(const void *s1, const void *s2)
 #if ENABLE_TCP || ENABLE_UDP
 	if (srv1->type == TCP_CONN || srv1->type == UDP_CONN) {
 		in_addr_t a1, a2;
-		a1 = srv1->info.tcp.srvsa.sin_addr.s_addr;
-		a2 = srv2->info.tcp.srvsa.sin_addr.s_addr;
+		a1 = srv1->info.in.srvsa.sin_addr.s_addr;
+		a2 = srv2->info.in.srvsa.sin_addr.s_addr;
 
 		if (a1 < a2) {
 			return -1;
 		} else if (a1 == a2) {
 			in_port_t p1, p2;
-			p1 = srv1->info.tcp.srvsa.sin_port;
-			p2 = srv2->info.tcp.srvsa.sin_port;
+			p1 = srv1->info.in.srvsa.sin_port;
+			p2 = srv2->info.in.srvsa.sin_port;
 
 			if (p1 < p2)
 				return -1;
diff --git a/libnmdb/nmdb.h b/libnmdb/nmdb.skel.h
similarity index 81%
rename from libnmdb/nmdb.h
rename to libnmdb/nmdb.skel.h
index 9184e8c..8bb5ac3 100644
--- a/libnmdb/nmdb.h
+++ b/libnmdb/nmdb.skel.h
@@ -1,30 +1,47 @@
 
+/* Header for the libnmdb library. */
+
 #ifndef _NMDB_H
 #define _NMDB_H
 
+/* Defined to 0 or 1 at libnmdb build time according the build configuration,
+ * not to be used externally. */
+#define _ENABLE_TIPC ++CONFIG_ENABLE_TIPC++
+#define _ENABLE_TCP ++CONFIG_ENABLE_TCP++
+#define _ENABLE_UDP ++CONFIG_ENABLE_UDP++
+
+
 #include <sys/types.h>		/* socket defines */
 #include <sys/socket.h>		/* socklen_t */
+
+#if _ENABLE_TIPC
 #include <linux/tipc.h>		/* struct sockaddr_tipc */
-#include <netinet/in.h>		/* struct sockaddr_in */
+#endif
 
+#if (_ENABLE_TCP || _ENABLE_UDP)
+#include <netinet/in.h>		/* struct sockaddr_in */
+#endif
 
 struct nmdb_srv {
 	int fd;
 	int type;
 	union {
+
+#if _ENABLE_TIPC
 		struct {
 			unsigned int port;
 			struct sockaddr_tipc srvsa;
 			socklen_t srvlen;
 		} tipc;
+#endif
+
+#if (_ENABLE_TCP || _ENABLE_UDP)
 		struct {
 			struct sockaddr_in srvsa;
 			socklen_t srvlen;
-		} tcp;
-		struct {
-			struct sockaddr_in srvsa;
-			socklen_t srvlen;
-		} udp;
+		} in;
+#endif
+
 	} info;
 };
 
diff --git a/libnmdb/tcp.c b/libnmdb/tcp.c
index 6e701ed..0bee671 100644
--- a/libnmdb/tcp.c
+++ b/libnmdb/tcp.c
@@ -43,12 +43,12 @@ static int add_tcp_server_addr(nmdb_t *db, in_addr_t *inetaddr, int port)
 	newsrv = &(db->servers[db->nservers - 1]);
 
 	newsrv->fd = fd;
-	newsrv->info.tcp.srvsa.sin_family = AF_INET;
-	newsrv->info.tcp.srvsa.sin_port = htons(port);
-	newsrv->info.tcp.srvsa.sin_addr.s_addr = *inetaddr;
+	newsrv->info.in.srvsa.sin_family = AF_INET;
+	newsrv->info.in.srvsa.sin_port = htons(port);
+	newsrv->info.in.srvsa.sin_addr.s_addr = *inetaddr;
 
-	rv = connect(fd, (struct sockaddr *) &(newsrv->info.tcp.srvsa),
-			sizeof(newsrv->info.tcp.srvsa));
+	rv = connect(fd, (struct sockaddr *) &(newsrv->info.in.srvsa),
+			sizeof(newsrv->info.in.srvsa));
 	if (rv < 0)
 		goto error_exit;
 
@@ -185,6 +185,7 @@ uint32_t tcp_get_rep(struct nmdb_srv *srv,
 #else
 /* Stubs to use when TCP is not enabled. */
 
+#include <stdint.h>
 #include "nmdb.h"
 
 int nmdb_add_tcp_server(nmdb_t *db, const char *addr, int port)
diff --git a/libnmdb/tipc.c b/libnmdb/tipc.c
index cea13ff..6482352 100644
--- a/libnmdb/tipc.c
+++ b/libnmdb/tipc.c
@@ -105,6 +105,7 @@ uint32_t tipc_get_rep(struct nmdb_srv *srv,
 #else
 /* Stubs to use when TIPC is not enabled. */
 
+#include <stdint.h>
 #include "nmdb.h"
 
 int nmdb_add_tipc_server(nmdb_t *db, int port)
diff --git a/libnmdb/udp.c b/libnmdb/udp.c
index 5ece6e8..55902d2 100644
--- a/libnmdb/udp.c
+++ b/libnmdb/udp.c
@@ -43,10 +43,10 @@ static int add_udp_server_addr(nmdb_t *db, in_addr_t *inetaddr, int port)
 	newsrv = &(db->servers[db->nservers - 1]);
 
 	newsrv->fd = fd;
-	newsrv->info.udp.srvsa.sin_family = AF_INET;
-	newsrv->info.udp.srvsa.sin_port = htons(port);
-	newsrv->info.udp.srvsa.sin_addr.s_addr = *inetaddr;
-	newsrv->info.udp.srvlen = sizeof(struct sockaddr_in);
+	newsrv->info.in.srvsa.sin_family = AF_INET;
+	newsrv->info.in.srvsa.sin_port = htons(port);
+	newsrv->info.in.srvsa.sin_addr.s_addr = *inetaddr;
+	newsrv->info.in.srvlen = sizeof(struct sockaddr_in);
 
 	newsrv->type = UDP_CONN;
 
@@ -82,8 +82,8 @@ int udp_srv_send(struct nmdb_srv *srv,
 {
 	ssize_t rv;
 	rv = sendto(srv->fd, buf, bsize, 0,
-			(struct sockaddr *) &(srv->info.udp.srvsa),
-			srv->info.udp.srvlen);
+			(struct sockaddr *) &(srv->info.in.srvsa),
+			srv->info.in.srvlen);
 	if (rv <= 0)
 		return 0;
 	return 1;
@@ -121,9 +121,10 @@ uint32_t udp_get_rep(struct nmdb_srv *srv,
 #else
 /* Stubs to use when UDP is not enabled. */
 
+#include <stdint.h>
 #include "nmdb.h"
 
-int nmdb_add_udp_server(nmdb_t *db, int port)
+int nmdb_add_udp_server(nmdb_t *db, const char *addr, int port)
 {
 	return 0;
 }