Sample code for IPv6 server program

The EZASO6PS PL/I sample program is a server program that shows you how to use the following calls provided by the call socket interface:
  • ACCEPT
  • BIND
  • CLOSE
  • EZACIC09
  • FREEADDRINFO
  • GETADDRINFO
  • GETHOSTNAME
  • GETSOCKNAME
  • INITAPI
  • LISTEN
  • NTOP
  • PTON
  • READ
  • SOCKET
  • TERMAPI
  • WRITE
 /*********************************************************************/
 /*                                                                   */
 /*   MODULE NAME:  EZASO6PS - THIS IS A VERY SIMPLE IPV6 SERVER      */
 /*                                                                   */
 /* Copyright:    Licensed Materials - Property of IBM                */
 /*                                                                   */
 /*               "Restricted Materials of IBM"                       */
 /*                                                                   */
 /*               5694-A01                                            */
 /*                                                                   */
 /*               (C) Copyright IBM Corp. 2002, 2005                  */
 /*                                                                   */
 /*               US Government Users Restricted Rights -             */
 /*               Use, duplication or disclosure restricted by        */
 /*               GSA ADP Schedule Contract with IBM Corp.            */
 /*                                                                   */
 /* Status:       CSV1R7                                              */
 /*                                                                   */
 /*********************************************************************/
 EZASO6PS: PROC OPTIONS(MAIN);

 /* INCLUDE CBLOCK - common variables                                 */
 % include CBLOCK;

 ID.TCPNAME = 'TCPCS';                 /* Set TCP to use              */
 ID.ADSNAME = 'EZASO6PS';              /* and address space name      */
 open file(driver);

 /*********************************************************************/
 /*                                                                   */
 /* Execute INITAPI                                                   */
 /*                                                                   */
 /*********************************************************************/

 /*********************************************************************/
 /*                                                                   */
 /* Uncomment this code to set max sockets to the maximum.            */
 /*                                                                   */
 /* MAXSOC_INPUT = 65535;                                             */
 /* MAXSOC_FWD = MAXSOC_INPUT;                                        */
 /*********************************************************************/

 call ezasoket(INITAPI, MAXSOC, ID, SUBTASK,
                       MAXSNO, ERRNO, RETCODE);
 if retcode < 0 then do;
    msg = 'FAIL: initapi' || errno;
    write file(driver) from (msg);
    goto getout;
 end;

 /*********************************************************************/
 /*                                                                   */
 /* Execute SOCKET                                                    */
 /*                                                                   */
 /*********************************************************************/

 call ezasoket(SOCKET, AF_INET6, TYPE_STREAM, PROTO,
                      ERRNO, RETCODE);
 if retcode < 0 then do;
    msg = blank;                       /* clear field                 */
    msg = 'FAIL: socket, stream, internet' || errno;
    write file(driver) from (msg);
    goto getout;
 end;
 else sock_stream = retcode;
 /*********************************************************************/
 /*                                                                   */
 /* Execute PTON                                                      */
 /*                                                                   */
 /*********************************************************************/
 PRESENTABLE_ADDR = IPV6_LOOPBACK;     /* Set IP address to use       */
 PRESENTABLE_ADDR_LEN = LENGTH(PRESENTABLE_ADDR) ;  /* and its length */
 call ezasoket(PTON, AF_INET6, PRESENTABLE_ADDR,
                     PRESENTABLE_ADDR_LEN, NUMERIC_ADDR,
                     ERRNO, RETCODE);
 if retcode < 0 then do;
    msg = blank;                       /* clear field                 */
    msg = 'FAIL: pton' || errno;
    write file(driver) from (msg);
    goto getout;
 end;
 name6_id.address = NUMERIC_ADDR;      /* IPV6 internet address       */
 /*********************************************************************/
 /*                                                                   */
 /* Execute GETHOSTNAME                                               */
 /*                                                                   */
 /*********************************************************************/
 call ezasoket(GETHOSTNAME, HOSTNAME_LEN, HOSTNAME,
                     ERRNO, RETCODE);
 msg = blank;                          /* clear field                 */
 if retcode < 0 then do;
    msg = 'FAIL: gethostname' || errno;
    write file(driver) from (msg);
    goto getout;
 end;
 else do;
    msg = 'gethostname = ' || HOSTNAME;
    write file(driver) from (msg);
    GAI_NODE = HOSTNAME;      /* Set host name for getaddrinfo to use */
 end;

 /*********************************************************************/
 /*                                                                   */
 /* Execute GETADDRINFO                                               */
 /*                                                                   */
 /*********************************************************************/
 GAI_SERVLEN = 0;                     /* set service length           */
 GAI_HINTS.FLAGS = ai_CANONNAMEOK;    /* Request canonical name       */
 HINTS = ADDR(GAI_HINTS);             /* Set results pointer          */
 call ezasoket(GETADDRINFO,
                      GAI_NODE, GAI_NODELEN,
                      GAI_SERVICE, GAI_SERVLEN,
                      HINTS, RES,
                      CANONNAME_LEN,
                      ERRNO, RETCODE);
 msg = blank;                          /* clear field                 */
 if retcode < 0 then do;
    msg = 'FAIL: getaddrinfo' || errno;
    write file(driver) from (msg);
 end;
 else do;                             /* process returned RES         */

 /*********************************************************************/
 /*                                                                   */
 /* Call EZACIC09 to format the returned result address information   */
 /*                                                                   */
 /*********************************************************************/

 call  ezacic09(RES, OPNAMELEN, OPCANON, OPNAME, OPNEXT,
                  RETCODE);
 msg = blank;                          /* clear field                 */
 if retcode ^= 0 then do;
    msg = 'FAIL: EZACIC09' || RETCODE;
    write file(driver) from (msg);
 end;
 else do;
    msg = 'OPCANON = ' || OPCANON;
    write file(driver) from (msg);
 end;

 /*********************************************************************/
 /*                                                                   */
 /* Execute FREEADDRINFO                                              */
 /*                                                                   */
 /*********************************************************************/
 call ezasoket(FREEADDRINFO, RES,
                      ERRNO, RETCODE);
 msg = blank;                          /* clear field                 */
 if retcode < 0 then do;
    msg = 'FAIL: freeaddrinfo' || errno;
    write file(driver) from (msg);
 end;

 end;   /* end from getaddrinfo */
 /*********************************************************************/
 /*                                                                   */
 /* Execute BIND                                                      */
 /*                                                                   */
 /*********************************************************************/

 name6_id.port = 8888;
 call ezasoket(BIND, SOCK_STREAM, NAME6_ID,
                     ERRNO, RETCODE);
 if retcode < 0 then do;
    msg = blank;                       /* clear field                 */
    msg = 'FAIL: bind' || errno;
    write file(driver) from (msg);
    goto getout;
 end;

 /*********************************************************************/
 /*                                                                   */
 /* Execute GETSOCKNAME                                               */
 /*                                                                   */
 /*********************************************************************/

 call ezasoket(GETSOCKNAME, SOCK_STREAM,
                      NAME6_ID, ERRNO, RETCODE);
 msg = blank;                          /* clear field                 */
 if retcode < 0 then do;
    msg = 'FAIL: getsockname, stream, internet' || errno;
    write file(driver) from (msg);
 end;

 /*********************************************************************/
 /*                                                                   */
 /* Execute LISTEN                                                    */
 /*                                                                   */
 /*********************************************************************/

 backlog = 5;
 call ezasoket(LISTEN, SOCK_STREAM, BACKLOG,
                      ERRNO, RETCODE);
 if retcode < 0 then do;
    msg = blank;                       /* clear field                 */
    msg = 'FAIL: listen w/ backlog = 5' || errno;
    write file(driver) from (msg);
    goto getout;
 end;

 /*********************************************************************/
 /*                                                                   */
 /* Execute ACCEPT                                                    */
 /*                                                                   */
 /*********************************************************************/

 call ezasoket(ACCEPT, SOCK_STREAM,
                      NAME6_ID, ERRNO, RETCODE);
 msg = blank;                          /* clear field                 */
 if retcode < 0 then do;
    msg = 'FAIL: accept' || errno;
    write file(driver) from (msg);
 end;
 else do;
    accpsock = retcode;
    msg = 'accept socket = ' || accpsock;
    write file(driver) from (msg);
 end;

 /*********************************************************************/
 /*                                                                   */
 /* Execute NTOP                                                      */
 /*                                                                   */
 /*********************************************************************/
 call ezasoket(NTOP, AF_INET6, NUMERIC_ADDR,
                     PRESENTABLE_ADDR, PRESENTABLE_ADDR_LEN,
                     ERRNO, RETCODE);
 msg = blank;                          /* clear field                 */
 if retcode < 0 then do;
    msg = 'FAIL: ntop' || errno;
    write file(driver) from (msg);
    goto getout;
 end;
 else do;
    msg = 'presentable address = ' || PRESENTABLE_ADDR;
    write file(driver) from (msg);
 end;                                   /*                            */

 /*********************************************************************/
 /*                                                                   */
 /* Execute READ                                                      */
 /*                                                                   */
 /*********************************************************************/

 nbyte = length(bufin);
 call ezasoket(READ, ACCPSOCK,
                      NBYTE, BUFIN, ERRNO, RETCODE);
 msg = blank;                          /* clear field                 */
 if retcode < 0 then do;
    msg = 'FAIL: read' || errno;
    write file(driver) from (msg);
 end;
 else do;
    msg = 'read = ' || bufin;
    write file(driver) from (msg);
    bufout = bufin;
    nbyte = retcode;
 end;

 /*********************************************************************/
 /*                                                                   */
 /* Execute WRITE                                                     */
 /*                                                                   */
 /*********************************************************************/

 call ezasoket(WRITE, ACCPSOCK, NBYTE, BUFOUT,
                      ERRNO, RETCODE);
 msg = blank;                          /* clear field                 */
 if retcode < 0 then do;
    msg = 'FAIL: write' || errno;
    write file(driver) from (msg);
 end;
 else do;
    msg = 'write = ' || bufout;
    write file(driver) from (msg);
 end;

 /*********************************************************************/
 /*                                                                   */
 /* Execute CLOSE accept socket                                       */
 /*                                                                   */
 /*********************************************************************/

 call ezasoket(CLOSE, ACCPSOCK,
                      ERRNO, RETCODE);
 if retcode < 0 then do;
    msg = blank;                       /* clear field                 */
    msg = 'FAIL: close, accept sock' || errno;
    write file(driver) from (msg);
 end;

 /*********************************************************************/
 /*                                                                   */
 /* Execute TERMAPI                                                   */
 /*                                                                   */
 /*********************************************************************/

 getout:
 call ezasoket(TERMAPI);

 close file(driver);
 end EZASO6PS;
Figure 1. EZASO6PS PL/1 sample server program for IPv6