Commit 28397bef authored by Mikio Hara's avatar Mikio Hara

net: replace error variable name e, errno with err

R=rsc, r
CC=golang-dev
https://golang.org/cl/5593046
parent f8e9bbe4
...@@ -275,20 +275,20 @@ func startServer() { ...@@ -275,20 +275,20 @@ func startServer() {
pollserver = p pollserver = p
} }
func newFD(fd, family, sotype int, net string) (f *netFD, err error) { func newFD(fd, family, sotype int, net string) (*netFD, error) {
onceStartServer.Do(startServer) onceStartServer.Do(startServer)
if e := syscall.SetNonblock(fd, true); e != nil { if err := syscall.SetNonblock(fd, true); err != nil {
return nil, e return nil, err
} }
f = &netFD{ netfd := &netFD{
sysfd: fd, sysfd: fd,
family: family, family: family,
sotype: sotype, sotype: sotype,
net: net, net: net,
} }
f.cr = make(chan bool, 1) netfd.cr = make(chan bool, 1)
f.cw = make(chan bool, 1) netfd.cw = make(chan bool, 1)
return f, nil return netfd, nil
} }
func (fd *netFD) setAddr(laddr, raddr Addr) { func (fd *netFD) setAddr(laddr, raddr Addr) {
...@@ -304,8 +304,8 @@ func (fd *netFD) setAddr(laddr, raddr Addr) { ...@@ -304,8 +304,8 @@ func (fd *netFD) setAddr(laddr, raddr Addr) {
fd.sysfile = os.NewFile(fd.sysfd, fd.net+":"+ls+"->"+rs) fd.sysfile = os.NewFile(fd.sysfd, fd.net+":"+ls+"->"+rs)
} }
func (fd *netFD) connect(ra syscall.Sockaddr) (err error) { func (fd *netFD) connect(ra syscall.Sockaddr) error {
err = syscall.Connect(fd.sysfd, ra) err := syscall.Connect(fd.sysfd, ra)
if err == syscall.EINPROGRESS { if err == syscall.EINPROGRESS {
pollserver.WaitWrite(fd) pollserver.WaitWrite(fd)
var e int var e int
...@@ -466,7 +466,7 @@ func (fd *netFD) ReadMsg(p []byte, oob []byte) (n, oobn, flags int, sa syscall.S ...@@ -466,7 +466,7 @@ func (fd *netFD) ReadMsg(p []byte, oob []byte) (n, oobn, flags int, sa syscall.S
return return
} }
func (fd *netFD) Write(p []byte) (n int, err error) { func (fd *netFD) Write(p []byte) (int, error) {
if fd == nil { if fd == nil {
return 0, os.EINVAL return 0, os.EINVAL
} }
...@@ -477,8 +477,9 @@ func (fd *netFD) Write(p []byte) (n int, err error) { ...@@ -477,8 +477,9 @@ func (fd *netFD) Write(p []byte) (n int, err error) {
if fd.sysfile == nil { if fd.sysfile == nil {
return 0, os.EINVAL return 0, os.EINVAL
} }
nn := 0
var err error
nn := 0
for { for {
var n int var n int
n, err = syscall.Write(fd.sysfile.Fd(), p[nn:]) n, err = syscall.Write(fd.sysfile.Fd(), p[nn:])
...@@ -565,7 +566,7 @@ func (fd *netFD) WriteMsg(p []byte, oob []byte, sa syscall.Sockaddr) (n int, oob ...@@ -565,7 +566,7 @@ func (fd *netFD) WriteMsg(p []byte, oob []byte, sa syscall.Sockaddr) (n int, oob
return return
} }
func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err error) { func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (netfd *netFD, err error) {
if fd == nil || fd.sysfile == nil { if fd == nil || fd.sysfile == nil {
return nil, os.EINVAL return nil, os.EINVAL
} }
...@@ -600,13 +601,13 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err err ...@@ -600,13 +601,13 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err err
syscall.CloseOnExec(s) syscall.CloseOnExec(s)
syscall.ForkLock.RUnlock() syscall.ForkLock.RUnlock()
if nfd, err = newFD(s, fd.family, fd.sotype, fd.net); err != nil { if netfd, err = newFD(s, fd.family, fd.sotype, fd.net); err != nil {
syscall.Close(s) syscall.Close(s)
return nil, err return nil, err
} }
lsa, _ := syscall.Getsockname(nfd.sysfd) lsa, _ := syscall.Getsockname(netfd.sysfd)
nfd.setAddr(toAddr(lsa), toAddr(rsa)) netfd.setAddr(toAddr(lsa), toAddr(rsa))
return nfd, nil return netfd, nil
} }
func (fd *netFD) dup() (f *os.File, err error) { func (fd *netFD) dup() (f *os.File, err error) {
......
...@@ -52,7 +52,7 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) { ...@@ -52,7 +52,7 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) {
} }
syscall.SetKevent(ev, fd, kmode, flags) syscall.SetKevent(ev, fd, kmode, flags)
n, err := syscall.Kevent(p.kq, p.kbuf[0:], p.kbuf[0:], nil) n, err := syscall.Kevent(p.kq, p.kbuf[:], p.kbuf[:], nil)
if err != nil { if err != nil {
return false, os.NewSyscallError("kevent", err) return false, os.NewSyscallError("kevent", err)
} }
...@@ -93,19 +93,19 @@ func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err erro ...@@ -93,19 +93,19 @@ func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err erro
} }
s.Unlock() s.Unlock()
nn, e := syscall.Kevent(p.kq, nil, p.eventbuf[0:], t) n, err := syscall.Kevent(p.kq, nil, p.eventbuf[:], t)
s.Lock() s.Lock()
if e != nil { if err != nil {
if e == syscall.EINTR { if err == syscall.EINTR {
continue continue
} }
return -1, 0, os.NewSyscallError("kevent", nil) return -1, 0, os.NewSyscallError("kevent", nil)
} }
if nn == 0 { if n == 0 {
return -1, 0, nil return -1, 0, nil
} }
p.events = p.eventbuf[0:nn] p.events = p.eventbuf[:n]
} }
ev := &p.events[0] ev := &p.events[0]
p.events = p.events[1:] p.events = p.events[1:]
......
...@@ -49,12 +49,12 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) { ...@@ -49,12 +49,12 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) {
} }
syscall.SetKevent(ev, fd, kmode, flags) syscall.SetKevent(ev, fd, kmode, flags)
n, e := syscall.Kevent(p.kq, p.kbuf[:], nil, nil) n, err := syscall.Kevent(p.kq, p.kbuf[:], nil, nil)
if e != nil { if err != nil {
return false, os.NewSyscallError("kevent", e) return false, os.NewSyscallError("kevent", err)
} }
if n != 1 || (ev.Flags&syscall.EV_ERROR) == 0 || int(ev.Ident) != fd || int(ev.Filter) != kmode { if n != 1 || (ev.Flags&syscall.EV_ERROR) == 0 || int(ev.Ident) != fd || int(ev.Filter) != kmode {
return false, os.NewSyscallError("kqueue phase error", e) return false, os.NewSyscallError("kqueue phase error", err)
} }
if ev.Data != 0 { if ev.Data != 0 {
return false, syscall.Errno(int(ev.Data)) return false, syscall.Errno(int(ev.Data))
...@@ -88,19 +88,19 @@ func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err erro ...@@ -88,19 +88,19 @@ func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err erro
} }
s.Unlock() s.Unlock()
nn, e := syscall.Kevent(p.kq, nil, p.eventbuf[:], t) n, err := syscall.Kevent(p.kq, nil, p.eventbuf[:], t)
s.Lock() s.Lock()
if e != nil { if err != nil {
if e == syscall.EINTR { if err == syscall.EINTR {
continue continue
} }
return -1, 0, os.NewSyscallError("kevent", e) return -1, 0, os.NewSyscallError("kevent", err)
} }
if nn == 0 { if n == 0 {
return -1, 0, nil return -1, 0, nil
} }
p.events = p.eventbuf[0:nn] p.events = p.eventbuf[:n]
} }
ev := &p.events[0] ev := &p.events[0]
p.events = p.events[1:] p.events = p.events[1:]
......
...@@ -35,17 +35,15 @@ type pollster struct { ...@@ -35,17 +35,15 @@ type pollster struct {
func newpollster() (p *pollster, err error) { func newpollster() (p *pollster, err error) {
p = new(pollster) p = new(pollster)
var e error if p.epfd, err = syscall.EpollCreate1(syscall.EPOLL_CLOEXEC); err != nil {
if err != syscall.ENOSYS {
if p.epfd, e = syscall.EpollCreate1(syscall.EPOLL_CLOEXEC); e != nil { return nil, os.NewSyscallError("epoll_create1", err)
if e != syscall.ENOSYS {
return nil, os.NewSyscallError("epoll_create1", e)
} }
// The arg to epoll_create is a hint to the kernel // The arg to epoll_create is a hint to the kernel
// about the number of FDs we will care about. // about the number of FDs we will care about.
// We don't know, and since 2.6.8 the kernel ignores it anyhow. // We don't know, and since 2.6.8 the kernel ignores it anyhow.
if p.epfd, e = syscall.EpollCreate(16); e != nil { if p.epfd, err = syscall.EpollCreate(16); err != nil {
return nil, os.NewSyscallError("epoll_create", e) return nil, os.NewSyscallError("epoll_create", err)
} }
syscall.CloseOnExec(p.epfd) syscall.CloseOnExec(p.epfd)
} }
...@@ -74,8 +72,8 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) { ...@@ -74,8 +72,8 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) {
} else { } else {
op = syscall.EPOLL_CTL_ADD op = syscall.EPOLL_CTL_ADD
} }
if e := syscall.EpollCtl(p.epfd, op, fd, &p.ctlEvent); e != nil { if err := syscall.EpollCtl(p.epfd, op, fd, &p.ctlEvent); err != nil {
return false, os.NewSyscallError("epoll_ctl", e) return false, os.NewSyscallError("epoll_ctl", err)
} }
p.events[fd] = p.ctlEvent.Events p.events[fd] = p.ctlEvent.Events
return false, nil return false, nil
...@@ -103,13 +101,13 @@ func (p *pollster) StopWaiting(fd int, bits uint) { ...@@ -103,13 +101,13 @@ func (p *pollster) StopWaiting(fd int, bits uint) {
if int32(events)&^syscall.EPOLLONESHOT != 0 { if int32(events)&^syscall.EPOLLONESHOT != 0 {
p.ctlEvent.Fd = int32(fd) p.ctlEvent.Fd = int32(fd)
p.ctlEvent.Events = events p.ctlEvent.Events = events
if e := syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_MOD, fd, &p.ctlEvent); e != nil { if err := syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_MOD, fd, &p.ctlEvent); err != nil {
print("Epoll modify fd=", fd, ": ", e.Error(), "\n") print("Epoll modify fd=", fd, ": ", err.Error(), "\n")
} }
p.events[fd] = events p.events[fd] = events
} else { } else {
if e := syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_DEL, fd, nil); e != nil { if err := syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_DEL, fd, nil); err != nil {
print("Epoll delete fd=", fd, ": ", e.Error(), "\n") print("Epoll delete fd=", fd, ": ", err.Error(), "\n")
} }
delete(p.events, fd) delete(p.events, fd)
} }
...@@ -144,14 +142,14 @@ func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err erro ...@@ -144,14 +142,14 @@ func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err erro
} }
s.Unlock() s.Unlock()
n, e := syscall.EpollWait(p.epfd, p.waitEventBuf[0:], msec) n, err := syscall.EpollWait(p.epfd, p.waitEventBuf[0:], msec)
s.Lock() s.Lock()
if e != nil { if err != nil {
if e == syscall.EAGAIN || e == syscall.EINTR { if err == syscall.EAGAIN || err == syscall.EINTR {
continue continue
} }
return -1, 0, os.NewSyscallError("epoll_wait", e) return -1, 0, os.NewSyscallError("epoll_wait", err)
} }
if n == 0 { if n == 0 {
return -1, 0, nil return -1, 0, nil
......
...@@ -49,12 +49,12 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) { ...@@ -49,12 +49,12 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) {
} }
syscall.SetKevent(ev, fd, kmode, flags) syscall.SetKevent(ev, fd, kmode, flags)
n, e := syscall.Kevent(p.kq, p.kbuf[:], nil, nil) n, err := syscall.Kevent(p.kq, p.kbuf[:], nil, nil)
if e != nil { if err != nil {
return false, os.NewSyscallError("kevent", e) return false, os.NewSyscallError("kevent", err)
} }
if n != 1 || (ev.Flags&syscall.EV_ERROR) == 0 || int(ev.Ident) != fd || int(ev.Filter) != kmode { if n != 1 || (ev.Flags&syscall.EV_ERROR) == 0 || int(ev.Ident) != fd || int(ev.Filter) != kmode {
return false, os.NewSyscallError("kqueue phase error", e) return false, os.NewSyscallError("kqueue phase error", err)
} }
if ev.Data != 0 { if ev.Data != 0 {
return false, syscall.Errno(int(ev.Data)) return false, syscall.Errno(int(ev.Data))
...@@ -88,19 +88,19 @@ func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err erro ...@@ -88,19 +88,19 @@ func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err erro
} }
s.Unlock() s.Unlock()
nn, e := syscall.Kevent(p.kq, nil, p.eventbuf[:], t) n, err := syscall.Kevent(p.kq, nil, p.eventbuf[:], t)
s.Lock() s.Lock()
if e != nil { if err != nil {
if e == syscall.EINTR { if err == syscall.EINTR {
continue continue
} }
return -1, 0, os.NewSyscallError("kevent", e) return -1, 0, os.NewSyscallError("kevent", err)
} }
if nn == 0 { if n == 0 {
return -1, 0, nil return -1, 0, nil
} }
p.events = p.eventbuf[0:nn] p.events = p.eventbuf[:n]
} }
ev := &p.events[0] ev := &p.events[0]
p.events = p.events[1:] p.events = p.events[1:]
......
...@@ -49,12 +49,12 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) { ...@@ -49,12 +49,12 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) {
} }
syscall.SetKevent(ev, fd, kmode, flags) syscall.SetKevent(ev, fd, kmode, flags)
n, e := syscall.Kevent(p.kq, p.kbuf[:], nil, nil) n, err := syscall.Kevent(p.kq, p.kbuf[:], nil, nil)
if e != nil { if err != nil {
return false, os.NewSyscallError("kevent", e) return false, os.NewSyscallError("kevent", err)
} }
if n != 1 || (ev.Flags&syscall.EV_ERROR) == 0 || int(ev.Ident) != fd || int(ev.Filter) != kmode { if n != 1 || (ev.Flags&syscall.EV_ERROR) == 0 || int(ev.Ident) != fd || int(ev.Filter) != kmode {
return false, os.NewSyscallError("kqueue phase error", e) return false, os.NewSyscallError("kqueue phase error", err)
} }
if ev.Data != 0 { if ev.Data != 0 {
return false, syscall.Errno(int(ev.Data)) return false, syscall.Errno(int(ev.Data))
...@@ -88,19 +88,19 @@ func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err erro ...@@ -88,19 +88,19 @@ func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err erro
} }
s.Unlock() s.Unlock()
nn, e := syscall.Kevent(p.kq, nil, p.eventbuf[:], t) n, err := syscall.Kevent(p.kq, nil, p.eventbuf[:], t)
s.Lock() s.Lock()
if e != nil { if err != nil {
if e == syscall.EINTR { if err == syscall.EINTR {
continue continue
} }
return -1, 0, os.NewSyscallError("kevent", e) return -1, 0, os.NewSyscallError("kevent", err)
} }
if nn == 0 { if n == 0 {
return -1, 0, nil return -1, 0, nil
} }
p.events = p.eventbuf[0:nn] p.events = p.eventbuf[:n]
} }
ev := &p.events[0] ev := &p.events[0]
p.events = p.events[1:] p.events = p.events[1:]
......
...@@ -30,7 +30,7 @@ func init() { ...@@ -30,7 +30,7 @@ func init() {
} }
} }
func closesocket(s syscall.Handle) (err error) { func closesocket(s syscall.Handle) error {
return syscall.Closesocket(s) return syscall.Closesocket(s)
} }
...@@ -38,7 +38,7 @@ func closesocket(s syscall.Handle) (err error) { ...@@ -38,7 +38,7 @@ func closesocket(s syscall.Handle) (err error) {
type anOpIface interface { type anOpIface interface {
Op() *anOp Op() *anOp
Name() string Name() string
Submit() (err error) Submit() error
} }
// IO completion result parameters. // IO completion result parameters.
...@@ -153,24 +153,24 @@ func (s *ioSrv) ProcessRemoteIO() { ...@@ -153,24 +153,24 @@ func (s *ioSrv) ProcessRemoteIO() {
// inline, or, if a deadline is employed, passes the request onto // inline, or, if a deadline is employed, passes the request onto
// a special goroutine and waits for completion or cancels request. // a special goroutine and waits for completion or cancels request.
// deadline is unix nanos. // deadline is unix nanos.
func (s *ioSrv) ExecIO(oi anOpIface, deadline int64) (n int, err error) { func (s *ioSrv) ExecIO(oi anOpIface, deadline int64) (int, error) {
var e error var err error
o := oi.Op() o := oi.Op()
if deadline != 0 { if deadline != 0 {
// Send request to a special dedicated thread, // Send request to a special dedicated thread,
// so it can stop the io with CancelIO later. // so it can stop the io with CancelIO later.
s.submchan <- oi s.submchan <- oi
e = <-o.errnoc err = <-o.errnoc
} else { } else {
e = oi.Submit() err = oi.Submit()
} }
switch e { switch err {
case nil: case nil:
// IO completed immediately, but we need to get our completion message anyway. // IO completed immediately, but we need to get our completion message anyway.
case syscall.ERROR_IO_PENDING: case syscall.ERROR_IO_PENDING:
// IO started, and we have to wait for its completion. // IO started, and we have to wait for its completion.
default: default:
return 0, &OpError{oi.Name(), o.fd.net, o.fd.laddr, e} return 0, &OpError{oi.Name(), o.fd.net, o.fd.laddr, err}
} }
// Wait for our request to complete. // Wait for our request to complete.
var r ioResult var r ioResult
...@@ -245,25 +245,25 @@ type netFD struct { ...@@ -245,25 +245,25 @@ type netFD struct {
wio sync.Mutex wio sync.Mutex
} }
func allocFD(fd syscall.Handle, family, sotype int, net string) (f *netFD) { func allocFD(fd syscall.Handle, family, sotype int, net string) *netFD {
f = &netFD{ netfd := &netFD{
sysfd: fd, sysfd: fd,
family: family, family: family,
sotype: sotype, sotype: sotype,
net: net, net: net,
} }
runtime.SetFinalizer(f, (*netFD).Close) runtime.SetFinalizer(netfd, (*netFD).Close)
return f return netfd
} }
func newFD(fd syscall.Handle, family, proto int, net string) (f *netFD, err error) { func newFD(fd syscall.Handle, family, proto int, net string) (*netFD, error) {
if initErr != nil { if initErr != nil {
return nil, initErr return nil, initErr
} }
onceStartServer.Do(startServer) onceStartServer.Do(startServer)
// Associate our socket with resultsrv.iocp. // Associate our socket with resultsrv.iocp.
if _, e := syscall.CreateIoCompletionPort(syscall.Handle(fd), resultsrv.iocp, 0, 0); e != nil { if _, err := syscall.CreateIoCompletionPort(syscall.Handle(fd), resultsrv.iocp, 0, 0); err != nil {
return nil, e return nil, err
} }
return allocFD(fd, family, proto, net), nil return allocFD(fd, family, proto, net), nil
} }
...@@ -273,7 +273,7 @@ func (fd *netFD) setAddr(laddr, raddr Addr) { ...@@ -273,7 +273,7 @@ func (fd *netFD) setAddr(laddr, raddr Addr) {
fd.raddr = raddr fd.raddr = raddr
} }
func (fd *netFD) connect(ra syscall.Sockaddr) (err error) { func (fd *netFD) connect(ra syscall.Sockaddr) error {
return syscall.Connect(fd.sysfd, ra) return syscall.Connect(fd.sysfd, ra)
} }
...@@ -340,7 +340,7 @@ type readOp struct { ...@@ -340,7 +340,7 @@ type readOp struct {
bufOp bufOp
} }
func (o *readOp) Submit() (err error) { func (o *readOp) Submit() error {
var d, f uint32 var d, f uint32
return syscall.WSARecv(syscall.Handle(o.fd.sysfd), &o.buf, 1, &d, &f, &o.o, nil) return syscall.WSARecv(syscall.Handle(o.fd.sysfd), &o.buf, 1, &d, &f, &o.o, nil)
} }
...@@ -349,7 +349,7 @@ func (o *readOp) Name() string { ...@@ -349,7 +349,7 @@ func (o *readOp) Name() string {
return "WSARecv" return "WSARecv"
} }
func (fd *netFD) Read(buf []byte) (n int, err error) { func (fd *netFD) Read(buf []byte) (int, error) {
if fd == nil { if fd == nil {
return 0, os.EINVAL return 0, os.EINVAL
} }
...@@ -362,11 +362,11 @@ func (fd *netFD) Read(buf []byte) (n int, err error) { ...@@ -362,11 +362,11 @@ func (fd *netFD) Read(buf []byte) (n int, err error) {
} }
var o readOp var o readOp
o.Init(fd, buf, 'r') o.Init(fd, buf, 'r')
n, err = iosrv.ExecIO(&o, fd.rdeadline) n, err := iosrv.ExecIO(&o, fd.rdeadline)
if err == nil && n == 0 { if err == nil && n == 0 {
err = io.EOF err = io.EOF
} }
return return n, err
} }
// ReadFrom from network. // ReadFrom from network.
...@@ -377,7 +377,7 @@ type readFromOp struct { ...@@ -377,7 +377,7 @@ type readFromOp struct {
rsan int32 rsan int32
} }
func (o *readFromOp) Submit() (err error) { func (o *readFromOp) Submit() error {
var d, f uint32 var d, f uint32
return syscall.WSARecvFrom(o.fd.sysfd, &o.buf, 1, &d, &f, &o.rsa, &o.rsan, &o.o, nil) return syscall.WSARecvFrom(o.fd.sysfd, &o.buf, 1, &d, &f, &o.rsa, &o.rsan, &o.o, nil)
} }
...@@ -417,7 +417,7 @@ type writeOp struct { ...@@ -417,7 +417,7 @@ type writeOp struct {
bufOp bufOp
} }
func (o *writeOp) Submit() (err error) { func (o *writeOp) Submit() error {
var d uint32 var d uint32
return syscall.WSASend(o.fd.sysfd, &o.buf, 1, &d, 0, &o.o, nil) return syscall.WSASend(o.fd.sysfd, &o.buf, 1, &d, 0, &o.o, nil)
} }
...@@ -426,7 +426,7 @@ func (o *writeOp) Name() string { ...@@ -426,7 +426,7 @@ func (o *writeOp) Name() string {
return "WSASend" return "WSASend"
} }
func (fd *netFD) Write(buf []byte) (n int, err error) { func (fd *netFD) Write(buf []byte) (int, error) {
if fd == nil { if fd == nil {
return 0, os.EINVAL return 0, os.EINVAL
} }
...@@ -449,7 +449,7 @@ type writeToOp struct { ...@@ -449,7 +449,7 @@ type writeToOp struct {
sa syscall.Sockaddr sa syscall.Sockaddr
} }
func (o *writeToOp) Submit() (err error) { func (o *writeToOp) Submit() error {
var d uint32 var d uint32
return syscall.WSASendto(o.fd.sysfd, &o.buf, 1, &d, 0, o.sa, &o.o, nil) return syscall.WSASendto(o.fd.sysfd, &o.buf, 1, &d, 0, o.sa, &o.o, nil)
} }
...@@ -458,7 +458,7 @@ func (o *writeToOp) Name() string { ...@@ -458,7 +458,7 @@ func (o *writeToOp) Name() string {
return "WSASendto" return "WSASendto"
} }
func (fd *netFD) WriteTo(buf []byte, sa syscall.Sockaddr) (n int, err error) { func (fd *netFD) WriteTo(buf []byte, sa syscall.Sockaddr) (int, error) {
if fd == nil { if fd == nil {
return 0, os.EINVAL return 0, os.EINVAL
} }
...@@ -486,7 +486,7 @@ type acceptOp struct { ...@@ -486,7 +486,7 @@ type acceptOp struct {
attrs [2]syscall.RawSockaddrAny // space for local and remote address only attrs [2]syscall.RawSockaddrAny // space for local and remote address only
} }
func (o *acceptOp) Submit() (err error) { func (o *acceptOp) Submit() error {
var d uint32 var d uint32
l := uint32(unsafe.Sizeof(o.attrs[0])) l := uint32(unsafe.Sizeof(o.attrs[0]))
return syscall.AcceptEx(o.fd.sysfd, o.newsock, return syscall.AcceptEx(o.fd.sysfd, o.newsock,
...@@ -497,7 +497,7 @@ func (o *acceptOp) Name() string { ...@@ -497,7 +497,7 @@ func (o *acceptOp) Name() string {
return "AcceptEx" return "AcceptEx"
} }
func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err error) { func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (*netFD, error) {
if fd == nil || fd.sysfd == syscall.InvalidHandle { if fd == nil || fd.sysfd == syscall.InvalidHandle {
return nil, os.EINVAL return nil, os.EINVAL
} }
...@@ -507,18 +507,18 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err err ...@@ -507,18 +507,18 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err err
// Get new socket. // Get new socket.
// See ../syscall/exec.go for description of ForkLock. // See ../syscall/exec.go for description of ForkLock.
syscall.ForkLock.RLock() syscall.ForkLock.RLock()
s, e := syscall.Socket(fd.family, fd.sotype, 0) s, err := syscall.Socket(fd.family, fd.sotype, 0)
if e != nil { if err != nil {
syscall.ForkLock.RUnlock() syscall.ForkLock.RUnlock()
return nil, e return nil, err
} }
syscall.CloseOnExec(s) syscall.CloseOnExec(s)
syscall.ForkLock.RUnlock() syscall.ForkLock.RUnlock()
// Associate our new socket with IOCP. // Associate our new socket with IOCP.
onceStartServer.Do(startServer) onceStartServer.Do(startServer)
if _, e = syscall.CreateIoCompletionPort(s, resultsrv.iocp, 0, 0); e != nil { if _, err := syscall.CreateIoCompletionPort(s, resultsrv.iocp, 0, 0); err != nil {
return nil, &OpError{"CreateIoCompletionPort", fd.net, fd.laddr, e} return nil, &OpError{"CreateIoCompletionPort", fd.net, fd.laddr, err}
} }
// Submit accept request. // Submit accept request.
...@@ -532,10 +532,10 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err err ...@@ -532,10 +532,10 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err err
} }
// Inherit properties of the listening socket. // Inherit properties of the listening socket.
e = syscall.Setsockopt(s, syscall.SOL_SOCKET, syscall.SO_UPDATE_ACCEPT_CONTEXT, (*byte)(unsafe.Pointer(&fd.sysfd)), int32(unsafe.Sizeof(fd.sysfd))) err = syscall.Setsockopt(s, syscall.SOL_SOCKET, syscall.SO_UPDATE_ACCEPT_CONTEXT, (*byte)(unsafe.Pointer(&fd.sysfd)), int32(unsafe.Sizeof(fd.sysfd)))
if e != nil { if err != nil {
closesocket(s) closesocket(s)
return nil, e return nil, err
} }
// Get local and peer addr out of AcceptEx buffer. // Get local and peer addr out of AcceptEx buffer.
...@@ -547,14 +547,14 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err err ...@@ -547,14 +547,14 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err err
lsa, _ := lrsa.Sockaddr() lsa, _ := lrsa.Sockaddr()
rsa, _ := rrsa.Sockaddr() rsa, _ := rrsa.Sockaddr()
nfd = allocFD(s, fd.family, fd.sotype, fd.net) netfd := allocFD(s, fd.family, fd.sotype, fd.net)
nfd.setAddr(toAddr(lsa), toAddr(rsa)) netfd.setAddr(toAddr(lsa), toAddr(rsa))
return nfd, nil return netfd, nil
} }
// Unimplemented functions. // Unimplemented functions.
func (fd *netFD) dup() (f *os.File, err error) { func (fd *netFD) dup() (*os.File, error) {
// TODO: Implement this // TODO: Implement this
return nil, os.NewSyscallError("dup", syscall.EWINDOWS) return nil, os.NewSyscallError("dup", syscall.EWINDOWS)
} }
......
...@@ -11,15 +11,15 @@ import ( ...@@ -11,15 +11,15 @@ import (
"syscall" "syscall"
) )
func newFileFD(f *os.File) (nfd *netFD, err error) { func newFileFD(f *os.File) (*netFD, error) {
fd, errno := syscall.Dup(f.Fd()) fd, err := syscall.Dup(f.Fd())
if errno != nil { if err != nil {
return nil, os.NewSyscallError("dup", errno) return nil, os.NewSyscallError("dup", err)
} }
proto, errno := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE) proto, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE)
if errno != nil { if err != nil {
return nil, os.NewSyscallError("getsockopt", errno) return nil, os.NewSyscallError("getsockopt", err)
} }
family := syscall.AF_UNSPEC family := syscall.AF_UNSPEC
...@@ -56,11 +56,12 @@ func newFileFD(f *os.File) (nfd *netFD, err error) { ...@@ -56,11 +56,12 @@ func newFileFD(f *os.File) (nfd *netFD, err error) {
sa, _ = syscall.Getpeername(fd) sa, _ = syscall.Getpeername(fd)
raddr := toAddr(sa) raddr := toAddr(sa)
if nfd, err = newFD(fd, family, proto, laddr.Network()); err != nil { netfd, err := newFD(fd, family, proto, laddr.Network())
if err != nil {
return nil, err return nil, err
} }
nfd.setAddr(laddr, raddr) netfd.setAddr(laddr, raddr)
return nfd, nil return netfd, nil
} }
// FileConn returns a copy of the network connection corresponding to // FileConn returns a copy of the network connection corresponding to
......
...@@ -15,23 +15,17 @@ import ( ...@@ -15,23 +15,17 @@ import (
// addresses for all network interfaces. Otherwise it returns // addresses for all network interfaces. Otherwise it returns
// addresses for a specific interface. // addresses for a specific interface.
func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) { func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) {
var ( tab, err := syscall.RouteRIB(syscall.NET_RT_IFLIST2, ifindex)
tab []byte if err != nil {
e error return nil, os.NewSyscallError("route rib", err)
msgs []syscall.RoutingMessage
ifmat []Addr
)
tab, e = syscall.RouteRIB(syscall.NET_RT_IFLIST2, ifindex)
if e != nil {
return nil, os.NewSyscallError("route rib", e)
} }
msgs, e = syscall.ParseRoutingMessage(tab) msgs, err := syscall.ParseRoutingMessage(tab)
if e != nil { if err != nil {
return nil, os.NewSyscallError("route message", e) return nil, os.NewSyscallError("route message", err)
} }
var ifmat []Addr
for _, m := range msgs { for _, m := range msgs {
switch v := m.(type) { switch v := m.(type) {
case *syscall.InterfaceMulticastAddrMessage: case *syscall.InterfaceMulticastAddrMessage:
...@@ -44,18 +38,16 @@ func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) { ...@@ -44,18 +38,16 @@ func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) {
} }
} }
} }
return ifmat, nil return ifmat, nil
} }
func newMulticastAddr(m *syscall.InterfaceMulticastAddrMessage) ([]Addr, error) { func newMulticastAddr(m *syscall.InterfaceMulticastAddrMessage) ([]Addr, error) {
var ifmat []Addr sas, err := syscall.ParseRoutingSockaddr(m)
if err != nil {
sas, e := syscall.ParseRoutingSockaddr(m) return nil, os.NewSyscallError("route sockaddr", err)
if e != nil {
return nil, os.NewSyscallError("route sockaddr", e)
} }
var ifmat []Addr
for _, s := range sas { for _, s := range sas {
switch v := s.(type) { switch v := s.(type) {
case *syscall.SockaddrInet4: case *syscall.SockaddrInet4:
...@@ -75,6 +67,5 @@ func newMulticastAddr(m *syscall.InterfaceMulticastAddrMessage) ([]Addr, error) ...@@ -75,6 +67,5 @@ func newMulticastAddr(m *syscall.InterfaceMulticastAddrMessage) ([]Addr, error)
ifmat = append(ifmat, ifma.toAddr()) ifmat = append(ifmat, ifma.toAddr())
} }
} }
return ifmat, nil return ifmat, nil
} }
...@@ -15,23 +15,17 @@ import ( ...@@ -15,23 +15,17 @@ import (
// addresses for all network interfaces. Otherwise it returns // addresses for all network interfaces. Otherwise it returns
// addresses for a specific interface. // addresses for a specific interface.
func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) { func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) {
var ( tab, err := syscall.RouteRIB(syscall.NET_RT_IFMALIST, ifindex)
tab []byte if err != nil {
e error return nil, os.NewSyscallError("route rib", err)
msgs []syscall.RoutingMessage
ifmat []Addr
)
tab, e = syscall.RouteRIB(syscall.NET_RT_IFMALIST, ifindex)
if e != nil {
return nil, os.NewSyscallError("route rib", e)
} }
msgs, e = syscall.ParseRoutingMessage(tab) msgs, err := syscall.ParseRoutingMessage(tab)
if e != nil { if err != nil {
return nil, os.NewSyscallError("route message", e) return nil, os.NewSyscallError("route message", err)
} }
var ifmat []Addr
for _, m := range msgs { for _, m := range msgs {
switch v := m.(type) { switch v := m.(type) {
case *syscall.InterfaceMulticastAddrMessage: case *syscall.InterfaceMulticastAddrMessage:
...@@ -44,18 +38,16 @@ func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) { ...@@ -44,18 +38,16 @@ func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) {
} }
} }
} }
return ifmat, nil return ifmat, nil
} }
func newMulticastAddr(m *syscall.InterfaceMulticastAddrMessage) ([]Addr, error) { func newMulticastAddr(m *syscall.InterfaceMulticastAddrMessage) ([]Addr, error) {
var ifmat []Addr sas, err := syscall.ParseRoutingSockaddr(m)
if err != nil {
sas, e := syscall.ParseRoutingSockaddr(m) return nil, os.NewSyscallError("route sockaddr", err)
if e != nil {
return nil, os.NewSyscallError("route sockaddr", e)
} }
var ifmat []Addr
for _, s := range sas { for _, s := range sas {
switch v := s.(type) { switch v := s.(type) {
case *syscall.SockaddrInet4: case *syscall.SockaddrInet4:
...@@ -75,6 +67,5 @@ func newMulticastAddr(m *syscall.InterfaceMulticastAddrMessage) ([]Addr, error) ...@@ -75,6 +67,5 @@ func newMulticastAddr(m *syscall.InterfaceMulticastAddrMessage) ([]Addr, error)
ifmat = append(ifmat, ifma.toAddr()) ifmat = append(ifmat, ifma.toAddr())
} }
} }
return ifmat, nil return ifmat, nil
} }
...@@ -17,8 +17,6 @@ import ( ...@@ -17,8 +17,6 @@ import (
// network interfaces. Otheriwse it returns a mapping of a specific // network interfaces. Otheriwse it returns a mapping of a specific
// interface. // interface.
func interfaceTable(ifindex int) ([]Interface, error) { func interfaceTable(ifindex int) ([]Interface, error) {
var ift []Interface
tab, err := syscall.NetlinkRIB(syscall.RTM_GETLINK, syscall.AF_UNSPEC) tab, err := syscall.NetlinkRIB(syscall.RTM_GETLINK, syscall.AF_UNSPEC)
if err != nil { if err != nil {
return nil, os.NewSyscallError("netlink rib", err) return nil, os.NewSyscallError("netlink rib", err)
...@@ -29,6 +27,7 @@ func interfaceTable(ifindex int) ([]Interface, error) { ...@@ -29,6 +27,7 @@ func interfaceTable(ifindex int) ([]Interface, error) {
return nil, os.NewSyscallError("netlink message", err) return nil, os.NewSyscallError("netlink message", err)
} }
var ift []Interface
for _, m := range msgs { for _, m := range msgs {
switch m.Header.Type { switch m.Header.Type {
case syscall.NLMSG_DONE: case syscall.NLMSG_DONE:
...@@ -45,7 +44,6 @@ func interfaceTable(ifindex int) ([]Interface, error) { ...@@ -45,7 +44,6 @@ func interfaceTable(ifindex int) ([]Interface, error) {
} }
} }
} }
done: done:
return ift, nil return ift, nil
} }
...@@ -111,13 +109,11 @@ func interfaceAddrTable(ifindex int) ([]Addr, error) { ...@@ -111,13 +109,11 @@ func interfaceAddrTable(ifindex int) ([]Addr, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return ifat, nil return ifat, nil
} }
func addrTable(msgs []syscall.NetlinkMessage, ifindex int) ([]Addr, error) { func addrTable(msgs []syscall.NetlinkMessage, ifindex int) ([]Addr, error) {
var ifat []Addr var ifat []Addr
for _, m := range msgs { for _, m := range msgs {
switch m.Header.Type { switch m.Header.Type {
case syscall.NLMSG_DONE: case syscall.NLMSG_DONE:
...@@ -133,7 +129,6 @@ func addrTable(msgs []syscall.NetlinkMessage, ifindex int) ([]Addr, error) { ...@@ -133,7 +129,6 @@ func addrTable(msgs []syscall.NetlinkMessage, ifindex int) ([]Addr, error) {
} }
} }
} }
done: done:
return ifat, nil return ifat, nil
} }
...@@ -165,32 +160,28 @@ func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) { ...@@ -165,32 +160,28 @@ func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) {
err error err error
ifi *Interface ifi *Interface
) )
if ifindex > 0 { if ifindex > 0 {
ifi, err = InterfaceByIndex(ifindex) ifi, err = InterfaceByIndex(ifindex)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
ifmat4 := parseProcNetIGMP(ifi) ifmat4 := parseProcNetIGMP(ifi)
ifmat6 := parseProcNetIGMP6(ifi) ifmat6 := parseProcNetIGMP6(ifi)
return append(ifmat4, ifmat6...), nil return append(ifmat4, ifmat6...), nil
} }
func parseProcNetIGMP(ifi *Interface) []Addr { func parseProcNetIGMP(ifi *Interface) []Addr {
var (
ifmat []Addr
name string
)
fd, err := open("/proc/net/igmp") fd, err := open("/proc/net/igmp")
if err != nil { if err != nil {
return nil return nil
} }
defer fd.close() defer fd.close()
var (
ifmat []Addr
name string
)
fd.readLine() // skip first line fd.readLine() // skip first line
b := make([]byte, IPv4len) b := make([]byte, IPv4len)
for l, ok := fd.readLine(); ok; l, ok = fd.readLine() { for l, ok := fd.readLine(); ok; l, ok = fd.readLine() {
...@@ -206,19 +197,17 @@ func parseProcNetIGMP(ifi *Interface) []Addr { ...@@ -206,19 +197,17 @@ func parseProcNetIGMP(ifi *Interface) []Addr {
name = f[1] name = f[1]
} }
} }
return ifmat return ifmat
} }
func parseProcNetIGMP6(ifi *Interface) []Addr { func parseProcNetIGMP6(ifi *Interface) []Addr {
var ifmat []Addr
fd, err := open("/proc/net/igmp6") fd, err := open("/proc/net/igmp6")
if err != nil { if err != nil {
return nil return nil
} }
defer fd.close() defer fd.close()
var ifmat []Addr
b := make([]byte, IPv6len) b := make([]byte, IPv6len)
for l, ok := fd.readLine(); ok; l, ok = fd.readLine() { for l, ok := fd.readLine(); ok; l, ok = fd.readLine() {
f := getFields(l) f := getFields(l)
...@@ -229,6 +218,5 @@ func parseProcNetIGMP6(ifi *Interface) []Addr { ...@@ -229,6 +218,5 @@ func parseProcNetIGMP6(ifi *Interface) []Addr {
} }
} }
return ifmat return ifmat
} }
...@@ -25,31 +25,31 @@ func getAdapterList() (*syscall.IpAdapterInfo, error) { ...@@ -25,31 +25,31 @@ func getAdapterList() (*syscall.IpAdapterInfo, error) {
b := make([]byte, 1000) b := make([]byte, 1000)
l := uint32(len(b)) l := uint32(len(b))
a := (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0])) a := (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0]))
e := syscall.GetAdaptersInfo(a, &l) err := syscall.GetAdaptersInfo(a, &l)
if e == syscall.ERROR_BUFFER_OVERFLOW { if err == syscall.ERROR_BUFFER_OVERFLOW {
b = make([]byte, l) b = make([]byte, l)
a = (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0])) a = (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0]))
e = syscall.GetAdaptersInfo(a, &l) err = syscall.GetAdaptersInfo(a, &l)
} }
if e != nil { if err != nil {
return nil, os.NewSyscallError("GetAdaptersInfo", e) return nil, os.NewSyscallError("GetAdaptersInfo", err)
} }
return a, nil return a, nil
} }
func getInterfaceList() ([]syscall.InterfaceInfo, error) { func getInterfaceList() ([]syscall.InterfaceInfo, error) {
s, e := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP) s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
if e != nil { if err != nil {
return nil, os.NewSyscallError("Socket", e) return nil, os.NewSyscallError("Socket", err)
} }
defer syscall.Closesocket(s) defer syscall.Closesocket(s)
ii := [20]syscall.InterfaceInfo{} ii := [20]syscall.InterfaceInfo{}
ret := uint32(0) ret := uint32(0)
size := uint32(unsafe.Sizeof(ii)) size := uint32(unsafe.Sizeof(ii))
e = syscall.WSAIoctl(s, syscall.SIO_GET_INTERFACE_LIST, nil, 0, (*byte)(unsafe.Pointer(&ii[0])), size, &ret, nil, 0) err = syscall.WSAIoctl(s, syscall.SIO_GET_INTERFACE_LIST, nil, 0, (*byte)(unsafe.Pointer(&ii[0])), size, &ret, nil, 0)
if e != nil { if err != nil {
return nil, os.NewSyscallError("WSAIoctl", e) return nil, os.NewSyscallError("WSAIoctl", err)
} }
c := ret / uint32(unsafe.Sizeof(ii[0])) c := ret / uint32(unsafe.Sizeof(ii[0]))
return ii[:c-1], nil return ii[:c-1], nil
...@@ -59,14 +59,14 @@ func getInterfaceList() ([]syscall.InterfaceInfo, error) { ...@@ -59,14 +59,14 @@ func getInterfaceList() ([]syscall.InterfaceInfo, error) {
// network interfaces. Otheriwse it returns a mapping of a specific // network interfaces. Otheriwse it returns a mapping of a specific
// interface. // interface.
func interfaceTable(ifindex int) ([]Interface, error) { func interfaceTable(ifindex int) ([]Interface, error) {
ai, e := getAdapterList() ai, err := getAdapterList()
if e != nil { if err != nil {
return nil, e return nil, err
} }
ii, e := getInterfaceList() ii, err := getInterfaceList()
if e != nil { if err != nil {
return nil, e return nil, err
} }
var ift []Interface var ift []Interface
...@@ -130,9 +130,9 @@ func interfaceTable(ifindex int) ([]Interface, error) { ...@@ -130,9 +130,9 @@ func interfaceTable(ifindex int) ([]Interface, error) {
// for all network interfaces. Otherwise it returns addresses // for all network interfaces. Otherwise it returns addresses
// for a specific interface. // for a specific interface.
func interfaceAddrTable(ifindex int) ([]Addr, error) { func interfaceAddrTable(ifindex int) ([]Addr, error) {
ai, e := getAdapterList() ai, err := getAdapterList()
if e != nil { if err != nil {
return nil, e return nil, err
} }
var ifat []Addr var ifat []Addr
......
...@@ -99,16 +99,12 @@ func JoinHostPort(host, port string) string { ...@@ -99,16 +99,12 @@ func JoinHostPort(host, port string) string {
// Convert "host:port" into IP address and port. // Convert "host:port" into IP address and port.
func hostPortToIP(net, hostport string) (ip IP, iport int, err error) { func hostPortToIP(net, hostport string) (ip IP, iport int, err error) {
var (
addr IP
p, i int
ok bool
)
host, port, err := SplitHostPort(hostport) host, port, err := SplitHostPort(hostport)
if err != nil { if err != nil {
goto Error return nil, 0, err
} }
var addr IP
if host != "" { if host != "" {
// Try as an IP address. // Try as an IP address.
addr = ParseIP(host) addr = ParseIP(host)
...@@ -121,34 +117,29 @@ func hostPortToIP(net, hostport string) (ip IP, iport int, err error) { ...@@ -121,34 +117,29 @@ func hostPortToIP(net, hostport string) (ip IP, iport int, err error) {
filter = ipv6only filter = ipv6only
} }
// Not an IP address. Try as a DNS name. // Not an IP address. Try as a DNS name.
addrs, err1 := LookupHost(host) addrs, err := LookupHost(host)
if err1 != nil { if err != nil {
err = err1 return nil, 0, err
goto Error
} }
addr = firstFavoriteAddr(filter, addrs) addr = firstFavoriteAddr(filter, addrs)
if addr == nil { if addr == nil {
// should not happen // should not happen
err = &AddrError{"LookupHost returned no suitable address", addrs[0]} return nil, 0, &AddrError{"LookupHost returned no suitable address", addrs[0]}
goto Error
} }
} }
} }
p, i, ok = dtoi(port, 0) p, i, ok := dtoi(port, 0)
if !ok || i != len(port) { if !ok || i != len(port) {
p, err = LookupPort(net, port) p, err = LookupPort(net, port)
if err != nil { if err != nil {
goto Error return nil, 0, err
} }
} }
if p < 0 || p > 0xFFFF { if p < 0 || p > 0xFFFF {
err = &AddrError{"invalid port", port} return nil, 0, &AddrError{"invalid port", port}
goto Error
} }
return addr, p, nil return addr, p, nil
Error:
return nil, 0, err
} }
...@@ -22,33 +22,22 @@ func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) { ...@@ -22,33 +22,22 @@ func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) {
// parsePlan9Addr parses address of the form [ip!]port (e.g. 127.0.0.1!80). // parsePlan9Addr parses address of the form [ip!]port (e.g. 127.0.0.1!80).
func parsePlan9Addr(s string) (ip IP, iport int, err error) { func parsePlan9Addr(s string) (ip IP, iport int, err error) {
var ( addr := IPv4zero // address contains port only
addr IP
p, i int
ok bool
)
addr = IPv4zero // address contains port only
i = byteIndex(s, '!') i = byteIndex(s, '!')
if i >= 0 { if i >= 0 {
addr = ParseIP(s[:i]) addr = ParseIP(s[:i])
if addr == nil { if addr == nil {
err = errors.New("net: parsing IP failed") return nil, 0, errors.New("net: parsing IP failed")
goto Error
} }
} }
p, _, ok = dtoi(s[i+1:], 0) p, _, ok := dtoi(s[i+1:], 0)
if !ok { if !ok {
err = errors.New("net: parsing port failed") return nil, 0, errors.New("net: parsing port failed")
goto Error
} }
if p < 0 || p > 0xFFFF { if p < 0 || p > 0xFFFF {
err = &AddrError{"invalid port", string(p)} return nil, 0, &AddrError{"invalid port", string(p)}
goto Error
} }
return addr, p, nil return addr, p, nil
Error:
return nil, 0, err
} }
func readPlan9Addr(proto, filename string) (addr Addr, err error) { func readPlan9Addr(proto, filename string) (addr Addr, err error) {
......
...@@ -21,9 +21,9 @@ var ( ...@@ -21,9 +21,9 @@ var (
func lookupProtocol(name string) (proto int, err error) { func lookupProtocol(name string) (proto int, err error) {
protoentLock.Lock() protoentLock.Lock()
defer protoentLock.Unlock() defer protoentLock.Unlock()
p, e := syscall.GetProtoByName(name) p, err := syscall.GetProtoByName(name)
if e != nil { if err != nil {
return 0, os.NewSyscallError("GetProtoByName", e) return 0, os.NewSyscallError("GetProtoByName", err)
} }
return int(p.Proto), nil return int(p.Proto), nil
} }
...@@ -43,9 +43,9 @@ func lookupHost(name string) (addrs []string, err error) { ...@@ -43,9 +43,9 @@ func lookupHost(name string) (addrs []string, err error) {
func lookupIP(name string) (addrs []IP, err error) { func lookupIP(name string) (addrs []IP, err error) {
hostentLock.Lock() hostentLock.Lock()
defer hostentLock.Unlock() defer hostentLock.Unlock()
h, e := syscall.GetHostByName(name) h, err := syscall.GetHostByName(name)
if e != nil { if err != nil {
return nil, os.NewSyscallError("GetHostByName", e) return nil, os.NewSyscallError("GetHostByName", err)
} }
switch h.AddrType { switch h.AddrType {
case syscall.AF_INET: case syscall.AF_INET:
...@@ -70,9 +70,9 @@ func lookupPort(network, service string) (port int, err error) { ...@@ -70,9 +70,9 @@ func lookupPort(network, service string) (port int, err error) {
} }
serventLock.Lock() serventLock.Lock()
defer serventLock.Unlock() defer serventLock.Unlock()
s, e := syscall.GetServByName(service, network) s, err := syscall.GetServByName(service, network)
if e != nil { if err != nil {
return 0, os.NewSyscallError("GetServByName", e) return 0, os.NewSyscallError("GetServByName", err)
} }
return int(syscall.Ntohs(s.Port)), nil return int(syscall.Ntohs(s.Port)), nil
} }
......
...@@ -42,10 +42,6 @@ func TestMulticastUDP(t *testing.T) { ...@@ -42,10 +42,6 @@ func TestMulticastUDP(t *testing.T) {
} }
for _, tt := range multicastUDPTests { for _, tt := range multicastUDPTests {
var (
ifi *Interface
found bool
)
if tt.ipv6 && (!supportsIPv6 || os.Getuid() != 0) { if tt.ipv6 && (!supportsIPv6 || os.Getuid() != 0) {
continue continue
} }
...@@ -53,6 +49,7 @@ func TestMulticastUDP(t *testing.T) { ...@@ -53,6 +49,7 @@ func TestMulticastUDP(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Interfaces failed: %v", err) t.Fatalf("Interfaces failed: %v", err)
} }
var ifi *Interface
for _, x := range ift { for _, x := range ift {
if x.Flags&tt.flags == tt.flags { if x.Flags&tt.flags == tt.flags {
ifi = &x ifi = &x
...@@ -80,6 +77,7 @@ func TestMulticastUDP(t *testing.T) { ...@@ -80,6 +77,7 @@ func TestMulticastUDP(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("MulticastAddrs failed: %v", err) t.Fatalf("MulticastAddrs failed: %v", err)
} }
var found bool
for _, ifma := range ifmat { for _, ifma := range ifmat {
if ifma.(*IPAddr).IP.Equal(tt.gaddr) { if ifma.(*IPAddr).IP.Equal(tt.gaddr) {
found = true found = true
......
...@@ -71,15 +71,15 @@ func TestDialError(t *testing.T) { ...@@ -71,15 +71,15 @@ func TestDialError(t *testing.T) {
return return
} }
for i, tt := range dialErrorTests { for i, tt := range dialErrorTests {
c, e := Dial(tt.Net, tt.Raddr) c, err := Dial(tt.Net, tt.Raddr)
if c != nil { if c != nil {
c.Close() c.Close()
} }
if e == nil { if err == nil {
t.Errorf("#%d: nil error, want match for %#q", i, tt.Pattern) t.Errorf("#%d: nil error, want match for %#q", i, tt.Pattern)
continue continue
} }
s := e.Error() s := err.Error()
match, _ := regexp.MatchString(tt.Pattern, s) match, _ := regexp.MatchString(tt.Pattern, s)
if !match { if !match {
t.Errorf("#%d: %q, want match for %#q", i, s, tt.Pattern) t.Errorf("#%d: %q, want match for %#q", i, s, tt.Pattern)
...@@ -111,16 +111,16 @@ var revAddrTests = []struct { ...@@ -111,16 +111,16 @@ var revAddrTests = []struct {
func TestReverseAddress(t *testing.T) { func TestReverseAddress(t *testing.T) {
for i, tt := range revAddrTests { for i, tt := range revAddrTests {
a, e := reverseaddr(tt.Addr) a, err := reverseaddr(tt.Addr)
if len(tt.ErrPrefix) > 0 && e == nil { if len(tt.ErrPrefix) > 0 && err == nil {
t.Errorf("#%d: expected %q, got <nil> (error)", i, tt.ErrPrefix) t.Errorf("#%d: expected %q, got <nil> (error)", i, tt.ErrPrefix)
continue continue
} }
if len(tt.ErrPrefix) == 0 && e != nil { if len(tt.ErrPrefix) == 0 && err != nil {
t.Errorf("#%d: expected <nil>, got %q (error)", i, e) t.Errorf("#%d: expected <nil>, got %q (error)", i, err)
} }
if e != nil && e.(*DNSError).Err != tt.ErrPrefix { if err != nil && err.(*DNSError).Err != tt.ErrPrefix {
t.Errorf("#%d: expected %q, got %q (mismatched error)", i, tt.ErrPrefix, e.(*DNSError).Err) t.Errorf("#%d: expected %q, got %q (mismatched error)", i, tt.ErrPrefix, err.(*DNSError).Err)
} }
if a != tt.Reverse { if a != tt.Reverse {
t.Errorf("#%d: expected %q, got %q (reverse address)", i, tt.Reverse, a) t.Errorf("#%d: expected %q, got %q (reverse address)", i, tt.Reverse, a)
......
...@@ -48,23 +48,23 @@ func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) { ...@@ -48,23 +48,23 @@ func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) {
if int64(n) > remain { if int64(n) > remain {
n = int(remain) n = int(remain)
} }
n, errno := syscall.Sendfile(dst, src, nil, n) n, err1 := syscall.Sendfile(dst, src, nil, n)
if n > 0 { if n > 0 {
written += int64(n) written += int64(n)
remain -= int64(n) remain -= int64(n)
} }
if n == 0 && errno == nil { if n == 0 && err1 == nil {
break break
} }
if errno == syscall.EAGAIN && c.wdeadline >= 0 { if err1 == syscall.EAGAIN && c.wdeadline >= 0 {
pollserver.WaitWrite(c) pollserver.WaitWrite(c)
continue continue
} }
if errno != nil { if err1 != nil {
// This includes syscall.ENOSYS (no kernel // This includes syscall.ENOSYS (no kernel
// support) and syscall.EINVAL (fd types which // support) and syscall.EINVAL (fd types which
// don't implement sendfile together) // don't implement sendfile together)
err = &OpError{"sendfile", c.net, c.raddr, errno} err = &OpError{"sendfile", c.net, c.raddr, err1}
break break
} }
} }
......
...@@ -135,8 +135,8 @@ func setWriteDeadline(fd *netFD, t time.Time) error { ...@@ -135,8 +135,8 @@ func setWriteDeadline(fd *netFD, t time.Time) error {
} }
func setDeadline(fd *netFD, t time.Time) error { func setDeadline(fd *netFD, t time.Time) error {
if e := setReadDeadline(fd, t); e != nil { if err := setReadDeadline(fd, t); err != nil {
return e return err
} }
return setWriteDeadline(fd, t) return setWriteDeadline(fd, t)
} }
......
...@@ -223,13 +223,13 @@ func (c *TCPConn) File() (f *os.File, err error) { return c.fd.dup() } ...@@ -223,13 +223,13 @@ func (c *TCPConn) File() (f *os.File, err error) { return c.fd.dup() }
// DialTCP connects to the remote address raddr on the network net, // DialTCP connects to the remote address raddr on the network net,
// which must be "tcp", "tcp4", or "tcp6". If laddr is not nil, it is used // which must be "tcp", "tcp4", or "tcp6". If laddr is not nil, it is used
// as the local address for the connection. // as the local address for the connection.
func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err error) { func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
if raddr == nil { if raddr == nil {
return nil, &OpError{"dial", net, nil, errMissingAddress} return nil, &OpError{"dial", net, nil, errMissingAddress}
} }
fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_STREAM, 0, "dial", sockaddrToTCP) fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_STREAM, 0, "dial", sockaddrToTCP)
if e != nil { if err != nil {
return nil, e return nil, err
} }
return newTCPConn(fd), nil return newTCPConn(fd), nil
} }
...@@ -245,7 +245,7 @@ type TCPListener struct { ...@@ -245,7 +245,7 @@ type TCPListener struct {
// Net must be "tcp", "tcp4", or "tcp6". // Net must be "tcp", "tcp4", or "tcp6".
// If laddr has a port of 0, it means to listen on some available port. // If laddr has a port of 0, it means to listen on some available port.
// The caller can use l.Addr() to retrieve the chosen address. // The caller can use l.Addr() to retrieve the chosen address.
func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err error) { func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) {
fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, 0, "listen", sockaddrToTCP) fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, 0, "listen", sockaddrToTCP)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -255,7 +255,7 @@ func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err error) { ...@@ -255,7 +255,7 @@ func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err error) {
closesocket(fd.sysfd) closesocket(fd.sysfd)
return nil, &OpError{"listen", net, laddr, err} return nil, &OpError{"listen", net, laddr, err}
} }
l = new(TCPListener) l := new(TCPListener)
l.fd = fd l.fd = fd
return l, nil return l, nil
} }
......
...@@ -210,7 +210,7 @@ func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) { ...@@ -210,7 +210,7 @@ func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) {
// DialUDP connects to the remote address raddr on the network net, // DialUDP connects to the remote address raddr on the network net,
// which must be "udp", "udp4", or "udp6". If laddr is not nil, it is used // which must be "udp", "udp4", or "udp6". If laddr is not nil, it is used
// as the local address for the connection. // as the local address for the connection.
func DialUDP(net string, laddr, raddr *UDPAddr) (c *UDPConn, err error) { func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
switch net { switch net {
case "udp", "udp4", "udp6": case "udp", "udp4", "udp6":
default: default:
...@@ -219,9 +219,9 @@ func DialUDP(net string, laddr, raddr *UDPAddr) (c *UDPConn, err error) { ...@@ -219,9 +219,9 @@ func DialUDP(net string, laddr, raddr *UDPAddr) (c *UDPConn, err error) {
if raddr == nil { if raddr == nil {
return nil, &OpError{"dial", net, nil, errMissingAddress} return nil, &OpError{"dial", net, nil, errMissingAddress}
} }
fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_DGRAM, 0, "dial", sockaddrToUDP) fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_DGRAM, 0, "dial", sockaddrToUDP)
if e != nil { if err != nil {
return nil, e return nil, err
} }
return newUDPConn(fd), nil return newUDPConn(fd), nil
} }
......
...@@ -298,10 +298,10 @@ func (c *UnixConn) File() (f *os.File, err error) { return c.fd.dup() } ...@@ -298,10 +298,10 @@ func (c *UnixConn) File() (f *os.File, err error) { return c.fd.dup() }
// DialUnix connects to the remote address raddr on the network net, // DialUnix connects to the remote address raddr on the network net,
// which must be "unix" or "unixgram". If laddr is not nil, it is used // which must be "unix" or "unixgram". If laddr is not nil, it is used
// as the local address for the connection. // as the local address for the connection.
func DialUnix(net string, laddr, raddr *UnixAddr) (c *UnixConn, err error) { func DialUnix(net string, laddr, raddr *UnixAddr) (*UnixConn, error) {
fd, e := unixSocket(net, laddr, raddr, "dial") fd, err := unixSocket(net, laddr, raddr, "dial")
if e != nil { if err != nil {
return nil, e return nil, err
} }
return newUnixConn(fd), nil return newUnixConn(fd), nil
} }
...@@ -337,15 +337,15 @@ func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) { ...@@ -337,15 +337,15 @@ func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) {
// AcceptUnix accepts the next incoming call and returns the new connection // AcceptUnix accepts the next incoming call and returns the new connection
// and the remote address. // and the remote address.
func (l *UnixListener) AcceptUnix() (c *UnixConn, err error) { func (l *UnixListener) AcceptUnix() (*UnixConn, error) {
if l == nil || l.fd == nil { if l == nil || l.fd == nil {
return nil, os.EINVAL return nil, os.EINVAL
} }
fd, e := l.fd.accept(sockaddrToUnix) fd, err := l.fd.accept(sockaddrToUnix)
if e != nil { if err != nil {
return nil, e return nil, err
} }
c = newUnixConn(fd) c := newUnixConn(fd)
return c, nil return c, nil
} }
...@@ -405,7 +405,7 @@ func (l *UnixListener) File() (f *os.File, err error) { return l.fd.dup() } ...@@ -405,7 +405,7 @@ func (l *UnixListener) File() (f *os.File, err error) { return l.fd.dup() }
// local address laddr. The returned connection c's ReadFrom // local address laddr. The returned connection c's ReadFrom
// and WriteTo methods can be used to receive and send UDP // and WriteTo methods can be used to receive and send UDP
// packets with per-packet addressing. The network net must be "unixgram". // packets with per-packet addressing. The network net must be "unixgram".
func ListenUnixgram(net string, laddr *UnixAddr) (c *UDPConn, err error) { func ListenUnixgram(net string, laddr *UnixAddr) (*UDPConn, error) {
switch net { switch net {
case "unixgram": case "unixgram":
default: default:
...@@ -414,9 +414,9 @@ func ListenUnixgram(net string, laddr *UnixAddr) (c *UDPConn, err error) { ...@@ -414,9 +414,9 @@ func ListenUnixgram(net string, laddr *UnixAddr) (c *UDPConn, err error) {
if laddr == nil { if laddr == nil {
return nil, &OpError{"listen", net, nil, errMissingAddress} return nil, &OpError{"listen", net, nil, errMissingAddress}
} }
fd, e := unixSocket(net, laddr, nil, "listen") fd, err := unixSocket(net, laddr, nil, "listen")
if e != nil { if err != nil {
return nil, e return nil, err
} }
return newUDPConn(fd), nil return newUDPConn(fd), nil
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment