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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [syscall/] [socket.go] - Blame information for rev 854

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// socket.go -- Socket handling.
2
 
3
// Copyright 2009 The Go Authors. All rights reserved.
4
// Use of this source code is governed by a BSD-style
5
// license that can be found in the LICENSE file.
6
 
7
// Low-level socket interface.
8
// Only for implementing net package.
9
// DO NOT USE DIRECTLY.
10
 
11
package syscall
12
 
13
import "unsafe"
14
 
15
// For testing: clients can set this flag to force
16
// creation of IPv6 sockets to return EAFNOSUPPORT.
17
var SocketDisableIPv6 bool
18
 
19
type Sockaddr interface {
20
        sockaddr() (ptr *RawSockaddrAny, len Socklen_t, err error) // lowercase; only we can define Sockaddrs
21
}
22
 
23
type RawSockaddrAny struct {
24
        Addr RawSockaddr
25
        Pad  [12]int8
26
}
27
 
28
const SizeofSockaddrAny = 0x1c
29
 
30
type SockaddrInet4 struct {
31
        Port int
32
        Addr [4]byte
33
        raw  RawSockaddrInet4
34
}
35
 
36
func (sa *SockaddrInet4) sockaddr() (*RawSockaddrAny, Socklen_t, error) {
37
        if sa.Port < 0 || sa.Port > 0xFFFF {
38
                return nil, 0, EINVAL
39
        }
40
        sa.raw.Family = AF_INET
41
        n := sa.raw.setLen()
42
        p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
43
        p[0] = byte(sa.Port >> 8)
44
        p[1] = byte(sa.Port)
45
        for i := 0; i < len(sa.Addr); i++ {
46
                sa.raw.Addr[i] = sa.Addr[i]
47
        }
48
        return (*RawSockaddrAny)(unsafe.Pointer(&sa.raw)), n, nil
49
}
50
 
51
type SockaddrInet6 struct {
52
        Port   int
53
        ZoneId uint32
54
        Addr   [16]byte
55
        raw    RawSockaddrInet6
56
}
57
 
58
func (sa *SockaddrInet6) sockaddr() (*RawSockaddrAny, Socklen_t, error) {
59
        if sa.Port < 0 || sa.Port > 0xFFFF {
60
                return nil, 0, EINVAL
61
        }
62
        sa.raw.Family = AF_INET6
63
        n := sa.raw.setLen()
64
        p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
65
        p[0] = byte(sa.Port >> 8)
66
        p[1] = byte(sa.Port)
67
        sa.raw.Scope_id = sa.ZoneId
68
        for i := 0; i < len(sa.Addr); i++ {
69
                sa.raw.Addr[i] = sa.Addr[i]
70
        }
71
        return (*RawSockaddrAny)(unsafe.Pointer(&sa.raw)), n, nil
72
}
73
 
74
type SockaddrUnix struct {
75
        Name string
76
        raw  RawSockaddrUnix
77
}
78
 
79
func (sa *SockaddrUnix) sockaddr() (*RawSockaddrAny, Socklen_t, error) {
80
        name := sa.Name
81
        n := len(name)
82
        if n >= len(sa.raw.Path) || n == 0 {
83
                return nil, 0, EINVAL
84
        }
85
        sa.raw.Family = AF_UNIX
86
        sa.raw.setLen(n)
87
        for i := 0; i < n; i++ {
88
                sa.raw.Path[i] = int8(name[i])
89
        }
90
        if sa.raw.Path[0] == '@' {
91
                sa.raw.Path[0] = 0
92
        }
93
 
94
        // length is family (uint16), name, NUL.
95
        return (*RawSockaddrAny)(unsafe.Pointer(&sa.raw)), 2 + Socklen_t(n) + 1, nil
96
}
97
 
