arduino-emulator
Loading...
Searching...
No Matches
IPAddress.h
1/*
2 IPAddress.h - Base class that provides IPAddress
3 Copyright (c) 2011 Adrian McEwen. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#pragma once
21
22#include <stdint.h>
23#include "Printable.h"
24#include "String.h"
25
26#define IPADDRESS_V4_BYTES_INDEX 12
27#define IPADDRESS_V4_DWORD_INDEX 3
28
29// forward declarations of global name space friend classes
30class EthernetClass;
31class DhcpClass;
32class DNSClient;
33
34namespace arduino {
35
36// A class to make it easier to handle and pass around IP addresses
37
38enum IPType {
39 IPv4,
40 IPv6
41};
42
43class IPAddress : public Printable {
44private:
45 union {
46 uint8_t bytes[16];
47 uint32_t dword[4];
48 } _address;
49 IPType _type;
50
51 // Access the raw byte array containing the address. Because this returns a pointer
52 // to the internal structure rather than a copy of the address this function should only
53 // be used when you know that the usage of the returned uint8_t* will be transient and not
54 // stored.
55 uint8_t* raw_address() { return _type == IPv4 ? &_address.bytes[IPADDRESS_V4_BYTES_INDEX] : _address.bytes; }
56
57public:
58 // Constructors
59
60 // Default IPv4
61 IPAddress();
62 IPAddress(IPType ip_type);
65 // IPv4; see implementation note
66 IPAddress(uint32_t address);
67 // Default IPv4
68 IPAddress(const uint8_t *address);
69 IPAddress(IPType ip_type, const uint8_t *address);
70 // If IPv4 fails tries IPv6 see fromString function
71 IPAddress(const char *address);
72
73 bool fromString(const char *address);
74 bool fromString(const String &address) { return fromString(address.c_str()); }
75
76 // Overloaded cast operator to allow IPAddress objects to be used where a uint32_t is expected
77 // NOTE: IPv4 only; see implementation note
78 operator uint32_t() const { return _type == IPv4 ? _address.dword[IPADDRESS_V4_DWORD_INDEX] : 0; };
79
80 bool operator==(const IPAddress& addr) const;
81 bool operator!=(const IPAddress& addr) const { return !(*this == addr); };
82
83 // NOTE: IPv4 only; we don't know the length of the pointer
84 bool operator==(const uint8_t* addr) const;
85
86 // Overloaded index operator to allow getting and setting individual octets of the address
87 uint8_t operator[](int index) const;
88 uint8_t& operator[](int index);
89
90 // Overloaded copy operators to allow initialisation of IPAddress objects from other types
91 // NOTE: IPv4 only
92 IPAddress& operator=(const uint8_t *address);
93 // NOTE: IPv4 only; see implementation note
94 IPAddress& operator=(uint32_t address);
95 // If IPv4 fails tries IPv6 see fromString function
96 IPAddress& operator=(const char *address);
97
98 virtual size_t printTo(Print& p) const;
99 String toString() const;
100
101 IPType type() const { return _type; }
102
103 friend class UDP;
104 friend class Client;
105 friend class Server;
106
107 friend ::EthernetClass;
108 friend ::DhcpClass;
109 friend ::DNSClient;
110
111protected:
112 bool fromString4(const char *address);
113 bool fromString6(const char *address);
114 String toString4() const;
115 String toString6() const;
116};
117
118extern const IPAddress IN6ADDR_ANY;
119extern const IPAddress INADDR_NONE;
120}
121
Definition Client.h:27
Definition DMAPool.h:103
Definition IPAddress.h:43
Definition Print.h:36
Definition Printable.h:35
Definition Server.h:26
Definition String.h:53
Definition Udp.h:42
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31