Exemplo n.º 1
0
ThreadError Tlv::GetOffset(const Message &aMessage, uint8_t aType, uint16_t &aOffset)
{
    ThreadError error = kThreadError_Parse;
    uint16_t offset = aMessage.GetOffset();
    uint16_t end = aMessage.GetLength();
    Tlv tlv;

    while (offset < end)
    {
        aMessage.Read(offset, sizeof(Tlv), &tlv);

        // skip extended TLV
        if (tlv.GetLength() == kExtendedLength)
        {
            uint16_t length = 0;

            offset += sizeof(tlv);
            aMessage.Read(offset, sizeof(length), &length);
            offset += sizeof(length) + HostSwap16(length);
        }
        else if (tlv.GetType() == aType && (offset + sizeof(tlv) + tlv.GetLength()) <= end)
        {
            aOffset = offset;
            ExitNow(error = kThreadError_None);
        }
        else
        {
            offset += sizeof(tlv) + tlv.GetLength();
        }
    }

exit:
    return error;
}
Exemplo n.º 2
0
void JoinerRouter::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
    ThreadNetif &          netif = GetNetif();
    otError                error;
    Coap::Message *        message = NULL;
    Ip6::MessageInfo       messageInfo;
    JoinerUdpPortTlv       udpPort;
    JoinerIidTlv           iid;
    JoinerRouterLocatorTlv rloc;
    ExtendedTlv            tlv;
    uint16_t               borderAgentRloc;
    uint16_t               offset;

    otLogInfoMeshCoP("JoinerRouter::HandleUdpReceive");

    SuccessOrExit(error = GetBorderAgentRloc(GetNetif(), borderAgentRloc));

    VerifyOrExit((message = NewMeshCoPMessage(netif.GetCoap())) != NULL, error = OT_ERROR_NO_BUFS);

    message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_POST);
    message->SetToken(Coap::Message::kDefaultTokenLength);
    message->AppendUriPathOptions(OT_URI_PATH_RELAY_RX);
    message->SetPayloadMarker();

    udpPort.Init();
    udpPort.SetUdpPort(aMessageInfo.GetPeerPort());
    SuccessOrExit(error = message->Append(&udpPort, sizeof(udpPort)));

    iid.Init();
    iid.SetIid(aMessageInfo.GetPeerAddr().mFields.m8 + 8);
    SuccessOrExit(error = message->Append(&iid, sizeof(iid)));

    rloc.Init();
    rloc.SetJoinerRouterLocator(netif.GetMle().GetRloc16());
    SuccessOrExit(error = message->Append(&rloc, sizeof(rloc)));

    tlv.SetType(Tlv::kJoinerDtlsEncapsulation);
    tlv.SetLength(aMessage.GetLength() - aMessage.GetOffset());
    SuccessOrExit(error = message->Append(&tlv, sizeof(tlv)));
    offset = message->GetLength();
    SuccessOrExit(error = message->SetLength(offset + tlv.GetLength()));
    aMessage.CopyTo(aMessage.GetOffset(), offset, tlv.GetLength(), *message);

    messageInfo.SetSockAddr(netif.GetMle().GetMeshLocal16());
    messageInfo.SetPeerAddr(netif.GetMle().GetMeshLocal16());
    messageInfo.GetPeerAddr().mFields.m16[7] = HostSwap16(borderAgentRloc);
    messageInfo.SetPeerPort(kCoapUdpPort);

    SuccessOrExit(error = netif.GetCoap().SendMessage(*message, messageInfo));

    otLogInfoMeshCoP("Sent relay rx");