98
func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) {
99
        switch rsa.Addr.Family {
100
        case AF_UNIX:
101
                pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
102
                sa := new(SockaddrUnix)
103
                n, err := pp.getLen()
104
                if err != nil {
105
                        return nil, err
106
                }
107
                bytes := (*[len(pp.Path)]byte)(unsafe.Pointer(&pp.Path[0]))
108
                sa.Name = string(bytes[0:n])
109
                return sa, nil
110
 
111
        case AF_INET:
112
                pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
113
                sa := new(SockaddrInet4)
114
                p := (*[2]byte)(unsafe.Pointer(&pp.Port))
115
                sa.Port = int(p[0])<<8 + int(p[1])
116
                for i := 0; i < len(sa.Addr); i++ {
117
                        sa.Addr[i] = pp.Addr[i]
118
                }
119
                return sa, nil
120
 
121
        case AF_INET6:
122
                pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
123
                sa := new(SockaddrInet6)
124
                p := (*[2]byte)(unsafe.Pointer(&pp.Port))
125
                sa.Port = int(p[0])<<8 + int(p[1])
126
                for i := 0; i < len(sa.Addr); i++ {
127
                        sa.Addr[i] = pp.Addr[i]
128
                }
129
                return sa, nil
130
        }
131
        return anyToSockaddrOS(rsa)
132
}
133
 
134
//sys   accept(fd int, sa *RawSockaddrAny, len *Socklen_t) (nfd int, err error)
135
//accept(fd int, sa *RawSockaddrAny, len *Socklen_t) int
136
 
137
func Accept(fd int) (nfd int, sa Sockaddr, err error) {
138
        var rsa RawSockaddrAny
139
        var len Socklen_t = SizeofSockaddrAny
140
        nfd, err = accept(fd, &rsa, &len)
141
        if err != nil {
142
                return
143
        }
144
        sa, err = anyToSockaddr(&rsa)
145
        if err != nil {
146
                Close(nfd)
147
                nfd = 0
148
        }
149
        return
150
}
151
 
152
//sysnb getsockname(fd int, sa *RawSockaddrAny, len *Socklen_t) (err error)
153
//getsockname(fd int, sa *RawSockaddrAny, len *Socklen_t) int
154
 
155
func Getsockname(fd int) (sa Sockaddr, err error) {
156
        var rsa RawSockaddrAny
157
        var len Socklen_t = SizeofSockaddrAny
158
        if err = getsockname(fd, &rsa, &len); err != nil {
159
                return
160
        }
161
        return anyToSockaddr(&rsa)
162
}
163
 
164
//sysnb getpeername(fd int, sa *RawSockaddrAny, len *Socklen_t) (err error)
165
//getpeername(fd int, sa *RawSockaddrAny, len *Socklen_t) int
166
 
167
func Getpeername(fd int) (sa Sockaddr, err error) {
168
        var rsa RawSockaddrAny
169
        var len Socklen_t = SizeofSockaddrAny
170
        if err = getpeername(fd, &rsa, &len); err != nil {
171
                return
172
        }
173
        return anyToSockaddr(&rsa)
174
}
175
 
176
//sys   bind(fd int, sa *RawSockaddrAny, len Socklen_t) (err error)
177
//bind(fd int, sa *RawSockaddrAny, len Socklen_t) int
178
 
179
func Bind(fd int, sa Sockaddr) (err error) {
180
        ptr, n, err := sa.sockaddr()
181
        if err != nil {
182
                return err
183
        }
184
        return bind(fd, ptr, n)
185
}
186
 
187
//sys   connect(s int, addr *RawSockaddrAny, addrlen Socklen_t) (err error)
188
//connect(s int, addr *RawSockaddrAny, addrlen Socklen_t) int
189
 
190
func Connect(fd int, sa Sockaddr) (err error) {
191
        ptr, n, err := sa.sockaddr()
192
        if err != nil {
193
                return err
194
        }
195
        return connect(fd, ptr, n)
196
}
197
 
198
//sysnb socket(domain int, typ int, proto int) (fd int, err error)
199
//socket(domain int, typ int, protocol int) int
200
 
201
func Socket(domain, typ, proto int) (fd int, err error) {
202
        if domain == AF_INET6 && SocketDisableIPv6 {
203
                return -1, EAFNOSUPPORT
204
        }
205
        fd, err = socket(domain, typ, proto)
206
        return
207
}
208
 
209
//sysnb socketpair(domain int, typ int, proto int, fd *[2]int) (err error)
210
//socketpair(domain int, typ int, protocol int, fd *[2]int) int
211
 
