Add rfc5444 example to code
authorHenning Rogge <henning.rogge@fkie.fraunhofer.de>
Fri, 12 Jul 2013 06:36:44 +0000 (08:36 +0200)
committerHenning Rogge <henning.rogge@fkie.fraunhofer.de>
Fri, 12 Jul 2013 06:36:44 +0000 (08:36 +0200)
CMakeLists.txt
examples/CMakeLists.txt [new file with mode: 0644]
examples/rfc5444_reader_writer/CMakeLists.txt [new file with mode: 0644]
examples/rfc5444_reader_writer/main.c [new file with mode: 0644]
examples/rfc5444_reader_writer/reader.c [new file with mode: 0644]
examples/rfc5444_reader_writer/reader.h [new file with mode: 0644]
examples/rfc5444_reader_writer/writer.c [new file with mode: 0644]
examples/rfc5444_reader_writer/writer.h [new file with mode: 0644]
files/default_licence.txt
tests/CMakeLists.txt

index d44b707..1c76382 100644 (file)
@@ -44,7 +44,6 @@ include_directories(${PROJECT_BINARY_DIR})
 include_directories(external)
 include_directories(src-api)
 include_directories(src-plugins)
-include_directories(tests)
 
 if (NOT OONF_NO_TESTING)
     ENABLE_TESTING()
@@ -54,6 +53,7 @@ endif (NOT OONF_NO_TESTING)
 add_subdirectory(src-api)
 add_subdirectory(src-plugins)
 add_subdirectory(external)
