Skip to content
Snippets Groups Projects
Select Git revision
  • c1b26acae295034e7aab2414ba50047cc889cafe
  • master default protected
  • android-7.1.2_r28_klist
  • oreo-mr1-iot-release
  • sdk-release
  • pie-cts-dev
  • pie-cts-release
  • pie-vts-release
  • nougat-iot-release
  • pie-gsi
  • pie-platform-release
  • pie-r2-release
  • pie-r2-s1-release
  • pie-release
  • pie-dev
  • oreo-m4-s4-release
  • o-mr1-iot-preview-8
  • oreo-m2-s2-release
  • oreo-m2-s1-release
  • oreo-m6-s2-release
  • oreo-m6-s3-release
  • android-o-mr1-iot-release-1.0.4
  • android-9.0.0_r8
  • android-9.0.0_r7
  • android-9.0.0_r6
  • android-9.0.0_r5
  • android-8.1.0_r46
  • android-8.1.0_r45
  • android-n-iot-release-smart-display-r2
  • android-vts-8.1_r5
  • android-cts-8.1_r8
  • android-cts-8.0_r12
  • android-cts-7.1_r20
  • android-cts-7.0_r24
  • android-cts-6.0_r31
  • android-o-mr1-iot-release-1.0.3
  • android-cts-9.0_r1
  • android-8.1.0_r43
  • android-8.1.0_r42
  • android-n-iot-release-smart-display
  • android-p-preview-5
41 results

socket_local_client_unix.c

Blame
    • David Pursell's avatar
      0eb8e1b7
      libcutils: share Windows networking code. · 0eb8e1b7
      David Pursell authored
      This CL moves Windows networking code from fastboot to libcutils so
      that it can be shared with other host programs such as adb.
      
      Not all libcutils networking functions have been implemented for
      Windows, just those necessary for fastboot. In the next CL I will do
      the same for adb, adding any additional required functions.
      
      Unit tests have also been added to test the functions using a loopback
      connection.
      
      Bug: http://b/26236380.
      Change-Id: Ibc51a67030fe69a04c23512eefa9d19b055c7c12
      0eb8e1b7
      History
      libcutils: share Windows networking code.
      David Pursell authored
      This CL moves Windows networking code from fastboot to libcutils so
      that it can be shared with other host programs such as adb.
      
      Not all libcutils networking functions have been implemented for
      Windows, just those necessary for fastboot. In the next CL I will do
      the same for adb, adding any additional required functions.
      
      Unit tests have also been added to test the functions using a loopback
      connection.
      
      Bug: http://b/26236380.
      Change-Id: Ibc51a67030fe69a04c23512eefa9d19b055c7c12
    socket_local_client_unix.c 4.26 KiB
    /*
     * Copyright (C) 2006 The Android Open Source Project
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    #include <errno.h>
    #include <stddef.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    #include <cutils/sockets.h>
    
    #if defined(_WIN32)
    
    int socket_local_client(const char *name, int namespaceId, int type)
    {
        errno = ENOSYS;
        return -1;
    }
    
    #else /* !_WIN32 */
    
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <sys/select.h>
    #include <sys/types.h>
    
    #include "socket_local_unix.h"
    
    #define UNUSED __attribute__((unused))
    
    #define LISTEN_BACKLOG 4
    
    /* Documented in header file. */
    int socket_make_sockaddr_un(const char *name, int namespaceId, 
            struct sockaddr_un *p_addr, socklen_t *alen)
    {
        memset (p_addr, 0, sizeof (*p_addr));
        size_t namelen;
    
        switch (namespaceId) {
            case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
    #if defined(__linux__)
                namelen  = strlen(name);
    
                // Test with length +1 for the *initial* '\0'.
                if ((namelen + 1) > sizeof(p_addr->sun_path)) {
                    goto error;
                }
    
                /*
                 * Note: The path in this case is *not* supposed to be
                 * '\0'-terminated. ("man 7 unix" for the gory details.)
                 */
                
                p_addr->sun_path[0] = 0;
                memcpy(p_addr->sun_path + 1, name, namelen);
    #else
                /* this OS doesn't have the Linux abstract namespace */
    
                namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
                /* unix_path_max appears to be missing on linux */
                if (namelen > sizeof(*p_addr) 
                        - offsetof(struct sockaddr_un, sun_path) - 1) {
                    goto error;
                }
    
                strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX);
                strcat(p_addr->sun_path, name);
    #endif
            break;
    
            case ANDROID_SOCKET_NAMESPACE_RESERVED:
                namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX);
                /* unix_path_max appears to be missing on linux */
                if (namelen > sizeof(*p_addr) 
                        - offsetof(struct sockaddr_un, sun_path) - 1) {
                    goto error;
                }
    
                strcpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX);
                strcat(p_addr->sun_path, name);
            break;
    
            case ANDROID_SOCKET_NAMESPACE_FILESYSTEM:
                namelen = strlen(name);
                /* unix_path_max appears to be missing on linux */
                if (namelen > sizeof(*p_addr) 
                        - offsetof(struct sockaddr_un, sun_path) - 1) {
                    goto error;
                }
    
                strcpy(p_addr->sun_path, name);
            break;
            default:
                // invalid namespace id
                return -1;
        }
    
        p_addr->sun_family = AF_LOCAL;
        *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
        return 0;
    error:
        return -1;
    }
    
    /**
     * connect to peer named "name" on fd
     * returns same fd or -1 on error.
     * fd is not closed on error. that's your job.
     * 
     * Used by AndroidSocketImpl
     */
    int socket_local_client_connect(int fd, const char *name, int namespaceId, 
            int type UNUSED)
    {
        struct sockaddr_un addr;
        socklen_t alen;
        int err;
    
        err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
    
        if (err < 0) {
            goto error;
        }
    
        if(connect(fd, (struct sockaddr *) &addr, alen) < 0) {
            goto error;
        }
    
        return fd;
    
    error:
        return -1;
    }
    
    /** 
     * connect to peer named "name"
     * returns fd or -1 on error
     */
    int socket_local_client(const char *name, int namespaceId, int type)
    {
        int s;
    
        s = socket(AF_LOCAL, type, 0);
        if(s < 0) return -1;
    
        if ( 0 > socket_local_client_connect(s, name, namespaceId, type)) {
            close(s);
            return -1;
        }
    
        return s;
    }
    
    #endif /* !_WIN32 */