void get_resource_uri(char *URI, char *resourceURI, int length);
int get_secure_information(CAPayload_t payLoad);
int get_address_set(const char *uri, addressSet_t* outAddress);
-void parse_coap_uri(const char* uri, addressSet_t* address);
+void parse_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags);
static CAToken_t g_last_request_token = NULL;
static const char SECURE_COAPS_PREFIX[] = "coaps://";
// create remote endpoint
CAEndpoint_t *endpoint = NULL;
+ CATransportFlags_t flags;
printf("URI : %s\n", uri);
addressSet_t address = {};
- parse_coap_uri(uri, &address);
+ parse_coap_uri(uri, &address, &flags);
- res = CACreateEndpoint(CA_DEFAULT_FLAGS, g_selected_nw_type,
+ res = CACreateEndpoint(flags, g_selected_nw_type,
(const char*)address.ipAddress, address.port, &endpoint);
if (CA_STATUS_OK != res || !endpoint)
{
return;
}
+ CATransportFlags_t flags;
addressSet_t address = {};
- parse_coap_uri(uri, &address);
+ parse_coap_uri(uri, &address, &flags);
// create remote endpoint
CAEndpoint_t *endpoint = NULL;
- res = CACreateEndpoint(0, g_selected_nw_type, address.ipAddress, address.port, &endpoint);
+ res = CACreateEndpoint(flags, g_selected_nw_type, address.ipAddress, address.port, &endpoint);
if (CA_STATUS_OK != res)
{
printf("Create remote endpoint error, error code: %d\n", res);
return -1;
}
- char portStr[4] = {0};
+ char portStr[6] = {0};
memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
printf("secured port is: %s\n", portStr);
}
-void parse_coap_uri(const char* uri, addressSet_t* address)
+void parse_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags)
{
if (NULL == uri)
{
{
printf("uri has '%s' prefix\n", COAPS_PREFIX);
startIndex = COAPS_PREFIX_LEN;
+ *flags = CA_SECURE;
}
else if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
{
printf("uri has '%s' prefix\n", COAP_PREFIX);
startIndex = COAP_PREFIX_LEN;
+ *flags = CA_DEFAULT_FLAGS;
}
// #2. copy uri for parse
OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
}
-static CAResult_t CACreateSocket(int *socketFD, const char *localIp, uint16_t *port)
+static CAResult_t CACreateSocket(int *socketFD, const char *localIp, uint16_t *port, bool isSecured)
{
VERIFY_NON_NULL(socketFD, IP_SERVER_TAG, "socketFD is NULL");
VERIFY_NON_NULL(localIp, IP_SERVER_TAG, "localIp is NULL");
return CA_STATUS_FAILED;
}
- if (0 != *port)
+ if (0 != *port && !isSecured)
{
int setOptionOn = SOCKETOPTION;
if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &setOptionOn,
sockAddr.sin_addr.s_addr = inet_addr(localIp);
}
+ bool isBound = false;
if (-1 != bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)))
{
+ isBound = true;
+ }
+ else if (isSecured)
+ {
+ //if secure port 5684 is occupied, trying for another port
+ serverPort = 0;
+ sockAddr.sin_port = htons(serverPort);
+ if (-1 != bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)))
+ {
+ isBound = true;
+ }
+ }
+
+ if (true == isBound)
+ {
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
- if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1)
+ if (-1 == getsockname(sock, (struct sockaddr *)&sin, &len))
{
OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to get socket[%s]!",
strerror(errno));
}
else
{
- OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]!",
- strerror(errno));
+ OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]!", strerror(errno));
close(sock);
return CA_STATUS_FAILED;
}
VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");
- CAResult_t ret = CACreateSocket(serverFD, localAddress, port);
+ CAResult_t ret = CACreateSocket(serverFD, localAddress, port, isSecured);
if (CA_STATUS_OK != ret)
{
OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create unicast socket");
if (!isMulticastServerStarted)
{
int mulicastServerFd = -1;
- CAResult_t ret = CACreateSocket(&mulicastServerFd, multicastAddress, &port);
+ CAResult_t ret = CACreateSocket(&mulicastServerFd, multicastAddress, &port, false);
if (ret != CA_STATUS_OK)
{
OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create multicast socket");