void
stop_broadcast (struct Plugin *plugin)
{
  if (plugin->broadcast_ipv4)
  {
    if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
    {
      GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
      plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
    }

    if (plugin->broadcast_ipv4_mst != NULL)
      GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);

    while (plugin->ipv4_broadcast_head != NULL)
    {
      struct BroadcastAddress *p = plugin->ipv4_broadcast_head;

      GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
                                   plugin->ipv4_broadcast_tail, p);
      GNUNET_free (p->addr);
      GNUNET_free (p);
    }
  }

  if (plugin->broadcast_ipv6)
  {
    /* Create IPv6 multicast request */
    struct ipv6_mreq multicastRequest;

    multicastRequest.ipv6mr_multiaddr =
        plugin->ipv6_multicast_address.sin6_addr;
    multicastRequest.ipv6mr_interface = 0;

    /* Join the multicast address */
    if (GNUNET_NETWORK_socket_setsockopt
        (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
        (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
    {
       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
    }
    else
    {
      LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
    }

    if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
    {
      GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
      plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
    }
    if (plugin->broadcast_ipv6_mst != NULL)
      GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
  }

}
Ejemplo n.º 2
0
/**
 * Free's the resources occupied by the helper handle
 *
 * @param h the helper handle to free
 */
void
GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
{
  unsigned int c;
  struct GNUNET_HELPER_SendHandle *sh;

  if (NULL != h->write_task)
  {
    GNUNET_SCHEDULER_cancel (h->write_task);
    h->write_task = NULL;
  }
  GNUNET_assert (NULL == h->read_task);
  GNUNET_assert (NULL == h->restart_task);
  while (NULL != (sh = h->sh_head))
  {
    GNUNET_CONTAINER_DLL_remove (h->sh_head,
				 h->sh_tail,
				 sh);
    if (NULL != sh->cont)
      sh->cont (sh->cont_cls, GNUNET_SYSERR);
    GNUNET_free (sh);
  }
  if (NULL != h->mst)
    GNUNET_SERVER_mst_destroy (h->mst);
  GNUNET_free (h->binary_name);
  for (c = 0; h->binary_argv[c] != NULL; c++)
    GNUNET_free (h->binary_argv[c]);
  GNUNET_free (h->binary_argv);
  GNUNET_free (h);
}
/**
 * Stop broadcasting subsystem.
 *
 * @param plugin
 */
void
stop_broadcast (struct Plugin *plugin)
{
  if (GNUNET_YES == plugin->enable_broadcasting)
  {
    /* Disable broadcasting */
    while (plugin->broadcast_head != NULL)
    {
      struct BroadcastAddress *p = plugin->broadcast_head;

      if (p->broadcast_task != NULL)
      {
        GNUNET_SCHEDULER_cancel (p->broadcast_task);
        p->broadcast_task = NULL;
      }
      if ((GNUNET_YES == plugin->enable_ipv6) &&
          (NULL != plugin->sockv6) &&
          (p->addrlen == sizeof (struct sockaddr_in6)))
      {
        /* Create IPv6 multicast request */
        struct ipv6_mreq multicastRequest;
        const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;

        multicastRequest.ipv6mr_multiaddr =
          plugin->ipv6_multicast_address.sin6_addr;
        multicastRequest.ipv6mr_interface = s6->sin6_scope_id;

        /* Leave the multicast group */
        if (GNUNET_OK ==
            GNUNET_NETWORK_socket_setsockopt
            (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
             &multicastRequest, sizeof (multicastRequest)))
        {
          GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
        }
        else
        {
          LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
        }
      }

#if LINUX
    GNUNET_DISK_file_close(p->cryogenic_fd);
#endif
      GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
                                   plugin->broadcast_tail, p);
      GNUNET_free (p->addr);
      GNUNET_free (p);
    }
  }

  /* Destroy MSTs */
  if (NULL != plugin->broadcast_mst)
  {
    GNUNET_SERVER_mst_destroy (plugin->broadcast_mst);
    plugin->broadcast_mst = NULL;
  }
}
void
delete_session (struct Session *s)
{
  stop_session_timeout(s);

  if (s->msg_tk != NULL)
  {
    GNUNET_SERVER_mst_destroy (s->msg_tk);
    s->msg_tk = NULL;
  }
  GNUNET_free (s->addr);
  GNUNET_free_non_null (s->server_recv);
  GNUNET_free_non_null (s->server_send);
  GNUNET_free (s);
}
/**
 * Delete session s
 *
 * @param s the session to delete
 */
static void
client_delete_session (struct Session *s)
{
  struct HTTP_Client_Plugin *plugin = s->plugin;
  struct HTTP_Message *pos;
  struct HTTP_Message *next;

  client_stop_session_timeout (s);

  if (GNUNET_SCHEDULER_NO_TASK != s->put_disconnect_task)
  {
      GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
      s->put_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
  }

  GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);

  next = s->msg_head;
  while (NULL != (pos = next))
  {
    next = pos->next;
    GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, pos);
    if (pos->transmit_cont != NULL)
      pos->transmit_cont (pos->transmit_cont_cls, &s->target, GNUNET_SYSERR,
                          pos->size, pos->pos + s->overhead);
    s->overhead = 0;
    GNUNET_free (pos);
  }

  if (s->msg_tk != NULL)
  {
    GNUNET_SERVER_mst_destroy (s->msg_tk);
    s->msg_tk = NULL;
  }
  GNUNET_free (s->addr);
  GNUNET_free (s->url);
  GNUNET_free (s);
}
/**
 * We're done with a particular client, clean up.
 *
 * @param sc client to clean up
 */
