Beispiel #1
0
static void Node_destroy(struct Node* node)
{
    if(node==NULL) return;
    Node_destroy(node->_left);
    Node_destroy(node->_right);
    free(node);
}
Beispiel #2
0
int Proxy_listener_send(Proxy *conn, Node *header, Node *body)
{
  assert_mem(header); assert_mem(conn);

  int rc = writenodes(conn->client.write_fd, header, body);
  check_err(rc > 0, LISTENER_IO, "Failed to write data to client listener.");

  Node_destroy(body); Node_destroy(header);

  ensure(return rc > 0);
}
Beispiel #3
0
void LinkedList_delete(LinkedList* list, Node* node)
{
	Node* next_node;
	next_node = node->next;
	
	if (node == list->first)
	{
		list->first = node->next;
	}
	else
	{
		node->prev->next = node->next;
	}
	
	if (node == list->last)
	{
		list->last = node->prev;
	}
	else
	{
		node->next->prev = node->prev;
	}	
	
	Node_destroy(node);
}
Beispiel #4
0
static List *neighbours_list(World *world, Point *point, Point *destination, Hashmap *nodes)
{
  List *neighbours = List_create();
  int nx, ny;

  for(nx = point->x - 1; nx <= point->x + 1; nx++) {
    if(nx < 0 || nx >= world->width) continue;
    for(ny = point->y - 1; ny <= point->y + 1; ny++) {
      if(ny < 0 || ny >= world->height ||
	 (ny == point->y && nx == point->x) ||
	 (!World_can_enter(world, nx, ny, point->z) &&
	  !(nx == destination->x && ny == destination->y))) continue;

      Point *p  = Point_create(nx, ny, point->z);
      Node *node = Node_create(p, 0, 0, NULL);

      Node *old_node = Hashmap_get(nodes, node);
      if(old_node) {
	Node_destroy(node);
	node = old_node;
      } else {
	Hashmap_set(nodes, node, node);
      }

      List_push(neighbours, node);
    }
  }

  return neighbours;
}
Beispiel #5
0
int Proxy_connect(Proxy *conn) 
{
  Node *rhdr = NULL, *rmsg = NULL;
  Node *imsg = NULL;
  bstring header = NULL;
  int rc = 0;

  assert_mem(conn);

  CryptState_init();

  // create the peer crypto we need
  conn->state = CryptState_create(conn->client.name, conn->client.key);
  check_err(conn->state, CRYPTO, "Failed to initialize cryptography subsystem.");
  conn->state->data = conn;

  log(INFO, "Connecting to %s:%s as %s expecting server named %s", 
      bdata(conn->host.host), bdata(conn->host.port),
      bdata(conn->client.name), bdata(conn->host.name));

  // connect to the hub
  conn->host.fd = tcp_client((char *)bdata(conn->host.host), (char *)bdata(conn->host.port));
  check_err(conn->host.fd >= 0, NET, "Failed to connect to Hub.");

  rmsg = readnodes(conn->host.fd, &rhdr);
  check_err(!rmsg && rhdr, PEER_ESTABLISH, "Incorrectly formatted initial message from Hub.");

  // generate first response
  imsg = CryptState_initiator_start(conn->state, rhdr, &header, Proxy_confirm_key);
  Node_destroy(rhdr); rhdr = NULL;
  check_err(imsg, PEER_ESTABLISH, "Failed to construct response to Hub's first message.");

  rc = write_header_node(conn->host.fd, header, imsg);
  Node_destroy(imsg); imsg = NULL;
  bdestroy(header); header = NULL;
  check_err(rc > 0, PEER_ESTABLISH, "Failed sending response to Hub's first message.");

  rmsg = readnodes(conn->host.fd, &rhdr);
  check_err(rmsg && rhdr, PEER_ESTABLISH, "Invalid response from hub.");
  header = Node_bstr(rhdr, 1); Node_destroy(rhdr); rhdr = NULL;

  rc = CryptState_initiator_process_response(conn->state, header, rmsg);
  check_err(rc, PEER_ESTABLISH, "Failed to process receiver 2nd message.");
  Node_destroy(rmsg); bdestroy(header); header = NULL; rmsg = NULL;

  imsg = CryptState_initiator_send_final(conn->state, &header);
  check_err(imsg, PEER_ESTABLISH, "Failed to generate final message.");

  rc = write_header_node(conn->host.fd, header, imsg);
  check_err(rc > 0, PEER_ESTABLISH, "Failed to write final message to hub.");

  check_err(Proxy_final_verify(conn), PEER_VERIFY, "Final verify of Hub info failed.");

  // remember, rc>0 is how we make sure that everything went ok during processing
  ensure(if(imsg) Node_destroy(imsg);
      if(rmsg) Node_destroy(rmsg);
      if(rhdr) Node_destroy(rhdr);
      if(header) bdestroy(header);
      return rc > 0);
}
Beispiel #6
0
void Proxy_error(Proxy *conn, UtuMendicantErrorCode code, bstring message)
{
  Node *err = Node_cons("[nbw", (uint64_t)code, message, "err");
  Node_dump(err, ' ', 1);
  // don't use Proxy_listener_send since that could be causing a write error
  // just try writing, and ignore the write if it failed
  writenodes(conn->client.write_fd, err, NULL);
  Node_destroy(err);
}
Beispiel #7
0
void KDTree_destroy(struct KDTree* tree)
{
    /* clean up KD tree */
    if (!tree) return;
    Node_destroy(tree->_root);
    Region_destroy(tree->_query_region);
    if (tree->_center_coord) free(tree->_center_coord);
    if (tree->_coords) free(tree->_coords);
    if (tree->_data_point_list) free(tree->_data_point_list);
    free(tree);
}
Beispiel #8
0
void* Stack_pop(Stack stack) {
	int size = List_getSize(stack);

	if(size == 0) return NULL;

	Node node = List_detachNodeAtIndex(stack, size - 1);
	void* data = Node_getData(node);

	Node_destroy(node, NULL);

	return data;
}
Beispiel #9
0
int Proxy_hub_send(Proxy *conn, Node *header, Node *body)
{
  bstring hdata = NULL;
  Node *msg = NULL;
  int rc = 0;
  assert_mem(header);  assert_mem(body); assert_mem(conn);

  hdata = Node_bstr(header, 1);

  msg = CryptState_encrypt_node(conn->state, &conn->state->them.skey, hdata, body);
  check_then(msg, "failed to encrypt payload", rc = -1);

  rc = write_header_node(conn->host.fd, hdata, msg);
  check_err(rc > 0, HUB_IO, "Failed to write encrypted message.");

  ensure(if(msg) Node_destroy(msg);  
      if(hdata) bdestroy(hdata);
      Node_destroy(header);
      Node_destroy(body);
      return rc > 0);
}
Beispiel #10
0
int main (int argc, char *argv[]) {
    Node *ast;
    enum { DOT, SYM } mode = DOT ;
    int i;

    for(i = 1; i < argc; i++) {
        if      (opt("-d")) mode = DOT;
        else if (opt("-s")) mode = SYM;
        else if (opt("-h")) {
            puts ("-d: produce dot");
            puts ("-s: produce symbol table");
            exit (EXIT_SUCCESS);
        } else {
            yyfile = argv[i];
        }
    }

    yyin = fopen (yyfile, "r");
    if (NULL == yyin) {
        fprintf (stderr, "unable to open %s\n", argv[1]);
        exit (EXIT_FAILURE);
    }

    next_token ();
    ast = rule_program ();

    switch (mode) {
    case DOT:
        AST_toDot (ast);
        break;
    case SYM:
        yysym = SymTable_new (
                    Node_countType (ast, N_VAR_DEC) +
                    Node_countType (ast, N_FUN_DEC) +
                    Node_countType (ast, N_ARR_DEC)
                );

        SymTable_build (yysym, ast);

        SymTable_print   (yysym);
        SymTable_destroy (yysym);
        break;
    }

    Node_destroy (ast);
    fclose (yyin);

    return 0;
}
Beispiel #11
0
int Proxy_configure(Proxy *conn)
{
  int rc = 0;
  Node *host = NULL;
  Node *client = NULL;

  // read the configuration stackish from the client
  log(INFO, "waiting for initial config");
  host = readnodes(conn->client.read_fd, &client);
  check_err(host && client, CONFIG, "Received an invalid host and client configuration.");

  rc = Node_decons(host, 1, "[bsssw", 
      &conn->host.key, &conn->host.name, 
      &conn->host.port, &conn->host.host, "host");
  check_err(rc, CONFIG, "Invalid configuration format for host: [sssbw");

  rc = Node_decons(client, 1, "[bsw", &conn->client.key, &conn->client.name, "client");
  check_err(rc, CONFIG, "Invalid configuration format for client: [sbw");

  // set any keys that are 0 length to NULL so they are generated
  if(blength(conn->host.key) == 0) {
    log(WARN, "No expected host key given, will accept anything.");
    bdestroy(conn->host.key);
    conn->host.key = NULL;
  }

  if(blength(conn->client.key) == 0) {
    log(WARN, "No client key given, will generate one.");
    bdestroy(conn->client.key);
    conn->client.key = NULL;
  }

  ensure(if(host) Node_destroy(host); 
      if(client) Node_destroy(client); 
      return rc);
}
Beispiel #12
0
void LinkedList_destroy(LinkedList* list) {
	Node* node = list->first;
	Node* next_node;

	list->first = NULL;
	list->last = NULL;

	while(NULL != node) 
	{
		next_node = node->next;
		Node_destroy(node);
		node = next_node;
	}
	
	free(list);	
}
Beispiel #13
0
Node *Proxy_hub_recv(Proxy *conn, Node **header)
{
  Node *msg = NULL, *packet = NULL;
  bstring hdata = NULL;
  assert_mem(header); assert_mem(conn); 

  packet = readnodes(conn->host.fd, header);
  check_err(packet && *header, HUB_IO, "Failed to read hub message.");

  hdata = Node_bstr(*header, 1);
  check_err(hdata, STACKISH, "Failed to convert stackish header from hub into a string for decrypt.");

  msg = CryptState_decrypt_node(conn->state, &conn->state->me.skey, hdata, packet);
  check_err(msg, CRYPTO, "Failed to decrypt payload from hub.");
  
  ensure(Node_destroy(packet); bdestroy(hdata); return msg);
}
int main(){
  Node* nodo = Node_create(8);
  Node* node_to_delete2 = Node_append_to_tail(nodo, 9);
  Node_append_to_tail(nodo, 10);
  Node_append_to_tail(nodo, 11);
  Node_append_to_tail(nodo, 12);
  Node* node_to_delete1 = Node_append_to_tail(nodo, 13);
  Node_append_to_tail(nodo, 14);
  Node* node_to_delete3 = Node_append_to_tail(nodo, 15);

  Node_print_each_element(nodo);
  Node_delete_in_the_middle(node_to_delete1);
  Node_print_each_element(nodo);
  Node_delete_in_the_middle(node_to_delete2);
  Node_print_each_element(nodo);
  Node_delete_in_the_middle(node_to_delete3);
  Node_print_each_element(nodo);
  Node_delete_in_the_middle(nodo);
  Node_print_each_element(nodo);
  
  Node_destroy(nodo);
  return 0;
}
Beispiel #15
0
int KDTree_set_data(struct KDTree* tree, float *coords, long int nr_points)
{
    long int i;
    int ok;

    Region_dim=tree->dim;

    /* clean up stuff from previous use */
    Node_destroy(tree->_root);
    if (tree->_coords) free(tree->_coords);
    if (tree->_radius_list)
    {
        free(tree->_radius_list);
        tree->_radius_list = NULL;
    }
    tree->_count=0;
    /* keep pointer to coords to delete it */
    tree->_coords=coords;
        
    for (i=0; i<nr_points; i++)
    {
        ok = KDTree_add_point(tree, i, coords+i*tree->dim);
        if (!ok) 
        {
            free(tree->_data_point_list);
            tree->_data_point_list = NULL;
            tree->_data_point_list_size = 0;
            return 0;
        }
    }

    /* build KD tree */
    tree->_root=KDTree_build_tree(tree, 0, 0, 0);
    if(!tree->_root) return 0;
    return 1;
}
Beispiel #16
0
int main (int argc, char* argv[]) {
  int i = 1;
  FILE *alvcin = NULL;
  Node *ast = NULL;
  SymTable *st = NULL;
  Program *p = NULL;
  int exit_status = EXIT_SUCCESS;

  struct {
    enum { COMPILE, EXEC, BYTECODE, TRACE } mode;
    char *out, *in;
  } options = { COMPILE, NULL, NULL };

  progname = basename (argv[0]);

  if (!(argc == 2 && arg("-h")) && argc < 3)
    fatal ("not enough arguments (run with -h)");

  if (arg("-h")) {
    puts (
      "alvc -- the Awesome L Virtual machine and Compiler\n"
      "  usage: alvc mode input [output]\n"
      "    mode:   may be either -c (compile), -x (execute),\n"
      "            -b (execute bytecode), -t (trace) ou -h (help)\n"
      "    input:  input file in L or in bytecode\n"
      "    output: output file, defaults to /dev/stdout (only for -c/-t)"
    );
    exit (EXIT_SUCCESS);
  }
  else if (arg("-c")) options.mode = COMPILE;
  else if (arg("-x")) options.mode = EXEC;
  else if (arg("-b")) options.mode = BYTECODE;
  else if (arg("-t")) options.mode = TRACE;
  else                fatal ("unknwon mode");

  yyfile     = argv[++i];
  options.in = yyfile;

  if (argv[++i]) options.out = argv[i];

  alvcin = fopen (options.in,  "r");
  if (NULL == alvcin) {
    fprintf (stderr, "unable to open %s for reading (%s)\n",
             options.in, strerror (errno));
    exit (errno);
  }
  yyin = alvcin;

  if (EXEC != options.mode && BYTECODE != options.mode && NULL != options.out) {
    stdout = freopen (options.out, "w+", stdout);
    if (NULL == stdout) {
      fprintf (stderr, "unable to open %s for writing (%s)\n",
               options.out, strerror (errno));
      exit (errno);
    }
  }

  if (options.mode != BYTECODE) {
    next_token ();
    ast = rule_program ();

    st = SymTable_new (
      Node_countType (ast, N_VAR_DEC) +
      Node_countType (ast, N_FUN_DEC) +
      Node_countType (ast, N_ARR_DEC)
    );
    SymTable_build (st, ast);
    if (SymTable_hasFailed (false)) exit(EXIT_FAILURE);

    p = AST_compile (ast, st);
  }

  switch (options.mode) {
  case COMPILE:
    Program_dump (p, st);
    break;
  case EXEC:
    runvm (VM_new (p, SymTable_globalSize (st)), p, &exit_status);
    break;
  case BYTECODE: {
    VM *vm = VM_fromBytecode (alvcin);
    runvm (vm, (p = VM_extractProgram (vm)), &exit_status);
    } break;
  case TRACE:
    SymTable_print (st);
    Program_print (p);
    break;
  }

  if (NULL != p)   Program_destroy (p);
  if (NULL != st)  SymTable_destroy (st);
  if (NULL != ast) Node_destroy (ast);

  fclose (alvcin);

  exit (exit_status);
}
Beispiel #17
0
static struct Node *KDTree_build_tree(struct KDTree* tree, long int offset_begin, long int offset_end, int depth)
{
    int localdim;

