int coap_delete_subscription(coap_context_t *context, coap_key_t key, struct sockaddr_in6 *subscriber) { coap_list_t *prev, *node; if (!context || !subscriber || key == COAP_INVALID_HASHKEY) return 0; for (prev = NULL, node = context->subscriptions; node; prev = node, node = node->next) { if (COAP_SUBSCRIPTION(node)->resource == key) { if (subscriber->sin6_port == COAP_SUBSCRIPTION(node)->subscriber.sin6_port && memcmp(&subscriber->sin6_addr, &COAP_SUBSCRIPTION(node)->subscriber.sin6_addr, sizeof(struct in6_addr)) == 0) { if (!prev) { context->subscriptions = node->next; coap_free(COAP_SUBSCRIPTION(node)->token.s); coap_delete(node); } else { prev->next = node->next; coap_free(COAP_SUBSCRIPTION(node)->token.s); coap_delete(node); } return 1; } } } return 0; }
void coap_free_async(coap_async_state_t *s) { if (s && (s->flags & COAP_ASYNC_RELEASE_DATA) != 0) { coap_free(s->appdata); } coap_free(s); }
inline void rd_delete(rd_t *rd) { if (rd) { coap_free(rd->data.s); coap_free(rd); } }
int coap_delete(coap_list_t *node) { if (!node) return 0; if (node->delete_func) node->delete_func(node->data); coap_free( node->data); coap_free( node); return 1; }
static inline void coap_delete_payload(coap_payload_t *payload) { if (payload) { coap_dynamic_uri_t *uri; HASH_FIND(hh, test_dynamic_uris, payload->resource_key, sizeof(coap_key_t), uri); if (uri) { HASH_DELETE(hh, test_dynamic_uris, uri); coap_free(uri); } } HASH_DELETE(hh, test_resources, payload); coap_free(payload); }
static void _DTLSFree_wrapper(void *ptr) { if (NULL != ptr) { coap_free(ptr); ptr = NULL; } }
void coap_free_resource(void *res) { if ( res ) { coap_free(((coap_resource_t *)res)->uri); coap_delete_string(((coap_resource_t *)res)->name); } }
int coap_delete(coap_list_t *node) { if (node) { coap_free(node); } return 1; }
coap_payload_t * make_large(char *filename) { coap_payload_t *payload; FILE *inputfile = NULL; struct stat statbuf; if (!filename) return NULL; /* read from specified input file */ if (stat(filename, &statbuf) < 0) { warn("cannot stat file %s\n", filename); return NULL; } payload = coap_new_payload(statbuf.st_size); if (!payload) return NULL; inputfile = fopen(filename, "r"); if ( !inputfile ) { warn("cannot read file %s\n", filename); coap_free(payload); return NULL; } payload->length = fread(payload->data, 1, statbuf.st_size, inputfile); payload->media_type = 41; fclose(inputfile); return payload; }
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; }
void coap_delete_pdu(coap_pdu_t *pdu) { #if defined(WITH_POSIX) || defined(WITH_ARDUINO) coap_free( pdu ); #endif #ifdef WITH_LWIP if (pdu != NULL) /* accepting double free as the other implementation accept that too */ pbuf_free(pdu->pbuf); #endif #ifdef WITH_CONTIKI memb_free(&pdu_storage, pdu); #endif }
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); }
coap_uri_t * coap_new_uri(const unsigned char *uri, unsigned int length) { unsigned char *result; result = coap_malloc(length + 1 + sizeof(coap_uri_t)); if (!result) return NULL; memcpy(URI_DATA(result), uri, length); URI_DATA(result)[length] = '\0'; /* make it zero-terminated */ if (coap_split_uri(URI_DATA(result), length, (coap_uri_t *)result) < 0) { coap_free(result); return NULL; } return (coap_uri_t *)result; }
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; }
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; }
unsigned int _DTLSSession_deinit(dtls_session_t *p_dtls_session) { int ret; if (p_dtls_session != NULL) { do { ret = mbedtls_ssl_close_notify(&p_dtls_session->context); } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE); mbedtls_net_free(&p_dtls_session->fd); #ifdef MBEDTLS_X509_CRT_PARSE_C mbedtls_x509_crt_free(&p_dtls_session->cacert); #endif mbedtls_ssl_cookie_free(&p_dtls_session->cookie_ctx); mbedtls_ssl_config_free(&p_dtls_session->conf); mbedtls_ssl_free(&p_dtls_session->context); mbedtls_ctr_drbg_free(&p_dtls_session->ctr_drbg); mbedtls_entropy_free(&p_dtls_session->entropy); coap_free(p_dtls_session); } return DTLS_SUCCESS; }
void hnd_put_resource(coap_context_t *ctx, struct coap_resource_t *resource, const coap_endpoint_t *local_interface, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response) { #if 1 response->hdr->code = COAP_RESPONSE_CODE(501); #else /* FIXME */ coap_opt_iterator_t opt_iter; coap_opt_t *token, *etag; coap_pdu_t *response; size_t size = sizeof(coap_hdr_t); int type = (request->hdr->type == COAP_MESSAGE_CON) ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON; rd_t *rd = NULL; unsigned char code; /* result code */ unsigned char *data; str tmp; HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd); if (rd) { /* found resource object, now check Etag */ etag = coap_check_option(request, COAP_OPTION_ETAG, &opt_iter); if (!etag || (COAP_OPT_LENGTH(etag) != rd->etag_len) || memcmp(COAP_OPT_VALUE(etag), rd->etag, rd->etag_len) != 0) { if (coap_get_data(request, &tmp.length, &data)) { tmp.s = (unsigned char *)coap_malloc(tmp.length); if (!tmp.s) { debug("hnd_put_rd: cannot allocate storage for new rd\n"); code = COAP_RESPONSE_CODE(503); goto finish; } coap_free(rd->data.s); rd->data.s = tmp.s; rd->data.length = tmp.length; memcpy(rd->data.s, data, rd->data.length); } } if (etag) { rd->etag_len = min(COAP_OPT_LENGTH(etag), sizeof(rd->etag)); memcpy(rd->etag, COAP_OPT_VALUE(etag), rd->etag_len); } code = COAP_RESPONSE_CODE(204); /* FIXME: update lifetime */ } else { code = COAP_RESPONSE_CODE(503); } finish: /* FIXME: do not create a new response but use the old one instead */ response = coap_pdu_init(type, code, request->hdr->id, size); if (!response) { debug("cannot create response for message %d\n", request->hdr->id); return; } if (request->hdr->token_length) coap_add_token(response, request->hdr->token_length, request->hdr->token); if (coap_send(ctx, peer, response) == COAP_INVALID_TID) { debug("hnd_get_rd: cannot send response for message %d\n", request->hdr->id); } coap_delete_pdu(response); #endif }
static inline void coap_free_posix_endpoint(struct coap_endpoint_t *ep) { coap_free(ep); }
void hnd_post_test(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response) { coap_opt_iterator_t opt_iter; coap_opt_t *option; coap_payload_t *test_payload; size_t len; size_t l = 6 + sizeof(void *); coap_dynamic_uri_t *uri; unsigned char *data; #define BUFSIZE 20 int res; unsigned char _buf[BUFSIZE]; unsigned char *buf = _buf; size_t buflen = BUFSIZE; (void)resource; (void)peer; (void)token; coap_get_data(request, &len, &data); /* allocate storage for resource and to hold URI */ test_payload = coap_new_payload(len); uri = (coap_dynamic_uri_t *)coap_malloc(sizeof(coap_dynamic_uri_t) + l); if (!(test_payload && uri)) { coap_log(LOG_CRIT, "cannot allocate new resource under /test"); response->hdr->code = COAP_RESPONSE_CODE(500); coap_free(test_payload); coap_free(uri); } else { coap_resource_t *r; memset(uri, 0, sizeof(coap_dynamic_uri_t)); uri->length = min(l, (size_t)snprintf((char *)uri->data, l, "test/%p", (void*)test_payload)); test_payload->length = len; memcpy(test_payload->data, data, len); r = coap_resource_init(uri->data, uri->length, 0); coap_register_handler(r, COAP_REQUEST_GET, hnd_get_resource); coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_resource); /* set media_type if available */ option = coap_check_option(request, COAP_OPTION_CONTENT_TYPE, &opt_iter); if (option) { test_payload->media_type = coap_decode_var_bytes(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option)); } coap_add_resource(ctx, r); coap_add_payload(r->key, test_payload, uri); /* add Location-Path */ res = coap_split_path(uri->data, uri->length, buf, &buflen); while (res--) { coap_add_option(response, COAP_OPTION_LOCATION_PATH, COAP_OPT_LENGTH(buf), COAP_OPT_VALUE(buf)); buf += COAP_OPT_SIZE(buf); } response->hdr->code = COAP_RESPONSE_CODE(201); } }
void coap_delete_string(str *s) { coap_free(s); }