Merge pull request #221 from lakinduakash/dev

Able to customize gateway
Add tests
This commit is contained in:
Lakindu Akash 2021-12-27 02:57:38 +05:30 committed by GitHub
commit 0b4960dee7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 386 additions and 185 deletions

View File

@ -13,5 +13,6 @@ jobs:
run: | run: |
sudo apt-get update sudo apt-get update
sudo apt-get install -y libgtk-3-dev libqrencode-dev libpng-dev build-essential gcc g++ pkg-config make sudo apt-get install -y libgtk-3-dev libqrencode-dev libpng-dev build-essential gcc g++ pkg-config make
make test
make make
sudo make install sudo make install

View File

@ -8,6 +8,11 @@ install:
@echo "Installing..." @echo "Installing..."
cd src && $(MAKE) install cd src && $(MAKE) install
test:
mkdir -p build
@echo "Testing..."
cd test && $(MAKE)
install-cli-only: install-cli-only:
@echo "Installing command line interface only..." @echo "Installing command line interface only..."
cd src/scripts && $(MAKE) install-cli-only cd src/scripts && $(MAKE) install-cli-only
@ -19,7 +24,8 @@ uninstall:
clean-old: clean-old:
cd src && $(MAKE) clean-old cd src && $(MAKE) clean-old
.PHONY: clean .PHONY: clean test
clean: clean:
cd src && $(MAKE) clean cd src && $(MAKE) clean
cd test && $(MAKE) clean

2
debian/changelog vendored
View File

@ -1,4 +1,4 @@
linux-wifi-hotspot (4.3.3) UNRELEASED; urgency=high linux-wifi-hotspot (4.4.0) UNRELEASED; urgency=low
* Minor secrurty fixes * Minor secrurty fixes

View File

@ -1,5 +1,5 @@
CHANNEL=default CHANNEL=default
GATEWAY=10.0.0.1 GATEWAY=192.168.12.1
WPA_VERSION=2 WPA_VERSION=2
ETC_HOSTS=0 ETC_HOSTS=0
DHCP_DNS=gateway DHCP_DNS=gateway

View File

@ -9,7 +9,7 @@
<property name="type_hint">dialog</property> <property name="type_hint">dialog</property>
<property name="gravity">center</property> <property name="gravity">center</property>
<property name="program_name">Linux Wifi Hotspot</property> <property name="program_name">Linux Wifi Hotspot</property>
<property name="version">4.3.3</property> <property name="version">4.4.0</property>
<property name="copyright" translatable="yes">(c) lakinduakash 2021</property> <property name="copyright" translatable="yes">(c) lakinduakash 2021</property>
<property name="comments" translatable="yes">For more info visit <property name="comments" translatable="yes">For more info visit
https://github.com/lakinduakash/linux-wifi-hotspot</property> https://github.com/lakinduakash/linux-wifi-hotspot</property>

View File

