Skip to content

Commit

Permalink
Merge pull request Ettercap#552 from LocutusOfBorg/405-fix
Browse files Browse the repository at this point in the history
405 fix
  • Loading branch information
LocutusOfBorg committed May 30, 2014
2 parents be97c11 + a64913d commit 5188dea
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 122 deletions.
5 changes: 4 additions & 1 deletion include/ef.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct globals {
};

/* in el_main.c */
extern struct globals gbls;
extern struct globals *gbls;

#define GBL_OPTIONS gbls
#define GBL gbls
Expand Down Expand Up @@ -100,6 +100,9 @@ extern struct globals gbls;
#define EC_COLOR_CYAN
#endif

EC_API_EXTERN void globals_alloc(void);
EC_API_EXTERN void globals_free(void);

#endif /* EL_H */

/* EOF */
Expand Down
8 changes: 4 additions & 4 deletions include/ef_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

#include <ec_filter.h>

#define SCRIPT_ERROR(x, ...) FATAL_ERROR("\n[%s:%d]: "x, GBL_OPTIONS.source_file, GBL.lineno, ## __VA_ARGS__ );
#define SCRIPT_ERROR(x, ...) FATAL_ERROR("\n[%s:%d]: "x, GBL_OPTIONS->source_file, GBL->lineno, ## __VA_ARGS__ );

#define WARNING(x) do { \
if (!GBL_OPTIONS.suppress_warnings) \
FATAL_ERROR("\n[%s:%ld]: WARNING "x, GBL_OPTIONS.source_file, (unsigned long)GBL.lineno); \
if (!GBL_OPTIONS->suppress_warnings) \
FATAL_ERROR("\n[%s:%ld]: WARNING "x, GBL_OPTIONS->source_file, (unsigned long)GBL->lineno); \
else \
fprintf(stderr, "\n[%s:%ld]: WARNING "x, GBL_OPTIONS.source_file, (unsigned long)GBL.lineno); \
fprintf(stderr, "\n[%s:%ld]: WARNING "x, GBL_OPTIONS->source_file, (unsigned long)GBL->lineno); \
} while(0)

/* ef_main */
Expand Down
11 changes: 7 additions & 4 deletions include/el.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ struct globals {
};

/* in el_main.c */
extern struct globals gbls;
extern struct globals *gbls;

#define GBL gbls


#define GBL_LOGFILE GBL.logfile
#define GBL_LOG_FD GBL.fd
#define GBL_TARGET (GBL.t)
#define GBL_LOGFILE GBL->logfile
#define GBL_LOG_FD GBL->fd
#define GBL_TARGET (GBL->t)
#define GBL_PROGRAM "etterlog"


Expand Down Expand Up @@ -148,6 +148,9 @@ extern struct globals gbls;
#define COL_MAGENTA 35
#define COL_CYAN 36

EC_API_EXTERN void globals_alloc(void);
EC_API_EXTERN void globals_free(void);


#endif /* EL_H */

Expand Down
44 changes: 29 additions & 15 deletions utils/etterfilter/ef_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,35 @@

#include <stdarg.h>

#define GBL_FREE(x) do{ if (x != NULL) { free(x); x = NULL; } }while(0)

/* globals */

extern FILE * yyin; /* from scanner */
extern int yyparse (void); /* from parser */

/* global options */
struct globals gbls;

/* protos */
void clean_exit(int errcode);
struct globals *gbls;

/*******************************************/

