#include <boost/lexical_cast.hpp>
#include <sstream>
+#if !defined(_WIN32)
+#include <arpa/inet.h>
+#else
+#include <ws2tcpip.h>
+#include <in6addr.h>
+#endif
namespace OC {
static const char COAP[] = "coap://";
static const char COAPS[] = "coaps://";
+static const char COAP_TCP[] = "coap+tcp://";
+
using OC::nil_guard;
using OC::result_guard;
using OC::checked_guard;
const std::vector<std::string>& interfaces)
: m_clientWrapper(clientWrapper), m_uri(uri),
m_resourceId(serverId, m_uri),
- m_devAddr{ OC_DEFAULT_ADAPTER },
+ m_devAddr{ OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0
+#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
+ , {0}
+#endif
+ },
m_isObservable(observable), m_isCollection(false),
m_resourceTypes(resourceTypes), m_interfaces(interfaces),
m_observeHandle(nullptr)
interfaces.empty(), m_clientWrapper.expired(), false, false);
}
+ if (uri.length() == 1 && uri[0] == '/')
+ {
+ throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
+ interfaces.empty(), m_clientWrapper.expired(), false, false);
+ }
+
+ if (uri[0] != '/')
+ {
+ throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
+ interfaces.empty(), m_clientWrapper.expired(), false, false);
+ }
+
// construct the devAddr from the pieces we have
m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
- size_t len = host.length();
- if (len >= MAX_ADDR_STR_SIZE)
- {
- throw std::length_error("host address is too long.");
- }
this->setHost(host);
}
prefix_len = sizeof(COAPS) - 1;
m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
}
+ else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0)
+ {
+ prefix_len = sizeof(COAP_TCP) - 1;
+ }
else
{
throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
m_interfaces.empty(), m_clientWrapper.expired(), false, false);
}
- // removed coap:// or coaps://
+ // remove 'coap://' or 'coaps://' or 'coap+tcp://'
std::string host_token = host.substr(prefix_len);
- if(host_token[0] == '[')
+ if(host_token[0] == '[') // IPv6
{
- m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
+ size_t bracket = host_token.find(']');
- size_t found = host_token.find(']');
+ if(bracket == std::string::npos || bracket == 0)
+ {
+ throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
+ m_interfaces.empty(), m_clientWrapper.expired(), false, false);
+ }
+ // extract the ipv6 address
+ std::string ip6Addr = host_token.substr(1, bracket - 1);
- if(found == std::string::npos || found == 0)
+ // address validity check
+ struct in6_addr buf;
+ const char *cAddr = ip6Addr.c_str();
+ if(0 == inet_pton(AF_INET6, cAddr, &buf))
{
throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
m_interfaces.empty(), m_clientWrapper.expired(), false, false);
}
- // extract the ipaddress
- std::string ip6Addr = host_token.substr(1, found-1);
- ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
- m_devAddr.addr[ip6Addr.length()] = '\0';
+
//skip ']' and ':' characters in host string
- host_token = host_token.substr(found + 2);
- }
- else
- {
- size_t found = host_token.find(':');
+ host_token = host_token.substr(bracket + 2);
+ int port = std::stoi(host_token);
- if(found == std::string::npos || found == 0)
+ if (0 > port || UINT16_MAX < port)
{
throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
m_interfaces.empty(), m_clientWrapper.expired(), false, false);
}
- std::string addrPart = host_token.substr(0, found);
- addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
- m_devAddr.addr[addrPart.length()] = '\0';
- //skip ':' character in host string
- host_token = host_token.substr(found + 1);
+ ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
+ m_devAddr.addr[ip6Addr.length()] = '\0';
+ m_devAddr.port = static_cast<uint16_t>(port);
+ m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
}
-
- int port = std::stoi(host_token);
-
- if( port < 0 || port > UINT16_MAX )
+ else if (host_token[0] == ':')
{
throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
m_interfaces.empty(), m_clientWrapper.expired(), false, false);
}
-
- m_devAddr.port = static_cast<uint16_t>(port);
-
+ else
+ {
+ size_t dot = host_token.find('.');
+ if (std::string::npos == dot) // MAC
+ {
+ std::string macAddr = host_token;
+
+ // address validity check
+ if (MAC_ADDR_STR_SIZE != macAddr.length())
+ {
+ throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
+ m_interfaces.empty(), m_clientWrapper.expired(), false, false);
+ }
+
+ for (size_t blockCnt = 0; blockCnt < MAC_ADDR_BLOCKS; blockCnt++)
+ {
+ std::string block = macAddr.substr(blockCnt * 3, 2);
+
+ if (std::string::npos != block.find_first_not_of("0123456789ABCDEFabcdef"))
+ {
+ throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
+ m_interfaces.empty(), m_clientWrapper.expired(), false, false);
+ }
+
+ if (MAC_ADDR_BLOCKS - 1 > blockCnt)
+ {
+ char delimiter = macAddr[blockCnt * 3 + 2];
+
+ if (':' != delimiter)
+ {
+ throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
+ m_interfaces.empty(), m_clientWrapper.expired(), false, false);
+ }
+ }
+ }
+
+ macAddr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
+ m_devAddr.addr[MAC_ADDR_STR_SIZE] = '\0';
+ }
+ else // IPv4
+ {
+ size_t colon = host_token.find(':');
+
+ if (colon == std::string::npos || colon == 0)
+ {
+ throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
+ m_interfaces.empty(), m_clientWrapper.expired(), false, false);
+ }
+
+ // extract the ipv4 address
+ std::string ip4Addr = host_token.substr(0, colon);
+
+ // address validity check
+ struct in_addr buf;
+ const char *cAddr = ip4Addr.c_str();
+ if(0 == inet_pton(AF_INET, cAddr, &buf))
+ {
+ throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
+ m_interfaces.empty(), m_clientWrapper.expired(), false, false);
+ }
+
+ //skip ':' characters in host string
+ host_token = host_token.substr(colon + 1);
+ int port = std::stoi(host_token);
+
+ if (0 > port || UINT16_MAX < port)
+ {
+ throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
+ m_interfaces.empty(), m_clientWrapper.expired(), false, false);
+ }
+
+ ip4Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
+ m_devAddr.addr[ip4Addr.length()] = '\0';
+ m_devAddr.port = static_cast<uint16_t>(port);
+ }
+ }
}
OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
return result;
}
+void OCResource::setHeaderOptions(const HeaderOptions& headerOptions)
+{
+ m_headerOptions = headerOptions;
+}
+
+void OCResource::unsetHeaderOptions()
+{
+ m_headerOptions.clear();
+}
+
std::string OCResource::host() const
{
std::ostringstream ss;
{
ss << COAPS;
}
+ else if ((m_devAddr.adapter & OC_ADAPTER_TCP)
+ || (m_devAddr.adapter & OC_ADAPTER_GATT_BTLE)
+ || (m_devAddr.adapter & OC_ADAPTER_RFCOMM_BTEDR))
+ {
+ ss << COAP_TCP;
+ }
else
{
ss << COAP;
return m_isObservable;
}
+std::vector<std::string> OCResource::getResourceTypes() const
+{
+ return m_resourceTypes;
+}
+
+std::vector<std::string> OCResource::getResourceInterfaces(void) const
+{
+ return m_interfaces;
+}
+
OCResourceIdentifier OCResource::uniqueIdentifier() const
{
return m_resourceId;
}
} // namespace OC
-