exit:

    if (error != OT_ERROR_NONE && message != NULL)
    {
        message->Free();
    }
}
Exemplo n.º 3
0
otError Udp::UpdateChecksum(Message &aMessage, uint16_t aChecksum)
{
    aChecksum = aMessage.UpdateChecksum(aChecksum, aMessage.GetOffset(), aMessage.GetLength() - aMessage.GetOffset());

    if (aChecksum != 0xffff)
    {
        aChecksum = ~aChecksum;
    }

    aChecksum = HostSwap16(aChecksum);
    aMessage.Write(aMessage.GetOffset() + UdpHeader::GetChecksumOffset(), sizeof(aChecksum), &aChecksum);
    return OT_ERROR_NONE;
}
Exemplo n.º 4
0
ThreadError AddMplOption(Message &message, Header &header, IpProto nextHeader, uint16_t payloadLength)
{
    ThreadError error = kThreadError_None;
    HopByHopHeader hbhHeader;
    OptionMpl mplOption;

    hbhHeader.SetNextHeader(nextHeader);
    hbhHeader.SetLength(0);
    sMpl.InitOption(mplOption, HostSwap16(header.GetSource().mFields.m16[7]));
    SuccessOrExit(error = message.Prepend(&mplOption, sizeof(mplOption)));
    SuccessOrExit(error = message.Prepend(&hbhHeader, sizeof(hbhHeader)));
    header.SetPayloadLength(sizeof(hbhHeader) + sizeof(mplOption) + payloadLength);
    header.SetNextHeader(kProtoHopOpts);
exit:
    return error;
}
Exemplo n.º 5
0
void UdpExample::HandleUdpReceive(otMessage *aMessage, const otMessageInfo *aMessageInfo)
{
    uint8_t buf[1500];
    int     length;

    mInterpreter.mServer->OutputFormat("%d bytes from ", otMessageGetLength(aMessage) - otMessageGetOffset(aMessage));
    mInterpreter.mServer->OutputFormat(
        "%x:%x:%x:%x:%x:%x:%x:%x %d ", HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[0]),
        HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[1]), HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[2]),
        HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[3]), HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[4]),
        HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[5]), HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[6]),
        HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[7]), aMessageInfo->mPeerPort);

    length      = otMessageRead(aMessage, otMessageGetOffset(aMessage), buf, sizeof(buf) - 1);
    buf[length] = '\0';

    mInterpreter.mServer->OutputFormat("%s\r\n", buf);
}
Exemplo n.º 6
0
void Leader::HandlePetition(Coap::Header &aHeader, Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
    CommissioningData data;
    CommissionerIdTlv commissionerId;
    StateTlv::State state = StateTlv::kReject;

    otLogInfoMeshCoP(GetInstance(), "received petition");

    SuccessOrExit(Tlv::GetTlv(aMessage, Tlv::kCommissionerId, sizeof(commissionerId), commissionerId));
    VerifyOrExit(commissionerId.IsValid());

    if (mTimer.IsRunning())
    {
        VerifyOrExit(!strncmp(commissionerId.GetCommissionerId(), mCommissionerId.GetCommissionerId(),
                              sizeof(mCommissionerId)));

        ResignCommissioner();
    }

    data.mBorderAgentLocator.Init();
    data.mBorderAgentLocator.SetBorderAgentLocator(HostSwap16(aMessageInfo.GetPeerAddr().mFields.m16[7]));

    data.mCommissionerSessionId.Init();
    data.mCommissionerSessionId.SetCommissionerSessionId(++mSessionId);

    data.mSteeringData.Init();
    data.mSteeringData.SetLength(1);
    data.mSteeringData.Clear();

    SuccessOrExit(GetNetif().GetNetworkDataLeader().SetCommissioningData(reinterpret_cast<uint8_t *>(&data),
                                                                         data.GetLength()));

    mCommissionerId = commissionerId;

    state = StateTlv::kAccept;
    mTimer.Start(TimerMilli::SecToMsec(kTimeoutLeaderPetition));

exit:
    OT_UNUSED_VARIABLE(aMessageInfo);
    SendPetitionResponse(aHeader, aMessageInfo, state);
}
Exemplo n.º 7
0
void ChannelTlv::SetChannel(uint16_t aChannel)
{
    uint8_t channelPage = OT_RADIO_CHANNEL_PAGE_0;

#if OPENTHREAD_CONFIG_RADIO_2P4GHZ_OQPSK_SUPPORT
    if ((OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MIN <= aChannel) && (aChannel <= OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MAX))
    {
        channelPage = OT_RADIO_CHANNEL_PAGE_0;
    }
#endif

#if OPENTHREAD_CONFIG_RADIO_915MHZ_OQPSK_SUPPORT
    if ((OT_RADIO_915MHZ_OQPSK_CHANNEL_MIN <= aChannel) && (aChannel <= OT_RADIO_915MHZ_OQPSK_CHANNEL_MAX))
    {
        channelPage = OT_RADIO_CHANNEL_PAGE_2;
    }
#endif

    SetChannelPage(channelPage);
    mChannel = HostSwap16(aChannel);
}
Exemplo n.º 8
0
otError Joiner::TryNextJoin()
{
    ThreadNetif & netif        = GetNetif();
    otError       error        = OT_ERROR_NOT_FOUND;
    JoinerRouter *joinerRouter = &mJoinerRouters[0];

    for (size_t i = 1; i < OPENTHREAD_CONFIG_MAX_JOINER_ROUTER_ENTRIES; i++)
    {
        if (mJoinerRouters[i].mPriority > joinerRouter->mPriority)
        {
            joinerRouter = &mJoinerRouters[i];
        }
    }

    if (joinerRouter->mPriority > 0)
    {
        Ip6::SockAddr sockaddr;

        joinerRouter->mPriority = 0;

        netif.GetMac().SetPanId(joinerRouter->mPanId);
        netif.GetMac().SetPanChannel(joinerRouter->mChannel);
        netif.GetIp6Filter().AddUnsecurePort(OPENTHREAD_CONFIG_JOINER_UDP_PORT);

        sockaddr.GetAddress().mFields.m16[0] = HostSwap16(0xfe80);
        sockaddr.GetAddress().SetIid(joinerRouter->mExtAddr);
        sockaddr.mPort    = joinerRouter->mJoinerUdpPort;
        sockaddr.mScopeId = OT_NETIF_INTERFACE_ID_THREAD;

        netif.GetCoapSecure().Connect(sockaddr, Joiner::HandleSecureCoapClientConnect, this);
        mState = OT_JOINER_STATE_CONNECT;
        error  = OT_ERROR_NONE;
    }
    else
    {
        otLogDebgMeshCoP("No joinable networks remaining to try");
    }

    return error;
}
Exemplo n.º 9
0
otError Tlv::GetOffset(const Message &aMessage, uint8_t aType, uint16_t &aOffset)
{
    otError error = OT_ERROR_NOT_FOUND;
    uint16_t offset = aMessage.GetOffset();
    uint16_t end = aMessage.GetLength();
    Tlv tlv;

    while (offset + sizeof(tlv) <= end)
    {
        uint32_t length = sizeof(tlv);

        aMessage.Read(offset, sizeof(tlv), &tlv);

        if (tlv.GetLength() != kExtendedLength)
        {
            length += tlv.GetLength();
        }
        else
        {
            uint16_t extLength;

            VerifyOrExit(sizeof(extLength) == aMessage.Read(offset + sizeof(tlv), sizeof(extLength), &extLength));
            length += sizeof(extLength) + HostSwap16(extLength);
        }

        VerifyOrExit(offset + length <= end);

        if (tlv.GetType() == aType)
        {
            aOffset = offset;
            ExitNow(error = OT_ERROR_NONE);
        }

        offset += static_cast<uint16_t>(length);
    }

exit:
    return error;
}
Exemplo n.º 10
0
otError Tlv::GetValueOffset(const Message &aMessage, uint8_t aType, uint16_t &aOffset, uint16_t &aLength)
{
    otError error = OT_ERROR_NOT_FOUND;
    uint16_t offset = aMessage.GetOffset();
    uint16_t end = aMessage.GetLength();
    Tlv tlv;

    while (offset + sizeof(tlv) <= end)
    {
        uint16_t length;

        aMessage.Read(offset, sizeof(tlv), &tlv);
        offset += sizeof(tlv);
        length = tlv.GetLength();

        if (length == kExtendedLength)
        {
            VerifyOrExit(offset + sizeof(length) <= end);
            aMessage.Read(offset, sizeof(length), &length);
            offset += sizeof(length);
            length = HostSwap16(length);
        }

        VerifyOrExit(length <= end - offset);

        if (tlv.GetType() == aType)
        {
            aOffset = offset;
            aLength = length;
            ExitNow(error = OT_ERROR_NONE);
        }

        offset += length;
    }

exit:
    return error;
}
Exemplo n.º 11
0
ThreadError Tlv::GetValueOffset(const Message &aMessage, uint8_t aType, uint16_t &aOffset, uint16_t &aLength)
{
    ThreadError error = kThreadError_Parse;
    uint16_t offset = aMessage.GetOffset();
    uint16_t end = aMessage.GetLength();

    while (offset < end)
    {
        Tlv tlv;
        uint16_t length;

        aMessage.Read(offset, sizeof(tlv), &tlv);
        offset += sizeof(tlv);

        length = tlv.GetLength();

        if (length == kExtendedLength)
        {
            aMessage.Read(offset, sizeof(length), &length);
            offset += sizeof(length);
            length = HostSwap16(length);
        }

        if (tlv.GetType() == aType)
        {
            aOffset = offset;
            aLength = length;
            ExitNow(error = kThreadError_None);
        }

        offset += length;
    }

exit:
    return error;
}
Exemplo n.º 12
0
otError Mpl::ProcessOption(Message &aMessage, const Address &aAddress, bool aIsOutbound)
{
    otError error;
    OptionMpl option;

    VerifyOrExit(aMessage.Read(aMessage.GetOffset(), sizeof(option), &option) >= OptionMpl::kMinLength &&
                 (option.GetSeedIdLength() == OptionMpl::kSeedIdLength0 ||
                  option.GetSeedIdLength() == OptionMpl::kSeedIdLength2),
                 error = OT_ERROR_DROP);

    if (option.GetSeedIdLength() == OptionMpl::kSeedIdLength0)
    {
        // Retrieve MPL Seed Id from the IPv6 Source Address.
        option.SetSeedId(HostSwap16(aAddress.mFields.m16[7]));
    }

    // Check MPL Data Messages in the MPL Buffered Set against sequence number.
    UpdateBufferedSet(option.GetSeedId(), option.GetSequence());

    // Check if the MPL Data Message is new.
    error = UpdateSeedSet(option.GetSeedId(), option.GetSequence());

    if (error == OT_ERROR_NONE)
    {
        AddBufferedMessage(aMessage, option.GetSeedId(), option.GetSequence(), aIsOutbound);
    }
    else if (aIsOutbound)
    {
        // In case MPL Data Message is generated locally, ignore potential error of the MPL Seed Set
        // to allow subsequent retransmissions with the same sequence number.
        ExitNow(error = OT_ERROR_NONE);
    }

exit:
    return error;
}
Exemplo n.º 13
0
 /**
  * This method returns the TCP Flags.
  *
  * @returns The TCP Flags.
  *
  */
 uint16_t GetFlags(void) const { return HostSwap16(mFlags); }
