Commit b2fe726c authored by Kishen Maloor's avatar Kishen Maloor

Update Android port

* Fix/update TCP and IP adapters to achieve functional equivalence
  to the Linux adapters.
* Add missing PKI and Cloud related code components to the Makefile.

Change-Id: Ib0d8b823014a42321b3d67dbea9a9315ea16bd8f
Signed-off-by: Kishen Maloor's avatarKishen Maloor <kishen.maloor@intel.com>
parent 663298b8
......@@ -92,16 +92,21 @@ DTLS= \
xtea.c pkcs11.c x509.c x509_crt.c debug.c net_sockets.c \
ssl_cache.c ssl_ciphersuites.c ssl_cli.c ssl_cookie.c \
ssl_srv.c ssl_ticket.c ssl_tls.c rsa_internal.c \
x509write_csr.c x509write_crt.c x509_create.c
x509write_csr.c x509write_crt.c x509_create.c x509_csr.c
DTLSFLAGS=-I../../deps/mbedtls/include -D__OC_RANDOM
CBOR=../../deps/tinycbor/src/cborencoder.c ../../deps/tinycbor/src/cborencoder_close_container_checked.c ../../deps/tinycbor/src/cborparser.c# ../../deps/tinycbor/src/cbortojson.c ../../deps/tinycbor/src/cborpretty.c ../../deps/tinycbor/src/cborparser_dup_string.c
CTIMESTAMP=../../api/c-timestamp/timestamp_format.c ../../api/c-timestamp/timestamp_valid.c ../../api/c-timestamp/timestamp_parse.c
ifeq ($(PKI),1)
CTIMESTAMP=../../api/c-timestamp/timestamp_tm.c
endif
CTIMESTAMP+=../../api/c-timestamp/timestamp_format.c ../../api/c-timestamp/timestamp_valid.c ../../api/c-timestamp/timestamp_parse.c
SRC_COMMON:=$(wildcard ../../util/*.c) ${CBOR} ${CTIMESTAMP}
SRC:=$(wildcard ../../messaging/coap/*.c ../../api/*.c ../../port/android/*.c)
SRC_CLOUD=$(wildcard ../../api/cloud/*.c)
# From API level 24 on Android supports getifaddrs itself.
ifeq ($(shell if [ ${ANDROID_API} -gt 23 ]; then echo gt; fi),gt)
......@@ -117,10 +122,12 @@ HEADERS_UTIL_PT = $(wildcard ../../util/pt/*.h)
HEADERS_PORT = $(wildcard ../../port/*.h)
HEADERS_TINYCBOR = $(wildcard ../../deps/tinycbor/src/*.h)
CFLAGS_CLOUD=-I../../api/cloud
CFLAGS?=-fPIC -fno-asynchronous-unwind-tables -fno-omit-frame-pointer -ffreestanding -Os -fno-stack-protector -ffunction-sections -fdata-sections -fno-strict-overflow -I./ -I../../include/ -I../../ -std=gnu99 -Wall -DLONG_BIT=64 -D__ANDROID_API__=${ANDROID_API}
OBJ_COMMON=$(addprefix ${OBJDIR}/,$(notdir $(SRC_COMMON:.c=.o)))
OBJ_CLIENT=$(addprefix ${OBJDIR}/client/,$(notdir $(SRC:.c=.o)))
OBJ_SERVER=$(addprefix ${OBJDIR}/server/,$(filter-out oc_obt.o oc_obt_otm_justworks.o oc_obt_otm_randompin.o,$(notdir $(SRC:.c=.o))))
OBJ_SERVER=$(addprefix ${OBJDIR}/server/,$(filter-out oc_obt.o oc_obt_otm_justworks.o oc_obt_otm_randompin.o oc_obt_otm_cert.o oc_obt_certs.o,$(notdir $(SRC:.c=.o))))
OBJ_CLOUD=$(addprefix ${OBJDIR}/cloud/,$(notdir $(SRC_CLOUD:.c=.o)))
OBJ_CLIENT_SERVER=$(addprefix ${OBJDIR}/client_server/,$(notdir $(SRC:.c=.o)))
VPATH=../../messaging/coap/:../../util/:../../api/:../../deps/tinycbor/src/:../../deps/mbedtls/library:../../api/c-timestamp:
LIBS?= -lm -llog
......@@ -129,6 +136,13 @@ SAMPLES := server client temp_sensor simpleserver simpleclient client_collection
server_collections_linux server_block_linux client_block_linux smart_home_server_linux multi_device_server multi_device_client \
smart_lock server_multithread_linux client_multithread_linux
ifeq ($(CLOUD),1)
CFLAGS += -DOC_CLOUD
TCP=1
IPV4=1
SAMPLES += cloud_linux cloud_tests
endif
OBT = onboarding_tool
ifeq ($(DEBUG),1)
......@@ -154,7 +168,9 @@ ifneq ($(SECURE),0)
ifeq ($(DYNAMIC),1)
SRC += ../../security/oc_obt.c \
../../security/oc_obt_otm_justworks.c \
../../security/oc_obt_otm_randompin.c
../../security/oc_obt_otm_randompin.c \
../../security/oc_obt_otm_cert.c \
../../security/oc_obt_certs.c
SAMPLES += ${OBT}
else
SRC_COMMON += $(MBEDTLS_DIR)/library/memory_buffer_alloc.c
......@@ -168,7 +184,7 @@ ifeq ($(IPV4),1)
endif
ifeq ($(TCP),1)
EXTRA_CFLAGS += -DOC_TCP
CFLAGS += -DOC_TCP
endif
CONSTRAINED_LIBS = libiotivity-constrained-server.a libiotivity-constrained-client.a \
......@@ -196,14 +212,18 @@ ${OBJDIR}/client_server/%.o: %.c
@mkdir -p ${@D}
${CC} -c -o $@ $< ${CFLAGS} -DOC_CLIENT -DOC_SERVER
${OBJDIR}/cloud/%.o: %.c
@mkdir -p ${@D}
${CC} -c -o $@ $< ${CFLAGS} ${CFLAGS_CLOUD}
libiotivity-constrained-server.a: $(OBJ_COMMON) $(OBJ_SERVER)
$(AR) -rcs $@ $(OBJ_COMMON) $(OBJ_SERVER)
libiotivity-constrained-client.a: $(OBJ_COMMON) $(OBJ_CLIENT)
$(AR) -rcs $@ $(OBJ_COMMON) $(OBJ_CLIENT)
libiotivity-constrained-client-server.a: $(OBJ_COMMON) $(OBJ_CLIENT_SERVER)
$(AR) -rcs $@ $(OBJ_COMMON) $(OBJ_CLIENT_SERVER)
libiotivity-constrained-client-server.a: $(OBJ_COMMON) $(OBJ_CLIENT_SERVER) $(OBJ_CLOUD)
$(AR) -rcs $@ $(OBJ_COMMON) $(OBJ_CLIENT_SERVER) $(OBJ_CLOUD)
server: libiotivity-constrained-server.a
${CC} -o $@ ../../apps/server_linux.c libiotivity-constrained-server.a -DOC_SERVER ${CFLAGS} ${LIBS}
......@@ -244,6 +264,12 @@ multi_device_server: libiotivity-constrained-server.a
multi_device_client: libiotivity-constrained-client.a
${CC} -o $@ ../../apps/multi_device_client_linux.c libiotivity-constrained-client.a -DOC_CLIENT ${CFLAGS} ${LIBS}
cloud_linux: libiotivity-constrained-client-server.a $(ROOT_DIR)/apps/cloud_linux.c
${CC} -o $@ ../../apps/cloud_linux.c libiotivity-constrained-client-server.a -DOC_CLIENT -DOC_SERVER ${CFLAGS} ${CFLAGS_CLOUD} ${LIBS}
cloud_tests: libiotivity-constrained-client-server.a $(ROOT_DIR)/apps/cloud_certification_tests.c
${CC} -o $@ ../../apps/cloud_certification_tests.c libiotivity-constrained-client-server.a -DOC_CLIENT -DOC_SERVER ${CFLAGS} ${CFLAGS_CLOUD} ${LIBS}
${OBT}: libiotivity-constrained-client.a
${CC} -o $@ ../../onboarding_tool/obtmain.c libiotivity-constrained-client.a -DOC_CLIENT ${CFLAGS} ${LIBS}
......
/*
// Copyright (c) 2016-2018 Intel Corporation
// Copyright (c) 2016-2019 Intel Corporation
// Copyright (c) 2017-2018 Lynx Technology
//
// Licensed under the Apache License, Version 2.0 (the "License");
......@@ -732,10 +732,14 @@ recv_msg(int sock, uint8_t *recv_buf, int recv_buf_size,
* address of a multicast response.
*/
oc_endpoint_t *dst = oc_connectivity_get_endpoints(endpoint->device);
while (dst->interface_index != endpoint->interface_index ||
!(dst->flags & IPV6)) {
while (dst != NULL &&
(dst->interface_index != endpoint->interface_index ||
!(dst->flags & IPV6))) {
dst = dst->next;
}
if (dst == NULL) {
return -1;
}
memcpy(endpoint->addr_local.ipv6.address, dst->addr.ipv6.address, 16);
}
break;
......@@ -756,10 +760,14 @@ recv_msg(int sock, uint8_t *recv_buf, int recv_buf_size,
memcpy(endpoint->addr_local.ipv4.address, &pktinfo->ipi_addr.s_addr, 4);
} else {
oc_endpoint_t *dst = oc_connectivity_get_endpoints(endpoint->device);
while (dst->interface_index != endpoint->interface_index ||
!(dst->flags & IPV4)) {
while (dst != NULL &&
(dst->interface_index != endpoint->interface_index ||
!(dst->flags & IPV4))) {
dst = dst->next;
}
if (dst == NULL) {
return -1;
}
memcpy(endpoint->addr_local.ipv4.address, dst->addr.ipv4.address, 4);
}
break;
......@@ -929,16 +937,15 @@ network_event_thread(void *data)
oc_tcp_receive_message(dev, &setfds, message);
if (tcp_status == ADAPTER_STATUS_RECEIVE) {
goto common;
} else {
oc_message_unref(message);
continue;
}
#endif /* OC_TCP */
oc_message_unref(message);
continue;
common:
#ifdef OC_DEBUG
PRINT("Incoming message of size %lu bytes from ",
(unsigned long int)message->length);
PRINT("Incoming message of size %zd bytes from ", message->length);
PRINTipaddr(message->endpoint);
PRINT("\n\n");
#endif /* OC_DEBUG */
......@@ -1025,7 +1032,7 @@ send_msg(int sock, struct sockaddr_storage *receiver, oc_message_t *message)
}
bytes_sent += x;
}
OC_DBG("Sent %d bytes", bytes_sent);
OC_DBG("Sent %zd bytes", bytes_sent);
if (bytes_sent == 0) {
return -1;
......@@ -1038,8 +1045,7 @@ int
oc_send_buffer(oc_message_t *message)
{
#ifdef OC_DEBUG
PRINT("Outgoing message of size %lu bytes to ",
(unsigned long int)message->length);
PRINT("Outgoing message of size %zd bytes to ", message->length);
PRINTipaddr(message->endpoint);
PRINT("\n\n");
#endif /* OC_DEBUG */
......@@ -1118,6 +1124,9 @@ oc_send_discovery_request(oc_message_t *message)
ip_context_t *dev = get_ip_context_for_device(message->endpoint.device);
#define IN6_IS_ADDR_MC_REALM_LOCAL(addr) \
IN6_IS_ADDR_MULTICAST(addr) && ((((const uint8_t *)(addr))[1] & 0x0f) == 0x03)
for (interface = ifs; interface != NULL; interface = interface->ifa_next) {
/* Only broadcast on LAN/WLAN. 3G/4G/5G should not have the broadcast
and multicast flags set. */
......@@ -1139,7 +1148,24 @@ oc_send_discovery_request(oc_message_t *message)
goto done;
}
message->endpoint.interface_index = mif;
message->endpoint.addr.ipv6.scope = mif;
if (IN6_IS_ADDR_MC_LINKLOCAL(message->endpoint.addr.ipv6.address)) {
message->endpoint.addr.ipv6.scope = mif;
unsigned int hops = 1;
setsockopt(dev->server_sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops,
sizeof(hops));
} else if (IN6_IS_ADDR_MC_REALM_LOCAL(
message->endpoint.addr.ipv6.address)) {
unsigned int hops = 255;
setsockopt(dev->server_sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops,
sizeof(hops));
message->endpoint.addr.ipv6.scope = 0;
} else if (IN6_IS_ADDR_MC_SITELOCAL(
message->endpoint.addr.ipv6.address)) {
unsigned int hops = 255;
setsockopt(dev->server_sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops,
sizeof(hops));
message->endpoint.addr.ipv6.scope = 0;
}
oc_send_buffer(message);
}
#ifdef OC_IPV4
......@@ -1272,7 +1298,7 @@ handle_session_event_callback(const oc_endpoint_t *endpoint,
static int
connectivity_ipv4_init(ip_context_t *dev)
{
OC_DBG("Initializing IPv4 connectivity for device %d", dev->device);
OC_DBG("Initializing IPv4 connectivity for device %zd", dev->device);
memset(&dev->mcast4, 0, sizeof(struct sockaddr_storage));
memset(&dev->server4, 0, sizeof(struct sockaddr_storage));
......@@ -1381,7 +1407,7 @@ connectivity_ipv4_init(ip_context_t *dev)
dev->dtls4_port = ntohs(sm->sin_port);
#endif /* OC_SECURITY */
OC_DBG("Successfully initialized IPv4 connectivity for device %d",
OC_DBG("Successfully initialized IPv4 connectivity for device %zd",
dev->device);
return 0;
......@@ -1391,7 +1417,7 @@ connectivity_ipv4_init(ip_context_t *dev)
int
oc_connectivity_init(size_t device)
{
OC_DBG("Initializing connectivity for device %d", device);
OC_DBG("Initializing connectivity for device %zd", device);
ip_context_t *dev = (ip_context_t *)oc_memb_alloc(&ip_context_s);
if (!dev) {
......@@ -1580,7 +1606,7 @@ oc_connectivity_init(size_t device)
return -1;
}
OC_DBG("Successfully initialized connectivity for device %d", device);
OC_DBG("Successfully initialized connectivity for device %zd", device);
return 0;
}
......@@ -1623,7 +1649,7 @@ oc_connectivity_shutdown(size_t device)
oc_list_remove(ip_contexts, dev);
oc_memb_free(&ip_context_s, dev);
OC_DBG("oc_connectivity_shutdown for device %d", device);
OC_DBG("oc_connectivity_shutdown for device %zd", device);
}
#ifdef OC_TCP
......@@ -1660,8 +1686,30 @@ oc_dns_lookup(const char *domain, oc_string_t *addr, enum transport_flags flags)
ret = getnameinfo(result->ai_addr, result->ai_addrlen, address,
sizeof(address), NULL, 0, 0);
if (ret == 0) {
OC_DBG("%s address is %s", domain, address);
oc_new_string(addr, address, strlen(address));
char address[INET6_ADDRSTRLEN + 2] = { 0 };
const char *dest = NULL;
if (flags & IPV6) {
struct sockaddr_in6 *s_addr = (struct sockaddr_in6 *)result->ai_addr;
address[0] = '[';
dest = inet_ntop(AF_INET6, (void *)&s_addr->sin6_addr, address + 1,
INET6_ADDRSTRLEN);
size_t addr_len = strlen(address);
address[addr_len] = ']';
address[addr_len + 1] = '\0';
}
#ifdef OC_IPV4
else {
struct sockaddr_in *s_addr = (struct sockaddr_in *)result->ai_addr;
dest = inet_ntop(AF_INET, (void *)&s_addr->sin_addr, address,
INET_ADDRSTRLEN);
}
#endif /* OC_IPV4 */
if (dest) {
OC_DBG("%s address is %s", domain, address);
oc_new_string(addr, address, strlen(address));
} else {
ret = -1;
}
}
}
......
......@@ -19,6 +19,7 @@
#define __USE_GNU
#include "tcpadapter.h"
#include "api/oc_session_events_internal.h"
#include "ipcontext.h"
#include "messaging/coap/coap.h"
#include "oc_endpoint.h"
......@@ -29,11 +30,25 @@
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdlib.h>
#include <unistd.h>
#include <android/api-level.h>
#if !defined(__ANDROID_API__) || __ANDROID_API__ == 10000
#error __ANDROID_API__ not defined
#endif
#include <arpa/inet.h>
#if __ANDROID_API__ >= 24
#include <ifaddrs.h>
#define OC_GETIFADDRS getifaddrs
#define OC_FREEIFADDRS freeifaddrs
#else
#include "ifaddrs-android.h"
#define OC_GETIFADDRS android_getifaddrs
#define OC_FREEIFADDRS android_freeifaddrs
#endif /* __ANDROID_API__ >= 24 */
#ifdef OC_TCP
#define OC_TCP_LISTEN_BACKLOG 3
......@@ -105,7 +120,7 @@ get_interface_index(int sock)
}
struct ifaddrs *ifs = NULL, *interface = NULL;
if (getifaddrs(&ifs) < 0) {
if (OC_GETIFADDRS(&ifs) < 0) {
OC_ERR("querying interfaces: %d", errno);
return -1;
}
......@@ -135,7 +150,7 @@ get_interface_index(int sock)
}
}
freeifaddrs(ifs);
OC_FREEIFADDRS(ifs);
return interface_index;
}
......@@ -159,7 +174,11 @@ oc_tcp_add_socks_to_fd_set(ip_context_t *dev)
static void
free_tcp_session(tcp_session_t *session)
{
oc_session_end_event(&session->endpoint);
oc_list_remove(session_list, session);
if (!oc_session_events_is_ongoing()) {
oc_session_end_event(&session->endpoint);
}
FD_CLR(session->sock, &session->dev->rfds);
......@@ -171,7 +190,6 @@ free_tcp_session(tcp_session_t *session)
close(session->sock);
oc_list_remove(session_list, session);
oc_memb_free(&tcp_session_s, session);
OC_DBG("freed TCP session");
......@@ -599,7 +617,7 @@ oc_tcp_send_buffer(ip_context_t *dev, oc_message_t *message,
bytes_sent += send_len;
} while (bytes_sent < message->length);
OC_DBG("Sent %d bytes", bytes_sent);
OC_DBG("Sent %zd bytes", bytes_sent);
oc_tcp_send_buffer_done:
pthread_mutex_unlock(&dev->tcp.mutex);
......@@ -614,7 +632,7 @@ oc_tcp_send_buffer_done:
static int
tcp_connectivity_ipv4_init(ip_context_t *dev)
{
OC_DBG("Initializing TCP adapter IPv4 for device %d", dev->device);
OC_DBG("Initializing TCP adapter IPv4 for device %zd", dev->device);
memset(&dev->tcp.server4, 0, sizeof(struct sockaddr_storage));
struct sockaddr_in *l = (struct sockaddr_in *)&dev->tcp.server4;
......@@ -670,7 +688,7 @@ tcp_connectivity_ipv4_init(ip_context_t *dev)
ntohs(((struct sockaddr_in *)&dev->tcp.secure4)->sin_port);
#endif /* OC_SECURITY */
OC_DBG("Successfully initialized TCP adapter IPv4 for device %d",
OC_DBG("Successfully initialized TCP adapter IPv4 for device %zd",
dev->device);
return 0;
......@@ -680,7 +698,7 @@ tcp_connectivity_ipv4_init(ip_context_t *dev)
int
oc_tcp_connectivity_init(ip_context_t *dev)
{
OC_DBG("Initializing TCP adapter for device %d", dev->device);
OC_DBG("Initializing TCP adapter for device %zd", dev->device);
if (pthread_mutex_init(&dev->tcp.mutex, NULL) != 0) {
oc_abort("error initializing TCP adapter mutex");
......@@ -761,7 +779,7 @@ oc_tcp_connectivity_init(ip_context_t *dev)
#endif
#endif
OC_DBG("Successfully initialized TCP adapter for device %d", dev->device);
OC_DBG("Successfully initialized TCP adapter for device %zd", dev->device);
return 0;
}
......@@ -796,7 +814,7 @@ oc_tcp_connectivity_shutdown(ip_context_t *dev)
pthread_mutex_destroy(&dev->tcp.mutex);
OC_DBG("oc_tcp_connectivity_shutdown for device %d", dev->device);
OC_DBG("oc_tcp_connectivity_shutdown for device %zd", dev->device);
}
tcp_csm_state_t
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment