OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [sockopt.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 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
// +build darwin freebsd linux netbsd openbsd windows
6
 
7
// Socket options
8
 
9
package net
10
 
11
import (
12
        "bytes"
13
        "os"
14
        "syscall"
15
        "time"
16
)
17
 
18
// Boolean to int.
19
func boolint(b bool) int {
20
        if b {
21
                return 1
22
        }
23
        return 0
24
}
25
 
26
func ipv4AddrToInterface(ip IP) (*Interface, error) {
27
        ift, err := Interfaces()
28
        if err != nil {
29
                return nil, err
30
        }
31
        for _, ifi := range ift {
32
                ifat, err := ifi.Addrs()
33
                if err != nil {
34
                        return nil, err
35
                }
36
                for _, ifa := range ifat {
37
                        switch v := ifa.(type) {
38
                        case *IPAddr:
39
                                if ip.Equal(v.IP) {
40
                                        return &ifi, nil
41
                                }
42
                        case *IPNet:
43
                                if ip.Equal(v.IP) {
44
                                        return &ifi, nil
45
                                }
46
                        }
47
                }
48
        }
49
        if ip.Equal(IPv4zero) {
50
                return nil, nil
51
        }
52
        return nil, errNoSuchInterface
53
}
54
 
55
func interfaceToIPv4Addr(ifi *Interface) (IP, error) {
56
        if ifi == nil {
57
                return IPv4zero, nil
58
        }
59
        ifat, err := ifi.Addrs()
60
        if err != nil {
61
                return nil, err
62
        }
63
        for _, ifa := range ifat {
64
                switch v := ifa.(type) {
65
                case *IPAddr:
66
                        if v.IP.To4() != nil {
67
                                return v.IP, nil
68
                        }
69
                case *IPNet:
70
                        if v.IP.To4() != nil {
71
                                return v.IP, nil
72
                        }
73
                }
74
        }
75
        return nil, errNoSuchInterface
76
}
77
 
78
func setIPv4MreqToInterface(mreq *syscall.IPMreq, ifi *Interface) error {
79
        if ifi == nil {
80
                return nil
81
        }
82
        ifat, err := ifi.Addrs()
83
        if err != nil {
84
                return err
85
        }
86
        for _, ifa := range ifat {
87
                switch v := ifa.(type) {
88
                case *IPAddr:
89
                        if a := v.IP.To4(); a != nil {
90
                                copy(mreq.Interface[:], a)
91
                                goto done
92
                        }
93
                case *IPNet:
94
                        if a := v.IP.To4(); a != nil {
95
                                copy(mreq.Interface[:], a)
96
                                goto done
97
                        }
98
                }
99
        }
100
done:
101
        if bytes.Equal(mreq.Multiaddr[:], IPv4zero.To4()) {
102
                return errNoSuchMulticastInterface
103
        }
104
        return nil
105
}
106
 
107
func setReadBuffer(fd *netFD, bytes int) error {
108
        fd.incref()
109
        defer fd.decref()
110
        return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_RCVBUF, bytes))
111
}
112
 
113
func setWriteBuffer(fd *netFD, bytes int) error {
114
        fd.incref()
115
        defer fd.decref()
116
        return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_SNDBUF, bytes))
117
}
118
 
119
func setReadDeadline(fd *netFD, t time.Time) error {
120
        if t.IsZero() {
121
                fd.rdeadline = 0
122
        } else {
123
                fd.rdeadline = t.UnixNano()
124
        }
125
        return nil
126
}
127
 
128
func setWriteDeadline(fd *netFD, t time.Time) error {
129
        if t.IsZero() {
130
                fd.wdeadline = 0
131
        } else {
132
                fd.wdeadline = t.UnixNano()
133
        }
134
        return nil
135
}
136
 
137
func setDeadline(fd *netFD, t time.Time) error {
138
        if err := setReadDeadline(fd, t); err != nil {
139
                return err
140
        }
141
        return setWriteDeadline(fd, t)
142
}
143
 
144
func setReuseAddr(fd *netFD, reuse bool) error {
145
        fd.incref()
146
        defer fd.decref()
147
        return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, boolint(reuse)))
148
}
149
 
150
func setDontRoute(fd *netFD, dontroute bool) error {
151
        fd.incref()
152
        defer fd.decref()
153
        return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_DONTROUTE, boolint(dontroute)))
154
}
155
 
156
func setKeepAlive(fd *netFD, keepalive bool) error {
157
        fd.incref()
158
        defer fd.decref()
159
        return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, boolint(keepalive)))
160
}
161
 
162
func setNoDelay(fd *netFD, noDelay bool) error {
163
        fd.incref()
164
        defer fd.decref()
165
        return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY, boolint(noDelay)))
166
}
167
 
168
func setLinger(fd *netFD, sec int) error {
169
        var l syscall.Linger
170
        if sec >= 0 {
171
                l.Onoff = 1
172
                l.Linger = int32(sec)
173
        } else {
174
                l.Onoff = 0
175
                l.Linger = 0
176
        }
177
        fd.incref()
178
        defer fd.decref()
179
        return os.NewSyscallError("setsockopt", syscall.SetsockoptLinger(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_LINGER, &l))
180
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.