FreeNOS
ICMPSocket.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 <MemoryBlock.h>
19 #include "Ethernet.h"
20 #include "IPV4.h"
21 #include "ICMP.h"
22 #include "ICMPSocket.h"
23 #include "NetworkClient.h"
24 
26  ICMP *icmp,
27  const ProcessID pid)
28  : NetworkSocket(inode, icmp->getMaximumPacketSize(), pid)
29 {
30  m_icmp = icmp;
31  m_gotReply = false;
32  MemoryBlock::set(&m_info, 0, sizeof(m_info));
33 }
34 
36 {
37 }
38 
40 {
41  return m_info.address;
42 }
43 
45  Size & size,
46  const Size offset)
47 {
48  DEBUG("");
49 
50  if (!m_gotReply)
51  {
53  }
54 
55  m_gotReply = false;
56  buffer.write(&m_reply, sizeof(m_reply));
57  size = sizeof(m_reply);
58  return FileSystem::Success;
59 }
60 
62  Size & size,
63  const Size offset)
64 {
65  DEBUG("");
66 
67  // Receive socket information first?
68  if (!m_info.address)
69  {
70  buffer.read(&m_info, sizeof(m_info));
71  return FileSystem::Success;
72  }
73  else
74  {
76  buffer.read(&header, sizeof(header));
77 
78  return m_icmp->sendPacket(m_info.address, &header, ZERO, 0);
79  }
80 }
81 
83 {
84  DEBUG("");
85 
86  return FileSystem::Success;
87 }
88 
90 {
91  DEBUG("");
92 
93  if (!m_gotReply)
94  {
96  m_gotReply = true;
97  }
98 }
MemoryBlock::copy
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Definition: MemoryBlock.cpp:36
ICMP::Header
Packet header format.
Definition: ICMP.h:64
MemoryBlock::set
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
Definition: MemoryBlock.cpp:25
ICMPSocket::process
virtual FileSystem::Result process(const NetworkQueue::Packet *pkt)
Process incoming network packet.
Definition: ICMPSocket.cpp:82
NetworkClient::SocketInfo::address
IPV4::Address address
Definition: NetworkClient.h:67
Ethernet.h
ICMPSocket::m_reply
ICMP::Header m_reply
Reply.
Definition: ICMPSocket.h:119
ProcessID
u32 ProcessID
Process Identification Number.
Definition: Types.h:140
MemoryBlock.h
FileSystem::Success
@ Success
Definition: FileSystem.h:54
ICMPSocket::write
virtual FileSystem::Result write(IOBuffer &buffer, Size &size, const Size offset)
Send ICMP request.
Definition: ICMPSocket.cpp:61
ICMPSocket::m_gotReply
bool m_gotReply
Definition: ICMPSocket.h:120
ICMP::sendPacket
FileSystem::Result sendPacket(const IPV4::Address ip, const Header *header, const void *payload, const Size payloadSize)
Send packet.
Definition: ICMP.cpp:148
ICMPSocket::setReply
void setReply(const ICMP::Header *reply)
Set ICMP reply.
Definition: ICMPSocket.cpp:89
ICMPSocket::getAddress
const IPV4::Address getAddress() const
Get associated IP host.
Definition: ICMPSocket.cpp:39
DEBUG
#define DEBUG(msg)
Output a debug message to standard output.
Definition: Log.h:89
header
SystemDescriptorHeader header
Definition: IntelACPI.h:63
NetworkSocket::m_info
NetworkClient::SocketInfo m_info
Socket connection.
Definition: NetworkSocket.h:85
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
ICMPSocket.h
ICMP.h
ICMPSocket::~ICMPSocket
virtual ~ICMPSocket()
Destructor.
Definition: ICMPSocket.cpp:35
IPV4::Address
u32 Address
IP-address.
Definition: IPV4.h:47
IPV4.h
ICMPSocket::read
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read ICMP response.
Definition: ICMPSocket.cpp:44
NetworkSocket
Network socket represents a single logical connection on a protocol.
Definition: NetworkSocket.h:37
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
FileSystem::RetryAgain
@ RetryAgain
Definition: FileSystem.h:57
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
ICMPSocket::ICMPSocket
ICMPSocket(const u32 inode, ICMP *icmp, const ProcessID pid)
Constructor.
Definition: ICMPSocket.cpp:25
NetworkClient.h
NetworkQueue::Packet
Represents a network packet.
Definition: NetworkQueue.h:50
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
ICMPSocket::m_icmp
ICMP * m_icmp
ICMP protocol instance.
Definition: ICMPSocket.h:116
ICMP
Internet Control Message Protocol (ICMP)
Definition: ICMP.h:42