Skip to content

Commit 3182e42

Browse files
author
enderunix
committed
Hafiye
0 parents  commit 3182e42

26 files changed

+1208
-0
lines changed

AUTHORS

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
EnderUNIX Hafiye 1.0 AUTHORS
3+
4+
5+
6+

Cfg.c

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <ctype.h>
5+
6+
#include "Data.h"
7+
#include "Cfg.h"
8+
#include "Defs.h"
9+
10+
extern int errno;
11+
extern int debug;
12+
13+
14+
void loadConfig(struct Prot *p, char *file)
15+
{
16+
FILE *fp;
17+
char buf[BUFSIZE];
18+
char keyword[KEYSIZE];
19+
char value[VALSIZE];
20+
char *cp1, *cp2;
21+
field *f;
22+
char *variables[] = { "Invalid",
23+
"Protocol_Id",
24+
"Protocol_Ident",
25+
"Protocol_Length",
26+
"Field"
27+
};
28+
int i, key, line, nkeys;
29+
30+
nkeys = sizeof(variables) / sizeof(char *);
31+
32+
if ((fp = fopen(file, "r")) == NULL) {
33+
fprintf(stderr, "Cannot open knowledge-base file %s: %s\n", file, strerror(errno));
34+
exit(1);
35+
}
36+
line = 0;
37+
38+
while ((fgets(buf, BUFSIZE, fp)) != NULL) {
39+
line++;
40+
if(buf[0] == '#')
41+
continue;
42+
cp1 = buf;
43+
cp2 = keyword;
44+
while(isspace((int)*cp1))
45+
cp1++;
46+
while(isgraph((int)*cp1) && (*cp1 != '='))
47+
*cp2++ = *cp1++;
48+
*cp2 = '\0';
49+
cp2 = value;
50+
51+
while((*cp1 != '\0') && (*cp1 != '\n')
52+
&& (*cp1 != '='))
53+
cp1++;
54+
cp1++;
55+
56+
while(isspace((int)*cp1))
57+
cp1++;
58+
59+
if (*cp1 == '"')
60+
cp1++;
61+
62+
while(*cp1 != '\0' && (*cp1 != '\n')
63+
&& (*cp1 != '#'))
64+
*cp2++ = *cp1++;
65+
*cp2-- = '\0';
66+
67+
if (keyword[0] == '\0' || value[0] == '\0')
68+
continue;
69+
70+
key = 0;
71+
72+
for (i = 0; i < nkeys; i++) {
73+
if (strcmp(keyword, variables[i]) == 0) {
74+
key = i;
75+
break;
76+
}
77+
}
78+
79+
switch(key) {
80+
case 0:
81+
printf("Invalid keyword \"%s\" in knowledge-base file %s, line %d\n",
82+
keyword, file, line);
83+
exit(-1);
84+
break;
85+
case 1:
86+
p->id = atoi(value);
87+
break;
88+
case 2:
89+
if (strlen(value) < BUFSIZE) /* strncpy weirdness! */
90+
strcpy(p->ident, value);
91+
break;
92+
case 3:
93+
p->len = atoi(value);
94+
break;
95+
case 4:
96+
f = setField(value);
97+
addField(p, f);
98+
break;
99+
}
100+
}
101+
if (strlen(p->ident) <= 0) {
102+
fprintf(stderr, "Protocol Identification not found in file %s\n", file);
103+
fprintf(stderr, "Either correct the file to have the proper form or if you think\n");
104+
fprintf(stderr, "this file is not a knowledge-base file, remove it!\n");
105+
fprintf(stderr, "\nExiting due to an irrecoverable failure...\n");
106+
exit(1);
107+
}
108+
if (debug)
109+
listFields(p);
110+
printf("Finished examining knowledge-base file %s Protocol Id: %d, Protocol Definition: %s\n", file, p->id, p->ident);
111+
fclose(fp);
112+
}

Cfg.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef CFGFILE_H
2+
#define CFGFILE_H
3+
4+
#include "Defs.h"
5+
#include "Data.h"
6+
7+
void loadConfig(prot *, char *);
8+
9+
#endif

ChangeLog

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
EnderUNIX Hafiye 1.0 Changelog
2+
------------------------------
3+
* Tue Aug 25 09:30:00 EEST 2004
4+
fixed a terminal escape sequence injection bug reported by
5+
Serkan Akpolat.
6+
7+
* Wed Jun 12 20:57:59 EEST 2002
8+
First public release