int main(int argc, char *argv[])
{

globals_alloc();
/* etterfilter copyright */
fprintf(stdout, "\n" EC_COLOR_BOLD "%s %s" EC_COLOR_END " copyright %s %s\n\n",
GBL_PROGRAM, EC_VERSION, EC_COPYRIGHT, EC_AUTHORS);

/* initialize the line number */
GBL.lineno = 1;
GBL->lineno = 1;

/* getopt related parsing... */
parse_options(argc, argv);

/* set the input for source file */
if (GBL_OPTIONS.source_file) {
yyin = fopen(GBL_OPTIONS.source_file, "r");
if (GBL_OPTIONS->source_file) {
yyin = fopen(GBL_OPTIONS->source_file, "r");
if (yyin == NULL)
FATAL_ERROR("Input file not found !");
} else {
Expand All @@ -72,7 +72,7 @@ int main(int argc, char *argv[])
load_constants();

/* print the message */
fprintf(stdout, "\n Parsing source file \'%s\' ", GBL_OPTIONS.source_file);
fprintf(stdout, "\n Parsing source file \'%s\' ", GBL_OPTIONS->source_file);
fflush(stdout);

ef_debug(1, "\n");
Expand All @@ -85,16 +85,12 @@ int main(int argc, char *argv[])

/* write to file */
if (write_output() != ESUCCESS)
FATAL_ERROR("Cannot write output file (%s)", GBL_OPTIONS.output_file);

FATAL_ERROR("Cannot write output file (%s)", GBL_OPTIONS->output_file);
globals_free();
return 0;
}


void clean_exit(int errcode) {
exit(errcode);
}

/*
* print debug information
*/
Expand All @@ -103,7 +99,7 @@ void ef_debug(u_char level, const char *message, ...)
va_list ap;

/* if not in debug don't print anything */
if (GBL_OPTIONS.debug < level)
if (GBL_OPTIONS->debug < level)
return;

/* print the mesasge */
Expand All @@ -114,6 +110,24 @@ void ef_debug(u_char level, const char *message, ...)

}

void globals_alloc(void)
{

SAFE_CALLOC(gbls, 1, sizeof(struct globals));

return;
}

void globals_free(void)
{
SAFE_FREE(gbls->source_file);
SAFE_FREE(gbls->output_file);
SAFE_FREE(gbls);

return;

}

/* EOF */

// vim:ts=3:expandtab
Expand Down
6 changes: 3 additions & 3 deletions utils/etterfilter/ef_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ int write_output(void)
return -ENOTHANDLED;

/* create the file */
fd = open(GBL_OPTIONS.output_file, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, 0644);
ON_ERROR(fd, -1, "Can't create file %s", GBL_OPTIONS.output_file);
fd = open(GBL_OPTIONS->output_file, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, 0644);
ON_ERROR(fd, -1, "Can't create file %s", GBL_OPTIONS->output_file);

/* display the message */
fprintf(stdout, " Writing output to \'%s\' ", GBL_OPTIONS.output_file);
fprintf(stdout, " Writing output to \'%s\' ", GBL_OPTIONS->output_file);
fflush(stdout);

/* compute the header */
Expand Down
12 changes: 6 additions & 6 deletions utils/etterfilter/ef_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ void parse_options(int argc, char **argv)
break;

case 'o':
GBL_OPTIONS.output_file = strdup(optarg);
GBL_OPTIONS->output_file = strdup(optarg);
break;

case 'd':
/* use many times to encrease debug level */
GBL_OPTIONS.debug++;
GBL_OPTIONS->debug++;
break;

case 'w':
GBL_OPTIONS.suppress_warnings = 1;
GBL_OPTIONS->suppress_warnings = 1;
break;

case 'h':
Expand All @@ -121,12 +121,12 @@ void parse_options(int argc, char **argv)

/* the source file to be compiled */
if (argv[optind]) {
GBL_OPTIONS.source_file = strdup(argv[optind]);
GBL_OPTIONS->source_file = strdup(argv[optind]);
}

/* make the default name */
if (GBL_OPTIONS.output_file == NULL)
GBL_OPTIONS.output_file = strdup("filter.ef");
if (GBL_OPTIONS->output_file == NULL)
GBL_OPTIONS->output_file = strdup("filter.ef");

return;
}
Expand Down
2 changes: 1 addition & 1 deletion utils/etterfilter/ef_syntax.l
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ SPACES [ \t]+

[\n\r] {
/* increment the line number (used for error reporting) */
GBL.lineno++;
GBL->lineno++;
}

. {
Expand Down
4 changes: 2 additions & 2 deletions utils/etterlog/el_analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void analyze_info(void);

void analyze(void)
{
switch(GBL.hdr.type) {
switch(GBL->hdr.type) {
case LOG_PACKET:
analyze_packet();
break;
Expand Down Expand Up @@ -82,7 +82,7 @@ void analyze_packet(void)
}

/* get the file stat */
ret = stat(GBL.logfile, &st);
ret = stat(GBL->logfile, &st);
ON_ERROR(ret, -1, "Cannot stat file");

fprintf(stdout, "\n\n");
Expand Down
14 changes: 7 additions & 7 deletions utils/etterlog/el_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void conn_table_create(void)
int ret, count = 0;
u_char *buf;

if (GBL.hdr.type == LOG_INFO)
if (GBL->hdr.type == LOG_INFO)
FATAL_ERROR("LOG_INFO files don't contain connections !");


Expand Down Expand Up @@ -134,7 +134,7 @@ static int insert_table(struct log_header_packet *pck, char *buf)
!ip_addr_cmp(&c->L3_dst, &pck->L3_dst)) {

/* add to the stream (if necessary) */
if (GBL.decode)
if (GBL->decode)
stream_add(&c->so, pck, buf);

return 0;
Expand All @@ -148,7 +148,7 @@ static int insert_table(struct log_header_packet *pck, char *buf)
!ip_addr_cmp(&c->L3_dst, &pck->L3_src)) {

/* add to the stream (if necessary) */
if (GBL.decode)
if (GBL->decode)
stream_add(&c->so, pck, buf);

return 0;
Expand All @@ -170,7 +170,7 @@ static int insert_table(struct log_header_packet *pck, char *buf)
stream_init(&c->so);

/* add to the stream (if necessary) */
if (GBL.decode)
if (GBL->decode)
stream_add(&c->so, pck, buf);

SLIST_INSERT_HEAD(&conn_list_head, c, next);
Expand Down Expand Up @@ -265,7 +265,7 @@ int is_conn(struct log_header_packet *pck, int *versus)
pck->L4_src == conn_target.L4_src &&
pck->L4_dst == conn_target.L4_dst &&
/* the packet is from source, but we are interested only in dest */
!GBL.only_dest ) {
!GBL->only_dest ) {
good = 1;
*versus = VERSUS_SOURCE;
}
Expand All @@ -276,13 +276,13 @@ int is_conn(struct log_header_packet *pck, int *versus)
pck->L4_src == conn_target.L4_dst &&
pck->L4_dst == conn_target.L4_src &&
/* the packet is from dest, but we are interested only in source */
!GBL.only_source ) {
!GBL->only_source ) {
good = 1;
*versus = VERSUS_DEST;
}

/* check the reverse option */
if (GBL.reverse ^ (good && proto) )
if (GBL->reverse ^ (good && proto) )
return 1;
else
return 0;
Expand Down
Loading

0 comments on commit 5188dea

Please sign in to comment.