#  This patch solves the issue with not knowing remote
#  host parameters in inetdCompatible network services.
#
#  0. PREFACE
#
#  There are many issues able to turn fully featured solution
#  into virtually useless stuff.
#
#  1. WHAT'S WRONG?
#  Launchd is a great feature of Mac OS X. It can run daemons
#  with custom uids/pids, it can start jobs at specifed
#  intervals, it can listen on ports without launching the
#  actual daemon. Also, it is stated to host 'inetd-style'
#  services. inetd-style is a cheap way to make network
#  service. Inetd (or other superserver) listens on the
#  desired port and spawns new instance of service executable
#  with stdin&stdout redirected to the accepted socket
#  connection. There is a problem, however: there is no
#  (prove me wrong, but I haven't found!) way to tell what
#  host are you dealing with. This is a minor issue for sshd
#  which is protected on it's own. However, I feel it
#  desirable to have this knowledge. I'm not asking too much,
#  I guess. Other superservers have no problem supplying
#  this essential values. DJB's ucspi-tcp has tcpserver.
#  I used [1] as a reference. My patch enables:
#
#  $TCPREMOTEIP is the IP address of the remote host, in
#    dotted-decimal form.
#
#  $TCPREMOTEPORT is the remote TCP port number, in decimal.
#
#  $TCPLOCALIP is the IP address of the local host, in
#    dotted-decimal form.
#
#  $TCPLOCALPORT is the local TCP port number, in decimal.
#
#  [1] http://cr.yp.to/ucspi-tcp/environment.html
#  Geee, I like the host name! I'd like to own ec.ho.es, but
#  there are no 2-char domains in .es.
#
#  2. HOW CAN I MAKE MY LAUNCHD PATCHED?
#  Go to http://www.opensource.apple.com/darwinsource/
#  Find your version of OS and its architecture, click Source
#  then find launchd, and click .tar.gz nearby.
#  Download it and unpack.
#  Open Terminal.
#  Go to the directory containing launchd sources:
#    xen:~ OCTAGRAM$ cd launchd-106.20
#  Apply this patch:
#    xen:~/launchd-106.20 OCTAGRAM$ patch -p1 < ~/launchd-8g.patch 
#    patching file launchd/src/launchproxy.c
#  Good! Now let's build it:
#    xen:~/launchd-106.20 OCTAGRAM$ export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/libexec
#    xen:~/launchd-106.20 OCTAGRAM$ make RC_ARCHS="ppc i386"
#  It's not a time-consuming process. Now you need to
#  replace launchproxy, which is the actual daemon
#  responsing to inetdCompatible launchd entries.
#  Backup the original:
#    xen:~/launchd-106.20 OCTAGRAM$ sudo mv /usr/libexec/launchproxy{,.orig}
#  Copy the new version you have just built:
#    xen:~/launchd-106.20 OCTAGRAM$ sudo cp /tmp/launchd/Build/src/launchproxy /usr/libexec/launchproxy
#   Update your network entries. You might need to reenable
#   inetdCompatible services. You must also unload/load the
#   launchd entry of the network service you are willing to
#   know the remote IP. For instance, I needed to know IPs
#   of those connecting to my NNTP server, and I don't care
#   that much about other services. So I toggled off/on my
#   NNTP launchd job. If you are reading this, you are likely
#   to be aware how to turn on/off things with launchctl:
#   sudo launchctl load plistfilename
#     and
#   sudo launchctl unload plistfilename
#
#   That's all.
#
#   3. HOW DOES IT WORK?
#
#   According to man launchproxy, "launchproxy handles the
#   inetd emulation for launchd.  This program may be
#   merged into launchd in the future." Happily enough, it
#   doesn't happen yet, so I was able to patch my system
#   without rebooting. Launchproxy is the only file
#   affected. Launchproxy was already programmed to
#   query information about socket for purposes of
#   just logging. Naturally, I maid it exporting this
#   info to the environment and also local ip/port info.
#   Knowing the local port might be helpful when one
#   service occupies several ports. I hope it was helpful
#   for somebody.
#
#   --
#   If you want to get to the top, you have to start at the bottom
#
#                      Ivan Levashew a.k.a. OCTAGRAM
#                                         23-04-2009
#  e-mail: octagram@bluebottle.com
#  jabber: octagram@jabber.ru
#
diff -Nru launchd-106.20/launchd/src/launchproxy.c launchd-106.20.patched/launchd/src/launchproxy.c
--- launchd-106.20/launchd/src/launchproxy.c	2006-06-09 01:05:32.000000000 +0700
+++ launchd-106.20.patched/launchd/src/launchproxy.c	2009-04-23 02:24:45.000000000 +0700
@@ -194,6 +194,19 @@
 					syslog(LOG_NOTICE, "%s: SessionCreate == NULL!", prog);
 				}
 			}
+			setenv ("TCPREMOTEIP", fromhost, 1);
+			setenv ("TCPREMOTEPORT", fromport, 1);
+			
+			slen = sizeof(ss);
+			getsockname (r, (struct sockaddr *)&ss, &slen);
+			gni_r = getnameinfo((struct sockaddr *)&ss, slen,
+					fromhost, sizeof(fromhost),
+					fromport, sizeof(fromport),
+					NI_NUMERICHOST | NI_NUMERICSERV);
+			
+			setenv ("TCPLOCALIP", fromhost, 1);
+			setenv ("TCPLOCALPORT", fromport, 1);
+			
 			assert(fcntl(r, F_SETFL, 0) != -1);
 			assert(dup2(r, STDIN_FILENO) != -1);
 			if (dupstdout)