212
func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
213
        err = socketpair(domain, typ, proto, &fd)
214
        return
215
}
216
 
217
//sys   getsockopt(s int, level int, name int, val uintptr, vallen *Socklen_t) (err error)
218
//getsockopt(s int, level int, name int, val *byte, vallen *Socklen_t) int
219
 
220
func GetsockoptInt(fd, level, opt int) (value int, err error) {
221
        var n int32
222
        vallen := Socklen_t(4)
223
        err = getsockopt(fd, level, opt, (uintptr)(unsafe.Pointer(&n)), &vallen)
224
        return int(n), err
225
}
226
 
227
func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
228
        vallen := Socklen_t(4)
229
        err = getsockopt(fd, level, opt, uintptr(unsafe.Pointer(&value[0])), &vallen)
230
        return value, err
231
}
232
 
233
func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
234
        var value IPMreq
235
        vallen := Socklen_t(SizeofIPMreq)
236
        err := getsockopt(fd, level, opt, uintptr(unsafe.Pointer(&value)), &vallen)
237
        return &value, err
238
}
239
 
240
func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) {
241
        var value IPMreqn
242
        vallen := Socklen_t(SizeofIPMreqn)
243
        err := getsockopt(fd, level, opt, uintptr(unsafe.Pointer(&value)), &vallen)
244
        return &value, err
245
}
246
 
247
func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
248
        var value IPv6Mreq
249
        vallen := Socklen_t(SizeofIPv6Mreq)
250
        err := getsockopt(fd, level, opt, uintptr(unsafe.Pointer(&value)), &vallen)
251
        return &value, err
252
}
253
 
254
//sys   setsockopt(s int, level int, name int, val *byte, vallen Socklen_t) (err error)
255
//setsockopt(s int, level int, optname int, val *byte, vallen Socklen_t) int
256
 
257
func SetsockoptInt(fd, level, opt int, value int) (err error) {
258
        var n = int32(value)
259
        return setsockopt(fd, level, opt, (*byte)(unsafe.Pointer(&n)), 4)
260
}
261
 
262
func SetsockoptInet4Addr(fd, level, opt int, value [4]byte) (err error) {
263
        return setsockopt(fd, level, opt, (*byte)(unsafe.Pointer(&value[0])), 4)
264
}
265
 
266
func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (err error) {
267
        return setsockopt(fd, level, opt, (*byte)(unsafe.Pointer(tv)), Socklen_t(unsafe.Sizeof(*tv)))
268
}
269
 
270
type Linger struct {
271
        Onoff  int32
272
        Linger int32
273
}
274
 
275
func SetsockoptLinger(fd, level, opt int, l *Linger) (err error) {
276
        return setsockopt(fd, level, opt, (*byte)(unsafe.Pointer(l)), Socklen_t(unsafe.Sizeof(*l)))
277
}
278
 
279
func SetsockoptIPMreq(fd, level, opt int, mreq *IPMreq) (err error) {
280
        return setsockopt(fd, level, opt, (*byte)(unsafe.Pointer(mreq)), Socklen_t(unsafe.Sizeof(*mreq)))
281
}
282
 
283
func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) {
284
        return setsockopt(fd, level, opt, (*byte)(unsafe.Pointer(mreq)), Socklen_t(unsafe.Sizeof(*mreq)))
285
}
286
 
287
func SetsockoptIPv6Mreq(fd, level, opt int, mreq *IPv6Mreq) (err error) {
288
        return setsockopt(fd, level, opt, (*byte)(unsafe.Pointer(mreq)), Socklen_t(unsafe.Sizeof(*mreq)))
289
}
290
 
291
func SetsockoptString(fd, level, opt int, s string) (err error) {
292
        return setsockopt(fd, level, opt, (*byte)(unsafe.Pointer(&[]byte(s)[0])), Socklen_t(len(s)))
293
}
294
 
295
//sys   recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *Socklen_t) (n int, err error)
296
//recvfrom(fd int, buf *byte, len Size_t, flags int, from *RawSockaddrAny, fromlen *Socklen_t) Ssize_t
297
 
