~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/code.google.com/p/go.net/ipv4/payload.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012 The Go Authors.  All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
package ipv4
 
6
 
 
7
import (
 
8
        "net"
 
9
        "syscall"
 
10
)
 
11
 
 
12
// A payloadHandler represents the IPv4 datagram payload handler.
 
13
type payloadHandler struct {
 
14
        c net.PacketConn
 
15
        rawOpt
 
16
}
 
17
 
 
18
func (c *payloadHandler) ok() bool { return c != nil && c.c != nil }
 
19
 
 
20
// ReadFrom reads a payload of the received IPv4 datagram, from the
 
21
// endpoint c, copying the payload into b.  It returns the number of
 
22
// bytes copied into b, the control message cm and the source address
 
23
// src of the received datagram.
 
24
func (c *payloadHandler) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error) {
 
25
        if !c.ok() {
 
26
                return 0, nil, nil, syscall.EINVAL
 
27
        }
 
28
        oob := newControlMessage(&c.rawOpt)
 
29
        var oobn int
 
30
        switch rd := c.c.(type) {
 
31
        case *net.UDPConn:
 
32
                if n, oobn, _, src, err = rd.ReadMsgUDP(b, oob); err != nil {
 
33
                        return 0, nil, nil, err
 
34
                }
 
35
        case *net.IPConn:
 
36
                nb := make([]byte, len(b)+maxHeaderLen)
 
37
                if n, oobn, _, src, err = rd.ReadMsgIP(nb, oob); err != nil {
 
38
                        return 0, nil, nil, err
 
39
                }
 
40
                hdrlen := (int(b[0]) & 0x0f) << 2
 
41
                copy(b, nb[hdrlen:])
 
42
                n -= hdrlen
 
43
        default:
 
44
                return 0, nil, nil, errInvalidConnType
 
45
        }
 
46
        if cm, err = parseControlMessage(oob[:oobn]); err != nil {
 
47
                return 0, nil, nil, err
 
48
        }
 
49
        if cm != nil {
 
50
                cm.Src = netAddrToIP4(src)
 
51
        }
 
52
        return
 
53
}
 
54
 
 
55
// WriteTo writes a payload of the IPv4 datagram, to the destination
 
56
// address dst through the endpoint c, copying the payload from b.  It
 
57
// returns the number of bytes written.  The control message cm allows
 
58
// the datagram path and the outgoing interface to be specified.
 
59
// Currently only Linux supports this.  The cm may be nil if control
 
60
// of the outgoing datagram is not required.
 
61
func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
 
62
        if !c.ok() {
 
63
                return 0, syscall.EINVAL
 
64
        }
 
65
        oob := marshalControlMessage(cm)
 
66
        if dst == nil {
 
67
                return 0, errMissingAddress
 
68
        }
 
69
        switch wr := c.c.(type) {
 
70
        case *net.UDPConn:
 
71
                n, _, err = wr.WriteMsgUDP(b, oob, dst.(*net.UDPAddr))
 
72
        case *net.IPConn:
 
73
                n, _, err = wr.WriteMsgIP(b, oob, dst.(*net.IPAddr))
 
74
        default:
 
75
                return 0, errInvalidConnType
 
76
        }
 
77
        if err != nil {
 
78
                return 0, err
 
79
        }
 
80
        return
 
81
}