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:
2019-04-10
Revision:
140:9000ea70b220
Parent:
116:60521b29e4c9
Child:
176:7eb916c22084

File content as of revision 140:9000ea70b220:

#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(uint32_t ip, 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(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(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(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(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;
    }
}