aboutsummaryrefslogtreecommitdiffstats
path: root/libcutils/adb_networking.c
blob: d819d44c8de97997efd6c7ab1a9a4de8c53a5469 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* libs/utils/adb_networking.c
**
** Copyright 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.
*/

#define ADB_PORT 5037

#define _GNU_SOURCE     /* for asprintf */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#include <cutils/adb_networking.h>
#include <cutils/sockets.h>
#include <cutils/properties.h>

#define ADB_RESPONSE_SIZE 4

/**
 * Unfortunately, java.net.Socket wants to create it's filedescriptor early
 * So, this function takes an fd that must be an unconnected
 * PF_LOCAL SOCK_STREAM
 */
int adb_networking_connect_fd(int fd, struct sockaddr_in *p_address)
{
    struct sockaddr_in local_addr;
    socklen_t alen;
    char *cmd;
    char buf[ADB_RESPONSE_SIZE + 1];
    ssize_t count_read;
    int ret;
    int err;
    /* for impl of inet_ntoa below*/
    union {
        uint8_t  b[4];
        uint32_t l;
    } a;

    /* First, connect to adb */
   
    memset(&local_addr, 0, sizeof(local_addr));
    local_addr.sin_family = AF_INET;
    local_addr.sin_port = htons(ADB_PORT);
    local_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

    do {
        err = connect(fd, (struct sockaddr *) &local_addr, sizeof(local_addr));
    } while (err < 0 && errno == EINTR);

    if (err < 0) {
        return -1;
    }

    a.l = p_address->sin_addr.s_addr;

    // compose the command
    asprintf(&cmd, "tcp:%u:%u.%u.%u.%u", 
                (unsigned int)ntohs(p_address->sin_port), 
                a.b[0],a.b[1],a.b[2],a.b[3]);

    // buf is now the ascii hex length of cmd
    snprintf(buf, sizeof(buf), "%04X", strlen(cmd));

    // write the 4-byte length
    do {
        err = write(fd, buf, 4);        
    } while (err < 0 && errno == EINTR);

    // write the command
    do {
        err = write(fd, cmd, strlen(cmd));        
    } while (err < 0 && errno == EINTR);

    // read the result
    do {
        count_read = read(fd, buf, sizeof(buf) - 1);
    } while (count_read < 0 && errno != EINTR);

    if (count_read == ADB_RESPONSE_SIZE 
            && 0 == strncmp(buf, "OKAY", ADB_RESPONSE_SIZE)) {
        ret = 0;
    } else {
        /* what errno here? <shrug? */
        errno = ENETUNREACH;
        ret = -1;
    }

    free(cmd);
    
    return ret;
}

/**
 * Fills in *p_out_addr and returns 0 on success
 * Memset's *p_out_addr and returns -1 on fail
 */

int adb_networking_gethostbyname(const char *name, struct in_addr *p_out_addr)
{
    int fd;
    char *cmd = NULL;
    char buf[ADB_RESPONSE_SIZE + 1];
    int err;
    ssize_t count_read;
    
    fd = socket_loopback_client(ADB_PORT, SOCK_STREAM);

    if (fd < 0) {
        return -1;
    }

    // compose the command
    asprintf(&cmd, "dns:%s", name);

    // buf is now the ascii hex length of cmd
    snprintf(buf, sizeof(buf), "%04X", strlen(cmd));

    // write the 4-byte length
    do {
        err = write(fd, buf, 4);        
    } while (err < 0 && errno == EINTR);

    // write the command
    do {
        err = write(fd, cmd, strlen(cmd));        
    } while (err < 0 && errno == EINTR);

    // read the result
    do {
        count_read = read(fd, buf, ADB_RESPONSE_SIZE);
    } while (count_read < 0 && errno != EINTR);

    if (count_read != ADB_RESPONSE_SIZE 
            || 0 != strncmp(buf, "OKAY", ADB_RESPONSE_SIZE)) {
        goto error;
    }

    // read the actual IP address
    do {
        count_read = read(fd, &(p_out_addr->s_addr), sizeof(p_out_addr->s_addr));
    } while (count_read < 0 && errno != EINTR);

    if (count_read != 4) {
        goto error;
    }

    free(cmd);
    close(fd);
    return 0;
error:
    free(cmd);
    close(fd);
    memset(p_out_addr, 0, sizeof(struct in_addr));
    return -1;
}