Ejemplo n.º 1
0
int ll_getLast(LinkedList *ll, void **element) {
   int status = 0;
   LLNode *p = SENTINEL(ll)->prev;

   if (p != SENTINEL(ll)) {
      status = 1;
      *element = p->element;
   }
   return status;
}
Ejemplo n.º 2
0
int
co_call(co_obj_t *connection, co_obj_t **response, const char *method, const size_t mlen, co_obj_t *request)
{
  CHECK(method != NULL && mlen > 0 && mlen < UINT8_MAX, "Invalid method name.");
  CHECK(connection != NULL && IS_SOCK(connection), "Invalid connection.");
  co_obj_t *params = NULL, *rlist = NULL, *rtree = NULL;
  int retval = 0;
  size_t reqlen = 0, resplen = 0;
  char req[REQUEST_MAX];
  char resp[RESPONSE_MAX];
  if(request != NULL)
  {
    CHECK(IS_LIST(request), "Not a valid request.");
    params = request;
  }
  else
  {
    params = co_list16_create();
  }
  co_obj_t *m = co_str8_create(method, mlen, 0);
  reqlen = co_request_alloc(req, sizeof(req), m, params);
  CHECK(((co_socket_t*)connection)->send((co_obj_t*)((co_socket_t*)connection)->fd, req, reqlen) != -1, "Send error!");
  if((resplen = ((co_socket_t*)connection)->receive(connection, (co_obj_t*)((co_socket_t*)connection)->fd, resp, sizeof(resp))) > 0) 
  {
    CHECK(co_list_import(&rlist, resp, resplen) > 0, "Failed to parse response.");
    rtree = co_list_element(rlist, 3);
    if(!IS_NIL(rtree))
    {
      retval = 1;
    }
    else
    {
      rtree = co_list_element(rlist, 2);
      retval = 0;
    }
    if(rtree != NULL && IS_TREE(rtree)) 
    {
      *response = rtree;
      hattach(*response, _pool);
    }
    else SENTINEL("Invalid response.");
  }
  else SENTINEL("Failed to receive data.");

  co_obj_free(m);
  if(params != request) co_obj_free(params);
  return retval;
  
error:
  co_obj_free(m);
  if(params != request) co_obj_free(params);
  return retval;
}
Ejemplo n.º 3
0
LinkedList *ll_create(void) {
   LinkedList *ll;

   ll = (LinkedList *)malloc(sizeof(LinkedList));
   if (ll != NULL) {
      ll->size = 0l;
      ll->freel = NULL;
      ll->sentinel.next = SENTINEL(ll);
      ll->sentinel.prev = SENTINEL(ll);
   }
   return ll;
}
Ejemplo n.º 4
0
int ll_addLast(LinkedList *ll, void *element) {
   int status = 0;
   LLNode *p = getEntry(ll);

   if (p != NULL) {
      p->element = element;
      status = 1;
      link(SENTINEL(ll)->prev, p, SENTINEL(ll));
      ll->size++;
   }
   return status;
}
Ejemplo n.º 5
0
int ll_removeLast(LinkedList *ll, void **element) {
   int status = 0;
   LLNode *p = SENTINEL(ll)->prev;

   if (p != SENTINEL(ll)) {
      status = 1;
      *element = p->element;
      unlink(p);
      putEntry(ll, p);
      ll->size--;
   }
   return status;
}
Ejemplo n.º 6
0
size_t
co_tree_raw(char *output, const size_t olen, const co_obj_t *tree)
{
  char *out = output;
  size_t written = 0;
  switch(CO_TYPE(tree))
  {
    case _tree16:
      memmove(out, &(tree->_type), sizeof(tree->_type));
      out += sizeof(tree->_type);
      written += sizeof(tree->_type);
      memmove(out, &(((co_tree16_t *)tree)->_len), sizeof(((co_tree16_t *)tree)->_len));
      out += sizeof(((co_tree16_t *)tree)->_len);
      written += sizeof(((co_tree16_t *)tree)->_len);
      break;
    case _tree32:
      memmove(out, &(tree->_type), sizeof(tree->_type));
      out += sizeof(tree->_type);
      written += sizeof(tree->_type);
      memmove(out, &(((co_tree32_t *)tree)->_len), sizeof(((co_tree32_t *)tree)->_len));
      out += sizeof(((co_tree32_t *)tree)->_len);
      written += sizeof(((co_tree32_t *)tree)->_len);
      break;
    default:
      SENTINEL("Not a tree object.");
      break;
  }
  
  _co_tree_raw_r(&out, &olen, &written, co_tree_root(tree));
  DEBUG("Tree bytes written: %d", (int)written);
  return written;
error:
  return -1;
}
Ejemplo n.º 7
0
/**
 * Print out the packet content.
 * @param p_packet Pointer to the packet.
 */
