Git Product home page Git Product logo

ltun's Introduction

Linux TUN/TAP module for Lua

ltun is a Lua module which let you create TUN/TAP device very easily.

License: MIT (see LICENSE)

Dependencies

Lua >= 5.1

Compilation

To compile, just type the following command in the terminal:

make

If you are on a debian-like system and you have installed all the required dependencies, it should work as-is. If you are out of luck, you can tweak the compilation process using the following variables:

  • LUA_VERSION
  • LUA_CFLAGS

For example, say that you want to compile ltun for Lua 5.1 (by default ltun is compiled for Lua 5.2) you can try:

make LUA_VERSION=5.1

Or for LuaJIT:

make LUA_VERSION=jit

If the Lua development headers are not in a common location, you can try:

make LUA_CFLAGS="-I/path/to/lua/headers"

Documentation

NOTE: On most distributions you will need to be root to create TUN/TAP devices.

To create a TUN device:

local ltun = require('ltun')

local tun = ltun.create()

To create a TAP device:

local ltun = require('ltun')

local tap = ltun.create(ltun.IFF_TAP)

To create a TUN/TAP device with a custom name:

local tun = ltun.create('mytun')

To specify additional flags:

local tun = ltun.create('mytun', ltun.IFF_TUN, ltun.IFF_NO_PI)

Or just:

local tun = ltun.create(ltun.IFF_TUN, ltun.IFF_NO_PI)

You can get/set some parameters of the device directly:

print(tun.name)
tun.addr = '10.8.0.1'
tun.dstaddr = '10.8.0.2'
tun.netmask = '255.255.255.0'
tun.mtu = 1500

If the device is a TAP you can also get/set its MAC address:

tap.hwaddr = '\x00\x11\x22\x33\x44\x55'
print(tap.hwaddr)

To make the device persistent:

tun:persist(true)

To bring up the device:

tun:up()

To bring down the device:

tun:down()

To read/write to the device, use the methods read(size) and write(buf):

buf = tun:read(tun.mtu)
tun:write(buf)

To close the device:

tun:close()

You can also get the file descriptor associated with a TUN/TAP device:

tun:fileno()

ltun's People

Contributors

montag451 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ltun's Issues

(string expected, got userdata) error setting addr

When I run this code in lua 5.1.4

local ltun = require("ltun")
local tun = ltun.create(ltun.IFF_TUN, ltun.IFF_NO_PI)
tun.addr = '10.8.0.1'
tun.dstaddr = '10.8.0.2'
tun.netmask = '255.255.255.0'
tun.mtu = 1500
print(tun.name)

I get this error:
lua: ./tun.lua:3: bad argument #1 to '?' (string expected, got userdata)

|>=tun.addr --Works OK
|>tun.addr = "10.8.0.1" --Fail

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.