From 3b30d1ba3e51e53e70cb45c02c8f62638e8c9f6b Mon Sep 17 00:00:00 2001 From: Ian Clowes Date: Mon, 25 May 2020 12:45:42 +0100 Subject: [PATCH] Merged remote head --- src/CMakeLists.txt | 4 +- src/include/datastorage.h | 64 +++++++------- src/include/ubus.h | 35 -------- src/include/uface.h | 41 ++++++++- src/network/tcpsocket.c | 6 +- src/storage/datastorage.c | 10 ++- src/storage/uface.c | 32 +------ src/test/storage_test.c | 93 -------------------- src/test/test_storage.c | 181 ++++++++++++++++++++++++++++++++++++++ src/utils/dawn_uci.c | 8 +- src/utils/ubus.c | 10 ++- src/utils/utils.c | 23 +++-- 12 files changed, 290 insertions(+), 217 deletions(-) delete mode 100644 src/test/storage_test.c create mode 100644 src/test/test_storage.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0b777da..52cdce5 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -47,12 +47,12 @@ SET(SOURCES include/ieee80211_utils.h) SET(SOURCES_TEST_STORAGE - test/storage_test.c + test/test_storage.c include/uface.h - include/utils.h utils/utils.c + include/utils.h storage/datastorage.c include/datastorage.h diff --git a/src/include/datastorage.h b/src/include/datastorage.h index f6c4094..5e2c0c6 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -18,7 +18,7 @@ // ---------------- Defines ------------------- #define MAC_LIST_LENGTH 100 -// ---------------- Structs ---------------- +// ---------------- Global variables ---------------- uint8_t mac_list[MAC_LIST_LENGTH][ETH_ALEN]; // ---------------- Functions ---------- @@ -28,11 +28,12 @@ int insert_to_maclist(uint8_t mac[]); int mac_in_maclist(uint8_t mac[]); +int mac_is_equal(uint8_t addr1[], uint8_t addr2[]); + +int mac_is_greater(uint8_t addr1[], uint8_t addr2[]); /* Metric */ -struct probe_metric_s dawn_metric; - // ---------------- Structs ---------------- struct probe_metric_s { int ap_weight; @@ -95,13 +96,11 @@ struct network_config_s { int bandwidth; }; +// ---------------- Global variables ---------------- struct network_config_s network_config; struct time_config_s timeout_config; -// ---------------- Global variables ---------------- struct probe_metric_s dawn_metric; -extern int probe_entry_last; - /* Probe, Auth, Assoc */ @@ -140,23 +139,21 @@ typedef struct hostapd_notify_entry_s { typedef struct auth_entry_s assoc_entry; -#define DENY_REQ_ARRAY_LEN 100 -struct auth_entry_s denied_req_array[DENY_REQ_ARRAY_LEN]; -pthread_mutex_t denied_array_mutex; - -auth_entry insert_to_denied_req_array(auth_entry entry, int inc_counter); - // ---------------- Defines ---------------- +#define DENY_REQ_ARRAY_LEN 100 #define PROBE_ARRAY_LEN 1000 #define SSID_MAX_LEN 32 #define NEIGHBOR_REPORT_LEN 200 // ---------------- Global variables ---------------- -struct probe_entry_s probe_array[PROBE_ARRAY_LEN]; -pthread_mutex_t probe_array_mutex; +struct auth_entry_s denied_req_array[DENY_REQ_ARRAY_LEN]; extern int denied_req_last; -auth_entry denied_req_array_delete(auth_entry entry); +pthread_mutex_t denied_array_mutex; + +struct probe_entry_s probe_array[PROBE_ARRAY_LEN]; +extern int probe_entry_last; +pthread_mutex_t probe_array_mutex; // ---------------- Functions ---------------- probe_entry insert_to_array(probe_entry entry, int inc_counter, int save_80211k, int is_beacon); @@ -167,13 +164,19 @@ probe_entry probe_array_delete(probe_entry entry); probe_entry probe_array_get_entry(uint8_t bssid_addr[], uint8_t client_addr[]); +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_auth_entry(auth_entry entry); +int eval_probe_metric(struct probe_entry_s probe_entry); -void uloop_add_data_cbs(); +auth_entry denied_req_array_delete(auth_entry entry); + +auth_entry insert_to_denied_req_array(auth_entry entry, int inc_counter); + +void print_auth_entry(auth_entry entry); /* AP, Client */ @@ -226,16 +229,13 @@ typedef struct ap_s { #define TIME_THRESHOLD_CLIENT_KICK 60 // ---------------- Global variables ---------------- -struct client_s client_array[ARRAY_CLIENT_LEN]; -pthread_mutex_t client_array_mutex; struct ap_s ap_array[ARRAY_AP_LEN]; -pthread_mutex_t ap_array_mutex; extern int ap_entry_last; +pthread_mutex_t ap_array_mutex; + +struct client_s client_array[ARRAY_CLIENT_LEN]; extern int client_entry_last; - -int mac_is_equal(uint8_t addr1[], uint8_t addr2[]); - -int mac_is_greater(uint8_t addr1[], uint8_t addr2[]); +pthread_mutex_t client_array_mutex; // ---------------- Functions ---------------- @@ -243,6 +243,8 @@ int probe_array_update_rssi(uint8_t bssid_addr[], uint8_t client_addr[], uint32_ int probe_array_update_rcpi_rsni(uint8_t bssid_addr[], uint8_t client_addr[], uint32_t rcpi, uint32_t rsni, int send_network); +void remove_old_client_entries(time_t current_time, long long int threshold); + void insert_client_to_array(client entry); void kick_clients(uint8_t bssid[], uint32_t id); @@ -255,8 +257,12 @@ void print_client_array(); void print_client_entry(client entry); +int is_connected_somehwere(uint8_t client_addr[]); + ap insert_to_ap_array(ap entry); +void remove_old_ap_entries(time_t current_time, long long int threshold); + void print_ap_array(); ap ap_array_get_ap(uint8_t bssid_addr[]); @@ -278,14 +284,4 @@ char *sort_string; // ---------------- Functions ------------------- int better_ap_available(uint8_t bssid_addr[], uint8_t client_addr[], char* neighbor_report, int automatic_kick); -int is_connected_somehwere(uint8_t client_addr[]); - -void remove_old_probe_entries(time_t current_time, long long int threshold); - -void remove_old_client_entries(time_t current_time, long long int threshold); - -void remove_old_ap_entries(time_t current_time, long long int threshold); - -int eval_probe_metric(struct probe_entry_s probe_entry); - #endif diff --git a/src/include/ubus.h b/src/include/ubus.h index 9c69fb8..e2315dd 100644 --- a/src/include/ubus.h +++ b/src/include/ubus.h @@ -85,16 +85,6 @@ int parse_to_clients(struct blob_attr *msg, int do_kick, uint32_t id); */ int parse_to_hostapd_notify(struct blob_attr *msg, hostapd_notify_entry *notify_req); -/** - * Kick client from hostapd interface. - * @param id - the ubus id. - * @param client_addr - the client adress of the client to kick. - * @param reason - the reason to kick the client. - * @param deauth - if the client should be deauthenticated. - * @param ban_time - the ban time the client is not allowed to connect again. - */ -void del_client_interface(uint32_t id, const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time); - /** * Kick client from all hostapd interfaces. * @param client_addr - the client adress of the client to kick. @@ -104,27 +94,12 @@ void del_client_interface(uint32_t id, const uint8_t *client_addr, uint32_t reas */ void del_client_all_interfaces(const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time); -void wnm_disassoc_imminent(uint32_t id, const uint8_t *client_addr, char* dest_ap, uint32_t duration); - -/** - * Send probe message via the network. - * @param probe_entry - * @return - */ -int ubus_send_probe_via_network(struct probe_entry_s probe_entry); - /** * Update the hostapd sockets. * @param t */ void update_hostapd_sockets(struct uloop_timeout *t); -/** - * Set client timer for updating the clients. - * @param time - */ -void add_client_update_timer(time_t time); - /** * Handle network messages. * @param msg @@ -148,14 +123,6 @@ int send_blob_attr_via_network(struct blob_attr *msg, char *method); */ void blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t *addr); -/** - * Function to set the probe counter to the min probe request. - * This allows that the client is able to connect directly without sending multiple probe requests to the Access Point. - * @param client_addr - * @return - */ -int send_set_probe(uint8_t client_addr[]); - /** * Send control message to all hosts to add the mac to a don't control list. * @param client_addr @@ -165,8 +132,6 @@ int send_add_mac(uint8_t *client_addr); int uci_send_via_network(); -void ubus_send_beacon_report(uint8_t client[], int id); - int build_hearing_map_sort_client(struct blob_buf *b); int build_network_overview(struct blob_buf *b); diff --git a/src/include/uface.h b/src/include/uface.h index 81527f4..ae3a6bd 100644 --- a/src/include/uface.h +++ b/src/include/uface.h @@ -1,8 +1,41 @@ -void send_beacon_report(uint8_t client[], int id); -int send_probe_via_network(struct probe_entry_s probe_entry); +#ifndef __DAWN_UFACE_H +#define __DAWN_UFACE_H -int send_set_probe(uint8_t client_addr[]); -void wnm_disassoc_imminent(uint32_t id, const uint8_t *client_addr, char* dest_ap, uint32_t duration); +/** + * Set client timer for updating the clients. + * @param time + */ void add_client_update_timer(time_t time); + +/** + * Kick client from hostapd interface. + * @param id - the ubus id. + * @param client_addr - the client adress of the client to kick. + * @param reason - the reason to kick the client. + * @param deauth - if the client should be deauthenticated. + * @param ban_time - the ban time the client is not allowed to connect again. + */ void del_client_interface(uint32_t id, const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time); +/** + * Function to set the probe counter to the min probe request. + * This allows that the client is able to connect directly without sending multiple probe requests to the Access Point. + * @param client_addr + * @return + */ +int send_set_probe(uint8_t client_addr[]); + +void ubus_send_beacon_report(uint8_t client[], int id); + +/** + * Send probe message via the network. + * @param probe_entry + * @return + */ +int ubus_send_probe_via_network(struct probe_entry_s probe_entry); + +void uloop_add_data_cbs(); + +void wnm_disassoc_imminent(uint32_t id, const uint8_t *client_addr, char* dest_ap, uint32_t duration); + +#endif // __DAWN_UFACE_H diff --git a/src/network/tcpsocket.c b/src/network/tcpsocket.c index 3e75d0a..c194028 100644 --- a/src/network/tcpsocket.c +++ b/src/network/tcpsocket.c @@ -162,11 +162,10 @@ static void client_not_be_used_read_cb(struct ustream *s, int bytes) { static void connect_cb(struct uloop_fd *f, unsigned int events) { - struct network_con_s *entry = container_of(f, - struct network_con_s, fd); + struct network_con_s *entry = container_of(f, struct network_con_s, fd); if (f->eof || f->error) { - fprintf(stderr, "Connection failed\n"); + fprintf(stderr, "Connection failed (%s)\n", f->eof ? "EOF" : "ERROR"); close(entry->fd.fd); list_del(&entry->list); free(entry); @@ -219,6 +218,7 @@ int add_tcp_conncection(char *ipv4, int port) { uloop_fd_add(&tcp_entry->fd, ULOOP_WRITE | ULOOP_EDGE_TRIGGER); printf("New TCP connection to %s:%d\n", ipv4, port); + fprintf(stderr, "New TCP connection to %s:%d\n", ipv4, port); list_add(&tcp_entry->list, &tcp_sock_list); return 0; diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index fcd7ec9..b5ae7f8 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -68,10 +68,13 @@ void send_beacon_reports(uint8_t bssid[], int id) { if (!mac_is_equal(client_array[j].bssid_addr, bssid)) { break; } - send_beacon_report(client_array[j].client_addr, id); + ubus_send_beacon_report(client_array[j].client_addr, id); } pthread_mutex_unlock(&client_array_mutex); } + +// TODO: Can metric be cached once calculated? Add score_fresh indicator and reset when signal changes +// TODO: as rest of values look to be static fr any given entry. int eval_probe_metric(struct probe_entry_s probe_entry) { int score = 0; @@ -583,7 +586,7 @@ int probe_array_update_rssi(uint8_t bssid_addr[], uint8_t client_addr[], uint32_ updated = 1; if(send_network) { - send_probe_via_network(probe_array[i]); + ubus_send_probe_via_network(probe_array[i]); } break; //TODO: break?! @@ -612,7 +615,7 @@ int probe_array_update_rcpi_rsni(uint8_t bssid_addr[], uint8_t client_addr[], ui updated = 1; if(send_network) { - send_probe_via_network(probe_array[i]); + ubus_send_probe_via_network(probe_array[i]); } break; //TODO: break?! @@ -841,6 +844,7 @@ void insert_macs_from_file() { size_t len = 0; ssize_t read; +// TODO: Loading to array is not constrained by array checks. Buffer overrun can occur. fp = fopen("/tmp/dawn_mac_list", "r"); if (fp == NULL) exit(EXIT_FAILURE); diff --git a/src/storage/uface.c b/src/storage/uface.c index 0c82b89..ffa7d30 100644 --- a/src/storage/uface.c +++ b/src/storage/uface.c @@ -4,36 +4,6 @@ #include "ubus.h" #include "uface.h" -void send_beacon_report(uint8_t client[], int id) -{ - ubus_send_beacon_report(client, id); -} - -int send_set_probe(uint8_t client_addr[]) -{ - return send_set_probe(client_addr); -} - -void wnm_disassoc_imminent(uint32_t id, const uint8_t *client_addr, char* dest_ap, uint32_t duration) -{ - wnm_disassoc_imminent(id, client_addr, dest_ap, duration); -} - -void add_client_update_timer(time_t time) -{ - add_client_update_timer(time); -} - -void del_client_interface(uint32_t id, const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time) -{ - del_client_interface(id, client_addr, reason, deauth, ban_time); -} - -int send_probe_via_network(struct probe_entry_s probe_entry) -{ - return ubus_send_probe_via_network(probe_entry); -} - void remove_probe_array_cb(struct uloop_timeout *t); struct uloop_timeout probe_timeout = { @@ -113,6 +83,8 @@ void denied_req_array_cb(struct uloop_timeout *t) { // maybe delete again? if (insert_to_maclist(denied_req_array[i].client_addr) == 0) { send_add_mac(denied_req_array[i].client_addr); +// TODO: File can grow arbitarily large. Resource consumption risk. +// TODO: Consolidate use of file across source: shared resource for name, single point of access? write_mac_to_file("/tmp/dawn_mac_list", denied_req_array[i].client_addr); } } diff --git a/src/test/storage_test.c b/src/test/storage_test.c deleted file mode 100644 index e2acae7..0000000 --- a/src/test/storage_test.c +++ /dev/null @@ -1,93 +0,0 @@ -#include -#include - -#include "dawn_iwinfo.h" -#include "utils.h" -#include "ieee80211_utils.h" - -#include "datastorage.h" -#include "uface.h" - -void ap_array_insert(ap entry); - -ap ap_array_delete(ap entry); - -int main() -{ -ap ap0; -union __attribute__((__packed__)){ -struct { -uint8_t the_mac[6]; -uint8_t packing[2]; -}mac; -uint64_t u64; -} mac_mangler; - - - print_ap_array(); - for (int m = 0; m < 1000; m++) - { - mac_mangler.u64 = m; - memcpy(ap0.bssid_addr, mac_mangler.mac.the_mac, sizeof(ap0.bssid_addr)); - ap_array_insert(ap0); - } - print_ap_array(); - for (int m = 0; m < 1000; m++) - { - mac_mangler.u64 = m; - memcpy(ap0.bssid_addr, mac_mangler.mac.the_mac, sizeof(ap0.bssid_addr)); - ap_array_delete(ap0); - } - print_ap_array(); -} - -void send_beacon_report(uint8_t client[], int id) -{ - printf("send_beacon_report() was called...\n"); -} - -int send_set_probe(uint8_t client_addr[]) -{ - printf("send_set_probe() was called...\n"); - return 0; -} - -void wnm_disassoc_imminent(uint32_t id, const uint8_t *client_addr, char* dest_ap, uint32_t duration) -{ - printf("wnm_disassoc_imminent() was called...\n"); -} - -void add_client_update_timer(time_t time) -{ - printf("add_client_update_timer() was called...\n"); -} - -void del_client_interface(uint32_t id, const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time) -{ - printf("del_client_interface() was called...\n"); -} - -int send_probe_via_network(struct probe_entry_s probe_entry) -{ - printf("send_probe_via_network() was called...\n"); - return 0; -} - -int get_rssi_iwinfo(uint8_t *client_addr) -{ - printf("get_rssi_iwinfo() was called...\n"); - return 0; -} - -int get_expected_throughput_iwinfo(uint8_t *client_addr) -{ - printf("get_expected_throughput_iwinfo() was called...\n"); - return 0; -} - -int get_bandwidth_iwinfo(uint8_t *client_addr, float *rx_rate, float *tx_rate) -{ - printf("get_bandwidth_iwinfo() was called...\n"); - return 0; -} - diff --git a/src/test/test_storage.c b/src/test/test_storage.c new file mode 100644 index 0000000..b91ff71 --- /dev/null +++ b/src/test/test_storage.c @@ -0,0 +1,181 @@ +#include +#include +#include + +#include "dawn_iwinfo.h" +#include "utils.h" +#include "ieee80211_utils.h" + +#include "datastorage.h" +#include "uface.h" + +/*** External functions ***/ +void ap_array_insert(ap entry); +ap ap_array_delete(ap entry); + +/*** Testing structures, etc ***/ +union __attribute__((__packed__)) mac_mangler +{ + struct { + uint8_t b[6]; + uint8_t packing[2]; + } u8; + uint64_t u64; +}; + +/*** Test code */ +int ap_array_helper_auto(int action, int i0, int i1); +int ap_array_helper_auto(int action, int i0, int i1) +{ +int m; +int step = (i0 > i1) ? -1 : 1; +int ret = 0; + + switch (action) + { + case 0: + case 1: + m = i0; + int cont = 1; + while (cont) { + union mac_mangler this_mac; + ap ap0; + + this_mac.u64 = m; + memcpy(ap0.bssid_addr, this_mac.u8.b, sizeof(ap0.bssid_addr)); + if (action == 0) + ap_array_insert(ap0); + else + ap_array_delete(ap0); + + if (m == i1) + cont = 0; + else + m += step; + } + break; + default: + ret = 1; + break; + } + + return ret; +} + +int main(int argc, char** argv) +{ +int ret = 0; +int args_ok = 1; +int arg_consumed = 0; + + printf("DAWN datastorage.c test harness. Ready for commands...\n"); + + int this_arg = 1; + argv++; + + while (args_ok) + { + if (strcmp(*argv, "help") == 0) + { + arg_consumed = 1; + if (this_arg + arg_consumed > argc) goto next_command; + + printf("Help is on its way...\n"); + } + else if (strcmp(*argv, "ap_show") == 0) + { + arg_consumed = 1; + if (this_arg + arg_consumed > argc) goto next_command; + + print_ap_array(); + } + else if (strcmp(*argv, "ap_add_auto") == 0) + { + arg_consumed = 3; + if (this_arg + arg_consumed > argc) goto next_command; + + ap_array_helper_auto(0, atoi(*(argv + 1)), atoi(*(argv + 2))); + } + else if (strcmp(*argv, "ap_del_auto") == 0) + { + arg_consumed = 3; + if (this_arg + arg_consumed > argc) goto next_command; + + ap_array_helper_auto(1, atoi(*(argv + 1)), atoi(*(argv + 2))); + } + else + { + arg_consumed = 1; + if (this_arg + arg_consumed > argc) goto next_command; + + printf("COMMAND \"%s\": Unknown - skipping!\n", *argv); + } + +next_command: + this_arg += arg_consumed; + if (this_arg > argc) + { + printf("Commands are mangled at: \"%s\"!\n", *argv); + args_ok = 0; + } + else if (this_arg == argc) + args_ok = 0; + else + argv += arg_consumed; + } + + printf("\n\nDAWN datastorage.c test harness - finshed. \n"); + + return ret; +} + +void ubus_send_beacon_report(uint8_t client[], int id) +{ + printf("send_beacon_report() was called...\n"); +} + +int send_set_probe(uint8_t client_addr[]) +{ + printf("send_set_probe() was called...\n"); + return 0; +} + +void wnm_disassoc_imminent(uint32_t id, const uint8_t *client_addr, char* dest_ap, uint32_t duration) +{ + printf("wnm_disassoc_imminent() was called...\n"); +} + +void add_client_update_timer(time_t time) +{ + printf("add_client_update_timer() was called...\n"); +} + +void del_client_interface(uint32_t id, const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time) +{ + printf("del_client_interface() was called...\n"); +} + +int ubus_send_probe_via_network(struct probe_entry_s probe_entry) +{ + printf("send_probe_via_network() was called...\n"); + return 0; +} + +int get_rssi_iwinfo(uint8_t *client_addr) +{ + printf("get_rssi_iwinfo() was called...\n"); + return 0; +} + +int get_expected_throughput_iwinfo(uint8_t *client_addr) +{ + printf("get_expected_throughput_iwinfo() was called...\n"); + return 0; +} + +int get_bandwidth_iwinfo(uint8_t *client_addr, float *rx_rate, float *tx_rate) +{ + printf("get_bandwidth_iwinfo() was called...\n"); + return 0; +} + diff --git a/src/utils/dawn_uci.c b/src/utils/dawn_uci.c index 37b3156..d28601c 100644 --- a/src/utils/dawn_uci.c +++ b/src/utils/dawn_uci.c @@ -116,13 +116,17 @@ struct network_config_s uci_get_dawn_network() { } const char *uci_get_dawn_hostapd_dir() { +const char *ret = NULL; + struct uci_element *e; uci_foreach_element(&uci_pkg->sections, e) { struct uci_section *s = uci_to_section(e); if (strcmp(s->type, "hostapd") == 0) { - return uci_lookup_option_string(uci_ctx, s, "hostapd_dir"); + ret = uci_lookup_option_string(uci_ctx, s, "hostapd_dir"); + fprintf(stderr, "Found hostapd_dir = %s\n", ret); + return ret; } } return NULL; @@ -205,4 +209,4 @@ int uci_set_network(char* uci_cmd) } return ret; -} \ No newline at end of file +} diff --git a/src/utils/ubus.c b/src/utils/ubus.c index cad5259..60e8d85 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -17,16 +17,17 @@ #define REQ_TYPE_AUTH 1 #define REQ_TYPE_ASSOC 2 -#include "ubus.h" - #include "networksocket.h" #include "utils.h" #include "dawn_uci.h" #include "dawn_iwinfo.h" -#include "datastorage.h" #include "tcpsocket.h" #include "ieee80211_utils.h" +#include "datastorage.h" +#include "uface.h" +#include "ubus.h" + static struct ubus_context *ctx = NULL; static struct blob_buf b; @@ -707,6 +708,7 @@ int handle_network_msg(char *msg) { // add inactive death... +// TODO: strncmp() look wrong - should all tests be for n = 5 characters? Shorthand checks? if (strncmp(method, "probe", 5) == 0) { probe_entry entry; if (parse_to_probe_req(data_buf.head, &entry) == 0) { @@ -1446,6 +1448,8 @@ static int parse_add_mac_to_file(struct blob_attr *msg) { hwaddr_aton(blobmsg_data(attr), addr); if (insert_to_maclist(addr) == 0) { +// TODO: File can grow arbitarily large. Resource consumption risk. +// TODO: Consolidate use of file across source: shared resource for name, single point of access? write_mac_to_file("/tmp/dawn_mac_list", addr); } } diff --git a/src/utils/utils.c b/src/utils/utils.c index 02616dd..96cb3b7 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -42,30 +42,37 @@ int hwaddr_aton(const char *txt, uint8_t *addr) { b = hex_to_bin(*txt++); if (b < 0) return -1; *addr++ = (a << 4) | b; - if (i < 5 && *txt++ != ':') return -1; +// TODO: Should NUL terminator be checked for? Is aa:bb:cc:dd:ee:ff00 valid input? + if (i < (ETH_ALEN - 1) && *txt++ != ':') return -1; } return 0; } +// TODO: Never called in DAWN code. Remove? +#if 0 +/* Convert badly formed MAC addresses with single digit element to double digit +** eg 11:2:33:4:55:6 to 11:02:33:04:55:06 */` int convert_mac(char *in, char *out) { int i, j = 0; - for (i = 0; i < 6; i++) { - if (in[j + 1] != ':' && in[j + 1] != '\0') { - out[3 * i] = toupper(in[j]); - out[(3 * i) + 1] = toupper(in[j + 1]); - out[(3 * i) + 2] = in[j + 2]; - j += 3; - } else { + for (i = 0; i < ETH_ALEN; i++) { + /* Do we have a single-digit element? */ + if (in[j + 1] == ':' || in[j + 1] == '\0') { out[3 * i] = '0'; out[(3 * i) + 1] = toupper(in[j]); out[(3 * i) + 2] = toupper(in[j + 1]); j += 2; + } else { + out[3 * i] = toupper(in[j]); + out[(3 * i) + 1] = toupper(in[j + 1]); + out[(3 * i) + 2] = in[j + 2]; + j += 3; } } return 0; } +#endif void write_mac_to_file(char *path, uint8_t addr[]) { FILE *f = fopen(path, "a");