@ -220,6 +220,60 @@
<property name="margin_bottom">5</property> <property name="margin_bottom">5</property>
<property name="row_spacing">10</property> <property name="row_spacing">10</property>
<property name="column_homogeneous">True</property> <property name="column_homogeneous">True</property>
<child>
<object class="GtkRadioButton" id="rb_freq_2">
<property name="label" translatable="yes">2.4Ghz</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
<property name="group">rb_freq_auto</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="rb_freq_auto">
<property name="label" translatable="yes">Auto</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
<property name="group">rb_freq_5</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Frequency band</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="rb_freq_5">
<property name="label" translatable="yes">5Ghz</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
<property name="group">rb_freq_auto</property>
</object>
<packing>
<property name="left_attach">3</property>
<property name="top_attach">0</property>
</packing>
</child>
<child> <child>
<object class="GtkCheckButton" id="cb_hidden"> <object class="GtkCheckButton" id="cb_hidden">
<property name="label" translatable="yes">Hidden</property> <property name="label" translatable="yes">Hidden</property>
@ -233,19 +287,6 @@
<property name="top_attach">1</property> <property name="top_attach">1</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkCheckButton" id="cb_no_haveged">
<property name="label">No Haveged</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">9</property>
</packing>
</child>
<child> <child>
<object class="GtkCheckButton" id="cb_psk"> <object class="GtkCheckButton" id="cb_psk">
<property name="label" translatable="yes">Use psk</property> <property name="label" translatable="yes">Use psk</property>
@ -322,57 +363,27 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkRadioButton" id="rb_freq_2"> <object class="GtkCheckButton" id="cb_gateway">
<property name="label" translatable="yes">2.4Ghz</property> <property name="label" translatable="yes">Gateway</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">False</property> <property name="receives_default">False</property>
<property name="draw_indicator">True</property> <property name="draw_indicator">True</property>
<property name="group">rb_freq_auto</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="rb_freq_auto">
<property name="label" translatable="yes">Auto</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
<property name="group">rb_freq_5</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Frequency band</property>
</object> </object>
<packing> <packing>
<property name="left_attach">0</property> <property name="left_attach">0</property>
<property name="top_attach">0</property> <property name="top_attach">6</property>
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkRadioButton" id="rb_freq_5"> <object class="GtkEntry" id="entry_gateway">
<property name="label" translatable="yes">5Ghz</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">False</property> <property name="halign">start</property>
<property name="draw_indicator">True</property>
<property name="group">rb_freq_auto</property>
</object> </object>
<packing> <packing>
<property name="left_attach">3</property> <property name="left_attach">1</property>
<property name="top_attach">0</property> <property name="top_attach">6</property>
</packing> </packing>
</child> </child>
<child> <child>
@ -385,7 +396,7 @@
</object> </object>
<packing> <packing>
<property name="left_attach">0</property> <property name="left_attach">0</property>
<property name="top_attach">6</property> <property name="top_attach">7</property>
</packing> </packing>
</child> </child>
<child> <child>
@ -398,7 +409,7 @@
</object> </object>
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
<property name="top_attach">6</property> <property name="top_attach">7</property>
<property name="height">2</property> <property name="height">2</property>
</packing> </packing>
</child> </child>
@ -412,7 +423,7 @@
</object> </object>
<packing> <packing>
<property name="left_attach">0</property> <property name="left_attach">0</property>
<property name="top_attach">8</property> <property name="top_attach">9</property>
</packing> </packing>
</child> </child>
<child> <child>
@ -425,9 +436,28 @@
</object> </object>
<packing> <packing>
<property name="left_attach">2</property> <property name="left_attach">2</property>
<property name="top_attach">8</property> <property name="top_attach">9</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkCheckButton" id="cb_no_haveged">
<property name="label">No Haveged</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">10</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child> <child>
<placeholder/> <placeholder/>
</child> </child>

View File

