From f5334808b2259917ba6f77340d09216bab84b3ff Mon Sep 17 00:00:00 2001 From: acanas Date: Wed, 2 Sep 2020 00:13:32 +0200 Subject: [PATCH] Version19.263 --- Makefile | 6 +- swad_MAC.c | 225 +++++++++++++++++++++++++++++++++++++++++++++++ swad_MAC.h | 44 +++++++++ swad_changelog.h | 3 +- swad_room.c | 121 ++----------------------- 5 files changed, 280 insertions(+), 119 deletions(-) create mode 100644 swad_MAC.c create mode 100644 swad_MAC.h diff --git a/Makefile b/Makefile index 2151c1447..18b604a77 100644 --- a/Makefile +++ b/Makefile @@ -47,9 +47,9 @@ OBJS = swad_account.o swad_action.o swad_agenda.o swad_alert.o \ swad_icon.o swad_ID.o swad_indicator.o swad_info.o swad_institution.o \ swad_institution_config.o \ swad_language.o swad_layout.o swad_link.o swad_log.o swad_logo.o \ - swad_mail.o swad_main.o swad_maintenance.o swad_map.o swad_mark.o \ - swad_match.o swad_match_print.o swad_match_result.o swad_media.o \ - swad_menu.o swad_message.o swad_MFU.o \ + swad_MAC.o swad_mail.o swad_main.o swad_maintenance.o swad_map.o \ + swad_mark.o swad_match.o swad_match_print.o swad_match_result.o \ + swad_media.o swad_menu.o swad_message.o swad_MFU.o \ swad_network.o swad_nickname.o swad_notice.o swad_notification.o \ swad_pagination.o swad_parameter.o swad_password.o swad_photo.o \ swad_place.o swad_plugin.o swad_privacy.o swad_profile.o \ diff --git a/swad_MAC.c b/swad_MAC.c new file mode 100644 index 000000000..ee3f51eb9 --- /dev/null +++ b/swad_MAC.c @@ -0,0 +1,225 @@ +// swad_MAC.c: MAC addresses + +/* + SWAD (Shared Workspace At a Distance), + is a web platform developed at the University of Granada (Spain), + and used to support university teaching. + + This file is part of SWAD core. + Copyright (C) 1999-2020 Antonio Cañas Vargas + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ +/*****************************************************************************/ +/********************************** Headers **********************************/ +/*****************************************************************************/ + +#include // For NULL + +#include "swad_action.h" +#include "swad_database.h" +#include "swad_form.h" +#include "swad_HTML.h" +#include "swad_parameter.h" + +/*****************************************************************************/ +/************** External global variables from others modules ****************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private constants *****************************/ +/*****************************************************************************/ + +#define MAC_NUM_BYTES 6 +#define MAC_LENGTH_MAC_ADDRESS (MAC_NUM_BYTES * 3 - 1) // xx:xx:xx:xx:xx:xx + +/*****************************************************************************/ +/******************************* Private types *******************************/ +/*****************************************************************************/ + +/* Parameters used in forms to edit MAC address */ +struct MAC_Params + { + long Cod; // Code (i.e. room code) + char MAC[MAC_LENGTH_MAC_ADDRESS + 1]; // MAC address + }; + +/*****************************************************************************/ +/***************************** Private variables *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private prototypes ****************************/ +/*****************************************************************************/ + +static void MAC_PutParams (void *Args); +static void MAC_PutFormToEditMACAddress (Act_Action_t NextAction,const char *Anchor, + void (*FuncParams) (void *Args),void *Args); + +static void MAC_MACnumToMACstr (unsigned long long MACnum,char MACstr[MAC_LENGTH_MAC_ADDRESS + 1]); + +/*****************************************************************************/ +/**************** Put hidden parameters to edit a MAC address ****************/ +/*****************************************************************************/ + +static void MAC_PutParams (void *Args) + { + if (Args) + { + Par_PutHiddenParamLong (NULL,"Cod",((struct MAC_Params *) Args)->Cod); + Par_PutHiddenParamString (NULL,"MAC",((struct MAC_Params *) Args)->MAC); + } + } + +/*****************************************************************************/ +/************************ Put form to edit a MAC address *********************/ +/*****************************************************************************/ + +static void MAC_PutFormToEditMACAddress (Act_Action_t NextAction,const char *Anchor, + void (*FuncParams) (void *Args),void *Args) + { + /* Form to enter a new MAC address */ + Frm_StartFormAnchor (NextAction,Anchor); + FuncParams (Args); + HTM_INPUT_TEXT ("NewMAC",MAC_LENGTH_MAC_ADDRESS,((struct MAC_Params *) Args)->MAC, + HTM_SUBMIT_ON_CHANGE, + "size=\"8\""); + Frm_EndForm (); + } + +/*****************************************************************************/ +/************************ List several MAC addresses *************************/ +/*****************************************************************************/ + +void MAC_ListMACAddresses (unsigned NumMACs,MYSQL_RES **mysql_res) + { + MYSQL_ROW row; + unsigned NumMAC; + unsigned long long MACnum; + char MACstr[MAC_LENGTH_MAC_ADDRESS + 1]; + + /***** Write the MAC addresses *****/ + for (NumMAC = 0; + NumMAC < NumMACs; + NumMAC++) + { + /* Get next MAC address */ + row = mysql_fetch_row (*mysql_res); + + /* Write break line */ + if (NumMAC) + HTM_BR (); + + /* Write MAC address (row[0]) */ + if (sscanf (row[0],"%llu",&MACnum) == 1) + { + MAC_MACnumToMACstr (MACnum,MACstr); + HTM_Txt (MACstr); + } + } + + /***** Free structure that stores the query result *****/ + DB_FreeMySQLResult (mysql_res); + } + +/*****************************************************************************/ +/************************ List several MAC addresses *************************/ +/*****************************************************************************/ + +void MAC_EditMACAddresses (long Cod,const char *Anchor, + unsigned NumMACs,MYSQL_RES **mysql_res) + { + MYSQL_ROW row; + unsigned NumMAC; + unsigned long long MACnum; + struct MAC_Params Params; + + /***** Write the forms to enter the MAC addresses *****/ + for (NumMAC = 0; + NumMAC < NumMACs; + NumMAC++) + { + /* Get next MAC address */ + row = mysql_fetch_row (*mysql_res); + + /* Write MAC address (row[0]) */ + if (sscanf (row[0],"%llu",&MACnum) == 1) + { + Params.Cod = Cod; // Code (i.e. room code) + MAC_MACnumToMACstr (MACnum,Params.MAC); // Current MAC address + MAC_PutFormToEditMACAddress (ActChgRooMAC,Anchor, + MAC_PutParams,&Params); + + /* Write break line */ + HTM_BR (); + } + } + + /* Form to enter a new MAC address */ + Params.Cod = Cod; // Code (i.e. room code) + Params.MAC[0] = '\0'; // Current MAC address + MAC_PutFormToEditMACAddress (ActChgRooMAC,Anchor, + MAC_PutParams,&Params); + + /***** Free structure that stores the query result *****/ + DB_FreeMySQLResult (mysql_res); + } + +/*****************************************************************************/ +/****************** Get MAC address as a number from a form ******************/ +/*****************************************************************************/ + +unsigned long long MAC_GetMACnumFromForm (const char *ParamName) + { + char MACstr[MAC_LENGTH_MAC_ADDRESS * Str_MAX_BYTES_PER_CHAR + 1]; + unsigned long long MAC[MAC_NUM_BYTES]; + unsigned long long MACnum; + + /***** Get parameter *****/ + Par_GetParToText (ParamName,MACstr,MAC_LENGTH_MAC_ADDRESS * Str_MAX_BYTES_PER_CHAR); + + if (MACstr[0]) // Not empty + { + /***** Try to scan it in xx:xx:xx:xx:xx:xx format (where x are hexadecimal digits) *****/ + if (sscanf (MACstr,"%02llx:%02llx:%02llx:%02llx:%02llx:%02llx", + &MAC[0],&MAC[1],&MAC[2],&MAC[3],&MAC[4],&MAC[5]) == MAC_NUM_BYTES) + return (MAC[0] << (CHAR_BIT * 5)) + + (MAC[1] << (CHAR_BIT * 4)) + + (MAC[2] << (CHAR_BIT * 3)) + + (MAC[3] << (CHAR_BIT * 2)) + + (MAC[4] << (CHAR_BIT * 1)) + + (MAC[5] << (CHAR_BIT * 0)); + + /***** Try to scan it in xxxxxxxxxxxx format (where x are hexadecimal digits) ******/ + if (sscanf (MACstr,"%llx",&MACnum) == 1) + return MACnum; + } + + return 0; + } + +/*****************************************************************************/ +/**** Convert from MAC as a number to string in xx:xx:xx:xx:xx:xx format *****/ +/*****************************************************************************/ + +static void MAC_MACnumToMACstr (unsigned long long MACnum,char MACstr[MAC_LENGTH_MAC_ADDRESS + 1]) + { + snprintf (MACstr,MAC_LENGTH_MAC_ADDRESS + 1,"%02x:%02x:%02x:%02x:%02x:%02x", + (unsigned char) ((MACnum >> (CHAR_BIT * 5)) & ((1 << CHAR_BIT) - 1)), + (unsigned char) ((MACnum >> (CHAR_BIT * 4)) & ((1 << CHAR_BIT) - 1)), + (unsigned char) ((MACnum >> (CHAR_BIT * 3)) & ((1 << CHAR_BIT) - 1)), + (unsigned char) ((MACnum >> (CHAR_BIT * 2)) & ((1 << CHAR_BIT) - 1)), + (unsigned char) ((MACnum >> (CHAR_BIT * 1)) & ((1 << CHAR_BIT) - 1)), + (unsigned char) ((MACnum >> (CHAR_BIT * 0)) & ((1 << CHAR_BIT) - 1))); + } diff --git a/swad_MAC.h b/swad_MAC.h new file mode 100644 index 000000000..0d6d491ab --- /dev/null +++ b/swad_MAC.h @@ -0,0 +1,44 @@ +// swad_MAC.h: MAC addresses + +#ifndef _SWAD_MAC +#define _SWAD_MAC +/* + SWAD (Shared Workspace At a Distance in Spanish), + is a web platform developed at the University of Granada (Spain), + and used to support university teaching. + + This file is part of SWAD core. + Copyright (C) 1999-2020 Antonio Cañas Vargas + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ +/*****************************************************************************/ +/********************************** Headers **********************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/************************** Public types and constants ***********************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Public prototypes *****************************/ +/*****************************************************************************/ + +void MAC_ListMACAddresses (unsigned NumMACs,MYSQL_RES **mysql_res); +void MAC_EditMACAddresses (long Cod,const char *Anchor, + unsigned NumMACs,MYSQL_RES **mysql_res); + +unsigned long long MAC_GetMACnumFromForm (const char *ParamName); + +#endif diff --git a/swad_changelog.h b/swad_changelog.h index 77a240aa3..d2f9cd4c0 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -555,7 +555,7 @@ enscript -2 --landscape --color --file-align=2 --highlight --line-numbers -o - * En OpenSWAD: ps2pdf source.ps destination.pdf */ -#define Log_PLATFORM_VERSION "SWAD 19.262 (2020-09-01)" +#define Log_PLATFORM_VERSION "SWAD 19.263 (2020-09-02)" #define CSS_FILE "swad19.253.css" #define JS_FILE "swad19.254.js" /* @@ -573,6 +573,7 @@ TODO: Que al generar un examen s TODO: Create module swad_test_result "sudo apt install webp" en Ubuntu, y "yum install libwebp libwebp-tools" en CentOS, para decodificar imágenes Web/ug reportado por Javier Fernández Baldomero. + Version 19.263 : Sep 02, 2020 New module swad_MAC for MAC addresses. (304364 lines) Version 19.262 : Sep 01, 2020 Edition of MAC addresses in listing of rooms. (304239 lines) Version 19.261 : Sep 01, 2020 MAC addresses are printed in listing of rooms. (304103 lines) Version 19.260.3: Aug 30, 2020 Fixed bug in API function answerMatchQuestion. Reported by Sergio Díaz Rueda. (304010 lines) diff --git a/swad_room.c b/swad_room.c index 8d191c393..b9dd028d5 100644 --- a/swad_room.c +++ b/swad_room.c @@ -35,6 +35,7 @@ #include "swad_form.h" #include "swad_global.h" #include "swad_HTML.h" +#include "swad_MAC.h" #include "swad_room.h" /*****************************************************************************/ @@ -122,8 +123,6 @@ static struct Roo_Room *Roo_EditingRoom = NULL; // Static variable to keep the r static void Roo_GetAndListMACAddresses (long RooCod); static void Roo_GetAndEditMACAddresses (long RooCod,const char *Anchor); static unsigned Roo_GetMACAddresses (long RooCod,MYSQL_RES **mysql_res); -static void Roo_MACnumToMACstr (unsigned long long MACnum,char MACstr[17 + 1]); -unsigned long long Roo_GetMACnum (const char *ParamName); static Roo_Order_t Roo_GetParamRoomOrder (void); static bool Roo_CheckIfICanCreateRooms (void); @@ -326,37 +325,13 @@ void Roo_SeeRooms (void) static void Roo_GetAndListMACAddresses (long RooCod) { MYSQL_RES *mysql_res; - MYSQL_ROW row; unsigned NumMACs; - unsigned NumMAC; - unsigned long long MACnum; - char MACstr[17 + 1]; /***** Get MAC addresses from database *****/ NumMACs = Roo_GetMACAddresses (RooCod,&mysql_res); /***** Write the MAC addresses *****/ - for (NumMAC = 0; - NumMAC < NumMACs; - NumMAC++) - { - /* Get next MAC address */ - row = mysql_fetch_row (mysql_res); - - /* Write break line */ - if (NumMAC) - HTM_BR (); - - /* Write MAC address (row[0]) */ - if (sscanf (row[0],"%llu",&MACnum) == 1) - { - Roo_MACnumToMACstr (MACnum,MACstr); - HTM_Txt (MACstr); - } - } - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); + MAC_ListMACAddresses (NumMACs,&mysql_res); } /*****************************************************************************/ @@ -366,50 +341,13 @@ static void Roo_GetAndListMACAddresses (long RooCod) static void Roo_GetAndEditMACAddresses (long RooCod,const char *Anchor) { MYSQL_RES *mysql_res; - MYSQL_ROW row; unsigned NumMACs; - unsigned NumMAC; - unsigned long long MACnum; - char MACstr[17 + 1]; /***** Get MAC addresses from database *****/ NumMACs = Roo_GetMACAddresses (RooCod,&mysql_res); /***** Write the MAC addresses *****/ - for (NumMAC = 0; - NumMAC < NumMACs; - NumMAC++) - { - /* Get next MAC address */ - row = mysql_fetch_row (mysql_res); - - /* Write MAC address (row[0]) */ - if (sscanf (row[0],"%llu",&MACnum) == 1) - { - Frm_StartFormAnchor (ActChgRooMAC,Anchor); - Roo_PutParamRooCod (RooCod); - Roo_MACnumToMACstr (MACnum,MACstr); - Par_PutHiddenParamString (NULL,"OldMAC",MACstr); - HTM_INPUT_TEXT ("NewMAC",17,MACstr, - HTM_SUBMIT_ON_CHANGE, - "size=\"8\""); - Frm_EndForm (); - - /* Write break line */ - HTM_BR (); - } - } - - /* New MAC address */ - Frm_StartFormAnchor (ActChgRooMAC,Anchor); - Roo_PutParamRooCod (RooCod); - HTM_INPUT_TEXT ("NewMAC",17,"", - HTM_SUBMIT_ON_CHANGE, - "size=\"8\""); - Frm_EndForm (); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); + MAC_EditMACAddresses (RooCod,Anchor,NumMACs,&mysql_res); } /*****************************************************************************/ @@ -427,21 +365,6 @@ static unsigned Roo_GetMACAddresses (long RooCod,MYSQL_RES **mysql_res) RooCod); } -/*****************************************************************************/ -/****** Get and list for edition the MAC addresses associated to a room ******/ -/*****************************************************************************/ - -static void Roo_MACnumToMACstr (unsigned long long MACnum,char MACstr[17 + 1]) - { - snprintf (MACstr,17 + 1,"%02x:%02x:%02x:%02x:%02x:%02x", - (unsigned char) ((MACnum >> 40) & 0xff), - (unsigned char) ((MACnum >> 32) & 0xff), - (unsigned char) ((MACnum >> 24) & 0xff), - (unsigned char) ((MACnum >> 16) & 0xff), - (unsigned char) ((MACnum >> 8) & 0xff), - (unsigned char) ((MACnum ) & 0xff)); - } - /*****************************************************************************/ /**************************** Change MAC of a room ***************************/ /*****************************************************************************/ @@ -456,14 +379,14 @@ void Roo_ChangeMAC (void) /***** Get parameters from form *****/ /* Get room code */ - if ((Roo_EditingRoom->RooCod = Roo_GetParamRooCod ()) == -1L) + if ((Roo_EditingRoom->RooCod = Par_GetParToLong ("Cod")) == -1L) Lay_ShowErrorAndExit ("Code of room is missing."); /* Get the old MAC address of the room */ - OldMAC = Roo_GetMACnum ("OldMAC"); + OldMAC = MAC_GetMACnumFromForm ("MAC"); /* Get the new MAC address of the room */ - NewMAC = Roo_GetMACnum ("NewMAC"); + NewMAC = MAC_GetMACnumFromForm ("NewMAC"); /***** Get data of the room from database *****/ Roo_GetDataOfRoomByCod (Roo_EditingRoom); @@ -483,38 +406,6 @@ void Roo_ChangeMAC (void) Roo_EditingRoom->MAC = NewMAC; } -/*****************************************************************************/ -/****************** Get MAC address as a number from a form ******************/ -/*****************************************************************************/ - -unsigned long long Roo_GetMACnum (const char *ParamName) - { - char MACstr[17 * Str_MAX_BYTES_PER_CHAR + 1]; - unsigned long long MAC[6 + 1]; - unsigned long long MACnum; - - /***** Get parameter *****/ - Par_GetParToText (ParamName,MACstr,17 * Str_MAX_BYTES_PER_CHAR); - - if (MACstr[0]) // Not empty - { - /***** Try to scan it in xx:xx:xx:xx:xx:xx format (where x are hexadecimal digits) *****/ - if (sscanf (MACstr,"%02llx:%02llx:%02llx:%02llx:%02llx:%02llx",&MAC[0],&MAC[1],&MAC[2],&MAC[3],&MAC[4],&MAC[5]) == 6) - return (MAC[0] << 40) + - (MAC[1] << 32) + - (MAC[2] << 24) + - (MAC[3] << 16) + - (MAC[4] << 8) + - MAC[5]; - - /***** Try to scan it in xxxxxxxxxxxx format (where x are hexadecimal digits) ******/ - if (sscanf (MACstr,"%llx",&MACnum) == 1) - return MACnum; - } - - return 0; - } - /*****************************************************************************/ /*********** Get parameter with the type or order in list of rooms ***********/ /*****************************************************************************/