19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.net;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.IOException;
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.FileDescriptor;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
23547b0acae14981581d050c2c3d674cc06f1c3cf5Elliott Hughes * Non-standard class for creating an inbound UNIX-domain socket
24547b0acae14981581d050c2c3d674cc06f1c3cf5Elliott Hughes * in the Linux abstract namespace.
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class LocalServerSocket {
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final LocalSocketImpl impl;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final LocalSocketAddress localAddress;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** 50 seems a bit much, but it's what was here */
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int LISTEN_BACKLOG = 50;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
34547b0acae14981581d050c2c3d674cc06f1c3cf5Elliott Hughes     * Creates a new server socket listening at specified name.
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * On the Android platform, the name is created in the Linux
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * abstract namespace (instead of on the filesystem).
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param name address for socket
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IOException
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public LocalServerSocket(String name) throws IOException
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        impl = new LocalSocketImpl();
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
45e7d309a929bf87a5752cd1fb2d48c72e47a671a2Mike Lockwood        impl.create(LocalSocket.SOCKET_STREAM);
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        localAddress = new LocalSocketAddress(name);
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        impl.bind(localAddress);
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        impl.listen(LISTEN_BACKLOG);
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Create a LocalServerSocket from a file descriptor that's already
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * been created and bound. listen() will be called immediately on it.
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Used for cases where file descriptors are passed in via environment
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * variables
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param fd bound file descriptor
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IOException
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public LocalServerSocket(FileDescriptor fd) throws IOException
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        impl = new LocalSocketImpl(fd);
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        impl.listen(LISTEN_BACKLOG);
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        localAddress = impl.getSockAddress();
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Obtains the socket's local address
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return local address
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public LocalSocketAddress getLocalSocketAddress()
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return localAddress;
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Accepts a new connection to the socket. Blocks until a new
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * connection arrives.
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return a socket representing the new connection.
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IOException
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public LocalSocket accept() throws IOException
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        LocalSocketImpl acceptedImpl = new LocalSocketImpl();
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        impl.accept (acceptedImpl);
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
92e7d309a929bf87a5752cd1fb2d48c72e47a671a2Mike Lockwood        return new LocalSocket(acceptedImpl, LocalSocket.SOCKET_UNKNOWN);
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns file descriptor or null if not yet open/already closed
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return fd or null
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public FileDescriptor getFileDescriptor() {
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return impl.getFileDescriptor();
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Closes server socket.
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IOException
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void close() throws IOException
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        impl.close();
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
114