Hi,
I wanted to retrieve the IP address of client signing on to the AS400. I
took this code from IBM technical document (# 457529679) and compiled it and
ran it.
It did not work. The data area was created, but it was blanks. I am a
complete novice in the world of API's. Anyone has any ideas, what the
problem could be? Running it in debug mode showed that the retrieveIPAddress
function was unsuccessful (returned a -1). I compiled it on V5R3 (did not
get it from the ftp site).
************************************************************************************************
Document Title:Retrieving the Client IP Address in a User Exit Program
 Document Description:
 A common request for exit programs that monitor the IBM(R) i5/OS™ host
servers is how the IP address of the client that is making the connection
can be determined.  This used to be a very difficult programming challenge
and, for the most part, exit program designers elected to not require the
IP address in an exit program.  However, there is a behavior of the i5/OS™
sockets code that can be taken advantage of to obtain the IP address of the
client without too much difficulty.  In current releases of i5/OS™, socket
descriptors (a programming handle to a TCP socket) are issued beginning at
number 0 for each job.  Because the host server opens the first socket in
the job, it is known that socket descriptor 0 can be used to obtain
information about the TCP socket over which the host server is
communicating.  By issuing a call to the getpeername API using socket
descriptor 0, the IP address of the client can be determined.
Following is the source code for a C program that could be called by an
exit program.  It logs the IP address of the client in a data area in the
QTEMP library.  The exit program could then read the data area to determine
if there are any restrictions for that particular IP address.  A typical
usage would be to use a CL program and the RTVDTAARA command to read the
contents of the data area.
/* Licensed Material - Property of IBM  */
/******************************************************************************/
  /* RTVIPA is a sample program that demonstrates how the remote IP  address
can */
  /* be retrieved when running in the context of a host server job.  This
*/
  /* sample will create a data area named QTEMP/IPADDRESS that can be
easily    */
  /* read by a CL exit program.  The CL program should call this sample
program */
  /* to retrieve the IP address, and then read the data area for any  checks
of  */
  /* the address.  */
/******************************************************************************/
  #include <string.h>           /* Used for memset() API  */
  #include <stdlib.h>           /* Used for the system() API  */
  #include <stdio.h>            /* Used for sprintf() API  */
  #include <xxdtaa.h>           /* Used for the QXXCHDA() API  */
  #include <sys/socket.h>       /* Used for the getpeername() API  */
  #include <sys/types.h>        /* Co-req for the getpeername() API  */
/******************************************************************************/
  /* Function prototypes.  */
/******************************************************************************/
  int createDataArea();
  void retrieveIPAddress();
  void writeAddressToDataArea(char* ipaddr);
/******************************************************************************/
  /* Main method.  */
/******************************************************************************/
  void main()
  {
      int rc;
      rc = createDataArea();
      if(rc==0) retrieveIPAddress();
  }
/******************************************************************************/
  /* Create the data area to store the IP address that will be retrieved
  from   */
  /* the sockets API.  */
  /* @return int Negative if create fails.  Otherwise 0.  */
/******************************************************************************/
  int createDataArea()
  {
      int rc;
      system("DLTDTAARA DTAARA(QTEMP/IPADDRESS)");
      /* Longest possible address '111.111.111.111' (at least for IPv4)  */
      rc = system("CRTDTAARA DTAARA(QTEMP/IPADDRESS) TYPE(*CHAR) LEN
(15)");
      if(rc!=0) return -1;       /* System returns 1 for unsuccessful
command   */
      else return 0;
  }
/******************************************************************************/
  /* Attempt to retrieve the remote IP address associated with socket  */
  /* descriptor 0, and update the data area if successful.  */
/******************************************************************************/
  void retrieveIPAddress()
  {
      int rc;                            /* Return code variable  */
      struct sockaddr address;           /* Pointer to address structure  */
      int avail = sizeof(address);       /* Needed for API call  */
      rc = getpeername(0,                           /* Use socket
descriptor 0  */
                  &address,                         /* Address structure.
*/
                  &avail);                          /* Amount of data
avail.    */
      if(rc==0) writeAddressToDataArea(((char*)&address)+4);
  }
/******************************************************************************/
  /* Write the retrieved IP address to the data area in QTEMP for later
  */
  /* retrieval by the calling program.
  */
  /* @param char* A char pointer that points to the 4 byte IPv4 address.
  */
/******************************************************************************/
  void writeAddressToDataArea(char* ipaddr)
  {
      char buffer[16];                       /* Create a buffer to build
string.*/
      _DTAA_NAME_T dtaname = {"IPADDRESS ", "QTEMP     "};
      memset(buffer, '\0', sizeof(buffer));  /* Initialize data buffer to
nulls */
      sprintf(buffer, "%d.%d.%d.%d",
          *((unsigned char*)(ipaddr)),
          *((unsigned char*)(ipaddr+1)),
          *((unsigned char*)(ipaddr+2)),
          *((unsigned char*)(ipaddr+3)));
      QXXCHGDA(dtaname,                      /* Name of data area  */
          1,                                 /* Starting position in data
area  */
          strlen(buffer),                    /* Number of chars to write.
*/
          buffer);                           /* Data buffer   */
  }
A compiled version may also be found for V5R3 at the following location:
ftp://testcase.software.ibm.com/fromibm/os400/ApiSamples/RTVIPA.SAVF
*********************************************************************************************
Thanks in advance for any help.
As an Amazon Associate we earn from qualifying purchases.