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