Exemplo n.º 14
0
 /**
  * This method sets the MPL Seed Id value.
  *
  * @param[in]  aSeedId  The MPL Seed Id value.
  *
  */
 void SetSeedId(uint16_t aSeedId) { mSeedId = HostSwap16(aSeedId); }
Exemplo n.º 15
0
 /**
  * This method returns the MPL Seed Id value.
  *
  * @returns The MPL Seed Id value.
  *
  */
 uint16_t GetSeedId(void) const { return HostSwap16(mSeedId); }
Exemplo n.º 16
0
 /**
  * This method sets the ICMPv6 message sequence for Echo Requests and Replies.
  *
  * @param[in]  aSequence  The ICMPv6 message sequence.
  *
  */
 void SetSequence(uint16_t aSequence) { mData.m16[1] = HostSwap16(aSequence); }
Exemplo n.º 17
0
 /**
  * This method returns the ICMPv6 message sequence for Echo Requests and Replies.
  *
  * @returns The ICMPv6 message sequence.
  *
  */
 uint16_t GetSequence(void) const { return HostSwap16(mData.m16[1]); }
Exemplo n.º 18
0
 /**
  * This method returns the Length value.
  *
  */
 uint16_t GetLength(void) const { return HostSwap16(mLength); }
Exemplo n.º 19
0
 /**
  * This method returns the TCP Destination Port.
  *
  * @returns The TCP Destination Port.
  *
  */
 uint16_t GetDestinationPort(void) const { return HostSwap16(mDestination); }
