Allow building on !linux

PiperOrigin-RevId: 209819644
Change-Id: I329d054bf8f4999e7db0dcd95b13f7793c65d4e2
This commit is contained in:
Googler 2018-08-22 13:29:57 -07:00 committed by Shentubot
parent 6b9133ba96
commit bbee911179
11 changed files with 65 additions and 17 deletions

View File

@ -4,7 +4,10 @@ load("//tools/go_stateify:defs.bzl", "go_library")
go_library(
name = "rand",
srcs = ["rand.go"],
srcs = [
"rand.go",
"rand_linux.go",
],
importpath = "gvisor.googlesource.com/gvisor/pkg/rand",
visibility = ["//:sandbox"],
deps = ["@org_golang_x_sys//unix:go_default_library"],

View File

@ -12,28 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !linux
// Package rand implements a cryptographically secure pseudorandom number
// generator.
package rand
import (
"io"
"golang.org/x/sys/unix"
)
// reader implements an io.Reader that returns pseudorandom bytes.
type reader struct{}
// Read implements io.Reader.Read.
func (reader) Read(p []byte) (int, error) {
return unix.Getrandom(p, 0)
}
import "crypto/rand"
// Reader is the default reader.
var Reader io.Reader = reader{}
var Reader = rand.Reader
// Read reads from the default reader.
// Read implements io.Reader.Read.
func Read(b []byte) (int, error) {
return io.ReadFull(Reader, b)
return rand.Read(b)
}

39
pkg/rand/rand_linux.go Normal file
View File

@ -0,0 +1,39 @@
// Copyright 2018 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.
// Package rand implements a cryptographically secure pseudorandom number
// generator.
package rand
import (
"io"
"golang.org/x/sys/unix"
)
// reader implements an io.Reader that returns pseudorandom bytes.
type reader struct{}
// Read implements io.Reader.Read.
func (reader) Read(p []byte) (int, error) {
return unix.Getrandom(p, 0)
}
// Reader is the default reader.
var Reader io.Reader = reader{}
// Read reads from the default reader.
func Read(b []byte) (int, error) {
return io.ReadFull(Reader, b)
}

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
// Package fdbased provides the implemention of data-link layer endpoints
// backed by boundary-preserving file descriptors (e.g., TUN devices,
// seqpacket/datagram sockets).

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
package fdbased
import (

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
package sharedmem
import (

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
// Package sharedmem provides the implemention of data-link layer endpoints
// backed by shared memory.
//

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
package sharedmem
import (

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
// Package tun contains methods to open TAP and TUN devices.
package tun

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
// This sample creates a stack with TCP and IPv4 protocols on top of a TUN
// device, and connects to a peer. Similar to "nc <address> <port>". While the
// sample is running, attempts to connect to its IPv4 address will result in

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
// This sample creates a stack with TCP and IPv4 protocols on top of a TUN
// device, and listens on a port. Data received by the server in the accepted
// connections is echoed back to the clients.