Permalink
Browse files

libhpfeeds initial commit

  • Loading branch information...
tillmannw committed Nov 8, 2011
1 parent 9ff33f3 commit fba1076fec3b57c772360fd4817baa958018ae87
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,7 @@
+# $Id$
+
+AUTOMAKE_OPTIONS = foreign
+
+EXTRA_DIST = configure.ac CHANGES
+
+SUBDIRS = src include tools
View
@@ -0,0 +1,44 @@
+# -*- mode: m4; -*-
+# -*- Autoconf -*-
+# Process this file with autoconf to produce a configure script.
+# $Id$
+
+AC_PREREQ(2.59)
+AC_INIT([libhpfeeds], [0.1.0], [tillmann.werner@gmx.de])
+AM_INIT_AUTOMAKE([libhpfeeds], [0.1.0])
+AC_REVISION([$Id$])
+
+AC_PREFIX_DEFAULT(/)
+AC_CONFIG_SRCDIR([include/hpfeeds.h])
+AM_CONFIG_HEADER([config.h])
+# AM_MAINTAINER_MODE
+
+AC_CANONICAL_HOST
+AC_C_CONST
+AC_C_BIGENDIAN
+
+# Checks for programs.
+AC_PROG_CC
+AC_PROG_MAKE_SET
+AC_PROG_LIBTOOL
+
+AC_CHECK_HEADERS([arpa/inet.h sys/types.h])
+
+AC_TYPE_SIZE_T
+
+# library soname
+# check http://sources.redhat.com/autobook/autobook/autobook_91.html#SEC91
+libhpfeeds=0
+libhpfeeds_current=1
+libhpfeeds_revision=0
+libhpfeeds_age=0
+libhpfeeds_soname=$libhpfeeds_current:$libhpfeeds_revision:$libhpfeeds_age
+AC_SUBST(libhpfeeds_soname)
+
+AC_CONFIG_FILES([Makefile
+ src/Makefile
+ include/Makefile
+ tools/Makefile
+ ])
+
+AC_OUTPUT
@@ -0,0 +1,7 @@
+# $Id$
+
+AUTOMAKE_OPTIONS = foreign
+
+includedir = $(prefix)/include
+
+include_HEADERS = hpfeeds.h
@@ -0,0 +1,58 @@
+/*
+ hpfeeds.h
+ Copyright (C) 2011 The Honeynet Project
+ Copyright (C) 2011 Tillmann Werner, tillmann.werner@gmx.de
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __hpfeeds_h
+#define __hpfeeds_h
+
+#include <sys/types.h>
+
+#define OP_ERROR 0
+#define OP_INFO 1
+#define OP_AUTH 2
+#define OP_PUBLISH 3
+#define OP_SUBSCRIBE 4
+
+typedef struct {
+ struct {
+ u_int32_t msglen;
+ u_int8_t opcode;
+ } __attribute__((__packed__)) hdr;
+ u_char data[];
+} hpf_msg_t;
+
+typedef struct {
+ u_int32_t len;
+ u_char data[];
+} hpf_chunk_t;
+
+
+void hpf_msg_delete(hpf_msg_t *m);
+
+hpf_msg_t *hpf_msg_getmsg(u_char *data);
+u_int32_t hpf_msg_getsize(hpf_msg_t *m);
+u_int32_t hpf_msg_gettype(hpf_msg_t *m);
+
+hpf_chunk_t *hpf_msg_get_chunk(u_char *data, size_t len);
+
+hpf_msg_t *hpf_msg_error(u_char *err, size_t err_size);
+hpf_msg_t *hpf_msg_info(u_int32_t nonce, u_char *fbname, size_t fbname_len);
+hpf_msg_t *hpf_msg_auth(u_int32_t nonce, u_char *ident, size_t ident_len, u_char *secret, size_t secret_len);
+hpf_msg_t *hpf_msg_publish(u_char *ident, size_t ident_len, u_char *channel, size_t channel_len, u_char *data, size_t data_len);
+hpf_msg_t *hpf_msg_subscribe(u_char *ident, size_t ident_len, u_char *channel, size_t channel_len);
+
+#endif
@@ -0,0 +1,9 @@
+AUTOMAKE_OPTIONS = foreign
+
+AM_CFLAGS = -I../include -Werror -Wall -g
+
+lib_LTLIBRARIES = libhpfeeds.la
+
+libhpfeeds_la_SOURCES = hpfeeds.c sha1.c
+
+libhpfeeds_la_LDFLAGS = -no-undefined -version-info @libhpfeeds_soname@ -export-symbols-regex "^hpf_"
View
@@ -0,0 +1,196 @@
+/*
+ hpfeeds.c
+ Copyright (C) 2011 The Honeynet Project
+ Copyright (C) 2011 Tillmann Werner, tillmann.werner@gmx.de
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <arpa/inet.h>
+#include <hpfeeds.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "sha1.h"
+
+
+hpf_msg_t *hpf_msg_new(void) {
+ hpf_msg_t *msg;
+
+ msg = calloc(1, sizeof(hpf_msg_t));
+ msg->hdr.msglen = htonl(sizeof(msg->hdr));
+
+ return msg;
+}
+
+void hpf_msg_delete(hpf_msg_t *m) {
+ if (m) free(m);
+
+ return;
+}
+
+hpf_msg_t *hpf_msg_getmsg(u_char *data) {
+ return (hpf_msg_t *) data;
+}
+
+u_int32_t hpf_msg_getsize(hpf_msg_t *m) {
+ return ntohl(m->hdr.msglen);
+}
+
+u_int32_t hpf_msg_gettype(hpf_msg_t *m) {
+ return m->hdr.opcode;
+}
+
+hpf_msg_t *hpf_msg_add_chunk(hpf_msg_t **m, const u_char *data, size_t len) {
+ hpf_msg_t *msg = *m;
+ u_char l;
+
+ if (!m || !data || !len)
+ return NULL;
+
+ l = len < 0xff ? len : 0xff;
+
+ *m = msg = realloc(msg, ntohl(msg->hdr.msglen) + l + 1);
+
+ if (msg == NULL)
+ return NULL;
+
+ ((u_char *) msg)[ntohl(msg->hdr.msglen)] = l;
+ memcpy(((u_char *) msg) + ntohl(msg->hdr.msglen) + 1, data, l);
+
+ msg->hdr.msglen += htonl(1 + l);
+
+ return msg;
+}
+
+hpf_chunk_t *hpf_msg_get_chunk(u_char *data, size_t len) {
+ hpf_chunk_t *c;
+
+ if (!data || !len) return NULL;
+
+ c = (hpf_chunk_t *) data;
+
+ // incomplete chunk?
+ if (c->len > len + 1) return NULL;
+
+ return c;
+}
+
+hpf_msg_t *hpf_msg_add_payload(hpf_msg_t **m, const u_char *data, size_t len) {
+ hpf_msg_t *msg = *m;
+
+ if (!m || !data || !len)
+ return NULL;
+
+ *m = msg = realloc(msg, ntohl(msg->hdr.msglen) + len);
+
+ if (msg == NULL)
+ return NULL;
+
+ memcpy(((u_char *) msg) + ntohl(msg->hdr.msglen), data, len);
+
+ msg->hdr.msglen += htonl(len);
+
+ return msg;
+}
+
+hpf_msg_t *hpf_msg_error(u_char *err, size_t err_size) {
+ hpf_msg_t *msg;
+
+ msg = hpf_msg_new();
+
+ if (msg == NULL)
+ return NULL;
+
+ msg->hdr.opcode = OP_ERROR;
+
+ hpf_msg_add_payload(&msg, err, err_size);
+
+ return msg;
+}
+
+hpf_msg_t *hpf_msg_info(u_int32_t nonce, u_char *fbname, size_t fbname_len) {
+ hpf_msg_t *msg;
+
+ msg = hpf_msg_new();
+
+ if (msg == NULL)
+ return NULL;
+
+ msg->hdr.opcode = OP_INFO;
+
+ hpf_msg_add_chunk(&msg, fbname, fbname_len);
+
+ hpf_msg_add_payload(&msg, (u_char *) &nonce, sizeof(u_int32_t));
+
+ return msg;
+}
+
+hpf_msg_t *hpf_msg_auth(u_int32_t nonce, u_char *ident, size_t ident_len, u_char *secret, size_t secret_len) {
+ hpf_msg_t *msg;
+ SHA1Context ctx;
+ u_char hash[SHA1HashSize];
+
+ msg = hpf_msg_new();
+
+ if (msg == NULL)
+ return NULL;
+
+ msg->hdr.opcode = OP_AUTH;
+
+ SHA1Reset(&ctx);
+ SHA1Input(&ctx, (u_int8_t *) &nonce, sizeof(nonce));
+ SHA1Input(&ctx, (u_int8_t *) secret, secret_len);
+ SHA1Result(&ctx, hash);
+
+ hpf_msg_add_chunk(&msg, ident, ident_len);
+
+ hpf_msg_add_payload(&msg, hash, SHA1HashSize);
+
+ return msg;
+}
+
+hpf_msg_t *hpf_msg_publish(u_char *ident, size_t ident_len, u_char *channel, size_t channel_len, u_char *data, size_t data_len) {
+ hpf_msg_t *msg;
+
+ msg = hpf_msg_new();
+
+ if (msg == NULL)
+ return NULL;
+
+ msg->hdr.opcode = OP_PUBLISH;
+
+ hpf_msg_add_chunk(&msg, ident, ident_len);
+ hpf_msg_add_chunk(&msg, channel, channel_len);
+
+ hpf_msg_add_payload(&msg, data, data_len);
+
+ return msg;
+}
+
+hpf_msg_t *hpf_msg_subscribe(u_char *ident, size_t ident_len, u_char *channel, size_t channel_len) {
+ hpf_msg_t *msg;
+
+ msg = hpf_msg_new();
+
+ if (msg == NULL)
+ return NULL;
+
+ msg->hdr.opcode = OP_SUBSCRIBE;
+
+ hpf_msg_add_chunk(&msg, ident, ident_len);
+
+ hpf_msg_add_payload(&msg, channel, channel_len);
+
+ return msg;
+}
Oops, something went wrong.

0 comments on commit fba1076

Please sign in to comment.