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 
32 {
33 }
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;
45  return FileSystem::Success;
46  }
47 
48  buffer.write(&addr, sizeof(addr));
49  size = sizeof(addr);
50 
51  return FileSystem::Success;
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 }
IPV4Address::m_ipv4
IPV4 * m_ipv4
IPV4 object pointer.
Definition: IPV4Address.h:85
MemoryBlock::copy
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Definition: MemoryBlock.cpp:36
IPV4Address.h
IPV4Address::~IPV4Address
virtual ~IPV4Address()
Destructor.
Definition: IPV4Address.cpp:31
MemoryBlock.h
FileSystem::Success
@ Success
Definition: FileSystem.h:54
IPV4Address::IPV4Address
IPV4Address(const u32 inode, IPV4 *ipv4)
Constructor.
Definition: IPV4Address.cpp:23
File
Represents a file present on a FileSystem.
Definition: File.h:39
Log.h
IPV4Address::read
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read IPV4 address.
Definition: IPV4Address.cpp:35
DEBUG
#define DEBUG(msg)
Output a debug message to standard output.
Definition: Log.h:89
File::m_size
Size m_size
Size of the file, in bytes.
Definition: File.h:148
IPV4::toAddress
static const Address toAddress(const char *address)
Convert string to IPV4 address.
Definition: IPV4.cpp:94
IOBuffer
Abstract Input/Output buffer.
Definition: IOBuffer.h:37
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
IPV4::setAddress
virtual FileSystem::Result setAddress(const Address *address)
Set current IP address.
Definition: IPV4.cpp:76
IPV4::Address
u32 Address
IP-address.
Definition: IPV4.h:47
IPV4
Internet Protocol Version 4.
Definition: IPV4.h:40
IPV4::toString
static const String toString(const Address address)
Convert address to string.
Definition: IPV4.cpp:82
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
IPV4::getAddress
virtual FileSystem::Result getAddress(Address *address)
Get current IP address.
Definition: IPV4.cpp:70
String.h
IOBuffer::read
FileSystem::Result read(void *buffer, const Size size, const Size offset=ZERO)
Read bytes from the I/O buffer.
Definition: IOBuffer.cpp:156
IOBuffer::write
FileSystem::Result write(const void *buffer, const Size size, const Size offset=ZERO)
Write bytes to the I/O buffer.
Definition: IOBuffer.cpp:180
IPV4Address::write
virtual FileSystem::Result write(IOBuffer &buffer, Size &size, const Size offset)
Set new IPV4 address.
Definition: IPV4Address.cpp:54