1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net;
18 
19 import java.io.Closeable;
20 import java.io.FileDescriptor;
21 import java.io.IOException;
22 
23 /**
24  * Non-standard class for creating an inbound UNIX-domain socket
25  * in the Linux abstract namespace.
26  */
27 public class LocalServerSocket implements Closeable {
28     private final LocalSocketImpl impl;
29     private final LocalSocketAddress localAddress;
30 
31     /** 50 seems a bit much, but it's what was here */
32     private static final int LISTEN_BACKLOG = 50;
33 
34     /**
35      * Creates a new server socket listening at specified name.
36      * On the Android platform, the name is created in the Linux
37      * abstract namespace (instead of on the filesystem).
38      *
39      * @param name address for socket
40      * @throws IOException
41      */
LocalServerSocket(String name)42     public LocalServerSocket(String name) throws IOException
43     {
44         impl = new LocalSocketImpl();
45 
46         impl.create(LocalSocket.SOCKET_STREAM);
47 
48         localAddress = new LocalSocketAddress(name);
49         impl.bind(localAddress);
50 
51         impl.listen(LISTEN_BACKLOG);
52     }
53 
54     /**
55      * Create a LocalServerSocket from a file descriptor that's already
56      * been created and bound. listen() will be called immediately on it.
57      * Used for cases where file descriptors are passed in via environment
58      * variables
59      *
60      * @param fd bound file descriptor
61      * @throws IOException
62      */
LocalServerSocket(FileDescriptor fd)63     public LocalServerSocket(FileDescriptor fd) throws IOException
64     {
65         impl = new LocalSocketImpl(fd);
66         impl.listen(LISTEN_BACKLOG);
67         localAddress = impl.getSockAddress();
68     }
69 
70     /**
71      * Obtains the socket's local address
72      *
73      * @return local address
74      */
getLocalSocketAddress()75     public LocalSocketAddress getLocalSocketAddress()
76     {
77         return localAddress;
78     }
79 
80     /**
81      * Accepts a new connection to the socket. Blocks until a new
82      * connection arrives.
83      *
84      * @return a socket representing the new connection.
85      * @throws IOException
86      */
accept()87     public LocalSocket accept() throws IOException
88     {
89         LocalSocketImpl acceptedImpl = new LocalSocketImpl();
90 
91         impl.accept(acceptedImpl);
92 
93         return LocalSocket.createLocalSocketForAccept(acceptedImpl);
94     }
95 
96     /**
97      * Returns file descriptor or null if not yet open/already closed
98      *
99      * @return fd or null
100      */
getFileDescriptor()101     public FileDescriptor getFileDescriptor() {
102         return impl.getFileDescriptor();
103     }
104 
105     /**
106      * Closes server socket.
107      *
108      * @throws IOException
109      */
close()110     @Override public void close() throws IOException
111     {
112         impl.close();
113     }
114 }
115