static void
terminate_stream (struct StreamClient *sc)
{
  GNUNET_STATISTICS_update (GSF_stats,
			    gettext_noop ("# stream connections active"), -1,
			    GNUNET_NO);
  if (GNUNET_SCHEDULER_NO_TASK != sc->terminate_task)
    GNUNET_SCHEDULER_cancel (sc->terminate_task); 
  if (GNUNET_SCHEDULER_NO_TASK != sc->timeout_task)
    GNUNET_SCHEDULER_cancel (sc->timeout_task); 
 if (NULL != sc->rh)
    GNUNET_STREAM_io_read_cancel (sc->rh);
  if (NULL != sc->wh)
    GNUNET_STREAM_io_write_cancel (sc->wh);
  if (NULL != sc->qe)
    GNUNET_DATASTORE_cancel (sc->qe);
  GNUNET_SERVER_mst_destroy (sc->mst);
  GNUNET_STREAM_close (sc->socket);
  GNUNET_CONTAINER_DLL_remove (sc_head,
			       sc_tail,
			       sc);
  sc_count--;
  GNUNET_free (sc);
}
/**
 * Main function of a program that pretends to be a WLAN card.
 *
 * @param argc should be 2
 * @param argv either '1' or '2', depending on which of the two cards this dummy is to emulate
 * @return 1 on error, 0 if terminated normally via signal
 */
