1#include "main-header.h"
3#include "route_command.h"
5static void route_command_list_routes(
struct command *com);
6static void route_command_clear_routes(
struct command *com);
7static void route_command_add_route(
struct command *com);
8static void route_command_remove_route(
struct command *com);
9static struct route *find_route(
long target,
long longsource,
byte device,
byte needsonline);
10#define MAX_ROUTING_ENTRIES 30
11static struct route routes[MAX_ROUTING_ENTRIES];
14void routing_command_debug(
byte b) {
18void routing_command_init() {
19 memset(&routes, 0,
sizeof(routes));
33void route_command(
struct command *com) {
34 long nid = ascii_parse_hex((
const byte *)
get_arg((
struct command *)com, 0), 8, NULL);
37 route_command_list_routes(com);
39 }
else if (nid == 2) {
40 route_command_add_route(com);
42 }
else if (nid == 3) {
43 route_command_remove_route(com);
45 }
else if (nid == 4) {
46 route_command_clear_routes(com);
52static void route_command_list_routes(
struct command *com) {
55static void route_command_clear_routes(
struct command *com) {
56 memset(&routes, 0,
sizeof(routes));
59int parse_route_command(
struct command *com,
struct route *r) {
63 r->target = ascii_parse_hex((
const uint8_t *)
get_arg(com, 1), strlen(
get_arg(com, 1)), NULL);
64 r->source = ascii_parse_hex((
const uint8_t *)
get_arg(com, 2), strlen(
get_arg(com, 2)), NULL);
65 r->sendto = ascii_parse_hex((
const uint8_t *)
get_arg(com, 3), strlen(
get_arg(com, 3)), NULL);
66 r->out_device = ascii_parse_hex((
const uint8_t *)
get_arg(com, 4), strlen(
get_arg(com, 4)), NULL);
67 r->match_device = ascii_parse_hex((
const uint8_t *)
get_arg(com, 5), strlen(
get_arg(com, 5)), NULL);
72static struct route *find_free_entry() {
75 for (i = 0; i < MAX_ROUTING_ENTRIES; i++) {
76 if (routes[i].used == 0) {
81 printf(
"[route_command] no free routing table slot available\r\n");
86static void route_command_add_route(
struct command *com) {
87 struct route *r = find_free_entry();
92 int err = parse_route_command(com, r);
98 struct route *nr = find_route(r->target, r->source, r->match_device, 0);
103 printf(
"[route_command] route added to %N via %N on %i\r\n", r->target, r->sendto, r->out_device);
106 nr->target = r->target;
107 nr->match_device = r->match_device;
108 nr->out_device = r->out_device;
109 nr->source = r->source;
110 nr->sendto = r->sendto;
111 printf(
"[route_command] route updated to %N via %N on %i\r\n", nr->target, nr->sendto, nr->out_device);
115static void route_command_remove_route(
struct command *com) {
119static struct route *find_route(
long target,
long source,
byte device,
byte needsonline) {
122 for (i = 0; i < MAX_ROUTING_ENTRIES; i++) {
123 struct route *r = &routes[i];
128 (r->target == target) &&
129 (r->source == source) &&
130 (r->match_device == device)) {
141struct route *get_configured_route(
struct command *com) {
146 printf(
"[route_command] route to %N via %N on %i\r\n", com->
target, r->sendto, r->out_device);
154 printf(
"[route_command] route to %N via %N on %i\r\n", com->
target, r->sendto, r->out_device);
162 printf(
"[route_command] route to %N via %N on %i\r\n", com->
target, r->sendto, r->out_device);
167 r = find_route(com->
target, 0, 0, 1);
170 printf(
"[route_command] route to %N via %N on %i\r\n", com->
target, r->sendto, r->out_device);
176 printf(
"[route_command] no route to %N\r\n", com->
target);
181void print_forwarding_table() {
184 printf(
"target | source | match_device | nexthop | out_device\r\n");
185 for (i = 0; i < MAX_ROUTING_ENTRIES; i++) {
186 struct route *r = &routes[i];
190 printf(
"%N %N %i %N %i\r\n", r->target, r->source, r->match_device, r->sendto, r->out_device);
192 printf(
"Routing list complete\r\n");
int send_command_reply(struct command *com, byte flags)
send a reply to a command
int is_device_online(int device)
return 1 if the specified device is online and routing
const char * get_arg(const struct command *com, int index)
given an argument by index[0..n], will return a pointer to the bytearray (excluding the fieldtype) th...
definitions of routing table structures