Ignore:
Timestamp:
2015-04-06T10:47:51Z (10 years ago)
Author:
Jan Kolarik <kolarik@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d7dadcb4
Parents:
59fa7ab
Message:

Scanning whole 2.4GHz spectrum, created supplicant for managing connection between device STA and AP, finished association process between STA and AP, handling 4way handshake protocol used for key management, written needed cryptographic algorithms (AES, SHA1, HMAC, PBKDF2) for CCMP protocol, data communication on OPEN/CCMP networks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/wifi_supplicant/wifi_supplicant.c

    r59fa7ab r1dcc0b9  
    3636#include <ieee80211_iface.h>
    3737
    38 #include <malloc.h>
    3938#include <errno.h>
    4039#include <stdio.h>
     
    4241
    4342#define NAME "wifi_supplicant"
     43
     44#define enum_name(name_arr, i) ((i < 0) ? "NA" : name_arr[i])
     45
     46static const char* ieee80211_security_type_strs[] = {
     47        "OPEN", "WEP", "WPA", "WPA2"
     48};
     49
     50static const char* ieee80211_security_alg_strs[] = {
     51        "WEP40", "WEP104", "CCMP", "TKIP"
     52};
     53
     54static const char* ieee80211_security_auth_strs[] = {
     55        "PSK", "8021X"
     56};
    4457
    4558static void print_syntax(void)
     
    4962        printf("\t<cmd> is:\n");
    5063        printf("\tlist - list wifi devices in <index>: <name> format\n");
    51         printf("\tscan <index> - output scan results for given device\n");
     64        printf("\tscan <index> [-n] - output scan results (force scan "
     65                "immediately)\n");
     66        printf("\tconnect <index> <ssid_prefix> [<password>] - connect to "
     67                "network\n");
     68        printf("\tdisconnect <index> - disconnect from network\n");
    5269}
    5370
     
    98115        }
    99116       
    100         if(i > count - 1) {
     117        if(i >= count) {
    101118                printf("Invalid wifi index.\n");
    102119                free(wifis);
     
    104121        }
    105122
    106         async_sess_t *sess = loc_service_connect(EXCHANGE_SERIALIZE, wifis[i], 0);
     123        async_sess_t *sess =
     124                loc_service_connect(EXCHANGE_SERIALIZE, wifis[i], 0);
    107125        if (sess == NULL) {
    108126                printf("Error connecting to service.\n");
     
    144162}
    145163
    146 static int wifi_connect(uint32_t index, char *ssid, char *password)
    147 {
    148         assert(ssid);
     164static int wifi_connect(uint32_t index, char *ssid_start, char *password)
     165{
     166        assert(ssid_start);
    149167       
    150168        async_sess_t *sess = get_wifi_by_index(index);
    151169        if (sess == NULL) {
    152                 printf("Specified WIFI doesn't exist or cannot connect to it.\n");
     170                printf("Specified WIFI doesn't exist or cannot connect to "
     171                        "it.\n");
    153172                return EINVAL;
    154173        }
    155174       
    156         int rc = ieee80211_connect(sess, ssid, password);
     175        int rc = ieee80211_connect(sess, ssid_start, password);
    157176        if(rc != EOK) {
    158                 printf("Failed connecting to network. Error: %d\n", rc);
    159                 return EINVAL;
    160         }
    161        
    162         printf("Successfully connected to %s!\n", ssid);
    163        
    164         return EOK;
    165 }
    166 
    167 static int wifi_scan(uint32_t index)
    168 {
    169         ieee80211_scan_results_t scan_results;
    170        
     177                if(rc == EREFUSED) {
     178                        printf("Device is not ready yet.\n");                   
     179                } else if(rc == ETIMEOUT) {
     180                        printf("Timeout when authenticating to network.\n");
     181                } else if(rc == EPERM) {
     182                        printf("Bad password provided.\n");
     183                } else {
     184                        printf("Error when connecting to network. "
     185                                "Error: %d\n", rc);
     186                }
     187               
     188                return rc;
     189        }
     190       
     191        printf("Successfully connected to network!\n");
     192       
     193        return EOK;
     194}
     195
     196static int wifi_disconnect(uint32_t index)
     197{
    171198        async_sess_t *sess = get_wifi_by_index(index);
    172199        if (sess == NULL) {
    173                 printf("Specified WIFI doesn't exist or cannot connect to it.\n");
     200                printf("Specified WIFI doesn't exist or cannot connect to "
     201                        "it.\n");
    174202                return EINVAL;
    175203        }
    176204       
    177         int rc = ieee80211_get_scan_results(sess, &scan_results);
     205        int rc = ieee80211_disconnect(sess);
    178206        if(rc != EOK) {
    179                 printf("Failed to fetch scan results. Error: %d\n", rc);
     207                if(rc == EREFUSED) {
     208                        printf("Device is not ready yet.\n");
     209                } else if(rc == EINVAL) {
     210                        printf("Not connected to any WiFi network.\n");
     211                } else {
     212                        printf("Error when disconnecting from network. "
     213                                "Error: %d\n", rc);
     214                }
     215                return rc;
     216        }
     217       
     218        printf("Successfully disconnected.\n");
     219       
     220        return EOK;
     221}
     222
     223static int wifi_scan(uint32_t index, bool now)
     224{
     225        ieee80211_scan_results_t scan_results;
     226       
     227        async_sess_t *sess = get_wifi_by_index(index);
     228        if (sess == NULL) {
     229                printf("Specified WIFI doesn't exist or cannot connect to "
     230                        "it.\n");
    180231                return EINVAL;
    181232        }
     233       
     234        int rc = ieee80211_get_scan_results(sess, &scan_results, now);
     235        if(rc != EOK) {
     236                if(rc == EREFUSED) {
     237                        printf("Device is not ready yet.\n");
     238                } else {
     239                        printf("Failed to fetch scan results. Error: %d\n", rc);
     240                }
     241               
     242                return rc;
     243        }
     244       
     245        if(scan_results.length == 0)
     246                return EOK;
     247       
     248        printf("%16.16s %17s %4s %5s %5s %7s %7s\n",
     249                "SSID", "MAC", "CHAN", "TYPE", "AUTH", "UNI-ALG", "GRP-ALG");
    182250       
    183251        for(int i = 0; i < scan_results.length; i++) {
    184252                ieee80211_scan_result_t result = scan_results.results[i];
    185253               
    186                 printf("SSID: %s, MAC addr: %s\n", result.ssid,
    187                         nic_addr_format(&result.bssid));
     254                printf("%16.16s %17s %4d %5s %5s %7s %7s\n",
     255                        result.ssid,
     256                        nic_addr_format(&result.bssid),
     257                        result.channel,
     258                        enum_name(ieee80211_security_type_strs,
     259                                result.security.type),
     260                        enum_name(ieee80211_security_auth_strs,
     261                                result.security.auth),
     262                        enum_name(ieee80211_security_alg_strs,
     263                                result.security.pair_alg),
     264                        enum_name(ieee80211_security_alg_strs,
     265                                result.security.group_alg)
     266                );
    188267        }
    189268       
     
    197276       
    198277        if(argc == 2) {
    199                 if (!str_cmp(argv[1], "list")) {
     278                if(!str_cmp(argv[1], "list")) {
    200279                        return wifi_list();
    201280                }
    202281        } else if(argc > 2) {
    203282                rc = str_uint32_t(argv[2], NULL, 10, false, &index);
    204                 if (rc != EOK) {
     283                if(rc != EOK) {
    205284                        printf(NAME ": Invalid argument.\n");
    206285                        print_syntax();
    207286                        return EINVAL;
    208287                }
    209                 if (!str_cmp(argv[1], "scan")) {
    210                         return wifi_scan(index);
    211                 } else if (!str_cmp(argv[1], "connect")) {
     288                if(!str_cmp(argv[1], "scan")) {
     289                        bool now = false;
     290                        if(argc > 3)
     291                                if(!str_cmp(argv[3], "-n"))
     292                                        now = true;
     293                        return wifi_scan(index, now);
     294                } else if(!str_cmp(argv[1], "connect")) {
    212295                        char *pass = NULL;
    213296                        if(argc > 3) {
     
    216299                                return wifi_connect(index, argv[3], pass);
    217300                        }
     301                } else if(!str_cmp(argv[1], "disconnect")) {
     302                        return wifi_disconnect(index);
    218303                }
    219304        }
Note: See TracChangeset for help on using the changeset viewer.