git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v7 11/16] daemon: get remote host address from root-process

From
Erik Faye-Lund <kusmabite@gmail.com>
Date
Nov 4, 2010, 01:35 UTC
Message-ID
<1288834524-2400-12-git-send-email-kusmabite@gmail.com>
In-Reply-To
<1288834524-2400-1-git-send-email-kusmabite@gmail.com>

Get remote host in the process that accept() and pass it through the REMOTE_ADDR environment variable to the handler-process. Introduce the REMOTE_PORT environmen variable for the port.

Use these variables for reporting instead of doing getpeername(0, ...), which doesn't work on Windows.

Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
---
 daemon.c |   79 ++++++++++++++++++++++++++++++-------------------------------
 1 files changed, 39 insertions(+), 40 deletions(-)
diff --git a/daemon.c b/daemon.c
index 941c095..8162f10 100644
--- a/daemon.c
+++ b/daemon.c
@@ -516,37 +516,14 @@ static void parse_host_arg(char *extra_args, int buflen)
 }
 
 
-static int execute(struct sockaddr *addr)
+static int execute(void)
 {
 	static char line[1000];
 	int pktlen, len, i;
+	char *addr = getenv("REMOTE_ADDR"), *port = getenv("REMOTE_PORT");
 
-	if (addr) {
-		char addrbuf[256] = "";
-		int port = -1;
-
-		if (addr->sa_family == AF_INET) {
-			struct sockaddr_in *sin_addr = (void *) addr;
-			inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf));
-			port = ntohs(sin_addr->sin_port);
-#ifndef NO_IPV6
-		} else if (addr && addr->sa_family == AF_INET6) {
-			struct sockaddr_in6 *sin6_addr = (void *) addr;
-
-			char *buf = addrbuf;
-			*buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
-			inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1);
-			strcat(buf, "]");
-
-			port = ntohs(sin6_addr->sin6_port);
-#endif
-		}
-		loginfo("Connection from %s:%d", addrbuf, port);
-		setenv("REMOTE_ADDR", addrbuf, 1);
-	}
-	else {
-		unsetenv("REMOTE_ADDR");
-	}
+	if (addr)
+		loginfo("Connection from %s:%s", addr, port);
 
 	alarm(init_timeout ? init_timeout : timeout);
 	pktlen = packet_read_line(0, line, sizeof(line));
@@ -680,6 +657,8 @@ static char **cld_argv;
 static void handle(int incoming, struct sockaddr *addr, int addrlen)
 {
 	struct child_process cld = { 0 };
+	char addrbuf[300] = "REMOTE_ADDR=", portbuf[300];
+	char *env[] = { addrbuf, portbuf, NULL };
 
 	if (max_connections && live_children >= max_connections) {
 		kill_some_child();
@@ -692,6 +671,28 @@ static void handle(int incoming, struct sockaddr *addr, int addrlen)
 		}
 	}
 
+	if (addr->sa_family == AF_INET) {
+		struct sockaddr_in *sin_addr = (void *) addr;
+		inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf + 12,
+		    sizeof(addrbuf) - 12);
+		snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d",
+		    ntohs(sin_addr->sin_port));
+#ifndef NO_IPV6
+	} else if (addr && addr->sa_family == AF_INET6) {
+		struct sockaddr_in6 *sin6_addr = (void *) addr;
+
+		char *buf = addrbuf + 12;
+		*buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
+		inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf,
+		    sizeof(addrbuf) - 13);
+		strcat(buf, "]");
+
+		snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d",
+		    ntohs(sin6_addr->sin6_port));
+#endif
+	}
+
+	cld.env = (const char **)env;
 	cld.argv = (const char **)cld_argv;
 	cld.in = incoming;
 	cld.out = dup(incoming);
@@ -902,9 +903,15 @@ static int service_loop(struct socketlist *socklist)
 
 		for (i = 0; i < socklist->nr; i++) {
 			if (pfd[i].revents & POLLIN) {
-				struct sockaddr_storage ss;
+				union {
+					struct sockaddr sa;
+					struct sockaddr_in sai;
+#ifndef NO_IPV6
+					struct sockaddr_in6 sai6;
+#endif
+				} ss;
 				unsigned int sslen = sizeof(ss);
-				int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen);
+				int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
 				if (incoming < 0) {
 					switch (errno) {
 					case EAGAIN:
@@ -915,7 +922,7 @@ static int service_loop(struct socketlist *socklist)
 						die_errno("accept returned");
 					}
 				}
-				handle(incoming, (struct sockaddr *)&ss, sslen);
+				handle(incoming, &ss.sa, sslen);
 			}
 		}
 	}
@@ -1160,16 +1167,8 @@ int main(int argc, char **argv)
 			die_errno("failed to redirect stderr to /dev/null");
 	}
 
-	if (inetd_mode || serve_mode) {
-		struct sockaddr_storage ss;
-		struct sockaddr *peer = (struct sockaddr *)&ss;
-		socklen_t slen = sizeof(ss);
-
-		if (getpeername(0, peer, &slen))
-			return execute(NULL);
-		else
-			return execute(peer);
-	}
+	if (inetd_mode || serve_mode)
+		return execute();
 
 	if (detach) {
 		daemonize();
-- 
1.7.3.2.162.g09d37
Previous: Erik Faye-LundNext: Erik Faye-Lund
Message 15 of 21 in “daemon-win32”
  1. 00/16 daemon-win32Erik Faye-Lund, Nov 4, 2010
  2. 01/16 mingw: add network-wrappers for daemonErik Faye-Lund, Nov 4, 2010
  3. 02/16 mingw: implement syslogErik Faye-Lund, Nov 4, 2010
  4. 03/16 compat: add inet_pton and inet_ntop prototypesErik Faye-Lund, Nov 4, 2010
  5. 04/16 inet_ntop: fix a couple of old-style declsErik Faye-Lund, Nov 4, 2010
  6. 05/16 mingw: use real pidErik Faye-Lund, Nov 4, 2010
  7. 06/16 mingw: support waitpid with pid > 0 and WNOHANGErik Faye-Lund, Nov 4, 2010
  8. 07/16 mingw: add kill emulationErik Faye-Lund, Nov 4, 2010
  9. 08/16 daemon: use run-command api for async servingErik Faye-Lund, Nov 4, 2010
  10. daemon: support <directory> arguments againJonathan Nieder, Jan 4, 2011
  11. Erik Faye-LundJan 4, 2011
  12. Junio C HamanoJan 4, 2011
  13. 09/16 daemon: use full buffered mode for stderrErik Faye-Lund, Nov 4, 2010
  14. 10/16 Improve the mingw getaddrinfo stub to handle more use casesErik Faye-Lund, Nov 4, 2010
  15. 11/16 daemon: get remote host address from root-processErik Faye-Lund, Nov 4, 2010
  16. 12/16 mingw: import poll-emulation from gnulibErik Faye-Lund, Nov 4, 2010
  17. Erik Faye-LundJul 6, 2011
  18. 13/16 mingw: use poll-emulation from gnulibErik Faye-Lund, Nov 4, 2010
  19. 14/16 daemon: use socklen_tErik Faye-Lund, Nov 4, 2010
  20. 15/16 daemon: make --inetd and --detach incompatibleErik Faye-Lund, Nov 4, 2010
  21. 16/16 daemon: opt-out on features that require posixErik Faye-Lund, Nov 4, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.