@ -101,9 +101,9 @@ const char *build_wh_start_command(char *iface_src, char *iface_dest, char *ssid
const char *build_wh_mkconfig_command(ConfigValues* cv){ const char *build_wh_mkconfig_command(ConfigValues* cv){
const char* a=get_config_file(CONFIG_FILE_NAME); const char* config_ffile_name=get_config_file(CONFIG_FILE_NAME);
snprintf(cmd_mkconfig, BUFSIZE, "%s %s %s %s '%s' '%s' %s %s",SUDO, CREATE_AP, cv->iface_wifi, cv->iface_inet, cv->ssid, cv->pass,MKCONFIG,a); snprintf(cmd_mkconfig, BUFSIZE, "%s %s %s %s '%s' '%s' %s %s",SUDO, CREATE_AP, cv->iface_wifi, cv->iface_inet, cv->ssid, cv->pass,MKCONFIG,config_ffile_name);
if(cv->freq!=NULL){ if(cv->freq!=NULL){
strcat(cmd_mkconfig," --freq-band "); strcat(cmd_mkconfig," --freq-band ");
@ -141,6 +141,11 @@ const char *build_wh_mkconfig_command(ConfigValues* cv){
strcat(cmd_mkconfig, cv->mac); strcat(cmd_mkconfig, cv->mac);
} }
if(cv->gateway!=NULL) {
strcat(cmd_mkconfig, " --gateway ");
strcat(cmd_mkconfig, cv->gateway);
}
if(cv->mac_filter!=NULL && (strcmp(cv->mac_filter,"1") == 0)){ if(cv->mac_filter!=NULL && (strcmp(cv->mac_filter,"1") == 0)){
strcat(cmd_mkconfig, " --mac-filter "); strcat(cmd_mkconfig, " --mac-filter ");
strcat(cmd_mkconfig, cv->mac_filter); strcat(cmd_mkconfig, cv->mac_filter);

View File

@ -133,6 +133,9 @@ static void setConfigValues(const char * key, char *value){
if( !strcmp ( IEEE80211AC, key )) if( !strcmp ( IEEE80211AC, key ))
configValues.ieee80211ac = value; configValues.ieee80211ac = value;
if( !strcmp ( GATEWAY, key ))
configValues.gateway = value;
} }

View File

@ -92,6 +92,7 @@ typedef struct{
char *ieee80211n; char *ieee80211n;
char *ieee80211ac; char *ieee80211ac;
char *no_haveged; char *no_haveged;
char *gateway;
} ConfigValues; } ConfigValues;

View File

@ -52,6 +52,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define ERROR_CHANNEL_MSG_2 "Channel must be 1-11" #define ERROR_CHANNEL_MSG_2 "Channel must be 1-11"
#define ERROR_CHANNEL_MSG_5 "Channel must be 1-196" #define ERROR_CHANNEL_MSG_5 "Channel must be 1-196"
#define ERROR_MAC_MSG "Invalid Mac address" #define ERROR_MAC_MSG "Invalid Mac address"
#define ERROR_GATEWAY_MSG "Invalid gateway IP address"
#define DEFAULT_GATEWAY_IP "192.168.12.1"
GtkBuilder *builder; GtkBuilder *builder;
GObject *window; GObject *window;
@ -72,6 +75,7 @@ GtkEntry *entry_ssd;
GtkEntry *entry_pass; GtkEntry *entry_pass;
GtkEntry *entry_mac; GtkEntry *entry_mac;
GtkEntry *entry_channel; GtkEntry *entry_channel;
GtkEntry *entry_gateway;
GtkTextView *tv_mac_filter; GtkTextView *tv_mac_filter;
GtkTextBuffer *buffer_mac_filter; GtkTextBuffer *buffer_mac_filter;
@ -86,6 +90,7 @@ GtkRadioButton *rb_freq_5;
GtkCheckButton *cb_hidden; GtkCheckButton *cb_hidden;
GtkCheckButton *cb_no_haveged; GtkCheckButton *cb_no_haveged;
GtkCheckButton *cb_psk; GtkCheckButton *cb_psk;
GtkCheckButton *cb_gateway;
GtkCheckButton *cb_mac; GtkCheckButton *cb_mac;
GtkCheckButton *cb_novirt; GtkCheckButton *cb_novirt;
GtkCheckButton *cb_channel; GtkCheckButton *cb_channel;
@ -111,6 +116,7 @@ GtkStyleContext *context_entry_ssid;
GtkStyleContext *context_entry_channel; GtkStyleContext *context_entry_channel;
GtkStyleContext *context_label_input_error; GtkStyleContext *context_label_input_error;
GtkStyleContext *context_tv_mac_filter; GtkStyleContext *context_tv_mac_filter;
GtkStyleContext *context_entry_gateway;
const char** iface_list; const char** iface_list;
@ -142,9 +148,12 @@ static void on_create_hp_clicked(GtkWidget *widget, gpointer data) {
if(validator(&configValues) == FALSE){ if(validator(&configValues) == FALSE){
set_error_text("Check inputs"); set_error_text("Some inputes are not valid!");
return; return;
} }
else{
set_error_text("");
}
startShell(build_wh_mkconfig_command(&configValues)); startShell(build_wh_mkconfig_command(&configValues));
@ -187,6 +196,7 @@ static void init_style_contexts(){
context_entry_channel = gtk_widget_get_style_context((GtkWidget*)entry_channel); context_entry_channel = gtk_widget_get_style_context((GtkWidget*)entry_channel);
context_label_input_error = gtk_widget_get_style_context((GtkWidget*)label_input_error); context_label_input_error = gtk_widget_get_style_context((GtkWidget*)label_input_error);
context_tv_mac_filter = gtk_widget_get_style_context((GtkWidget*)tv_mac_filter); context_tv_mac_filter = gtk_widget_get_style_context((GtkWidget*)tv_mac_filter);
context_entry_gateway = gtk_widget_get_style_context((GtkWidget*)entry_gateway);
} }
@ -343,6 +353,30 @@ static void* tv_mac_filter_warn(){
return NULL; return NULL;
} }
static void* entry_gateway_warn(GtkWidget *widget, gpointer data){
const char *gateway = gtk_entry_get_text(GTK_ENTRY(widget));
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb_gateway))==TRUE){
if (isValidIPaddress(gateway)==-1){
gtk_style_context_add_class(context_tv_mac_filter, "entry-error");
set_error_text(ERROR_GATEWAY_MSG);
return FALSE;
}
else{
set_error_text("");
gtk_style_context_remove_class(context_tv_mac_filter,"entry-error");
return NULL;
}
}
gtk_style_context_remove_class(context_tv_mac_filter,"entry-error");
set_error_text("");
return NULL;
}
static void *update_freq_toggle(){ static void *update_freq_toggle(){
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(rb_freq_2))) if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(rb_freq_2)))
@ -386,7 +420,9 @@ int initUi(int argc, char *argv[]){
entry_mac = (GtkEntry *) gtk_builder_get_object(builder, "entry_mac"); entry_mac = (GtkEntry *) gtk_builder_get_object(builder, "entry_mac");
entry_channel = (GtkEntry *) gtk_builder_get_object(builder, "entry_channel"); entry_channel = (GtkEntry *) gtk_builder_get_object(builder, "entry_channel");
tv_mac_filter = (GtkTextView *) gtk_builder_get_object(builder, "tv_mac_filter"); entry_gateway = (GtkEntry *) gtk_builder_get_object(builder, "entry_gateway");
tv_mac_filter = (GtkTextView *) gtk_builder_get_object(builder, "entry_gateway");
buffer_mac_filter = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv_mac_filter)); buffer_mac_filter = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv_mac_filter));
@ -399,6 +435,7 @@ int initUi(int argc, char *argv[]){
cb_mac = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_mac"); cb_mac = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_mac");
cb_novirt = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_novirt"); cb_novirt = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_novirt");
cb_channel = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_channel"); cb_channel = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_channel");
cb_gateway = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_gateway");
cb_open = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_open"); cb_open = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_open");
cb_mac_filter = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_mac_filter"); cb_mac_filter = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_mac_filter");
cb_ieee80211n = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_ieee80211n"); cb_ieee80211n = (GtkCheckButton *) gtk_builder_get_object(builder, "cb_ieee80211n");
@ -430,6 +467,7 @@ int initUi(int argc, char *argv[]){
g_signal_connect (cb_mac, "toggled", G_CALLBACK(on_cb_mac_toggle), NULL); //new g_signal_connect (cb_mac, "toggled", G_CALLBACK(on_cb_mac_toggle), NULL); //new
g_signal_connect (cb_channel, "toggled", G_CALLBACK(on_cb_channel_toggle), NULL); //new g_signal_connect (cb_channel, "toggled", G_CALLBACK(on_cb_channel_toggle), NULL); //new
g_signal_connect (cb_mac_filter, "toggled", G_CALLBACK(on_cb_mac_filter_toggle), NULL); //new g_signal_connect (cb_mac_filter, "toggled", G_CALLBACK(on_cb_mac_filter_toggle), NULL); //new
g_signal_connect (cb_gateway, "toggled", G_CALLBACK(on_cb_gateway_toggle), NULL); //new
g_signal_connect (entry_mac, "changed", G_CALLBACK(entry_mac_warn), NULL); g_signal_connect (entry_mac, "changed", G_CALLBACK(entry_mac_warn), NULL);
g_signal_connect (entry_ssd, "changed", G_CALLBACK(entry_ssid_warn), NULL); g_signal_connect (entry_ssd, "changed", G_CALLBACK(entry_ssid_warn), NULL);
@ -438,6 +476,8 @@ int initUi(int argc, char *argv[]){
g_signal_connect (entry_channel, "changed", G_CALLBACK(entry_channel_warn), NULL); g_signal_connect (entry_channel, "changed", G_CALLBACK(entry_channel_warn), NULL);
g_signal_connect (buffer_mac_filter, "changed", G_CALLBACK(tv_mac_filter_warn), NULL); g_signal_connect (buffer_mac_filter, "changed", G_CALLBACK(tv_mac_filter_warn), NULL);
g_signal_connect (entry_gateway, "changed", G_CALLBACK(entry_gateway_warn), NULL);
g_signal_connect (rb_freq_2, "toggled", G_CALLBACK(update_freq_toggle), NULL); g_signal_connect (rb_freq_2, "toggled", G_CALLBACK(update_freq_toggle), NULL);
g_signal_connect (rb_freq_5, "toggled", G_CALLBACK(update_freq_toggle), NULL); g_signal_connect (rb_freq_5, "toggled", G_CALLBACK(update_freq_toggle), NULL);
g_signal_connect (rb_freq_auto, "toggled", G_CALLBACK(update_freq_toggle), NULL); g_signal_connect (rb_freq_auto, "toggled", G_CALLBACK(update_freq_toggle), NULL);
@ -545,6 +585,16 @@ void init_ui_from_config(){
gtk_toggle_button_set_active((GtkToggleButton*) cb_novirt,TRUE); gtk_toggle_button_set_active((GtkToggleButton*) cb_novirt,TRUE);
} }
// Check if default ip is set as gateway
if(strcmp(values->gateway,DEFAULT_GATEWAY_IP)!=0){
gtk_toggle_button_set_active((GtkToggleButton*) cb_gateway,TRUE);
gtk_entry_set_text(entry_gateway,values->gateway);
} else {
gtk_widget_set_sensitive((GtkWidget*)entry_gateway, FALSE);
gtk_toggle_button_set_active((GtkToggleButton*) cb_gateway,FALSE); // Check this line needed
gtk_entry_set_text(entry_gateway,values->gateway);
}
if(strcmp(values->mac_filter,"1")==0){ if(strcmp(values->mac_filter,"1")==0){
gtk_toggle_button_set_active((GtkToggleButton*) cb_mac_filter,TRUE); gtk_toggle_button_set_active((GtkToggleButton*) cb_mac_filter,TRUE);
} else { } else {
@ -806,6 +856,11 @@ static gboolean validator(ConfigValues *cv){
return FALSE; return FALSE;
} }
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb_gateway))==TRUE){
if (isValidIPaddress(cv->gateway)==-1)
return FALSE;
}
return TRUE; return TRUE;
} }
@ -846,7 +901,11 @@ static int init_config_val_input(ConfigValues* cv){
else else
cv->channel = NULL; cv->channel = NULL;
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb_gateway)))
cv->gateway = (char*)gtk_entry_get_text(entry_gateway);
else
cv->gateway = NULL;
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb_mac))) if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb_mac)))
cv->mac = (char*)gtk_entry_get_text(entry_mac); cv->mac = (char*)gtk_entry_get_text(entry_mac);
else else
@ -1025,3 +1084,16 @@ static void on_cb_mac_filter_toggle(GtkWidget *widget, gpointer data)
gtk_widget_set_sensitive((GtkWidget*)tv_mac_filter, FALSE); gtk_widget_set_sensitive((GtkWidget*)tv_mac_filter, FALSE);
} }
} }
/**
* When gateway button is not toogled, disable gateway entry
*/
static void on_cb_gateway_toggle(GtkWidget *widget, gpointer data)
{
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
gtk_widget_set_sensitive((GtkWidget*)entry_gateway, TRUE);
} else {
gtk_widget_set_sensitive((GtkWidget*)entry_gateway, FALSE);
}
}