Exemplo n.º 20
0
 /**
  * This method returns the TCP Urgent Pointer.
  *
  * @returns The TCP Urgent Pointer.
  *
  */
 uint16_t GetUrgentPointer(void) const { return HostSwap16(mUrgentPointer); }
Exemplo n.º 21
0
 /**
  * This method returns the TCP Window.
  *
  * @returns The TCP Window.
  *
  */
 uint16_t GetWindow(void) const { return HostSwap16(mWindow); }
Exemplo n.º 22
0
void JoinerRouter::HandleRelayTransmit(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
    OT_UNUSED_VARIABLE(aMessageInfo);

    otError            error;
    JoinerUdpPortTlv   joinerPort;
    JoinerIidTlv       joinerIid;
    JoinerRouterKekTlv kek;
    uint16_t           offset;
    uint16_t           length;
    Message *          message  = NULL;
    otMessageSettings  settings = {false, static_cast<otMessagePriority>(kMeshCoPMessagePriority)};
    Ip6::MessageInfo   messageInfo;

    VerifyOrExit(aMessage.GetType() == OT_COAP_TYPE_NON_CONFIRMABLE && aMessage.GetCode() == OT_COAP_CODE_POST,
                 error = OT_ERROR_DROP);

    otLogInfoMeshCoP("Received relay transmit");

    SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kJoinerUdpPort, sizeof(joinerPort), joinerPort));
    VerifyOrExit(joinerPort.IsValid(), error = OT_ERROR_PARSE);

    SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kJoinerIid, sizeof(joinerIid), joinerIid));
    VerifyOrExit(joinerIid.IsValid(), error = OT_ERROR_PARSE);

    SuccessOrExit(error = Tlv::GetValueOffset(aMessage, Tlv::kJoinerDtlsEncapsulation, offset, length));

    VerifyOrExit((message = mSocket.NewMessage(0, &settings)) != NULL, error = OT_ERROR_NO_BUFS);

    while (length)
    {
        uint16_t copyLength = length;
        uint8_t  tmp[16];

        if (copyLength >= sizeof(tmp))
        {
            copyLength = sizeof(tmp);
        }

        aMessage.Read(offset, copyLength, tmp);
        SuccessOrExit(error = message->Append(tmp, copyLength));

        offset += copyLength;
        length -= copyLength;
    }

    aMessage.CopyTo(offset, 0, length, *message);

    messageInfo.mPeerAddr.mFields.m16[0] = HostSwap16(0xfe80);
    memcpy(messageInfo.mPeerAddr.mFields.m8 + 8, joinerIid.GetIid(), 8);
    messageInfo.SetPeerPort(joinerPort.GetUdpPort());
    messageInfo.SetInterfaceId(GetNetif().GetInterfaceId());

    SuccessOrExit(error = mSocket.SendTo(*message, messageInfo));

    if (Tlv::GetTlv(aMessage, Tlv::kJoinerRouterKek, sizeof(kek), kek) == OT_ERROR_NONE)
    {
        otLogInfoMeshCoP("Received kek");

        DelaySendingJoinerEntrust(messageInfo, kek);
    }

