Files @ 483a34f1fa56
Branch filter:

Location: DistRen/src/server/listen.c

binki
added doc and struct general_info to TODO
/*
  Copyright 2009 Nathan Phillip Brink

  This file is a part of DistRen.

  DistRen is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  DistRen is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "listen.h"

#include <list.h>
#include <malloc.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>

/* local */

struct distrend_clientset
{
  LIST *clients;

  /*
    for select()
   */
  fd_set readfds;
  fd_set writefds;
  int nfds;
};


int distrend_listen(struct distrend_config *config, struct distrend_clientset **clients)
{
  int tmp;

  struct sockaddr_in6 sockaddr =
    {
      .sin6_family = AF_INET6,
      .sin6_port = 0,
      .sin6_flowinfo = 0,
      .sin6_addr = IN6ADDR_ANY_INIT,
      .sin6_scope_id = 0
    };

  *clients = malloc(sizeof(struct distrend_clientset));

  (*clients)->clients = list_init();

  sockaddr.sin6_port = htonl(4050);

  config->listens->sock = socket(AF_INET6, SOCK_STREAM, 0);
  tmp = bind(config->listens->sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  if(tmp == -1)
    {
      perror("bind");
      free(*clients);

      return 1;
    }

  tmp = listen(config->listens->sock, 1);

  FD_ZERO(&(*clients)->readfds);
  FD_ZERO(&(*clients)->writefds);

  return 0;
}

int distrend_accept_find_client(fd_set *fdset, struct distrend_client *client)
{
  if(FD_ISSET(client->sock, fdset))
    return FALSE;
  return TRUE;
}

int distrend_accept(struct distrend_config *config, struct distrend_clientset *clients, struct distrend_action **action)
{
  int tmp;
  fd_set readfds;
  fd_set writefds;

  struct distrend_client *cli;

  memcpy(&readfds, &clients->readfds, sizeof(fd_set));
  memcpy(&writefds, &clients->writefds, sizeof(fd_set));

  tmp = select(clients->nfds, &readfds, &writefds, NULL, (struct timeval *)NULL);
  if(tmp == -1)
    {
      perror("select");

      return 1;
    }

  /**
     check if our traversal function found a bit that was set
   */
  if(list_traverse(clients->clients, &readfds, &distrend_accept_find_client, LIST_FRNT | LIST_ALTR) == LIST_OK)
    {
      cli = (struct distrend_client *)list_curr(clients->clients);
      fprintf(stderr, "%s:%d: My traversal says that sock %d has data waiting, which is %s\n",
	     __FILE__, __LINE__,
	     cli->sock, FD_ISSET(cli->sock, &readfds) ? "true" : "false");
      fprintf(stderr, "stub\n");
      return 1;
    }

  return 0;
}

int distrend_unlisten(struct distrend_listen *listens, struct distrend_clientset *clients)
{
  fprintf(stderr, "%s:%d: I am a stub that needn't be implemented 'til later\n", __FILE__, __LINE__);

  return 1;
}
/**
   This is probably just NOT a placeholder for remotio
*/
void remotio_send_to_client()
{
  fprintf(stderr, "%s:%d: I am futile! And I'm happy because of it :-D\n", __FILE__, __LINE__);
}