Пример #1
0
coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
{
    OIC_LOG(DEBUG, TAG, "IN");

    if (!data)
    {
        OIC_LOG(ERROR, TAG, "invalid pointer parameter");
        return NULL;
    }

    coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
    if (!option)
    {
        OIC_LOG(ERROR, TAG, "Out of memory");
        return NULL;
    }
    memset(option, 0, sizeof(coap_option) + length + 1);

    COAP_OPTION_KEY(*option) = key;

    coap_option_def_t* def = coap_opt_def(key);
    if (NULL != def && coap_is_var_bytes(def))
    {
        if (length > def->max)
        {
            // make sure we shrink the value so it fits the coap option definition
            // by truncating the value, disregard the leading bytes.
            OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
                      def->key, length, def->max);
            data = &(data[length-def->max]);
            length = def->max;
        }
        // Shrink the encoding length to a minimum size for coap
        // options that support variable length encoding.
        COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
                                          COAP_OPTION_DATA(*option),
                                          coap_decode_var_bytes((unsigned char *)data, length));
    }
    else
    {
        COAP_OPTION_LENGTH(*option) = length;
        memcpy(COAP_OPTION_DATA(*option), data, length);
    }

    /* we can pass NULL here as delete function since option is released automatically  */
    coap_list_t *node = coap_new_listnode(option, NULL);

    if (!node)
    {
        OIC_LOG(ERROR, TAG, "node is NULL");
        coap_free(option);
        return NULL;
    }

    OIC_LOG(DEBUG, TAG, "OUT");
    return node;
}
Пример #2
0
coap_list_t *
coap_list_push_first(coap_list_t **list, void *data, void (*delete_func)(void *) ) {
  coap_list_t *node;
  node = coap_new_listnode(data, delete_func);
  if ( !node || !list )
    return NULL;

  if ( !*list ) {
    *list = node;
  } else {
    node->next = *list;
    *list = node;
  }

  return node;
}
Пример #3
0
coap_key_t
coap_add_subscription(coap_context_t *context,
		      coap_subscription_t *subscription) {
  coap_list_t *node;
  if ( !context || !subscription )
    return COAP_INVALID_HASHKEY;

  if ( !(node = coap_new_listnode(subscription, NULL)) )
    return COAP_INVALID_HASHKEY;

  if ( !coap_insert(&context->subscriptions, node, _order_subscription ) ) {
    coap_free( node );	/* do not call coap_delete(), so subscription object will survive */
    return COAP_INVALID_HASHKEY;
  }

  return coap_subscription_hash(subscription);
}
Пример #4
0
static coap_list_t *
new_option_node(unsigned short key, unsigned char *data, unsigned int length) {
	coap_option *option;
	coap_list_t *node;

	option = coap_malloc(sizeof(coap_option) + length);
	if ( !option )
		return NULL;

	COAP_OPTION_KEY(*option) = key;
	COAP_OPTION_LENGTH(*option) = length;
	memcpy(COAP_OPTION_DATA(*option), data, length);

	/* we can pass NULL here as delete function since option is released automatically	*/
	node = coap_new_listnode(option, free);
	if	(!node)
		coap_free(option);
	return node;
}
Пример #5
0
coap_key_t
coap_add_resource(coap_context_t *context, coap_resource_t *resource) {
  coap_list_t *node;

  if ( !context || !resource )
    return COAP_INVALID_HASHKEY;

  node = coap_new_listnode(resource, coap_free_resource);
  if ( !node )
    return COAP_INVALID_HASHKEY;

  if ( !context->resources ) {
    context->resources = node;
  } else {
    node->next = context->resources;
    context->resources = node;
  }

  return coap_uri_hash( resource->uri );
}
Пример #6
0
coap_list_t* CACreateNewOptionNode(const uint16_t key, const uint32_t length, const uint8_t *data)
{
    coap_option *option;
    coap_list_t *node;

    option = coap_malloc(sizeof(coap_option) + length);
    if (!option)
        goto error;

    COAP_OPTION_KEY(*option) = key;
    COAP_OPTION_LENGTH(*option) = length;
    memcpy(COAP_OPTION_DATA(*option), data, length);

    /* we can pass NULL here as delete function since option is released automatically  */
    node = coap_new_listnode(option, NULL);

    if (node)
        return node;

    error: perror("new_option_node: malloc");
    coap_free( option);
    return NULL;
}
Пример #7
0
int 
dia_dns_lookup(const char *server, void *dst) {
	struct addrinfo *res, *best;
	struct addrinfo hints;
	static char addrstr[256];
	int error;
	coap_list_t *node;
	dns_cache_t *entry;

	if	(server && *server)
		strcpy (addrstr, server);
	else
		strcpy (addrstr, "localhost");

	//	Already in cache ?
	for	(node = gCache; node; node = node->next) {
		entry	= (dns_cache_t *)node->data;
		if	(!strcmp(addrstr, entry->host)) {
			rtl_trace (5, "dns_lookup : %s found in cache\n", addrstr);
			memcpy (dst, &entry->dst, entry->len);
			return	entry->len;
		}
	}

//	rtl_trace (5, "dns_lookup : %s not found. search in DNS\n", addrstr);

	//	DNS lookup

	memset ((char *)&hints, 0, sizeof(hints));
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_family = AF_UNSPEC;
#ifdef ANDROID
	error = getaddrinfo(addrstr, NULL, &hints, &res);
#else
	error = getaddrinfo(addrstr, "", &hints, &res);
#endif

	//error = getaddrinfo(addrstr, NULL, NULL, &res);

	if (error) {
		rtl_trace (4, "getaddrinfo: %s\n", gai_strerror(error));
		return -1;
	}

	best	= searchBest (res);
	if	(!best)	return 0;

	int len = best->ai_addrlen;
	memcpy(dst, best->ai_addr, len);

	//	Create DNS cache entry
	entry	= (dns_cache_t *)malloc(sizeof(dns_cache_t));
	strcpy (entry->host, addrstr);
	memcpy(&entry->dst, best->ai_addr, len);
	entry->len	= len;

	char tmp[100];
	rtl_trace (5, "dns_lookup : %s found in DNS %s %s\n", addrstr, get_ip_fam(dst), get_ip_str(dst, tmp, sizeof(tmp)));

	//	Add to the list
	node = coap_new_listnode((void *)entry, NULL);
	coap_insert( &gCache, node, _order_null);

	freeaddrinfo(res);
	return len;
}