2019-04-29 21:25:05 +00:00
|
|
|
// Copyright 2018 The gVisor Authors.
|
2018-07-09 21:03:03 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2018-04-27 17:37:02 +00:00
|
|
|
|
2018-10-17 18:36:32 +00:00
|
|
|
package transport
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
import (
|
2018-10-24 18:04:11 +00:00
|
|
|
"gvisor.googlesource.com/gvisor/pkg/syserr"
|
2018-04-27 17:37:02 +00:00
|
|
|
"gvisor.googlesource.com/gvisor/pkg/tcpip"
|
|
|
|
"gvisor.googlesource.com/gvisor/pkg/waiter"
|
|
|
|
)
|
|
|
|
|
|
|
|
// connectionlessEndpoint is a unix endpoint for unix sockets that support operating in
|
|
|
|
// a conectionless fashon.
|
|
|
|
//
|
|
|
|
// Specifically, this means datagram unix sockets not created with
|
|
|
|
// socketpair(2).
|
2018-08-02 17:41:44 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2018-04-27 17:37:02 +00:00
|
|
|
type connectionlessEndpoint struct {
|
|
|
|
baseEndpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConnectionless creates a new unbound dgram endpoint.
|
|
|
|
func NewConnectionless() Endpoint {
|
|
|
|
ep := &connectionlessEndpoint{baseEndpoint{Queue: &waiter.Queue{}}}
|
2018-12-18 01:52:05 +00:00
|
|
|
ep.receiver = &queueReceiver{readQueue: &queue{ReaderQueue: ep.Queue, WriterQueue: &waiter.Queue{}, limit: initialLimit}}
|
2018-04-27 17:37:02 +00:00
|
|
|
return ep
|
|
|
|
}
|
|
|
|
|
|
|
|
// isBound returns true iff the endpoint is bound.
|
|
|
|
func (e *connectionlessEndpoint) isBound() bool {
|
|
|
|
return e.path != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close puts the endpoint in a closed state and frees all resources associated
|
|
|
|
// with it.
|
|
|
|
//
|
|
|
|
// The socket will be a fresh state after a call to close and may be reused.
|
|
|
|
// That is, close may be used to "unbind" or "disconnect" the socket in error
|
|
|
|
// paths.
|
|
|
|
func (e *connectionlessEndpoint) Close() {
|
|
|
|
e.Lock()
|
|
|
|
var r Receiver
|
|
|
|
if e.Connected() {
|
|
|
|
e.receiver.CloseRecv()
|
|
|
|
r = e.receiver
|
|
|
|
e.receiver = nil
|
|
|
|
|
|
|
|
e.connected.Release()
|
|
|
|
e.connected = nil
|
|
|
|
}
|
|
|
|
if e.isBound() {
|
|
|
|
e.path = ""
|
|
|
|
}
|
|
|
|
e.Unlock()
|
|
|
|
if r != nil {
|
|
|
|
r.CloseNotify()
|
|
|
|
r.Release()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BidirectionalConnect implements BoundEndpoint.BidirectionalConnect.
|
2018-10-24 18:04:11 +00:00
|
|
|
func (e *connectionlessEndpoint) BidirectionalConnect(ce ConnectingEndpoint, returnConnect func(Receiver, ConnectedEndpoint)) *syserr.Error {
|
|
|
|
return syserr.ErrConnectionRefused
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnidirectionalConnect implements BoundEndpoint.UnidirectionalConnect.
|
2018-10-24 18:04:11 +00:00
|
|
|
func (e *connectionlessEndpoint) UnidirectionalConnect() (ConnectedEndpoint, *syserr.Error) {
|
2018-08-09 02:23:02 +00:00
|
|
|
e.Lock()
|
|
|
|
r := e.receiver
|
|
|
|
e.Unlock()
|
|
|
|
if r == nil {
|
2018-10-24 18:04:11 +00:00
|
|
|
return nil, syserr.ErrConnectionRefused
|
2018-08-09 02:23:02 +00:00
|
|
|
}
|
2018-10-21 00:57:19 +00:00
|
|
|
q := r.(*queueReceiver).readQueue
|
|
|
|
if !q.TryIncRef() {
|
2018-10-24 18:04:11 +00:00
|
|
|
return nil, syserr.ErrConnectionRefused
|
2018-10-21 00:57:19 +00:00
|
|
|
}
|
2018-04-27 17:37:02 +00:00
|
|
|
return &connectedEndpoint{
|
|
|
|
endpoint: e,
|
2018-10-21 00:57:19 +00:00
|
|
|
writeQueue: q,
|
2018-04-27 17:37:02 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendMsg writes data and a control message to the specified endpoint.
|
|
|
|
// This method does not block if the data cannot be written.
|
2018-10-24 18:04:11 +00:00
|
|
|
func (e *connectionlessEndpoint) SendMsg(data [][]byte, c ControlMessages, to BoundEndpoint) (uintptr, *syserr.Error) {
|
2018-04-27 17:37:02 +00:00
|
|
|
if to == nil {
|
|
|
|
return e.baseEndpoint.SendMsg(data, c, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
connected, err := to.UnidirectionalConnect()
|
|
|
|
if err != nil {
|
2018-10-24 18:04:11 +00:00
|
|
|
return 0, syserr.ErrInvalidEndpointState
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
defer connected.Release()
|
|
|
|
|
|
|
|
e.Lock()
|
|
|
|
n, notify, err := connected.Send(data, c, tcpip.FullAddress{Addr: tcpip.Address(e.path)})
|
|
|
|
e.Unlock()
|
2018-10-10 21:09:24 +00:00
|
|
|
|
2018-04-27 17:37:02 +00:00
|
|
|
if notify {
|
|
|
|
connected.SendNotify()
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:09:24 +00:00
|
|
|
return n, err
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type implements Endpoint.Type.
|
|
|
|
func (e *connectionlessEndpoint) Type() SockType {
|
|
|
|
return SockDgram
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect attempts to connect directly to server.
|
2018-10-24 18:04:11 +00:00
|
|
|
func (e *connectionlessEndpoint) Connect(server BoundEndpoint) *syserr.Error {
|
2018-04-27 17:37:02 +00:00
|
|
|
connected, err := server.UnidirectionalConnect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Lock()
|
|
|
|
e.connected = connected
|
|
|
|
e.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen starts listening on the connection.
|
2018-10-24 18:04:11 +00:00
|
|
|
func (e *connectionlessEndpoint) Listen(int) *syserr.Error {
|
|
|
|
return syserr.ErrNotSupported
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accept accepts a new connection.
|
2018-10-24 18:04:11 +00:00
|
|
|
func (e *connectionlessEndpoint) Accept() (Endpoint, *syserr.Error) {
|
|
|
|
return nil, syserr.ErrNotSupported
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bind binds the connection.
|
|
|
|
//
|
|
|
|
// For Unix endpoints, this _only sets the address associated with the socket_.
|
|
|
|
// Work associated with sockets in the filesystem or finding those sockets must
|
|
|
|
// be done by a higher level.
|
|
|
|
//
|
|
|
|
// Bind will fail only if the socket is connected, bound or the passed address
|
|
|
|
// is invalid (the empty string).
|
2018-10-24 18:04:11 +00:00
|
|
|
func (e *connectionlessEndpoint) Bind(addr tcpip.FullAddress, commit func() *syserr.Error) *syserr.Error {
|
2018-04-27 17:37:02 +00:00
|
|
|
e.Lock()
|
|
|
|
defer e.Unlock()
|
|
|
|
if e.isBound() {
|
2018-10-24 18:04:11 +00:00
|
|
|
return syserr.ErrAlreadyBound
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
if addr.Addr == "" {
|
|
|
|
// The empty string is not permitted.
|
2018-10-24 18:04:11 +00:00
|
|
|
return syserr.ErrBadLocalAddress
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
if commit != nil {
|
|
|
|
if err := commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the bound address.
|
|
|
|
e.path = string(addr.Addr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readiness returns the current readiness of the endpoint. For example, if
|
|
|
|
// waiter.EventIn is set, the endpoint is immediately readable.
|
|
|
|
func (e *connectionlessEndpoint) Readiness(mask waiter.EventMask) waiter.EventMask {
|
|
|
|
e.Lock()
|
|
|
|
defer e.Unlock()
|
|
|
|
|
|
|
|
ready := waiter.EventMask(0)
|
|
|
|
if mask&waiter.EventIn != 0 && e.receiver.Readable() {
|
|
|
|
ready |= waiter.EventIn
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Connected() {
|
|
|
|
if mask&waiter.EventOut != 0 && e.connected.Writable() {
|
|
|
|
ready |= waiter.EventOut
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ready
|
|
|
|
}
|