### Windows Platform-Specific TAP Interface Configuration (Go) Source: https://context7.com/songgao/water/llms.txt This Go example demonstrates configuring a TAP interface with Windows-specific settings using the Water library. It shows how to set the ComponentID, InterfaceName, and network configuration (IP address and subnet mask) for TAP-Windows adapters. Error handling for interface creation is included. ```go package main import ( "log" "github.com/songgao/water" ) func main() { // Create TAP interface with Windows-specific configuration config := water.Config{ DeviceType: water.TAP, PlatformSpecificParams: water.PlatformSpecificParams{ ComponentID: "tap0901", // TAP-Windows adapter component ID InterfaceName: "Ethernet 3", // Windows interface name Network: "192.168.1.10/24", // Network configuration }, } ifce, err := water.New(config) if err != nil { log.Fatalf("Failed to create interface: %v", err) } defer ifce.Close() log.Printf("Created Windows TAP interface: %s", ifce.Name()) } ``` -------------------------------- ### Create TAP Interface with Custom Name (Go) Source: https://context7.com/songgao/water/llms.txt This example shows how to create a TAP (layer 2) interface with a custom name using the Water library. It configures the interface for Ethernet frame handling and demonstrates reading a frame and echoing it back. Error handling for creation, reading, and writing is present. ```go package main import ( "log" "github.com/songgao/water" ) func main() { // Create TAP interface with custom name config := water.Config{ DeviceType: water.TAP, } config.Name = "mytap0" ifce, err := water.New(config) if err != nil { log.Fatalf("Failed to create TAP interface: %v", err) } defer ifce.Close() log.Printf("Created TAP interface: %s", ifce.Name()) log.Printf("Is TAP: %v", ifce.IsTAP()) // Read and write frames frame := make([]byte, 1522) // MTU + Ethernet header n, err := ifce.Read(frame) if err != nil { log.Fatalf("Read error: %v", err) } // Echo back the frame _, err = ifce.Write(frame[:n]) if err != nil { log.Fatalf("Write error: %v", err) } } ``` -------------------------------- ### Go VPN Tunnel Example: Packet Filtering and Forwarding Source: https://context7.com/songgao/water/llms.txt This Go program sets up a TUN interface to create a VPN tunnel. It reads network packets, filters them to only allow ICMP traffic, logs packet details (source, destination, protocol, size), and then writes the ICMP packets back to the interface, simulating a basic forwarding mechanism. It also includes packet and byte statistics. ```go package main import ( "log" "net" "time" "github.com/songgao/water" "github.com/songgao/water/waterutil" ) func main() { // Create TUN interface for VPN tunnel config := water.Config{ DeviceType: water.TUN, } config.Name = "vpn0" ifce, err := water.New(config) if err != nil { log.Fatalf("Failed to create TUN interface: %v", err) } defer ifce.Close() log.Printf("Created VPN tunnel interface: %s", ifce.Name()) log.Println("Configure with: sudo ip addr add 10.8.0.1/24 dev vpn0") log.Println("Bring up with: sudo ip link set dev vpn0 up") // Statistics var packetsReceived, packetsSent, bytesReceived, bytesSent uint64 // Start statistics printer go func() { ticker := time.NewTicker(5 * time.Second) for range ticker.C { log.Printf("Stats - RX: %d packets (%d bytes), TX: %d packets (%d bytes)", packetsReceived, bytesReceived, packetsSent, bytesSent) } }() packet := make([]byte, 2000) for { // Read packet from tunnel n, err := ifce.Read(packet) if err != nil { log.Printf("Read error: %v", err) continue } packetsReceived++ bytesReceived += uint64(n) ipPacket := packet[:n] // Validate IPv4 if !waterutil.IsIPv4(ipPacket) { continue } srcIP := waterutil.IPv4Source(ipPacket) dstIP := waterutil.IPv4Destination(ipPacket) protocol := waterutil.IPv4Protocol(ipPacket) // Log packet details protocolName := "UNKNOWN" switch protocol { case waterutil.ICMP: protocolName = "ICMP" case waterutil.TCP: protocolName = "TCP" case waterutil.UDP: protocolName = "UDP" } log.Printf("Packet: %s -> %s, Protocol: %s, Size: %d bytes", srcIP, dstIP, protocolName, n) // Filter: only allow ICMP traffic if protocol != waterutil.ICMP { log.Printf(" Dropping non-ICMP packet") continue } // In a real VPN, you would encrypt and send over network here // For this example, we just echo it back _, err = ifce.Write(ipPacket) if err != nil { log.Printf("Write error: %v", err) continue } packetsSent++ bytesSent += uint64(n) } } ``` -------------------------------- ### Go: Create and Read from TAP Interface on Windows Source: https://github.com/songgao/water/blob/master/README.md This Go program demonstrates how to create a TAP interface using the 'water' library on Windows. It then enters a loop to continuously read Ethernet frames from the interface, logging details such as destination, source, Ethertype, and payload. Requires administrator privileges to run and assign an IP address. ```go package main import ( "log" "github.com/songgao/packets/ethernet" "github.com/songgao/water" ) func main() { ifce, err := water.New(water.Config{ DeviceType: water.TAP, }) if err != nil { log.Fatal(err) } var frame ethernet.Frame for { frame.Resize(1500) n, err := ifce.Read([]byte(frame)) if err != nil { log.Fatal(err) } frame = frame[:n] log.Printf("Dst: %s\n", frame.Destination()) log.Printf("Src: %s\n", frame.Source()) log.Printf("Ethertype: % x\n", frame.Ethertype()) log.Printf("Payload: % x\n", frame.Payload()) } } ``` -------------------------------- ### Create TAP Interface and Read Frames on Linux Source: https://github.com/songgao/water/blob/master/README.md This Go code snippet demonstrates how to create a TAP network interface on Linux using the 'water' library. It then enters a loop to continuously read network frames from the interface, printing header information such as destination and source MAC addresses, ethertype, and payload. This requires root privileges to run and to configure the interface. ```go package main import ( "log" "github.com/songgao/packets/ethernet" "github.com/songgao/water" ) func main() { config := water.Config{ DeviceType: water.TAP, } config.Name = "O_O" ifce, err := water.New(config) if err != nil { log.Fatal(err) } var frame ethernet.Frame for { frame.Resize(1500) n, err := ifce.Read([]byte(frame)) if err != nil { log.Fatal(err) } frame = frame[:n] log.Printf("Dst: %s\n", frame.Destination()) log.Printf("Src: %s\n", frame.Source()) log.Printf("Ethertype: % x\n", frame.Ethertype()) log.Printf("Payload: % x\n", frame.Payload()) } } ``` -------------------------------- ### Linux Platform-Specific TAP Interface Configuration (Go) Source: https://context7.com/songgao/water/llms.txt This Go snippet illustrates how to configure a TAP interface with Linux-specific parameters using the Water library. It includes settings for persistence, permissions (UID/GID), and enabling multiqueue support for parallel packet processing. Error handling for interface creation is included. ```go package main import ( "log" "github.com/songgao/water" ) func main() { // Create interface with Linux-specific parameters config := water.Config{ DeviceType: water.TAP, PlatformSpecificParams: water.PlatformSpecificParams{ Name: "custom_tap", Persist: true, Permissions: &water.DevicePermissions{ Owner: 1000, // User ID Group: 1000, // Group ID }, MultiQueue: true, // Enable multiqueue for parallel processing }, } ifce, err := water.New(config) if err != nil { log.Fatalf("Failed to create interface: %v", err) } defer ifce.Close() log.Printf("Created persistent interface: %s", ifce.Name()) log.Printf("MultiQueue enabled for parallel packet processing") } ``` -------------------------------- ### Create TUN Interface and Read Packets on macOS Source: https://github.com/songgao/water/blob/master/README.md This Go code snippet illustrates the creation of a point-to-point TUN interface on macOS using the 'water' library. It then enters an infinite loop to read incoming network packets from the interface, logging the received packet data in hexadecimal format. The code requires root privileges to execute and subsequently configure the 'utun' interface with IP addresses. ```go package main import ( "log" "github.com/songgao/water" ) func main() { ifce, err := water.New(water.Config{ DeviceType: water.TUN, }) if err != nil { log.Fatal(err) } log.Printf("Interface Name: %s\n", ifce.Name()) packet := make([]byte, 2000) for { n, err := ifce.Read(packet) if err != nil { log.Fatal(err) } log.Printf("Packet Received: % x\n", packet[:n]) } } ``` -------------------------------- ### Go: Configure TAP Interface with Platform Specific Parameters Source: https://github.com/songgao/water/blob/master/README.md This Go code snippet shows how to configure a TAP interface with specific platform parameters on Windows. It allows setting a ComponentID, InterfaceName, and Network details, which is useful when managing multiple TAP devices. ```go ifce, err := water.New(water.Config{ DeviceType: water.TAP, PlatformSpecificParams: water.PlatformSpecificParams{ ComponentID: "tap0901", InterfaceName: "Ethernet 3", Network: "192.168.1.10/24", }, }) ``` -------------------------------- ### Create TUN Interface with Default Config (Go) Source: https://context7.com/songgao/water/llms.txt This snippet demonstrates how to create a default TUN (layer 3) interface using the Water library. It configures the interface for IP packet handling and shows how to read packets from it. Error handling for interface creation and reading is included. ```go package main import ( "log" "github.com/songgao/water" ) func main() { // Create TUN interface with default configuration config := water.Config{ DeviceType: water.TUN, } ifce, err := water.New(config) if err != nil { log.Fatalf("Failed to create TUN interface: %v", err) } defer ifce.Close() log.Printf("Interface Name: %s", ifce.Name()) log.Printf("Is TUN: %v", ifce.IsTUN()) // Read packets from the interface packet := make([]byte, 2000) for { n, err := ifce.Read(packet) if err != nil { log.Fatalf("Read error: %v", err) } log.Printf("Received %d bytes: % x", n, packet[:n]) } } ``` -------------------------------- ### Windows: Assign Static IP Address to TAP Interface Source: https://github.com/songgao/water/blob/master/README.md This command configures a static IP address, subnet mask, and gateway for a specified network interface on Windows. It is used after creating a TAP interface with the 'water' library to enable network communication. Requires administrator privileges. ```dos # Replace with your device name, it can be achieved by ifce.Name(). netsh interface ip set address name="Ehternet 2" source=static addr=10.1.0.10 mask=255.255.255.0 gateway=none ``` -------------------------------- ### Process Ethernet Frame Types in Go Source: https://context7.com/songgao/water/llms.txt Demonstrates identifying and handling different Ethernet frame types using the water library. It reads frames, determines the ethertype, and logs information specific to IPv4 packets, such as source and destination IP addresses. ```go package main import ( "log" "github.com/songgao/water" "github.com/songgao/water/waterutil" ) func main() { config := water.Config{DeviceType: water.TAP} ifce, err := water.New(config) if err != nil { log.Fatal(err) } defer ifce.Close() frame := make([]byte, 1522) for { n, err := ifce.Read(frame) if err != nil { log.Fatal(err) } macFrame := frame[:n] ethertype := waterutil.MACEthertype(macFrame) // Handle different protocol types switch ethertype { case waterutil.IPv4: log.Println("IPv4 packet received") payload := waterutil.MACPayload(macFrame) srcIP := waterutil.IPv4Source(payload) dstIP := waterutil.IPv4Destination(payload) log.Printf(" %s -> %s", srcIP, dstIP) case waterutil.IPv6: log.Println("IPv6 packet received") case waterutil.ARP: log.Println("ARP frame received") case waterutil.IEEE802_1X: log.Println("802.1X authentication frame") case waterutil.LLDP: log.Println("LLDP frame received") default: log.Printf("Unknown ethertype: %02x %02x", ethertype[0], ethertype[1]) } } } ``` -------------------------------- ### Parse MAC Frame Headers in Go Source: https://context7.com/songgao/water/llms.txt Extracts MAC layer information from Ethernet frames, including source and destination addresses, VLAN tagging, and ethertype. This function reads from a network interface and parses the raw frame data. ```go package main import ( "log" "net" "github.com/songgao/water" "github.com/songgao/water/waterutil" ) func main() { config := water.Config{DeviceType: water.TAP} ifce, err := water.New(config) if err != nil { log.Fatal(err) } defer ifce.Close() frame := make([]byte, 1522) for { n, err := ifce.Read(frame) if err != nil { log.Fatal(err) } macFrame := frame[:n] // Extract MAC header information dst := waterutil.MACDestination(macFrame) src := waterutil.MACSource(macFrame) tagging := waterutil.MACTagging(macFrame) ethertype := waterutil.MACEthertype(macFrame) payload := waterutil.MACPayload(macFrame) log.Printf("Destination MAC: %s", dst.String()) log.Printf("Source MAC: %s", src.String()) log.Printf("VLAN Tagging: %d bytes", tagging) log.Printf("Ethertype: %02x %02x", ethertype[0], ethertype[1]) log.Printf("Payload length: %d bytes", len(payload)) // Check for broadcast or multicast if waterutil.IsBroadcast(dst) { log.Println("Broadcast frame detected") } if waterutil.IsIPv4Multicast(dst) { log.Println("IPv4 multicast frame detected") } } } ``` -------------------------------- ### Modify IPv4 Packet Headers and Ports in Go Source: https://context7.com/songgao/water/llms.txt Demonstrates modifying IPv4 packet headers and transport layer ports using the water library. This is useful for applications like NAT, packet forwarding, or filtering. It reads packets, checks if they are IPv4, modifies source and destination IPs and ports, and writes them back. ```go package main import ( "log" "net" "github.com/songgao/water" "github.com/songgao/water/waterutil" ) func main() { config := water.Config{DeviceType: water.TUN} ifce, err := water.New(config) if err != nil { log.Fatal(err) } defer ifce.Close() packet := make([]byte, 2000) for { n, err := ifce.Read(packet) if err != nil { log.Fatal(err) } ipPacket := packet[:n] if !waterutil.IsIPv4(ipPacket) { continue } // Log original packet info log.Printf("Original - Src: %s, Dst: %s", waterutil.IPv4Source(ipPacket), waterutil.IPv4Destination(ipPacket)) // Modify IP addresses (e.g., for NAT) newSrc := net.IPv4(10, 0, 0, 1) newDst := net.IPv4(192, 168, 1, 100) waterutil.SetIPv4Source(ipPacket, newSrc) waterutil.SetIPv4Destination(ipPacket, newDst) // Modify ports for TCP/UDP protocol := waterutil.IPv4Protocol(ipPacket) if protocol == waterutil.TCP || protocol == waterutil.UDP { waterutil.SetIPv4SourcePort(ipPacket, 8080) waterutil.SetIPv4DestinationPort(ipPacket, 443) log.Printf("Modified ports - Src: %d, Dst: %d", waterutil.IPv4SourcePort(ipPacket), waterutil.IPv4DestinationPort(ipPacket)) } log.Printf("Modified - Src: %s, Dst: %s", waterutil.IPv4Source(ipPacket), waterutil.IPv4Destination(ipPacket)) // Write modified packet back _, err = ifce.Write(ipPacket) if err != nil { log.Printf("Write error: %v", err) } } } ``` -------------------------------- ### Parse IPv4 Packet Headers in Go Source: https://context7.com/songgao/water/llms.txt Extracts IPv4 packet information, including source and destination addresses, protocol, TTL, and transport layer ports. This function reads from a TUN device and parses the IP packet data. ```go package main import ( "log" "github.com/songgao/water" "github.com/songgao/water/waterutil" ) func main() { config := water.Config{DeviceType: water.TUN} ifce, err := water.New(config) if err != nil { log.Fatal(err) } defer ifce.Close() packet := make([]byte, 2000) for { n, err := ifce.Read(packet) if err != nil { log.Fatal(err) } ipPacket := packet[:n] // Check IP version if !waterutil.IsIPv4(ipPacket) { log.Println("Not an IPv4 packet") continue } // Extract IPv4 header fields srcIP := waterutil.IPv4Source(ipPacket) dstIP := waterutil.IPv4Destination(ipPacket) protocol := waterutil.IPv4Protocol(ipPacket) ttl := waterutil.IPv4TTL(ipPacket) dscp := waterutil.IPv4DSCP(ipPacket) ecn := waterutil.IPv4ECN(ipPacket) log.Printf("Source IP: %s", srcIP.String()) log.Printf("Destination IP: %s", dstIP.String()) log.Printf("Protocol: 0x%02x", protocol) log.Printf("TTL: %d", ttl) log.Printf("DSCP: 0x%02x, ECN: 0x%02x", dscp, ecn) // Extract TCP/UDP port information if protocol == waterutil.TCP || protocol == waterutil.UDP { srcPort := waterutil.IPv4SourcePort(ipPacket) dstPort := waterutil.IPv4DestinationPort(ipPacket) log.Printf("Source Port: %d", srcPort) log.Printf("Destination Port: %d", dstPort) } // Get payload payload := waterutil.IPv4Payload(ipPacket) log.Printf("Payload length: %d bytes", len(payload)) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.