Skip to content
Snippets Groups Projects
Commit 64843360 authored by Marek Vavrusa's avatar Marek Vavrusa
Browse files

Workaround setjmpbuf(), fixed parser, now working with server.

parent e90ed08e
No related branches found
No related tags found
No related merge requests found
all: cf-test
clean:
rm -f cf-test
rm -f *.o
rm -f cf-lex.c
rm -f cf-parse.output cf-parse.tab.c cf-parse.tab.h
cf-test: cf-lex.o cf-parse.tab.o main.o conf.o
gcc -o cf-test cf-lex.o cf-parse.tab.o main.o conf.o
%.o: %.c
gcc -I.. -c $^
cf-parse.tab.h: cf-parse.tab.c
cf-parse.tab.c: cf-parse.y
bison -bcf-parse -dv -pcf_ cf-parse.y
cf-lex.c: cf-lex.l
flex -s -B -8 -ocf-lex.c -Pcf_ cf-lex.l
......@@ -19,13 +19,13 @@
#include "conf/conf.h"
#include "cf-parse.h"
//int (*cf_read_hook)(unsigned char *buf, unsigned int max);
extern void yyerror(const char *str); // cf-parse.y
//#define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max);
//#define YY_NO_UNPUT
//#define YY_FATAL_ERROR(msg) yyerror(msg)
/*%token <alg> TSIG_ALGO_NAME*/
/* Imported symbols. */
extern void cf_error(const char *msg);
extern int cf_read_hook(char *buf, size_t nbytes);
#define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max);
#define YY_NO_UNPUT
%}
......@@ -62,7 +62,7 @@ zones return ZONES;
storage return STORAGE;
{DIGIT}+ {
yylval.i = atoi(yytext);
cf_lval.i = atoi(yytext);
return NUM;
}
......@@ -70,39 +70,39 @@ storage return STORAGE;
unsigned char buf[sizeof(struct in_addr)];
if (inet_pton(AF_INET, yytext, buf))
return IPA;
yyerror("Invalid IP address.");
cf_error("Invalid IP address.");
}
({HEXA}*::|({HEXA}*:){3,})({HEXA}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {
unsigned char buf[sizeof(struct in6_addr)];
if (inet_pton(AF_INET6, yytext, buf))
return IPA;
yyerror("Invalid IPv6 address.");
cf_error("Invalid IPv6 address.");
}
gss-tsig { yylval.alg = GSS_TSIG; return TSIG_ALGO_NAME; }
hmac-md5 { yylval.alg = HMAC_MD5; return TSIG_ALGO_NAME; }
hmac-sha1 { yylval.alg = HMAC_SHA1; return TSIG_ALGO_NAME; }
hmac-sha224 { yylval.alg = HMAC_SHA224; return TSIG_ALGO_NAME; }
hmac-sha256 { yylval.alg = HMAC_SHA256; return TSIG_ALGO_NAME; }
hamc-sha384 { yylval.alg = HMAC_SHA384; return TSIG_ALGO_NAME; }
hmac-sha512 { yylval.alg = HMAC_SHA512; return TSIG_ALGO_NAME; }
gss-tsig { cf_lval.alg = GSS_TSIG; return TSIG_ALGO_NAME; }
hmac-md5 { cf_lval.alg = HMAC_MD5; return TSIG_ALGO_NAME; }
hmac-sha1 { cf_lval.alg = HMAC_SHA1; return TSIG_ALGO_NAME; }
hmac-sha224 { cf_lval.alg = HMAC_SHA224; return TSIG_ALGO_NAME; }
hmac-sha256 { cf_lval.alg = HMAC_SHA256; return TSIG_ALGO_NAME; }
hamc-sha384 { cf_lval.alg = HMAC_SHA384; return TSIG_ALGO_NAME; }
hmac-sha512 { cf_lval.alg = HMAC_SHA512; return TSIG_ALGO_NAME; }
["][^"\n]*["] {
yytext[yyleng-1] = 0;
yylval.t = strdup(yytext + 1);
cf_lval.t = strdup(yytext + 1);
return TEXT;
}
["][^"\n]*\n yyerror("Unterminated string.");
["][^"\n]*\n cf_error("Unterminated string.");
{ALNUM}+ {
yylval.t = strdup(yytext);
cf_lval.t = strdup(yytext);
return TEXT /* Last resort, alphanumeric word. */;
}
[a-zA-Z0-9\.]+ {
yylval.t = strdup(yytext);
cf_lval.t = strdup(yytext);
return ZONE;
}
......
......@@ -3,32 +3,13 @@
#include <stdio.h>
#include "conf.h"
config_t *new_config;
extern void cf_error(const char *msg);
extern config_t *new_config;
static conf_iface_t *this_iface;
static conf_key_t *this_key;
static conf_server_t *this_server;
static conf_zone_t *this_zone;
#ifdef CF_STANDALONE
#include <stdlib.h>
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap()
{
return 1;
}
main()
{
new_config = malloc(sizeof(config_t));
yyparse();
free(new_config);
}
#endif // CF_STANDALONE
%}
%union {
......
......@@ -2,15 +2,39 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>
#include <pthread.h>
#include "conf.h"
#include "common.h"
/* Prevent warnings from -Wmissing-prototypes. */
/* Prototypes for cf-parse.y */
extern int cf_parse();
static jmp_buf conf_jmpbuf;
config_t *new_config;
static config_t *s_config;
/* Singleton config. */
static config_t *s_config = 0;
/* Config parser lock. */
static volatile int _parser_res = 0;
static FILE* _parser_fp = 0;
static pthread_mutex_t _parser_lock = PTHREAD_MUTEX_INITIALIZER;
/* Config file read hook. */
int cf_read_hook(char *buf, size_t nbytes) {
if (_parser_fp == 0) {
return -1;
}
// Read a maximum of nbytes
return fread(buf, 1, nbytes, _parser_fp);
}
/* Config error report. */
void cf_error(const char *msg)
{
log_error("Config parser error: %s\n", msg);
_parser_res = -1;
}
config_t *config_new(const char* path)
{
......@@ -26,20 +50,32 @@ config_t *config_new(const char* path)
int config_parse(config_t *conf)
{
if (setjmp(conf_jmpbuf)) {
return 1;
if (!conf->filename) {
return -1;
}
int ret = 0;
pthread_mutex_lock(&_parser_lock);
// {
// Hook new configuration
new_config = conf;
//cf_parse();
return 0;
}
_parser_fp = fopen(conf->filename, "r");
if (_parser_fp == 0) {
pthread_mutex_unlock(&_parser_lock);
return -2;
}
void cf_error(char *msg)
{
fputs(msg, stderr);
fputc('\n', stderr);
longjmp(conf_jmpbuf, 1);
// Parse config
_parser_res = 0;
cf_parse();
ret = _parser_res;
// }
pthread_mutex_unlock(&_parser_lock);
fclose(_parser_fp);
_parser_fp = 0;
return ret;
}
void config_free(config_t *conf)
......
......@@ -14,7 +14,6 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include "lib/lists.h"
......@@ -184,10 +183,6 @@ typedef struct {
list zones; /*!< List of zones. */
} config_t;
extern config_t *new_config;
extern int (*cf_read_hook)(unsigned char *buf, unsigned int max);
/* Specific configuration API. */
config_t *config_new(const char* path);
int config_parse(config_t *conf);
......
#include <unistd.h>
#include <stdio.h>
#include "tap_unit.h"
#include "conf/conf.h"
static FILE* conf_fp = 0;
static int conf_tests_count(int argc, char *argv[]);
static int conf_tests_run(int argc, char *argv[]);
......@@ -21,12 +19,7 @@ unit_api conf_tests_api = {
*/
static int conf_tests_count(int argc, char *argv[])
{
return 3;
}
static int cf_read(unsigned char *dest, unsigned int len)
{
return read(fileno(conf_fp), dest, len);
return 2;
}
/*! Run all scheduled tests for given parameters.
......@@ -45,26 +38,17 @@ static int conf_tests_run(int argc, char *argv[])
}
}
// Test 1: Open configuration file for reading
conf_fp = fopen(config_fn, "r");
ok(conf_fp != 0, "open configuration file %s", config_fn);
if (conf_fp == 0) {
return 1;
}
// Test 2: Allocate new config
// Test 1: Allocate new config
config_t *conf = config_new(config_fn);
ok(conf != 0, "config_new()");
cf_read_hook = cf_read;
// Test 3: Parse config
// Test 2: Parse config
int ret = config_parse(conf);
ok(ret == 0, "parsing configuration file %s", config_fn);
// Deallocating config
config_free(conf);
fclose(conf_fp);
conf_fp = 0;
return 0;
}
......@@ -20,15 +20,15 @@ int main(int argc, char *argv[])
// Build test set
unit_api *tests[] = {
&skiplist_tests_api, //! Skip list unit
&dthreads_tests_api, //! DThreads testing unit
&da_tests_api, //! Dynamic array unit
&cuckoo_tests_api, //! Cuckoo hashing unit
&zonedb_tests_api, //! Zone database unit
//&skiplist_tests_api, //! Skip list unit
//&dthreads_tests_api, //! DThreads testing unit
//&da_tests_api, //! Dynamic array unit
//&cuckoo_tests_api, //! Cuckoo hashing unit
//&zonedb_tests_api, //! Zone database unit
&conf_tests_api, //! Configuration parser tests
&dnslib_tests_api, //! DNS library unit
&server_tests_api, //! Server unit
&slab_tests_api, //! SLAB allocator unit
//&dnslib_tests_api, //! DNS library unit
//&server_tests_api, //! Server unit
//&slab_tests_api, //! SLAB allocator unit
NULL
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment