Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/android dns #943

Merged
merged 23 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Send UDP packet with DNS info as response.
  • Loading branch information
gigovich committed Jun 7, 2023
commit 8e66c0b01737cd7e46896988fb0ec313b2a391e8
5 changes: 3 additions & 2 deletions client/internal/dns/response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/miekg/dns"
"golang.zx2c4.com/wireguard/device"

"github.com/netbirdio/netbird/iface"
)
Expand Down Expand Up @@ -78,7 +77,9 @@ func (r *responseWriter) Write(data []byte) (int, error) {
return 0, fmt.Errorf("failed to serialize packet: %v", err)
}

return r.wgInterface.GetDevice().Write([][]byte{buffer.Bytes()}, device.MessageTransportOffsetContent)
send := buffer.Bytes()
r.wgInterface.GetDevice().NextRead(send)
return len(send), nil
}

// Close closes the connection.
Expand Down
23 changes: 23 additions & 0 deletions iface/device_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,35 @@ type DeviceWrapper struct {
tun.Device
filter PacketFilter
mutex sync.RWMutex
next chan []byte
}

// newDeviceWrapper constructor function
func newDeviceWrapper(device tun.Device) *DeviceWrapper {
return &DeviceWrapper{
Device: device,
next: make(chan []byte, 10),
}
}

// Read wraps read method with filtering feature
func (d *DeviceWrapper) Read(bufs [][]byte, sizes []int, offset int) (n int, err error) {
d.mutex.RLock()
var next []byte
if len(d.next) > 0 {
next = <-d.next
}
d.mutex.RUnlock()

if next != nil {
if len(bufs) == 0 {
bufs = append(bufs, next)
} else {
bufs[0] = next
}
return 1, nil
}

if n, err = d.Device.Read(bufs, sizes, offset); err != nil {
return 0, err
}
Expand Down Expand Up @@ -88,6 +106,11 @@ func (d *DeviceWrapper) Write(bufs [][]byte, offset int) (int, error) {
return n, err
}

// NextRead pushes packet data to next read
func (d *DeviceWrapper) NextRead(data []byte) {
d.next <- data
}

// SetFilter sets packet filter to device
func (d *DeviceWrapper) SetFilter(filter PacketFilter) {
d.mutex.Lock()
Expand Down
7 changes: 3 additions & 4 deletions iface/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/netbirdio/netbird/iface/bind"

log "github.com/sirupsen/logrus"
"golang.zx2c4.com/wireguard/tun"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)

Expand Down Expand Up @@ -146,10 +145,10 @@ func (w *WGIface) GetFilter() PacketFilter {
return w.filter
}

// GetDevice to interact with raw device (without filtering)
func (w *WGIface) GetDevice() tun.Device {
// GetDevice to interact with raw device (with filtering)
func (w *WGIface) GetDevice() *DeviceWrapper {
w.mu.Lock()
defer w.mu.Unlock()

return w.tun.wrapper.Device
return w.tun.wrapper
}