Skip to content

Commit 620a5ac

Browse files
committed
cs144 finised Thank you my friend
1 parent 896a767 commit 620a5ac

File tree

7 files changed

+86
-23
lines changed

7 files changed

+86
-23
lines changed

libsponge/byte_stream.cc

+2-9
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,8 @@ string ByteStream::peek_output(const size_t len) const {
4545
size_t length = len;
4646
if (length > _buffer.size())
4747
length = _buffer.size();
48-
string tempstr;
49-
// return string().assign(_buffer.begin(), _buffer.begin() + length);
50-
for (auto it = _buffer.begin(); it < _buffer.begin() + length; ++it) {
51-
if(!_buffer.empty()){
52-
tempstr += *it;
53-
}
54-
}
55-
return tempstr;
56-
48+
return string().assign(_buffer.begin(), _buffer.begin() + length);
49+
5750
}
5851

5952
//! \param[in] len bytes will be removed from the output side of the buffer

libsponge/byte_stream.hh

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @Author: xp.Zhang
55
* @Date: 2023-07-12 15:22:53
66
* @LastEditors: xp.Zhang
7-
* @LastEditTime: 2023-09-15 10:59:01
7+
* @LastEditTime: 2023-11-09 00:01:16
88
*/
99
#ifndef SPONGE_LIBSPONGE_BYTE_STREAM_HH
1010
#define SPONGE_LIBSPONGE_BYTE_STREAM_HH
@@ -24,6 +24,7 @@
2424
class ByteStream {
2525
private:
2626
// Your code here -- add private members as necessary.
27+
2728
std::deque<char> _buffer = {};
2829
size_t _capacity = 0;
2930
size_t _readCount = 0;

libsponge/network_interface.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @Author: xp.Zhang
55
* @Date: 2023-11-06 18:00:19
66
* @LastEditors: xp.Zhang
7-
* @LastEditTime: 2023-11-07 04:36:55
7+
* @LastEditTime: 2023-11-07 22:41:46
88
*/
99
#include "network_interface.hh"
1010

@@ -62,9 +62,9 @@ void NetworkInterface::send_datagram(const InternetDatagram &dgram, const Addres
6262
//! \param[in] frame the incoming Ethernet frame
6363
optional<InternetDatagram> NetworkInterface::recv_frame(const EthernetFrame &frame) {
6464
//determine whether frame needs to be receives locally
65-
bool isArpFrame = _ethernet_address_equal(frame.header().dst, ETHERNET_BROADCAST);
65+
bool isBoardcastArpFrame = _ethernet_address_equal(frame.header().dst, ETHERNET_BROADCAST);
6666
bool isnormalFrame = _ethernet_address_equal(frame.header().dst, _ethernet_address);
67-
if(!isArpFrame && !isnormalFrame)
67+
if(!isBoardcastArpFrame && !isnormalFrame)
6868
return nullopt;
6969
else if (frame.header().type == EthernetHeader::TYPE_IPv4) // return InternetDatagram
7070
{

libsponge/router.cc

+51-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/*
2+
* @Descripttion:
3+
* @version:
4+
* @Author: xp.Zhang
5+
* @Date: 2023-11-07 23:01:50
6+
* @LastEditors: xp.Zhang
7+
* @LastEditTime: 2023-11-08 05:57:26
8+
*/
19
#include "router.hh"
210

311
#include <iostream>
@@ -28,15 +36,52 @@ void Router::add_route(const uint32_t route_prefix,
2836
const size_t interface_num) {
2937
cerr << "DEBUG: adding route " << Address::from_ipv4_numeric(route_prefix).ip() << "/" << int(prefix_length)
3038
<< " => " << (next_hop.has_value() ? next_hop->ip() : "(direct)") << " on interface " << interface_num << "\n";
31-
32-
DUMMY_CODE(route_prefix, prefix_length, next_hop, interface_num);
33-
// Your code here.
39+
RouteItem temp;
40+
temp.route_prefix = route_prefix;
41+
temp.prefix_length = prefix_length;
42+
temp.next_hop = next_hop;
43+
temp.interface_num = interface_num;
44+
_route_list.push_back(temp);
3445
}
3546

3647
//! \param[in] dgram The datagram to be routed
37-
void Router::route_one_datagram(InternetDatagram &dgram) {
38-
DUMMY_CODE(dgram);
39-
// Your code here.
48+
void Router::route_one_datagram(InternetDatagram &dgram) {
49+
bool route_found = false;
50+
RouteItem item;
51+
uint32_t dst_ip = dgram.header().dst;
52+
// Traverse the routing table
53+
for (size_t i = 0; i < _route_list.size(); ++i){
54+
if(prefix_equal(dst_ip, _route_list[i].route_prefix, _route_list[i].prefix_length)){
55+
if(!route_found ||item.prefix_length < _route_list[i].prefix_length){
56+
item = _route_list[i];
57+
route_found = true;
58+
}
59+
}
60+
}
61+
if(!route_found){
62+
return;
63+
}
64+
if (dgram.header().ttl <= 1) {
65+
return;
66+
}
67+
dgram.header().ttl--;
68+
//transfor to next hop
69+
if(item.next_hop.has_value()){
70+
_interfaces[item.interface_num].send_datagram(dgram, item.next_hop.value());
71+
}
72+
//through interface
73+
else{
74+
_interfaces[item.interface_num].send_datagram(dgram, Address::from_ipv4_numeric(dgram.header().dst));
75+
}
76+
}
77+
78+
bool Router::prefix_equal(uint32_t ip1, uint32_t ip2, uint8_t len){
79+
//determin the number of digits need to compare
80+
uint32_t offset = 0xffffffff << (32 - len);
81+
uint32_t a = ip1 & offset;
82+
uint32_t b = ip2 & offset;
83+
cout << "IPcmp " << std::hex <<a << " " << std::hex << b << " " << std::hex <<offset << endl;
84+
return a == b;
4085
}
4186

4287
void Router::route() {

libsponge/router.hh

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/*
2+
* @Descripttion:
3+
* @version:
4+
* @Author: xp.Zhang
5+
* @Date: 2023-11-07 23:01:50
6+
* @LastEditors: xp.Zhang
7+
* @LastEditTime: 2023-11-08 01:12:10
8+
*/
19
#ifndef SPONGE_LIBSPONGE_ROUTER_HH
210
#define SPONGE_LIBSPONGE_ROUTER_HH
311

@@ -41,8 +49,21 @@ class AsyncNetworkInterface : public NetworkInterface {
4149
//! \brief A router that has multiple network interfaces and
4250
//! performs longest-prefix-match routing between them.
4351
class Router {
52+
private:
53+
54+
struct RouteItem{
55+
uint32_t route_prefix = 0;
56+
uint8_t prefix_length = 0;
57+
std::optional<Address> next_hop = std::nullopt;
58+
size_t interface_num = 0;
59+
};
60+
61+
std::vector<RouteItem> _route_list{};
62+
bool prefix_equal(uint32_t ip1, uint32_t ip2, uint8_t len);
63+
4464
//! The router's collection of network interfaces
45-
std::vector<AsyncNetworkInterface> _interfaces{};
65+
std::vector<AsyncNetworkInterface>
66+
_interfaces{};
4667

4768
//! Send a single datagram from the appropriate outbound interface to the next hop,
4869
//! as specified by the route with the longest prefix_length that matches the

tests/a.out

8.66 KB
Binary file not shown.

tests/test.cc

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
* @Author: xp.Zhang
55
* @Date: 2023-07-24 10:13:04
66
* @LastEditors: xp.Zhang
7-
* @LastEditTime: 2023-07-24 14:14:22
7+
* @LastEditTime: 2023-11-08 01:05:45
88
*/
99
#include <cstdint>
10+
#include <iostream>
1011
#include <ostream>
11-
#include "wrapping_integers.hh"
12+
// #include "wrapping_integers.hh"
1213
int main()
1314
{
14-
unwrap(WrappingInt32(UINT32_MAX - 1), WrappingInt32(0), 3 * (1ul << 32));
15+
//unwrap(WrappingInt32(UINT32_MAX - 1), WrappingInt32(0), 3 * (1ul << 32));
16+
uint32_t a = 0xffffffff << 32;
17+
std::cout << a << std::endl;
1518
return 0;
1619
}

0 commit comments

Comments
 (0)