added CLI app

This commit is contained in:
itsmattkc committed 2024-05-09 14:51:01 -07:00
1 parent 7e604bb009
commit 7a0599da2d
7 files changed
+242 -42

No files matched your search

+4
View File
@@ -2,5 +2,9 @@ cmake_minimum_required(VERSION 3.29)
project(Vanilla C CXX)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_subdirectory(server)
add_subdirectory(client)
+35 -4
View File
@@ -1,16 +1,47 @@
add_library(vanillaserver SHARED
server.c
status.c
)
add_executable(vanillaserver_cli
main.c
)
target_link_libraries(vanillaserver_cli PRIVATE
vanillaserver
)
target_include_directories(vanillaserver PRIVATE
/home/matt/src/drc-hostap/src/common
/home/matt/src/drc-hostap/src/utils
${CMAKE_CURRENT_SOURCE_DIR}/hostap/src/common
${CMAKE_CURRENT_SOURCE_DIR}/hostap/src/utils
)
# Build WPA supplicant
add_custom_target(
wpa_supplicant
COMMAND make
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/hostap/wpa_supplicant"
)
# Build client library
add_custom_target(
wpa_client_lib
COMMAND make libwpa_client.so
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/hostap/wpa_supplicant"
DEPENDS wpa_supplicant
)
# Make vanillaserver depend on client library
add_dependencies(vanillaserver wpa_client_lib)
# Link wpa_client
target_link_directories(vanillaserver PRIVATE
/home/matt/src/drc-hostap/wpa_supplicant
"${CMAKE_CURRENT_SOURCE_DIR}/hostap/wpa_supplicant"
)
target_link_libraries(vanillaserver PRIVATE
wpa_client
)
)
# Copy files into build directory
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/hostap/wpa_supplicant/wpa_supplicant" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/wpa_supplicant_drc" COPYONLY)
+62
View File
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "server.h"
#include "status.h"
int main(int argc, char **argv)
{
print_status(VANILLA_READY);
char *line = NULL;
size_t size;
while (1) {
ssize_t sz = getline(&line, &size, stdin);
if (sz == -1) {
break;
}
static const int cmd_max_args = 10;
static const int cmd_max_arg_length = 20;
int cmd_nb_arg = 0;
char cmd_args[cmd_max_args][cmd_max_arg_length];
static const char *delim = " \n";
char *token = strtok(line, delim);
while (token && cmd_nb_arg < cmd_max_args) {
strncpy(cmd_args[cmd_nb_arg], token, cmd_max_arg_length);
token = strtok(NULL, delim);
cmd_nb_arg++;
}
if (cmd_nb_arg == 0) {
print_status(VANILLA_UNKNOWN_COMMAND);
continue;
}
if (!strcmp("SYNC", cmd_args[0])) {
if (cmd_nb_arg != 3) {
print_status(VANILLA_INVALID_ARGUMENT);
continue;
}
const char *wireless_interface = cmd_args[1];
const char *code = cmd_args[2];
int sync_status = vanilla_sync_with_console(wireless_interface, atoi(code));
if (sync_status == VANILLA_SUCCESS) {
print_status(VANILLA_SUCCESS);
} else {
print_status(VANILLA_ERROR);
}
} else if (!strcmp("EXIT", cmd_args[0])) {
break;
} else {
print_status(VANILLA_UNKNOWN_COMMAND);
}
}
return 0;
}
+94 -31
View File
@@ -4,43 +4,101 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <unistd.h>
#include <wpa_ctrl.h>
#include "status.h"
int start_wpa_supplicant(const char *wireless_interface, const char *config_file, pid_t *pid)
{
int pipes[2];
pipe(pipes);
pid_t ppid_before_fork = getpid();
(*pid) = fork();
if ((*pid) == 0) {
int r = prctl(PR_SET_PDEATHSIG, SIGHUP);
if (r == -1) {
perror(0);
exit(1);
}
if (getppid() != ppid_before_fork) {
exit(1);
}
dup2(pipes[1], STDOUT_FILENO);
dup2(pipes[1], STDERR_FILENO);
close(pipes[0]);
close(pipes[1]);
// Currently in the child
// char *app = "/home/matt/src/hostap/wpa_supplicant/wpa_supplicant";
char *app = "/usr/local/bin/wpa_supplicant_drc";
if (execl(app, app, "-Dnl80211", "-i", wireless_interface, "-c", config_file, NULL) < 0) {
long path_size = pathconf(".", _PC_PATH_MAX);
char *path_buf = malloc(path_size);
char *wpa_buf = malloc(path_size);
if (!path_buf) {
// Failed to allocate buffer
_exit(1);
}
if (!getcwd(path_buf, path_size)) {
// Failed to get current working directory
_exit(1);
}
snprintf(wpa_buf, path_size, "%s/%s", path_buf, "wpa_supplicant_drc");
free(path_buf);
r = execl(wpa_buf, wpa_buf, "-Dnl80211", "-i", wireless_interface, "-c", config_file, NULL);
free(wpa_buf);
if (r < 0) {
_exit(1);
}
} else if ((*pid) < 0) {
// Fork error
return VANILLA_ERROR_BAD_FORK;
return VANILLA_ERROR;
} else {
// Continuation of parent
close(pipes[1]);
// Give WPA supplicant some time to start (TODO: probably should wait for success message)
sleep(1);
// Wait for WPA supplicant to start
static const int max_attempts = 5;
int nbytes, attempts = 0, success = 0, total_nbytes = 0;
static const char *expected = "Successfully initialized wpa_supplicant";
static const int expected_len = 39; //strlen(expected)
char buf[expected_len];
do {
// Read string from child process
do {
nbytes = read(pipes[0], buf + total_nbytes, expected_len - total_nbytes);
total_nbytes += nbytes;
} while (total_nbytes < expected_len);
/*char buf[4096];
int nbytes = read(pipes[0], buf, sizeof(buf));
printf("Output: (%.*s)\n", nbytes, buf);*/
return VANILLA_ERROR_SUCCESS;
attempts++;
// We got success message!
if (!strncmp(buf, expected, expected_len)) {
success = 1;
break;
}
// Haven't gotten success message (yet), wait and try again
sleep(1);
} while (attempts < max_attempts);
if (success) {
// WPA initialized correctly! Continue with action...
return VANILLA_SUCCESS;
} else {
// Give up
kill((*pid), SIGINT);
return VANILLA_ERROR;
}
}
}
@@ -56,10 +114,11 @@ void wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd, char *buf, size_t
int vanilla_sync_with_console(const char *wireless_interface, uint16_t code)
{
static const char *wireless_conf_file = "/tmp/wireless.conf";
static const char *wireless_conf_file = "/tmp/vanilla_wpa.conf";
FILE *config = fopen(wireless_conf_file, "wb");
if (!config) {
return VANILLA_ERROR_CONFIG_FILE;
print_info("FAILED TO WRITE TEMP CONFIG");
return VANILLA_ERROR;
}
static const char *ctrl_interface = "/var/run/wpa_supplicant_drc";
@@ -69,20 +128,27 @@ int vanilla_sync_with_console(const char *wireless_interface, uint16_t code)
// Start modified WPA supplicant
pid_t pid;
int err = start_wpa_supplicant(wireless_interface, wireless_conf_file, &pid);
if (err != VANILLA_ERROR_SUCCESS) {
return err;
if (err != VANILLA_SUCCESS) {
print_info("FAILED TO START WPA SUPPLICANT");
return VANILLA_ERROR;
}
// Get control interface
int ret;
int ret = VANILLA_ERROR;
char buf[16384];
sprintf(buf, "%s/%s", ctrl_interface, wireless_interface);
snprintf(buf, sizeof(buf), "%s/%s", ctrl_interface, wireless_interface);
printf("%s\n", buf);
struct wpa_ctrl *ctrl = wpa_ctrl_open(buf);
if (!ctrl) {
ret = VANILLA_ERROR_UNKNOWN;
while (!ctrl) {
print_info("FAILED TO ATTACH TO WPA");
goto die;
}
if (wpa_ctrl_attach(ctrl) < 0) {
print_info("FAILED TO ATTACH TO WPA");
goto die_and_close;
}
int found_console = 0;
do {
size_t actual_buf_len = sizeof(buf);
@@ -109,7 +175,7 @@ int vanilla_sync_with_console(const char *wireless_interface, uint16_t code)
}
char wps_buf[100];
sprintf(wps_buf, "WPS_PIN %.*s %04d5678", 17, line, code);
snprintf(wps_buf, sizeof(wps_buf), "WPS_PIN %.*s %04d5678", 17, line, code);
actual_buf_len = sizeof(buf);
wpa_ctrl_command(ctrl, wps_buf, buf, &actual_buf_len);
@@ -125,7 +191,7 @@ int vanilla_sync_with_console(const char *wireless_interface, uint16_t code)
actual_buf_len = sizeof(buf);
wpa_ctrl_command(ctrl, "SAVE_CONFIG", buf, &actual_buf_len);
}
printf("%s\n", line);
//printf("%s\n", line);
line = strtok(NULL, "\n");
}
} while (!found_console);
@@ -137,18 +203,15 @@ int vanilla_sync_with_console(const char *wireless_interface, uint16_t code)
} else {
ret = VANILLA_ERROR_CANCELLED;
}*/
ret = VANILLA_ERROR_SUCCESS;
ret = VANILLA_SUCCESS;
die_and_detach:
wpa_ctrl_detach(ctrl);
die_and_close:
wpa_ctrl_close(ctrl);
die:
kill(pid, SIGINT);
return ret;
// Scan
// Check results for WiiU
// Send WPS Pin
// Save config
// Close
}
}
-7
View File
@@ -3,13 +3,6 @@
#include <stdint.h>
#define VANILLA_ERROR_SUCCESS 0
#define VANILLA_ERROR_UNKNOWN -100
#define VANILLA_ERROR_CONFIG_FILE -101
#define VANILLA_ERROR_BAD_FORK -102
#define VANILLA_ERROR_TERMINATED -103
#define VANILLA_ERROR_CANCELLED -104
int vanilla_sync_with_console(const char *wireless_interface, uint16_t code);
#endif // VANILLA_SERVER_H
+33
View File
@@ -0,0 +1,33 @@
#include "status.h"
#include <stdio.h>
#include <stdarg.h>
static const char *VANILLA_STATUS_STRINGS[] = {
"SUCCESS",
"ERROR",
"READY",
"INFO",
"UNKNOWN_COMMAND",
"INVALID_ARGUMENT"
};
//static const char *VANILL
void print_status(int errno)
{
printf("%s\n", VANILLA_STATUS_STRINGS[-errno]);
}
void print_info(const char *errstr, ...)
{
va_list args;
va_start(args, errstr);
char buf[16384];
vsnprintf(buf, sizeof(buf), errstr, args);
printf("%s %s\n", VANILLA_STATUS_STRINGS[-VANILLA_INFO], buf);
va_end(args);
}
+14
View File
@@ -0,0 +1,14 @@
#ifndef VANILLA_SERVER_STATUS_H
#define VANILLA_SERVER_STATUS_H
#define VANILLA_SUCCESS 0
#define VANILLA_ERROR -1
#define VANILLA_READY -2
#define VANILLA_INFO -3
#define VANILLA_UNKNOWN_COMMAND -4
#define VANILLA_INVALID_ARGUMENT -5
void print_status(int errno);
void print_info(const char *str, ...);
#endif // VANILLA_SERVER_STATUS_H