A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.

Dependents:   oldheating gps motorhome heating

ip6/slaac.c

Committer:
andrewboyson
Date:
2020-04-02
Revision:
167:3ba4e3c49631
Parent:
80:4ef1500fca1d
Child:
172:9bc3c7b2cca1

File content as of revision 167:3ba4e3c49631:

#include <string.h>

#include "mac.h"
#include "ip6addr.h"

char SlaacLinkLocalIp[16];
char SlaacGlobalIp[16];

int SlaacScope(char* ip)
{
    if (Ip6AddressIsSame(ip, SlaacLinkLocalIp)) return SCOPE_LOCAL;
    if (Ip6AddressIsSame(ip, SlaacGlobalIp   )) return SCOPE_GLOBAL;
    return SCOPE_NONE;
}
void SlaacAddressFromScope(int scope, char* pSrcIp)
{
    if (scope == SCOPE_GLOBAL) Ip6AddressCopy(pSrcIp, SlaacGlobalIp   );
    else                       Ip6AddressCopy(pSrcIp, SlaacLinkLocalIp);
    //Note that scope could be SCOPE_NONE if source was multicast in which case should return the link local ip.
}

void SlaacMakeGlobal(char* pPrefix)
{
    memcpy(SlaacGlobalIp, pPrefix, 8);
    char* p = SlaacGlobalIp + 8;
    *p++ = MacLocal[0] | 0x02; //Modified EUI-64
    *p++ = MacLocal[1];
    *p++ = MacLocal[2];
    *p++ = 0xFF;
    *p++ = 0xFE;
    *p++ = MacLocal[3];
    *p++ = MacLocal[4];
    *p++ = MacLocal[5];
    
}
void SlaacInit()
{
    char* p = SlaacLinkLocalIp; //fe80::::202:f7ff:fef2:7d27
    *p++ = 0xFE;
    *p++ = 0x80;
    *p++ = 0x00;
    *p++ = 0x00;
    *p++ = 0x00;
    *p++ = 0x00;
    *p++ = 0x00;
    *p++ = 0x00;
    *p++ = MacLocal[0] | 0x02; //Modified EUI-64
    *p++ = MacLocal[1];
    *p++ = MacLocal[2];
    *p++ = 0xFF;
    *p++ = 0xFE;
    *p++ = MacLocal[3];
    *p++ = MacLocal[4];
    *p++ = MacLocal[5];
}