Git Product home page Git Product logo

water's Introduction

water

Documentation: GoDoc
Build Status: Build Status

water is a native Go library for TUN/TAP interfaces.

water is designed to be simple and efficient. It:

  • wraps almost only syscalls and uses only Go standard types;
  • exposes standard interfaces; plays well with standard packages like io, bufio, etc..
  • does not handle memory management (allocating/destructing slice). It's up to user to decide how to deal with buffers; whether to use GC.

water/waterutil has some useful functions to interpret MAC frame headers and IP packet headers. It also contains some constants such as protocol numbers and ethernet frame types.

Installation

go get -u github.com/meshbird/water
go get -u github.com/meshbird/water/waterutil

Documentation

http://godoc.org/github.com/meshbird/water

http://godoc.org/github.com/meshbird/water/waterutil

Example

package main

import (
	"github.com/meshbird/water"
	"github.com/meshbird/water/waterutil"
	"fmt"
)

const BUFFERSIZE = 1522

func main() {
	ifce, err := water.NewTAP("")
	fmt.Printf("%v, %v\n\n", err, ifce)
	buffer := make([]byte, BUFFERSIZE)
	for {
		_, err = ifce.Read(buffer)
		if err != nil {
			break
		}
		ethertype := waterutil.MACEthertype(buffer)
		if ethertype == waterutil.IPv4 {
			packet := waterutil.MACPayload(buffer)
			if waterutil.IsIPv4(packet) {
				fmt.Printf("Source:      %v [%v]\n", waterutil.MACSource(buffer), waterutil.IPv4Source(packet))
				fmt.Printf("Destination: %v [%v]\n", waterutil.MACDestination(buffer), waterutil.IPv4Destination(packet))
				fmt.Printf("Protocol:    %v\n\n", waterutil.IPv4Protocol(packet))
			}
		}
	}
}

This piece of code creates a TAP interface, and prints some header information for every IPv4 packet. After pull up the main.go, you'll need to bring up the interface and assign IP address. All of these need root permission.

sudo go run main.go
sudo ip link set dev tap0 up
sudo ip addr add 10.0.0.1/24 dev tap0

Now, try sending some ICMP broadcast message:

ping -b 10.0.0.255

You'll see the main.go print something like:

<nil>, &{true 0xf84003f058 tap0}

Source:      42:35:da:af:2b:00 [10.0.0.1]
Destination: ff:ff:ff:ff:ff:ff [10.0.0.255]
Protocol:    1

Source:      42:35:da:af:2b:00 [10.0.0.1]
Destination: ff:ff:ff:ff:ff:ff [10.0.0.255]
Protocol:    1

Source:      42:35:da:af:2b:00 [10.0.0.1]
Destination: ff:ff:ff:ff:ff:ff [10.0.0.255]
Protocol:    1

TODO

  • IPv6 Support in waterutil
  • Darwin(Mac) Support

LICENSE

BSD 3-Clause License

Alternatives

tuntap: https://code.google.com/p/tuntap/

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.