uint8_t print_packet( icp_packet_t *p_packet ) {
	int i = 0;

	if (p_packet == NULL) {
		SENTINEL("Packet is NULL");
	}
	
	printf("Packet content:\n");
	printf("   destination[%02X]:\t%s\n", p_packet->destination, mac_addrs[p_packet->destination]);
	printf("        source[%02X]:\t%s\n", p_packet->source, mac_addrs[p_packet->source]);
	printf("            received:\t%hu\n", p_packet->receive_timestamp);
	printf("  data_len[%02X %02X]:\t%d\n", p_packet->data_length >> 8, (uint8_t)(p_packet->data_length & 0x00FF), p_packet->data_length);
	printf("               data:\t");
	for (i=0; i < p_packet->data_length+1; i++) {
		putchar( i ? ' ': '[' );
		printf("%02X", p_packet->data[i]);
	}
	printf("] \"");
	for (i=0; i < p_packet->data_length+1; i++) {
		putchar( p_packet->data[i] );
	}
	puts("\"");

	return 0;

error:
	return -1;
	
}
Ejemplo n.º 8
0
static int
_co_node_set_uint(_treenode_t *n, const unsigned long value)
{
  CHECK(n != NULL, "Invalid node supplied.");
  switch(CO_TYPE(n->value))
  {
    case _uint8:
      (((co_uint8_t *)(n->value))->data) = value;
      break;
    case _uint16:
      (((co_uint16_t *)(n->value))->data) = value;
      break;
    case _uint32:
      (((co_uint32_t *)(n->value))->data) = value;
      break;
    case _uint64:
      (((co_uint64_t *)(n->value))->data) = value;
      break;
    default:
      SENTINEL("Specified object is not a unsigned integer.");
      break;
  }
  return 1;
error:
  return 0;
}
Ejemplo n.º 9
0
int
co_response_get_int(co_obj_t *response, signed long *output, const char *key, const size_t klen) 
{
  co_obj_t *obj = co_response_get(response, key, klen);
  CHECK(obj != NULL, "Response value %s does not exist.", key);
  switch(CO_TYPE(obj))
  {
    case _int8:
      *output = (unsigned long)(((co_int8_t *)obj)->data);
      break;
    case _int16:
      *output = (unsigned long)(((co_int16_t *)obj)->data);
      break;
    case _int32:
      *output = (unsigned long)(((co_int32_t *)obj)->data);
      break;
    case _int64:
      *output = (unsigned long)(((co_int64_t *)obj)->data);
      break;
    default:
      SENTINEL("Not an unsigned integer.");
      break;
  }
  return 1;

error:
  return 0;
}
Ejemplo n.º 10
0
/*
 * traverses linked list, calling userFunction on each element and freeing
 * node associated with element
 */
