diff --git a/src/crypto/crypto.c b/src/crypto/crypto.c index 00fddc7..4550cc3 100644 --- a/src/crypto/crypto.c +++ b/src/crypto/crypto.c @@ -3,6 +3,7 @@ #include +#include "utils.h" #include "memory_utils.h" #include "crypto.h" @@ -14,7 +15,7 @@ gcry_cipher_hd_t gcry_cipher_hd; void gcrypt_init() { if (!gcry_check_version(GCRYPT_VERSION)) { - fprintf(stderr, "gcrypt: library version mismatch"); + dawnlog_error("gcrypt: library version mismatch"); } gcry_error_t err = 0; err = gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN); @@ -23,7 +24,7 @@ void gcrypt_init() { err |= gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); if (err) { - fprintf(stderr, "gcrypt: failed initialization"); + dawnlog_error("gcrypt: failed initialization"); } } @@ -37,7 +38,7 @@ void gcrypt_set_key_and_iv(const char *key, const char *iv) { GCRY_C_MODE, // int 0); if (gcry_error_handle) { - fprintf(stderr, "gcry_cipher_open failed: %s/%s\n", + dawnlog_error("gcry_cipher_open failed: %s/%s\n", gcry_strsource(gcry_error_handle), gcry_strerror(gcry_error_handle)); return; @@ -45,7 +46,7 @@ void gcrypt_set_key_and_iv(const char *key, const char *iv) { gcry_error_handle = gcry_cipher_setkey(gcry_cipher_hd, key, keylen); if (gcry_error_handle) { - fprintf(stderr, "gcry_cipher_setkey failed: %s/%s\n", + dawnlog_error("gcry_cipher_setkey failed: %s/%s\n", gcry_strsource(gcry_error_handle), gcry_strerror(gcry_error_handle)); return; @@ -53,7 +54,7 @@ void gcrypt_set_key_and_iv(const char *key, const char *iv) { gcry_error_handle = gcry_cipher_setiv(gcry_cipher_hd, iv, blklen); if (gcry_error_handle) { - fprintf(stderr, "gcry_cipher_setiv failed: %s/%s\n", + dawnlog_error("gcry_cipher_setiv failed: %s/%s\n", gcry_strsource(gcry_error_handle), gcry_strerror(gcry_error_handle)); return; @@ -67,12 +68,12 @@ char *gcrypt_encrypt_msg(char *msg, size_t msg_length, int *out_length) { char *out = dawn_malloc(msg_length); if (!out){ - fprintf(stderr, "gcry_cipher_encrypt error: not enought memory\n"); + dawnlog_error("gcry_cipher_encrypt error: not enough memory\n"); return NULL; } gcry_error_handle = gcry_cipher_encrypt(gcry_cipher_hd, out, msg_length, msg, msg_length); if (gcry_error_handle) { - fprintf(stderr, "gcry_cipher_encrypt failed: %s/%s\n", + dawnlog_error("gcry_cipher_encrypt failed: %s/%s\n", gcry_strsource(gcry_error_handle), gcry_strerror(gcry_error_handle)); return NULL; @@ -88,12 +89,12 @@ char *gcrypt_decrypt_msg(char *msg, size_t msg_length) { char *out_buffer = dawn_malloc(msg_length); if (!out_buffer){ - fprintf(stderr, "gcry_cipher_decrypt error: not enought memory\n"); + dawnlog_error("gcry_cipher_decrypt error: not enough memory\n"); return NULL; } gcry_error_handle = gcry_cipher_decrypt(gcry_cipher_hd, out_buffer, msg_length, msg, msg_length); if (gcry_error_handle) { - fprintf(stderr, "gcry_cipher_decrypt failed: %s/%s\n", + dawnlog_error("gcry_cipher_decrypt failed: %s/%s\n", gcry_strsource(gcry_error_handle), gcry_strerror(gcry_error_handle)); dawn_free(out_buffer); @@ -102,7 +103,7 @@ char *gcrypt_decrypt_msg(char *msg, size_t msg_length) { char *out = dawn_malloc(strlen(out_buffer) + 1); if (!out){ dawn_free(out_buffer); - fprintf(stderr, "gcry_cipher_decrypt error: not enought memory\n"); + dawnlog_error("gcry_cipher_decrypt error: not enough memory\n"); return NULL; } strcpy(out, out_buffer); diff --git a/src/include/datastorage.h b/src/include/datastorage.h index 5e10491..377188e 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -127,6 +127,10 @@ struct time_config_s { time_t update_beacon_reports; }; +struct local_config_s { + int loglevel; +}; + #define MAX_IP_LENGTH 46 #define MAX_KEY_LENGTH 65 @@ -145,12 +149,10 @@ struct network_config_s { extern struct network_config_s network_config; extern struct time_config_s timeout_config; +extern struct local_config_s local_config; extern struct probe_metric_s dawn_metric; /*** Core DAWN data structures for tracking network devices and status ***/ -// Define this to remove printing / reporing of fields, and hence observe -// which fields are evaluated in use at compile time. -// #define DAWN_NO_OUTPUT // TODO notes: // Never used? = No code reference @@ -174,11 +176,9 @@ typedef struct probe_entry_s { uint8_t vht_capabilities; // eval_probe_metric() time_t time; // remove_old...entries int counter; -#ifndef DAWN_NO_OUTPUT int deny_counter; // TODO: Never used? uint8_t max_supp_datarate; // TODO: Never used? uint8_t min_supp_datarate; // TODO: Never used? -#endif uint32_t rcpi; uint32_t rsni; } probe_entry; @@ -316,7 +316,7 @@ void remove_old_probe_entries(time_t current_time, long long int threshold); void print_probe_array(); -void print_probe_entry(probe_entry *entry); +void print_probe_entry(int level, probe_entry *entry); int eval_probe_metric(struct probe_entry_s * probe_entry, ap *ap_entry); @@ -326,7 +326,7 @@ auth_entry *insert_to_denied_req_array(auth_entry*entry, int inc_counter, time_t void remove_old_denied_req_entries(time_t current_time, long long int threshold, int logmac); -void print_auth_entry(auth_entry *entry); +void print_auth_entry(int level, auth_entry *entry); // ---------------- Functions ---------------- @@ -350,7 +350,7 @@ client *client_array_delete(client *entry, int unlink_only); void print_client_array(); -void print_client_entry(client *entry); +void print_client_entry(int level, client *entry); int is_connected_somehwere(struct dawn_mac client_addr); diff --git a/src/include/dawn_uci.h b/src/include/dawn_uci.h index 3324310..97d3219 100644 --- a/src/include/dawn_uci.h +++ b/src/include/dawn_uci.h @@ -28,6 +28,12 @@ struct probe_metric_s uci_get_dawn_metric(); */ struct time_config_s uci_get_time_config(); +/** + * Function that returns a struct with all the local config values. + * @return the local config values. + */ +struct local_config_s uci_get_local_config(); + /** * Function that returns all the network informations. * @return the network config values. diff --git a/src/include/utils.h b/src/include/utils.h index 5c0bd63..8f9f563 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -2,6 +2,7 @@ #define __DAWN_UTILS_H #include +#include /** * Check if a string is greater than another one. @@ -11,4 +12,91 @@ */ int string_is_greater(char *str, char *str_2); + +/* +** Log handling for dawn process +*/ +#define DAWNLOG_DEST_SYSLOG 0 // Send log output to syslog... +#define DAWNLOG_DEST_STDIO 1 // ... or stdout / stderr as appropriate + +#define DAWNLOG_PERROR 0x08 // Bit flag to signal inclusion of errno from system calls + +#define DAWNLOG_PRIMASK 0x07 // Bitmask to obtain only priority value + +#define DAWNLOG_ERROR 5 // Serious malfunction / unexpected behaviour - eg: OS resource exhaustion +#define DAWNLOG_WARNING 4 // Something appears wrong, but recoverable - eg: data structures inconsistent +#define DAWNLOG_ALWAYS 3 // Standard behaviour always worth reporting - should be very low frequency messages +#define DAWNLOG_INFO 2 // Reporting on standard behaviour - should be comprehensible to user +#define DAWNLOG_TRACE 1 // More info to help trace where algorithms may be going wrong +#define DAWNLOG_DEBUG 0 // Deeper tracing to fix bugs + +#define DAWNLOG_COMPILE_MIN DAWNLOG_DEBUG // Messages lower than this priority are not compiled +#define DAWNLOG_COMPILING(level) (level >= DAWNLOG_COMPILE_MIN) + +#define dawnlog_perror(s, ...) dawnlog(DAWNLOG_ERROR|DAWNLOG_PERROR, "%s()=%s@%d %s - " s, __func__, dawnlog_basename(__FILE__), __LINE__, dawnlog_pbuf, ##__VA_ARGS__) +#define dawnlog_error(fmt, ...) dawnlog(DAWNLOG_ERROR, "%s()=%s@%d " fmt, __func__, dawnlog_basename(__FILE__), __LINE__, ##__VA_ARGS__) + +#define dawnlog_warning(fmt, ...) dawnlog(DAWNLOG_WARNING, fmt, ##__VA_ARGS__) + +#define dawnlog_always(fmt, ...) dawnlog(DAWNLOG_ALWAYS, fmt, ##__VA_ARGS__) + +#if DAWNLOG_COMPILING(DAWNLOG_INFO) +#define dawnlog_info(fmt, ...) dawnlog(DAWNLOG_INFO, fmt, ##__VA_ARGS__) +#else +#define dawnlog_info(fmt, ...) +#endif + +// Use the ..._func variants to get source code position added automatically: function, filename, line +#if DAWNLOG_COMPILING(DAWNLOG_TRACE) +#define dawnlog_trace(fmt, ...) dawnlog(DAWNLOG_TRACE, fmt, ##__VA_ARGS__) +#define dawnlog_trace_func(fmt, ...) dawnlog(DAWNLOG_TRACE, "%s()=%s@%d " fmt, __func__, dawnlog_basename(__FILE__), __LINE__, ##__VA_ARGS__) +#else +#define dawnlog_trace(fmt, ...) +#define dawnlog_trace_func(fmt, ...) +#endif + +#if DAWNLOG_COMPILING(DAWNLOG_DEBUG) +#define dawnlog_debug(fmt, ...) dawnlog(DAWNLOG_DEBUG, fmt, ##__VA_ARGS__) +#define dawnlog_debug_func(fmt, ...) dawnlog(DAWNLOG_DEBUG, "%s()=%s@%d " fmt, __func__, dawnlog_basename(__FILE__), __LINE__, ##__VA_ARGS__) +#else +#define dawnlog_debug(fmt, ...) +#define dawnlog_debug_func(fmt, ...) +#endif + +extern char dawnlog_pbuf[]; // Buffer for errno conversion for dawnlog_perror() + +/** + * Set the output target for dawnlog() + * @param logdest: DAWNLOG_DEST_* + */ +void dawnlog_dest(int logdest); + +/** + * Minimum priority level to be logged + * @param level: A priority level + */ +void dawnlog_minlevel(int level); + +/** + * Check whether a priority level would be actually logged to allow callers + * to skip "expensive" preparation such as string manipulation or loops + * @param level + * @return TRUE if the priority level would be logged + */ +int dawnlog_showing(int level); + +/** + * Log a message. + * @param level + * @param fmt + * @return + */ +void dawnlog(int level, const char* fmt, ...); + +/** + * Return pointer to filename part of full path. + * @param file + * @return + */ +const char* dawnlog_basename(const char* file); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index edcbd23..4151115 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "memory_utils.h" #include "datastorage.h" @@ -47,11 +48,50 @@ void signal_handler(int sig) { } int main(int argc, char **argv) { - const char *ubus_socket = NULL; - argc -= optind; - argv += optind; + /* Load local config now so command line can override it */ + uci_init(); + local_config = uci_get_local_config(); + + int logdest = DAWNLOG_DEST_SYSLOG; + + int opt = getopt(argc, argv, "l:o:"); + + while (opt != -1) { + switch (opt) { + case 'l': + if (!strcmp(optarg, "info")) + dawnlog_minlevel(DAWNLOG_INFO); + else if (!strcmp(optarg, "trace")) + dawnlog_minlevel(DAWNLOG_TRACE); + else if (!strcmp(optarg, "debug")) + dawnlog_minlevel(DAWNLOG_DEBUG); + else + dawnlog_warning("Unrecognised option for -l: %s\n", optarg); + break; + case 'o': + if (!strcmp(optarg, "stdio")) + logdest = DAWNLOG_DEST_STDIO; + else if (!strcmp(optarg, "syslog")) + logdest = DAWNLOG_DEST_SYSLOG; + else + dawnlog_warning("Unrecognised option for -o: %s\n", optarg); + break; + default: /* '?' */ + dawnlog_warning("Unrecognised option (%s), aborting read of them\n", argv[0]); + break; + } + + opt = getopt(argc, argv, "l:o:"); + } + + if (logdest == DAWNLOG_DEST_SYSLOG) + openlog("dawn", LOG_PID, LOG_DAEMON); + + dawnlog_dest(logdest); + + dawnlog_info("DAWN instance built around %s on %s starting...", __TIME__, __DATE__); // connect signals signal_action.sa_handler = signal_handler; @@ -61,7 +101,6 @@ int main(int argc, char **argv) { sigaction(SIGTERM, &signal_action, NULL); sigaction(SIGINT, &signal_action, NULL); - uci_init(); // TODO: Why the extra loacl struct to retuen into? struct network_config_s net_config = uci_get_dawn_network(); network_config = net_config; diff --git a/src/network/broadcastsocket.c b/src/network/broadcastsocket.c index c857da7..5b48240 100644 --- a/src/network/broadcastsocket.c +++ b/src/network/broadcastsocket.c @@ -2,6 +2,7 @@ #include #include +#include "utils.h" #include "broadcastsocket.h" int setup_broadcast_socket(const char *_broadcast_ip, unsigned short _broadcast_port, struct sockaddr_in *addr) { @@ -10,7 +11,7 @@ int setup_broadcast_socket(const char *_broadcast_ip, unsigned short _broadcast_ // Create socket if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { - fprintf(stderr, "Failed to create socket.\n"); + dawnlog_error("Failed to create socket.\n"); return -1; } @@ -18,7 +19,7 @@ int setup_broadcast_socket(const char *_broadcast_ip, unsigned short _broadcast_ broadcast_permission = 1; if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcast_permission, sizeof(broadcast_permission)) < 0) { - fprintf(stderr, "Failed to create socket.\n"); + dawnlog_error("Failed to create socket.\n"); return -1; } @@ -30,7 +31,7 @@ int setup_broadcast_socket(const char *_broadcast_ip, unsigned short _broadcast_ // Bind socket while (bind(sock, (struct sockaddr *) addr, sizeof(*addr)) < 0) { - fprintf(stderr, "Binding socket failed!\n"); + dawnlog_error("Binding socket failed!\n"); sleep(1); } return sock; diff --git a/src/network/multicastsocket.c b/src/network/multicastsocket.c index a4678c3..2cc9ca4 100644 --- a/src/network/multicastsocket.c +++ b/src/network/multicastsocket.c @@ -2,6 +2,7 @@ #include #include +#include "utils.h" #include "multicastsocket.h" // based on: http://openbook.rheinwerk-verlag.de/linux_unix_programmierung/Kap11-018.htm @@ -18,7 +19,7 @@ int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_ addr->sin_port = htons (_multicast_port); if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { - perror("socket()"); + dawnlog_perror("socket()"); exit(EXIT_FAILURE); } @@ -28,13 +29,13 @@ int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_ SOL_SOCKET, SO_REUSEADDR, &loop, sizeof(loop)) < 0) { - perror("setsockopt:SO_REUSEADDR"); + dawnlog_perror("setsockopt:SO_REUSEADDR"); exit(EXIT_FAILURE); } if (bind(sock, (struct sockaddr *) addr, sizeof(*addr)) < 0) { - perror("bind"); + dawnlog_perror("bind"); exit(EXIT_FAILURE); } @@ -44,7 +45,7 @@ int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_ IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) { - perror("setsockopt:IP_MULTICAST_LOOP"); + dawnlog_perror("setsockopt:IP_MULTICAST_LOOP"); exit(EXIT_FAILURE); } @@ -52,14 +53,14 @@ int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_ command.imr_multiaddr.s_addr = inet_addr(_multicast_ip); command.imr_interface.s_addr = htonl (INADDR_ANY); if (command.imr_multiaddr.s_addr == -1) { - perror("Wrong multicast address!\n"); + dawnlog_perror("Wrong multicast address!\n"); exit(EXIT_FAILURE); } if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &command, sizeof(command)) < 0) { - perror("setsockopt:IP_ADD_MEMBERSHIP"); + dawnlog_perror("setsockopt:IP_ADD_MEMBERSHIP"); } return sock; } @@ -69,7 +70,7 @@ int remove_multicast_socket(int socket) { IPPROTO_IP, IP_DROP_MEMBERSHIP, &command, sizeof(command)) < 0) { - perror("setsockopt:IP_DROP_MEMBERSHIP"); + dawnlog_perror("setsockopt:IP_DROP_MEMBERSHIP"); return -1; } return 0; diff --git a/src/network/networksocket.c b/src/network/networksocket.c index 19bc083..6f483fa 100644 --- a/src/network/networksocket.c +++ b/src/network/networksocket.c @@ -3,6 +3,7 @@ #include #include +#include "utils.h" #include "memory_utils.h" #include "multicastsocket.h" #include "broadcastsocket.h" @@ -37,9 +38,7 @@ int init_socket_runopts(const char *_ip, int _port, int _multicast_socket) { multicast_socket = _multicast_socket; if (multicast_socket) { -#ifndef DAWN_NO_OUTPUT - printf("Settingup multicastsocket!\n"); -#endif + dawnlog_info("Settingup multicastsocket!\n"); sock = setup_multicast_socket(ip, port, &addr); } else { sock = setup_broadcast_socket(ip, port, &addr); @@ -48,19 +47,17 @@ int init_socket_runopts(const char *_ip, int _port, int _multicast_socket) { pthread_t sniffer_thread; if (network_config.use_symm_enc) { if (pthread_create(&sniffer_thread, NULL, receive_msg_enc, NULL)) { - fprintf(stderr, "Could not create receiving thread!\n"); + dawnlog_error("Could not create receiving thread!\n"); return -1; } } else { if (pthread_create(&sniffer_thread, NULL, receive_msg, NULL)) { - fprintf(stderr, "Could not create receiving thread!\n"); + dawnlog_error("Could not create receiving thread!\n"); return -1; } } -#ifndef DAWN_NO_OUTPUT - fprintf(stdout, "Connected to %s:%d\n", ip, port); -#endif + dawnlog_info("Connected to %s:%d\n", ip, port); return 0; } @@ -69,7 +66,7 @@ void *receive_msg(void *args) { while (1) { if ((recv_string_len = recvfrom(sock, recv_string, MAX_RECV_STRING, 0, NULL, 0)) < 0) { - fprintf(stderr, "Could not receive message!"); + dawnlog_error("Could not receive message!"); continue; } @@ -83,9 +80,7 @@ void *receive_msg(void *args) { } recv_string[recv_string_len] = '\0'; -#ifndef DAWN_NO_OUTPUT - printf("Received network message: %s\n", recv_string); -#endif + dawnlog_debug("Received network message: %s\n", recv_string); handle_network_msg(recv_string); } } @@ -94,7 +89,7 @@ void *receive_msg_enc(void *args) { while (1) { if ((recv_string_len = recvfrom(sock, recv_string, MAX_RECV_STRING, 0, NULL, 0)) < 0) { - fprintf(stderr, "Could not receive message!\n"); + dawnlog_error("Could not receive message!\n"); continue; } @@ -110,20 +105,18 @@ void *receive_msg_enc(void *args) { char *base64_dec_str = dawn_malloc(B64_DECODE_LEN(strlen(recv_string))); if (!base64_dec_str){ - fprintf(stderr, "Received network error: not enough memory\n"); + dawnlog_error("Received network error: not enough memory\n"); return 0; } int base64_dec_length = b64_decode(recv_string, base64_dec_str, B64_DECODE_LEN(strlen(recv_string))); char *dec = gcrypt_decrypt_msg(base64_dec_str, base64_dec_length); if (!dec){ dawn_free(base64_dec_str); - fprintf(stderr, "Received network error: not enough memory\n"); + dawnlog_error("Received network error: not enough memory\n"); return 0; } -#ifndef DAWN_NO_OUTPUT - printf("Received network message: %s\n", dec); -#endif + dawnlog_debug("Received network message: %s\n", dec); dawn_free(base64_dec_str); handle_network_msg(dec); dawn_free(dec); @@ -140,7 +133,7 @@ int send_string(char *msg) { 0, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - perror("sendto()"); + dawnlog_perror("sendto()"); pthread_mutex_unlock(&send_mutex); exit(EXIT_FAILURE); } @@ -156,7 +149,7 @@ int send_string_enc(char *msg) { size_t msglen = strlen(msg); char *enc = gcrypt_encrypt_msg(msg, msglen + 1, &length_enc); if (!enc){ - fprintf(stderr, "sendto() error: not enough memory\n"); + dawnlog_error("sendto() error: not enough memory\n"); pthread_mutex_unlock(&send_mutex); exit(EXIT_FAILURE); } @@ -164,7 +157,7 @@ int send_string_enc(char *msg) { char *base64_enc_str = dawn_malloc(B64_ENCODE_LEN(length_enc)); if (!base64_enc_str){ dawn_free(enc); - fprintf(stderr, "sendto() error: not enough memory\n"); + dawnlog_error("sendto() error: not enough memory\n"); pthread_mutex_unlock(&send_mutex); exit(EXIT_FAILURE); } @@ -176,7 +169,7 @@ int send_string_enc(char *msg) { 0, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - perror("sendto()"); + dawnlog_perror("sendto()"); pthread_mutex_unlock(&send_mutex); exit(EXIT_FAILURE); } diff --git a/src/network/tcpsocket.c b/src/network/tcpsocket.c index 92dcf9a..3f9a30e 100644 --- a/src/network/tcpsocket.c +++ b/src/network/tcpsocket.c @@ -40,9 +40,11 @@ struct client { static void client_close(struct ustream *s) { + dawnlog_debug_func("Entering..."); + struct client *cl = container_of(s, struct client, s.stream); - fprintf(stderr, "Connection closed\n"); + dawnlog_warning("Connection closed\n"); ustream_free(s); close(cl->s.fd.fd); dawn_free(cl); @@ -59,7 +61,7 @@ static void client_notify_state(struct ustream *s) { if (!s->eof) return; - fprintf(stderr, "eof!, pending: %d, total: %d\n", s->w.data_bytes, cl->ctr); + dawnlog_error("eof!, pending: %d, total: %d\n", s->w.data_bytes, cl->ctr); if (!s->w.data_bytes) return client_close(s); @@ -70,7 +72,9 @@ static void client_to_server_close(struct ustream *s) { struct network_con_s *con = container_of(s, struct network_con_s, stream.stream); - fprintf(stderr, "Connection to server closed\n"); + dawnlog_debug_func("Entering..."); + + dawnlog_warning("Connection to server closed\n"); ustream_free(s); close(con->fd.fd); list_del(&con->list); @@ -81,10 +85,12 @@ static void client_to_server_state(struct ustream *s) { struct client *cl = container_of(s, struct client, s.stream); + dawnlog_debug_func("Entering..."); + if (!s->eof) return; - fprintf(stderr, "eof!, pending: %d, total: %d\n", s->w.data_bytes, cl->ctr); + dawnlog_error("eof!, pending: %d, total: %d\n", s->w.data_bytes, cl->ctr); if (!s->w.data_bytes) return client_to_server_close(s); @@ -94,18 +100,22 @@ static void client_to_server_state(struct ustream *s) { static void client_read_cb(struct ustream *s, int bytes) { struct client *cl = container_of(s, struct client, s.stream); + dawnlog_debug_func("Entering..."); + while(1) { if (cl->state == READ_STATUS_READY) { + dawnlog_debug("tcp_socket: commencing message...\n"); cl->str = dawn_malloc(HEADER_SIZE); if (!cl->str) { - fprintf(stderr,"not enough memory (" STR_QUOTE(__LINE__) ")\n"); + dawnlog_error("not enough memory (" STR_QUOTE(__LINE__) ")\n"); break; } uint32_t avail_len = ustream_pending_data(s, false); if (avail_len < HEADER_SIZE){//ensure recv sizeof(uint32_t) + dawnlog_debug("not complete msg, len:%d\n", avail_len); dawn_free(cl->str); cl->str = NULL; break; @@ -113,7 +123,7 @@ static void client_read_cb(struct ustream *s, int bytes) { if (ustream_read(s, cl->str, HEADER_SIZE) != HEADER_SIZE) // read msg length bytes { - fprintf(stdout,"msg length read failed\n"); + dawnlog_error("msg length read failed\n"); dawn_free(cl->str); cl->str = NULL; break; @@ -126,7 +136,7 @@ static void client_read_cb(struct ustream *s, int bytes) { // remains valid and may need to be deallocated. char *str_tmp = dawn_realloc(cl->str, cl->final_len); if (!str_tmp) { - fprintf(stderr,"not enough memory (%" PRIu32 " @ " STR_QUOTE(__LINE__) ")\n", cl->final_len); + dawnlog_error("not enough memory (%" PRIu32 " @ " STR_QUOTE(__LINE__) ")\n", cl->final_len); dawn_free(cl->str); cl->str = NULL; break; @@ -139,6 +149,7 @@ static void client_read_cb(struct ustream *s, int bytes) { if (cl->state == READ_STATUS_COMMENCED) { + dawnlog_debug("tcp_socket: reading message...\n"); uint32_t read_len = ustream_pending_data(s, false); if (read_len == 0) @@ -147,23 +158,26 @@ static void client_read_cb(struct ustream *s, int bytes) { if (read_len > (cl->final_len - cl->curr_len)) read_len = cl->final_len - cl->curr_len; + dawnlog_debug("tcp_socket: reading %" PRIu32 " bytes to add to %" PRIu32 " of %" PRIu32 "...\n", + read_len, cl->curr_len, cl->final_len); + uint32_t this_read = ustream_read(s, cl->str + cl->curr_len, read_len); cl->curr_len += this_read; + dawnlog_debug("tcp_socket: ...and we're back, now have %" PRIu32 " bytes\n", cl->curr_len); if (cl->curr_len == cl->final_len){//ensure recv final_len bytes. // Full message now received cl->state = READ_STATUS_COMPLETE; + dawnlog_debug("tcp_socket: message completed\n"); } } if (cl->state == READ_STATUS_COMPLETE) { -#ifndef DAWN_NO_OUTPUT - printf("tcp_socket: processing message...\n"); -#endif + dawnlog_debug("tcp_socket: processing message...\n"); if (network_config.use_symm_enc) { char *dec = gcrypt_decrypt_msg(cl->str + HEADER_SIZE, cl->final_len - HEADER_SIZE);//len of str is final_len if (!dec) { - fprintf(stderr,"not enough memory (" STR_QUOTE(__LINE__) ")\n"); + dawnlog_error("not enough memory (" STR_QUOTE(__LINE__) ")\n"); dawn_free(cl->str); cl->str = NULL; break; @@ -182,6 +196,7 @@ static void client_read_cb(struct ustream *s, int bytes) { } } + dawnlog_debug("tcp_socket: leaving\n"); return; } @@ -190,6 +205,8 @@ static void server_cb(struct uloop_fd *fd, unsigned int events) { unsigned int sl = sizeof(struct sockaddr_in); int sfd; + dawnlog_debug_func("Entering..."); + if (!next_client) next_client = dawn_calloc(1, sizeof(*next_client)); @@ -197,7 +214,7 @@ static void server_cb(struct uloop_fd *fd, unsigned int events) { sfd = accept(server.fd, (struct sockaddr *) &cl->sin, &sl); if (sfd < 0) { - fprintf(stderr, "Accept failed\n"); + dawnlog_error("Accept failed\n"); return; } @@ -207,17 +224,18 @@ static void server_cb(struct uloop_fd *fd, unsigned int events) { cl->s.stream.notify_write = client_notify_write; ustream_fd_init(&cl->s, sfd); next_client = NULL; // TODO: Why is this here? To avoid resetting if above return happens? - fprintf(stderr, "New connection\n"); + dawnlog_info("New connection\n"); } int run_server(int port) { + dawnlog_debug("Adding socket!\n"); char port_str[12]; - sprintf(port_str, "%d", port); + sprintf(port_str, "%d", port); // TODO: Manage buffer length server.cb = server_cb; server.fd = usock(USOCK_TCP | USOCK_SERVER | USOCK_IPV4ONLY | USOCK_NUMERIC, INADDR_ANY, port_str); if (server.fd < 0) { - perror("usock"); + dawnlog_perror("usock"); return 1; } @@ -230,22 +248,28 @@ static void client_not_be_used_read_cb(struct ustream *s, int bytes) { int len; char buf[2048]; + dawnlog_debug_func("Entering..."); + len = ustream_read(s, buf, sizeof(buf)); buf[len] = '\0'; + dawnlog_debug("Read %d bytes from SSL connection: %s\n", len, buf); } static void connect_cb(struct uloop_fd *f, unsigned int events) { struct network_con_s *entry = container_of(f, struct network_con_s, fd); + dawnlog_debug_func("Entering..."); + if (f->eof || f->error) { - fprintf(stderr, "Connection failed (%s)\n", f->eof ? "EOF" : "ERROR"); + dawnlog_error("Connection failed (%s)\n", f->eof ? "EOF" : "ERROR"); close(entry->fd.fd); list_del(&entry->list); dawn_free(entry); return; } + dawnlog_debug("Connection established\n"); uloop_fd_delete(&entry->fd); entry->stream.stream.notify_read = client_not_be_used_read_cb; @@ -258,8 +282,10 @@ static void connect_cb(struct uloop_fd *f, unsigned int events) { int add_tcp_conncection(char *ipv4, int port) { struct sockaddr_in serv_addr; + dawnlog_debug_func("Entering..."); + char port_str[12]; - sprintf(port_str, "%d", port); + sprintf(port_str, "%d", port); // TODO: Manage buffer length memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; @@ -290,20 +316,25 @@ int add_tcp_conncection(char *ipv4, int port) { tcp_entry->fd.cb = connect_cb; uloop_fd_add(&tcp_entry->fd, ULOOP_WRITE | ULOOP_EDGE_TRIGGER); + dawnlog_debug("New TCP connection to %s:%d\n", ipv4, port); list_add(&tcp_entry->list, &tcp_sock_list); return 0; } void send_tcp(char *msg) { - print_tcp_array(); + dawnlog_debug_func("Entering..."); + + if (dawnlog_showing(DAWNLOG_DEBUG)) + print_tcp_array(); + struct network_con_s *con, *tmp; if (network_config.use_symm_enc) { int length_enc; size_t msglen = strlen(msg)+1; char *enc = gcrypt_encrypt_msg(msg, msglen, &length_enc); if (!enc){ - fprintf(stderr, "Ustream error: not enought memory (" STR_QUOTE(__LINE__) ")\n"); + dawnlog_error("Ustream error: not enough memory (" STR_QUOTE(__LINE__) ")\n"); return; } @@ -311,7 +342,7 @@ void send_tcp(char *msg) { char *final_str = dawn_malloc(final_len); if (!final_str){ dawn_free(enc); - fprintf(stderr, "Ustream error: not enought memory (" STR_QUOTE(__LINE__) ")\n"); + dawnlog_error("Ustream error: not enough memory (" STR_QUOTE(__LINE__) ")\n"); return; } uint32_t *msg_header = (uint32_t *)final_str; @@ -321,8 +352,9 @@ void send_tcp(char *msg) { { if (con->connected) { int len_ustream = ustream_write(&con->stream.stream, final_str, final_len, 0); + dawnlog_debug("Ustream send: %d\n", len_ustream); if (len_ustream <= 0) { - fprintf(stderr,"Ustream error(" STR_QUOTE(__LINE__) ")!\n"); + dawnlog_error("Ustream error(" STR_QUOTE(__LINE__) ")!\n"); //ERROR HANDLING! if (con->stream.stream.write_error) { ustream_free(&con->stream.stream); @@ -342,7 +374,7 @@ void send_tcp(char *msg) { uint32_t final_len = msglen + sizeof(final_len); char *final_str = dawn_malloc(final_len); if (!final_str){ - fprintf(stderr, "Ustream error: not enought memory (" STR_QUOTE(__LINE__) ")\n"); + dawnlog_error("Ustream error: not enough memory (" STR_QUOTE(__LINE__) ")\n"); return; } uint32_t *msg_header = (uint32_t *)final_str; @@ -353,9 +385,10 @@ void send_tcp(char *msg) { { if (con->connected) { int len_ustream = ustream_write(&con->stream.stream, final_str, final_len, 0); + dawnlog_debug("Ustream send: %d\n", len_ustream); if (len_ustream <= 0) { //ERROR HANDLING! - fprintf(stderr,"Ustream error(" STR_QUOTE(__LINE__) ")!\n"); + dawnlog_error("Ustream error(" STR_QUOTE(__LINE__) ")!\n"); if (con->stream.stream.write_error) { ustream_free(&con->stream.stream); close(con->fd.fd); @@ -372,6 +405,8 @@ void send_tcp(char *msg) { struct network_con_s* tcp_list_contains_address(struct sockaddr_in entry) { struct network_con_s *con; + dawnlog_debug_func("Entering..."); + list_for_each_entry(con, &tcp_sock_list, list) { if(entry.sin_addr.s_addr == con->sock_addr.sin_addr.s_addr) @@ -383,14 +418,11 @@ struct network_con_s* tcp_list_contains_address(struct sockaddr_in entry) { } void print_tcp_array() { -#ifndef DAWN_NO_OUTPUT struct network_con_s *con; - - printf("--------Connections------\n"); + dawnlog_debug("--------Connections------\n"); list_for_each_entry(con, &tcp_sock_list, list) { - printf("Connecting to Port: %d, Connected: %s\n", ntohs(con->sock_addr.sin_port), con->connected ? "True" : "False"); + dawnlog_debug("Connecting to Port: %d, Connected: %s\n", ntohs(con->sock_addr.sin_port), con->connected ? "True" : "False"); } - printf("------------------\n"); -#endif + dawnlog_debug("------------------\n"); } diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index 0b418a0..c18296a 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -15,6 +15,7 @@ struct probe_metric_s dawn_metric; struct network_config_s network_config; struct time_config_s timeout_config; +struct local_config_s local_config; #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] @@ -95,6 +96,8 @@ static struct probe_entry_s** probe_skip_array_find_first_entry(struct dawn_mac struct probe_entry_s** lo_ptr = &probe_skip_set; int hi = probe_skip_entry_last; + dawnlog_debug_func("Entering..."); + while (lo < hi) { struct probe_entry_s** i = lo_ptr; int scan_pos = lo; @@ -132,6 +135,8 @@ static probe_entry** probe_array_find_first_entry(struct dawn_mac client_mac, st probe_entry** lo_skip_ptr = &probe_skip_set; probe_entry** lo_ptr = &probe_set; + dawnlog_debug_func("Entering..."); + while ((*lo_skip_ptr != NULL)) { int this_cmp = mac_compare_bb(((*lo_skip_ptr))->client_addr, client_mac); @@ -168,6 +173,8 @@ static ap** ap_array_find_first_entry(struct dawn_mac bssid_mac, const uint8_t* ap** lo_ptr = &ap_set; int hi = ap_entry_last; + dawnlog_debug_func("Entering..."); + while (lo < hi) { ap** i = lo_ptr; int scan_pos = lo; @@ -213,6 +220,8 @@ static struct client_s** client_skip_array_find_first_entry(struct dawn_mac clie struct client_s** lo_ptr = &client_skip_set; int hi = client_skip_entry_last; + dawnlog_debug_func("Entering..."); + while (lo < hi) { struct client_s** i = lo_ptr; int scan_pos = lo; @@ -250,6 +259,8 @@ static client** client_find_first_bc_entry(struct dawn_mac bssid_mac, struct daw client ** lo_skip_ptr = &client_skip_set; client ** lo_ptr = &client_set_bc; + dawnlog_debug_func("Entering..."); + while ((*lo_skip_ptr != NULL)) { int this_cmp = mac_compare_bb(((*lo_skip_ptr))->bssid_addr, bssid_mac); @@ -288,6 +299,8 @@ static client** client_find_first_c_entry(struct dawn_mac client_mac) client** lo_ptr = &client_set_c; int hi = client_entry_last; + dawnlog_debug_func("Entering..."); + while (lo < hi) { client** i = lo_ptr; int scan_pos = lo; @@ -324,6 +337,8 @@ auth_entry** auth_entry_find_first_entry(struct dawn_mac bssid_mac, struct dawn_ auth_entry** lo_ptr = &denied_req_set; int hi = denied_req_last; + dawnlog_debug_func("Entering..."); + while (lo < hi) { auth_entry** i = lo_ptr; int scan_pos = lo; @@ -362,6 +377,8 @@ static struct mac_entry_s** mac_find_first_entry(struct dawn_mac mac) struct mac_entry_s** lo_ptr = &mac_set; int hi = mac_set_last; + dawnlog_debug_func("Entering..."); + while (lo < hi) { struct mac_entry_s** i = lo_ptr; int scan_pos = lo; @@ -394,18 +411,20 @@ static struct mac_entry_s** mac_find_first_entry(struct dawn_mac mac) void send_beacon_reports(ap *a, int id) { pthread_mutex_lock(&client_array_mutex); + dawnlog_debug_func("Entering..."); + // Seach for BSSID client* i = *client_find_first_bc_entry(a->bssid_addr, dawn_mac_null, false); - // Go threw clients + // Go through clients while (i != NULL && mac_is_equal_bb(i->bssid_addr, a->bssid_addr)) { -#ifndef DAWN_NO_OUTPUT - printf("Client " MACSTR ": rrm_enabled_capa=%02x: PASSIVE=%d, ACTIVE=%d, TABLE=%d\n", - MAC2STR(i->client_addr.u8), i->rrm_enabled_capa, - !!(i->rrm_enabled_capa & WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE), - !!(i->rrm_enabled_capa & WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE), - !!(i->rrm_enabled_capa & WLAN_RRM_CAPS_BEACON_REPORT_TABLE)); -#endif + if (dawnlog_showing(DAWNLOG_DEBUG)) + dawnlog_debug("Station " MACSTR ": rrm_enabled_capa=%02x: PASSIVE=%d, ACTIVE=%d, TABLE=%d\n", + MAC2STR(i->client_addr.u8), i->rrm_enabled_capa, + !!(i->rrm_enabled_capa & WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE), + !!(i->rrm_enabled_capa & WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE), + !!(i->rrm_enabled_capa & WLAN_RRM_CAPS_BEACON_REPORT_TABLE)); + if (i->rrm_enabled_capa & dawn_metric.rrm_mode_mask) ubus_send_beacon_report(i, a, id); @@ -418,11 +437,13 @@ void send_beacon_reports(ap *a, int id) { int get_band(int freq) { int band; + dawnlog_debug_func("Entering..."); + for (band=0; band < __DAWN_BAND_MAX; band++) if (freq <= max_band_freq[band]) return band; band--; - fprintf(stderr, "Warning: frequency %d is beyond the last known band. " + dawnlog_warning("frequency %d is beyond the last known band. " "Using '%s' band parameters.\n", freq, band_config_name[band]); return band; } @@ -433,6 +454,8 @@ int eval_probe_metric(struct probe_entry_s* probe_entry, ap* ap_entry) { int band, score = 0; + dawnlog_debug_func("Entering..."); + // TODO: Should RCPI be used here as well? band = get_band(probe_entry->freq); score = dawn_metric.initial_score[band]; @@ -461,33 +484,28 @@ int eval_probe_metric(struct probe_entry_s* probe_entry, ap* ap_entry) { if (score < 0) score = -2; // -1 already used... -#ifndef DAWN_NO_OUTPUT - printf("Score: %d of:\n", score); - print_probe_entry(probe_entry); -#endif - return score; } static int compare_station_count(ap* ap_entry_own, ap* ap_entry_to_compare, struct dawn_mac client_addr) { -#ifndef DAWN_NO_OUTPUT - printf("Comparing own %d to %d\n", ap_entry_own->station_count, ap_entry_to_compare->station_count); -#endif + dawnlog_debug_func("Entering..."); + + dawnlog_info("Comparing own %d to %d\n", ap_entry_own->station_count, ap_entry_to_compare->station_count); int sta_count = ap_entry_own->station_count; int sta_count_to_compare = ap_entry_to_compare->station_count; if (is_connected(ap_entry_own->bssid_addr, client_addr)) { + dawnlog_debug("Own is already connected! Decrease counter!\n"); sta_count--; } if (is_connected(ap_entry_to_compare->bssid_addr, client_addr)) { + dawnlog_debug("Comparing station is already connected! Decrease counter!\n"); sta_count_to_compare--; } -#ifndef DAWN_NO_OUTPUT - printf("Comparing own station count %d to %d\n", sta_count, sta_count_to_compare); -#endif + dawnlog_info("Comparing own station count %d to %d\n", sta_count, sta_count_to_compare); if (sta_count - sta_count_to_compare > dawn_metric.max_station_diff) return 1; @@ -500,6 +518,8 @@ static int compare_station_count(ap* ap_entry_own, ap* ap_entry_to_compare, stru static struct kicking_nr *find_position(struct kicking_nr *nrlist, int score) { struct kicking_nr *ret = NULL; + dawnlog_debug_func("Entering..."); + while (nrlist && nrlist->score < score) { ret = nrlist; nrlist = nrlist->next; @@ -510,6 +530,8 @@ static struct kicking_nr *find_position(struct kicking_nr *nrlist, int score) { static void remove_kicking_nr_list(struct kicking_nr *nr_list) { struct kicking_nr *n; + dawnlog_debug_func("Entering..."); + while(nr_list) { n = nr_list->next; dawn_free(nr_list); @@ -520,6 +542,8 @@ static void remove_kicking_nr_list(struct kicking_nr *nr_list) { static struct kicking_nr *prune_kicking_nr_list(struct kicking_nr *nr_list, int min_score) { struct kicking_nr *next; + dawnlog_debug_func("Entering..."); + while (nr_list && nr_list->score <= min_score) { next = nr_list->next; dawn_free(nr_list); @@ -531,6 +555,8 @@ static struct kicking_nr *prune_kicking_nr_list(struct kicking_nr *nr_list, int static struct kicking_nr *insert_kicking_nr(struct kicking_nr *head, char *nr, int score, bool prune) { struct kicking_nr *new_entry, *pos; + dawnlog_debug_func("Entering..."); + if (prune) head = prune_kicking_nr_list(head, score - dawn_metric.kicking_threshold); @@ -553,19 +579,21 @@ static struct kicking_nr *insert_kicking_nr(struct kicking_nr *head, char *nr, i int better_ap_available(ap *kicking_ap, struct dawn_mac client_mac, struct kicking_nr **neighbor_report) { + dawnlog_debug_func("Entering..."); + // This remains set to the current AP of client for rest of function probe_entry* own_probe = *probe_array_find_first_entry(client_mac, kicking_ap->bssid_addr, true); int own_score = -1; - if (own_probe != NULL && mac_is_equal_bb(own_probe->client_addr, client_mac) && mac_is_equal_bb(own_probe->bssid_addr, kicking_ap->bssid_addr)) { -#ifndef DAWN_NO_OUTPUT - printf("Calculating own score!\n"); -#endif - + if (own_probe != NULL + && mac_is_equal_bb(own_probe->client_addr, client_mac) + && mac_is_equal_bb(own_probe->bssid_addr, kicking_ap->bssid_addr)) { own_score = eval_probe_metric(own_probe, kicking_ap); //TODO: Should the -2 return be handled? + dawnlog_trace("Current AP score = %d for:\n", own_score); + print_probe_entry(DAWNLOG_TRACE, own_probe); } // no entry for own ap - should never happen? else { - printf("Current AP not found in probe array!\n"); + dawnlog_warning("Current AP not found in probe array!\n"); return -1; } @@ -576,6 +604,7 @@ int better_ap_available(ap *kicking_ap, struct dawn_mac client_mac, struct kicki while (i != NULL && mac_is_equal_bb(i->client_addr, client_mac)) { if (i == own_probe) { + dawnlog_trace("Own Score! Skipping!\n"); i = i->next_probe; continue; } @@ -583,26 +612,26 @@ int better_ap_available(ap *kicking_ap, struct dawn_mac client_mac, struct kicki ap* candidate_ap = ap_array_get_ap(i->bssid_addr, kicking_ap->ssid); if (candidate_ap == NULL) { + dawnlog_trace("Candidate AP not in array\n"); i = i->next_probe; continue; } // check if same ssid! if (strcmp((char*)kicking_ap->ssid, (char*)candidate_ap->ssid) != 0) { + dawnlog_trace("Candidate AP has different SSID\n"); i = i->next_probe; continue; } -#ifndef DAWN_NO_OUTPUT - printf("Calculating score to compare!\n"); -#endif + dawnlog_debug("Calculating score to compare!\n"); int score_to_compare = eval_probe_metric(i, candidate_ap); // Find better score... if (score_to_compare > max_score + (kick ? 0 : dawn_metric.kicking_threshold)) { if(neighbor_report == NULL) { - fprintf(stderr,"Neighbor-Report is NULL!\n"); + dawnlog_error("Neighbor-Report is NULL!\n"); return 1; // TODO: Should this be -1? } @@ -621,7 +650,7 @@ int better_ap_available(ap *kicking_ap, struct dawn_mac client_mac, struct kicki if (compare > 0) { if (neighbor_report == NULL) { - fprintf(stderr, "Neighbor-Report is NULL!\n"); + dawnlog_error( "Neighbor-Report is NULL!\n"); return 1; // TODO: Should this be -1? } @@ -656,30 +685,25 @@ static int kick_client(ap* kicking_ap, struct client_s *client_entry, struct kic } int kick_clients(ap* kicking_ap, uint32_t id) { + dawnlog_debug_func("Entering..."); + pthread_mutex_lock(&client_array_mutex); pthread_mutex_lock(&probe_array_mutex); int kicked_clients = 0; -#ifndef DAWN_NO_OUTPUT - printf("-------- KICKING CLIENTS!!!---------\n"); - char mac_buf_ap[20]; - sprintf(mac_buf_ap, MACSTR, MAC2STR(kicking_ap->bssid_addr.u8)); - printf("EVAL %s\n", mac_buf_ap); -#endif + dawnlog_info("AP BSSID " MACSTR ": Looking for candidates to kick\n", MAC2STR(kicking_ap->bssid_addr.u8)); // Seach for BSSID client *j = *client_find_first_bc_entry(kicking_ap->bssid_addr, dawn_mac_null, false); - // Go threw clients + // Go through clients while (j != NULL && mac_is_equal_bb(j->bssid_addr, kicking_ap->bssid_addr)) { struct kicking_nr *neighbor_report = NULL; int do_kick = kick_client(kicking_ap, j, &neighbor_report); -#ifndef DAWN_NO_OUTPUT for (struct kicking_nr *n = neighbor_report; n; n = n->next) - printf("Chosen AP candidate: " NR_MACSTR ", score=%d\n", NR_MAC2STR(n->nr), n->score); -#endif + dawnlog_debug("Chosen AP candidate: " NR_MACSTR ", score=%d\n", NR_MAC2STR(n->nr), n->score); // better ap available if (do_kick > 0) { @@ -689,23 +713,17 @@ int kick_clients(ap* kicking_ap, uint32_t id) { // + chan util is changing a lot // + ping pong behavior of clients will be reduced j->kick_count++; -#ifndef DAWN_NO_OUTPUT - printf("Comparing kick count! kickcount: %d to min_number_to_kick: %d!\n", j->kick_count, + dawnlog_debug("Comparing kick count! kickcount: %d to min_number_to_kick: %d!\n", j->kick_count, dawn_metric.min_number_to_kick); -#endif if (j->kick_count >= dawn_metric.min_number_to_kick) { -#ifndef DAWN_NO_OUTPUT - printf("Better AP available. Kicking client:\n"); - print_client_entry(j); - printf("Check if client is active receiving!\n"); -#endif + dawnlog_debug("Better AP available. Kicking client:\n"); + print_client_entry(DAWNLOG_DEBUG, j); + dawnlog_debug("Check if client is active receiving!\n"); float rx_rate, tx_rate; bool have_bandwidth_iwinfo = !(get_bandwidth_iwinfo(j->client_addr, &rx_rate, &tx_rate)); if (!have_bandwidth_iwinfo && dawn_metric.bandwidth_threshold > 0) { -#ifndef DAWN_NO_OUTPUT - printf("No active transmission data for client. Don't kick!\n"); -#endif + dawnlog_debug("No active transmission data for client. Don't kick!\n"); } else { @@ -713,19 +731,23 @@ int kick_clients(ap* kicking_ap, uint32_t id) { // <= 6MBits <- probably no transmission // tx_rate has always some weird value so don't use ist if (have_bandwidth_iwinfo && rx_rate > dawn_metric.bandwidth_threshold) { -#ifndef DAWN_NO_OUTPUT - printf("Client is probably in active transmisison. Don't kick! RxRate is: %f\n", rx_rate); -#endif + dawnlog_info("Station " MACSTR ": Client is probably in active transmisison. Don't kick! RxRate is: %f\n", MAC2STR(j->client_addr.u8), rx_rate); } else { -#ifndef DAWN_NO_OUTPUT if (have_bandwidth_iwinfo) - printf("Client is probably NOT in active transmisison. KICK! RxRate is: %f\n", rx_rate); + dawnlog_always("Station " MACSTR ": Kicking as probably NOT in active transmisison. RxRate is: %f\n", MAC2STR(j->client_addr.u8), rx_rate); else - printf("No active tranmission data for client, but bandwidth_threshold=%d means we don't care. KICK!\n", - dawn_metric.bandwidth_threshold); -#endif + dawnlog_always("Station " MACSTR ": Kicking as no active transmission data for client, but bandwidth_threshold=%d is OK.\n", + MAC2STR(j->client_addr.u8), dawn_metric.bandwidth_threshold); + + print_client_entry(DAWNLOG_TRACE, j); + + if (dawnlog_showing(DAWNLOG_INFO)) + { + for (struct kicking_nr* n = neighbor_report; n; n = n->next) + dawnlog_info("Kicking NR entry: " NR_MACSTR ", score=%d\n", NR_MAC2STR(n->nr), n->score); + } // here we should send a messsage to set the probe.count for all aps to the min that there is no delay between switching // the hearing map is full... @@ -759,18 +781,14 @@ int kick_clients(ap* kicking_ap, uint32_t id) { // no entry in probe array for own bssid // TODO: Is test against -1 from (1 && -1) portable? else if (do_kick == -1) { -#ifndef DAWN_NO_OUTPUT - printf("No Information about client. Force reconnect:\n"); - print_client_entry(j); -#endif + dawnlog_info("Station " MACSTR ": No Information about client. Force reconnect:\n", MAC2STR(j->client_addr.u8)); + print_client_entry(DAWNLOG_TRACE, j); del_client_interface(id, j->client_addr, 0, 1, 0); } // ap is best else { -#ifndef DAWN_NO_OUTPUT - printf("AP is best. Client will stay:\n"); - print_client_entry(j); -#endif + dawnlog_info("Station " MACSTR ": Current AP is best. Client will stay:\n", MAC2STR(j->client_addr.u8)); + print_client_entry(DAWNLOG_TRACE, j); // set kick counter to 0 again j->kick_count = 0; } @@ -780,9 +798,7 @@ int kick_clients(ap* kicking_ap, uint32_t id) { j = j->next_entry_bc; } -#ifndef DAWN_NO_OUTPUT - printf("---------------------------\n"); -#endif + dawnlog_trace("KICKING: --------- AP Finished ---------\n"); pthread_mutex_unlock(&probe_array_mutex); pthread_mutex_unlock(&client_array_mutex); @@ -791,40 +807,34 @@ int kick_clients(ap* kicking_ap, uint32_t id) { } void update_iw_info(struct dawn_mac bssid_mac) { + dawnlog_debug_func("Entering..."); + pthread_mutex_lock(&client_array_mutex); pthread_mutex_lock(&probe_array_mutex); -#ifndef DAWN_NO_OUTPUT - printf("-------- IW INFO UPDATE!!!---------\n"); - char mac_buf_ap[20]; - sprintf(mac_buf_ap, MACSTR, MAC2STR(bssid_mac.u8)); - printf("EVAL %s\n", mac_buf_ap); -#endif + dawnlog_trace("-------- IW INFO UPDATE!!!---------\n"); + dawnlog_trace("EVAL " MACSTR "\n", MAC2STR(bssid_mac.u8)); // Seach for BSSID - // Go threw clients + // Go through clients for (client* j = *client_find_first_bc_entry(bssid_mac, dawn_mac_null, false); j != NULL && mac_is_equal_bb(j->bssid_addr, bssid_mac); j = j->next_entry_bc) { // update rssi int rssi = get_rssi_iwinfo(j->client_addr); -#ifndef DAWN_NO_OUTPUT - int exp_thr = get_expected_throughput_iwinfo(j->client_addr); - double exp_thr_tmp = iee80211_calculate_expected_throughput_mbit(exp_thr); - printf("Expected throughput %f Mbit/sec\n", exp_thr_tmp); -#endif + dawnlog_trace("Expected throughput %f Mbit/sec\n", + iee80211_calculate_expected_throughput_mbit(get_expected_throughput_iwinfo(j->client_addr))); if (rssi != INT_MIN) { if (!probe_array_update_rssi(j->bssid_addr, j->client_addr, rssi, true)) { -#ifndef DAWN_NO_OUTPUT - printf("Failed to update rssi!\n"); -#endif + dawnlog_warning("Failed to update rssi!\n"); + } + else { + dawnlog_trace("Updated rssi: %d\n", rssi); } } } -#ifndef DAWN_NO_OUTPUT - printf("---------------------------\n"); -#endif + dawnlog_trace("---------------------------\n"); pthread_mutex_unlock(&probe_array_mutex); pthread_mutex_unlock(&client_array_mutex); @@ -833,6 +843,8 @@ void update_iw_info(struct dawn_mac bssid_mac) { int is_connected_somehwere(struct dawn_mac client_addr) { int found_in_array = 0; + dawnlog_debug_func("Entering..."); + #ifndef DAWN_CLIENT_SCAN_BC_ONLY client* i = *client_find_first_c_entry(client_addr); #else @@ -854,6 +866,8 @@ int is_connected_somehwere(struct dawn_mac client_addr) { static int is_connected(struct dawn_mac bssid_mac, struct dawn_mac client_mac) { int found_in_array = 0; + dawnlog_debug_func("Entering..."); + client** i = client_find_first_bc_entry(bssid_mac, client_mac, true); if (*i != NULL && mac_is_equal_bb((*i)->bssid_addr, bssid_mac) && mac_is_equal_bb((*i)->client_addr, client_mac)) @@ -863,6 +877,8 @@ static int is_connected(struct dawn_mac bssid_mac, struct dawn_mac client_mac) { } static struct client_s* insert_to_client_bc_skip_array(struct client_s* entry) { + dawnlog_debug_func("Entering..."); + struct client_s** insert_pos = client_skip_array_find_first_entry(entry->client_addr, entry->bssid_addr, true); @@ -874,6 +890,8 @@ static struct client_s* insert_to_client_bc_skip_array(struct client_s* entry) { } void client_array_insert(client *entry, client** insert_pos) { + dawnlog_debug_func("Entering..."); + // Passed insert_pos is where to insert in bc set if (insert_pos == NULL) insert_pos = client_find_first_bc_entry(entry->bssid_addr, entry->client_addr, true); @@ -889,7 +907,7 @@ void client_array_insert(client *entry, client** insert_pos) { client_entry_last++; if (client_entry_last == ARRAY_CLIENT_LEN) { - printf("warning: client_array overflowing (now contains %d entries)!\n", client_entry_last); + dawnlog_warning("client_array overflowing (now contains %d entries)!\n", client_entry_last); } // Try to keep skip list density stable @@ -901,6 +919,8 @@ void client_array_insert(client *entry, client** insert_pos) { } client *client_array_get_client(const struct dawn_mac client_addr) { + dawnlog_debug_func("Entering..."); + //pthread_mutex_lock(&client_array_mutex); #ifndef DAWN_CLIENT_SCAN_BC_ONLY @@ -925,6 +945,8 @@ static client* client_array_unlink_entry(client** ref_bc, int unlink_only) { client* entry = *ref_bc; // Both ref_bc and ref_c point to the entry we're deleting + dawnlog_debug_func("Entering..."); + for (struct client_s** s = &client_skip_set; *s != NULL; s = &((*s)->next_skip_entry_bc)) { if (*s == entry) { *s = (*s)->next_skip_entry_bc; @@ -966,6 +988,8 @@ client *client_array_delete(client *entry, int unlink_only) { client** ref_bc = NULL; + dawnlog_debug_func("Entering..."); + // Bodyless for-loop: test done in control logic for (ref_bc = &client_set_bc; (*ref_bc != NULL) && (*ref_bc != entry); ref_bc = &((*ref_bc)->next_entry_bc)); @@ -1010,6 +1034,8 @@ static __inline__ void probe_array_unlink_next(probe_entry** i) { probe_entry* victim = *i; + dawnlog_debug_func("Entering..."); + // TODO: Can we pre-test that entry is in skip set with // if ((*s)->next_probe_skip != NULL)... ??? for (struct probe_entry_s** s = &probe_skip_set; *s != NULL; s = &((*s)->next_probe_skip)) { @@ -1030,6 +1056,8 @@ probe_entry* victim = *i; int probe_array_delete(probe_entry *entry) { int found_in_array = false; + dawnlog_debug_func("Entering..."); + for (probe_entry** i = &probe_set; *i != NULL; i = &((*i)->next_probe)) { if (*i == entry) { probe_array_unlink_next(i); @@ -1045,15 +1073,16 @@ int probe_array_set_all_probe_count(struct dawn_mac client_addr, uint32_t probe_ int updated = 0; + dawnlog_debug_func("Entering..."); + // MUSTDO: Has some code been lost here? updated never set... Certain to hit not found... pthread_mutex_lock(&probe_array_mutex); for (probe_entry *i = probe_set; i != NULL; i = i->next_probe) { if (mac_is_equal_bb(client_addr, i->client_addr)) { + dawnlog_debug("Setting probecount for given mac!\n"); i->counter = probe_count; } else if (mac_compare_bb(client_addr, i->client_addr) > 0) { -#ifndef DAWN_NO_OUTPUT - printf("MAC not found!\n"); -#endif + dawnlog_info("MAC not found!\n"); break; } } @@ -1066,6 +1095,8 @@ int probe_array_update_rssi(struct dawn_mac bssid_addr, struct dawn_mac client_a { int updated = 0; + dawnlog_debug_func("Entering..."); + probe_entry* i = probe_array_get_entry(bssid_addr, client_addr); if (i != NULL) { @@ -1084,6 +1115,8 @@ int probe_array_update_rcpi_rsni(struct dawn_mac bssid_addr, struct dawn_mac cli { int updated = 0; + dawnlog_debug_func("Entering..."); + pthread_mutex_lock(&probe_array_mutex); probe_entry* i = probe_array_get_entry(bssid_addr, client_addr); @@ -1103,6 +1136,8 @@ int probe_array_update_rcpi_rsni(struct dawn_mac bssid_addr, struct dawn_mac cli } probe_entry *probe_array_get_entry(struct dawn_mac bssid_mac, struct dawn_mac client_mac) { + dawnlog_debug_func("Entering..."); + probe_entry* ret = *probe_array_find_first_entry(client_mac, bssid_mac, true); // Check if we've been given the insert position rather than actually finding the entry @@ -1113,18 +1148,21 @@ probe_entry *probe_array_get_entry(struct dawn_mac bssid_mac, struct dawn_mac cl } void print_probe_array() { -#ifndef DAWN_NO_OUTPUT - printf("------------------\n"); - printf("Probe Entry Last: %d\n", probe_entry_last); - for (probe_entry* i = probe_set; i != NULL ; i = i->next_probe) { - print_probe_entry(i); + if (dawnlog_showing(DAWNLOG_DEBUG)) + { + dawnlog_debug("------------------\n"); + dawnlog_debug("Probe Entry Last: %d\n", probe_entry_last); + for (probe_entry* i = probe_set; i != NULL; i = i->next_probe) { + print_probe_entry(DAWNLOG_DEBUG, i); + } + dawnlog_debug("------------------\n"); } - printf("------------------\n"); -#endif } static struct probe_entry_s* insert_to_skip_array(struct probe_entry_s* entry) { + dawnlog_debug_func("Entering..."); + struct probe_entry_s** insert_pos = probe_skip_array_find_first_entry(entry->client_addr, entry->bssid_addr, true); entry->next_probe_skip = *insert_pos; @@ -1135,6 +1173,8 @@ static struct probe_entry_s* insert_to_skip_array(struct probe_entry_s* entry) { } probe_entry* insert_to_array(probe_entry* entry, int inc_counter, int save_80211k, int is_beacon, time_t expiry) { + dawnlog_debug_func("Entering..."); + pthread_mutex_lock(&probe_array_mutex); entry->time = expiry; @@ -1168,7 +1208,7 @@ probe_entry* insert_to_array(probe_entry* entry, int inc_counter, int save_80211 } else { - //printf("Adding...\n"); + dawnlog_debug("Adding...\n"); if (inc_counter) entry->counter = 1; else @@ -1180,7 +1220,7 @@ probe_entry* insert_to_array(probe_entry* entry, int inc_counter, int save_80211 probe_entry_last++; if (probe_entry_last == PROBE_ARRAY_LEN) { - printf("warning: probe_array overflowing (now contains %d entries)!\n", probe_entry_last); + dawnlog_warning("probe_array overflowing (now contains %d entries)!\n", probe_entry_last); } // Try to keep skip list density stable @@ -1196,8 +1236,9 @@ probe_entry* insert_to_array(probe_entry* entry, int inc_counter, int save_80211 } ap *insert_to_ap_array(ap* entry, time_t expiry) { - pthread_mutex_lock(&ap_array_mutex); + dawnlog_debug_func("Entering..."); + pthread_mutex_lock(&ap_array_mutex); // TODO: Why do we delete and add here? ap* old_entry = *ap_array_find_first_entry(entry->bssid_addr, entry->ssid); @@ -1214,6 +1255,8 @@ ap *insert_to_ap_array(ap* entry, time_t expiry) { ap_array_insert(entry); pthread_mutex_unlock(&ap_array_mutex); + print_ap_array(); + return entry; } @@ -1223,6 +1266,8 @@ int ap_get_collision_count(int col_domain) { int ret_sta_count = 0; + dawnlog_debug_func("Entering...");; + pthread_mutex_lock(&ap_array_mutex); for (ap* i = ap_set; i != NULL; i = i->next_ap) { @@ -1239,6 +1284,8 @@ int ap_get_collision_count(int col_domain) { // as quick if we're not using an optimised search. void ap_array_insert(ap* entry) { ap** i; + dawnlog_debug_func("Entering...");; + for (i = &ap_set; *i != NULL; i = &((*i)->next_ap)) { // TODO: Not sure these tests are right way around to ensure SSID / MAC ordering // TODO: Do we do any SSID checks elsewhere? @@ -1253,12 +1300,14 @@ void ap_array_insert(ap* entry) { ap_entry_last++; if (ap_entry_last == ARRAY_AP_LEN) { - printf("warning: ap_array overflowing (contains %d entries)!\n", ap_entry_last); + dawnlog_warning("ap_array overflowing (contains %d entries)!\n", ap_entry_last); } } ap* ap_array_get_ap(struct dawn_mac bssid_mac, const uint8_t* ssid) { + dawnlog_debug_func("Entering...");; + pthread_mutex_lock(&ap_array_mutex); ap* ret = *ap_array_find_first_entry(bssid_mac, ssid); @@ -1273,6 +1322,8 @@ ap* ap_array_get_ap(struct dawn_mac bssid_mac, const uint8_t* ssid) { static __inline__ void ap_array_unlink_next(ap** i) { + dawnlog_debug_func("Entering...");; + ap* entry = *i; *i = entry->next_ap; dawn_free(entry); @@ -1282,6 +1333,8 @@ static __inline__ void ap_array_unlink_next(ap** i) int ap_array_delete(ap *entry) { int not_found = 1; + dawnlog_debug_func("Entering...");; + // TODO: Some parts of AP entry management look at SSID as well. Not this? ap** i = &ap_set; while ( *i != NULL) { @@ -1298,6 +1351,8 @@ int ap_array_delete(ap *entry) { } void remove_old_client_entries(time_t current_time, long long int threshold) { + dawnlog_debug_func("Entering..."); + client **i = &client_set_bc; while (*i != NULL) { if ((*i)->time < current_time - threshold) { @@ -1310,6 +1365,8 @@ void remove_old_client_entries(time_t current_time, long long int threshold) { } void remove_old_probe_entries(time_t current_time, long long int threshold) { + dawnlog_debug_func("Entering..."); + probe_entry **i = &probe_set; while (*i != NULL ) { if (((*i)->time < current_time - threshold) && !is_connected((*i)->bssid_addr, (*i)->client_addr)) { @@ -1334,6 +1391,8 @@ void remove_old_ap_entries(time_t current_time, long long int threshold) { } void remove_old_denied_req_entries(time_t current_time, long long int threshold, int logmac) { + dawnlog_debug_func("Entering..."); + auth_entry** i = &denied_req_set; while (*i != NULL) { // check counter @@ -1343,7 +1402,7 @@ void remove_old_denied_req_entries(time_t current_time, long long int threshold, // client is not connected for a given time threshold! if (logmac && !is_connected_somehwere((*i)->client_addr)) { - printf("Client has probably a bad driver!\n"); + dawnlog_warning("Client has probably a bad driver!\n"); // problem that somehow station will land into this list // maybe delete again? @@ -1367,6 +1426,8 @@ void remove_old_denied_req_entries(time_t current_time, long long int threshold, client *insert_client_to_array(client *entry, time_t expiry) { client * ret = NULL; + dawnlog_debug_func("Entering..."); + client **client_tmp = client_find_first_bc_entry(entry->bssid_addr, entry->client_addr, true); if (*client_tmp == NULL || !mac_is_equal_bb(entry->bssid_addr, (*client_tmp)->bssid_addr) || !mac_is_equal_bb(entry->client_addr, (*client_tmp)->client_addr)) { @@ -1390,10 +1451,15 @@ void insert_macs_from_file() { size_t len = 0; ssize_t read; + dawnlog_debug_func("Entering..."); // TODO: Loading to array is not constrained by array checks. Buffer overrun can occur. fp = fopen("/tmp/dawn_mac_list", "r"); if (fp == NULL) + { + dawnlog_error("Failed opening MAC list file - quitting!\n"); exit(EXIT_FAILURE); + } + dawn_regmem(fp); while ((read = getline(&line, &len, fp)) != -1) { @@ -1414,7 +1480,7 @@ void insert_macs_from_file() { struct mac_entry_s* new_mac = dawn_malloc(sizeof(struct mac_entry_s)); if (new_mac == NULL) { - printf("dawn_malloc of MAC struct failed!\n"); + dawnlog_error("malloc of MAC struct failed!\n"); } else { @@ -1427,14 +1493,14 @@ void insert_macs_from_file() { } } -#ifndef DAWN_NO_OUTPUT - printf("Printing MAC list:\n"); - for (struct mac_entry_s *i = mac_set; i != NULL; i = i->next_mac) { - char mac_buf_target[20]; - sprintf(mac_buf_target, MACSTR, MAC2STR(i->mac.u8)); - printf("%s\n", mac_buf_target); + + if (dawnlog_showing(DAWNLOG_DEBUG)) + { + dawnlog_debug("Printing MAC list:\n"); + for (struct mac_entry_s* i = mac_set; i != NULL; i = i->next_mac) { + dawnlog_debug(MACSTR "\n", MAC2STR(i->mac.u8)); + } } -#endif fclose(fp); dawn_unregmem(fp); @@ -1449,6 +1515,8 @@ int insert_to_maclist(struct dawn_mac mac) { int ret = 0; struct mac_entry_s** i = mac_find_first_entry(mac); + dawnlog_debug_func("Entering..."); + if (*i != NULL && mac_is_equal_bb((*i)->mac, mac)) { ret = -1; @@ -1458,7 +1526,7 @@ struct mac_entry_s** i = mac_find_first_entry(mac); struct mac_entry_s* new_mac = dawn_malloc(sizeof(struct mac_entry_s)); if (new_mac == NULL) { - printf("dawn_malloc of MAC struct failed!\n"); + dawnlog_error("malloc of MAC struct failed!\n"); } else { @@ -1477,6 +1545,8 @@ int mac_in_maclist(struct dawn_mac mac) { int ret = 0; struct mac_entry_s** i = mac_find_first_entry(mac); + dawnlog_debug_func("Entering..."); + if (*i != NULL && mac_is_equal_bb((*i)->mac, mac)) { ret = 1; @@ -1486,6 +1556,8 @@ struct mac_entry_s** i = mac_find_first_entry(mac); } auth_entry* insert_to_denied_req_array(auth_entry* entry, int inc_counter, time_t expiry) { + dawnlog_debug_func("Entering..."); + pthread_mutex_lock(&denied_array_mutex); auth_entry** i = auth_entry_find_first_entry(entry->bssid_addr, entry->client_addr); @@ -1512,7 +1584,7 @@ auth_entry* insert_to_denied_req_array(auth_entry* entry, int inc_counter, time_ denied_req_last++; if (denied_req_last == DENY_REQ_ARRAY_LEN) { - printf("warning: denied_req_array overflowing (now contains %d entries)!\n", denied_req_last); + dawnlog_warning("denied_req_array overflowing (now contains %d entries)!\n", denied_req_last); } } @@ -1525,6 +1597,8 @@ void denied_req_array_delete(auth_entry* entry) { auth_entry** i; + dawnlog_debug_func("Entering..."); + for (i = &denied_req_set; *i != NULL; i = &((*i)->next_auth)) { if (*i == entry) { *i = entry->next_auth; @@ -1538,6 +1612,8 @@ void denied_req_array_delete(auth_entry* entry) { } struct mac_entry_s* insert_to_mac_array(struct mac_entry_s* entry, struct mac_entry_s** insert_pos) { + dawnlog_debug_func("Entering...");; + if (insert_pos == NULL) insert_pos = mac_find_first_entry(entry->mac); @@ -1546,7 +1622,7 @@ struct mac_entry_s* insert_to_mac_array(struct mac_entry_s* entry, struct mac_en mac_set_last++; if (mac_set_last == DENY_REQ_ARRAY_LEN) { - printf("warning: denied_req_array overflowing (now contains %d entries)!\n", mac_set_last); + dawnlog_warning("denied_req_array overflowing (now contains %d entries)!\n", mac_set_last); } return entry; @@ -1556,6 +1632,8 @@ void mac_array_delete(struct mac_entry_s* entry) { struct mac_entry_s** i; + dawnlog_debug_func("Entering...");; + for (i = &mac_set; *i != NULL; i = &((*i)->next_mac)) { if (*i == entry) { *i = entry->next_mac; @@ -1567,99 +1645,78 @@ void mac_array_delete(struct mac_entry_s* entry) { return; } -void print_probe_entry(probe_entry *entry) { -#ifndef DAWN_NO_OUTPUT - char mac_buf_ap[20]; - char mac_buf_client[20]; - char mac_buf_target[20]; - - sprintf(mac_buf_ap, MACSTR, MAC2STR(entry->bssid_addr.u8)); - sprintf(mac_buf_client, MACSTR, MAC2STR(entry->client_addr.u8)); - sprintf(mac_buf_target, MACSTR, MAC2STR(entry->target_addr.u8)); - - - printf( - "bssid_addr: %s, client_addr: %s, signal: %d, freq: " +void print_probe_entry(int level, probe_entry *entry) { + if (dawnlog_showing(level)) + { + dawnlog(level, + "bssid_addr: " MACSTR ", client_addr: " MACSTR ", signal : % d, freq : " "%d, counter: %d, vht: %d, min_rate: %d, max_rate: %d\n", - mac_buf_ap, mac_buf_client, entry->signal, entry->freq, entry->counter, entry->vht_capabilities, + MAC2STR(entry->bssid_addr.u8), MAC2STR(entry->client_addr.u8), + entry->signal, entry->freq, entry->counter, entry->vht_capabilities, entry->min_supp_datarate, entry->max_supp_datarate); -#endif + } } -void print_auth_entry(auth_entry *entry) { -#ifndef DAWN_NO_OUTPUT - char mac_buf_ap[20]; - char mac_buf_client[20]; - char mac_buf_target[20]; - - sprintf(mac_buf_ap, MACSTR, MAC2STR(entry->bssid_addr.u8)); - sprintf(mac_buf_client, MACSTR, MAC2STR(entry->client_addr.u8)); - sprintf(mac_buf_target, MACSTR, MAC2STR(entry->target_addr.u8)); - - printf( - "bssid_addr: %s, client_addr: %s, signal: %d, freq: " - "%d\n", - mac_buf_ap, mac_buf_client, entry->signal, entry->freq); -#endif +void print_auth_entry(int level, auth_entry *entry) { + if (dawnlog_showing(DAWNLOG_INFO)) + { + dawnlog_info( + "bssid_addr: " MACSTR ", client_addr: " MACSTR ", signal : % d, freq : %d\n", + MAC2STR(entry->bssid_addr.u8), MAC2STR(entry->client_addr.u8), entry->signal, entry->freq); + } } -void print_client_entry(client *entry) { -#ifndef DAWN_NO_OUTPUT - char mac_buf_ap[20]; - char mac_buf_client[20]; - - sprintf(mac_buf_ap, MACSTR, MAC2STR(entry->bssid_addr.u8)); - sprintf(mac_buf_client, MACSTR, MAC2STR(entry->client_addr.u8)); - - printf("bssid_addr: %s, client_addr: %s, freq: %d, ht_supported: %d, vht_supported: %d, ht: %d, vht: %d, kick: %d\n", - mac_buf_ap, mac_buf_client, entry->freq, entry->ht_supported, entry->vht_supported, entry->ht, entry->vht, - entry->kick_count); -#endif +void print_client_entry(int level, client *entry) { + if (dawnlog_showing(level)) + { + dawnlog(level, "bssid_addr: " MACSTR ", client_addr: " MACSTR ", freq: %d, ht_supported: %d, vht_supported: %d, ht: %d, vht: %d, kick: %d\n", + MAC2STR(entry->bssid_addr.u8), MAC2STR(entry->client_addr.u8), entry->freq, entry->ht_supported, entry->vht_supported, entry->ht, entry->vht, + entry->kick_count); + } } void print_client_array() { -#ifndef DAWN_NO_OUTPUT - printf("--------Clients------\n"); - printf("Client Entry Last: %d\n", client_entry_last); - for (client* i = client_set_bc; i != NULL; i = i->next_entry_bc) { - print_client_entry(i); + if (dawnlog_showing(DAWNLOG_DEBUG)) + { + dawnlog_debug("--------Clients------\n"); + dawnlog_debug("Client Entry Last: %d\n", client_entry_last); + for (client* i = client_set_bc; i != NULL; i = i->next_entry_bc) { + print_client_entry(DAWNLOG_DEBUG, i); + } + dawnlog_debug("------------------\n"); } - printf("------------------\n"); -#endif } -#ifndef DAWN_NO_OUTPUT -static void print_ap_entry(ap *entry) { - char mac_buf_ap[20]; - - sprintf(mac_buf_ap, MACSTR, MAC2STR(entry->bssid_addr.u8)); - printf("ssid: %s, bssid_addr: %s, freq: %d, ht: %d, vht: %d, chan_utilz: %d, col_d: %d, bandwidth: %d, col_count: %d neighbor_report: %s\n", - entry->ssid, mac_buf_ap, entry->freq, entry->ht_support, entry->vht_support, - entry->channel_utilization, entry->collision_domain, entry->bandwidth, - ap_get_collision_count(entry->collision_domain), entry->neighbor_report - ); +static void print_ap_entry(int level, ap *entry) { + if (dawnlog_showing(DAWNLOG_INFO)) + { + dawnlog_info("ssid: %s, bssid_addr: " MACSTR ", freq: %d, ht: %d, vht: %d, chan_utilz: %d, col_d: %d, bandwidth: %d, col_count: %d neighbor_report: %s\n", + entry->ssid, MAC2STR(entry->bssid_addr.u8), entry->freq, entry->ht_support, entry->vht_support, + entry->channel_utilization, entry->collision_domain, entry->bandwidth, + ap_get_collision_count(entry->collision_domain), entry->neighbor_report + ); + } } -#endif void print_ap_array() { -#ifndef DAWN_NO_OUTPUT - printf("--------APs------\n"); - for (ap *i = ap_set; i != NULL; i = i->next_ap) { - print_ap_entry(i); + if (dawnlog_showing(DAWNLOG_DEBUG)) + { + dawnlog_debug("--------APs------\n"); + for (ap* i = ap_set; i != NULL; i = i->next_ap) { + print_ap_entry(DAWNLOG_DEBUG, i); + } + dawnlog_debug("------------------\n"); } - printf("------------------\n"); -#endif } void destroy_mutex() { // free resources -#ifndef DAWN_NO_OUTPUT - fprintf(stdout, "Freeing mutex resources\n"); -#endif + dawnlog_info("Freeing mutex resources\n"); pthread_mutex_destroy(&probe_array_mutex); pthread_mutex_destroy(&client_array_mutex); pthread_mutex_destroy(&ap_array_mutex); + pthread_mutex_destroy(&denied_array_mutex); return; } @@ -1667,22 +1724,22 @@ void destroy_mutex() { int init_mutex() { if (pthread_mutex_init(&probe_array_mutex, NULL) != 0) { - fprintf(stderr, "Mutex init failed!\n"); + dawnlog_error("Mutex init failed!\n"); return 1; } if (pthread_mutex_init(&client_array_mutex, NULL) != 0) { - fprintf(stderr, "Mutex init failed!\n"); + dawnlog_error("Mutex init failed!\n"); return 1; } if (pthread_mutex_init(&ap_array_mutex, NULL) != 0) { - fprintf(stderr, "Mutex init failed!\n"); + dawnlog_error("Mutex init failed!\n"); return 1; } if (pthread_mutex_init(&denied_array_mutex, NULL) != 0) { - fprintf(stderr, "Mutex init failed!\n"); + dawnlog_error("Mutex init failed!\n"); return 1; } return 0; diff --git a/src/test/test_storage.c b/src/test/test_storage.c index 2812c8b..a59a3a4 100644 --- a/src/test/test_storage.c +++ b/src/test/test_storage.c @@ -493,7 +493,8 @@ static int consume_actions(int argc, char* argv[], int harness_verbosity) { args_required = 1; - print_probe_array(); + if (dawnlog_showing(DAWNLOG_INFO)) + print_probe_array(); } else if (strcmp(*argv, "client_show") == 0) { @@ -505,11 +506,11 @@ static int consume_actions(int argc, char* argv[], int harness_verbosity) { args_required = 1; - printf("--------APs------\n"); + dawnlog_info("--------APs------\n"); for (auth_entry *i = denied_req_set; i != NULL; i = i->next_auth) { - print_auth_entry(i); + print_auth_entry(DAWNLOG_INFO, i); } - printf("------------------\n"); + dawnlog_info("------------------\n"); } else if (strcmp(*argv, "ap_add_auto") == 0) { @@ -893,11 +894,9 @@ static int consume_actions(int argc, char* argv[], int harness_verbosity) else if (!strncmp(fn, "vht_cap=", 8)) load_u8(&pr0->vht_capabilities, fn + 8); else if (!strncmp(fn, "time=", 5)) load_time(&pr0->time, fn + 5); else if (!strncmp(fn, "counter=", 8)) load_int(&pr0->counter, fn + 8); -#ifndef DAWN_NO_OUTPUT else if (!strncmp(fn, "deny=", 5)) load_int(&pr0->deny_counter, fn + 5); else if (!strncmp(fn, "max_rate=", 9)) load_u8(&pr0->max_supp_datarate, fn + 9); else if (!strncmp(fn, "min_rate=", 9)) load_u8(&pr0->min_supp_datarate, fn + 9); -#endif else if (!strncmp(fn, "rcpi=", 5)) load_u32(&pr0->rcpi, fn + 5); else if (!strncmp(fn, "rsni=", 5)) load_u32(&pr0->rsni, fn + 5); else { @@ -936,11 +935,9 @@ static int consume_actions(int argc, char* argv[], int harness_verbosity) pr0->vht_capabilities = 0; pr0->time = faketime; pr0->counter = 0; -#ifndef DAWN_NO_OUTPUT pr0->deny_counter = 0; pr0->max_supp_datarate = 0; pr0->min_supp_datarate = 0; -#endif pr0->rcpi = 0; pr0->rsni = 0; @@ -1072,6 +1069,9 @@ static int consume_actions(int argc, char* argv[], int harness_verbosity) ap* ap_entry = ap_array_get_ap(pr0->bssid_addr, NULL); int this_metric = eval_probe_metric(pr0, ap_entry); + dawnlog_info("Score: %d of:\n", this_metric); + print_probe_entry(DAWNLOG_DEBUG, pr0); + printf("eval_probe_metric: Returned %d\n", this_metric); } @@ -1184,6 +1184,8 @@ int main(int argc, char* argv[]) int ret = 0; int harness_verbosity = 1; + dawnlog_dest(DAWNLOG_DEST_STDIO); // Send messages to stderr / stdout + printf("DAWN datastorage.c test harness...\n\n"); if ((argc == 1) || !strcmp(*(argv + 1), "help") || !strcmp(*(argv + 1), "--help") || !strcmp(*(argv + 1), "-h")) diff --git a/src/utils/dawn_iwinfo.c b/src/utils/dawn_iwinfo.c index 32595e8..dcf18b8 100644 --- a/src/utils/dawn_iwinfo.c +++ b/src/utils/dawn_iwinfo.c @@ -33,7 +33,7 @@ int compare_essid_iwinfo(struct dawn_mac bssid_addr, struct dawn_mac bssid_addr_ struct dirent *entry; dirp = opendir(hostapd_dir_glob); // error handling? if (!dirp) { - fprintf(stderr, "[COMPARE ESSID] Failed to open %s\n", hostapd_dir_glob); + dawnlog_error("[COMPARE ESSID] Failed to open %s\n", hostapd_dir_glob); return 0; } @@ -72,6 +72,8 @@ int compare_essid_iwinfo(struct dawn_mac bssid_addr, struct dawn_mac bssid_addr_ } closedir(dirp); + dawnlog_debug("Comparing: %s with %s\n", essid, essid_to_compare); + if (essid == NULL || essid_to_compare == NULL) { return -1; } @@ -89,9 +91,13 @@ int get_bandwidth_iwinfo(struct dawn_mac client_addr, float *rx_rate, float *tx_ struct dirent *entry; dirp = opendir(hostapd_dir_glob); // error handling? if (!dirp) { - fprintf(stderr, "[BANDWIDTH INFO] Failed to open %s\n", hostapd_dir_glob); + dawnlog_error("[BANDWIDTH INFO] Failed to open %s\n", hostapd_dir_glob); return 0; } + else + { + dawnlog_debug("[BANDWIDTH INFO] Opened %s\n", hostapd_dir_glob); + } int sucess = 0; @@ -145,7 +151,7 @@ int get_rssi_iwinfo(struct dawn_mac client_addr) { struct dirent *entry; dirp = opendir(hostapd_dir_glob); // error handling? if (!dirp) { - fprintf(stderr, "[RSSI INFO] No hostapd sockets!\n"); + dawnlog_error("[RSSI INFO] No hostapd sockets!\n"); return INT_MIN; } @@ -173,15 +179,11 @@ int get_rssi(const char *ifname, struct dawn_mac client_addr) { iw = iwinfo_backend(ifname); if (iw->assoclist(ifname, buf, &len)) { -#ifndef DAWN_NO_OUTPUT - fprintf(stdout, "No information available\n"); -#endif + dawnlog_warning("No information available\n"); iwinfo_finish(); return INT_MIN; } else if (len <= 0) { -#ifndef DAWN_NO_OUTPUT - fprintf(stdout, "No station connected\n"); -#endif + dawnlog_warning("No station connected\n"); iwinfo_finish(); return INT_MIN; } @@ -205,7 +207,7 @@ int get_expected_throughput_iwinfo(struct dawn_mac client_addr) { struct dirent *entry; dirp = opendir(hostapd_dir_glob); // error handling? if (!dirp) { - fprintf(stderr, "[RSSI INFO] Failed to open dir:%s\n", hostapd_dir_glob); + dawnlog_error("[RSSI INFO] Failed to open dir:%s\n", hostapd_dir_glob); return INT_MIN; } @@ -233,15 +235,11 @@ int get_expected_throughput(const char *ifname, struct dawn_mac client_addr) { iw = iwinfo_backend(ifname); if (iw->assoclist(ifname, buf, &len)) { -#ifndef DAWN_NO_OUTPUT - fprintf(stdout, "No information available\n"); -#endif + dawnlog_warning("No information available\n"); iwinfo_finish(); return INT_MIN; } else if (len <= 0) { -#ifndef DAWN_NO_OUTPUT - fprintf(stdout, "No station connected\n"); -#endif + dawnlog_warning("No station connected\n"); iwinfo_finish(); return INT_MIN; } @@ -310,13 +308,13 @@ int get_channel_utilization(const char *ifname, uint64_t *last_channel_time, uin if (iw->survey(ifname, buf, &len)) { - fprintf(stderr, "Survey not possible!\n\n"); + dawnlog_warning("Survey not possible!\n"); iwinfo_finish(); return 0; } else if (len <= 0) { - fprintf(stderr, "No survey results\n\n"); + dawnlog_warning("No survey results\n"); iwinfo_finish(); return 0; } @@ -352,6 +350,7 @@ int support_ht(const char *ifname) { if (iw->htmodelist(ifname, &htmodes)) { + dawnlog_debug("No HT mode information available\n"); iwinfo_finish(); return 0; } @@ -371,6 +370,7 @@ int support_vht(const char *ifname) { if (iw->htmodelist(ifname, &htmodes)) { + dawnlog_error("No VHT mode information available\n"); iwinfo_finish(); return 0; } diff --git a/src/utils/dawn_uci.c b/src/utils/dawn_uci.c index d887215..2d6d5c6 100644 --- a/src/utils/dawn_uci.c +++ b/src/utils/dawn_uci.c @@ -25,6 +25,8 @@ static void set_if_present_int(int *ret, struct uci_section *s, const char* opti void uci_get_hostname(char* hostname) { + dawnlog_debug_func("Entering..."); + char path[]= "system.@system[0].hostname"; struct uci_ptr ptr; struct uci_context *c = uci_alloc_context(); @@ -80,6 +82,8 @@ struct time_config_s uci_get_time_config() { .update_beacon_reports = 20, }; + dawnlog_debug_func("Entering..."); + struct uci_element *e; uci_foreach_element(&uci_pkg->sections, e) { @@ -102,6 +106,43 @@ struct time_config_s uci_get_time_config() { return ret; } +struct local_config_s uci_get_local_config() { + struct local_config_s ret = { + .loglevel = 0, + }; + + dawnlog_debug_func("Entering..."); + + struct uci_element* e; + uci_foreach_element(&uci_pkg->sections, e) + { + struct uci_section* s = uci_to_section(e); + + if (strcmp(s->type, "local") == 0) { + DAWN_SET_CONFIG_INT(ret, s, loglevel); + } + } + + switch (ret.loglevel) + { + case 3: + dawnlog_minlevel(DAWNLOG_DEBUG); + break; + case 2: + dawnlog_minlevel(DAWNLOG_TRACE); + break; + case 1: + dawnlog_minlevel(DAWNLOG_INFO); + break; + case 0: + default: + dawnlog_minlevel(DAWNLOG_ALWAYS); + break; + } + + return ret; +} + static int get_rrm_mode_val(char mode) { switch (tolower(mode)) { @@ -123,6 +164,8 @@ static int parse_rrm_mode(int *rrm_mode_order, const char *mode_string) { int len, mode_val; int mask = 0, order = 0, pos = 0; + dawnlog_debug_func("Entering..."); + if (!mode_string) mode_string = DEFAULT_RRM_MODE_ORDER; len = strlen(mode_string); @@ -141,15 +184,17 @@ static int parse_rrm_mode(int *rrm_mode_order, const char *mode_string) { static struct mac_entry_s *insert_neighbor_mac(struct mac_entry_s *head, const char* mac) { + dawnlog_debug_func("Entering..."); + struct mac_entry_s *new; if (!(new = dawn_malloc(sizeof (struct mac_entry_s)))) { - fprintf(stderr, "Warning: Failed to create neighbor entry for '%s'\n", mac); + dawnlog_error("Failed to allocate neighbor entry for '%s'\n", mac); return head; } memset(new, 0, sizeof (struct mac_entry_s)); if (hwaddr_aton(mac, new->mac.u8) != 0) { - fprintf(stderr, "Warning: Failed to parse MAC from '%s'\n", mac); + dawnlog_error("Failed to parse MAC from '%s'\n", mac); dawn_free(new); return head; } @@ -159,6 +204,8 @@ static struct mac_entry_s *insert_neighbor_mac(struct mac_entry_s *head, const c static void free_neighbor_mac_list(struct mac_entry_s *list) { struct mac_entry_s *ptr = list; + dawnlog_debug_func("Entering..."); + while (list) { ptr = list; list = list->next_mac; @@ -171,6 +218,8 @@ static struct mac_entry_s* uci_lookup_mac_list(struct uci_option *o) { struct mac_entry_s *head = NULL; char *str; + dawnlog_debug_func("Entering..."); + if (o == NULL) return NULL; @@ -196,6 +245,8 @@ static struct uci_section *uci_find_metric_section(const char *name) { struct uci_section *s; struct uci_element *e; + dawnlog_debug_func("Entering..."); + uci_foreach_element(&uci_pkg->sections, e) { s = uci_to_section(e); if (strcmp(s->type, "metric") == 0 && @@ -257,9 +308,9 @@ struct probe_metric_s uci_get_dawn_metric() { if (!(global_s = uci_find_metric_section("global"))) { if (!(global_s = uci_find_metric_section(NULL))) { - fprintf(stderr, "Warning: config metric global section not found! Using defaults.\n"); + dawnlog_warning("config metric global section not found! Using defaults.\n"); } else { - fprintf(stderr, "Warning: config metric global section not found. " + dawnlog_warning("config metric global section not found. " "Using first unnamed config metric.\n" "Consider naming a 'global' metric section to avoid ambiguity.\n"); } @@ -325,6 +376,8 @@ struct network_config_s uci_get_dawn_network() { .bandwidth = -1, }; + dawnlog_debug_func("Entering..."); + struct uci_element *e; uci_foreach_element(&uci_pkg->sections, e) { @@ -362,6 +415,8 @@ struct network_config_s uci_get_dawn_network() { } bool uci_get_dawn_hostapd_dir() { + dawnlog_debug_func("Entering..."); + struct uci_element *e; uci_foreach_element(&uci_pkg->sections, e) { @@ -377,6 +432,8 @@ bool uci_get_dawn_hostapd_dir() { } bool uci_get_dawn_sort_order() { + dawnlog_debug_func("Entering..."); + struct uci_element *e; uci_foreach_element(&uci_pkg->sections, e) { @@ -393,6 +450,8 @@ bool uci_get_dawn_sort_order() { int uci_reset() { + dawnlog_debug_func("Entering..."); + struct uci_context *ctx = uci_ctx; if (!ctx) { @@ -410,6 +469,8 @@ int uci_reset() } int uci_init() { + dawnlog_debug_func("Entering..."); + struct uci_context *ctx = uci_ctx; if (!ctx) { @@ -439,6 +500,8 @@ int uci_init() { } int uci_clear() { + dawnlog_debug_func("Entering..."); + for (int band = 0; band < __DAWN_BAND_MAX; band++) free_neighbor_mac_list(dawn_metric.neighbors[band]); @@ -456,6 +519,8 @@ int uci_clear() { int uci_set_network(char* uci_cmd) { + dawnlog_debug_func("Entering..."); + struct uci_ptr ptr; int ret = UCI_OK; struct uci_context *ctx = uci_ctx; @@ -480,7 +545,7 @@ int uci_set_network(char* uci_cmd) } if (uci_commit(ctx, &ptr.p, 0) != UCI_OK) { - fprintf(stderr, "Failed to commit UCI cmd: %s\n", uci_cmd); + dawnlog_error("Failed to commit UCI cmd: %s\n", uci_cmd); } return ret; diff --git a/src/utils/mac_utils.c b/src/utils/mac_utils.c index b72b19a..7f49d06 100644 --- a/src/utils/mac_utils.c +++ b/src/utils/mac_utils.c @@ -49,7 +49,7 @@ int hwaddr_aton(const char* txt, uint8_t* addr) { void write_mac_to_file(char* path, struct dawn_mac addr) { FILE* f = fopen(path, "a"); if (f == NULL) { - fprintf(stderr, "Error opening mac file!\n"); + dawnlog_error("Error opening mac file!\n"); // TODO: Should this be an exit()? exit(1); diff --git a/src/utils/memory_utils.c b/src/utils/memory_utils.c index 03614a6..2948dc2 100644 --- a/src/utils/memory_utils.c +++ b/src/utils/memory_utils.c @@ -4,6 +4,7 @@ #include #include +#include "utils.h" #include "memory_utils.h" #define DAWN_MEM_FILENAME_LEN 20 @@ -73,7 +74,7 @@ void* dawn_memory_register(enum dawn_memop type, char* file, int line, size_t si type_c = 'X'; break; default: - printf("mem-audit: Unexpected memory op tag!\n"); + dawnlog_warning("mem-audit: Unexpected memory op tag!\n"); break; } @@ -84,7 +85,7 @@ void* dawn_memory_register(enum dawn_memop type, char* file, int line, size_t si if (*ipos != NULL && (*ipos)->ptr == ptr) { - printf("mem-audit: attempting to register memory already registered (%c@%s:%d)...\n", type_c, file, line); + dawnlog_warning("mem-audit: attempting to register memory already registered (%c@%s:%d)...\n", type_c, file, line); } else { @@ -92,7 +93,7 @@ void* dawn_memory_register(enum dawn_memop type, char* file, int line, size_t si if (this_log == NULL) { - printf("mem-audit: Oh the irony! malloc() failed in dawn_memory_register()!\n"); + dawnlog_warning("mem-audit: Oh the irony! malloc() failed in dawn_memory_register()!\n"); } else { @@ -140,7 +141,7 @@ char type_c = '?'; type_c = 'R'; break; default: - printf("mem-audit: Unexpected memory op tag!\n"); + dawnlog_warning("mem-audit: Unexpected memory op tag!\n"); break; } @@ -152,7 +153,7 @@ char type_c = '?'; } else { - printf("mem-audit: Releasing (%c) memory we hadn't registered (%s:%d)...\n", type_c, file, line); + dawnlog_warning("mem-audit: Releasing (%c) memory we hadn't registered (%s:%d)...\n", type_c, file, line); } return; @@ -171,10 +172,10 @@ void dawn_memory_audit() { size_t total = 0; - printf("mem-audit: Currently recorded allocations...\n"); + dawnlog_always("mem-audit: Currently recorded allocations...\n"); for (struct mem_list* mem = mem_base; mem != NULL; mem = mem->next_mem) { - printf("mem-audit: %8" PRIu64 "=%c - %s@%d: %zu\n", mem->ref, mem->type, mem->file, mem->line, mem->size); + dawnlog_always("mem-audit: %8" PRIu64 "=%c - %s@%d: %zu\n", mem->ref, mem->type, mem->file, mem->line, mem->size); total += mem->size; } @@ -185,5 +186,5 @@ size_t total = 0; suffix = "kbytes"; } - printf("mem-audit: [End of list: %zu %s]\n", total, suffix); + dawnlog_always("mem-audit: [End of list: %zu %s]\n", total, suffix); } diff --git a/src/utils/msghandler.c b/src/utils/msghandler.c index 7cbbe3f..7a0ead0 100644 --- a/src/utils/msghandler.c +++ b/src/utils/msghandler.c @@ -133,6 +133,8 @@ static int handle_uci_config(struct blob_attr* msg); int parse_to_hostapd_notify(struct blob_attr* msg, hostapd_notify_entry* notify_req) { struct blob_attr* tb[__HOSTAPD_NOTIFY_MAX]; + dawnlog_debug_func("Entering..."); + blobmsg_parse(hostapd_notify_policy, __HOSTAPD_NOTIFY_MAX, tb, blob_data(msg), blob_len(msg)); if (hwaddr_aton(blobmsg_data(tb[HOSTAPD_NOTIFY_BSSID_ADDR]), notify_req->bssid_addr.u8)) @@ -147,10 +149,12 @@ int parse_to_hostapd_notify(struct blob_attr* msg, hostapd_notify_entry* notify_ probe_entry *parse_to_probe_req(struct blob_attr* msg) { struct blob_attr* tb[__PROB_MAX]; + dawnlog_debug_func("Entering..."); + probe_entry* prob_req = dawn_malloc(sizeof(probe_entry)); if (prob_req == NULL) { - fprintf(stderr, "dawn_malloc of probe_entry failed!\n"); + dawnlog_error("dawn_malloc of probe_entry failed!\n"); return NULL; } @@ -220,6 +224,8 @@ int handle_deauth_req(struct blob_attr* msg) { hostapd_notify_entry notify_req; parse_to_hostapd_notify(msg, ¬ify_req); + dawnlog_debug_func("Entering..."); + pthread_mutex_lock(&client_array_mutex); client* client_entry = client_array_get_client(notify_req.client_addr); @@ -228,11 +234,15 @@ int handle_deauth_req(struct blob_attr* msg) { pthread_mutex_unlock(&client_array_mutex); + dawnlog_debug("[WC] Deauth: %s\n", "deauth"); + return 0; } static int handle_set_probe(struct blob_attr* msg) { + dawnlog_debug_func("Entering..."); + hostapd_notify_entry notify_req; parse_to_hostapd_notify(msg, ¬ify_req); @@ -246,6 +256,8 @@ int handle_network_msg(char* msg) { char* method; char* data; + dawnlog_debug_func("Entering..."); + blob_buf_init(&network_buf, 0); blobmsg_add_json_from_string(&network_buf, msg); @@ -258,18 +270,18 @@ int handle_network_msg(char* msg) { method = blobmsg_data(tb[NETWORK_METHOD]); data = blobmsg_data(tb[NETWORK_DATA]); -#ifndef DAWN_NO_OUTPUT - printf("Network Method new: %s : %s\n", method, msg); -#endif + dawnlog_debug("Network Method new: %s : %s\n", method, msg); blob_buf_init(&data_buf, 0); blobmsg_add_json_from_string(&data_buf, data); if (!data_buf.head) { + dawnlog_warning("Data header not formed!"); return -1; } if (blob_len(data_buf.head) <= 0) { + dawnlog_warning("Data header invalid length!"); return -1; } @@ -294,15 +306,11 @@ int handle_network_msg(char* msg) { parse_to_clients(data_buf.head, 0, 0); } else if (strncmp(method, "deauth", 5) == 0) { -#ifndef DAWN_NO_OUTPUT - printf("METHOD DEAUTH\n"); -#endif + dawnlog_debug("METHOD DEAUTH\n"); handle_deauth_req(data_buf.head); } else if (strncmp(method, "setprobe", 5) == 0) { -#ifndef DAWN_NO_OUTPUT - printf("HANDLING SET PROBE!\n"); -#endif + dawnlog_debug("HANDLING SET PROBE!\n"); handle_set_probe(data_buf.head); } else if (strncmp(method, "addmac", 5) == 0) { @@ -312,25 +320,21 @@ int handle_network_msg(char* msg) { parse_add_mac_to_file(data_buf.head); } else if (strncmp(method, "uci", 2) == 0) { -#ifndef DAWN_NO_OUTPUT - printf("HANDLING UCI!\n"); -#endif + dawnlog_debug("HANDLING UCI!\n"); handle_uci_config(data_buf.head); } else if (strncmp(method, "beacon-report", 12) == 0) { // TODO: Check beacon report stuff - //printf("HANDLING BEACON REPORT NETWORK!\n"); - //printf("The Method for beacon-report is: %s\n", method); + dawnlog_debug("HANDLING BEACON REPORT NETWORK!\n"); + dawnlog_debug("The Method for beacon-report is: %s\n", method); // ignore beacon reports send via network!, use probe functions for it //probe_entry entry; // for now just stay at probe entry stuff... //parse_to_beacon_rep(data_buf.head, &entry, true); } else { -#ifndef DAWN_NO_OUTPUT - printf("No method fonud for: %s\n", method); -#endif + dawnlog_warning("No method found for: %s\n", method); } return 0; @@ -339,8 +343,10 @@ int handle_network_msg(char* msg) { static uint8_t dump_rrm_data(struct blob_attr* head) { + dawnlog_debug_func("Entering..."); + if (blob_id(head) != BLOBMSG_TYPE_INT32) { - fprintf(stderr, "wrong type of rrm array.\n"); + dawnlog_error("wrong type of rrm array.\n"); return 0; } return (uint8_t)blobmsg_get_u32(head); @@ -350,6 +356,8 @@ dump_rrm_data(struct blob_attr* head) static void dump_client(struct blob_attr** tb, struct dawn_mac client_addr, const char* bssid_addr, uint32_t freq, uint8_t ht_supported, uint8_t vht_supported) { + dawnlog_debug_func("Entering..."); + client *client_entry = dawn_malloc(sizeof(struct client_s)); if (client_entry == NULL) { @@ -430,6 +438,8 @@ dump_client_table(struct blob_attr* head, int len, const char* bssid_addr, uint3 struct blobmsg_hdr* hdr; int station_count = 0; + dawnlog_debug_func("Entering..."); + __blob_for_each_attr(attr, head, len) { hdr = blob_data(attr); @@ -453,6 +463,8 @@ dump_client_table(struct blob_attr* head, int len, const char* bssid_addr, uint3 int parse_to_clients(struct blob_attr* msg, int do_kick, uint32_t id) { struct blob_attr* tb[__CLIENT_TABLE_MAX]; + dawnlog_debug_func("Entering..."); + if (!msg) { return -1; } @@ -682,12 +694,14 @@ static const struct blobmsg_policy uci_times_policy[__UCI_TIMES_MAX] = { static int handle_uci_config(struct blob_attr* msg) { + dawnlog_debug_func("Entering..."); + struct blob_attr* tb[__UCI_TABLE_MAX]; blobmsg_parse(uci_table_policy, __UCI_TABLE_MAX, tb, blob_data(msg), blob_len(msg)); const char *version_string = blobmsg_get_string(tb[UCI_CONFIG_VERSION]); if (version_string == NULL || strcmp(version_string, DAWN_CONFIG_VERSION)) { - fprintf(stderr, "Ignoring network config message with incompatible version string '%s'.\n", + dawnlog_warning("Ignoring network config message with incompatible version string '%s'.\n", version_string ? : ""); return -1; } @@ -768,7 +782,7 @@ static int handle_uci_config(struct blob_attr* msg) { break; } if (band == __DAWN_BAND_MAX) { - fprintf(stderr, "handle_uci_config: Warning: unknown band '%s'.\n", band_name); + dawnlog_warning("handle_uci_config: unknown band '%s'.\n", band_name); continue; // Should we write the metrics of an unknown band to the config file? } diff --git a/src/utils/ubus.c b/src/utils/ubus.c index c8bbe56..32259fc 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -236,6 +236,8 @@ subscription_wait(struct ubus_event_handler *handler) { void blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const struct dawn_mac addr) { char *s; + dawnlog_debug_func("Entering..."); + s = blobmsg_alloc_string_buffer(buf, name, 20); sprintf(s, MACSTR, MAC2STR(addr.u8)); blobmsg_add_string_buffer(buf); @@ -276,6 +278,8 @@ static int decide_function(probe_entry *prob_req, int req_type) { int parse_to_auth_req(struct blob_attr *msg, auth_entry *auth_req) { struct blob_attr *tb[__AUTH_MAX]; + dawnlog_debug_func("Entering..."); + blobmsg_parse(auth_policy, __AUTH_MAX, tb, blob_data(msg), blob_len(msg)); if (hwaddr_aton(blobmsg_data(tb[AUTH_BSSID_ADDR]), auth_req->bssid_addr.u8)) @@ -299,6 +303,8 @@ int parse_to_auth_req(struct blob_attr *msg, auth_entry *auth_req) { } int parse_to_assoc_req(struct blob_attr *msg, assoc_entry *assoc_req) { + dawnlog_debug_func("Entering..."); + return (parse_to_auth_req(msg, assoc_req)); } @@ -307,6 +313,8 @@ int parse_to_beacon_rep(struct blob_attr *msg) { struct dawn_mac msg_bssid; struct dawn_mac msg_client; + dawnlog_debug_func("Entering..."); + blobmsg_parse(beacon_rep_policy, __BEACON_REP_MAX, tb, blob_data(msg), blob_len(msg)); if(!tb[BEACON_REP_BSSID] || !tb[BEACON_REP_ADDR]) @@ -319,7 +327,7 @@ int parse_to_beacon_rep(struct blob_attr *msg) { if(mac_is_null(msg_bssid.u8)) { - fprintf(stderr, "Received NULL MAC! Client is strange!\n"); + dawnlog_warning("Received NULL MAC! Client is strange!\n"); return -1; } @@ -339,14 +347,16 @@ int parse_to_beacon_rep(struct blob_attr *msg) { // HACKY WORKAROUND! + dawnlog_debug("Try update RCPI and RSNI for beacon report!\n"); if(!probe_array_update_rcpi_rsni(msg_bssid, msg_client, rcpi, rsni, true)) { + dawnlog_debug("Beacon: No Probe Entry Existing!\n"); probe_entry* beacon_rep = dawn_malloc(sizeof(probe_entry)); probe_entry* beacon_rep_updated = NULL; if (beacon_rep == NULL) { - fprintf(stderr, "dawn_malloc of probe_entry failed!\n"); + dawnlog_error("dawn_malloc of probe_entry failed!\n"); return -1; } @@ -364,6 +374,7 @@ int parse_to_beacon_rep(struct blob_attr *msg) { beacon_rep->ht_capabilities = false; // that is very problematic!!! beacon_rep->vht_capabilities = false; // that is very problematic!!! + dawnlog_debug("Inserting to array!\n"); // TODO: kept original code order here - send on network first to simplify? beacon_rep_updated = insert_to_array(beacon_rep, false, false, true, time(0)); @@ -383,19 +394,19 @@ int handle_auth_req(struct blob_attr* msg) { int ret = WLAN_STATUS_SUCCESS; bool discard_entry = true; -#ifndef DAWN_NO_OUTPUT - print_probe_array(); -#endif + dawnlog_debug_func("Entering..."); + auth_entry *auth_req = dawn_malloc(sizeof(struct auth_entry_s)); if (auth_req == NULL) + { + dawnlog_error("Memory allocation of auth req failed!"); return -1; + } parse_to_auth_req(msg, auth_req); -#ifndef DAWN_NO_OUTPUT - printf("Auth entry: "); - print_auth_entry(auth_req); -#endif + dawnlog_debug("Auth entry: "); + print_auth_entry(DAWNLOG_DEBUG, auth_req); if (!mac_in_maclist(auth_req->client_addr)) { pthread_mutex_lock(&probe_array_mutex); @@ -424,18 +435,15 @@ static int handle_assoc_req(struct blob_attr *msg) { int ret = WLAN_STATUS_SUCCESS; int discard_entry = true; -#ifndef DAWN_NO_OUTPUT + dawnlog_debug_func("Entering..."); print_probe_array(); -#endif auth_entry* auth_req = dawn_malloc(sizeof(struct auth_entry_s)); if (auth_req == NULL) return -1; parse_to_assoc_req(msg, auth_req); -#ifndef DAWN_NO_OUTPUT - printf("Association entry: "); - print_auth_entry(auth_req); -#endif + dawnlog_debug("Association entry: "); + print_auth_entry(DAWNLOG_DEBUG, auth_req); if (!mac_in_maclist(auth_req->client_addr)) { pthread_mutex_lock(&probe_array_mutex); @@ -464,6 +472,7 @@ static int handle_probe_req(struct blob_attr *msg) { // MUSTDO: Untangle dawn_malloc() and linking of probe_entry probe_entry* probe_req = parse_to_probe_req(msg); probe_entry* probe_req_updated = NULL; + dawnlog_debug_func("Entering..."); if (probe_req != NULL) { probe_req_updated = insert_to_array(probe_req, true, true, false, time(0)); @@ -485,8 +494,11 @@ static int handle_probe_req(struct blob_attr *msg) { } static int handle_beacon_rep(struct blob_attr *msg) { + dawnlog_debug_func("Entering..."); if (parse_to_beacon_rep(msg) == 0) { + // dawnlog_debug("Inserting beacon Report!\n"); // insert_to_array(beacon_rep, 1); + // dawnlog_debug("Sending via network!\n"); // send_blob_attr_via_network(msg, "beacon-report"); } return 0; @@ -495,6 +507,8 @@ static int handle_beacon_rep(struct blob_attr *msg) { int send_blob_attr_via_network(struct blob_attr* msg, char* method) { + dawnlog_debug_func("Entering..."); + if (!msg) { return -1; } @@ -530,19 +544,19 @@ int send_blob_attr_via_network(struct blob_attr* msg, char* method) { return 0; } -static int hostapd_notify(struct ubus_context *ctx_local, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) { - char *str; +static int hostapd_notify(struct ubus_context* ctx_local, struct ubus_object* obj, + struct ubus_request_data* req, const char* method, + struct blob_attr* msg) { int ret = 0; struct blob_buf b = {0}; - str = blobmsg_format_json(msg, true); - dawn_regmem(str); -#ifndef DAWN_NO_OUTPUT - printf("Method new: %s : %s\n", method, str); -#endif - dawn_free(str); + if (dawnlog_showing(DAWNLOG_DEBUG)) + { + char* str = blobmsg_format_json(msg, true); + dawn_regmem(str); + dawnlog_debug("Method new: %s : %s\n", method, str); + dawn_free(str); + } struct hostapd_sock_entry *entry; struct ubus_subscriber *subscriber; @@ -581,11 +595,14 @@ int dawn_init_ubus(const char *ubus_socket, const char *hostapd_dir) { uloop_init(); signal(SIGPIPE, SIG_IGN); + dawnlog_debug_func("Entering..."); + ctx = ubus_connect(ubus_socket); if (!ctx) { - fprintf(stderr, "Failed to connect to ubus\n"); + dawnlog_error("Failed to connect to ubus\n"); return -1; } else { + dawnlog_debug("Connected to ubus\n"); dawn_regmem(ctx); } @@ -632,6 +649,8 @@ int dawn_init_ubus(const char *ubus_socket, const char *hostapd_dir) { static int get_band_from_bssid(struct dawn_mac bssid) { ap *a; + dawnlog_debug_func("Entering..."); + for (a = ap_set; a; a = a->next_ap) { if (mac_is_equal_bb(a->bssid_addr, bssid)) return get_band(a->freq); @@ -661,14 +680,14 @@ static void ubus_get_clients_cb(struct ubus_request *req, int type, struct blob_ } if (entry == NULL) { - fprintf(stderr, "Failed to find interface!\n"); + dawnlog_error("Failed to find interface!\n"); dawn_free(data_str); blob_buf_free(&b); return; } if (!entry->subscribed) { - fprintf(stderr, "Interface %s is not subscribed!\n", entry->iface_name); + dawnlog_error("Interface %s is not subscribed!\n", entry->iface_name); dawn_free(data_str); blob_buf_free(&b); return; @@ -706,6 +725,7 @@ static void ubus_get_clients_cb(struct ubus_request *req, int type, struct blob_ static int ubus_get_clients() { int timeout = 1; struct hostapd_sock_entry *sub; + dawnlog_debug_func("Entering..."); list_for_each_entry(sub, &hostapd_sock_list, list) { if (sub->subscribed) { @@ -722,6 +742,8 @@ static void ubus_get_rrm_cb(struct ubus_request *req, int type, struct blob_attr struct hostapd_sock_entry *sub, *entry = NULL; struct blob_attr *tb[__RRM_MAX]; + dawnlog_debug_func("Entering..."); + if (!msg) return; @@ -748,6 +770,7 @@ static void ubus_get_rrm_cb(struct ubus_request *req, int type, struct blob_attr { char* neighborreport = blobmsg_get_string(attr); strcpy(entry->neighbor_report,neighborreport); + dawnlog_debug("Copied Neighborreport: %s,\n", entry->neighbor_report); } i++; } @@ -757,6 +780,8 @@ static int ubus_get_rrm() { int timeout = 1; struct hostapd_sock_entry *sub; + dawnlog_debug_func("Entering..."); + list_for_each_entry(sub, &hostapd_sock_list, list) { if (sub->subscribed) { @@ -770,6 +795,8 @@ static int ubus_get_rrm() { } void update_clients(struct uloop_timeout *t) { + dawnlog_debug_func("Entering..."); + ubus_get_clients(); if(dawn_metric.set_hostapd_nr) ubus_set_nr(); @@ -778,6 +805,8 @@ void update_clients(struct uloop_timeout *t) { } void run_server_update(struct uloop_timeout *t) { + dawnlog_debug_func("Entering..."); + if(run_server(network_config.tcp_port)) uloop_timeout_set(&usock_timer, 1 * 1000); } @@ -785,6 +814,8 @@ void run_server_update(struct uloop_timeout *t) { void update_channel_utilization(struct uloop_timeout *t) { struct hostapd_sock_entry *sub; + dawnlog_debug_func("Entering..."); + list_for_each_entry(sub, &hostapd_sock_list, list) { @@ -804,6 +835,8 @@ void update_channel_utilization(struct uloop_timeout *t) { } static int get_mode_from_capability(int capability) { + dawnlog_debug_func("Entering..."); + for (int n = 0; n < __RRM_BEACON_RQST_MODE_MAX; n++) { switch (capability & dawn_metric.rrm_mode_order[n]) { case WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE: @@ -820,6 +853,9 @@ static int get_mode_from_capability(int capability) { void ubus_send_beacon_report(client *c, ap *a, int id) { struct blob_buf b = {0}; + dawnlog_debug_func("Entering..."); + + dawnlog_debug("Crafting Beacon Report\n"); int timeout = 1; blob_buf_init(&b, 0); @@ -830,6 +866,7 @@ void ubus_send_beacon_report(client *c, ap *a, int id) blobmsg_add_u32(&b, "mode", get_mode_from_capability(c->rrm_enabled_capa)); blobmsg_add_string(&b, "ssid", (char*)a->ssid); + dawnlog_debug("Invoking beacon report!\n"); ubus_invoke(ctx, id, "rrm_beacon_req", b.head, NULL, NULL, timeout * 1000); blob_buf_free(&b); } @@ -837,14 +874,18 @@ void ubus_send_beacon_report(client *c, ap *a, int id) void update_beacon_reports(struct uloop_timeout *t) { ap *a; + dawnlog_debug_func("Entering..."); + if(!timeout_config.update_beacon_reports) // if 0 just return { return; } + dawnlog_debug("Sending beacon report!\n"); struct hostapd_sock_entry *sub; list_for_each_entry(sub, &hostapd_sock_list, list) { if (sub->subscribed && (a = ap_array_get_ap(sub->bssid_addr, (uint8_t*)sub->ssid))) { + dawnlog_debug("Sending beacon report Sub!\n"); send_beacon_reports(a, sub->id); } } @@ -852,6 +893,8 @@ void update_beacon_reports(struct uloop_timeout *t) { } void update_tcp_connections(struct uloop_timeout *t) { + dawnlog_debug_func("Entering..."); + if (strcmp(network_config.server_ip, "")) { // nothing happens if tcp connection is already established @@ -865,16 +908,22 @@ void update_tcp_connections(struct uloop_timeout *t) { } void start_tcp_con_update() { + dawnlog_debug_func("Entering..."); + // update connections uloop_timeout_add(&tcp_con_timer); // callback = update_tcp_connections } void update_hostapd_sockets(struct uloop_timeout *t) { + dawnlog_debug_func("Entering..."); + subscribe_to_new_interfaces(hostapd_dir_glob); uloop_timeout_set(&hostapd_timer, timeout_config.update_hostapd * 1000); } void ubus_set_nr(){ + dawnlog_debug_func("Entering..."); + struct hostapd_sock_entry *sub; int timeout = 1; @@ -894,6 +943,8 @@ void del_client_all_interfaces(const struct dawn_mac client_addr, uint32_t reaso struct hostapd_sock_entry *sub; struct blob_buf b = {0}; + dawnlog_debug_func("Entering..."); + blob_buf_init(&b, 0); blobmsg_add_macaddr(&b, "addr", client_addr); blobmsg_add_u32(&b, "reason", reason); @@ -935,6 +986,8 @@ int wnm_disassoc_imminent(uint32_t id, const struct dawn_mac client_addr, struct struct hostapd_sock_entry *sub; struct blob_buf b = {0}; + dawnlog_debug_func("Entering..."); + blob_buf_init(&b, 0); blobmsg_add_macaddr(&b, "addr", client_addr); blobmsg_add_u32(&b, "duration", duration); @@ -942,9 +995,7 @@ int wnm_disassoc_imminent(uint32_t id, const struct dawn_mac client_addr, struct void* nbs = blobmsg_open_array(&b, "neighbors"); while(neighbor_list != NULL) { -#ifndef DAWN_NO_OUTPUT - printf("BSS TRANSITION NEIGHBOR " NR_MACSTR ", Score=%d\n", NR_MAC2STR(neighbor_list->nr), neighbor_list->score); -#endif + dawnlog_info("BSS TRANSITION NEIGHBOR " NR_MACSTR ", Score=%d\n", NR_MAC2STR(neighbor_list->nr), neighbor_list->score); blobmsg_add_string(&b, NULL, neighbor_list->nr); neighbor_list = neighbor_list->next; } @@ -966,6 +1017,8 @@ int wnm_disassoc_imminent(uint32_t id, const struct dawn_mac client_addr, struct static void ubus_umdns_cb(struct ubus_request *req, int type, struct blob_attr *msg) { struct blob_attr *tb[__DAWN_UMDNS_TABLE_MAX]; + dawnlog_debug_func("Entering..."); + if (!msg) return; @@ -980,19 +1033,17 @@ static void ubus_umdns_cb(struct ubus_request *req, int type, struct blob_attr * __blob_for_each_attr(attr, blobmsg_data(tb[DAWN_UMDNS_TABLE]), len) { -#ifndef DAWN_NO_OUTPUT +#if DAWNLOG_COMPILING(DAWNLOG_DEBUG) struct blobmsg_hdr *hdr = blob_data(attr); - printf("Hostname: %s\n", hdr->name); + dawnlog_debug("Hostname: %s\n", hdr->name); #endif struct blob_attr *tb_dawn[__DAWN_UMDNS_MAX]; blobmsg_parse(dawn_umdns_policy, __DAWN_UMDNS_MAX, tb_dawn, blobmsg_data(attr), blobmsg_len(attr)); if (tb_dawn[DAWN_UMDNS_IPV4] && tb_dawn[DAWN_UMDNS_PORT]) { -#ifndef DAWN_NO_OUTPUT - printf("IPV4: %s\n", blobmsg_get_string(tb_dawn[DAWN_UMDNS_IPV4])); - printf("Port: %d\n", blobmsg_get_u32(tb_dawn[DAWN_UMDNS_PORT])); -#endif + dawnlog_debug("IPV4: %s\n", blobmsg_get_string(tb_dawn[DAWN_UMDNS_IPV4])); + dawnlog_debug("Port: %d\n", blobmsg_get_u32(tb_dawn[DAWN_UMDNS_PORT])); } else { return; } @@ -1004,8 +1055,10 @@ int ubus_call_umdns() { u_int32_t id; struct blob_buf b = {0}; + dawnlog_debug_func("Entering..."); + if (ubus_lookup_id(ctx, "umdns", &id)) { - fprintf(stderr, "Failed to look up test object for %s\n", "umdns"); + dawnlog_error("Failed to look up test object for %s\n", "umdns"); return -1; } @@ -1022,6 +1075,8 @@ int ubus_call_umdns() { int ubus_send_probe_via_network(struct probe_entry_s *probe_entry) { // TODO: probe_entry is also a typedef - fix? struct blob_buf b = {0}; + dawnlog_debug_func("Entering..."); + blob_buf_init(&b, 0); blobmsg_add_macaddr(&b, "bssid", probe_entry->bssid_addr); blobmsg_add_macaddr(&b, "address", probe_entry->client_addr); @@ -1056,6 +1111,8 @@ int ubus_send_probe_via_network(struct probe_entry_s *probe_entry) { // TODO: p int send_set_probe(struct dawn_mac client_addr) { struct blob_buf b = {0}; + dawnlog_debug_func("Entering..."); + blob_buf_init(&b, 0); blobmsg_add_macaddr(&b, "bssid", client_addr); blobmsg_add_macaddr(&b, "address", client_addr); @@ -1097,15 +1154,21 @@ int parse_add_mac_to_file(struct blob_attr *msg) { struct blob_attr *tb[__ADD_DEL_MAC_MAX]; struct blob_attr *attr; + dawnlog_debug_func("Entering..."); + + dawnlog_debug("Parsing MAC!\n"); + blobmsg_parse(add_del_policy, __ADD_DEL_MAC_MAX, tb, blob_data(msg), blob_len(msg)); if (!tb[MAC_ADDR]) return UBUS_STATUS_INVALID_ARGUMENT; int len = blobmsg_data_len(tb[MAC_ADDR]); + dawnlog_debug("Length of array maclist: %d\n", len); __blob_for_each_attr(attr, blobmsg_data(tb[MAC_ADDR]), len) { + dawnlog_debug("Iteration through MAC-list\n"); struct dawn_mac addr; hwaddr_aton(blobmsg_data(attr), addr.u8); @@ -1122,6 +1185,10 @@ int parse_add_mac_to_file(struct blob_attr *msg) { static int add_mac(struct ubus_context *ctx_local, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { + dawnlog_debug_func("Entering..."); + + dawnlog_trace("UBUS invoking add_mac()"); + parse_add_mac_to_file(msg); // here we need to send it via the network! @@ -1136,6 +1203,10 @@ static int reload_config(struct ubus_context *ctx_local, struct ubus_object *obj int ret; struct blob_buf b = {0}; + dawnlog_debug_func("Entering..."); + + dawnlog_trace("UBUS invoking reload_config()"); + blob_buf_init(&b, 0); uci_reset(); dawn_metric = uci_get_dawn_metric(); @@ -1149,7 +1220,7 @@ static int reload_config(struct ubus_context *ctx_local, struct ubus_object *obj uci_send_via_network(); ret = ubus_send_reply(ctx_local, req, b.head); if (ret) - fprintf(stderr, "Failed to send reply: %s\n", ubus_strerror(ret)); + dawnlog_error("Failed to send reply: %s\n", ubus_strerror(ret)); blob_buf_free(&b); @@ -1162,11 +1233,15 @@ static int get_hearing_map(struct ubus_context *ctx_local, struct ubus_object *o int ret; struct blob_buf b = {0}; + dawnlog_debug_func("Entering..."); + + dawnlog_trace("UBUS invoking get_hearing_map()"); + blob_buf_init(&b, 0); build_hearing_map_sort_client(&b); ret = ubus_send_reply(ctx_local, req, b.head); if (ret) - fprintf(stderr, "Failed to send reply: %s\n", ubus_strerror(ret)); + dawnlog_error("Failed to send reply: %s\n", ubus_strerror(ret)); blob_buf_free(&b); return 0; @@ -1179,11 +1254,15 @@ static int get_network(struct ubus_context *ctx_local, struct ubus_object *obj, int ret; struct blob_buf b = {0}; + dawnlog_debug_func("Entering..."); + + dawnlog_trace("UBUS invoking get_network()"); + blob_buf_init(&b, 0); build_network_overview(&b); ret = ubus_send_reply(ctx_local, req, b.head); if (ret) - fprintf(stderr, "Failed to send reply: %s\n", ubus_strerror(ret)); + dawnlog_error("Failed to send reply: %s\n", ubus_strerror(ret)); blob_buf_free(&b); return 0; @@ -1192,9 +1271,11 @@ static int get_network(struct ubus_context *ctx_local, struct ubus_object *obj, static void ubus_add_oject() { int ret; + dawnlog_debug_func("Entering..."); + ret = ubus_add_object(ctx, &dawn_object); if (ret) - fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret)); + dawnlog_error("Failed to add object: %s\n", ubus_strerror(ret)); } static void respond_to_notify(uint32_t id) { @@ -1205,12 +1286,14 @@ static void respond_to_notify(uint32_t id) { struct blob_buf b = {0}; int timeout = 1; + dawnlog_debug_func("Entering..."); + blob_buf_init(&b, 0); blobmsg_add_u32(&b, "notify_response", 1); ret = ubus_invoke(ctx, id, "notify_response", b.head, NULL, NULL, timeout * 1000); if (ret) - fprintf(stderr, "Failed to invoke: %s\n", ubus_strerror(ret)); + dawnlog_error("Failed to invoke: %s\n", ubus_strerror(ret)); blob_buf_free(&b); } @@ -1227,17 +1310,21 @@ static void enable_rrm(uint32_t id) { int timeout = 1; ret = ubus_invoke(ctx, id, "bss_mgmt_enable", b.head, NULL, NULL, timeout * 1000); if (ret) - fprintf(stderr, "Failed to invoke: %s\n", ubus_strerror(ret)); + dawnlog_error("Failed to invoke: %s\n", ubus_strerror(ret)); blob_buf_free(&b); } static void hostapd_handle_remove(struct ubus_context *ctx_local, struct ubus_subscriber *s, uint32_t id) { + dawnlog_debug_func("Entering..."); + + dawnlog_debug("Object %08x went away\n", id); struct hostapd_sock_entry *hostapd_sock = container_of(s, struct hostapd_sock_entry, subscriber); if (hostapd_sock->id != id) { + dawnlog_debug("ID is not the same!\n"); return; } @@ -1247,6 +1334,8 @@ static void hostapd_handle_remove(struct ubus_context *ctx_local, } bool subscribe(struct ubus_context *ctx_local, struct hostapd_sock_entry *hostapd_entry) { + dawnlog_debug_func("Entering..."); + char subscribe_name[sizeof("hostapd.") + MAX_INTERFACE_NAME + 1]; if (hostapd_entry->subscribed) @@ -1255,13 +1344,13 @@ bool subscribe(struct ubus_context *ctx_local, struct hostapd_sock_entry *hostap sprintf(subscribe_name, "hostapd.%s", hostapd_entry->iface_name); if (ubus_lookup_id(ctx_local, subscribe_name, &hostapd_entry->id)) { - fprintf(stdout, "Failed to lookup ID!"); + dawnlog_warning("Failed to lookup ID!"); subscription_wait(&hostapd_entry->wait_handler); return false; } if (ubus_subscribe(ctx_local, &hostapd_entry->subscriber, hostapd_entry->id)) { - fprintf(stdout, "Failed to register subscriber!"); + dawnlog_warning("Failed to register subscriber!"); subscription_wait(&hostapd_entry->wait_handler); return false; } @@ -1280,6 +1369,8 @@ bool subscribe(struct ubus_context *ctx_local, struct hostapd_sock_entry *hostap enable_rrm(hostapd_entry->id); ubus_get_rrm(); + dawnlog_debug("Subscribed to: %s\n", hostapd_entry->iface_name); + return true; } @@ -1290,6 +1381,8 @@ wait_cb(struct ubus_context *ctx_local, struct ubus_event_handler *ev_handler, "path", BLOBMSG_TYPE_STRING }; + dawnlog_debug_func("Entering..."); + struct blob_attr *attr; const char *path; struct hostapd_sock_entry *sub = container_of(ev_handler, @@ -1318,6 +1411,8 @@ bool subscriber_to_interface(const char *ifname) { struct hostapd_sock_entry *hostapd_entry; + dawnlog_debug_func("Entering..."); + hostapd_entry = dawn_calloc(1, sizeof(struct hostapd_sock_entry)); strcpy(hostapd_entry->iface_name, ifname); @@ -1331,7 +1426,7 @@ bool subscriber_to_interface(const char *ifname) { hostapd_entry->subscribed = false; if (ubus_register_subscriber(ctx, &hostapd_entry->subscriber)) { - fprintf(stderr, "Failed to register subscriber!"); + dawnlog_error("Failed to register subscriber!"); return false; } @@ -1345,13 +1440,15 @@ void subscribe_to_new_interfaces(const char *hostapd_sock_path) { struct dirent *entry; struct hostapd_sock_entry *sub = NULL; + dawnlog_debug_func("Entering..."); + if (ctx == NULL) { return; } dirp = opendir(hostapd_sock_path); // error handling? if (!dirp) { - fprintf(stderr, "[SUBSCRIBING] No hostapd sockets!\n"); + dawnlog_error("[SUBSCRIBING] No hostapd sockets!\n"); return; } while ((entry = readdir(dirp)) != NULL) { @@ -1378,6 +1475,8 @@ void subscribe_to_new_interfaces(const char *hostapd_sock_path) { static char get_rrm_mode_char(int val) { + dawnlog_debug_func("Entering..."); + switch (val) { case WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE: return 'p'; @@ -1392,6 +1491,8 @@ static char get_rrm_mode_char(int val) const static char* get_rrm_mode_string(int *rrm_mode_order) { static char rrm_mode_string [__RRM_BEACON_RQST_MODE_MAX + 1] = {0}; + dawnlog_debug_func("Entering..."); + for (int n = 0; n < __RRM_BEACON_RQST_MODE_MAX && rrm_mode_order[n]; n++) rrm_mode_string[n] = get_rrm_mode_char(rrm_mode_order[n]); return rrm_mode_string; @@ -1402,6 +1503,8 @@ int uci_send_via_network() void *metric, *times, *band_table, *band_entry; struct blob_buf b = {0}; + dawnlog_debug_func("Entering..."); + blob_buf_init(&b, 0); blobmsg_add_string(&b, "version", DAWN_CONFIG_VERSION); metric = blobmsg_open_table(&b, "metric"); @@ -1469,6 +1572,11 @@ int uci_send_via_network() } int build_hearing_map_sort_client(struct blob_buf *b) { + dawnlog_debug_func("Entering..."); + + if (dawnlog_showing(DAWNLOG_DEBUG)) + print_probe_array(); + pthread_mutex_lock(&probe_array_mutex); void *client_list, *ap_list, *ssid_list; @@ -1554,6 +1662,8 @@ int build_network_overview(struct blob_buf *b) { char client_mac_buf[20]; struct hostapd_sock_entry *sub; + dawnlog_debug_func("Entering..."); + bool add_ssid = true; for (ap* m = ap_set; m != NULL; m = m->next_ap) { if(add_ssid) @@ -1645,6 +1755,8 @@ static void blobmsg_add_nr(struct blob_buf *b_local, ap *i) { void* nr_entry = blobmsg_open_array(b_local, NULL); char mac_buf[20]; + dawnlog_debug_func("Entering..."); + sprintf(mac_buf, MACSTRLOWER, MAC2STR(i->bssid_addr.u8)); blobmsg_add_string(b_local, NULL, mac_buf); @@ -1654,6 +1766,8 @@ static void blobmsg_add_nr(struct blob_buf *b_local, ap *i) { } static int mac_is_in_entry_list(const struct dawn_mac mac, const struct mac_entry_s *list) { + dawnlog_debug_func("Entering..."); + for (const struct mac_entry_s *i = list; i; i = i->next_mac) if (mac_is_equal_bb(i->mac, mac)) return 1; @@ -1670,6 +1784,8 @@ int ap_get_nr(struct blob_buf *b_local, struct dawn_mac own_bssid_addr, const ch ap *i, *own_ap; struct mac_entry_s *preferred_list, *n; + dawnlog_debug_func("Entering..."); + void* nbs = blobmsg_open_array(b_local, "list"); own_ap = ap_array_get_ap(own_bssid_addr, (uint8_t*)ssid); @@ -1700,6 +1816,8 @@ int ap_get_nr(struct blob_buf *b_local, struct dawn_mac own_bssid_addr, const ch } void uloop_add_data_cbs() { + dawnlog_debug_func("Entering..."); + uloop_timeout_add(&probe_timeout); // callback = remove_probe_array_cb uloop_timeout_add(&client_timeout); // callback = remove_client_array_cb uloop_timeout_add(&ap_timeout); // callback = remove_ap_array_cb @@ -1712,14 +1830,12 @@ void uloop_add_data_cbs() { // TODO: Move mutex handling to remove_??? function to make test harness simpler? // Or not needed as test harness not threaded? void remove_probe_array_cb(struct uloop_timeout* t) { + dawnlog_debug_func("Entering..."); + pthread_mutex_lock(&probe_array_mutex); -#ifndef DAWN_NO_OUTPUT - printf("[Thread] : Removing old probe entries!\n"); -#endif + dawnlog_debug("[Thread] : Removing old probe entries!\n"); remove_old_probe_entries(time(0), timeout_config.remove_probe); -#ifndef DAWN_NO_OUTPUT - printf("[Thread] : Removing old entries finished!\n"); -#endif + dawnlog_debug("[Thread] : Removing old entries finished!\n"); pthread_mutex_unlock(&probe_array_mutex); uloop_timeout_set(&probe_timeout, timeout_config.remove_probe * 1000); } @@ -1727,10 +1843,10 @@ void remove_probe_array_cb(struct uloop_timeout* t) { // TODO: Move mutex handling to remove_??? function to make test harness simpler? // Or not needed as test harness not threaded? void remove_client_array_cb(struct uloop_timeout* t) { + dawnlog_debug_func("Entering..."); + pthread_mutex_lock(&client_array_mutex); -#ifndef DAWN_NO_OUTPUT - printf("[Thread] : Removing old client entries!\n"); -#endif + dawnlog_debug("[Thread] : Removing old client entries!\n"); remove_old_client_entries(time(0), timeout_config.update_client); pthread_mutex_unlock(&client_array_mutex); uloop_timeout_set(&client_timeout, timeout_config.update_client * 1000); @@ -1739,10 +1855,10 @@ void remove_client_array_cb(struct uloop_timeout* t) { // TODO: Move mutex handling to remove_??? function to make test harness simpler? // Or not needed as test harness not threaded? void remove_ap_array_cb(struct uloop_timeout* t) { + dawnlog_debug_func("Entering..."); + pthread_mutex_lock(&ap_array_mutex); -#ifndef DAWN_NO_OUTPUT - printf("[ULOOP] : Removing old ap entries!\n"); -#endif + dawnlog_debug("[ULOOP] : Removing old ap entries!\n"); remove_old_ap_entries(time(0), timeout_config.remove_ap); pthread_mutex_unlock(&ap_array_mutex); uloop_timeout_set(&ap_timeout, timeout_config.remove_ap * 1000); @@ -1751,10 +1867,10 @@ void remove_ap_array_cb(struct uloop_timeout* t) { // TODO: Move mutex handling to (new) remove_??? function to make test harness simpler? // Or not needed as test harness not threaded? void denied_req_array_cb(struct uloop_timeout* t) { + dawnlog_debug_func("Entering..."); + pthread_mutex_lock(&denied_array_mutex); -#ifndef DAWN_NO_OUTPUT - printf("[ULOOP] : Processing denied authentication!\n"); -#endif + dawnlog_debug("[ULOOP] : Processing denied authentication!\n"); remove_old_denied_req_entries(time(0), timeout_config.denied_req_threshold, true); @@ -1765,6 +1881,8 @@ void denied_req_array_cb(struct uloop_timeout* t) { int send_add_mac(struct dawn_mac client_addr) { struct blob_buf b = {0}; + dawnlog_debug_func("Entering..."); + blob_buf_init(&b, 0); blobmsg_add_macaddr(&b, "addr", client_addr); send_blob_attr_via_network(b.head, "addmac"); diff --git a/src/utils/utils.c b/src/utils/utils.c index 0ca6c11..3d05b21 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -1,5 +1,10 @@ #include #include +#include + +#include +#include +#include #include "utils.h" @@ -20,3 +25,106 @@ int string_is_greater(char *str, char *str_2) { } return length_1 > length_2; } + +// Size implied by https://man7.org/linux/man-pages/man3/strerror.3.html +char dawnlog_pbuf[1024] = "NOT SET"; + +static int _logdest = DAWNLOG_DEST_SYSLOG; // Assume daemon, logging to syslog +static int _logmin = DAWNLOG_ALWAYS; // Anything of lower priority is suppressed + +void dawnlog_dest(int logdest) +{ + _logdest = logdest; +} + +void dawnlog_minlevel(int level) +{ + // Don't allow certain prioriites to be suppressed + if (level < DAWNLOG_ALWAYS) + { + _logmin = level; + } +} + +int dawnlog_showing(int level) +{ + return(level >= _logmin); +} + +void dawnlog(int level, const char* fmt, ...) +{ + + if ((level & DAWNLOG_PRIMASK) >= _logmin) + { + // Attempt to replicate what perror() does... + // dawnlog_buf is already referenced by macro expanded format string, so set it to a value + if ((level & DAWNLOG_PERROR) == DAWNLOG_PERROR) + { + strerror_r(errno, dawnlog_pbuf, 1024); + } + + va_list ap; + va_start(ap, fmt); + + int sl = LOG_NOTICE; // Should always be mapped to a different value + char *iotag = "default: "; + + switch (level) + { + case DAWNLOG_ERROR: + sl = LOG_ERR; + iotag = "error: "; + break; + case DAWNLOG_WARNING: + sl = LOG_WARNING; + iotag = "warning: "; + break; + case DAWNLOG_ALWAYS: + sl = LOG_INFO; + iotag = "info: "; + break; + case DAWNLOG_INFO: + sl = LOG_INFO; + iotag = "info: "; + break; + case DAWNLOG_TRACE: + sl = LOG_DEBUG; + iotag = "trace: "; + break; + case DAWNLOG_DEBUG: + sl = LOG_DEBUG; + iotag = "debug: "; + break; + } + + if (_logdest == DAWNLOG_DEST_SYSLOG) + { + vsyslog(sl, fmt, ap); + } + else + { + int l = strlen(fmt); + if (l) + { + FILE* f = (level == DAWNLOG_ERROR || level == DAWNLOG_WARNING) ? stderr : stdout; + + fprintf(f, "%s", iotag); + vfprintf(f, fmt, ap); + + // Messages created for syslog() may not have a closing newline, so add one if using stdio + if (fmt[l - 1] != '\n') + fprintf(f, "\n"); + } + } + + va_end(ap); + } +} + +/* Return pointer to filename part of full path */ +const char* dawnlog_basename(const char* file) +{ + char* xfile = strrchr(file, '/'); + + return(xfile ? xfile + 1 : file); +}