File phreebird-systemd.patch of Package phreebird

--- phreebird_suite_1.02/phreebird.c.orig	2011-12-16 16:56:11.000000000 +0100
+++ phreebird_suite_1.02/phreebird.c	2011-12-16 17:41:52.000000000 +0100
@@ -40,6 +40,8 @@
 /* for OpenSSL, specifically so we can +1/-1 NSEC3 White Lies */
 #include <openssl/bn.h>
 
+#include <systemd/sd-daemon.h>
+
 // SECTION 1: STRUCTS
 
 struct phreebird_opts_struct
@@ -332,9 +334,11 @@
 
 int init_sockets(phreebird_opts *opts){
 
-	opts->stubsock = init_udp_socket(53);	
 	opts->backsock = init_udp_socket(0);
-	opts->listensock = init_tcp_socket(53);
+	if (sd_listen_fds(0) <= 0) {
+		opts->stubsock = init_udp_socket(53);	
+		opts->listensock = init_tcp_socket(53);
+	}
 	return 1;
 }
 
@@ -368,15 +372,32 @@
 
 	struct event rec_from_stub, rec_from_backend, rec_from_tcp;
 	struct evhttp *httpd;
+	int i = sd_listen_fds(0);
 
 	event_init();
-	event_set(&rec_from_stub, opts->stubsock, EV_READ | EV_PERSIST, stub_handler_UDP, opts);
 	event_set(&rec_from_backend, opts->backsock, EV_READ | EV_PERSIST, backend_handler_UDP, opts);
-	event_set(&rec_from_tcp, opts->listensock, EV_READ | EV_PERSIST, stub_handler_TCP, opts);
-
-	event_add(&rec_from_stub, NULL);
 	event_add(&rec_from_backend, NULL);
-	event_add(&rec_from_tcp, NULL);
+	if (i <= 0) {
+		event_set(&rec_from_stub, opts->stubsock, EV_READ | EV_PERSIST, stub_handler_UDP, opts);
+		event_set(&rec_from_tcp, opts->listensock, EV_READ | EV_PERSIST, stub_handler_TCP, opts);
+		event_add(&rec_from_stub, NULL);
+		event_add(&rec_from_tcp, NULL);
+	} else {
+		for (i--; i >= 0; i--) {
+			struct event *rec_event = (struct event *) malloc(sizeof(struct event));
+			if (!rec_event) {
+				pb_abort("malloc failed!\n");
+			}
+			if (sd_is_socket(SD_LISTEN_FDS_START + i, AF_UNSPEC, SOCK_DGRAM, -1)) {
+				event_set(rec_event, SD_LISTEN_FDS_START + i, EV_READ | EV_PERSIST, stub_handler_UDP, opts);
+			} else if (sd_is_socket(SD_LISTEN_FDS_START + i, AF_UNSPEC, SOCK_STREAM, 1)) {
+				event_set(rec_event, SD_LISTEN_FDS_START + i, EV_READ | EV_PERSIST, stub_handler_TCP, opts);
+			} else {
+				pb_abort("SD_LISTEN_FD socket is neither STREAM nor DGRAM?!\n");
+			}
+			event_add(rec_event, NULL);
+		}
+	}
 
 	httpd = evhttp_start("0.0.0.0", opts->http_port);
 	if(httpd != NULL) {
@@ -413,6 +437,7 @@
 	memcpy(&(store_cache->addr),&cAddr, sizeof(struct sockaddr_in));
 	store_cache->method = METHOD_UDP;
 	store_cache->free_buf = 0;
+	store_cache->clientfd = fd;
 
 	stub_handle_request(opts, buf, len, store_cache);
 }
@@ -1037,7 +1062,7 @@
 			if(status != LDNS_STATUS_OK) { pb_abort("couldn't gen packet\n"); } 
 			newlen = max;
 			}
-		sendto(opts->stubsock, newbuf, newlen, 0, (void *)&store_cache->addr, slen);
+		sendto(store_cache->clientfd, newbuf, newlen, 0, (void *)&store_cache->addr, slen);
  		}
 	if(store_cache->method == METHOD_HTTP){
 		http_reply(HTTP_OK, newbuf, newlen, store_cache->req);
@@ -1311,6 +1336,9 @@
 }
 
 void pb_abort(char *str){
+	if (sd_listen_fds(0) > 0) {
+		fprintf(stderr, "%s", SD_ERR);
+	}
 	fprintf(stderr, "%s\n", str);
 	exit(1);
 }