static void purge(LinkedList *ll, void (*userFunction)(void *element)) {
   LLNode *cur = ll->sentinel.next;

   while (cur != SENTINEL(ll)) {
      LLNode *next;
      if (userFunction != NULL)
         (*userFunction)(cur->element);
      next = cur->next;
      putEntry(ll, cur);
      cur = next;
   }
}
Ejemplo n.º 11
0
int ll_get(LinkedList *ll, long index, void **element) {
   int status = 0;

   if (index < ll->size) {
      long n;
      LLNode *p;

      status = 1;
      for (n = 0, p = SENTINEL(ll)->next; n < index; n++, p = p->next)
         ;
      *element = p->element;
   }
   return status;
}
Ejemplo n.º 12
0
void dispatch(int argc, char *argv[])
{
    /* ignore blank commands */
    if (argc == 0 || argv[0] == NULL)
        return;

    for (Namefun *nfp = handlers; nfp < SENTINEL(handlers); nfp++) {
        if (istreq(argv[0], nfp->name)) {
            (nfp->fn)(argc, argv);
            return;
        }
    }

    ERROR("Unknown command '%s'", argv[0]);
}
Ejemplo n.º 13
0
int co_socket_getopt(void * self, int level, int option, void *optval, socklen_t optvallen) {
  co_socket_t *this = self;

  //Check to see if this is a standard socket option, or needs custom handling.
  if(level <= MAX_IPPROTO) {
    CHECK(!getsockopt(this->fd, level, option, optval, &optvallen), "Problem setting socket options.");
  } else {
    SENTINEL("No custom socket options defined!");
  }

  return 1;

error:
  return 0;
}
Ejemplo n.º 14
0
int ll_remove(LinkedList *ll, long index, void **element) {
   int status = 0;

   if (index < ll->size) {
      long n;
      LLNode *p;

      status = 1;
      for (n = 0, p = SENTINEL(ll)->next; n < index; n++, p = p->next)
         ;
      *element = p->element;
      unlink(p);
      putEntry(ll, p);
      ll->size--;
   }
   return status;
}
Ejemplo n.º 15
0
int ll_insert(LinkedList *ll, long index, void *element) {
   int status = 0;
   LLNode *p;

   if (index <= ll->size && (p = getEntry(ll)) != NULL) {
      long n;
      LLNode *b;

      p->element = element;
      status = 1;
      for (n = 0, b = SENTINEL(ll); n < index; n++, b = b->next)
         ;
      link(b, p, b->next);
      ll->size++;
   }
   return status;
}
Ejemplo n.º 16
0
_treenode_t *
co_tree_root(const co_obj_t *tree)
{
  CHECK_MEM(tree);
  _treenode_t *n = NULL;
  if(CO_TYPE(tree) == _tree16)
  {
    n = ((co_tree16_t *)tree)->root;
  } 
  else if(CO_TYPE(tree) == _tree32) 
  {
    n = ((co_tree32_t *)tree)->root;
  }
  else SENTINEL("Specified object is not a tree.");

  return n;
error:
  return NULL;
}
Ejemplo n.º 17
0
int 
co_tree_process(co_obj_t *tree, const co_iter_t iter, void *context)
{
  switch(CO_TYPE(tree))
  {
    case _tree16:
      _co_tree_process_r(tree, ((co_tree16_t *)tree)->root, iter, context);
      break;
    case _tree32:
      _co_tree_process_r(tree, ((co_tree32_t *)tree)->root, iter, context);
      break;
    default:
      SENTINEL("Object is not a tree.");
      break;
  }
  return 1;
error:
  return 0;
}
Ejemplo n.º 18
0
static int
_co_node_set_str(_treenode_t *n, const char *value, const size_t vlen)
{
  CHECK(n != NULL, "Invalid node supplied.");
  CHECK(n->value != NULL, "Invalid node supplied.");
  switch(CO_TYPE(n->value))
  {
    case _str8:
      CHECK(vlen <= UINT8_MAX, "Value too large for type str8.");
      if(vlen != (((co_str8_t *)(n->value))->_len))
      {
        n->value = h_realloc(n->value, (size_t)(vlen + sizeof(co_str8_t) - 1));
      }
      CHECK_MEM(memmove((((co_str8_t *)(n->value))->data), value, vlen));
      (((co_str8_t *)(n->value))->_len) = (uint8_t)vlen;
      break;
    case _str16:
      CHECK(vlen <= UINT16_MAX, "Value too large for type str16.");
      if(vlen != (((co_str16_t *)(n->value))->_len))
      {
        n->value = h_realloc(n->value, (size_t)(vlen + sizeof(co_str16_t) - 1));
      }
      CHECK_MEM(memmove((((co_str16_t *)(n->value))->data), value, vlen));
      (((co_str16_t *)(n->value))->_len) = (uint16_t)vlen;
      break;
    case _str32:
      CHECK(vlen <= UINT32_MAX, "Value too large for type str32.");
      if(vlen != (((co_str32_t *)(n->value))->_len))
      {
        n->value = h_realloc(n->value, (size_t)(vlen + sizeof(co_str32_t) - 1));
      }
      CHECK_MEM(memmove((((co_str32_t *)(n->value))->data), value, vlen));
      (((co_str32_t *)(n->value))->_len) = (uint32_t)vlen;
      break;
    default:
      SENTINEL("Specified object is not a string.");
      break;
  }

  return 1;
error:
  return 0;
}
Ejemplo n.º 19
0
int
co_response_get_bool(co_obj_t *response, bool *output, const char *key, const size_t klen) {
  co_obj_t *obj = co_response_get(response, key, klen);
  CHECK(obj != NULL, "Response value %s does not exist.", key);
  switch(CO_TYPE(obj))
  {
    case _false:
      *output = false;
      break;
    case _true:
      *output = true;
      break;
    default:
      SENTINEL("Not a boolean.");
      break;
  }
  return 1;
  
  error:
  return 0;
}
Ejemplo n.º 20
0
co_socket_t *co_socket_create(size_t size, co_socket_t proto) {

  if(!proto.init) proto.init = NULL;
  if(!proto.destroy) proto.destroy = co_socket_destroy;
  if(!proto.hangup) proto.hangup = co_socket_hangup;
  if(!proto.bind) proto.bind = NULL;
  if(!proto.connect) proto.connect = NULL;
  if(!proto.send) proto.send = co_socket_send;
  if(!proto.receive) proto.receive = co_socket_receive;
  if(!proto.setopt) proto.setopt = co_socket_setopt;
  if(!proto.getopt) proto.getopt = co_socket_getopt;
  co_socket_t *new_sock = malloc(size);
  *new_sock = proto;
  
  if((proto.init != NULL) && (!new_sock->init(new_sock))) {
    SENTINEL("Failed to initialize new socket.");
  } else {
    return new_sock;
  }

error:
  new_sock->destroy(new_sock);
  return NULL;
}
Ejemplo n.º 21
0
size_t
co_tree_import(co_obj_t **tree, const char *input, const size_t ilen)
{
  size_t length = 0, olen = 0, read = 0, klen = 0;
  char *kstr = NULL;
  int i = 0;
  co_obj_t *obj = NULL;
  const char *cursor = input;
  switch((uint8_t)input[0])
  {
    case _tree16:
      length = *((uint16_t *)(input + 1));
      *tree = co_tree16_create();
      cursor += sizeof(uint16_t) + 1;
      read = sizeof(uint16_t) + 1;
      break;
    case _tree32:
      length = (uint32_t)(*(uint32_t*)(input + 1));
      *tree = co_tree32_create();
      cursor += sizeof(uint32_t) + 1;
      read = sizeof(uint32_t) + 1;
      break;
    default:
      SENTINEL("Not a tree.");
      break;
  }
  while(i < length && read <= ilen)
  {
    DEBUG("Importing tuple:");
    if((uint8_t)cursor[0] == _str8)
    {
      DEBUG("Reading key...");
      cursor += 1;
      read += 1;
      klen = (uint8_t)cursor[0];
      kstr = (char *)&cursor[1];
      cursor += klen + 1;
      read += klen + 1;

      DEBUG("Reading value...");
      switch((uint8_t)cursor[0])
      {
        case _list16:
        case _list32:
          olen = co_list_import(&obj, cursor, ilen - read);
          break;
        case _tree16:
        case _tree32:
          olen = co_tree_import(&obj, cursor, ilen - read);
          break;
        default:
          olen = co_obj_import(&obj, cursor, ilen - read, 0);
          break;
      }
      CHECK(olen > 0, "Failed to import object.");
      cursor +=olen;
      read += olen;

      DEBUG("Inserting value into tree with key.");
      CHECK(co_tree_insert(*tree, kstr, klen, obj), "Failed to insert object.");
      i++;
    }
  }
  return read;
error:
  if(obj != NULL) co_obj_free(obj);
  return -1;
}
Ejemplo n.º 22
0
void ll_clear(LinkedList *ll, void (*userFunction)(void *element)) {
   purge(ll, userFunction);
   ll->size = 0L;
   ll->sentinel.next = SENTINEL(ll);
   ll->sentinel.prev = SENTINEL(ll);
}