298
func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) {
299
        var rsa RawSockaddrAny
300
        var len Socklen_t = SizeofSockaddrAny
301
        if n, err = recvfrom(fd, p, flags, &rsa, &len); err != nil {
302
                return
303
        }
304
        from, err = anyToSockaddr(&rsa)
305
        return
306
}
307
 
308
//sys   sendto(s int, buf []byte, flags int, to *RawSockaddrAny, tolen Socklen_t) (err error)
309
//sendto(s int, buf *byte, len Size_t, flags int, to *RawSockaddrAny, tolen Socklen_t) Ssize_t
310
 
311
func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) {
312
        ptr, n, err := to.sockaddr()
313
        if err != nil {
314
                return err
315
        }
316
        return sendto(fd, p, flags, ptr, n)
317
}
318
 
319
//sys   recvmsg(s int, msg *Msghdr, flags int) (n int, err error)
320
//recvmsg(s int, msg *Msghdr, flags int) Ssize_t
321
 
322
func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
323
        var msg Msghdr
324
        var rsa RawSockaddrAny
325
        msg.Name = (*byte)(unsafe.Pointer(&rsa))
326
        msg.Namelen = uint32(SizeofSockaddrAny)
327
        var iov Iovec
328
        if len(p) > 0 {
329
                iov.Base = (*byte)(unsafe.Pointer(&p[0]))
330
                iov.SetLen(len(p))
331
        }
332
        var dummy byte
333
        if len(oob) > 0 {
334
                // receive at least one normal byte
335
                if len(p) == 0 {
336
                        iov.Base = &dummy
337
                        iov.SetLen(1)
338
                }
339
                msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
340
                msg.SetControllen(len(oob))
341
        }
342
        msg.Iov = &iov
343
        msg.Iovlen = 1
344
        if n, err = recvmsg(fd, &msg, flags); err != nil {
345
                return
346
        }
347
        oobn = int(msg.Controllen)
348
        recvflags = int(msg.Flags)
349
        // source address is only specified if the socket is unconnected
350
        if rsa.Addr.Family != AF_UNSPEC {
351
                from, err = anyToSockaddr(&rsa)
352
        }
353
        return
354
}
355
 
356
//sys   sendmsg(s int, msg *Msghdr, flags int) (err error)
357
//sendmsg(s int, msg *Msghdr, flags int) Ssize_t
358
 
359
func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
360
        var ptr *RawSockaddrAny
361
        var salen Socklen_t
362
        if to != nil {
363
                var err error
364
                ptr, salen, err = to.sockaddr()
365
                if err != nil {
366
                        return err
367
                }
368
        }
369
        var msg Msghdr
370
        msg.Name = (*byte)(unsafe.Pointer(ptr))
371
        msg.Namelen = uint32(salen)
372
        var iov Iovec
373
        if len(p) > 0 {
374
                iov.Base = (*byte)(unsafe.Pointer(&p[0]))
375
                iov.SetLen(len(p))
376
        }
377
        var dummy byte
378
        if len(oob) > 0 {
379
                // send at least one normal byte
380
                if len(p) == 0 {
381
                        iov.Base = &dummy
382
                        iov.SetLen(1)
383
                }
384
                msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
385
                msg.SetControllen(len(oob))
386
        }
387
        msg.Iov = &iov
388
        msg.Iovlen = 1
389
        if err = sendmsg(fd, &msg, flags); err != nil {
390
                return
391
        }
392
        return
393
}
394
 
395
//sys   Listen(fd int, n int) (err error)
396
//listen(fd int, n int) int
397
 
398
//sys   Shutdown(fd int, how int) (err error)
399
//shutdown(fd int, how int) int
400
 
401
func (iov *Iovec) SetLen(length int) {
402
        iov.Len = Iovec_len_t(length)
403
}
404
 
405
func (msghdr *Msghdr) SetControllen(length int) {
406
        msghdr.Controllen = Msghdr_controllen_t(length)
407
}
408
 
409
func (cmsg *Cmsghdr) SetLen(length int) {
410
        cmsg.Len = Cmsghdr_len_t(length)
411
}

powered by: WebSVN 2.1.0

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