git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Erik Faye-Lund <kusmabite@gmail.com>
To: git@vger.kernel.org
Cc: msysgit@googlegroups.com, j6t@kdbg.org, avarab@gmail.com,
	sunshine@sunshineco.com, jrnieder@gmail.com,
	schwab@linux-m68k.org, patthoyts@gmail.com
Subject: [PATCH 10/15] daemon: get remote host address from root-process
Date: Fri, 22 Oct 2010 02:05:39 +0200	[thread overview]
Message-ID: <1287705944-5668-10-git-send-email-kusmabite@gmail.com> (raw)
In-Reply-To: <1287705944-5668-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 |   67 +++++++++++++++++++++++++++----------------------------------
 1 files changed, 30 insertions(+), 37 deletions(-)

diff --git a/daemon.c b/daemon.c
index b72c89f..19b4eb3 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);
@@ -1160,16 +1161,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))
-			peer = NULL;
-
-		return execute(peer);
-	}
+	if (inetd_mode || serve_mode)
+		return execute();
 
 	if (detach) {
 		daemonize();
-- 
1.7.3.1.199.g72340

  parent reply	other threads:[~2010-10-22  0:07 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-22  0:05 [PATCH 01/15] mingw: implement syslog Erik Faye-Lund
2010-10-22  0:05 ` [PATCH 02/15] compat: add inet_pton and inet_ntop prototypes Erik Faye-Lund
2010-10-22  0:05 ` [PATCH 03/15] inet_ntop: fix a couple of old-style decls Erik Faye-Lund
2010-10-22  0:05 ` [PATCH 04/15] mingw: use real pid Erik Faye-Lund
2010-10-22  0:05 ` [PATCH 05/15] mingw: support waitpid with pid > 0 and WNOHANG Erik Faye-Lund
2010-10-22  0:05 ` [PATCH 06/15] mingw: add kill emulation Erik Faye-Lund
2010-10-22  0:05 ` [PATCH 07/15] daemon: use run-command api for async serving Erik Faye-Lund
2010-10-22  0:10   ` Jonathan Nieder
2010-10-22  0:20     ` Erik Faye-Lund
2010-10-22  0:05 ` [PATCH 08/15] daemon: use full buffered mode for stderr Erik Faye-Lund
2010-10-22  0:05 ` [PATCH 09/15] Improve the mingw getaddrinfo stub to handle more use cases Erik Faye-Lund
2010-10-22  0:05 ` Erik Faye-Lund [this message]
2010-10-22  0:05 ` [PATCH 11/15] mingw: import poll-emulation from gnulib Erik Faye-Lund
2010-10-22  0:05 ` [PATCH 12/15] mingw: use " Erik Faye-Lund
2010-10-22  0:05 ` [PATCH 13/15] daemon: use socklen_t Erik Faye-Lund
2010-10-22  0:05 ` [PATCH 14/15] daemon: make --inetd and --detcach incompatible Erik Faye-Lund
2010-10-22  0:05 ` [PATCH 15/15] daemon: opt-out on features that require posix Erik Faye-Lund
2010-10-22  0:13 ` [PATCH 01/15] mingw: implement syslog Erik Faye-Lund

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1287705944-5668-10-git-send-email-kusmabite@gmail.com \
    --to=kusmabite@gmail.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=j6t@kdbg.org \
    --cc=jrnieder@gmail.com \
    --cc=msysgit@googlegroups.com \
    --cc=patthoyts@gmail.com \
    --cc=schwab@linux-m68k.org \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).