Lines Matching refs:session

95 /** creates a Qx.FRAC_BITS from session's 'ack_random_factor' */
97 Q(FRAC_BITS, session->ack_random_factor)
99 /** creates a Qx.FRAC_BITS from session's 'ack_timeout' */
100 #define ACK_TIMEOUT Q(FRAC_BITS, session->ack_timeout)
208 if (node->session) {
212 if (node->session->context->sendqueue) {
213 LL_DELETE(node->session->context->sendqueue, node);
215 coap_session_release(node->session);
271 coap_get_session_client_psk_key(const coap_session_t *session) {
273 if (session->psk_key) {
274 return session->psk_key;
276 if (session->cpsk_setup_data.psk_info.key.length)
277 return &session->cpsk_setup_data.psk_info.key;
285 coap_get_session_client_psk_identity(const coap_session_t *session) {
287 if (session->psk_identity) {
288 return session->psk_identity;
290 if (session->cpsk_setup_data.psk_info.identity.length)
291 return &session->cpsk_setup_data.psk_info.identity;
299 coap_get_session_server_psk_key(const coap_session_t *session) {
301 if (session->psk_key)
302 return session->psk_key;
304 if (session->context->spsk_setup_data.psk_info.key.length)
305 return &session->context->spsk_setup_data.psk_info.key;
312 coap_get_session_server_psk_hint(const coap_session_t *session) {
314 if (session->psk_hint)
315 return session->psk_hint;
317 if (session->context->spsk_setup_data.psk_info.hint.length)
318 return &session->context->spsk_setup_data.psk_info.hint;
662 coap_option_check_critical(coap_session_t *session,
665 coap_context_t *ctx = session->context;
713 if (session->proxy_session)
748 if (coap_get_block_b(session, pdu, opt_iter.number, &block)) {
777 coap_send_ack(coap_session_t *session, const coap_pdu_t *request) {
782 COAP_PROTO_NOT_RELIABLE(session->proto)) {
785 result = coap_send_internal(session, response);
791 coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu) {
796 bytes_written = session->sock.lfunc[COAP_LAYER_SESSION].l_write(session,
804 coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) {
807 if (session->state == COAP_SESSION_STATE_NONE) {
811 if (session->type != COAP_SESSION_TYPE_CLIENT)
817 (session->sock.flags & COAP_SOCKET_NOT_EMPTY) &&
818 (session->sock.flags & COAP_SOCKET_MULTICAST)) {
824 if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
826 session->con_active >= COAP_NSTART(session))) {
827 return coap_session_delay_pdu(session, pdu, node);
830 if ((session->sock.flags & COAP_SOCKET_NOT_EMPTY) &&
831 (session->sock.flags & COAP_SOCKET_WANT_WRITE))
832 return coap_session_delay_pdu(session, pdu, node);
834 bytes_written = coap_session_send_pdu(session, pdu);
836 COAP_PROTO_NOT_RELIABLE(session->proto))
837 session->con_active++;
843 coap_send_error(coap_session_t *session,
851 assert(session);
855 result = coap_send_internal(session, response);
861 coap_send_message_type(coap_session_t *session, const coap_pdu_t *request,
866 if (request && COAP_PROTO_NOT_RELIABLE(session->proto)) {
869 result = coap_send_internal(session, response);
875 * Calculates the initial timeout based on the session CoAP transmission
881 * @param session session timeout is associated with
888 coap_calc_timeout(coap_session_t *session, unsigned char r) {
914 coap_wait_ack(coap_context_t *context, coap_session_t *session,
918 node->session = coap_session_reference(session);
941 coap_session_str(node->session), node->id,
957 coap_send_test_extended_token(coap_session_t *session) {
966 coap_new_message_id(session),
967 coap_session_max_pdu_size(session));
971 token = coap_new_binary(session->max_token_size);
976 for (i = 0; i < session->max_token_size; i++) {
979 coap_add_token(pdu, session->max_token_size, token->s);
984 session->max_token_checked = COAP_EXT_T_CHECKING; /* Checking out this one */
985 if ((mid = coap_send_internal(session, pdu)) == COAP_INVALID_MID)
987 session->remote_test_mid = mid;
993 coap_client_delay_first(coap_session_t *session) {
995 if (session->type == COAP_SESSION_TYPE_CLIENT && session->doing_first) {
998 if (session->delay_recursive) {
1002 session->delay_recursive = 1;
1008 coap_session_reference(session);
1009 while (session->doing_first != 0) {
1010 int result = coap_io_process(session->context, 1000);
1013 session->doing_first = 0;
1014 session->delay_recursive = 0;
1015 coap_session_release(session);
1021 if (session->doing_first == 1) {
1024 coap_session_str(session));
1025 session->doing_first = 0;
1029 session->delay_recursive = 0;
1030 coap_session_release(session);
1033 (void)session;
1039 coap_send(coap_session_t *session, coap_pdu_t *pdu) {
1052 pdu->session = session;
1054 if (session->type == COAP_SESSION_TYPE_CLIENT &&
1055 !coap_netif_available(session)) {
1065 if (!coap_client_delay_first(session)) {
1071 if (session->max_token_checked == COAP_EXT_T_NOT_CHECKED &&
1072 session->max_token_size > COAP_TOKEN_DEFAULT_MAX &&
1073 session->type == COAP_SESSION_TYPE_CLIENT &&
1075 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1080 if (coap_send_test_extended_token(session) == COAP_INVALID_MID) {
1089 session->doing_first = 1;
1090 if (!coap_client_delay_first(session)) {
1100 pdu->actual_token.length > session->max_token_size) {
1102 pdu->actual_token.length, session->max_token_size);
1108 if (COAP_PROTO_RELIABLE(session->proto) && pdu->type == COAP_MESSAGE_NON)
1112 if (session->oscore_encryption) {
1113 if (session->recipient_ctx->initial_state == 1) {
1119 session->doing_first = 1;
1129 if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) {
1130 return coap_send_internal(session, pdu);
1143 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block) &&
1148 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block) &&
1159 if (session->last_token &&
1160 coap_binary_equal(&pdu->actual_token, session->last_token)) {
1163 coap_delete_bin_const(session->last_token);
1164 session->last_token = coap_new_bin_const(pdu->actual_token.s,
1168 (session->block_mode & COAP_BLOCK_NO_PREEMPTIVE_RTAG) == 0 &&
1173 ++session->tx_rtag),
1181 if (session->block_mode & COAP_BLOCK_TRY_Q_BLOCK &&
1182 session->type == COAP_SESSION_TYPE_CLIENT &&
1184 if (coap_block_test_q_block(session, pdu) == COAP_INVALID_MID) {
1188 session->doing_first = 1;
1189 if (!coap_client_delay_first(session)) {
1191 set_block_mode_drop_q(session->block_mode);
1199 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK))
1205 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
1215 LL_FOREACH(session->lg_xmit, lg_xmit) {
1229 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
1239 LL_FOREACH(session->lg_xmit, lg_xmit) {
1261 coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
1281 LL_FOREACH(session->lg_xmit, lg_xmit) {
1301 session->oscore_encryption ||
1303 ((pdu->type == COAP_MESSAGE_NON || COAP_PROTO_RELIABLE(session->proto)) &&
1307 if (!session->lg_xmit && have_block1) {
1312 LL_FOREACH(session->lg_crcv, lg_crcv) {
1331 LL_DELETE(session->lg_crcv, lg_crcv);
1336 LL_DELETE(session->lg_crcv, lg_crcv);
1337 coap_block_delete_lg_crcv(session, lg_crcv);
1342 if (have_block1 && session->lg_xmit) {
1343 LL_FOREACH(session->lg_xmit, lg_xmit) {
1351 lg_crcv = coap_block_new_lg_crcv(session, pdu, lg_xmit);
1357 /* Need to update the token as set up in the session->lg_xmit */
1361 if (session->sock.flags & COAP_SOCKET_MULTICAST)
1362 coap_address_copy(&session->addr_info.remote, &session->sock.mcast_addr);
1367 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
1368 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
1371 mid = coap_send_internal(session, pdu);
1373 mid = coap_send_internal(session, pdu);
1378 LL_PREPEND(session->lg_crcv, lg_crcv);
1380 coap_block_delete_lg_crcv(session, lg_crcv);
1388 coap_send_internal(coap_session_t *session, coap_pdu_t *pdu) {
1393 pdu->session = session;
1404 if (coap_print_addr(&session->addr_info.local, (uint8_t *)addr_str,
1503 if (session->echo) {
1504 if (!coap_insert_option(pdu, COAP_OPTION_ECHO, session->echo->length,
1505 session->echo->s))
1507 coap_delete_bin_const(session->echo);
1508 session->echo = NULL;
1511 if (session->oscore_encryption) {
1518 if (!coap_pdu_encode_header(pdu, session->proto)) {
1523 if (COAP_PROTO_RELIABLE(session->proto) &&
1524 session->state == COAP_SESSION_STATE_ESTABLISHED) {
1525 if (!session->csm_block_supported) {
1538 } else if (!session->csm_bert_rem_support) {
1554 if (session->oscore_encryption &&
1557 coap_pdu_t *osc_pdu = coap_oscore_new_pdu_encrypted(session, pdu, NULL,
1564 bytes_written = coap_send_pdu(session, osc_pdu, NULL);
1569 bytes_written = coap_send_pdu(session, pdu, NULL);
1572 /* do not free pdu as it is stored with session for later use */
1580 if (COAP_PROTO_RELIABLE(session->proto) &&
1582 if (coap_session_delay_pdu(session, pdu, NULL) == COAP_PDU_DELAYED) {
1583 session->partial_write = (size_t)bytes_written;
1584 /* do not free pdu as it is stored with session for later use */
1593 || COAP_PROTO_RELIABLE(session->proto)) {
1609 node->timeout = coap_calc_timeout(session, r);
1610 return coap_wait_ack(session->context, session, node);
1622 if (node->retransmit_cnt < node->session->max_retransmit) {
1628 coap_handle_event(context, COAP_EVENT_MSG_RETRANSMITTED, node->session);
1652 coap_session_str(node->session), node->id);
1655 coap_session_str(node->session), node->id,
1660 if (node->session->con_active)
1661 node->session->con_active--;
1662 bytes_written = coap_send_pdu(node->session, node->pdu, node);
1665 coap_session_connected(node->session);
1671 in progress. node was moved to the send queue of the session. */
1683 coap_session_str(node->session), node->id, node->retransmit_cnt);
1689 coap_handle_failed_notify(context, node->session, &node->pdu->actual_token);
1692 if (node->session->con_active) {
1693 node->session->con_active--;
1694 if (node->session->state == COAP_SESSION_STATE_ESTABLISHED) {
1697 * session that needs to be immediately released,
1703 coap_session_connected(node->session);
1709 coap_check_update_token(node->session, node->pdu);
1710 context->nack_handler(node->session, node->pdu, COAP_NACK_TOO_MANY_RETRIES, node->id);
1717 coap_handle_dgram_for_proto(coap_context_t *ctx, coap_session_t *session, coap_packet_t *packet) {
1723 if (session->proto == COAP_PROTO_DTLS) {
1725 if (session->type == COAP_SESSION_TYPE_HELLO)
1726 result = coap_dtls_hello(session, data, data_len);
1729 if (session->tls)
1730 result = coap_dtls_receive(session, data, data_len);
1731 } else if (session->proto == COAP_PROTO_UDP) {
1732 result = coap_handle_dgram(ctx, session, data, data_len);
1739 coap_connect_session(coap_session_t *session, coap_tick_t now) {
1743 session->sock.flags &= ~(COAP_SOCKET_WANT_CONNECT | COAP_SOCKET_CAN_CONNECT);
1745 if (coap_netif_strm_connect2(session)) {
1746 session->last_rx_tx = now;
1747 coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session);
1748 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1750 coap_handle_event(session->context, COAP_EVENT_TCP_FAILED, session);
1751 coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE);
1758 coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) {
1760 assert(session->sock.flags & COAP_SOCKET_CONNECTED);
1762 while (session->delayqueue) {
1764 coap_queue_t *q = session->delayqueue;
1766 coap_session_str(session), (int)q->pdu->mid);
1767 assert(session->partial_write < q->pdu->used_size + q->pdu->hdr_size);
1768 bytes_written = session->sock.lfunc[COAP_LAYER_SESSION].l_write(session,
1769 q->pdu->token - q->pdu->hdr_size + session->partial_write,
1770 q->pdu->used_size + q->pdu->hdr_size - session->partial_write);
1772 session->last_rx_tx = now;
1774 (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size - session->partial_write) {
1776 session->partial_write += (size_t)bytes_written;
1779 session->delayqueue = q->next;
1780 session->partial_write = 0;
1786 coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) {
1802 assert(session->sock.flags & (COAP_SOCKET_CONNECTED | COAP_SOCKET_MULTICAST | COAP_SOCKET_BROADCAST));
1804 assert(session->sock.flags & (COAP_SOCKET_CONNECTED | COAP_SOCKET_MULTICAST));
1810 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1812 memcpy(&packet->addr_info, &session->addr_info, sizeof(packet->addr_info));
1813 bytes_read = coap_netif_dgrm_read(session, packet);
1817 /* Reset the session back to startup defaults */
1818 coap_session_disconnected(session, COAP_NACK_ICMP_ISSUE);
1820 session->last_rx_tx = now;
1821 memcpy(&session->addr_info, &packet->addr_info,
1822 sizeof(session->addr_info));
1823 coap_handle_dgram_for_proto(ctx, session, packet);
1826 } else if (session->proto == COAP_PROTO_WS ||
1827 session->proto == COAP_PROTO_WSS) {
1831 bytes_read = session->sock.lfunc[COAP_LAYER_SESSION].l_read(session,
1835 coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE);
1839 session->last_rx_tx = now;
1841 pdu = coap_pdu_init(0, 0, 0, coap_session_max_pdu_rcv_size(session));
1849 if (!coap_pdu_parse(session->proto, packet->payload, bytes_read, pdu)) {
1850 coap_handle_event(session->context, COAP_EVENT_BAD_PACKET, session);
1862 coap_dispatch(ctx, session, pdu);
1872 bytes_read = session->sock.lfunc[COAP_LAYER_SESSION].l_read(session,
1876 session->last_rx_tx = now;
1881 if (session->partial_pdu) {
1882 size_t len = session->partial_pdu->used_size
1883 + session->partial_pdu->hdr_size
1884 - session->partial_read;
1886 memcpy(session->partial_pdu->token - session->partial_pdu->hdr_size
1887 + session->partial_read, p, n);
1891 if (coap_pdu_parse_header(session->partial_pdu, session->proto)
1892 && coap_pdu_parse_opt(session->partial_pdu)) {
1896 coap_dispatch(ctx, session, session->partial_pdu);
1901 coap_delete_pdu(session->partial_pdu);
1902 session->partial_pdu = NULL;
1903 session->partial_read = 0;
1905 session->partial_read += n;
1907 } else if (session->partial_read > 0) {
1908 size_t hdr_size = coap_pdu_parse_header_size(session->proto,
1909 session->read_header);
1910 size_t tkl = session->read_header[0] & 0x0f;
1913 size_t len = hdr_size + tok_ext_bytes - session->partial_read;
1915 memcpy(session->read_header + session->partial_read, p, n);
1919 size_t size = coap_pdu_parse_size(session->proto, session->read_header,
1923 coap_session_str(session),
1929 session->partial_pdu = coap_pdu_init(0, 0, 0,
1930 coap_session_max_pdu_rcv_size(session));
1931 if (session->partial_pdu == NULL) {
1935 if (session->partial_pdu->alloc_size < size && !coap_pdu_resize(session->partial_pdu, size)) {
1939 session->partial_pdu->hdr_size = (uint8_t)hdr_size;
1940 session->partial_pdu->used_size = size;
1941 memcpy(session->partial_pdu->token - hdr_size, session->read_header, hdr_size + tok_ext_bytes);
1942 session->partial_read = hdr_size + tok_ext_bytes;
1944 if (coap_pdu_parse_header(session->partial_pdu, session->proto)) {
1948 coap_dispatch(ctx, session, session->partial_pdu);
1953 coap_delete_pdu(session->partial_pdu);
1954 session->partial_pdu = NULL;
1955 session->partial_read = 0;
1958 session->partial_read += bytes_read;
1961 session->read_header[0] = *p++;
1963 if (!coap_pdu_parse_header_size(session->proto,
1964 session->read_header)) {
1968 session->partial_read = 1;
1973 coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE);
2014 coap_session_t *session = coap_endpoint_get_session(endpoint, packet, now);
2015 if (session) {
2017 coap_session_str(session), bytes_read);
2018 result = coap_handle_dgram_for_proto(ctx, session, packet);
2019 if (endpoint->proto == COAP_PROTO_DTLS && session->type == COAP_SESSION_TYPE_HELLO && result == 1)
2020 coap_session_new_dtls_session(session, now);
2041 coap_session_t *session = coap_new_server_session(ctx, endpoint);
2042 if (session)
2043 session->last_rx_tx = now;
2044 return session != NULL;
2070 /* Make sure the session object is not deleted in one of the callbacks */
2085 /* Make sure the session object is not deleted in one of the callbacks */
2153 if (sock->session) {
2154 coap_session_t *session = sock->session;
2156 /* Make sure the session object is not deleted
2158 coap_session_reference(session);
2163 coap_connect_session(session, now);
2164 if (coap_netif_available(session) &&
2174 coap_read_session(session->context, session, now);
2185 coap_write_session(session->context, session, now);
2187 /* Now dereference session so it can go away if needed */
2188 coap_session_release(session);
2211 coap_handle_dgram(coap_context_t *ctx, coap_session_t *session,
2216 assert(COAP_PROTO_NOT_RELIABLE(session->proto));
2223 pdu = coap_pdu_init(0, 0, 0, coap_session_max_pdu_rcv_size(session));
2227 if (!coap_pdu_parse(session->proto, msg, msg_len, pdu)) {
2228 coap_handle_event(session->context, COAP_EVENT_BAD_PACKET, session);
2233 coap_dispatch(ctx, session, pdu);
2242 coap_send_rst(session, pdu);
2248 coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t id,
2257 if (session == (*queue)->session && id == (*queue)->id) { /* found message id */
2265 coap_session_str(session), id);
2274 } while (q && (session != q->session || id != q->id));
2284 coap_session_str(session), id);
2293 coap_cancel_session_messages(coap_context_t *context, coap_session_t *session,
2297 while (context->sendqueue && context->sendqueue->session == session) {
2301 coap_session_str(session), q->id);
2303 coap_check_update_token(session, q->pdu);
2304 context->nack_handler(session, q->pdu, reason, q->id);
2316 if (q->session == session) {
2319 coap_session_str(session), q->id);
2321 coap_check_update_token(session, q->pdu);
2322 context->nack_handler(session, q->pdu, reason, q->id);
2334 coap_cancel_all_messages(coap_context_t *context, coap_session_t *session,
2336 /* cancel all messages in sendqueue that belong to session
2347 if (q->session == session &&
2351 coap_session_str(session), q->id);
2352 if (q->pdu->type == COAP_MESSAGE_CON && session->con_active) {
2353 session->con_active--;
2354 if (session->state == COAP_SESSION_STATE_ESTABLISHED)
2355 /* Flush out any entries on session->delayqueue */
2356 coap_session_connected(session);
2497 free_wellknown_response(coap_session_t *session COAP_UNUSED, void *app_ptr) {
2503 coap_session_t *session,
2510 ssize_t wkc_len = get_wkc_len(session->context, query);
2520 result = coap_print_wellknown(session->context, data_string->s, &len, 0,
2529 if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) {
2549 free_wellknown_response(session, data_string);
2550 } else if (!coap_add_data_large_response(resource, session, request,
2564 free_wellknown_response(session, data_string);
2576 * This function cancels outstanding messages for the session and
2578 * sent->session and the token are removed. Calling this function is
2599 coap_cancel_all_messages(context, sent->session, &sent->pdu->actual_token);
2600 num_cancelled += coap_delete_observer(r, sent->session, &sent->pdu->actual_token);
2623 * Checks if the response code is 0.00 and if either the session is reliable or
2653 * @param session The session this request/response are associated with.
2661 coap_session_t *session, coap_resource_t *resource) {
2683 COAP_PROTO_NOT_RELIABLE(session->proto)) {
2699 } else if (resource && session->context->mcast_per_resource &&
2700 coap_is_mcast(&session->addr_info.local)) {
2723 COAP_PROTO_RELIABLE(session->proto))) {
2733 if (coap_is_mcast(&session->addr_info.local)) {
2738 if ((!resource || session->context->mcast_per_resource == 0) &&
2758 handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu) {
2789 if (coap_is_mcast(&session->addr_info.local)) {
2790 if (COAP_PROTO_RELIABLE(session->proto) || pdu->type != COAP_MESSAGE_NON) {
2796 async = coap_find_async(session, pdu->actual_token);
2804 coap_send_ack(session, pdu);
3010 if ((resource->flags & COAP_RESOURCE_FLAGS_OSCORE_ONLY) && !session->oscore_encryption) {
3045 coap_is_mcast(&session->addr_info.local)) {
3052 0, pdu->mid, coap_session_max_pdu_size(session));
3058 response->session = session;
3084 if (session->block_mode & COAP_BLOCK_USE_LIBCOAP) {
3085 uint8_t block_mode = session->block_mode;
3089 session->block_mode |= COAP_BLOCK_SINGLE_BODY;
3090 if (coap_handle_request_put_block(context, session, pdu, response,
3093 session->block_mode = block_mode;
3096 session->block_mode = block_mode;
3098 if (coap_handle_request_send_block(session, pdu, response, resource,
3115 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK2, &block)) {
3121 } else if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2,
3129 subscription = coap_add_observer(resource, session, &pdu->actual_token,
3134 coap_touch_observer(context, session, &pdu->actual_token);
3141 coap_delete_observer(resource, session, &pdu->actual_token);
3149 COAP_PROTO_NOT_RELIABLE(session->proto) &&
3155 coap_send_ack(session, pdu);
3156 if (pdu->mid == session->last_con_mid) {
3162 session->last_con_mid = pdu->mid;
3168 context->unknown_session = session;
3179 h(resource, session, pdu, query, response);
3182 coap_check_code_lg_xmit(session, pdu, response, resource, query);
3190 LL_DELETE(session->lg_srcv, free_lg_srcv);
3191 coap_block_delete_lg_srcv(session, free_lg_srcv);
3209 respond = no_response(pdu, response, session, resource);
3221 coap_delete_observer(resource, session, &pdu->actual_token);
3239 if (!coap_is_mcast(&session->addr_info.local) ||
3245 if (session->block_mode & COAP_BLOCK_USE_LIBCOAP &&
3247 coap_get_block_b(session, response, COAP_OPTION_Q_BLOCK2, &block) &&
3249 if (coap_send_q_block2(session, resource, query, pdu->code, block,
3259 if (coap_send_internal(session, response) == COAP_INVALID_MID) {
3272 if (!coap_pdu_encode_header(response, session->proto)) {
3281 delay = (COAP_DEFAULT_LEISURE_TICKS(session) * r) / 256;
3283 coap_session_str(session),
3290 coap_wait_ack(session->context, session, node);
3294 coap_session_str(session),
3303 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
3304 if (COAP_PROTO_RELIABLE(session->proto)) {
3311 if (block.m && ((block.num + 1) % COAP_MAX_PAYLOADS(session))) {
3337 handle_response(coap_context_t *context, coap_session_t *session,
3345 coap_cancel_all_messages(context, session, &rcvd->actual_token);
3348 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
3350 if (rcvd->mid == session->last_con_mid) {
3354 session->last_con_mid = rcvd->mid;
3356 if (rcvd->mid == session->last_ack_mid) {
3360 session->last_ack_mid = rcvd->mid;
3364 if (session->max_token_checked == COAP_EXT_T_CHECKING &&
3365 session->remote_test_mid == rcvd->mid) {
3367 if (rcvd->actual_token.length != session->max_token_size ||
3371 session->max_token_size = COAP_TOKEN_DEFAULT_MAX;
3375 session->max_token_checked = COAP_EXT_T_CHECKED;
3376 session->doing_first = 0;
3381 if (session->block_mode & COAP_BLOCK_PROBE_Q_BLOCK &&
3382 session->remote_test_mid == rcvd->mid) {
3385 set_block_mode_drop_q(session->block_mode);
3389 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &qblock)) {
3391 set_block_mode_has_q(session->block_mode);
3394 set_block_mode_drop_q(session->block_mode);
3397 session->doing_first = 0;
3402 if (session->block_mode & COAP_BLOCK_USE_LIBCOAP) {
3404 if (coap_handle_response_send_block(session, sent, rcvd)) {
3406 coap_send_ack(session, rcvd);
3411 if (coap_handle_response_get_block(context, session, sent, rcvd,
3417 if (session->doing_first)
3418 session->doing_first = 0;
3422 if (context->response_handler(session, sent, rcvd,
3424 coap_send_rst(session, rcvd);
3426 coap_send_ack(session, rcvd);
3428 coap_send_ack(session, rcvd);
3435 handle_signaling(coap_context_t *context, coap_session_t *session,
3444 if (session->max_token_checked == COAP_EXT_T_NOT_CHECKED) {
3445 session->max_token_size = COAP_TOKEN_DEFAULT_MAX;
3449 coap_session_set_mtu(session, coap_decode_var_bytes(coap_opt_value(option),
3453 session->csm_block_supported = 1;
3455 session->max_token_size =
3458 if (session->max_token_size < COAP_TOKEN_DEFAULT_MAX)
3459 session->max_token_size = COAP_TOKEN_DEFAULT_MAX;
3460 else if (session->max_token_size > COAP_TOKEN_EXT_MAX)
3461 session->max_token_size = COAP_TOKEN_EXT_MAX;
3462 session->max_token_checked = COAP_EXT_T_CHECKED;
3466 if (session->mtu > COAP_BERT_BASE && session->csm_block_supported)
3467 session->csm_bert_rem_support = 1;
3469 session->csm_bert_rem_support = 0;
3471 if (session->state == COAP_SESSION_STATE_CSM)
3472 coap_session_connected(session);
3476 context->ping_handler(session, pdu, pdu->mid);
3480 coap_send_internal(session, pong);
3483 session->last_pong = session->last_rx_tx;
3485 context->pong_handler(session, pdu, pdu->mid);
3489 coap_session_disconnected(session, COAP_NACK_RST);
3495 check_token_size(coap_session_t *session, const coap_pdu_t *pdu) {
3498 (session->type == COAP_SESSION_TYPE_CLIENT ?
3499 session->max_token_size : session->context->max_token_size)) {
3501 if (session->max_token_size > COAP_TOKEN_DEFAULT_MAX) {
3515 if (coap_send_internal(session, response) == COAP_INVALID_MID)
3520 coap_send_rst(session, pdu);
3528 coap_dispatch(coap_context_t *context, coap_session_t *session,
3541 pdu->session = session;
3548 coap_option_check_critical(session, pdu, &opt_filter) == 0) {
3550 coap_send_rst(session, pdu);
3560 if (coap_send_internal(session, response) == COAP_INVALID_MID)
3564 coap_send_rst(session, pdu);
3578 if (COAP_PDU_IS_RESPONSE(pdu) && !session->oscore_encryption)
3586 /* Need to check whether this is a direct or proxy session */
3608 coap_remove_from_queue(&context->sendqueue, session, pdu->mid, &sent);
3609 if ((dec_pdu = coap_oscore_decrypt_pdu(session, pdu)) == NULL) {
3610 if (session->recipient_ctx == NULL ||
3611 session->recipient_ctx->initial_state == 0) {
3617 session->oscore_encryption = 1;
3629 coap_remove_from_queue(&context->sendqueue, session, pdu->mid, &sent);
3631 if (sent && session->con_active) {
3632 session->con_active--;
3633 if (session->state == COAP_SESSION_STATE_ESTABLISHED)
3634 /* Flush out any entries on session->delayqueue */
3635 coap_session_connected(session);
3637 if (coap_option_check_critical(session, pdu, &opt_filter) == 0) {
3647 coap_touch_observer(context, sent->session, &sent->pdu->actual_token);
3657 COAP_PROTO_NOT_RELIABLE(session->proto) &&
3658 coap_get_block_b(session, sent->pdu,
3665 coap_send_q_block1(session, block, sent->pdu,
3669 coap_send_q_blocks(session, sent->pdu->lg_xmit, block,
3686 if (pdu->mid == session->last_ping_mid &&
3687 context->ping_timeout && session->last_ping > 0)
3692 if (session->block_mode & COAP_BLOCK_PROBE_Q_BLOCK &&
3693 session->remote_test_mid == pdu->mid) {
3695 set_block_mode_drop_q(session->block_mode);
3701 if (session->max_token_checked == COAP_EXT_T_CHECKING &&
3702 session->remote_test_mid == pdu->mid) {
3704 session->max_token_size = COAP_TOKEN_DEFAULT_MAX;
3705 session->max_token_checked = COAP_EXT_T_CHECKED;
3706 session->doing_first = 0;
3713 if (session->con_active) {
3714 session->con_active--;
3715 if (session->state == COAP_SESSION_STATE_ESTABLISHED)
3716 /* Flush out any entries on session->delayqueue */
3717 coap_session_connected(session);
3721 coap_remove_from_queue(&context->sendqueue, session, pdu->mid, &sent);
3728 coap_check_update_token(sent->session, sent->pdu);
3729 context->nack_handler(sent->session, sent->pdu,
3734 context->pong_handler(session, pdu, pdu->mid);
3736 session->last_pong = session->last_rx_tx;
3737 session->last_ping_mid = COAP_INVALID_MID;
3745 if (obs->pdu->mid == pdu->mid && obs->session == session) {
3746 /* Need to do this now as session may get de-referenced */
3747 coap_session_reference(session);
3748 coap_delete_observer(r, session, &obs->pdu->actual_token);
3750 context->nack_handler(session, NULL, COAP_NACK_RST, pdu->mid);
3751 coap_session_release(session);
3758 context->nack_handler(session, NULL, COAP_NACK_RST, pdu->mid);
3765 coap_remove_from_queue(&context->sendqueue, session, pdu->mid, &sent);
3768 if (coap_option_check_critical(session, pdu, &opt_filter) == 0) {
3770 coap_send_rst(session, pdu);
3773 if (!check_token_size(session, pdu)) {
3780 coap_option_check_critical(session, pdu, &opt_filter) == 0) {
3789 if (coap_send_internal(session, response) == COAP_INVALID_MID)
3793 coap_send_rst(session, pdu);
3797 if (!check_token_size(session, pdu)) {
3809 handle_signaling(context, session, pdu);
3814 handle_request(context, session, pdu);
3819 handle_response(context, session, sent ? sent->pdu : NULL, pdu);
3825 context->ping_handler(session, pdu, pdu->mid);
3834 if (!coap_is_mcast(&session->addr_info.local)) {
3836 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
3839 if (session->last_tx_rst + COAP_TICKS_PER_SECOND/4 < now) {
3840 coap_send_message_type(session, pdu, COAP_MESSAGE_RST);
3841 session->last_tx_rst = now;
3845 coap_send_message_type(session, pdu, COAP_MESSAGE_RST);
3854 coap_check_update_token(session, sent->pdu);
3855 context->nack_handler(session, sent->pdu, COAP_NACK_BAD_RESPONSE, sent->id);
3858 coap_handle_event(context, COAP_EVENT_BAD_PACKET, session);
3930 coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session) {
3934 return context->handle_event(session, event);
3979 handle_request(context, async->session, async->pdu);
3982 coap_free_async(async->session, async);
4350 coap_mcast_set_hops(coap_session_t *session, size_t hops) {
4351 if (session && coap_is_mcast(&session->addr_info.remote)) {
4352 switch (session->addr_info.remote.addr.sa.sa_family) {
4355 if (setsockopt(session->sock.fd, IPPROTO_IP, IP_MULTICAST_TTL,
4365 if (setsockopt(session->sock.fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
4390 coap_mcast_set_hops(coap_session_t *session COAP_UNUSED,