exit:
    if (error != OT_ERROR_NONE && message != NULL)
    {
        message->Free();
    }
}
Exemplo n.º 23
0
 /**
  * This method sets the Length value.
  *
  * @param[in]  aLength  The Length value.
  *
  */
 void SetLength(uint16_t aLength)
 {
     Tlv::SetLength(kExtendedLength);
     mLength = HostSwap16(aLength);
 }
Exemplo n.º 24
0
void Coap::HandleServerResponse(otCoapHeader *aHeader, otMessage *aMessage, const otMessageInfo *aMessageInfo)
{
    otError error = OT_ERROR_NONE;
    otCoapHeader responseHeader;
    otMessage *responseMessage;
    otCoapCode responseCode = OT_COAP_CODE_EMPTY;
    char responseContent = '0';

    mInterpreter.mServer->OutputFormat("Received coap request from [%x:%x:%x:%x:%x:%x:%x:%x]: ",
                                       HostSwap16(aMessageInfo->mSockAddr.mFields.m16[0]),
                                       HostSwap16(aMessageInfo->mSockAddr.mFields.m16[1]),
                                       HostSwap16(aMessageInfo->mSockAddr.mFields.m16[2]),
                                       HostSwap16(aMessageInfo->mSockAddr.mFields.m16[3]),
                                       HostSwap16(aMessageInfo->mSockAddr.mFields.m16[4]),
                                       HostSwap16(aMessageInfo->mSockAddr.mFields.m16[5]),
                                       HostSwap16(aMessageInfo->mSockAddr.mFields.m16[6]),
                                       HostSwap16(aMessageInfo->mSockAddr.mFields.m16[7]));

    switch (otCoapHeaderGetCode(aHeader))
    {
    case OT_COAP_CODE_GET:
        mInterpreter.mServer->OutputFormat("GET");
        break;

    case OT_COAP_CODE_DELETE:
        mInterpreter.mServer->OutputFormat("DELETE");
        break;

    case OT_COAP_CODE_PUT:
        mInterpreter.mServer->OutputFormat("PUT");
        break;

    case OT_COAP_CODE_POST:
        mInterpreter.mServer->OutputFormat("POST");
        break;

    default:
        mInterpreter.mServer->OutputFormat("Undefined\r\n");
        return;
    }

    PrintPayload(aMessage);

    if ((otCoapHeaderGetType(aHeader) == OT_COAP_TYPE_CONFIRMABLE) || otCoapHeaderGetCode(aHeader) == OT_COAP_CODE_GET)
    {
        if (otCoapHeaderGetCode(aHeader) == OT_COAP_CODE_GET)
        {
            responseCode = OT_COAP_CODE_CONTENT;
        }
        else
        {
            responseCode = OT_COAP_CODE_VALID;
        }

        otCoapHeaderInit(&responseHeader, OT_COAP_TYPE_ACKNOWLEDGMENT, responseCode);
        otCoapHeaderSetMessageId(&responseHeader, otCoapHeaderGetMessageId(aHeader));
        otCoapHeaderSetToken(&responseHeader, otCoapHeaderGetToken(aHeader), otCoapHeaderGetTokenLength(aHeader));

        if (otCoapHeaderGetCode(aHeader) == OT_COAP_CODE_GET)
        {
            otCoapHeaderSetPayloadMarker(&responseHeader);
        }

        responseMessage = otCoapNewMessage(mInterpreter.mInstance, &responseHeader);
        VerifyOrExit(responseMessage != NULL, error = OT_ERROR_NO_BUFS);

        if (otCoapHeaderGetCode(aHeader) == OT_COAP_CODE_GET)
        {
            SuccessOrExit(error = otMessageAppend(responseMessage, &responseContent, sizeof(responseContent)));
        }

        SuccessOrExit(error = otCoapSendResponse(mInterpreter.mInstance, responseMessage, aMessageInfo));
    }

exit:

    if (error != OT_ERROR_NONE && responseMessage != NULL)
    {
        mInterpreter.mServer->OutputFormat("Cannot send coap response message: Error %d: %s\r\n",
                                           error, otThreadErrorToString(error));
        otMessageFree(responseMessage);
    }
    else if (responseCode >= OT_COAP_CODE_RESPONSE_MIN)
    {
        mInterpreter.mServer->OutputFormat("coap response sent successfully!\r\n");
    }
}
Exemplo n.º 25
0
 /**
  * This method returns the TCP Checksum.
  *
  * @returns The TCP Checksum.
  *
  */
 uint16_t GetChecksum(void) const { return HostSwap16(mChecksum); }
