Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
IP connection level system calls are not supported on Windows.
Browse files Browse the repository at this point in the history
On Windows, we cannot do low level operations like setting control message or reading control message from the connections. Creating a different file for Windows with limited functionality.

Test failures: http://screen/yznZGqOdB6G

PiperOrigin-RevId: 329998928
  • Loading branch information
manugarg committed Sep 3, 2020
1 parent 0567dba commit 4cbdccb
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 59 deletions.
61 changes: 2 additions & 59 deletions servers/udp/udp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Google Inc.
// Copyright 2017-2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -20,15 +20,11 @@ package udp

import (
"context"
"fmt"
"io"
"net"

"github.com/google/cloudprober/logger"
"github.com/google/cloudprober/metrics"
configpb "github.com/google/cloudprober/servers/udp/proto"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)

const (
Expand All @@ -42,8 +38,6 @@ const (
type Server struct {
c *configpb.ServerConf
conn *net.UDPConn
p6 *ipv6.PacketConn
p4 *ipv4.PacketConn
l *logger.Logger
}

Expand All @@ -58,20 +52,9 @@ func New(initCtx context.Context, c *configpb.ServerConf, l *logger.Logger) (*Se
conn.Close()
}()

// Set up PacketConn wrappers around the connection, to receive packet
// destination IPs (FlagDst) and to specify source IPs with control messages.
// See readAndEcho() for usage.
p6 := ipv6.NewPacketConn(conn)
p4 := ipv4.NewPacketConn(conn)
if err := p6.SetControlMessage(ipv6.FlagDst, true); err != nil {
return nil, fmt.Errorf("SetControlMessage(FlagDst): %v", err)
}

return &Server{
c: c,
conn: conn,
p4: p4,
p6: p6,
l: l,
}, nil
}
Expand Down Expand Up @@ -104,7 +87,7 @@ func (s *Server) Start(ctx context.Context, dataChan chan<- *metrics.EventMetric
return s.conn.Close()
default:
}
readAndEcho(s.p6, s.p4, s.l)
readAndEcho(s.conn, s.l)
}
case configpb.ServerConf_DISCARD:
s.l.Infof("Starting UDP DISCARD server on port %d", int(s.c.GetPort()))
Expand All @@ -120,46 +103,6 @@ func (s *Server) Start(ctx context.Context, dataChan chan<- *metrics.EventMetric
return nil
}

func readAndEcho(p6 *ipv6.PacketConn, p4 *ipv4.PacketConn, l *logger.Logger) {
// TODO(manugarg): We read and echo back only 4098 bytes. We should look at raising this
// limit or making it configurable. Also of note, ReadFromUDP reads a single UDP datagram
// (up to the max size of 64K-sizeof(UDPHdr)) and discards the rest.
buf := make([]byte, 4098)

// ipv6.PacketConn also receives IPv4 packets.
len, cm, addr, err := p6.ReadFrom(buf)
if err != nil {
l.Errorf("ReadFrom(): %v", err)
return
}

var n int
if cm.Dst.To4() != nil {
// We have a v4 packet, but need to use an ipv4.PacketConn for sending.
wcm := &ipv4.ControlMessage{
Src: cm.Dst.To4(),
}
n, err = p4.WriteTo(buf[:len], wcm, addr)
} else {
// We have a v6 packet.
wcm := &ipv6.ControlMessage{
Src: cm.Dst.To16(),
}
n, err = p6.WriteTo(buf[:len], wcm, addr)
}

if err == io.EOF {
return
}
if err != nil {
l.Errorf("WriteTo(): %v", err)
return
}
if n < len {
l.Warningf("Reply truncated! Got %v bytes but only sent %v bytes", len, n)
}
}

// listenAndServeDiscard launches an UDP discard server listening on port.
func readAndDiscard(conn *net.UDPConn, l *logger.Logger) {
buf := make([]byte, 4098)
Expand Down
77 changes: 77 additions & 0 deletions servers/udp/udp_nonwindows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2017-2020 Google Inc.
//
// 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.

// +build !windows

package udp

import (
"io"
"net"

"github.com/google/cloudprober/logger"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)

func readAndEcho(conn *net.UDPConn, l *logger.Logger) {
// TODO(manugarg): We read and echo back only 4098 bytes. We should look at raising this
// limit or making it configurable. Also of note, ReadFromUDP reads a single UDP datagram
// (up to the max size of 64K-sizeof(UDPHdr)) and discards the rest.
buf := make([]byte, 4098)

// We use an IPv6 connection wrapper to receive both IPv4 and IPv6 packets.
// ipv6.PacketConn lets us use control messages to:
// -- receive packet destination IP (FlagDst)
// -- set source IP (Src).
p6 := ipv6.NewPacketConn(conn)
if err := p6.SetControlMessage(ipv6.FlagDst, true); err != nil {
l.Error("error running SetControlMessage(FlagDst): ", err.Error())
return
}

// ipv6.PacketConn also receives IPv4 packets.
len, cm, addr, err := p6.ReadFrom(buf)
if err != nil {
l.Errorf("ReadFrom(): %v", err)
return
}

var n int
if cm.Dst.To4() != nil {
// We have a v4 packet, use an ipv4.PacketConn for sending.
p4 := ipv4.NewPacketConn(conn)
wcm := &ipv4.ControlMessage{
Src: cm.Dst.To4(),
}
n, err = p4.WriteTo(buf[:len], wcm, addr)
} else {
// We have a v6 packet.
wcm := &ipv6.ControlMessage{
Src: cm.Dst.To16(),
}
n, err = p6.WriteTo(buf[:len], wcm, addr)
}

if err == io.EOF {
return
}
if err != nil {
l.Errorf("WriteTo(): %v", err)
return
}
if n < len {
l.Warningf("Reply truncated! Got %v bytes but only sent %v bytes", len, n)
}
}
48 changes: 48 additions & 0 deletions servers/udp/udp_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2017-2020 Google Inc.
//
// 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.

// +build windows

package udp

import (
"io"
"net"

"github.com/google/cloudprober/logger"
)

func readAndEcho(conn *net.UDPConn, l *logger.Logger) {
// TODO(manugarg): We read and echo back only 4098 bytes. We should look at raising this
// limit or making it configurable. Also of note, ReadFromUDP reads a single UDP datagram
// (up to the max size of 64K-sizeof(UDPHdr)) and discards the rest.
buf := make([]byte, 4098)
len, addr, err := conn.ReadFromUDP(buf)
if err != nil {
l.Errorf("ReadFromUDP: %v", err)
return
}

n, err := conn.WriteToUDP(buf[:len], addr)
if err == io.EOF {
return
}
if err != nil {
l.Errorf("WriteToUDP: %v", err)
return
}
if n < len {
l.Warningf("Reply truncated! Got %v bytes but only sent %v bytes", len, n)
}
}

0 comments on commit 4cbdccb

Please sign in to comment.