OSDN Git Service

net/http: delete temporary files.
[pf3gnuchains/gcc-fork.git] / libgo / go / net / file.go
1 // Copyright 2011 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 openbsd
6
7 package net
8
9 import (
10         "os"
11         "syscall"
12 )
13
14 func newFileFD(f *os.File) (nfd *netFD, err error) {
15         fd, errno := syscall.Dup(f.Fd())
16         if errno != 0 {
17                 return nil, os.NewSyscallError("dup", errno)
18         }
19
20         proto, errno := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE)
21         if errno != 0 {
22                 return nil, os.NewSyscallError("getsockopt", errno)
23         }
24
25         family := syscall.AF_UNSPEC
26         toAddr := sockaddrToTCP
27         sa, _ := syscall.Getsockname(fd)
28         switch sa.(type) {
29         default:
30                 closesocket(fd)
31                 return nil, os.EINVAL
32         case *syscall.SockaddrInet4:
33                 family = syscall.AF_INET
34                 if proto == syscall.SOCK_DGRAM {
35                         toAddr = sockaddrToUDP
36                 } else if proto == syscall.SOCK_RAW {
37                         toAddr = sockaddrToIP
38                 }
39         case *syscall.SockaddrInet6:
40                 family = syscall.AF_INET6
41                 if proto == syscall.SOCK_DGRAM {
42                         toAddr = sockaddrToUDP
43                 } else if proto == syscall.SOCK_RAW {
44                         toAddr = sockaddrToIP
45                 }
46         case *syscall.SockaddrUnix:
47                 family = syscall.AF_UNIX
48                 toAddr = sockaddrToUnix
49                 if proto == syscall.SOCK_DGRAM {
50                         toAddr = sockaddrToUnixgram
51                 } else if proto == syscall.SOCK_SEQPACKET {
52                         toAddr = sockaddrToUnixpacket
53                 }
54         }
55         laddr := toAddr(sa)
56         sa, _ = syscall.Getpeername(fd)
57         raddr := toAddr(sa)
58
59         if nfd, err = newFD(fd, family, proto, laddr.Network()); err != nil {
60                 return nil, err
61         }
62         nfd.setAddr(laddr, raddr)
63         return nfd, nil
64 }
65
66 // FileConn returns a copy of the network connection corresponding to
67 // the open file f.  It is the caller's responsibility to close f when
68 // finished.  Closing c does not affect f, and closing f does not
69 // affect c.
70 func FileConn(f *os.File) (c Conn, err error) {
71         fd, err := newFileFD(f)
72         if err != nil {
73                 return nil, err
74         }
75         switch fd.laddr.(type) {
76         case *TCPAddr:
77                 return newTCPConn(fd), nil
78         case *UDPAddr:
79                 return newUDPConn(fd), nil
80         case *UnixAddr:
81                 return newUnixConn(fd), nil
82         case *IPAddr:
83                 return newIPConn(fd), nil
84         }
85         fd.Close()
86         return nil, os.EINVAL
87 }
88
89 // FileListener returns a copy of the network listener corresponding
90 // to the open file f.  It is the caller's responsibility to close l
91 // when finished.  Closing c does not affect l, and closing l does not
92 // affect c.
93 func FileListener(f *os.File) (l Listener, err error) {
94         fd, err := newFileFD(f)
95         if err != nil {
96                 return nil, err
97         }
98         switch laddr := fd.laddr.(type) {
99         case *TCPAddr:
100                 return &TCPListener{fd}, nil
101         case *UnixAddr:
102                 return &UnixListener{fd, laddr.Name}, nil
103         }
104         fd.Close()
105         return nil, os.EINVAL
106 }
107
108 // FilePacketConn returns a copy of the packet network connection
109 // corresponding to the open file f.  It is the caller's
110 // responsibility to close f when finished.  Closing c does not affect
111 // f, and closing f does not affect c.
112 func FilePacketConn(f *os.File) (c PacketConn, err error) {
113         fd, err := newFileFD(f)
114         if err != nil {
115                 return nil, err
116         }
117         switch fd.laddr.(type) {
118         case *UDPAddr:
119                 return newUDPConn(fd), nil
120         case *UnixAddr:
121                 return newUnixConn(fd), nil
122         }
123         fd.Close()
124         return nil, os.EINVAL
125 }