Skip to content

Commit 91833e5

Browse files
committed
Clean up
1 parent 600f853 commit 91833e5

File tree

5 files changed

+75
-97
lines changed

5 files changed

+75
-97
lines changed

src/main.cpp

-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <stdlib.h>
22
#include <syslog.h>
33
#include "configInterface.h"
4-
#include <iostream>
54

65
bool dual_tor_sock = false;
76

@@ -24,25 +23,6 @@ int main(int argc, char *argv[]) {
2423
}
2524
}
2625
try {
27-
/*
28-
unsigned char udp[] = {
29-
0x02, 0x22, 0x02, 0x23, 0x00, 0x0c, 0xbd, 0xfd, 0x01, 0x00,
30-
0x30, 0x39 };
31-
char *ptr = (char *)udp;
32-
static uint8_t buffer[4096];
33-
auto current_buffer_position = buffer;
34-
35-
const uint8_t *current_position = (uint8_t *)ptr;
36-
const uint8_t *tmp = NULL;
37-
38-
auto udp_header = parse_udp(current_position, &tmp);
39-
current_position = tmp;
40-
auto dhcp_message_length = ntohs(udp_header->len) - sizeof(udphdr);
41-
relay_forward(current_buffer_position, parse_dhcpv6_hdr(current_position), dhcp_message_length);
42-
*/
43-
//printf( " |-Destination Address : %s \n", dst );
44-
//printf( " |-Source Address : %.2X:%.2X:%.2X:%.2X:%.2X:%.2X \n", ether_header->ether_shost[0] , ether_header->ether_shost[1] , ether_header->ether_shost[2] , ether_header->ether_shost[3] , ether_header->ether_shost[4] , ether_header->ether_shost[5] );
45-
//printf( " |-Protocol : %s \n",dst);
4626
std::vector<relay_config> vlans;
4727
initialize_swss(&vlans);
4828
loop_relay(&vlans);

src/relay.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ struct event_base *base;
1919
struct event *ev_sigint;
2020
struct event *ev_sigterm;
2121
static std::string vlan_member = "VLAN_MEMBER|";
22-
2322
static std::string counter_table = "DHCPv6_COUNTER_TABLE|";
2423
struct database redis_db;
2524

@@ -579,6 +578,7 @@ void relay_relay_forw(int sock, const uint8_t *msg, int32_t len, const ip6_hdr *
579578
void callback(evutil_socket_t fd, short event, void *arg) {
580579
struct relay_config *config = (struct relay_config *)arg;
581580
static uint8_t message_buffer[4096];
581+
std::string counterVlan = counter_table;
582582
int32_t len = recv(config->filter, message_buffer, 4096, 0);
583583
if (len <= 0) {
584584
syslog(LOG_WARNING, "recv: Failed to receive data at filter socket: %s\n", strerror(errno));
@@ -614,9 +614,7 @@ void callback(evutil_socket_t fd, short event, void *arg) {
614614
current_position = tmp;
615615

616616
auto msg = parse_dhcpv6_hdr(current_position);
617-
counters[msg->msg_type]++;
618-
std::string counterVlan = counter_table;
619-
update_counter(config->db, counterVlan.append(config->interface), msg->msg_type);
617+
auto option_position = current_position + sizeof(struct dhcpv6_msg);
620618

621619
switch (msg->msg_type) {
622620
case DHCPv6_MESSAGE_TYPE_RELAY_FORW:
@@ -650,7 +648,7 @@ void callback(evutil_socket_t fd, short event, void *arg) {
650648
}
651649
default:
652650
{
653-
relay_client(config->local_sock, current_position, ntohs(udp_header->len) - sizeof(udphdr), ip_header, ether_header, config);
651+
syslog(LOG_WARNING, "DHCPv6 client message received was not relayed\n");
654652
break;
655653
}
656654
}

src/relay.h

+36-17
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
#include <vector>
1313
#include <event2/util.h>
1414
#include "dbconnector.h"
15+
#include "table.h"
1516
#include "sender.h"
1617

1718
#define PACKED __attribute__ ((packed))
1819

1920
#define RELAY_PORT 547
2021
#define CLIENT_PORT 546
2122
#define HOP_LIMIT 8 //HOP_LIMIT reduced from 32 to 8 as stated in RFC8415
23+
#define DHCPv6_OPTION_LIMIT 56 // DHCPv6 option code greater than 56 are currently unassigned
2224

2325
#define lengthof(A) (sizeof (A) / sizeof (A)[0])
2426

@@ -41,8 +43,11 @@ typedef enum
4143
DHCPv6_MESSAGE_TYPE_REPLY = 7,
4244
DHCPv6_MESSAGE_TYPE_RELEASE = 8,
4345
DHCPv6_MESSAGE_TYPE_DECLINE = 9,
46+
DHCPv6_MESSAGE_TYPE_RECONFIGURE = 10,
47+
DHCPv6_MESSAGE_TYPE_INFORMATION_REQUEST = 11,
4448
DHCPv6_MESSAGE_TYPE_RELAY_FORW = 12,
4549
DHCPv6_MESSAGE_TYPE_RELAY_REPL = 13,
50+
DHCPv6_MESSAGE_TYPE_MALFORMED = 14,
4651

4752
DHCPv6_MESSAGE_TYPE_COUNT
4853
} dhcp_message_type_t;
@@ -64,14 +69,14 @@ struct relay_config {
6469
struct database {
6570
std::shared_ptr<swss::DBConnector> config_db;
6671
std::shared_ptr<swss::Table> muxTable;
67-
std::shared_ptr<swss::Table> counterTable;
6872
};
6973

7074

7175
/* DHCPv6 messages and options */
7276

7377
struct dhcpv6_msg {
7478
uint8_t msg_type;
79+
uint8_t xid[3];
7580
};
7681

7782
struct PACKED dhcpv6_relay_msg {
@@ -100,7 +105,7 @@ struct interface_id_option {
100105
};
101106

102107
/**
103-
* @code sock_open(const struct sock_fprog *fprog);
108+
* @code sock_open(int ifindex, const struct sock_fprog *fprog);
104109
*
105110
* @brief prepare L2 socket to attach to "udp and port 547" filter
106111
*
@@ -245,11 +250,11 @@ void signal_callback(evutil_socket_t fd, short event, void *arg);
245250
void shutdown();
246251

247252
/**
248-
* @code void initialize_counter(std::shared_ptr<swss::Table> state_db, std::string counterVlan);
253+
* @code initialize_counter(std::shared_ptr<swss::DBConnector> state_db, std::string counterVlan);
249254
*
250255
* @brief initialize the counter by each Vlan
251256
*
252-
* @param std::shared_ptr<swss::Table> state_db state_db connector
257+
* @param std::shared_ptr<swss::DBConnector> state_db state_db connector pointer
253258
* @param counterVlan counter table with interface name
254259
*
255260
* @return none
@@ -355,32 +360,46 @@ const struct dhcpv6_relay_msg *parse_dhcpv6_relay(const uint8_t *buffer);
355360
const struct dhcpv6_option *parse_dhcpv6_opt(const uint8_t *buffer, const uint8_t **out_end);
356361

357362
/**
358-
* @code void process_sent_msg(relay_config *config, uint8_t msg_type);
363+
* @code callback(evutil_socket_t fd, short event, void *arg);
359364
*
360-
* @brief process packet after successfully sent udp
361-
362-
* @param relay_config *config pointer to relay_config
363-
* @param uint8_t msg_type message type of dhcpv6 option of relayed message
364-
*
365-
* @return Update counter / syslog
365+
* @brief callback for libevent that is called everytime data is received at the filter socket
366+
*
367+
* @param fd filter socket
368+
* @param event libevent triggered event
369+
* @param arg callback argument provided by user
370+
*
371+
* @return none
366372
*/
367-
void process_sent_msg(relay_config *config, uint8_t msg_type);
373+
void callback(evutil_socket_t fd, short event, void *arg);
368374

369375
/**
370-
* @code callback(evutil_socket_t fd, short event, void *arg);
376+
* @code callback_dual_tor(evutil_socket_t fd, short event, void *arg);
377+
*
378+
* @brief callback for libevent that is called everytime data is received at the filter socket with dual tor option enabled
379+
*
380+
* @param fd filter socket
381+
* @param event libevent triggered event
382+
* @param arg callback argument provided by user
383+
*
384+
* @return none
385+
*/
386+
void callback_dual_tor(evutil_socket_t fd, short event, void *arg);
387+
388+
/**
389+
* @code void server_callback(evutil_socket_t fd, short event, void *arg);
371390
*
372-
* @brief callback for libevent that is called everytime data is received at the filter socket
391+
* @brief callback for libevent that is called everytime data is received at the server socket
373392
*
374393
* @param fd filter socket
375394
* @param event libevent triggered event
376395
* @param arg callback argument provided by user
377396
*
378397
* @return none
379398
*/
380-
void callback(evutil_socket_t fd, short event, void *arg);
399+
void server_callback(evutil_socket_t fd, short event, void *arg);
381400

382401
/**
383-
* @code void server_callback(evutil_socket_t fd, short event, void *arg);
402+
* @code void server_callback_dual_tor(evutil_socket_t fd, short event, void *arg);
384403
*
385404
* @brief callback for libevent that is called everytime data is received at the server socket
386405
*
@@ -390,4 +409,4 @@ void callback(evutil_socket_t fd, short event, void *arg);
390409
*
391410
* @return none
392411
*/
393-
void server_callback(evutil_socket_t fd, short event, void *arg);
412+
void server_callback_dual_tor(evutil_socket_t fd, short event, void *arg);

test/MockRelay.cpp

+36-40
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "MockRelay.h"
1010

11+
bool dual_tor_sock = false;
1112
extern struct event_base *base;
1213
extern struct event *ev_sigint;
1314
extern struct event *ev_sigterm;
@@ -191,15 +192,13 @@ TEST(sock, sock_open)
191192
lengthof(ether_relay_filter),
192193
ether_relay_filter
193194
};
194-
int index = if_nametoindex("lo");
195-
EXPECT_GE(sock_open(index, &ether_relay_fprog), 0);
195+
EXPECT_GE(sock_open(&ether_relay_fprog), 0);
196196
}
197197

198198
TEST(sock, sock_open_invalid_filter)
199199
{
200200
const struct sock_fprog ether_relay_fprog = {0,{}};
201-
int index = if_nametoindex("lo");
202-
EXPECT_EQ(sock_open(index, &ether_relay_fprog), -1);
201+
EXPECT_EQ(sock_open(&ether_relay_fprog), -1);
203202
}
204203

205204
TEST(sock, sock_open_invalid_ifindex_zero)
@@ -211,7 +210,7 @@ TEST(sock, sock_open_invalid_ifindex_zero)
211210
lengthof(ether_relay_filter),
212211
ether_relay_filter
213212
};
214-
EXPECT_EQ(sock_open(0, &ether_relay_fprog), -1);
213+
EXPECT_EQ(sock_open(&ether_relay_fprog), -1);
215214
}
216215

217216
TEST(sock, sock_open_invalid_ifindex)
@@ -223,16 +222,16 @@ TEST(sock, sock_open_invalid_ifindex)
223222
lengthof(ether_relay_filter),
224223
ether_relay_filter
225224
};
226-
EXPECT_EQ(sock_open(42384239, &ether_relay_fprog), -1);
225+
EXPECT_EQ(sock_open(&ether_relay_fprog), -1);
227226
}
228227

