SingingCat 0
application
forwarding.c
Go to the documentation of this file.
1#include "main-header.h"
2#include "routing.h"
3#include "espressif/esp8266_flash.h"
4//#include "user_app_exe.h"
5#include "route_command.h"
6
21static byte debug = 0;
22
23void set_forward_debug(byte b) {
24 debug = b;
25}
31int forward_packet(struct command *com) {
32 struct hostroute *host;
33
34 if (!config_get_flag(CONFIG_FLAGS_ROUTING_ENABLED)) {
35 printf("[forwarding] Forwarding of packets disabled\r\n");
36 return 151;
37 }
38 com->flags = com->flags | COMFLAGS_FORWARDED;
39 if (debug) {
40 printf("[forwarding] Forwarding packet...\r\n");
41 command_print(com);
42 }
43
44 if (!is_command_valid(com)) {
45 error_com(com);
46 return 100;
47 }
48 // special case - if we are connected to server
49 // and we are to forward a packet to the server, then
50 // unconditionally push it to the server
51 if ((com->target == CLOUD_SERVER) && (get_hops_to_server() == 1)) {
52 if (debug) {
53 printf("[forwarding] unconditionally push to server\r\n");
54 }
55 com->recipient = CLOUD_SERVER;
56 com->sourcedev = SOURCE_WIFI;
57 int e = send_command(com);
58 // I think this might break because we changed the recipient
59 //send_command_reply(com, COMFLAGS_FORWARDED | COMFLAGS_SUCCESS);
60 return e;
61 }
62
63
64 // check configured routes
65 struct route *r = get_configured_route(com);
66
67 if (r == NULL) {
68 if (debug) {
69 printf("[forwarding] no route configured for target %N\r\n", com->target);
70 }
71
72 // (got no configured routes, so check leaerned routes (direct attached))
73 host = routing_find_host(com->target);
74 if (host == NULL) {
75 // no learned route either
76 send_command_fw_info(com, 1);// indicate failure forwarding
77 return 102;
78 }
79 // learned host route in 'host'
80 com->recipient = host->nexthop;
81 com->sourcedev = host->device;
82 int e = send_command(com);
83 return e;
84 }
85
86 host = routing_find_host(r->sendto);
87 if (host == NULL) {
88 printf("[forwarding] No hostroute known for target (%N)\r\n", r->sendto);
89 send_command_fw_info(com, 1); // indicate failure forwarding
90 return 101;
91 }
92
93 if (debug) {
94 printf("[forwarding] forwarding packet from %N to %N via %N on device %i\r\n", com->sender, com->target, r->sendto, r->out_device);
95 }
96 com->recipient = r->sendto;
97 com->sourcedev = r->out_device;
98 int e = send_command(com);
99
100 return e;
101}
long nexthop
here the nodeif of the intermediary hop (the proxy)
Definition: routing.h:27
int send_command(struct command *com)
send a command to another module (or broadcast)
Definition: queue.c:374
struct hostroute * routing_find_host(const long nodeid)
find route to host or NULL if none known
Definition: routing.c:315
long host
Definition: routing.h:21
byte get_hops_to_server()
how many hops to the server?
Definition: routing.c:526
void command_print(struct command *com)
prints a command in human readable format to serial console
int forward_packet(struct command *com)
a command is forwarded to target based on our hostroutes
Definition: forwarding.c:31
int send_command_fw_info(struct command *com, int err)
send a reply to a command
Definition: queue.c:533
definitions of routing table structures
long target
Definition: command.h:16
uint8_t sourcedev
Definition: command.h:17
long recipient
Definition: command.h:15
uint8_t flags
Definition: command.h:23
long sender
Definition: command.h:14