View File

@ -82,6 +82,8 @@ static void on_cb_channel_toggle(GtkWidget *widget, gpointer data);
static void on_cb_mac_filter_toggle(GtkWidget *widget, gpointer data); static void on_cb_mac_filter_toggle(GtkWidget *widget, gpointer data);
static void on_cb_gateway_toggle(GtkWidget *widget, gpointer data);
static void clear_connecetd_devices_list(); static void clear_connecetd_devices_list();
#endif //WIHOTSPOT_UI_H #endif //WIHOTSPOT_UI_H

View File

@ -1,122 +1,151 @@
// //
// Created by lakinduakash on 16/04/19. // Created by lakinduakash on 16/04/19.
// //
/* /*
Copyright (c) 2019, lakinduaksh Copyright (c) 2019, lakinduaksh
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this * Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, * Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution. and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <ctype.h> #include <ctype.h>
#include "util.h" #include "util.h"
#include <regex.h> #include <regex.h>
regex_t regex; regex_t regex;
int reti; int reti;
char msgbuf[100]; char msgbuf[100];
int find_str(char *find, const char **array, int length) { int find_str(char *find, const char **array, int length) {
int i; int i;
for ( i = 0; i < length; i++ ) { for ( i = 0; i < length; i++ ) {
if (strcmp(array[i], find) == 0) { if (strcmp(array[i], find) == 0) {
return i; return i;
} }
} }
return -1; return -1;
} }
void rand_str(char *dest, size_t length) { void rand_str(char *dest, size_t length) {
char charset[] = "0123456789" char charset[] = "0123456789"
"abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
while (length-- > 0) { while (length-- > 0) {
size_t index = (size_t) ((double) rand() / RAND_MAX * (sizeof charset - 1)); size_t index = (size_t) ((double) rand() / RAND_MAX * (sizeof charset - 1));
*dest++ = charset[index]; *dest++ = charset[index];
} }
*dest = '\0'; *dest = '\0';
} }
int isValidMacAddress(const char* mac) { int isValidMacAddress(const char* mac) {
int i = 0; int i = 0;
int s = 0; int s = 0;
while (*mac) { while (*mac) {
if (isxdigit(*mac)) { if (isxdigit(*mac)) {
i++; i++;
} }
else if (*mac == ':' || *mac == '-') { else if (*mac == ':' || *mac == '-') {
if (i == 0 || i / 2 - 1 != s) if (i == 0 || i / 2 - 1 != s)
break; break;
++s; ++s;
} }
else { else {
s = -1; s = -1;
} }
++mac; ++mac;
} }
return (i == 12 && (s == 5 || s == 0)); return (i == 12 && (s == 5 || s == 0));
} }
int isValidAcceptedMacs(const char *macs){ int isValidAcceptedMacs(const char *macs){
/* Compile regular expression */ /* Compile regular expression */
reti = regcomp(&regex, "^(((([0-9A-Fa-f]{2}):){5}[0-9A-Fa-f]{2}\\s*)(^((([0-9A-Fa-f]{2}):){5}[0-9A-Fa-f]{2}\\s*))*)$", REG_EXTENDED); reti = regcomp(&regex, "^(((([0-9A-Fa-f]{2}):){5}[0-9A-Fa-f]{2}\\s*)(^((([0-9A-Fa-f]{2}):){5}[0-9A-Fa-f]{2}\\s*))*)$", REG_EXTENDED);
if (reti) { if (reti) {
//printf( "Could not compile regex\n"); //printf( "Could not compile regex\n");
return -1; return -1;
} }
/* Execute regular expression */ /* Execute regular expression */
reti = regexec(&regex, macs, 0, NULL, 0); reti = regexec(&regex, macs, 0, NULL, 0);
if (!reti) { if (!reti) {
return 0; return 0;
} }
else if (reti == REG_NOMATCH) { else if (reti == REG_NOMATCH) {
//puts("Invalid mac addresses"); //puts("Invalid mac addresses");
return -1; return -1;
} }
else { else {
regerror(reti, &regex, msgbuf, sizeof(msgbuf)); regerror(reti, &regex, msgbuf, sizeof(msgbuf));
//printf("Regex match failed: %s\n", msgbuf); //printf("Regex match failed: %s\n", msgbuf);
return -1; return -1;
} }
} }
int isValidIPaddress(const char * ip){
/* Compile regular expression */
reti = regcomp(&regex,
"^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$",
REG_EXTENDED);
if (reti) {
//printf( "Could not compile regex\n");
return -1;
}
/* Execute regular expression */
reti = regexec(&regex, ip, 0, NULL, 0);
if (!reti) {
return 0;
}
else if (reti == REG_NOMATCH) {
//puts("Invalid ip addresses");
return -1;
}
else {
regerror(reti, &regex, msgbuf, sizeof(msgbuf));
//printf("Regex match failed: %s\n", msgbuf);
return -1;
}
}