Data.c

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
7+
#include "Data.h"
8+
#include "Defs.h"
9+
10+
extern int errno;
11+
extern int debug;
12+
13+
14+
field * setField(char *v)
15+
{
16+
field *f;
17+
char *tok;
18+
19+
if ((f = (field *)malloc(sizeof(struct Field))) == NULL) {
20+
fprintf(stderr, "setField: memory allocation error: %s\n", strerror(errno));
21+
exit(-1);
22+
}
23+
24+
/* Field Identifier */
25+
if ((tok = strtok(v, "-")) != NULL)
26+
strcpy(f->ident, tok);
27+
else
28+
fprintf(stderr, "Warning: cannot get field-ident from %s\n", v);
29+
30+
/* Field Length */
31+
if ((tok = strtok(NULL, "-")) != NULL)
32+
f->len = (u_short)atoi(tok);
33+
else
34+
fprintf(stderr, "Warning: cannot get field-length from %s\n", v);
35+
36+
/* Field Start bit */
37+
if ((tok = strtok(NULL, "-")) != NULL)
38+
f->start = (u_short)atoi(tok);
39+
else
40+
fprintf(stderr, "Warning: cannot get field start bit from %s\n", v);
41+
42+
/* is next protocol identifier? */
43+
if ((tok = strtok(NULL, "-")) != NULL)
44+
f->protident = (u_short)atoi(tok);
45+
else
46+
fprintf(stderr, "Warning: cannot get field protident from %s\n", v);
47+
48+
/* will inet_ntoa() be applied? */
49+
if ((tok = strtok(NULL, "-")) != NULL)
50+
f->inet_ntoa = (u_short)atoi(tok);
51+
else
52+
fprintf(stderr, "Warning: cannot get field inet_ntoa from %s\n", v);
53+
54+
f->next = NULL;
55+
56+
return f;
57+
}
58+
59+
60+
void addField(prot *p, field *f)
61+
{
62+
field *ptr;
63+
64+
if (p->fields == NULL) {
65+
p->fields = f;
66+
return;
67+
}
68+
69+
for (ptr = p->fields; ptr->next != NULL; ptr = ptr->next)
70+
;
71+
72+
ptr->next = f;
73+
}
74+
75+
76+
void listFields(prot *p)
77+
{
78+
field *ptr;
79+
80+
for (ptr = p->fields; ptr->next != NULL; ptr = ptr->next)
81+
printf("%s, %d bits, starts at bit %d, protident %d, inet_ntoa %d, ntohs %d\n",
82+
ptr->ident,
83+
ptr->len,
84+
ptr->start,
85+
ptr->protident,
86+
ptr->inet_ntoa,
87+
ptr->ntohs);
88+
}

Data.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
#ifndef DATA_H
3+
#define DATA_H
4+
5+
6+
#include "Defs.h"
7+
8+
#include <sys/types.h>
9+
10+
typedef struct Field {
11+
char ident[BUFSIZE];
12+
u_short len;
13+
u_short start;
14+
u_char protident;
15+
u_char inet_ntoa;
16+
u_char ntohs;
17+
struct Field *next;
18+
} field;
19+
20+
21+
typedef struct Prot {
22+
int id;
23+
char ident[BUFSIZE];
24+
u_short len;
25+
struct Field *fields;
26+
} prot;
27+
28+
29+
struct Prot *ltwo[MAXLAYER]; /* Layer II Protocols */
30+
struct Prot *lthree[MAXLAYER]; /* Layer III Protocosl */
31+
struct Prot *lfour[MAXLAYER]; /* Layer IV Protocols */
32+
33+
34+
void addField(prot *, field *);
35+
void listFields(prot *);
36+
field * setField(char *);
37+
38+
#endif

Defs.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef DEFS_H
2+
#define DEFS_H
3+
4+
5+
static const char VERSION[] = "EnderUNIX Hafiye Version 1.0";
6+
7+
8+
enum {
9+
DEVSIZ = 16,
10+
SNAPLEN = 1514,
11+
PROMISC = 1,
12+
READTIMEOUT = 500,
13+
PACKET_COUNT = -1, /* Infinite loop */
14+
LAYERONE_LEN = 14,
15+
BUFSIZE = 1024,
16+
HBUFSIZE = 512,
17+
KEYSIZE = 64,
18+
VALSIZE = 256,
19+
MAXLAYER = 5000
20+
};
21+
22+
23+
#endif

INSTALL

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
EnderUNIX Hafiye 1.0 INSTALL
2+
----------------------------
3+
4+
Installation is straightforward, it should compile on Posix
5+
compliant systems without any trouble.
6+
7+
However, hafiye uses libpcap interface to capture packets.
8+
You have to have it istalled before hafiye. You can download
9+
latest pcap from http://www.tcpdump.org/release/libpcap-0.7.1.tar.gz
10+
11+
Load Makefile with you favorite editor and change PCAPINC and PCAPLIB
12+
variables to point to the pcap includes and pcap library respectively.
13+
14+
Then,
15+
Just type:
16+
17+
# make
18+
19+
and you'll get a working binary named hafiye, then type
20+
21+
# make install
22+
23+
and you'll get hafiye installed in /usr/local/bin/. Configuration
24+
files will be installed as /usr/local/share/hafiye/...
25+

0 commit comments

Comments
 (0)