FreeNOS
IPV4Address.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <Log.h>
19#include <String.h>
20#include <MemoryBlock.h>
21#include "IPV4Address.h"
22
24 IPV4 *ipv4)
25 : File(inode)
26 , m_ipv4(ipv4)
27{
28 m_size = sizeof(IPV4::Address);
29}
30
34
36 Size & size,
37 const Size offset)
38{
39 IPV4::Address addr;
40 m_ipv4->getAddress(&addr);
41
42 if (offset >= m_size)
43 {
44 size = 0;
46 }
47
48 buffer.write(&addr, sizeof(addr));
49 size = sizeof(addr);
50
52}
53
55 Size & size,
56 const Size offset)
57{
58 IPV4::Address addr;
59 char tmp[32];
60
61 buffer.read(tmp, size < sizeof(tmp) ? size : sizeof(tmp));
62 tmp[sizeof(tmp) - 1] = 0;
63
64 // Address can be provided in 32-bit format or dotted text format
65 if (size == sizeof(IPV4::Address))
66 {
67 MemoryBlock::copy(&addr, tmp, sizeof(addr));
68 }
69 else
70 {
71 addr = IPV4::toAddress(tmp);
72 }
73
74 DEBUG("address = " << *IPV4::toString(addr));
75
76 // Set the address
77 return m_ipv4->setAddress(&addr);
78}
Represents a file present on a FileSystem.
Definition File.h:40
Size m_size
Size of the file, in bytes.
Definition File.h:148
Abstract Input/Output buffer.
Definition IOBuffer.h:38
FileSystem::Result read(void *buffer, const Size size, const Size offset=ZERO)
Read bytes from the I/O buffer.
Definition IOBuffer.cpp:156
FileSystem::Result write(const void *buffer, const Size size, const Size offset=ZERO)
Write bytes to the I/O buffer.
Definition IOBuffer.cpp:180
virtual ~IPV4Address()
Destructor.
virtual FileSystem::Result write(IOBuffer &buffer, Size &size, const Size offset)
Set new IPV4 address.
IPV4 * m_ipv4
IPV4 object pointer.
Definition IPV4Address.h:85
IPV4Address(const u32 inode, IPV4 *ipv4)
Constructor.
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read IPV4 address.
Internet Protocol Version 4.
Definition IPV4.h:41
static const String toString(const Address address)
Convert address to string.
Definition IPV4.cpp:82
u32 Address
IP-address.
Definition IPV4.h:47
virtual FileSystem::Result getAddress(Address *address)
Get current IP address.
Definition IPV4.cpp:70
virtual FileSystem::Result setAddress(const Address *address)
Set current IP address.
Definition IPV4.cpp:76
static const Address toAddress(const char *address)
Convert string to IPV4 address.
Definition IPV4.cpp:94
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
unsigned int u32
Unsigned 32-bit number.
Definition Types.h:53
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define DEBUG(msg)
Output a debug message to standard output.
Definition Log.h:89
Result
Result code for filesystem Actions.
Definition FileSystem.h:53