View File

@ -35,5 +35,6 @@ int find_str(char *find, const char **array, int length);
void rand_str(char *dest, size_t length); void rand_str(char *dest, size_t length);
int isValidMacAddress(const char*); int isValidMacAddress(const char*);
int isValidAcceptedMacs(const char*); int isValidAcceptedMacs(const char*);
int isValidIPaddress(const char*);
#endif //WIHOTSPOT_UTIL_H #endif //WIHOTSPOT_UTIL_H

37
test/Makefile Normal file
View File

@ -0,0 +1,37 @@
CC=gcc
PKGCONFIG = $(shell which pkg-config)
CFLAGS=`pkg-config --cflags gtk+-3.0` -I./../src/ui
LIBS=`pkg-config --libs gtk+-3.0 --libs x11` -lstdc++ -lpng -lqrencode
ODIR=./../build
# Determine this makefile's path.
# Be sure to place this BEFORE `include` directives, if any.
THIS_FILE := $(lastword $(MAKEFILE_LIST))
_OBJ = util.o test_util.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
.PHONY: clean
all: $(OBJ) test
$(ODIR)/util.o: ../src/ui/util.c
$(CC) -c $? -o $@ $(CFLAGS)
$(ODIR)/test_util.o: test_util.c
$(CC) -c $? -o $@ $(CFLAGS)
test: $(OBJ)
$(CC) -o $(ODIR)/test $^
@$(ODIR)/test
clean:
rm -f $(OBJ)
rm -f $(ODIR)/test

14
test/test_util.c Normal file
View File

@ -0,0 +1,14 @@
#include <assert.h>
#include <stdio.h>
#include <util.h>
int main(int argc, char *argv[]){
assert(0 == isValidIPaddress("192.12.23.123"));
assert(0 == isValidIPaddress("255.255.255.255"));
assert(-1 == isValidIPaddress("255.255.255.256"));
assert(-1 == isValidIPaddress("255.255.255.2551"));
assert(-1 == isValidIPaddress("192.168.12"));
return 0;
}