    if (depth==0)
    {
        /* start with [begin, end+1] */
        offset_begin=0;
        offset_end=tree->_data_point_list_size;
        localdim=0;
    }
    else
    {
        localdim=depth%tree->dim;
    }

    if ((offset_end-offset_begin)<=tree->_bucket_size) 
    {
        /* leaf node */
        return Node_create(-1, localdim, offset_begin, offset_end);
    }
    else
    {
        long int offset_split;
        long int left_offset_begin, left_offset_end;
        long int right_offset_begin, right_offset_end;
        long int d;
        float cut_value;
        struct DataPoint data_point;
        struct Node *left_node, *right_node, *new_node;

        DataPoint_sort(tree->_data_point_list+offset_begin, offset_end-offset_begin, localdim);

        /* calculate index of split point */
        d=offset_end-offset_begin;
        offset_split=d/2+d%2;

        data_point=tree->_data_point_list[offset_begin+offset_split-1];
        cut_value = data_point._coord[localdim];

        /* create new node and bind to left & right nodes */
        new_node=Node_create(cut_value, localdim, offset_begin, offset_end);
        if (new_node==NULL) return NULL;

        /* left */
        left_offset_begin=offset_begin;
        left_offset_end=offset_begin+offset_split;
        left_node=KDTree_build_tree(tree, left_offset_begin, left_offset_end, depth+1);

        /* right */
        right_offset_begin=left_offset_end;
        right_offset_end=offset_end;
        right_node=KDTree_build_tree(tree, right_offset_begin, right_offset_end, depth+1);

        new_node->_left = left_node;
        new_node->_right = right_node;

        if (left_node==NULL || right_node==NULL)
        {
            Node_destroy(new_node);
            return NULL;
        }

        return new_node;
    }
}
Beispiel #18
0
static int traverse_clean(HashmapNode *hnode)
{
  Node *node = hnode->key;
  Node_destroy(node);
  return 0;
}
Beispiel #19
0
	LIST_FOREACH(list, first, next, cur) {
		Node_destroy(cur->prev);
	}