Friday, March 23, 2012

Server Code to Create ATM SVC

This code creates ATM Switched Virtual Connection with AAL5 UBR socket 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 frm_addr, lsn_addr;
  struct atm_blli        blli;
  struct atm_qos         qos, fm_qos;
  struct atm_sap         lstn_sap;
  socklen_t              fm_len, lsn_len;
  fd_set                 rd, wd, ed;
  int                    lsn_fd, fm_fd;
  int                    pid, lst;
  static unsigned char   buffer[20];
  int                    num;

  /*char   *lstn = "47.0005.80.ffe100.0000.f21a.26d8.0020ea000ee0.00";
  */
  char *lstn = "nestor-atm";
 
  printf("server: creating listen socket...\n");
  if ((lsn_fd = socket(PF_ATMSVC, SOCK_DGRAM, ATM_AAL5)) < 0)
  {
    perror("makeConn:socket error\n");
    return 1;
  }
  printf("socket id:");
  printf("%d",lsn_fd);
  printf("\n");

  printf("server: setting listen 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(lsn_fd,SOL_ATM,SO_ATMQOS,&qos,sizeof(qos)) < 0)
  {
        perror("server: setsockopt SO_ATMQOS\n");
        return 1;
  }

  printf("server: setting listen sap...\n");
  memset(&lstn_sap,0, sizeof(lstn_sap));
  if (setsockopt(lsn_fd, SOL_ATM, SO_ATMSAP,&lstn_sap, sizeof(lstn_sap)) <0)
  {
    perror("server: sap paremeter set failed\n");
    close(lsn_fd);
    return 1;
  }

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

   printf("server: binding listen socket...\n");
    if (bind(lsn_fd,(struct sockaddr *) &lsn_addr,sizeof(lsn_addr)) < 0)
    {
        perror("server: bind failed\n");
        return 1;
    }
 
   fm_len = sizeof(frm_addr);
   memset(&frm_addr,0,fm_len);

    printf("server: listening...\n");
    if (listen(lsn_fd, 3) < 0)
    {
       perror("server: listen failed\n");
       close(lsn_fd);
       return 1;
    }
    srandom(0); /* we want it to be deterministic */
 
  while(1)
  {

    FD_ZERO(&rd);
    FD_ZERO(&wd);
    FD_ZERO(&ed);
    FD_SET(lsn_fd, &rd);
    select(lsn_fd+1, &rd, &wd, &ed, NULL);
   
    printf("server: accepting on new socket...\n");
    fm_fd = accept(lsn_fd, (struct sockaddr *)&frm_addr,&fm_len);
    if (fm_fd < 0)
    {
      perror("server: did not accept\n");
      close(fm_fd);
      close(lsn_fd);
      exit(1);
    }

    while(1)
    {
      FD_SET(fm_fd, &rd);
      select(fm_fd+1, &rd, &wd, &ed, NULL);
      memset(buffer,0,sizeof(buffer));
      printf("server: waiting...\n");
      lst = read(fm_fd,buffer,sizeof(buffer));
      if (lst < 0)
      {
        perror("server: listen error\n");
        close(lsn_fd);
        close(fm_fd);
        exit(0);
      }
      if (lst > 0)
      {
        printf("server: received bytes %d\n", lst);
        printf("server: message = %s\n", buffer);
      }
      if (lst == 0)
      {
       printf("server: closing connection...\n");
       close(lsn_fd);
       close(fm_fd);
       exit(0);
      }
    }
  }
  return 0;
}

No comments: