/* * Copyright (c) 2016, The OpenThread Authors. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holder nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /** * @file * This file implements the OpenThread IPv6 API. */ #define WPP_NAME "ip6_api.tmh" #include "openthread-core-config.h" #include #include "common/instance.hpp" #include "common/locator-getters.hpp" #include "common/logging.hpp" #if OPENTHREAD_CONFIG_ENABLE_SLAAC #include "utils/slaac_address.hpp" #endif using namespace ot; otError otIp6SetEnabled(otInstance *aInstance, bool aEnabled) { otError error = OT_ERROR_NONE; Instance &instance = *static_cast(aInstance); #if OPENTHREAD_ENABLE_RAW_LINK_API VerifyOrExit(!instance.Get().IsEnabled(), error = OT_ERROR_INVALID_STATE); #endif if (aEnabled) { instance.Get().Up(); } else { instance.Get().Down(); } #if OPENTHREAD_ENABLE_RAW_LINK_API exit: #endif return error; } bool otIp6IsEnabled(otInstance *aInstance) { Instance &instance = *static_cast(aInstance); return instance.Get().IsUp(); } const otNetifAddress *otIp6GetUnicastAddresses(otInstance *aInstance) { Instance &instance = *static_cast(aInstance); return instance.Get().GetUnicastAddresses(); } otError otIp6AddUnicastAddress(otInstance *aInstance, const otNetifAddress *aAddress) { Instance &instance = *static_cast(aInstance); return instance.Get().AddExternalUnicastAddress( *static_cast(aAddress)); } otError otIp6RemoveUnicastAddress(otInstance *aInstance, const otIp6Address *aAddress) { Instance &instance = *static_cast(aInstance); return instance.Get().RemoveExternalUnicastAddress(*static_cast(aAddress)); } const otNetifMulticastAddress *otIp6GetMulticastAddresses(otInstance *aInstance) { Instance &instance = *static_cast(aInstance); return instance.Get().GetMulticastAddresses(); } otError otIp6SubscribeMulticastAddress(otInstance *aInstance, const otIp6Address *aAddress) { Instance &instance = *static_cast(aInstance); return instance.Get().SubscribeExternalMulticast(*static_cast(aAddress)); } otError otIp6UnsubscribeMulticastAddress(otInstance *aInstance, const otIp6Address *aAddress) { Instance &instance = *static_cast(aInstance); return instance.Get().UnsubscribeExternalMulticast(*static_cast(aAddress)); } bool otIp6IsMulticastPromiscuousEnabled(otInstance *aInstance) { Instance &instance = *static_cast(aInstance); return instance.Get().IsMulticastPromiscuousEnabled(); } void otIp6SetMulticastPromiscuousEnabled(otInstance *aInstance, bool aEnabled) { Instance &instance = *static_cast(aInstance); instance.Get().SetMulticastPromiscuous(aEnabled); } void otIp6SetReceiveCallback(otInstance *aInstance, otIp6ReceiveCallback aCallback, void *aCallbackContext) { Instance &instance = *static_cast(aInstance); instance.Get().SetReceiveDatagramCallback(aCallback, aCallbackContext); } void otIp6SetAddressCallback(otInstance *aInstance, otIp6AddressCallback aCallback, void *aCallbackContext) { Instance &instance = *static_cast(aInstance); instance.Get().SetAddressCallback(aCallback, aCallbackContext); } bool otIp6IsReceiveFilterEnabled(otInstance *aInstance) { Instance &instance = *static_cast(aInstance); return instance.Get().IsReceiveIp6FilterEnabled(); } void otIp6SetReceiveFilterEnabled(otInstance *aInstance, bool aEnabled) { Instance &instance = *static_cast(aInstance); instance.Get().SetReceiveIp6FilterEnabled(aEnabled); } otError otIp6Send(otInstance *aInstance, otMessage *aMessage) { otError error; Instance &instance = *static_cast(aInstance); error = instance.Get().SendRaw(*static_cast(aMessage), instance.Get().GetInterfaceId()); return error; } otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSettings *aSettings) { Instance &instance = *static_cast(aInstance); Message * message; if (aSettings != NULL) { VerifyOrExit(aSettings->mPriority <= OT_MESSAGE_PRIORITY_HIGH, message = NULL); } message = instance.Get().NewMessage(0, aSettings); exit: return message; } otMessage *otIp6NewMessageFromBuffer(otInstance * aInstance, const uint8_t * aData, uint16_t aDataLength, const otMessageSettings *aSettings) { Instance &instance = *static_cast(aInstance); Message * message; VerifyOrExit((message = instance.Get().NewMessage(aData, aDataLength, aSettings)) != NULL); exit: return message; } otError otIp6AddUnsecurePort(otInstance *aInstance, uint16_t aPort) { Instance &instance = *static_cast(aInstance); return instance.Get().AddUnsecurePort(aPort); } otError otIp6RemoveUnsecurePort(otInstance *aInstance, uint16_t aPort) { Instance &instance = *static_cast(aInstance); return instance.Get().RemoveUnsecurePort(aPort); } void otIp6RemoveAllUnsecurePorts(otInstance *aInstance) { Instance &instance = *static_cast(aInstance); instance.Get().RemoveAllUnsecurePorts(); } const uint16_t *otIp6GetUnsecurePorts(otInstance *aInstance, uint8_t *aNumEntries) { Instance &instance = *static_cast(aInstance); return instance.Get().GetUnsecurePorts(*aNumEntries); } bool otIp6IsAddressEqual(const otIp6Address *aFirst, const otIp6Address *aSecond) { return *static_cast(aFirst) == *static_cast(aSecond); } otError otIp6AddressFromString(const char *aString, otIp6Address *aAddress) { return static_cast(aAddress)->FromString(aString); } uint8_t otIp6PrefixMatch(const otIp6Address *aFirst, const otIp6Address *aSecond) { uint8_t rval; VerifyOrExit(aFirst != NULL && aSecond != NULL, rval = 0); rval = static_cast(aFirst)->PrefixMatch(*static_cast(aSecond)); exit: return rval; } bool otIp6IsAddressUnspecified(const otIp6Address *aAddress) { return static_cast(aAddress)->IsUnspecified(); } otError otIp6SelectSourceAddress(otInstance *aInstance, otMessageInfo *aMessageInfo) { otError error = OT_ERROR_NONE; Instance & instance = *static_cast(aInstance); const Ip6::NetifUnicastAddress *netifAddr; netifAddr = instance.Get().SelectSourceAddress(*static_cast(aMessageInfo)); VerifyOrExit(netifAddr != NULL, error = OT_ERROR_NOT_FOUND); memcpy(&aMessageInfo->mSockAddr, &netifAddr->mAddress, sizeof(aMessageInfo->mSockAddr)); exit: return error; } #if OPENTHREAD_CONFIG_ENABLE_SLAAC bool otIp6IsSlaacEnabled(otInstance *aInstance) { return static_cast(aInstance)->Get().IsEnabled(); } void otIp6SetSlaacEnabled(otInstance *aInstance, bool aEnabled) { Instance &instance = *static_cast(aInstance); if (aEnabled) { instance.Get().Enable(); } else { instance.Get().Disable(); } } void otIp6SetSlaacPrefixFilter(otInstance *aInstance, otIp6SlaacPrefixFilter aFilter) { Instance &instance = *static_cast(aInstance); instance.Get().SetFilter(aFilter); } #endif // OPENTHREAD_CONFIG_ENABLE_SLAAC