Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
cmake_minimum_required(VERSION 2.8)

# Define project name
project("libviface")

# The version number
set(libviface_VERSION_STRING "1.1.0")

# Setup project variables
set(VIFACE_TESTS ON CACHE BOOL "Enable compilation of unit tests.")
set(VIFACE_EXAMPLES ON CACHE BOOL "Enable compilation of the examples.")
set(VIFACE_FORCE_32BITS OFF CACHE BOOL "Force library compilation for 32 bits.")

# Force C++11 compiler
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# Installation variables
set(CMAKE_INSTALL_LIBDIR lib)
set(CMAKE_INSTALL_INCLUDEDIR include)
set(CMAKE_INSTALL_BINDIR bin)

# Configure library install location
if(VIFACE_FORCE_32BITS)
set(CMAKE_INSTALL_LIBDIR lib32)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
endif(VIFACE_FORCE_32BITS)

# Support for pkg-config file
set(pkgconfig_prefix ${CMAKE_INSTALL_PREFIX})
set(pkgconfig_exec_prefix ${CMAKE_INSTALL_PREFIX})
set(pkgconfig_libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
set(pkgconfig_version ${libviface_VERSION_STRING})
configure_file(
"${libviface_SOURCE_DIR}/libviface.pc.in"
"${CMAKE_BINARY_DIR}/libviface.pc"
@ONLY
)

include(FindPkgConfig)

# Check and set APR dependencies
pkg_check_modules(APR apr-1>=1.5.1)
set (LIBVIFACE_PC_REQUIRED_PACKAGES ${LIBVIFACE_PC_REQUIRED_PACKAGES} "apr-1 >= 1.5.1")
if(NOT APR_FOUND)
message(FATAL_ERROR "Apache Portable Runtime not found, please install it (>= 1.5.1)")
endif()

pkg_check_modules(APRUTIL apr-util-1>=1.5.3)
set (LIBVIFACE_PC_REQUIRED_PACKAGES ${LIBVIFACE_PC_REQUIRED_PACKAGES} "apr-util-1 >= 1.5.3")
if(NOT APRUTIL_FOUND)
message(FATAL_ERROR "Apache Portable Runtime Utils not found, please install it (>= 1.5.3)")
endif()

add_definitions(${APR_CFLAGS} ${APRUTIL_CFLAGS})
include_directories(${APR_INCLUDE_DIRS} ${APRUTIL_INCLUDE_DIRS})
link_directories(${APR_LIBRARY_DIRS} ${APRUTIL_LIBRARY_DIRS})

install(
FILES
"${CMAKE_BINARY_DIR}/libviface.pc"
DESTINATION
"${CMAKE_INSTALL_LIBDIR}/pkgconfig"
)

# Configure library
configure_file(
"${libviface_SOURCE_DIR}/include/viface/config.h.in"
"${CMAKE_BINARY_DIR}/include/viface/config.h"
@ONLY
)

# Creates custom target to build library
add_custom_target(
${PROJECT_NAME}
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
COMMENT "Building ${PROJECT_NAME} library..." VERBATIM
)

# Specify include directory
include_directories("${libviface_SOURCE_DIR}/include/")
include_directories("${CMAKE_BINARY_DIR}/include/")

# Specify source directory
add_subdirectory("${libviface_SOURCE_DIR}/src/")
if(VIFACE_TESTS)
message(STATUS "Including libviface test suite...")
add_subdirectory("${libviface_SOURCE_DIR}/test/")
endif(VIFACE_TESTS)
if(VIFACE_EXAMPLES)
message(STATUS "Including libviface examples...")
add_subdirectory("${libviface_SOURCE_DIR}/examples/")
endif(VIFACE_EXAMPLES)

# Specify bindings directory
add_subdirectory("${libviface_SOURCE_DIR}/bindings/")

# Specify public interface install directory
install(
FILES
"${libviface_SOURCE_DIR}/include/viface/viface.h"
"${CMAKE_BINARY_DIR}/include/viface/config.h"
DESTINATION
"${CMAKE_INSTALL_INCLUDEDIR}/viface"
)

# Creates a symbolic link of the shared library
add_custom_command(
TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ln -sf ${CMAKE_SOURCE_DIR}/build/src/libviface.so.1.1.0 /usr/lib/
COMMENT "Generating symbolic link to ${PROJECT_NAME} library"
)

# Add API Reference generation
find_package(Doxygen)
if(DOXYGEN_FOUND)
configure_file(
"${libviface_SOURCE_DIR}/doc/doxygen.conf.in"
"${CMAKE_BINARY_DIR}/doxygen.conf"
@ONLY
)
add_custom_target(
doc
"${DOXYGEN_EXECUTABLE}"
"${CMAKE_BINARY_DIR}/doxygen.conf"
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
COMMENT "Generating API Reference documentation..." VERBATIM
)
endif(DOXYGEN_FOUND)

# Setup Git hooks
if(EXISTS ${CMAKE_SOURCE_DIR}/.git/hooks)

# Setup our git hooks to avoid people from checking in code against the
# Coding Standard (at least syntactically)
if(NOT EXISTS ${CMAKE_SOURCE_DIR}/.git/hooks/pre-commit)

message(STATUS "Installing Coding Standard pre-commit hook...")
execute_process(
COMMAND ln -s ../../tools/uncrustify/pre-commit ${CMAKE_SOURCE_DIR}/.git/hooks/pre-commit
COMMAND chmod 0755 ${CMAKE_SOURCE_DIR}/.git/hooks/pre-commit
)
endif()
endif()
43 changes: 32 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
==================================================================
libviface : C++ bindings for Linux tun/tap and netdevice interface
libviface : libviface C object oriented interface for managing and creating network interfaces.
==================================================================

``libviface`` is a small C++11 library that allows to create and configure
``libviface`` is a small C library that allows to create and configure
virtual network interfaces in Linux based Operating Systems.

::

#include "viface/viface.hpp"
#include "viface/viface.h"

// Create interface
viface::VIface iface("viface%d");
// Creates viface struct
struct viface *self;

// Configure interface
iface.setMAC("66:23:2d:28:c6:84");
iface.setIPv4("192.168.20.21");
// Interface configuration data
char *name = "viface0";
char *ip = "192.168.25.46";
char* mac = "ec:f1:f8:d5:47:6b";
int id = 1;

// Bring-up interface
iface.up();
apr_initialize();

// Creates parent pool
apr_pool_t *parent_pool;
apr_pool_create(&parent_pool, NULL);

// Creates interface
if ((viface_create(&parent_pool, &self) == EXIT_FAILURE) ||
(vifaceImpl(&self, name, true, id) == EXIT_FAILURE)) {
return EXIT_FAILURE;
}

// Configures interface
if ((setIPv4(&self, ip) == EXIT_FAILURE) ||
(setMAC(&self, mac) == EXIT_FAILURE)) {
return EXIT_FAILURE;
}

// Brings-up interface
if (up(&self) == EXIT_FAILURE) {
return EXIT_FAILURE;
}

Then you can ``send()``, ``receive()`` or setup a ``dispath()`` callback to
handle virtual interfaces incoming and outgoing packets. Also, interface
Expand All @@ -34,7 +56,6 @@ Features
- Multiple strategies for packet reception and emission.
- Interface configuration API (MAC, Ipv4, IPv6, MTU).
- Interface statistics reading and clearing.
- Easily integrated with ``libtins``.


Dependencies
Expand Down
2 changes: 2 additions & 0 deletions bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory("cpp")
add_subdirectory("go")
37 changes: 22 additions & 15 deletions bindings/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 2.8)
project("libvifacecpp")

