transport_local.c revision 4f6e8d7a00cbeda1e70cc15be9c4af1018bdad53
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    fd = socket_loopback_client(port, SOCK_STREAM);
111#if ADB_HOST
112    if(fd < 0) {
113        const char *host = getenv("ADBHOST");
114        if(host) {
115            fd = socket_network_client(host, port, SOCK_STREAM);
116        }
117    }
118#endif
119    if (fd >= 0) {
120        D("client: connected on remote on fd %d\n", fd);
121        close_on_exec(fd);
122        disable_tcp_nagle(fd);
123        snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, port - 1);
124        register_socket_transport(fd, buf, port);
125        return 0;
126    }
127    return -1;
128}
129
130
131static void *client_socket_thread(void *x)
132{
133#if ADB_HOST
134    int  port  = ADB_LOCAL_TRANSPORT_PORT;
135    int  count = ADB_LOCAL_TRANSPORT_MAX;
136
137    D("transport: client_socket_thread() starting\n");
138
139    /* try to connect to any number of running emulator instances     */
140    /* this is only done when ADB starts up. later, each new emulator */
141    /* will send a message to ADB to indicate that is is starting up  */
142    for ( ; count > 0; count--, port += 2 ) {
143        (void) local_connect(port);
144    }
145#endif
146    return 0;
147}
148
149static void *server_socket_thread(void *x)
150{
151    int serverfd, fd;
152    struct sockaddr addr;
153    socklen_t alen;
154
155    D("transport: server_socket_thread() starting\n");
156    serverfd = -1;
157    for(;;) {
158        if(serverfd == -1) {
159            serverfd = socket_inaddr_any_server(ADB_LOCAL_TRANSPORT_PORT, SOCK_STREAM);
160            if(serverfd < 0) {
161                D("server: cannot bind socket yet\n");
162                adb_sleep_ms(1000);
163                continue;
164            }
165            close_on_exec(serverfd);
166        }
167
168        alen = sizeof(addr);
169        D("server: trying to get new connection from %d\n", ADB_LOCAL_TRANSPORT_PORT);
170        fd = adb_socket_accept(serverfd, &addr, &alen);
171        if(fd >= 0) {
172            D("server: new connection on fd %d\n", fd);
173            close_on_exec(fd);
174            disable_tcp_nagle(fd);
175            register_socket_transport(fd,"host",ADB_LOCAL_TRANSPORT_PORT);
176        }
177    }
178    D("transport: server_socket_thread() exiting\n");
179    return 0;
180}
181
182void local_init(void)
183{
184    adb_thread_t thr;
185    void* (*func)(void *);
186
187    if(HOST) {
188        func = client_socket_thread;
189    } else {
190        func = server_socket_thread;
191    }
192
193    D("transport: local %s init\n", HOST ? "client" : "server");
194
195    if(adb_thread_create(&thr, func, 0)) {
196        fatal_errno("cannot create local socket %s thread",
197                    HOST ? "client" : "server");
198    }
199}
200
201static void remote_kick(atransport *t)
202{
203    int fd = t->sfd;
204    t->sfd = -1;
205    adb_close(fd);
206
207#if ADB_HOST
208    if(HOST) {
209        int  nn;
210        adb_mutex_lock( &local_transports_lock );
211        for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
212            if (local_transports[nn] == t) {
213                local_transports[nn] = NULL;
214                break;
215            }
216        }
217        adb_mutex_unlock( &local_transports_lock );
218    }
219#endif
220}
221
222static void remote_close(atransport *t)
223{
224    adb_close(t->fd);
225}
226
227int init_socket_transport(atransport *t, int s, int  port)
228{
229    int  fail = 0;
230
231    t->kick = remote_kick;
232    t->close = remote_close;
233    t->read_from_remote = remote_read;
234    t->write_to_remote = remote_write;
235    t->sfd = s;
236    t->sync_token = 1;
237    t->connection_state = CS_OFFLINE;
238    t->type = kTransportLocal;
239
240#if ADB_HOST
241    if (HOST) {
242        adb_mutex_lock( &local_transports_lock );
243        {
244            int  index = (port - ADB_LOCAL_TRANSPORT_PORT)/2;
245
246            if (!(port & 1) || index < 0 || index >= ADB_LOCAL_TRANSPORT_MAX) {
247                D("bad local transport port number: %d\n", port);
248                fail = -1;
249            }
250            else if (local_transports[index] != NULL) {
251                D("local transport for port %d already registered (%p)?\n",
252                port, local_transports[index]);
253                fail = -1;
254            }
255            else
256                local_transports[index] = t;
257        }
258        adb_mutex_unlock( &local_transports_lock );
259    }
260#endif
261    return fail;
262}
263