Refactor build system to use waf. Waf is better at handling dependency paths which I need for IDL support

This commit is contained in:
Wilco Baan Hofman 2012-08-04 22:18:52 +02:00
parent df7f98bc6f
commit 9fd10dec9c
77 changed files with 312 additions and 86 deletions

3
.gitignore vendored
View file

@ -7,3 +7,6 @@ secip.h
ndr_secip.c
ndr_secip.h
tags
bin/.*
.lock-waf*
build/

View file

@ -1,52 +1,14 @@
CC := gcc
# Enable for debug
CFLAGS := -g -ggdb -std=c99 -Wall -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wdeclaration-after-statement -Werror-implicit-function-declaration -Wstrict-prototypes
INCLUDES := -I/usr/include -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/samba-4.0
siahsd_LIB := -ltalloc -ldbi -lglib-2.0
siahsd_OBJ := sia.o status.o database.o config.o siahsd.o
secipd_LIB := -ltalloc -ldbi -lglib-2.0 -lndr
secipd_OBJ := sia.o status.o database.o config.o ndr_secip.o secipd.o
OBJ := $(siahsd_OBJ) $(secipd_OBJ)
binaries := siahsd secipd
all: $(binaries)
all:
bin/waf build
clean:
rm -f $(binaries)
rm -f $(OBJ)
rm -f $(OBJ:.o=.d)
rm -f ndr_*.[ch]
rm -f secip.h
bin/waf clean
distclean: clean
rm -f tags
secipd: $(secipd_OBJ)
@echo Linking $@
@$(CC) $(secipd_OBJ) $(secipd_LIB) -o secipd
siahsd: $(siahsd_OBJ)
@echo Linking $@
@$(CC) $(siahsd_OBJ) $(siahsd_LIB) -o siahsd
distclean:
bin/waf distclean
ctags:
ctags `find -name \*.[ch]`
idl:
pidl/pidl --ndr-parser=ndr_secip.c secip.idl
pidl/pidl --header=secip.h secip.idl
%.o: %.c
@echo Compiling $*.c
@$(CC) -c $(CFLAGS) $(INCLUDES) -o $*.o $<
@$(CC) -MM $(CFLAGS) -MT $*.o $(INCLUDES) -o $*.d $<
-include $(OBJ:.o=.d)

BIN
bin/waf vendored Executable file

Binary file not shown.

View file