# The version number
set(libviface_VERSION_STRING "1.1.0")
set(libvifacecpp_VERSION_STRING "1.1.0")

# Setup project variables
set(VIFACE_TESTS ON CACHE BOOL "Enable compilation of unit tests.")
Expand All @@ -31,9 +31,9 @@ endif(VIFACE_FORCE_32BITS)
set(pkgconfig_prefix ${CMAKE_INSTALL_PREFIX})
set(pkgconfig_exec_prefix ${CMAKE_INSTALL_PREFIX})
set(pkgconfig_libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
set(pkgconfig_version ${libviface_VERSION_STRING})
set(pkgconfig_version ${libvifacecpp_VERSION_STRING})
configure_file(
"${libviface_SOURCE_DIR}/libvifacecpp.pc.in"
"${libvifacecpp_SOURCE_DIR}/libvifacecpp.pc.in"
"${CMAKE_BINARY_DIR}/libvifacecpp.pc"
@ONLY
)
Expand All @@ -46,30 +46,37 @@ install(

# Configure library
configure_file(
"${libviface_SOURCE_DIR}/include/viface/config.hpp.in"
"${libvifacecpp_SOURCE_DIR}/include/viface/config.hpp.in"
"${CMAKE_BINARY_DIR}/include/viface/config.hpp"
@ONLY
)

# Creates custom target to build library
add_custom_target(
${PROJECT_NAME}
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
COMMENT "Building ${PROJECT_NAME} library..." VERBATIM
)

# Specify include directory
include_directories("${libviface_SOURCE_DIR}/include/")
include_directories("${libvifacecpp_SOURCE_DIR}/include/")
include_directories("${CMAKE_BINARY_DIR}/include/")

# Specify source directory
add_subdirectory("${libviface_SOURCE_DIR}/src/")
add_subdirectory("${libvifacecpp_SOURCE_DIR}/src/")
if(VIFACE_TESTS)
message(STATUS "Including libviface test suite...")
add_subdirectory("${libviface_SOURCE_DIR}/test/")
message(STATUS "Including libvifacecpp test suite...")
add_subdirectory("${libvifacecpp_SOURCE_DIR}/test/")
endif(VIFACE_TESTS)
if(VIFACE_EXAMPLES)
message(STATUS "Including libviface examples...")
add_subdirectory("${libviface_SOURCE_DIR}/examples/")
message(STATUS "Including libvifacecpp examples...")
add_subdirectory("${libvifacecpp_SOURCE_DIR}/examples/")
endif(VIFACE_EXAMPLES)

# Specify public interface install directory
install(
FILES
"${libviface_SOURCE_DIR}/include/viface/viface.hpp"
"${libvifacecpp_SOURCE_DIR}/include/viface/viface.hpp"
"${CMAKE_BINARY_DIR}/include/viface/config.hpp"
DESTINATION
"${CMAKE_INSTALL_INCLUDEDIR}/viface"
Expand All @@ -79,14 +86,14 @@ install(
find_package(Doxygen)
if(DOXYGEN_FOUND)
configure_file(
"${libviface_SOURCE_DIR}/doc/doxygen.conf.in"
"${CMAKE_BINARY_DIR}/doxygen.conf"
"${libvifacecpp_SOURCE_DIR}/doc/doxygen.conf.in"
"${CMAKE_BINARY_DIR}/bindings/cpp/doxygen.conf"
@ONLY
)
add_custom_target(
doc
doccpp
"${DOXYGEN_EXECUTABLE}"
"${CMAKE_BINARY_DIR}/doxygen.conf"
"${CMAKE_BINARY_DIR}/bindings/cpp/doxygen.conf"
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
COMMENT "Generating API Reference documentation..." VERBATIM
)
Expand Down
86 changes: 86 additions & 0 deletions bindings/cpp/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
==================================================================
libvifacecpp : C++ bindings for Linux tun/tap and netdevice interface
==================================================================

``libvifacecpp`` is a small C++11 library that allows to create and configure
virtual network interfaces in Linux based Operating Systems.

::

#include "viface/viface.hpp"

// Create interface
viface::VIface iface("viface%d");

// Configure interface
iface.setMAC("66:23:2d:28:c6:84");
iface.setIPv4("192.168.20.21");

// Bring-up interface
iface.up();

Then you can ``send()``, ``receive()`` or setup a ``dispath()`` callback to
handle virtual interfaces incoming and outgoing packets. Also, interface
statistics (rx/tx packets, bytes, etc) are available to read using
``readStat()`` and related functions.

For a complete overview check the reference documentation and examples.


Features
========

- Object Oriented approach to create virtual interfaces.
- Multiple strategies for packet reception and emission.
- Interface configuration API (MAC, Ipv4, IPv6, MTU).
- Interface statistics reading and clearing.
- Easily integrated with ``libtins``.


Dependencies
============

::

sudo apt-get install build-essential cmake doxygen graphviz


Build
=====

::

mkdir build
cd build
cmake ..
make
make doccpp


Improvements
============

- Improve and fix possible race conditions when up/down is issued (and thus
packet buffer based on MTU is resized) and a dispatcher is active or any
other IO is active.


License
=======

::

Copyright (C) 2015 Hewlett Packard Enterprise Development LP

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
Loading