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

eth/ar6.c

Committer:
andrewboyson
Date:
2018-12-05
Revision:
94:e2973a2c488e
Parent:
93:580fc113d9e9

File content as of revision 94:e2973a2c488e:

#include    <stdint.h>
#include <stdbool.h>

#include     "log.h"
#include "mstimer.h"
#include     "net.h"
#include     "mac.h"
#include "ip6addr.h"
#include      "ns.h"
#include    "http.h"

bool Ar6Trace = false;

#define  CACHE_TIMEOUT_MS  3600 * 1000
#define FREEZE_TIMEOUT_MS  1800 * 1000
#define  REPLY_TIMEOUT_MS     2 * 1000
#define SEND_ATTEMPTS      3
#define RECORDS_COUNT     20

#define STATE_EMPTY 0
#define STATE_WANT  1
#define STATE_SENT  2
#define STATE_VALID 3

struct record
{
    uint32_t elapsed;
    char     ip[16];
    uint8_t  state;
    uint8_t  tries;
    char     mac[6];
};
static struct record records[RECORDS_COUNT];
static int getExistingIp(char* ip)
{
    for (int i = 0; i < RECORDS_COUNT; i++)
    {
        if (records[i].state && Ip6AddressIsSame(records[i].ip, ip)) return i;
    }
    return -1;
}
static int getOldest()
{
    int        iOldest = 0;
    uint32_t ageOldest = 0;
    for (int i = 0; i < RECORDS_COUNT; i++)
    {
        if (!records[i].state) return i; //Found an empty slot so just return it
        uint32_t age = MsTimerCount - records[i].elapsed;
        if (age >= ageOldest)
        {
            ageOldest = age;
              iOldest = i;
        }
    }  
    return iOldest;                      //Otherwise return the oldest
}
void Ar6MakeRequestForMacFromIp(char* ip)
{
    //Don't treat non ips
    if (!ip[0]) return;
    int i;
    
    //If a record already exists then request an update
    i = getExistingIp(ip);
    if (i > -1)
    {
        if (!MsTimerHasElapsed(records[i].elapsed, FREEZE_TIMEOUT_MS)) return;
        if (Ar6Trace)
        {
            LogTime("AR6 Updated request for MAC of ");
            Ip6AddressLog(ip);
            Log("\r\n");
        }
        records[i].state    = STATE_WANT;
        records[i].tries    = 0;
        records[i].elapsed  = MsTimerCount;
        return;
    }
    
    //If a record does not exist then find the first empty slot and add the IP and date
    if (Ar6Trace)
    {
        LogTime("AR6 Made request for MAC of ");
        Ip6AddressLog(ip);
        Log("\r\n");
    }
    i = getOldest();
    Ip6AddressCopy(records[i].ip, ip);
    records[i].state    = STATE_WANT;
    records[i].tries    = 0;
    records[i].elapsed  = MsTimerCount;
    MacClear(records[i].mac);
}
int Ar6AddIpRecord(void (*traceback)(void), char* mac, char* ip)
{
    //Don't treat non ips
    if (!ip[0])
    {
        if (Ar6Trace)
        {
            LogTime("Ar6AddIpRecord had blank ip\r\n");
            if (NetTraceStack) traceback();
        }
        return -1;
    }
    if (MacIsEmpty(mac))
    {
        if (Ar6Trace)
        {
            LogTime("Ar6AddIpRecord had blank mac\r\n");
            if (NetTraceStack) traceback();
        }
        return -1;
    }
    int i;
    
    //See if any record corresponds to the IP and, if so, update the MAC and time
    i = getExistingIp(ip);
    if (i > -1)
    {
        records[i].elapsed = MsTimerCount;
        records[i].state = STATE_VALID;
        MacCopy(records[i].mac, mac);
        return i;
    }
    
    //Otherwise find the first empty slot and add the MAC, IP and date
    i = getOldest();
    MacCopy(records[i].mac, mac);
    Ip6AddressCopy(records[i].ip, ip);
    records[i].elapsed   = MsTimerCount;
    records[i].state = STATE_VALID;
    return i;
}
void Ar6IpToMac(char* ip, char* mac)
{
    for (int i = 0; i < RECORDS_COUNT; i++)
    {
        if (records[i].state == STATE_VALID && Ip6AddressIsSame(records[i].ip, ip))
        {
            MacCopy(mac, records[i].mac);
            return;
        }
    }
    MacClear(mac);
}
void Ar6IndexToIp(int i, char* ip)
{
    Ip6AddressCopy(ip, records[i].ip);
}
void Ar6SendHttp()
{
    for (int i = 0; i < RECORDS_COUNT; i++)
    {
        if (records[i].state)
        {
            HttpAddF("%4u ", (MsTimerCount - records[i].elapsed) / 1000 / 60);
            
            int ipLen = Ip6AddressHttp(records[i].ip);
            HttpFillChar(' ', 40 - ipLen);
            
            MacHttp(records[i].mac);
            
            HttpAddChar('\r');
            HttpAddChar('\n');
        }
    }
}
static void clear(struct record* pr)
{
    pr->state = STATE_EMPTY;
}
static void clearCache(struct record* pr)
{
    if (MsTimerHasElapsed(pr->elapsed, CACHE_TIMEOUT_MS)) clear(pr);
}
static void retry(struct record* pr)
{
    if (pr->state == STATE_SENT && MsTimerHasElapsed(pr->elapsed, REPLY_TIMEOUT_MS))
    {
        if (pr->tries < SEND_ATTEMPTS)
        {
            pr->state   = STATE_WANT;
            pr->elapsed = MsTimerCount;
            pr->tries++;
        }
        else
        {
            clear(pr);
        }
    }
}
static void sendRequest(struct record* pr)
{
    if (!NsResolveRequestFlag)
    {
        if (pr->state == STATE_WANT)
        {
            if (Ar6Trace)
            {
                LogTime("AR6 Send request for MAC from IP6 ");
                Ip6AddressLog(pr->ip);
                Log("\r\n");
            }
            Ip6AddressCopy(NsAddressToResolve, pr->ip);
            NsResolveRequestFlag = true;
            pr->state   = STATE_SENT;
            pr->elapsed = MsTimerCount;
            return;
        }
    }
}
void Ar6Main()
{
    static int i = -1;
    i++;
    if (i >= RECORDS_COUNT) i = 0;
    
    struct record* pr = &records[i];
    
    clearCache (pr);
    retry      (pr);
    sendRequest(pr);
}
void Ar6Init()
{
    for (int i = 0; i < RECORDS_COUNT; i++)
    {
    
        struct record* pr = &records[i];
        clear(pr);
    }
}