int
main (int argc, char *argv[])
{
  struct stat st;
  int erg;
  FILE *fpin = NULL;
  FILE *fpout = NULL;
  int fdpin;
  int fdpout;
  char readbuf[MAXLINE];
  int readsize;
  struct SendBuffer write_std;
  struct SendBuffer write_pout;
  int ret;
  int maxfd;
  fd_set rfds;
  fd_set wfds;
  struct timeval tv;
  int retval;
  struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst = NULL;
  struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst = NULL;
  struct GNUNET_TRANSPORT_WLAN_MacAddress macaddr;
  int first;

  if ( (2 != argc) ||
       ((0 != strcmp (argv[1], "1")) && (0 != strcmp (argv[1], "2"))) )
  {
    FPRINTF (stderr,
             "%s",
	     "This program must be started with the operating mode (1 or 2) as the only argument.\n");
    return 1;
  }

  /* make the fifos if needed */
  umask (0);
  if ( (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE1)) ||
       (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE2)) )
  {
    FPRINTF (stderr, "Failed to create directory for file `%s'\n", FIFO_FILE1);
    return 1;
  }
  if (0 == strcmp (argv[1], "1") )
  {
    if (0 != stat (FIFO_FILE1, &st))
    {
      erg = mkfifo (FIFO_FILE1, 0666);
      if ( (0 != erg) && (EEXIST != errno) )
	FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1,
		 strerror (errno));    
    }
  }
  else
  {
    if (0 != stat (FIFO_FILE2, &st))
    {
      erg = mkfifo (FIFO_FILE2, 0666);
      if ( (0 != erg) && (EEXIST != errno) )
	FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE2,
		 strerror (errno));
    }
  }

  if (0 == strcmp (argv[1], "1"))
  {
    first = 1;
    fpin = fopen (FIFO_FILE1, "r");
    if (NULL == fpin)
    {
      FPRINTF (stderr, "fopen of read FIFO_FILE1 failed: %s\n", STRERROR (errno));
      goto end;
    }
    if (NULL == (fpout = fopen (FIFO_FILE2, "w")))
    {
      erg = mkfifo (FIFO_FILE2, 0666);
      fpout = fopen (FIFO_FILE2, "w");
    }
    if (NULL == fpout)
    {
      FPRINTF (stderr, "fopen of write FIFO_FILE2 failed: %s\n", STRERROR (errno));
      goto end;
    }
  }
  else
  {
    first = 0;
    if (NULL == (fpout = fopen (FIFO_FILE1, "w")))
    {
      erg = mkfifo (FIFO_FILE1, 0666);
      fpout = fopen (FIFO_FILE1, "w");
    }
    if (NULL == fpout)
    {
      FPRINTF (stderr, "fopen of write FIFO_FILE1 failed: %s\n", STRERROR (errno));
      goto end;
    }
    fpin = fopen (FIFO_FILE2, "r");
    if (NULL == fpin)
    {
      FPRINTF (stderr, "fopen of read FIFO_FILE2 failed: %s\n", STRERROR (errno));
      goto end;
    }
  }

  fdpin = fileno (fpin);
  GNUNET_assert (fpin >= 0);
  if (fdpin >= FD_SETSIZE)
  {
    FPRINTF (stderr, "File fdpin number too large (%d > %u)\n", fdpin,
             (unsigned int) FD_SETSIZE);
    goto end;
  }

  fdpout = fileno (fpout);
  GNUNET_assert (fdpout >= 0);

  if (fdpout >= FD_SETSIZE)
  {
    FPRINTF (stderr, "File fdpout number too large (%d > %u)\n", fdpout,
             (unsigned int) FD_SETSIZE);
    goto end;
  }

  signal (SIGINT, &sigfunc);
  signal (SIGTERM, &sigfunc);

  write_std.size = 0;
  write_std.pos = 0;
  write_pout.size = 0;
  write_pout.pos = 0;
  stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout);
  file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std);

  /* Send 'random' mac address */
  macaddr.mac[0] = 0x13;
  macaddr.mac[1] = 0x22;
  macaddr.mac[2] = 0x33;
  macaddr.mac[3] = 0x44;
  macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256);
  macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256);
  write_std.size = send_mac_to_plugin (write_std.buf, &macaddr);

  while (0 == closeprog)
  {
    maxfd = -1;
    tv.tv_sec = 5;
    tv.tv_usec = 0;

    FD_ZERO (&rfds);
    FD_ZERO (&wfds);
    /* if output queue is empty, read */
    if (0 == write_pout.size)
    {
      FD_SET (STDIN_FILENO, &rfds);
      maxfd = MAX (STDIN_FILENO, maxfd);
    }
    if (0 == write_std.size)
    {
      FD_SET (fdpin, &rfds);
      maxfd = MAX (fdpin, maxfd);
    }

    /* if there is something to write, try to write */
    if (0 < write_std.size)
    {
      FD_SET (STDOUT_FILENO, &wfds);
      maxfd = MAX (maxfd, STDOUT_FILENO);
    }
    if (0 < write_pout.size)
    {
      FD_SET (fdpout, &wfds);
      maxfd = MAX (maxfd, fdpout);
    }

    retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv);
    if ((-1 == retval) && (EINTR == errno))
      continue;
    if (0 > retval)
    {
      FPRINTF (stderr, "select failed: %s\n", STRERROR (errno));
      closeprog = 1;
      break;
    }

    if (FD_ISSET (STDOUT_FILENO, &wfds))
    {
      ret =
          write (STDOUT_FILENO, write_std.buf + write_std.pos,
                 write_std.size - write_std.pos);
      if (0 > ret)
      {
        closeprog = 1;
        FPRINTF (stderr, "Write ERROR to STDOUT_FILENO: %s\n",
                 STRERROR (errno));
        break;
      }
      else
      {
        write_std.pos += ret;
        /* check if finished writing */
        if (write_std.pos == write_std.size)
        {
          write_std.pos = 0;
          write_std.size = 0;
        }
      }
    }

    if (FD_ISSET (fdpout, &wfds))
    {
      ret =
          write (fdpout, write_pout.buf + write_pout.pos,
                 write_pout.size - write_pout.pos);

      if (0 > ret)
      {
        closeprog = 1;
        FPRINTF (stderr, "Write ERROR to fdpout failed: %s\n", STRERROR (errno));
      }
      else
      {
        write_pout.pos += ret;
        /* check if finished writing */
        if (write_pout.pos == write_pout.size)
        {
          write_pout.pos = 0;
          write_pout.size = 0;
        }
      }
    }

    if (FD_ISSET (STDIN_FILENO, &rfds))
    {
      readsize = read (STDIN_FILENO, readbuf, sizeof (readbuf));

      if (0 > readsize)
      {
        closeprog = 1;
        FPRINTF (stderr, "Error reading from STDIN_FILENO: %s\n",
                 STRERROR (errno));
      }
      else if (0 < readsize)
      {
        GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, readsize,
                                   GNUNET_NO, GNUNET_NO);

      }
      else
      {
        /* eof */
        closeprog = 1;
      }
    }

    if (FD_ISSET (fdpin, &rfds))
    {
      readsize = read (fdpin, readbuf, sizeof (readbuf));
      if (0 > readsize)
      {
        closeprog = 1;
        FPRINTF (stderr, "Error reading from fdpin: %s\n", STRERROR (errno));
        break;
      }
      else if (0 < readsize)
      {
        GNUNET_SERVER_mst_receive (file_in_mst, NULL, readbuf, readsize,
                                   GNUNET_NO, GNUNET_NO);
      }
      else
      {
        /* eof */
        closeprog = 1;
      }
    }
  }

end:
  /* clean up */
  if (NULL != stdin_mst)
    GNUNET_SERVER_mst_destroy (stdin_mst);
  if (NULL != file_in_mst)
    GNUNET_SERVER_mst_destroy (file_in_mst);

  if (NULL != fpout)
    fclose (fpout);
  if (NULL != fpin)
    fclose (fpin);
  if (1 == first)
  {
    (void) unlink (FIFO_FILE1);
    (void) unlink (FIFO_FILE2);
  }
  return 0;
}