about summary refs log tree commit diff
path: root/src/libwpactrl/wpactrl_associate.c
blob: 08f365677a921cdd0058b25f47f34e94db219ebc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* ISC license. */

#include <stdint.h>
#include <errno.h>
#include <bcnm/wpactrl.h>

int wpactrl_associate (wpactrl_t *a, char const *ssid, char const *psk, tain_t *stamp)
{
  uint32_t id ;
  int r = wpactrl_findnetwork(a, ssid, &id, stamp) ;
  if (r < 0) return 0 ;
  if (!r)
  {
    if (!wpactrl_addnetwork(a, &id, stamp)) goto err ;
  }

  if (wpactrl_setnetworkoption(a, id, "ssid", ssid, stamp) != WPA_OK) goto err ;
  if (psk)
  {
    wparesponse_t r = wpactrl_setnetworkoption(a, id, "key_mgmt", "WPA-PSK WPA-PSK-SHA256", stamp) ;
    if (r != WPA_OK)
    {
      if (r != WPA_FAIL
       || wpactrl_setnetworkoption(a, id, "key_mgmt", "WPA-PSK", stamp) != WPA_OK) goto err ;
    }
    if (wpactrl_setnetworkoption(a, id, "mem_only_psk", "1", stamp) != WPA_OK
     || wpactrl_setnetworkoption(a, id, "psk", psk, stamp) != WPA_OK) goto err ;
  }
  else
  {
    if (wpactrl_setnetworkoption(a, id, "key_mgmt", "NONE", stamp) != WPA_OK)
      goto err ;
  }

  if (wpactrl_selectnetwork(a, id, stamp) != WPA_OK) goto err ;
  return 1 ;

 err:
  return (errno = EIO, 0) ;
}