Commit 92242777 authored by Marc Ariberti's avatar Marc Ariberti

* vlcs.c splitted in vlcs.c, logger.c and config.c

parent 4a64feb6
......@@ -9,6 +9,8 @@ N: Marc Ariberti
E: marcari@via.ecp.fr
C: marcari
D: Remove network part
D: Configuration system
D: Logging system
N: Christophe Massiot
E: massiot@via.ecp.fr
......
......@@ -4,6 +4,7 @@
HEAD
* vlcs.c file splited into vlcs.c logger.c and config.c
* More elaborated logging system
* Now takes only the first field (space separated) in the config file
* The number of defined channels is now unlimited (now use a linked list)
......
......@@ -3,12 +3,23 @@
###############################################################################
VERSION=0.1.2
CC=gcc
CFLAGS=-O3 -g -I. -Wall
all: vlcs
vlcs: vlcs.c vlcs.h
$(CC) -O3 -g -I. -Wall -o $@ vlcs.c
vlcs.o: vlcs.c vlcs.h logger.h
$(CC) $(CFLAGS) -c -o vlcs.o vlcs.c
logger.o: logger.c logger.h vlcs.h
$(CC) $(CFLAGS) -c -o logger.o logger.c
config.o: config.c config.h logger.h vlcs.h
$(CC) $(CFLAGS) -c -o config.o config.c
vlcs: vlcs.o logger.o config.o
$(CC) $(CFLAGS) -o vlcs vlcs.o logger.o config.o
tar: all
mkdir miniVLCS-$(VERSION)
cp vlcs.c vlcs.h vlcs.conf Makefile README COPYING miniVLCS-$(VERSION)
......@@ -16,4 +27,4 @@ tar: all
rm -rf miniVLCS-$(VERSION)
clean:
-rm -f vlcs *.tar.gz
-rm -f vlcs *.tar.gz *.o
/*****************************************************************************
* config.c: mini-VideoLAN Channel Server, configuration system
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: config.c,v 1.1 2002/05/25 20:59:15 marcari Exp $
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Marc Ariberti <marcari@via.ecp.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>
#include <errno.h>
#include <arpa/inet.h>
#include <signal.h>
#include <time.h>
#include "vlcs.h"
#include "logger.h"
#include "config.h"
static int i_channel_max;
static time_t st_mtime; /* last modification time of the config file */
static channel_t * p_channels;
/*
* Destroy the linked list
*/
void config_DestroyList( void )
{
channel_t * p_channel = p_channels;
channel_t * p_next = NULL;
while( p_channel != NULL )
{
p_next = p_channel->next;
free(p_channel);
p_channel = p_next;
}
p_channels = NULL;
log_Info("List of channels initialized\n");
}
char * config_RemoveLF( char * str )
{
int i_pos = 0;
while( str[i_pos] )
{
if( str[i_pos] == '\n' )
{
str[i_pos] = ' ';
}
i_pos++;
}
return str;
}
/*
* Adds the item to the list
*/
void config_AddList( char * psz_item )
{
channel_t ** pp_cur = &p_channels;
channel_t * p_new;
/* finds the end of the linked list */
while( (*pp_cur) != NULL )
{
pp_cur = &((*pp_cur)->next);
}
p_new = malloc( sizeof( channel_t ) );
*pp_cur = p_new;
strncpy( p_new->psz_vlc_config, psz_item, 255 );
p_new->psz_vlc_config[255] = 0;
p_new->next = NULL;
config_RemoveLF( p_new->psz_vlc_config);
i_channel_max++;
log_Info("Adding channel %d : %s\n", i_channel_max, p_new->psz_vlc_config);
}
time_t config_GetModificationTime( void )
{
struct stat buf;
stat( CHANNEL_CONF, &buf);
return buf.st_mtime;
}
void config_ReadFile( void )
{
char psz_vlc_command[2048];
char psz_buffer[2048];
FILE * p_config;
config_DestroyList();
st_mtime = config_GetModificationTime();
p_config = fopen( CHANNEL_CONF, "r" );
if( p_config == NULL )
{
log_Alert( "Cannot fopen " CHANNEL_CONF " (%s)\n", strerror(errno) );
exit( -1 );
}
i_channel_max = -1;
while( fgets( psz_buffer, sizeof( psz_buffer ), p_config ) != NULL )
{
if( psz_buffer[0] != '#' && psz_buffer[0] != '\0' )
{
sscanf(psz_buffer, "%s", psz_vlc_command);
config_AddList(psz_vlc_command);
}
}
fclose( p_config );
}
void config_CheckUpdate()
{
if( st_mtime != config_GetModificationTime() )
{
config_ReadFile();
}
}
channel_t * config_GetChannel( int i_channel )
{
channel_t * p_channel = p_channels;
while( i_channel > 0 )
{
p_channel = p_channel->next;
if( p_channel == NULL )
{
return NULL;
}
i_channel--;
}
return p_channel;
}
int config_GetChannelNumber( void )
{
return i_channel_max + 1;
}
/*****************************************************************************
* config.c: mini-VideoLAN Channel Server, configuration system
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: config.h,v 1.1 2002/05/25 20:59:15 marcari Exp $
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Marc Ariberti <marcari@via.ecp.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
typedef struct channel_s
{
char psz_vlc_config[256];
struct channel_s * next;
} channel_t;
void config_DestroyList( void );
void config_AddList( char * psz_item );
char * config_RemoveLF( char * str );
time_t config_GetModificationTime( void );
void config_ReadFile( void );
channel_t * config_GetChannel( int i_channel );
void config_CheckUpdate( void );
int config_GetChannelNumber( void );
/*****************************************************************************
* logger.c: VideoLAN Channel Server logging facility
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: logger.c,v 1.1 2002/05/25 20:59:15 marcari Exp $
*
* Authors: Marc Ariberti <marcari@via.ecp.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdarg.h>
#include <syslog.h>
#include <errno.h>
#include <time.h>
#include "vlcs.h"
#ifdef LOG_FILE
static FILE * log_file;
#endif
/* logging system */
void log_Init( void )
{
#ifdef SYSLOG_NAME
openlog( SYSLOG_NAME, LOG_PERROR, SYSLOG_FACILITY );
#endif
#ifdef LOG_FILE
log_file = fopen( LOG_FILE, "a" );
#endif
}
void log_Close( void )
{
#ifdef SYSLOG_NAME
closelog();
#endif
#ifdef LOG_FILE
fclose(log_file);
#endif
}
void log_Info(char * format, ...)
{
va_list ap;
va_start(ap, format);
#ifdef LOG_STDOUT
vfprintf(stdout, format, ap);
#endif
#ifdef LOG_FILE
vfprintf(log_file, format, ap);
#endif
#ifdef SYSLOG_NAME
vsyslog(LOG_INFO, format, ap);
#endif
va_end(ap);
}
void log_Warn(char * format, ...)
{
va_list ap;
va_start(ap, format);
#ifdef LOG_STDOUT
vfprintf(stderr, format, ap);
#endif
#ifdef LOG_FILE
vfprintf(log_file, format, ap);
#endif
#ifdef SYSLOG_NAME
vsyslog(LOG_WARNING, format, ap);
#endif
va_end(ap);
}
void log_Error(char * format, ...)
{
va_list ap;
va_start(ap, format);
#ifdef LOG_STDOUT
vfprintf(stderr, format, ap);
#endif
#ifdef LOG_FILE
vfprintf(log_file, format, ap);
#endif
#ifdef SYSLOG_NAME
vsyslog(LOG_ERR, format, ap);
#endif
va_end(ap);
}
void log_Alert(char * format, ...)
{
va_list ap;
va_start(ap, format);
#ifdef LOG_STDOUT
vfprintf(stderr, format, ap);
#endif
#ifdef LOG_FILE
vfprintf(log_file, format, ap);
#endif
#ifdef SYSLOG_NAME
vsyslog(LOG_ALERT, format, ap);
#endif
va_end(ap);
}
/*****************************************************************************
* logger.h: VideoLAN Channel Server logging facility
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: logger.h,v 1.1 2002/05/25 20:59:15 marcari Exp $
*
* Authors: Marc Ariberti <marcari@via.ecp.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
void log_Init( void );
void log_Close( void );
void log_Info(char * format, ...);
void log_Warn(char * format, ...);
void log_Error(char * format, ...);
void log_Alert(char * format, ...);
/*****************************************************************************
* vlcs.c: VideoLAN Channel Server socket facility
* vlcs.c: mini-VideoLAN Channel Server
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: vlcs.c,v 1.7 2002/05/25 20:12:44 marcari Exp $
* $Id: vlcs.c,v 1.8 2002/05/25 20:59:15 marcari Exp $
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Marc Ariberti <marcari@via.ecp.fr>
......@@ -40,6 +40,8 @@
#include <time.h>
#include "vlcs.h"
#include "logger.h"
#include "config.h"
#define VLCS_OK 0
#define VLCS_NOTFOUND 1
......@@ -53,132 +55,10 @@
void socket_Init( void );
void socket_Get( void );
void socket_Send( struct sockaddr_in * p_sa, char * psz_message );
void config_DestroyList( void );
void config_AddList( char * psz_item );
char * config_RemoveLF( char * str );
time_t config_GetModificationTime( void );
void config_ReadFile( void );
void Close( int i_ret );
int main( int argc, char ** argv );
int i_handle;
int i_channel_min, i_channel_max;
time_t st_mtime; /* last modification time of the config file */
typedef struct channel_s
{
char psz_vlc_config[256];
struct channel_s * next;
} channel_t;
channel_t * p_channels;
FILE * log_file;
/* logging system */
void log_Init( void )
{
#ifdef vsyslog_NAME
openlog( vsyslog_NAME, LOG_PERROR, SYSLOG_FACILITY );
#endif
#ifdef LOG_FILE
log_file = fopen( LOG_FILE, "a" );
#endif
}
void log_Close( void )
{
#ifdef vsyslog_NAME
closelog();
#endif
#ifdef LOG_FILE
fclose(log_file);
#endif
}
void log_Info(char * format, ...)
{
va_list ap;
va_start(ap, format);
#ifdef LOG_STDOUT
vfprintf(stdout, format, ap);
#endif
#ifdef LOG_FILE
vfprintf(log_file, format, ap);
#endif
#ifdef vsyslog_NAME
vsyslog(LOG_INFO, format, ap);
#endif
va_end(ap);
}
void log_Warn(char * format, ...)
{
va_list ap;
va_start(ap, format);
#ifdef LOG_STDOUT
vfprintf(stderr, format, ap);
#endif
#ifdef LOG_FILE
vfprintf(log_file, format, ap);
#endif
#ifdef vsyslog_NAME
vsyslog(LOG_WARNING, format, ap);
#endif
va_end(ap);
}
void log_Error(char * format, ...)
{
va_list ap;
va_start(ap, format);
#ifdef LOG_STDOUT
vfprintf(stderr, format, ap);
#endif
#ifdef LOG_FILE
vfprintf(log_file, format, ap);
#endif
#ifdef vsyslog_NAME
vsyslog(LOG_ERR, format, ap);
#endif
va_end(ap);
}
void log_Alert(char * format, ...)
{
va_list ap;
va_start(ap, format);
#ifdef LOG_STDOUT
vfprintf(stderr, format, ap);
#endif
#ifdef LOG_FILE
vfprintf(log_file, format, ap);
#endif
#ifdef vsyslog_NAME
vsyslog(LOG_ALERT, format, ap);
#endif
va_end(ap);
}
/* Socket management */
......@@ -233,12 +113,6 @@ void socket_Get( void )
timeout.tv_usec = 0;
timeout.tv_sec = 1;
/* check if the configuratio file has been updated */
if( st_mtime != config_GetModificationTime() )
{
config_ReadFile();
}
if( select( i_handle + 1, &sockets, NULL, NULL, &timeout ) < 0 )
{
if( errno == EINTR )
......@@ -248,6 +122,8 @@ void socket_Get( void )
log_Alert( "Cannot select (%s), exiting\n", strerror(errno) );
Close( -1 );
}
config_CheckUpdate();
if( FD_ISSET( 0, &sockets ) )
{
......@@ -311,25 +187,20 @@ void socket_Get( void )
}
i_channel = strtol( psz_channel, &psz_tmp, 0 );
if( psz_tmp[0] != '\0' || i_channel < i_channel_min ||
i_channel > i_channel_max )
if( psz_tmp[0] != '\0' || i_channel < 0 ||
i_channel >= config_GetChannelNumber() )
{
socket_Send( &sa_client, "E: Channel doesn't exist" );
log_Error( "Bad channel (%s)\n", psz_channel );
return;
}
p_channel = p_channels;
while( i_channel > 0 )
p_channel = config_GetChannel( i_channel );
if( p_channel == NULL )
{
p_channel = p_channel->next;
if( p_channel == NULL )
{
socket_Send( &sa_client, "E: Channel doesn't exist" );
log_Error( "Bad channel (%s)\n", psz_channel );
return;
}
i_channel--;
log_Error( "Bad channel (%s)\n", psz_channel );
socket_Send( &sa_client, "E: Channel doesn't exist" );
return;
}
psz_config = p_channel->psz_vlc_config;
socket_Send( &sa_client, psz_config);
......@@ -363,110 +234,6 @@ void socket_Send( struct sockaddr_in * p_sa, char * psz_message )
}
}
/*
* Destroy the linked list
*/
void config_DestroyList( void )
{
channel_t * p_channel = p_channels;
channel_t * p_next = NULL;
while( p_channel != NULL )
{
p_next = p_channel->next;
free(p_channel);
p_channel = p_next;
}
p_channels = NULL;
log_Info("List of channels initialized\n");
}
char * config_RemoveLF( char * str )
{
int i_pos = 0;
while( str[i_pos] )
{
if( str[i_pos] == '\n' )
{
str[i_pos] = ' ';
}
i_pos++;
}
return str;
}
/*
* Adds the item to the list
*/
void config_AddList( char * psz_item )
{
channel_t ** pp_cur = &p_channels;
channel_t * p_new;
/* finds the end of the linked list */
while( (*pp_cur) != NULL )
{
pp_cur = &((*pp_cur)->next);
}
p_new = malloc( sizeof( channel_t ) );
*pp_cur = p_new;
strncpy( p_new->psz_vlc_config, psz_item, 255 );
p_new->psz_vlc_config[255] = 0;
p_new->next = NULL;
config_RemoveLF( p_new->psz_vlc_config);
i_channel_max++;
log_Info("Adding channel %d : %s\n", i_channel_max, p_new->psz_vlc_config);
}
time_t config_GetModificationTime( void )
{
struct stat buf;
stat( CHANNEL_CONF, &buf);
return buf.st_mtime;
}
void config_ReadFile( void )
{
char psz_vlc_command[2048];
char psz_buffer[2048];
FILE * p_config;
config_DestroyList();
st_mtime = config_GetModificationTime();
p_config = fopen( CHANNEL_CONF, "r" );
if( p_config == NULL )
{
log_Alert( "Cannot fopen " CHANNEL_CONF " (%s)\n", strerror(errno) );
exit( -1 );
}
i_channel_min = 0;
i_channel_max = -1;
while( fgets( psz_buffer, sizeof( psz_buffer ), p_config ) != NULL )
{
if( psz_buffer[0] != '#' && psz_buffer[0] != '\0' )
{
sscanf(psz_buffer, "%s", psz_vlc_command);
config_AddList(psz_vlc_command);
}
}