1756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell/*
2756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * Copyright (C) 2016 The Android Open Source Project
3756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * All rights reserved.
4756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell *
5756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * Redistribution and use in source and binary forms, with or without
6756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * modification, are permitted provided that the following conditions
7756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * are met:
8756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell *  * Redistributions of source code must retain the above copyright
9756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell *    notice, this list of conditions and the following disclaimer.
10756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell *  * Redistributions in binary form must reproduce the above copyright
11756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell *    notice, this list of conditions and the following disclaimer in
12756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell *    the documentation and/or other materials provided with the
13756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell *    distribution.
14756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell *
15756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell * SUCH DAMAGE.
27756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell */
28756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell
29756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell// This file contains socket implementation that can be shared between
30756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell// platforms as long as the correct headers are included.
31756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell
32756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell#include <cutils/sockets.h>
33756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell
34756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell#if !defined(_WIN32)
35756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell#include <netinet/in.h>
36756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell#endif
37756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell
38756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursellint socket_get_local_port(cutils_socket_t sock) {
39756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell    sockaddr_storage addr;
40756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell    socklen_t addr_size = sizeof(addr);
41756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell
42756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell    if (getsockname(sock, reinterpret_cast<sockaddr*>(&addr), &addr_size) == 0) {
43756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell        // sockaddr_in and sockaddr_in6 always overlap the port field.
44756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell        return ntohs(reinterpret_cast<sockaddr_in*>(&addr)->sin_port);
45756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell    }
46756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell    return -1;
47756e1c81e84c7353e0e518d9b53065357d27937bDavid Pursell}
48