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