The Pedigree Project  0.1
Lo.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "Lo.h"
21 #include "modules/system/network-stack/NetworkStack.h"
22 #include "pedigree/kernel/Log.h"
23 #include "pedigree/kernel/machine/Machine.h"
24 #include "pedigree/kernel/machine/Network.h"
25 #include "pedigree/kernel/processor/Processor.h"
26 
27 #include "modules/system/network-stack/RoutingTable.h"
28 
29 Loopback Loopback::m_Instance;
30 
31 static uint8_t g_LocalIpv6[16] = {0, 0, 0, 0, 0, 0, 0, 0,
32  0, 0, 0, 0, 0, 0, 0, 1};
33 
34 Loopback::Loopback() : Network()
35 {
36  setSpecificType(String("Loopback-card"));
39 
40  m_StationInfo.ipv4.setIp(Network::convertToIpv4(127, 0, 0, 1));
41  m_StationInfo.subnetMask.setIp(Network::convertToIpv4(255, 0, 0, 0));
42  m_StationInfo.mac.setMac(static_cast<uint8_t>(0));
43 
44  m_StationInfo.ipv6 = new IpAddress(g_LocalIpv6);
45  m_StationInfo.ipv6->setIpv6Prefix(128);
46  m_StationInfo.nIpv6Addresses = 1;
47 }
48 
49 Loopback::Loopback(Network *pDev) : Network(pDev)
50 {
51  setSpecificType(String("Loopback-card"));
54 
55  m_StationInfo.ipv4.setIp(Network::convertToIpv4(127, 0, 0, 1));
56  m_StationInfo.subnetMask.setIp(Network::convertToIpv4(255, 0, 0, 0));
57  m_StationInfo.mac.setMac(static_cast<uint8_t>(0));
58 
59  m_StationInfo.ipv6 = new IpAddress(g_LocalIpv6);
60  m_StationInfo.ipv6->setIpv6Prefix(128);
61  m_StationInfo.nIpv6Addresses = 1;
62 }
63 
64 Loopback::~Loopback()
65 {
66 }
67 
68 bool Loopback::send(size_t nBytes, uintptr_t buffer)
69 {
70  if (nBytes > 0xffff)
71  {
72  ERROR("Loopback: Attempt to send a packet with size > 64 KB");
73  return false;
74  }
75  NetworkStack::instance().receive(nBytes, buffer, this, 0);
76  return true;
77 }
78 
79 bool Loopback::setStationInfo(StationInfo info)
80 {
81  // Nothing here is modifiable
82  return true;
83 }
84 
86 {
87  return m_StationInfo;
88 }
Definition: Lo.h:34
virtual bool send(size_t nBytes, uintptr_t buffer)
Definition: Lo.cc:68
static EXPORTED_PUBLIC uint32_t convertToIpv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
Definition: Network.cc:87
Definition: String.h:49
void receive(size_t nBytes, uintptr_t packet, Network *pCard, uint32_t offset)
static NetworkStack & instance()
Definition: NetworkStack.h:47
void registerDevice(Network *pDevice)
virtual StationInfo getStationInfo()
Definition: Lo.cc:85
#define ERROR(text)
Definition: Log.h:82
void setLoopback(Network *pCard)
Definition: NetworkStack.h:69