Exemple #1
0
static smcp_status_t
request_handler(void* context) {
	/*	This will respond to every GET request to `/hello-world' with
	**	"Hello world!". Everyone else gets a 4.04 Not Found. */

	printf("Got a request!\n");

	if(smcp_inbound_get_code() != COAP_METHOD_GET) {
		return SMCP_STATUS_NOT_IMPLEMENTED;
	}

	while(smcp_inbound_peek_option(NULL, NULL) != COAP_OPTION_URI_PATH)
		if(smcp_inbound_next_option(NULL, NULL) == COAP_OPTION_INVALID)
			break;

	if(smcp_inbound_option_strequal(COAP_OPTION_URI_PATH, "hello-world")) {

		smcp_outbound_begin_response(COAP_RESULT_205_CONTENT);

		smcp_outbound_add_option_uint(
			COAP_OPTION_CONTENT_TYPE,
			COAP_CONTENT_TYPE_TEXT_PLAIN
		);

		smcp_outbound_append_content("Hello world!", SMCP_CSTR_LEN);

		return smcp_outbound_send();
	}

	return SMCP_STATUS_NOT_FOUND;
}
Exemple #2
0
smcp_status_t
plugtest_separate_async_resend_response(void* context)
{
	smcp_status_t ret = 0;
	struct smcp_async_response_s* async_response = (void*)context;

#if !SMCP_AVOID_PRINTF || VERBOSE_DEBUG
	printf("Resending async response. . . %p\n",async_response);
#endif

	ret = smcp_outbound_begin_async_response(COAP_RESULT_205_CONTENT,async_response);
	require_noerr(ret,bail);

	ret = smcp_outbound_add_option_uint(COAP_OPTION_CONTENT_TYPE, COAP_CONTENT_TYPE_TEXT_PLAIN);
	require_noerr(ret,bail);

	ret = smcp_outbound_append_content("This was an asynchronous response!",SMCP_CSTR_LEN);
	require_noerr(ret,bail);

	ret = smcp_outbound_send();
	require_noerr(ret,bail);

bail:
	return ret;
}
Exemple #3
0
smcp_status_t
smcp_outbound_set_var_content_unsigned_long_int(unsigned long int v) {
	smcp_outbound_add_option_uint(
		COAP_OPTION_CONTENT_TYPE,
		SMCP_CONTENT_TYPE_APPLICATION_FORM_URLENCODED
	);
#if SMCP_AVOID_PRINTF
{
		char nstr[12];
		smcp_outbound_append_content("v=", SMCP_CSTR_LEN);
		return smcp_outbound_append_content(uint32_to_dec_cstr(nstr,v), SMCP_CSTR_LEN);
	}
#else
	return smcp_outbound_set_content_formatted_const("v=%ul",v);
#endif
}
Exemple #4
0
static smcp_status_t
request_handler(void* context)
{
	/* This request handler will respond to every GET request with "Hello world!".
	 * It isn't usually a good idea to (almost) completely ignore the actual
	 * request, but since this is just an example we can get away with it.
	 * We do more processing on the request in the other examples. */

	printf("Got a request!\n");

	// Only handle GET requests for now. Returning SMCP_STATUS_NOT_IMPLEMENTED
	// here without sending a response will cause us to automatically
	// send a METHOD_NOT_IMPLEMENTED response.
	if(smcp_inbound_get_code() != COAP_METHOD_GET) {
		return SMCP_STATUS_NOT_IMPLEMENTED;
	}

	// Begin describing the response message. (2.05 CONTENT,
	// in this case)
	smcp_outbound_begin_response(COAP_RESULT_205_CONTENT);

	// Add an option describing the content type as plaintext.
	smcp_outbound_add_option_uint(
		COAP_OPTION_CONTENT_TYPE,
		COAP_CONTENT_TYPE_TEXT_PLAIN
	);

	// Set the content of our response to be "Hello world!".
	smcp_outbound_append_content("Hello world!", SMCP_CSTR_LEN);

	// Send the response we hae created, passing the return value
	// to our caller.
	return smcp_outbound_send();
}
Exemple #5
0
smcp_status_t
smcp_outbound_quick_response(coap_code_t code, const char* body) {
	smcp_outbound_begin_response(code);
	if (body) {
		smcp_outbound_append_content(body, SMCP_CSTR_LEN);
	}
	return smcp_outbound_send();
}
Exemple #6
0
static smcp_status_t
resend_async_response(void* context) {
    smcp_status_t ret = 0;
    smcp_curl_request_t request = (smcp_curl_request_t)context;
    struct smcp_async_response_s* async_response = &request->async_response;

    coap_size_t len = (coap_size_t)request->content_len;
    if(len>512) {
        len = 512;
    }

    {
        uint16_t code;
        curl_easy_getinfo(request->curl, CURLINFO_RESPONSE_CODE,&code);
        code = http_to_coap_code(code);

        ret = smcp_outbound_begin_async_response(code,async_response);
        require_noerr(ret,bail);
    }

    {
        const char* content_type_string;
        curl_easy_getinfo(request->curl, CURLINFO_CONTENT_TYPE,&content_type_string);
        coap_content_type_t content_type = coap_content_type_from_cstr(content_type_string);
        if(content_type!=COAP_CONTENT_TYPE_UNKNOWN) {
            ret = smcp_outbound_add_option_uint(COAP_OPTION_CONTENT_TYPE, content_type);
            check_noerr(ret);
        } else {
            DEBUG_PRINTF("Unrecognised content-type: %s",content_type_string);
        }
    }

    ret = smcp_outbound_append_content(request->content, len);
    require_noerr(ret,bail);

    ret = smcp_outbound_send();
    require_noerr(ret,bail);

    if(ret) {
        assert_printf(
            "smcp_outbound_send() returned error %d(%s).\n",
            ret,
            smcp_status_to_cstr(ret)
        );
        goto bail;
    }

bail:
    return ret;
}
Exemple #7
0
static smcp_status_t
request_handler_get_listing_(
	smcp_group_mgr_t self
) {
	smcp_status_t ret = 0;
	int i;
	char name_str[3] = { };

	// Fetching the group table.
	ret = smcp_outbound_begin_response(COAP_RESULT_205_CONTENT);
	require_noerr(ret, bail);

	ret = smcp_outbound_add_option_uint(COAP_OPTION_CONTENT_TYPE, COAP_CONTENT_TYPE_APPLICATION_LINK_FORMAT);
	require_noerr(ret, bail);

	for (i = 0; i < SMCP_CONF_MAX_GROUPS; i++) {
		if (!self->group_table[i].in_use) {
			continue;
		}
		name_str[0] = get_hex_char(i >> 4);
		name_str[1] = get_hex_char(i >> 0);

		smcp_outbound_append_content("<", SMCP_CSTR_LEN);
		smcp_outbound_append_content(name_str, SMCP_CSTR_LEN);

		smcp_outbound_append_content("/>;ct=40;obs,", SMCP_CSTR_LEN);

#if SMCP_ADD_NEWLINES_TO_LIST_OUTPUT
		smcp_outbound_append_content("\n", SMCP_CSTR_LEN);
#endif
	}

	ret = smcp_outbound_send();

bail:
	return ret;
}
Exemple #8
0
static smcp_status_t
request_handler(void* context) {
	/*	This will respond to every GET request to `/hello-world' with
	**	"Hello world!". Everyone else gets a 4.04 Not Found. */

	printf("Got a request!\n");

	// Only handle GET requests for now. Returning SMCP_STATUS_NOT_IMPLEMENTED
	// here without sending a response will cause us to automatically
	// send a METHOD_NOT_IMPLEMENTED response.
	if(smcp_inbound_get_code() != COAP_METHOD_GET) {
		return SMCP_STATUS_NOT_IMPLEMENTED;
	}

	// Skip to the URI path option
	while(smcp_inbound_peek_option(NULL, NULL) != COAP_OPTION_URI_PATH)
		if(smcp_inbound_next_option(NULL, NULL) == COAP_OPTION_INVALID)
			break;

	// If our URI path matches what we are looking for...
	if(smcp_inbound_option_strequal(COAP_OPTION_URI_PATH, "hello-world")) {

		// Begin describing the response message. (2.05 CONTENT,
		// in this case)
		smcp_outbound_begin_response(COAP_RESULT_205_CONTENT);

		// Add an option describing the content type as plaintext.
		smcp_outbound_add_option_uint(
			COAP_OPTION_CONTENT_TYPE,
			COAP_CONTENT_TYPE_TEXT_PLAIN
		);

		// Set the content of our response to be "Hello world!".
		smcp_outbound_append_content("Hello world!", SMCP_CSTR_LEN);

		// Send the response we hae created, passing the return value
		// to our caller.
		return smcp_outbound_send();
	}

	return SMCP_STATUS_NOT_FOUND;
}
smcp_status_t
smcp_variable_node_request_handler(
	smcp_variable_node_t		node
) {
	// TODO: Make this function use less stack space!

	smcp_method_t method = smcp_inbound_get_code();
	smcp_status_t ret = SMCP_STATUS_NOT_FOUND;
	SMCP_NON_RECURSIVE coap_content_type_t content_type;
	SMCP_NON_RECURSIVE char* content_ptr;
	SMCP_NON_RECURSIVE size_t content_len;
	SMCP_NON_RECURSIVE char buffer[SMCP_VARIABLE_MAX_VALUE_LENGTH+1];
	SMCP_NON_RECURSIVE coap_content_type_t reply_content_type;
	uint8_t key_index = BAD_KEY_INDEX;
	size_t value_len;
	bool needs_prefix = true;
	char* prefix_name = "";

	content_type = smcp_inbound_get_content_type();
	content_ptr = (char*)smcp_inbound_get_content_ptr();
	content_len = smcp_inbound_get_content_len();

	reply_content_type = SMCP_CONTENT_TYPE_APPLICATION_FORM_URLENCODED;

	require(node, bail);

	// Look up the key index.
	if(smcp_inbound_peek_option(NULL,&value_len)==COAP_OPTION_URI_PATH) {
		if(!value_len) {
			needs_prefix = false;
			smcp_inbound_next_option(NULL,NULL);
		} else for(key_index=0;key_index<BAD_KEY_INDEX;key_index++) {
			ret = node->func(node,SMCP_VAR_GET_KEY,key_index,buffer);
			require_action(ret==0,bail,ret=SMCP_STATUS_NOT_FOUND);
			if(smcp_inbound_option_strequal(COAP_OPTION_URI_PATH, buffer)) {
				smcp_inbound_next_option(NULL,NULL);
				break;
			}
		}
	}

	{
		coap_option_key_t key;
		const uint8_t* value;
		while((key=smcp_inbound_next_option(&value, &value_len))!=COAP_OPTION_INVALID) {
			require_action(key!=COAP_OPTION_URI_PATH,bail,ret=SMCP_STATUS_NOT_FOUND);
			if(key==COAP_OPTION_URI_QUERY) {
				if(	method == COAP_METHOD_POST
					&& value_len>=2
					&& strhasprefix_const((const char*)value,"v=")
				) {
					DEBUG_PRINTF("variable-node: value is in the query.");
					content_type = COAP_CONTENT_TYPE_TEXT_PLAIN;
					content_ptr = (char*)value+2;
					content_len = value_len-2;
				}
//			} else if(key==COAP_OPTION_ETAG) {
//			} else if(key==COAP_OPTION_IF_MATCH) {
//			} else if(key==COAP_OPTION_IF_NONE_MATCH) {
			} else if(key==COAP_OPTION_ACCEPT) {
				reply_content_type = 0;
				if(value_len==1)
					reply_content_type = value[0];
				else {
					// Unsupported type.
				}
			} else if(COAP_OPTION_IS_CRITICAL(key)) {
				ret=SMCP_STATUS_BAD_OPTION;
				assert_printf("Unrecognized option %d, \"%s\"",
					key,
					coap_option_key_to_cstr(key, false)
				);
				goto bail;
			}
		}
	}

	// TODO: Implement me!
	if(method == COAP_METHOD_PUT)
		method = COAP_METHOD_POST;

	if(method == COAP_METHOD_POST) {
		require_action(!smcp_inbound_is_dupe(),bail,ret=0);

		require_action(
			key_index!=BAD_KEY_INDEX,
			bail,
			ret=SMCP_STATUS_NOT_ALLOWED
		);
		if(content_type==SMCP_CONTENT_TYPE_APPLICATION_FORM_URLENCODED) {
			char* key = NULL;
			char* value = NULL;
			content_len = 0;
			while(
				url_form_next_value(
					(char**)&content_ptr,
					&key,
					&value
				)
				&& key
				&& value
			) {
				if(strequal_const(key, "v")) {
					content_ptr = value;
					content_len = strlen(value);
					break;
				}
			}
		}

		// Make sure our content is zero terminated.
		((char*)content_ptr)[content_len] = 0;

		ret = node->func(node,SMCP_VAR_SET_VALUE,key_index,(char*)content_ptr);
		require_noerr(ret,bail);

		ret = smcp_outbound_begin_response(COAP_RESULT_204_CHANGED);
		require_noerr(ret,bail);

		ret = smcp_outbound_send();
		require_noerr(ret,bail);
	} else if(method == COAP_METHOD_GET) {

		if(key_index==BAD_KEY_INDEX) {
			char* content_end_ptr;

			ret = smcp_outbound_begin_response(COAP_RESULT_205_CONTENT);
			require_noerr(ret,bail);

			smcp_outbound_add_option_uint(COAP_OPTION_CONTENT_TYPE, COAP_CONTENT_TYPE_APPLICATION_LINK_FORMAT);

			ret = smcp_observable_update(&node->observable, SMCP_OBSERVABLE_BROADCAST_KEY);
			check_string(ret==0,smcp_status_to_cstr(ret));

			content_ptr = smcp_outbound_get_content_ptr(&content_len);
			content_end_ptr = content_ptr+content_len;

			for(key_index=0;key_index<BAD_KEY_INDEX;key_index++) {
				ret = node->func(node,SMCP_VAR_GET_KEY,key_index,buffer);
				if(ret) break;

				if(content_ptr+2>=content_end_ptr) {
					// No more room for content.
					break;
				}

				*content_ptr++ = '<';
				if(needs_prefix) {
					content_ptr += url_encode_cstr(content_ptr, prefix_name, (content_end_ptr-content_ptr)-1);
					content_ptr = stpncpy(content_ptr,"/",MIN(1,(content_end_ptr-content_ptr)-1));
				}
				content_ptr += url_encode_cstr(content_ptr, buffer, (content_end_ptr-content_ptr)-1);
				*content_ptr++ = '>';

				ret = node->func(node,SMCP_VAR_GET_VALUE,key_index,buffer);

				if(content_ptr+4>=content_end_ptr) {
					// No more room for content.
					break;
				}

				if(!ret) {
					strcpy(content_ptr,";v=");
					content_ptr += 3;
					content_ptr += quoted_cstr(content_ptr, buffer, (content_end_ptr-content_ptr)-1);
				}

				ret = node->func(node,SMCP_VAR_GET_LF_TITLE,key_index,buffer);

				if(content_ptr+8>=content_end_ptr) {
					// No more room for content.
					break;
				}

				if(!ret) {
					strcpy(content_ptr,";title=");
					content_ptr += 7;
					content_ptr += quoted_cstr(content_ptr, buffer, (content_end_ptr-content_ptr)-1);
				}

				// Observation flag
				if(0==node->func(node,SMCP_VAR_GET_OBSERVABLE,key_index,NULL)) {
					content_ptr = stpncpy(content_ptr,";obs",MIN(4,(content_end_ptr-content_ptr)-1));
				}

				*content_ptr++ = ',';
			}
			ret = smcp_outbound_set_content_len(content_len-(content_end_ptr-content_ptr));
			require_noerr(ret,bail);

			ret = smcp_outbound_send();
		} else {
			size_t replyContentLength = 0;
			char *replyContent;

			ret = smcp_outbound_begin_response(COAP_RESULT_205_CONTENT);
			require_noerr(ret,bail);

			if(0==node->func(node,SMCP_VAR_GET_OBSERVABLE,key_index,buffer)) {
				ret = smcp_observable_update(&node->observable, key_index);
				check_string(ret==0,smcp_status_to_cstr(ret));
			}

			if(reply_content_type == SMCP_CONTENT_TYPE_APPLICATION_FORM_URLENCODED) {
				uint32_t etag;

				if(0==node->func(node,SMCP_VAR_GET_MAX_AGE,key_index,buffer)) {
#if HAVE_STRTOL
					uint32_t max_age = strtol(buffer,NULL,0)&0xFFFFFF;
#else
					uint32_t max_age = atoi(buffer)&0xFFFFFF;
#endif
					smcp_outbound_add_option_uint(COAP_OPTION_MAX_AGE, max_age);
				}

				ret = node->func(node,SMCP_VAR_GET_VALUE,key_index,buffer);
				require_noerr(ret,bail);

				fasthash_start(0);
				fasthash_feed((const uint8_t*)buffer,strlen(buffer));
				etag = fasthash_finish_uint32();

				smcp_outbound_add_option_uint(COAP_OPTION_CONTENT_TYPE, SMCP_CONTENT_TYPE_APPLICATION_FORM_URLENCODED);

				smcp_outbound_add_option_uint(COAP_OPTION_ETAG, etag);

				replyContent = smcp_outbound_get_content_ptr(&replyContentLength);

				*replyContent++ = 'v';
				*replyContent++ = '=';
				replyContentLength -= 2;
				replyContentLength = url_encode_cstr(
					replyContent,
					buffer,
					replyContentLength
				);
				ret = smcp_outbound_set_content_len(replyContentLength+2);
			} else {
				ret = node->func(node,SMCP_VAR_GET_VALUE,key_index,buffer);
				require_noerr(ret,bail);

				ret = smcp_outbound_append_content(buffer, SMCP_CSTR_LEN);
			}

			require_noerr(ret,bail);

			ret = smcp_outbound_send();
		}
	} else {
		ret = smcp_default_request_handler(
			(void*)node
		);
		check_string(ret == SMCP_STATUS_OK, smcp_status_to_cstr(ret));
	}

bail:
	check_string(ret == SMCP_STATUS_OK, smcp_status_to_cstr(ret));
	return ret;
}
Exemple #10
0
static smcp_status_t
request_handler_new_group_(
	smcp_group_mgr_t self
) {
	smcp_status_t ret = SMCP_STATUS_OK;
	coap_content_type_t content_type;
	coap_size_t content_len;
	char* fqdn = NULL;
	char* addr_cstr = NULL;
	smcp_sockaddr_t saddr = { };
	char* content_ptr = NULL;
	char* key = NULL;
	char* value = NULL;
	smcp_group_t group = NULL;
	char name_str[3] = { };

	content_type = smcp_inbound_get_content_type();
	content_ptr = (char*)smcp_inbound_get_content_ptr();
	content_len = smcp_inbound_get_content_len();

	switch (content_type) {
	case COAP_CONTENT_TYPE_UNKNOWN:
	case COAP_CONTENT_TYPE_TEXT_PLAIN:
	case SMCP_CONTENT_TYPE_APPLICATION_FORM_URLENCODED:
		content_len = 0;
		while(
			url_form_next_value(
				(char**)&content_ptr,
				&key,
				&value
			)
			&& key
			&& value
		) {

			if (strequal_const(key, names[I_FQDN])) {
				fqdn = value;
				DEBUG_PRINTF("new_group: fqdn=\"%s\"", value);
			} else if (strequal_const(key, names[I_ADDR])) {
				addr_cstr = value;
				DEBUG_PRINTF("new_group: addr=\"%s\"", value);
			}
		}
		break;

	default:
		return smcp_outbound_quick_response(HTTP_RESULT_CODE_UNSUPPORTED_MEDIA_TYPE, NULL);
		break;
	}

	if (NULL == addr_cstr) {
		require_action(NULL != fqdn, bail, ret = SMCP_STATUS_INVALID_ARGUMENT);
		addr_cstr = fqdn;
	} else if (NULL == fqdn) {
		fqdn = addr_cstr;
	}

	smcp_plat_lookup_hostname(addr_cstr, &saddr, SMCP_LOOKUP_HOSTNAME_FLAG_DEFAULT);

	group = smcp_group_mgr_new_group(
		self,
		fqdn,
		&saddr.smcp_addr,
		0
	);

	if (!group) {
		return smcp_outbound_quick_response(COAP_RESULT_400_BAD_REQUEST, "(400) Failed to create group");
	}

	// Default to enabled.
	smcp_group_set_enabled(group, true);

	name_str[0] = get_hex_char(group->index >> 4);
	name_str[1] = get_hex_char(group->index >> 0);

	smcp_outbound_begin_response(COAP_RESULT_201_CREATED);
	smcp_outbound_add_option(COAP_OPTION_LOCATION_PATH, name_str, SMCP_CSTR_LEN);
	smcp_outbound_append_content(name_str, SMCP_CSTR_LEN);
	smcp_outbound_send();

bail:
	return ret;
}