remote: track remote hosts as a separate data structure
[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 "usteer.h"
25 #include "event.h"
26
27 struct ubus_context *ubus_ctx;
28 struct usteer_config config = {};
29 uint64_t current_time;
30
31 LIST_HEAD(node_handlers);
32
33 const char * const event_types[__EVENT_TYPE_MAX] = {
34 [EVENT_TYPE_PROBE] = "probe",
35 [EVENT_TYPE_AUTH] = "auth",
36 [EVENT_TYPE_ASSOC] = "assoc",
37 };
38
39 void log_msg(char *msg)
40 {
41 if (config.syslog)
42 syslog(LOG_INFO, "%s\n", msg);
43 else
44 fprintf(stderr, "%s\n", msg);
45 }
46
47 void debug_msg(int level, const char *func, int line, const char *format, ...)
48 {
49 va_list ap;
50
51 if (config.debug_level < level)
52 return;
53
54 if (!config.syslog)
55 fprintf(stderr, "[%s:%d] ", func, line);
56
57 va_start(ap, format);
58 if (config.syslog)
59 vsyslog(level >= MSG_DEBUG ? LOG_DEBUG : LOG_INFO, format, ap);
60 else
61 vfprintf(stderr, format, ap);
62 va_end(ap);
63
64 }
65
66 void debug_msg_cont(int level, const char *format, ...)
67 {
68 va_list ap;
69
70 if (config.debug_level < level)
71 return;
72
73 va_start(ap, format);
74 vfprintf(stderr, format, ap);
75 va_end(ap);
76 }
77
78 void usteer_init_defaults(void)
79 {
80 memset(&config, 0, sizeof(config));
81
82 config.sta_block_timeout = 30 * 1000;
83 config.local_sta_timeout = 120 * 1000;
84 config.local_sta_update = 1 * 1000;
85 config.max_retry_band = 5;
86 config.seen_policy_timeout = 30 * 1000;
87 config.band_steering_threshold = 5;
88 config.load_balancing_threshold = 5;
89 config.remote_update_interval = 1000;
90 config.initial_connect_delay = 0;
91 config.remote_node_timeout = 120 * 1000;
92
93 config.roam_kick_delay = 100;
94 config.roam_scan_tries = 3;
95 config.roam_scan_interval = 10 * 1000;
96 config.roam_trigger_interval = 60 * 1000;
97
98 config.load_kick_enabled = false;
99 config.load_kick_threshold = 75;
100 config.load_kick_delay = 10 * 1000;
101 config.load_kick_min_clients = 10;
102 config.load_kick_reason_code = 5; /* WLAN_REASON_DISASSOC_AP_BUSY */
103
104 config.debug_level = MSG_FATAL;
105 }
106
107 void usteer_update_time(void)
108 {
109 struct timespec ts;
110
111 clock_gettime(CLOCK_MONOTONIC, &ts);
112 current_time = (uint64_t) ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
113 }
114
115 static int usage(const char *prog)
116 {
117 fprintf(stderr, "Usage: %s [options]\n"
118 "Options:\n"
119 " -v: Increase debug level (repeat for more messages):\n"
120 " 1: info messages\n"
121 " 2: debug messages\n"
122 " 3: verbose debug messages\n"
123 " 4: include network messages\n"
124 " 5: include extra testing messages\n"
125 " -i <name>: Connect to other instances on interface <name>\n"
126 " -s: Output log messages via syslog instead of stderr\n"
127 "\n", prog);
128 return 1;
129 }
130
131 int main(int argc, char **argv)
132 {
133 int ch;
134
135 usteer_init_defaults();
136
137 while ((ch = getopt(argc, argv, "i:sv")) != -1) {
138 switch(ch) {
139 case 'v':
140 config.debug_level++;
141 break;
142 case 's':
143 config.syslog = true;
144 break;
145 case 'i':
146 usteer_interface_add(optarg);
147 break;
148 default:
149 return usage(argv[0]);
150 }
151 }
152
153 openlog("usteer", 0, LOG_USER);
154
155 config_set_event_log_types(NULL);
156 usteer_update_time();
157 uloop_init();
158
159 ubus_ctx = ubus_connect(NULL);
160 if (!ubus_ctx) {
161 fprintf(stderr, "Failed to connect to ubus\n");
162 return -1;
163 }
164
165 ubus_add_uloop(ubus_ctx);
166 usteer_ubus_init(ubus_ctx);
167 usteer_interface_init();
168 usteer_local_nodes_init(ubus_ctx);
169 uloop_run();
170
171 uloop_done();
172 return 0;
173 }