/* aatxrx.c */

/* This program runs as two processes sending to an */
/* atm socket and receiving from the same socket.   */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>

#ifdef GLIBC_PATCH
#include <stdint.h>
#undef __GLIBC__
#include <linux/socket.h>
#include <linux/atm.h>
#else
#include <sys/socket.h>
#include <linux/types.h>
#endif


char buf[ 512] = "This is a test";
char bufi[512] = "This is a test";

void aa_send(
int s)                  /* socket handle */
{
   int rc;

   rc = read(0, buf, sizeof(buf));
   printf("aa_send: rc from read was %d \n");
   while (rc > 0)
   {
      rc = write(s, buf, rc);
      if (rc > 0)
         rc = read(0, buf, sizeof(buf));
   }
}

void aa_recv(
int s)                  /* socket handle */
{
   int rc;

   rc = read(s, buf, sizeof(buf));
   while (rc >= 0)
   {
      rc = write(1, buf, rc);
      rc = read(s, buf, sizeof(buf));
   }
}



int main(
int argc,
char **argv)
{
   struct sockaddr_atmpvc addr;
   struct atm_qos         qos;
   int    s,size,offset;
   int    rc;
   int    pid;

   s = socket(PF_ATMPVC,SOCK_DGRAM,ATM_AAL5);
   printf("Socket call returned %d \n", s);

   qos.aal = 5;
   qos.txtp.traffic_class = ATM_UBR;
   qos.txtp.max_sdu = 768;
   qos.txtp.max_pcr = 5 * 1024 * 1024;
   qos.txtp.min_pcr = 1 * 1024 * 1024;
   qos.txtp.max_cdv = 1 * 1024 * 1024;

   qos.rxtp.traffic_class = ATM_UBR;
   qos.rxtp.max_sdu = 768;
   qos.rxtp.max_pcr = 5 * 1024 * 1024;
   qos.rxtp.min_pcr = 1 * 1024 * 1024;
   qos.rxtp.max_cdv = 1 * 1024 * 1024;

   rc = setsockopt(s, SOL_ATM, SO_ATMQOS, &qos, sizeof(qos));
   printf("Return from setsock is %d \n", rc);

   addr.sap_family   = AF_ATMPVC;
   addr.sap_addr.itf = 0;
   addr.sap_addr.vpi = 0;
   addr.sap_addr.vci = 33;

   while (rc = connect(s,(struct sockaddr *) &addr,sizeof(addr)))
   {
      sleep(1);
   }
   printf("Return from connect is %d \n", rc);

   pid = fork();

   if (pid == 0)
   {
     aa_send(s);
     exit(1);
   }
   else
   {
     aa_recv(s);
   }
}
