2020-09-02 00:13:32 +02:00
|
|
|
|
// 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.
|
2021-02-09 12:43:45 +01:00
|
|
|
|
Copyright (C) 1999-2021 Antonio Ca<EFBFBD>as Vargas
|
2020-09-02 00:13:32 +02:00
|
|
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************************** Headers **********************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <stddef.h> // For NULL
|
|
|
|
|
|
|
|
|
|
#include "swad_action.h"
|
|
|
|
|
#include "swad_database.h"
|
|
|
|
|
#include "swad_form.h"
|
|
|
|
|
#include "swad_HTML.h"
|
2020-09-02 12:20:21 +02:00
|
|
|
|
#include "swad_MAC.h"
|
2020-09-02 00:13:32 +02:00
|
|
|
|
#include "swad_parameter.h"
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************** External global variables from others modules ****************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/***************************** Private constants *****************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******************************* Private types *******************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/* Parameters used in forms to edit MAC address */
|
|
|
|
|
struct MAC_Params
|
|
|
|
|
{
|
|
|
|
|
long Cod; // Code (i.e. room code)
|
2020-09-02 12:20:21 +02:00
|
|
|
|
char MACstr[MAC_LENGTH_MAC_ADDRESS + 1]; // MAC address
|
2020-09-02 00:13:32 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/***************************** 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);
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/**************** 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);
|
2020-09-02 12:20:21 +02:00
|
|
|
|
Par_PutHiddenParamString (NULL,"MAC",((struct MAC_Params *) Args)->MACstr);
|
2020-09-02 00:13:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************************ 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);
|
2020-09-02 12:20:21 +02:00
|
|
|
|
HTM_INPUT_TEXT ("NewMAC",MAC_LENGTH_MAC_ADDRESS,((struct MAC_Params *) Args)->MACstr,
|
2020-09-02 00:13:32 +02:00
|
|
|
|
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)
|
2020-09-02 12:20:21 +02:00
|
|
|
|
MAC_MACnumToMACstr (MACnum,Params.MACstr); // Current MAC address in xx:xx:xx:xx:xx:xx format
|
2020-09-02 00:13:32 +02:00
|
|
|
|
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)
|
2020-09-02 12:20:21 +02:00
|
|
|
|
Params.MACstr[0] = '\0'; // Current MAC address in xx:xx:xx:xx:xx:xx format
|
2020-09-02 00:13:32 +02:00
|
|
|
|
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 *****/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2020-09-02 12:20:21 +02:00
|
|
|
|
void MAC_MACnumToMACstr (unsigned long long MACnum,char MACstr[MAC_LENGTH_MAC_ADDRESS + 1])
|
2020-09-02 00:13:32 +02:00
|
|
|
|
{
|
2020-09-02 12:20:21 +02:00
|
|
|
|
if (MACnum)
|
|
|
|
|
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)));
|
|
|
|
|
else
|
|
|
|
|
MACstr[0] = '\0';
|
2020-09-02 00:13:32 +02:00
|
|
|
|
}
|