Friday, March 23, 2012

Client Code to Create ATM SVC

This is client code that creates Switched Virtual Connection with AAL5 UBR sockets using ATM on Linux distribution from sourceforge.net



#define MAX_SDU     20000
#define MAX_OFFSET  8

void usage(char *name)
{
  fprintf(stderr,"usage: %s [vpi] [vci]\n",name);
  exit(1);
}

int main(int argc, char *argv[])
{
  struct sockaddr_atmsvc to_addr;
  struct atm_blli        blli;
  struct atm_qos         qos, to_qos;
  struct atm_sap         to_sap;
  fd_set                 rd, wd, ed;
  int                    to_fd;
  char                   command[20] = "";
  int                    compare = 1;
  int                    num;

  char   *quit = "q";
  char   *dest = "47.0005.80.ffe100.0000f21a26d8.0020ea000ee0.00";

  /*char   *dest = "nestor-atm";
  */
 
  printf("client: creating new socket...\n");
  if ((to_fd = socket(PF_ATMSVC, SOCK_DGRAM, ATM_AAL5)) < 0)
  {
    perror("client:socket create error\n");
    return 1;
  }
  printf("socket id:");
  printf("%d",to_fd);
  printf("\n");

  printf("client: setting qos...\n");
  memset(&qos,0,sizeof(qos));
  qos.aal = ATM_AAL5;
  qos.txtp.traffic_class = qos.rxtp.traffic_class = ATM_UBR;
  qos.txtp.max_sdu = qos.rxtp.max_sdu = MAX_SDU;

  if (setsockopt(to_fd,SOL_ATM,SO_ATMQOS,&qos,sizeof(qos)) < 0)
  {
        perror("client: setsockopt SO_ATMQOS\n");
        return 1;
  }

  printf("client: setting sap parameters...\n");
  memset(&to_sap,0, sizeof(to_sap));
  if (setsockopt(to_fd, SOL_ATM, SO_ATMSAP,&to_sap, sizeof(to_sap)) <0)
  {
    perror("client: sap paremeter set failed\n");
    close(to_fd);
    return 1;
  }
 

  memset(&to_addr,0, sizeof(to_addr));
  to_addr.sas_family = AF_ATMSVC;
  text2atm(dest, (struct sockaddr *)&to_addr, sizeof(to_addr), T2A_SVC | T2A_LOCAL);

   printf("client: calling connect...\n");
    if (connect(to_fd,(struct sockaddr *) &to_addr,sizeof(to_addr)) < 0)
    {
        perror("client: bind failed\n");
        close(to_fd);
        return 1;
    }
 
    srandom(0); /* we want it to be deterministic */


    FD_ZERO(&rd);
    FD_ZERO(&wd);
    FD_ZERO(&ed);
    FD_SET(to_fd, &wd);
   
    printf("\n");
    printf("Enter message, q to quit: ");
    gets(command);
    compare = strcmp(command, quit);

    while (compare!=0)
    {
      printf("\n");
      printf("client sending message...\n");
     
      num = write(to_fd,command,sizeof(command));
      if (num < 0)
      {
        perror("client: write failed\n");
        close(to_fd);
        return 0;
      }

      printf("\n");
      memset(command,0,sizeof(command));
      printf("Enter message, q to quit: ");
      gets(command);
      compare= strcmp(command, quit);
    }
    printf("Shutting down...!\n");
    shutdown(to_fd, 2);
    printf("Goodbye!\n");
    close(to_fd);
  return 0;
}

No comments: