Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Extracting an IP address from an inetd task instance: (3 Items)
   
Extracting an IP address from an inetd task instance  
I want inetd to run a script when a socket service is connected. The script will log the start of the connection, run 
the service, and then log the end of the connection, like this:

logger start service
service args
logger end service

I would like to include the IP address of the other end of the socket in the log entry. What might be a simple way to 
extract this information from stdin when this script is run? Right now I am doing something like

who=netstat -a | grep service

This works, but I was wondering if there was something I could do, perhaps in C, that would do something similar and 
cleaner.

Thanks,

Kevin
Re: Extracting an IP address from an inetd task instance  
Hey Kevin,

from C, it's perfectly easy:
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main( int argc, char *argv[] ) {
	struct sockaddr_in  sin;
	socklen_t  len = sizeof( sin );
	puts( getpeername( 0, (void*) &sin, &len ) ? "Unknown" : inet_ntoa( sin.sin_addr ) );
	return EXIT_SUCCESS;
}

Command-line based, I'd wouldn't know right now...

Cheers,
- Thomas
Re: Extracting an IP address from an inetd task instance  
This works perfectly. Thanks!