@ -20,7 +20,8 @@
/* My global state */
configuration *conf = NULL;
const char *process_name = NULL;
struct rsa_public_key *public_key = NULL;
struct rsa_private_key *private_key = NULL;
configuration *get_conf(void) {
return conf;
@ -35,6 +36,22 @@ STATUS set_process_name(const char *name) {
return ST_OK;
}
STATUS get_rsa_keys(struct rsa_public_key **pub, struct rsa_private_key **priv) {
if (pub == NULL || priv == NULL) {
return ST_NO_SUCH_OBJECT;
}
*pub = public_key;
*priv = private_key;
return ST_OK;
}
STATUS set_rsa_keys(struct rsa_public_key *pub, struct rsa_private_key *priv) {
public_key = pub;
private_key = priv;
return ST_OK;
}
STATUS read_configuration_file(TALLOC_CTX *mem_ctx)
{
GError *error = NULL;
@ -79,11 +96,6 @@ STATUS read_configuration_file(TALLOC_CTX *mem_ctx)
return ST_CONFIGURATION_ERROR;
}
conf->siahs_port = g_key_file_get_integer(keyfile, "siahs", "port", &error);
if (error) {
fprintf(stderr, "No SIA-HS port supplied in the configuration.\n");
return ST_CONFIGURATION_ERROR;
}
conf->log_file = g_key_file_get_string(keyfile, "siahsd", "log file", &error);
if (error) {
fprintf(stderr, "No log file supplied in the configuration.\n");
@ -103,11 +115,10 @@ STATUS read_configuration_file(TALLOC_CTX *mem_ctx)
if (error) {
conf->foreground = false;
}
/* Optional parameters are protocol-specific */
conf->siahs_port = g_key_file_get_integer(keyfile, "siahs", "port", &error);
conf->secip_port = g_key_file_get_integer(keyfile, "secip", "port", &error);
if (error) {
fprintf(stderr, "No SecIP port supplied in the configuration.\n");
return ST_CONFIGURATION_ERROR;
}
conf->rsa_key_file = g_key_file_get_string(keyfile, "secip", "rsa key file", &error);
return ST_OK;
}

View file

@ -30,11 +30,14 @@ typedef struct {
gboolean foreground;
char *pid_file;
gint secip_port;
char *rsa_key_file;
} configuration;
configuration *get_conf(void);
const char *get_process_name(void);
STATUS get_rsa_keys(struct rsa_public_key **pub, struct rsa_private_key **priv);
STATUS set_rsa_keys(struct rsa_public_key *pub, struct rsa_private_key *priv);
const char *get_process_name(void);
STATUS set_process_name(const char *name);
STATUS read_configuration_file(TALLOC_CTX *mem_ctx);

10
configure vendored Executable file
View file

@ -0,0 +1,10 @@
#!/bin/bash
if ! /usr/bin/env python -c "import sys;sys.exit(0)";then
echo "You need to install python for this to work"
echo "You might also want to grab the following debian packages:"
echo "libglib-dev libdbi-dev libtalloc-dev nettle-dev"
exit 1
fi
bin/waf configure

20
crc16.c Normal file
View file

@ -0,0 +1,20 @@
#include <stdint.h>
uint16_t calculate_crc (const uint8_t *ptr, uint16_t count) {
#define CRC16_SEED 0x1021
uint16_t crc;
uint8_t i;
crc = 0;
while (count-- > 0) {
crc = crc ^ ((uint16_t) *ptr++ << 8);
for (i = 0; i < 8; i++) {
if (crc & 0x8000) {
crc = crc << 1 ^ CRC16_SEED;
} else {
crc = crc << 1;
}
}
}
return crc;
}

2
crc16.h Normal file
View file

@ -0,0 +1,2 @@
uint16_t calculate_crc (const uint8_t *ptr, uint16_t count);

View file

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
@ -31,10 +31,12 @@
#include <errno.h>
/* Libs */
#include <talloc.h>
#include <dbi/dbi.h>
#include <glib.h>
#include <nettle/rsa.h>
/* Private */
#include "status.h"

View file

@ -1,5 +1,6 @@
[
helper("../crc16.h")
]
interface secip
{
typedef [public,enum8bit] enum {
@ -40,7 +41,7 @@ interface secip
typedef [public,flag(LIBNDR_FLAG_NOALIGN)] struct {
[value(0)] uint16 session_id;
uint8 rsa_key[128];
uint8 padding[74];
uint8 padding[75];
} secip_ppk_com;
typedef [public,flag(LIBNDR_FLAG_NOALIGN)] struct {
@ -76,13 +77,30 @@ interface secip
typedef [public,flag(LIBNDR_FLAG_NOALIGN)] struct {
uint16 connection_id; /* 0xffff is unassigned */
uint8 pad; /* This is pretty weird actually */
uint8 pad; /* This is pretty weird actually, alphatronics bug?? */
secip_message message_id;
uint16 sequence_number;
char device_id[16];
[switch_is(message_id)] secip_msg_union msg;
uint8 padding[30]; /* random */
uint16 crc;
} secip_packet;
typedef [public,flag(LIBNDR_FLAG_NOALIGN)] struct {
uint16 connection_id; /* 0xffff is unassigned */
secip_message message_id;
uint16 sequence_number;
char device_id[16];
[switch_is(message_id)] secip_msg_union msg;
uint8 padding[30]; /* random */
} secip_out_packet;
typedef [public,flag(LIBNDR_FLAG_NOALIGN)] struct {
uint8 raw_packet[256];
[value(calculate_crc(raw_packet, 256))] uint16 crc;
} secip_setup_packet;
typedef [public,flag(LIBNDR_FLAG_NOALIGN)] struct {
uint8 raw_packet[128];
[value(calculate_crc(raw_packet, 128))] uint16 crc;
} secip_comm_packet;
};

127
secipd.c
View file

@ -16,26 +16,97 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "ndr_secip.h"
#include "build/ndr_secip.h"
uint16_t calculate_crc (char *ptr, uint16_t count)
{
#define CRC16_SEED 0x1021
uint16_t crc;
uint8_t i;
crc = 0;
static int read_rsa_keys(void) {
int res;
FILE *file;
uint8_t buf[1024];
struct rsa_private_key *priv;
struct rsa_public_key *pub;
configuration *conf = get_conf();
uint8_t *buffer = NULL;
size_t n, size=0;
while (count-- > 0) {
crc = crc ^ ((uint16_t) *ptr++ << 8);
for (i = 0; i < 8; i++) {
if (crc & 0x8000) {
crc = crc << 1 ^ CRC16_SEED;
} else {
crc = crc << 1;
priv = talloc(conf, struct rsa_private_key);
pub = talloc(conf, struct rsa_public_key);
rsa_public_key_init (pub);
rsa_private_key_init (priv);
file = fopen(conf->rsa_key_file, "r");
if (file == NULL) {
DEBUG(0, "Can't open configured rsa key file: %s", conf->rsa_key_file);
exit(ST_CONFIGURATION_ERROR);
}
while (1) {
n = fread(&buf, 1, 1024, file);
buffer = talloc_realloc(conf, buffer, uint8_t, size + n);
memcpy(buffer + size, buf, n);
size += n;
if (n < 1024)
break;
}
fclose(file);
res = rsa_keypair_from_sexp(pub, priv, 0, size, buffer);
set_rsa_keys(pub, priv);
return res;
}
STATUS send_ppk_com(TALLOC_CTX *mem_ctx, int sock, struct sockaddr_in from, struct secip_packet *pkt) {
struct secip_setup_packet *setup_pkt;
struct secip_out_packet *ppk_com;
DATA_BLOB raw_pkt, raw_setup_pkt;
enum ndr_err_code ndr_err;
size_t n;
struct rsa_private_key *priv;
struct rsa_public_key *pub;
size_t count;
setup_pkt = talloc_zero(mem_ctx, struct secip_setup_packet);
ppk_com = talloc_zero(setup_pkt, struct secip_out_packet);
ppk_com->connection_id = pkt->connection_id;
ppk_com->message_id = SECIP_MSG_PPK_COM;
ppk_com->sequence_number = 1;
memcpy(ppk_com->device_id, "MyFirstAlarm[TM]", strlen("MyFirstAlarm[TM]"));
ppk_com->msg.ppk_com.session_id = 0;
get_rsa_keys(&pub, &priv);
mpz_export(&ppk_com->msg.ppk_com.rsa_key, &count, 1, 4, 1, 0, pub->n);
DEBUG(0, "RSA Words written: %u", count);
printf("%s\n", ndr_print_struct_string(pkt,(ndr_print_fn_t)ndr_print_secip_out_packet, "ppk_com packet", ppk_com));
ndr_err = ndr_push_struct_blob(&raw_pkt, ppk_com, ppk_com, (ndr_push_flags_fn_t)ndr_push_secip_out_packet);
if (ndr_err != NDR_ERR_SUCCESS) {
DEBUG(0, "Oh holy shitstorm! That didn't work!\n");
return ST_GENERAL_FAILURE;
}
return crc;
memcpy(setup_pkt->raw_packet, raw_pkt.data, raw_pkt.length);
ndr_err = ndr_push_struct_blob(&raw_setup_pkt, setup_pkt, setup_pkt, (ndr_push_flags_fn_t)ndr_push_secip_setup_packet);
if (ndr_err != NDR_ERR_SUCCESS) {
DEBUG(0, "Oh holy shitstorm! That didn't work!\n");
return ST_GENERAL_FAILURE;
}
n = sendto(sock, raw_setup_pkt.data, raw_setup_pkt.length, 0, (struct sockaddr *)&from, sizeof(from));
talloc_free(setup_pkt);
return 0;
}
@ -100,6 +171,7 @@ int main (int argc, char **argv) {
return ST_BIND_FAILURE;
}
read_rsa_keys();
DEBUG(0, "Started %s and waiting for SecIP packets on port %d",
get_process_name(), conf->secip_port);
@ -116,12 +188,14 @@ int main (int argc, char **argv) {
fromlen = sizeof(struct sockaddr_in);
while (1) {
uint16_t src_port;
struct secip_setup_packet *setup_pkt;
struct secip_packet *pkt;
char buf[1024]; /* Purposefully static length */
enum ndr_err_code ndr_err;
DATA_BLOB data;
pkt = talloc(mem_ctx, struct secip_packet);
setup_pkt = talloc(mem_ctx, struct secip_setup_packet);
pkt = talloc(setup_pkt, struct secip_packet);
n = recvfrom(sock, &buf, sizeof(buf), 0, (struct sockaddr *) &from, &fromlen);
if (n < 0) {
@ -133,11 +207,22 @@ int main (int argc, char **argv) {
}
src_port = ntohs(from.sin_port);
/* Copy to data blob */
/* Copy packet to data blob */
data.length = n;
data.data = talloc_memdup(pkt, buf, n);
data.data = talloc_memdup(setup_pkt, buf, n);
/* Parse the header */
ndr_err = ndr_pull_struct_blob_all(&data, setup_pkt, setup_pkt, (ndr_pull_flags_fn_t)ndr_pull_secip_setup_packet);
if (ndr_err != NDR_ERR_SUCCESS) {
DEBUG(0, "Could not parse this CRC packet");
}
printf("%s\n", ndr_print_struct_string(setup_pkt,(ndr_print_fn_t)ndr_print_secip_setup_packet, "setup packet", setup_pkt));
/* Copy packet to data blob */
data.length = data.length - sizeof(uint16_t);
data.data = talloc_memdup(pkt, buf, n);
ndr_err = ndr_pull_struct_blob_all(&data, pkt, pkt, (ndr_pull_flags_fn_t)ndr_pull_secip_packet);
if (ndr_err != NDR_ERR_SUCCESS) {
@ -145,10 +230,12 @@ int main (int argc, char **argv) {
}
printf("%s\n", ndr_print_struct_string(pkt,(ndr_print_fn_t)ndr_print_secip_packet, "packet", pkt));
DEBUG(0, "%x %x %x %x", pkt->connection_id, pkt->message_id, pkt->sequence_number, pkt->crc);
DEBUG(0, "%x %x %x %x", pkt->connection_id, pkt->message_id, pkt->sequence_number);
if (pkt->message_id == SECIP_MSG_ATE_ENC && pkt->msg.ate_enc.session_id == 0x0000) {
send_ppk_com(sock, from, pkt);
send_ppk_com(pkt, sock, from, pkt);
}
DEBUG(3, "Received packet with len %d from %u", n, src_port);
talloc_free(setup_pkt);
}
}

View file

@ -228,19 +228,19 @@ int main(int argc, char **argv) {
uint16_t src_port;
struct siahs_packet *pkt;
uint8_t *decoded;
char buf[1024]; /* Purposefully static length */
uint8_t *buf = talloc_array(conf, uint8_t, 1024);
char *reply_message;
pkt = talloc_zero(mem_ctx, struct siahs_packet);
NO_MEM_RETURN(pkt);
n = recvfrom(sock, &buf, sizeof(buf), 0, (struct sockaddr *) &from, &fromlen);
n = recvfrom(sock, &buf, 1024, 0, (struct sockaddr *) &from, &fromlen);
if (n < 0) {
DEBUG( 0, "Error when storing packet in buffer!");
talloc_free(pkt);
continue;
} else if (n == sizeof(buf)) {
} else if (n == 1024) {
DEBUG(0, "Maximum packet size exceeded!");
talloc_free(pkt);
continue;
@ -248,7 +248,7 @@ int main(int argc, char **argv) {
src_port = ntohs(from.sin_port);
pkt->len = ntohl(*(uint32_t *)buf);
pkt->len = ntohl(*(uint32_t *)&buf[0]);
if (pkt->len > n-4) {
DEBUG(0, "Message length is longer than the packet (malformed packet!)");

108
wscript Normal file
View file

@ -0,0 +1,108 @@
#! /usr/bin/env python
# encoding: utf-8
from waflib.Task import Task
from waflib.TaskGen import extension
class idl_header(Task):
run_str = '../bin/pidl/pidl --header ${TGT[0].abspath()} ${SRC}'
color = 'BLUE'
ext_out = ['.h']
class idl_parser(Task):
run_str = '../bin/pidl/pidl --ndr-parser ${TGT[0].abspath()} ${SRC}'
color = 'BLUE'
ext_out = ['.h']
@extension('.idl')
def process_idl(self, node):
header_node = node.change_ext('.h')
self.create_task('idl_header', node, [header_node ])
c_node = node.change_ext('.c')
if c_node.name[:len('ndr_')] != 'ndr_':
c_node.name = 'ndr_' + c_node.name
self.create_task('idl_parser', node, [ c_node ])
self.source.append(c_node)
def dist(ctx):
ctx.base_name = 'siahsd'
ctx.algo = 'bz2'
ctx.excl = ' **/.waf-1* **/*~ **/*.o **/*.swp **/.lock-w*'
ctx.files = ctx.path.ant_glob('**/wscript')
def configure(conf):
conf.env.CC = 'gcc'
conf.load('gcc')
# Check for glib
conf.check_cfg(package='glib-2.0', uselib_store='glib-2.0',
args=['--cflags', '--libs'])
# Check for talloc
conf.check_cfg(package='talloc', uselib_store='talloc',
args=['--cflags', '--libs'])
# Check for tevent (Needed for pkg-config of ndr)
conf.check_cfg(package='tevent', uselib_store='tevent',
args=['--cflags', '--libs'])
# Check for ndr
conf.check_cfg(package='ndr', uselib_store='ndr',
args=['--cflags', '--libs'])
# Check for headers
conf.check(header_name='stdio.h', features='c cprogram')
conf.check(header_name='stdlib.h', features='c cprogram')
conf.check(header_name='stdint.h', features='c cprogram')
conf.check(header_name='stdbool.h', features='c cprogram')
conf.check(header_name='sys/time.h', features='c cprogram')
conf.check(header_name='sys/types.h', features='c cprogram')
conf.check(header_name='sys/stat.h', features='c cprogram')
conf.check(header_name='netinet/in.h', features='c cprogram')
conf.check(header_name='arpa/inet.h', features='c cprogram')
conf.check(header_name='unistd.h', features='c cprogram')
conf.check(header_name='string.h', features='c cprogram')
conf.check(header_name='fcntl.h', features='c cprogram')
conf.check(header_name='errno.h', features='c cprogram')
# Used libraries
conf.check(header_name='talloc.h', use='talloc', features='c cprogram')
conf.check(header_name='glib.h', use='glib-2.0', features='c cprogram')
conf.check(header_name='glibconfig.h', use='glib-2.0', features='c cprogram')
conf.check(header_name='dbi/dbi.h', features='c cprogram')
conf.check_cc(lib='dbi', uselib_store='dbi')
conf.check_cc(lib='talloc', uselib_store='talloc')
conf.check_cc(lib='ndr', uselib_store='ndr')
conf.check_cc(lib='hogweed', uselib_store='nettle')
conf.check_cc(lib='nettle', uselib_store='nettle')
conf.check_cc(lib='gmp', uselib_store='nettle')
# Purposefully at the bottom because waf configuration tests fail with -Wstrict-prototypes and -Werror
conf.env.CFLAGS = ['-O2', '-g', '-ggdb', '-std=c99', '-Wall', '-Wshadow', '-Wpointer-arith', '-Wcast-align', '-Wwrite-strings', '-Wdeclaration-after-statement',
'-Werror-implicit-function-declaration', '-Wstrict-prototypes', '-Werror']
def build(bld):
bld.stlib(source="database.c", target="database", use='glib-2.0')
bld.stlib(source="status.c", target="status", use='glib-2.0')
bld.stlib(source="config.c", target="config", use='glib-2.0')
bld.stlib(source="sia.c", target="sia", use='glib-2.0')
bld.program(
source = 'config.c status.c sia.c siahsd.c',
target = 'siahsd',
use = [ 'database', 'config', 'status', 'sia', 'dbi', 'talloc','glib-2.0' ])
bld.program(
source = 'secip.idl config.c status.c sia.c secipd.c crc16.c',
target = 'secipd',
use = [ 'database', 'config', 'status', 'sia', 'dbi', 'talloc','glib-2.0', 'nettle', 'ndr' ])
pass
def clean(ctx):
pass