From: Sachin Agrawal Date: Mon, 2 Feb 2015 22:50:35 +0000 (-0800) Subject: Code cleanup in Security sample apps X-Git-Tag: 1.2.0+RC1~1855^2~194 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=46c13d97f22bd357d04b03672a88269b72aa28b4;p=platform%2Fupstream%2Fiotivity.git Code cleanup in Security sample apps Removed CA_INT specific code. Updated OCInit calls to remove IP address and port number. Change-Id: Iba506b67e33060ea32b2430b443443305bfa0243 Signed-off-by: Sachin Agrawal Reviewed-on: https://gerrit.iotivity.org/gerrit/264 Tested-by: jenkins-iotivity Reviewed-by: Sashi Penta Reviewed-by: Sudarshan Prasad --- diff --git a/resource/csdk/stack/samples/linux/secure/SConscript b/resource/csdk/stack/samples/linux/secure/SConscript index a27c565..d19ea6f 100644 --- a/resource/csdk/stack/samples/linux/secure/SConscript +++ b/resource/csdk/stack/samples/linux/secure/SConscript @@ -42,7 +42,7 @@ if target_os == 'android': if target_os in ['darwin', 'ios']: samples_env.AppendUnique(CPPDEFINES = ['_DARWIN_C_SOURCE']) -samples_env.AppendUnique(CPPDEFINES = ['CA_INT_DTLS', 'TB_LOG']) +samples_env.AppendUnique(CPPDEFINES = ['TB_LOG']) ###################################################################### # Source files and Targets diff --git a/resource/csdk/stack/samples/linux/secure/common.cpp b/resource/csdk/stack/samples/linux/secure/common.cpp index 55fb32a..16dd8a8 100644 --- a/resource/csdk/stack/samples/linux/secure/common.cpp +++ b/resource/csdk/stack/samples/linux/secure/common.cpp @@ -115,3 +115,16 @@ const char *getResult(OCStackResult result) { return "UNKNOWN"; } } + +void StripNewLineChar(char* str) { + int i = 0; + if (str) + { + while( str[i]) + { + if (str[i] == '\n') + str[i] = '\0'; + i++; + } + } +} diff --git a/resource/csdk/stack/samples/linux/secure/common.h b/resource/csdk/stack/samples/linux/secure/common.h index 408ba59..9eba8d3 100644 --- a/resource/csdk/stack/samples/linux/secure/common.h +++ b/resource/csdk/stack/samples/linux/secure/common.h @@ -27,5 +27,8 @@ const char *getResult(OCStackResult result); /* Read the credentials from persistent storage and provide to OC stack. */ OCStackResult SetCredentials(const char* filename); +/* Removes the new line character from a NULL terminated C string. */ +void StripNewLineChar(char* str); + #endif //OCSAMPLE_COMMON_H_ diff --git a/resource/csdk/stack/samples/linux/secure/occlientbasicops.cpp b/resource/csdk/stack/samples/linux/secure/occlientbasicops.cpp index bcf20f6..49b5582 100644 --- a/resource/csdk/stack/samples/linux/secure/occlientbasicops.cpp +++ b/resource/csdk/stack/samples/linux/secure/occlientbasicops.cpp @@ -35,16 +35,16 @@ static int UNICAST_DISCOVERY = 0; static int TEST_CASE = 0; -#ifdef CA_INT_DTLS static int IPV4_ADDR_SIZE = 16; -#else -static const char * TEST_APP_UNICAST_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core"; -#endif +static char UNICAST_DISCOVERY_QUERY[] = "coap://%s:5298/oc/core"; +static char MULTICAST_DISCOVERY_QUERY[] = "/oc/core"; + static std::string putPayload = "{\"state\":\"off\",\"power\":10}"; static std::string coapServerIP; static std::string coapServerPort; static std::string coapServerResource; static int coapSecureResource; +static OCConnectivityType ocConnType; //File containing Client's Identity and the PSK credentials //of other devices which the client trusts @@ -87,8 +87,7 @@ OCStackResult InvokeOCDoResource(std::ostringstream &query, ret = OCDoResource(&handle, method, query.str().c_str(), 0, (method == OC_REST_PUT || method == OC_REST_POST) ? putPayload.c_str() : NULL, - (OC_WIFI), - qos, &cbData, options, numOptions); + ocConnType, qos, &cbData, options, numOptions); if (ret != OC_STACK_OK) { @@ -159,6 +158,8 @@ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu); + ocConnType = clientResponse->connType; + if (parseClientResponse(clientResponse) != -1) { switch(TEST_CASE) @@ -239,31 +240,47 @@ int InitDiscovery() OCStackResult ret; OCCallbackData cbData; OCDoHandle handle; - /* Start a discovery query*/ char szQueryUri[MAX_URI_LENGTH] = { 0 }; + OCConnectivityType discoveryReqConnType; if (UNICAST_DISCOVERY) { -#ifdef CA_INT_DTLS char ipv4addr[IPV4_ADDR_SIZE]; - printf("Enter IPv4 address of the Server hosting secure resource (Ex: 11.12.13.14)\n"); - fgets(ipv4addr, IPV4_ADDR_SIZE, stdin); - snprintf(szQueryUri, sizeof(szQueryUri), "coap://%s:5683/oc/core", ipv4addr); -#else - strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY); -#endif + if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin)) + { + //Strip newline char from ipv4addr + StripNewLineChar(ipv4addr); + snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr); + } + else + { + OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!"); + return OC_STACK_INVALID_PARAM; + } + + printf("Select Connectivity type on which discovery request needs to be send : "); + printf("0:ETH, 1:WIFI\n"); + discoveryReqConnType = ((getchar() - '0') == 0) ? OC_ETHERNET : OC_WIFI; } else { - strcpy(szQueryUri, OC_WELL_KNOWN_QUERY); + //Send discovery request on Wifi and Ethernet interface + discoveryReqConnType = OC_ALL; + strcpy(szQueryUri, MULTICAST_DISCOVERY_QUERY); } + cbData.cb = discoveryReqCB; cbData.context = NULL; cbData.cd = NULL; + + /* Start a discovery query*/ + OC_LOG_V(INFO, TAG, "Initiating %s Resource Discovery : %s\n", + (UNICAST_DISCOVERY) ? "Unicast" : "Multicast", + szQueryUri); + ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, - (OC_WIFI), - OC_LOW_QOS, &cbData, NULL, 0); + discoveryReqConnType, OC_LOW_QOS, &cbData, NULL, 0); if (ret != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack resource error"); @@ -273,10 +290,6 @@ int InitDiscovery() int main(int argc, char* argv[]) { - uint8_t addr[20] = {0}; - uint8_t* paddr = NULL; - uint16_t port = USE_RANDOM_PORT; - uint8_t ifname[] = "eth0"; int opt; struct timespec timeout; @@ -303,18 +316,8 @@ int main(int argc, char* argv[]) return -1; } - - /*Get Ip address on defined interface and initialize coap on it with random port number - * this port number will be used as a source port in all coap communications*/ - if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, - sizeof(addr)) == ERR_SUCCESS) - { - OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr); - paddr = addr; - } - /* Initialize OCStack*/ - if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) + if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack init error"); return 0; diff --git a/resource/csdk/stack/samples/linux/secure/ocserverbasicops.cpp b/resource/csdk/stack/samples/linux/secure/ocserverbasicops.cpp index b6d579c..acaa52b 100644 --- a/resource/csdk/stack/samples/linux/secure/ocserverbasicops.cpp +++ b/resource/csdk/stack/samples/linux/secure/ocserverbasicops.cpp @@ -41,8 +41,6 @@ static LEDResource gLedInstance[SAMPLE_MAX_NUM_POST_INSTANCE]; char *gResourceUri= (char *)"/a/led"; -static uint16_t OC_WELL_KNOWN_PORT = 5683; - //File containing Server's Identity and the PSK credentials //of other devices which the server trusts //This can be generated using 'gen_sec_bin' application @@ -297,23 +295,11 @@ void handleSigInt(int signum) int main(int argc, char* argv[]) { - uint8_t addr[20] = {0}; - uint8_t* paddr = NULL; - uint16_t port = OC_WELL_KNOWN_PORT; - uint8_t ifname[] = "eth0"; struct timespec timeout; OC_LOG(DEBUG, TAG, "OCServer is starting..."); - /*Get Ip address on defined interface and initialize coap on it with random port number - * this port number will be used as a source port in all coap communications*/ - if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, - sizeof(addr)) == ERR_SUCCESS) - { - OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port); - paddr = addr; - } - if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK) + if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack init error"); return 0; diff --git a/resource/csdk/stack/src/ocstack.c b/resource/csdk/stack/src/ocstack.c index 81c3e52..1ca8301 100644 --- a/resource/csdk/stack/src/ocstack.c +++ b/resource/csdk/stack/src/ocstack.c @@ -35,6 +35,7 @@ #include "occoap.h" #include "ocmalloc.h" #include "ocserverrequest.h" +#include "ocsecurityinternal.h" #include "cacommon.h" #include "cainterface.h"