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@116:60521b29e4c9, 2019-01-25 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Jan 25 17:37:51 2019 +0000
- Revision:
- 116:60521b29e4c9
- Parent:
- 113:904b40231907
- Child:
- 176:7eb916c22084
Added backoff delay to DHCP
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 61:aad055f1b0d1 | 1 | #include <stdint.h> |
andrewboyson | 61:aad055f1b0d1 | 2 | #include <stdio.h> |
andrewboyson | 61:aad055f1b0d1 | 3 | |
andrewboyson | 49:1a6336f2b3f9 | 4 | #include "log.h" |
andrewboyson | 49:1a6336f2b3f9 | 5 | #include "http.h" |
andrewboyson | 49:1a6336f2b3f9 | 6 | #include "action.h" |
andrewboyson | 49:1a6336f2b3f9 | 7 | #include "ip4addr.h" |
andrewboyson | 49:1a6336f2b3f9 | 8 | #include "dhcp.h" |
andrewboyson | 112:f8694d0b8858 | 9 | #include "ntpclient.h" |
andrewboyson | 57:e0fb648acf48 | 10 | #include "tftp.h" |
andrewboyson | 49:1a6336f2b3f9 | 11 | |
andrewboyson | 49:1a6336f2b3f9 | 12 | int Ip4AddressToString(uint32_t ip, int size, char* text) |
andrewboyson | 49:1a6336f2b3f9 | 13 | { |
andrewboyson | 49:1a6336f2b3f9 | 14 | int a0 = (ip & 0xFF000000) >> 24; |
andrewboyson | 49:1a6336f2b3f9 | 15 | int a1 = (ip & 0x00FF0000) >> 16; |
andrewboyson | 49:1a6336f2b3f9 | 16 | int a2 = (ip & 0x0000FF00) >> 8; |
andrewboyson | 49:1a6336f2b3f9 | 17 | int a3 = (ip & 0x000000FF); |
andrewboyson | 49:1a6336f2b3f9 | 18 | return snprintf(text, size, "%d.%d.%d.%d", a3, a2, a1, a0); |
andrewboyson | 49:1a6336f2b3f9 | 19 | } |
andrewboyson | 49:1a6336f2b3f9 | 20 | int Ip4AddressLog(uint32_t ip) |
andrewboyson | 49:1a6336f2b3f9 | 21 | { |
andrewboyson | 49:1a6336f2b3f9 | 22 | int a0 = (ip & 0xFF000000) >> 24; |
andrewboyson | 49:1a6336f2b3f9 | 23 | int a1 = (ip & 0x00FF0000) >> 16; |
andrewboyson | 49:1a6336f2b3f9 | 24 | int a2 = (ip & 0x0000FF00) >> 8; |
andrewboyson | 49:1a6336f2b3f9 | 25 | int a3 = (ip & 0x000000FF); |
andrewboyson | 49:1a6336f2b3f9 | 26 | return LogF("%d.%d.%d.%d", a3, a2, a1, a0); |
andrewboyson | 49:1a6336f2b3f9 | 27 | } |
andrewboyson | 49:1a6336f2b3f9 | 28 | int Ip4AddressHttp(uint32_t ip) |
andrewboyson | 49:1a6336f2b3f9 | 29 | { |
andrewboyson | 49:1a6336f2b3f9 | 30 | int a0 = (ip & 0xFF000000) >> 24; |
andrewboyson | 49:1a6336f2b3f9 | 31 | int a1 = (ip & 0x00FF0000) >> 16; |
andrewboyson | 49:1a6336f2b3f9 | 32 | int a2 = (ip & 0x0000FF00) >> 8; |
andrewboyson | 49:1a6336f2b3f9 | 33 | int a3 = (ip & 0x000000FF); |
andrewboyson | 54:84ef2b29cf7e | 34 | return HttpAddF("%d.%d.%d.%d", a3, a2, a1, a0); |
andrewboyson | 49:1a6336f2b3f9 | 35 | } |
andrewboyson | 49:1a6336f2b3f9 | 36 | |
andrewboyson | 49:1a6336f2b3f9 | 37 | uint32_t Ip4AddressParse(char* text) |
andrewboyson | 49:1a6336f2b3f9 | 38 | { |
andrewboyson | 49:1a6336f2b3f9 | 39 | int ints[4]; |
andrewboyson | 49:1a6336f2b3f9 | 40 | sscanf(text, "%d.%d.%d.%d", &ints[3], &ints[2], &ints[1], &ints[0]); |
andrewboyson | 49:1a6336f2b3f9 | 41 | return (ints[0] << 24) + (ints[1] << 16) + (ints[2] << 8) + ints[3]; |
andrewboyson | 49:1a6336f2b3f9 | 42 | } |
andrewboyson | 49:1a6336f2b3f9 | 43 | |
andrewboyson | 49:1a6336f2b3f9 | 44 | void Ip4AddressFromDest(int dest, uint32_t* pDstIp) |
andrewboyson | 49:1a6336f2b3f9 | 45 | { |
andrewboyson | 49:1a6336f2b3f9 | 46 | switch (dest) |
andrewboyson | 49:1a6336f2b3f9 | 47 | { |
andrewboyson | 49:1a6336f2b3f9 | 48 | case UNICAST: /*No change*/ break; |
andrewboyson | 116:60521b29e4c9 | 49 | case UNICAST_DNS: *pDstIp = DhcpDnsServerIp; break; |
andrewboyson | 116:60521b29e4c9 | 50 | case UNICAST_DHCP: *pDstIp = DhcpServerIp; break; |
andrewboyson | 113:904b40231907 | 51 | case UNICAST_NTP: *pDstIp = NtpClientQueryServerIp4; break; |
andrewboyson | 57:e0fb648acf48 | 52 | case UNICAST_TFTP: *pDstIp = TftpServerIp4; break; |
andrewboyson | 49:1a6336f2b3f9 | 53 | case MULTICAST_NODE: *pDstIp = IP4_MULTICAST_ALL_HOSTS; break; |
andrewboyson | 49:1a6336f2b3f9 | 54 | case MULTICAST_ROUTER: *pDstIp = IP4_MULTICAST_ALL_ROUTERS; break; |
andrewboyson | 49:1a6336f2b3f9 | 55 | case MULTICAST_MDNS: *pDstIp = IP4_MULTICAST_DNS_ADDRESS; break; |
andrewboyson | 49:1a6336f2b3f9 | 56 | case MULTICAST_LLMNR: *pDstIp = IP4_MULTICAST_LLMNR_ADDRESS; break; |
andrewboyson | 107:cc58b4c2090b | 57 | case MULTICAST_NTP: *pDstIp = IP4_MULTICAST_NTP_ADDRESS; break; |
andrewboyson | 49:1a6336f2b3f9 | 58 | case BROADCAST: *pDstIp = IP4_BROADCAST_ADDRESS; break; |
andrewboyson | 49:1a6336f2b3f9 | 59 | default: |
andrewboyson | 52:fbc5a46b5e16 | 60 | LogTimeF("Ip4AddressFromDest unknown destination %d\r\n", dest); |
andrewboyson | 49:1a6336f2b3f9 | 61 | break; |
andrewboyson | 49:1a6336f2b3f9 | 62 | } |
andrewboyson | 49:1a6336f2b3f9 | 63 | } |