Initial Makefile and first cut at fwknop.h, the spa_random_number function, and a program for testing the functions.

git-svn-id: file:///home/mbr/svn/fwknop/trunk@2 510a4753-2344-4c79-9c09-4d669213fbeb
This commit is contained in:
Damien Stuart 2008-11-29 18:50:33 +00:00
parent 0cdcbddf0d
commit 55dd479c68
5 changed files with 363 additions and 0 deletions

100
Makefile Normal file
View File

@ -0,0 +1,100 @@
# $Id$
############################################################################
#
# File: Makefile
#
# Author: Damien Stuart
#
# Purpose: Makefile for fwknop-c implementation
#
# Copyright (C) 2008 Damien Stuart (dstuart@dstuart.org)
#
# License (GNU Public License):
#
# 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-1307
# USA
#
############################################################################
#
CC = gcc
# Specify pcap library (typically pcap or pcap_ring).
#
PCAP_LIB = -lpcap
# Base CFLAGS
# For Full debugging (for extreme verbose output at runtime), add
# "-DDEBUG to the BASE_CFLAGS arg. This should not be used on a
# production build.
#
BASE_CFLAGS = -Wall -fno-strict-aliasing
# Uncomment one of these CFLAGS based on your needs
#
## Prod Build
#CFLAGS = -O2 $(BASE_CFLAGS)
#
## For debugging symbols if you plan to use a debugger
CFLAGS = -g -O0 $(BASE_CFLAGS)
LDFLAGS =
LIBS = $(PCAP_LIB) -lm -lz
PROG = fko_test
SRCS = fko_test.c \
spa_random_number.c \
OBJS = $(SRCS:.c=.o)
###########################################################################
# Targets
#
all: $(PROG)
show:
@echo MY_ARCH=$(MY_ARCH)
@echo CFLAGS=$(CFLAGS)
$(PROG): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
# Force a normal rebuild.
#
rebuild: clean $(PROG)
strip: $(PROG)
strip $(PROG)
clean:
rm -f $(PROG) $(OBJS)
realclean:
rm -f $(PROG) $(OBJS) core *.bak *.tmp *[-~]
# Generate the dependencies for the sources in this current directory
# while ignoring warnings. Note: If you don't have makedepend in your PATH,
# you will simple get a warning and noting will happen.
#
depend:
@`which makedepend 2>/dev/null` -Y -- $(CFLAGS) -- $(SRCS) 2> /dev/null \
&& echo "makedepend -Y -- $(CFLAGS) -- $(SRCS) 2> /dev/null" \
|| echo " - makedepend not found. Aborting..."
###########################################################################
# Dependencies - (These are automatically generate with "make depend")
#
# DO NOT DELETE
fko_test.o: fwknop.h
spa_random_number.o: fwknop.h

27
README
View File

@ -1,3 +1,30 @@
This is the top-level directory for the C version of fwknop.
libfwnop source files:
======================
spa_random_number.c
Usage: "char* spa_random_number(char* rand_val);"
Where 'rand_val' must be a char array of at least 17 bytes.
Returns a pointer to the provided char array.
spa_user.c
spa_timestamp.c
spa_version.c
spa_message_type.c
spa_message.c
spa_nat_access.c
spa_server_auth.c
spa_client_timeout.c
spa_digest.c
Someday, I'll put something meaningful here... --DSS

46
fko_test.c Normal file
View File

@ -0,0 +1,46 @@
/* $Id$
*****************************************************************************
*
* File: fko_test.c
*
* Author: Damien S. Stuart
*
* Purpose: Temp test program for libfwknop
*
* Copyright (C) 2008 Damien Stuart (dstuart@dstuart.org)
*
* License (GNU Public License):
*
* 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-1307
* USA
*
*****************************************************************************
*/
#include "fwknop.h"
int main(int argc, char** argv)
{
spa_message_t sm;
//char test_str[1024] = {0};
/* Zero our SPA message struct.
*/
memset(&sm, 0x0, sizeof(spa_message_t));
/* Get a random 16-byte string of hex values.
*/
spa_random_number(&sm);
printf("SPA_RAND_VAL: %s\n", sm.rand_val);
return(0);
}
/***EOF***/

118
fwknop.h Normal file
View File

