6 #include "client_auth.h"
7 #include "common/interrupt.h"
10 struct ClientAuth client_auth;
13 static void auth_loop()
15 while (!interrupt.set) switch (client_auth.state) {
18 free(client_auth.name);
20 if (!(client_auth.name = linenoise("Enter name: ")))
23 printf("[access] authenticating as %s...\n", client_auth.name);
24 client_auth.state = AUTH_WAIT;
26 dragonnet_peer_send_ToServerAuth(client, &(ToServerAuth) {
27 .name = client_auth.name,
30 __attribute__((fallthrough));
33 pthread_cond_wait(&client_auth.cv, &client_auth.mtx);
41 void client_auth_init()
43 client_auth.name = NULL;
44 pthread_cond_init(&client_auth.cv, NULL);
45 pthread_mutex_init(&client_auth.mtx, NULL);
47 pthread_mutex_lock(&client_auth.mtx);
48 client_auth.state = AUTH_INIT;
49 flag_sub(&interrupt, &client_auth.cv); // make sure Ctrl+C will work during AUTH_WAIT
53 flag_uns(&interrupt, &client_auth.cv);
55 if (client_auth.state != AUTH_SUCCESS) {
56 fprintf(stderr, "[error] authentication failed due to interruption or read failure\n");
60 pthread_mutex_unlock(&client_auth.mtx);
63 void client_auth_deinit()
65 pthread_cond_destroy(&client_auth.cv);
66 pthread_mutex_destroy(&client_auth.mtx);
67 free(client_auth.name);