--- phreebird_suite_1.02/Makefile.orig	2011-12-16 17:39:06.000000000 +0100
+++ phreebird_suite_1.02/Makefile	2011-12-16 17:39:41.000000000 +0100
@@ -11,7 +11,7 @@
 	mkdir lib
 		
 phreebird: bin
-	$(CC) -o bin/phreebird phreebird.c -lldns -lcrypto -levent -lghthash
+	$(CC) -o bin/phreebird phreebird.c -lldns -lcrypto -levent -lghthash $(EXTRALIBS)
 
 phreeload: bin lib
 	$(CC) -D_GNU_SOURCE -lunbound -lcrypto -ldl -Wall -shared -fPIC -o lib/phreeload.so phreeload.c -I /usr/local/ssl/include
--- phreebird_suite_1.02/phreebird.c.orig	2011-12-16 17:45:58.000000000 +0100
+++ phreebird_suite_1.02/phreebird.c	2011-12-19 10:41:15.000000000 +0100
@@ -292,20 +292,12 @@
 	time_rdf = ldns_dname_new_frm_str("_dns._time.");
 	}
 
-int init_udp_socket(unsigned short port){
-	int sock;
+static void set_sockopt_udp(int sock) {
 	int yes = 1;
 	int bsize = 65536*64; // arbitrary
-	int len = sizeof(struct sockaddr);
-	struct sockaddr_in addr;
-	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-		fprintf(stderr, "Unable to make UDP socket %u\n", port);
-		exit(255);
-		}
 	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
 		fprintf(stderr, "Error setting socket option (reuseaddr).\n");
 		exit(255);
-		
 	}
 	if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &bsize, sizeof(int)) < 0) {
 		fprintf(stderr, "Error setting socket option (rcvbuf %u).\n", bsize);
@@ -315,6 +307,17 @@
 		fprintf(stderr, "Error setting socket option. (sndbuf %u).\n", bsize);
 		exit(255);
 	}
+}
+
+int init_udp_socket(unsigned short port){
+	int sock;
+	int len = sizeof(struct sockaddr);
+	struct sockaddr_in addr;
+	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+		fprintf(stderr, "Unable to make UDP socket %u\n", port);
+		exit(255);
+	}
+	set_sockopt_udp(sock);
 	
 	addr.sin_family = AF_INET;
 	addr.sin_port = htons(port);
@@ -342,16 +345,20 @@
 	return 1;
 }
 
+static void set_sockopt_tcp(int sock) {
+	int optval=1;
+	setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval));
+	fcntl(sock, F_SETFL, O_NONBLOCK);
+}
+
 int init_tcp_socket(unsigned short port){
 	int sock;	
-	int optval=1;
 	struct sockaddr_in addr;
 	int status;
 
 
 	sock = socket(PF_INET, SOCK_STREAM, 0);
-	setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval));
-	fcntl(sock, F_SETFL, O_NONBLOCK);
+	set_sockopt_tcp(sock);
 
 	addr.sin_family = AF_INET;
 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
@@ -392,8 +399,10 @@
 				pb_abort("malloc failed!\n");
 			}
 			if (sd_is_socket(SD_LISTEN_FDS_START + i, AF_UNSPEC, SOCK_DGRAM, -1)) {
+				set_sockopt_udp(SD_LISTEN_FDS_START + i);
 				event_set(rec_event, SD_LISTEN_FDS_START + i, EV_READ | EV_PERSIST, stub_handler_UDP, opts);
 			} else if (sd_is_socket(SD_LISTEN_FDS_START + i, AF_UNSPEC, SOCK_STREAM, 1)) {
+				set_sockopt_tcp(SD_LISTEN_FDS_START + i);
 				event_set(rec_event, SD_LISTEN_FDS_START + i, EV_READ | EV_PERSIST, stub_handler_TCP, opts);
 			} else {
 				pb_abort("SD_LISTEN_FD socket is neither STREAM nor DGRAM?!\n");
openSUSE Build Service is sponsored by