The Pedigree Project  0.1
Network.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 "pedigree/kernel/machine/Network.h"
21 #include "pedigree/kernel/utilities/String.h"
22 
23 StationInfo::StationInfo()
24  : ipv4(), ipv6(0), nIpv6Addresses(0), subnetMask(), broadcast(0xFFFFFFFF),
25  gateway(), gatewayIpv6(IpAddress::IPv6), dnsServers(0), nDnsServers(0),
26  mac(), nPackets(0), nDropped(0), nBad(0)
27 {
28 }
29 
30 StationInfo::StationInfo(const StationInfo &info)
31  : ipv4(info.ipv4), ipv6(info.ipv6), nIpv6Addresses(info.nIpv6Addresses),
32  subnetMask(info.subnetMask), broadcast(info.broadcast),
33  gateway(info.gateway), gatewayIpv6(info.gatewayIpv6),
34  dnsServers(info.dnsServers), nDnsServers(info.nDnsServers), mac(info.mac),
35  nPackets(info.nPackets), nDropped(info.nDropped), nBad(info.nBad)
36 {
37 }
38 
39 StationInfo::~StationInfo()
40 {
41 }
42 
43 Network::Network() : m_StationInfo()
44 {
45  m_SpecificType = "Generic Network Device";
46 }
47 
48 Network::Network(Network *pDev) : Device(pDev), m_StationInfo()
49 {
50 }
51 
52 Network::~Network()
53 {
54 }
55 
57 {
58  return Device::Network;
59 }
60 
62 {
63  str = "Generic Network Device";
64 }
65 
67 {
68  str = "Generic Network Device";
69 }
70 
72 {
73  return false; // failed by default
74 }
75 
77 {
78  static StationInfo info;
79  return info; // not to be trusted
80 }
81 
83 {
84  return true;
85 }
86 
87 uint32_t Network::convertToIpv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
88 {
89  return a | (b << 8) | (c << 16) | (d << 24);
90 }
91 
93  uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f, uint8_t g,
94  uint8_t h, uint8_t i, uint8_t j, uint8_t k, uint8_t l, uint8_t m, uint8_t n,
95  uint8_t o, uint8_t p)
96 {
97  uint8_t temp[16] = {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p};
98  IpAddress ipv6(temp);
99  return temp;
100 }
101 
102 uint16_t Network::calculateChecksum(uintptr_t buffer, size_t nBytes)
103 {
104  uint32_t sum = 0;
105  uint16_t *data = reinterpret_cast<uint16_t *>(buffer);
106 
107  while (nBytes > 1)
108  {
109  sum += *data++;
110  nBytes -= sizeof(uint16_t);
111  }
112 
113  // odd bytes
114  if (nBytes > 0)
115  {
116  uint8_t *data8 = reinterpret_cast<uint8_t *>(data);
117  sum += *data8;
118  }
119 
120  // fold to 16 bits
121  while (sum >> 16)
122  sum = (sum & 0xFFFF) + (sum >> 16);
123 
124  uint16_t ret = static_cast<uint16_t>(~sum);
125  return ret;
126 }
127 
129 {
130  m_StationInfo.nPackets++;
131 }
132 
134 {
135  m_StationInfo.nDropped++;
136 }
137 
139 {
140  m_StationInfo.nBad++;
141 }
virtual void droppedPacket()
Called when a packet is dropped by the system.
Definition: Network.cc:133
virtual bool setStationInfo(const StationInfo &info)
Definition: Network.cc:71
static EXPORTED_PUBLIC IpAddress convertToIpv6(uint8_t a, uint8_t b=0, uint8_t c=0, uint8_t d=0, uint8_t e=0, uint8_t f=0, uint8_t g=0, uint8_t h=0, uint8_t i=0, uint8_t j=0, uint8_t k=0, uint8_t l=0, uint8_t m=0, uint8_t n=0, uint8_t o=0, uint8_t p=0)
Definition: Network.cc:92
virtual const StationInfo & getStationInfo()
Definition: Network.cc:76
static EXPORTED_PUBLIC uint32_t convertToIpv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
Definition: Network.cc:87
A communication device.
Definition: Device.h:59
size_t nBad
Number of packets dropped by the filter.
Definition: String.h:49
Definition: Device.h:43
static EXPORTED_PUBLIC uint16_t calculateChecksum(uintptr_t buffer, size_t nBytes)
Definition: Network.cc:102
size_t nDropped
Number of packets passed through the interface.
virtual void dump(String &str)
Definition: Network.cc:66
virtual void badPacket()
Definition: Network.cc:138
virtual void getName(String &str)
Definition: Network.cc:61
virtual Type getType()
Definition: Network.cc:56
virtual bool isConnected()
Definition: Network.cc:82
Type
Definition: Device.h:50
virtual void gotPacket()
Definition: Network.cc:128