~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/net/net.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
 
45
45
import (
46
46
        "errors"
 
47
        "io"
 
48
        "os"
 
49
        "sync"
 
50
        "syscall"
47
51
        "time"
48
52
)
49
53
 
103
107
        SetWriteDeadline(t time.Time) error
104
108
}
105
109
 
 
110
type conn struct {
 
111
        fd *netFD
 
112
}
 
113
 
 
114
func (c *conn) ok() bool { return c != nil && c.fd != nil }
 
115
 
 
116
// Implementation of the Conn interface.
 
117
 
 
118
// Read implements the Conn Read method.
 
119
func (c *conn) Read(b []byte) (int, error) {
 
120
        if !c.ok() {
 
121
                return 0, syscall.EINVAL
 
122
        }
 
123
        return c.fd.Read(b)
 
124
}
 
125
 
 
126
// Write implements the Conn Write method.
 
127
func (c *conn) Write(b []byte) (int, error) {
 
128
        if !c.ok() {
 
129
                return 0, syscall.EINVAL
 
130
        }
 
131
        return c.fd.Write(b)
 
132
}
 
133
 
 
134
// Close closes the connection.
 
135
func (c *conn) Close() error {
 
136
        if !c.ok() {
 
137
                return syscall.EINVAL
 
138
        }
 
139
        return c.fd.Close()
 
140
}
 
141
 
 
142
// LocalAddr returns the local network address.
 
143
func (c *conn) LocalAddr() Addr {
 
144
        if !c.ok() {
 
145
                return nil
 
146
        }
 
147
        return c.fd.laddr
 
148
}
 
149
 
 
150
// RemoteAddr returns the remote network address.
 
151
func (c *conn) RemoteAddr() Addr {
 
152
        if !c.ok() {
 
153
                return nil
 
154
        }
 
155
        return c.fd.raddr
 
156
}
 
157
 
 
158
// SetDeadline implements the Conn SetDeadline method.
 
159
func (c *conn) SetDeadline(t time.Time) error {
 
160
        if !c.ok() {
 
161
                return syscall.EINVAL
 
162
        }
 
163
        return setDeadline(c.fd, t)
 
164
}
 
165
 
 
166
// SetReadDeadline implements the Conn SetReadDeadline method.
 
167
func (c *conn) SetReadDeadline(t time.Time) error {
 
168
        if !c.ok() {
 
169
                return syscall.EINVAL
 
170
        }
 
171
        return setReadDeadline(c.fd, t)
 
172
}
 
173
 
 
174
// SetWriteDeadline implements the Conn SetWriteDeadline method.
 
175
func (c *conn) SetWriteDeadline(t time.Time) error {
 
176
        if !c.ok() {
 
177
                return syscall.EINVAL
 
178
        }
 
179
        return setWriteDeadline(c.fd, t)
 
180
}
 
181
 
 
182
// SetReadBuffer sets the size of the operating system's
 
183
// receive buffer associated with the connection.
 
184
func (c *conn) SetReadBuffer(bytes int) error {
 
185
        if !c.ok() {
 
186
                return syscall.EINVAL
 
187
        }
 
188
        return setReadBuffer(c.fd, bytes)
 
189
}
 
190
 
 
191
// SetWriteBuffer sets the size of the operating system's
 
192
// transmit buffer associated with the connection.
 
193
func (c *conn) SetWriteBuffer(bytes int) error {
 
194
        if !c.ok() {
 
195
                return syscall.EINVAL
 
196
        }
 
197
        return setWriteBuffer(c.fd, bytes)
 
198
}
 
199
 
 
200
// File sets the underlying os.File to blocking mode and returns a copy.
 
201
// It is the caller's responsibility to close f when finished.
 
202
// Closing c does not affect f, and closing f does not affect c.
 
203
//
 
204
// The returned os.File's file descriptor is different from the connection's.
 
205
// Attempting to change properties of the original using this duplicate
 
206
// may or may not have the desired effect.
 
207
func (c *conn) File() (f *os.File, err error) { return c.fd.dup() }
 
208
 
106
209
// An Error represents a network error.
107
210
type Error interface {
108
211
        error
173
276
 
174
277
var errMissingAddress = errors.New("missing address")
175
278
 
 
279
// OpError is the error type usually returned by functions in the net
 
280
// package. It describes the operation, network type, and address of
 
281
// an error.
176
282
type OpError struct {
177
 
        Op   string
178
 
        Net  string
 
283
        // Op is the operation which caused the error, such as
 
284
        // "read" or "write".
 
285
        Op string
 
286
 
 
287
        // Net is the network type on which this error occurred,
 
288
        // such as "tcp" or "udp6".
 
289
        Net string
 
290
 
 
291
        // Addr is the network address on which this error occurred.
179
292
        Addr Addr
180
 
        Err  error
 
293
 
 
294
        // Err is the error that occurred during the operation.
 
295
        Err error
181
296
}
182
297
 
183
298
func (e *OpError) Error() string {
204
319
        return ok && t.Temporary()
205
320
}
206
321
 
 
322
var noDeadline = time.Time{}
 
323
 
207
324
type timeout interface {
208
325
        Timeout() bool
209
326
}
221
338
 
222
339
var errTimeout error = &timeoutError{}
223
340
 
 
341
var errClosing = errors.New("use of closed network connection")
 
342
 
224
343
type AddrError struct {
225
344
        Err  string
226
345
        Addr string
262
381
 
263
382
func (e *DNSConfigError) Timeout() bool   { return false }
264
383
func (e *DNSConfigError) Temporary() bool { return false }
 
384
 
 
385
type writerOnly struct {
 
386
        io.Writer
 
387
}
 
388
 
 
389
// Fallback implementation of io.ReaderFrom's ReadFrom, when sendfile isn't
 
390
// applicable.
 
391
func genericReadFrom(w io.Writer, r io.Reader) (n int64, err error) {
 
392
        // Use wrapper to hide existing r.ReadFrom from io.Copy.
 
393
        return io.Copy(writerOnly{w}, r)
 
394
}
 
395
 
 
396
// deadline is an atomically-accessed number of nanoseconds since 1970
 
397
// or 0, if no deadline is set.
 
398
type deadline struct {
 
399
        sync.Mutex
 
400
        val int64
 
401
}
 
402
 
 
403
func (d *deadline) expired() bool {
 
404
        t := d.value()
 
405
        return t > 0 && time.Now().UnixNano() >= t
 
406
}
 
407
 
 
408
func (d *deadline) value() (v int64) {
 
409
        d.Lock()
 
410
        v = d.val
 
411
        d.Unlock()
 
412
        return
 
413
}
 
414
 
 
415
func (d *deadline) set(v int64) {
 
416
        d.Lock()
 
417
        d.val = v
 
418
        d.Unlock()
 
419
}
 
420
 
 
421
func (d *deadline) setTime(t time.Time) {
 
422
        if t.IsZero() {
 
423
                d.set(0)
 
424
        } else {
 
425
                d.set(t.UnixNano())
 
426
        }
 
427
}