@ -0,0 +1,118 @@
/* $Id$
*****************************************************************************
*
* File: fwknop.h
*
* Author: Damien S. Stuart
*
* Purpose: Header for the fwknop source files
*
* Copyright (C) 2008 Damien Stuart (dstuart@dstuart.org)
*
* License (GNU Public License):
*
* 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-1307
* USA
*
*****************************************************************************
*/
#ifndef _FWKNOP_H_
#define _FWKNOP_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
/* General params
*/
#define FWKNOP_VERSION "1.9.10-pre1" /* The fwknop client version # */
#define VERSION_LENGTH 11 /* Length of the version string */
#define MIN_PORT 10000
#define MAX_PORT 65535
#define ENC_KEYSIZE 16 /* RIJNDAEL Key Size */
/* For random string generation.
*/
#define RAND_VAL_SIZE 16
#define RAND_FILE "/dev/urandom"
#define RAND_MASK 0xFFFF
#define USER_SIZE 32
#define TIMESTAMP_SIZE 10
#define MAX_MESSAGE_SIZE 128
#define MAX_NAT_ACCESS_SIZE 128
#define MAX_SERVER_AUTH_SIZE 128
#define MAX_DIGEST_SIZE 128
/* SPA Message types...
*/
enum {
SPA_COMMAND_MODE,
SPA_ACCESS_MODE,
SPA_NAT_ACCESS_MODE,
SPA_CLIENT_TIMEOUT_ACCESS_MODE,
SPA_CLIENT_TIMEOUT_NAT_ACCESS_MODE,
SPA_LOCAL_NAT_ACCESS_MODE,
SPA_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MODE
};
/* Digest types...
*/
enum {
MD5_DIGEST,
SHA1_DIGEST,
SHA256_DIGEST
};
/* General Defaults
*/
#define DEFAULT_PORT 62201
#define DEFAULT_DIGEST SHA256_DIGEST
#define KNOCK_INTERVAL 60
/* The pieces we need to make a SPA packet.
*/
typedef struct _spa_message {
unsigned short digest_type;
unsigned short enc_pcap_port;
char rand_val[RAND_VAL_SIZE+1];
char user[USER_SIZE+1];
unsigned int timestamp;
char version[VERSION_LENGTH+1];
unsigned short message_type;
char message[MAX_MESSAGE_SIZE+1];
char nat_access[MAX_NAT_ACCESS_SIZE+1];
char server_auth[MAX_SERVER_AUTH_SIZE+1];
unsigned int client_timeout;
char digest[MAX_DIGEST_SIZE+1];
} spa_message_t;
/* Function prototypes
*/
char* spa_random_number(spa_message_t *sm);
char* spa_user(spa_message_t *sm);
char* spa_timestamp(spa_message_t *sm);
char* spa_version(spa_message_t *sm);
char* spa_message_type(spa_message_t *sm);
char* spa_message(spa_message_t *sm);
char* spa_nat_access(spa_message_t *sm);
char* spa_server_auth(spa_message_t *sm);
char* spa_client_timeout(spa_message_t *sm);
char* spa_digest(spa_message_t *sm);
#endif /* _FWKNOP_H_ */
/***EOF***/

72
spa_random_number.c Normal file
View File

@ -0,0 +1,72 @@
/* $Id$
*****************************************************************************
*
* File: spa_random_number.c
*
* Author: Damien S. Stuart
*
* Purpose: Generate a 16-byte random hex value.
*
* Copyright (C) 2008 Damien Stuart (dstuart@dstuart.org)
*
* License (GNU Public License):
*
* 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-1307
* USA
*
*****************************************************************************
*/
#include "fwknop.h"
char* spa_random_number(spa_message_t *sm)
{
FILE *rfd;
struct timeval tv;
unsigned int seed;
/* Attempt to read seed data from /dev/urandom. If that does not
* work, then fall back to a time-based method (less secure, but
* probably more portable).
*/
if((rfd = fopen(RAND_FILE, "r")) != NULL)
{
/* Read seed from /dev/urandom
*/
fread(&seed, 4, 1, rfd);
fclose(rfd);
#ifdef DEBUG
fprintf(stderr, "Using /dev/urandom for seed: %u\n", seed);
#endif
}
else
{
/* Seed based on time (current usecs).
*/
gettimeofday(&tv, NULL);
seed = tv.tv_usec;
#ifdef DEBUG
fprintf(stderr, "Using time and pids for seed: %u\n", seed);
#endif
}
srand(seed);
sprintf(sm->rand_val, "%04x%04x%04x%04x",
rand() % RAND_MASK,
rand() % RAND_MASK,
rand() % RAND_MASK,
rand() % RAND_MASK
);
return(sm->rand_val);
}
/***EOF***/