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

ip4/ip4addr.c

Committer:
andrewboyson
Date:
2021-01-16
Revision:
186:24198369b198
Parent:
183:ee809769bf89
Child:
187:122fc1996c86

File content as of revision 186:24198369b198:

#include <stdint.h>
#include <stdio.h>

#include "log.h"
#include "http.h"
#include "action.h"
#include "ip4addr.h"
#include "dhcp.h"
#include "ntpclient.h"
#include "tftp.h"

int Ip4AddressToString(const uint32_t ip, const int size, char* text)
{
    int a0 = (ip & 0xFF000000) >> 24;
    int a1 = (ip & 0x00FF0000) >> 16;
    int a2 = (ip & 0x0000FF00) >>  8;
    int a3 = (ip & 0x000000FF);
    return snprintf(text, size, "%d.%d.%d.%d", a3, a2, a1, a0); 
}
int Ip4AddressLog(const uint32_t ip)
{
    int a0 = (ip & 0xFF000000) >> 24;
    int a1 = (ip & 0x00FF0000) >> 16;
    int a2 = (ip & 0x0000FF00) >>  8;
    int a3 = (ip & 0x000000FF);
    return LogF("%d.%d.%d.%d", a3, a2, a1, a0); 
}
int Ip4AddressHttp(const uint32_t ip)
{
    int a0 = (ip & 0xFF000000) >> 24;
    int a1 = (ip & 0x00FF0000) >> 16;
    int a2 = (ip & 0x0000FF00) >>  8;
    int a3 = (ip & 0x000000FF);
    return HttpAddF("%d.%d.%d.%d", a3, a2, a1, a0); 
}

uint32_t Ip4AddressParse(const char* text)
{
    int ints[4];
    sscanf(text, "%d.%d.%d.%d", &ints[3], &ints[2], &ints[1], &ints[0]);
    return (ints[0] << 24) + (ints[1] << 16) + (ints[2] << 8) + ints[3];
}

void Ip4AddressFromDest(const int dest, uint32_t* pDstIp)
{
    switch (dest)
    {
        case UNICAST:          /*No change*/                          break;
        case UNICAST_DNS:      *pDstIp = DhcpDnsServerIp;             break;
        case UNICAST_DHCP:     *pDstIp = DhcpServerIp;                break;
        case UNICAST_NTP:      *pDstIp = NtpClientQueryServerIp4;     break;
        case UNICAST_TFTP:     *pDstIp = TftpServerIp4;               break;
        case MULTICAST_NODE:   *pDstIp = IP4_MULTICAST_ALL_HOSTS;     break;
        case MULTICAST_ROUTER: *pDstIp = IP4_MULTICAST_ALL_ROUTERS;   break;
        case MULTICAST_MDNS:   *pDstIp = IP4_MULTICAST_DNS_ADDRESS;   break;
        case MULTICAST_LLMNR:  *pDstIp = IP4_MULTICAST_LLMNR_ADDRESS; break;
        case MULTICAST_NTP:    *pDstIp = IP4_MULTICAST_NTP_ADDRESS;   break;
        case BROADCAST:        *pDstIp = IP4_BROADCAST_ADDRESS;       break;
        default:
            LogTimeF("Ip4AddressFromDest unknown destination %d\r\n", dest);
            break;
    }
}
bool Ip4AddrIsRoutable(uint32_t ip)
{
    if ((ip & DhcpSubnetMask) == (DhcpLocalIp & DhcpSubnetMask)) return false; // Ip is same as local ip in the unmasked area
    if ( ip == (DhcpLocalIp | 0xFF000000)                      ) return false; // Ip == 192.168.0.255; '|' is lower precendence than '=='
    if ( ip ==  IP4_BROADCAST_ADDRESS                          ) return false; // dstIp == 255.255.255.255
    if ((ip & 0xE0) == 0xE0                                    ) return false; // 224.x.x.x == 1110 0000 == E0.xx.xx.xx == xx.xx.xx.E0 in little endian
    
    return true;
}