main: add a command line option for dumping remote node data
[project/usteer.git] / main.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
14 *
15 * Copyright (C) 2020 embedd.ch
16 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
17 * Copyright (C) 2020 John Crispin <john@phrozen.org>
18 */
19
20 #include <unistd.h>
21 #include <stdarg.h>
22 #include <syslog.h>
23
24 #include <libubox/blobmsg_json.h>
25
26 #include "usteer.h"
27 #include "event.h"
28 #include "node.h"
29
30 struct ubus_context *ubus_ctx;
31 struct usteer_config config = {};
32 struct blob_attr *host_info_blob;
33 uint64_t current_time;
34 static int dump_time;
35
36 LIST_HEAD(node_handlers);
37
38 const char * const event_types[__EVENT_TYPE_MAX] = {
39 [EVENT_TYPE_PROBE] = "probe",
40 [EVENT_TYPE_AUTH] = "auth",
41 [EVENT_TYPE_ASSOC] = "assoc",
42 };
43
44 void log_msg(char *msg)
45 {
46 if (config.syslog)
47 syslog(LOG_INFO, "%s\n", msg);
48 else
49 fprintf(stderr, "%s\n", msg);
50 }
51
52 void debug_msg(int level, const char *func, int line, const char *format, ...)
53 {
54 va_list ap;
55
56 if (config.debug_level < level)
57 return;
58
59 if (!config.syslog)
60 fprintf(stderr, "[%s:%d] ", func, line);
61
62 va_start(ap, format);
63 if (config.syslog)
64 vsyslog(level >= MSG_DEBUG ? LOG_DEBUG : LOG_INFO, format, ap);
65 else
66 vfprintf(stderr, format, ap);
67 va_end(ap);
68
69 }
70
71 void debug_msg_cont(int level, const char *format, ...)
72 {
73 va_list ap;
74
75 if (config.debug_level < level)
76 return;
77
78 va_start(ap, format);
79 vfprintf(stderr, format, ap);
80 va_end(ap);
81 }
82
83 void usteer_init_defaults(void)
84 {
85 memset(&config, 0, sizeof(config));
86
87 config.sta_block_timeout = 30 * 1000;
88 config.local_sta_timeout = 120 * 1000;
89 config.local_sta_update = 1 * 1000;
90 config.max_retry_band = 5;
91 config.seen_policy_timeout = 30 * 1000;
92 config.band_steering_threshold = 5;
93 config.load_balancing_threshold = 5;
94 config.remote_update_interval = 1000;
95 config.initial_connect_delay = 0;
96 config.remote_node_timeout = 10;
97
98 config.roam_kick_delay = 100;
99 config.roam_scan_tries = 3;
100 config.roam_scan_interval = 10 * 1000;
101 config.roam_trigger_interval = 60 * 1000;
102
103 config.load_kick_enabled = false;
104 config.load_kick_threshold = 75;
105 config.load_kick_delay = 10 * 1000;
106 config.load_kick_min_clients = 10;
107 config.load_kick_reason_code = 5; /* WLAN_REASON_DISASSOC_AP_BUSY */
108
109 config.debug_level = MSG_FATAL;
110 }
111
112 void usteer_update_time(void)
113 {
114 struct timespec ts;
115
116 clock_gettime(CLOCK_MONOTONIC, &ts);
117 current_time = (uint64_t) ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
118 }
119
120 static int usage(const char *prog)
121 {
122 fprintf(stderr, "Usage: %s [options]\n"
123 "Options:\n"
124 " -v: Increase debug level (repeat for more messages):\n"
125 " 1: info messages\n"
126 " 2: debug messages\n"
127 " 3: verbose debug messages\n"
128 " 4: include network messages\n"
129 " 5: include extra testing messages\n"
130 " -i <name>: Connect to other instances on interface <name>\n"
131 " -s: Output log messages via syslog instead of stderr\n"
132 " -D <n>: Do not daemonize, wait for <n> seconds and print\n"
133 " remote hosts and nodes\n"
134 "\n", prog);
135 return 1;
136 }
137
138 static void
139 usteer_dump_timeout(struct uloop_timeout *t)
140 {
141 struct usteer_remote_host *host;
142 struct usteer_remote_node *rn;
143 struct blob_buf b = {};
144 char *str;
145 void *c;
146
147 blob_buf_init(&b, 0);
148
149 c = blobmsg_open_table(&b, "hosts");
150 avl_for_each_element(&remote_hosts, host, avl)
151 usteer_dump_host(&b, host);
152 blobmsg_close_table(&b, c);
153
154 c = blobmsg_open_table(&b, "nodes");
155 for_each_remote_node(rn)
156 usteer_dump_node(&b, &rn->node);
157 blobmsg_close_table(&b, c);
158
159 str = blobmsg_format_json(b.head, true);
160 blob_buf_free(&b);
161
162 puts(str);
163 free(str);
164
165 uloop_end();
166 }
167
168 int main(int argc, char **argv)
169 {
170 struct uloop_timeout dump_timer;
171 int ch;
172
173 usteer_init_defaults();
174
175 while ((ch = getopt(argc, argv, "D:i:sv")) != -1) {
176 switch(ch) {
177 case 'v':
178 config.debug_level++;
179 break;
180 case 's':
181 config.syslog = true;
182 break;
183 case 'i':
184 usteer_interface_add(optarg);
185 break;
186 case 'D':
187 dump_time = atoi(optarg);
188 break;
189 default:
190 return usage(argv[0]);
191 }
192 }
193
194 openlog("usteer", 0, LOG_USER);
195
196 config_set_event_log_types(NULL);
197 usteer_update_time();
198 uloop_init();
199
200 ubus_ctx = ubus_connect(NULL);
201 if (!ubus_ctx) {
202 fprintf(stderr, "Failed to connect to ubus\n");
203 return -1;
204 }
205
206 ubus_add_uloop(ubus_ctx);
207 usteer_interface_init();
208 if (dump_time) {
209 dump_timer.cb = usteer_dump_timeout;
210 uloop_timeout_set(&dump_timer, dump_time * 1000);
211 } else {
212 usteer_ubus_init(ubus_ctx);
213 usteer_local_nodes_init(ubus_ctx);
214 }
215 uloop_run();
216
217 uloop_done();
218 return 0;
219 }