transport_local.c revision 35237d135807af84bf9b0e5b8d7f8633e58db6f5
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#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <errno.h>
21
22#include "sysdeps.h"
23#include <sys/types.h>
24
25#define  TRACE_TAG  TRACE_TRANSPORT
26#include "adb.h"
27
28#ifdef __ppc__
29#define H4(x)	(((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
30static inline void fix_endians(apacket *p)
31{
32    p->msg.command     = H4(p->msg.command);
33    p->msg.arg0        = H4(p->msg.arg0);
34    p->msg.arg1        = H4(p->msg.arg1);
35    p->msg.data_length = H4(p->msg.data_length);
36    p->msg.data_check  = H4(p->msg.data_check);
37    p->msg.magic       = H4(p->msg.magic);
38}
39#else
40#define fix_endians(p) do {} while (0)
41#endif
42
43#if ADB_HOST
44/* we keep a list of opened transports, transport 0 is bound to 5555,
45 * transport 1 to 5557, .. transport n to 5555 + n*2. the list is used
46 * to detect when we're trying to connect twice to a given local transport
47 */
48#define  ADB_LOCAL_TRANSPORT_MAX  16
49
50ADB_MUTEX_DEFINE( local_transports_lock );
51
52static atransport*  local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
53#endif /* ADB_HOST */
54
55static int remote_read(apacket *p, atransport *t)
56{
57    if(readx(t->sfd, &p->msg, sizeof(amessage))){
58        D("remote local: read terminated (message)\n");
59        return -1;
60    }
61
62    fix_endians(p);
63
64#if 0 && defined __ppc__
65    D("read remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
66      p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
67#endif
68    if(check_header(p)) {
69        D("bad header: terminated (data)\n");
70        return -1;
71    }
72
73    if(readx(t->sfd, p->data, p->msg.data_length)){
74        D("remote local: terminated (data)\n");
75        return -1;
76    }
77
78    if(check_data(p)) {
79        D("bad data: terminated (data)\n");
80        return -1;
81    }
82
83    return 0;
84}
85
86static int remote_write(apacket *p, atransport *t)
87{
88    int   length = p->msg.data_length;
89
90    fix_endians(p);
91
92#if 0 && defined __ppc__
93    D("write remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
94      p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
95#endif
96    if(writex(t->sfd, &p->msg, sizeof(amessage) + length)) {
97        D("remote local: write terminated\n");
98        return -1;
99    }
100
101    return 0;
102}
103
104
105int  local_connect(int  port)
106{
107    char buf[64];
108    int  fd = -1;
109
110#if ADB_HOST
111    const char *host = getenv("ADBHOST");
112    if (host) {
113        fd = socket_network_client(host, port, SOCK_STREAM);
114    }
115#endif
116    if (fd < 0) {
117        fd = socket_loopback_client(port, SOCK_STREAM);
118    }
119
120    if (fd >= 0) {
121        D("client: connected on remote on fd %d\n", fd);
122        close_on_exec(fd);
123        disable_tcp_nagle(fd);
124        snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, port - 1);
125        register_socket_transport(fd, buf, port);
126        return 0;
127    }
128    return -1;
129}
130
131
132static void *client_socket_thread(void *x)
133{
134#if ADB_HOST
135    int  port  = ADB_LOCAL_TRANSPORT_PORT;
136    int  count = ADB_LOCAL_TRANSPORT_MAX;
137
138    D("transport: client_socket_thread() starting\n");
139
140    /* try to connect to any number of running emulator instances     */
141    /* this is only done when ADB starts up. later, each new emulator */
142    /* will send a message to ADB to indicate that is is starting up  */
143    for ( ; count > 0; count--, port += 2 ) {
144        (void) local_connect(port);
145    }
146#endif
147    return 0;
148}
149
150static void *server_socket_thread(void *x)
151{
152    int serverfd, fd;
153    struct sockaddr addr;
154    socklen_t alen;
155
156    D("transport: server_socket_thread() starting\n");
157    serverfd = -1;
158    for(;;) {
159        if(serverfd == -1) {
160            serverfd = socket_inaddr_any_server(ADB_LOCAL_TRANSPORT_PORT, SOCK_STREAM);
161            if(serverfd < 0) {
162                D("server: cannot bind socket yet\n");
163                adb_sleep_ms(1000);
164                continue;
165            }
166            close_on_exec(serverfd);
167        }
168
169        alen = sizeof(addr);
170        D("server: trying to get new connection from %d\n", ADB_LOCAL_TRANSPORT_PORT);
171        fd = adb_socket_accept(serverfd, &addr, &alen);
172        if(fd >= 0) {
173            D("server: new connection on fd %d\n", fd);
174            close_on_exec(fd);
175            disable_tcp_nagle(fd);
176            register_socket_transport(fd,"host",ADB_LOCAL_TRANSPORT_PORT);
177        }
178    }
179    D("transport: server_socket_thread() exiting\n");
180    return 0;
181}
182
183void local_init(void)
184{
185    adb_thread_t thr;
186    void* (*func)(void *);
187
188    if(HOST) {
189        func = client_socket_thread;
190    } else {
191        func = server_socket_thread;
192    }
193
194    D("transport: local %s init\n", HOST ? "client" : "server");
195
196    if(adb_thread_create(&thr, func, 0)) {
197        fatal_errno("cannot create local socket %s thread",
198                    HOST ? "client" : "server");
199    }
200}
201
202static void remote_kick(atransport *t)
203{
204    int fd = t->sfd;
205    t->sfd = -1;
206    adb_close(fd);
207
208#if ADB_HOST
209    if(HOST) {
210        int  nn;
211        adb_mutex_lock( &local_transports_lock );
212        for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
213            if (local_transports[nn] == t) {
214                local_transports[nn] = NULL;
215                break;
216            }
217        }
218        adb_mutex_unlock( &local_transports_lock );
219    }
220#endif
221}
222
223static void remote_close(atransport *t)
224{
225    adb_close(t->fd);
226}
227
228int init_socket_transport(atransport *t, int s, int  port)
229{
230    int  fail = 0;
231
232    t->kick = remote_kick;
233    t->close = remote_close;
234    t->read_from_remote = remote_read;
235    t->write_to_remote = remote_write;
236    t->sfd = s;
237    t->sync_token = 1;
238    t->connection_state = CS_OFFLINE;
239    t->type = kTransportLocal;
240
241#if ADB_HOST
242    if (HOST) {
243        adb_mutex_lock( &local_transports_lock );
244        {
245            int  index = (port - ADB_LOCAL_TRANSPORT_PORT)/2;
246
247            if (!(port & 1) || index < 0 || index >= ADB_LOCAL_TRANSPORT_MAX) {
248                D("bad local transport port number: %d\n", port);
249                fail = -1;
250            }
251            else if (local_transports[index] != NULL) {
252                D("local transport for port %d already registered (%p)?\n",
253                port, local_transports[index]);
254                fail = -1;
255            }
256            else
257                local_transports[index] = t;
258        }
259        adb_mutex_unlock( &local_transports_lock );
260    }
261#endif
262    return fail;
263}
264