On 30 May 2002, Gerald Timothy Quimpo wrote:

> i'm writing a little server that will run from xinetd.
> since xinetd servers read incoming data from stdin and
> write outgoing data to stdout, i don't see how to get
> the IP number of the remote host. one way to do that


use getpeername() and inet_ntop(). Below is the classic mydaytime server
managed my xinetd or inetd that logs client ipaddr and port number.

1) compile it:

gcc mydaytime.c -o mydaytime

2) modify xinetd.conf and restart xinetd.
3) modify syslog.conf and restart syslogd to allow user and info logs.
4) test it:

%telnet mydaytime_host_name mydaytime_port_number
Trying XXX.XXX.XXX.XXX...
Connected to mydaytime_host_name.
Escape character is '^]'.
Server time: Fri May 31 12:06:25 2002
Connection closed by foreign host.

rowel

/*************mydaytime.c***********************/

#include <syslog.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <time.h>
#define MAXBUF 128

int main (int argc, char **argv)
{
 socklen_t len;
 struct sockaddr *cliaddr;
 struct sockaddr_in *cliaddr_in;
 char ipaddr[MAXBUF];
 char mytime[MAXBUF];
 unsigned short int port;
 time_t ticks;

 cliaddr = (struct sockaddr*) malloc(MAXBUF);
 cliaddr_in = (struct sockaddr_in*) cliaddr;
 len = MAXBUF;
 getpeername(0,cliaddr,&len);
 inet_ntop(AF_INET,&(cliaddr_in->sin_addr), ipaddr, sizeof(ipaddr));
 port = ntohs(cliaddr_in->sin_port);
 syslog(LOG_USER|LOG_INFO,"Connection from %s port %d",ipaddr,port);

 ticks = time(NULL);
 strcpy(mytime,"Server time: ");
 strcat(mytime,ctime(&ticks));
 write(1,mytime,strlen(mytime));
 close(0);
 exit(0);
}

/******************mydaytime.c**********************/

_
Philippine Linux Users Group. Web site and archives at http://plug.linux.org.ph
To leave: send "unsubscribe" in the body to [EMAIL PROTECTED]

To subscribe to the Linux Newbies' List: send "subscribe" in the body to 
[EMAIL PROTECTED]

Reply via email to