Beispiel #1
0
static void aio_accept_connection(AIO_SLOT *slot)
{
  struct sockaddr_storage client_addr;
  unsigned int len;
  int fd;
  char clientName[120];
  memset(clientName,'\0',120);
  AIO_SLOT *new_slot, *saved_slot;
  //fprintf(stderr,"------ new conection --\n");
  if (!slot->close_f) {
    len = sizeof(client_addr);
    fd = accept(slot->fd, (struct sockaddr *) &client_addr, &len);
    if (fd < 0)
      return;
    
    getnameinfo((struct sockaddr *)&client_addr,len,clientName,40,NULL,0,NI_NUMERICHOST);   
    //No colocar fprintf errorr.... en la antenna se redirecciona a
   // el socket
    //fprintf(stderr,"conection from : %s\n",clientName);
    new_slot = aio_new_slot(fd, clientName, slot->bytes_to_read);
   // fprintf(stderr,"new slot created for %s\n",clientName);
    saved_slot = cur_slot;
    cur_slot = new_slot;
    (*slot->readfunc)();
    cur_slot = saved_slot;
  }
}
Beispiel #2
0
int aio_listen(int port, AIO_FUNCPTR initfunc, AIO_FUNCPTR acceptfunc,
               size_t slot_size)
{
  AIO_SLOT *slot, *saved_slot;
  int listen_fd;
  struct sockaddr_in listen_addr;
  int optval = 1;

  errno = 0;

  /* initfunc is optional but acceptfunc should be provided. */
  if (acceptfunc == NULL)
    return 0;
  
  listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (listen_fd < 0)
    return 0;

  if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR,
                 &optval, sizeof(int)) != 0) {
    close(listen_fd);
    return 0;
  }

  listen_addr.sin_family = AF_INET;
  listen_addr.sin_addr.s_addr = s_bind_address.s_addr;
  listen_addr.sin_port = htons((unsigned short)port);

  if ( bind(listen_fd, (struct sockaddr *)&listen_addr,
            sizeof(listen_addr)) != 0 ||
       fcntl(listen_fd, F_SETFL, O_NONBLOCK) != 0 ||
       listen(listen_fd, 5) != 0 ) {
    close(listen_fd);
    return 0;
  }

  slot = aio_new_slot(listen_fd, "[listening slot]", sizeof(AIO_SLOT));
  if (slot == NULL)
    return 0;

  slot->listening_f = 1;
  slot->bytes_to_read = slot_size;
  slot->readfunc = acceptfunc;

  if (initfunc != NULL) {
    saved_slot = cur_slot;
    cur_slot = slot;
    (*initfunc)();
    cur_slot = saved_slot;
  }

  return 1;
}
Beispiel #3
0
int aio_add_slot(int fd, char *name, AIO_FUNCPTR initfunc, size_t slot_size)
{
  AIO_SLOT *slot, *saved_slot;

  if (initfunc == NULL)
    return 0;

  slot = aio_new_slot(fd, name, slot_size);
  if (slot == NULL)
    return 0;

  /* Saving cur_slot value, calling initfunc with different cur_slot */
  saved_slot = cur_slot;
  cur_slot = slot;
  (*initfunc)();
  cur_slot = saved_slot;

  return 1;
}
Beispiel #4
0
static void aio_accept_connection(AIO_SLOT *slot)
{
  struct sockaddr_in client_addr;
  unsigned int len;
  int fd;
  AIO_SLOT *new_slot, *saved_slot;

  if (!slot->close_f) {
    len = sizeof(client_addr);
    fd = accept(slot->fd, (struct sockaddr *) &client_addr, &len);
    if (fd < 0)
      return;

    new_slot = aio_new_slot(fd, inet_ntoa(client_addr.sin_addr),
                            slot->bytes_to_read);

    saved_slot = cur_slot;
    cur_slot = new_slot;
    (*slot->readfunc)();
    cur_slot = saved_slot;
  }
}
Beispiel #5
0
int aio_listen(int port, AIO_FUNCPTR initfunc, AIO_FUNCPTR acceptfunc,
               size_t slot_size)
{

//IPv6
 int  error;
  struct addrinfo hints, *res, *aux;
  int one = 1;
 char service[32];


  AIO_SLOT *slot, *saved_slot;
  int listen_fd;
  struct sockaddr_in listen_addr;
  int optval = 1;
  errno = 0;
  fprintf(stderr,"Abriendo socket listener\n");

  /* initfunc is optional but acceptfunc should be provided. */
  if (acceptfunc == NULL)
    return 0;

//ipv6
 memset(service, 0, 32);
  sprintf(service, "%d", port);
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_socktype = SOCK_STREAM;
  if (error = getaddrinfo("0::0", service, &hints, &res)) {
    if (error = getaddrinfo("0.0.0.0", service, &hints, &res)) {
      perror(": ListenAtTcpPort: getaddrinfo");
      return 0;
    }
  } 



  for (aux = res; aux; aux = aux->ai_next) {
    listen_fd = socket(aux->ai_family, aux->ai_socktype, aux->ai_protocol);
    if (listen_fd < 0) {
      perror(": ListenAtTcpPort: socket");
      continue;
    }

    if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR,
                   (const char *)&one, sizeof(one)) < 0)
    {
      perror(": ListenAtTcpPort: setsockopt");
      close(listen_fd);
      return 0;
    }

    if (bind(listen_fd, aux->ai_addr, aux->ai_addrlen) < 0) {
      perror(": ListenAtTcpPort: bind");
      close(listen_fd);
      continue;
    }
    fcntl(listen_fd, F_SETFL, O_NONBLOCK);
    if (listen(listen_fd, 5) < 0) {
      perror(": ListenAtTcpPort: listen");
      close(listen_fd);
      continue;
    }
  slot = aio_new_slot(listen_fd, "[listening slot]", sizeof(AIO_SLOT));
  if (slot == NULL){
        freeaddrinfo(res);
	return 0;
   }
  
  fprintf(stderr,"ha llegado una conexion\n");
  slot->listening_f = 1;
  slot->bytes_to_read = slot_size;
  slot->readfunc = acceptfunc;

  if (initfunc != NULL) {
    saved_slot = cur_slot;
    cur_slot = slot;
    (*initfunc)();
    cur_slot = saved_slot;
  }

     freeaddrinfo(res);
    return 1;
  }

  freeaddrinfo(res);
  return 0 ;


 /* 
  listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (listen_fd < 0)
    return 0;

  if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR,
                 &optval, sizeof(int)) != 0) {
    close(listen_fd);
    return 0;
  }

  listen_addr.sin_family = AF_INET;
  listen_addr.sin_addr.s_addr = s_bind_address.s_addr;
  listen_addr.sin_port = htons((unsigned short)port);

  if ( bind(listen_fd, (struct sockaddr *)&listen_addr,
            sizeof(listen_addr)) != 0 ||
       fcntl(listen_fd, F_SETFL, O_NONBLOCK) != 0 ||
       listen(listen_fd, 5) != 0 ) {
    close(listen_fd);
    return 0;
  }


  slot = aio_new_slot(listen_fd, "[listening slot]", sizeof(AIO_SLOT));
  if (slot == NULL)
    return 0;

  slot->listening_f = 1;
  slot->bytes_to_read = slot_size;
  slot->readfunc = acceptfunc;

  if (initfunc != NULL) {
    saved_slot = cur_slot;
    cur_slot = slot;
    (*initfunc)();
    cur_slot = saved_slot;
  }

  return 1;*/
}