Exemplo n.º 26
0
 /**
  * This method sets the ICMPv6 message checksum.
  *
  * @param[in]  aChecksum  The ICMPv6 message checksum.
  *
  */
 void SetChecksum(uint16_t aChecksum) { mChecksum = HostSwap16(aChecksum); }
Exemplo n.º 27
0
 /**
  * This method returns the TCP Source Port.
  *
  * @returns The TCP Source Port.
  *
  */
 uint16_t GetSourcePort(void) const { return HostSwap16(mSource); }
Exemplo n.º 28
0
 /**
  * This method returns the ICMPv6 message ID for Echo Requests and Replies.
  *
  * @returns The ICMPv6 message ID.
  *
  */
 uint16_t GetId(void) const { return HostSwap16(mData.m16[0]); }
Exemplo n.º 29
0
otError Dhcp6Server::UpdateService(void)
{
    ThreadNetif &netif = GetNetif();
    otError error = OT_ERROR_NONE;
    bool found;
    uint8_t i;
    uint16_t rloc16 = netif.GetMle().GetRloc16();
    Ip6::Address *address = NULL;
    otNetworkDataIterator iterator;
    otBorderRouterConfig config;
    Lowpan::Context lowpanContext;

    // remove dhcp agent aloc and prefix delegation
    for (i = 0; i < OPENTHREAD_CONFIG_NUM_DHCP_PREFIXES; i++)
    {
        found = false;

        if (!mAgentsAloc[i].mValid)
        {
            continue;
        }

        address = &(mAgentsAloc[i].GetAddress());
        iterator = OT_NETWORK_DATA_ITERATOR_INIT;

        while (netif.GetNetworkDataLeader().GetNextOnMeshPrefix(&iterator, rloc16, &config) == OT_ERROR_NONE)
        {
            if (!config.mDhcp)
            {
                continue;
            }

            netif.GetNetworkDataLeader().GetContext(*static_cast<const Ip6::Address *>(&(config.mPrefix.mPrefix)),
                                                    lowpanContext);

            if (address->mFields.m8[15] == lowpanContext.mContextId)
            {
                // still in network data
                found = true;
                break;
            }
        }

        if (!found)
        {
            netif.GetNetworkDataLeader().GetContext(address->mFields.m8[15], lowpanContext);
            netif.RemoveUnicastAddress(mAgentsAloc[i]);
            mAgentsAloc[i].mValid = false;
            RemovePrefixAgent(lowpanContext.mPrefix);
        }
    }

    // add dhcp agent aloc and prefix delegation
    iterator = OT_NETWORK_DATA_ITERATOR_INIT;

    while (netif.GetNetworkDataLeader().GetNextOnMeshPrefix(&iterator, rloc16, &config) == OT_ERROR_NONE)
    {
        found = false;

        if (!config.mDhcp)
        {
            continue;
        }

        netif.GetNetworkDataLeader().GetContext(*static_cast<const Ip6::Address *>(&config.mPrefix.mPrefix),
                                                lowpanContext);

        for (i = 0; i < OPENTHREAD_CONFIG_NUM_DHCP_PREFIXES; i++)
        {
            address = &(mAgentsAloc[i].GetAddress());

            if ((mAgentsAloc[i].mValid) && (address->mFields.m8[15] == lowpanContext.mContextId))
            {
                found = true;
                break;
            }
        }

        // alreay added
        if (found)
        {
            continue;
        }

        for (i = 0; i < OPENTHREAD_CONFIG_NUM_DHCP_PREFIXES; i++)
        {
            if (!mAgentsAloc[i].mValid)
            {
                address = &(mAgentsAloc[i].GetAddress());
                memcpy(address, netif.GetMle().GetMeshLocalPrefix(), 8);
                address->mFields.m16[4] = HostSwap16(0x0000);
                address->mFields.m16[5] = HostSwap16(0x00ff);
                address->mFields.m16[6] = HostSwap16(0xfe00);
                address->mFields.m8[14] = Ip6::Address::kAloc16Mask;
                address->mFields.m8[15] = lowpanContext.mContextId;
                mAgentsAloc[i].mPrefixLength = 128;
                mAgentsAloc[i].mPreferred = true;
                mAgentsAloc[i].mValid = true;
                netif.AddUnicastAddress(mAgentsAloc[i]);
                AddPrefixAgent(config.mPrefix);
                break;
            }
        }

        // if no available Dhcp Agent Aloc resources
        if (i == OPENTHREAD_CONFIG_NUM_DHCP_PREFIXES)
        {
            ExitNow(error = OT_ERROR_NO_BUFS);
        }
    }

    if (mPrefixAgentsCount > 0)
    {
        Start();
    }
    else
    {
        Stop();
    }

exit:
    return error;
}
Exemplo n.º 30
0
 /**
  * This method sets the ICMPv6 message ID for Echo Requests and Replies.
  *
  * @param[in]  aId  The ICMPv6 message ID.
  *
  */
 void SetId(uint16_t aId) { mData.m16[0] = HostSwap16(aId); }