blob: c528d1f8b850ef363ab5dbe42c3374251ca3144e [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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);
Mike Lockwoodff196702009-08-24 15:58:40 -0700125 register_socket_transport(fd, buf, port, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 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
Mike Lockwoodff196702009-08-24 15:58:40 -0700150static void *server_socket_thread(void * arg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151{
152 int serverfd, fd;
153 struct sockaddr addr;
154 socklen_t alen;
Mike Lockwoodff196702009-08-24 15:58:40 -0700155 int port = (int)arg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156
157 D("transport: server_socket_thread() starting\n");
158 serverfd = -1;
159 for(;;) {
160 if(serverfd == -1) {
Mike Lockwoodff196702009-08-24 15:58:40 -0700161 serverfd = socket_inaddr_any_server(port, SOCK_STREAM);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 if(serverfd < 0) {
163 D("server: cannot bind socket yet\n");
164 adb_sleep_ms(1000);
165 continue;
166 }
167 close_on_exec(serverfd);
168 }
169
170 alen = sizeof(addr);
Mike Lockwoodff196702009-08-24 15:58:40 -0700171 D("server: trying to get new connection from %d\n", port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 fd = adb_socket_accept(serverfd, &addr, &alen);
173 if(fd >= 0) {
174 D("server: new connection on fd %d\n", fd);
175 close_on_exec(fd);
176 disable_tcp_nagle(fd);
Mike Lockwoodff196702009-08-24 15:58:40 -0700177 register_socket_transport(fd, "host", port, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 }
179 }
180 D("transport: server_socket_thread() exiting\n");
181 return 0;
182}
183
Mike Lockwoodff196702009-08-24 15:58:40 -0700184void local_init(int port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185{
186 adb_thread_t thr;
187 void* (*func)(void *);
188
189 if(HOST) {
190 func = client_socket_thread;
191 } else {
192 func = server_socket_thread;
193 }
194
195 D("transport: local %s init\n", HOST ? "client" : "server");
196
Mike Lockwoodff196702009-08-24 15:58:40 -0700197 if(adb_thread_create(&thr, func, (void *)port)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 fatal_errno("cannot create local socket %s thread",
199 HOST ? "client" : "server");
200 }
201}
202
203static void remote_kick(atransport *t)
204{
205 int fd = t->sfd;
206 t->sfd = -1;
207 adb_close(fd);
208
209#if ADB_HOST
210 if(HOST) {
211 int nn;
212 adb_mutex_lock( &local_transports_lock );
213 for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
214 if (local_transports[nn] == t) {
215 local_transports[nn] = NULL;
216 break;
217 }
218 }
219 adb_mutex_unlock( &local_transports_lock );
220 }
221#endif
222}
223
224static void remote_close(atransport *t)
225{
226 adb_close(t->fd);
227}
228
Mike Lockwoodff196702009-08-24 15:58:40 -0700229int init_socket_transport(atransport *t, int s, int port, int local)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230{
231 int fail = 0;
232
233 t->kick = remote_kick;
234 t->close = remote_close;
235 t->read_from_remote = remote_read;
236 t->write_to_remote = remote_write;
237 t->sfd = s;
238 t->sync_token = 1;
239 t->connection_state = CS_OFFLINE;
240 t->type = kTransportLocal;
241
242#if ADB_HOST
Mike Lockwoodff196702009-08-24 15:58:40 -0700243 if (HOST && local) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 adb_mutex_lock( &local_transports_lock );
245 {
246 int index = (port - ADB_LOCAL_TRANSPORT_PORT)/2;
247
248 if (!(port & 1) || index < 0 || index >= ADB_LOCAL_TRANSPORT_MAX) {
249 D("bad local transport port number: %d\n", port);
250 fail = -1;
251 }
252 else if (local_transports[index] != NULL) {
253 D("local transport for port %d already registered (%p)?\n",
254 port, local_transports[index]);
255 fail = -1;
256 }
257 else
258 local_transports[index] = t;
259 }
260 adb_mutex_unlock( &local_transports_lock );
261 }
262#endif
263 return fail;
264}