Commit 56b0821a authored by Benoit Steiner's avatar Benoit Steiner

Update du Makefile pour qu'il genere les dependances.

Ajout dans le code source des macros de debug (ASSERT, RZERO, etc...)
parent e878aff9
# Makefile for VideoLAN bridge program
# Benoit Steiner, VIA, ECP, <benny@via.ecp.fr>
############ Beginning of the configuration section ############
# Use the GNU C compiler
......@@ -18,20 +19,29 @@ CFLAGS += -g
#CFLAGS += -g -pg
# Compiler parameters style
CFLAGS += -Wall -O6 -static
CFLAGS += -Wall
#CFLAGS += -O6
#CFLAGS += -static
# To generate the dependencies
DFLAGS += -MM
SHELL = /bin/sh
# Use threads
CFLAGS += -D_REENTRANT
LIB += -lpthread
# Use electric fence to detect illegal memory access
LIB += -lefence
#LIB += -lefence
# Use dmalloc to detect memory leaks
#LIB += -ldmalloc
# Use ccmalloc to check memory use
#LIB += -lccmalloc -ldl
LIB += -lccmalloc -ldl
############ Beginning of the objet description section ############
############ Beginning of the files description section ############
OBJ = \
VLANbridge.o \
......@@ -48,19 +58,37 @@ OBJ = \
route.o \
arp.o \
connect.o \
cfgfile.o \
cfgfile.o
SRC := $(OBJ:%.o=%.c)
DEP := $(OBJ:%.o=dep/%.d)
############ Beginning of the target definition section ############
all: VLANbridge
VLANbridge: $(OBJ)
$(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS) $(CFLAGS)
chmod 755 $@
$(DEP): dep/%.d: %.c
@test -d dep/$(dir $*) || mkdir -p dep/$(dir $*)
@echo "Generating dependancies for $*.c"
@$(SHELL) -ec '$(CC) $(DFLAGS) $(CFLAGS) $< \
| sed '\''s/$(subst .,\.,$(notdir $*))\.o[ :]*/$(subst /,\/,$*).o \
dep\/$(subst /,\/,$*).d : /g'\'' > $@; \
[ -s $@ ] || rm -f $@'
$(OBJ): %.o: dep/%.d
clean:
for i in $(OBJ); \
do rm -f $$i; \
done; \
rm -f core
cvsclean: clean
rm -f VLANbridge; \
rm -Rf *.o *.log
rm -f $(OBJ)
distclean: clean
rm -f VLANbridge
rm -Rf dep/
rm -Rf *.o *.log *.dbg
rm -Rf *~ \#*
......@@ -7,6 +7,11 @@
*/
/* For Solaris */
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
......@@ -16,6 +21,7 @@
#include <netinet/in.h>
#include "define.h"
#include "debug.h"
#include "signal.h"
#include "log.h"
#include "cfgfile.h"
......@@ -45,7 +51,11 @@ int ConfigBridge(FILE* hCfgFile, char** pstrLogFile, char** pstrPort)
char* strVarName = NULL;
char* strVarVal = NULL;
int iRc = 0;
ASSERT(hCfgFile);
ASSERT(pstrLogFile);
ASSERT(pstrPort);
/* Find the right section */
iRc = FindCfg(hCfgFile, "Bridge");
......@@ -112,6 +122,8 @@ int StopBridge(int iErrorStatus)
/***************************************************************************/
void Usage(char* strProg)
{
ASSERT(strProg);
#define HELP " \
Usage : %s\n \
[-c <cfg>]: Config file\n"
......
......@@ -8,6 +8,10 @@
*/
/* For Solaris */
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
......@@ -15,8 +19,10 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "debug.h"
#include "log.h"
#include "arp.h"
......@@ -36,6 +42,9 @@ int Arp_Add(struct sockaddr_in* psaAddr, char* strDev, int iSockFd)
int iRc = 0;
struct arpreq arReq;
ASSERT(psaAddr);
ASSERT(strDev);
/* Init the structure */
bzero (&arReq, sizeof(arReq));
......@@ -81,6 +90,9 @@ int Arp_Del (struct sockaddr_in* psaAddr, char* strDev, int iSockFd)
int iRc = 0;
struct arpreq arReq;
ASSERT(psaAddr);
ASSERT(strDev);
/* Init the structure */
bzero(&arReq, sizeof(arReq));
......
......@@ -6,12 +6,17 @@
*/
/* For Solaris */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <pthread.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include "debug.h"
#include "base.h"
......@@ -21,6 +26,9 @@
/***************************************************************************/
int Remove(struct s_Base* psbBase, struct s_Cell* pcCellPos)
{
ASSERT(psbBase);
ASSERT(pcCellPos);
psbBase->iSize--;
/* Rebuild all links */
......@@ -69,6 +77,8 @@ struct s_Base* CreateBase()
int DestroyBase(struct s_Base* psbBase)
{
int iRc = 0;
ASSERT(psbBase);
/* Empty the list */
while (psbBase->iSize > 0 && !iRc)
......@@ -92,6 +102,9 @@ int AddToBase(struct s_Base* psbBase, struct s_Record* psrNewMember)
int iRc = 0;
struct s_Cell* pcNewCell = NULL;
ASSERT(psbBase);
ASSERT(psrNewMember);
if ((pcNewCell = (struct s_Cell *)malloc(sizeof(struct s_Cell))) > 0)
{
/* initialise the links of the new element */
......@@ -122,16 +135,18 @@ struct s_Record* FindInBase(struct s_Base* psbBase, struct sockaddr_in* psaHost)
struct s_Cell* pcCurrent = psbBase->cFirst.pcNext;
struct s_Record* psrResult = NULL;
printf ("Searching in list...\n");
ASSERT(psbBase);
ASSERT(psaHost);
// printf ("Searching in list...\n");
while (iIndex < psbBase->iSize)
{
printf ("Search %d\n", iIndex);
if (pcCurrent->psrJob->saHost.sin_addr.s_addr == psaHost->sin_addr.s_addr)
{
/* Set return value */
psrResult = pcCurrent->psrJob;
// printf("Founded at offset: %d\n", iIndex);
break;
}
......@@ -141,3 +156,11 @@ struct s_Record* FindInBase(struct s_Base* psbBase, struct sockaddr_in* psaHost)
return psrResult;
};
inline int GetBaseSize(struct s_Base* psbBase)
{
ASSERT(psbBase);
return psbBase->iSize;
}
/* VideoLAN VLANbridge : internal database
*
* Definition file
*
* You need to include <pthread.h> and "performer.h" to use this module
*/
......@@ -38,5 +39,6 @@ int DestroyBase();
int AddToBase(struct s_Base* psbBase, struct s_Record* psrJob);
int DeleteFromBase(struct s_Base* psbBase, struct sockaddr_in psaHost);
struct s_Record* FindInBase(struct s_Base* psbBase, struct sockaddr_in* psaHost);
int GetBaseSize(struct s_Base* psbBase);
#endif
......@@ -21,6 +21,7 @@
#include <errno.h>
#include <string.h>
#include "debug.h"
#include "log.h"
#include "cfgfile.h"
......@@ -39,6 +40,8 @@ FILE* OpenCfg (const char* strFileName)
{
/* Open file for read */
FILE* pfdCfgFile = fopen (strFileName, "r");
ASSERT(strFileName);
if (!pfdCfgFile)
{
......@@ -62,6 +65,10 @@ int ReadCfg (FILE* pfdCfgFile, char** pstrVarName, char** pstrVarVal)
char strValue[LINE_MAX_SIZE];
char* pcPosInLine = NULL;
ASSERT(pfdCfgFile);
ASSERT(pstrVarName);
ASSERT(pstrVarVal);
/* Read until one useful line was found or an error occured */
while (!iStop)
{
......@@ -153,6 +160,9 @@ int FindCfg(FILE* pfdCfgFile, const char* strSection)
char* strVarName = NULL;
char* strVarVal = NULL;
ASSERT(pfdCfgFile);
ASSERT(strSection);
while (iRc == CFG_OK || iRc == CFG_BEGIN || iRc == CFG_END)
{
iRc = ReadCfg(pfdCfgFile, &strVarName, &strVarVal);
......@@ -174,6 +184,8 @@ int FindCfg(FILE* pfdCfgFile, const char* strSection)
int CloseCfg (FILE* pfdCfgFile)
{
int iRc = 0;
ASSERT(pfdCfgFile);
if (fclose(pfdCfgFile) == EOF)
{
......
......@@ -3,7 +3,7 @@
* Benoit Steiner, ECP, <benny@via.ecp.fr>
*
* TO DO: Check whether incoming conections come from VLANserver
Refaire la sequence de login (trop mauvaise, password pas chackes, etc...)
Refaire la sequence de login (trop mauvaise, password pas checkes, etc...)
*/
#include <sys/types.h>
......@@ -17,6 +17,7 @@
#include <string.h>
#include <pthread.h>
#include "debug.h"
#include "listener.h"
#include "performer.h"
#include "sender.h"
......@@ -143,6 +144,8 @@ int OpenWaitingSocket (const char* strWaitPort)
struct servent* sp;
int iRc = 0;
ASSERT(strWaitPort);
/* Open socket for waiting on TCP connections */
if ((iWaitSockFd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
{
......@@ -227,6 +230,8 @@ int RecvMsg(int iSockFd, char* strBuff)
int iPos = 0;
int iRc = 0;
ASSERT(strBuff);
while (iRc >= 0)
{
iRc = recv(iSockFd, strBuff+iPos, 1, 0);
......@@ -273,6 +278,10 @@ int Authenticate (const char* strLogin, const char* strPasswd, int* piLogin)
{
int iRc = 0;
ASSERT(strLogin);
ASSERT(strPasswd);
ASSERT(piLogin);
if (!strcmp (strLogin, "admin"))
{
/* Administrative call */
......@@ -305,6 +314,8 @@ int CheckIdentity (int iSockFd, int* piLogin)
/* Receive the login */
int iRc = RecvMsg(iSockFd, strLoginBuff);
ASSERT(piLogin);
/* Parse the received message */
if (!iRc)
{
......
......@@ -7,11 +7,17 @@
*/
/* For Solaris */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <net/if.h>
#include <netinet/in.h>
#include "debug.h"
#include "log.h"
#include "fifo.h"
#include "define.h"
......@@ -65,6 +71,9 @@ int FifoPush(struct s_Fifo* psfFifo, struct s_Request* psrToStore)
/* Keep the lock during the complete operation */
int iRc = pthread_mutex_lock(&psfFifo->FifoLock);
ASSERT(psfFifo);
ASSERT(psrToStore);
if (!iRc)
{
// printf ("fifo Size: %d\n", psfFifo->iSize);
......@@ -111,6 +120,9 @@ int FifoPull(struct s_Fifo* psfFifo, struct s_Request** psrTask)
{
int iRc = pthread_mutex_lock(&psfFifo->FifoLock);
ASSERT(psfFifo);
ASSERT(psrTask);
if (!iRc)
{
if (psfFifo->iSize == 0)
......
......@@ -9,11 +9,17 @@
*/
/* For Solaris */
#include <sys/types.h>
//#include <sys/sockio.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <linux/if_ether.h>
//#include <linux/if_ether.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
......@@ -24,8 +30,8 @@
#include "log.h"
#include "gratuitous.h"
#define MAC_BCAST_ADDR "\xff\xff\xff\xff\xff\xff"
#define IP_BCAST_ADDR "\xff\xff\xff\xff"
#define MAC_BCAST "\xff\xff\xff\xff\xff\xff"
#define IP_BCAST "\xff\xff\xff\xff"
#define MOD_NAME MOD_PERFORMER
......@@ -33,7 +39,8 @@
/* ARP message which will be send in the raw socket */
struct s_ARPMsg
{
struct ethhdr ethhdr; /* Ethernet header */
// struct ethhdr ethhdr; /* Ethernet header */
struct ether_header ethhdr; /* Ethernet header */
u_short htype; /* hardware type (must be ARPHRD_ETHER) */
u_short ptype; /* protocol type (must be ETH_P_IP) */
u_char hlen; /* hardware address length (must be 6) */
......@@ -56,12 +63,12 @@ int MakeMsg (const char* pTInAddr, const char* pTHwAddr,
{
bzero(pamPacket, sizeof(*pamPacket));
bcopy(MAC_BCAST_ADDR, pamPacket->ethhdr.h_dest, 6); /* MAC destination address */
bcopy(pSHwAddr, pamPacket->ethhdr.h_source, 6); /* MAC source address */
pamPacket->ethhdr.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
bcopy(MAC_BCAST, pamPacket->ethhdr.ether_dhost, 6); /* MAC destination address */
bcopy(pSHwAddr, pamPacket->ethhdr.ether_shost, 6); /* MAC source address */
pamPacket->ethhdr.ether_type = htons(ETHERTYPE_ARP); /* protocol type (Ethernet) */
pamPacket->htype = htons(ARPHRD_ETHER); /* hardware type */
pamPacket->ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
pamPacket->ptype = htons(ETHERTYPE_IP); /* protocol type (IP) */
pamPacket->hlen = 6; /* hardware address length */
pamPacket->plen = 4; /* protocol address length */
pamPacket->operation = htons(ARPOP_REQUEST); /* ARP op code */
......@@ -83,7 +90,7 @@ int OpenSocket(int* piSockFd)
int iRc = 0;
int iOptVal = 1;
*piSockFd = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ARP));
*piSockFd = socket(AF_INET, SOCK_PACKET, htons(ETHERTYPE_ARP));
if (*piSockFd < 0)
{
......@@ -117,7 +124,7 @@ int SendMsg (const char* strDev, const struct s_ARPMsg* pamPacket, int iSockFd)
bzero(&saIfName, sizeof(saIfName));
strcpy(saIfName.sa_data, strDev);
if (sendto(iSockFd, pamPacket, sizeof(*pamPacket), 0, &saIfName,
if (sendto(iSockFd, (char *)pamPacket, sizeof(*pamPacket), 0, &saIfName,
sizeof(saIfName)) < 0)
{
Log(LOG_ERROR, MOD_NAME, "Unable to send gratuitous ARP: %s",
......
......@@ -5,15 +5,19 @@
* To-do-list : Everything
*/
#include "list.h"
#include <stdlib.h>
#include <stdio.h>
#include "debug.h"
#include "list.h"
void remove_from_list (struct list *l, struct cell* pos)
/* Remove element from the list */
void remove_from_list (struct list *l, struct cell* pos)
{
ASSERT(l);
ASSERT(pos);
/* Rebuild all links */
pos->previous->next = pos->next;
pos->next->previous = pos->previous;
......@@ -30,6 +34,9 @@ int find_in_list (struct list *l, pthread_t id, struct cell **answer)
{ int i;
struct cell *c = l->first.next;
ASSERT(l);
ASSERT(answer);
/*printf ("Seeking the element (Beginning at %p)\n", l->first.next);*/
if (l->size <= 0)
return -1;
......@@ -69,7 +76,10 @@ struct list * create_list ()
void destroy_list (struct list *l)
{ while (l->size > 0)
{
ASSERT(l);
while (l->size > 0)
{ remove_from_list (l, l->first.next);
l->size --;
};
......@@ -81,9 +91,18 @@ void destroy_list (struct list *l)
int add_to_list (struct list *l, struct s_performer *p)
/* Add an element to the end of the list (before the 'last' element) and return its address */
{
struct cell *c;
struct cell *c = (struct cell *)malloc (sizeof (struct cell));
ASSERT(l);
ASSERT(p);
if ( (c = (struct cell *)malloc (sizeof (struct cell))) )
if (!c)
{
/* Coud'nt allocate memory space */
log(LOG_WARN, "Can't create member");
return -1;
}
else
{
/*printf ("member = %p\n", c);*/
c->next = &l->last;
......@@ -98,8 +117,6 @@ int add_to_list (struct list *l, struct s_performer *p)
c->performer = p; /* Tell the address of the s_performer structure */
return 1;
};
printf ("Can't create member");
return -1; /* Coud'nt allocate memory space */
};
......@@ -108,15 +125,18 @@ int delete_from_list (struct list *l, pthread_t id)
{
struct cell *pos;
ASSERT(l);
switch (find_in_list (l, id, &pos))
{ case 1 :
{
case 1 :
/*printf ("Removing element at %p\n", pos);
remove_from_list (l, pos);
printf ("Element removed from list\nList is now : %p, %d, %p, %p\n", l, l->size, l->first.next, l->last.previous);*/
break;
case 0 :
printf ("Element was not found\n");
log(LOG_WARN, "Element was not found\n");
break;
default :
......
......@@ -7,6 +7,10 @@
*/
/* For Solaris */
#include <strings.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
......@@ -17,6 +21,7 @@
#include <stdio.h>
#include <pthread.h>
#include "debug.h"
#include "log.h"
#include "performer.h"
#include "manager.h"
......@@ -46,6 +51,8 @@ int InitListener(struct s_Listener* listener)
/* Thread initialisations */
int iRc = pthread_cond_init (&listener->WakeUpSignal, NULL);
ASSERT(listener);
/* Now create the thread used to listen to the VLANserver requests */
if (!iRc)
{
......@@ -79,6 +86,9 @@ int mac_atob (char* MAC, unsigned char* mac_addr)
int hex, n;
int iRc = 0;
ASSERT(MAC);
ASSERT(mac_addr);
if (strlen (MAC) == 17) /* Good MAC address length */
{
for (n = 0; n < 6; n++)
......@@ -104,17 +114,20 @@ int mac_atob (char* MAC, unsigned char* mac_addr)
int ParseMsg (char* strMsg, struct s_Request* psrBuf)
{
int iRc = 0;
char IP[16];
char MAC[17];
char strIP[16];
char strMAC[17];
ASSERT(strMsg);
ASSERT(psrBuf);
/* Init the buffers */
bzero((char *)psrBuf, sizeof (struct s_Request));
bzero(IP, 16);
bzero(MAC, 17);
bzero(strIP, 16);
bzero(strMAC, 17);
/* Parse received request */
if (sscanf(strMsg, "%d %d %s %s %d %d", &psrBuf->iType, &psrBuf->iRequestId,
MAC, IP, &psrBuf->iVLANdst, &psrBuf->iVLANsrc) != 6)
strMAC, strIP, &psrBuf->iVLANdst, &psrBuf->iVLANsrc) != 6)
{
/* Message did not respect the good format */
iRc = 1;
......@@ -124,12 +137,12 @@ int ParseMsg (char* strMsg, struct s_Request* psrBuf)
if (!iRc)
{
psrBuf->saClientIP.sin_family = AF_INET;
iRc = !inet_aton(IP, &(psrBuf->saClientIP.sin_addr));
iRc = !inet_aton(strIP, &(psrBuf->saClientIP.sin_addr));
}
/* Convert MAC */
if (!iRc)
iRc = mac_atob(MAC, (unsigned char *)&psrBuf->saClientMAC.sa_data);
iRc = mac_atob(strMAC, (unsigned char *)&psrBuf->saClientMAC.sa_data);
return iRc;
}
......@@ -142,6 +155,8 @@ int GetSocket(struct s_Listener* pslListener)
{
int iRc = pthread_mutex_lock(&ConnectionLock);
ASSERT(pslListener);
if (!iRc)
{
if (iConnectionState == 0)
......@@ -168,6 +183,8 @@ int Receive(struct s_Listener* share)
int iConnectionOK = 1;
int iIndex;
ASSERT(share);
Log (LOG_NOTE, MOD_NAME, "Waiting for VLANserver requests");
while (iConnectionOK > 0)
......@@ -234,6 +251,8 @@ void *ListenerThread(void *arg)
int iMustLoop = 1;
int iRc = 0;
ASSERT(arg);
#ifdef DEBUG
Log (LOG_NOTE, MOD_NAME, "Listener thread is running");
#endif
......@@ -294,6 +313,8 @@ int CancelListener (struct s_Listener* pslListener)
{
int iRc = 0;
ASSERT(pslListener);
if (pslListener->tid != 0)
{
Log(LOG_NOTE, MOD_NAME, "Stoppping listener thread");
......@@ -313,6 +334,8 @@ int FreeListener(struct s_Listener* pslListener)
{
int iRc = 0;
ASSERT(pslListener);
if (pslListener->tid != 0)
{
iRc = pthread_cancel(pslListener->tid);
......
......@@ -12,6 +12,9 @@
*/
/* For Solaris */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
......@@ -19,6 +22,7 @@
#include <time.h>
#include <pthread.h>
#include "debug.h"
#include "log.h"
......@@ -39,6 +43,7 @@ pthread_mutex_t ptmLogFile;
int OpenLog (char* strLogName)
{
int iRc = 0;
ASSERT(strLogName)
if ( !(fdLogFile = fopen (strLogName, "w+")) )
{
......@@ -91,12 +96,13 @@ int CloseLog ()
/**************************************************************************/
/* Append a string in the log file */
/**************************************************************************/
void Log (int iLevel, int iModule, const char* pcMsg, ...)
void Log (int iLevel, int iModule, const char* strMsg, ...)
{
char strLogBuff[LOG_TIMESIZE+LOG_LEVELSIZE+LOG_MODULESIZE+LOG_BUFFSIZE+1];
va_list ap;
time_t tTime;
struct tm* ptmTime;
ASSERT(strMsg)
/* Record the time */
time (&tTime);
......@@ -145,8 +151,8 @@ void Log (int iLevel, int iModule, const char* pcMsg, ...)
}
/* Now add the log message to the buffer */
va_start(ap, pcMsg);
vsnprintf(strLogBuff+strlen(strLogBuff), LOG_BUFFSIZE, pcMsg, ap);
va_start(ap, strMsg);
vsnprintf(strLogBuff+strlen(strLogBuff), LOG_BUFFSIZE, strMsg, ap);
va_end(ap);
/* Add the final CR */
......@@ -175,6 +181,7 @@ void LogScreen (int iLevel, int iModule, const char* strMsg, ...)
{
char strLogBuff[LOG_TIMESIZE+LOG_LEVELSIZE+LOG_MODULESIZE+LOG_BUFFSIZE];
va_list ap;
ASSERT(strMsg)
/* Build the complete message */
va_start(ap, strMsg);
......
......@@ -16,6 +16,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include "debug.h"
#include "log.h"
#include "listener.h"
#include "performer.h"
......@@ -42,15 +43,15 @@ int WarnManager(struct s_Manager* psmManager, struct s_Request* psrJob,
{
int iRc = 0;
ASSERT(psmManager);
ASSERT(psrJob);
/* Set the next action to performed */
psrJob->iNextStep = iJobType;
/* Push the request in the manager FIFO */
iRc = FifoPush(psmManager->psfWaitingJobs, psrJob);
// printf ("Push -> %d\n", iRc);
// printf ("Signal sent: %d\n", psrJob->iNextStep);
return iRc;
}
......@@ -61,9 +62,11 @@ int WarnManager(struct s_Manager* psmManager, struct s_Request* psrJob,