229228
TEST(sock, prepare_socket)
230229
{
231230
relay_config *config = new struct relay_config;;
232-
int local_sock = -1, server_sock = -1, index = 1;
231+
int local_sock = -1, server_sock = -1;
233232
int socket_type = -1;
234233
socklen_t socket_type_len = sizeof(socket_type);
235-
prepare_socket(&local_sock, &server_sock, config, index);
234+
prepare_socket(&local_sock, &server_sock, config);
236235
EXPECT_GE(local_sock, 0);
237236
EXPECT_GE(server_sock, 0);
238237
EXPECT_EQ(0, getsockopt(local_sock, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len));
@@ -278,8 +277,8 @@ TEST(prepareConfig, prepare_relay_config)
278277
config.servers.push_back("fc02:2000::2");
279278

280279
config.interface = "Vlan1000";
281-
swss::DBConnector stateDb("STATE_DB", 0, true);
282-
config.db = &stateDb;
280+
std::shared_ptr<swss::DBConnector> state_db = std::make_shared<swss::DBConnector> ("STATE_DB", 0);
281+
config.state_db = state_db;
283282

284283
prepare_relay_config(&config, &local_sock, filter);
285284

@@ -297,28 +296,28 @@ TEST(prepareConfig, prepare_relay_config)
297296

298297
TEST(counter, initialize_counter)
299298
{
300-
swss::DBConnector stateDb("STATE_DB", 0, true);
301-
initialize_counter(&stateDb, "DHCPv6_COUNTER_TABLE|Vlan1000");
302-
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Unknown"));
303-
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Solicit"));
304-
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Advertise"));
305-
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Request"));
306-
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Confirm"));
307-
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Renew"));
308-
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Rebind"));
309-
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Reply"));
310-
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Release"));
311-
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Decline"));
312-
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Relay-Forward"));
313-
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Relay-Reply"));
299+
std::shared_ptr<swss::DBConnector> state_db = std::make_shared<swss::DBConnector> ("STATE_DB", 0);
300+
initialize_counter(state_db, "DHCPv6_COUNTER_TABLE|Vlan1000");
301+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Unknown"));
302+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Solicit"));
303+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Advertise"));
304+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Request"));
305+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Confirm"));
306+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Renew"));
307+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Rebind"));
308+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Reply"));
309+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Release"));
310+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Decline"));
311+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Relay-Forward"));
312+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Relay-Reply"));
314313
}
315314

316315
TEST(counter, update_counter)
317316
{
318-
swss::DBConnector stateDb("STATE_DB", 0, true);
319-
stateDb.hset("DHCPv6_COUNTER_TABLE|Vlan1000", "Solicit", "1");
320-
update_counter(&stateDb, "DHCPv6_COUNTER_TABLE|Vlan1000", 1);
321-
std::shared_ptr<std::string> output = stateDb.hget("DHCPv6_COUNTER_TABLE|Vlan1000", "Solicit");
317+
std::shared_ptr<swss::DBConnector> state_db = std::make_shared<swss::DBConnector> ("STATE_DB", 0);
318+
state_db->hset("DHCPv6_COUNTER_TABLE|Vlan1000", "Solicit", "1");
319+
update_counter(state_db, "DHCPv6_COUNTER_TABLE|Vlan1000", 1);
320+
std::shared_ptr<std::string> output = state_db->hget("DHCPv6_COUNTER_TABLE|Vlan1000", "Solicit");
322321
std::string *ptr = output.get();
323322
EXPECT_EQ(*ptr, "0");
324323
}
@@ -352,8 +351,8 @@ TEST(relay, relay_client)
352351
tmp.sin6_scope_id = 0;
353352
config.servers_sock.push_back(tmp);
354353
}
355-
swss::DBConnector stateDb("STATE_DB", 0, true);
356-
config.db = &stateDb;
354+
std::shared_ptr<swss::DBConnector> state_db = std::make_shared<swss::DBConnector> ("STATE_DB", 0);
355+
config.state_db = state_db;
357356

358357
struct ether_header ether_hdr;
359358
ether_hdr.ether_shost[0] = 0x5a;
@@ -440,10 +439,8 @@ TEST(relay, relay_relay_forw) {
440439
tmp.sin6_scope_id = 0;
441440
config.servers_sock.push_back(tmp);
442441
}
443-
swss::DBConnector stateDb("STATE_DB", 0, true);
444-
config.db = &stateDb;
445-
446-
442+
std::shared_ptr<swss::DBConnector> state_db = std::make_shared<swss::DBConnector> ("STATE_DB", 0);
443+
config.state_db = state_db;
447444

448445
ip6_hdr ip_hdr;
449446
std::string s_addr = "2000::3";
@@ -505,8 +502,8 @@ TEST(relay, relay_relay_reply)
505502
config.servers.push_back("fc02:2000::2");
506503

507504
config.interface = "Vlan1000";
508-
swss::DBConnector stateDb("STATE_DB", 0, true);
509-
config.db = &stateDb;
505+
std::shared_ptr<swss::DBConnector> state_db = std::make_shared<swss::DBConnector> ("STATE_DB", 0);
506+
config.state_db = state_db;
510507

511508
int local_sock = 1;
512509
int filter = 1;
@@ -564,13 +561,12 @@ TEST(relay, callback) {
564561
config.servers.push_back("fc02:2000::2");
565562

566563
config.interface = "Vlan1000";
567-
swss::DBConnector stateDb("STATE_DB", 0, true);
568-
config.db = &stateDb;
564+
std::shared_ptr<swss::DBConnector> state_db = std::make_shared<swss::DBConnector> ("STATE_DB", 0);
565+
config.state_db = state_db;
569566

570567
int local_sock = 1;
571568
int filter = 1;
572-
int index = if_nametoindex("lo");
573-
config.filter = sock_open(index, &ether_relay_fprog);
569+
config.filter = sock_open(&ether_relay_fprog);
574570
EXPECT_GE(config.filter, 0);
575571
prepare_relay_config(&config, &local_sock, filter);
576572

test/MockRelay.h

-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,2 @@
11
#include "../src/relay.h"
22
#include "mock_send.h"
3-
/* #include <cstring>
4-
5-
uint8_t sender_buffer[4096];
6-
int32_t valid_byte_count;
7-
int last_used_sock;
8-
sockaddr_in6 last_target;
9-
10-
bool send_udp(int sock, uint8_t *buffer, struct sockaddr_in6 target, uint32_t n) {
11-
12-
last_used_sock = sock;
13-
valid_byte_count = n;
14-
memcpy(sender_buffer, buffer, n);
15-
last_target = target;
16-
return true;
17-
} */

0 commit comments

Comments
 (0)