+add_subdirectory(examples)
 
 get_property (targets GLOBAL PROPERTY OONF_TARGETS)
 export (TARGETS ${targets} FILE "${PROJECT_BINARY_DIR}/OONFLibraryDepends_api.cmake")
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
new file mode 100644 (file)
index 0000000..60780ed
--- /dev/null
@@ -0,0 +1,4 @@
+# add example directories
+include_directories(.)
+
+add_subdirectory(rfc5444_reader_writer)
diff --git a/examples/rfc5444_reader_writer/CMakeLists.txt b/examples/rfc5444_reader_writer/CMakeLists.txt
new file mode 100644 (file)
index 0000000..0c16cbf
--- /dev/null
@@ -0,0 +1,16 @@
+# create executable
+ADD_EXECUTABLE(example_rfc5444_reader_writer reader.c writer.c main.c)
+
+TARGET_LINK_LIBRARIES(example_rfc5444_reader_writer oonf_rfc5444)
+TARGET_LINK_LIBRARIES(example_rfc5444_reader_writer oonf_common)
+
+# link regex for windows and android
+IF (WIN32 OR ANDROID)
+    TARGET_LINK_LIBRARIES(example_rfc5444_reader_writer oonf_regex)
+ENDIF(WIN32 OR ANDROID)
+
+# link extra win32 libs
+IF(WIN32)
+    SET_TARGET_PROPERTIES(example_rfc5444_reader_writer PROPERTIES ENABLE_EXPORTS true)
+    TARGET_LINK_LIBRARIES(example_rfc5444_reader_writer ws2_32 iphlpapi)
+ENDIF(WIN32)
diff --git a/examples/rfc5444_reader_writer/main.c b/examples/rfc5444_reader_writer/main.c
new file mode 100644 (file)
index 0000000..99ae066
--- /dev/null
@@ -0,0 +1,57 @@
+#include <string.h>\r
+#include <stdio.h>\r
+\r
+#include "common/common_types.h"\r
+#include "common/netaddr.h"\r
+\r
+#include "rfc5444/rfc5444_reader.h"\r
+#include "rfc5444/rfc5444_writer.h"\r
+#include "rfc5444/rfc5444_print.h"\r
+\r
+#include "rfc5444_reader_writer/reader.h"\r
+#include "rfc5444_reader_writer/writer.h"\r
+\r
+static struct autobuf _hexbuf;\r
+\r
+/**\r
+ * Handle the output of the RFC5444 packet creation process\r
+ * @param wr\r
+ * @param iface\r
+ * @param buffer\r
+ * @param length\r
+ */\r
+static void\r
+write_packet(struct rfc5444_writer *wr __attribute__ ((unused)),\r
+    struct rfc5444_writer_target *iface __attribute__((unused)),\r
+    void *buffer, size_t length) {\r
+  printf("%s()\n", __func__);\r
+\r
+  /* generate hexdump of packet */\r
+  rfc5444_print_hexdump(&_hexbuf, "\t", buffer, length);\r
+\r
+  /* print hexdump to console */\r
+  printf("%s", abuf_getptr(&_hexbuf));\r
+\r
+  /* parse packet */\r
+  rfc5444_reader_handle_packet(&reader, buffer, length);\r
+}\r
+\r
+int main(int argc __attribute__ ((unused)), char **argv __attribute__ ((unused))) {\r
+  /* initialize buffer for hexdump */\r
+  abuf_init(&_hexbuf);\r
+\r
+  /* init reader and writer */\r
+  reader_init();\r
+  writer_init(write_packet);\r
+  \r
+  /* send message */\r
+  rfc5444_writer_create_message_alltarget(&writer, 1);\r
+  rfc5444_writer_flush(&writer, &interface_1, false);\r
+\r
+  /* cleanup */\r
+  reader_cleanup();\r
+  writer_cleanup();\r
+  abuf_free(&_hexbuf);\r
+\r
+  return 0;\r
+}\r
diff --git a/examples/rfc5444_reader_writer/reader.c b/examples/rfc5444_reader_writer/reader.c
new file mode 100644 (file)
index 0000000..2fcbcc5
--- /dev/null
@@ -0,0 +1,134 @@
+\r
+/*\r
+ * The olsr.org Optimized Link-State Routing daemon(olsrd)\r
+ * Copyright (c) 2004-2013, the olsr.org team - see HISTORY file\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ *\r
+ * * Redistributions of source code must retain the above copyright\r
+ *   notice, this list of conditions and the following disclaimer.\r
+ * * Redistributions in binary form must reproduce the above copyright\r
+ *   notice, this list of conditions and the following disclaimer in\r
+ *   the documentation and/or other materials provided with the\r
+ *   distribution.\r
+ * * Neither the name of olsr.org, olsrd nor the names of its\r
+ *   contributors may be used to endorse or promote products derived\r
+ *   from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE\r
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
+ * POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ * Visit http://www.olsr.org for more information.\r
+ *\r
+ * If you find this software useful feel free to make a donation\r
+ * to the project. For more information see the website or contact\r
+ * the copyright holders.\r
+ *\r
+ */\r
+\r
+#include <string.h>\r
+#include <stdio.h>\r
+\r
+#include "common/common_types.h"\r
+#include "common/netaddr.h"\r
+#include "rfc5444/rfc5444_reader.h"\r
+\r
+#include "rfc5444_reader_writer/reader.h"\r
+\r
+static enum rfc5444_result _cb_blocktlv_packet_okay(\r
+    struct rfc5444_reader_tlvblock_context *cont);\r
+\r
+/*\r
+ * message consumer entries definition\r
+ * TLV type 0\r
+ * TLV type 1 (mandatory)\r
+ */\r
+static struct rfc5444_reader_tlvblock_consumer_entry _consumer_entries[] = {\r
+  { .type = 0 },\r
+  { .type = 1, .mandatory = true }\r
+};\r
+\r
+static struct rfc5444_reader_tlvblock_consumer _consumer = {\r
+  /* parse message type 1 */\r
+  .msg_id = 1,\r
+\r
+  /* use a block callback */\r
+  .block_callback = _cb_blocktlv_packet_okay,\r
+};\r
+\r
+struct rfc5444_reader reader;\r
+\r
+/**\r
+ * This block callback is only called if message tlv type 1 is present,\r
+ * because it was declared as mandatory\r
+ *\r
+ * @param cont\r
+ * @return\r
+ */\r
+static enum rfc5444_result\r
+_cb_blocktlv_packet_okay(struct rfc5444_reader_tlvblock_context *cont) {\r
+  int value;\r
+\r
+  printf("%s()\n", __func__);\r
+\r
+  printf("\tmessage type: %d\n", cont->type);\r
+\r
+  if (cont->has_origaddr) {\r
+    struct netaddr_str dst;\r
+    printf("\torig_addr: %s\n", netaddr_to_string(&dst, &cont->orig_addr));\r
+  }\r
+\r
+  if (cont->has_seqno) {\r
+    printf("\tseqno: %d\n", cont->seqno);\r
+  }\r
+\r
+  /* tlv type 0 was not defined mandatory in block callback entries */\r
+  if (_consumer_entries[0].tlv) {\r
+    memcpy(&value, _consumer_entries[0].tlv->single_value, sizeof(value));\r
+    printf("\ttlv 0: %d\n", ntohl(value));\r
+  }\r
+\r
+  /* tlv type 1 was defined mandatory in block callback entries */\r
+  memcpy(&value, _consumer_entries[1].tlv->single_value, sizeof(value));\r
+  printf("\ttlv 1: %d\n", ntohl(value));\r
+\r
+  return RFC5444_OKAY;\r
+}\r
+\r
+/**\r
+ * Initialize RFC5444 reader\r
+ */\r
+void\r
+reader_init(void) {\r
+  printf("%s()\n", __func__);\r
+\r
+  /* initialize reader */\r
+  rfc5444_reader_init(&reader);\r
+\r
+  /* register message consumer */\r
+  rfc5444_reader_add_message_consumer(&reader, &_consumer,\r
+      _consumer_entries, ARRAYSIZE(_consumer_entries));\r
+}\r
+\r
+/**\r
+ * Cleanup RFC5444 reader\r
+ */\r
+void\r
+reader_cleanup(void) {\r
+  printf("%s()\n", __func__);\r
+\r
+  rfc5444_reader_cleanup(&reader);\r
+}\r
diff --git a/examples/rfc5444_reader_writer/reader.h b/examples/rfc5444_reader_writer/reader.h
new file mode 100644 (file)
index 0000000..0cd4d7f
--- /dev/null
@@ -0,0 +1,53 @@
+
+/*
+ * The olsr.org Optimized Link-State Routing daemon(olsrd)
+ * Copyright (c) 2004-2013, the olsr.org team - see HISTORY file
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in
+ *   the documentation and/or other materials provided with the
+ *   distribution.
+ * * Neither the name of olsr.org, olsrd nor the names of its
+ *   contributors may be used to endorse or promote products derived
+ *   from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Visit http://www.olsr.org for more information.
+ *
+ * If you find this software useful feel free to make a donation
+ * to the project. For more information see the website or contact
+ * the copyright holders.
+ *
+ */
+
+#ifndef READER_H_
+#define READER_H_
+
+#include "common/common_types.h"
+#include "rfc5444/rfc5444_reader.h"
+
+EXPORT extern struct rfc5444_reader reader;
+
+void reader_init(void);
+void reader_cleanup(void);
+
+#endif /* READER_H_ */
diff --git a/examples/rfc5444_reader_writer/writer.c b/examples/rfc5444_reader_writer/writer.c
new file mode 100644 (file)
index 0000000..28002a8
--- /dev/null
@@ -0,0 +1,99 @@
+#include <string.h>\r
+#include <stdio.h>\r
+\r
+#include "common/common_types.h"\r
+#include "common/netaddr.h"\r
+#include "rfc5444/rfc5444_writer.h"\r
+\r
+#include "rfc5444_reader_writer/writer.h"\r
+\r
+static void _cb_addMessageTLVs(struct rfc5444_writer *wr);\r
+\r
+static uint8_t _msg_buffer[128];\r
+static uint8_t _msg_addrtlvs[1000];\r
+static uint8_t _packet_buffer[128];\r
+\r
+static struct rfc5444_writer_message *_msg;\r
+\r
+struct rfc5444_writer_target interface_1 = {\r
+       .packet_buffer = _packet_buffer,\r
+       .packet_size = sizeof(_packet_buffer),\r
+};\r
+\r
+struct rfc5444_writer writer = {\r
+       .msg_buffer = _msg_buffer,\r
+       .msg_size = sizeof(_msg_buffer),\r
+       .addrtlv_buffer = _msg_addrtlvs,\r
+       .addrtlv_size = sizeof(_msg_addrtlvs),\r
+};\r
+\r
+static struct rfc5444_writer_content_provider _message_content_provider = {\r
+       .msg_type = 1,\r
+       .addMessageTLVs = _cb_addMessageTLVs,\r
+};\r
+\r
+/**\r
+ * Callback to add message TLVs to a RFC5444 message\r
+ * @param wr\r
+ */\r
+static void\r
+_cb_addMessageTLVs(struct rfc5444_writer *wr) {\r
+       int foo;\r
+  printf("%s()\n", __func__);\r
+\r
+       /* add message tlv type 0 (ext 0) with 4-byte value 23 */\r
+       foo = htonl(23);\r
+       rfc5444_writer_add_messagetlv(wr, 0, 0, &foo, sizeof (foo));\r
+\r
+  /* add message tlv type 1 (ext 0) with 4-byte value 42 */\r
+       foo = htonl(42);\r
+       rfc5444_writer_add_messagetlv(wr, 1, 0, &foo, sizeof (foo));\r
+}\r
+\r
+/**\r
+ * Callback to define the message header for a RFC5444 message\r
+ * @param wr\r
+ * @param message\r
+ */\r
+static void\r
+_cb_addMessageHeader(struct rfc5444_writer *wr, struct rfc5444_writer_message *message) {\r
+  printf("%s()\n", __func__);\r
+       \r
+       /* no originator, no sequence number, not hopcount, no hoplimit */\r
+       rfc5444_writer_set_msg_header(wr, message, false, false, false, false);\r
+}\r
+\r
+/**\r
+ * Initialize RFC5444 writer\r
+ * @param ptr pointer to "send_packet" function\r
+ */\r
+void\r
+writer_init(write_packet_func_ptr ptr) {\r
+  printf("%s()\n", __func__);\r
+\r
+  /* initialize writer */\r
+  rfc5444_writer_init(&writer);\r
+\r
+  /* register a target (for sending messages to) in writer */\r
+  rfc5444_writer_register_target(&writer, &interface_1);\r
+\r
+  /* register a message content provider */\r
+  rfc5444_writer_register_msgcontentprovider(&writer, &_message_content_provider, 0, 0);\r
+\r
+  /* register message type 1 with 4 byte addresses */\r
+  _msg = rfc5444_writer_register_message(&writer, 1, false, 4);\r
+  _msg->addMessageHeader = _cb_addMessageHeader;\r
+\r
+  /* set function to send binary packet content */\r
+  interface_1.sendPacket = ptr;\r
+}\r
+\r
+/**\r
+ * Cleanup RFC5444 writer\r
+ */\r
+void\r
+writer_cleanup(void) {\r
+  printf("%s()\n", __func__);\r
+\r
+  rfc5444_writer_cleanup(&writer);\r
+}\r
diff --git a/examples/rfc5444_reader_writer/writer.h b/examples/rfc5444_reader_writer/writer.h
new file mode 100644 (file)
index 0000000..1879946
--- /dev/null
@@ -0,0 +1,57 @@
+
+/*
+ * The olsr.org Optimized Link-State Routing daemon(olsrd)
+ * Copyright (c) 2004-2013, the olsr.org team - see HISTORY file
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in
+ *   the documentation and/or other materials provided with the
+ *   distribution.
+ * * Neither the name of olsr.org, olsrd nor the names of its
+ *   contributors may be used to endorse or promote products derived
+ *   from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Visit http://www.olsr.org for more information.
+ *
+ * If you find this software useful feel free to make a donation
+ * to the project. For more information see the website or contact
+ * the copyright holders.
+ *
+ */
+
+#ifndef WRITER_H_
+#define WRITER_H_
+
+#include "common/common_types.h"
+#include "rfc5444/rfc5444_writer.h"
+
+EXPORT extern struct rfc5444_writer_target interface_1;
+EXPORT extern struct rfc5444_writer writer;
+
+typedef void (*write_packet_func_ptr)(
+    struct rfc5444_writer *wr, struct rfc5444_writer_target *iface, void *buffer, size_t length);
+
+void writer_init(write_packet_func_ptr ptr);
+void writer_cleanup(void);
+
+#endif /* WRITER_H_ */
index 5adf217..50d602c 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * The olsr.org Optimized Link-State Routing daemon(olsrd)
+ * The olsr.org Optimized Link-State Routing daemon version 2 (olsrd )
  * Copyright (c) 2004-2013, the olsr.org team - see HISTORY file
  * All rights reserved.
  *
index 17c79d9..d9cb75d 100644 (file)
@@ -1,4 +1,6 @@
 # add test directories
+include_directories(.)
+
 add_subdirectory(cunit)
 add_subdirectory(common)
 add_subdirectory(config)