Commit f63df20e authored by Damien Lucas's avatar Damien Lucas

. added lool's code (function to info about a mac by the interface)

  lool: please have a look, I changed few things.

. added a test to knnow if mac is alone on the port or not
  This test is actually only in interface part
  I will soon include it inside server's part to bridge ALL macs

. changed a type definition in server/request_handler
  long long int instead of VS_MachineId
  (but this should not be reason of the segfault)
parent 97d6c6cf
......@@ -6,7 +6,7 @@ CC = gcc
# Compile debugging version
# Without the debugging version, LOGDEBUG has no effect.
CFLAGS += -DDEBUG
# CFLAGS += -DDEBUG
#Debug messages in color
CFLAGS += -DLINUX_COLOR
......
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include "../types.h"
......@@ -32,3 +33,55 @@ ERR_CODE IF_reload(void)
return 0;
}
char* IF_info(VS_MachineId mMac)
{
unsigned int rc;
struct DB_machines_elt* machine_elt;
char* sMessage;
struct SNMP_switch * zwitch;
struct DB_port * port;
sMessage=malloc(256*sizeof(char));
rc=DB_machines_lock(&vs->db, mMac, &machine_elt);
if(rc)
{
sprintf(sMessage,"Mac not found in database\r\n");
}
else
{
rc=DB_switchs_port_lock(&vs->db, machine_elt->switch_ip,\
machine_elt->port, &port, &zwitch);
if(rc)
{
sprintf(sMessage,"Mac found on switch:%s port:%u\r\n but switch unknown\r\n",
VS_SID2A(machine_elt->switch_ip),\
(unsigned)machine_elt->port);
}
else
{
if(zwitch->machines[(unsigned)machine_elt->port]->next!=NULL)
{
sprintf(sMessage,"Mac found on switch:%s port:%u\r\n"\
"mac is NOT alone on this port\r\n",\
VS_SID2A(machine_elt->switch_ip),\
(unsigned)machine_elt->port);
}
else
{
sprintf(sMessage,"Mac found on switch:%s port:%u\r\n"\
"mac is alone on this port\r\n",\
VS_SID2A(machine_elt->switch_ip),\
(unsigned)machine_elt->port);
}
}
}
return sMessage;
}
ERR_CODE IF_start(void);
ERR_CODE IF_reload(void);
char* IF_info(VS_MachineId);
......@@ -29,22 +29,24 @@
/* Protoypes of static functions further defined */
static void IF_telnet_init(int isocket);
static char* IF_telnet_login (int isocket);
static void IF_telnet_commands (int iSocket, char* sUser);
static void IF_telnet_init (int isocket);
static char* IF_telnet_login (int isocket);
static void IF_telnet_commands(int iSocket, char* sUser);
static void Menu_init (struct IF_Menu* mMenu);
static void Menu_send (int iSocket, struct IF_Menu mMenu);
ERR_CODE TELNET_stop();
ERR_CODE TELNET_reload();
ERR_CODE TELNET_stop (int iSocket);
ERR_CODE TELNET_reload(int iSocket);
ERR_CODE TELNET_info (int iSocket);
static char* Catch_Word (int iSocket);
static char* Catch_Word (int iSocket);
static char* Catch_Passwd (int iSocket);
static VS_MachineId Catch_MAC (int iSocket);
static void Request_send (int iSocket, char bAction, char bOption);
static void Message_send (int iSocket, char* sMessage, size_t size);
static void Prompt_send (int iSocket, char* sUser);
static void Prompt_send (int iSocket, char* sUser);
static unsigned int uStop;
......@@ -200,7 +202,7 @@ static void IF_telnet_commands (int iSocket, char* sUser)
{
if(!strcmp(sCmd,z->sName))
{
z->function();
z->function(iSocket);
}
}
free(sCmd);
......@@ -215,33 +217,63 @@ static void IF_telnet_commands (int iSocket, char* sUser)
******************************************************************************/
static void Menu_init (struct IF_Menu* mMenu)
{
mMenu->Command=malloc(sizeof(struct IFT_Command));
mMenu->Command->iIndex='0';
mMenu->Command->function=TELNET_stop;
sprintf(mMenu->Command->sName,"logout");
sprintf(mMenu->Command->sDescr,"Close the current connection");
mMenu->Command->next=malloc(sizeof(struct IFT_Command));
mMenu->Command->next->iIndex='1';
mMenu->Command->next->function=TELNET_reload;
sprintf(mMenu->Command->next->sName,"reload");
sprintf(mMenu->Command->next->sDescr,"Reload the database");
struct IFT_Command* mMenu_current; /* pointer on current chained list cell */
mMenu->Command = malloc(sizeof(struct IFT_Command));
mMenu_current = mMenu->Command;
mMenu_current->iIndex='0';
mMenu_current->function=TELNET_stop;
sprintf(mMenu_current->sName,"logout");
sprintf(mMenu_current->sDescr,"Close the current connection");
mMenu_current->next = malloc(sizeof(struct IFT_Command));
mMenu_current = mMenu_current->next;
mMenu->Command->next->next=NULL;
mMenu_current->iIndex='1';
mMenu_current->function=TELNET_reload;
sprintf(mMenu_current->sName,"reload");
sprintf(mMenu_current->sDescr,"Reload the database");
mMenu_current->next = malloc(sizeof(struct IFT_Command));
mMenu_current = mMenu_current->next;
mMenu_current->iIndex='2';
mMenu_current->function=TELNET_info;
sprintf(mMenu_current->sName,"info");
sprintf(mMenu_current->sDescr,"Informations for a MAC address");
mMenu_current->next=NULL;
}
ERR_CODE TELNET_stop()
ERR_CODE TELNET_stop(int iSocket)
{
uStop=1;
return 0;
}
ERR_CODE TELNET_reload()
ERR_CODE TELNET_reload(int iSocket)
{
IF_reload();
return 0;
}
ERR_CODE TELNET_info(int iSocket)
{
char * sMessage;
VS_MachineId mMac;
mMac = Catch_MAC(iSocket);
sMessage = IF_info(mMac);
Message_send(iSocket,sMessage,strlen(sMessage));
Message_send(iSocket,"",0);
return 0;
}
/******************************************************************************
* Menu_send() *
******************************************************************************
......@@ -288,7 +320,8 @@ static char* Catch_Word (int iSocket)
switch(bBuff)
{
case IAC:
read(iSocket, &bBuff,2);
read(iSocket, &bBuff,1);
read(iSocket, &bBuff,1);
break; //TODO Terminal negociation
case '\r':
......@@ -297,22 +330,21 @@ static char* Catch_Word (int iSocket)
Message_send(iSocket,"\r\n",2);
return sData;
case 0x7F:
if(iPos>0)
case 0x7f:
if(iPos > 0)
{
unsigned char b4Buff[4];
sData[iPos-1]='\0';
iPos--;
sData[iPos]='\0';
b4Buff[0]=0x08;
b4Buff[1]=0x20;
b4Buff[2]=0x08;
Message_send(iSocket,&bBuff,4);
Message_send(iSocket,b4Buff,3);
}
break;
case KEY_UP:
case KEY_DOWN:
case KEY_LEFT:
case KEY_RIGHT:
break;
case 27:
break; // TODO Add test 27 91 KEY_UP ../
default:
sData[iPos]=bBuff;
......@@ -325,7 +357,80 @@ static char* Catch_Word (int iSocket)
}
/******************************************************************************
* Catch_MAC() *
******************************************************************************
* Read in the socket and catch chars '0'..'9', ':' with local echo *
* Return after a \r\n sequence *
******************************************************************************/
static VS_MachineId Catch_MAC (int iSocket)
{
unsigned char bBuff;
char* sData;
char end_loop = 0;
int iPos = 0;
int iIndex1, iIndex2;
VS_MachineId mMac;
sData = malloc(18 * sizeof(char));
while (!end_loop)
{
read(iSocket, &bBuff, 1);
switch(bBuff)
{
case IAC:
read(iSocket, &bBuff, 1);
read(iSocket, &bBuff, 1);
break; //TODO Terminal negociation
case '\r':
sData[iPos]='\0';
read(iSocket, &bBuff, 1); //Need to read next byte: should be \n
Message_send(iSocket,"\r\n", 2);
end_loop = 1;
break;
case 0x7f: // Backspace
if(iPos > 0)
{
iPos--;
sData[iPos]='\0';
Message_send(iSocket, "\x8\x20\x8", 3);
} // {0x08, 0x20, 0x08} = Backspace - Space - Backspace
break;
case 27:
break;
default:
if (((bBuff>='0')&&(bBuff<='9')) || ((bBuff>='A')&&(bBuff<='F')) || \
(bBuff==':'))
{
if (iPos <= 16)
{
sData[iPos] = bBuff;
iPos++;
sData[iPos] = '\0';
Message_send(iSocket, &bBuff, 1);
}
}
break;
}
}
// this removes ':' chars of MAC address TODO fill with 0 between 2 ':'
for (iIndex1 = 0, iIndex2 = 0; iIndex1 < iPos; iIndex1++)
{
if (sData[iIndex1] != ':')
{
sData[iIndex2] = sData[iIndex1];
iIndex2++;
}
}
sData[iIndex2] = '\0';
sscanf(sData, "%llx", &mMac);
return mMac; // sData is null padded
}
/******************************************************************************
* Catch_Passwd() *
......@@ -350,17 +455,17 @@ static char* Catch_Passwd (int iSocket)
while(iPos<254)
{
read(iSocket, &bBuff,1);
read(iSocket, &bBuff, 1);
switch(bBuff)
{
case IAC:
read(iSocket, &bBuff,2);
read(iSocket, &bBuff, 2);
break; //TODO Terminal negociation
case '\r':
sData[iPos]='\0';
read(iSocket, &bBuff,1); //Need to read next byte: should be \n
Message_send(iSocket,"\r\n",2);
read(iSocket, &bBuff, 1); //Need to read next byte: should be \n
Message_send(iSocket,"\r\n", 2);
return sData;
default:
......
......@@ -30,7 +30,7 @@
*****************************************************************************/
ERR_CODE VS_request_handler(struct VS_info_client * info_client)
{
unsigned long long macaddr_db;
VS_MachineId macaddr_db;
VS_CHANNEL channel_dest;
VS_VLAN vlan_dest;
unsigned int rc;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment