-
Notifications
You must be signed in to change notification settings - Fork 0
/
des-hello.c
174 lines (140 loc) · 6.2 KB
/
des-hello.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/******************************************************************************
Copyright 2011, The DES-SERT Team, Freie Universitaet Berlin (FUB).
All rights reserved.
These sources were originally developed by Philipp Schmidt
at Freie Universitaet Berlin (http://www.fu-berlin.de/),
Computer Systems and Telematics / Distributed, Embedded Systems (DES) group
(http://cst.mi.fu-berlin.de/, http://www.des-testbed.net/)
------------------------------------------------------------------------------
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see http://www.gnu.org/licenses/ .
------------------------------------------------------------------------------
For further information and questions please use the web site
http://www.des-testbed.net/
*******************************************************************************/
#include <dessert.h>
#include <string.h>
#include <time.h>
#define HELLO_EXT_TYPE (DESSERT_EXT_USER + 4)
mac_addr *hwaddr_follow = NULL;
dessert_per_result_t periodic_send_hello(void *data, struct timeval *scheduled, struct timeval *interval) {
dessert_msg_t* hello_msg;
dessert_ext_t* ext;
// create new HELLO message with hello_ext.
dessert_msg_new(&hello_msg);
hello_msg->ttl = 1;
dessert_msg_addext(hello_msg, &ext, HELLO_EXT_TYPE, 0);
if(dessert_meshsend(hello_msg, NULL) != DESSERT_OK) {
fputs("FAIL\n", stderr);
}
dessert_msg_destroy(hello_msg);
return DESSERT_PER_KEEP;
}
dessert_cb_result handle_ttl(dessert_msg_t* msg, uint32_t len, dessert_msg_proc_t *proc, dessert_meshif_t *iface, dessert_frameid_t id) {
if(msg->ttl == 0)
return DESSERT_MSG_DROP;
else
return DESSERT_MSG_KEEP;
}
dessert_cb_result handle_hello(dessert_msg_t* msg, uint32_t len, dessert_msg_proc_t *proc, dessert_meshif_t *iface, dessert_frameid_t id){
dessert_ext_t* hallo_ext;
if (dessert_msg_getext(msg, &hallo_ext, HELLO_EXT_TYPE, 0) != 0) {
msg->ttl--;
if (memcmp(iface->hwaddr, msg->l2h.ether_dhost, ETH_ALEN) == 0) {
dessert_debug("received hello resp from " MAC, EXPLODE_ARRAY6(msg->l2h.ether_shost));
}
return DESSERT_MSG_DROP;
}
return DESSERT_MSG_KEEP;
}
/**
* Send dessert message via all registered interfaces.
**/
dessert_cb_result toMesh(dessert_msg_t* msg, uint32_t len, dessert_msg_proc_t *proc, dessert_sysif_t *tunif, dessert_frameid_t id) {
dessert_meshsend(msg, NULL);
return DESSERT_MSG_DROP;
}
dessert_per_result_t periodic_report_follow(void *data, struct timeval *scheduled, struct timeval *interval) {
return dessert_log_monitored_neighbour(*hwaddr_follow);
}
static int cli_cmd_follow(struct cli_def *cli, char *command, char *argv[], int argc) {
if(*hwaddr_follow == NULL) {
hwaddr_follow = calloc(1, sizeof(hwaddr_follow));
} else {
cli_print(cli, "FOLLOW - already following MAC [" MAC "]", EXPLODE_ARRAY6((*hwaddr_follow)));
return CLI_ERROR;
}
struct timeval follow_interval_t;
follow_interval_t.tv_sec = 1;
follow_interval_t.tv_usec = 0;
int valid = -1;
if(argc >= 1) {
valid = dessert_parse_mac(argv[0], hwaddr_follow);
} else {
cli_print(cli, "FOLLOW - no MAC Address given...");
return CLI_ERROR;
}
if(valid >= 0) {
cli_print(cli, "FOLLOW - MAC Address: [%s]", argv[0]);
} else {
cli_print(cli, "FOLLOW - MAC Address not valid: [%s]", argv[0]);
return CLI_ERROR;
}
if(argc >= 3) {
follow_interval_t.tv_sec = atoi(argv[1]);
follow_interval_t.tv_usec = atoi(argv[2]);
cli_print(cli, "FOLLOW - set interval: %lldsec + %lldusec", (long long) follow_interval_t.tv_sec, (long long) follow_interval_t.tv_usec);
} else {
cli_print(cli, "FOLLOW - no interval...taking default: %lldsec + %lldusec", (long long) follow_interval_t.tv_sec, (long long) follow_interval_t.tv_usec);
}
cli_print(cli, "FOLLOW - writing RSSI-info for neighbour [%s] in log file", argv[0]);
dessert_periodic_add(periodic_report_follow, NULL, NULL, &follow_interval_t);
return CLI_OK;
}
int main(int argc, char *argv[]) {
/* initialize daemon with correct parameters */
FILE *cfg = NULL;
if ((argc == 2) && (strcmp(argv[1], "-nondaemonize") == 0)) {
dessert_info("starting HELLO in non daemonize mode");
dessert_init("DESX", 0xEE, DESSERT_OPT_NODAEMONIZE);
char cfg_file_name[] = "./des-hello.cli";
cfg = fopen(cfg_file_name, "r");
if (cfg == NULL) {
printf("Config file '%s' not found. Exit ...\n", cfg_file_name);
return EXIT_FAILURE;
}
} else {
dessert_info("starting HELLO in daemonize mode");
cfg = dessert_cli_get_cfg(argc, argv);
dessert_init("DESX", 0xEE, DESSERT_OPT_DAEMONIZE);
}
/* initalize logging */
dessert_logcfg(DESSERT_LOG_STDERR | DESSERT_LOG_FILE);
/* cli initialization */
cli_register_command(dessert_cli, dessert_cli_cfg_iface, "sys", dessert_cli_cmd_addsysif, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "initialize sys interface");
cli_register_command(dessert_cli, dessert_cli_cfg_iface, "mesh", dessert_cli_cmd_addmeshif, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "initialize mesh interface");
cli_register_command(dessert_cli, NULL, "follow", cli_cmd_follow, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "configure a neighbour to follow -> output to logfile");
/* registering callbacks */
dessert_meshrxcb_add(dessert_msg_ifaceflags_cb, 20);
dessert_meshrxcb_add(handle_ttl, 30);
dessert_meshrxcb_add(handle_hello, 40);
dessert_sysrxcb_add(toMesh, 100);
/* registering periodic tasks */
struct timeval hello_interval_t;
hello_interval_t.tv_sec = 1;
hello_interval_t.tv_usec = 0;
dessert_periodic_add(periodic_send_hello, NULL, NULL, &hello_interval_t);
dessert_info("starting periodic send hello - interval set to %d.%ds", hello_interval_t.tv_sec, hello_interval_t.tv_usec);
/* running cli & daemon */
cli_file(dessert_cli, cfg, PRIVILEGE_PRIVILEGED, MODE_CONFIG);
dessert_cli_run();
dessert_run();
return EXIT_SUCCESS;
}