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